I am building a small programming language and I am using nearley.js for parsing. I am defining the grammar, and the grammar syntax is working fine except in one case, which is the newline token.
This is the grammar:
statement -> var_assign {% id %}
| fun_call {% id %}
| function_def {% id %}
| conditional {% id %}
| loop {% id %}
statements
-> statement %NL
{%
(data) => {
return [data[0]];
}
%}
| statements %NL statement %NL
{%
(data) => {
return [...data[0], data[2]];
}
This is the error:
Error while parsing Error: invalid syntax at line 1 col 19:
1 @name = "mohammad"
^
2 call print(name)
Unexpected input (lexer error).
I did not expect any more input. Here is the state of my parse table:
expression → %string ●
var_assign → %var_declaration %identifier _ "=" _ expression ●
statement → var_assign ●
I am only facing the error in case of newline token. Everything else is working fine.
i just found out that the error is not the caused by the newline token instead the parser is not knowing when is the end of the statement but still enable to solve this