Skip to content

Commit

Permalink
Merge pull request #13 from makenowjust/line-comment
Browse files Browse the repository at this point in the history
Support `//` style comment
  • Loading branch information
yui-knk authored May 16, 2023
2 parents fa95f7f + d569111 commit d448102
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
24 changes: 22 additions & 2 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ def lex_common(lines, tokens)
when ss.scan(/\/\*/)
# TODO: Need to keep comment?
line = lex_comment(ss, line, lines, "")
when ss.scan(/\/\//)
line = lex_line_comment(ss, line, "")
when ss.scan(/'(.)'/)
tokens << create_token(Token::Char, ss[0], line, ss.pos - column)
when ss.scan(/'\\(.)'/) # '\\', '\t'
Expand Down Expand Up @@ -276,6 +278,9 @@ def lex_user_code(ss, line, column, lines)
when ss.scan(/\/\*/)
str << ss[0]
line = lex_comment(ss, line, lines, str)
when ss.scan(/\/\//)
str << ss[0]
line = lex_line_comment(ss, line, str)
else
# noop, just consume char
str << ss.getch
Expand Down Expand Up @@ -314,8 +319,6 @@ def lex_string(ss, terminator, line, lines)
raise "Parse error (quote mismatch): #{ss.string.split("\n")[l]} \"#{ss.string[ss.pos]}\" (#{line}: #{ss.pos})"
end

# TODO: Need to handle // style comment
#
# /* */ style comment
def lex_comment(ss, line, lines, str)
while !ss.eos? do
Expand All @@ -337,6 +340,23 @@ def lex_comment(ss, line, lines, str)
raise "Parse error (comment mismatch): #{ss.string.split("\n")[l]} \"#{ss.string[ss.pos]}\" (#{line}: #{ss.pos})"
end

# // style comment
def lex_line_comment(ss, line, str)
while !ss.eos? do
case
when ss.scan(/\n/)
return line + 1
else
str << ss.getch
next
end

str << ss[0]
end

line # Reach to end of input
end

def lex_grammar_rules_tokens
lex_common(@grammar_rules, @grammar_rules_tokens)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/common/basic.y
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
%token tEQ "="
%token tEQEQ "=="

%type <i> class /* comment for class */
%type <i> class /* comment for class */ // line-comment for class

%nonassoc tEQEQ
%left tPLUS tMINUS '>'
Expand Down
22 changes: 11 additions & 11 deletions spec/lrama/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
%token tEQ "="
%token tEQEQ "=="
%type <i> class /* comment for class */
%type <i> class /* comment for class */ // line-comment for class
%nonassoc tEQEQ
%left tPLUS tMINUS '>'
Expand Down Expand Up @@ -387,11 +387,11 @@ class : keyword_class tSTRING keyword_end %prec tPLUS
stmt: tBODY
{
int i = 1; /* @ */
int j = 1; /* $ */
int k = 1; /* @1 */
int l = 1; /* $$ */
int m = 1; /* $2 */
int i = 1; /* @ */ // @
int j = 1; /* $ */ // $
int k = 1; /* @1 */ // @1
int l = 1; /* $$ */ // $$
int m = 1; /* $2 */ // $2
}
;
%%
Expand All @@ -403,11 +403,11 @@ class : keyword_class tSTRING keyword_end %prec tPLUS

expected = <<-CODE.chomp
{
int i = 1; /* @ */
int j = 1; /* $ */
int k = 1; /* @1 */
int l = 1; /* $$ */
int m = 1; /* $2 */
int i = 1; /* @ */ // @
int j = 1; /* $ */ // $
int k = 1; /* @1 */ // @1
int l = 1; /* $$ */ // $$
int m = 1; /* $2 */ // $2
}
CODE

Expand Down

0 comments on commit d448102

Please sign in to comment.