From 768bfc0cf2e705cd0eeaa0ab11bcd18f453442a0 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 8 Mar 2012 13:24:08 -0600 Subject: [PATCH 01/58] Add ProxyErrorListener to allow dispatching error reporting to multiple listeners without manually iterating over the list of listeners. --- .../Java/src/org/antlr/v4/runtime/Lexer.java | 7 +- .../Java/src/org/antlr/v4/runtime/Parser.java | 7 +- .../antlr/v4/runtime/ProxyErrorListener.java | 115 ++++++++++++++++++ .../src/org/antlr/v4/runtime/Recognizer.java | 4 + 4 files changed, 125 insertions(+), 8 deletions(-) create mode 100644 runtime/Java/src/org/antlr/v4/runtime/ProxyErrorListener.java diff --git a/runtime/Java/src/org/antlr/v4/runtime/Lexer.java b/runtime/Java/src/org/antlr/v4/runtime/Lexer.java index 22bfcb405..3bca85c83 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Lexer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Lexer.java @@ -309,10 +309,9 @@ public abstract class Lexer extends Recognizer public void notifyListeners(LexerNoViableAltException e) { String msg = "token recognition error at: '"+ _input.substring(_tokenStartCharIndex, _input.index())+"'"; - List> listeners = getErrorListeners(); - for (ANTLRErrorListener listener : listeners) { - listener.error(this, null, _tokenStartLine, _tokenStartCharPositionInLine, msg, e); - } + + ANTLRErrorListener listener = getErrorListenerDispatch(); + listener.error(this, null, _tokenStartLine, _tokenStartCharPositionInLine, msg, e); } public String getCharErrorDisplay(int c) { diff --git a/runtime/Java/src/org/antlr/v4/runtime/Parser.java b/runtime/Java/src/org/antlr/v4/runtime/Parser.java index fee05b775..b967f204f 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Parser.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Parser.java @@ -280,10 +280,9 @@ public abstract class Parser extends Recognizer line = ((Token) offendingToken).getLine(); charPositionInLine = ((Token) offendingToken).getCharPositionInLine(); } - List> listeners = getErrorListeners(); - for (ANTLRErrorListener listener : listeners) { - listener.error(this, offendingToken, line, charPositionInLine, msg, e); - } + + ANTLRErrorListener listener = getErrorListenerDispatch(); + listener.error(this, offendingToken, line, charPositionInLine, msg, e); } /** Consume the current symbol and return it. E.g., given the following diff --git a/runtime/Java/src/org/antlr/v4/runtime/ProxyErrorListener.java b/runtime/Java/src/org/antlr/v4/runtime/ProxyErrorListener.java new file mode 100644 index 000000000..fa08c334c --- /dev/null +++ b/runtime/Java/src/org/antlr/v4/runtime/ProxyErrorListener.java @@ -0,0 +1,115 @@ +/* + [The "BSD license"] + Copyright (c) 2012 Terence Parr + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.antlr.v4.runtime; + +import java.util.Collection; +import org.antlr.v4.runtime.atn.ATNConfigSet; +import org.antlr.v4.runtime.atn.DecisionState; +import org.antlr.v4.runtime.atn.SemanticContext; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.IntervalSet; + +/** + * + * @author Sam Harwell + */ +public class ProxyErrorListener implements ANTLRErrorListener { + private final Collection> delegates; + + public ProxyErrorListener(Collection> delegates) { + this.delegates = delegates; + } + + @Override + public void error(Recognizer recognizer, + T offendingSymbol, + int line, + int charPositionInLine, + String msg, + RecognitionException e) + { + for (ANTLRErrorListener listener : delegates) { + listener.error(recognizer, offendingSymbol, line, charPositionInLine, msg, e); + } + } + + @Override + public void reportAmbiguity(Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + IntervalSet ambigAlts, + ATNConfigSet configs) + { + for (ANTLRErrorListener listener : delegates) { + listener.reportAmbiguity(recognizer, dfa, startIndex, stopIndex, ambigAlts, configs); + } + } + + @Override + public void reportAttemptingFullContext(Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + ATNConfigSet configs) + { + for (ANTLRErrorListener listener : delegates) { + listener.reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, configs); + } + } + + @Override + public void reportContextSensitivity(Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + ATNConfigSet configs) + { + for (ANTLRErrorListener listener : delegates) { + listener.reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, configs); + } + } + + @Override + public void reportInsufficientPredicates(Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + IntervalSet ambigAlts, + DecisionState decState, + SemanticContext[] altToPred, + ATNConfigSet configs, + boolean fullContextParse) + { + for (ANTLRErrorListener listener : delegates) { + listener.reportInsufficientPredicates(recognizer, dfa, startIndex, stopIndex, ambigAlts, decState, altToPred, configs, fullContextParse); + } + } + +} diff --git a/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java b/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java index 331823698..218da3111 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java @@ -120,6 +120,10 @@ public abstract class Recognizer { return new ArrayList>(_listeners); } + public ANTLRErrorListener getErrorListenerDispatch() { + return new ProxyErrorListener(getErrorListeners()); + } + // subclass needs to override these if there are sempreds or actions // that the ATN interp needs to execute public boolean sempred(@Nullable RuleContext _localctx, int ruleIndex, int actionIndex) { From 3f1f76df7d44332c637e5a92f27933e9c9f3e5ac Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 8 Mar 2012 13:36:46 -0600 Subject: [PATCH 02/58] Move reportAmbiguity, reportContextSensitivity, reportAttemptingFullContext, reportInsufficientPredicates from ANTLRErrorStrategy to ANTLRErrorListener. Add BaseErrorListener to allow implementing ANTLRErrorListener without implementing every method (e.g. ConsoleErrorListener). DiagnosticErrorStrategy is now DiagnosticErrorListener, updated tests. --- .../antlr/v4/runtime/ANTLRErrorListener.java | 44 +++++++++ .../antlr/v4/runtime/ANTLRErrorStrategy.java | 43 --------- .../antlr/v4/runtime/BaseErrorListener.java | 93 +++++++++++++++++++ .../v4/runtime/ConsoleErrorListener.java | 2 +- .../v4/runtime/DefaultErrorStrategy.java | 33 ------- ...tegy.java => DiagnosticErrorListener.java} | 2 +- .../v4/runtime/atn/ParserATNSimulator.java | 8 +- .../org/antlr/v4/runtime/misc/TestRig.java | 2 +- tool/playground/TestR.java | 4 +- tool/test/org/antlr/v4/test/BaseTest.java | 2 +- .../org/antlr/v4/test/TestPerformance.java | 2 +- 11 files changed, 148 insertions(+), 87 deletions(-) create mode 100644 runtime/Java/src/org/antlr/v4/runtime/BaseErrorListener.java rename runtime/Java/src/org/antlr/v4/runtime/{DiagnosticErrorStrategy.java => DiagnosticErrorListener.java} (98%) diff --git a/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorListener.java b/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorListener.java index fed6ba2f8..deb386a72 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorListener.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorListener.java @@ -29,6 +29,12 @@ package org.antlr.v4.runtime; +import org.antlr.v4.runtime.atn.ATNConfigSet; +import org.antlr.v4.runtime.atn.DecisionState; +import org.antlr.v4.runtime.atn.SemanticContext; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.IntervalSet; +import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.misc.Nullable; /** How to emit recognition errors */ @@ -72,4 +78,42 @@ public interface ANTLRErrorListener { int charPositionInLine, String msg, @Nullable RecognitionException e); + + /** Called when the parser detects a true ambiguity: an input sequence can be matched + * literally by two or more pass through the grammar. ANTLR resolves the ambiguity in + * favor of the alternative appearing first in the grammar. The start and stop index are + * zero-based absolute indices into the token stream. ambigAlts is a set of alternative numbers + * that can match the input sequence. This method is only called when we are parsing with + * full context. + */ + void reportAmbiguity(@NotNull Parser recognizer, + DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, + @NotNull ATNConfigSet configs); + + void reportAttemptingFullContext(@NotNull Parser recognizer, + @NotNull DFA dfa, + int startIndex, int stopIndex, + @NotNull ATNConfigSet configs); + + /** Called by the parser when it find a conflict that is resolved by retrying the parse + * with full context. This is not a warning; it simply notifies you that your grammar + * is more complicated than Strong LL can handle. The parser moved up to full context + * parsing for that input sequence. + */ + void reportContextSensitivity(@NotNull Parser recognizer, + @NotNull DFA dfa, + int startIndex, int stopIndex, + @NotNull ATNConfigSet configs); + + /** Called by the parser when it finds less than n-1 predicates for n ambiguous alternatives. + * If there are n-1, we assume that the missing predicate is !(the "or" of the other predicates). + * If there are fewer than n-1, then we don't know which make it alternative to protect + * if the predicates fail. + */ + void reportInsufficientPredicates(@NotNull Parser recognizer, + @NotNull DFA dfa, + int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, + DecisionState decState, + @NotNull SemanticContext[] altToPred, + @NotNull ATNConfigSet configs, boolean fullContextParse); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java index d3aed46e1..dbf4163af 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java @@ -1,10 +1,5 @@ package org.antlr.v4.runtime; -import org.antlr.v4.runtime.atn.ATNConfigSet; -import org.antlr.v4.runtime.atn.DecisionState; -import org.antlr.v4.runtime.atn.SemanticContext; -import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.misc.Nullable; @@ -114,42 +109,4 @@ public interface ANTLRErrorStrategy { void reportError(@NotNull Parser recognizer, @Nullable RecognitionException e) throws RecognitionException; - - /** Called when the parser detects a true ambiguity: an input sequence can be matched - * literally by two or more pass through the grammar. ANTLR resolves the ambiguity in - * favor of the alternative appearing first in the grammar. The start and stop index are - * zero-based absolute indices into the token stream. ambigAlts is a set of alternative numbers - * that can match the input sequence. This method is only called when we are parsing with - * full context. - */ - void reportAmbiguity(@NotNull Parser recognizer, - DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, - @NotNull ATNConfigSet configs); - - void reportAttemptingFullContext(@NotNull Parser recognizer, - @NotNull DFA dfa, - int startIndex, int stopIndex, - @NotNull ATNConfigSet configs); - - /** Called by the parser when it find a conflict that is resolved by retrying the parse - * with full context. This is not a warning; it simply notifies you that your grammar - * is more complicated than Strong LL can handle. The parser moved up to full context - * parsing for that input sequence. - */ - void reportContextSensitivity(@NotNull Parser recognizer, - @NotNull DFA dfa, - int startIndex, int stopIndex, - @NotNull ATNConfigSet configs); - - /** Called by the parser when it finds less than n-1 predicates for n ambiguous alternatives. - * If there are n-1, we assume that the missing predicate is !(the "or" of the other predicates). - * If there are fewer than n-1, then we don't know which make it alternative to protect - * if the predicates fail. - */ - void reportInsufficientPredicates(@NotNull Parser recognizer, - @NotNull DFA dfa, - int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, - DecisionState decState, - @NotNull SemanticContext[] altToPred, - @NotNull ATNConfigSet configs, boolean fullContextParse); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/BaseErrorListener.java b/runtime/Java/src/org/antlr/v4/runtime/BaseErrorListener.java new file mode 100644 index 000000000..51d7c98dd --- /dev/null +++ b/runtime/Java/src/org/antlr/v4/runtime/BaseErrorListener.java @@ -0,0 +1,93 @@ +/* + [The "BSD license"] + Copyright (c) 2012 Terence Parr + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.antlr.v4.runtime; + +import org.antlr.v4.runtime.atn.ATNConfigSet; +import org.antlr.v4.runtime.atn.DecisionState; +import org.antlr.v4.runtime.atn.SemanticContext; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.IntervalSet; + +/** + * + * @author Sam Harwell + */ +public class BaseErrorListener implements ANTLRErrorListener { + + @Override + public void error(Recognizer recognizer, + T offendingSymbol, + int line, + int charPositionInLine, + String msg, + RecognitionException e) + { + } + + @Override + public void reportAmbiguity(Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + IntervalSet ambigAlts, + ATNConfigSet configs) + { + } + + @Override + public void reportAttemptingFullContext(Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + ATNConfigSet configs) + { + } + + @Override + public void reportContextSensitivity(Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + ATNConfigSet configs) + { + } + + @Override + public void reportInsufficientPredicates(Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + IntervalSet ambigAlts, + DecisionState decState, + SemanticContext[] altToPred, + ATNConfigSet configs, + boolean fullContextParse) + { + } +} diff --git a/runtime/Java/src/org/antlr/v4/runtime/ConsoleErrorListener.java b/runtime/Java/src/org/antlr/v4/runtime/ConsoleErrorListener.java index 2fa89eecd..fd2030e05 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ConsoleErrorListener.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ConsoleErrorListener.java @@ -32,7 +32,7 @@ package org.antlr.v4.runtime; * * @author Sam Harwell */ -public class ConsoleErrorListener implements ANTLRErrorListener { +public class ConsoleErrorListener extends BaseErrorListener { public static final ConsoleErrorListener INSTANCE = new ConsoleErrorListener(); @Override diff --git a/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java index f4f7cf87e..c62799480 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java @@ -30,7 +30,6 @@ package org.antlr.v4.runtime; import org.antlr.v4.runtime.atn.*; -import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.runtime.misc.NotNull; @@ -549,36 +548,4 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy { ttype = recognizer.getInputStream().LA(1); } } - - @Override - public void reportAmbiguity(@NotNull Parser recognizer, - DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, - @NotNull ATNConfigSet configs) - { - } - - @Override - public void reportAttemptingFullContext(@NotNull Parser recognizer, - @NotNull DFA dfa, - int startIndex, int stopIndex, - @NotNull ATNConfigSet configs) - { - } - - @Override - public void reportContextSensitivity(@NotNull Parser recognizer, @NotNull DFA dfa, - int startIndex, int stopIndex, @NotNull ATNConfigSet configs) - { - } - - @Override - public void reportInsufficientPredicates(@NotNull Parser recognizer, - @NotNull DFA dfa, - int startIndex, int stopIndex, - @NotNull IntervalSet ambigAlts, - DecisionState decState, - @NotNull SemanticContext[] altToPred, - @NotNull ATNConfigSet configs, boolean fullContextParse) - { - } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorListener.java similarity index 98% rename from runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java rename to runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorListener.java index d05944ca3..1c6f65fd7 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorListener.java @@ -38,7 +38,7 @@ import org.antlr.v4.runtime.misc.NotNull; import java.util.Arrays; -public class DiagnosticErrorStrategy extends DefaultErrorStrategy { +public class DiagnosticErrorListener extends BaseErrorListener { @Override public void reportAmbiguity(@NotNull Parser recognizer, DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java index 5d3d05489..41cae1f0c 100755 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java @@ -1370,7 +1370,7 @@ public class ParserATNSimulator extends ATNSimulator { System.out.println("reportAttemptingFullContext decision="+dfa.decision+":"+configs+ ", input="+parser.getInputString(startIndex, stopIndex)); } - if ( parser!=null ) parser.getErrorHandler().reportAttemptingFullContext(parser, dfa, startIndex, stopIndex, configs); + if ( parser!=null ) parser.getErrorListenerDispatch().reportAttemptingFullContext(parser, dfa, startIndex, stopIndex, configs); } public void reportContextSensitivity(DFA dfa, ATNConfigSet configs, int startIndex, int stopIndex) { @@ -1378,7 +1378,7 @@ public class ParserATNSimulator extends ATNSimulator { System.out.println("reportContextSensitivity decision="+dfa.decision+":"+configs+ ", input="+parser.getInputString(startIndex, stopIndex)); } - if ( parser!=null ) parser.getErrorHandler().reportContextSensitivity(parser, dfa, startIndex, stopIndex, configs); + if ( parser!=null ) parser.getErrorListenerDispatch().reportContextSensitivity(parser, dfa, startIndex, stopIndex, configs); } /** If context sensitive parsing, we know it's ambiguity not conflict */ @@ -1407,7 +1407,7 @@ public class ParserATNSimulator extends ATNSimulator { ambigAlts+":"+configs+ ", input="+parser.getInputString(startIndex, stopIndex)); } - if ( parser!=null ) parser.getErrorHandler().reportAmbiguity(parser, dfa, startIndex, stopIndex, + if ( parser!=null ) parser.getErrorListenerDispatch().reportAmbiguity(parser, dfa, startIndex, stopIndex, ambigAlts, configs); } @@ -1424,7 +1424,7 @@ public class ParserATNSimulator extends ATNSimulator { parser.getInputString(startIndex, stopIndex)); } if ( parser!=null ) { - parser.getErrorHandler().reportInsufficientPredicates(parser, dfa, startIndex, stopIndex, ambigAlts, + parser.getErrorListenerDispatch().reportInsufficientPredicates(parser, dfa, startIndex, stopIndex, ambigAlts, decState, altToPred, configs, fullContextParse); } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java b/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java index cae2de3e7..424605f10 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java @@ -147,7 +147,7 @@ public class TestRig { Constructor parserCtor = parserClass.getConstructor(TokenStream.class); Parser parser = parserCtor.newInstance(tokens); - parser.setErrorHandler(new DiagnosticErrorStrategy()); + parser.addErrorListener(new DiagnosticErrorListener()); if ( printTree || gui || psFile!=null ) { parser.setBuildParseTree(true); diff --git a/tool/playground/TestR.java b/tool/playground/TestR.java index ff028942c..818a62e22 100644 --- a/tool/playground/TestR.java +++ b/tool/playground/TestR.java @@ -29,7 +29,7 @@ import org.antlr.v4.runtime.ANTLRFileStream; import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.DiagnosticErrorStrategy; +import org.antlr.v4.runtime.DiagnosticErrorListener; public class TestR { public static void main(String[] args) throws Exception { @@ -41,7 +41,7 @@ public class TestR { // } RParser p = new RParser(tokens); p.setBuildParseTree(true); - p.setErrorHandler(new DiagnosticErrorStrategy()); + p.addErrorListener(new DiagnosticErrorListener()); p.prog(); } } diff --git a/tool/test/org/antlr/v4/test/BaseTest.java b/tool/test/org/antlr/v4/test/BaseTest.java index 2261b40ea..6975faefe 100644 --- a/tool/test/org/antlr/v4/test/BaseTest.java +++ b/tool/test/org/antlr/v4/test/BaseTest.java @@ -895,7 +895,7 @@ public abstract class BaseTest { createParserST = new ST( " parser = new (tokens);\n" + - " parser.setErrorHandler(new DiagnosticErrorStrategy());\n"); + " parser.addErrorListener(new DiagnosticErrorListener());\n"); } outputFileST.add("createParser", createParserST); outputFileST.add("parserName", parserName); diff --git a/tool/test/org/antlr/v4/test/TestPerformance.java b/tool/test/org/antlr/v4/test/TestPerformance.java index 8b37d2e5e..46c19b3fe 100644 --- a/tool/test/org/antlr/v4/test/TestPerformance.java +++ b/tool/test/org/antlr/v4/test/TestPerformance.java @@ -513,7 +513,7 @@ public class TestPerformance extends BaseTest { void parseFile(CharStream input); } - private static class DescriptiveErrorListener implements ANTLRErrorListener { + private static class DescriptiveErrorListener extends BaseErrorListener { public static DescriptiveErrorListener INSTANCE = new DescriptiveErrorListener(); @Override From a70cb6f36a60edb39b5fc1488cd5884144ac861e Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 19 Mar 2012 08:17:59 -0500 Subject: [PATCH 03/58] Fix NPE in LexerATNSimulator.execDFA error recovery --- .../v4/runtime/atn/LexerATNSimulator.java | 3 ++- .../org/antlr/v4/test/TestLexerErrors.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java index 73a1ed416..19687e5d1 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java @@ -241,7 +241,8 @@ public class LexerATNSimulator extends ATNSimulator { t = input.LA(1); } - return failOrAccept(prevAccept, input, prevAccept.state.configset, t); + ATNConfigSet reach = prevAccept.state != null ? prevAccept.state.configset : null; + return failOrAccept(prevAccept, input, reach, t); } protected int execATN(@NotNull CharStream input, @NotNull ATNConfigSet s0, @Nullable DFAState ds0) { diff --git a/tool/test/org/antlr/v4/test/TestLexerErrors.java b/tool/test/org/antlr/v4/test/TestLexerErrors.java index 10f0eebcd..6ba0f3a3a 100644 --- a/tool/test/org/antlr/v4/test/TestLexerErrors.java +++ b/tool/test/org/antlr/v4/test/TestLexerErrors.java @@ -142,4 +142,29 @@ public class TestLexerErrors extends BaseTest { // TEST RECOVERY + /** + * This is a regression test for #45 "NullPointerException in LexerATNSimulator.execDFA". + * https://github.com/antlr/antlr4/issues/46 + */ + @Test + public void testLexerExecDFA() throws Exception { + String grammar = + "grammar T;\n" + + "start : ID ':' expr;\n" + + "expr : primary expr? {} | expr '->' ID;\n" + + "primary : ID;\n" + + "ID : [a-z]+;\n" + + "\n"; + String result = execLexer("T.g", grammar, "TLexer", "x : x", false); + String expecting = + "[@0,0:0='x',<5>,1:0]\n" + + "[@1,2:2=':',<4>,1:2]\n" + + "[@2,4:4='x',<5>,1:4]\n" + + "[@3,5:4='',<-1>,1:5]\n"; + assertEquals(expecting, result); + assertEquals("line 1:1 token recognition error at: ' '\n" + + "line 1:3 token recognition error at: ' '\n", + this.stderrDuringParse); + } + } From 4bc615d72ff72756f8fcbf284911b9dbcb79a2cc Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 19 Mar 2012 08:32:34 -0500 Subject: [PATCH 04/58] Ensure that serialized transitions only point to states which weren't removed. Add unit test for a current failure case (will be a regression test once fixed). --- .../org/antlr/v4/automata/ATNSerializer.java | 5 ++++ .../org/antlr/v4/test/TestParseErrors.java | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/tool/src/org/antlr/v4/automata/ATNSerializer.java b/tool/src/org/antlr/v4/automata/ATNSerializer.java index 70d1a953f..64e7a0f71 100644 --- a/tool/src/org/antlr/v4/automata/ATNSerializer.java +++ b/tool/src/org/antlr/v4/automata/ATNSerializer.java @@ -133,6 +133,11 @@ public class ATNSerializer { if ( s==null ) continue; // might be optimized away for (int i=0; i' ID;\n" + + "primary : ID;\n" + + "ID : [a-z]+;\n" + + "\n"; + String result = execParser("T.g", grammar, "TParser", "TLexer", "start", "x:x", true); + String expecting = ""; + assertEquals(expecting, result); + assertNull(this.stderrDuringParse); + } + } From 5e0f9a4490673b886f80abd4789b452532ea6887 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 19 Mar 2012 08:33:36 -0500 Subject: [PATCH 05/58] Ensure target non-null in Transition constructor (additional runtime-side consistency check). --- runtime/Java/src/org/antlr/v4/runtime/atn/Transition.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/Transition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/Transition.java index 5dc791975..4cc711ba8 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/Transition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/Transition.java @@ -91,7 +91,13 @@ public abstract class Transition { @NotNull public ATNState target; - protected Transition(@NotNull ATNState target) { this.target = target; } + protected Transition(@NotNull ATNState target) { + if (target == null) { + throw new NullPointerException("target cannot be null."); + } + + this.target = target; + } public int getSerializationType() { return 0; } From bd7796544d980639586b15c10fa68b016dea6d1e Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 19 Mar 2012 08:33:55 -0500 Subject: [PATCH 06/58] Update comments --- .../antlr/v4/runtime/atn/ATNSimulator.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java index e4515f07c..e1f534f89 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java @@ -60,6 +60,10 @@ public abstract class ATNSimulator { int p = 0; atn.grammarType = toInt(data[p++]); atn.maxTokenType = toInt(data[p++]); + + // + // STATES + // int nstates = toInt(data[p++]); for (int i=1; i<=nstates; i++) { int stype = toInt(data[p++]); @@ -75,6 +79,10 @@ public abstract class ATNSimulator { } atn.addState(s); } + + // + // RULES + // int nrules = toInt(data[p++]); if ( atn.grammarType == ATN.LEXER ) { atn.ruleToTokenType = new int[nrules]; @@ -92,11 +100,19 @@ public abstract class ATNSimulator { atn.ruleToActionIndex[i] = actionIndex; } } + + // + // MODES + // int nmodes = toInt(data[p++]); for (int i=0; i Date: Mon, 19 Mar 2012 17:50:51 -0700 Subject: [PATCH 07/58] was not computing lookahead correctly in _LOOK. It assumed all epsilons were predicates. --- .../org/antlr/v4/runtime/atn/LL1Analyzer.java | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java index 28e60f97d..bc9f6735f 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java @@ -114,29 +114,34 @@ public class LL1Analyzer { int n = s.getNumberOfTransitions(); for (int i=0; i Date: Sat, 24 Mar 2012 16:48:26 -0700 Subject: [PATCH 08/58] tweak to remove insuff pred method --- .../antlr/v4/runtime/BaseErrorListener.java | 15 ------------- .../antlr/v4/runtime/ProxyErrorListener.java | 22 ++----------------- 2 files changed, 2 insertions(+), 35 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/BaseErrorListener.java b/runtime/Java/src/org/antlr/v4/runtime/BaseErrorListener.java index 51d7c98dd..739480c26 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/BaseErrorListener.java +++ b/runtime/Java/src/org/antlr/v4/runtime/BaseErrorListener.java @@ -29,8 +29,6 @@ package org.antlr.v4.runtime; import org.antlr.v4.runtime.atn.ATNConfigSet; -import org.antlr.v4.runtime.atn.DecisionState; -import org.antlr.v4.runtime.atn.SemanticContext; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.misc.IntervalSet; @@ -77,17 +75,4 @@ public class BaseErrorListener implements ANTLRErrorListener { ATNConfigSet configs) { } - - @Override - public void reportInsufficientPredicates(Parser recognizer, - DFA dfa, - int startIndex, - int stopIndex, - IntervalSet ambigAlts, - DecisionState decState, - SemanticContext[] altToPred, - ATNConfigSet configs, - boolean fullContextParse) - { - } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/ProxyErrorListener.java b/runtime/Java/src/org/antlr/v4/runtime/ProxyErrorListener.java index fa08c334c..a47c3b8e0 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ProxyErrorListener.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ProxyErrorListener.java @@ -28,13 +28,12 @@ */ package org.antlr.v4.runtime; -import java.util.Collection; import org.antlr.v4.runtime.atn.ATNConfigSet; -import org.antlr.v4.runtime.atn.DecisionState; -import org.antlr.v4.runtime.atn.SemanticContext; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.misc.IntervalSet; +import java.util.Collection; + /** * * @author Sam Harwell @@ -95,21 +94,4 @@ public class ProxyErrorListener implements ANTLRErrorListener { listener.reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, configs); } } - - @Override - public void reportInsufficientPredicates(Parser recognizer, - DFA dfa, - int startIndex, - int stopIndex, - IntervalSet ambigAlts, - DecisionState decState, - SemanticContext[] altToPred, - ATNConfigSet configs, - boolean fullContextParse) - { - for (ANTLRErrorListener listener : delegates) { - listener.reportInsufficientPredicates(recognizer, dfa, startIndex, stopIndex, ambigAlts, decState, altToPred, configs, fullContextParse); - } - } - } From c6365fb5e2b395ae163616a0ef242f31e7d2b576 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Sat, 24 Mar 2012 16:56:36 -0700 Subject: [PATCH 09/58] removed config list (huge) from default message. --- .../org/antlr/v4/runtime/DiagnosticErrorListener.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorListener.java b/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorListener.java index a14741f03..d79d37acd 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorListener.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorListener.java @@ -40,7 +40,8 @@ public class DiagnosticErrorListener extends BaseErrorListener { DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, @NotNull ATNConfigSet configs) { - recognizer.notifyErrorListeners("reportAmbiguity d=" + dfa.decision + ": ambigAlts=" + ambigAlts + ":" + configs + ", input='" + + recognizer.notifyErrorListeners("reportAmbiguity d=" + dfa.decision + + ": ambigAlts=" + ambigAlts + ", input='" + recognizer.getInputString(startIndex, stopIndex) + "'"); } @@ -50,7 +51,8 @@ public class DiagnosticErrorListener extends BaseErrorListener { int startIndex, int stopIndex, @NotNull ATNConfigSet configs) { - recognizer.notifyErrorListeners("reportAttemptingFullContext d=" + dfa.decision + ": " + configs + ", input='" + + recognizer.notifyErrorListeners("reportAttemptingFullContext d=" + + dfa.decision + ", input='" + recognizer.getInputString(startIndex, stopIndex) + "'"); } @@ -58,7 +60,8 @@ public class DiagnosticErrorListener extends BaseErrorListener { public void reportContextSensitivity(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull ATNConfigSet configs) { - recognizer.notifyErrorListeners("reportContextSensitivity d=" + dfa.decision + ": " + configs + ", input='" + + recognizer.notifyErrorListeners("reportContextSensitivity d=" + + dfa.decision + ", input='" + recognizer.getInputString(startIndex, stopIndex) + "'"); } } From 4ea3c73d1fb305abeef2eeb779117d7c3d5d2d55 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Sat, 24 Mar 2012 18:17:35 -0700 Subject: [PATCH 10/58] cleaned up interval stuff. moved getSourceInterval to ParserRuleContext; renamed create to of() in Interval. --- .../antlr/v4/runtime/ParserRuleContext.java | 7 ++++++ .../src/org/antlr/v4/runtime/RuleContext.java | 15 +++++-------- .../org/antlr/v4/runtime/misc/Interval.java | 22 +++++++++++++------ .../antlr/v4/runtime/misc/IntervalSet.java | 12 +++++++--- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java index dde87a1ac..bb05ef019 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java @@ -30,6 +30,7 @@ package org.antlr.v4.runtime; import org.antlr.v4.runtime.atn.ATN; import org.antlr.v4.runtime.atn.ATNState; +import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.misc.Nullable; import org.antlr.v4.runtime.tree.ParseTree; @@ -291,6 +292,12 @@ public class ParserRuleContext extends RuleContext { @Override public int getRuleIndex() { return ruleIndex; } + @Override + public Interval getSourceInterval() { + return Interval.of(start.getTokenIndex(), stop.getTokenIndex()); + } + + public Symbol getStart() { return start; } public Symbol getStop() { return stop; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java index e8714f2ff..a679587af 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java @@ -208,7 +208,12 @@ public class RuleContext implements ParseTree.RuleNode { return invokingState == -1; } - // satisfy the ParseTree interface + // satisfy the ParseTree / SyntaxTree interface + + @Override + public Interval getSourceInterval() { + return Interval.EMPTY; + } @Override public RuleContext getRuleContext() { return this; } @@ -231,14 +236,6 @@ public class RuleContext implements ParseTree.RuleNode { return 0; } - @Override - public Interval getSourceInterval() { - if ( getChildCount()==0 ) return Interval.INVALID; - int start = getChild(0).getSourceInterval().a; - int stop = getChild(getChildCount()-1).getSourceInterval().b; - return new Interval(start, stop); - } - @Override public T accept(ParseTreeVisitor visitor) { return visitor.visitChildren(this); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java b/runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java index e7516df45..bdf2fbd0c 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java @@ -33,6 +33,7 @@ public class Interval { public static final int INTERVAL_POOL_MAX_VALUE = 1000; public static final Interval INVALID = new Interval(-1,-2); + public static final Interval EMPTY = new Interval(0,-1); // len 0 static Interval[] cache = new Interval[INTERVAL_POOL_MAX_VALUE+1]; @@ -52,8 +53,7 @@ public class Interval { * Interval object with a..a in it. On Java.g, 218623 IntervalSets * have a..a (set with 1 element). */ - public static Interval create(int a, int b) { - //return new Interval(a,b); + public static Interval of(int a, int b) { // cache just a..a if ( a!=b || a<0 || a>INTERVAL_POOL_MAX_VALUE ) { return new Interval(a,b); @@ -64,6 +64,14 @@ public class Interval { return cache[a]; } + /** return number of elements between a and b inclusively. x..x is length 1. + * if b < a, then length is 0. 9..10 has length 2. + */ + public int length() { + if ( b Date: Sun, 25 Mar 2012 10:01:27 -0700 Subject: [PATCH 11/58] fix antlr/antlr4/#48 (getText from parse tree node). Don't NPE when start/stop null in getSourceInterval. --- .../src/org/antlr/v4/runtime/ParserRuleContext.java | 10 ++++++++++ .../Java/src/org/antlr/v4/runtime/tree/SyntaxTree.java | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java index bb05ef019..3ba0bb249 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java @@ -294,9 +294,19 @@ public class ParserRuleContext extends RuleContext { @Override public Interval getSourceInterval() { + if ( start==null || stop==null ) return Interval.EMPTY; return Interval.of(start.getTokenIndex(), stop.getTokenIndex()); } + /** Return the text matched by this context and below in the parse + * tree. It includes tokens from this.start .. this.stop inclusive. + * It includes hidden channel tokens between start, stop. The + * edge tokens are always on-channel tokens. + */ + public String getText(TokenStream tokens) { + Interval range = getSourceInterval(); + return range==Interval.EMPTY ? null : tokens.toString(range.a, range.b); + } public Symbol getStart() { return start; } public Symbol getStop() { return stop; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/SyntaxTree.java b/runtime/Java/src/org/antlr/v4/runtime/tree/SyntaxTree.java index bf30cc35a..4133d9b2f 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/SyntaxTree.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/SyntaxTree.java @@ -41,7 +41,7 @@ public interface SyntaxTree extends Tree { * node is a leaf, then the interval represents a single token. * * If source interval is unknown, this does not return null. - * It returns an interval of length 0. + * It returns an interval of length 0: Interval.EMPTY. */ Interval getSourceInterval(); } From bdda174af64a4a325ccfb0c7f721b2c3b4f4c701 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 25 Mar 2012 07:43:54 -0500 Subject: [PATCH 12/58] Set stop token even when RecognitionException occurs. Add ParserRuleContext.exception field to hold exception if one occurs. Resolves antlr/antlr4#49. --- runtime/Java/src/org/antlr/v4/runtime/Parser.java | 2 ++ .../Java/src/org/antlr/v4/runtime/ParserRuleContext.java | 6 ++++++ .../org/antlr/v4/tool/templates/codegen/Java/Java.stg | 4 ++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/Parser.java b/runtime/Java/src/org/antlr/v4/runtime/Parser.java index 66c234f3c..3430fc628 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Parser.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Parser.java @@ -386,6 +386,7 @@ public abstract class Parser extends Recognizer } public void exitRule() { + _ctx.stop = _input.LT(-1); // trigger event on _ctx, before it reverts to parent if ( _parseListeners != null) triggerExitRuleEvent(); _ctx = (ParserRuleContext)_ctx.parent; @@ -411,6 +412,7 @@ public abstract class Parser extends Recognizer } public void unrollRecursionContexts(ParserRuleContext _parentctx) { + _ctx.stop = _input.LT(-1); ParserRuleContext retctx = _ctx; // save current ctx (return value) // unroll so _ctx is as it was before call to recursive method diff --git a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java index 3ba0bb249..23928bc6d 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java @@ -109,6 +109,12 @@ public class ParserRuleContext extends RuleContext { /** Set during parsing to identify which alt of rule parser is in. */ public int altNum; + /** + * The exception which forced this rule to return. If the rule successfully + * completed, this is {@code null}. + */ + public RecognitionException exception; + public ParserRuleContext() { } /** COPY a ctx (I'm deliberately not using copy constructor) */ diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index 422e4ac3d..7fb374f6c 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -229,11 +229,11 @@ RuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAc int _alt; - _localctx.stop = _input.LT(-1); } catch (RecognitionException re) { + _localctx.exception = re; _errHandler.reportError(this, re); _errHandler.recover(this, re); } @@ -265,11 +265,11 @@ LeftRecursiveRuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs, int _alt; - _localctx.stop = _input.LT(-1); } catch (RecognitionException re) { + _localctx.exception = re; _errHandler.reportError(this, re); _errHandler.recover(this, re); } From be4caa3d111a1e88f3e164439f67a7f4e9e24674 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 25 Mar 2012 08:21:49 -0500 Subject: [PATCH 13/58] Fix tests affected by c6365fb5e2 --- .../antlr/v4/test/TestFullContextParsing.java | 36 +++++++++---------- .../org/antlr/v4/test/TestNonGreedyLoops.java | 12 +++---- .../antlr/v4/test/TestSemPredEvalParser.java | 4 +-- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/tool/test/org/antlr/v4/test/TestFullContextParsing.java b/tool/test/org/antlr/v4/test/TestFullContextParsing.java index 4377de4db..4126e57a3 100644 --- a/tool/test/org/antlr/v4/test/TestFullContextParsing.java +++ b/tool/test/org/antlr/v4/test/TestFullContextParsing.java @@ -55,7 +55,7 @@ public class TestFullContextParsing extends BaseTest { "Decision 0:\n" + "s0-ID->:s1=>1\n"; // not ctx sensitive assertEquals(expecting, result); - assertEquals("line 1:0 reportAmbiguity d=0: ambigAlts={1..2}:[(1,1,[]), (1,2,[])],conflictingAlts={1..2}, input='abc'\n", + assertEquals("line 1:0 reportAmbiguity d=0: ambigAlts={1..2}, input='abc'\n", this.stderrDuringParse); } @@ -77,8 +77,8 @@ public class TestFullContextParsing extends BaseTest { "s0-INT->s1\n" + "s1-ID->s2^\n"; assertEquals(expecting, result); - assertEquals("line 1:5 reportAttemptingFullContext d=1: [(28,1,[18 10]), (20,2,[10])], input='34abc'\n" + - "line 1:2 reportContextSensitivity d=1: [(20,1,[10])],uniqueAlt=1, input='34'\n", + assertEquals("line 1:5 reportAttemptingFullContext d=1, input='34abc'\n" + + "line 1:2 reportContextSensitivity d=1, input='34'\n", this.stderrDuringParse); result = execParser("T.g", grammar, "TParser", "TLexer", "s", @@ -88,8 +88,8 @@ public class TestFullContextParsing extends BaseTest { "s0-INT->s1\n" + "s1-ID->s2^\n"; assertEquals(expecting, result); - assertEquals("line 1:5 reportAttemptingFullContext d=1: [(28,1,[22 14]), (24,2,[14])], input='34abc'\n" + - "line 1:5 reportContextSensitivity d=1: [(1,2,[])],uniqueAlt=2, input='34abc'\n", + assertEquals("line 1:5 reportAttemptingFullContext d=1, input='34abc'\n" + + "line 1:5 reportContextSensitivity d=1, input='34abc'\n", this.stderrDuringParse); } @@ -116,10 +116,10 @@ public class TestFullContextParsing extends BaseTest { "s0-INT->s1\n" + "s1-ID->s2^\n"; assertEquals(expecting, result); - assertEquals("line 1:5 reportAttemptingFullContext d=2: [(30,1,[20 10]), (22,2,[10])], input='34abc'\n" + - "line 1:2 reportContextSensitivity d=2: [(22,1,[10])],uniqueAlt=1, input='34'\n" + - "line 1:14 reportAttemptingFullContext d=2: [(30,1,[24 14]), (26,2,[14])], input='34abc'\n" + - "line 1:14 reportContextSensitivity d=2: [(8,2,[18]), (12,2,[18]), (1,2,[])],uniqueAlt=2, input='34abc'\n", + assertEquals("line 1:5 reportAttemptingFullContext d=2, input='34abc'\n" + + "line 1:2 reportContextSensitivity d=2, input='34'\n" + + "line 1:14 reportAttemptingFullContext d=2, input='34abc'\n" + + "line 1:14 reportContextSensitivity d=2, input='34abc'\n", this.stderrDuringParse); } @@ -161,7 +161,7 @@ public class TestFullContextParsing extends BaseTest { "s0-'else'->:s1=>1\n" + "s0-'}'->:s2=>2\n"; assertEquals(expecting, result); - assertEquals("line 1:29 reportAmbiguity d=1: ambigAlts={1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2},dipsIntoOuterContext, input='else'\n", + assertEquals("line 1:29 reportAmbiguity d=1: ambigAlts={1..2}, input='else'\n", this.stderrDuringParse); input = "{ if x then return else foo }"; @@ -181,7 +181,7 @@ public class TestFullContextParsing extends BaseTest { // the start of a stat. But, we are using the theory that // SLL(1)=LL(1) and so we are avoiding full context parsing // by declaring all else clause parsing to be ambiguous. - assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2},dipsIntoOuterContext, input='else'\n", + assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}, input='else'\n", this.stderrDuringParse); input = "{ if x then return else foo }"; @@ -195,7 +195,7 @@ public class TestFullContextParsing extends BaseTest { "Decision 1:\n" + "s0-'else'->:s1=>1\n"; assertEquals(expecting, result); - assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2},dipsIntoOuterContext, input='else'\n", + assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}, input='else'\n", this.stderrDuringParse); input = @@ -212,7 +212,7 @@ public class TestFullContextParsing extends BaseTest { "s0-'else'->:s1=>1\n" + "s0-'}'->:s2=>2\n"; assertEquals(expecting, result); - assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2},dipsIntoOuterContext, input='else'\n", + assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}, input='else'\n", this.stderrDuringParse); input = @@ -229,7 +229,7 @@ public class TestFullContextParsing extends BaseTest { "s0-'else'->:s1=>1\n" + "s0-'}'->:s2=>2\n"; assertEquals(expecting, result); - assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2},dipsIntoOuterContext, input='else'\n", + assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}, input='else'\n", this.stderrDuringParse); } @@ -259,10 +259,10 @@ public class TestFullContextParsing extends BaseTest { assertEquals("pass.\n", found); String expecting = - "line 1:4 reportAttemptingFullContext d=1: [(35,1,[27 15 8]), (41,1,[27 15 8]), (49,1,[27 15 8]), (35,2,[27 21 8]), (41,2,[27 21 8]), (49,2,[27 21 8])], input='a(i)<-'\n" + - "line 1:7 reportContextSensitivity d=1: [(53,2,[])],uniqueAlt=2, input='a(i)<-x'\n" + - "line 1:3 reportAttemptingFullContext d=3: [(35,1,[27 21 8]), (41,2,[27 21 8]), (49,3,[27 21 8])], input='a(i)'\n" + - "line 1:7 reportAmbiguity d=3: ambigAlts={2..3}:[(53,2,[]), (53,3,[])],conflictingAlts={2..3}, input='a(i)<-x'\n"; + "line 1:4 reportAttemptingFullContext d=1, input='a(i)<-'\n" + + "line 1:7 reportContextSensitivity d=1, input='a(i)<-x'\n" + + "line 1:3 reportAttemptingFullContext d=3, input='a(i)'\n" + + "line 1:7 reportAmbiguity d=3: ambigAlts={2..3}, input='a(i)<-x'\n"; assertEquals(expecting, this.stderrDuringParse); } diff --git a/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java b/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java index 6a5c02432..b4770366f 100644 --- a/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java +++ b/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java @@ -195,7 +195,7 @@ public class TestNonGreedyLoops extends BaseTest { "\n" + "Decision 1:\n" + "s0-INT->:s1=>2\n", found); // resolves INT EOF to alt 1 from s since ambig 'tween a and b - assertEquals("line 1:2 reportAmbiguity d=0: ambigAlts={1..2}:[(1,1,[]), (1,2,[])],conflictingAlts={1..2}, input='34'\n" + + assertEquals("line 1:2 reportAmbiguity d=0: ambigAlts={1..2}, input='34'\n" + "line 1:0 extraneous input '34' expecting \n", this.stderrDuringParse); } @@ -527,11 +527,11 @@ public class TestNonGreedyLoops extends BaseTest { "Decision 3:\n" + "s0-'>'->:s2=>2\n" + "s0-ID->:s1=>1\n", found); - assertEquals("line 1:6 reportAttemptingFullContext d=1: [(20,1,[14 6]), (16,2,[6])], input='foo<'\n" + - "line 1:6 reportAmbiguity d=1: ambigAlts={1..2}:[(26,1,[32 32 32 32 14 6]), (33,1,[14 6]), (22,1,[14 6 10 10]), (26,1,[14 6 10 10]), (33,1,[14 6 10 10]), (20,1,[14 6 10 10 10]), (16,1,[6 10 10 10]), (1,1,[]), (22,2,[14 6 10 10 10 10]), (26,2,[14 6 10 10 10 10]), (33,2,[14 6 10 10 10 10]), (20,2,[14 6 10 10 10 10 10]), (16,2,[6 10 10 10 10 10]), (1,2,[])],conflictingAlts={1..2}, input='foo<'\n" + - "line 1:10 reportAttemptingFullContext d=1: [(20,1,[14 6]), (16,2,[6])], input=''\n" + - "line 1:10 reportAmbiguity d=1: ambigAlts={1..2}:[(35,1,[]), (35,2,[])],conflictingAlts={1..2}, input=''\n" + - "line 1:7 reportAmbiguity d=2: ambigAlts={1..2}:[(26,1,[]), (33,1,[]), (26,2,[]), (33,2,[])],conflictingAlts={1..2}, input='/'\n", + assertEquals("line 1:6 reportAttemptingFullContext d=1, input='foo<'\n" + + "line 1:6 reportAmbiguity d=1: ambigAlts={1..2}, input='foo<'\n" + + "line 1:10 reportAttemptingFullContext d=1, input=''\n" + + "line 1:10 reportAmbiguity d=1: ambigAlts={1..2}, input=''\n" + + "line 1:7 reportAmbiguity d=2: ambigAlts={1..2}, input='/'\n", this.stderrDuringParse); found = execParser("T.g", grammar, "TParser", "TLexer", "s", diff --git a/tool/test/org/antlr/v4/test/TestSemPredEvalParser.java b/tool/test/org/antlr/v4/test/TestSemPredEvalParser.java index a1cf21039..4cb2ab7f9 100644 --- a/tool/test/org/antlr/v4/test/TestSemPredEvalParser.java +++ b/tool/test/org/antlr/v4/test/TestSemPredEvalParser.java @@ -153,7 +153,7 @@ public class TestSemPredEvalParser extends BaseTest { "alt 1\n" + "alt 1\n"; assertEquals(expecting, found); - assertEquals("line 1:0 reportAmbiguity d=0: ambigAlts={1..2}:[(6,1,[],up=1), (1,1,[],up=1), (6,2,[],up=1), (1,2,[],up=1), (6,3,[],{1:0}?,up=1), (1,3,[],{1:0}?,up=1)],hasSemanticContext=true,conflictingAlts={1..3},dipsIntoOuterContext, input='x'\n", + assertEquals("line 1:0 reportAmbiguity d=0: ambigAlts={1..2}, input='x'\n", this.stderrDuringParse); } @@ -184,7 +184,7 @@ public class TestSemPredEvalParser extends BaseTest { "alt 2\n" + "alt 2\n"; assertEquals(expecting, found); - assertEquals("line 1:4 reportAmbiguity d=0: ambigAlts={2..3}:[(6,2,[],up=1), (10,2,[],up=1), (1,2,[],up=1), (6,3,[],up=1), (10,3,[],up=1), (1,3,[],up=1), (6,4,[],{1:0}?,up=1), (10,4,[],{1:0}?,up=1), (1,4,[],{1:0}?,up=1)],hasSemanticContext=true,conflictingAlts={2..4},dipsIntoOuterContext, input='x'\n", + assertEquals("line 1:4 reportAmbiguity d=0: ambigAlts={2..3}, input='x'\n", this.stderrDuringParse); } From fdb92ccf6d8f6dd9341e2dab6c4773d5158bd87a Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 25 Mar 2012 18:54:01 -0500 Subject: [PATCH 14/58] Fix tests affected by 9a0aaacbee and 2232ea5101 --- .../antlr/v4/test/TestFullContextParsing.java | 58 ++++------- .../org/antlr/v4/test/TestNonGreedyLoops.java | 95 ++----------------- .../antlr/v4/test/TestSemPredEvalParser.java | 10 +- 3 files changed, 36 insertions(+), 127 deletions(-) diff --git a/tool/test/org/antlr/v4/test/TestFullContextParsing.java b/tool/test/org/antlr/v4/test/TestFullContextParsing.java index 4126e57a3..5ea7486ea 100644 --- a/tool/test/org/antlr/v4/test/TestFullContextParsing.java +++ b/tool/test/org/antlr/v4/test/TestFullContextParsing.java @@ -107,11 +107,6 @@ public class TestFullContextParsing extends BaseTest { String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "$ 34 abc @ 34 abc", true); String expecting = - "Decision 1:\n" + - "s0-EOF->:s3=>2\n" + - "s0-'@'->:s2=>1\n" + - "s0-'$'->:s1=>1\n" + - "\n" + "Decision 2:\n" + "s0-INT->s1\n" + "s1-ID->s2^\n"; @@ -139,10 +134,6 @@ public class TestFullContextParsing extends BaseTest { String result = execParser("T.g", grammar, "TParser", "TLexer", "s", input, true); String expecting = - "Decision 0:\n" + - "s0-'if'->:s1=>1\n" + - "s0-'}'->:s2=>2\n" + - "\n" + "Decision 1:\n" + "s0-'}'->:s1=>2\n"; assertEquals(expecting, result); @@ -153,27 +144,20 @@ public class TestFullContextParsing extends BaseTest { result = execParser("T.g", grammar, "TParser", "TLexer", "s", input, true); expecting = - "Decision 0:\n" + - "s0-'if'->:s1=>1\n" + - "s0-'}'->:s2=>2\n" + - "\n" + "Decision 1:\n" + - "s0-'else'->:s1=>1\n" + + "s0-'else'->s1^\n" + "s0-'}'->:s2=>2\n"; assertEquals(expecting, result); - assertEquals("line 1:29 reportAmbiguity d=1: ambigAlts={1..2}, input='else'\n", + assertEquals("line 1:29 reportAttemptingFullContext d=1, input='else'\n" + + "line 1:38 reportAmbiguity d=1: ambigAlts={1..2}, input='elsefoo}'\n", this.stderrDuringParse); input = "{ if x then return else foo }"; result = execParser("T.g", grammar, "TParser", "TLexer", "s", input, true); expecting = - "Decision 0:\n" + - "s0-'if'->:s1=>1\n" + - "s0-'}'->:s2=>2\n" + - "\n" + "Decision 1:\n" + - "s0-'else'->:s1=>1\n"; + "s0-'else'->s1^\n"; assertEquals(expecting, result); // Technically, this input sequence is not ambiguous because else // uniquely predicts going into the optional subrule. else cannot @@ -181,21 +165,19 @@ public class TestFullContextParsing extends BaseTest { // the start of a stat. But, we are using the theory that // SLL(1)=LL(1) and so we are avoiding full context parsing // by declaring all else clause parsing to be ambiguous. - assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}, input='else'\n", + assertEquals("line 1:19 reportAttemptingFullContext d=1, input='else'\n" + + "line 1:19 reportContextSensitivity d=1, input='else'\n", this.stderrDuringParse); input = "{ if x then return else foo }"; result = execParser("T.g", grammar, "TParser", "TLexer", "s", input, true); expecting = - "Decision 0:\n" + - "s0-'if'->:s1=>1\n" + - "s0-'}'->:s2=>2\n" + - "\n" + "Decision 1:\n" + - "s0-'else'->:s1=>1\n"; + "s0-'else'->s1^\n"; assertEquals(expecting, result); - assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}, input='else'\n", + assertEquals("line 1:19 reportAttemptingFullContext d=1, input='else'\n" + + "line 1:19 reportContextSensitivity d=1, input='else'\n", this.stderrDuringParse); input = @@ -204,15 +186,14 @@ public class TestFullContextParsing extends BaseTest { result = execParser("T.g", grammar, "TParser", "TLexer", "s", input, true); expecting = - "Decision 0:\n" + - "s0-'if'->:s1=>1\n" + - "s0-'}'->:s2=>2\n" + - "\n" + "Decision 1:\n" + - "s0-'else'->:s1=>1\n" + + "s0-'else'->s1^\n" + "s0-'}'->:s2=>2\n"; assertEquals(expecting, result); - assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}, input='else'\n", + assertEquals("line 1:19 reportAttemptingFullContext d=1, input='else'\n" + + "line 1:19 reportContextSensitivity d=1, input='else'\n" + + "line 2:27 reportAttemptingFullContext d=1, input='else'\n" + + "line 2:36 reportAmbiguity d=1: ambigAlts={1..2}, input='elsefoo}'\n", this.stderrDuringParse); input = @@ -221,15 +202,14 @@ public class TestFullContextParsing extends BaseTest { result = execParser("T.g", grammar, "TParser", "TLexer", "s", input, true); expecting = - "Decision 0:\n" + - "s0-'if'->:s1=>1\n" + - "s0-'}'->:s2=>2\n" + - "\n" + "Decision 1:\n" + - "s0-'else'->:s1=>1\n" + + "s0-'else'->s1^\n" + "s0-'}'->:s2=>2\n"; assertEquals(expecting, result); - assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}, input='else'\n", + assertEquals("line 1:19 reportAttemptingFullContext d=1, input='else'\n" + + "line 1:19 reportContextSensitivity d=1, input='else'\n" + + "line 2:27 reportAttemptingFullContext d=1, input='else'\n" + + "line 2:36 reportAmbiguity d=1: ambigAlts={1..2}, input='elsefoo}'\n", this.stderrDuringParse); } diff --git a/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java b/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java index b4770366f..0f8df75e4 100644 --- a/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java +++ b/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java @@ -180,23 +180,16 @@ public class TestNonGreedyLoops extends BaseTest { "x", true); assertEquals("alt 1\n" + "Decision 0:\n" + - "s0-ID->:s1=>1\n" + - "\n" + - "Decision 1:\n" + - "s0-ID->:s1=>2\n", found); - assertEquals("line 1:0 extraneous input 'x' expecting \n", this.stderrDuringParse); + "s0-ID->:s1=>1\n", found); + assertNull(this.stderrDuringParse); found = execParser("T.g", grammar, "TParser", "TLexer", "s", "34", true); assertEquals("alt 1\n" + "Decision 0:\n" + "s0-INT->s1\n" + - "s1-EOF->:s2=>1\n" + - "\n" + - "Decision 1:\n" + - "s0-INT->:s1=>2\n", found); // resolves INT EOF to alt 1 from s since ambig 'tween a and b - assertEquals("line 1:2 reportAmbiguity d=0: ambigAlts={1..2}, input='34'\n" + - "line 1:0 extraneous input '34' expecting \n", + "s1-EOF->:s2=>1\n", found); // resolves INT EOF to alt 1 from s since ambig 'tween a and b + assertEquals("line 1:2 reportAmbiguity d=0: ambigAlts={1..2}, input='34'\n", this.stderrDuringParse); } @@ -360,10 +353,7 @@ public class TestNonGreedyLoops extends BaseTest { "s2-INT->:s3=>1\n" + "s2-ID->s4\n" + "s4-';'->s5\n" + - "s5-EOF->:s6=>2\n" + - "\n" + - "Decision 1:\n" + - "s0-ID->:s1=>3\n", found); + "s5-EOF->:s6=>2\n", found); input = "if ( 1 ) { x=3; { return 4; } } return 99; abc=def;"; found = execParser("T.g", grammar, "TParser", "TLexer", "s", @@ -376,19 +366,7 @@ public class TestNonGreedyLoops extends BaseTest { "s3-'='->s4\n" + "s4-ID->s5\n" + "s5-';'->s6\n" + - "s6-EOF->:s7=>2\n" + - "\n" + - "Decision 1:\n" + - "s0-'{'->:s2=>4\n" + - "s0-'if'->:s1=>1\n" + - "s0-'return'->:s4=>2\n" + - "s0-ID->:s3=>3\n" + - "\n" + - "Decision 2:\n" + - "s0-'{'->:s2=>1\n" + - "s0-'return'->:s3=>1\n" + - "s0-'}'->:s4=>2\n" + - "s0-ID->:s1=>1\n", found); + "s6-EOF->:s7=>2\n", found); input = "x=1; a=3;"; // FAILS to match since it can't match last element execParser("T.g", grammar, "TParser", "TLexer", "s", @@ -436,10 +414,7 @@ public class TestNonGreedyLoops extends BaseTest { "s1-'='->s2\n" + "s2-INT->:s3=>1\n" + "s2-ID->s4\n" + - "s4-';'->:s5=>2\n" + - "\n" + - "Decision 1:\n" + - "s0-ID->:s1=>3\n", found); // ignores x=1 that follows first a=b assignment + "s4-';'->:s5=>2\n", found); // ignores x=1 that follows first a=b assignment input = "if ( 1 ) { x=3; { return 4; } } return 99; abc=def;"; found = execParser("T.g", grammar, "TParser", "TLexer", "s", @@ -451,19 +426,7 @@ public class TestNonGreedyLoops extends BaseTest { "s0-ID->s3\n" + "s3-'='->s4\n" + "s4-ID->s5\n" + - "s5-';'->:s6=>2\n" + - "\n" + - "Decision 1:\n" + - "s0-'{'->:s2=>4\n" + - "s0-'if'->:s1=>1\n" + - "s0-'return'->:s4=>2\n" + - "s0-ID->:s3=>3\n" + - "\n" + - "Decision 2:\n" + - "s0-'{'->:s2=>1\n" + - "s0-'return'->:s3=>1\n" + - "s0-'}'->:s4=>2\n" + - "s0-ID->:s1=>1\n", found); + "s5-';'->:s6=>2\n", found); input = "x=1; a=3;"; // FAILS to match since it can't match either stat execParser("T.g", grammar, "TParser", "TLexer", "s", @@ -481,10 +444,7 @@ public class TestNonGreedyLoops extends BaseTest { "s1-'='->s2\n" + "s2-INT->:s3=>1\n" + "s2-ID->s4\n" + - "s4-';'->:s5=>2\n" + - "\n" + - "Decision 1:\n" + - "s0-ID->:s1=>3\n", found); // should not finish all input + "s4-';'->:s5=>2\n", found); // should not finish all input } @Test public void testHTMLTags() throws Exception { @@ -504,11 +464,6 @@ public class TestNonGreedyLoops extends BaseTest { found = execParser("T.g", grammar, "TParser", "TLexer", "s", "foo", true); assertEquals("foo\n" + - "Decision 0:\n" + - "s0-EOF->:s3=>2\n" + - "s0-'<'->:s1=>1\n" + - "s0-ID->:s2=>1\n" + - "\n" + "Decision 1:\n" + "s0-'<'->s1\n" + "s0-ID->:s5=>2\n" + @@ -537,10 +492,6 @@ public class TestNonGreedyLoops extends BaseTest { found = execParser("T.g", grammar, "TParser", "TLexer", "s", "", true); assertEquals("\n" + - "Decision 0:\n" + - "s0-EOF->:s2=>2\n" + - "s0-'<'->:s1=>1\n" + - "\n" + "Decision 1:\n" + "s0-'<'->s1\n" + "s1-'/'->s2\n" + @@ -560,10 +511,6 @@ public class TestNonGreedyLoops extends BaseTest { found = execParser("T.g", grammar, "TParser", "TLexer", "s", "", true); assertEquals("\n" + - "Decision 0:\n" + - "s0-EOF->:s2=>2\n" + - "s0-'<'->:s1=>1\n" + - "\n" + "Decision 1:\n" + "s0-'<'->s1\n" + "s1-'/'->s2\n" + @@ -626,15 +573,6 @@ public class TestNonGreedyLoops extends BaseTest { "s3-INT->s3\n" + "s4-'='->s3\n" + "\n" + - "Decision 1:\n" + // (tag|header) - "s0-'<'->:s1=>1\n" + - "\n" + - "Decision 2:\n" + // (...)* - "s0-EOF->:s3=>2\n" + - "s0-'<'->:s2=>1\n" + - "s0-','->:s1=>1\n" + - "s0-INT->:s1=>1\n" + - "\n" + "Decision 3:\n" + // .+ "s0-'x'->:s1=>1\n" + "s0-'>'->:s2=>2\n" + @@ -655,13 +593,6 @@ public class TestNonGreedyLoops extends BaseTest { "s4-ID->s5\n" + "s5-'>'->:s6=>2\n" + "\n" + - "Decision 1:\n" + - "s0-'<'->:s1=>1\n" + - "\n" + - "Decision 2:\n" + - "s0-EOF->:s2=>2\n" + - "s0-'x'->:s1=>1\n" + - "\n" + "Decision 3:\n" + "s0-'>'->:s2=>2\n" + "s0-ID->:s1=>1\n", found); @@ -682,14 +613,6 @@ public class TestNonGreedyLoops extends BaseTest { "s4-'>'->:s7=>2\n" + "s4-'<'->:s5=>2\n" + "\n" + - "Decision 1:\n" + - "s0-'<'->:s1=>1\n" + - "\n" + - "Decision 2:\n" + - "s0-EOF->:s3=>2\n" + - "s0-'x'->:s1=>1\n" + - "s0-'>'->:s2=>1\n" + - "\n" + "Decision 3:\n" + "s0-'>'->:s1=>2\n" + "s0-ID->:s2=>1\n", // doesn't match tag; null diff --git a/tool/test/org/antlr/v4/test/TestSemPredEvalParser.java b/tool/test/org/antlr/v4/test/TestSemPredEvalParser.java index 4cb2ab7f9..5bf09dafb 100644 --- a/tool/test/org/antlr/v4/test/TestSemPredEvalParser.java +++ b/tool/test/org/antlr/v4/test/TestSemPredEvalParser.java @@ -153,7 +153,10 @@ public class TestSemPredEvalParser extends BaseTest { "alt 1\n" + "alt 1\n"; assertEquals(expecting, found); - assertEquals("line 1:0 reportAmbiguity d=0: ambigAlts={1..2}, input='x'\n", + assertEquals("line 1:0 reportAttemptingFullContext d=0, input='x'\n" + + "line 1:0 reportAmbiguity d=0: ambigAlts={1..2}, input='x'\n" + + "line 1:3 reportAttemptingFullContext d=0, input='y'\n" + + "line 1:3 reportAmbiguity d=0: ambigAlts={1..2}, input='y'\n", this.stderrDuringParse); } @@ -184,7 +187,10 @@ public class TestSemPredEvalParser extends BaseTest { "alt 2\n" + "alt 2\n"; assertEquals(expecting, found); - assertEquals("line 1:4 reportAmbiguity d=0: ambigAlts={2..3}, input='x'\n", + assertEquals("line 1:4 reportAttemptingFullContext d=0, input='x'\n" + + "line 1:4 reportAmbiguity d=0: ambigAlts={2..3}, input='x'\n" + + "line 1:7 reportAttemptingFullContext d=0, input='y'\n" + + "line 1:7 reportAmbiguity d=0: ambigAlts={2..3}, input='y'\n", this.stderrDuringParse); } From df2e24a228c7ac56026d2120625979434f3beb8c Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 25 Mar 2012 07:53:59 -0500 Subject: [PATCH 15/58] Override getRuleIndex() in generated context objects so ParserRuleContext.ruleIndex field can be removed --- runtime/Java/src/org/antlr/v4/runtime/Parser.java | 8 +++----- .../Java/src/org/antlr/v4/runtime/ParserRuleContext.java | 7 ------- .../org/antlr/v4/tool/templates/codegen/Java/Java.stg | 1 + 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/Parser.java b/runtime/Java/src/org/antlr/v4/runtime/Parser.java index 66c234f3c..04c5b56ca 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Parser.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Parser.java @@ -45,18 +45,18 @@ public abstract class Parser extends Recognizer public class TraceListener implements ParseListener { @Override public void enterNonLRRule(ParserRuleContext ctx) { - System.out.println("enter " + getRuleNames()[ctx.ruleIndex] + ", LT(1)=" + _input.LT(1).getText()); + System.out.println("enter " + getRuleNames()[ctx.getRuleIndex()] + ", LT(1)=" + _input.LT(1).getText()); } @Override public void exitEveryRule(ParserRuleContext ctx) { - System.out.println("exit "+getRuleNames()[ctx.ruleIndex]+", LT(1)="+_input.LT(1).getText()); + System.out.println("exit "+getRuleNames()[ctx.getRuleIndex()]+", LT(1)="+_input.LT(1).getText()); } @Override public void visitTerminal(ParserRuleContext parent, Token token) { System.out.println("consume "+token+" rule "+ - getRuleNames()[parent.ruleIndex]+ + getRuleNames()[parent.getRuleIndex()]+ " alt="+parent.altNum); } } @@ -380,7 +380,6 @@ public abstract class Parser extends Recognizer public void enterRule(ParserRuleContext localctx, int ruleIndex) { _ctx = localctx; _ctx.start = _input.LT(1); - _ctx.ruleIndex = ruleIndex; if (_buildParseTrees) addContextToParseTree(); if ( _parseListeners != null) triggerEnterRuleEvent(); } @@ -407,7 +406,6 @@ public abstract class Parser extends Recognizer public void pushNewRecursionContext(ParserRuleContext localctx, int ruleIndex) { _ctx = localctx; _ctx.start = _input.LT(1); - _ctx.ruleIndex = ruleIndex; } public void unrollRecursionContexts(ParserRuleContext _parentctx) { diff --git a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java index 3ba0bb249..3571a5c83 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java @@ -103,9 +103,6 @@ public class ParserRuleContext extends RuleContext { public Symbol start, stop; - /** Set during parsing to identify which rule parser is in. */ - public int ruleIndex; - /** Set during parsing to identify which alt of rule parser is in. */ public int altNum; @@ -120,7 +117,6 @@ public class ParserRuleContext extends RuleContext { this.start = ctx.start; this.stop = ctx.stop; - this.ruleIndex = ctx.ruleIndex; } public ParserRuleContext(@Nullable ParserRuleContext parent, int invokingStateNumber, int stateNumber) { @@ -289,9 +285,6 @@ public class ParserRuleContext extends RuleContext { @Override public int getChildCount() { return children!=null ? children.size() : 0; } - @Override - public int getRuleIndex() { return ruleIndex; } - @Override public Interval getSourceInterval() { if ( start==null || stop==null ) return Interval.EMPTY; diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index 422e4ac3d..b838d8af2 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -608,6 +608,7 @@ public static class extends implement super(parent, state); = ;}; separator="\n"> } + @Override public int getRuleIndex() { return RULE_; } public () { } public void copyFrom( ctx) { From 169f58a3ff52b139be9c3875d0a17b97533383aa Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Sun, 25 Mar 2012 21:19:39 -0700 Subject: [PATCH 16/58] fix def of EMPTY->INVALID --- runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java | 4 ++-- runtime/Java/src/org/antlr/v4/runtime/RuleContext.java | 2 +- runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java | 1 - runtime/Java/src/org/antlr/v4/runtime/tree/SyntaxTree.java | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java index 3ba0bb249..4f3edc121 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java @@ -294,7 +294,7 @@ public class ParserRuleContext extends RuleContext { @Override public Interval getSourceInterval() { - if ( start==null || stop==null ) return Interval.EMPTY; + if ( start==null || stop==null ) return Interval.INVALID; return Interval.of(start.getTokenIndex(), stop.getTokenIndex()); } @@ -305,7 +305,7 @@ public class ParserRuleContext extends RuleContext { */ public String getText(TokenStream tokens) { Interval range = getSourceInterval(); - return range==Interval.EMPTY ? null : tokens.toString(range.a, range.b); + return range==Interval.INVALID ? null : tokens.toString(range.a, range.b); } public Symbol getStart() { return start; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java index a679587af..485b369f6 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java @@ -212,7 +212,7 @@ public class RuleContext implements ParseTree.RuleNode { @Override public Interval getSourceInterval() { - return Interval.EMPTY; + return Interval.INVALID; } @Override diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java b/runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java index bdf2fbd0c..6f7b4257b 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java @@ -33,7 +33,6 @@ public class Interval { public static final int INTERVAL_POOL_MAX_VALUE = 1000; public static final Interval INVALID = new Interval(-1,-2); - public static final Interval EMPTY = new Interval(0,-1); // len 0 static Interval[] cache = new Interval[INTERVAL_POOL_MAX_VALUE+1]; diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/SyntaxTree.java b/runtime/Java/src/org/antlr/v4/runtime/tree/SyntaxTree.java index 4133d9b2f..a4da54d8b 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/SyntaxTree.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/SyntaxTree.java @@ -41,7 +41,7 @@ public interface SyntaxTree extends Tree { * node is a leaf, then the interval represents a single token. * * If source interval is unknown, this does not return null. - * It returns an interval of length 0: Interval.EMPTY. + * It returns Interval.INVALID. */ Interval getSourceInterval(); } From 9b42e7dfe6dffae056737b716c4aacbbc2c016ea Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 26 Mar 2012 18:00:16 -0500 Subject: [PATCH 17/58] Implement support for abstract grammars via the "abstract" grammar option and "-abstract" command line option. Resolves antlr/antlr4#36. --- .../antlr/v4/tool/templates/codegen/Java/Java.stg | 4 ++-- tool/src/org/antlr/v4/Tool.java | 2 ++ tool/src/org/antlr/v4/codegen/model/Lexer.java | 2 ++ tool/src/org/antlr/v4/codegen/model/Parser.java | 3 +++ .../org/antlr/v4/semantics/BasicSemanticChecks.java | 2 ++ tool/src/org/antlr/v4/tool/Grammar.java | 12 ++++++++++++ 6 files changed, 23 insertions(+), 2 deletions(-) diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index 422e4ac3d..41f2e3c49 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -117,7 +117,7 @@ Parser(parser, funcs, atn, sempredFuncs, superclass) ::= << Parser_(parser, funcs, atn, sempredFuncs, ctor, extras, superclass) ::= << @SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) -public class extends { +public abstract class extends { public static final int =}; separator=", ", wrap, anchor>; @@ -713,7 +713,7 @@ import org.antlr.v4.runtime.misc.*; Lexer(lexer, atn, actionFuncs, sempredFuncs) ::= << @SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) -public class extends Lexer { +public abstract class extends Lexer { public static final int =}; separator=", ", wrap, anchor>; = ;}; separator="\n"> diff --git a/tool/src/org/antlr/v4/Tool.java b/tool/src/org/antlr/v4/Tool.java index 50b021242..8a81148fc 100644 --- a/tool/src/org/antlr/v4/Tool.java +++ b/tool/src/org/antlr/v4/Tool.java @@ -116,6 +116,7 @@ public class Tool { public boolean gen_listener = true; public boolean gen_parse_listener = false; public boolean gen_visitor = false; + public boolean abstract_recognizer = false; public static Option[] optionDefs = { new Option("outputDirectory", "-o", OptionArgType.STRING, "specify output directory where all output is generated"), @@ -133,6 +134,7 @@ public class Tool { new Option("gen_parse_listener", "-no-parse-listener", "don't generate parse listener (default)"), new Option("gen_visitor", "-visitor", "generate parse tree visitor"), new Option("gen_visitor", "-no-visitor", "don't generate parse tree visitor (default)"), + new Option("abstract_recognizer", "-abstract", "generate abstract recognizer classes"), new Option("saveLexer", "-Xsave-lexer", "save temp lexer file created for combined grammars"), new Option("launch_ST_inspector", "-XdbgST", "launch StringTemplate visualizer on generated code"), diff --git a/tool/src/org/antlr/v4/codegen/model/Lexer.java b/tool/src/org/antlr/v4/codegen/model/Lexer.java index 45edc6193..0f357d797 100644 --- a/tool/src/org/antlr/v4/codegen/model/Lexer.java +++ b/tool/src/org/antlr/v4/codegen/model/Lexer.java @@ -49,6 +49,7 @@ public class Lexer extends OutputModelObject { public String[] tokenNames; public Set ruleNames; public Collection modes; + public boolean abstractRecognizer; @ModelElement public SerializedATN atn; @ModelElement public LinkedHashMap actionFuncs = @@ -89,6 +90,7 @@ public class Lexer extends OutputModelObject { } } ruleNames = g.rules.keySet(); + abstractRecognizer = g.isAbstract(); } } diff --git a/tool/src/org/antlr/v4/codegen/model/Parser.java b/tool/src/org/antlr/v4/codegen/model/Parser.java index 76c693ddf..f5cab4add 100644 --- a/tool/src/org/antlr/v4/codegen/model/Parser.java +++ b/tool/src/org/antlr/v4/codegen/model/Parser.java @@ -47,6 +47,7 @@ public class Parser extends OutputModelObject { public Set ruleNames; public Collection rules; public ParserFile file; + public boolean abstractRecognizer; @ModelElement public List funcs = new ArrayList(); @ModelElement public SerializedATN atn; @@ -89,5 +90,7 @@ public class Parser extends OutputModelObject { } else { superclass = new DefaultParserSuperClass(); } + + abstractRecognizer = g.isAbstract(); } } diff --git a/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java b/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java index 1c324d75a..4ab8c9dc0 100644 --- a/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java +++ b/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java @@ -74,6 +74,7 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { add("TokenLabelType"); add("superClass"); add("filter"); + add("abstract"); } }; @@ -83,6 +84,7 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { add("tokenVocab"); add("TokenLabelType"); add("superClass"); + add("abstract"); } }; diff --git a/tool/src/org/antlr/v4/tool/Grammar.java b/tool/src/org/antlr/v4/tool/Grammar.java index bcd8510b2..bf30b8a9d 100644 --- a/tool/src/org/antlr/v4/tool/Grammar.java +++ b/tool/src/org/antlr/v4/tool/Grammar.java @@ -362,6 +362,11 @@ public class Grammar implements AttributeResolver { return parent.getOutermostGrammar(); } + public boolean isAbstract() { + return Boolean.parseBoolean(getOptionString("abstract")) + || (tool != null && tool.abstract_recognizer); + } + /** Get the name of the generated recognizer; may or may not be same * as grammar name. * Recognizer is TParser and TLexer from T if combined, else @@ -377,9 +382,16 @@ public class Grammar implements AttributeResolver { buf.append(g.name); buf.append('_'); } + if (isAbstract()) { + buf.append("Abstract"); + } buf.append(name); qualifiedName = buf.toString(); } + else if (isAbstract()) { + qualifiedName = "Abstract" + name; + } + if ( isCombined() || (isLexer() && implicitLexer!=null) ) { suffix = Grammar.getGrammarTypeToFileNameSuffix(getType()); From 9c1e58db7cbf6e5707694ed271911ce7085c9abb Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Tue, 27 Mar 2012 16:21:01 -0700 Subject: [PATCH 18/58] add {} in primary alt block to prevent ID|INT from becoming SET, which breaks code gen needs. --- tool/playground/T.g | 8 +-- .../v4/tool/templates/LeftRecursiveRules.stg | 2 +- .../org/antlr/v4/test/TestLeftRecursion.java | 53 +++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/tool/playground/T.g b/tool/playground/T.g index eebbe9ddd..9949f8074 100644 --- a/tool/playground/T.g +++ b/tool/playground/T.g @@ -1,6 +1,8 @@ grammar T; -s : f f EOF; -f : | x; -x : 'a' 'b'; +s : e ';' ; +e : e '*' e + | ID + | INT + ; INT : '0'..'9'+; WS : (' '|'\n') {skip();} ; diff --git a/tool/resources/org/antlr/v4/tool/templates/LeftRecursiveRules.stg b/tool/resources/org/antlr/v4/tool/templates/LeftRecursiveRules.stg index 9ac580e88..7c0bcddc2 100644 --- a/tool/resources/org/antlr/v4/tool/templates/LeftRecursiveRules.stg +++ b/tool/resources/org/antlr/v4/tool/templates/LeftRecursiveRules.stg @@ -37,7 +37,7 @@ recRule(ruleName, precArgDef, argName, primaryAlts, opAlts, setResultAction, userRetvals, leftRecursiveRuleRefLabels) ::= << [] returns [] - : ( }; separator="\n | "> + : ( {} }; separator="\n | "> ) ( )* diff --git a/tool/test/org/antlr/v4/test/TestLeftRecursion.java b/tool/test/org/antlr/v4/test/TestLeftRecursion.java index f5b8400f9..205f737c8 100644 --- a/tool/test/org/antlr/v4/test/TestLeftRecursion.java +++ b/tool/test/org/antlr/v4/test/TestLeftRecursion.java @@ -276,6 +276,59 @@ public class TestLeftRecursion extends BaseTest { runTests(grammar, tests, "s"); } + @Test + public void testAmbigLR() throws Exception { + String grammar = + "// START: g\n" + + "grammar Expr;\n" + + "// END: g\n" + + "\n" + + "// START:stat\n" + + "prog: stat ;\n" + + "\n" + + "stat: expr NEWLINE -> printExpr\n" + + " | ID '=' expr NEWLINE -> assign\n" + + " | NEWLINE -> blank\n" + + " ;\n" + + "// END:stat\n" + + "\n" + + "// START:expr\n" + + "expr: expr ('*'|'/') expr -> MulDiv\n" + + " | expr ('+'|'-') expr -> AddSub\n" + + " | INT -> int\n" + + " | ID -> id\n" + + " | '(' expr ')' -> parens\n" + + " ;\n" + + "// END:expr\n" + + "\n" + + "// show marginal cost of adding a clear/wipe command for memory\n" + + "\n" + + "// START:tokens\n" + + "MUL : '*' ; // assigns token name to '*' used above in grammar\n" + + "DIV : '/' ;\n" + + "ADD : '+' ;\n" + + "SUB : '-' ;\n" + + "ID : [a-zA-Z]+ ; // match identifiers\n" + + "INT : [0-9]+ ; // match integers\n" + + "NEWLINE:'\\r'? '\\n' ; // return newlines to parser (is end-statement signal)\n" + + "WS : [ \\t]+ -> skip ; // toss out whitespace\n" + + "// END:tokens\n"; + String result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "1\n", true); + assertNull(stderrDuringParse); + + result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "a = 5\n", true); + assertNull(stderrDuringParse); + + result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "b = 6\n", true); + assertNull(stderrDuringParse); + + result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "a+b*2\n", true); + assertNull(stderrDuringParse); + + result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "(1+2)*3\n", true); + assertNull(stderrDuringParse); + } + public void runTests(String grammar, String[] tests, String startRule) { rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer"); writeRecognizerAndCompile("TParser", From 4b0040f2a17d5a20980121db8226aab927ac7fb4 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 27 Mar 2012 21:20:29 -0500 Subject: [PATCH 19/58] Fix test affected by bdda174af6 (set stop token even if exception occurs) --- tool/test/org/antlr/v4/test/TestNonGreedyLoops.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java b/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java index 0f8df75e4..8ad547ba6 100644 --- a/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java +++ b/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java @@ -601,7 +601,7 @@ public class TestNonGreedyLoops extends BaseTest { // Seeing '.' in a lookahead prediction can be misleading!! found = execParser("T.g", grammar, "TParser", "TLexer", "s", "x <>", true); - assertEquals("null\n" + + assertEquals("<\n" + "\n" + "Decision 0:\n" + "s0-'x'->s1\n" + From eb48cdf55bf33c684f6a0a294d403418cca7c8b5 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 27 Mar 2012 21:22:04 -0500 Subject: [PATCH 20/58] Fix test affected by 9c1e58db7c (add {} in primary alt block of LR rule) --- tool/test/org/antlr/v4/test/TestListeners.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tool/test/org/antlr/v4/test/TestListeners.java b/tool/test/org/antlr/v4/test/TestListeners.java index 46691b54d..e6b15ba3d 100644 --- a/tool/test/org/antlr/v4/test/TestListeners.java +++ b/tool/test/org/antlr/v4/test/TestListeners.java @@ -192,7 +192,7 @@ public class TestListeners extends BaseTest { "1\n" + "2\n" + "3\n" + - "1 [14 6]\n"; + "1 [16 6]\n"; assertEquals(expecting, result); } } From 95b6cd58c4bc8da2c17996d05a8acfb7fd1fc741 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 27 Mar 2012 21:25:23 -0500 Subject: [PATCH 21/58] Fix NPE when a grammar contains no rules (instead of appropriate error message) --- tool/src/org/antlr/v4/tool/GrammarTransformPipeline.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tool/src/org/antlr/v4/tool/GrammarTransformPipeline.java b/tool/src/org/antlr/v4/tool/GrammarTransformPipeline.java index 45b1e6ba8..f1627cf3f 100644 --- a/tool/src/org/antlr/v4/tool/GrammarTransformPipeline.java +++ b/tool/src/org/antlr/v4/tool/GrammarTransformPipeline.java @@ -317,7 +317,14 @@ public class GrammarTransformPipeline { (GrammarAST)adaptor.create(ANTLRParser.RULES, "RULES"); lexerAST.addChild(lexerRulesRoot); List rulesWeMoved = new ArrayList(); - GrammarASTWithOptions[] rules = ((List)combinedRulesRoot.getChildren()).toArray(new GrammarASTWithOptions[0]); + GrammarASTWithOptions[] rules; + if (combinedRulesRoot.getChildCount() > 0) { + rules = ((List)combinedRulesRoot.getChildren()).toArray(new GrammarASTWithOptions[0]); + } + else { + rules = new GrammarASTWithOptions[0]; + } + if ( rules!=null ) { for (GrammarASTWithOptions r : rules) { String ruleName = r.getChild(0).getText(); From 2ff9c128487c6af7ba96a963a9eb4cbf8715441d Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 27 Mar 2012 21:51:31 -0500 Subject: [PATCH 22/58] Fix expected error/warning numbers --- .../v4/test/TestBasicSemanticErrors.java | 24 +++++------ .../org/antlr/v4/test/TestSymbolIssues.java | 27 +++++++------ .../antlr/v4/test/TestToolSyntaxErrors.java | 40 +++++++++---------- 3 files changed, 46 insertions(+), 45 deletions(-) diff --git a/tool/test/org/antlr/v4/test/TestBasicSemanticErrors.java b/tool/test/org/antlr/v4/test/TestBasicSemanticErrors.java index 1affdebcc..b7ffc81fd 100644 --- a/tool/test/org/antlr/v4/test/TestBasicSemanticErrors.java +++ b/tool/test/org/antlr/v4/test/TestBasicSemanticErrors.java @@ -50,18 +50,18 @@ public class TestBasicSemanticErrors extends BaseTest { "b : ( options { ick=bar; greedy=true; } : ID )+ ;\n" + "c : ID ID ;", // YIELDS - "warning(47): U.g:2:10: illegal option foo\n" + - "warning(47): U.g:2:19: illegal option k\n" + - ": U.g:4:8: token names must start with an uppercase letter: f\n" + - ": U.g:4:8: can't assign string value to token name f in non-combined grammar\n" + - ": U.g:5:8: can't assign string value to token name S in non-combined grammar\n" + - "warning(47): U.g:8:10: illegal option x\n" + - ": U.g:8:0: repeated grammar prequel spec (option, token, or import); please merge\n" + - ": U.g:7:0: repeated grammar prequel spec (option, token, or import); please merge\n" + - "warning(47): U.g:11:10: illegal option blech\n" + - "warning(47): U.g:11:21: illegal option greedy\n" + - "warning(47): U.g:14:16: illegal option ick\n" + - "warning(47): U.g:15:16: illegal option x\n", + "warning(48): U.g:2:10: illegal option foo\n" + + "warning(48): U.g:2:19: illegal option k\n" + + "error(26): U.g:4:8: token names must start with an uppercase letter: f\n" + + "error(25): U.g:4:8: can't assign string value to token name f in non-combined grammar\n" + + "error(25): U.g:5:8: can't assign string value to token name S in non-combined grammar\n" + + "warning(48): U.g:8:10: illegal option x\n" + + "error(20): U.g:8:0: repeated grammar prequel spec (option, token, or import); please merge\n" + + "error(20): U.g:7:0: repeated grammar prequel spec (option, token, or import); please merge\n" + + "warning(48): U.g:11:10: illegal option blech\n" + + "warning(48): U.g:11:21: illegal option greedy\n" + + "warning(48): U.g:14:16: illegal option ick\n" + + "warning(48): U.g:15:16: illegal option x\n", }; @Test public void testU() { super.testErrors(U, false); } diff --git a/tool/test/org/antlr/v4/test/TestSymbolIssues.java b/tool/test/org/antlr/v4/test/TestSymbolIssues.java index 2c56322d5..31b401640 100644 --- a/tool/test/org/antlr/v4/test/TestSymbolIssues.java +++ b/tool/test/org/antlr/v4/test/TestSymbolIssues.java @@ -20,8 +20,9 @@ public class TestSymbolIssues extends BaseTest { "\n" + "ID : 'a'..'z'+ ID ;", // YIELDS - "warning(51): A.g:2:10: illegal option opt\n" + - "error(60): A.g:7:1: redefinition of header action\n" + + "warning(48): A.g:2:10: illegal option opt\n" + + "warning(48): A.g:2:21: illegal option k\n" + + "error(59): A.g:7:1: redefinition of header action\n" + "warning(51): A.g:2:10: illegal option opt\n" + "error(19): A.g:11:0: rule a redefinition\n" + "error(60): A.g:5:1: redefinition of members action\n" + @@ -41,11 +42,11 @@ public class TestSymbolIssues extends BaseTest { "\n" + "s : FOO ;", // YIELDS - "error(26): B.g:2:9: can't assign string value to token name X in non-combined grammar\n" + - "error(36): B.g:4:4: label s conflicts with rule with same name\n" + - "error(36): B.g:4:9: label b conflicts with rule with same name\n" + - "error(37): B.g:4:15: label X conflicts with token with same name\n" + - "error(42): B.g:6:9: label x type mismatch with previous definition: TOKEN_LIST_LABEL!=TOKEN_LABEL\n" + "error(25): B.g:2:9: can't assign string value to token name X in non-combined grammar\n" + + "error(35): B.g:4:4: label s conflicts with rule with same name\n" + + "error(35): B.g:4:9: label b conflicts with rule with same name\n" + + "error(36): B.g:4:15: label X conflicts with token with same name\n" + + "error(40): B.g:6:9: label x type mismatch with previous definition: TOKEN_LIST_LABEL!=TOKEN_LABEL\n" }; static String[] D = { @@ -60,8 +61,8 @@ public class TestSymbolIssues extends BaseTest { " : ID ;", // YIELDS - "error(39): D.g:3:21: label j conflicts with rule a's return value or parameter with same name\n" + - "error(43): D.g:5:0: rule b's argument i conflicts a return value with same name\n" + "error(37): D.g:3:21: label j conflicts with rule a's return value or parameter with same name\n" + + "error(41): D.g:5:0: rule b's argument i conflicts a return value with same name\n" }; static String[] E = { @@ -77,10 +78,10 @@ public class TestSymbolIssues extends BaseTest { "a : A ;\n", // YIELDS - "error(74): E.g:4:8: cannot redefine B; token name already defined\n" + - "error(74): E.g:5:4: cannot redefine C; token name already defined\n" + - "error(74): E.g:6:8: cannot redefine D; token name already defined\n" + - "error(73): E.g:7:8: cannot alias X='e'; string already assigned to E\n" + "error(73): E.g:4:8: cannot redefine B; token name already defined\n" + + "error(73): E.g:5:4: cannot redefine C; token name already defined\n" + + "error(73): E.g:6:8: cannot redefine D; token name already defined\n" + + "error(72): E.g:7:8: cannot alias X='e'; string already assigned to E\n" }; @Test public void testA() { super.testErrors(A, false); } diff --git a/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java b/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java index 8c25bae88..2c66c496b 100644 --- a/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java +++ b/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java @@ -8,37 +8,37 @@ public class TestToolSyntaxErrors extends BaseTest { "grammar A;\n" + "", // YIELDS - "error(63): A.g::: grammar A has no rules\n", + "error(64): A.g::: grammar A has no rules\n", "A;", - "error(17): :1:0: 'A' came as a complete surprise to me\n", + "error(16): :1:0: 'A' came as a complete surprise to me\n", "grammar ;", - "error(17): :1:8: ';' came as a complete surprise to me while looking for an identifier\n", + "error(16): :1:8: ';' came as a complete surprise to me while looking for an identifier\n", "grammar A\n" + "a : ID ;\n", - "error(17): :2:0: missing SEMI at 'a'\n", + "error(16): :2:0: missing SEMI at 'a'\n", "grammar A;\n" + "a : ID ;;\n"+ "b : B ;", - "error(17): A.g:2:8: ';' came as a complete surprise to me\n", + "error(16): A.g:2:8: ';' came as a complete surprise to me\n", "grammar A;;\n" + "a : ID ;\n", - "error(17): A;.g:1:10: ';' came as a complete surprise to me\n", + "error(16): A;.g:1:10: ';' came as a complete surprise to me\n", "grammar A;\n" + "a @init : ID ;\n", - "error(17): A.g:2:8: mismatched input ':' expecting ACTION while matching rule preamble\n", + "error(16): A.g:2:8: mismatched input ':' expecting ACTION while matching rule preamble\n", "grammar A;\n" + "a ( A | B ) D ;\n" + "b : B ;", - ": A.g:2:3: '(' came as a complete surprise to me while matching rule preamble\n" + - ": A.g:2:11: mismatched input ')' expecting SEMI while matching a rule\n" + - ": A.g:2:15: mismatched input ';' expecting COLON while matching a lexer rule\n", + "error(16): A.g:2:3: '(' came as a complete surprise to me while matching rule preamble\n" + + "error(16): A.g:2:11: mismatched input ')' expecting SEMI while matching a rule\n" + + "error(16): A.g:2:15: mismatched input ';' expecting COLON while matching a lexer rule\n", }; @Test public void testA() { super.testErrors(A, true); } @@ -48,7 +48,7 @@ public class TestToolSyntaxErrors extends BaseTest { "grammar A;\n" + "a : : A ;\n" + "b : B ;", - "error(17): A.g:2:4: ':' came as a complete surprise to me while matching alternative\n", + "error(16): A.g:2:4: ':' came as a complete surprise to me while matching alternative\n", }; super.testErrors(pair, true); } @@ -58,7 +58,7 @@ public class TestToolSyntaxErrors extends BaseTest { "grammar A;\n" + "a : A \n" + "b : B ;", - "error(17): A.g:3:0: unterminated rule (missing ';') detected at 'b :' while looking for rule element\n", + "error(16): A.g:3:0: unterminated rule (missing ';') detected at 'b :' while looking for rule element\n", }; super.testErrors(pair, true); } @@ -68,7 +68,7 @@ public class TestToolSyntaxErrors extends BaseTest { "lexer grammar A;\n" + "A : 'a' \n" + "B : 'b' ;", - "error(17): A.g:3:0: unterminated rule (missing ';') detected at 'B :' while looking for lexer rule element\n", + "error(16): A.g:3:0: unterminated rule (missing ';') detected at 'B :' while looking for lexer rule element\n", }; super.testErrors(pair, true); } @@ -78,7 +78,7 @@ public class TestToolSyntaxErrors extends BaseTest { "grammar A;\n" + "a : A \n" + "b[int i] returns [int y] : B ;", - "error(17): A.g:3:9: unterminated rule (missing ';') detected at 'returns int y' while looking for rule element\n" + "error(16): A.g:3:9: unterminated rule (missing ';') detected at 'returns int y' while looking for rule element\n" }; super.testErrors(pair, true); } @@ -90,7 +90,7 @@ public class TestToolSyntaxErrors extends BaseTest { " catch [Exception e] {...}\n" + "b : B ;\n", - "error(17): A.g:2:4: unterminated rule (missing ';') detected at 'b catch' while looking for rule element\n" + "error(16): A.g:2:4: unterminated rule (missing ';') detected at 'b catch' while looking for rule element\n" }; super.testErrors(pair, true); } @@ -101,7 +101,7 @@ public class TestToolSyntaxErrors extends BaseTest { "a : A \n" + " catch [Exception e] {...}\n", - "error(17): A.g:2:4: unterminated rule (missing ';') detected at 'A catch' while looking for rule element\n" + "error(16): A.g:2:4: unterminated rule (missing ';') detected at 'A catch' while looking for rule element\n" }; super.testErrors(pair, true); } @@ -112,7 +112,7 @@ public class TestToolSyntaxErrors extends BaseTest { "a @ options {k=1;} : A ;\n" + "b : B ;", - "error(17): A.g:2:4: 'options {' came as a complete surprise to me while looking for an identifier\n" + "error(16): A.g:2:4: 'options {' came as a complete surprise to me while looking for an identifier\n" }; super.testErrors(pair, true); } @@ -123,7 +123,7 @@ public class TestToolSyntaxErrors extends BaseTest { "a } : A ;\n" + "b : B ;", - "error(17): A.g:2:2: '}' came as a complete surprise to me while matching rule preamble\n" + "error(16): A.g:2:2: '}' came as a complete surprise to me while matching rule preamble\n" }; super.testErrors(pair, true); } @@ -135,8 +135,8 @@ public class TestToolSyntaxErrors extends BaseTest { "mode foo;\n" + "b : B ;", - ": A.g:4:0: 'b' came as a complete surprise to me\n" + - ": A.g:4:6: mismatched input ';' expecting COLON while matching a lexer rule\n" + "error(16): A.g:4:0: 'b' came as a complete surprise to me\n" + + "error(16): A.g:4:6: mismatched input ';' expecting COLON while matching a lexer rule\n" }; super.testErrors(pair, true); } From 8d16912fb99149fd303c4317fad6203eb58215ec Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 27 Mar 2012 21:51:55 -0500 Subject: [PATCH 23/58] Fix expected output for testAmbigLR --- tool/test/org/antlr/v4/test/TestLeftRecursion.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tool/test/org/antlr/v4/test/TestLeftRecursion.java b/tool/test/org/antlr/v4/test/TestLeftRecursion.java index 205f737c8..5f9eaa35a 100644 --- a/tool/test/org/antlr/v4/test/TestLeftRecursion.java +++ b/tool/test/org/antlr/v4/test/TestLeftRecursion.java @@ -323,10 +323,18 @@ public class TestLeftRecursion extends BaseTest { assertNull(stderrDuringParse); result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "a+b*2\n", true); - assertNull(stderrDuringParse); + assertEquals("line 1:1 reportAttemptingFullContext d=3, input='+'\n" + + "line 1:1 reportContextSensitivity d=3, input='+'\n" + + "line 1:3 reportAttemptingFullContext d=3, input='*'\n" + + "line 1:3 reportAmbiguity d=3: ambigAlts={1..2}, input='*'\n", + stderrDuringParse); result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "(1+2)*3\n", true); - assertNull(stderrDuringParse); + assertEquals("line 1:2 reportAttemptingFullContext d=3, input='+'\n" + + "line 1:2 reportContextSensitivity d=3, input='+'\n" + + "line 1:5 reportAttemptingFullContext d=3, input='*'\n" + + "line 1:5 reportContextSensitivity d=3, input='*'\n", + stderrDuringParse); } public void runTests(String grammar, String[] tests, String startRule) { From cc7e9c1b0c0675fb385f0e3db3f439ca512c079f Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 27 Mar 2012 21:52:34 -0500 Subject: [PATCH 24/58] Don't strip error numbers from lines because the numbers are constants of the public API --- tool/test/org/antlr/v4/test/BaseTest.java | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/tool/test/org/antlr/v4/test/BaseTest.java b/tool/test/org/antlr/v4/test/BaseTest.java index 66da039e0..3376246cf 100644 --- a/tool/test/org/antlr/v4/test/BaseTest.java +++ b/tool/test/org/antlr/v4/test/BaseTest.java @@ -639,29 +639,10 @@ public abstract class BaseTest { msg = msg.replaceAll("\r","\\\\r"); msg = msg.replaceAll("\t","\\\\t"); - // ignore error number - if ( expect!=null ) expect = stripErrorNum(expect); - actual = stripErrorNum(actual); assertEquals("error in: "+msg,expect,actual); } } - // can be multi-line - //error(29): A.g:2:11: unknown attribute reference a in $a - //error(29): A.g:2:11: unknown attribute reference a in $a - String stripErrorNum(String errs) { - String[] lines = errs.split("\n"); - for (int i=0; i=0 && rp>=0 ) { - lines[i] = s.substring(0, lp) + s.substring(rp+1, s.length()); - } - } - return Utils.join(lines, "\n"); - } - public String getFilenameFromFirstLineOfGrammar(String line) { String fileName = ""; int grIndex = line.lastIndexOf("grammar"); From 447a5620f0feddebf64f14d943dddf7641d3b968 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Wed, 28 Mar 2012 11:07:23 -0700 Subject: [PATCH 25/58] add -diagnostics option to test rig. --- .../Java/src/org/antlr/v4/runtime/misc/TestRig.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java b/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java index 5b723b7e3..f78d090f5 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java @@ -53,6 +53,7 @@ import java.lang.reflect.Method; * [-print] * [-tokens] [-gui] [-ps file.ps] * [-trace] + * [-diagnostics] * [input-filename] */ public class TestRig { @@ -65,10 +66,12 @@ public class TestRig { String psFile = null; boolean showTokens = false; boolean trace = false; + boolean diagnostics = false; String encoding = null; if ( args.length < 2 ) { System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName" + - " [-tokens] [-print] [-gui] [-ps file.ps] [-encoding encodingname] [-trace]"+ + " [-tokens] [-print] [-gui] [-ps file.ps] [-encoding encodingname]" + + " [-trace] [-diagnostics]"+ " [input-filename]"); return; } @@ -96,6 +99,9 @@ public class TestRig { else if ( arg.equals("-trace") ) { trace = true; } + else if ( arg.equals("-diagnostics") ) { + diagnostics = true; + } else if ( arg.equals("-encoding") ) { if ( i>=args.length ) { System.err.println("missing encoding on -encoding"); @@ -155,7 +161,7 @@ public class TestRig { Constructor parserCtor = parserClass.getConstructor(TokenStream.class); Parser parser = parserCtor.newInstance(tokens); - parser.addErrorListener(new DiagnosticErrorListener()); + if ( diagnostics ) parser.addErrorListener(new DiagnosticErrorListener()); if ( printTree || gui || psFile!=null ) { parser.setBuildParseTree(true); From 42706485e97d5b7b320c8532ef09890302366fc0 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Thu, 29 Mar 2012 14:59:43 -0700 Subject: [PATCH 26/58] was looking for imports with .g not .g4 --- tool/src/org/antlr/v4/tool/Grammar.java | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/tool/src/org/antlr/v4/tool/Grammar.java b/tool/src/org/antlr/v4/tool/Grammar.java index bf30b8a9d..76f0f7496 100644 --- a/tool/src/org/antlr/v4/tool/Grammar.java +++ b/tool/src/org/antlr/v4/tool/Grammar.java @@ -49,11 +49,24 @@ import org.antlr.v4.runtime.misc.IntSet; import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.misc.Nullable; -import org.antlr.v4.tool.ast.*; +import org.antlr.v4.tool.ast.ActionAST; +import org.antlr.v4.tool.ast.GrammarAST; +import org.antlr.v4.tool.ast.GrammarASTErrorNode; +import org.antlr.v4.tool.ast.GrammarASTWithOptions; +import org.antlr.v4.tool.ast.GrammarRootAST; +import org.antlr.v4.tool.ast.PredAST; +import org.antlr.v4.tool.ast.TerminalAST; import java.io.File; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; public class Grammar implements AttributeResolver { public static final String GRAMMAR_FROM_STRING_NAME = ""; @@ -234,17 +247,18 @@ public class Grammar implements AttributeResolver { } GrammarAST grammarAST = null; try { - grammarAST = tool.loadImportedGrammar(this, importedGrammarName + ".g"); + grammarAST = tool.loadImportedGrammar(this, importedGrammarName + ".g4"); } catch (IOException ioe) { - tool.errMgr.toolError(ErrorType.CANNOT_FIND_IMPORTED_FILE, ioe, importedGrammarName+".g"); + tool.errMgr.toolError(ErrorType.CANNOT_FIND_IMPORTED_FILE, ioe, + importedGrammarName+".g"); continue; } // did it come back as error node or missing? if ( grammarAST==null || grammarAST instanceof GrammarASTErrorNode) return; GrammarRootAST ast = (GrammarRootAST)grammarAST; Grammar g = tool.createGrammar(ast); - File f = tool.getImportedGrammarFile(this, importedGrammarName+".g"); + File f = tool.getImportedGrammarFile(this, importedGrammarName+".g4"); g.fileName = f.getAbsolutePath(); g.parent = this; importedGrammars.add(g); From 1b1ff9915d64ddc0a572208c0d0f22e692947a09 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Thu, 29 Mar 2012 15:51:57 -0700 Subject: [PATCH 27/58] tweak to Gen.java so it looks in correct templates area. --- gunit/src/org/antlr/v4/gunit/Gen.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/gunit/src/org/antlr/v4/gunit/Gen.java b/gunit/src/org/antlr/v4/gunit/Gen.java index 804c59e2c..39ceb705f 100644 --- a/gunit/src/org/antlr/v4/gunit/Gen.java +++ b/gunit/src/org/antlr/v4/gunit/Gen.java @@ -1,6 +1,8 @@ package org.antlr.v4.gunit; -import org.antlr.runtime.*; +import org.antlr.runtime.ANTLRFileStream; +import org.antlr.runtime.CommonTokenStream; +import org.antlr.runtime.RuleReturnScope; import org.antlr.runtime.tree.BufferedTreeNodeStream; import org.antlr.runtime.tree.CommonTree; import org.antlr.runtime.tree.CommonTreeNodeStream; @@ -8,17 +10,19 @@ import org.antlr.stringtemplate.AutoIndentWriter; import org.antlr.stringtemplate.StringTemplate; import org.antlr.stringtemplate.StringTemplateGroup; -import java.io.*; +import java.io.BufferedWriter; +import java.io.FileReader; +import java.io.FileWriter; import java.util.ArrayList; import java.util.List; public class Gen { // TODO: don't hardcode public static final String TEMPLATE_FILE = - "/Users/parrt/antlr/code/antlr4/main/gunit/resources/org/antlr/v4/gunit/jUnit.stg"; + "/Users/parrt/antlr/code/antlr4/gunit/resources/org/antlr/v4/gunit/jUnit.stg"; public static void main(String[] args) throws Exception { - if ( args.length==0 ) System.exit(0); + if ( args.length==0 ) {help(); System.exit(0);} String outputDirName = "."; String fileName = args[0]; if ( args[0].equals("-o") ) { From 11cd720cc674fed7f0111be2e299d09283059216 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Thu, 29 Mar 2012 21:37:56 -0700 Subject: [PATCH 28/58] added failing test so i go back and fix issue with op=('+'|'-') in LR rules --- .../org/antlr/v4/test/TestLeftRecursion.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tool/test/org/antlr/v4/test/TestLeftRecursion.java b/tool/test/org/antlr/v4/test/TestLeftRecursion.java index 5f9eaa35a..854461ab6 100644 --- a/tool/test/org/antlr/v4/test/TestLeftRecursion.java +++ b/tool/test/org/antlr/v4/test/TestLeftRecursion.java @@ -228,6 +228,26 @@ public class TestLeftRecursion extends BaseTest { runTests(grammar, tests, "s"); } + @Test public void testLabelsOnOpSubrule() throws Exception { + // FAILS + String grammar = + "grammar T;\n" + + "s : e {System.out.println($e.v);} ;\n" + + "e returns [int v, List ignored]\n" + + " : a=e op=('*'|'/') b=e {$v = $a.v * $b.v;}\n" + + " | INT {$v = $INT.int;}\n" + + " | '(' x=e ')' {$v = $x.v;}\n" + + " ;\n" + + "INT : '0'..'9'+ ;\n" + + "WS : (' '|'\\n') {skip();} ;\n"; + String[] tests = { + "4", "4", + "1*2/3", "7", + "(1/2)*3", "9", + }; + runTests(grammar, tests, "s"); + } + @Test public void testReturnValueAndActionsAndLabels() throws Exception { String grammar = "grammar T;\n" + From cfcb2a1a55defd310519ca12564cd74ea59e8422 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Fri, 30 Mar 2012 11:36:20 -0500 Subject: [PATCH 29/58] Add Tool.GRAMMAR_EXTENSION (.g4) and Tool.LEGACY_GRAMMAR_EXTENSION (.g), allow imported grammars to use either extension (favoring .g4 over .g). --- tool/src/org/antlr/v4/Tool.java | 34 +++++++++++++++++++---- tool/src/org/antlr/v4/tool/ErrorType.java | 2 +- tool/src/org/antlr/v4/tool/Grammar.java | 14 ++++------ 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/tool/src/org/antlr/v4/Tool.java b/tool/src/org/antlr/v4/Tool.java index 8a81148fc..2a9ae8158 100644 --- a/tool/src/org/antlr/v4/Tool.java +++ b/tool/src/org/antlr/v4/Tool.java @@ -70,6 +70,7 @@ import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.List; @@ -77,6 +78,12 @@ import java.util.List; public class Tool { public String VERSION = "4.0-"+new Date(); + public static final String GRAMMAR_EXTENSION = ".g4"; + public static final String LEGACY_GRAMMAR_EXTENSION = ".g"; + + public static final List ALL_GRAMMAR_EXTENSIONS = + Collections.unmodifiableList(Arrays.asList(GRAMMAR_EXTENSION, LEGACY_GRAMMAR_EXTENSION)); + public static enum OptionArgType { NONE, STRING } // NONE implies boolean public static class Option { String fieldName; @@ -384,16 +391,31 @@ public class Tool { return null; } - /** Try current dir then dir of g then lib dir */ - public GrammarRootAST loadImportedGrammar(Grammar g, String fileName) throws IOException { - g.tool.log("grammar", "load "+fileName + " from " + g.fileName); - File importedFile = getImportedGrammarFile(g, fileName); + /** + * Try current dir then dir of g then lib dir + * @param g + * @param name The imported grammar name. + */ + public Grammar loadImportedGrammar(Grammar g, String name) throws IOException { + g.tool.log("grammar", "load " + name + " from " + g.fileName); + File importedFile = null; + for (String extension : ALL_GRAMMAR_EXTENSIONS) { + importedFile = getImportedGrammarFile(g, name + extension); + if (importedFile != null) { + break; + } + } + if ( importedFile==null ) { - errMgr.toolError(ErrorType.CANNOT_FIND_IMPORTED_FILE, fileName, g.fileName); + errMgr.toolError(ErrorType.CANNOT_FIND_IMPORTED_GRAMMAR, name, g.fileName); return null; } + ANTLRFileStream in = new ANTLRFileStream(importedFile.getAbsolutePath()); - return load(in); + GrammarRootAST root = load(in); + Grammar imported = createGrammar(root); + imported.fileName = importedFile.getAbsolutePath(); + return imported; } public GrammarRootAST loadFromString(String grammar) { diff --git a/tool/src/org/antlr/v4/tool/ErrorType.java b/tool/src/org/antlr/v4/tool/ErrorType.java index c1aeaae20..dde0e9aa7 100644 --- a/tool/src/org/antlr/v4/tool/ErrorType.java +++ b/tool/src/org/antlr/v4/tool/ErrorType.java @@ -129,7 +129,7 @@ public enum ErrorType { //TOKEN_VOCAB_IN_DELEGATE(, "tokenVocab option ignored in imported grammar ", ErrorSeverity.ERROR), OPTIONS_IN_DELEGATE(109, "options ignored in imported grammar ", ErrorSeverity.WARNING), // TOKEN_ALIAS_IN_DELEGATE(, "can't assign string to token name to string in imported grammar ", ErrorSeverity.ERROR), - CANNOT_FIND_IMPORTED_FILE(110, "can't find or load grammar from ", ErrorSeverity.ERROR), + CANNOT_FIND_IMPORTED_GRAMMAR(110, "can't find or load grammar from ", ErrorSeverity.ERROR), INVALID_IMPORT(111, " grammar cannot import grammar ", ErrorSeverity.ERROR), IMPORTED_TOKENS_RULE_EMPTY(112, "", ErrorSeverity.ERROR), IMPORT_NAME_CLASH(113, " grammar and imported grammar both generate ", ErrorSeverity.ERROR), diff --git a/tool/src/org/antlr/v4/tool/Grammar.java b/tool/src/org/antlr/v4/tool/Grammar.java index 76f0f7496..960c71bf3 100644 --- a/tool/src/org/antlr/v4/tool/Grammar.java +++ b/tool/src/org/antlr/v4/tool/Grammar.java @@ -245,21 +245,17 @@ public class Grammar implements AttributeResolver { importedGrammarName = t.getText(); tool.log("grammar", "import " + t.getText()); } - GrammarAST grammarAST = null; + Grammar g; try { - grammarAST = tool.loadImportedGrammar(this, importedGrammarName + ".g4"); + g = tool.loadImportedGrammar(this, importedGrammarName); } catch (IOException ioe) { - tool.errMgr.toolError(ErrorType.CANNOT_FIND_IMPORTED_FILE, ioe, - importedGrammarName+".g"); + tool.errMgr.toolError(ErrorType.CANNOT_FIND_IMPORTED_GRAMMAR, ioe, + importedGrammarName); continue; } // did it come back as error node or missing? - if ( grammarAST==null || grammarAST instanceof GrammarASTErrorNode) return; - GrammarRootAST ast = (GrammarRootAST)grammarAST; - Grammar g = tool.createGrammar(ast); - File f = tool.getImportedGrammarFile(this, importedGrammarName+".g4"); - g.fileName = f.getAbsolutePath(); + if ( g == null ) continue; g.parent = this; importedGrammars.add(g); g.loadImportedGrammars(); // recursively pursue any imports in this import From e5d7c27b09566985d1f1604f2e636eb6a0465c4a Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Fri, 30 Mar 2012 11:37:22 -0500 Subject: [PATCH 30/58] Updated comments to indicate preference of .g4 over .g --- .../org/antlr/v4/runtime/misc/Interval.java | 2 +- tool/playground/TestJava.java | 2 +- tool/playground/TestYang.java | 2 +- tool/src/org/antlr/v4/Tool.java | 16 +++++++-------- .../org/antlr/v4/codegen/CodeGenerator.java | 2 +- .../org/antlr/v4/parse/TokenVocabParser.java | 2 +- tool/src/org/antlr/v4/tool/ErrorType.java | 2 +- .../antlr/v4/test/TestTopologicalSort.java | 20 +++++++++---------- 8 files changed, 24 insertions(+), 24 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java b/runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java index 6f7b4257b..1e6446b60 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/Interval.java @@ -49,7 +49,7 @@ public class Interval { /** Interval objects are used readonly so share all with the * same single value a==b up to some max size. Use an array as a perfect hash. * Return shared object for 0..INTERVAL_POOL_MAX_VALUE or a new - * Interval object with a..a in it. On Java.g, 218623 IntervalSets + * Interval object with a..a in it. On Java.g4, 218623 IntervalSets * have a..a (set with 1 element). */ public static Interval of(int a, int b) { diff --git a/tool/playground/TestJava.java b/tool/playground/TestJava.java index 33f5990ab..d70c336e1 100644 --- a/tool/playground/TestJava.java +++ b/tool/playground/TestJava.java @@ -7,7 +7,7 @@ import org.antlr.v4.runtime.atn.ParserATNSimulator; import java.io.File; /** Parse a java file or directory of java files using the generated parser - * ANTLR builds from java.g + * ANTLR builds from java.g4 */ class TestJava { public static long lexerTime = 0; diff --git a/tool/playground/TestYang.java b/tool/playground/TestYang.java index 2862a7a80..dbf4860e4 100644 --- a/tool/playground/TestYang.java +++ b/tool/playground/TestYang.java @@ -7,7 +7,7 @@ import org.antlr.v4.runtime.atn.ParserATNSimulator; import java.io.File; /** Parse a java file or directory of java files using the generated parser - * ANTLR builds from java.g + * ANTLR builds from java.g4 */ class TestYang { public static long lexerTime = 0; diff --git a/tool/src/org/antlr/v4/Tool.java b/tool/src/org/antlr/v4/Tool.java index 2a9ae8158..dad9c8bab 100644 --- a/tool/src/org/antlr/v4/Tool.java +++ b/tool/src/org/antlr/v4/Tool.java @@ -471,13 +471,13 @@ public class Tool { * files. If the outputDir set by -o is not present it will be created. * The final filename is sensitive to the output directory and * the directory where the grammar file was found. If -o is /tmp - * and the original grammar file was foo/t.g then output files + * and the original grammar file was foo/t.g4 then output files * go in /tmp/foo. * * The output dir -o spec takes precedence if it's absolute. * E.g., if the grammar file dir is absolute the output dir is given - * precendence. "-o /tmp /usr/lib/t.g" results in "/tmp/T.java" as - * output (assuming t.g holds T.java). + * precendence. "-o /tmp /usr/lib/t.g4" results in "/tmp/T.java" as + * output (assuming t.g4 holds T.java). * * If no -o is specified, then just write to the directory where the * grammar file was found. @@ -489,7 +489,7 @@ public class Tool { return new StringWriter(); } // output directory is a function of where the grammar file lives - // for subdir/T.g, you get subdir here. Well, depends on -o etc... + // for subdir/T.g4, you get subdir here. Well, depends on -o etc... // But, if this is a .tokens file, then we force the output to // be the base output directory (or current directory if there is not a -o) // @@ -555,9 +555,9 @@ public class Tool { fileDirectory = fileNameWithPath.substring(0, fileNameWithPath.lastIndexOf(File.separatorChar)); } if ( haveOutputDir ) { - // -o /tmp /var/lib/t.g => /tmp/T.java - // -o subdir/output /usr/lib/t.g => subdir/output/T.java - // -o . /usr/lib/t.g => ./T.java + // -o /tmp /var/lib/t.g4 => /tmp/T.java + // -o subdir/output /usr/lib/t.g4 => subdir/output/T.java + // -o . /usr/lib/t.g4 => ./T.java if (fileDirectory != null && (new File(fileDirectory).isAbsolute() || fileDirectory.startsWith("~"))) { // isAbsolute doesn't count this :( @@ -565,7 +565,7 @@ public class Tool { outputDir = new File(outputDirectory); } else { - // -o /tmp subdir/t.g => /tmp/subdir/t.g + // -o /tmp subdir/t.g4 => /tmp/subdir/t.g4 if (fileDirectory != null) { outputDir = new File(outputDirectory, fileDirectory); } diff --git a/tool/src/org/antlr/v4/codegen/CodeGenerator.java b/tool/src/org/antlr/v4/codegen/CodeGenerator.java index c88ebee2e..b03bff05a 100644 --- a/tool/src/org/antlr/v4/codegen/CodeGenerator.java +++ b/tool/src/org/antlr/v4/codegen/CodeGenerator.java @@ -257,7 +257,7 @@ public class CodeGenerator { } } - /** Generate TParser.java and TLexer.java from T.g if combined, else + /** Generate TParser.java and TLexer.java from T.g4 if combined, else * just use T.java as output regardless of type. */ public String getRecognizerFileName() { diff --git a/tool/src/org/antlr/v4/parse/TokenVocabParser.java b/tool/src/org/antlr/v4/parse/TokenVocabParser.java index 29fee6d0a..67c4f9853 100644 --- a/tool/src/org/antlr/v4/parse/TokenVocabParser.java +++ b/tool/src/org/antlr/v4/parse/TokenVocabParser.java @@ -137,7 +137,7 @@ public class TokenVocabParser { } /** Return a File descriptor for vocab file. Look in library or - * in -o output path. antlr -o foo T.g U.g where U needs T.tokens + * in -o output path. antlr -o foo T.g4 U.g4 where U needs T.tokens * won't work unless we look in foo too. If we do not find the * file in the lib directory then must assume that the .tokens file * is going to be generated as part of this build and we have defined diff --git a/tool/src/org/antlr/v4/tool/ErrorType.java b/tool/src/org/antlr/v4/tool/ErrorType.java index dde0e9aa7..3d406d9dd 100644 --- a/tool/src/org/antlr/v4/tool/ErrorType.java +++ b/tool/src/org/antlr/v4/tool/ErrorType.java @@ -149,7 +149,7 @@ public enum ErrorType { // Dependency sorting errors // - /** t1.g -> t2.g -> t3.g ->t1.g */ + /** t1.g4 -> t2.g4 -> t3.g4 ->t1.g4 */ CIRCULAR_DEPENDENCY(130, "your grammars contain a circular dependency and cannot be sorted into a valid build order", ErrorSeverity.ERROR), // Simple informational messages diff --git a/tool/test/org/antlr/v4/test/TestTopologicalSort.java b/tool/test/org/antlr/v4/test/TestTopologicalSort.java index 6b23a7a23..0e5044f52 100644 --- a/tool/test/org/antlr/v4/test/TestTopologicalSort.java +++ b/tool/test/org/antlr/v4/test/TestTopologicalSort.java @@ -87,12 +87,12 @@ public class TestTopologicalSort extends BaseTest { @Test public void testSimpleTokenDependence() throws Exception { Graph g = new Graph(); - g.addEdge("Java.g", "MyJava.tokens"); // Java feeds off manual token file - g.addEdge("Java.tokens", "Java.g"); - g.addEdge("Def.g", "Java.tokens"); // walkers feed off generated tokens - g.addEdge("Ref.g", "Java.tokens"); + g.addEdge("Java.g4", "MyJava.tokens"); // Java feeds off manual token file + g.addEdge("Java.tokens", "Java.g4"); + g.addEdge("Def.g4", "Java.tokens"); // walkers feed off generated tokens + g.addEdge("Ref.g4", "Java.tokens"); - String expecting = "[MyJava.tokens, Java.g, Java.tokens, Ref.g, Def.g]"; + String expecting = "[MyJava.tokens, Java.g4, Java.tokens, Ref.g4, Def.g4]"; List nodes = g.sort(); String result = nodes.toString(); assertEquals(expecting, result); @@ -101,12 +101,12 @@ public class TestTopologicalSort extends BaseTest { @Test public void testParserLexerCombo() throws Exception { Graph g = new Graph(); - g.addEdge("JavaLexer.tokens", "JavaLexer.g"); - g.addEdge("JavaParser.g", "JavaLexer.tokens"); - g.addEdge("Def.g", "JavaLexer.tokens"); - g.addEdge("Ref.g", "JavaLexer.tokens"); + g.addEdge("JavaLexer.tokens", "JavaLexer.g4"); + g.addEdge("JavaParser.g4", "JavaLexer.tokens"); + g.addEdge("Def.g4", "JavaLexer.tokens"); + g.addEdge("Ref.g4", "JavaLexer.tokens"); - String expecting = "[JavaLexer.g, JavaLexer.tokens, JavaParser.g, Ref.g, Def.g]"; + String expecting = "[JavaLexer.g4, JavaLexer.tokens, JavaParser.g4, Ref.g4, Def.g4]"; List nodes = g.sort(); String result = nodes.toString(); assertEquals(expecting, result); From f166df7b9401cd85f9f52dc50f40c8c76e4d01b3 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Fri, 30 Mar 2012 11:44:34 -0500 Subject: [PATCH 31/58] Updated unit tests to use .g4 as the preferred grammar extension --- tool/test/org/antlr/v4/test/BaseTest.java | 4 +- .../antlr/v4/test/{Java-LR.g => Java-LR.g4} | 0 .../org/antlr/v4/test/{Java.g => Java.g4} | 0 .../antlr/v4/test/TestAttributeChecks.java | 130 ++++++++-------- .../v4/test/TestBasicSemanticErrors.java | 24 +-- .../antlr/v4/test/TestCompositeGrammars.java | 140 +++++++++--------- .../antlr/v4/test/TestFullContextParsing.java | 22 +-- .../org/antlr/v4/test/TestLeftRecursion.java | 10 +- .../org/antlr/v4/test/TestLexerErrors.java | 16 +- .../test/org/antlr/v4/test/TestLexerExec.java | 52 +++---- .../test/org/antlr/v4/test/TestListeners.java | 14 +- .../org/antlr/v4/test/TestNonGreedyLoops.java | 66 ++++----- .../org/antlr/v4/test/TestParseErrors.java | 40 ++--- .../org/antlr/v4/test/TestParseTrees.java | 16 +- .../org/antlr/v4/test/TestParserExec.java | 34 ++--- .../org/antlr/v4/test/TestPerformance.java | 8 +- .../antlr/v4/test/TestSemPredEvalLexer.java | 6 +- .../antlr/v4/test/TestSemPredEvalParser.java | 38 ++--- tool/test/org/antlr/v4/test/TestSets.java | 50 +++---- .../org/antlr/v4/test/TestSymbolIssues.java | 40 ++--- .../v4/test/TestTokenTypeAssignment.java | 4 +- .../antlr/v4/test/TestToolSyntaxErrors.java | 34 ++--- 22 files changed, 374 insertions(+), 374 deletions(-) rename tool/test/org/antlr/v4/test/{Java-LR.g => Java-LR.g4} (100%) rename tool/test/org/antlr/v4/test/{Java.g => Java.g4} (100%) diff --git a/tool/test/org/antlr/v4/test/BaseTest.java b/tool/test/org/antlr/v4/test/BaseTest.java index 3376246cf..08fe5b8a2 100644 --- a/tool/test/org/antlr/v4/test/BaseTest.java +++ b/tool/test/org/antlr/v4/test/BaseTest.java @@ -649,9 +649,9 @@ public abstract class BaseTest { int semi = line.lastIndexOf(';'); if ( grIndex>=0 && semi>=0 ) { int space = line.indexOf(' ', grIndex); - fileName = line.substring(space+1, semi)+".g"; + fileName = line.substring(space+1, semi)+Tool.GRAMMAR_EXTENSION; } - if ( fileName.length()==".g".length() ) fileName = ""; + if ( fileName.length()==Tool.GRAMMAR_EXTENSION.length() ) fileName = ""; return fileName; } diff --git a/tool/test/org/antlr/v4/test/Java-LR.g b/tool/test/org/antlr/v4/test/Java-LR.g4 similarity index 100% rename from tool/test/org/antlr/v4/test/Java-LR.g rename to tool/test/org/antlr/v4/test/Java-LR.g4 diff --git a/tool/test/org/antlr/v4/test/Java.g b/tool/test/org/antlr/v4/test/Java.g4 similarity index 100% rename from tool/test/org/antlr/v4/test/Java.g rename to tool/test/org/antlr/v4/test/Java.g4 diff --git a/tool/test/org/antlr/v4/test/TestAttributeChecks.java b/tool/test/org/antlr/v4/test/TestAttributeChecks.java index 80eb58bf0..071ab27aa 100644 --- a/tool/test/org/antlr/v4/test/TestAttributeChecks.java +++ b/tool/test/org/antlr/v4/test/TestAttributeChecks.java @@ -24,8 +24,8 @@ public class TestAttributeChecks extends BaseTest { "d : ;\n"; String[] membersChecks = { - "$a", "error(29): A.g:2:11: unknown attribute reference a in $a\n", - "$a.y", "error(29): A.g:2:11: unknown attribute reference a in $a.y\n", + "$a", "error(29): A.g4:2:11: unknown attribute reference a in $a\n", + "$a.y", "error(29): A.g4:2:11: unknown attribute reference a in $a.y\n", }; String[] initChecks = { @@ -36,8 +36,8 @@ public class TestAttributeChecks extends BaseTest { "$lab.e", "", "$ids", "", - "$c", "error(29): A.g:4:8: unknown attribute reference c in $c\n", - "$a.q", "error(31): A.g:4:10: unknown attribute q for rule a in $a.q\n", + "$c", "error(29): A.g4:4:8: unknown attribute reference c in $c\n", + "$a.q", "error(31): A.g4:4:10: unknown attribute q for rule a in $a.q\n", }; String[] inlineChecks = { @@ -58,19 +58,19 @@ public class TestAttributeChecks extends BaseTest { }; String[] bad_inlineChecks = { - "$lab", "error(33): A.g:6:4: missing attribute access on rule reference lab in $lab\n", - "$q", "error(29): A.g:6:4: unknown attribute reference q in $q\n", - "$q.y", "error(29): A.g:6:4: unknown attribute reference q in $q.y\n", - "$q = 3", "error(29): A.g:6:4: unknown attribute reference q in $q\n", - "$q = 3;", "error(29): A.g:6:4: unknown attribute reference q in $q = 3;\n", - "$q.y = 3;", "error(29): A.g:6:4: unknown attribute reference q in $q.y = 3;\n", - "$q = $blort;", "error(29): A.g:6:4: unknown attribute reference q in $q = $blort;\n" + - "error(29): A.g:6:9: unknown attribute reference blort in $blort\n", - "$a.ick", "error(31): A.g:6:6: unknown attribute ick for rule a in $a.ick\n", - "$a.ick = 3;", "error(31): A.g:6:6: unknown attribute ick for rule a in $a.ick = 3;\n", - "$b.d", "error(30): A.g:6:6: cannot access rule d's parameter: $b.d\n", // can't see rule ref's arg - "$d.text", "error(29): A.g:6:4: unknown attribute reference d in $d.text\n", // valid rule, but no ref - "$lab.d", "error(30): A.g:6:8: cannot access rule d's parameter: $lab.d\n", + "$lab", "error(33): A.g4:6:4: missing attribute access on rule reference lab in $lab\n", + "$q", "error(29): A.g4:6:4: unknown attribute reference q in $q\n", + "$q.y", "error(29): A.g4:6:4: unknown attribute reference q in $q.y\n", + "$q = 3", "error(29): A.g4:6:4: unknown attribute reference q in $q\n", + "$q = 3;", "error(29): A.g4:6:4: unknown attribute reference q in $q = 3;\n", + "$q.y = 3;", "error(29): A.g4:6:4: unknown attribute reference q in $q.y = 3;\n", + "$q = $blort;", "error(29): A.g4:6:4: unknown attribute reference q in $q = $blort;\n" + + "error(29): A.g4:6:9: unknown attribute reference blort in $blort\n", + "$a.ick", "error(31): A.g4:6:6: unknown attribute ick for rule a in $a.ick\n", + "$a.ick = 3;", "error(31): A.g4:6:6: unknown attribute ick for rule a in $a.ick = 3;\n", + "$b.d", "error(30): A.g4:6:6: cannot access rule d's parameter: $b.d\n", // can't see rule ref's arg + "$d.text", "error(29): A.g4:6:4: unknown attribute reference d in $d.text\n", // valid rule, but no ref + "$lab.d", "error(30): A.g4:6:8: cannot access rule d's parameter: $lab.d\n", }; String[] finallyChecks = { @@ -84,20 +84,20 @@ public class TestAttributeChecks extends BaseTest { "$id.text", "", "$ids", "", - "$lab", "error(33): A.g:9:14: missing attribute access on rule reference lab in $lab\n", - "$q", "error(29): A.g:9:14: unknown attribute reference q in $q\n", - "$q.y", "error(29): A.g:9:14: unknown attribute reference q in $q.y\n", - "$q = 3", "error(29): A.g:9:14: unknown attribute reference q in $q\n", - "$q = 3;", "error(29): A.g:9:14: unknown attribute reference q in $q = 3;\n", - "$q.y = 3;", "error(29): A.g:9:14: unknown attribute reference q in $q.y = 3;\n", - "$q = $blort;", "error(29): A.g:9:14: unknown attribute reference q in $q = $blort;\n" + - "error(29): A.g:9:19: unknown attribute reference blort in $blort\n", - "$a.ick", "error(31): A.g:9:16: unknown attribute ick for rule a in $a.ick\n", - "$a.ick = 3;", "error(31): A.g:9:16: unknown attribute ick for rule a in $a.ick = 3;\n", - "$b.e", "error(29): A.g:9:14: unknown attribute reference b in $b.e\n", // can't see rule refs outside alts - "$b.d", "error(29): A.g:9:14: unknown attribute reference b in $b.d\n", - "$c.text", "error(29): A.g:9:14: unknown attribute reference c in $c.text\n", - "$lab.d", "error(30): A.g:9:18: cannot access rule d's parameter: $lab.d\n", + "$lab", "error(33): A.g4:9:14: missing attribute access on rule reference lab in $lab\n", + "$q", "error(29): A.g4:9:14: unknown attribute reference q in $q\n", + "$q.y", "error(29): A.g4:9:14: unknown attribute reference q in $q.y\n", + "$q = 3", "error(29): A.g4:9:14: unknown attribute reference q in $q\n", + "$q = 3;", "error(29): A.g4:9:14: unknown attribute reference q in $q = 3;\n", + "$q.y = 3;", "error(29): A.g4:9:14: unknown attribute reference q in $q.y = 3;\n", + "$q = $blort;", "error(29): A.g4:9:14: unknown attribute reference q in $q = $blort;\n" + + "error(29): A.g4:9:19: unknown attribute reference blort in $blort\n", + "$a.ick", "error(31): A.g4:9:16: unknown attribute ick for rule a in $a.ick\n", + "$a.ick = 3;", "error(31): A.g4:9:16: unknown attribute ick for rule a in $a.ick = 3;\n", + "$b.e", "error(29): A.g4:9:14: unknown attribute reference b in $b.e\n", // can't see rule refs outside alts + "$b.d", "error(29): A.g4:9:14: unknown attribute reference b in $b.d\n", + "$c.text", "error(29): A.g4:9:14: unknown attribute reference c in $c.text\n", + "$lab.d", "error(30): A.g4:9:18: cannot access rule d's parameter: $lab.d\n", }; String[] dynMembersChecks = { @@ -105,11 +105,11 @@ public class TestAttributeChecks extends BaseTest { "$S::i", "", "$S::i=$S::i", "", - "$b::f", "error(54): A.g:3:1: unknown dynamic scope: b in $b::f\n", - "$S::j", "error(55): A.g:3:4: unknown dynamically-scoped attribute for scope S: j in $S::j\n", - "$S::j = 3;", "error(55): A.g:3:4: unknown dynamically-scoped attribute for scope S: j in $S::j = 3;\n", - "$S::j = $S::k;", "error(55): A.g:3:4: unknown dynamically-scoped attribute for scope S: j in $S::j = $S::k;\n" + - "error(55): A.g:3:12: unknown dynamically-scoped attribute for scope S: k in $S::k\n", + "$b::f", "error(54): A.g4:3:1: unknown dynamic scope: b in $b::f\n", + "$S::j", "error(55): A.g4:3:4: unknown dynamically-scoped attribute for scope S: j in $S::j\n", + "$S::j = 3;", "error(55): A.g4:3:4: unknown dynamically-scoped attribute for scope S: j in $S::j = 3;\n", + "$S::j = $S::k;", "error(55): A.g4:3:4: unknown dynamically-scoped attribute for scope S: j in $S::j = $S::k;\n" + + "error(55): A.g4:3:12: unknown dynamically-scoped attribute for scope S: k in $S::k\n", }; String[] dynInitChecks = { @@ -122,10 +122,10 @@ public class TestAttributeChecks extends BaseTest { "$a::z", "", "$S", "", - "$S::j", "error(55): A.g:8:11: unknown dynamically-scoped attribute for scope S: j in $S::j\n", - "$S::j = 3;", "error(55): A.g:8:11: unknown dynamically-scoped attribute for scope S: j in $S::j = 3;\n", - "$S::j = $S::k;", "error(55): A.g:8:11: unknown dynamically-scoped attribute for scope S: j in $S::j = $S::k;\n" + - "error(55): A.g:8:19: unknown dynamically-scoped attribute for scope S: k in $S::k\n", + "$S::j", "error(55): A.g4:8:11: unknown dynamically-scoped attribute for scope S: j in $S::j\n", + "$S::j = 3;", "error(55): A.g4:8:11: unknown dynamically-scoped attribute for scope S: j in $S::j = 3;\n", + "$S::j = $S::k;", "error(55): A.g4:8:11: unknown dynamically-scoped attribute for scope S: j in $S::j = $S::k;\n" + + "error(55): A.g4:8:19: unknown dynamically-scoped attribute for scope S: k in $S::k\n", }; String[] dynInlineChecks = { @@ -138,27 +138,27 @@ public class TestAttributeChecks extends BaseTest { "$S::i=$S::i", "", "$a::z", "", - "$S::j", "error(55): A.g:10:7: unknown dynamically-scoped attribute for scope S: j in $S::j\n", - "$S::j = 3;", "error(55): A.g:10:7: unknown dynamically-scoped attribute for scope S: j in $S::j = 3;\n", - "$S::j = $S::k;", "error(55): A.g:10:7: unknown dynamically-scoped attribute for scope S: j in $S::j = $S::k;\n" + - "error(55): A.g:10:15: unknown dynamically-scoped attribute for scope S: k in $S::k\n", - "$Q[-1]::y", "error(54): A.g:10:4: unknown dynamic scope: Q in $Q[-1]::y\n", - "$Q[-i]::y", "error(54): A.g:10:4: unknown dynamic scope: Q in $Q[-i]::y\n", - "$Q[i]::y", "error(54): A.g:10:4: unknown dynamic scope: Q in $Q[i]::y\n", - "$Q[0]::y", "error(54): A.g:10:4: unknown dynamic scope: Q in $Q[0]::y\n", - "$Q[-1]::y = 23;", "error(54): A.g:10:4: unknown dynamic scope: Q in $Q[-1]::y = 23;\n", - "$Q[-i]::y = 23;", "error(54): A.g:10:4: unknown dynamic scope: Q in $Q[-i]::y = 23;\n", - "$Q[i]::y = 23;", "error(54): A.g:10:4: unknown dynamic scope: Q in $Q[i]::y = 23;\n", - "$Q[0]::y = 23;", "error(54): A.g:10:4: unknown dynamic scope: Q in $Q[0]::y = 23;\n", - "$S[-1]::y", "error(55): A.g:10:11: unknown dynamically-scoped attribute for scope S: y in $S[-1]::y\n", - "$S[-i]::y", "error(55): A.g:10:11: unknown dynamically-scoped attribute for scope S: y in $S[-i]::y\n", - "$S[i]::y", "error(55): A.g:10:10: unknown dynamically-scoped attribute for scope S: y in $S[i]::y\n", - "$S[0]::y", "error(55): A.g:10:10: unknown dynamically-scoped attribute for scope S: y in $S[0]::y\n", - "$S[-1]::y = 23;", "error(55): A.g:10:11: unknown dynamically-scoped attribute for scope S: y in $S[-1]::y = 23;\n", - "$S[-i]::y = 23;", "error(55): A.g:10:11: unknown dynamically-scoped attribute for scope S: y in $S[-i]::y = 23;\n", - "$S[i]::y = 23;", "error(55): A.g:10:10: unknown dynamically-scoped attribute for scope S: y in $S[i]::y = 23;\n", - "$S[0]::y = 23;", "error(55): A.g:10:10: unknown dynamically-scoped attribute for scope S: y in $S[0]::y = 23;\n", - "$S[$S::y]::i", "error(55): A.g:10:10: unknown dynamically-scoped attribute for scope S: y in $S::y\n" + "$S::j", "error(55): A.g4:10:7: unknown dynamically-scoped attribute for scope S: j in $S::j\n", + "$S::j = 3;", "error(55): A.g4:10:7: unknown dynamically-scoped attribute for scope S: j in $S::j = 3;\n", + "$S::j = $S::k;", "error(55): A.g4:10:7: unknown dynamically-scoped attribute for scope S: j in $S::j = $S::k;\n" + + "error(55): A.g4:10:15: unknown dynamically-scoped attribute for scope S: k in $S::k\n", + "$Q[-1]::y", "error(54): A.g4:10:4: unknown dynamic scope: Q in $Q[-1]::y\n", + "$Q[-i]::y", "error(54): A.g4:10:4: unknown dynamic scope: Q in $Q[-i]::y\n", + "$Q[i]::y", "error(54): A.g4:10:4: unknown dynamic scope: Q in $Q[i]::y\n", + "$Q[0]::y", "error(54): A.g4:10:4: unknown dynamic scope: Q in $Q[0]::y\n", + "$Q[-1]::y = 23;", "error(54): A.g4:10:4: unknown dynamic scope: Q in $Q[-1]::y = 23;\n", + "$Q[-i]::y = 23;", "error(54): A.g4:10:4: unknown dynamic scope: Q in $Q[-i]::y = 23;\n", + "$Q[i]::y = 23;", "error(54): A.g4:10:4: unknown dynamic scope: Q in $Q[i]::y = 23;\n", + "$Q[0]::y = 23;", "error(54): A.g4:10:4: unknown dynamic scope: Q in $Q[0]::y = 23;\n", + "$S[-1]::y", "error(55): A.g4:10:11: unknown dynamically-scoped attribute for scope S: y in $S[-1]::y\n", + "$S[-i]::y", "error(55): A.g4:10:11: unknown dynamically-scoped attribute for scope S: y in $S[-i]::y\n", + "$S[i]::y", "error(55): A.g4:10:10: unknown dynamically-scoped attribute for scope S: y in $S[i]::y\n", + "$S[0]::y", "error(55): A.g4:10:10: unknown dynamically-scoped attribute for scope S: y in $S[0]::y\n", + "$S[-1]::y = 23;", "error(55): A.g4:10:11: unknown dynamically-scoped attribute for scope S: y in $S[-1]::y = 23;\n", + "$S[-i]::y = 23;", "error(55): A.g4:10:11: unknown dynamically-scoped attribute for scope S: y in $S[-i]::y = 23;\n", + "$S[i]::y = 23;", "error(55): A.g4:10:10: unknown dynamically-scoped attribute for scope S: y in $S[i]::y = 23;\n", + "$S[0]::y = 23;", "error(55): A.g4:10:10: unknown dynamically-scoped attribute for scope S: y in $S[0]::y = 23;\n", + "$S[$S::y]::i", "error(55): A.g4:10:10: unknown dynamically-scoped attribute for scope S: y in $S::y\n" }; String[] dynFinallyChecks = { @@ -171,10 +171,10 @@ public class TestAttributeChecks extends BaseTest { "$S::i=$S::i", "", "$a::z", "", - "$S::j", "error(55): A.g:12:17: unknown dynamically-scoped attribute for scope S: j in $S::j\n", - "$S::j = 3;", "error(55): A.g:12:17: unknown dynamically-scoped attribute for scope S: j in $S::j = 3;\n", - "$S::j = $S::k;", "error(55): A.g:12:17: unknown dynamically-scoped attribute for scope S: j in $S::j = $S::k;\n" + - "error(55): A.g:12:25: unknown dynamically-scoped attribute for scope S: k in $S::k\n", + "$S::j", "error(55): A.g4:12:17: unknown dynamically-scoped attribute for scope S: j in $S::j\n", + "$S::j = 3;", "error(55): A.g4:12:17: unknown dynamically-scoped attribute for scope S: j in $S::j = 3;\n", + "$S::j = $S::k;", "error(55): A.g4:12:17: unknown dynamically-scoped attribute for scope S: j in $S::j = $S::k;\n" + + "error(55): A.g4:12:25: unknown dynamically-scoped attribute for scope S: k in $S::k\n", }; @Test public void testMembersActions() throws RecognitionException { diff --git a/tool/test/org/antlr/v4/test/TestBasicSemanticErrors.java b/tool/test/org/antlr/v4/test/TestBasicSemanticErrors.java index b7ffc81fd..4b4ad4cb5 100644 --- a/tool/test/org/antlr/v4/test/TestBasicSemanticErrors.java +++ b/tool/test/org/antlr/v4/test/TestBasicSemanticErrors.java @@ -50,18 +50,18 @@ public class TestBasicSemanticErrors extends BaseTest { "b : ( options { ick=bar; greedy=true; } : ID )+ ;\n" + "c : ID ID ;", // YIELDS - "warning(48): U.g:2:10: illegal option foo\n" + - "warning(48): U.g:2:19: illegal option k\n" + - "error(26): U.g:4:8: token names must start with an uppercase letter: f\n" + - "error(25): U.g:4:8: can't assign string value to token name f in non-combined grammar\n" + - "error(25): U.g:5:8: can't assign string value to token name S in non-combined grammar\n" + - "warning(48): U.g:8:10: illegal option x\n" + - "error(20): U.g:8:0: repeated grammar prequel spec (option, token, or import); please merge\n" + - "error(20): U.g:7:0: repeated grammar prequel spec (option, token, or import); please merge\n" + - "warning(48): U.g:11:10: illegal option blech\n" + - "warning(48): U.g:11:21: illegal option greedy\n" + - "warning(48): U.g:14:16: illegal option ick\n" + - "warning(48): U.g:15:16: illegal option x\n", + "warning(48): U.g4:2:10: illegal option foo\n" + + "warning(48): U.g4:2:19: illegal option k\n" + + "error(26): U.g4:4:8: token names must start with an uppercase letter: f\n" + + "error(25): U.g4:4:8: can't assign string value to token name f in non-combined grammar\n" + + "error(25): U.g4:5:8: can't assign string value to token name S in non-combined grammar\n" + + "warning(48): U.g4:8:10: illegal option x\n" + + "error(20): U.g4:8:0: repeated grammar prequel spec (option, token, or import); please merge\n" + + "error(20): U.g4:7:0: repeated grammar prequel spec (option, token, or import); please merge\n" + + "warning(48): U.g4:11:10: illegal option blech\n" + + "warning(48): U.g4:11:21: illegal option greedy\n" + + "warning(48): U.g4:14:16: illegal option ick\n" + + "warning(48): U.g4:15:16: illegal option x\n", }; @Test public void testU() { super.testErrors(U, false); } diff --git a/tool/test/org/antlr/v4/test/TestCompositeGrammars.java b/tool/test/org/antlr/v4/test/TestCompositeGrammars.java index ad83dc641..ea2be8676 100644 --- a/tool/test/org/antlr/v4/test/TestCompositeGrammars.java +++ b/tool/test/org/antlr/v4/test/TestCompositeGrammars.java @@ -43,7 +43,7 @@ public class TestCompositeGrammars extends BaseTest { "parser grammar S;\n" + "a : B . C ;\n"; // not qualified ID mkdir(tmpdir); - Grammar g = new Grammar(tmpdir + "/S.g", grammar); + Grammar g = new Grammar(tmpdir + "/S.g4", grammar); g.name = "S"; ErrorQueue equeue = new ErrorQueue(); @@ -61,14 +61,14 @@ public class TestCompositeGrammars extends BaseTest { "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + "s : a ;\n" + "B : 'b' ;" + // defines B from inherited token space "WS : (' '|'\\n') {skip();} ;\n" ; - String found = execParser("M.g", master, "MParser", "MLexer", + String found = execParser("M.g4", master, "MParser", "MLexer", "s", "b", debug); assertEquals("S.a\n", found); } @@ -78,13 +78,13 @@ public class TestCompositeGrammars extends BaseTest { "parser grammar S;\n" + "a : '=' 'a' {System.out.println(\"S.a\");} ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + "s : a ;\n" + "WS : (' '|'\\n') {skip();} ;\n" ; - String found = execParser("M.g", master, "MParser", "MLexer", + String found = execParser("M.g4", master, "MParser", "MLexer", "s", "=a", debug); assertEquals("S.a\n", found); } @@ -97,14 +97,14 @@ public class TestCompositeGrammars extends BaseTest { "parser grammar S;\n" + "a[int x] returns [int y] : B {System.out.print(\"S.a\"); $y=1000;} ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + "s : label=a[3] {System.out.println($label.y);} ;\n" + "B : 'b' ;" + // defines B from inherited token space "WS : (' '|'\\n') {skip();} ;\n" ; - String found = execParser("M.g", master, "MParser", "MLexer", + String found = execParser("M.g4", master, "MParser", "MLexer", "s", "b", debug); assertEquals("S.a1000\n", found); } @@ -117,14 +117,14 @@ public class TestCompositeGrammars extends BaseTest { "parser grammar S;\n" + "a : B {System.out.print(\"S.a\");} ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + "s : a {System.out.println($a.text);} ;\n" + "B : 'b' ;" + // defines B from inherited token space "WS : (' '|'\\n') {skip();} ;\n" ; - String found = execParser("M.g", master, "MParser", "MLexer", + String found = execParser("M.g4", master, "MParser", "MLexer", "s", "b", debug); assertEquals("S.ab\n", found); } @@ -137,13 +137,13 @@ public class TestCompositeGrammars extends BaseTest { "}\n" + "a : B ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "grammar M;\n" + // uses no rules from the import "import S;\n" + "s : 'b' {foo();} ;\n" + // gS is import pointer "WS : (' '|'\\n') {skip();} ;\n" ; - String found = execParser("M.g", master, "MParser", "MLexer", + String found = execParser("M.g4", master, "MParser", "MLexer", "s", "b", debug); assertEquals("foo\n", found); } @@ -154,18 +154,18 @@ public class TestCompositeGrammars extends BaseTest { "a : b {System.out.println(\"S.a\");} ;\n" + "b : B ;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String slave2 = "parser grammar T;\n" + "a : B {System.out.println(\"T.a\");} ;\n"; // hidden by S.a - writeFile(tmpdir, "T.g", slave2); + writeFile(tmpdir, "T.g4", slave2); String master = "grammar M;\n" + "import S,T;\n" + "s : a ;\n" + "B : 'b' ;\n" + "WS : (' '|'\\n') {skip();} ;\n" ; - String found = execParser("M.g", master, "MParser", "MLexer", + String found = execParser("M.g4", master, "MParser", "MLexer", "s", "b", debug); assertEquals("S.a\n", found); } @@ -176,13 +176,13 @@ public class TestCompositeGrammars extends BaseTest { "tokens { A; B; C; }\n" + "x : A {System.out.println(\"S.x\");} ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String slave2 = "parser grammar T;\n" + "tokens { C; B; A; }\n" + // reverse order "y : A {System.out.println(\"T.y\");} ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "T.g", slave2); + writeFile(tmpdir, "T.g4", slave2); // The lexer will create rules to match letters a, b, c. // The associated token types A, B, C must have the same value // and all import'd parsers. Since ANTLR regenerates all imports @@ -202,7 +202,7 @@ public class TestCompositeGrammars extends BaseTest { "A : 'a' ;\n" + "C : 'c' ;\n" + "WS : (' '|'\\n') {skip();} ;\n" ; - String found = execParser("M.g", master, "MParser", "MLexer", + String found = execParser("M.g4", master, "MParser", "MLexer", "s", "aa", debug); assertEquals("S.x\n" + "T.y\n", found); @@ -215,13 +215,13 @@ public class TestCompositeGrammars extends BaseTest { "tokens { A; B; C; }\n" + "x : A {System.out.println(\"S.x\");} ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String slave2 = "parser grammar T;\n" + "tokens { C; B; A; }\n" + // reverse order "y : A {System.out.println(\"T.y\");} ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "T.g", slave2); + writeFile(tmpdir, "T.g4", slave2); String master = "grammar M;\n" + @@ -231,8 +231,8 @@ public class TestCompositeGrammars extends BaseTest { "A : 'a' ;\n" + "C : 'c' ;\n" + "WS : (' '|'\\n') {skip();} ;\n" ; - writeFile(tmpdir, "M.g", master); - Grammar g = new Grammar(tmpdir+"/M.g", master, equeue); + writeFile(tmpdir, "M.g4", master); + Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue); String expectedTokenIDToTypeMap = "{EOF=-1, B=3, A=4, C=5, WS=6}"; String expectedStringLiteralToTypeMap = "{'c'=5, 'a'=4, 'b'=3}"; @@ -244,7 +244,7 @@ public class TestCompositeGrammars extends BaseTest { assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); - String found = execParser("M.g", master, "MParser", "MLexer", + String found = execParser("M.g4", master, "MParser", "MLexer", "s", "aa", debug); assertEquals("S.x\n" + "T.y\n", found); @@ -260,18 +260,18 @@ public class TestCompositeGrammars extends BaseTest { "INT : '0'..'9'+ ;\n" + "WS : (' '|'\\n') {skip();} ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + "s : x INT ;\n"; - writeFile(tmpdir, "M.g", master); - Grammar g = new Grammar(tmpdir+"/M.g", master, equeue); + writeFile(tmpdir, "M.g4", master); + Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue); assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); - String found = execParser("M.g", master, "MParser", "MLexer", + String found = execParser("M.g4", master, "MParser", "MLexer", "s", "x 34 9", debug); assertEquals("S.x\n", found); } @@ -284,15 +284,15 @@ public class TestCompositeGrammars extends BaseTest { "tokens { A='a'; }\n" + "x : A {System.out.println(\"S.x\");} ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + "s : x ;\n" + "WS : (' '|'\\n') {skip();} ;\n" ; - writeFile(tmpdir, "M.g", master); - Grammar g = new Grammar(tmpdir+"/M.g", master, equeue); + writeFile(tmpdir, "M.g4", master); + Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue); Object expectedArg = "S"; ErrorType expectedMsgID = ErrorType.OPTIONS_IN_DELEGATE; @@ -310,15 +310,15 @@ public class TestCompositeGrammars extends BaseTest { "parser grammar S;\n" + "options {toke\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + "s : x ;\n" + "WS : (' '|'\\n') {skip();} ;\n" ; - writeFile(tmpdir, "M.g", master); - Grammar g = new Grammar(tmpdir+"/M.g", master, equeue); + writeFile(tmpdir, "M.g4", master); + Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue); assertEquals(ErrorType.SYNTAX_ERROR, equeue.errors.get(0).errorType); } @@ -329,13 +329,13 @@ public class TestCompositeGrammars extends BaseTest { "a : b {System.out.println(\"S.a\");} ;\n" + "b : B ;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + "b : 'b'|'c' ;\n" + "WS : (' '|'\\n') {skip();} ;\n" ; - String found = execParser("M.g", master, "MParser", "MLexer", + String found = execParser("M.g4", master, "MParser", "MLexer", "a", "c", debug); assertEquals("S.a\n", found); } @@ -349,7 +349,7 @@ public class TestCompositeGrammars extends BaseTest { " ;\n" + "init : '=' INT ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "JavaDecl.g", slave); + writeFile(tmpdir, "JavaDecl.g4", slave); String master = "grammar Java;\n" + "import JavaDecl;\n" + @@ -360,7 +360,7 @@ public class TestCompositeGrammars extends BaseTest { "INT : '0'..'9'+ ;\n" + "WS : (' '|'\\n') {skip();} ;\n" ; // for float to work in decl, type must be overridden - String found = execParser("Java.g", master, "JavaParser", "JavaLexer", + String found = execParser("Java.g4", master, "JavaParser", "JavaLexer", "prog", "float x = 3;", debug); assertEquals("JavaDecl: floatx=3;\n", found); } @@ -371,20 +371,20 @@ public class TestCompositeGrammars extends BaseTest { "a : b {System.out.println(\"S.a\");} ;\n" + "b : B ;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String slave2 = "parser grammar T;\n" + "tokens { A='x'; }\n" + "b : B {System.out.println(\"T.b\");} ;\n"; - writeFile(tmpdir, "T.g", slave2); + writeFile(tmpdir, "T.g4", slave2); String master = "grammar M;\n" + "import S, T;\n" + "b : 'b'|'c' {System.out.println(\"M.b\");}|B|A ;\n" + "WS : (' '|'\\n') {skip();} ;\n" ; - String found = execParser("M.g", master, "MParser", "MLexer", + String found = execParser("M.g4", master, "MParser", "MLexer", "a", "c", debug); assertEquals("M.b\n" + "S.a\n", found); @@ -397,7 +397,7 @@ public class TestCompositeGrammars extends BaseTest { "A : 'a' {System.out.println(\"S.A\");} ;\n" + "C : 'c' ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "lexer grammar M;\n" + "import S;\n" + @@ -409,7 +409,7 @@ public class TestCompositeGrammars extends BaseTest { "[@1,1:1='b',<3>,1:1]\n" + "[@2,2:2='c',<6>,1:2]\n" + "[@3,3:2='',<-1>,1:3]\n"; - String found = execLexer("M.g", master, "M", "abc", debug); + String found = execLexer("M.g4", master, "M", "abc", debug); assertEquals(expecting, found); } @@ -419,13 +419,13 @@ public class TestCompositeGrammars extends BaseTest { "A : 'a' {System.out.println(\"S.A\");} ;\n" + "B : 'b' {System.out.println(\"S.B\");} ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "lexer grammar M;\n" + "import S;\n" + "A : 'a' B {System.out.println(\"M.A\");} ;\n" + "WS : (' '|'\\n') {skip();} ;\n" ; - String found = execLexer("M.g", master, "M", "ab", debug); + String found = execLexer("M.g4", master, "M", "ab", debug); assertEquals("M.A\n" + "[@0,0:1='ab',<3>,1:0]\n" + "[@1,2:1='',<-1>,1:2]\n", found); @@ -440,14 +440,14 @@ public class TestCompositeGrammars extends BaseTest { "lexer grammar S;\n" + "ID : 'a'..'z'+ ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + "a : A {System.out.println(\"M.a: \"+$A);} ;\n" + "A : 'abc' {System.out.println(\"M.A\");} ;\n" + "WS : (' '|'\\n') {skip();} ;\n" ; - String found = execParser("M.g", master, "MParser", "MLexer", + String found = execParser("M.g4", master, "MParser", "MLexer", "a", "abc", debug); assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); @@ -464,20 +464,20 @@ public class TestCompositeGrammars extends BaseTest { "parser grammar T;\n" + "a : T ;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "T.g", slave); + writeFile(tmpdir, "T.g4", slave); String slave2 = "parser grammar S;\n" + "import T;\n" + "a : S ;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave2); + writeFile(tmpdir, "S.g4", slave2); String master = "grammar M;\n" + "import S;\n" + "a : M ;\n" ; - writeFile(tmpdir, "M.g", master); - Grammar g = new Grammar(tmpdir+"/M.g", master, equeue); + writeFile(tmpdir, "M.g4", master); + Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue); String expectedTokenIDToTypeMap = "{EOF=-1, M=3}"; // S and T aren't imported; overridden String expectedStringLiteralToTypeMap = "{}"; @@ -492,7 +492,7 @@ public class TestCompositeGrammars extends BaseTest { assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); boolean ok = - rawGenerateAndBuildRecognizer("M.g", master, "MParser", null); + rawGenerateAndBuildRecognizer("M.g4", master, "MParser", null); boolean expecting = true; // should be ok assertEquals(expecting, ok); } @@ -503,37 +503,37 @@ public class TestCompositeGrammars extends BaseTest { "parser grammar T;\n" + "x : T ;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "T.g", slave); + writeFile(tmpdir, "T.g4", slave); slave = "parser grammar S;\n" + "import T;\n" + "y : S ;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); slave = "parser grammar C;\n" + "i : C ;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "C.g", slave); + writeFile(tmpdir, "C.g4", slave); slave = "parser grammar B;\n" + "j : B ;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "B.g", slave); + writeFile(tmpdir, "B.g4", slave); slave = "parser grammar A;\n" + "import B,C;\n" + "k : A ;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "A.g", slave); + writeFile(tmpdir, "A.g4", slave); String master = "grammar M;\n" + "import S,A;\n" + "a : M ;\n" ; - writeFile(tmpdir, "M.g", master); - Grammar g = new Grammar(tmpdir+"/M.g", master, equeue); + writeFile(tmpdir, "M.g4", master); + Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue); assertEquals(equeue.errors.toString(), "[]"); assertEquals(equeue.warnings.toString(), "[]"); @@ -548,7 +548,7 @@ public class TestCompositeGrammars extends BaseTest { realElements(g.typeToTokenList).toString()); boolean ok = - rawGenerateAndBuildRecognizer("M.g", master, "MParser", null); + rawGenerateAndBuildRecognizer("M.g4", master, "MParser", null); boolean expecting = true; // should be ok assertEquals(expecting, ok); } @@ -559,20 +559,20 @@ public class TestCompositeGrammars extends BaseTest { "parser grammar T;\n" + "x : T ;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "T.g", slave); + writeFile(tmpdir, "T.g4", slave); String slave2 = "parser grammar S;\n" + // A, B, C token type order "import T;\n" + "a : S ;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave2); + writeFile(tmpdir, "S.g4", slave2); String master = "grammar M;\n" + "import S;\n" + "a : M x ;\n" ; // x MUST BE VISIBLE TO M - writeFile(tmpdir, "M.g", master); - Grammar g = new Grammar(tmpdir+"/M.g", master, equeue); + writeFile(tmpdir, "M.g4", master); + Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue); String expectedTokenIDToTypeMap = "{EOF=-1, M=3, T=4}"; String expectedStringLiteralToTypeMap = "{}"; @@ -597,29 +597,29 @@ public class TestCompositeGrammars extends BaseTest { "T3: '3';\n" + "T4: '4';\n" ; mkdir(tmpdir); - writeFile(tmpdir, "L.g", gstr); + writeFile(tmpdir, "L.g4", gstr); gstr = "parser grammar G1;\n" + "s: a | b;\n" + "a: T1;\n" + "b: T2;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "G1.g", gstr); + writeFile(tmpdir, "G1.g4", gstr); gstr = "parser grammar G2;\n" + "import G1;\n" + "a: T3;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "G2.g", gstr); + writeFile(tmpdir, "G2.g4", gstr); String G3str = "grammar G3;\n" + "import G2;\n" + "b: T4;\n" ; mkdir(tmpdir); - writeFile(tmpdir, "G3.g", G3str); + writeFile(tmpdir, "G3.g4", G3str); - Grammar g = new Grammar(tmpdir+"/G3.g", G3str, equeue); + Grammar g = new Grammar(tmpdir+"/G3.g4", G3str, equeue); String expectedTokenIDToTypeMap = "{EOF=-1, T4=3, T3=4}"; String expectedStringLiteralToTypeMap = "{}"; @@ -634,7 +634,7 @@ public class TestCompositeGrammars extends BaseTest { assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); boolean ok = - rawGenerateAndBuildRecognizer("G3.g", G3str, "G3Parser", null); + rawGenerateAndBuildRecognizer("G3.g4", G3str, "G3Parser", null); boolean expecting = true; // should be ok assertEquals(expecting, ok); } @@ -644,7 +644,7 @@ public class TestCompositeGrammars extends BaseTest { "parser grammar S;\n" + "a : B {System.out.print(\"S.a\");} ;\n"; mkdir(tmpdir); - writeFile(tmpdir, "S.g", slave); + writeFile(tmpdir, "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + @@ -653,7 +653,7 @@ public class TestCompositeGrammars extends BaseTest { "s : a ;\n" + "B : 'b' ;" + // defines B from inherited token space "WS : (' '|'\\n') {skip();} ;\n" ; - boolean ok = antlr("M.g", "M.g", master); + boolean ok = antlr("M.g4", "M.g4", master); boolean expecting = true; // should be ok assertEquals(expecting, ok); } diff --git a/tool/test/org/antlr/v4/test/TestFullContextParsing.java b/tool/test/org/antlr/v4/test/TestFullContextParsing.java index 5ea7486ea..06454db62 100644 --- a/tool/test/org/antlr/v4/test/TestFullContextParsing.java +++ b/tool/test/org/antlr/v4/test/TestFullContextParsing.java @@ -49,7 +49,7 @@ public class TestFullContextParsing extends BaseTest { " : ID | ID {;} ;\n" + "ID : 'a'..'z'+ ;\n"+ "WS : (' '|'\\t'|'\\n')+ {skip();} ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "abc", true); String expecting = "Decision 0:\n" + @@ -70,7 +70,7 @@ public class TestFullContextParsing extends BaseTest { "ID : 'a'..'z'+ ;\n"+ "INT : '0'..'9'+ ;\n"+ "WS : (' '|'\\t'|'\\n')+ {skip();} ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "$ 34 abc", true); String expecting = "Decision 1:\n" + @@ -81,7 +81,7 @@ public class TestFullContextParsing extends BaseTest { "line 1:2 reportContextSensitivity d=1, input='34'\n", this.stderrDuringParse); - result = execParser("T.g", grammar, "TParser", "TLexer", "s", + result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "@ 34 abc", true); expecting = "Decision 1:\n" + @@ -104,7 +104,7 @@ public class TestFullContextParsing extends BaseTest { "ID : 'a'..'z'+ ;\n"+ "INT : '0'..'9'+ ;\n"+ "WS : (' '|'\\t'|'\\n')+ {skip();} ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "$ 34 abc @ 34 abc", true); String expecting = "Decision 2:\n" + @@ -131,7 +131,7 @@ public class TestFullContextParsing extends BaseTest { "ID : 'a'..'z'+ ;\n"+ "WS : (' '|'\\t'|'\\n')+ {skip();} ;\n"; String input = "{ if x then return }"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); String expecting = "Decision 1:\n" + @@ -141,7 +141,7 @@ public class TestFullContextParsing extends BaseTest { input = "{ if x then if y then return else foo }"; - result = execParser("T.g", grammar, "TParser", "TLexer", "s", + result = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); expecting = "Decision 1:\n" + @@ -153,7 +153,7 @@ public class TestFullContextParsing extends BaseTest { this.stderrDuringParse); input = "{ if x then return else foo }"; - result = execParser("T.g", grammar, "TParser", "TLexer", "s", + result = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); expecting = "Decision 1:\n" + @@ -170,7 +170,7 @@ public class TestFullContextParsing extends BaseTest { this.stderrDuringParse); input = "{ if x then return else foo }"; - result = execParser("T.g", grammar, "TParser", "TLexer", "s", + result = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); expecting = "Decision 1:\n" + @@ -183,7 +183,7 @@ public class TestFullContextParsing extends BaseTest { input = "{ if x then return else foo\n" + "if x then if y then return else foo }"; - result = execParser("T.g", grammar, "TParser", "TLexer", "s", + result = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); expecting = "Decision 1:\n" + @@ -199,7 +199,7 @@ public class TestFullContextParsing extends BaseTest { input = "{ if x then return else foo\n" + "if x then if y then return else foo }"; - result = execParser("T.g", grammar, "TParser", "TLexer", "s", + result = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); expecting = "Decision 1:\n" + @@ -235,7 +235,7 @@ public class TestFullContextParsing extends BaseTest { "ID : [a-z]+ ;\n" + ""; - String found = execParser("T.g", grammar, "TParser", "TLexer", "prog", "a(i)<-x", true); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "prog", "a(i)<-x", true); assertEquals("pass.\n", found); String expecting = diff --git a/tool/test/org/antlr/v4/test/TestLeftRecursion.java b/tool/test/org/antlr/v4/test/TestLeftRecursion.java index 854461ab6..d37671bef 100644 --- a/tool/test/org/antlr/v4/test/TestLeftRecursion.java +++ b/tool/test/org/antlr/v4/test/TestLeftRecursion.java @@ -15,17 +15,17 @@ public class TestLeftRecursion extends BaseTest { " ;\n" + "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x", debug); String expecting = "(s (a x))\n"; assertEquals(expecting, found); - found = execParser("T.g", grammar, "TParser", "TLexer", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x y", debug); expecting = "(s (a (a x) y))\n"; assertEquals(expecting, found); - found = execParser("T.g", grammar, "TParser", "TLexer", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x y z", debug); expecting = "(s (a (a (a x) y) z))\n"; assertEquals(expecting, found); @@ -40,7 +40,7 @@ public class TestLeftRecursion extends BaseTest { " ;\n" + "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x y z", debug); String expecting = "(s (a (a (a x) y) z))\n"; assertEquals(expecting, found); @@ -358,7 +358,7 @@ public class TestLeftRecursion extends BaseTest { } public void runTests(String grammar, String[] tests, String startRule) { - rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer"); + rawGenerateAndBuildRecognizer("T.g4", grammar, "TParser", "TLexer"); writeRecognizerAndCompile("TParser", "TLexer", startRule, diff --git a/tool/test/org/antlr/v4/test/TestLexerErrors.java b/tool/test/org/antlr/v4/test/TestLexerErrors.java index 6ba0f3a3a..452d13dba 100644 --- a/tool/test/org/antlr/v4/test/TestLexerErrors.java +++ b/tool/test/org/antlr/v4/test/TestLexerErrors.java @@ -37,7 +37,7 @@ public class TestLexerErrors extends BaseTest { String grammar = "lexer grammar L;\n" + "A : 'a' 'b' ;\n"; - String tokens = execLexer("L.g", grammar, "L", "x"); + String tokens = execLexer("L.g4", grammar, "L", "x"); String expectingTokens = "[@0,1:0='',<-1>,1:1]\n"; assertEquals(expectingTokens, tokens); @@ -50,7 +50,7 @@ public class TestLexerErrors extends BaseTest { String grammar = "lexer grammar L;\n" + "A : 'a' 'b' ;\n"; - String tokens = execLexer("L.g", grammar, "L", "abx"); + String tokens = execLexer("L.g4", grammar, "L", "abx"); String expectingTokens = "[@0,0:1='ab',<3>,1:0]\n" + "[@1,3:2='',<-1>,1:3]\n"; @@ -64,7 +64,7 @@ public class TestLexerErrors extends BaseTest { String grammar = "lexer grammar L;\n" + "A : 'a' 'b' ;\n"; - String tokens = execLexer("L.g", grammar, "L", "ax"); + String tokens = execLexer("L.g4", grammar, "L", "ax"); String expectingTokens = "[@0,2:1='',<-1>,1:2]\n"; assertEquals(expectingTokens, tokens); @@ -77,7 +77,7 @@ public class TestLexerErrors extends BaseTest { String grammar = "lexer grammar L;\n" + "A : 'a' 'b' ;\n"; - String tokens = execLexer("L.g", grammar, "L", "abax"); + String tokens = execLexer("L.g4", grammar, "L", "abax"); String expectingTokens = "[@0,0:1='ab',<3>,1:0]\n" + "[@1,4:3='',<-1>,1:4]\n"; @@ -95,7 +95,7 @@ public class TestLexerErrors extends BaseTest { // The first ab caches the DFA then abx goes through the DFA but // into the ATN for the x, which fails. Must go back into DFA // and return to previous dfa accept state - String tokens = execLexer("L.g", grammar, "L", "ababx"); + String tokens = execLexer("L.g4", grammar, "L", "ababx"); String expectingTokens = "[@0,0:1='ab',<3>,1:0]\n" + "[@1,2:3='ab',<3>,1:2]\n" + @@ -116,7 +116,7 @@ public class TestLexerErrors extends BaseTest { // into the ATN for the c. It marks that hasn't except state // and then keeps going in the ATN. It fails on the x, but // uses the previous accepted in the ATN not DFA - String tokens = execLexer("L.g", grammar, "L", "ababcx"); + String tokens = execLexer("L.g4", grammar, "L", "ababcx"); String expectingTokens = "[@0,0:1='ab',<3>,1:0]\n" + "[@1,2:4='abc',<4>,1:2]\n" + @@ -131,7 +131,7 @@ public class TestLexerErrors extends BaseTest { String grammar = "lexer grammar L;\n" + "A : 'abc' ;\n"; - String tokens = execLexer("L.g", grammar, "L", "abx"); + String tokens = execLexer("L.g4", grammar, "L", "abx"); String expectingTokens = "[@0,3:2='',<-1>,1:3]\n"; assertEquals(expectingTokens, tokens); @@ -155,7 +155,7 @@ public class TestLexerErrors extends BaseTest { "primary : ID;\n" + "ID : [a-z]+;\n" + "\n"; - String result = execLexer("T.g", grammar, "TLexer", "x : x", false); + String result = execLexer("T.g4", grammar, "TLexer", "x : x", false); String expecting = "[@0,0:0='x',<5>,1:0]\n" + "[@1,2:2=':',<4>,1:2]\n" + diff --git a/tool/test/org/antlr/v4/test/TestLexerExec.java b/tool/test/org/antlr/v4/test/TestLexerExec.java index a97bbe99b..2fc4ca2e0 100644 --- a/tool/test/org/antlr/v4/test/TestLexerExec.java +++ b/tool/test/org/antlr/v4/test/TestLexerExec.java @@ -7,7 +7,7 @@ public class TestLexerExec extends BaseTest { String grammar = "lexer grammar L;\n"+ "QUOTE : '\"' ;\n"; // make sure this compiles - String found = execLexer("L.g", grammar, "L", "\""); + String found = execLexer("L.g4", grammar, "L", "\""); String expecting = "[@0,0:0='\"',<3>,1:0]\n" + "[@1,1:0='',<-1>,1:1]\n"; @@ -20,7 +20,7 @@ public class TestLexerExec extends BaseTest { "A : '-' I ;\n" + "I : '0'..'9'+ ;\n"+ "WS : (' '|'\\n') {skip();} ;"; - String found = execLexer("L.g", grammar, "L", "34 -21 3"); + String found = execLexer("L.g4", grammar, "L", "34 -21 3"); String expecting = "[@0,0:1='34',<4>,1:0]\n" + "[@1,3:5='-21',<3>,1:3]\n" + @@ -34,7 +34,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "I : '0'..'9'+ {System.out.println(\"I\");} ;\n"+ "WS : (' '|'\\n') {skip();} ;"; - String found = execLexer("L.g", grammar, "L", "34 34"); + String found = execLexer("L.g4", grammar, "L", "34 34"); String expecting = "I\n" + "I\n" + @@ -49,7 +49,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "I : '0'..'9'+ {System.out.println(\"I\");} ;\n"+ "WS : (' '|'\\n') -> skip ;"; - String found = execLexer("L.g", grammar, "L", "34 34"); + String found = execLexer("L.g4", grammar, "L", "34 34"); String expecting = "I\n" + "I\n" + @@ -64,7 +64,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "I : '0'..'9'+ {System.out.println(\"I\");} ;\n"+ "WS : '#' -> more ;"; - String found = execLexer("L.g", grammar, "L", "34#10"); + String found = execLexer("L.g4", grammar, "L", "34#10"); String expecting = "I\n" + "I\n" + @@ -79,7 +79,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "I : '0'..'9'+ {System.out.println(\"I\");} ;\n"+ "HASH : '#' -> type(HASH) ;"; - String found = execLexer("L.g", grammar, "L", "34#"); + String found = execLexer("L.g4", grammar, "L", "34#"); String expecting = "I\n" + "[@0,0:1='34',<3>,1:0]\n" + @@ -93,7 +93,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "I : '0'..'9'+ {System.out.println(\"I\");} ;\n"+ "HASH : '#' -> type(HASH), skip, more ;"; - String found = execLexer("L.g", grammar, "L", "34#11"); + String found = execLexer("L.g4", grammar, "L", "34#11"); String expecting = "I\n" + "I\n" + @@ -111,7 +111,7 @@ public class TestLexerExec extends BaseTest { "mode STRING_MODE;\n"+ "STRING : '\"' {popMode();} ;\n"+ "ANY : . {more();} ;\n"; - String found = execLexer("L.g", grammar, "L", "\"abc\" \"ab\""); + String found = execLexer("L.g4", grammar, "L", "\"abc\" \"ab\""); String expecting = "[@0,0:4='\"abc\"',<5>,1:0]\n" + "[@1,6:9='\"ab\"',<5>,1:6]\n" + @@ -127,7 +127,7 @@ public class TestLexerExec extends BaseTest { "mode STRING_MODE;\n"+ "STRING : '\"' -> popMode ;\n"+ "ANY : . -> more ;\n"; - String found = execLexer("L.g", grammar, "L", "\"abc\" \"ab\""); + String found = execLexer("L.g4", grammar, "L", "\"abc\" \"ab\""); String expecting = "[@0,0:4='\"abc\"',<5>,1:0]\n" + "[@1,6:9='\"ab\"',<5>,1:6]\n" + @@ -143,7 +143,7 @@ public class TestLexerExec extends BaseTest { "mode STRING_MODE;\n"+ "STRING : '\"' -> mode(DEFAULT_MODE) ;\n"+ "ANY : . -> more ;\n"; - String found = execLexer("L.g", grammar, "L", "\"abc\" \"ab\""); + String found = execLexer("L.g4", grammar, "L", "\"abc\" \"ab\""); String expecting = "[@0,0:4='\"abc\"',<5>,1:0]\n" + "[@1,6:9='\"ab\"',<5>,1:6]\n" + @@ -157,7 +157,7 @@ public class TestLexerExec extends BaseTest { "KEND : 'end' ;\n" + // has priority "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\n')+ ;"; - String found = execLexer("L.g", grammar, "L", "end eend ending a"); + String found = execLexer("L.g4", grammar, "L", "end eend ending a"); String expecting = "[@0,0:2='end',<3>,1:0]\n" + "[@1,3:3=' ',<5>,1:3]\n" + @@ -180,7 +180,7 @@ public class TestLexerExec extends BaseTest { "ID : 'a'..'z'+ ;\n" + "fragment HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;\n" + "WS : (' '|'\n')+ ;"; - String found = execLexer("L.g", grammar, "L", "x 0 1 a.b a.l"); + String found = execLexer("L.g4", grammar, "L", "x 0 1 a.b a.l"); String expecting = "[@0,0:0='x',<7>,1:0]\n" + "[@1,1:1=' ',<8>,1:1]\n" + @@ -205,7 +205,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n" + "DONE : EOF ;\n" + "A : 'a';\n"; - String found = execLexer("L.g", grammar, "L", ""); + String found = execLexer("L.g4", grammar, "L", ""); String expecting = "[@0,0:-1='',<3>,1:0]\n" + "[@1,0:-1='',<-1>,1:0]\n"; @@ -218,12 +218,12 @@ public class TestLexerExec extends BaseTest { "A : 'a' EOF ;\n"+ "B : 'a';\n"+ "C : 'c';\n"; - String found = execLexer("L.g", grammar, "L", ""); + String found = execLexer("L.g4", grammar, "L", ""); String expecting = "[@0,0:-1='',<-1>,1:0]\n"; assertEquals(expecting, found); - found = execLexer("L.g", grammar, "L", "a"); + found = execLexer("L.g4", grammar, "L", "a"); expecting = "[@0,0:0='a',<3>,1:0]\n" + "[@1,1:0='',<-1>,1:1]\n"; @@ -235,7 +235,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "I : '0'..'9'+ {System.out.println(\"I\");} ;\n"+ "WS : [ \\n\\u000D] -> skip ;"; - String found = execLexer("L.g", grammar, "L", "34\r\n 34"); + String found = execLexer("L.g4", grammar, "L", "34\r\n 34"); String expecting = "I\n" + "I\n" + @@ -250,7 +250,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "I : '0'..'9'+ {System.out.println(\"I\");} ;\n"+ "WS : [ \\n\\u000D]+ -> skip ;"; - String found = execLexer("L.g", grammar, "L", "34\r\n 34"); + String found = execLexer("L.g4", grammar, "L", "34\r\n 34"); String expecting = "I\n" + "I\n" + @@ -265,7 +265,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "I : ~[ab \n] ~[ \ncd]* {System.out.println(\"I\");} ;\n"+ "WS : [ \\n\\u000D]+ -> skip ;"; - String found = execLexer("L.g", grammar, "L", "xaf"); + String found = execLexer("L.g4", grammar, "L", "xaf"); String expecting = "I\n" + "[@0,0:2='xaf',<3>,1:0]\n" + @@ -278,7 +278,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "I : (~[ab \n]|'a') {System.out.println(\"I\");} ;\n"+ "WS : [ \\n\\u000D]+ -> skip ;"; - String found = execLexer("L.g", grammar, "L", "a x"); + String found = execLexer("L.g4", grammar, "L", "a x"); String expecting = "I\n" + "I\n" + @@ -294,7 +294,7 @@ public class TestLexerExec extends BaseTest { "I : [0-9]+ {System.out.println(\"I\");} ;\n"+ "ID : [a-zA-Z] [a-zA-Z0-9]* {System.out.println(\"ID\");} ;\n"+ "WS : [ \\n\\u0009\r]+ -> skip ;"; - String found = execLexer("L.g", grammar, "L", "34\r 34 a2 abc \n "); + String found = execLexer("L.g4", grammar, "L", "34\r 34 a2 abc \n "); String expecting = "I\n" + "I\n" + @@ -313,7 +313,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "I : [0-]+ {System.out.println(\"I\");} ;\n"+ "WS : [ \\n\\u000D]+ -> skip ;"; - String found = execLexer("L.g", grammar, "L", "00\r\n"); + String found = execLexer("L.g4", grammar, "L", "00\r\n"); String expecting = "I\n" + "[@0,0:1='00',<3>,1:0]\n" + @@ -326,7 +326,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "I : [0-9]+ {System.out.println(\"I\");} ;\n"+ "WS : [ \\u]+ -> skip ;"; - String found = execLexer("L.g", grammar, "L", "34 "); + String found = execLexer("L.g4", grammar, "L", "34 "); String expecting = "I\n" + "[@0,0:1='34',<3>,1:0]\n" + @@ -339,7 +339,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "DASHBRACK : [\\-\\]]+ {System.out.println(\"DASHBRACK\");} ;\n"+ "WS : [ \\u]+ -> skip ;"; - String found = execLexer("L.g", grammar, "L", "- ] "); + String found = execLexer("L.g4", grammar, "L", "- ] "); String expecting = "DASHBRACK\n" + "DASHBRACK\n" + @@ -354,7 +354,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "A : [z-a9]+ {System.out.println(\"A\");} ;\n"+ "WS : [ \\u]+ -> skip ;"; - String found = execLexer("L.g", grammar, "L", "9"); + String found = execLexer("L.g4", grammar, "L", "9"); String expecting = "A\n" + "[@0,0:0='9',<3>,1:0]\n" + @@ -367,7 +367,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "A : [\"a-z]+ {System.out.println(\"A\");} ;\n"+ "WS : [ \n\t]+ -> skip ;"; - String found = execLexer("L.g", grammar, "L", "b\"a"); + String found = execLexer("L.g4", grammar, "L", "b\"a"); String expecting = "A\n" + "[@0,0:2='b\"a',<3>,1:0]\n" + @@ -380,7 +380,7 @@ public class TestLexerExec extends BaseTest { "lexer grammar L;\n"+ "A : [\"\\\\ab]+ {System.out.println(\"A\");} ;\n"+ "WS : [ \n\t]+ -> skip ;"; - String found = execLexer("L.g", grammar, "L", "b\"\\a"); + String found = execLexer("L.g4", grammar, "L", "b\"\\a"); String expecting = "A\n" + "[@0,0:3='b\"\\a',<3>,1:0]\n" + diff --git a/tool/test/org/antlr/v4/test/TestListeners.java b/tool/test/org/antlr/v4/test/TestListeners.java index e6b15ba3d..0d05a00ca 100644 --- a/tool/test/org/antlr/v4/test/TestListeners.java +++ b/tool/test/org/antlr/v4/test/TestListeners.java @@ -28,7 +28,7 @@ public class TestListeners extends BaseTest { "INT : [0-9]+ ;\n" + "ID : [a-z]+ ;\n" + "WS : [ \\t\\n]+ -> skip ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "1 2", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1 2", false); String expecting = "(a 1 2)\n" + "1\n" + "2\n"; @@ -61,13 +61,13 @@ public class TestListeners extends BaseTest { "INT : [0-9]+ ;\n" + "ID : [a-z]+ ;\n" + "WS : [ \\t\\n]+ -> skip ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "1 2", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1 2", false); String expecting = "(a 1 2)\n" + "1 2 [1, 2]\n"; assertEquals(expecting, result); - result = execParser("T.g", grammar, "TParser", "TLexer", "s", "abc", false); + result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "abc", false); expecting = "(a abc)\n" + "[@0,0:2='abc',<6>,1:0]\n"; assertEquals(expecting, result); @@ -103,12 +103,12 @@ public class TestListeners extends BaseTest { "INT : [0-9]+ ;\n" + "ID : [a-z]+ ;\n" + "WS : [ \\t\\n]+ -> skip ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "1 2", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1 2", false); String expecting = "(a (b 1) (b 2))\n" + "1 2 1\n"; assertEquals(expecting, result); - result = execParser("T.g", grammar, "TParser", "TLexer", "s", "abc", false); + result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "abc", false); expecting = "(a (b abc))\n" + "abc\n"; assertEquals(expecting, result); @@ -145,7 +145,7 @@ public class TestListeners extends BaseTest { "ADD : '+' ;\n" + "INT : [0-9]+ ;\n" + "WS : [ \\t\\n]+ -> skip ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "1+2*3", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1+2*3", false); String expecting = "(e (e 1) + (e (e 2) * (e 3)))\n" + "1\n" + @@ -186,7 +186,7 @@ public class TestListeners extends BaseTest { "ADD : '+' ;\n" + "INT : [0-9]+ ;\n" + "WS : [ \\t\\n]+ -> skip ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "1(2,3)", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1(2,3)", false); String expecting = "(e (e 1) ( (eList (e 2) , (e 3)) ))\n" + "1\n" + diff --git a/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java b/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java index 8ad547ba6..23b6ab35f 100644 --- a/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java +++ b/tool/test/org/antlr/v4/test/TestNonGreedyLoops.java @@ -40,14 +40,14 @@ public class TestNonGreedyLoops extends BaseTest { "INT : '0'..'9'+ ;\n" + "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n')+ {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x", true); assertEquals("x\n" + "Decision 0:\n" + "s0-ID->:s1=>2\n", found); assertEquals(null, this.stderrDuringParse); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "34 x", true); assertEquals("34x\n" + "Decision 0:\n" + @@ -63,7 +63,7 @@ public class TestNonGreedyLoops extends BaseTest { "INT : '0'..'9'+ ;\n" + "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n')+ {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x", true); assertEquals("x\n" + "Decision 0:\n" + @@ -80,7 +80,7 @@ public class TestNonGreedyLoops extends BaseTest { "INT : '0'..'9'+ ;\n" + "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n')+ {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x", true); assertEquals("alt 1\n" + "Decision 0:\n" + @@ -91,7 +91,7 @@ public class TestNonGreedyLoops extends BaseTest { "s0-ID->:s1=>2\n", found); assertEquals(null, this.stderrDuringParse); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "34", true); assertEquals("alt 2\n" + "Decision 0:\n" + @@ -102,7 +102,7 @@ public class TestNonGreedyLoops extends BaseTest { "s0-INT->:s1=>2\n", found); assertEquals(null, this.stderrDuringParse); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "34 x", true); assertEquals("alt 1\n" + "Decision 0:\n" + @@ -125,7 +125,7 @@ public class TestNonGreedyLoops extends BaseTest { "INT : '0'..'9'+ ;\n" + "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n')+ {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "2 3 x", true); assertEquals("alt 1\n" + "Decision 0:\n" + @@ -139,7 +139,7 @@ public class TestNonGreedyLoops extends BaseTest { "s0-ID->:s2=>2\n", found); assertEquals(null, this.stderrDuringParse); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "2 3", true); assertEquals("alt 2\n" + "Decision 0:\n" + @@ -151,7 +151,7 @@ public class TestNonGreedyLoops extends BaseTest { "s0-INT->:s1=>2\n", found); assertEquals("line 1:0 no viable alternative at input '2'\n", this.stderrDuringParse); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a b c 3", true); assertEquals("alt 2\n" + "Decision 0:\n" + @@ -176,14 +176,14 @@ public class TestNonGreedyLoops extends BaseTest { "INT : '0'..'9'+ ;\n" + "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n')+ {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x", true); assertEquals("alt 1\n" + "Decision 0:\n" + "s0-ID->:s1=>1\n", found); assertNull(this.stderrDuringParse); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "34", true); assertEquals("alt 1\n" + "Decision 0:\n" + @@ -204,14 +204,14 @@ public class TestNonGreedyLoops extends BaseTest { "WS : (' '|'\\n')+ {skip();} ;\n"; String input = "{ }"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("{}\n" + "Decision 0:\n" + "s0-'}'->:s1=>2\n", found); input = "{a b { }"; - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("{ab{}\n" + "Decision 0:\n" + @@ -220,7 +220,7 @@ public class TestNonGreedyLoops extends BaseTest { "s0-ID->:s1=>1\n", found); input = "{ } a 2 { }"; // FAILS to match since it terminates loop at first { } - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("", found); // should not print output; resync kills rest of input til '}' then returns normally } @@ -237,7 +237,7 @@ public class TestNonGreedyLoops extends BaseTest { "WS : (' '|'\\n')+ {skip();} ;\n"; String input = "if ( x=34 ) { } ;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("if(x=34){};\n" + "Decision 0:\n" + @@ -249,7 +249,7 @@ public class TestNonGreedyLoops extends BaseTest { "s3-'}'->:s4=>2\n", found); input = "if ( ))) ) { } ;"; - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("if()))){};\n" + "Decision 0:\n" + @@ -259,7 +259,7 @@ public class TestNonGreedyLoops extends BaseTest { "s3-'}'->:s4=>2\n", found); input = "if (() { } a 2) { } ;"; // The first { } should match block so should stop - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("", found); // should not finish to print output } @@ -276,7 +276,7 @@ public class TestNonGreedyLoops extends BaseTest { "WS : (' '|'\\n')+ {skip();} ;\n"; String input = "if ( x=34 ) { {return a} b 34 } ;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("if(x=34){{returna}b34};\n" + "Decision 0:\n" + @@ -302,7 +302,7 @@ public class TestNonGreedyLoops extends BaseTest { input = "if ( ()) ) { {return a} b 34 } ;"; - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("if(())){{returna}b34};\n" + "Decision 0:\n" + @@ -344,7 +344,7 @@ public class TestNonGreedyLoops extends BaseTest { String input = "x=1; a=b;"; String found = null; - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("x=1;a=b;\n" + "Decision 0:\n" + @@ -356,7 +356,7 @@ public class TestNonGreedyLoops extends BaseTest { "s5-EOF->:s6=>2\n", found); input = "if ( 1 ) { x=3; { return 4; } } return 99; abc=def;"; - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("if(1){x=3;{return4;}}return99;abc=def;\n" + "Decision 0:\n" + @@ -369,7 +369,7 @@ public class TestNonGreedyLoops extends BaseTest { "s6-EOF->:s7=>2\n", found); input = "x=1; a=3;"; // FAILS to match since it can't match last element - execParser("T.g", grammar, "TParser", "TLexer", "s", + execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); // can't match EOF to ID '=' '3' ';' assertEquals("line 1:9 no viable alternative at input ''\n", @@ -377,7 +377,7 @@ public class TestNonGreedyLoops extends BaseTest { input = "x=1; a=b; z=3;"; // FAILS to match since it can't match last element - execParser("T.g", grammar, "TParser", "TLexer", "s", + execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("line 1:14 no viable alternative at input ''\n", this.stderrDuringParse); @@ -406,7 +406,7 @@ public class TestNonGreedyLoops extends BaseTest { String input = "x=1; a=b; x=y;"; String found = null; - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("x=1;a=b;\n" + "Decision 0:\n" + @@ -417,7 +417,7 @@ public class TestNonGreedyLoops extends BaseTest { "s4-';'->:s5=>2\n", found); // ignores x=1 that follows first a=b assignment input = "if ( 1 ) { x=3; { return 4; } } return 99; abc=def;"; - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("if(1){x=3;{return4;}}return99;abc=def;\n" + "Decision 0:\n" + @@ -429,14 +429,14 @@ public class TestNonGreedyLoops extends BaseTest { "s5-';'->:s6=>2\n", found); input = "x=1; a=3;"; // FAILS to match since it can't match either stat - execParser("T.g", grammar, "TParser", "TLexer", "s", + execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); // can't match EOF to ID '=' '0' ';' assertEquals("line 1:9 no viable alternative at input ''\n", this.stderrDuringParse); input = "x=1; a=b; z=3;"; // stops at a=b; ignores z=3; - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); assertEquals("x=1;a=b;\n" + "Decision 0:\n" + @@ -461,7 +461,7 @@ public class TestNonGreedyLoops extends BaseTest { "WS : (' '|'\\n') {skip();} ;\n"; String found = null; - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "foo", true); assertEquals("foo\n" + "Decision 1:\n" + @@ -489,7 +489,7 @@ public class TestNonGreedyLoops extends BaseTest { "line 1:7 reportAmbiguity d=2: ambigAlts={1..2}, input='/'\n", this.stderrDuringParse); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "", true); assertEquals("\n" + "Decision 1:\n" + @@ -508,7 +508,7 @@ public class TestNonGreedyLoops extends BaseTest { "Decision 3:\n" + "s0-'>'->:s2=>2\n" + "s0-ID->:s1=>1\n", found); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "", true); assertEquals("\n" + "Decision 1:\n" + @@ -556,7 +556,7 @@ public class TestNonGreedyLoops extends BaseTest { String found = null; System.out.println(grammar); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", ",=foo 32skidoo", true); assertEquals("\n" + "\n" + @@ -582,7 +582,7 @@ public class TestNonGreedyLoops extends BaseTest { assertEquals(null, this.stderrDuringParse); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x x", true); assertEquals("\n" + "Decision 0:\n" + @@ -599,7 +599,7 @@ public class TestNonGreedyLoops extends BaseTest { // gets line 1:3 no viable alternative at input '>'. Why?? // oH! it sees .+ and figures it matches > so <> predicts tag CORRECT! // Seeing '.' in a lookahead prediction can be misleading!! - found = execParser("T.g", grammar, "TParser", "TLexer", "s", + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x <>", true); assertEquals("<\n" + "\n" + diff --git a/tool/test/org/antlr/v4/test/TestParseErrors.java b/tool/test/org/antlr/v4/test/TestParseErrors.java index 924e09fe4..62c8cf67f 100644 --- a/tool/test/org/antlr/v4/test/TestParseErrors.java +++ b/tool/test/org/antlr/v4/test/TestParseErrors.java @@ -37,7 +37,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' 'b' ;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "aa", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aa", false); String expecting = "line 1:1 mismatched input 'a' expecting 'b'\n"; String result = stderrDuringParse; assertEquals(expecting, result); @@ -47,7 +47,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' 'b' ;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "aab", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aab", false); String expecting = "line 1:1 extraneous input 'a' expecting 'b'\n"; String result = stderrDuringParse; assertEquals(expecting, result); @@ -57,7 +57,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' ('b'|'c') ;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "aab", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aab", false); String expecting = "line 1:1 extraneous input 'a' expecting {'b', 'c'}\n"; String result = stderrDuringParse; assertEquals(expecting, result); @@ -67,7 +67,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' 'b' 'c' ;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "ac", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ac", false); String expecting = "line 1:1 missing 'b' at 'c'\n"; String result = stderrDuringParse; assertEquals(expecting, result); @@ -77,7 +77,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' x='b' {System.out.println(\"conjured=\"+$x);} 'c' ;"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "a", "ac", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ac", false); String expecting = "conjured=[@-1,-1:-1='',<3>,1:1]\n"; assertEquals(expecting, result); } @@ -86,7 +86,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' ('b'|'c') 'd' ;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "ad", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ad", false); String expecting = "line 1:1 missing {'b', 'c'} at 'd'\n"; String result = stderrDuringParse; assertEquals(expecting, result); @@ -96,7 +96,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' x=('b'|'c') {System.out.println(\"conjured=\"+$x);} 'd' ;"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "a", "ad", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ad", false); String expecting = "conjured=[@-1,-1:-1='',<3>,1:1]\n"; assertEquals(expecting, result); } @@ -108,7 +108,7 @@ public class TestParseErrors extends BaseTest { " | 'a' 'c'" + ";\n" + "q : 'e' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "ae", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ae", false); String expecting = "line 1:1 no viable alternative at input 'ae'\n"; String result = stderrDuringParse; assertEquals(expecting, result); @@ -122,7 +122,7 @@ public class TestParseErrors extends BaseTest { " ;\n" + "q : 'e' ;\n"; System.out.println(grammar); - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "abe", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abe", false); String expecting = "line 1:2 no viable alternative at input 'abe'\n"; String result = stderrDuringParse; assertEquals(expecting, result); @@ -135,7 +135,7 @@ public class TestParseErrors extends BaseTest { " | 'a'+ 'c'" + ";\n" + "q : 'e' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "aaae", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aaae", false); String expecting = "line 1:3 no viable alternative at input 'aaae'\n"; String result = stderrDuringParse; assertEquals(expecting, result); @@ -145,7 +145,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' 'b'*;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "aabc", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aabc", false); String expecting = "line 1:1 extraneous input 'a' expecting {, 'b'}\n" + "line 1:3 token recognition error at: 'c'\n"; String result = stderrDuringParse; @@ -157,7 +157,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' 'b'* 'c';"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "aacabc", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aacabc", false); String expecting = "line 1:1 extraneous input 'a' expecting {'b', 'c'}\n"; String result = stderrDuringParse; @@ -168,7 +168,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' 'b'* 'c' ;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "ababbc", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ababbc", false); String expecting = "line 1:2 extraneous input 'a' expecting {'b', 'c'}\n"; String result = stderrDuringParse; assertEquals(expecting, result); @@ -178,7 +178,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' 'b'* 'c' ;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "abaaababc", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaaababc", false); String expecting = "line 1:2 extraneous input 'a' expecting {'b', 'c'}\n" + "line 1:6 extraneous input 'a' expecting {'b', 'c'}\n"; @@ -192,7 +192,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' ('b'|'z'{;})*;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "aabc", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aabc", false); String expecting = "line 1:1 extraneous input 'a' expecting {, 'b', 'z'}\n" + "line 1:3 token recognition error at: 'c'\n"; String result = stderrDuringParse; @@ -204,7 +204,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' ('b'|'z'{;})* 'c';"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "aacabc", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aacabc", false); String expecting = "line 1:1 extraneous input 'a' expecting {'b', 'z', 'c'}\n"; String result = stderrDuringParse; @@ -215,7 +215,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' ('b'|'z'{;})* 'c' ;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "ababbc", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ababbc", false); String expecting = "line 1:2 extraneous input 'a' expecting {'b', 'z', 'c'}\n"; String result = stderrDuringParse; assertEquals(expecting, result); @@ -225,7 +225,7 @@ public class TestParseErrors extends BaseTest { String grammar = "grammar T;\n" + "a : 'a' ('b'|'z'{;})* 'c' ;"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "abaaababc", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaaababc", false); String expecting = "line 1:2 extraneous input 'a' expecting {'b', 'z', 'c'}\n" + "line 1:6 extraneous input 'a' expecting {'b', 'z', 'c'}\n"; @@ -249,7 +249,7 @@ public class TestParseErrors extends BaseTest { "@init\n" + "{ System.out.println(getExpectedTokens().toString(tokenNames)); }\n" + " : ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "start", "dog and software", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "start", "dog and software", false); String expecting = "{'hardware', 'software'}\n"; assertEquals(expecting, result); } @@ -271,7 +271,7 @@ public class TestParseErrors extends BaseTest { "primary : ID;\n" + "ID : [a-z]+;\n" + "\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "start", "x:x", true); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "start", "x:x", true); String expecting = ""; assertEquals(expecting, result); assertNull(this.stderrDuringParse); diff --git a/tool/test/org/antlr/v4/test/TestParseTrees.java b/tool/test/org/antlr/v4/test/TestParseTrees.java index e9418ed4b..a6d11bb87 100644 --- a/tool/test/org/antlr/v4/test/TestParseTrees.java +++ b/tool/test/org/antlr/v4/test/TestParseTrees.java @@ -11,7 +11,7 @@ public class TestParseTrees extends BaseTest { "@after {System.out.println($r.ctx.toStringTree(this));}\n" + " :r=a ;\n" + "a : 'x' {System.out.println(getRuleInvocationStack());} ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "x", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x", false); String expecting = "[a, s]\n(a x)\n"; assertEquals(expecting, result); } @@ -25,7 +25,7 @@ public class TestParseTrees extends BaseTest { " :r=a ;\n" + "a : 'x' 'y'\n" + " ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "xy", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xy", false); String expecting = "(a x y)\n"; assertEquals(expecting, result); } @@ -39,7 +39,7 @@ public class TestParseTrees extends BaseTest { " :r=a ;\n" + "a : 'x' | 'y'\n" + " ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "y", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "y", false); String expecting = "(a y)\n"; assertEquals(expecting, result); } @@ -53,7 +53,7 @@ public class TestParseTrees extends BaseTest { " :r=a ;\n" + "a : ('x' | 'y')* 'z'\n" + " ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "xyyxyxz", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xyyxyxz", false); String expecting = "(a x y y x y x z)\n"; assertEquals(expecting, result); } @@ -68,7 +68,7 @@ public class TestParseTrees extends BaseTest { "a : b 'x'\n" + " ;\n" + "b : 'y' ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "yx", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "yx", false); String expecting = "(a (b y) x)\n"; assertEquals(expecting, result); } @@ -85,7 +85,7 @@ public class TestParseTrees extends BaseTest { "a : 'x' 'y'\n" + " ;\n" + "Z : 'z'; \n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "xzy", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xzy", false); String expecting = "(a x z y)\n"; // ERRORs not shown. z is colored red in tree view assertEquals(expecting, result); } @@ -100,7 +100,7 @@ public class TestParseTrees extends BaseTest { "a : 'x' | 'y'\n" + " ;\n" + "Z : 'z'; \n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "z", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "z", false); String expecting = "(a z)\n"; assertEquals(expecting, result); } @@ -115,7 +115,7 @@ public class TestParseTrees extends BaseTest { "a : 'x' 'y'* '!'\n" + " ;\n" + "Z : 'z'; \n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "xzyy!", false); + String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xzyy!", false); String expecting = "(a x z y y !)\n"; assertEquals(expecting, result); } diff --git a/tool/test/org/antlr/v4/test/TestParserExec.java b/tool/test/org/antlr/v4/test/TestParserExec.java index bd71eecfd..0df311bc6 100644 --- a/tool/test/org/antlr/v4/test/TestParserExec.java +++ b/tool/test/org/antlr/v4/test/TestParserExec.java @@ -43,7 +43,7 @@ public class TestParserExec extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abc 34", false); assertEquals("", found); assertEquals(null, stderrDuringParse); @@ -57,7 +57,7 @@ public class TestParserExec extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abc 34", false); assertEquals("abc34\n", found); } @@ -72,7 +72,7 @@ public class TestParserExec extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "34", false); assertEquals("alt 2\n", found); } @@ -84,7 +84,7 @@ public class TestParserExec extends BaseTest { "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "a b c", false); assertEquals("abc\n", found); } @@ -97,7 +97,7 @@ public class TestParserExec extends BaseTest { "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "a b c", false); assertEquals("abc\n", found); } @@ -109,10 +109,10 @@ public class TestParserExec extends BaseTest { "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "", false); assertEquals("\n", found); - found = execParser("T.g", grammar, "TParser", "TLexer", "a", + found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "a b c", false); assertEquals("abc\n", found); } @@ -125,10 +125,10 @@ public class TestParserExec extends BaseTest { "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "", false); assertEquals("\n", found); - found = execParser("T.g", grammar, "TParser", "TLexer", "a", + found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "a b c", false); assertEquals("abc\n", found); } @@ -141,7 +141,7 @@ public class TestParserExec extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "a 34 c", false); assertEquals("a34c\n", found); } @@ -154,10 +154,10 @@ public class TestParserExec extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "", false); assertEquals("\n", found); - found = execParser("T.g", grammar, "TParser", "TLexer", "a", + found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "a 34 c", false); assertEquals("a34c\n", found); } @@ -175,19 +175,19 @@ public class TestParserExec extends BaseTest { "a : 'a' s ('b' s)?;\n" ; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", "x", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x", false); assertEquals("", found); assertNull(this.stderrDuringParse); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", "axbx", false); + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "axbx", false); assertEquals("", found); assertNull(this.stderrDuringParse); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", "ax", false); + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "ax", false); assertEquals("", found); assertNull(this.stderrDuringParse); - found = execParser("T.g", grammar, "TParser", "TLexer", "s", "aaxbx", false); + found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "aaxbx", false); assertEquals("", found); assertNull(this.stderrDuringParse); } @@ -207,7 +207,7 @@ public class TestParserExec extends BaseTest { "WS : (' ' | '\\t')+ -> skip;\n" ; - String found = execParser("T.g", grammar, "TParser", "TLexer", "stmt", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "stmt", "if x if x a else b", true); String expecting = ""; assertEquals(expecting, found); diff --git a/tool/test/org/antlr/v4/test/TestPerformance.java b/tool/test/org/antlr/v4/test/TestPerformance.java index 98215eaea..84773cb7a 100644 --- a/tool/test/org/antlr/v4/test/TestPerformance.java +++ b/tool/test/org/antlr/v4/test/TestPerformance.java @@ -78,8 +78,8 @@ public class TestPerformance extends BaseTest { private static final boolean RECURSIVE = true; /** - * True to use the Java grammar with expressions in the v4 left-recursive syntax (Java-LR.g). False to use - * the standard grammar (Java.g). In either case, the grammar is renamed in the temporary directory to Java.g + * True to use the Java grammar with expressions in the v4 left-recursive syntax (Java-LR.g4). False to use + * the standard grammar (Java.g4). In either case, the grammar is renamed in the temporary directory to Java.g4 * before compiling. */ private static final boolean USE_LR_GRAMMAR = true; @@ -421,8 +421,8 @@ public class TestPerformance extends BaseTest { } protected void compileJavaParser(boolean leftRecursive) throws IOException { - String grammarFileName = "Java.g"; - String sourceName = leftRecursive ? "Java-LR.g" : "Java.g"; + String grammarFileName = "Java.g4"; + String sourceName = leftRecursive ? "Java-LR.g4" : "Java.g4"; String body = load(sourceName, null); @SuppressWarnings({"ConstantConditions"}) List extraOptions = new ArrayList(); diff --git a/tool/test/org/antlr/v4/test/TestSemPredEvalLexer.java b/tool/test/org/antlr/v4/test/TestSemPredEvalLexer.java index 3e2c9f64e..0606462b2 100644 --- a/tool/test/org/antlr/v4/test/TestSemPredEvalLexer.java +++ b/tool/test/org/antlr/v4/test/TestSemPredEvalLexer.java @@ -11,7 +11,7 @@ public class TestSemPredEvalLexer extends BaseTest { "E2 : {true}? 'enum' ;\n" + // winner not E1 or ID "ID : 'a'..'z'+ ;\n"+ "WS : (' '|'\\n') {skip();} ;"; - String found = execLexer("L.g", grammar, "L", "enum abc", true); + String found = execLexer("L.g4", grammar, "L", "enum abc", true); String expecting = "[@0,0:3='enum',<4>,1:0]\n" + "[@1,5:7='abc',<5>,1:5]\n" + @@ -26,7 +26,7 @@ public class TestSemPredEvalLexer extends BaseTest { "E2 : 'enum' {true}? ;\n" + // winner not E1 or ID "ID : 'a'..'z'+ ;\n"+ "WS : (' '|'\\n') {skip();} ;"; - String found = execLexer("L.g", grammar, "L", "enum abc enum", true); + String found = execLexer("L.g4", grammar, "L", "enum abc enum", true); String expecting = "[@0,0:3='enum',<4>,1:0]\n" + "[@1,5:7='abc',<5>,1:5]\n" + @@ -50,7 +50,7 @@ public class TestSemPredEvalLexer extends BaseTest { "B : {int n=0;} ({n<=2}? DIGIT {n++})+ ;\n" + "fragment DIGIT : '0'..'9' ;\n"+ "WS : (' '|'\\n') {skip();} ;"; - String found = execLexer("L.g", grammar, "L", "1234 56", true); + String found = execLexer("L.g4", grammar, "L", "1234 56", true); String expecting = "[@0,0:3='enum',<4>,1:0]\n" + "[@1,5:7='abc',<5>,1:5]\n" + diff --git a/tool/test/org/antlr/v4/test/TestSemPredEvalParser.java b/tool/test/org/antlr/v4/test/TestSemPredEvalParser.java index 5bf09dafb..531a714c1 100644 --- a/tool/test/org/antlr/v4/test/TestSemPredEvalParser.java +++ b/tool/test/org/antlr/v4/test/TestSemPredEvalParser.java @@ -45,7 +45,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "3 4 x", false); String expecting = "alt 2\n" + @@ -70,7 +70,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x ; y", false); String expecting = ""; assertEquals(expecting, found); @@ -95,7 +95,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x y 3", false); String expecting = "alt 2\n" + @@ -120,7 +120,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x y", false); String expecting = "alt 1\n" + @@ -147,7 +147,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x; y", true); String expecting = "alt 1\n" + @@ -180,7 +180,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "34; x; y", true); String expecting = "alt 1\n" + @@ -208,7 +208,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "y 3 x 4", false); String expecting = "alt 2\n" + @@ -229,7 +229,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - execParser("T.g", grammar, "TParser", "TLexer", "s", + execParser("T.g4", grammar, "TParser", "TLexer", "s", "y 3 x 4", false); String expecting = "line 1:0 no viable alternative at input 'y'\n"; String result = stderrDuringParse; @@ -247,7 +247,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x x y", false); String expecting = "alt 2\n" + @@ -272,7 +272,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x 4", false); String expecting = "alt 1\n"; @@ -295,7 +295,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x x y", false); String expecting = "alt 1\n" + @@ -321,7 +321,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x x y", false); String expecting = "i=1\n" + @@ -352,7 +352,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a b", false); String expecting = "alt 2\n" + @@ -382,7 +382,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a b", false); String expecting = ""; @@ -403,7 +403,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a;", false); String expecting = "alt 2\n"; @@ -423,7 +423,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a;", false); String expecting = "alt 2\n"; @@ -447,7 +447,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a!", false); String expecting = "eval=true\n" + // now we are parsing @@ -473,7 +473,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a!", false); String expecting = "eval=true\n" + @@ -500,7 +500,7 @@ public class TestSemPredEvalParser extends BaseTest { "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "s", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a!", false); String expecting = "eval=true\n" + diff --git a/tool/test/org/antlr/v4/test/TestSets.java b/tool/test/org/antlr/v4/test/TestSets.java index 2abf50bfb..412ca3e3c 100644 --- a/tool/test/org/antlr/v4/test/TestSets.java +++ b/tool/test/org/antlr/v4/test/TestSets.java @@ -46,7 +46,7 @@ public class TestSets extends BaseTest { "fragment A : '1' | '2';\n" + "fragment B : '3' '4';\n" + "C : A | B;\n"; - String found = execParser("P.g", grammar, "PParser", "PLexer", + String found = execParser("P.g4", grammar, "PParser", "PLexer", "a", "34", debug); assertEquals("34\n", found); } @@ -55,7 +55,7 @@ public class TestSets extends BaseTest { String grammar = "grammar T;\n" + "a : t=('x'|'y') {System.out.println($t.text);} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", debug); assertEquals("x\n", found); } @@ -64,7 +64,7 @@ public class TestSets extends BaseTest { String grammar = "grammar T;\n" + "a : t=~('x'|'y') 'z' {System.out.println($t.text);} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "zz", debug); assertEquals("z\n", found); } @@ -73,7 +73,7 @@ public class TestSets extends BaseTest { String grammar = "grammar T;\n" + "a : ~'x' 'z' {System.out.println(_input);} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "zz", debug); assertEquals("zz\n", found); } @@ -82,7 +82,7 @@ public class TestSets extends BaseTest { String grammar = "grammar T;\n" + "a : t=~'x' 'z' {System.out.println($t.text);} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "zz", debug); assertEquals("z\n", found); } @@ -91,7 +91,7 @@ public class TestSets extends BaseTest { String grammar = "grammar T;\n" + "a @after {System.out.println(_input);} : 'a' | 'b' |'c' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "b", debug); assertEquals("b\n", found); } @@ -101,7 +101,7 @@ public class TestSets extends BaseTest { "grammar T;\n" + "a : A {System.out.println($A.text);} ;\n" + "A : ~'b' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", debug); assertEquals("x\n", found); } @@ -111,7 +111,7 @@ public class TestSets extends BaseTest { "grammar T;\n" + "a : A? 'c' {System.out.println(_input);} ;\n" + "A : 'b' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "bc", debug); assertEquals("bc\n", found); } @@ -121,7 +121,7 @@ public class TestSets extends BaseTest { "grammar T;\n" + "a : A {System.out.println(_input);} ;\n" + "A : 'b'? 'c' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "bc", debug); assertEquals("bc\n", found); } @@ -131,10 +131,10 @@ public class TestSets extends BaseTest { "grammar T;\n" + "a : A {System.out.println(_input);} ;\n" + "A : 'b'* 'c' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "bbbbc", debug); assertEquals("bbbbc\n", found); - found = execParser("T.g", grammar, "TParser", "TLexer", + found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "c", debug); assertEquals("c\n", found); } @@ -144,7 +144,7 @@ public class TestSets extends BaseTest { "grammar T;\n" + "a : A {System.out.println(_input);} ;\n" + "A : 'b'+ 'c' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "bbbbc", debug); assertEquals("bbbbc\n", found); } @@ -153,7 +153,7 @@ public class TestSets extends BaseTest { String grammar = "grammar T;\n" + "a : ('a'|'b')? 'c' {System.out.println(_input);} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ac", debug); assertEquals("ac\n", found); } @@ -162,7 +162,7 @@ public class TestSets extends BaseTest { String grammar = "grammar T;\n" + "a : ('a'|'b')* 'c' {System.out.println(_input);} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaac", debug); assertEquals("abaac\n", found); } @@ -171,7 +171,7 @@ public class TestSets extends BaseTest { String grammar = "grammar T;\n" + "a : ('a'|'b')+ 'c' {System.out.println(_input);} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaac", debug); assertEquals("abaac\n", found); } @@ -181,7 +181,7 @@ public class TestSets extends BaseTest { "grammar T;\n" + "a : A {System.out.println(_input);} ;\n" + "A : ('a'|'b')? 'c' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ac", debug); assertEquals("ac\n", found); } @@ -191,7 +191,7 @@ public class TestSets extends BaseTest { "grammar T;\n" + "a : A {System.out.println(_input);} ;\n" + "A : ('a'|'b')* 'c' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaac", debug); assertEquals("abaac\n", found); } @@ -201,7 +201,7 @@ public class TestSets extends BaseTest { "grammar T;\n" + "a : A {System.out.println(_input);} ;\n" + "A : ('a'|'b')+ 'c' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaac", debug); assertEquals("abaac\n", found); } @@ -211,7 +211,7 @@ public class TestSets extends BaseTest { "grammar T;\n" + "a : A {System.out.println($A.text);} ;\n" + "A : ~('b'|'c') ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", debug); assertEquals("x\n", found); } @@ -221,7 +221,7 @@ public class TestSets extends BaseTest { "grammar T;\n" + "a : A {System.out.println($A.text);} ;\n" + "A : h=~('b'|'c') ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", debug); assertEquals("x\n", found); } @@ -232,7 +232,7 @@ public class TestSets extends BaseTest { "a : A {System.out.println($A.text);} ;\n" + "A : ~('a'|B) ;\n" + "B : 'b' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", debug); assertEquals("x\n", found); } @@ -243,7 +243,7 @@ public class TestSets extends BaseTest { "a : A {System.out.println($A.text);} ;\n" + "A : ~('a'|B) ;\n" + "B : 'b'|'c' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", debug); assertEquals("x\n", found); } @@ -255,7 +255,7 @@ public class TestSets extends BaseTest { "A : ('a'|B) ;\n" + "fragment\n" + "B : ~('a'|'c') ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", debug); assertEquals("x\n", found); } @@ -269,7 +269,7 @@ public class TestSets extends BaseTest { "B : ~('a'|C) ;\n" + "fragment\n" + "C : 'c'|'d' ;\n "; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", debug); assertEquals("x\n", found); } @@ -280,7 +280,7 @@ public class TestSets extends BaseTest { "a : (A {System.out.println($A.text);})+ ;\n" + "A : [AaBb] ;\n" + "WS : (' '|'\\n')+ {skip();} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "A a B b", debug); assertEquals("A\n" + "a\n" + diff --git a/tool/test/org/antlr/v4/test/TestSymbolIssues.java b/tool/test/org/antlr/v4/test/TestSymbolIssues.java index 31b401640..a7af0fe8c 100644 --- a/tool/test/org/antlr/v4/test/TestSymbolIssues.java +++ b/tool/test/org/antlr/v4/test/TestSymbolIssues.java @@ -20,15 +20,15 @@ public class TestSymbolIssues extends BaseTest { "\n" + "ID : 'a'..'z'+ ID ;", // YIELDS - "warning(48): A.g:2:10: illegal option opt\n" + - "warning(48): A.g:2:21: illegal option k\n" + - "error(59): A.g:7:1: redefinition of header action\n" + - "warning(51): A.g:2:10: illegal option opt\n" + - "error(19): A.g:11:0: rule a redefinition\n" + - "error(60): A.g:5:1: redefinition of members action\n" + - "error(47): A.g:9:37: rule b has no defined parameters\n" + - "error(24): A.g:9:43: reference to undefined rule: q\n" + - "error(46): A.g:10:31: missing parameter(s) on rule reference: a\n" + "warning(48): A.g4:2:10: illegal option opt\n" + + "warning(48): A.g4:2:21: illegal option k\n" + + "error(59): A.g4:7:1: redefinition of header action\n" + + "warning(51): A.g4:2:10: illegal option opt\n" + + "error(19): A.g4:11:0: rule a redefinition\n" + + "error(60): A.g4:5:1: redefinition of members action\n" + + "error(47): A.g4:9:37: rule b has no defined parameters\n" + + "error(24): A.g4:9:43: reference to undefined rule: q\n" + + "error(46): A.g4:10:31: missing parameter(s) on rule reference: a\n" }; static String[] B = { @@ -42,11 +42,11 @@ public class TestSymbolIssues extends BaseTest { "\n" + "s : FOO ;", // YIELDS - "error(25): B.g:2:9: can't assign string value to token name X in non-combined grammar\n" + - "error(35): B.g:4:4: label s conflicts with rule with same name\n" + - "error(35): B.g:4:9: label b conflicts with rule with same name\n" + - "error(36): B.g:4:15: label X conflicts with token with same name\n" + - "error(40): B.g:6:9: label x type mismatch with previous definition: TOKEN_LIST_LABEL!=TOKEN_LABEL\n" + "error(25): B.g4:2:9: can't assign string value to token name X in non-combined grammar\n" + + "error(35): B.g4:4:4: label s conflicts with rule with same name\n" + + "error(35): B.g4:4:9: label b conflicts with rule with same name\n" + + "error(36): B.g4:4:15: label X conflicts with token with same name\n" + + "error(40): B.g4:6:9: label x type mismatch with previous definition: TOKEN_LIST_LABEL!=TOKEN_LABEL\n" }; static String[] D = { @@ -61,8 +61,8 @@ public class TestSymbolIssues extends BaseTest { " : ID ;", // YIELDS - "error(37): D.g:3:21: label j conflicts with rule a's return value or parameter with same name\n" + - "error(41): D.g:5:0: rule b's argument i conflicts a return value with same name\n" + "error(37): D.g4:3:21: label j conflicts with rule a's return value or parameter with same name\n" + + "error(41): D.g4:5:0: rule b's argument i conflicts a return value with same name\n" }; static String[] E = { @@ -78,10 +78,10 @@ public class TestSymbolIssues extends BaseTest { "a : A ;\n", // YIELDS - "error(73): E.g:4:8: cannot redefine B; token name already defined\n" + - "error(73): E.g:5:4: cannot redefine C; token name already defined\n" + - "error(73): E.g:6:8: cannot redefine D; token name already defined\n" + - "error(72): E.g:7:8: cannot alias X='e'; string already assigned to E\n" + "error(73): E.g4:4:8: cannot redefine B; token name already defined\n" + + "error(73): E.g4:5:4: cannot redefine C; token name already defined\n" + + "error(73): E.g4:6:8: cannot redefine D; token name already defined\n" + + "error(72): E.g4:7:8: cannot alias X='e'; string already assigned to E\n" }; @Test public void testA() { super.testErrors(A, false); } diff --git a/tool/test/org/antlr/v4/test/TestTokenTypeAssignment.java b/tool/test/org/antlr/v4/test/TestTokenTypeAssignment.java index 4c166e9dd..6c4a431c1 100644 --- a/tool/test/org/antlr/v4/test/TestTokenTypeAssignment.java +++ b/tool/test/org/antlr/v4/test/TestTokenTypeAssignment.java @@ -136,7 +136,7 @@ public class TestTokenTypeAssignment extends BaseTest { "A : 'a' ;\n" + "B : '}' ;\n"+ "WS : (' '|'\\n') {skip();} ;"; - String found = execParser("P.g", grammar, "PParser", "PLexer", + String found = execParser("P.g4", grammar, "PParser", "PLexer", "a", "a}", false); assertEquals("a}\n", found); } @@ -151,7 +151,7 @@ public class TestTokenTypeAssignment extends BaseTest { "A : 'a' ;\n" + "B : '}' ;\n"+ "WS : (' '|'\\n') {skip();} ;"; - String found = execParser("P.g", grammar, "PParser", "PLexer", + String found = execParser("P.g4", grammar, "PParser", "PLexer", "a", "a}", false); assertEquals("a}\n", found); } diff --git a/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java b/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java index 2c66c496b..6b7e3fa65 100644 --- a/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java +++ b/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java @@ -8,7 +8,7 @@ public class TestToolSyntaxErrors extends BaseTest { "grammar A;\n" + "", // YIELDS - "error(64): A.g::: grammar A has no rules\n", + "error(64): A.g4::: grammar A has no rules\n", "A;", "error(16): :1:0: 'A' came as a complete surprise to me\n", @@ -23,22 +23,22 @@ public class TestToolSyntaxErrors extends BaseTest { "grammar A;\n" + "a : ID ;;\n"+ "b : B ;", - "error(16): A.g:2:8: ';' came as a complete surprise to me\n", + "error(16): A.g4:2:8: ';' came as a complete surprise to me\n", "grammar A;;\n" + "a : ID ;\n", - "error(16): A;.g:1:10: ';' came as a complete surprise to me\n", + "error(16): A;.g4:1:10: ';' came as a complete surprise to me\n", "grammar A;\n" + "a @init : ID ;\n", - "error(16): A.g:2:8: mismatched input ':' expecting ACTION while matching rule preamble\n", + "error(16): A.g4:2:8: mismatched input ':' expecting ACTION while matching rule preamble\n", "grammar A;\n" + "a ( A | B ) D ;\n" + "b : B ;", - "error(16): A.g:2:3: '(' came as a complete surprise to me while matching rule preamble\n" + - "error(16): A.g:2:11: mismatched input ')' expecting SEMI while matching a rule\n" + - "error(16): A.g:2:15: mismatched input ';' expecting COLON while matching a lexer rule\n", + "error(16): A.g4:2:3: '(' came as a complete surprise to me while matching rule preamble\n" + + "error(16): A.g4:2:11: mismatched input ')' expecting SEMI while matching a rule\n" + + "error(16): A.g4:2:15: mismatched input ';' expecting COLON while matching a lexer rule\n", }; @Test public void testA() { super.testErrors(A, true); } @@ -48,7 +48,7 @@ public class TestToolSyntaxErrors extends BaseTest { "grammar A;\n" + "a : : A ;\n" + "b : B ;", - "error(16): A.g:2:4: ':' came as a complete surprise to me while matching alternative\n", + "error(16): A.g4:2:4: ':' came as a complete surprise to me while matching alternative\n", }; super.testErrors(pair, true); } @@ -58,7 +58,7 @@ public class TestToolSyntaxErrors extends BaseTest { "grammar A;\n" + "a : A \n" + "b : B ;", - "error(16): A.g:3:0: unterminated rule (missing ';') detected at 'b :' while looking for rule element\n", + "error(16): A.g4:3:0: unterminated rule (missing ';') detected at 'b :' while looking for rule element\n", }; super.testErrors(pair, true); } @@ -68,7 +68,7 @@ public class TestToolSyntaxErrors extends BaseTest { "lexer grammar A;\n" + "A : 'a' \n" + "B : 'b' ;", - "error(16): A.g:3:0: unterminated rule (missing ';') detected at 'B :' while looking for lexer rule element\n", + "error(16): A.g4:3:0: unterminated rule (missing ';') detected at 'B :' while looking for lexer rule element\n", }; super.testErrors(pair, true); } @@ -78,7 +78,7 @@ public class TestToolSyntaxErrors extends BaseTest { "grammar A;\n" + "a : A \n" + "b[int i] returns [int y] : B ;", - "error(16): A.g:3:9: unterminated rule (missing ';') detected at 'returns int y' while looking for rule element\n" + "error(16): A.g4:3:9: unterminated rule (missing ';') detected at 'returns int y' while looking for rule element\n" }; super.testErrors(pair, true); } @@ -90,7 +90,7 @@ public class TestToolSyntaxErrors extends BaseTest { " catch [Exception e] {...}\n" + "b : B ;\n", - "error(16): A.g:2:4: unterminated rule (missing ';') detected at 'b catch' while looking for rule element\n" + "error(16): A.g4:2:4: unterminated rule (missing ';') detected at 'b catch' while looking for rule element\n" }; super.testErrors(pair, true); } @@ -101,7 +101,7 @@ public class TestToolSyntaxErrors extends BaseTest { "a : A \n" + " catch [Exception e] {...}\n", - "error(16): A.g:2:4: unterminated rule (missing ';') detected at 'A catch' while looking for rule element\n" + "error(16): A.g4:2:4: unterminated rule (missing ';') detected at 'A catch' while looking for rule element\n" }; super.testErrors(pair, true); } @@ -112,7 +112,7 @@ public class TestToolSyntaxErrors extends BaseTest { "a @ options {k=1;} : A ;\n" + "b : B ;", - "error(16): A.g:2:4: 'options {' came as a complete surprise to me while looking for an identifier\n" + "error(16): A.g4:2:4: 'options {' came as a complete surprise to me while looking for an identifier\n" }; super.testErrors(pair, true); } @@ -123,7 +123,7 @@ public class TestToolSyntaxErrors extends BaseTest { "a } : A ;\n" + "b : B ;", - "error(16): A.g:2:2: '}' came as a complete surprise to me while matching rule preamble\n" + "error(16): A.g4:2:2: '}' came as a complete surprise to me while matching rule preamble\n" }; super.testErrors(pair, true); } @@ -135,8 +135,8 @@ public class TestToolSyntaxErrors extends BaseTest { "mode foo;\n" + "b : B ;", - "error(16): A.g:4:0: 'b' came as a complete surprise to me\n" + - "error(16): A.g:4:6: mismatched input ';' expecting COLON while matching a lexer rule\n" + "error(16): A.g4:4:0: 'b' came as a complete surprise to me\n" + + "error(16): A.g4:4:6: mismatched input ';' expecting COLON while matching a lexer rule\n" }; super.testErrors(pair, true); } From 9fbe9b6e21e306820c340ba29a177644c28d9775 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Fri, 30 Mar 2012 13:00:31 -0700 Subject: [PATCH 32/58] op=(x|y) works as left-recur binary op now. --- .../antlr/v4/parse/LeftRecursiveRuleWalker.g | 21 ++++++++++--------- .../org/antlr/v4/test/TestLeftRecursion.java | 16 +++++++------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/tool/src/org/antlr/v4/parse/LeftRecursiveRuleWalker.g b/tool/src/org/antlr/v4/parse/LeftRecursiveRuleWalker.g index b02745302..679d7daa5 100644 --- a/tool/src/org/antlr/v4/parse/LeftRecursiveRuleWalker.g +++ b/tool/src/org/antlr/v4/parse/LeftRecursiveRuleWalker.g @@ -128,17 +128,18 @@ outerAlternative returns [boolean isLeftRec] {otherAlt((AltAST)$start, currentOuterAltNumber);} ; -binary - : ^( ALT recurse (op=token)+ {setTokenPrec($op.t, currentOuterAltNumber);} recurse ACTION? ) +// (ALT (= a e) (= op (SET '*' '/')) (= b e) {}) (ALT INT {}) (ALT '(' (= x e) ')' {}) +binaryMultipleOp + : ^( ALT recurse bops recurse ACTION? ) ; -binaryMultipleOp - : ^( ALT recurse - ( ^( BLOCK ( ^( ALT (op=token)+ {setTokenPrec($op.t, currentOuterAltNumber);} ) )+ ) - | ^(SET (op=token)+ {setTokenPrec($op.t, currentOuterAltNumber);}) - ) - recurse ACTION? - ) +bops: ^(ASSIGN ID bops) + | ^( BLOCK ( ^( ALT (op=token)+ {setTokenPrec($op.t, currentOuterAltNumber);} ) )+ ) + | ^(SET (op=token)+ {setTokenPrec($op.t, currentOuterAltNumber);}) + ; + +binary + : ^( ALT recurse (op=token)+ {setTokenPrec($op.t, currentOuterAltNumber);} recurse ACTION? ) ; ternary @@ -222,4 +223,4 @@ atom | ^(WILDCARD elementOptions) | WILDCARD | ^(DOT ID element) - ; \ No newline at end of file + ; diff --git a/tool/test/org/antlr/v4/test/TestLeftRecursion.java b/tool/test/org/antlr/v4/test/TestLeftRecursion.java index d37671bef..2eb813f66 100644 --- a/tool/test/org/antlr/v4/test/TestLeftRecursion.java +++ b/tool/test/org/antlr/v4/test/TestLeftRecursion.java @@ -229,21 +229,19 @@ public class TestLeftRecursion extends BaseTest { } @Test public void testLabelsOnOpSubrule() throws Exception { - // FAILS String grammar = "grammar T;\n" + - "s : e {System.out.println($e.v);} ;\n" + - "e returns [int v, List ignored]\n" + - " : a=e op=('*'|'/') b=e {$v = $a.v * $b.v;}\n" + - " | INT {$v = $INT.int;}\n" + - " | '(' x=e ')' {$v = $x.v;}\n" + + "s @after {System.out.println($ctx.toStringTree(this));} : e ;\n" + + "e : a=e op=('*'|'/') b=e {}\n" + + " | INT {}\n" + + " | '(' x=e ')' {}\n" + " ;\n" + "INT : '0'..'9'+ ;\n" + "WS : (' '|'\\n') {skip();} ;\n"; String[] tests = { - "4", "4", - "1*2/3", "7", - "(1/2)*3", "9", + "4", "(s (e 4))", + "1*2/3", "(s (e (e (e 1) * (e 2)) / (e 3)))", + "(1/2)*3", "(s (e (e ( (e (e 1) / (e 2)) )) * (e 3)))", }; runTests(grammar, tests, "s"); } From cc20a52cdd6f04d17f53050c7457aafd1ba51b60 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Sat, 31 Mar 2012 15:54:00 -0700 Subject: [PATCH 33/58] allow special "tokens" start rule name so we can test pure lexer grammars. --- .../org/antlr/v4/runtime/misc/TestRig.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java b/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java index f78d090f5..dd600321f 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java @@ -57,6 +57,9 @@ import java.lang.reflect.Method; * [input-filename] */ public class TestRig { + + public static final String LEXER_START_RULE_NAME = "tokens"; + public static void main(String[] args) throws Exception { String grammarName; String startRuleName; @@ -69,10 +72,12 @@ public class TestRig { boolean diagnostics = false; String encoding = null; if ( args.length < 2 ) { - System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName" + - " [-tokens] [-print] [-gui] [-ps file.ps] [-encoding encodingname]" + - " [-trace] [-diagnostics]"+ - " [input-filename]"); + System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName\n" + + " [-tokens] [-print] [-gui] [-ps file.ps] [-encoding encodingname]\n" + + " [-trace] [-diagnostics]\n"+ + " [input-filename]"); + System.err.println("Use startRuleName='tokens' if GrammarName is a lexer grammar."); + System.err.println("Omitting input-filename makes rig read from stdin."); return; } int i=0; @@ -121,16 +126,11 @@ public class TestRig { } // System.out.println("exec "+grammarName+"."+startRuleName); String lexerName = grammarName+"Lexer"; - String parserName = grammarName+"Parser"; ClassLoader cl = Thread.currentThread().getContextClassLoader(); Class lexerClass = cl.loadClass(lexerName); if ( lexerClass==null ) { System.err.println("Can't load "+lexerName); } - Class parserClass = cl.loadClass(parserName); - if ( parserClass==null ) { - System.err.println("Can't load "+parserName); - } InputStream is = System.in; if ( inputFile!=null ) { @@ -158,6 +158,13 @@ public class TestRig { } } + if ( startRuleName.equals(LEXER_START_RULE_NAME) ) return; + + String parserName = grammarName+"Parser"; + Class parserClass = cl.loadClass(parserName); + if ( parserClass==null ) { + System.err.println("Can't load "+parserName); + } Constructor parserCtor = parserClass.getConstructor(TokenStream.class); Parser parser = parserCtor.newInstance(tokens); From f238d7579ef4de47535e16c9d5f98573b252e014 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Sat, 31 Mar 2012 17:27:11 -0700 Subject: [PATCH 34/58] added getText() to ParseTree. getText(tokens) is still really what you want but convenient for ctx.type().getText() when it's just one token. --- .../antlr/v4/runtime/BufferedTokenStream.java | 17 +++++++------ .../antlr/v4/runtime/ParserRuleContext.java | 25 +++++++++++++++++++ .../src/org/antlr/v4/runtime/RuleContext.java | 6 +++++ .../org/antlr/v4/runtime/tree/ParseTree.java | 9 +++++++ 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java b/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java index e038357c8..fc7a2fc41 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java @@ -29,7 +29,11 @@ package org.antlr.v4.runtime; -import java.util.*; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Set; /** Buffer all input tokens but do on-demand fetching of new tokens from * lexer. Useful when the parser or lexer has to set context/mode info before @@ -246,13 +250,10 @@ public class BufferedTokenStream implements TokenStream { if ( start<0 || stop<0 ) return ""; if ( p == -1 ) setup(); if ( stop>=tokens.size() ) stop = tokens.size()-1; - StringBuilder buf = new StringBuilder(); - for (int i = start; i <= stop; i++) { - T t = tokens.get(i); - if ( t.getType()==Token.EOF ) break; - buf.append(t.getText()); - } - return buf.toString(); + + int a = tokens.get(start).getStartIndex(); + int b = tokens.get(stop).getStopIndex(); + return tokenSource.getInputStream().substring(a, b); } @Override diff --git a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java index 906c96b5a..48d748ab1 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java @@ -297,6 +297,31 @@ public class ParserRuleContext extends RuleContext { return Interval.of(start.getTokenIndex(), stop.getTokenIndex()); } + /** Return the combined text of all leaf nodes. Does not get any + * off-channel tokens (if any) so won't return whitespace and + * comments if they are sent to parser on hidden channel. + * + * This just recursively collects all leaf nodes and combines text. + */ + @Override + public String getText() { + StringBuilder buf = new StringBuilder(); + getText_(this, buf); + return buf.toString(); + } + + protected void getText_(ParseTree p, StringBuilder buf) { + if ( p instanceof TerminalNode ) { + buf.append(((TerminalNode)p).getSymbol().getText()); + return; + } + int n = p.getChildCount(); + for (int i=0; i