lib/antelope/ace/scanner/second.rb in antelope-0.1.8 vs lib/antelope/ace/scanner/second.rb in antelope-0.1.9
- old
+ new
@@ -43,11 +43,11 @@
# @return [Boolean] if it matched
# @see #scan_second_rule_label
# @see #scan_second_rule_body
# @see #error!
def scan_second_rule
- if @scanner.check(/([a-z._-]+):/)
+ if @scanner.check(/(#{IDENTIFIER})(\[#{IDENTIFIER}\])?:/)
scan_second_rule_label or error!
scan_second_rule_body
true
end
end
@@ -55,12 +55,12 @@
# Scans the label for a rule. It should contain only lower
# case letters and a colon.
#
# @return [Boolean] if it matched.
def scan_second_rule_label
- if @scanner.scan(/([a-z._-]+): ?/)
- tokens << [:label, @scanner[1]]
+ if @scanner.scan(/(#{IDENTIFIER})(?:\[(#{IDENTIFIER})\])?: ?/)
+ tokens << [:label, @scanner[1], @scanner[2]]
end
end
# The body can contain parts, ors, precs, or blocks (or
# whitespaces). Scans all of them, and then attempts to
@@ -86,12 +86,12 @@
# alphabetical characters that are not followed by a
# colon.
#
# @return [Boolean] if it matched.
def scan_second_rule_part
- if @scanner.scan(/([A-Za-z._-]+)(?!\:|[A-Za-z._-])/)
- tokens << [:part, @scanner[1]]
+ if @scanner.scan(/(#{IDENTIFIER})(?:\[(#{IDENTIFIER})\])?(?!\:|[A-Za-z._])/)
+ tokens << [:part, @scanner[1], @scanner[2]]
end
end
# Attempts to scan an "or". It's just a vertical bar.
#
@@ -105,11 +105,11 @@
# Attempts to scan a precedence definition. A precedence
# definition is "%prec " followed by a terminal or nonterminal.
#
# @return [Boolean] if it matched.
def scan_second_rule_prec
- if @scanner.scan(/%prec ([A-Za-z._-]+)/)
+ if @scanner.scan(/%prec (#{IDENTIFIER})/)
tokens << [:prec, @scanner[1]]
end
end
# Attempts to scan a block. This correctly balances brackets;
@@ -137,20 +137,35 @@
# @raise [SyntaxError] if it reaches the end before the final
# bracket is closed.
def _scan_block
brack = 1
body = "{"
+ scan_for = %r{
+ (
+ (?: " ( \\\\ | \\" | [^"] )* "? )
+ | (?: ' ( \\\\ | \\' | [^'] )* '? )
+ | (?: // .*? \n )
+ | (?: \# .*? \n )
+ | (?: /\* [\s\S]+? \*/ )
+ | (?: \} )
+ | (?: \{ )
+ )
+ }x
until brack.zero?
- if part = @scanner.scan_until(/(\}|\{)/)
+ if part = @scanner.scan_until(scan_for)
body << part
+
if @scanner[1] == "}"
brack -= 1
- else
+ elsif @scanner[1] == "{"
brack += 1
end
else
+ if @scanner.scan(/(.+)/m)
+ @line += @scanner[1].count("\n")
+ end
error!
end
end
body