Skip to content

Commit

Permalink
Update parser
Browse files Browse the repository at this point in the history
  • Loading branch information
junk0612 committed Oct 4, 2023
1 parent 08bc098 commit 211ce6b
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 99 deletions.
85 changes: 85 additions & 0 deletions lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,88 @@ def nterms
end

def extract_references
@printers.each do |printer|
scanner = StringScanner.new(printer.code.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 (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
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
end

printer.code.token_code.references = references
build_references(printer.code.token_code)
end

@error_tokens.each do |error_token|
scanner = StringScanner.new(error_token.code.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 (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
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
end

error_token.code.token_code.references = references
build_references(error_token.code.token_code)
end

@_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
Expand Down Expand Up @@ -342,6 +424,9 @@ def extract_references
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]

when scanner.scan(/\/\*/)
scanner.scan_until(/\*\//)
else
scanner.getch
end
Expand Down
Loading

0 comments on commit 211ce6b

Please sign in to comment.