Skip to content

Commit

Permalink
BNF grammar now ignores new lines
Browse files Browse the repository at this point in the history
Fixed BNF strings and comments
Fixed parser crashing when ending on an ignored token
  • Loading branch information
james-pre committed Dec 14, 2024
1 parent 7e180d7 commit cfb43c1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
3 changes: 2 additions & 1 deletion test/asm.bnf → grammars/asm.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ comment = "#.*";

##ignore whitespace line_terminator comment

operand = register | immediate | number;
operand = register
| immediate | number;

operand_list = operand, {",", operand};

Expand Down
12 changes: 6 additions & 6 deletions src/bnf.bnf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

string = '"(?:\\.|[^"\\])*"|\'(?:\\.|[^\'\\])*\'';
string = '("(?:\\.|[^"\\])*"|\'(?:\\.|[^\'\\])*\')';
identifier = '[a-zA-Z_]\w*';
whitespace = '[ \t]+';
line_terminator = "[\n;]+";
comment = "#[^\n]*";
line_terminator = "\n+";
comment = "#(?!#)([^\n]*)";
directive = "##\w+ [^\n]*";

# Simplifies internals
Expand All @@ -16,7 +16,7 @@ right_bracket = '\]';
left_brace = '\{';
right_brace = '\}';

##ignore whitespace comment
##ignore whitespace comment line_terminator

parenthesized = left_param, expression, right_param;
optional = left_bracket, expression, right_bracket;
Expand All @@ -32,6 +32,6 @@ expression = term, {',', term};

rule = identifier, '=', expression, ';';

rule_list = (rule | comment | directive), {line_terminator, rule | comment | directive};
rule_list = (rule | directive), {rule | directive};

##root rule_list
##root rule_list
11 changes: 4 additions & 7 deletions src/bnf.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,14 @@
"type": "oneof",
"pattern": [
{ "kind": "rule", "type": "required" },
{ "kind": "comment", "type": "required" },
{ "kind": "directive", "type": "required" }
]
},
{
"name": "rule_list#1",
"type": "oneof",
"pattern": [
{ "kind": "line_terminator", "type": "required" },
{ "kind": "rule", "type": "required" },
{ "kind": "comment", "type": "required" },
{ "kind": "directive", "type": "required" }
]
},
Expand All @@ -116,11 +113,11 @@
}
],
"literals": [
{ "name": "string", "pattern": "\"(?:\\\\.|[^\"\\\\])*\"|'(?:\\\\.|[^'\\\\])*'" },
{ "name": "string", "pattern": "(\"(?:\\\\.|[^\"\\\\])*\"|'(?:\\\\.|[^'\\\\])*')" },
{ "name": "identifier", "pattern": "[a-zA-Z_]\\w*" },
{ "name": "whitespace", "pattern": "[ \\t]+" },
{ "name": "line_terminator", "pattern": "[\\n;]+" },
{ "name": "comment", "pattern": "#[^\\n]*" },
{ "name": "line_terminator", "pattern": "\\n+" },
{ "name": "comment", "pattern": "#(?!#)([^\\n]*)" },
{ "name": "directive", "pattern": "##\\w+ [^\\n]*" },
{ "name": "pipe", "pattern": "\\|" },
{ "name": "left_param", "pattern": "\\(" },
Expand All @@ -134,5 +131,5 @@
{ "name": ";", "pattern": ";" }
],
"rootNode": "rule_list",
"ignoreLiterals": ["whitespace", "comment"]
"ignoreLiterals": ["whitespace", "comment", "line_terminator"]
}
1 change: 1 addition & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export function parse(options: ParseOptions): Node[] {
while (position < tokens.length) {
const node = parseNode(options.rootNode);
if (!node) {
if (position >= tokens.length && options.ignoreLiterals.includes(tokens.at(-1)!.kind)) break;
const token = tokens[dirtyPosition || position];
throw new Error(`Unexpected token "${token.text}" at line ${token.line}, column ${token.column}`);
}
Expand Down

0 comments on commit cfb43c1

Please sign in to comment.