From c13cc882626a245a7a5a97af036f95e40e50a2c7 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 13 Apr 2014 16:08:35 +0200 Subject: [PATCH] JavaScript example: Remove reserved word detection Reserved word detection as it was implemented in the JavaScript example grammar had two big downsides: 1. It required changes in ordering of choices in some rules in order not to trigger the detection prematurely. One of the changes was already implemented (in the |Statement| rule, see the diff), but apparently more were needed (the grammar didn't parse inputs like |true| or |function f() {}|). And I'm not 100% sure that would be the end of it (maybe deeper structural changes would be needed). 2. It made error messages confusing. Consider the following example: var a = @; Instead of reporting: Expected ... but "@" found. the generated parser reported: Reserved word "var" can't be used as an identifier. This was because the parser parsed the statement first as |VariableStatement| and when this failed, it tried to parse it as |ExpressionStatement|, triggering the reserved word detection. Because of these, I decided to remove reserved word detection from the JavaScript example grammar. --- examples/javascript.pegjs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/examples/javascript.pegjs b/examples/javascript.pegjs index 74b02e9..572cb33 100644 --- a/examples/javascript.pegjs +++ b/examples/javascript.pegjs @@ -146,11 +146,6 @@ SingleLineComment Identifier = !ReservedWord name:IdentifierName { return name; } - / name:IdentifierName { - error( - "Reserved word \"" + name.name + "\" can't be used as an identifier." - ); - } IdentifierName "identifier" = first:IdentifierStart rest:IdentifierPart* { @@ -993,23 +988,18 @@ Statement = Block / VariableStatement / EmptyStatement + / ExpressionStatement / IfStatement / IterationStatement / ContinueStatement / BreakStatement / ReturnStatement / WithStatement + / LabelledStatement / SwitchStatement / ThrowStatement / TryStatement / DebuggerStatement - /* - * The following two statements begin with identifiers. They must be at the - * very end of the list to prevent triggering reserved word detection in the - * |Identifier| rule by keywords the statemets above begin with (e.g. |if|). - */ - / ExpressionStatement - / LabelledStatement Block = "{" __ body:(StatementList __)? "}" {