From aace1ce399e74e964c57f58522c4a961edd4fa56 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 18 Nov 2012 18:00:19 -0600 Subject: [PATCH 1/6] V3 semantic predicate gate (=>) can only appear after a predicate {...}?, not an action {...} --- tool/src/org/antlr/v4/parse/ANTLRLexer.g | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tool/src/org/antlr/v4/parse/ANTLRLexer.g b/tool/src/org/antlr/v4/parse/ANTLRLexer.g index 6e01ae71f..7c89ae2c1 100644 --- a/tool/src/org/antlr/v4/parse/ANTLRLexer.g +++ b/tool/src/org/antlr/v4/parse/ANTLRLexer.g @@ -298,15 +298,16 @@ ARG_ACTION // ACTION : NESTED_ACTION - ('?' {$type = SEMPRED;} )? - ( '=>' // v3 gated sempred - { - Token t = new CommonToken(input, state.type, state.channel, state.tokenStartCharIndex, getCharIndex()-1); - t.setLine(state.tokenStartLine); - t.setText(state.text); - t.setCharPositionInLine(state.tokenStartCharPositionInLine); - grammarError(ErrorType.V3_GATED_SEMPRED, t); - } + ( '?' {$type = SEMPRED;} + ( '=>' // v3 gated sempred + { + Token t = new CommonToken(input, state.type, state.channel, state.tokenStartCharIndex, getCharIndex()-1); + t.setLine(state.tokenStartLine); + t.setText(state.text); + t.setCharPositionInLine(state.tokenStartCharPositionInLine); + grammarError(ErrorType.V3_GATED_SEMPRED, t); + } + )? )? ; From 178df68183d55d86d11b6c4c5daac7c70d00b605 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 18 Nov 2012 18:01:07 -0600 Subject: [PATCH 2/6] Whitespace can appear between {...}? and => in v3 gated semantic predicates --- tool/src/org/antlr/v4/parse/ANTLRLexer.g | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tool/src/org/antlr/v4/parse/ANTLRLexer.g b/tool/src/org/antlr/v4/parse/ANTLRLexer.g index 7c89ae2c1..446d5aac6 100644 --- a/tool/src/org/antlr/v4/parse/ANTLRLexer.g +++ b/tool/src/org/antlr/v4/parse/ANTLRLexer.g @@ -299,7 +299,7 @@ ARG_ACTION ACTION : NESTED_ACTION ( '?' {$type = SEMPRED;} - ( '=>' // v3 gated sempred + ( (WSNLCHARS* '=>') => WSNLCHARS* '=>' // v3 gated sempred { Token t = new CommonToken(input, state.type, state.channel, state.tokenStartCharIndex, getCharIndex()-1); t.setLine(state.tokenStartLine); From 16ace4b429574f160ca9e515818d1d842ad39a3f Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 18 Nov 2012 18:02:10 -0600 Subject: [PATCH 3/6] Fix token definitions: tokens in a tokens{...} block now have type TOKEN_REF (was ID before commit 38214f6312) --- tool/src/org/antlr/v4/semantics/SemanticPipeline.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tool/src/org/antlr/v4/semantics/SemanticPipeline.java b/tool/src/org/antlr/v4/semantics/SemanticPipeline.java index da87b3b64..550b33a4c 100644 --- a/tool/src/org/antlr/v4/semantics/SemanticPipeline.java +++ b/tool/src/org/antlr/v4/semantics/SemanticPipeline.java @@ -151,7 +151,7 @@ public class SemanticPipeline { void assignLexerTokenTypes(Grammar g, List tokensDefs) { Grammar G = g.getOutermostGrammar(); // put in root, even if imported for (GrammarAST def : tokensDefs) { - if ( def.getType()== ANTLRParser.ID ) G.defineTokenName(def.getText()); + if ( def.getType()== ANTLRParser.TOKEN_REF ) G.defineTokenName(def.getText()); } /* Define token types for nonfragment rules which do not include a 'type(...)' From d6e1ca2b61bb8b72b3f7cc52e68701287cb0f175 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 18 Nov 2012 18:13:27 -0600 Subject: [PATCH 4/6] Fix NPE when the grammar has no rules --- tool/src/org/antlr/v4/Tool.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tool/src/org/antlr/v4/Tool.java b/tool/src/org/antlr/v4/Tool.java index e39b22d1c..16780393c 100644 --- a/tool/src/org/antlr/v4/Tool.java +++ b/tool/src/org/antlr/v4/Tool.java @@ -433,10 +433,16 @@ public class Tool { public boolean checkForRuleIssues(final Grammar g) { // check for redefined rules GrammarAST RULES = (GrammarAST)g.ast.getFirstChildWithType(ANTLRParser.RULES); - List rules = (List)RULES.getChildren(); + + List rules = (List)RULES.getChildren(); + if (rules == null) { + rules = Collections.emptyList(); + } + final Map ruleToAST = new HashMap(); - for (RuleAST r : rules) { - GrammarAST ID = (GrammarAST)r.getChild(0); + for (Object r : rules) { + RuleAST ruleAST = (RuleAST)r; + GrammarAST ID = (GrammarAST)ruleAST.getChild(0); String ruleName = ID.getText(); RuleAST prev = ruleToAST.get(ruleName); if ( prev !=null ) { @@ -446,10 +452,10 @@ public class Tool { ID.getToken(), ruleName, prevChild.getToken().getLine()); - r.dead = true; + ruleAST.dead = true; continue; } - ruleToAST.put(ruleName, r); + ruleToAST.put(ruleName, ruleAST); } // check for undefined rules From e610f2699871112915e9ce1d4af64c5a99906bea Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 18 Nov 2012 18:13:49 -0600 Subject: [PATCH 5/6] Fix support for the special token reference `EOF` --- tool/src/org/antlr/v4/Tool.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tool/src/org/antlr/v4/Tool.java b/tool/src/org/antlr/v4/Tool.java index 16780393c..52f7d7b19 100644 --- a/tool/src/org/antlr/v4/Tool.java +++ b/tool/src/org/antlr/v4/Tool.java @@ -463,6 +463,11 @@ public class Tool { public boolean undefined = false; @Override public void tokenRef(TerminalAST ref) { + if ("EOF".equals(ref.getText())) { + // this is a special predefined reference + return; + } + if ( g.isLexer() ) ruleRef(ref, null); } From 7fe48dfebfce30d087181aa90a3cc8bc6d56db43 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 18 Nov 2012 18:17:21 -0600 Subject: [PATCH 6/6] Remove erroneous lexer command placement error message for rules with more than 1 command --- tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java b/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java index f4793a63b..0ef42fcd8 100644 --- a/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java +++ b/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java @@ -347,8 +347,7 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { boolean outerMostAlt = blk.parent.getType() == RULE; Tree rule = tree.getAncestor(RULE); String fileName = tree.getToken().getInputStream().getSourceName(); - if ( !outerMostAlt || tree.getChildIndex() != alt.getChildCount()-1 || - blk.getChildCount()>1 ) + if ( !outerMostAlt || blk.getChildCount()>1 ) { ErrorType e = ErrorType.LEXER_COMMAND_PLACEMENT_ISSUE; if ( tree.getType() == ACTION ) e = ErrorType.LEXER_ACTION_PLACEMENT_ISSUE;