From c5bf2ba0bd102856b31727bceab32543fe9ccac8 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Sun, 18 Nov 2012 13:59:43 -0800 Subject: [PATCH] * added check for v3 backward incompatibilities: ** {...}?=> gate semantic predicates --- CHANGES.txt | 1 + tool/src/org/antlr/v4/Tool.java | 4 ++-- tool/src/org/antlr/v4/parse/ANTLRLexer.g | 12 ++++++++++- .../org/antlr/v4/parse/ToolANTLRLexer.java | 20 +++++++++++++++++++ tool/src/org/antlr/v4/tool/ErrorType.java | 1 + 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 tool/src/org/antlr/v4/parse/ToolANTLRLexer.java diff --git a/CHANGES.txt b/CHANGES.txt index 8248bbd96..0b5aad921 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -10,6 +10,7 @@ November 18, 2012 ** labels in lexer rules ** tokens {A;B;} syntax ** tokens {A='C';} syntax +** {...}?=> gate semantic predicates November 17, 2012 diff --git a/tool/src/org/antlr/v4/Tool.java b/tool/src/org/antlr/v4/Tool.java index 82df03793..e39b22d1c 100644 --- a/tool/src/org/antlr/v4/Tool.java +++ b/tool/src/org/antlr/v4/Tool.java @@ -41,10 +41,10 @@ import org.antlr.v4.automata.LexerATNFactory; import org.antlr.v4.automata.ParserATNFactory; import org.antlr.v4.codegen.CodeGenPipeline; import org.antlr.v4.misc.Graph; -import org.antlr.v4.parse.ANTLRLexer; import org.antlr.v4.parse.ANTLRParser; import org.antlr.v4.parse.GrammarASTAdaptor; import org.antlr.v4.parse.GrammarTreeVisitor; +import org.antlr.v4.parse.ToolANTLRLexer; import org.antlr.v4.parse.ToolANTLRParser; import org.antlr.v4.parse.v3TreeGrammarException; import org.antlr.v4.runtime.misc.LogManager; @@ -600,7 +600,7 @@ public class Tool { public GrammarRootAST load(String fileName, CharStream in) { try { GrammarASTAdaptor adaptor = new GrammarASTAdaptor(in); - ANTLRLexer lexer = new ANTLRLexer(in); + ToolANTLRLexer lexer = new ToolANTLRLexer(in, this); CommonTokenStream tokens = new CommonTokenStream(lexer); lexer.tokens = tokens; ToolANTLRParser p = new ToolANTLRParser(tokens, this); diff --git a/tool/src/org/antlr/v4/parse/ANTLRLexer.g b/tool/src/org/antlr/v4/parse/ANTLRLexer.g index 170dab432..dd7b8d5b1 100644 --- a/tool/src/org/antlr/v4/parse/ANTLRLexer.g +++ b/tool/src/org/antlr/v4/parse/ANTLRLexer.g @@ -297,7 +297,17 @@ ARG_ACTION // the delimiting {} is no additional overhead. // ACTION - : NESTED_ACTION ('?' {$type = SEMPRED;} )? + : 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); + } + )? ; // ---------------- diff --git a/tool/src/org/antlr/v4/parse/ToolANTLRLexer.java b/tool/src/org/antlr/v4/parse/ToolANTLRLexer.java new file mode 100644 index 000000000..9f84ddc53 --- /dev/null +++ b/tool/src/org/antlr/v4/parse/ToolANTLRLexer.java @@ -0,0 +1,20 @@ +package org.antlr.v4.parse; + +import org.antlr.runtime.CharStream; +import org.antlr.runtime.Token; +import org.antlr.v4.Tool; +import org.antlr.v4.tool.ErrorType; + +public class ToolANTLRLexer extends ANTLRLexer { + public Tool tool; + + public ToolANTLRLexer(CharStream input, Tool tool) { + super(input); + this.tool = tool; + } + + @Override + public void grammarError(ErrorType etype, Token token, Object... args) { + tool.errMgr.grammarError(etype, getSourceName(), token, args); + } +} diff --git a/tool/src/org/antlr/v4/tool/ErrorType.java b/tool/src/org/antlr/v4/tool/ErrorType.java index 1adcb4d53..a42caf713 100644 --- a/tool/src/org/antlr/v4/tool/ErrorType.java +++ b/tool/src/org/antlr/v4/tool/ErrorType.java @@ -123,6 +123,7 @@ public enum ErrorType { "getText() to get the entire text matched for the rule", ErrorSeverity.WARNING_ONE_OFF), V3_TOKENS_SYNTAX(202, "'tokens {A; B;}' syntax is now 'tokens {A, B}' in ANTLR v4", ErrorSeverity.WARNING), V3_ASSIGN_IN_TOKENS(203, "assignments in tokens{} are not supported in ANTLR v4; use lexical rule ' : ;' instead", ErrorSeverity.ERROR_ONE_OFF), + V3_GATED_SEMPRED(204, "{...}?=> gate semantic predicates in v3 behave like normal predicates in ANTLR v4; use {...}? instead", ErrorSeverity.WARNING_ONE_OFF), // Dependency sorting errors