Skip to content

Commit

Permalink
Merge pull request #95 from yui-knk/fix_named_ref_without_bracket
Browse files Browse the repository at this point in the history
Fix to not include '.' and '-' into identifier without brackets
  • Loading branch information
yui-knk authored Sep 11, 2023
2 parents d6e607f + 7e129d1 commit 28696f1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
16 changes: 14 additions & 2 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,33 @@ def lex_user_code(ss, line, column, lines)
string, line = lex_string(ss, "'", line, lines)
str << string
next

# $ references
# It need to wrap an identifier with brackets to use ".-" for identifiers
when ss.scan(/\$(<[a-zA-Z0-9_]+>)?\$/) # $$, $<long>$
tag = ss[1] ? create_token(Token::Tag, ss[1], line, str.length) : nil
references << [:dollar, "$", tag, str.length, str.length + ss[0].length - 1]
when ss.scan(/\$(<[a-zA-Z0-9_]+>)?(\d+)/) # $1, $2, $<long>1
tag = ss[1] ? create_token(Token::Tag, ss[1], line, str.length) : nil
references << [:dollar, Integer(ss[2]), tag, str.length, str.length + ss[0].length - 1]
when ss.scan(/\$(<[a-zA-Z0-9_]+>)?([a-zA-Z_.][-a-zA-Z0-9_.]*)/) # $foo, $expr, $<long>program
when ss.scan(/\$(<[a-zA-Z0-9_]+>)?([a-zA-Z_][a-zA-Z0-9_]*)/) # $foo, $expr, $<long>program (named reference without brackets)
tag = ss[1] ? create_token(Token::Tag, ss[1], line, str.length) : nil
references << [:dollar, ss[2], tag, str.length, str.length + ss[0].length - 1]
when ss.scan(/\$(<[a-zA-Z0-9_]+>)?\[([a-zA-Z_.][-a-zA-Z0-9_.]*)\]/) # $expr.right, $expr-right, $<long>program (named reference with brackets)
tag = ss[1] ? create_token(Token::Tag, ss[1], line, str.length) : nil
references << [:dollar, ss[2], tag, str.length, str.length + ss[0].length - 1]

# @ references
# It need to wrap an identifier with brackets to use ".-" for identifiers
when ss.scan(/@\$/) # @$
references << [:at, "$", nil, str.length, str.length + ss[0].length - 1]
when ss.scan(/@(\d+)/) # @1
references << [:at, Integer(ss[1]), nil, str.length, str.length + ss[0].length - 1]
when ss.scan(/@([a-zA-Z_.][-a-zA-Z0-9_.]*)/) # @foo, @expr
when ss.scan(/@([a-zA-Z][a-zA-Z0-9_]*)/) # @foo, @expr (named reference without brackets)
references << [:at, ss[1], nil, str.length, str.length + ss[0].length - 1]
when ss.scan(/@\[([a-zA-Z_.][-a-zA-Z0-9_.]*)\]/) # @expr.right, @expr-right (named reference with brackets)
references << [:at, ss[1], nil, str.length, str.length + ss[0].length - 1]

when ss.scan(/{/)
brace_count += 1
when ss.scan(/}/)
Expand Down
1 change: 1 addition & 0 deletions lib/lrama/lexer/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def numberize_references(lhs, rhs)
if lhs.referred_by?(ref_name)
'$'
else
# TODO: Better error message for wrong reference
rhs.find_index {|token| token.referred_by?(ref_name) } + 1
end
[ref[0], value, ref[2], ref[3], ref[4]]
Expand Down
16 changes: 8 additions & 8 deletions spec/lrama/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def test_rules(rules, input, expected, command_args: [], debug: false)
]
cases = generate_lexer_body(input)

test_grammar(<<~Grammar, "expr[left]: 0.0-0.1. expr[right]: 1.0-1.1. line: 0.0-2.1. => 3")
test_grammar(<<~Grammar, "expr[ex-left] (0): 0.0-0.1. expr[ex.right] (1): 1.0-1.1. line (0): 0.0-2.1. => 3")
%{
#include <stdio.h>
Expand Down Expand Up @@ -264,23 +264,23 @@ def test_rules(rules, input, expected, command_args: [], debug: false)
{
(void)yynerrs;
printf("line: ");
printf("line (%d): ", @expr.first_line);
print_location(&@expr);
printf("=> %d", $expr);
}
;
expr[result]: NUM
| expr[left] expr[right] '+'
| expr[ex-left] expr[ex.right] '+'
{
printf("expr[left]: ");
print_location(&@left);
printf("expr[ex-left] (%d): ", @[ex-left].first_line);
print_location(&@[ex-left]);
printf("expr[right]: ");
print_location(&@right);
printf("expr[ex.right] (%d): ", @[ex.right].first_line);
print_location(&@[ex.right]);
$result = $left + $right;
$result = $[ex-left] + $[ex.right];
}
;
Expand Down

0 comments on commit 28696f1

Please sign in to comment.