Skip to content

Commit

Permalink
Apply named references
Browse files Browse the repository at this point in the history
  • Loading branch information
junk0612 committed Sep 26, 2023
1 parent 3972ed8 commit 8b2526e
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 241 deletions.
2 changes: 0 additions & 2 deletions lib/lrama/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ def run(argv)
text = options.y.read
options.y.close if options.y != STDIN
grammar = Lrama::NewParser.new(text).parse
pp Lrama::Parser.new(text).parse
pp grammar
states = Lrama::States.new(grammar, warning, trace_state: (options.trace_opts[:automaton] || options.trace_opts[:closure]))
states.compute
context = Lrama::Context.new(states)
Expand Down
18 changes: 15 additions & 3 deletions lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,27 +309,39 @@ def nterms
def extract_references
@_rules.each do |lhs, rhs, _|
rhs.each_with_index do |token, index|
next if token.class == Lrama::Grammar::Symbol || token.type != Lrama::Lexer::Token::User_code
next if token.class == Lrama::Grammar::Symbol || token.type != Lrama::Lexer::Token::User_code

scanner = StringScanner.new(token.s_value)
references = []

while !scanner.eos? do
start = scanner.pos
case
# $ references
# It need to wrap an identifier with brackets to use ".-" for identifiers
when scanner.scan(/\$(<[a-zA-Z0-9_]+>)?\$/) # $$, $<long>$
tag = scanner[1] ? Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: scanner[1]) : nil
references << [:dollar, "$", tag, start, scanner.pos - 1]
when scanner.scan(/\$(<[a-zA-Z0-9_]+>)?(\d+)/) # $1, $2, $<long>1
tag = scanner[1] ? Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: scanner[1]) : nil
references << [:dollar, Integer(scanner[2]), tag, start, scanner.pos - 1]
when scanner.scan(/\$(<[a-zA-Z0-9_]+>)?([a-zA-Z_.][-a-zA-Z0-9_.]*)/) # $foo, $expr, $<long>program
when scanner.scan(/\$(<[a-zA-Z0-9_]+>)?([a-zA-Z_][a-zA-Z0-9_]*)/) # $foo, $expr, $<long>program (named reference without brackets)
tag = scanner[1] ? Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: scanner[1]) : nil
references << [:dollar, scanner[2], tag, start, scanner.pos - 1]
when scanner.scan(/\$(<[a-zA-Z0-9_]+>)?\[([a-zA-Z_.][-a-zA-Z0-9_.]*)\]/) # $expr.right, $expr-right, $<long>program (named reference with brackets)
tag = scanner[1] ? Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: scanner[1]) : nil
references << [:dollar, scanner[2], tag, start, scanner.pos - 1]

# @ references
# It need to wrap an identifier with brackets to use ".-" for identifiers
when scanner.scan(/@\$/) # @$
references << [:at, "$", nil, start, scanner.pos - 1]
when scanner.scan(/@(\d)+/) # @1
when scanner.scan(/@(\d+)/) # @1
references << [:at, Integer(scanner[1]), nil, start, scanner.pos - 1]
when scanner.scan(/@([a-zA-Z][a-zA-Z0-9_]*)/) # @foo, @expr (named reference without brackets)
references << [:at, scanner[1], nil, start, scanner.pos - 1]
when scanner.scan(/@\[([a-zA-Z_.][-a-zA-Z0-9_.]*)\]/) # @expr.right, @expr-right (named reference with brackets)
references << [:at, scanner[1], nil, start, scanner.pos - 1]
else
scanner.getch
end
Expand Down
5 changes: 4 additions & 1 deletion lib/lrama/new_lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def initialize(text)
def next_token
case @status
when :initial
pp @line
lex_token
when :c_declaration
lex_c_code
Expand Down Expand Up @@ -51,6 +50,10 @@ def lex_token
return [@scanner.matched, @scanner.matched]
when @scanner.scan(/}/)
return [@scanner.matched, @scanner.matched]
when @scanner.scan(/\[/)
return [@scanner.matched, @scanner.matched]
when @scanner.scan(/\]/)
return [@scanner.matched, @scanner.matched]
when @scanner.scan(/:/)
return [@scanner.matched, @scanner.matched]
when @scanner.scan(/\|/)
Expand Down
Loading

0 comments on commit 8b2526e

Please sign in to comment.