Here is an example of the behavior that I would like to have for a simplified grammar:
# 1. One number.
100
==> Program(Number)
# 2. Two numbers.
100
200
==> Program(Number, Number)
# 3. Comment after space.
100 /Comment
200
==> Program(Number, LineComment, Number)
# 4. Comment after newline.
100
/Comment
200
==> Program(Number, LineComment, Number)
# 5. Comment after newline 2
100/200
/Comment
==> Program(Number, Adverb, Number, LineComment)
# 6. Comment at the start of the first line.
/Comment
200
==> Program(LineComment, Number)
Here is the grammar.
@top Program { sentences }
@skip { LineComment }
sentences { (sentence newline)* sentence }
sentence { Number | Number Adverb Number }
@tokens {
@precedence { LineComment, newline }
Adverb { "/" }
Number { @digit+ }
LineComment { " "+ "/" ![\n]* | "\n/" ![\n]* }
space { " "+ }
newline { "\n" }
}
Test case 6 doesn’t parse. I understand why, but I’m not sure how to fix it. What I would like to do is interpret /
as the start of a comment if it’s at the beginning of a line or if it follows a space. I can match the beginnings of other lines by detecting a \n
.