diff --git a/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java index 1dd15181a..86ca2845c 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java @@ -30,9 +30,9 @@ import org.antlr.v4.runtime.misc.OrderedHashSet; * * TODO: what to do about lexers */ -public interface ANTLRErrorStrategy { +public interface ANTLRErrorStrategy { /** Report any kind of RecognitionException. */ - void reportError(@NotNull BaseRecognizer recognizer, + void reportError(@NotNull BaseRecognizer recognizer, @Nullable RecognitionException e) throws RecognitionException; @@ -52,7 +52,7 @@ public interface ANTLRErrorStrategy { * "inserting" tokens, we need to specify what that implicitly created * token is. We use object, because it could be a tree node. */ - Symbol recoverInline(@NotNull BaseRecognizer recognizer) + Token recoverInline(@NotNull BaseRecognizer recognizer) throws RecognitionException; /** Resynchronize the parser by consuming tokens until we find one @@ -60,7 +60,7 @@ public interface ANTLRErrorStrategy { * the current rule. The exception contains info you might want to * use to recover better. */ - void recover(@NotNull BaseRecognizer recognizer, + void recover(@NotNull BaseRecognizer recognizer, @Nullable RecognitionException e); /** Make sure that the current lookahead symbol is consistent with @@ -90,14 +90,14 @@ public interface ANTLRErrorStrategy { * turn off this functionality by simply overriding this method as * a blank { }. */ - void sync(@NotNull BaseRecognizer recognizer); + void sync(@NotNull BaseRecognizer recognizer); /** Notify handler that parser has entered an error state. The * parser currently doesn't call this--the handler itself calls this * in report error methods. But, for symmetry with endErrorCondition, * this method is in the interface. */ - void beginErrorCondition(@NotNull BaseRecognizer recognizer); + void beginErrorCondition(@NotNull BaseRecognizer recognizer); /** Is the parser in the process of recovering from an error? Upon * a syntax error, the parser enters recovery mode and stays there until @@ -105,13 +105,13 @@ public interface ANTLRErrorStrategy { * avoid sending out spurious error messages. We only want one error * message per syntax error */ - boolean inErrorRecoveryMode(@NotNull BaseRecognizer recognizer); + boolean inErrorRecoveryMode(@NotNull BaseRecognizer recognizer); /** Reset the error handler. Call this when the parser * matches a valid token (indicating no longer in recovery mode) * and from its own reset method. */ - void endErrorCondition(@NotNull BaseRecognizer recognizer); + void endErrorCondition(@NotNull BaseRecognizer recognizer); /** 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 @@ -120,7 +120,7 @@ public interface ANTLRErrorStrategy { * that can match the input sequence. This method is only called when we are parsing with * full context. */ - void reportAmbiguity(@NotNull BaseRecognizer recognizer, + void reportAmbiguity(@NotNull BaseRecognizer recognizer, DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, @NotNull OrderedHashSet configs); @@ -130,12 +130,12 @@ public interface ANTLRErrorStrategy { * we can't be sure if a conflict is an ambiguity or simply a weakness in the Strong LL parsing * strategy. If we are parsing with full context, this method is never called. */ -// void reportConflict(@NotNull BaseRecognizer recognizer, +// void reportConflict(@NotNull BaseRecognizer recognizer, // int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, // @NotNull OrderedHashSet configs); - void reportAttemptingFullContext(@NotNull BaseRecognizer recognizer, + void reportAttemptingFullContext(@NotNull BaseRecognizer recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull OrderedHashSet configs); @@ -145,7 +145,7 @@ public interface ANTLRErrorStrategy { * is more complicated than Strong LL can handle. The parser moved up to full context * parsing for that input sequence. */ - void reportContextSensitivity(@NotNull BaseRecognizer recognizer, + void reportContextSensitivity(@NotNull BaseRecognizer recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull OrderedHashSet configs); @@ -155,7 +155,7 @@ public interface ANTLRErrorStrategy { * 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 BaseRecognizer recognizer, + void reportInsufficientPredicates(@NotNull BaseRecognizer recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, DecisionState decState, diff --git a/runtime/Java/src/org/antlr/v4/runtime/BailErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/BailErrorStrategy.java index af29fbdb2..a0be4fc7f 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/BailErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/BailErrorStrategy.java @@ -32,14 +32,14 @@ package org.antlr.v4.runtime; /** Bail out of parser at first syntax error. Do this to use it: * myparser.setErrorHandler(new BailErrorStrategy()); */ -public class BailErrorStrategy extends DefaultErrorStrategy { +public class BailErrorStrategy extends DefaultErrorStrategy { /** Instead of recovering from exception e, Re-throw wrote it wrapped * in a generic RuntimeException so it is not caught by the * rule function catches. Exception e is the "cause" of the * RuntimeException. */ @Override - public void recover(BaseRecognizer recognizer, RecognitionException e) { + public void recover(BaseRecognizer recognizer, RecognitionException e) { throw new RuntimeException(e); } @@ -47,7 +47,7 @@ public class BailErrorStrategy extends DefaultErrorStrategy { * successfully recovers, it won't throw an exception. */ @Override - public Symbol recoverInline(BaseRecognizer recognizer) + public Token recoverInline(BaseRecognizer recognizer) throws RecognitionException { throw new RuntimeException(new InputMismatchException(recognizer)); @@ -55,5 +55,5 @@ public class BailErrorStrategy extends DefaultErrorStrategy { /** Make sure we don't attempt to recover from problems in subrules. */ @Override - public void sync(BaseRecognizer recognizer) { } + public void sync(BaseRecognizer recognizer) { } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/BaseRecognizer.java b/runtime/Java/src/org/antlr/v4/runtime/BaseRecognizer.java index 99921e93d..e84d3c1d4 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/BaseRecognizer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/BaseRecognizer.java @@ -32,7 +32,6 @@ 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.Nullable; -import org.antlr.v4.runtime.tree.BufferedASTNodeStream; import org.antlr.v4.runtime.tree.ParseTreeListener; import java.util.ArrayList; @@ -43,17 +42,19 @@ import java.util.List; * support code essentially; most of it is error recovery stuff and * backtracking. * - * TODO: rename since lexer not under. or reorg parser/treeparser; treeparser under parser? + * TODO: rename / reorg with parser */ -public abstract class BaseRecognizer extends Recognizer> { +public abstract class BaseRecognizer extends Recognizer> { public static final String NEXT_TOKEN_RULE_NAME = "nextToken"; + protected TokenStream _input; + /** The RuleContext object for the currently executing rule. This * must be non-null during parsing, but is initially null. * When somebody calls the start rule, this gets set to the * root context. */ - protected ParserRuleContext _ctx; + protected ParserRuleContext _ctx; protected boolean buildParseTrees; protected boolean traceATNStates; @@ -64,7 +65,7 @@ public abstract class BaseRecognizer extends Recognizer _listener; + protected ParseTreeListener _listener; /** Did the recognizer encounter a syntax error? Track how many. */ protected int syntaxErrors = 0; @@ -89,9 +90,9 @@ public abstract class BaseRecognizer extends Recognizer extends Recognizer getListener() { + public ParseTreeListener getListener() { return _listener; } - public void setListener(ParseTreeListener listener) { + public void setListener(ParseTreeListener listener) { this._listener = listener; } @@ -164,38 +165,49 @@ public abstract class BaseRecognizer extends Recognizer getInputStream(); + @Override + public TokenStream getInputStream() { return getTokenStream(); } + + @Override + public final void setInputStream(IntStream input) { + setTokenStream((TokenStream)input); + } + + public TokenStream getTokenStream() { + return _input; + } + + /** Set the token stream and reset the parser */ + public void setTokenStream(TokenStream input) { + this._input = null; + reset(); + this._input = input; + } public String getInputString(int start) { return getInputString(start, getInputStream().index()); } public String getInputString(int start, int stop) { - SymbolStream input = getInputStream(); + SymbolStream input = getInputStream(); if ( input instanceof TokenStream ) { return ((TokenStream)input).toString(start,stop); } - else if ( input instanceof BufferedASTNodeStream ) { - return ((BufferedASTNodeStream)input).toString(input.get(start),input.get(stop)); - } return "n/a"; } /** Match needs to return the current input symbol, which gets put - * into the label for the associated token ref; e.g., x=ID. Token - * and tree parsers need to return different objects. Rather than test - * for input stream type or change the IntStream interface, I use - * a simple method to ask the recognizer to tell me what the current - * input symbol is. + * into the label for the associated token ref; e.g., x=ID. */ - public abstract Symbol getCurrentInputSymbol(); - - public void notifyListeners(String msg) { - notifyListeners(getCurrentInputSymbol(), msg, null); + public Token getCurrentToken() { + return _input.LT(1); } - public void notifyListeners(Symbol offendingToken, String msg, + public void notifyListeners(String msg) { + notifyListeners(getCurrentToken(), msg, null); + } + + public void notifyListeners(Token offendingToken, String msg, @Nullable RecognitionException e) { int line = -1; @@ -204,17 +216,17 @@ public abstract class BaseRecognizer extends Recognizer[] listeners = getListeners(); + ANTLRErrorListener[] listeners = getListeners(); if ( listeners.length == 0 ) { System.err.println("line "+line+":"+charPositionInLine+" "+msg); return; } - for (ANTLRErrorListener pl : listeners) { + for (ANTLRErrorListener pl : listeners) { pl.error(this, offendingToken, line, charPositionInLine, msg, e); } } - public void enterOuterAlt(ParserRuleContext localctx, int altNum) { + public void enterOuterAlt(ParserRuleContext localctx, int altNum) { // if we have new localctx, make sure we replace existing ctx // that is previous child of parse tree if ( buildParseTrees && _ctx != localctx ) { @@ -239,8 +251,8 @@ public abstract class BaseRecognizer extends Recognizer extends Recognizer localctx, int ruleIndex); - - public void exitRule(int ruleIndex) { - _ctx = (ParserRuleContext)_ctx.parent; + /** Always called by generated parsers upon entry to a rule. + * This occurs after the new context has been pushed. Access field + * _ctx get the current context. + * + * This is flexible because users do not have to regenerate parsers + * to get trace facilities. + */ + public void enterRule(ParserRuleContext localctx, int ruleIndex) { + _ctx = localctx; + _ctx.start = _input.LT(1); + _ctx.ruleIndex = ruleIndex; + if ( buildParseTrees ) addContextToParseTree(); + if ( _listener != null) { + _listener.enterEveryRule(_ctx); + _ctx.enterRule(_listener); + } } - public ParserRuleContext getInvokingContext(int ruleIndex) { - ParserRuleContext p = _ctx; + public void exitRule(int ruleIndex) { + // trigger event on _ctx, before it reverts to parent + if ( _listener != null) { + _ctx.exitRule(_listener); + _listener.exitEveryRule(_ctx); + } + _ctx = (ParserRuleContext)_ctx.parent; + } + + public ParserRuleContext getInvokingContext(int ruleIndex) { + ParserRuleContext p = _ctx; while ( p!=null ) { if ( p.getRuleIndex() == ruleIndex ) return p; - p = (ParserRuleContext)p.parent; + p = (ParserRuleContext)p.parent; } return null; } - public ParserRuleContext getContext() { + public ParserRuleContext getContext() { return _ctx; } @@ -409,7 +442,9 @@ public abstract class BaseRecognizer extends Recognizer to List diff --git a/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java index 5a15a0725..07c8d71e2 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java @@ -34,12 +34,11 @@ 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.OrderedHashSet; -import org.antlr.v4.runtime.tree.AST; /** This is the default error handling mechanism for ANTLR parsers * and tree parsers. */ -public class DefaultErrorStrategy implements ANTLRErrorStrategy { +public class DefaultErrorStrategy implements ANTLRErrorStrategy { /** This is true after we see an error and before having successfully * matched a token. Prevents generation of more than one error message * per error. @@ -57,24 +56,24 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy protected IntervalSet lastErrorStates; @Override - public void beginErrorCondition(BaseRecognizer recognizer) { + public void beginErrorCondition(BaseRecognizer recognizer) { errorRecoveryMode = true; } @Override - public boolean inErrorRecoveryMode(BaseRecognizer recognizer) { + public boolean inErrorRecoveryMode(BaseRecognizer recognizer) { return errorRecoveryMode; } @Override - public void endErrorCondition(BaseRecognizer recognizer) { + public void endErrorCondition(BaseRecognizer recognizer) { errorRecoveryMode = false; lastErrorStates = null; lastErrorIndex = -1; } @Override - public void reportError(BaseRecognizer recognizer, + public void reportError(BaseRecognizer recognizer, RecognitionException e) throws RecognitionException { @@ -98,7 +97,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy else { System.err.println("unknown recognition error type: "+e.getClass().getName()); if ( recognizer!=null ) { - recognizer.notifyListeners((Symbol)e.offendingNode, e.getMessage(), e); + recognizer.notifyListeners((Token)e.offendingToken, e.getMessage(), e); } } } @@ -107,7 +106,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy * token that the match() routine could not recover from. */ @Override - public void recover(BaseRecognizer recognizer, RecognitionException e) { + public void recover(BaseRecognizer recognizer, RecognitionException e) { // System.out.println("recover in "+recognizer.getRuleInvocationStack()+ // " index="+recognizer.getInputStream().index()+ // ", lastErrorIndex="+ @@ -147,13 +146,13 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy * We opt to stay in the loop as long as possible. */ @Override - public void sync(BaseRecognizer recognizer) { + public void sync(BaseRecognizer recognizer) { ATNState s = recognizer.getInterpreter().atn.states.get(recognizer._ctx.s); // System.err.println("sync @ "+s.stateNumber+"="+s.getClass().getSimpleName()); // If already recovering, don't try to sync if ( errorRecoveryMode ) return; - SymbolStream tokens = recognizer.getInputStream(); + SymbolStream tokens = recognizer.getInputStream(); int la = tokens.LA(1); // try cheaper subset first; might get lucky. seems to shave a wee bit off @@ -185,46 +184,47 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy // do nothing if we can't identify the exact kind of ATN state } - public void reportNoViableAlternative(BaseRecognizer recognizer, + public void reportNoViableAlternative(BaseRecognizer recognizer, NoViableAltException e) throws RecognitionException { - SymbolStream tokens = recognizer.getInputStream(); + SymbolStream tokens = recognizer.getInputStream(); String input; if (tokens instanceof TokenStream) { if ( e.startToken.getType()==Token.EOF ) input = ""; else input = ((TokenStream)tokens).toString(e.startToken, e.offendingToken); - } else { + } + else { input = ""; } String msg = "no viable alternative at input "+escapeWSAndQuote(input); - recognizer.notifyListeners((Symbol) e.offendingNode, msg, e); + recognizer.notifyListeners((Token) e.offendingToken, msg, e); } - public void reportInputMismatch(BaseRecognizer recognizer, + public void reportInputMismatch(BaseRecognizer recognizer, InputMismatchException e) throws RecognitionException { - String msg = "mismatched input "+getTokenErrorDisplay((Symbol)e.offendingNode)+ + String msg = "mismatched input "+getTokenErrorDisplay((Token)e.offendingToken)+ " expecting "+e.getExpectedTokens().toString(recognizer.getTokenNames()); - recognizer.notifyListeners((Symbol)e.offendingNode, msg, e); + recognizer.notifyListeners((Token)e.offendingToken, msg, e); } - public void reportFailedPredicate(BaseRecognizer recognizer, + public void reportFailedPredicate(BaseRecognizer recognizer, FailedPredicateException e) throws RecognitionException { String ruleName = recognizer.getRuleNames()[recognizer._ctx.getRuleIndex()]; String msg = "rule "+ruleName+" "+e.msg; - recognizer.notifyListeners((Symbol)e.offendingNode, msg, e); + recognizer.notifyListeners((Token)e.offendingToken, msg, e); } - public void reportUnwantedToken(BaseRecognizer recognizer) { + public void reportUnwantedToken(BaseRecognizer recognizer) { if (errorRecoveryMode) return; recognizer.syntaxErrors++; beginErrorCondition(recognizer); - Symbol t = recognizer.getCurrentInputSymbol(); + Token t = recognizer.getCurrentToken(); String tokenName = getTokenErrorDisplay(t); IntervalSet expecting = getExpectedTokens(recognizer); String msg = "extraneous input "+tokenName+" expecting "+ @@ -232,12 +232,12 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy recognizer.notifyListeners(t, msg, null); } - public void reportMissingToken(BaseRecognizer recognizer) { + public void reportMissingToken(BaseRecognizer recognizer) { if (errorRecoveryMode) return; recognizer.syntaxErrors++; beginErrorCondition(recognizer); - Symbol t = recognizer.getCurrentInputSymbol(); + Token t = recognizer.getCurrentToken(); IntervalSet expecting = getExpectedTokens(recognizer); String msg = "missing "+expecting.toString(recognizer.getTokenNames())+ " at "+getTokenErrorDisplay(t); @@ -275,11 +275,11 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy * reference in rule atom. It can assume that you forgot the ')'. */ @Override - public Symbol recoverInline(BaseRecognizer recognizer) + public Token recoverInline(BaseRecognizer recognizer) throws RecognitionException { // SINGLE TOKEN DELETION - Symbol matchedSymbol = singleTokenDeletion(recognizer); + Token matchedSymbol = singleTokenDeletion(recognizer); if ( matchedSymbol!=null ) { // we have deleted the extra token. // now, move past ttype token as if all were ok @@ -297,7 +297,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy } // if next token is what we are looking for then "delete" this token - public boolean singleTokenInsertion(BaseRecognizer recognizer) { + public boolean singleTokenInsertion(BaseRecognizer recognizer) { int currentSymbolType = recognizer.getInputStream().LA(1); // if current token is consistent with what could come after current // ATN state, then we know we're missing a token; error recovery @@ -313,7 +313,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy return false; } - public Symbol singleTokenDeletion(BaseRecognizer recognizer) { + public Token singleTokenDeletion(BaseRecognizer recognizer) { int nextTokenType = recognizer.getInputStream().LA(2); IntervalSet expecting = getExpectedTokens(recognizer); if ( expecting.contains(nextTokenType) ) { @@ -326,7 +326,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy */ recognizer.consume(); // simply delete extra token // we want to return the token we're actually matching - Symbol matchedSymbol = recognizer.getCurrentInputSymbol(); + Token matchedSymbol = recognizer.getCurrentToken(); endErrorCondition(recognizer); // we know current token is correct return matchedSymbol; } @@ -352,8 +352,8 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy * If you change what tokens must be created by the lexer, * override this method to create the appropriate tokens. */ - protected Symbol getMissingSymbol(BaseRecognizer recognizer) { - Symbol currentSymbol = recognizer.getCurrentInputSymbol(); + protected Token getMissingSymbol(BaseRecognizer recognizer) { + Token currentSymbol = recognizer.getCurrentToken(); if (!(currentSymbol instanceof Token)) { throw new UnsupportedOperationException("This error strategy only supports Token symbols."); } @@ -373,10 +373,10 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy t.channel = Token.DEFAULT_CHANNEL; t.source = current.getTokenSource(); t.index = -1; // indicate we conjured this up because it has no index - return (Symbol)t; + return (Token)t; } - public IntervalSet getExpectedTokens(BaseRecognizer recognizer) { + public IntervalSet getExpectedTokens(BaseRecognizer recognizer) { return recognizer.getExpectedTokens(); } @@ -388,7 +388,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy * your token objects because you don't have to go modify your lexer * so that it creates a new Java type. */ - public String getTokenErrorDisplay(Symbol t) { + public String getTokenErrorDisplay(Token t) { if ( t==null ) return ""; String s = getSymbolText(t); if ( s==null ) { @@ -402,25 +402,19 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy return escapeWSAndQuote(s); } - protected String getSymbolText(@NotNull Symbol symbol) { + protected String getSymbolText(@NotNull Token symbol) { if (symbol instanceof Token) { return ((Token)symbol).getText(); } - else if (symbol instanceof AST) { - return ((AST)symbol).getText(); - } else { return symbol.toString(); } } - protected int getSymbolType(@NotNull Symbol symbol) { + protected int getSymbolType(@NotNull Token symbol) { if (symbol instanceof Token) { return ((Token)symbol).getType(); } - else if (symbol instanceof AST) { - return ((AST)symbol).getType(); - } else { return Token.INVALID_TYPE; } @@ -526,7 +520,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy * Like Grosch I implement context-sensitive FOLLOW sets that are combined * at run-time upon error to avoid overhead during parsing. */ - protected IntervalSet getErrorRecoverySet(BaseRecognizer recognizer) { + protected IntervalSet getErrorRecoverySet(BaseRecognizer recognizer) { ATN atn = recognizer.getInterpreter().atn; RuleContext ctx = recognizer._ctx; IntervalSet recoverSet = new IntervalSet(); @@ -544,7 +538,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy } /** Consume tokens until one matches the given token set */ - public void consumeUntil(BaseRecognizer recognizer, IntervalSet set) { + public void consumeUntil(BaseRecognizer recognizer, IntervalSet set) { // System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")"); int ttype = recognizer.getInputStream().LA(1); while (ttype != Token.EOF && !set.contains(ttype) ) { @@ -556,14 +550,14 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy } @Override - public void reportAmbiguity(@NotNull BaseRecognizer recognizer, + public void reportAmbiguity(@NotNull BaseRecognizer recognizer, DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, @NotNull OrderedHashSet configs) { } @Override - public void reportAttemptingFullContext(@NotNull BaseRecognizer recognizer, + public void reportAttemptingFullContext(@NotNull BaseRecognizer recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull OrderedHashSet configs) @@ -571,13 +565,13 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy } @Override - public void reportContextSensitivity(@NotNull BaseRecognizer recognizer, @NotNull DFA dfa, + public void reportContextSensitivity(@NotNull BaseRecognizer recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull OrderedHashSet configs) { } @Override - public void reportInsufficientPredicates(@NotNull BaseRecognizer recognizer, + public void reportInsufficientPredicates(@NotNull BaseRecognizer recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, diff --git a/runtime/Java/src/org/antlr/v4/runtime/DefaultTreeGrammarErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DefaultTreeGrammarErrorStrategy.java deleted file mode 100644 index 9ef25308d..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/DefaultTreeGrammarErrorStrategy.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree.*; -import org.antlr.v4.runtime.tree.gui.TreeViewer; - -import java.util.*; - -public class DefaultTreeGrammarErrorStrategy extends DefaultErrorStrategy { - @Override - public void beginErrorCondition(BaseRecognizer recognizer) { - } - - @Override - public void reportError(BaseRecognizer recognizer, RecognitionException e) - throws RecognitionException - { - super.reportError(recognizer, e); - TreeParser parser = (TreeParser)recognizer; - ASTNodeStream input = parser.getInputStream(); - Object root = input.getTreeSource(); - // If instanceof Tree, we can show in TreeViewer - if ( root instanceof Tree ) { - TreeViewer viewer = new TreeViewer(recognizer, (Tree)root); - viewer.open(); - List unmatchedNodes = null; - if ( e instanceof NoViableTreeGrammarAltException ) { - NoViableTreeGrammarAltException nva = - (NoViableTreeGrammarAltException)e; - unmatchedNodes = getNodeList(input, nva); - } - else { - unmatchedNodes = new ArrayList(); - unmatchedNodes.add((T)e.offendingNode); - } - viewer.setHighlightedBoxColor(TreeViewer.LIGHT_RED); - viewer.addHighlightedNodes((List)unmatchedNodes); - } - } - - @Override - public void reportNoViableAlternative(BaseRecognizer recognizer, - NoViableAltException e) - throws RecognitionException - { - TreeParser parser = (TreeParser)recognizer; - ASTNodeStream input = parser.getInputStream(); - List unmatchedNodes = - getNodeList(input, (NoViableTreeGrammarAltException)e); - StringBuilder buf = new StringBuilder(); - ASTAdaptor adap = input.getTreeAdaptor(); - for (int i = 0; i < unmatchedNodes.size(); i++) { - if ( i>0 ) buf.append(" "); - T t = unmatchedNodes.get(i); - buf.append(adap.getText(t)); - } - String s = buf.toString(); - String msg = "no viable alternative at node(s) "+escapeWSAndQuote(s); - recognizer.notifyListeners((T)e.offendingNode, msg, e); - } - - protected List getNodeList(ASTNodeStream input, - NoViableTreeGrammarAltException nva) - { - List unmatchedNodes; - T start = (T)nva.startNode; - T stop = (T)nva.offendingNode; - if ( input instanceof BufferedASTNodeStream) { - BufferedASTNodeStream b = - (BufferedASTNodeStream)input; - unmatchedNodes = b.get(start, stop); - } - else { - // if not buffered then we can't get from start to stop; - // just highlight the start/stop nodes, but not in between - unmatchedNodes = new ArrayList(); - if ( nva.startNode!=null ) { - unmatchedNodes.add((T)nva.startNode); - } - if ( nva.startNode==null || nva.offendingNode!=nva.startNode ) { - unmatchedNodes.add((T)nva.offendingNode); - } - } - return unmatchedNodes; - } - - @Override - public T recoverInline(BaseRecognizer recognizer) throws RecognitionException { - InputMismatchException e = new InputMismatchException(recognizer); - reportError(recognizer, e); - throw e; - } - - @Override - public void recover(BaseRecognizer recognizer, RecognitionException e) { - throw new RuntimeException(e); - } - - @Override - public void sync(BaseRecognizer recognizer) { - } - - @Override - public boolean inErrorRecoveryMode(BaseRecognizer recognizer) { - return false; - } - - @Override - public void endErrorCondition(BaseRecognizer recognizer) { - } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java index 9a84e5671..11404cbb9 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java @@ -39,9 +39,9 @@ import org.antlr.v4.runtime.misc.OrderedHashSet; import java.util.Arrays; -public class DiagnosticErrorStrategy extends DefaultErrorStrategy { +public class DiagnosticErrorStrategy extends DefaultErrorStrategy { @Override - public void reportAmbiguity(@NotNull BaseRecognizer recognizer, + public void reportAmbiguity(@NotNull BaseRecognizer recognizer, DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, @NotNull OrderedHashSet configs) { @@ -50,7 +50,7 @@ public class DiagnosticErrorStrategy extends DefaultErrorStrategy recognizer, + public void reportAttemptingFullContext(@NotNull BaseRecognizer recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull OrderedHashSet configs) @@ -60,7 +60,7 @@ public class DiagnosticErrorStrategy extends DefaultErrorStrategy recognizer, @NotNull DFA dfa, + public void reportContextSensitivity(@NotNull BaseRecognizer recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull OrderedHashSet configs) { recognizer.notifyListeners("reportContextSensitivity d="+dfa.decision +": "+ configs + ", input='" + @@ -68,7 +68,7 @@ public class DiagnosticErrorStrategy extends DefaultErrorStrategy recognizer, + public void reportInsufficientPredicates(@NotNull BaseRecognizer recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, diff --git a/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java b/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java index 1245115db..ada98738e 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java @@ -28,9 +28,9 @@ */ package org.antlr.v4.runtime; +import org.antlr.v4.runtime.atn.ATNState; +import org.antlr.v4.runtime.atn.PredicateTransition; import org.antlr.v4.runtime.misc.Nullable; -import org.antlr.v4.runtime.atn.*; -import org.antlr.v4.runtime.tree.AST; /** A semantic predicate failed during validation. Validation of predicates * occurs when normally parsing the alternative just like matching a token. @@ -42,24 +42,18 @@ public class FailedPredicateException extends RecognitionException { public int predIndex; public String msg; - public FailedPredicateException(BaseRecognizer recognizer) { + public FailedPredicateException(BaseRecognizer recognizer) { this(recognizer, null); } - public FailedPredicateException(BaseRecognizer recognizer, @Nullable String msg) { + public FailedPredicateException(BaseRecognizer recognizer, @Nullable String msg) { super(recognizer, recognizer.getInputStream(), recognizer._ctx); ATNState s = recognizer.getInterpreter().atn.states.get(recognizer._ctx.s); PredicateTransition trans = (PredicateTransition)s.transition(0); ruleIndex = trans.ruleIndex; predIndex = trans.predIndex; this.msg = msg; - Object la = recognizer.getCurrentInputSymbol(); - this.offendingNode = la; - if ( la instanceof AST) { - this.offendingToken = ((AST)la).getPayload(); - } - else { - this.offendingToken = (Token)la; - } + Token la = recognizer.getCurrentToken(); + this.offendingToken = la; } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java b/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java index 2a0c4c800..3d478af40 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java @@ -1,20 +1,12 @@ package org.antlr.v4.runtime; -import org.antlr.v4.runtime.tree.AST; - /** This signifies any kind of mismatched input exceptions such as * when the current input does not match the expected token or tree node. */ public class InputMismatchException extends RecognitionException { - public InputMismatchException(BaseRecognizer recognizer) { + public InputMismatchException(BaseRecognizer recognizer) { super(recognizer, recognizer.getInputStream(), recognizer._ctx); - Object la = recognizer.getCurrentInputSymbol(); - this.offendingNode = la; - if ( la instanceof AST ) { - this.offendingToken = ((AST)la).getPayload(); - } - else { - this.offendingToken = (Token)la; - } + Token la = recognizer.getCurrentToken(); + this.offendingToken = la; } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java b/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java index 207294508..694c637a3 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java @@ -45,27 +45,24 @@ public class NoViableAltException extends RecognitionException { */ public Token startToken; - public NoViableAltException(BaseRecognizer recognizer) { // LL(1) error + public NoViableAltException(BaseRecognizer recognizer) { // LL(1) error this(recognizer,recognizer.getInputStream(), - recognizer.getCurrentInputSymbol(), - recognizer.getCurrentInputSymbol(), - recognizer.getCurrentInputSymbol(), + recognizer.getCurrentToken(), + recognizer.getCurrentToken(), null, recognizer._ctx); } - public NoViableAltException(BaseRecognizer recognizer, + public NoViableAltException(BaseRecognizer recognizer, SymbolStream input, Token startToken, Token offendingToken, - Symbol offendingNode, OrderedHashSet deadEndConfigs, ParserRuleContext ctx) { super(recognizer, input, ctx); this.deadEndConfigs = deadEndConfigs; this.startToken = startToken; - this.offendingNode = offendingNode; this.offendingToken = offendingToken; } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/NoViableTreeGrammarAltException.java b/runtime/Java/src/org/antlr/v4/runtime/NoViableTreeGrammarAltException.java deleted file mode 100644 index 6c7489044..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/NoViableTreeGrammarAltException.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.ATNConfig; -import org.antlr.v4.runtime.misc.OrderedHashSet; -import org.antlr.v4.runtime.tree.ASTNodeStream; -import org.antlr.v4.runtime.tree.TreeParser; - -public class NoViableTreeGrammarAltException extends NoViableAltException { - protected Object startNode; - - public NoViableTreeGrammarAltException(TreeParser recognizer) { - this(recognizer, - recognizer.getInputStream(), - recognizer.getCurrentInputSymbol(), - recognizer.getCurrentInputSymbol(), - null, - recognizer._ctx); - } - - public NoViableTreeGrammarAltException(BaseRecognizer recognizer, - ASTNodeStream input, - Symbol startNode, - Symbol offendingNode, - OrderedHashSet deadEndConfigs, - ParserRuleContext ctx) { - super(recognizer, input, - input.getTreeAdaptor().getToken(startNode), - input.getTreeAdaptor().getToken(offendingNode), - offendingNode, - deadEndConfigs, ctx); - this.startNode = startNode; - } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/Parser.java b/runtime/Java/src/org/antlr/v4/runtime/Parser.java index b5dacb31c..e132ca21e 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Parser.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Parser.java @@ -32,71 +32,11 @@ package org.antlr.v4.runtime; /** A parser for TokenStreams. "parser grammars" result in a subclass * of this. */ -public class Parser extends BaseRecognizer { - protected TokenStream _input; - +public class Parser extends BaseRecognizer { public Parser(TokenStream input) { super(input); } - /** Always called by generated parsers upon entry to a rule. - * This occurs after the new context has been pushed. Access field - * _ctx get the current context. - * - * This is flexible because users do not have to regenerate parsers - * to get trace facilities. - */ - @Override - public void enterRule(ParserRuleContext localctx, int ruleIndex) { - _ctx = localctx; - _ctx.start = _input.LT(1); - _ctx.ruleIndex = ruleIndex; - if ( buildParseTrees ) addContextToParseTree(); - if ( _listener != null) { - _listener.enterEveryRule(_ctx); - _ctx.enterRule(_listener); - } - } - - @Override - public void exitRule(int ruleIndex) { - // trigger event on _ctx, before it reverts to parent - if ( _listener != null) { - _ctx.exitRule(_listener); - _listener.exitEveryRule(_ctx); - } - super.exitRule(ruleIndex); - } - - @Override - public Token match(int ttype) throws RecognitionException { - return super.match(ttype); - } - - @Override - public Token getCurrentInputSymbol() { - return _input.LT(1); - } - - @Override - public TokenStream getInputStream() { return _input; } - - @Override - public final void setInputStream(IntStream input) { - setTokenStream((TokenStream)input); - } - - /** Set the token stream and reset the parser */ - public void setTokenStream(TokenStream input) { - this._input = null; - reset(); - this._input = input; - } - - public TokenStream getTokenStream() { - return _input; - } - @Override public String getSourceName() { return _input.getSourceName(); diff --git a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java index 87372a7d8..11308440c 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java @@ -220,7 +220,7 @@ public class ParserRuleContext extends RuleContext { } /** Used for rule context info debugging during runtime, not so much for ATN debugging */ - public String toInfoString(BaseRecognizer recognizer) { + public String toInfoString(BaseRecognizer recognizer) { List rules = recognizer.getRuleInvocationStack(); Collections.reverse(rules); return "ParserRuleContext"+rules+"{" + diff --git a/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java b/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java index b49382ccb..aa7a76648 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java @@ -58,11 +58,6 @@ public class RecognitionException extends RuntimeException { */ protected Token offendingToken; - /** If this is a tree parser exception, node is set to the node with - * the problem. - */ - protected Object offendingNode; - protected int offendingState; public RecognitionException(@Nullable Recognizer recognizer, IntStream input, @@ -84,7 +79,7 @@ public class RecognitionException extends RuntimeException { public IntervalSet getExpectedTokens() { // TODO: do we really need this type check? - if ( recognizer!=null && recognizer instanceof BaseRecognizer ) { + if ( recognizer!=null && recognizer instanceof BaseRecognizer ) { return ((BaseRecognizer) recognizer).getExpectedTokens(); } return null; @@ -105,18 +100,4 @@ public class RecognitionException extends RuntimeException { public Recognizer getRecognizer() { return recognizer; } - - // /** Return the token type or char of the unexpected input element */ -// public int getUnexpectedType() { -// if ( recognizer==null ) return offendingToken.getType(); -// if ( recognizer.getInputStream() instanceof TokenStream) { -// return offendingToken.getType(); -// } -// else if ( recognizer.getInputStream() instanceof ASTNodeStream) { -// ASTNodeStream nodes = (ASTNodeStream)recognizer.getInputStream(); -// ASTAdaptor adaptor = nodes.getTreeAdaptor(); -// return adaptor.getType(offendingNode); -// } -// return Token.INVALID_TYPE; -// } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java b/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java index 85aaa8c66..1a2d351a1 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java @@ -41,7 +41,7 @@ import java.util.List; public abstract class Recognizer { public static final int EOF=-1; - protected ANTLRErrorStrategy _errHandler = new DefaultErrorStrategy(); + protected ANTLRErrorStrategy _errHandler = new DefaultErrorStrategy(); private List> _listeners; @@ -117,9 +117,9 @@ public abstract class Recognizer { return _listeners.toArray(EMPTY_LISTENERS); } - public ANTLRErrorStrategy getErrorHandler() { return _errHandler; } + public ANTLRErrorStrategy getErrorHandler() { return _errHandler; } - public void setErrorHandler(ANTLRErrorStrategy h) { this._errHandler = h; } + public void setErrorHandler(ANTLRErrorStrategy h) { this._errHandler = h; } // subclass needs to override these if there are sempreds or actions // that the ATN interp needs to execute diff --git a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java index 1f330a532..878b2c35a 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java @@ -255,18 +255,18 @@ public class RuleContext implements ParseTree.RuleNode { return new Interval(start, stop); } - public void inspect(BaseRecognizer parser) { + public void inspect(BaseRecognizer parser) { TreeViewer viewer = new TreeViewer(parser, this); viewer.open(); } - public void save(BaseRecognizer parser, String fileName) + public void save(BaseRecognizer parser, String fileName) throws IOException, PrintException { Trees.writePS(this, parser, fileName); } - public void save(BaseRecognizer parser, String fileName, + public void save(BaseRecognizer parser, String fileName, String fontName, int fontSize) throws IOException { @@ -277,7 +277,7 @@ public class RuleContext implements ParseTree.RuleNode { * (root child1 .. childN). Print just a node if this is a leaf. * We have to know the recognizer so we can get rule names. */ - public String toStringTree(BaseRecognizer recog) { + public String toStringTree(BaseRecognizer recog) { return Trees.toStringTree(this, recog); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/Token.java b/runtime/Java/src/org/antlr/v4/runtime/Token.java index 170b703bd..52ca12020 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Token.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Token.java @@ -87,8 +87,8 @@ public interface Token { int getChannel(); /** An index from 0..n-1 of the token object in the input stream. - * This must be valid in order to print token streams, - * use TokenRewriteStream, and generally deal with ASTs. + * This must be valid in order to print token streams and + * use TokenRewriteStream. * * Return -1 to indicate that this token was conjured up since * it doesn't have a valid index. diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/v2ParserATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/v2ParserATNSimulator.java index 9bb2d5183..8f8cba718 100755 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/v2ParserATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/v2ParserATNSimulator.java @@ -35,9 +35,6 @@ import org.antlr.v4.runtime.dfa.DFAState; 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.runtime.tree.ASTNodeStream; -import org.antlr.v4.runtime.tree.BufferedASTNodeStream; -import org.antlr.v4.runtime.tree.TreeParser; import org.stringtemplate.v4.misc.MultiMap; import java.util.*; @@ -239,7 +236,7 @@ public class v2ParserATNSimulator extends ATNSimulator { public static int retry_with_context_indicates_no_conflict = 0; @Nullable - protected final BaseRecognizer parser; + protected final BaseRecognizer parser; @NotNull public final DFA[] decisionToDFA; @@ -249,7 +246,7 @@ public class v2ParserATNSimulator extends ATNSimulator { this(null, atn); } - public v2ParserATNSimulator(@Nullable BaseRecognizer parser, @NotNull ATN atn) { + public v2ParserATNSimulator(@Nullable BaseRecognizer parser, @NotNull ATN atn) { super(atn); this.parser = parser; // ctxToDFAs = new HashMap(); @@ -264,7 +261,7 @@ public class v2ParserATNSimulator extends ATNSimulator { public void reset() { } - public int adaptivePredict(@NotNull SymbolStream input, int decision, + public int adaptivePredict(@NotNull SymbolStream input, int decision, @Nullable ParserRuleContext outerContext) { predict_calls++; @@ -291,7 +288,7 @@ public class v2ParserATNSimulator extends ATNSimulator { } } - public int predictATN(@NotNull DFA dfa, @NotNull SymbolStream input, + public int predictATN(@NotNull DFA dfa, @NotNull SymbolStream input, @Nullable ParserRuleContext outerContext) { if ( outerContext==null ) outerContext = ParserRuleContext.EMPTY; @@ -323,7 +320,7 @@ public class v2ParserATNSimulator extends ATNSimulator { } public int execDFA(@NotNull DFA dfa, @NotNull DFAState s0, - @NotNull SymbolStream input, int startIndex, + @NotNull SymbolStream input, int startIndex, @Nullable ParserRuleContext outerContext) { if ( outerContext==null ) outerContext = ParserRuleContext.EMPTY; @@ -476,7 +473,7 @@ public class v2ParserATNSimulator extends ATNSimulator { */ public int execATN(@NotNull DFA dfa, @NotNull DFAState s0, - @NotNull SymbolStream input, int startIndex, + @NotNull SymbolStream input, int startIndex, ParserRuleContext outerContext) { if ( debug ) System.out.println("execATN decision "+dfa.decision+" exec LA(1)=="+ getLookaheadName(input)); @@ -597,7 +594,7 @@ public class v2ParserATNSimulator extends ATNSimulator { public ATNConfigSet execATNWithFullContext(DFA dfa, DFAState D, // how far we got before failing over @NotNull ATNConfigSet s0, - @NotNull SymbolStream input, int startIndex, + @NotNull SymbolStream input, int startIndex, ParserRuleContext outerContext, int nalts, boolean greedy) @@ -1219,7 +1216,7 @@ public class v2ParserATNSimulator extends ATNSimulator { return String.valueOf(t); } - public String getLookaheadName(SymbolStream input) { + public String getLookaheadName(SymbolStream input) { return getTokenName(input.LA(1)); } @@ -1244,29 +1241,15 @@ public class v2ParserATNSimulator extends ATNSimulator { } @NotNull - public NoViableAltException noViableAlt(@NotNull SymbolStream input, + public NoViableAltException noViableAlt(@NotNull SymbolStream input, @NotNull ParserRuleContext outerContext, @NotNull ATNConfigSet configs, int startIndex) { - if ( parser instanceof TreeParser) { - Symbol startNode = null; - if ( input instanceof BufferedASTNodeStream) { - startNode = input.get(startIndex); - } - return new NoViableTreeGrammarAltException(parser, - (ASTNodeStream)input, - startNode, - input.LT(1), - configs, outerContext); - } - else { - return new NoViableAltException(parser, input, + return new NoViableAltException(parser, input, (Token)input.get(startIndex), (Token)input.LT(1), - input.LT(1), configs, outerContext); - } } public static int getUniqueAlt(@NotNull Collection configs) { 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 00a297b1a..c5c747789 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java @@ -33,6 +33,8 @@ import org.antlr.v4.runtime.*; import java.io.FileInputStream; import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.lang.reflect.Constructor; import java.lang.reflect.Method; @@ -50,8 +52,9 @@ public class TestRig { boolean gui = false; String psFile = null; boolean showTokens = false; + String encoding = null; if ( args.length < 2 ) { - System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName [-print] [-tokens] [-gui] [-ps file.ps] [input-filename]"); + System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName [-print] [-tokens] [-gui] [-encoding encodingname] [-ps file.ps] [input-filename]"); return; } int i=0; @@ -75,6 +78,14 @@ public class TestRig { if ( arg.equals("-tokens") ) { showTokens = true; } + else if ( arg.equals("-encoding") ) { + if ( i>=args.length ) { + System.err.println("missing encoding on -encoding"); + return; + } + encoding = args[i]; + i++; + } else if ( arg.equals("-ps") ) { if ( i>=args.length ) { System.err.println("missing filename on -ps"); @@ -101,40 +112,53 @@ public class TestRig { if ( inputFile!=null ) { is = new FileInputStream(inputFile); } + Reader r; + if ( encoding!=null ) { + r = new InputStreamReader(is, encoding); + } + else { + r = new InputStreamReader(is); + } - ANTLRInputStream input = new ANTLRInputStream(is); + try { + ANTLRInputStream input = new ANTLRInputStream(r); - Constructor lexerCtor = lexerClass.getConstructor(CharStream.class); - Lexer lexer = lexerCtor.newInstance(input); - CommonTokenStream tokens = new CommonTokenStream(lexer); + Constructor lexerCtor = lexerClass.getConstructor(CharStream.class); + Lexer lexer = lexerCtor.newInstance(input); + CommonTokenStream tokens = new CommonTokenStream(lexer); - if ( showTokens ) { - tokens.fill(); - for (Object tok : tokens.getTokens()) { - System.out.println(tok); + if ( showTokens ) { + tokens.fill(); + for (Object tok : tokens.getTokens()) { + System.out.println(tok); + } + } + + Constructor parserCtor = parserClass.getConstructor(TokenStream.class); + Parser parser = parserCtor.newInstance(tokens); + + parser.setErrorHandler(new DiagnosticErrorStrategy()); + + if ( printTree || gui || psFile!=null ) { + parser.setBuildParseTree(true); + } + + Method startRule = parserClass.getMethod(startRuleName, (Class[])null); + ParserRuleContext tree = (ParserRuleContext)startRule.invoke(parser, (Object[])null); + + if ( printTree ) { + System.out.println(tree.toStringTree(parser)); + } + if ( gui ) { + tree.inspect(parser); + } + if ( psFile!=null ) { + tree.save(parser, psFile); // Generate postscript } } - - Constructor parserCtor = parserClass.getConstructor(TokenStream.class); - Parser parser = parserCtor.newInstance(tokens); - - parser.setErrorHandler(new DiagnosticErrorStrategy()); - - if ( printTree || gui || psFile!=null ) { - parser.setBuildParseTree(true); - } - - Method startRule = parserClass.getMethod(startRuleName, (Class[])null); - ParserRuleContext tree = (ParserRuleContext)startRule.invoke(parser, (Object[])null); - - if ( printTree ) { - System.out.println(tree.toStringTree(parser)); - } - if ( gui ) { - tree.inspect(parser); - } - if ( psFile!=null ) { - tree.save(parser, psFile); // Generate postscript + finally { + if ( r!=null ) r.close(); + if ( is!=null ) is.close(); } } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/AST.java b/runtime/Java/src/org/antlr/v4/runtime/tree/AST.java deleted file mode 100644 index 8c0714cd2..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/AST.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -import org.antlr.v4.runtime.Token; - -/** An abstract syntax tree built by ANTLR during a parse. */ -public interface AST extends SyntaxTree { - /** Indicates the node is a nil node but may still have children, meaning - * the tree is a flat list. - */ - boolean isNil(); - - /** Return a token type; needed for tree parsing */ - int getType(); - - /** Text for this node alone, not the subtree */ - String getText(); - - // TODO: do we need line/charpos???? - /** In case we don't have a token payload, what is the line for errors? */ - int getLine(); - - int getCharPositionInLine(); - - /** Redefined from Tree interface so we can narrow the return type */ - @Override - AST getParent(); - - /** Redefined from Tree interface so we can narrow the return type */ - @Override - Token getPayload(); -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/ASTAdaptor.java b/runtime/Java/src/org/antlr/v4/runtime/tree/ASTAdaptor.java deleted file mode 100644 index a8bb8d680..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/ASTAdaptor.java +++ /dev/null @@ -1,262 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -import org.antlr.v4.runtime.Token; - -import java.util.List; - -/** How to create and navigate trees. Rather than have a separate factory - * and adaptor, I've merged them. Makes sense to encapsulate. - * - * I do not need to know the type of a tree at all so they are all - * generic Objects. - */ -public interface ASTAdaptor { - // BEGIN new v4 stuff - - /** Used to track elements to left of -> for use in rewrite. These - * are some kind of trees, but we generically use Object - * for tree types in ANTLR. - */ - public List createElementList(); - - /** Get the absolute index starting from 0 of this note within - * a node stream. This should w -ork even if the stream is not buffered - */ -// public int getNodeIndex(T t); - - // END new v4 stuff - - // C o n s t r u c t i o n - - /** Create a tree node from Token object; for CommonAST type trees, - * then the token just becomes the payload. This is the most - * common create call. - * - * Override if you want another kind of node to be built. - */ - public T create(Token payload); - - /** Duplicate a single tree node. - * Override if you want another kind of node to be built. - */ - public T dupNode(T treeNode); - - /** Duplicate tree recursively, using dupNode() for each node */ - public T dupTree(T tree); - - /** Return a nil node (an empty but non-null node) that can hold - * a list of element as the children. If you want a flat tree (a list) - * use "t=adaptor.nil(); t.addChild(x); t.addChild(y);" - */ - public T nil(); - - /** Is tree considered a nil node used to make lists of child nodes? */ - public boolean isNil(T tree); - - /** Add a child to the tree t. If child is a flat tree (a list), make all - * in list children of t. Warning: if t has no children, but child does - * and child isNil then you can decide it is ok to move children to t via - * t.children = child.children; i.e., without copying the array. Just - * make sure that this is consistent with have the user will build - * ASTs. Do nothing if t or child is null. - */ - public void addChild(T t, T child); - - /** If oldRoot is a nil root, just copy or move the children to newRoot. - * If not a nil root, make oldRoot a child of newRoot. - * - * old=^(nil a b c), new=r yields ^(r a b c) - * old=^(a b c), new=r yields ^(r ^(a b c)) - * - * If newRoot is a nil-rooted single child tree, use the single - * child as the new root node. - * - * old=^(nil a b c), new=^(nil r) yields ^(r a b c) - * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) - * - * If oldRoot was null, it's ok, just return newRoot (even if isNil). - * - * old=null, new=r yields r - * old=null, new=^(nil r) yields ^(nil r) - * - * Return newRoot. Throw an exception if newRoot is not a - * simple node or nil root with a single child node--it must be a root - * node. If newRoot is ^(nil x) return x as newRoot. - * - * Be advised that it's ok for newRoot to point at oldRoot's - * children; i.e., you don't have to copy the list. We are - * constructing these nodes so we should have this control for - * efficiency. - */ - public T becomeRoot(T newRoot, T oldRoot); - - /** Given the root of the subtree created for this rule, post process - * it to do any simplifications or whatever you want. A required - * behavior is to convert ^(nil singleSubtree) to singleSubtree - * as the setting of start/stop indexes relies on a single non-nil root - * for non-flat trees. - * - * Flat trees such as for lists like "idlist : ID+ ;" are left alone - * unless there is only one ID. For a list, the start/stop indexes - * are set in the nil node. - * - * This method is executed after all rule tree construction and right - * before setTokenBoundaries(). - */ - public T rulePostProcessing(T root); - - /** For identifying trees. - * - * How to identify nodes so we can say "add node to a prior node"? - * Even becomeRoot is an issue. Use System.identityHashCode(node) - * usually. - */ - public int getUniqueID(T node); - - - // R e w r i t e R u l e s - - /** Create a node for newRoot make it the root of oldRoot. - * If oldRoot is a nil root, just copy or move the children to newRoot. - * If not a nil root, make oldRoot a child of newRoot. - * - * Return node created for newRoot. - * - * Be advised: when debugging ASTs, the DebugTreeAdaptor manually - * calls create(Token child) and then plain becomeRoot(node, node) - * because it needs to trap calls to create, but it can't since it delegates - * to not inherits from the ASTAdaptor. - */ - public T becomeRoot(Token newRoot, T oldRoot); - - /** Create a new node derived from a token, with a new token type. - * This is invoked from an imaginary node ref on right side of a - * rewrite rule as IMAG[$tokenLabel]. - * - * This should invoke createToken(Token). - */ - public T create(int tokenType, Token fromToken); - - /** Same as create(tokenType,fromToken) except set the text too. - * This is invoked from an imaginary node ref on right side of a - * rewrite rule as IMAG[$tokenLabel, "IMAG"]. - * - * This should invoke createToken(Token). - */ - public T create(int tokenType, Token fromToken, String text); - - /** Create a new node derived from a token, with a new token type. - * This is invoked from an imaginary node ref on right side of a - * rewrite rule as IMAG["IMAG"]. - * - * This should invoke createToken(int,String). - */ - public T create(int tokenType, String text); - - - // C o n t e n t - - /** For tree parsing, I need to know the token type of a node */ - public int getType(T t); - - /** Node constructors can set the type of a node */ - public void setType(T t, int type); - - public String getText(T t); - - /** Node constructors can set the text of a node */ - public void setText(T t, String text); - - /** Return the token object from which this node was created. - * Currently used only for printing an error message. - * The error display routine in BaseRecognizer needs to - * display where the input the error occurred. If your - * tree of limitation does not store information that can - * lead you to the token, you can create a token filled with - * the appropriate information and pass that back. See - * BaseRecognizer.getErrorMessage(). - */ - public Token getToken(T t); - - /** Where are the bounds in the input token stream for this node and - * all children? Each rule that creates AST nodes will call this - * method right before returning. Flat trees (i.e., lists) will - * still usually have a nil root node just to hold the children list. - * That node would contain the start/stop indexes then. - */ - public void setTokenBoundaries(T t, Token startToken, Token stopToken); - - /** Get the token start index for this subtree; return -1 if no such index */ - public int getTokenStartIndex(T t); - - /** Get the token stop index for this subtree; return -1 if no such index */ - public int getTokenStopIndex(T t); - - - // N a v i g a t i o n / T r e e P a r s i n g - - /** Get a child 0..n-1 node */ - public T getChild(T t, int i); - - /** Set ith child (0..n-1) to t; t must be non-null and non-nil node */ - public void setChild(T t, int i, T child); - - /** Remove ith child and shift children down from right. */ - public T deleteChild(T t, int i); - - /** How many children? If 0, then this is a leaf node */ - public int getChildCount(T t); - - /** Who is the parent node of this node; if null, implies node is root. - * If your node type doesn't handle this, it's ok but the tree rewrites - * in tree parsers need this functionality. - */ - public T getParent(T t); - public void setParent(T t, T parent); - - /** What index is this node in the child list? Range: 0..n-1 - * If your node type doesn't handle this, it's ok but the tree rewrites - * in tree parsers need this functionality. - */ - public int getChildIndex(T t); - public void setChildIndex(T t, int index); - - /** Replace from start to stop child index of parent with t, which might - * be a list. Number of children may be different - * after this call. - * - * If parent is null, don't do anything; must be at root of overall tree. - * Can't replace whatever points to the parent externally. Do nothing. - */ - public void replaceChildren(T parent, int startChildIndex, int stopChildIndex, T t); -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/ASTContext.java b/runtime/Java/src/org/antlr/v4/runtime/tree/ASTContext.java deleted file mode 100644 index 3503bd03e..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/ASTContext.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -public interface ASTContext { - T getTree(); -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/ASTIterator.java b/runtime/Java/src/org/antlr/v4/runtime/tree/ASTIterator.java deleted file mode 100644 index a1735553d..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/ASTIterator.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.misc.FastQueue; - -import java.util.Iterator; - -/** Return a node stream from a doubly-linked tree whose nodes - * know what child index they are. No remove() is supported. - * - * Emit navigation nodes (DOWN, UP, and EOF) to let show tree structure. - */ -public class ASTIterator implements Iterator { - protected ASTAdaptor adaptor; - protected T root; - protected T tree; - protected boolean firstTime = true; - - // navigation nodes to return during walk and at end - public T up; - public T down; - public T eof; - - /** If we emit UP/DOWN nodes, we need to spit out multiple nodes per - * next() call. - */ - protected FastQueue nodes; - - public ASTIterator(T tree) { - this((tree instanceof CommonAST) ? (ASTAdaptor)new CommonASTAdaptor() : null, - tree); - } - - public ASTIterator(ASTAdaptor adaptor, T tree) { - this.adaptor = adaptor; - this.tree = tree; - this.root = tree; - nodes = new FastQueue(); - if ( adaptor!=null ) { - down = adaptor.create(Token.DOWN, "DOWN"); - up = adaptor.create(Token.UP, "UP"); - eof = adaptor.create(Token.EOF, "EOF"); - } - } - - public void reset() { - firstTime = true; - tree = root; - nodes.clear(); - } - - @Override - public boolean hasNext() { - if ( firstTime ) return root!=null; - if ( nodes!=null && nodes.size()>0 ) return true; - if ( tree==null ) return false; - if ( adaptor.getChildCount(tree)>0 ) return true; - return adaptor.getParent(tree)!=null; // back at root? - } - - @Override - public T next() { - if ( firstTime ) { // initial condition - firstTime = false; - if ( adaptor.getChildCount(tree)==0 ) { // single node tree (special) - nodes.add(eof); - return tree; - } - return tree; - } - // if any queued up, use those first - if ( nodes!=null && nodes.size()>0 ) return nodes.remove(); - - // no nodes left? - if ( tree==null ) return eof; - - // next node will be child 0 if any children - if ( adaptor.getChildCount(tree)>0 ) { - tree = adaptor.getChild(tree, 0); - nodes.add(tree); // real node is next after DOWN - return down; - } - // if no children, look for next sibling of tree or ancestor - T parent = adaptor.getParent(tree); - // while we're out of siblings, keep popping back up towards root - while ( parent!=null && - adaptor.getChildIndex(tree)+1 >= adaptor.getChildCount(parent) ) - { - nodes.add(up); // we're moving back up - tree = parent; - parent = adaptor.getParent(tree); - } - // no nodes left? - if ( parent==null ) { - tree = null; // back at root? nothing left then - nodes.add(eof); // add to queue, might have UP nodes in there - return nodes.remove(); - } - - // must have found a node with an unvisited sibling - // move to it and return it - int nextSiblingIndex = adaptor.getChildIndex(tree) + 1; - tree = adaptor.getChild(parent, nextSiblingIndex); - nodes.add(tree); // add to queue, might have UP nodes in there - return nodes.remove(); - } - - @Override - public void remove() { throw new UnsupportedOperationException(); } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/ASTNodeStream.java b/runtime/Java/src/org/antlr/v4/runtime/tree/ASTNodeStream.java deleted file mode 100644 index 961f1d85b..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/ASTNodeStream.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -import org.antlr.v4.runtime.*; - -/** A stream of tree nodes, accessing nodes from a tree of some kind */ -public interface ASTNodeStream extends SymbolStream { - /** Get a tree node at an absolute index i; 0..n-1. - * If you don't want to buffer up nodes, then this method makes no - * sense for you. - */ - @Override - T get(int i); - - /** Get tree node at current input pointer + i ahead where i=1 is next node. - * i<0 indicates nodes in the past. So LT(-1) is previous node, but - * implementations are not required to provide results for k < -1. - * LT(0) is undefined. For i>=n, return null. - * Return null for LT(0) and any index that results in an absolute address - * that is negative. - * - * This is analogus to the LT() method of the TokenStream, but this - * returns a tree node instead of a token. Makes code gen identical - * for both parser and tree grammars. :) - */ - @Override - T LT(int k); - - /** Where is this stream pulling nodes from? This is not the name, but - * the object that provides node objects. - */ - public T getTreeSource(); - - /** If the tree associated with this stream was created from a TokenStream, - * you can specify it here. Used to do rule $text attribute in tree - * parser. Optional unless you use tree parser rule text attribute - * or output=template and rewrite=true options. - */ - public TokenStream getTokenStream(); - - /** What adaptor can tell me how to interpret/navigate nodes and - * trees. E.g., get text of a node. - */ - public ASTAdaptor getTreeAdaptor(); - - /** As we flatten the tree, we use UP, DOWN nodes to represent - * the tree structure. When debugging we need unique nodes - * so we have to instantiate new ones. When doing normal tree - * parsing, it's slow and a waste of memory to create unique - * navigation nodes. Default should be false; - */ - public void setUniqueNavigationNodes(boolean uniqueNavigationNodes); - - /** Reset the tree node stream in such a way that it acts like - * a freshly constructed stream. - */ - public void reset(); - - /** Return the text of all nodes from start to stop, inclusive. - * If the stream does not buffer all the nodes then it can still - * walk recursively from start until stop. You can always return - * null or "" too, but users should not access $ruleLabel.text in - * an action of course in that case. - */ - public String toString(T start, T stop); - - - // REWRITING TREES (used by tree parser) - - /** Replace from start to stop child index of parent with t, which might - * be a list. Number of children may be different - * after this call. The stream is notified because it is walking the - * tree and might need to know you are monkeying with the underlying - * tree. Also, it might be able to modify the node stream to avoid - * restreaming for future phases. - * - * If parent is null, don't do anything; must be at root of overall tree. - * Can't replace whatever points to the parent externally. Do nothing. - */ - public void replaceChildren(T parent, int startChildIndex, int stopChildIndex, T t); -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/BaseAST.java b/runtime/Java/src/org/antlr/v4/runtime/tree/BaseAST.java deleted file mode 100644 index a3cf480ac..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/BaseAST.java +++ /dev/null @@ -1,308 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -import org.antlr.v4.runtime.BaseRecognizer; -import org.antlr.v4.runtime.misc.NotNull; -import org.antlr.v4.runtime.tree.gui.TreeViewer; - -import java.io.IOException; -import java.util.*; - -/** A generic AST implementation with no payload. You must subclass to - * actually have any user data. ANTLR v3 uses a list of children approach - * instead of the child-sibling approach in v2. A flat tree (a list) is - * an empty node whose children represent the list. An empty, but - * non-null node is called "nil". - */ -public abstract class BaseAST implements AST { - /** Who is the parent node of this node; if null, implies node is root */ - public BaseAST parent; - - protected List children; - - /** What index is this node in the child list? Range: 0..n-1 */ - public int childIndex = -1; - - public BaseAST() { - } - - /** Create a new node from an existing node does nothing for BaseTree - * as there are no fields other than the children list, which cannot - * be copied as the children are not considered part of this node. - */ - public BaseAST(AST node) { - } - - @Override - public BaseAST getChild(int i) { - if ( children==null || i>=children.size() ) { - return null; - } - return children.get(i); - } - - /** Get the children internal List; note that if you directly mess with - * the list, do so at your own risk. - */ - public List getChildren() { return children; } - - public AST getFirstChildWithType(int type) { - return Trees.getFirstChildWithType(this, type); - } - - @Override - public int getChildCount() { - if ( children==null ) return 0; - return children.size(); - } - - /** Add t as child of this node. - * - * Warning: if t has no children, but child does - * and child isNil then this routine moves children to t via - * t.children = child.children; i.e., without copying the array. - */ - public void addChild(BaseAST t) { - //System.out.println("add child "+t.toStringTree()+" "+this.toStringTree()); - //System.out.println("existing children: "+children); - if ( t==null ) { - return; // do nothing upon addChild(null) - } - BaseAST childTree = t; - if ( childTree.isNil() ) { // t is an empty node possibly with children - if ( this.children!=null && this.children == childTree.children ) { - throw new RuntimeException("attempt to add child list to itself"); - } - // just add all of childTree's children to this - if ( childTree.children!=null ) { - if ( this.children!=null ) { // must copy, this has children already - int n = childTree.children.size(); - for (int i = 0; i < n; i++) { - BaseAST c = childTree.children.get(i); - this.children.add(c); - // handle double-link stuff for each child of nil root - c.setParent(this); - c.setChildIndex(children.size()-1); - } - } - else { - // no children for this but t has children; just set pointer - // call general freshener routine - this.children = childTree.children; - this.freshenParentAndChildIndexes(); - } - } - } - else { // child is not nil (don't care about children) - if ( children==null ) { - children = createChildrenList(); // create children list on demand - } - children.add(t); - childTree.setParent(this); - childTree.setChildIndex(children.size()-1); - } - // System.out.println("now children are: "+children); - } - - /** Add all elements of kids list as children of this node */ - public void addChildren(List kids) { - if ( kids==null ) return; - for (int i = 0; i < kids.size(); i++) { - BaseAST t = kids.get(i); - addChild(t); - } - } - - public void setChild(int i, BaseAST t) { - if ( t==null ) { - return; - } - if ( t.isNil() ) { - throw new IllegalArgumentException("Can't set single child to a list"); - } - if ( children==null ) { - children = createChildrenList(); - } - children.set(i, t); - t.setParent(this); - t.setChildIndex(i); - } - - public int getChildIndex() { - return childIndex; - } - - @Override - public AST getParent() { - return parent; - } - - public void setParent(BaseAST t) { - this.parent = t; - } - - public void setChildIndex(int index) { - this.childIndex = index; - } - - public BaseAST deleteChild(int i) { - if ( children==null ) { - return null; - } - BaseAST killed = children.remove(i); - // walk rest and decrement their child indexes - this.freshenParentAndChildIndexes(i); - return killed; - } - - public boolean deleteChild(BaseAST t) { - for (int i=0; i getChildCount()) { - throw new IndexOutOfBoundsException(i+" out or range"); - } - if ( children==null ) children = createChildrenList(); - children.add(i, t); - // walk others to increment their child indexes - // set index, parent of this one too - this.freshenParentAndChildIndexes(i); - } - - /** Override in a subclass to change the impl of children list */ - protected List createChildrenList() { - return new ArrayList(); - } - - @Override - public boolean isNil() { - return false; - } - - /** Set the parent and child index values for all child of t */ - public void freshenParentAndChildIndexes() { - freshenParentAndChildIndexes(0); - } - - public void freshenParentAndChildIndexes(int offset) { - int n = getChildCount(); - for (int c = offset; c < n; c++) { - BaseAST child = getChild(c); - child.setChildIndex(c); - child.setParent(this); - } - } - - public void freshenParentAndChildIndexesDeeply() { - freshenParentAndChildIndexesDeeply(0); - } - - public void freshenParentAndChildIndexesDeeply(int offset) { - int n = getChildCount(); - for (int c = offset; c < n; c++) { - BaseAST child = getChild(c); - child.setChildIndex(c); - child.setParent(this); - child.freshenParentAndChildIndexesDeeply(); - } - } - - /** Walk upwards looking for ancestor with this token type. */ - public boolean hasAncestor(int ttype) { return getAncestor(ttype)!=null; } - - /** Walk upwards and get first ancestor with this token type. */ - public Tree getAncestor(int ttype) { return Trees.getAncestor(this, ttype); } - - /** Return a list of all ancestors of this node. The first node of - * list is the root and the last is the parent of this node. - */ - @NotNull - public List getAncestors() { return Trees.getAncestors(this); } - - public void inspect(BaseRecognizer parser) { - TreeViewer viewer = new TreeViewer(parser, this); - viewer.open(); - } - - public void save(BaseRecognizer parser, String fileName) - throws IOException - { - Trees.writePS(this, parser, fileName); - } - - public void save(BaseRecognizer parser, String fileName, - String fontName, int fontSize) - throws IOException - { - Trees.writePS(this, parser, fileName, fontName, fontSize); - } - - /** Don't use standard tree printing mechanism since ASTs can have nil - * root nodes. - */ - @Override - public String toStringTree() { - return Trees.toStringTree(this, null); -// if ( children==null || children.size()==0 ) { -// return this.toString(); -// } -// StringBuffer buf = new StringBuffer(); -// if ( !isNil() ) { -// buf.append("("); -// buf.append(this.toString()); -// buf.append(' '); -// } -// for (int i = 0; children!=null && i < children.size(); i++) { -// AST t = children.get(i); -// if ( i>0 ) { -// buf.append(' '); -// } -// buf.append(t.toStringTree()); -// } -// if ( !isNil() ) { -// buf.append(")"); -// } -// return buf.toString(); - } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/BaseASTAdaptor.java b/runtime/Java/src/org/antlr/v4/runtime/tree/BaseASTAdaptor.java deleted file mode 100644 index 722fa73f4..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/BaseASTAdaptor.java +++ /dev/null @@ -1,286 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.WritableToken; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** An ASTAdaptor that works with any BaseAST implementation. */ -public abstract class BaseASTAdaptor implements ASTAdaptor { - /** System.identityHashCode() is not always unique; we have to - * track ourselves. That's ok, it's only for debugging, though it's - * expensive: we have to create a hashtable with all tree nodes in it. - * TODO: rm? - */ - protected Map treeToUniqueIDMap; - protected int uniqueNodeID = 1; - - @Override - public List createElementList() { - return new ElementList(this); - } - - // END v4 stuff - - @Override - public T nil() { - return create(null); - } - - @Override - public boolean isNil(T tree) { - return tree.isNil(); - } - - @Override - public T dupTree(T tree) { - return dupTree(tree, null); - } - - /** This is generic in the sense that it will work with any kind of - * tree (not just AST interface). It invokes the adaptor routines - * not the tree node routines to do the construction. - */ - public T dupTree(T t, T parent) { - if ( t==null ) { - return null; - } - T newTree = dupNode(t); - // ensure new subtree root has parent/child index set - setChildIndex(newTree, getChildIndex(t)); // same index in new tree - setParent(newTree, parent); - int n = getChildCount(t); - for (int i = 0; i < n; i++) { - T child = getChild(t, i); - T newSubTree = dupTree(child, t); - addChild(newTree, newSubTree); - } - return newTree; - } - - /** Add a child to the tree t. If child is a flat tree (a list), make all - * in list children of t. Warning: if t has no children, but child does - * and child isNil then you can decide it is ok to move children to t via - * t.children = child.children; i.e., without copying the array. Just - * make sure that this is consistent with have the user will build - * ASTs. - */ - @Override - public void addChild(T t, T child) { - if ( t!=null && child!=null ) { - t.addChild(child); - } - } - - /** If oldRoot is a nil root, just copy or move the children to newRoot. - * If not a nil root, make oldRoot a child of newRoot. - * - * old=^(nil a b c), new=r yields ^(r a b c) - * old=^(a b c), new=r yields ^(r ^(a b c)) - * - * If newRoot is a nil-rooted single child tree, use the single - * child as the new root node. - * - * old=^(nil a b c), new=^(nil r) yields ^(r a b c) - * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) - * - * If oldRoot was null, it's ok, just return newRoot (even if isNil). - * - * old=null, new=r yields r - * old=null, new=^(nil r) yields ^(nil r) - * - * Return newRoot. Throw an exception if newRoot is not a - * simple node or nil root with a single child node--it must be a root - * node. If newRoot is ^(nil x) return x as newRoot. - * - * Be advised that it's ok for newRoot to point at oldRoot's - * children; i.e., you don't have to copy the list. We are - * constructing these nodes so we should have this control for - * efficiency. - */ - @Override - public T becomeRoot(T newRoot, T oldRoot) { - //System.out.println("becomeroot new "+newRoot.toString()+" old "+oldRoot); - if ( oldRoot==null ) { - return newRoot; - } - if ( newRoot==null ) { - return oldRoot; - } - // handle ^(nil real-node) - if ( newRoot.isNil() ) { - int nc = newRoot.getChildCount(); - if ( nc==1 ) newRoot = (T)newRoot.getChild(0); - else if ( nc >1 ) { - // TODO: make tree run time exceptions hierarchy - throw new RuntimeException("more than one node as root (TODO: make exception hierarchy)"); - } - } - // add oldRoot to newRoot; addChild takes care of case where oldRoot - // is a flat list (i.e., nil-rooted tree). All children of oldRoot - // are added to newRoot. - newRoot.addChild(oldRoot); - return newRoot; - } - - /** Transform ^(nil x) to x and nil to null */ - @Override - public T rulePostProcessing(T root) { - //System.out.println("rulePostProcessing: "+((AST)root).toStringTree()); - if ( root!=null && root.isNil() ) { - if ( root.getChildCount()==0 ) { - root = null; - } - else if ( root.getChildCount()==1 ) { - root = (T)root.getChild(0); - // whoever invokes rule will set parent and child index - root.setParent(null); - root.setChildIndex(-1); - } - } - return root; - } - - @Override - public T becomeRoot(Token newRoot, T oldRoot) { - return becomeRoot(create(newRoot), oldRoot); - } - - @Override - public T create(int tokenType, Token fromToken) { - WritableToken tok = createToken(fromToken); - //((ClassicToken)fromToken).setType(tokenType); - tok.setType(tokenType); - return create(tok); - } - - @Override - public T create(int tokenType, Token fromToken, String text) { - if (fromToken == null) return create(tokenType, text); - WritableToken tok = createToken(fromToken); - tok.setType(tokenType); - tok.setText(text); - return create(tok); - } - - @Override - public T create(int tokenType, String text) { - Token fromToken = createToken(tokenType, text); - return create(fromToken); - } - - @Override - public int getType(T t) { - return t.getType(); - } - - @Override - public void setType(T t, int type) { - throw new UnsupportedOperationException("don't know enough about AST node"); - } - - @Override - public String getText(T t) { - return t.getText(); - } - - @Override - public void setText(T t, String text) { - throw new UnsupportedOperationException("don't know enough about AST node"); - } - - @Override - public T getChild(T t, int i) { - return (T)t.getChild(i); - } - - @Override - public void setChild(T t, int i, T child) { - t.setChild(i, child); - } - - @Override - public T deleteChild(T t, int i) { - return (T)t.deleteChild(i); - } - - @Override - public int getChildCount(T t) { - return t.getChildCount(); - } - - @Override - public int getUniqueID(T node) { - if ( treeToUniqueIDMap==null ) { - treeToUniqueIDMap = new HashMap(); - } - Integer prevID = treeToUniqueIDMap.get(node); - if ( prevID!=null ) { - return prevID; - } - int ID = uniqueNodeID; - treeToUniqueIDMap.put(node, ID); - uniqueNodeID++; - return ID; - // GC makes these nonunique: - // return System.identityHashCode(node); - } - - /** Tell me how to create a token for use with imaginary token nodes. - * For example, there is probably no input symbol associated with imaginary - * token DECL, but you need to create it as a payload or whatever for - * the DECL node as in ^(DECL type ID). - * - * If you care what the token payload objects' type is, you should - * override this method and any other createToken variant. - */ - public abstract WritableToken createToken(int tokenType, String text); - - /** Tell me how to create a token for use with imaginary token nodes. - * For example, there is probably no input symbol associated with imaginary - * token DECL, but you need to create it as a payload or whatever for - * the DECL node as in ^(DECL type ID). - * - * This is a variant of createToken where the new token is derived from - * an actual real input token. Typically this is for converting '{' - * tokens to BLOCK etc... You'll see - * - * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; - * - * If you care what the token payload objects' type is, you should - * override this method and any other createToken variant. - */ - public abstract WritableToken createToken(Token fromToken); -} - diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/BufferedASTNodeStream.java b/runtime/Java/src/org/antlr/v4/runtime/tree/BufferedASTNodeStream.java deleted file mode 100644 index f297e8d91..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/BufferedASTNodeStream.java +++ /dev/null @@ -1,524 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2005-2009 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.tree; - -import org.antlr.v4.runtime.*; - -import java.util.*; - -/** A buffered stream of tree nodes. Nodes can be from a tree of ANY kind. - * - * This node stream sucks all nodes out of the tree specified in - * the constructor during construction and makes pointers into - * the tree using an array of Object pointers. The stream necessarily - * includes pointers to DOWN and UP and EOF nodes. - * - * This stream knows how to mark/release for backtracking. - * - * This stream is most suitable for tree interpreters that need to - * jump around a lot or for tree parsers requiring speed (at cost of memory). - * There is some duplicated functionality here with UnBufferedASTNodeStream - * but just in bookkeeping, not tree walking etc... - * - * TARGET DEVELOPERS: - * - * This is the old CommonASTNodeStream that buffered up entire node stream. - * No need to implement really as new CommonASTNodeStream is much better - * and covers what we need. - * - * @see CommonASTNodeStream - */ -public class BufferedASTNodeStream implements ASTNodeStream { - public static final int DEFAULT_INITIAL_BUFFER_SIZE = 100; - public static final int INITIAL_CALL_STACK_SIZE = 10; - - protected class StreamIterator implements Iterator { - int i = 0; - @Override - public boolean hasNext() { - return i nodes; - - /** Pull nodes from which tree? */ - protected T root; - - /** IF this tree (root) was created from a token stream, track it. */ - protected TokenStream tokens; - - /** What tree adaptor was used to build these trees */ - ASTAdaptor adaptor; - - /** Reuse same DOWN, UP navigation nodes unless this is true */ - protected boolean uniqueNavigationNodes = false; - - /** The index into the nodes list of the current node (next node - * to consume). If -1, nodes array not filled yet. - */ - protected int p = -1; - - /** Track the last mark() call result value for use in rewind(). */ - protected int lastMarker; - - /** Stack of indexes used for push/pop calls */ - protected List calls; - - public BufferedASTNodeStream(T tree) { - this((ASTAdaptor)new CommonASTAdaptor(), tree); - } - - public BufferedASTNodeStream(ASTAdaptor adaptor, T tree) { - this(adaptor, tree, DEFAULT_INITIAL_BUFFER_SIZE); - } - - public BufferedASTNodeStream(ASTAdaptor adaptor, T tree, int initialBufferSize) { - this.root = tree; - this.adaptor = adaptor; - nodes = new ArrayList(initialBufferSize); - down = adaptor.create(Token.DOWN, "DOWN"); - up = adaptor.create(Token.UP, "UP"); - eof = adaptor.create(Token.EOF, "EOF"); - } - - /** Walk tree with depth-first-search and fill nodes buffer. - * Don't do DOWN, UP nodes if its a list (t is isNil). - */ - protected void fillBuffer() { - fillBuffer(root); - //System.out.println("revIndex="+tokenTypeToStreamIndexesMap); - p = 0; // buffer of nodes intialized now - } - - public void fillBuffer(T t) { - boolean nil = adaptor.isNil(t); - if ( !nil ) { - nodes.add(t); // add this node - } - // add DOWN node if t has children - int n = adaptor.getChildCount(t); - if ( !nil && n>0 ) { - addNavigationNode(Token.DOWN); - } - // and now add all its children - for (int c=0; c0 ) { - addNavigationNode(Token.UP); - } - } - - /** What is the stream index for node? 0..n-1 - * Return -1 if node not found. - */ - protected int getNodeIndex(T node) { - if ( p==-1 ) { - fillBuffer(); - } - for (int i = 0; i < nodes.size(); i++) { - T t = nodes.get(i); - if ( t==node ) { - return i; - } - } - return -1; - } - - /** As we flatten the tree, we use UP, DOWN nodes to represent - * the tree structure. When debugging we need unique nodes - * so instantiate new ones when uniqueNavigationNodes is true. - */ - protected void addNavigationNode(final int ttype) { - T navNode = null; - if ( ttype==Token.DOWN ) { - if ( hasUniqueNavigationNodes() ) { - navNode = adaptor.create(Token.DOWN, "DOWN"); - } - else { - navNode = down; - } - } - else { - if ( hasUniqueNavigationNodes() ) { - navNode = adaptor.create(Token.UP, "UP"); - } - else { - navNode = up; - } - } - nodes.add(navNode); - } - - @Override - public T get(int i) { - if ( p==-1 ) { - fillBuffer(); - } - return nodes.get(i); - } - - public List get(int i, int j) { - if ( p==-1 ) { - fillBuffer(); - } - return nodes.subList(i,j+1); - } - - public List get(T start, T stop) { - int i=0; - for (; i=nodes.size() ) return null; - if ( j>=nodes.size() ) j = nodes.size()-1; - return get(i,j); - } - - @Override - public T LT(int k) { - if ( p==-1 ) { - fillBuffer(); - } - if ( k==0 ) { - return null; - } - if ( k<0 ) { - return LB(-k); - } - //System.out.print("LT(p="+p+","+k+")="); - if ( (p+k-1) >= nodes.size() ) { - return eof; - } - return nodes.get(p+k-1); - } - - public T getCurrentSymbol() { return LT(1); } - -/* - public T getLastTreeNode() { - int i = index(); - if ( i>=size() ) { - i--; // if at EOF, have to start one back - } - System.out.println("start last node: "+i+" size=="+nodes.size()); - while ( i>=0 && - (adaptor.getType(get(i))==Token.EOF || - adaptor.getType(get(i))==Token.UP || - adaptor.getType(get(i))==Token.DOWN) ) - { - i--; - } - System.out.println("stop at node: "+i+" "+nodes.get(i)); - return nodes.get(i); - } -*/ - - /** Look backwards k nodes */ - protected T LB(int k) { - if ( k==0 ) { - return null; - } - if ( (p-k)<0 ) { - return null; - } - return nodes.get(p-k); - } - - @Override - public T getTreeSource() { - return root; - } - - @Override - public String getSourceName() { - return getTokenStream().getSourceName(); - } - - @Override - public ASTAdaptor getTreeAdaptor() { - return adaptor; - } - - @Override - public TokenStream getTokenStream() { - return tokens; - } - - public void setTokenStream(TokenStream tokens) { - this.tokens = tokens; - } - - public ASTAdaptor getASTAdaptor() { - return adaptor; - } - - public void setASTAdaptor(ASTAdaptor adaptor) { - this.adaptor = adaptor; - } - - public boolean hasUniqueNavigationNodes() { - return uniqueNavigationNodes; - } - - @Override - public void setUniqueNavigationNodes(boolean uniqueNavigationNodes) { - this.uniqueNavigationNodes = uniqueNavigationNodes; - } - - @Override - public void consume() { - if ( p==-1 ) { - fillBuffer(); - } - p++; - } - - @Override - public int LA(int i) { - return adaptor.getType(LT(i)); - } - - @Override - public int mark() { - if ( p==-1 ) { - fillBuffer(); - } - lastMarker = index(); - return lastMarker; - } - - @Override - public void release(int marker) { - // no resources to release - } - - @Override - public int index() { - return p; - } - - public void rewind(int marker) { - seek(marker); - } - - public void rewind() { - seek(lastMarker); - } - - @Override - public void seek(int index) { - if ( p==-1 ) { - fillBuffer(); - } - p = index; - } - - /** Make stream jump to a new location, saving old location. - * Switch back with pop(). - */ - public void push(int index) { - if ( calls==null ) { - calls = new ArrayList(); - } - calls.add(p); // save current index - seek(index); - } - - /** Seek back to previous index saved during last push() call. - * Return top of stack (return index). - */ - public int pop() { - int ret = calls.remove(calls.size() - 1); - seek(ret); - return ret; - } - - @Override - public void reset() { - p = 0; - lastMarker = 0; - if (calls != null) { - calls.clear(); - } - } - - @Override - public int size() { - if ( p==-1 ) { - fillBuffer(); - } - return nodes.size(); - } - - public Iterator iterator() { - if ( p==-1 ) { - fillBuffer(); - } - return new StreamIterator(); - } - - // TREE REWRITE INTERFACE - - @Override - public void replaceChildren(T parent, int startChildIndex, int stopChildIndex, T t) { - if ( parent!=null ) { - adaptor.replaceChildren(parent, startChildIndex, stopChildIndex, t); - } - } - - /** Used for testing, just return the token type stream */ - public String toTokenTypeString() { - if ( p==-1 ) { - fillBuffer(); - } - StringBuffer buf = new StringBuffer(); - for (int i = 0; i < nodes.size(); i++) { - T t = nodes.get(i); - buf.append(" "); - buf.append(adaptor.getType(t)); - } - return buf.toString(); - } - - /** Debugging */ - public String toTokenString(int start, int stop) { - if ( p==-1 ) { - fillBuffer(); - } - StringBuffer buf = new StringBuffer(); - for (int i = start; i < nodes.size() && i <= stop; i++) { - T t = nodes.get(i); - buf.append(" "); - buf.append(adaptor.getToken(t)); - } - return buf.toString(); - } - - @Override - public String toString(T start, T stop) { - System.out.println("toString"); - if ( start==null || stop==null ) { - return null; - } - if ( p==-1 ) { - fillBuffer(); - } - //System.out.println("stop: "+stop); - if ( start instanceof CommonAST ) - System.out.print("toString: "+((CommonAST)start).getToken()+", "); - else - System.out.println(start); - if ( stop instanceof CommonAST ) - System.out.println(((CommonAST)stop).getToken()); - else - System.out.println(stop); - // if we have the token stream, use that to dump text in order - if ( tokens!=null ) { - int beginTokenIndex = adaptor.getTokenStartIndex(start); - int endTokenIndex = adaptor.getTokenStopIndex(stop); - // if it's a tree, use start/stop index from start node - // else use token range from start/stop nodes - if ( adaptor.getType(stop)==Token.UP ) { - endTokenIndex = adaptor.getTokenStopIndex(start); - } - else if ( adaptor.getType(stop)==Token.EOF ) { - endTokenIndex = size()-2; // don't use EOF - } - return tokens.toString(beginTokenIndex, endTokenIndex); - } - // walk nodes looking for start - T t = null; - int i = 0; - for (; i < nodes.size(); i++) { - t = nodes.get(i); - if ( t==start ) { - break; - } - } - // now walk until we see stop, filling string buffer with text - StringBuffer buf = new StringBuffer(); - t = nodes.get(i); - while ( t!=stop ) { - String text = adaptor.getText(t); - if ( text==null ) { - text = " "+String.valueOf(adaptor.getType(t)); - } - buf.append(text); - i++; - t = nodes.get(i); - } - // include stop node too - String text = adaptor.getText(stop); - if ( text==null ) { - text = " "+String.valueOf(adaptor.getType(stop)); - } - buf.append(text); - return buf.toString(); - } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonAST.java b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonAST.java deleted file mode 100644 index e591e2f06..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonAST.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.misc.Interval; - -/** A tree node that is wrapper for a Token object. */ -public class CommonAST extends BaseAST { - /** A single token is the payload */ - public Token token; - - /** What token indexes bracket all tokens associated with this node - * and below? - */ - protected int startIndex=-1, stopIndex=-1; - - public CommonAST() { } - - public CommonAST(CommonAST node) { - super(node); - this.token = node.token; - this.startIndex = node.startIndex; - this.stopIndex = node.stopIndex; - } - - public CommonAST(Token t) { - this.token = t; - } - - public Token getToken() { - return token; - } - - @Override - public Token getPayload() { - return getToken(); - } - - @Override - public Interval getSourceInterval() { - return new Interval(getTokenStartIndex(), getTokenStopIndex()); - } - - @Override - public boolean isNil() { - return token==null; - } - - @Override - public CommonAST getChild(int i) { - return (CommonAST)super.getChild(i); - } - - @Override - public CommonAST getParent() { - return (CommonAST)super.getParent(); - } - - @Override - public int getType() { - if ( token==null ) { - return Token.INVALID_TYPE; - } - return token.getType(); - } - - @Override - public String getText() { - if ( token==null ) { - return null; - } - return token.getText(); - } - - @Override - public int getLine() { - if ( token==null || token.getLine()==0 ) { - if ( getChildCount()>0 ) { - return getChild(0).getLine(); - } - return 0; - } - return token.getLine(); - } - - @Override - public int getCharPositionInLine() { - if ( token==null || token.getCharPositionInLine()==-1 ) { - if ( getChildCount()>0 ) { - return getChild(0).getCharPositionInLine(); - } - return 0; - } - return token.getCharPositionInLine(); - } - - public int getTokenStartIndex() { - if ( startIndex==-1 && token!=null ) { - return token.getTokenIndex(); - } - return startIndex; - } - - public void setTokenStartIndex(int index) { - startIndex = index; - } - - public int getTokenStopIndex() { - if ( stopIndex==-1 && token!=null ) { - return token.getTokenIndex(); - } - return stopIndex; - } - - public void setTokenStopIndex(int index) { - stopIndex = index; - } - - public String toString() { - if ( isNil() ) { - return "nil"; - } - if ( getType()==Token.INVALID_TYPE) { - return ""; - } - if ( token==null ) { - return null; - } - return token.getText(); - } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTAdaptor.java b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTAdaptor.java deleted file mode 100644 index 802708b48..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTAdaptor.java +++ /dev/null @@ -1,185 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -import org.antlr.v4.runtime.*; - -/** An ASTAdaptor that works with CommonAST. It provides - * really just factory methods; all the work is done by BaseTreeAdaptor. - * If you would like to have different tokens created than CommonToken - * objects, you need to override this and then set the parser tree adaptor to - * use your subclass. - * - * To get your parser to build nodes of a different type, override - * create(Token). - */ -public class CommonASTAdaptor extends BaseASTAdaptor { - /** Duplicate a node. This is part of the factory; - * override if you want another kind of node to be built. - * - * I could use reflection to prevent having to override this - * but reflection is slow. - */ - @Override - public CommonAST dupNode(CommonAST t) { - if ( t==null ) return null; - return new CommonAST(t); - } - - @Override - public CommonAST create(Token payload) { - return new CommonAST(payload); - } - - - - /** Tell me how to create a token for use with imaginary token nodes. - * For example, there is probably no input symbol associated with imaginary - * token DECL, but you need to create it as a payload or whatever for - * the DECL node as in ^(DECL type ID). - * - * If you care what the token payload objects' type is, you should - * override this method and any other createToken variant. - */ - @Override - public WritableToken createToken(int tokenType, String text) { - return new CommonToken(tokenType, text); - } - - /** Tell me how to create a token for use with imaginary token nodes. - * For example, there is probably no input symbol associated with imaginary - * token DECL, but you need to create it as a payload or whatever for - * the DECL node as in ^(DECL type ID). - * - * This is a variant of createToken where the new token is derived from - * an actual real input token. Typically this is for converting '{' - * tokens to BLOCK etc... You'll see - * - * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; - * - * If you care what the token payload objects' type is, you should - * override this method and any other createToken variant. - */ - @Override - public WritableToken createToken(Token fromToken) { - return new CommonToken(fromToken); - } - - /** Track start/stop token for subtree root created for a rule. - * Only works with CommonAST nodes. For rules that match nothing, - * seems like this will yield start=i and stop=i-1 in a nil node. - * Might be useful info so I'll not force to be i..i. - */ - @Override - public void setTokenBoundaries(CommonAST t, Token startToken, Token stopToken) { - if ( t==null ) return; - int start = 0; - int stop = 0; - if ( startToken!=null ) start = startToken.getTokenIndex(); - if ( stopToken!=null ) stop = stopToken.getTokenIndex(); - t.setTokenStartIndex(start); - t.setTokenStopIndex(stop); - } - - @Override - public int getTokenStartIndex(CommonAST t) { - if ( t==null ) return -1; - return t.getTokenStartIndex(); - } - - @Override - public int getTokenStopIndex(CommonAST t) { - if ( t==null ) return -1; - return t.getTokenStopIndex(); - } - - @Override - public String getText(CommonAST t) { - if ( t==null ) return null; - return t.getText(); - } - - @Override - public int getType(CommonAST t) { - if ( t==null ) return Token.INVALID_TYPE; - return t.getType(); - } - - /** What is the Token associated with this node? If - * you are not using CommonAST, then you must - * override this in your own adaptor. - */ - @Override - public Token getToken(CommonAST t) { - if ( t==null ) return null; - return t.getToken(); - } - - @Override - public CommonAST getChild(CommonAST t, int i) { - if ( t==null ) return null; - return t.getChild(i); - } - - @Override - public int getChildCount(CommonAST t) { - if ( t==null ) return 0; - return t.getChildCount(); - } - - @Override - public CommonAST getParent(CommonAST t) { - if ( t==null ) return null; - return t.getParent(); - } - - @Override - public void setParent(CommonAST t, CommonAST parent) { - if ( t!=null ) t.setParent(parent); - } - - @Override - public int getChildIndex(CommonAST t) { - if ( t==null ) return 0; - return t.getChildIndex(); - } - - @Override - public void setChildIndex(CommonAST t, int index) { - if ( t!=null ) t.setChildIndex(index); - } - - @Override - public void replaceChildren(CommonAST parent, int startChildIndex, int stopChildIndex, CommonAST t) { - if ( parent!=null ) { - Trees.replaceChildren(parent, startChildIndex, stopChildIndex, t); - } - } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTNodeStream.java b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTNodeStream.java deleted file mode 100644 index 2727a46b5..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTNodeStream.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.misc.LookaheadStream; - -import java.util.Stack; - -public class CommonASTNodeStream extends LookaheadStream - implements ASTNodeStream -{ - public static final int DEFAULT_INITIAL_BUFFER_SIZE = 100; - public static final int INITIAL_CALL_STACK_SIZE = 10; - - /** Pull nodes from which tree? */ - protected T root; - - /** If this tree (root) was created from a token stream, track it. */ - protected TokenStream tokens; - - /** What tree adaptor was used to build these trees */ - ASTAdaptor adaptor; - - /** The tree iterator we using */ - protected ASTIterator it; - - /** Stack of indexes used for push/pop calls */ - protected Stack calls; - - /** Tree (nil A B C) trees like flat A B C streams */ - protected boolean hasNilRoot = false; - - /** Tracks tree depth. Level=0 means we're at root node level. */ - protected int level = 0; - - public CommonASTNodeStream(T tree) { - this((tree instanceof CommonAST)?(ASTAdaptor)new CommonASTAdaptor():null, - tree); - } - - public CommonASTNodeStream(ASTAdaptor adaptor, T tree) { - this.root = tree; - this.adaptor = adaptor; - it = new ASTIterator(adaptor,root); - } - - @Override - public void reset() { - super.reset(); - it.reset(); - hasNilRoot = false; - level = 0; - if ( calls != null ) calls.clear(); - } - - /** Pull elements from tree iterator. Track tree level 0..max_level. - * If nil rooted tree, don't give initial nil and DOWN nor final UP. - */ - @Override - public T nextElement() { - T t = it.next(); - //System.out.println("pulled "+adaptor.getType(t)); - if ( t == it.up ) { - level--; - if ( level==0 && hasNilRoot ) return it.next(); // don't give last UP; get EOF - } - else if ( t == it.down ) level++; - if ( level==0 && adaptor.isNil(t) ) { // if nil root, scarf nil, DOWN - hasNilRoot = true; - t = it.next(); // t is now DOWN, so get first real node next - level++; - t = it.next(); - } - return t; - } - - @Override - public boolean isEOF(T o) { return adaptor.getType(o) == Token.EOF; } - - @Override - public void setUniqueNavigationNodes(boolean uniqueNavigationNodes) { } - - @Override - public T getTreeSource() { return root; } - - @Override - public String getSourceName() { return getTokenStream().getSourceName(); } - - @Override - public TokenStream getTokenStream() { return tokens; } - - public void setTokenStream(TokenStream tokens) { this.tokens = tokens; } - - @Override - public ASTAdaptor getTreeAdaptor() { return adaptor; } - - public void setTreeAdaptor(ASTAdaptor adaptor) { this.adaptor = adaptor; } - - @Override - public T get(int i) { - throw new UnsupportedOperationException("Absolute node indexes are meaningless in an unbuffered stream"); - } - - @Override - public int LA(int i) { return adaptor.getType(LT(i)); } - - /** Make stream jump to a new location, saving old location. - * Switch back with pop(). - */ - public void push(int index) { - if ( calls==null ) { - calls = new Stack(); - } - calls.push(p); // save current index - seek(index); - } - - /** Seek back to previous index saved during last push() call. - * Return top of stack (return index). - */ - public int pop() { - int ret = calls.pop(); - seek(ret); - return ret; - } - - // TREE REWRITE INTERFACE - - @Override - public void replaceChildren(T parent, int startChildIndex, int stopChildIndex, T t) { - if ( parent!=null ) { - adaptor.replaceChildren(parent, startChildIndex, stopChildIndex, t); - } - } - - /** Print the token text between start and stop nodes. If stop is an UP - * node, then we have to walk it back until we see the first non-UP node. - * Then, just get the token indexes and look into the token stream. - */ - @Override - public String toString(T start, T stop) { - if ( tokens==null ) throw new UnsupportedOperationException("can't print from null token stream in node stream"); - if ( start==null || stop==null ) return ""; - Token startToken = adaptor.getToken(start); - Token stopToken = adaptor.getToken(stop); - while ( stopToken.getType()==Token.UP ) { - stopToken = adaptor.getToken(stop); - } - return tokens.toString(startToken.getTokenIndex(), stopToken.getTokenIndex()); - } - - /** For debugging; destructive: moves tree iterator to end. */ - public String toTokenTypeString() { - reset(); - StringBuffer buf = new StringBuffer(); - T o = LT(1); - int type = adaptor.getType(o); - while ( type!=Token.EOF ) { - buf.append(" "); - buf.append(type); - consume(); - o = LT(1); - type = adaptor.getType(o); - } - return buf.toString(); - } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/ElementList.java b/runtime/Java/src/org/antlr/v4/runtime/tree/ElementList.java deleted file mode 100644 index acca68ae9..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/ElementList.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -import java.util.*; - -/** This list tracks elements to left of -> for use on right of -> */ -public class ElementList extends ArrayList { - protected ASTAdaptor adaptor; - - /** Once a node / subtree has been used in a stream, it must be dup'd - * from then on. - */ - protected HashSet used = new HashSet(); - - public class ElementListIterator implements Iterator { - int cursor = 0; - - /** If just 1 element, we still track cursor; next() will dup if - * cursor beyond 1 element. - */ - @Override - public boolean hasNext() { - int n = size(); - return (n==1 && cursor<1) || (n>1 && cursor= n) { // out of elements? - if ( n == 1 ) { // if size is 1, it's ok; return and we'll dup - return adaptor.dupTree( get(0) ); - } - // out of elements and size was not 1, so we can't dup - throw new RewriteCardinalityException("size=="+n+" and out of elements"); - } - - // we have elements - if ( n == 1 ) { - cursor++; // move cursor even for single element list - return adaptor.dupTree( get(0) ); - } - // must have more than one in list, pull from elements - E e = get(cursor); - cursor++; - return e; - } - - @Override - public void remove() { throw new UnsupportedOperationException(); } - } - - public ElementList(ASTAdaptor adaptor) { - this.adaptor = adaptor; - } - - @Override - public E get(int index) { - E o = super.get(index); - if ( used.contains(index) ) { - return adaptor.dupTree( o ); - } - used.add(index); // any subsequent ref must be dup'd - return o; - } - - @Override - public Iterator iterator() { - return new ElementListIterator(); - } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTree.java b/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTree.java index 5b8f773dc..53e7864c0 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTree.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTree.java @@ -29,7 +29,8 @@ package org.antlr.v4.runtime.tree; -import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.RuleContext; +import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.misc.Interval; /** An interface to access the tree of RuleContext objects created @@ -37,8 +38,7 @@ import org.antlr.v4.runtime.misc.Interval; * This node represents both internal nodes, rule invocations, * and leaf nodes, token matches. * - * Unlike the common AST stuff in the runtime library, there is no such thing - * as a nil node. The payload is either a token or a context object. + * The payload is either a token or a context object. */ public interface ParseTree extends SyntaxTree { public interface RuleNode extends ParseTree { @@ -89,13 +89,8 @@ public interface ParseTree extends SyntaxTree { if (symbol instanceof Token) { if ( ((Token)symbol).getType() == Token.EOF ) return ""; return ((Token)symbol).getText(); - } else if (symbol instanceof AST) { - if (((AST)symbol).getType() == Token.EOF) { - return ""; - } else { - return ((AST)symbol).getText(); - } - } else { + } + else { throw new UnsupportedOperationException("This symbol type is not supported by the default implementation."); } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/RewriteCardinalityException.java b/runtime/Java/src/org/antlr/v4/runtime/tree/RewriteCardinalityException.java deleted file mode 100644 index 45c009afd..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/RewriteCardinalityException.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -/** Base class for all exceptions thrown during AST rewrite construction. - * This signifies a case where the cardinality of two or more elements - * in a subrule are different: (ID INT)* where |ID|!=|INT| - */ -public class RewriteCardinalityException extends RuntimeException { - public String elementDescription; - - public RewriteCardinalityException(String elementDescription) { - this.elementDescription = elementDescription; - } - - @Override - public String getMessage() { - if ( elementDescription!=null ) { - return elementDescription; - } - return null; - } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/RewriteEmptyStreamException.java b/runtime/Java/src/org/antlr/v4/runtime/tree/RewriteEmptyStreamException.java deleted file mode 100644 index adb5a4825..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/RewriteEmptyStreamException.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -/** Ref to ID or expr but no tokens in ID stream or subtrees in expr stream */ -public class RewriteEmptyStreamException extends RewriteCardinalityException { - public RewriteEmptyStreamException(String elementDescription) { - super(elementDescription); - } -} 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 dd8f818c3..bf30cc35a 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/SyntaxTree.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/SyntaxTree.java @@ -33,7 +33,7 @@ import org.antlr.v4.runtime.misc.Interval; /** A tree that knows about an interval in a token stream * is some kind of syntax tree. Subinterfaces distinguish - * between parse trees and ASTs. + * between parse trees and other kinds of syntax trees we might want to create. */ public interface SyntaxTree extends Tree { /** Return an interval indicating the index in the TokenStream of diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeFilter.java b/runtime/Java/src/org/antlr/v4/runtime/tree/TreeFilter.java deleted file mode 100644 index 9f18c7d0a..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeFilter.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -import org.antlr.v4.runtime.*; - -/** - Cut-n-paste from material I'm not using in the book anymore (edit later - to make sense): - - Now, how are we going to test these tree patterns against every -subtree in our original tree? In what order should we visit nodes? -For this application, it turns out we need a simple ``apply once'' -rule application strategy and a ``down then up'' tree traversal -strategy. Let's look at rule application first. - -As we visit each node, we need to see if any of our patterns match. If -a pattern matches, we execute the associated tree rewrite and move on -to the next node. In other words, we only look for a single rule -application opportunity (we'll see below that we sometimes need to -repeatedly apply rules). The following method applies a rule in a @cl -TreeParser (derived from a tree grammar) to a tree: - -here is where weReferenced code/walking/patterns/TreePatternMatcher.java - -It uses reflection to lookup the appropriate rule within the generated -tree parser class (@cl Simplify in this case). Most of the time, the -rule will not match the tree. To avoid issuing syntax errors and -attempting error recovery, it bumps up the backtracking level. Upon -failure, the invoked rule immediately returns. If you don't plan on -using this technique in your own ANTLR-based application, don't sweat -the details. This method boils down to ``call a rule to match a tree, -executing any embedded actions and rewrite rules.'' - -At this point, we know how to define tree grammar rules and how to -apply them to a particular subtree. The final piece of the tree -pattern matcher is the actual tree traversal. We have to get the -correct node visitation order. In particular, we need to perform the -scalar-vector multiply transformation on the way down (preorder) and -we need to reduce multiply-by-zero subtrees on the way up (postorder). - -To implement a top-down visitor, we do a depth first walk of the tree, -executing an action in the preorder position. To get a bottom-up -visitor, we execute an action in the postorder position. ANTLR -provides a standard @cl TreeVisitor class with a depth first search @v -visit method. That method executes either a @m pre or @m post method -or both. In our case, we need to call @m applyOnce in both. On the way -down, we'll look for @r vmult patterns. On the way up, -we'll look for @r mult0 patterns. - */ -public class TreeFilter extends TreeParser { - public interface fptr { - public void rule() throws RecognitionException; - } - - protected TokenStream originalTokenStream; - protected ASTAdaptor originalAdaptor; - - public TreeFilter(ASTNodeStream input) { - super(input); - originalAdaptor = input.getTreeAdaptor(); - originalTokenStream = input.getTokenStream(); - } - - public void applyOnce(T t, fptr whichRule) { - if ( t==null ) return; - try { - // share TreeParser object but not parsing-related state - _input = new CommonASTNodeStream(originalAdaptor, t); - ((CommonASTNodeStream) _input).setTokenStream(originalTokenStream); - whichRule.rule(); - } - catch (RecognitionException e) { } - } - - public void downup(T t) { - TreeVisitor v = new TreeVisitor((ASTAdaptor)new CommonASTAdaptor()); - TreeVisitorAction actions = new TreeVisitorAction() { - @Override - public T pre(T t) { applyOnce(t, topdown_fptr); return t; } - @Override - public T post(T t) { applyOnce(t, bottomup_fptr); return t; } - }; - v.visit(t, actions); - } - - fptr topdown_fptr = new fptr() { - @Override - public void rule() throws RecognitionException { - topdown(); - } - }; - - fptr bottomup_fptr = new fptr() { - @Override - public void rule() throws RecognitionException { - bottomup(); - } - }; - - // methods the downup strategy uses to do the up and down rules. - // to override, just define tree grammar rule topdown and turn on - // filter=true. - public void topdown() throws RecognitionException {} - public void bottomup() throws RecognitionException {} -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeParser.java b/runtime/Java/src/org/antlr/v4/runtime/tree/TreeParser.java deleted file mode 100644 index 91e72a5e5..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeParser.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -import org.antlr.v4.runtime.*; - -import java.util.regex.*; - -/** A parser for a stream of tree nodes. "tree grammars" result in a subclass - * of this. All the error reporting and recovery is shared with Parser via - * the BaseRecognizer superclass. -*/ -public class TreeParser extends BaseRecognizer { - public static final int DOWN = Token.DOWN; - public static final int UP = Token.UP; - - public ASTAdaptor _adaptor = (ASTAdaptor)new CommonASTAdaptor(); - - // precompiled regex used by inContext - static String dotdot = ".*[^.]\\.\\.[^.].*"; - static String doubleEtc = ".*\\.\\.\\.\\s+\\.\\.\\..*"; - static Pattern dotdotPattern = Pattern.compile(dotdot); - static Pattern doubleEtcPattern = Pattern.compile(doubleEtc); - - protected ASTNodeStream _input; - - public TreeParser(ASTNodeStream input) { - super(input); - _errHandler = new DefaultTreeGrammarErrorStrategy(); - } - - @Override - public void reset() { - super.reset(); // reset all recognizer state variables - if ( _input !=null ) { - _input.seek(0); // rewind the input - } - } - - @Override - public T getCurrentInputSymbol() { return _input.LT(1); } - - @Override - public ASTNodeStream getInputStream() { return _input; } - - @Override - public void setInputStream(IntStream input) { _input = (ASTNodeStream)input; } - - /** Always called by generated parsers upon entry to a rule. - * This occurs after the new context has been pushed. Access field - * _ctx get the current context. - * - * This is flexible because users do not have to regenerate parsers - * to get trace facilities. - * - * TODO: this shouldn't be needed now that ParserRuleContext is generic. - */ - @Override - public void enterRule(ParserRuleContext localctx, int ruleIndex) { - _ctx = localctx; - _ctx.start = _input.LT(1); - _ctx.ruleIndex = ruleIndex; - } - - @Override - public String getSourceName() { - return _input.getSourceName(); - } - -// protected T getCurrentInputSymbol(IntStream input) { -// return ((ASTNodeStream)input).LT(1); -// } - - protected T getMissingSymbol(IntStream input, - RecognitionException e, - int expectedTokenType) - { - String tokenText = - ""; - ASTAdaptor adaptor = ((ASTNodeStream)e.getInputStream()).getTreeAdaptor(); - return adaptor.create(new CommonToken(expectedTokenType, tokenText)); - } - - /** Match '.' in tree parser has special meaning. Skip node or - * entire tree if node has children. If children, scan until - * corresponding UP node. - */ - public void matchAny(IntStream ignore) { // ignore stream, copy of input - _errHandler.endErrorCondition(this); - T look = _input.LT(1); - if ( _input.getTreeAdaptor().getChildCount(look)==0 ) { - _input.consume(); // not subtree, consume 1 node and return - return; - } - // current node is a subtree, skip to corresponding UP. - // must count nesting level to get right UP - int level=0; - int tokenType = _input.getTreeAdaptor().getType(look); - while ( tokenType!=Token.EOF && !(tokenType==UP && level==0) ) { - _input.consume(); - look = _input.LT(1); - tokenType = _input.getTreeAdaptor().getType(look); - if ( tokenType == DOWN ) { - level++; - } - else if ( tokenType == UP ) { - level--; - } - } - _input.consume(); // consume UP - } - - /** We have DOWN/UP nodes in the stream that have no line info; override. - * plus we want to alter the exception type. Don't try to recover - * from tree parser errors inline... - */ - protected T recoverFromMismatchedToken(IntStream input, - int ttype) - throws RecognitionException - { - //throw new MismatchedTreeNodeException(ttype, (TreeNodeStream)input); - return null; - } - - /** Prefix error message with the grammar name because message is - * always intended for the programmer because the parser built - * the input tree not the user. - public String getErrorHeader(RecognitionException e) { - // todo: might not have token; use node? - int line = e.offendingToken.getLine(); - int charPositionInLine = e.offendingToken.getCharPositionInLine(); - return getGrammarFileName()+": node from "+ - (e.approximateLineInfo?"after ":"")+"line "+line+":"+charPositionInLine; - } - */ - - /** Tree parsers parse nodes they usually have a token object as - * payload. Set the exception token and do the default behavior. - public String getErrorMessage(RecognitionException e, String[] tokenNames) { - if ( this instanceof TreeParser ) { - ASTAdaptor adaptor = ((ASTNodeStream)e.input).getTreeAdaptor(); - e.offendingToken = adaptor.getToken(e.offendingNode); - if ( e.offendingToken ==null ) { // could be an UP/DOWN node - e.offendingToken = new CommonToken(adaptor.getType(e.offendingNode), - adaptor.getText(e.offendingNode)); - } - } - return super.getErrorMessage(e); - } - */ - - /** Check if current node in input has a context. Context means sequence - * of nodes towards root of tree. For example, you might say context - * is "MULT" which means my parent must be MULT. "CLASS VARDEF" says - * current node must be child of a VARDEF and whose parent is a CLASS node. - * You can use "..." to mean zero-or-more nodes. "METHOD ... VARDEF" - * means my parent is VARDEF and somewhere above that is a METHOD node. - * The first node in the context is not necessarily the root. The context - * matcher stops matching and returns true when it runs out of context. - * There is no way to force the first node to be the root. - */ - @Override - public boolean inContext(String context) { - return inContext(_input.getTreeAdaptor(), getTokenNames(), _input.LT(1), context); - } - - /** The worker for inContext. It's static and full of parameters for - * testing purposes. - */ - public static boolean inContext(ASTAdaptor adaptor, - String[] tokenNames, - T t, - String context) - { - Matcher dotdotMatcher = dotdotPattern.matcher(context); - Matcher doubleEtcMatcher = doubleEtcPattern.matcher(context); - if ( dotdotMatcher.find() ) { // don't allow "..", must be "..." - throw new IllegalArgumentException("invalid syntax: .."); - } - if ( doubleEtcMatcher.find() ) { // don't allow double "..." - throw new IllegalArgumentException("invalid syntax: ... ..."); - } - context = context.replaceAll("\\.\\.\\.", " ... "); // ensure spaces around ... - context = context.trim(); - String[] nodes = context.split("\\s+"); - int ni = nodes.length-1; - t = adaptor.getParent(t); - while ( ni>=0 && t!=null ) { - if ( nodes[ni].equals("...") ) { - // walk upwards until we see nodes[ni-1] then continue walking - if ( ni==0 ) return true; // ... at start is no-op - String goal = nodes[ni-1]; - T ancestor = getAncestor(adaptor, tokenNames, t, goal); - if ( ancestor==null ) return false; - t = ancestor; - ni--; - } - String name = tokenNames[adaptor.getType(t)]; - if ( !name.equals(nodes[ni]) ) { - //System.err.println("not matched: "+nodes[ni]+" at "+t); - return false; - } - // advance to parent and to previous element in context node list - ni--; - t = adaptor.getParent(t); - } - - if ( t==null && ni>=0 ) return false; // at root but more nodes to match - return true; - } - - /** Helper for static inContext */ - protected static T getAncestor(ASTAdaptor adaptor, String[] tokenNames, T t, String goal) { - while ( t!=null ) { - String name = tokenNames[adaptor.getType(t)]; - if ( name.equals(goal) ) return t; - t = adaptor.getParent(t); - } - return null; - } - -// public void traceIn(String ruleName, int ruleIndex) { -// super.traceIn(ruleName, ruleIndex, input.LT(1)); -// } -// -// public void traceOut(String ruleName, int ruleIndex) { -// super.traceOut(ruleName, ruleIndex, input.LT(1)); -// } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/TreePatternLexer.java b/runtime/Java/src/org/antlr/v4/runtime/tree/TreePatternLexer.java deleted file mode 100644 index 2e0ee4a81..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/TreePatternLexer.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -public class TreePatternLexer { - public static final int EOF = -1; - public static final int BEGIN = 1; - public static final int END = 2; - public static final int ID = 3; - public static final int ARG = 4; - public static final int PERCENT = 5; - public static final int COLON = 6; - public static final int DOT = 7; - - /** The tree pattern to lex like "(A B C)" */ - protected String pattern; - - /** Index into input string */ - protected int p = -1; - - /** Current char */ - protected int c; - - /** How long is the pattern in char? */ - protected int n; - - /** Set when token type is ID or ARG (name mimics Java's StreamTokenizer) */ - public StringBuffer sval = new StringBuffer(); - - public boolean error = false; - - public TreePatternLexer(String pattern) { - this.pattern = pattern; - this.n = pattern.length(); - consume(); - } - - public int nextToken() { - sval.setLength(0); // reset, but reuse buffer - while ( c != EOF ) { - if ( c==' ' || c=='\n' || c=='\r' || c=='\t' ) { - consume(); - continue; - } - if ( (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_' ) { - sval.append((char)c); - consume(); - while ( (c>='a' && c<='z') || (c>='A' && c<='Z') || - (c>='0' && c<='9') || c=='_' ) - { - sval.append((char)c); - consume(); - } - return ID; - } - if ( c=='(' ) { - consume(); - return BEGIN; - } - if ( c==')' ) { - consume(); - return END; - } - if ( c=='%' ) { - consume(); - return PERCENT; - } - if ( c==':' ) { - consume(); - return COLON; - } - if ( c=='.' ) { - consume(); - return DOT; - } - if ( c=='[' ) { // grab [x] as a string, returning x - consume(); - while ( c!=']' ) { - if ( c=='\\' ) { - consume(); - if ( c!=']' ) { - sval.append('\\'); - } - sval.append((char)c); - } - else { - sval.append((char)c); - } - consume(); - } - consume(); - return ARG; - } - consume(); - error = true; - return EOF; - } - return EOF; - } - - protected void consume() { - p++; - if ( p>=n ) { - c = EOF; - } - else { - c = pattern.charAt(p); - } - } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/TreePatternParser.java b/runtime/Java/src/org/antlr/v4/runtime/tree/TreePatternParser.java deleted file mode 100644 index 6b73ecc32..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/TreePatternParser.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - - -import org.antlr.v4.runtime.*; - -public class TreePatternParser { - protected TreePatternLexer tokenizer; - protected int ttype; - protected TreeWizard wizard; - // TODO: would be nice to use ASTAdaptor... - protected ASTAdaptor adaptor; - - public TreePatternParser(TreePatternLexer tokenizer, TreeWizard wizard, ASTAdaptor adaptor) { - this.tokenizer = tokenizer; - this.wizard = wizard; - this.adaptor = adaptor; - ttype = tokenizer.nextToken(); // kickstart - } - - public TreeWizard.TreePattern pattern() { - if ( ttype==TreePatternLexer.BEGIN ) { - return parseTree(); - } - else if ( ttype==TreePatternLexer.ID ) { - TreeWizard.TreePattern node = parseNode(); - if ( ttype==TreePatternLexer.EOF ) { - return node; - } - return null; // extra junk on end - } - return null; - } - - public TreeWizard.TreePattern parseTree() { - if ( ttype != TreePatternLexer.BEGIN ) { - throw new RuntimeException("no BEGIN"); - } - ttype = tokenizer.nextToken(); - TreeWizard.TreePattern root = parseNode(); - if ( root==null ) { - return null; - } - while ( ttype==TreePatternLexer.BEGIN || - ttype==TreePatternLexer.ID || - ttype==TreePatternLexer.PERCENT || - ttype==TreePatternLexer.DOT ) - { - if ( ttype==TreePatternLexer.BEGIN ) { - TreeWizard.TreePattern subtree = parseTree(); - adaptor.addChild(root, subtree); - } - else { - TreeWizard.TreePattern child = parseNode(); - if ( child==null ) { - return null; - } - adaptor.addChild(root, child); - } - } - if ( ttype != TreePatternLexer.END ) { - throw new RuntimeException("no END"); - } - ttype = tokenizer.nextToken(); - return root; - } - - public TreeWizard.TreePattern parseNode() { - // "%label:" prefix - String label = null; - if ( ttype == TreePatternLexer.PERCENT ) { - ttype = tokenizer.nextToken(); - if ( ttype != TreePatternLexer.ID ) { - return null; - } - label = tokenizer.sval.toString(); - ttype = tokenizer.nextToken(); - if ( ttype != TreePatternLexer.COLON ) { - return null; - } - ttype = tokenizer.nextToken(); // move to ID following colon - } - - // Wildcard? - if ( ttype == TreePatternLexer.DOT ) { - ttype = tokenizer.nextToken(); - Token wildcardPayload = new CommonToken(0, "."); - TreeWizard.TreePattern node = - new TreeWizard.WildcardTreePattern(wildcardPayload); - if ( label!=null ) { - node.label = label; - } - return node; - } - - // "ID" or "ID[arg]" - if ( ttype != TreePatternLexer.ID ) { - return null; - } - String tokenName = tokenizer.sval.toString(); - ttype = tokenizer.nextToken(); - if ( tokenName.equals("nil") ) { - return (TreeWizard.TreePattern)adaptor.nil(); - } - String text = tokenName; - // check for arg - String arg = null; - if ( ttype == TreePatternLexer.ARG ) { - arg = tokenizer.sval.toString(); - text = arg; - ttype = tokenizer.nextToken(); - } - - // create node - int treeNodeType = wizard.getTokenType(tokenName); - if ( treeNodeType== Token.INVALID_TYPE ) { - return null; - } - TreeWizard.TreePattern node; - node = (TreeWizard.TreePattern)adaptor.create(treeNodeType, text); - if ( label!=null && node.getClass()==TreeWizard.TreePattern.class ) { - node.label = label; - } - if ( arg!=null && node.getClass()==TreeWizard.TreePattern.class ) { - node.hasTextArg = true; - } - return node; - } -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeVisitor.java b/runtime/Java/src/org/antlr/v4/runtime/tree/TreeVisitor.java deleted file mode 100644 index 921e99d61..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeVisitor.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - -/** Do a depth first walk of a tree, applying pre() and post() actions - * as we discover and finish nodes. - */ -public class TreeVisitor { - protected ASTAdaptor adaptor; - - public TreeVisitor(ASTAdaptor adaptor) { - this.adaptor = adaptor; - } - - public TreeVisitor() { this((ASTAdaptor)new CommonASTAdaptor()); } - - /** Visit every node in tree t and trigger an action for each node - * before/after having visited all of its children. - * Execute both actions even if t has no children. - * If a child visit yields a new child, it can update its - * parent's child list or just return the new child. The - * child update code works even if the child visit alters its parent - * and returns the new tree. - * - * Return result of applying post action to this node. - */ - public T visit(T t, TreeVisitorAction action) { - // System.out.println("visit "+((Tree)t).toStringTree()); - boolean isNil = adaptor.isNil(t); - if ( action!=null && !isNil ) { - t = action.pre(t); // if rewritten, walk children of new t - } - for (int i=0; i { - /** Execute an action before visiting children of t. Return t or - * a rewritten t. It is up to the visitor to decide what to do - * with the return value. Children of returned value will be - * visited if using TreeVisitor.visit(). - */ - public T pre(T t); - - /** Execute an action after visiting children of t. Return t or - * a rewritten t. It is up to the visitor to decide what to do - * with the return value. - */ - public T post(T t); -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeWizard.java b/runtime/Java/src/org/antlr/v4/runtime/tree/TreeWizard.java deleted file mode 100644 index 34ab751f9..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeWizard.java +++ /dev/null @@ -1,535 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.tree; - - -import org.antlr.v4.runtime.misc.Nullable; -import org.antlr.v4.runtime.Token; - -import java.util.*; - -/** Build and navigate trees with this object. Must know about the names - * of tokens so you have to pass in a map or array of token names (from which - * this class can build the map). I.e., Token DECL means nothing unless the - * class can translate it to a token type. - * - * In order to create nodes and navigate, this class needs a ASTAdaptor. - * - * This class can build a token type -> node index for repeated use or for - * iterating over the various nodes with a particular type. - * - * This class works in conjunction with the ASTAdaptor rather than moving - * all this functionality into the adaptor. An adaptor helps build and - * navigate trees using methods. This class helps you do it with string - * patterns like "(A B C)". You can create a tree from that pattern or - * match subtrees against it. - */ -public class TreeWizard { - protected ASTAdaptor adaptor; - protected Map tokenNameToTypeMap; - - public interface ContextVisitor { - // TODO: should this be called visit or something else? - public void visit(T t, Object parent, int childIndex, @Nullable Map labels); - } - - public static abstract class Visitor implements ContextVisitor { - @Override - public void visit(T t, Object parent, int childIndex, Map labels) { - visit(t); - } - public abstract void visit(T t); - } - - /** When using %label:TOKENNAME in a tree for parse(), we must - * track the label. - */ - public static class TreePattern extends CommonAST { - public String label; - public boolean hasTextArg; - public TreePattern(Token payload) { - super(payload); - } - public String toString() { - if ( label!=null ) { - return "%"+label+":"+super.toString(); - } - else { - return super.toString(); - } - } - } - - public static class WildcardTreePattern extends TreePattern { - public WildcardTreePattern(Token payload) { - super(payload); - } - } - - /** This adaptor creates TreePattern objects for use during scan() */ - public static class TreePatternASTAdaptor extends CommonASTAdaptor { - public CommonAST create(Token payload) { - return new TreePattern(payload); - } - } - - // TODO: build indexes for the wizard - - /** During fillBuffer(), we can make a reverse index from a set - * of token types of interest to the list of indexes into the - * node stream. This lets us convert a node pointer to a - * stream index semi-efficiently for a list of interesting - * nodes such as function definition nodes (you'll want to seek - * to their bodies for an interpreter). Also useful for doing - * dynamic searches; i.e., go find me all PLUS nodes. - protected Map tokenTypeToStreamIndexesMap; - - /** If tokenTypesToReverseIndex set to INDEX_ALL then indexing - * occurs for all token types. - public static final Set INDEX_ALL = new HashSet(); - - /** A set of token types user would like to index for faster lookup. - * If this is INDEX_ALL, then all token types are tracked. If null, - * then none are indexed. - protected Set tokenTypesToReverseIndex = null; - */ - - public TreeWizard(ASTAdaptor adaptor) { - this.adaptor = adaptor; - } - - public TreeWizard(ASTAdaptor adaptor, Map tokenNameToTypeMap) { - this.adaptor = adaptor; - this.tokenNameToTypeMap = tokenNameToTypeMap; - } - - public TreeWizard(ASTAdaptor adaptor, String[] tokenNames) { - this.adaptor = adaptor; - this.tokenNameToTypeMap = computeTokenTypes(tokenNames); - } - - public TreeWizard(String[] tokenNames) { - this((ASTAdaptor)new TreePatternASTAdaptor(), tokenNames); - } - - /** Compute a Map that is an inverted index of - * tokenNames (which maps int token types to names). - */ - public Map computeTokenTypes(String[] tokenNames) { - Map m = new HashMap(); - if ( tokenNames==null ) { - return m; - } - for (int ttype = Token.MIN_TOKEN_TYPE; ttype < tokenNames.length; ttype++) { - String name = tokenNames[ttype]; - m.put(name, ttype); - } - return m; - } - - /** Using the map of token names to token types, return the type. */ - public int getTokenType(String tokenName) { - if ( tokenNameToTypeMap==null ) { - return Token.INVALID_TYPE; - } - Integer ttypeI = tokenNameToTypeMap.get(tokenName); - if ( ttypeI!=null ) { - return ttypeI; - } - return Token.INVALID_TYPE; - } - - /** Walk the entire tree and make a node name to nodes mapping. - * For now, use recursion but later nonrecursive version may be - * more efficient. Returns Map where the List is - * of your AST node type. The Integer is the token type of the node. - * - * TODO: save this index so that find and visit are faster - */ - public Map> index(T t) { - Map> m = new HashMap>(); - _index(t, m); - return m; - } - - /** Do the work for index */ - protected void _index(T t, Map> m) { - if ( t==null ) { - return; - } - int ttype = adaptor.getType(t); - List elements = m.get(ttype); - if ( elements==null ) { - elements = new ArrayList(); - m.put(ttype, elements); - } - elements.add(t); - int n = adaptor.getChildCount(t); - for (int i=0; i find(T t, int ttype) { - final List nodes = new ArrayList(); - visit(t, ttype, new TreeWizard.Visitor() { - @Override - public void visit(T t) { - nodes.add(t); - } - }); - return nodes; - } - - /** Return a List of subtrees matching pattern. */ - public List find(T t, String pattern) { - final List subtrees = new ArrayList(); - // Create a TreePattern from the pattern - TreePatternLexer tokenizer = new TreePatternLexer(pattern); - TreePatternParser parser = - new TreePatternParser(tokenizer, this, new TreePatternASTAdaptor()); - final TreePattern tpattern = parser.pattern(); - // don't allow invalid patterns - if ( tpattern==null || - tpattern.isNil() || - tpattern.getClass()==WildcardTreePattern.class ) - { - return null; - } - int rootTokenType = tpattern.getType(); - visit(t, rootTokenType, new TreeWizard.ContextVisitor() { - @Override - public void visit(T t, Object parent, int childIndex, Map labels) { - if ( _parse(t, tpattern, null) ) { - subtrees.add(t); - } - } - }); - return subtrees; - } - - public T findFirst(T t, int ttype) { - return null; - } - - public T findFirst(T t, String pattern) { - return null; - } - - /** Visit every ttype node in t, invoking the visitor. This is a quicker - * version of the general visit(t, pattern) method. The labels arg - * of the visitor action method is never set (it's null) since using - * a token type rather than a pattern doesn't let us set a label. - */ - public void visit(T t, int ttype, ContextVisitor visitor) { - _visit(t, null, 0, ttype, visitor); - } - - /** Do the recursive work for visit */ - protected void _visit(T t, @Nullable Object parent, int childIndex, int ttype, ContextVisitor visitor) { - if ( t==null ) { - return; - } - if ( adaptor.getType(t)==ttype ) { - visitor.visit(t, parent, childIndex, null); - } - int n = adaptor.getChildCount(t); - for (int i=0; i visitor) { - // Create a TreePattern from the pattern - TreePatternLexer tokenizer = new TreePatternLexer(pattern); - TreePatternParser parser = - new TreePatternParser(tokenizer, this, new TreePatternASTAdaptor()); - final TreePattern tpattern = parser.pattern(); - // don't allow invalid patterns - if ( tpattern==null || - tpattern.isNil() || - tpattern.getClass()==WildcardTreePattern.class ) - { - return; - } - final Map labels = new HashMap(); // reused for each _parse - int rootTokenType = tpattern.getType(); - visit(t, rootTokenType, new TreeWizard.ContextVisitor() { - @Override - public void visit(T t, Object parent, int childIndex, Map unusedlabels) { - // the unusedlabels arg is null as visit on token type doesn't set. - labels.clear(); - if ( _parse(t, tpattern, labels) ) { - visitor.visit(t, parent, childIndex, labels); - } - } - }); - } - - /** Given a pattern like (ASSIGN %lhs:ID %rhs:.) with optional labels - * on the various nodes and '.' (dot) as the node/subtree wildcard, - * return true if the pattern matches and fill the labels Map with - * the labels pointing at the appropriate nodes. Return false if - * the pattern is malformed or the tree does not match. - * - * If a node specifies a text arg in pattern, then that must match - * for that node in t. - * - * TODO: what's a better way to indicate bad pattern? Exceptions are a hassle - */ - public boolean parse(T t, String pattern, @Nullable Map labels) { - TreePatternLexer tokenizer = new TreePatternLexer(pattern); - TreePatternParser parser = - new TreePatternParser(tokenizer, this, new TreePatternASTAdaptor()); - TreePattern tpattern = parser.pattern(); - /* - System.out.println("t="+((Tree)t).toStringTree()); - System.out.println("scant="+tpattern.toStringTree()); - */ - boolean matched = _parse(t, tpattern, labels); - return matched; - } - - public boolean parse(T t, String pattern) { - return parse(t, pattern, null); - } - - /** Do the work for parse. Check to see if the t2 pattern fits the - * structure and token types in t1. Check text if the pattern has - * text arguments on nodes. Fill labels map with pointers to nodes - * in tree matched against nodes in pattern with labels. - */ - protected boolean _parse(T t1, TreePattern tpattern, @Nullable Map labels) { - // make sure both are non-null - if ( t1==null || tpattern==null ) { - return false; - } - // check roots (wildcard matches anything) - if ( tpattern.getClass() != WildcardTreePattern.class ) { - if ( adaptor.getType(t1) != tpattern.getType() ) return false; - // if pattern has text, check node text - if ( tpattern.hasTextArg && !adaptor.getText(t1).equals(tpattern.getText()) ) { - return false; - } - } - if ( tpattern.label!=null && labels!=null ) { - // map label in pattern to node in t1 - labels.put(tpattern.label, t1); - } - // check children - int n1 = adaptor.getChildCount(t1); - int n2 = tpattern.getChildCount(); - if ( n1 != n2 ) { - return false; - } - for (int i=0; i boolean equals(T t1, T t2, ASTAdaptor adaptor) { - return _equals(t1, t2, adaptor); - } - - /** Compare type, structure, and text of two trees, assuming adaptor in - * this instance of a TreeWizard. - */ - public boolean equals(T t1, T t2) { - return _equals(t1, t2, adaptor); - } - - protected static boolean _equals(T t1, T t2, ASTAdaptor adaptor) { - // make sure both are non-null - if ( t1==null || t2==null ) { - return false; - } - // check roots - if ( adaptor.getType(t1) != adaptor.getType(t2) ) { - return false; - } - if ( !adaptor.getText(t1).equals(adaptor.getText(t2)) ) { - return false; - } - // check children - int n1 = adaptor.getChildCount(t1); - int n2 = adaptor.getChildCount(t2); - if ( n1 != n2 ) { - return false; - } - for (int i=0; i> - * - * This data structure allows you to find all nodes with type INT in order. - * - * If you really need to find a node of type, say, FUNC quickly then perhaps - * - * Map> - * - * would be better for you. The interior maps map a tree node to - * the index so you don't have to search linearly for a specific node. - * - * If you change this method, you will likely need to change - * getNodeIndex(), which extracts information. - protected void fillReverseIndex(Object node, int streamIndex) { - //System.out.println("revIndex "+node+"@"+streamIndex); - if ( tokenTypesToReverseIndex==null ) { - return; // no indexing if this is empty (nothing of interest) - } - if ( tokenTypeToStreamIndexesMap==null ) { - tokenTypeToStreamIndexesMap = new HashMap(); // first indexing op - } - int tokenType = adaptor.getType(node); - Integer tokenTypeI = new Integer(tokenType); - if ( !(tokenTypesToReverseIndex==INDEX_ALL || - tokenTypesToReverseIndex.contains(tokenTypeI)) ) - { - return; // tokenType not of interest - } - Integer streamIndexI = new Integer(streamIndex); - ArrayList indexes = (ArrayList)tokenTypeToStreamIndexesMap.get(tokenTypeI); - if ( indexes==null ) { - indexes = new ArrayList(); // no list yet for this token type - indexes.add(streamIndexI); // not there yet, add - tokenTypeToStreamIndexesMap.put(tokenTypeI, indexes); - } - else { - if ( !indexes.contains(streamIndexI) ) { - indexes.add(streamIndexI); // not there yet, add - } - } - } - - /** Track the indicated token type in the reverse index. Call this - * repeatedly for each type or use variant with Set argument to - * set all at once. - * @param tokenType - public void reverseIndex(int tokenType) { - if ( tokenTypesToReverseIndex==null ) { - tokenTypesToReverseIndex = new HashSet(); - } - else if ( tokenTypesToReverseIndex==INDEX_ALL ) { - return; - } - tokenTypesToReverseIndex.add(new Integer(tokenType)); - } - - /** Track the indicated token types in the reverse index. Set - * to INDEX_ALL to track all token types. - public void reverseIndex(Set tokenTypes) { - tokenTypesToReverseIndex = tokenTypes; - } - - /** Given a node pointer, return its index into the node stream. - * This is not its Token stream index. If there is no reverse map - * from node to stream index or the map does not contain entries - * for node's token type, a linear search of entire stream is used. - * - * Return -1 if exact node pointer not in stream. - public int getNodeIndex(Object node) { - //System.out.println("get "+node); - if ( tokenTypeToStreamIndexesMap==null ) { - return getNodeIndexLinearly(node); - } - int tokenType = adaptor.getType(node); - Integer tokenTypeI = new Integer(tokenType); - ArrayList indexes = (ArrayList)tokenTypeToStreamIndexesMap.get(tokenTypeI); - if ( indexes==null ) { - //System.out.println("found linearly; stream index = "+getNodeIndexLinearly(node)); - return getNodeIndexLinearly(node); - } - for (int i = 0; i < indexes.size(); i++) { - Integer streamIndexI = (Integer)indexes.get(i); - Object n = get(streamIndexI.intValue()); - if ( n==node ) { - //System.out.println("found in index; stream index = "+streamIndexI); - return streamIndexI.intValue(); // found it! - } - } - return -1; - } - - */ -} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java b/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java index f14b3a077..425875174 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java @@ -29,17 +29,22 @@ package org.antlr.v4.runtime.tree; -import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.BaseRecognizer; +import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.tree.gui.TreePostScriptGenerator; -import java.io.*; -import java.util.*; +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; /** A set of utility routines useful for all kinds of ANTLR trees */ public class Trees { - public static String getPS(Tree t, BaseRecognizer recog, + public static String getPS(Tree t, BaseRecognizer recog, String fontName, int fontSize) { TreePostScriptGenerator psgen = @@ -47,11 +52,11 @@ public class Trees { return psgen.getPS(); } - public static String getPS(Tree t, BaseRecognizer recog) { + public static String getPS(Tree t, BaseRecognizer recog) { return getPS(t, recog, "Helvetica", 11); } - public static void writePS(Tree t, BaseRecognizer recog, + public static void writePS(Tree t, BaseRecognizer recog, String fileName, String fontName, int fontSize) throws IOException @@ -63,7 +68,7 @@ public class Trees { bw.close(); } - public static void writePS(Tree t, BaseRecognizer recog, String fileName) + public static void writePS(Tree t, BaseRecognizer recog, String fileName) throws IOException { writePS(t, recog, fileName, "Helvetica", 11); @@ -73,29 +78,21 @@ public class Trees { * node payloads to get the text for the nodes. Detect * parse trees and extract data appropriately. */ - public static String toStringTree(Tree t, BaseRecognizer recog) { + public static String toStringTree(Tree t, BaseRecognizer recog) { if ( t.getChildCount()==0 ) return getNodeText(t, recog); StringBuilder buf = new StringBuilder(); - boolean nilRoot = t instanceof AST && ((AST)t).isNil(); - if ( !nilRoot ) { - buf.append("("); - buf.append(getNodeText(t, recog)); - buf.append(' '); - } + buf.append("("); + buf.append(getNodeText(t, recog)); + buf.append(' '); for (int i = 0; i0 ) buf.append(' '); buf.append(toStringTree(t.getChild(i), recog)); } - if ( !nilRoot ) { - buf.append(")"); - } + buf.append(")"); return buf.toString(); } - public static String getNodeText(Tree t, BaseRecognizer recog) { - if ( t instanceof AST ) { - return t.toString(); - } + public static String getNodeText(Tree t, BaseRecognizer recog) { if ( recog!=null ) { if ( t instanceof ParseTree.RuleNode ) { int ruleIndex = ((ParseTree.RuleNode)t).getRuleContext().getRuleIndex(); @@ -112,7 +109,7 @@ public class Trees { } } } - // not AST and no recog for rule names + // no recog for rule names Object payload = t.getPayload(); if ( payload instanceof Token ) { return ((Token)payload).getText(); @@ -120,16 +117,6 @@ public class Trees { return t.getPayload().toString(); } - /** Walk upwards and get first ancestor with this token type. */ - public static AST getAncestor(AST t, int ttype) { - t = t.getParent(); - while ( t!=null ) { - if ( t.getType()==ttype ) return t; - t = t.getParent(); - } - return null; - } - /** Return a list of all ancestors of this node. The first node of * list is the root and the last is the parent of this node. */ @@ -145,162 +132,4 @@ public class Trees { return ancestors; } - public static AST getFirstChildWithType(AST t, int type) { - for (int i = 0; i newChildren; - // normalize to a list of children to add: newChildren - if ( newTree.isNil() ) { - newChildren = newTree.children; - } - else { - newChildren = new ArrayList(1); - newChildren.add(newTree); - } - replacingWithHowMany = newChildren.size(); - int numNewChildren = newChildren.size(); - int delta = replacingHowMany - replacingWithHowMany; - // if same number of nodes, do direct replace - if ( delta == 0 ) { - int j = 0; // index into new children - for (int i=startChildIndex; i<=stopChildIndex; i++) { - BaseAST child = newChildren.get(j); - tree.setChild(i, child); - child.setParent(tree); - child.setChildIndex(i); - j++; - } - } - else if ( delta > 0 ) { // fewer new nodes than there were - // set children and then delete extra - for (int j=0; j { @@ -75,11 +71,11 @@ public class TreePostScriptGenerator { protected PostScriptDocument doc; - public TreePostScriptGenerator(BaseRecognizer parser, Tree root) { + public TreePostScriptGenerator(BaseRecognizer parser, Tree root) { this(parser, root, "CourierNew", 11); } - public TreePostScriptGenerator(BaseRecognizer parser, Tree root, + public TreePostScriptGenerator(BaseRecognizer parser, Tree root, String fontName, int fontSize) { this.root = root; @@ -167,36 +163,4 @@ public class TreePostScriptGenerator { this.treeTextProvider = treeTextProvider; } - public static void main(String[] args) throws IOException { - CommonAST t = new CommonAST(new CommonToken(1, "s")); - CommonAST ifstat = new CommonAST(new CommonToken(1, "ifstat")); - CommonAST iff = new CommonAST(new CommonToken(1, "if")); - CommonAST b = new CommonAST(new CommonToken(1, "(")); - CommonAST c = new CommonAST(new CommonToken(1, "expr")); - CommonAST d = new CommonAST(new CommonToken(1, ")")); - CommonAST e = new CommonAST(new CommonToken(1, "assign")); - CommonAST f = new CommonAST(new CommonToken(1, "34")); - CommonAST g = new CommonAST(new CommonToken(1, "a")); - CommonAST h = new CommonAST(new CommonToken(1, "=")); - CommonAST i = new CommonAST(new CommonToken(1, "expr")); - CommonAST j = new CommonAST(new CommonToken(1, ";")); - CommonAST k = new CommonAST(new CommonToken(1, "b")); - t.addChild(ifstat); - ifstat.addChild(iff); - ifstat.addChild(b); - ifstat.addChild(c); - ifstat.addChild(d); - ifstat.addChild(e); - c.addChild(f); - e.addChild(g); - e.addChild(h); - e.addChild(i); - e.addChild(j); - i.addChild(k); - Trees.writePS(t, null, - "/Users/parrt/antlr/code/antlr4/main/tool/playground/t.eps", - "ArialNarrow", 11); -// TreePostScriptGenerator psgen = new TreePostScriptGenerator(null, t, "CourierNew", 11); -// System.out.println(psgen.getPS()); - } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java index f3866e4fe..c5a1d7f12 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java @@ -53,9 +53,9 @@ public class TreeViewer extends JComponent { public static final Color LIGHT_RED = new Color(244, 213, 211); public static class DefaultTreeTextProvider implements TreeTextProvider { - BaseRecognizer parser; + BaseRecognizer parser; - public DefaultTreeTextProvider(BaseRecognizer parser) { + public DefaultTreeTextProvider(BaseRecognizer parser) { this.parser = parser; } @@ -111,9 +111,9 @@ public class TreeViewer extends JComponent { protected Color borderColor = null; protected Color textColor = Color.black; - protected BaseRecognizer parser; + protected BaseRecognizer parser; - public TreeViewer(BaseRecognizer parser, Tree tree) { + public TreeViewer(BaseRecognizer parser, Tree tree) { this.parser = parser; setTreeTextProvider(new DefaultTreeTextProvider(parser)); boolean useIdentity = true; // compare node identity @@ -139,7 +139,7 @@ public class TreeViewer extends JComponent { // ---------------- PAINT ----------------------------------------------- private boolean useCurvedEdges = false; - + public boolean getUseCurvedEdges() { return useCurvedEdges; } diff --git a/tool/MIGRATION.txt b/tool/MIGRATION.txt index 706aad38e..c292b8ee7 100644 --- a/tool/MIGRATION.txt +++ b/tool/MIGRATION.txt @@ -13,3 +13,5 @@ Trees Lexers * Added [Abc] notation + +* unicode rule/token names \ No newline at end of file diff --git a/tool/playground/JavaParser.g b/tool/playground/JavaParser.g index fdd6ccdb6..995959f98 100644 --- a/tool/playground/JavaParser.g +++ b/tool/playground/JavaParser.g @@ -1,4 +1,4 @@ -ûparser grammar JavaParser; +parser grammar JavaParser; options {backtrack=true; memoize=true; tokenVocab=JavaLexer;} // starting point for parsing a java file diff --git a/tool/playground/TestR.java b/tool/playground/TestR.java index fc4c0b98f..ff028942c 100644 --- a/tool/playground/TestR.java +++ b/tool/playground/TestR.java @@ -30,7 +30,6 @@ import org.antlr.v4.runtime.ANTLRFileStream; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.DiagnosticErrorStrategy; -import org.antlr.v4.runtime.Token; public class TestR { public static void main(String[] args) throws Exception { @@ -42,7 +41,7 @@ public class TestR { // } RParser p = new RParser(tokens); p.setBuildParseTree(true); - p.setErrorHandler(new DiagnosticErrorStrategy()); + p.setErrorHandler(new DiagnosticErrorStrategy()); p.prog(); } } 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 51fd19e22..4bd04bfa1 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 @@ -27,10 +27,6 @@ import java.util.ArrayList; >> -TreeParserFile(file, parser, namedActions) ::= << - ->> - ListenerFile(file, header) ::= <<
import org.antlr.v4.runtime.tree.*; @@ -64,16 +60,6 @@ Parser(parser, funcs, atn, sempredFuncs, superclass) ::= << >> -TreeParserModel(parser, funcs, atn, sempredFuncs, superclass) ::= << - match(int ttype) throws RecognitionException { - return ()super.match(ttype); -\} -}, ...)> ->> - Parser_(parser, funcs, atn, sempredFuncs, ctor, extras, superclass) ::= << @SuppressWarnings({"all", "warnings", "unchecked", "unused"}) public class extends { @@ -148,13 +134,6 @@ public (TokenStream input) { } >> -treeparser_ctor(p) ::= << -public (ASTNodeStream\<\> input) { - super(input); - _interp = new v2ParserATNSimulator\<\>(this,_ATN); -} ->> - RuleActionFunction(r, actions) ::= << public void _action( _localctx, int actionIndex) { switch ( actionIndex ) { @@ -379,14 +358,6 @@ case : Sync(s) ::= "sync();" ThrowNoViableAlt(t) ::= "throw new NoViableAltException(this);" -ThrowNoViableTreeAlt(t) ::= << -/* -NoViableTreeGrammarAltException e = new NoViableTreeGrammarAltException(this); -_errHandler.reportError(this, e); -throw e; -*/ -throw new NoViableTreeGrammarAltException(this); ->> TestSetInline(s) ::= << ==}; separator=" || "> @@ -396,32 +367,6 @@ cases(ttypes) ::= << :}; separator="\n"> >> -MatchTree(t, root, down, leftActions, kids, rightActions, up) ::= << -// match tree - - - -if ( _input.LA(1)==Token.DOWN ) { - - - -} - - - - - - ->> - -MatchDOWN(m) ::= << -setState(); match(Token.DOWN); ->> - -MatchUP(m) ::= << -setState(); match(Token.UP); ->> - InvokeRule(r, argExprsChunks) ::= << setState(); = }>(); >> @@ -462,18 +407,7 @@ setState(); if (!()) throw new FailedPredicateException(this, ); >> -ParserASTExtensionMembers(s) ::= << -public ASTAdaptor\<\> _adaptor = (ASTAdaptor)new CommonASTAdaptor(); ->> - -ParserASTContextInterface(s) ::= "ASTContext\<>" -ParserASTTreeFieldDecl(s) ::= " tree" -ParserASTContextMembers(s) ::= << -@Override public getTree() { return tree; } ->> - DefaultParserSuperClass(s) ::= "Parser" -DefaultTreeParserSuperClass(s) ::= "TreeParser\<>" ActionText(t) ::= "" ArgRef(a) ::= "_localctx." @@ -488,7 +422,6 @@ SetAttr(s,rhsChunks) ::= "_localctx. = ;" LexerSetAttr(s,rhsChunks) ::= " = ;" //SetQAttr(s,rhsChunks) ::= ". = ;" -ASTLabelType() ::= "" TokenLabelType() ::= "" InputSymbolType() ::= "" @@ -498,32 +431,18 @@ TokenPropertyRef_line(t) ::= "(_localctx.!=null?_localctx..get TokenPropertyRef_pos(t) ::= "(_localctx.!=null?_localctx..getCharPositionInLine():0)" TokenPropertyRef_channel(t) ::= "(_localctx.!=null?_localctx..getChannel():0)" TokenPropertyRef_index(t) ::= "(_localctx.!=null?_localctx..getTokenIndex():0)" -TokenPropertyRef_tree(t) ::= "_localctx._tree" TokenPropertyRef_int(t) ::= "(_localctx.!=null?Integer.valueOf(_localctx..getText()):0)" RulePropertyRef_start(r) ::= "(_localctx.!=null?(_localctx..start):null)" RulePropertyRef_stop(r) ::= "(_localctx.!=null?(_localctx..stop):null)" -RulePropertyRef_tree(r) ::= "(_localctx.!=null?(_localctx..tree):null)" RulePropertyRef_text(r) ::= "(_localctx.!=null?_input.toString(_localctx..start,_localctx..stop):null)" -RulePropertyRef_st(r) ::= "(_localctx.!=null?_localctx..st:null)" RulePropertyRef_ctx(r) ::= "_localctx." ThisRulePropertyRef_start(r) ::= "_localctx.start" ThisRulePropertyRef_stop(r) ::= "_localctx.stop" -ThisRulePropertyRef_tree(r) ::= "_localctx.tree" ThisRulePropertyRef_text(r) ::= "_input.toString(_localctx.start, _input.LT(-1))" -ThisRulePropertyRef_st(r) ::= "_localctx.st" ThisRulePropertyRef_ctx(r) ::= "_localctx" -/* -TreeRulePropertyRef_text(r) ::= <% -(_localctx.!=null? - ((ASTNodeStream\<>)_input).toString(_localctx..start,_localctx..stop) - :null -) -%> -*/ - NonLocalAttrRef(s) ::= "((Context)getInvokingContext())." SetNonLocalAttr(s, rhsChunks) ::= "((Context)getInvokingContext()). = ;" @@ -536,9 +455,6 @@ TokenListDecl(t) ::= "List\ = new ArrayList\();" RuleContextDecl(r) ::= " ;" RuleContextListDecl(rdecl) ::= "List\<> = new ArrayList\<>();" -NodeDecl(t) ::= " ;" -NodeListDecl(t) ::= "List\<> = new ArrayList\<>();" - /** Default RuleContext type name for a Parser rule */ ParserRuleContext() ::= "ParserRuleContext\" @@ -575,8 +491,6 @@ public static class extends implements > -TreeParserStructDecl ::= StructDecl - AltLabelStructDecl(s,attrs,visitorDispatchMethods) ::= << public static class Context extends Context { public Context(Context ctx) { copyFrom(ctx); } @@ -591,17 +505,6 @@ public void enterexitRule(ParseTreeListener\<> -/* -SwitchedVisitorDispatchMethod(method) ::= << -public void enterexitRule(ParseTreeListener\<\> listener) { - switch ( altNum ) { - : ((Listener)listener).enterexit(this); break;}; separator="\n"> - } -} ->> -*/ - AttributeDecl(d) ::= "" /** If we don't know location of label def x, use this template */ @@ -614,112 +517,6 @@ recRuleAltPredicate(ruleName,opPrec) ::= " >= " recRuleSetResultAction() ::= "$tree=$_primary.tree;" recRuleSetReturnAction(src,name) ::= "$=$.;" -// AST stuff (TODO: separate?) - -RootDecl(d) ::= " = _adaptor.nil();" -RootName(level) ::= "_root" - -TokenAST(t) ::= "_adaptor.create()" -RuleAST(r) ::= ".tree" -AssignTreeResult(a) ::= "_localctx.tree = _root0;" -RuleASTCleanup(r) ::= << -_localctx.tree = _adaptor.rulePostProcessing(_localctx.tree); -_adaptor.setTokenBoundaries(_localctx.tree, _localctx.start, _localctx.stop); ->> - -ElementListDecl(d) ::= "List\<> = _adaptor.createElementList();" -ElementListName(elemName) ::= "_track_" -ClearElementList(c) ::= ".clear();" -TrackRuleElement(e) ::= ".add(.tree);" -TrackTokenElement(e) ::= ".add(_adaptor.create());" - -// REWRITE AST stuff -// assume roots are always locals in tree rewrites - -TreeRewrite(tr, locals, preamble, alts) ::= << -// rewrite: code level= , tree level = - - - -_localctx.tree = _root0; ->> - -RewriteChoice(c, predicate, ops) ::= << - -if ( ) { - -} - -{ - -} - ->> - -RewriteIteratorDecl(d) ::= "Iterator\<> ;" -RewriteIteratorInit(i) ::= " = .iterator();" -RewriteIteratorName(elemName,level) ::= "it_" - -RewriteTreeOptional(o, locals, preamble, ops) ::= << -// ? code level= , tree level = -{ - - - if ( .hasNext()}; separator=" || "> ) { - - } -} ->> - -RewriteTreeClosure(c, locals, preamble, ops) ::= << -// * code level= , tree level = -{ - - - while ( .hasNext()}; separator=" || "> ) { - - } -} ->> - -RewriteTreeStructure(t, locals, ops) ::= << -// TREE code level= , tree level = -{ - - - _adaptor.addChild(_root, _root); -} ->> - -RewriteTokenRef(t) ::= ".next()" - -RewriteImagTokenRef(t, argChunks) ::= << -_adaptor.create(, "") ->> - -RewriteRuleRef(r) ::= ".next()" - -RewriteLabelRef(t) ::= ".next()" - -// -> $e in rule e -RewriteSelfRuleLabelRef(s) ::= "_localctx.tree" - -RewriteAction(a, chunks) ::= "" - -/** how to add child in rewrite section */ -AddChild(x, rootName, child) ::= "_adaptor.addChild(, );" - -/** how to make something a new root in rewrite section */ -BecomeRoot(x, rootName, newRoot) ::= - " = _adaptor.becomeRoot(, );" - - -/* -BitSetDecl(b) ::= << -public static final LABitSet =new LABitSet(new long[]{L};separator=",">}, true); ->> -*/ - LexerFile(lexerFile, lexer, namedActions) ::= << // $ANTLR ANTLRVersion> generatedTimestamp> diff --git a/tool/src/org/antlr/v4/Tool.java b/tool/src/org/antlr/v4/Tool.java index 9b50b5173..754cfebe8 100644 --- a/tool/src/org/antlr/v4/Tool.java +++ b/tool/src/org/antlr/v4/Tool.java @@ -34,7 +34,6 @@ import org.antlr.v4.analysis.AnalysisPipeline; import org.antlr.v4.automata.ATNFactory; import org.antlr.v4.automata.LexerATNFactory; import org.antlr.v4.automata.ParserATNFactory; -import org.antlr.v4.automata.TreeParserATNFactory; import org.antlr.v4.codegen.CodeGenPipeline; import org.antlr.v4.parse.ANTLRLexer; import org.antlr.v4.parse.ANTLRParser; @@ -88,6 +87,7 @@ public class Tool { public boolean profile = false; public boolean trace = false; public boolean generate_ATN_dot = false; + public String grammarEncoding = null; // use default locale's encoding public String msgFormat = "antlr"; public boolean saveLexer = false; public boolean genListener = true; @@ -105,7 +105,8 @@ public class Tool { new Option("profile", "-profile", "generate a parser that computes profiling information"), new Option("trace", "-trace", "generate a recognizer that traces rule entry/exit"), new Option("generate_ATN_dot", "-atn", "generate rule augmented transition networks"), - new Option("msgFormat", "-message-format", OptionArgType.STRING, "specify output style for messages"), + new Option("grammarEncoding", "-encoding", OptionArgType.STRING, "specify grammar file encoding; e.g., euc-jp"), + new Option("msgFormat", "-message-format", OptionArgType.STRING, "specify output style for messages"), new Option("genListener", "-walker", "generate parse tree walker and listener"), 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"), @@ -299,7 +300,6 @@ public class Tool { // BUILD ATN FROM AST ATNFactory factory; if ( g.isLexer() ) factory = new LexerATNFactory((LexerGrammar)g); - else if ( g.isTreeGrammar() ) factory = new TreeParserATNFactory(g); else factory = new ParserATNFactory(g); g.atn = factory.createATN(); @@ -338,7 +338,7 @@ public class Tool { public GrammarRootAST loadGrammar(String fileName) { try { - ANTLRFileStream in = new ANTLRFileStream(fileName); + ANTLRFileStream in = new ANTLRFileStream(fileName, grammarEncoding); GrammarRootAST t = load(in); return t; } diff --git a/tool/src/org/antlr/v4/automata/TreeParserATNFactory.java b/tool/src/org/antlr/v4/automata/TreeParserATNFactory.java deleted file mode 100644 index 105bb8099..000000000 --- a/tool/src/org/antlr/v4/automata/TreeParserATNFactory.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.automata; - -import org.antlr.v4.runtime.ParserRuleContext; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.atn.*; -import org.antlr.v4.runtime.misc.IntervalSet; -import org.antlr.v4.tool.Grammar; -import org.antlr.v4.tool.ast.GrammarAST; -import org.antlr.v4.tool.ast.TreePatternAST; - -import java.util.ArrayList; -import java.util.List; - -/** Build ATNs for tree grammars */ -public class TreeParserATNFactory extends ParserATNFactory { - // track stuff for ^(...) patterns in grammar to fix up nullable after ATN build - List treePatternRootNodes = new ArrayList(); - List firstChildStates = new ArrayList(); - List downStates = new ArrayList(); - List upTargetStates = new ArrayList(); - - public TreeParserATNFactory(Grammar g) { - super(g); - } - - public ATN createATN() { - super.createATN(); - - for (int i=0; io-DOWN->o-y->o-z->o-UP->o - * ANTLRParser.g has added DOWN_TOKEN, UP_TOKEN into AST. - * Elems are [root, DOWN_TOKEN, x, y, UP_TOKEN] - */ - public Handle tree(GrammarAST node, List els) { - TreePatternAST root = (TreePatternAST) node; - - Handle h = elemList(els); - treePatternRootNodes.add(root); - // find DOWN node then first child - for (Handle elh : els) { - Transition trans = elh.left.transition(0); - if ( !trans.isEpsilon() && trans.label().contains(Token.DOWN) ) { - ATNState downState = elh.left; - downStates.add(downState); - root.downState = downState; - firstChildStates.add(downState.transition(0).target); - break; - } - } - // find UP node - for (Handle elh : els) { - Transition trans = elh.left.transition(0); - if ( trans instanceof AtomTransition && trans.label().contains(Token.UP) ) { - ATNState upTargetState = elh.right; - root.upState = elh.left; - upTargetStates.add(upTargetState); - break; - } - } - - return h; - } -} diff --git a/tool/src/org/antlr/v4/codegen/ActionTranslator.java b/tool/src/org/antlr/v4/codegen/ActionTranslator.java index 3b5f2f6f8..cb52618e1 100644 --- a/tool/src/org/antlr/v4/codegen/ActionTranslator.java +++ b/tool/src/org/antlr/v4/codegen/ActionTranslator.java @@ -52,18 +52,14 @@ public class ActionTranslator implements ActionSplitterListener { public static final Map thisRulePropToModelMap = new HashMap() {{ put("start", ThisRulePropertyRef_start.class); put("stop", ThisRulePropertyRef_stop.class); - put("tree", ThisRulePropertyRef_tree.class); put("text", ThisRulePropertyRef_text.class); - put("st", ThisRulePropertyRef_st.class); put("ctx", ThisRulePropertyRef_ctx.class); }}; public static final Map rulePropToModelMap = new HashMap() {{ put("start", RulePropertyRef_start.class); put("stop", RulePropertyRef_stop.class); - put("tree", RulePropertyRef_tree.class); put("text", RulePropertyRef_text.class); - put("st", RulePropertyRef_st.class); put("ctx", RulePropertyRef_ctx.class); }}; @@ -76,7 +72,6 @@ public class ActionTranslator implements ActionSplitterListener { put("index", TokenPropertyRef_index.class); put("pos", TokenPropertyRef_pos.class); put("channel", TokenPropertyRef_channel.class); - put("tree", TokenPropertyRef_tree.class); put("int", TokenPropertyRef_int.class); }}; @@ -288,9 +283,7 @@ public class ActionTranslator implements ActionSplitterListener { RulePropertyRef getRulePropertyRef(Token x, Token prop) { Grammar g = factory.getGrammar(); try { - Class c = g.isTreeGrammar() ? - treeRulePropToModelMap.get(prop.getText()) : - rulePropToModelMap.get(prop.getText()); + Class c = rulePropToModelMap.get(prop.getText()); Constructor ctor = c.getConstructor(new Class[] {String.class}); RulePropertyRef ref = (RulePropertyRef)ctor.newInstance(getRuleLabel(x.getText())); diff --git a/tool/src/org/antlr/v4/codegen/BlankOutputModelFactory.java b/tool/src/org/antlr/v4/codegen/BlankOutputModelFactory.java index f8d4d9eb1..b631cb6d5 100644 --- a/tool/src/org/antlr/v4/codegen/BlankOutputModelFactory.java +++ b/tool/src/org/antlr/v4/codegen/BlankOutputModelFactory.java @@ -30,14 +30,11 @@ package org.antlr.v4.codegen; import org.antlr.v4.codegen.model.*; -import org.antlr.v4.codegen.model.ast.*; import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.tool.Alternative; import org.antlr.v4.tool.Rule; -import org.antlr.v4.tool.ast.ActionAST; import org.antlr.v4.tool.ast.BlockAST; import org.antlr.v4.tool.ast.GrammarAST; -import org.antlr.v4.tool.ast.PredAST; import java.util.List; @@ -62,13 +59,15 @@ public abstract class BlankOutputModelFactory implements OutputModelFactory { public CodeBlockForAlt epsilon() { return null; } - public List ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args, GrammarAST astOp) { return null; } + public List ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args) { return null; } - public List tokenRef(GrammarAST ID, GrammarAST label, GrammarAST args, GrammarAST astOp) { return null; } + public List tokenRef(GrammarAST ID, GrammarAST label, GrammarAST args) { return null; } - public List stringRef(GrammarAST ID, GrammarAST label, GrammarAST astOp) { return tokenRef(ID, label, null, astOp); } + public List stringRef(GrammarAST ID, GrammarAST label) { return tokenRef(ID, label, null); } - public List set(GrammarAST setAST, GrammarAST label, GrammarAST astOp, boolean invert) { return null; } + public List set(GrammarAST setAST, GrammarAST label, boolean invert) { return null; } + + public List wildcard(GrammarAST ast, GrammarAST labelAST) { return null; } // ACTIONS @@ -78,40 +77,6 @@ public abstract class BlankOutputModelFactory implements OutputModelFactory { public List sempred(GrammarAST ast) { return null; } - // AST OPS - - public List rootToken(List ops) { return ops; } - - public List rootRule(List ops) { return ops; } - - public List wildcard(GrammarAST ast, GrammarAST labelAST, GrammarAST astOp) { return null; } - - // AST REWRITES - - public TreeRewrite treeRewrite(GrammarAST ast) { return null; } - - public RewriteChoice rewrite_choice(PredAST pred, List ops) { return null; } - - public RewriteTreeOptional rewrite_optional(GrammarAST ast) { return null; } - - public RewriteTreeClosure rewrite_closure(GrammarAST ast) { return null; } - - public RewriteTreeStructure rewrite_treeStructure(GrammarAST root) { return null; } - - public List rewrite_ruleRef(GrammarAST ID, boolean isRoot) { return null; } - - public List rewrite_tokenRef(GrammarAST ID, boolean isRoot, ActionAST argAST) { return null; } - - public List rewrite_stringRef(GrammarAST ID, boolean isRoot) { - return rewrite_tokenRef(ID, isRoot, null); - } - - public List rewrite_labelRef(GrammarAST ID, boolean isRoot) { return null; } - - public List rewrite_action(ActionAST action, boolean isRoot) { return null; } - - public List rewrite_epsilon(GrammarAST epsilon) { return null; } - // BLOCKS public Choice getChoiceBlock(BlockAST blkAST, List alts, GrammarAST label) { return null; } diff --git a/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java b/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java index ed92ac19c..a870cc235 100644 --- a/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java +++ b/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java @@ -44,9 +44,6 @@ public class CodeGenPipeline { if ( g.isLexer() ) { gen.writeRecognizer(gen.generateLexer()); } - else if ( g.isTreeGrammar() ) { - gen.writeRecognizer(gen.generateTreeParser()); - } else { gen.writeRecognizer(gen.generateParser()); if ( g.tool.genListener) { diff --git a/tool/src/org/antlr/v4/codegen/CodeGenerator.java b/tool/src/org/antlr/v4/codegen/CodeGenerator.java index c3b3bce56..69f642105 100644 --- a/tool/src/org/antlr/v4/codegen/CodeGenerator.java +++ b/tool/src/org/antlr/v4/codegen/CodeGenerator.java @@ -116,21 +116,6 @@ public class CodeGenerator { } } -// public void buildOutputModel() { -// OutputModelFactory factory; -// if ( g.isLexer() ) factory = new LexerFactory(this); -// else factory = new ParserFactory(this); -// // CREATE OUTPUT MODEL FROM GRAMMAR OBJ AND AST WITHIN RULES -// OutputModelController controller = new OutputModelController(factory); -// if ( g.hasASTOption() ) { -// controller.addExtension( new ParserASTExtension(factory) ); -// } -// factory.setController(controller); -// -// if ( g.isLexer() ) outputModel = controller.buildLexerOutputModel(); -// else outputModel = controller.buildParserOutputModel(); -// } - // CREATE TEMPLATES BY WALKING MODEL public ST generateLexer() { OutputModelFactory factory = new LexerFactory(this); @@ -160,9 +145,6 @@ public class CodeGenerator { // CREATE OUTPUT MODEL FROM GRAMMAR OBJ AND AST WITHIN RULES OutputModelController controller = new OutputModelController(factory); - if ( g.hasASTOption() ) { - controller.addExtension( new ParserASTExtension(factory) ); - } factory.setController(controller); OutputModelObject outputModel = controller.buildParserOutputModel(); @@ -178,29 +160,6 @@ public class CodeGenerator { return st; } - public ST generateTreeParser() { - OutputModelFactory factory = new TreeParserFactory(this); - - // CREATE OUTPUT MODEL FROM GRAMMAR OBJ AND AST WITHIN RULES - OutputModelController controller = new OutputModelController(factory); - if ( g.hasASTOption() ) { - controller.addExtension( new ParserASTExtension(factory) ); - } - factory.setController(controller); - - OutputModelObject outputModel = controller.buildTreeParserOutputModel(); - - OutputModelWalker walker = new OutputModelWalker(tool, templates); - ST st = walker.walk(outputModel); - - if ( tool.launch_ST_inspector ) { - st.inspect(); - //if ( templates.isDefined("headerFile") ) headerFileST.inspect(); - } - - return st; - } - public ST generateListener() { OutputModelFactory factory = new ParserFactory(this); diff --git a/tool/src/org/antlr/v4/codegen/CodeGeneratorExtension.java b/tool/src/org/antlr/v4/codegen/CodeGeneratorExtension.java index 45e007b99..139c75b9a 100644 --- a/tool/src/org/antlr/v4/codegen/CodeGeneratorExtension.java +++ b/tool/src/org/antlr/v4/codegen/CodeGeneratorExtension.java @@ -31,7 +31,6 @@ package org.antlr.v4.codegen; import org.antlr.v4.codegen.model.*; -import org.antlr.v4.codegen.model.ast.*; import org.antlr.v4.tool.ast.GrammarAST; import java.util.List; @@ -52,10 +51,6 @@ public class CodeGeneratorExtension { public Lexer lexer(Lexer l) { return l; } - public TreeParserFile treeParserFile(TreeParserFile f) { return f; } - - public TreeParserModel treeParser(TreeParserModel p) { return p; } - public RuleFunction rule(RuleFunction rf) { return rf; } public List rulePostamble(List ops) { return ops; } @@ -76,36 +71,12 @@ public class CodeGeneratorExtension { public List wildcard(List ops) { return ops; } - public MatchTree tree(MatchTree matchTree) { return matchTree; } - // ACTIONS public List action(List ops) { return ops; } public List sempred(List ops) { return ops; } - // AST OPS - - public List rootRule(List ops) { return ops; } - - public List leafRule(List ops) { return ops; } - - public List rootToken(List ops) { return ops; } - - public List leafToken(List ops) { return ops; } - - public List rootString(List ops) { return ops; } - - public List leafString(List ops) { return ops; } - - public List rootSet(List ops) { return ops; } - - public List leafSet(List ops) { return ops; } - - public List rootWildcard(List ops) { return ops; } - - public List leafWildcard(List ops) { return ops; } - // BLOCKS public Choice getChoiceBlock(Choice c) { return c; } @@ -113,28 +84,4 @@ public class CodeGeneratorExtension { public Choice getEBNFBlock(Choice c) { return c; } public boolean needsImplicitLabel(GrammarAST ID, LabeledOp op) { return false; } - - // AST REWRITEs - - public TreeRewrite treeRewrite(TreeRewrite r) { return r; } - - public RewriteChoice rewrite_choice(RewriteChoice r) { return r; } - - public RewriteTreeOptional rewrite_optional(RewriteTreeOptional o) { return o; } - - public RewriteTreeClosure rewrite_closure(RewriteTreeClosure c) { return c; } - - public RewriteTreeStructure rewrite_treeStructure(RewriteTreeStructure t) { return t; } - - public List rewrite_ruleRef(List ops) { return ops; } - - public List rewrite_tokenRef(List ops) { return ops; } - - public List rewrite_stringRef(List ops) { return ops; } - - public List rewrite_labelRef(List ops) { return ops; } - - public List rewrite_action(List ops) { return ops; } - - public List rewrite_epsilon(List ops) { return ops; } } diff --git a/tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.java b/tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.java index c2d678019..cbf8f1710 100644 --- a/tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.java +++ b/tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.java @@ -29,14 +29,15 @@ package org.antlr.v4.codegen; -import org.antlr.v4.runtime.misc.NotNull; -import org.antlr.v4.runtime.misc.Nullable; -import org.antlr.v4.codegen.model.*; +import org.antlr.v4.codegen.model.OutputModelObject; +import org.antlr.v4.codegen.model.RuleFunction; +import org.antlr.v4.codegen.model.SrcOp; import org.antlr.v4.codegen.model.decl.CodeBlock; import org.antlr.v4.codegen.model.decl.Decl; +import org.antlr.v4.runtime.misc.NotNull; +import org.antlr.v4.runtime.misc.Nullable; import org.antlr.v4.tool.Alternative; import org.antlr.v4.tool.Grammar; -import org.antlr.v4.tool.ast.GrammarAST; import java.util.ArrayList; import java.util.List; @@ -94,21 +95,6 @@ public abstract class DefaultOutputModelFactory extends BlankOutputModelFactory @Override public int getTreeLevel() { return controller.treeLevel; } - @Override - public TreeParserFile treeParserFile(String fileName) { - return null; - } - - @Override - public TreeParserModel treeParser(TreeParserFile file) { - return null; - } - - @Override - public MatchTree tree(GrammarAST treeBeginAST, List omos) { - throw new UnsupportedOperationException("^(...) in non-tree grammar"); - } - // MISC @NotNull diff --git a/tool/src/org/antlr/v4/codegen/OutputModelController.java b/tool/src/org/antlr/v4/codegen/OutputModelController.java index fd26a008f..af5d5f45a 100644 --- a/tool/src/org/antlr/v4/codegen/OutputModelController.java +++ b/tool/src/org/antlr/v4/codegen/OutputModelController.java @@ -31,7 +31,6 @@ package org.antlr.v4.codegen; import org.antlr.runtime.tree.CommonTreeNodeStream; import org.antlr.v4.codegen.model.*; -import org.antlr.v4.codegen.model.ast.*; import org.antlr.v4.codegen.model.decl.CodeBlock; import org.antlr.v4.parse.ANTLRParser; import org.antlr.v4.parse.GrammarASTAdaptor; @@ -111,21 +110,6 @@ public class OutputModelController { return file; } - public OutputModelObject buildTreeParserOutputModel() { - Grammar g = delegate.getGrammar(); - CodeGenerator gen = delegate.getGenerator(); - TreeParserFile file = treeParserFile(gen.getRecognizerFileName()); - setRoot(file); - Parser parser = treeParser(file); - file.parser = parser; - - for (Rule r : g.rules.values()) { - buildRuleFunction(parser, r); - } - - return file; - } - public OutputModelObject buildListenerOutputModel() { CodeGenerator gen = delegate.getGenerator(); return new ListenerFile(delegate, gen.getListenerFileName()); @@ -156,18 +140,6 @@ public class OutputModelController { return new Lexer(delegate, file); } - public TreeParserFile treeParserFile(String fileName) { - TreeParserFile f = delegate.treeParserFile(fileName); - for (CodeGeneratorExtension ext : extensions) f = ext.treeParserFile(f); - return f; - } - - public TreeParserModel treeParser(TreeParserFile file) { - TreeParserModel p = delegate.treeParser(file); - for (CodeGeneratorExtension ext : extensions) p = ext.treeParser(p); - return p; - } - /** Create RuleFunction per rule and update sempreds,actions of parser * output object with stuff found in r. */ @@ -183,7 +155,8 @@ public class OutputModelController { CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor,blk); walker = new SourceGenTriggers(nodes, this); try { - function.code = DefaultOutputModelFactory.list(walker.block(null, null, null)); // walk AST of rule alts/elements + // walk AST of rule alts/elements + function.code = DefaultOutputModelFactory.list(walker.block(null, null)); } catch (Exception e){ e.printStackTrace(System.err); @@ -284,64 +257,36 @@ public class OutputModelController { return blk; } - public List ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args, - GrammarAST astOp) - { - List ops = delegate.ruleRef(ID, label, args, astOp); + public List ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args) { + List ops = delegate.ruleRef(ID, label, args); for (CodeGeneratorExtension ext : extensions) { ops = ext.ruleRef(ops); - if ( astOp!=null && astOp.getType()==ANTLRParser.ROOT ) { - ops = ext.rootRule(ops); - } - else if ( astOp==null ) { - ops = ext.leafRule(ops); - } } return ops; } - public List tokenRef(GrammarAST ID, GrammarAST label, GrammarAST args, - GrammarAST astOp) + public List tokenRef(GrammarAST ID, GrammarAST label, GrammarAST args) { - List ops = delegate.tokenRef(ID, label, args, astOp); + List ops = delegate.tokenRef(ID, label, args); for (CodeGeneratorExtension ext : extensions) { ops = ext.tokenRef(ops); - if ( astOp!=null && astOp.getType()==ANTLRParser.ROOT ) { - ops = ext.rootToken(ops); - } - else if ( astOp==null ) { - ops = ext.leafToken(ops); - } } return ops; } - public List stringRef(GrammarAST ID, GrammarAST label, GrammarAST astOp) { - List ops = delegate.stringRef(ID, label, astOp); + public List stringRef(GrammarAST ID, GrammarAST label) { + List ops = delegate.stringRef(ID, label); for (CodeGeneratorExtension ext : extensions) { ops = ext.stringRef(ops); - if ( astOp!=null && astOp.getType()==ANTLRParser.ROOT ) { - ops = ext.rootString(ops); - } - else if ( astOp==null ) { - ops = ext.leafString(ops); - } } return ops; } /** (A|B|C) possibly with ebnfRoot and label */ - public List set(GrammarAST setAST, GrammarAST labelAST, - GrammarAST astOp, boolean invert) { - List ops = delegate.set(setAST, labelAST, astOp, invert); + public List set(GrammarAST setAST, GrammarAST labelAST, boolean invert) { + List ops = delegate.set(setAST, labelAST, invert); for (CodeGeneratorExtension ext : extensions) { ops = ext.set(ops); - if ( astOp!=null && astOp.getType()==ANTLRParser.ROOT ) { - ops = ext.rootSet(ops); - } - else if ( astOp==null ) { - ops = ext.leafSet(ops); - } } return ops; } @@ -352,6 +297,14 @@ public class OutputModelController { return blk; } + public List wildcard(GrammarAST ast, GrammarAST labelAST) { + List ops = delegate.wildcard(ast, labelAST); + for (CodeGeneratorExtension ext : extensions) { + ops = ext.set(ops); + } + return ops; + } + public List action(GrammarAST ast) { List ops = delegate.action(ast); for (CodeGeneratorExtension ext : extensions) ops = ext.action(ops); @@ -364,26 +317,6 @@ public class OutputModelController { return ops; } - public List wildcard(GrammarAST ast, GrammarAST labelAST, GrammarAST astOp) { - List ops = delegate.wildcard(ast, labelAST, astOp); - for (CodeGeneratorExtension ext : extensions) { - ops = ext.set(ops); - if ( astOp!=null && astOp.getType()==ANTLRParser.ROOT ) { - ops = ext.rootWildcard(ops); - } - else if ( astOp==null ) { - ops = ext.leafWildcard(ops); - } - } - return ops; - } - - public MatchTree tree(GrammarAST treeBeginAST, List omos) { - MatchTree matchTree = delegate.tree(treeBeginAST, omos); - for (CodeGeneratorExtension ext : extensions) matchTree = ext.tree(matchTree); - return matchTree; - } - public Choice getChoiceBlock(BlockAST blkAST, List alts, GrammarAST label) { Choice c = delegate.getChoiceBlock(blkAST, alts, label); for (CodeGeneratorExtension ext : extensions) c = ext.getChoiceBlock(c); @@ -402,72 +335,6 @@ public class OutputModelController { return needs; } - // REWRITES - - public TreeRewrite treeRewrite(GrammarAST ast) { - TreeRewrite r = delegate.treeRewrite(ast); - for (CodeGeneratorExtension ext : extensions) r = ext.treeRewrite(r); - return r; - } - - public RewriteChoice rewrite_choice(PredAST pred, List ops) { - RewriteChoice r = delegate.rewrite_choice(pred, ops); - for (CodeGeneratorExtension ext : extensions) r = ext.rewrite_choice(r); - return r; - } - - public RewriteTreeOptional rewrite_optional(GrammarAST ast) { - RewriteTreeOptional o = delegate.rewrite_optional(ast); - for (CodeGeneratorExtension ext : extensions) o = ext.rewrite_optional(o); - return o; - } - - public RewriteTreeClosure rewrite_closure(GrammarAST ast) { - RewriteTreeClosure c = delegate.rewrite_closure(ast); - for (CodeGeneratorExtension ext : extensions) c = ext.rewrite_closure(c); - return c; - } - - public RewriteTreeStructure rewrite_treeStructure(GrammarAST root) { - RewriteTreeStructure t = delegate.rewrite_treeStructure(root); - for (CodeGeneratorExtension ext : extensions) t = ext.rewrite_treeStructure(t); - return t; - } - - public List rewrite_ruleRef(GrammarAST ID, boolean isRoot) { - List ops = delegate.rewrite_ruleRef(ID, isRoot); - for (CodeGeneratorExtension ext : extensions) ops = ext.rewrite_ruleRef(ops); - return ops; - } - - public List rewrite_tokenRef(GrammarAST ID, boolean isRoot, ActionAST argAST) { - List ops = delegate.rewrite_tokenRef(ID, isRoot, argAST); - for (CodeGeneratorExtension ext : extensions) ops = ext.rewrite_tokenRef(ops); - return ops; - } - - public List rewrite_stringRef(GrammarAST ID, boolean isRoot) { - return rewrite_tokenRef(ID, isRoot, null); - } - - public List rewrite_labelRef(GrammarAST ID, boolean isRoot) { - List ops = delegate.rewrite_labelRef(ID, isRoot); - for (CodeGeneratorExtension ext : extensions) ops = ext.rewrite_labelRef(ops); - return ops; - } - - public List rewrite_action(ActionAST action, boolean isRoot) { - List ops = delegate.rewrite_action(action, isRoot); - for (CodeGeneratorExtension ext : extensions) ops = ext.rewrite_action(ops); - return ops; - } - - public List rewrite_epsilon(GrammarAST epsilon) { - List ops = delegate.rewrite_epsilon(epsilon); - for (CodeGeneratorExtension ext : extensions) ops = ext.rewrite_epsilon(ops); - return ops; - } - public OutputModelObject getRoot() { return root; } public void setRoot(OutputModelObject root) { this.root = root; } diff --git a/tool/src/org/antlr/v4/codegen/OutputModelFactory.java b/tool/src/org/antlr/v4/codegen/OutputModelFactory.java index 573565e4e..e0abb230e 100644 --- a/tool/src/org/antlr/v4/codegen/OutputModelFactory.java +++ b/tool/src/org/antlr/v4/codegen/OutputModelFactory.java @@ -30,16 +30,13 @@ package org.antlr.v4.codegen; import org.antlr.v4.codegen.model.*; -import org.antlr.v4.codegen.model.ast.*; import org.antlr.v4.codegen.model.decl.CodeBlock; import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.tool.Alternative; import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.Rule; -import org.antlr.v4.tool.ast.ActionAST; import org.antlr.v4.tool.ast.BlockAST; import org.antlr.v4.tool.ast.GrammarAST; -import org.antlr.v4.tool.ast.PredAST; import java.util.List; @@ -58,10 +55,6 @@ public interface OutputModelFactory { Lexer lexer(LexerFile file); - TreeParserFile treeParserFile(String fileName); - - TreeParserModel treeParser(TreeParserFile file); - RuleFunction rule(Rule r); List rulePostamble(RuleFunction function, Rule r); @@ -74,13 +67,15 @@ public interface OutputModelFactory { CodeBlockForAlt epsilon(); - List ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args, GrammarAST astOp); + List ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args); - List tokenRef(GrammarAST ID, GrammarAST label, GrammarAST args, GrammarAST astOp); + List tokenRef(GrammarAST ID, GrammarAST label, GrammarAST args); - List stringRef(GrammarAST ID, GrammarAST label, GrammarAST astOp); + List stringRef(GrammarAST ID, GrammarAST label); - List set(GrammarAST setAST, GrammarAST label, GrammarAST astOp, boolean invert); + List set(GrammarAST setAST, GrammarAST label, boolean invert); + + List wildcard(GrammarAST ast, GrammarAST labelAST); List action(GrammarAST ast); @@ -88,14 +83,6 @@ public interface OutputModelFactory { List sempred(GrammarAST ast); - List rootToken(List ops); - - List rootRule(List ops); - - List wildcard(GrammarAST ast, GrammarAST labelAST, GrammarAST astOp); - - MatchTree tree(GrammarAST treeBeginAST, List omos); - Choice getChoiceBlock(BlockAST blkAST, List alts, GrammarAST label); Choice getEBNFBlock(GrammarAST ebnfRoot, List alts); @@ -112,32 +99,6 @@ public interface OutputModelFactory { boolean needsImplicitLabel(GrammarAST ID, LabeledOp op); - // AST REWRITE TRIGGERS - // Though dealing with ASTs, we must deal with here since these are - // triggered from elements in ANTLR's internal GrammarAST - - TreeRewrite treeRewrite(GrammarAST ast); - - RewriteChoice rewrite_choice(PredAST pred, List ops); - - RewriteTreeOptional rewrite_optional(GrammarAST ast); - - RewriteTreeClosure rewrite_closure(GrammarAST ast); - - RewriteTreeStructure rewrite_treeStructure(GrammarAST root); - - List rewrite_ruleRef(GrammarAST ID, boolean isRoot); - - List rewrite_tokenRef(GrammarAST ID, boolean isRoot, ActionAST argAST); - - List rewrite_stringRef(GrammarAST ID, boolean isRoot); - - List rewrite_labelRef(GrammarAST ID, boolean isRoot); - - List rewrite_action(ActionAST action, boolean isRoot); - - List rewrite_epsilon(GrammarAST epsilon); - // CONTEXT INFO OutputModelObject getRoot(); diff --git a/tool/src/org/antlr/v4/codegen/ParserASTExtension.java b/tool/src/org/antlr/v4/codegen/ParserASTExtension.java deleted file mode 100644 index 40451a35d..000000000 --- a/tool/src/org/antlr/v4/codegen/ParserASTExtension.java +++ /dev/null @@ -1,278 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen; - -import org.antlr.v4.codegen.model.*; -import org.antlr.v4.codegen.model.actions.ParserASTExtensionMembers; -import org.antlr.v4.codegen.model.ast.*; -import org.antlr.v4.codegen.model.decl.*; -import org.antlr.v4.misc.Utils; -import org.antlr.v4.parse.ANTLRParser; -import org.antlr.v4.tool.Alternative; -import org.antlr.v4.tool.Attribute; -import org.antlr.v4.tool.ast.GrammarAST; - -import java.util.List; - -public class ParserASTExtension extends CodeGeneratorExtension { - public ParserASTExtension(OutputModelFactory factory) { - super(factory); - } - - @Override - public ParserFile parserFile(ParserFile file) { - Action members = file.namedActions.get("members"); - if (members == null) { - members = new Action(factory, null); - file.namedActions.put("members", members); - } - - members.chunks.add(new ParserASTExtensionMembers()); - return super.parserFile(file); - } - - @Override - public RuleFunction rule(RuleFunction rf) { - rf.ruleCtx.addDecl(new ParserASTTreeFieldDecl(factory)); - rf.ruleCtx.addExtensionMember(new ParserASTContextMembers()); - rf.ruleCtx.implementInterface(new ParserASTContextInterface()); - return super.rule(rf); - } - - @Override - public Choice getChoiceBlock(Choice choice) { - Alternative alt = factory.getCurrentOuterMostAlt(); - if ( alt.hasRewrite() && choice.label!=null ) { - trackExplicitLabel(choice.preamble, choice.label, choice); - } - return choice; - } - - @Override - public CodeBlockForAlt alternative(CodeBlockForAlt blk, boolean outerMost) { - Alternative alt = factory.getCurrentOuterMostAlt(); - if ( outerMost && !alt.hasRewrite() ) { - blk.addLocalDecl(new RootDecl(factory, 0)); - } - return blk; - } - - @Override - public CodeBlockForAlt finishAlternative(CodeBlockForAlt blk, boolean outerMost) { - Alternative alt = factory.getCurrentOuterMostAlt(); - if ( !alt.hasRewrite() ) blk.addOp(new AssignTreeResult(factory)); - return blk; - } - - @Override - public List rulePostamble(List ops) { - RuleASTCleanup cleanup = new RuleASTCleanup(factory); - return DefaultOutputModelFactory.list(ops, cleanup); - } - - @Override - public List rootRule(List ops) { - Alternative alt = factory.getCurrentOuterMostAlt(); - if ( alt.hasRewrite() ) { - return ops; - } - else { - InvokeRule invokeOp = (InvokeRule)Utils.find(ops, InvokeRule.class); - SrcOp treeOp = new RuleAST(factory, invokeOp.ast, invokeOp.getLabels().get(0)); - String rootName = factory.getGenerator().target.getRootName(0); - SrcOp add = new BecomeRoot(factory, rootName, treeOp); - return DefaultOutputModelFactory.list(ops, add); - } - } - - @Override - public List leafRule(List ops) { - InvokeRule invokeOp = (InvokeRule)Utils.find(ops, InvokeRule.class); - Alternative alt = factory.getCurrentOuterMostAlt(); - if ( alt.hasRewrite() ) { - return leafRuleInRewriteAlt(invokeOp, ops); - } - else { - RuleContextDecl label = (RuleContextDecl)invokeOp.getLabels().get(0); - SrcOp treeOp = new RuleAST(factory, invokeOp.ast, label); - String rootName = factory.getGenerator().target.getRootName(0); - SrcOp add = new AddChild(factory, rootName, treeOp); - ops.add(add); - return ops; - } - } - - @Override - public List rootToken(List ops) { - Alternative alt = factory.getCurrentOuterMostAlt(); - if ( alt.hasRewrite() ) { - return ops; - } - else { - MatchToken matchOp = (MatchToken)Utils.find(ops, MatchToken.class); - SrcOp treeOp = new TokenAST(factory, matchOp.ast, matchOp.getLabels().get(0)); - String rootName = factory.getGenerator().target.getRootName(0); - SrcOp add = new BecomeRoot(factory, rootName, treeOp); - return DefaultOutputModelFactory.list(ops, add); - } - } - - @Override - public List leafToken(List ops) { - MatchToken matchOp = (MatchToken)Utils.find(ops, MatchToken.class); - Alternative alt = factory.getCurrentOuterMostAlt(); - if ( alt.hasRewrite() ) { - return leafTokenInRewriteAlt(matchOp, ops); - } - else { - TokenDecl label = (TokenDecl)matchOp.getLabels().get(0); - SrcOp treeOp = new TokenAST(factory, matchOp.ast, label); - String rootName = factory.getGenerator().target.getRootName(0); - SrcOp add = new AddChild(factory, rootName, treeOp); - ops.add(add); - return ops; - } - } - - @Override - public List rootString(List ops) { return rootToken(ops); } - - @Override - public List leafString(List ops) { return leafToken(ops); } - - public List leafRuleInRewriteAlt(InvokeRule invokeOp, List ops) { - RuleContextDecl label = (RuleContextDecl)invokeOp.getLabels().get(0); - CodeBlock blk = factory.getCurrentOuterMostAlternativeBlock(); - String elemListName = factory.getGenerator().target.getElementListName(invokeOp.ast.getText()); - blk.addLocalDecl(new ElementListDecl(factory, elemListName)); - - // add code to track rule results in _track_r - String trackName = factory.getGenerator().target.getElementListName(invokeOp.ast.getText()); - TrackRuleElement t = new TrackRuleElement(factory, invokeOp.ast, trackName, label); - ops.add(t); - - // track any explicit label like _track_label but not implicit label - if ( !label.isImplicit ) trackExplicitLabel(ops, label, invokeOp); - - return ops; - } - - public List leafTokenInRewriteAlt(SrcOp matchOp, List ops) { - CodeBlock blk = factory.getCurrentOuterMostAlternativeBlock(); - TokenDecl label = (TokenDecl)((LabeledOp)matchOp).getLabels().get(0); - // First declare tracking lists for elements, labels - // track the named element like _track_A - String elemName = matchOp.ast.getText(); - if ( matchOp.ast.getType()==ANTLRParser.SET ) { - elemName = String.valueOf(matchOp.ast.token.getTokenIndex()); - } - String elemListName = factory.getGenerator().target.getElementListName(elemName); - blk.addLocalDecl(new ElementListDecl(factory, elemListName)); - // Now, generate track instructions for element and any labels - // do element - String trackName = factory.getGenerator().target.getElementListName(elemName); - TrackTokenElement t = new TrackTokenElement(factory, matchOp.ast, trackName, - label); - ops.add(t); - if ( !label.isImplicit ) trackExplicitLabel(ops, label, matchOp); - return ops; - } - - @Override - public List rootSet(List ops) { return rootToken(ops); } - - @Override - public List leafSet(List ops) { return leafToken(ops); } - - @Override - public List wildcard(List ops) { - Wildcard wild = (Wildcard)Utils.find(ops, Wildcard.class); - Alternative alt = factory.getCurrentOuterMostAlt(); - if ( alt.hasRewrite() ) { - TokenDecl label = (TokenDecl)((LabeledOp)wild).getLabels().get(0); - if ( !label.isImplicit ) trackExplicitLabel(ops, label, wild); - return ops; - } - else { - TokenDecl label = (TokenDecl)wild.getLabels().get(0); - SrcOp treeOp = new TokenAST(factory, wild.ast, label); - String rootName = factory.getGenerator().target.getRootName(0); - SrcOp add = new AddChild(factory, rootName, treeOp); - ops.add(add); - return ops; - } - } - - @Override - public List rootWildcard(List ops) { return rootToken(ops); } - - @Override - public List leafWildcard(List ops) { return leafToken(ops); } - - public void trackExplicitLabel(List ops, Decl label, SrcOp opWithLabel) { - CodeBlock blk = factory.getCurrentOuterMostAlternativeBlock(); - // declare _track_label - String labelListName = - factory.getGenerator().target.getElementListName(label.name); - blk.addLocalDecl(new ElementListDecl(factory, labelListName)); - - // add elements to _track_label - SrcOp trk; - if ( opWithLabel instanceof InvokeRule ) { - trk = new TrackRuleElement(factory, opWithLabel.ast, labelListName, label); - } - else if ( opWithLabel instanceof Choice || - opWithLabel instanceof MatchToken || - opWithLabel instanceof Wildcard ) - { - trk = new TrackTokenElement(factory, opWithLabel.ast, labelListName, label); - } - else { - trk = null; - } - clearTrackingIfSingularLabel(ops, opWithLabel, labelListName); - ops.add(trk); - } - - public void clearTrackingIfSingularLabel(List ops, SrcOp opWithLabel, String trackName) { - if ( opWithLabel.ast.parent.getType() == ANTLRParser.ASSIGN ) { - // if x=A must keep it a single-element list; clear before add - ClearElementList c = new ClearElementList(factory, opWithLabel.ast, trackName); - ops.add(c); - } - } - - @Override - public boolean needsImplicitLabel(GrammarAST ID, LabeledOp op) { - return op.getLabels().size()==0 && factory.getGrammar().hasASTOption(); - } - - // REWRITES -} diff --git a/tool/src/org/antlr/v4/codegen/ParserFactory.java b/tool/src/org/antlr/v4/codegen/ParserFactory.java index bfdd90896..fd2cdcbfc 100644 --- a/tool/src/org/antlr/v4/codegen/ParserFactory.java +++ b/tool/src/org/antlr/v4/codegen/ParserFactory.java @@ -1,6 +1,6 @@ /* [The "BSD license"] - Copyright (c) 2011 Terence Parr + Copyright (c) 2012 Terence Parr All rights reserved. Redistribution and use in source and binary forms, with or without @@ -31,17 +31,20 @@ package org.antlr.v4.codegen; import org.antlr.v4.analysis.AnalysisPipeline; import org.antlr.v4.codegen.model.*; -import org.antlr.v4.codegen.model.ast.*; -import org.antlr.v4.codegen.model.decl.*; +import org.antlr.v4.codegen.model.decl.Decl; +import org.antlr.v4.codegen.model.decl.RuleContextDecl; +import org.antlr.v4.codegen.model.decl.TokenDecl; +import org.antlr.v4.codegen.model.decl.TokenListDecl; import org.antlr.v4.parse.ANTLRParser; import org.antlr.v4.runtime.atn.DecisionState; import org.antlr.v4.runtime.atn.PlusBlockStartState; import org.antlr.v4.runtime.atn.StarLoopEntryState; import org.antlr.v4.runtime.misc.IntervalSet; -import org.antlr.v4.semantics.UseDefAnalyzer; import org.antlr.v4.tool.Alternative; import org.antlr.v4.tool.Rule; -import org.antlr.v4.tool.ast.*; +import org.antlr.v4.tool.ast.BlockAST; +import org.antlr.v4.tool.ast.GrammarAST; +import org.antlr.v4.tool.ast.TerminalAST; import java.util.List; @@ -80,20 +83,15 @@ public class ParserFactory extends DefaultOutputModelFactory { public List sempred(GrammarAST ast) { return list(new SemPred(this, ast)); } - public List ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args, - GrammarAST astOp) - { + public List ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args) { InvokeRule invokeOp = new InvokeRule(this, ID, label); - // If no manual label and action refs as token/rule not label or - // we're adding to trees, we need to define implicit label + // If no manual label and action refs as token/rule not label, we need to define implicit label if ( controller.needsImplicitLabel(ID, invokeOp) ) defineImplicitLabel(ID, invokeOp); AddToLabelList listLabelOp = getListLabelIfPresent(invokeOp, label); return list(invokeOp, listLabelOp); } - public List tokenRef(GrammarAST ID, GrammarAST labelAST, GrammarAST args, - GrammarAST astOp) - { + public List tokenRef(GrammarAST ID, GrammarAST labelAST, GrammarAST args) { LabeledOp matchOp = new MatchToken(this, (TerminalAST) ID); if ( labelAST!=null ) { String label = labelAST.getText(); @@ -119,9 +117,7 @@ public class ParserFactory extends DefaultOutputModelFactory { } @Override - public List set(GrammarAST setAST, GrammarAST labelAST, - GrammarAST astOp, boolean invert) - { + public List set(GrammarAST setAST, GrammarAST labelAST, boolean invert) { LabeledOp matchOp; if ( invert ) matchOp = new MatchNotSet(this, setAST); else matchOp = new MatchSet(this, setAST); @@ -141,7 +137,7 @@ public class ParserFactory extends DefaultOutputModelFactory { } @Override - public List wildcard(GrammarAST ast, GrammarAST labelAST, GrammarAST astOp) { + public List wildcard(GrammarAST ast, GrammarAST labelAST) { Wildcard wild = new Wildcard(this, ast); // TODO: dup with tokenRef if ( labelAST!=null ) { @@ -263,128 +259,8 @@ public class ParserFactory extends DefaultOutputModelFactory { return op.getLabels().size()==0 && (actionRefsAsToken || actionRefsAsRule); } - // AST REWRITE - - - @Override - public TreeRewrite treeRewrite(GrammarAST ast) { - TreeRewrite tr = new TreeRewrite(this, getTreeLevel(), getCodeBlockLevel()); - tr.addLocalDecl(new RootDecl(this, 0)); - List refs = - UseDefAnalyzer.getElementReferencesShallowInOuterAlt(ast); - refs = UseDefAnalyzer.filterForRuleAndTokenRefs(getCurrentOuterMostAlt(), refs); - if ( refs!=null ) { - for (GrammarAST ref : refs) { - RewriteIteratorDecl d = new RewriteIteratorDecl(this, ref, getCodeBlockLevel()); - tr.addLocalDecl(d); - RewriteIteratorInit init = new RewriteIteratorInit(this, d); - tr.addPreambleOp(init); - } - } - return tr; - } - - @Override - public RewriteChoice rewrite_choice(PredAST pred, List ops) { - RewriteAction predAction = null; - if ( pred!=null ) predAction = new RewriteAction(this, pred); - RewriteChoice c = new RewriteChoice(this, predAction, ops); - return c; - } - - @Override - public RewriteTreeOptional rewrite_optional(GrammarAST ast) { - RewriteTreeOptional o = - new RewriteTreeOptional(this, ast, getTreeLevel(), getCodeBlockLevel()); - List refs = UseDefAnalyzer.getElementReferencesInEBNF(ast, true); - refs = UseDefAnalyzer.filterForRuleAndTokenRefs(getCurrentOuterMostAlt(), refs); - if ( refs!=null ) { - for (GrammarAST ref : refs) { - RewriteIteratorDecl d = new RewriteIteratorDecl(this, ref, getCodeBlockLevel()); - o.addLocalDecl(d); - o.conditionalDecls.add(d); - RewriteIteratorInit init = new RewriteIteratorInit(this, d); - o.addPreambleOp(init); - } - } - return o; - } - - @Override - public RewriteTreeClosure rewrite_closure(GrammarAST ast) { - RewriteTreeClosure c = - new RewriteTreeClosure(this, ast, getTreeLevel(), getCodeBlockLevel()); - List refs = UseDefAnalyzer.getElementReferencesInEBNF(ast, false); - refs = UseDefAnalyzer.filterForRuleAndTokenRefs(getCurrentOuterMostAlt(), refs); - if ( refs!=null ) { - for (GrammarAST ref : refs) { - RewriteIteratorDecl d = new RewriteIteratorDecl(this, ref, getCodeBlockLevel()); - c.addLocalDecl(d); - c.iteratorDecls.add(d); - RewriteIteratorInit init = new RewriteIteratorInit(this, d); - c.addPreambleOp(init); - } - } - return c; - } - - @Override - public RewriteTreeStructure rewrite_treeStructure(GrammarAST root) { - RewriteTreeStructure t = new RewriteTreeStructure(this, root, getTreeLevel(), getCodeBlockLevel()); - t.addLocalDecl( new RootDecl(this, getTreeLevel()) ); - return t; - } - - public List rewrite_ruleRef(GrammarAST ID, boolean isRoot) { - String iterName = gen.target.getRewriteIteratorName(ID, getCodeBlockLevel()); - RewriteRuleRef ruleRef = new RewriteRuleRef(this, ID, iterName); - return list(makeChildOrRoot(ruleRef, isRoot)); - } - - public List rewrite_tokenRef(GrammarAST ID, boolean isRoot, ActionAST argAST) { - Alternative alt = getCurrentOuterMostAlt(); - String iterName = gen.target.getRewriteIteratorName(ID, getCodeBlockLevel()); - // not ref'd on left hand side or it is but we have an argument like ID["x"] - // implies create new node - SrcOp tokenRef; - if ( alt.tokenRefs.get(ID.getText())==null || argAST!=null ) { - tokenRef = new RewriteImagTokenRef(this, ID, ID.getText(), argAST); - } - else { // must be token ref on left of -> - tokenRef = new RewriteTokenRef(this, ID, iterName); - } - return list(makeChildOrRoot(tokenRef, isRoot)); - } - - @Override - public List rewrite_labelRef(GrammarAST ID, boolean isRoot) { - String iterName = gen.target.getRewriteIteratorName(ID, getCodeBlockLevel()); - SrcOp labelRef; - if ( ID.getText().equals(getCurrentRuleFunction().rule.name) ) { // $e in rule e - labelRef = new RewriteSelfRuleLabelRef(this, ID); - } - else { // normal element label - labelRef = new RewriteLabelRef(this, ID, iterName); - } - return list(makeChildOrRoot(labelRef, isRoot)); - } - - @Override - public List rewrite_action(ActionAST actionAST, boolean isRoot) { - RewriteAction action = new RewriteAction(this, actionAST); - return list(makeChildOrRoot(action, isRoot)); - } - // support - public SrcOp makeChildOrRoot(SrcOp elemToAdd, boolean isRoot) { - String rootName = gen.target.getRootName(getTreeLevel()); - SrcOp op; - if ( isRoot ) op = new BecomeRoot(this, rootName, elemToAdd); - else op = new AddChild(this, rootName, elemToAdd); - return op; - } - public void defineImplicitLabel(GrammarAST ast, LabeledOp op) { Decl d; Rule r = g.getRule(ast.getText()); diff --git a/tool/src/org/antlr/v4/codegen/SourceGenTriggers.g b/tool/src/org/antlr/v4/codegen/SourceGenTriggers.g index 80ad568d1..230e99113 100644 --- a/tool/src/org/antlr/v4/codegen/SourceGenTriggers.g +++ b/tool/src/org/antlr/v4/codegen/SourceGenTriggers.g @@ -38,7 +38,6 @@ package org.antlr.v4.codegen; import org.antlr.v4.misc.Utils; import org.antlr.v4.codegen.model.*; import org.antlr.v4.codegen.model.decl.*; -import org.antlr.v4.codegen.model.ast.*; import org.antlr.v4.tool.*; import org.antlr.v4.tool.ast.*; import java.util.Collections; @@ -54,9 +53,9 @@ import java.util.HashMap; } } -dummy : block[null, null, null] ; +dummy : block[null, null] ; -block[GrammarAST label, GrammarAST ebnfRoot, GrammarAST astOp] returns [List omos] +block[GrammarAST label, GrammarAST ebnfRoot] returns [List omos] : ^( blk=BLOCK (^(OPTIONS .+))? {List alts = new ArrayList();} ( alternative {alts.add($alternative.altCodeBlock);} )+ @@ -79,14 +78,7 @@ alternative returns [CodeBlockForAlt altCodeBlock, List ops] @after { controller.finishAlternative($altCodeBlock, $ops, outerMost); } - : ^(ALT_REWRITE - a=alt[outerMost] - ( rewrite {$a.ops.add($rewrite.code);} // insert at end of alt's code - | - ) - {$altCodeBlock=$a.altCodeBlock; $ops=$a.ops;} - ) - | a=alt[outerMost] {$altCodeBlock=$a.altCodeBlock; $ops=$a.ops;} + : a=alt[outerMost] {$altCodeBlock=$a.altCodeBlock; $ops=$a.ops;} ; alt[boolean outerMost] returns [CodeBlockForAlt altCodeBlock, List ops] @@ -105,40 +97,28 @@ alt[boolean outerMost] returns [CodeBlockForAlt altCodeBlock, List ops] element returns [List omos] : labeledElement {$omos = $labeledElement.omos;} - | atom[null,null,false] {$omos = $atom.omos;} + | atom[null,false] {$omos = $atom.omos;} | subrule {$omos = $subrule.omos;} | ACTION {$omos = controller.action($ACTION);} | SEMPRED {$omos = controller.sempred($SEMPRED);} | ^(ACTION elementOptions) {$omos = controller.action($ACTION);} | ^(SEMPRED elementOptions) {$omos = controller.sempred($SEMPRED);} - | treeSpec {$omos = DefaultOutputModelFactory.list($treeSpec.treeMatch);} ; labeledElement returns [List omos] - : ^(ASSIGN ID atom[$ID,null,false] ) {$omos = $atom.omos;} - | ^(PLUS_ASSIGN ID atom[$ID,null,false]) {$omos = $atom.omos;} - | ^(ASSIGN ID block[$ID,null,null] ) {$omos = $block.omos;} - | ^(PLUS_ASSIGN ID block[$ID,null,null]) {$omos = $block.omos;} + : ^(ASSIGN ID atom[$ID,false] ) {$omos = $atom.omos;} + | ^(PLUS_ASSIGN ID atom[$ID,false]) {$omos = $atom.omos;} + | ^(ASSIGN ID block[$ID,null] ) {$omos = $block.omos;} + | ^(PLUS_ASSIGN ID block[$ID,null]) {$omos = $block.omos;} ; -treeSpec returns [MatchTree treeMatch] -@init { - List elems = new ArrayList(); -} - : ^(TREE_BEGIN - (e=element {if ($e.omos!=null) elems.addAll($e.omos);})+ - ) - {$treeMatch = controller.tree($TREE_BEGIN, elems);} - ; - subrule returns [List omos] - : ^(astBlockSuffix block[null,null,$astBlockSuffix.start]) {$omos = $block.omos;} - | ^(OPTIONAL b=block[null,$OPTIONAL,null]) + : ^(OPTIONAL b=block[null,$OPTIONAL]) { $omos = $block.omos; } - | ( ^(op=CLOSURE b=block[null,null,null]) - | ^(op=POSITIVE_CLOSURE b=block[null,null,null]) + | ( ^(op=CLOSURE b=block[null,null]) + | ^(op=POSITIVE_CLOSURE b=block[null,null]) ) { List alts = new ArrayList(); @@ -149,17 +129,11 @@ subrule returns [List omos] SrcOp loop = controller.getEBNFBlock($op, alts); // "star it" $omos = DefaultOutputModelFactory.list(loop); } - | block[null, null,null] {$omos = $block.omos;} + | block[null, null] {$omos = $block.omos;} ; -astBlockSuffix - : ROOT - | IMPLIES - | BANG - ; - -blockSet[GrammarAST label, GrammarAST astOp, boolean invert] returns [List omos] - : ^(SET atom[null,null,false]+) {$omos = controller.set($SET, $label, $astOp, invert);} +blockSet[GrammarAST label, boolean invert] returns [List omos] + : ^(SET atom[label,invert]+) {$omos = controller.set($SET, $label, invert);} ; /* @@ -172,35 +146,32 @@ setElement // TODO: combine ROOT/BANG into one then just make new op ref'ing return value of atom/terminal... // TODO: same for NOT -atom[GrammarAST label, GrammarAST astOp, boolean invert] returns [List omos] - : ^(op=(ROOT|BANG) a=atom[$label, $op, $invert] ) {$omos = $a.omos;} - | ^(NOT a=atom[$label, $astOp, true]) {$omos = $a.omos;} +atom[GrammarAST label, boolean invert] returns [List omos] + : ^(NOT a=atom[$label, true]) {$omos = $a.omos;} | range[label] {$omos = $range.omos;} - | ^(DOT ID terminal[$label, null]) - | ^(DOT ID ruleref[$label, null]) - | ^(WILDCARD .) {$omos = controller.wildcard($WILDCARD, $label, $astOp);} - | WILDCARD {$omos = controller.wildcard($WILDCARD, $label, $astOp);} - | terminal[label, $astOp] {$omos = $terminal.omos;} - | ruleref[label, $astOp] {$omos = $ruleref.omos;} - | blockSet[$label, $astOp, invert] {$omos = $blockSet.omos;} + | ^(DOT ID terminal[$label]) + | ^(DOT ID ruleref[$label]) + | ^(WILDCARD .) {$omos = controller.wildcard($WILDCARD, $label);} + | WILDCARD {$omos = controller.wildcard($WILDCARD, $label);} + | terminal[label] {$omos = $terminal.omos;} + | ruleref[label] {$omos = $ruleref.omos;} + | blockSet[$label, invert] {$omos = $blockSet.omos;} ; -ruleref[GrammarAST label, GrammarAST astOp] returns [List omos] - : ^(RULE_REF ARG_ACTION?) {$omos = controller.ruleRef($RULE_REF, $label, $ARG_ACTION, $astOp);} +ruleref[GrammarAST label] returns [List omos] + : ^(RULE_REF ARG_ACTION?) {$omos = controller.ruleRef($RULE_REF, $label, $ARG_ACTION);} ; range[GrammarAST label] returns [List omos] : ^(RANGE a=STRING_LITERAL b=STRING_LITERAL) ; -terminal[GrammarAST label, GrammarAST astOp] returns [List omos] - : ^(STRING_LITERAL .) {$omos = controller.stringRef($STRING_LITERAL, $label, $astOp);} - | STRING_LITERAL {$omos = controller.stringRef($STRING_LITERAL, $label, $astOp);} - | ^(TOKEN_REF ARG_ACTION .) {$omos = controller.tokenRef($TOKEN_REF, $label, $ARG_ACTION, $astOp);} - | ^(TOKEN_REF .) {$omos = controller.tokenRef($TOKEN_REF, $label, null, $astOp);} - | TOKEN_REF {$omos = controller.tokenRef($TOKEN_REF, $label, null, $astOp);} - | DOWN_TOKEN {$omos = controller.tokenRef($DOWN_TOKEN, null, null, null);} - | UP_TOKEN {$omos = controller.tokenRef($UP_TOKEN, null, null, null);} +terminal[GrammarAST label] returns [List omos] + : ^(STRING_LITERAL .) {$omos = controller.stringRef($STRING_LITERAL, $label);} + | STRING_LITERAL {$omos = controller.stringRef($STRING_LITERAL, $label);} + | ^(TOKEN_REF ARG_ACTION .) {$omos = controller.tokenRef($TOKEN_REF, $label, $ARG_ACTION);} + | ^(TOKEN_REF .) {$omos = controller.tokenRef($TOKEN_REF, $label, null);} + | TOKEN_REF {$omos = controller.tokenRef($TOKEN_REF, $label, null);} ; elementOptions @@ -211,131 +182,5 @@ elementOption : ID | ^(ASSIGN ID ID) | ^(ASSIGN ID STRING_LITERAL) - | ^(ASSIGN ID DOUBLE_QUOTE_STRING_LITERAL) | ^(ASSIGN ID ACTION) ; - -// R E W R I T E S T U F F - -rewrite returns [Rewrite code] - : { - controller.treeLevel = 0; - controller.codeBlockLevel++; - $code = controller.treeRewrite($start); - CodeBlock save = controller.getCurrentBlock(); - controller.setCurrentBlock($code); - } - ( (p=predicatedRewrite {$code.alts.add($p.alt);})+ - r=nakedRewrite {$code.alts.add($r.alt);} - | r=nakedRewrite {$code.alts.add($r.alt);} - ) - { - controller.setCurrentBlock(save); - controller.codeBlockLevel--; - } - ; - -predicatedRewrite returns [RewriteChoice alt] - : ^(ST_RESULT SEMPRED rewriteSTAlt) - | ^(RESULT SEMPRED rewriteTreeAlt) {$alt = controller.rewrite_choice((PredAST)$SEMPRED, $rewriteTreeAlt.omos);} - ; - -nakedRewrite returns [RewriteChoice alt] - : ^(ST_RESULT rewriteSTAlt) - | ^(RESULT rewriteTreeAlt) {$alt = controller.rewrite_choice(null, $rewriteTreeAlt.omos);} - ; - -rewriteTreeAlt returns [List omos] - : ^(REWRITE_SEQ - {List elems = new ArrayList();} - ( rewriteTreeElement {elems.addAll($rewriteTreeElement.omos);} )+ - ) - {$omos = elems;} - | ETC - | EPSILON {$omos = controller.rewrite_epsilon($EPSILON);} - ; - -rewriteTreeElement returns [List omos] - : rewriteTreeAtom[false] {$omos = $rewriteTreeAtom.omos;} - | rewriteTree {$omos = $rewriteTree.omos;} - | rewriteTreeEbnf {$omos = DefaultOutputModelFactory.list($rewriteTreeEbnf.op);} - ; - -rewriteTreeAtom[boolean isRoot] returns [List omos] - : ^(TOKEN_REF elementOptions ARG_ACTION) {$omos = controller.rewrite_tokenRef($TOKEN_REF, $isRoot, (ActionAST)$ARG_ACTION);} - | ^(TOKEN_REF elementOptions) {$omos = controller.rewrite_tokenRef($TOKEN_REF, $isRoot, null);} - | ^(TOKEN_REF ARG_ACTION) {$omos = controller.rewrite_tokenRef($TOKEN_REF, $isRoot, (ActionAST)$ARG_ACTION);} - | TOKEN_REF {$omos = controller.rewrite_tokenRef($TOKEN_REF, $isRoot, null);} - | RULE_REF {$omos = controller.rewrite_ruleRef($RULE_REF, $isRoot);} - | ^(STRING_LITERAL elementOptions) {$omos = controller.rewrite_stringRef($STRING_LITERAL, $isRoot);} - | STRING_LITERAL {$omos = controller.rewrite_stringRef($STRING_LITERAL, $isRoot);} - | LABEL {$omos = controller.rewrite_labelRef($LABEL, $isRoot);} - | ACTION {$omos = controller.rewrite_action((ActionAST)$ACTION, $isRoot);} - ; - -rewriteTreeEbnf returns [CodeBlock op] - : ^( (a=OPTIONAL|a=CLOSURE|a=POSITIVE_CLOSURE) - ^( REWRITE_BLOCK - { - controller.codeBlockLevel++; - if ( $a.getType()==OPTIONAL ) $op = controller.rewrite_optional($start); - else $op = controller.rewrite_closure($start); - CodeBlock save = controller.getCurrentBlock(); - controller.setCurrentBlock($op); - } - talt=rewriteTreeAlt - ) - ) - { - $op.addOps($talt.omos); - controller.setCurrentBlock(save); - controller.codeBlockLevel--; - } - ; - -rewriteTree returns [List omos] - : { - controller.treeLevel++; - List elems = new ArrayList(); - RewriteTreeStructure t = controller.rewrite_treeStructure($start); - } - ^( TREE_BEGIN - rewriteTreeAtom[true] {elems.addAll($rewriteTreeAtom.omos);} - ( rewriteTreeElement {elems.addAll($rewriteTreeElement.omos);} )* - ) - { - t.ops = elems; - $omos = DefaultOutputModelFactory.list(t); - controller.treeLevel--; - } - ; - -rewriteSTAlt returns [List omos] - : rewriteTemplate - | ETC - | EPSILON - ; - -rewriteTemplate returns [List omos] - : ^(TEMPLATE rewriteTemplateArgs? DOUBLE_QUOTE_STRING_LITERAL) - | ^(TEMPLATE rewriteTemplateArgs? DOUBLE_ANGLE_STRING_LITERAL) - | rewriteTemplateRef - | rewriteIndirectTemplateHead - | ACTION - ; - -rewriteTemplateRef returns [List omos] - : ^(TEMPLATE ID rewriteTemplateArgs?) - ; - -rewriteIndirectTemplateHead returns [List omos] - : ^(TEMPLATE ACTION rewriteTemplateArgs?) - ; - -rewriteTemplateArgs returns [List omos] - : ^(ARGLIST rewriteTemplateArg+) - ; - -rewriteTemplateArg returns [List omos] - : ^(ARG ID ACTION) - ; diff --git a/tool/src/org/antlr/v4/codegen/Target.java b/tool/src/org/antlr/v4/codegen/Target.java index 967e6ddec..1abcf8448 100644 --- a/tool/src/org/antlr/v4/codegen/Target.java +++ b/tool/src/org/antlr/v4/codegen/Target.java @@ -392,21 +392,6 @@ public class Target { return st.render(); } - // AST - - public String getRootName(int level) { - ST st = gen.templates.getInstanceOf("RootName"); - st.add("level", level); - return st.render(); - } - - public String getRewriteIteratorName(GrammarAST elem, int level) { - ST st = gen.templates.getInstanceOf("RewriteIteratorName"); - st.add("elemName", getElementName(elem.getText())); - st.add("level", level); - return st.render(); - } - public String getElementListName(String name) { ST st = gen.templates.getInstanceOf("ElementListName"); st.add("elemName", getElementName(name)); diff --git a/tool/src/org/antlr/v4/codegen/TreeParserFactory.java b/tool/src/org/antlr/v4/codegen/TreeParserFactory.java deleted file mode 100644 index 8dea27379..000000000 --- a/tool/src/org/antlr/v4/codegen/TreeParserFactory.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen; - -import org.antlr.v4.codegen.model.MatchTree; -import org.antlr.v4.codegen.model.SrcOp; -import org.antlr.v4.codegen.model.TreeParserModel; -import org.antlr.v4.codegen.model.decl.Decl; -import org.antlr.v4.codegen.model.decl.NodeDecl; -import org.antlr.v4.codegen.model.decl.NodeListDecl; -import org.antlr.v4.codegen.model.decl.TokenListDecl; -import org.antlr.v4.tool.ast.GrammarAST; - -import java.util.List; - -public class TreeParserFactory extends ParserFactory { - public TreeParserFactory(CodeGenerator gen) { - super(gen); - } - - @Override - public TreeParserFile treeParserFile(String fileName) { - return new TreeParserFile(this, fileName); - } - - @Override - public TreeParserModel treeParser(TreeParserFile file) { - return new TreeParserModel(this, file); - } - - @Override - public MatchTree tree(GrammarAST treeBeginAST, List omos) { - return new MatchTree(this, treeBeginAST, omos); - } - - @Override - public Decl getTokenLabelDecl(String label) { - return new NodeDecl(this, label); - } - - @Override - public TokenListDecl getTokenListLabelDecl(String label) { - return new NodeListDecl(this, gen.target.getListLabel(label)); - } -} diff --git a/tool/src/org/antlr/v4/codegen/TreeParserFile.java b/tool/src/org/antlr/v4/codegen/TreeParserFile.java deleted file mode 100644 index a3f0b2c85..000000000 --- a/tool/src/org/antlr/v4/codegen/TreeParserFile.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen; - -import org.antlr.v4.codegen.model.ParserFile; - -public class TreeParserFile extends ParserFile { - public TreeParserFile(OutputModelFactory factory, String fileName) { - super(factory, fileName); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/AddToLabelList.java b/tool/src/org/antlr/v4/codegen/model/AddToLabelList.java index e5691ef0f..04af8fe81 100644 --- a/tool/src/org/antlr/v4/codegen/model/AddToLabelList.java +++ b/tool/src/org/antlr/v4/codegen/model/AddToLabelList.java @@ -30,15 +30,16 @@ package org.antlr.v4.codegen.model; import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.ast.ElementASTOp; import org.antlr.v4.codegen.model.decl.Decl; /** */ -public class AddToLabelList extends ElementASTOp { +public class AddToLabelList extends SrcOp { + public Decl label; public String listName; public AddToLabelList(OutputModelFactory factory, String listName, Decl label) { - super(factory, null, label); + super(factory); + this.label = label; this.listName = listName; } } diff --git a/tool/src/org/antlr/v4/codegen/model/Choice.java b/tool/src/org/antlr/v4/codegen/model/Choice.java index 7f125ff0f..478334147 100644 --- a/tool/src/org/antlr/v4/codegen/model/Choice.java +++ b/tool/src/org/antlr/v4/codegen/model/Choice.java @@ -30,12 +30,14 @@ package org.antlr.v4.codegen.model; import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.decl.*; +import org.antlr.v4.codegen.model.decl.Decl; +import org.antlr.v4.codegen.model.decl.TokenTypeDecl; import org.antlr.v4.misc.Utils; import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.tool.ast.GrammarAST; -import java.util.*; +import java.util.ArrayList; +import java.util.List; /** The class hierarchy underneath SrcOp is pretty deep but makes sense that, * for example LL1StarBlock is a kind of LL1Loop which is a kind of Choice. @@ -91,11 +93,6 @@ public abstract class Choice extends RuleElement { public ThrowNoViableAlt getThrowNoViableAlt(OutputModelFactory factory, GrammarAST blkAST, IntervalSet expecting) { - if ( factory.getGrammar().isTreeGrammar() ) { - return new ThrowNoViableTreeAlt(factory, blkAST, expecting); - } - else { - return new ThrowNoViableAlt(factory, blkAST, expecting); - } + return new ThrowNoViableAlt(factory, blkAST, expecting); } } diff --git a/tool/src/org/antlr/v4/codegen/model/MatchDOWN.java b/tool/src/org/antlr/v4/codegen/model/MatchDOWN.java deleted file mode 100644 index defffb3e7..000000000 --- a/tool/src/org/antlr/v4/codegen/model/MatchDOWN.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.tool.ast.GrammarAST; - -public class MatchDOWN extends RuleElement { - public MatchDOWN(OutputModelFactory factory, GrammarAST ast) { - super(factory, ast); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/MatchTree.java b/tool/src/org/antlr/v4/codegen/model/MatchTree.java deleted file mode 100644 index ba8864d2f..000000000 --- a/tool/src/org/antlr/v4/codegen/model/MatchTree.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.misc.Utils; -import org.antlr.v4.runtime.ParserRuleContext; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.atn.ATNState; -import org.antlr.v4.runtime.atn.LL1Analyzer; -import org.antlr.v4.runtime.misc.IntervalSet; -import org.antlr.v4.tool.ast.GrammarAST; -import org.antlr.v4.tool.ast.TreePatternAST; - -import java.util.List; - -public class MatchTree extends RuleElement { - public boolean isNullable; - - @ModelElement public SrcOp root; - @ModelElement public List leftActions; - @ModelElement public SrcOp down; - @ModelElement public List kids; - @ModelElement public SrcOp up; - @ModelElement public List rightActions; - - public MatchTree(OutputModelFactory factory, GrammarAST ast, List elems) { - super(factory, ast); - TreePatternAST rootNode = (TreePatternAST)ast; - this.isNullable = isNullable(rootNode); - List afterRoot = elems.subList(1, elems.size()); - int downIndex = - Utils.indexOf(afterRoot, new Utils.Filter() { - public boolean select(SrcOp op) { - return op instanceof MatchToken && ((MatchToken)op).ttype==Token.DOWN; - } - }); - downIndex++; // we skipped root - down = elems.get(downIndex); - int upIndex = - Utils.lastIndexOf(elems, new Utils.Filter() { - public boolean select(SrcOp op) { - return op instanceof MatchToken && ((MatchToken) op).ttype == Token.UP; - } - }); - up = elems.get(upIndex); - root = elems.get(0); - leftActions = elems.subList(1, downIndex); - rightActions = elems.subList(upIndex+1, elems.size()); - this.kids = elems.subList(downIndex+1, upIndex); - } - - boolean isNullable(TreePatternAST rootNode) { - ATNState firstChildState = rootNode.downState.transition(0).target; - LL1Analyzer analyzer = new LL1Analyzer(firstChildState.atn); - IntervalSet look = analyzer.LOOK(firstChildState, ParserRuleContext.EMPTY); - return look.contains(Token.UP); - } - -} diff --git a/tool/src/org/antlr/v4/codegen/model/MatchUP.java b/tool/src/org/antlr/v4/codegen/model/MatchUP.java deleted file mode 100644 index cd3435103..000000000 --- a/tool/src/org/antlr/v4/codegen/model/MatchUP.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.tool.ast.GrammarAST; - -public class MatchUP extends RuleElement { - public MatchUP(OutputModelFactory factory, GrammarAST ast) { - super(factory, ast); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/OutputFile.java b/tool/src/org/antlr/v4/codegen/model/OutputFile.java index 437949c93..ce51295ae 100644 --- a/tool/src/org/antlr/v4/codegen/model/OutputFile.java +++ b/tool/src/org/antlr/v4/codegen/model/OutputFile.java @@ -35,7 +35,6 @@ import org.antlr.v4.tool.Grammar; public abstract class OutputFile extends OutputModelObject { public final String fileName; public final String TokenLabelType; - public final String ASTLabelType; public final String InputSymbolType; public OutputFile(OutputModelFactory factory, String fileName) { @@ -43,8 +42,6 @@ public abstract class OutputFile extends OutputModelObject { this.fileName = fileName; Grammar g = factory.getGrammar(); TokenLabelType = g.getOptionString("TokenLabelType"); - ASTLabelType = g.getOptionString("ASTLabelType", "CommonAST"); - if ( g.isTreeGrammar() ) InputSymbolType = ASTLabelType; - else InputSymbolType = TokenLabelType; + InputSymbolType = TokenLabelType; } } diff --git a/tool/src/org/antlr/v4/codegen/model/Rewrite.java b/tool/src/org/antlr/v4/codegen/model/Rewrite.java deleted file mode 100644 index 622092c60..000000000 --- a/tool/src/org/antlr/v4/codegen/model/Rewrite.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.ast.RewriteChoice; -import org.antlr.v4.codegen.model.decl.CodeBlock; - -import java.util.*; - -/** Either an ST or Tree rewrite */ -public class Rewrite extends CodeBlock { - @ModelElement public List alts = new ArrayList(); - - public Rewrite(OutputModelFactory factory, int treeLevel, int codeBlockLevel) { - super(factory, treeLevel, codeBlockLevel); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java index ac51a46ce..aa1986a08 100644 --- a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java +++ b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java @@ -30,11 +30,14 @@ package org.antlr.v4.codegen.model; import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.decl.*; +import org.antlr.v4.codegen.model.decl.AltLabelStructDecl; +import org.antlr.v4.codegen.model.decl.Decl; +import org.antlr.v4.codegen.model.decl.StructDecl; import org.antlr.v4.misc.Utils; import org.antlr.v4.runtime.atn.ATNState; import org.antlr.v4.runtime.misc.OrderedHashSet; -import org.antlr.v4.tool.*; +import org.antlr.v4.tool.Attribute; +import org.antlr.v4.tool.Rule; import org.antlr.v4.tool.ast.GrammarAST; import java.util.*; @@ -73,9 +76,7 @@ public class RuleFunction extends OutputModelObject { index = r.index; - ruleCtx = r.g.isTreeGrammar() ? - new TreeParserStructDecl(factory, r) : - new StructDecl(factory, r); + ruleCtx = new StructDecl(factory, r); List labels = r.getAltLabels(); if ( labels!=null ) { diff --git a/tool/src/org/antlr/v4/codegen/model/ThrowNoViableTreeAlt.java b/tool/src/org/antlr/v4/codegen/model/ThrowNoViableTreeAlt.java deleted file mode 100644 index 488ab5295..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ThrowNoViableTreeAlt.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.runtime.misc.IntervalSet; -import org.antlr.v4.tool.ast.GrammarAST; - -public class ThrowNoViableTreeAlt extends ThrowNoViableAlt { - public ThrowNoViableTreeAlt(OutputModelFactory factory, - GrammarAST blkOrEbnfRootAST, - IntervalSet expecting) - { - super(factory, blkOrEbnfRootAST, expecting); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/TreeParserModel.java b/tool/src/org/antlr/v4/codegen/model/TreeParserModel.java deleted file mode 100644 index 07280f920..000000000 --- a/tool/src/org/antlr/v4/codegen/model/TreeParserModel.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.actions.ActionText; -import org.antlr.v4.codegen.model.actions.DefaultTreeParserSuperClass; -import org.antlr.v4.tool.Grammar; - -public class TreeParserModel extends Parser { - public TreeParserModel(OutputModelFactory factory, ParserFile file) { - super(factory, file); - Grammar g = factory.getGrammar(); - if (g.getOptionString("superClass") != null) { - superclass = new ActionText(g.getOptionString("superClass")); - } else { - superclass = new DefaultTreeParserSuperClass(); - } - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/actions/DefaultTreeParserSuperClass.java b/tool/src/org/antlr/v4/codegen/model/actions/DefaultTreeParserSuperClass.java deleted file mode 100644 index 1b4316504..000000000 --- a/tool/src/org/antlr/v4/codegen/model/actions/DefaultTreeParserSuperClass.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.actions; - -public class DefaultTreeParserSuperClass extends ActionChunk { -} diff --git a/tool/src/org/antlr/v4/codegen/model/actions/ParserASTExtensionMembers.java b/tool/src/org/antlr/v4/codegen/model/actions/ParserASTExtensionMembers.java deleted file mode 100644 index d335c15f7..000000000 --- a/tool/src/org/antlr/v4/codegen/model/actions/ParserASTExtensionMembers.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.actions; - -public class ParserASTExtensionMembers extends ActionChunk { -} diff --git a/tool/src/org/antlr/v4/codegen/model/actions/RulePropertyRef_st.java b/tool/src/org/antlr/v4/codegen/model/actions/RulePropertyRef_st.java deleted file mode 100644 index 570c34de8..000000000 --- a/tool/src/org/antlr/v4/codegen/model/actions/RulePropertyRef_st.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.actions; - -/** */ -public class RulePropertyRef_st extends RulePropertyRef { - public RulePropertyRef_st(String label) { - super(label); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/actions/RulePropertyRef_tree.java b/tool/src/org/antlr/v4/codegen/model/actions/RulePropertyRef_tree.java deleted file mode 100644 index 18d139d89..000000000 --- a/tool/src/org/antlr/v4/codegen/model/actions/RulePropertyRef_tree.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.actions; - -/** */ -public class RulePropertyRef_tree extends RulePropertyRef { - public RulePropertyRef_tree(String label) { - super(label); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/actions/ThisRulePropertyRef_st.java b/tool/src/org/antlr/v4/codegen/model/actions/ThisRulePropertyRef_st.java deleted file mode 100644 index b022eb74a..000000000 --- a/tool/src/org/antlr/v4/codegen/model/actions/ThisRulePropertyRef_st.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.actions; - -/** */ -public class ThisRulePropertyRef_st extends RulePropertyRef { - public ThisRulePropertyRef_st(String label) { - super(label); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/actions/ThisRulePropertyRef_tree.java b/tool/src/org/antlr/v4/codegen/model/actions/ThisRulePropertyRef_tree.java deleted file mode 100644 index 9a37634ec..000000000 --- a/tool/src/org/antlr/v4/codegen/model/actions/ThisRulePropertyRef_tree.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.actions; - -/** */ -public class ThisRulePropertyRef_tree extends RulePropertyRef { - public ThisRulePropertyRef_tree(String label) { - super(label); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/actions/TokenPropertyRef_tree.java b/tool/src/org/antlr/v4/codegen/model/actions/TokenPropertyRef_tree.java deleted file mode 100644 index eb4e34b27..000000000 --- a/tool/src/org/antlr/v4/codegen/model/actions/TokenPropertyRef_tree.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.actions; - -/** */ -public class TokenPropertyRef_tree extends TokenPropertyRef { - public TokenPropertyRef_tree(String label) { - super(label); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/AddChild.java b/tool/src/org/antlr/v4/codegen/model/ast/AddChild.java deleted file mode 100644 index 844d1c0f4..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/AddChild.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.*; - -public class AddChild extends SrcOp { - public String rootName; - @ModelElement public SrcOp child; - - public AddChild(OutputModelFactory factory, String rootName, SrcOp child) { - super(factory); - this.rootName = rootName; - this.child = child; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/AssignTreeResult.java b/tool/src/org/antlr/v4/codegen/model/ast/AssignTreeResult.java deleted file mode 100644 index a9b999a2b..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/AssignTreeResult.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.SrcOp; - -public class AssignTreeResult extends SrcOp { - public AssignTreeResult(OutputModelFactory factory) { - super(factory); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/BecomeRoot.java b/tool/src/org/antlr/v4/codegen/model/ast/BecomeRoot.java deleted file mode 100644 index 008175217..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/BecomeRoot.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.*; - -public class BecomeRoot extends SrcOp { - public String rootName; - @ModelElement public SrcOp newRoot; - - public BecomeRoot(OutputModelFactory factory, String rootName, SrcOp newRoot) { - super(factory); - this.rootName = rootName; - this.newRoot = newRoot; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/ClearElementList.java b/tool/src/org/antlr/v4/codegen/model/ast/ClearElementList.java deleted file mode 100644 index 5318d4352..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/ClearElementList.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.SrcOp; -import org.antlr.v4.tool.ast.GrammarAST; - -public class ClearElementList extends SrcOp { - public String name; - - public ClearElementList(OutputModelFactory factory, GrammarAST ast, String name) { - super(factory, ast); - this.name = name; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/ElementASTOp.java b/tool/src/org/antlr/v4/codegen/model/ast/ElementASTOp.java deleted file mode 100644 index 8f736dcb7..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/ElementASTOp.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.SrcOp; -import org.antlr.v4.codegen.model.decl.Decl; -import org.antlr.v4.tool.ast.GrammarAST; - -public class ElementASTOp extends SrcOp { - public Decl label; - - public ElementASTOp(OutputModelFactory factory, GrammarAST ast, Decl label) { - super(factory, ast); - this.label = label; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/ParserASTContextInterface.java b/tool/src/org/antlr/v4/codegen/model/ast/ParserASTContextInterface.java deleted file mode 100644 index e69d9d6e3..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/ParserASTContextInterface.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.model.OutputModelObject; - -public class ParserASTContextInterface extends OutputModelObject { -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/ParserASTContextMembers.java b/tool/src/org/antlr/v4/codegen/model/ast/ParserASTContextMembers.java deleted file mode 100644 index 2ebf07c60..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/ParserASTContextMembers.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.model.OutputModelObject; - -public class ParserASTContextMembers extends OutputModelObject { -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteAction.java b/tool/src/org/antlr/v4/codegen/model/ast/RewriteAction.java deleted file mode 100644 index 9d3fb5787..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteAction.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.ActionTranslator; -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.ModelElement; -import org.antlr.v4.codegen.model.SrcOp; -import org.antlr.v4.codegen.model.actions.ActionChunk; -import org.antlr.v4.tool.ast.ActionAST; - -import java.util.List; - -public class RewriteAction extends SrcOp { - @ModelElement public List chunks; - - public RewriteAction(OutputModelFactory factory, ActionAST ast) { - super(factory, ast); - if ( ast!=null ) { - chunks = ActionTranslator.translateAction(factory, - factory.getCurrentRuleFunction(), - ast.token, ast); - } - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteChoice.java b/tool/src/org/antlr/v4/codegen/model/ast/RewriteChoice.java deleted file mode 100644 index 845091607..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteChoice.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.*; - -import java.util.List; - -/** A collection of operations possibly with a predicate - * that indicates whether to apply the rewrite. - * This is a single alternative and a list of predicated choices. - */ -public class RewriteChoice extends SrcOp { - @ModelElement public SrcOp predicate; - @ModelElement public List ops; - - public RewriteChoice(OutputModelFactory factory, SrcOp predicate, List ops) { - super(factory); - this.predicate = predicate; - this.ops = ops; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteImagTokenRef.java b/tool/src/org/antlr/v4/codegen/model/ast/RewriteImagTokenRef.java deleted file mode 100644 index 5bae6da72..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteImagTokenRef.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.ActionTranslator; -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.ModelElement; -import org.antlr.v4.codegen.model.SrcOp; -import org.antlr.v4.codegen.model.actions.ActionChunk; -import org.antlr.v4.tool.ast.ActionAST; -import org.antlr.v4.tool.ast.GrammarAST; - -import java.util.List; - -public class RewriteImagTokenRef extends SrcOp { - public String tokenType; - - @ModelElement public List argChunks; - - public RewriteImagTokenRef(OutputModelFactory factory, - GrammarAST ast, - String tokenType, - ActionAST argAST) - { - super(factory, ast); - this.tokenType = tokenType; - if ( argAST!=null ) { - argChunks = ActionTranslator.translateAction(factory, - factory.getCurrentRuleFunction(), - argAST.token, argAST); - } - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteIteratorInit.java b/tool/src/org/antlr/v4/codegen/model/ast/RewriteIteratorInit.java deleted file mode 100644 index 019feebc8..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteIteratorInit.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.SrcOp; -import org.antlr.v4.codegen.model.decl.RewriteIteratorDecl; - -public class RewriteIteratorInit extends SrcOp { - public RewriteIteratorDecl decl; - public RewriteIteratorInit(OutputModelFactory factory, RewriteIteratorDecl decl) { - super(factory); - this.decl = decl; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteLabelRef.java b/tool/src/org/antlr/v4/codegen/model/ast/RewriteLabelRef.java deleted file mode 100644 index 744f344b6..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteLabelRef.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.SrcOp; -import org.antlr.v4.tool.ast.GrammarAST; - -public class RewriteLabelRef extends SrcOp { - public String iterName; - - public RewriteLabelRef(OutputModelFactory factory, GrammarAST ast, - String iterName) { - super(factory, ast); - this.iterName = iterName; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteRuleRef.java b/tool/src/org/antlr/v4/codegen/model/ast/RewriteRuleRef.java deleted file mode 100644 index ba6cb8239..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteRuleRef.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.SrcOp; -import org.antlr.v4.tool.ast.GrammarAST; - -public class RewriteRuleRef extends SrcOp { - public String iterName; - - public RewriteRuleRef(OutputModelFactory factory, GrammarAST ast, - String iterName) - { - super(factory, ast); - this.iterName = iterName; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteSelfRuleLabelRef.java b/tool/src/org/antlr/v4/codegen/model/ast/RewriteSelfRuleLabelRef.java deleted file mode 100644 index 042391eba..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteSelfRuleLabelRef.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.SrcOp; -import org.antlr.v4.tool.ast.GrammarAST; - -public class RewriteSelfRuleLabelRef extends SrcOp { - public RewriteSelfRuleLabelRef(OutputModelFactory factory, GrammarAST ast) { - super(factory, ast); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteTokenRef.java b/tool/src/org/antlr/v4/codegen/model/ast/RewriteTokenRef.java deleted file mode 100644 index 8ba987f88..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteTokenRef.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.SrcOp; -import org.antlr.v4.tool.ast.GrammarAST; - -public class RewriteTokenRef extends SrcOp { - public String iterName; - public RewriteTokenRef(OutputModelFactory factory, GrammarAST ast, - String iterName) - { - super(factory, ast); - this.iterName = iterName; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteTreeClosure.java b/tool/src/org/antlr/v4/codegen/model/ast/RewriteTreeClosure.java deleted file mode 100644 index e250d692a..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteTreeClosure.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.decl.CodeBlock; -import org.antlr.v4.codegen.model.decl.Decl; -import org.antlr.v4.tool.ast.GrammarAST; - -import java.util.ArrayList; -import java.util.List; - -public class RewriteTreeClosure extends CodeBlock { - public List iteratorDecls = new ArrayList(); - - public RewriteTreeClosure(OutputModelFactory factory, GrammarAST ast, - int treeLevel, int codeBlockLevel) - { - super(factory, treeLevel, codeBlockLevel); - this.ast = ast; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteTreeOptional.java b/tool/src/org/antlr/v4/codegen/model/ast/RewriteTreeOptional.java deleted file mode 100644 index 78cad7e29..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteTreeOptional.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.decl.CodeBlock; -import org.antlr.v4.codegen.model.decl.Decl; -import org.antlr.v4.tool.ast.GrammarAST; - -import java.util.ArrayList; -import java.util.List; - -public class RewriteTreeOptional extends CodeBlock { - public List conditionalDecls = new ArrayList(); - - public RewriteTreeOptional(OutputModelFactory factory, GrammarAST ast, - int treeLevel, int codeBlockLevel) { - super(factory, treeLevel, codeBlockLevel); - this.ast = ast; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteTreeStructure.java b/tool/src/org/antlr/v4/codegen/model/ast/RewriteTreeStructure.java deleted file mode 100644 index 0d67dbacf..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteTreeStructure.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.ModelElement; -import org.antlr.v4.codegen.model.SrcOp; -import org.antlr.v4.codegen.model.decl.Decl; -import org.antlr.v4.runtime.misc.OrderedHashSet; -import org.antlr.v4.tool.ast.GrammarAST; - -import java.util.List; - -/** ^(A B C) */ -public class RewriteTreeStructure extends SrcOp { - public int treeLevel; - public int codeBlockLevel; - - @ModelElement public List ops; - @ModelElement public OrderedHashSet locals; - - public RewriteTreeStructure(OutputModelFactory factory, - GrammarAST ast, - int treeLevel, - int codeBlockLevel) - { - super(factory, ast); - this.treeLevel = treeLevel; - this.codeBlockLevel = codeBlockLevel; - } - - /** Add local var decl */ - public void addLocalDecl(Decl d) { - if ( locals==null ) locals = new OrderedHashSet(); - locals.add(d); - d.isLocal = true; - } - - public int getEnclosingTreeLevel() { return treeLevel - 1; } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RuleAST.java b/tool/src/org/antlr/v4/codegen/model/ast/RuleAST.java deleted file mode 100644 index 247762c9b..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RuleAST.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.decl.Decl; -import org.antlr.v4.tool.ast.GrammarAST; - -public class RuleAST extends ElementASTOp { - public RuleAST(OutputModelFactory factory, GrammarAST ast, Decl label) { - super(factory, ast, label); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RuleASTCleanup.java b/tool/src/org/antlr/v4/codegen/model/ast/RuleASTCleanup.java deleted file mode 100644 index be51b15ca..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RuleASTCleanup.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.SrcOp; - -public class RuleASTCleanup extends SrcOp { - public RuleASTCleanup(OutputModelFactory factory) { - super(factory); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/TemplateRewrite.java b/tool/src/org/antlr/v4/codegen/model/ast/TemplateRewrite.java deleted file mode 100644 index 97d7054b3..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/TemplateRewrite.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.Rewrite; - -public class TemplateRewrite extends Rewrite { - public TemplateRewrite(OutputModelFactory factory, int treeLevel, int codeBlockLevel) { - super(factory, treeLevel, codeBlockLevel); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/TokenAST.java b/tool/src/org/antlr/v4/codegen/model/ast/TokenAST.java deleted file mode 100644 index 1053e3c7b..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/TokenAST.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.decl.Decl; -import org.antlr.v4.tool.ast.GrammarAST; - -/** */ -public class TokenAST extends ElementASTOp { - public TokenAST(OutputModelFactory factory, GrammarAST ast, Decl label) { - super(factory, ast, label); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/TrackRuleElement.java b/tool/src/org/antlr/v4/codegen/model/ast/TrackRuleElement.java deleted file mode 100644 index f5ada7ba1..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/TrackRuleElement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.decl.Decl; -import org.antlr.v4.tool.ast.GrammarAST; - -public class TrackRuleElement extends ElementASTOp { - public String name; - public TrackRuleElement(OutputModelFactory factory, GrammarAST ast, String trackName, Decl label) { - super(factory, ast, label); - name = trackName; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/TrackTokenElement.java b/tool/src/org/antlr/v4/codegen/model/ast/TrackTokenElement.java deleted file mode 100644 index b7a5dc0b6..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/TrackTokenElement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.decl.Decl; -import org.antlr.v4.tool.ast.GrammarAST; - -public class TrackTokenElement extends ElementASTOp { - public String name; - public TrackTokenElement(OutputModelFactory factory, GrammarAST ast, String trackName, Decl label) { - super(factory, ast, label); - name = trackName; - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/TreeRewrite.java b/tool/src/org/antlr/v4/codegen/model/ast/TreeRewrite.java deleted file mode 100644 index b8b7af837..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/TreeRewrite.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.Rewrite; - -public class TreeRewrite extends Rewrite { - public TreeRewrite(OutputModelFactory factory, int treeLevel, int codeBlockLevel) { - super(factory, treeLevel, codeBlockLevel); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/decl/NodeDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/NodeDecl.java deleted file mode 100644 index 344217631..000000000 --- a/tool/src/org/antlr/v4/codegen/model/decl/NodeDecl.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.antlr.v4.codegen.model.decl; - -import org.antlr.v4.codegen.OutputModelFactory; - -/** x=ID or implicit _tID label in tree grammar */ -public class NodeDecl extends TokenDecl { - public NodeDecl(OutputModelFactory factory, String varName) { - super(factory, varName); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/decl/NodeListDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/NodeListDecl.java deleted file mode 100644 index 0ba8b75fa..000000000 --- a/tool/src/org/antlr/v4/codegen/model/decl/NodeListDecl.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.antlr.v4.codegen.model.decl; - -import org.antlr.v4.codegen.OutputModelFactory; - -/** */ -public class NodeListDecl extends TokenListDecl { - public NodeListDecl(OutputModelFactory factory, String varName) { - super(factory, varName); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/decl/ParserASTTreeFieldDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/ParserASTTreeFieldDecl.java deleted file mode 100644 index 3d54e93a3..000000000 --- a/tool/src/org/antlr/v4/codegen/model/decl/ParserASTTreeFieldDecl.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.decl; - -import org.antlr.v4.codegen.OutputModelFactory; - -public class ParserASTTreeFieldDecl extends AttributeDecl { - public ParserASTTreeFieldDecl(OutputModelFactory factory) { - super(factory, "tree", "Unknown"); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/decl/RewriteIteratorDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/RewriteIteratorDecl.java deleted file mode 100644 index 42096c8e8..000000000 --- a/tool/src/org/antlr/v4/codegen/model/decl/RewriteIteratorDecl.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.decl; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.tool.ast.GrammarAST; - -public class RewriteIteratorDecl extends Decl { - public String listName; - public RewriteIteratorDecl(OutputModelFactory factory, - GrammarAST elem, - int codeBlockLevel) - { - super(factory, factory.getGenerator().target - .getRewriteIteratorName(elem, codeBlockLevel)); - listName = factory.getGenerator().target.getElementListName(elem.getText()); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/decl/RootDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/RootDecl.java deleted file mode 100644 index 6ba641220..000000000 --- a/tool/src/org/antlr/v4/codegen/model/decl/RootDecl.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.codegen.model.decl; - -import org.antlr.v4.codegen.OutputModelFactory; - -public class RootDecl extends Decl { - public RootDecl(OutputModelFactory factory, int treeLevel) { - super(factory, factory.getGenerator().target.getRootName(treeLevel)); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/decl/StructDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/StructDecl.java index c3a0e0b49..fb30715b6 100644 --- a/tool/src/org/antlr/v4/codegen/model/decl/StructDecl.java +++ b/tool/src/org/antlr/v4/codegen/model/decl/StructDecl.java @@ -54,9 +54,6 @@ public class StructDecl extends Decl { public StructDecl(OutputModelFactory factory, Rule r) { super(factory, factory.getGenerator().target.getRuleFunctionContextStructName(r)); - if ( !factory.getGrammar().isTreeGrammar() ) { - addVisitorDispatchMethods(r); - } } public void addVisitorDispatchMethods(Rule r) { diff --git a/tool/src/org/antlr/v4/codegen/model/decl/TreeParserStructDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/TreeParserStructDecl.java deleted file mode 100644 index ad62d7676..000000000 --- a/tool/src/org/antlr/v4/codegen/model/decl/TreeParserStructDecl.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.antlr.v4.codegen.model.decl; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.tool.Rule; - -public class TreeParserStructDecl extends StructDecl { - public TreeParserStructDecl(OutputModelFactory factory, Rule r) { - super(factory, r); - } -} diff --git a/tool/src/org/antlr/v4/parse/ANTLRLexer.g b/tool/src/org/antlr/v4/parse/ANTLRLexer.g index 1d02d0f44..a12b48246 100644 --- a/tool/src/org/antlr/v4/parse/ANTLRLexer.g +++ b/tool/src/org/antlr/v4/parse/ANTLRLexer.g @@ -82,7 +82,7 @@ options { //superclass = AbstractA3Lexer; } -tokens { SEMPRED; } +tokens { SEMPRED; TOKEN_REF; RULE_REF; } // Include the copyright in this source and also the generated source // @@ -220,14 +220,6 @@ COMMENT ; -DOUBLE_QUOTE_STRING_LITERAL - : '"' (('\\')=>'\\' . | ~'"' )* '"' - ; - -DOUBLE_ANGLE_STRING_LITERAL - : '<<' (options {greedy=false;} : . )* '>>' - ; - // -------------- // Argument specs // @@ -401,12 +393,10 @@ NESTED_ACTION OPTIONS : 'options' WSNLCHARS* '{' ; TOKENS : 'tokens' WSNLCHARS* '{' ; -SCOPE : 'scope' ; IMPORT : 'import' ; FRAGMENT : 'fragment' ; LEXER : 'lexer' ; PARSER : 'parser' ; -TREE : 'tree' ; GRAMMAR : 'grammar' ; PROTECTED : 'protected' ; PUBLIC : 'public' ; @@ -440,23 +430,20 @@ LT : '<' ; GT : '>' ; ASSIGN : '=' ; QUESTION : '?' ; -BANG : '!' ; STAR : '*' ; PLUS : '+' ; PLUS_ASSIGN : '+=' ; OR : '|' ; -ROOT : '^' ; DOLLAR : '$' ; DOT : '.' ; // can be WILDCARD or DOT in qid or imported rule ref RANGE : '..' ; ETC : '...' ; -RARROW : '->' ; -TREE_BEGIN : '^(' ; AT : '@' ; POUND : '#' ; NOT : '~' ; RBRACE : '}' ; +/* // --------------- // Token reference // @@ -477,7 +464,38 @@ TOKEN_REF RULE_REF : ('a'..'z') ('A'..'Z' | 'a'..'z' | '0'..'9' | '_')* ; + */ +/** Allow unicode rule/token names */ +ID : a=NameStartChar NameChar* + { + if ( Character.isUpperCase($a.text.charAt(0)) ) $type = TOKEN_REF; + else $type = RULE_REF; + }; + +fragment +NameChar : NameStartChar + | '0'..'9' + | '\u00B7' + | '\u0300'..'\u036F' + | '\u203F'..'\u2040' + ; + +fragment +NameStartChar + : 'A'..'Z' | 'a'..'z'|'_' + | '\u00C0'..'\u00D6' + | '\u00D8'..'\u00F6' + | '\u00F8'..'\u02FF' + | '\u0370'..'\u037D' + | '\u037F'..'\u1FFF' + | '\u200C'..'\u200D' + | '\u2070'..'\u218F' + | '\u2C00'..'\u2FEF' + | '\u3001'..'\uD7FF' + | '\uF900'..'\uFDCF' + | '\uFDF0'..'\uFFFD' + ; // ignores | ['\u10000-'\uEFFFF] ; // ---------------------------- // Literals embedded in actions @@ -657,10 +675,7 @@ WS | '\n' | '\f' )+ - { - - $channel=2; - } + {$channel=HIDDEN;} ; // A fragment rule for use in recognizing end of line in diff --git a/tool/src/org/antlr/v4/parse/ANTLRParser.g b/tool/src/org/antlr/v4/parse/ANTLRParser.g index af6793222..518514d42 100644 --- a/tool/src/org/antlr/v4/parse/ANTLRParser.g +++ b/tool/src/org/antlr/v4/parse/ANTLRParser.g @@ -64,8 +64,6 @@ tokens { RULEMODIFIERS; RULEACTIONS; BLOCK; - REWRITE_BLOCK; - REWRITE_SEQ; OPTIONAL; CLOSURE; POSITIVE_CLOSURE; @@ -90,15 +88,10 @@ tokens { // LIST; ELEMENT_OPTIONS; // TOKEN - ST_RESULT; // distinguish between ST and tree rewrites RESULT; - ALT_REWRITE; // indicate ALT is rewritten // lexer action stuff LEXER_ALT_ACTION; - - DOWN_TOKEN; // AST node representing DOWN node in tree parser code gen - UP_TOKEN; } // Include the copyright in this source and also the generated source @@ -216,9 +209,6 @@ grammarType | // A standalone parser specification t=PARSER g=GRAMMAR -> GRAMMAR[$g, "PARSER_GRAMMAR"] - | // A standalone tree parser specification - t=TREE g=GRAMMAR -> GRAMMAR[$g, "TREE_GRAMMAR"] - // A combined lexer and parser specification | g=GRAMMAR -> GRAMMAR[$g, "COMBINED_GRAMMAR"] ) @@ -269,7 +259,6 @@ optionValue // and so on. Many option values meet this description qid | STRING_LITERAL - | DOUBLE_QUOTE_STRING_LITERAL | ACTION | INT ; @@ -442,8 +431,6 @@ exceptionHandler : CATCH ARG_ACTION ACTION -> ^(CATCH ARG_ACTION ACTION) ; -// Specifies a block of code to run after the rule and any -// expcetion blocks have exceuted. finallyClause : FINALLY ACTION -> ^(FINALLY ACTION) ; @@ -606,7 +593,33 @@ lexerElement | actionElement // actions only allowed at end of outer alt actually, // but preds can be anywhere ; - + catch [RecognitionException re] { + retval.tree = (GrammarAST)adaptor.errorNode(input, retval.start, input.LT(-1), re); + int ttype = input.get(input.range()).getType(); + // look for anything that really belongs at the start of the rule minus the initial ID + if ( ttype==COLON || ttype==RETURNS || ttype==CATCH || ttype==FINALLY || ttype==AT ) { + RecognitionException missingSemi = + new v4ParserException("unterminated rule (missing ';') detected at '"+ + input.LT(1).getText()+" "+input.LT(2).getText()+"'", input); + reportError(missingSemi); + if ( ttype==CATCH || ttype==FINALLY ) { + input.seek(input.range()); // ignore what's before rule trailer stuff + } + if ( ttype==RETURNS || ttype==AT ) { // scan back looking for ID of rule header + int p = input.index(); + Token t = input.get(p); + while ( t.getType()!=RULE_REF && t.getType()!=TOKEN_REF ) { + p--; + t = input.get(p); + } + input.seek(p); + } + throw new ResyncToEndOfRuleBlock(); // make sure it goes back to rule block level to recover + } + reportError(re); + recover(input,re); + } + labeledLexerElement : id (ass=ASSIGN|ass=PLUS_ASSIGN) ( lexerAtom -> ^($ass id lexerAtom) @@ -646,11 +659,7 @@ altList alternative @init { paraphrases.push("matching alternative"); } @after { paraphrases.pop(); } - : elements - ( rewrite -> ^(ALT_REWRITE elements rewrite) - | -> elements - ) - | rewrite -> ^(ALT_REWRITE ^(ALT EPSILON) rewrite) // empty alt with rewrite + : elements -> elements | -> ^(ALT EPSILON) // empty alt ; @@ -674,10 +683,6 @@ element ) | ebnf | actionElement - | treeSpec - ( ebnfSuffix -> ^( ebnfSuffix ^(BLOCK[$treeSpec.start,"BLOCK"] ^(ALT treeSpec ) ) ) - | -> treeSpec - ) ; catch [RecognitionException re] { retval.tree = (GrammarAST)adaptor.errorNode(input, retval.start, input.LT(-1), re); @@ -722,47 +727,10 @@ actionElement labeledElement : id (ass=ASSIGN|ass=PLUS_ASSIGN) ( atom -> ^($ass id atom) - | block (op=ROOT|op=BANG)? -> {$op!=null}? ^($ass id ^($op block)) - -> ^($ass id block) + | block -> ^($ass id block) ) ; -// Tree specifying alt -// Tree grammars need to have alts that describe a tree structure they -// will walk of course. Alts for trees therefore start with ^( XXX, which -// says we will see a root node of XXX then DOWN etc -treeSpec -@after { - GrammarAST down = new DownAST(DOWN_TOKEN, $begin); - GrammarAST up = new UpAST(UP_TOKEN, $begin); - int i = 1; // skip root element - GrammarAST p = (GrammarAST)$tree.getChild(i); - while ( p.getType()==ACTION || p.getType()==SEMPRED ) { - i++; - p = (GrammarAST)$tree.getChild(i); - } - $tree.insertChild(i, down); // ADD DOWN - i = $tree.getChildCount()-1; - p = (GrammarAST)$tree.getChild(i); - while ( p.getType()==ACTION || p.getType()==SEMPRED ) { - i--; - p = (GrammarAST)$tree.getChild(i); - } - if ( i+1 >= $tree.getChildCount() ) $tree.addChild(up); - else $tree.insertChild(i+1, up); // ADD UP -} - : begin=TREE_BEGIN - // Only a subset of elements are allowed to be a root node. However - // we allow any element to appear here and reject silly ones later - // when we walk the AST. - root=element - // After the tree root we get the usual suspects, - // all members of the element set. - (kids+=element)+ - RPAREN - -> ^( TREE_BEGIN $root $kids+ ) - ; - // A block of gramamr structure optionally followed by standard EBNF // notation, or ANTLR specific notation. I.E. ? + ^ and so on ebnf @@ -778,11 +746,6 @@ ebnf // sense only to ANTLR, in the context of a grammar block. blockSuffix : ebnfSuffix // Standard EBNF - - // ANTLR Specific Suffixes - | ROOT -// | IMPLIES // We will change this to syn/sem pred in the next phase - | BANG ; ebnfSuffix @@ -815,10 +778,10 @@ atom | */ - range (ROOT^ | BANG^)? // Range x..y - only valid in lexers - | terminal (ROOT^ | BANG^)? + range // Range x..y - only valid in lexers + | terminal | ruleref - | notSet (ROOT^|BANG^)? + | notSet | wildcard ; catch [RecognitionException re] { throw re; } // pass upwards to element @@ -835,9 +798,8 @@ wildcard // Because the terminal rule is allowed to be the node // specification for the start of a tree rule, we must // later check that wildcard was not used for that. - DOT elementOptions? (astop=ROOT|astop=BANG)? - -> {astop!=null}? ^($astop ^(WILDCARD[$DOT] elementOptions?)) - -> ^(WILDCARD[$DOT] elementOptions?) + DOT elementOptions? + -> ^(WILDCARD[$DOT] elementOptions?) ; // -------------------- @@ -893,10 +855,7 @@ if ( options!=null ) { // directive to become the root node or ignore the tree produced // ruleref - : RULE_REF ARG_ACTION? - ( (op=ROOT|op=BANG) -> ^($op ^(RULE_REF ARG_ACTION?)) - | -> ^(RULE_REF ARG_ACTION?) - ) + : RULE_REF ARG_ACTION? -> ^(RULE_REF ARG_ACTION?) ; catch [RecognitionException re] { throw re; } // pass upwards to element @@ -938,138 +897,6 @@ elementOption | id ASSIGN^ optionValue ; -rewrite - : predicatedRewrite* nakedRewrite -> predicatedRewrite* nakedRewrite - ; - -predicatedRewrite - : RARROW SEMPRED rewriteAlt - -> {$rewriteAlt.isTemplate}? ^(ST_RESULT[$RARROW] SEMPRED rewriteAlt) - -> ^(RESULT[$RARROW] SEMPRED rewriteAlt) - ; - -nakedRewrite - : RARROW rewriteAlt -> {$rewriteAlt.isTemplate}? ^(ST_RESULT[$RARROW] rewriteAlt) - -> ^(RESULT[$RARROW] rewriteAlt) - ; - -// distinguish between ST and tree rewrites; for ETC/EPSILON and trees, -// rule altAndRewrite makes REWRITE root. for ST, we use ST_REWRITE -rewriteAlt returns [boolean isTemplate] -options {backtrack=true;} - : // If we are not building templates, then we must be - // building ASTs or have rewrites in a grammar that does not - // have output=AST; options. If that is the case, we will issue - // errors/warnings in the next phase, so we just eat them here - rewriteTreeAlt - - | // try to parse a template rewrite - rewriteTemplate {$isTemplate=true;} // must be 2nd so "ACTION ..." matches as tree rewrite - - | ETC - - | /* empty rewrite */ -> EPSILON - ; - -rewriteTreeAlt - : rewriteTreeElement+ -> ^(REWRITE_SEQ rewriteTreeElement+) - ; - -rewriteTreeElement - : rewriteTreeAtom - | rewriteTreeAtom ebnfSuffix -> ^( ebnfSuffix ^(REWRITE_BLOCK ^(REWRITE_SEQ rewriteTreeAtom)) ) - | rewriteTree - ( ebnfSuffix - -> ^(ebnfSuffix ^(REWRITE_BLOCK ^(REWRITE_SEQ rewriteTree)) ) - | -> rewriteTree - ) - | rewriteTreeEbnf - ; - -rewriteTreeAtom -@after { -GrammarAST options = (GrammarAST)$tree.getFirstChildWithType(ANTLRParser.ELEMENT_OPTIONS); -if ( options!=null ) { - Grammar.setNodeOptions($tree, options); -} -} - : TOKEN_REF elementOptions? ARG_ACTION? -> ^(TOKEN_REF elementOptions? ARG_ACTION?) // for imaginary nodes - | RULE_REF - | STRING_LITERAL elementOptions? -> ^(STRING_LITERAL elementOptions?) - | DOLLAR id -> LABEL[$DOLLAR,$id.text] // reference to a label in a rewrite rule - | ACTION - ; - -rewriteTreeEbnf -@init { - Token firstToken = input.LT(1); -} -@after { - $rewriteTreeEbnf.tree.getToken().setLine(firstToken.getLine()); - $rewriteTreeEbnf.tree.getToken().setCharPositionInLine(firstToken.getCharPositionInLine()); -} - : lp=LPAREN rewriteTreeAlt RPAREN rewriteEbnfSuffix - -> ^(rewriteEbnfSuffix ^(REWRITE_BLOCK[$lp,"REWRITE_BLOCK"] rewriteTreeAlt)) - ; - -rewriteEbnfSuffix - : QUESTION -> OPTIONAL[$start] - | STAR -> CLOSURE[$start] - ; - -rewriteTree - : TREE_BEGIN rewriteTreeAtom rewriteTreeElement* RPAREN - -> ^(TREE_BEGIN rewriteTreeAtom rewriteTreeElement* ) - ; - -/** Build a tree for a template rewrite: - ^(TEMPLATE (ID|ACTION) ^(ARGLIST ^(ARG ID ACTION) ...) ) - ID can be "template" keyword. If first child is ACTION then it's - an indirect template ref - - -> foo(a={...}, b={...}) - -> ({string-e})(a={...}, b={...}) // e evaluates to template name - -> {%{$ID.text}} // create literal template from string (done in ActionTranslator) - -> {st-expr} // st-expr evaluates to ST - */ -rewriteTemplate - : // -> template(a={...},...) "..." inline template - TEMPLATE LPAREN rewriteTemplateArgs RPAREN - ( str=DOUBLE_QUOTE_STRING_LITERAL | str=DOUBLE_ANGLE_STRING_LITERAL ) - -> ^(TEMPLATE[$TEMPLATE,"TEMPLATE"] rewriteTemplateArgs? $str) - - | // -> foo(a={...}, ...) - rewriteTemplateRef - - | // -> ({expr})(a={...}, ...) - rewriteIndirectTemplateHead - - | // -> {...} - ACTION - ; - -/** -> foo(a={...}, ...) */ -rewriteTemplateRef - : id LPAREN rewriteTemplateArgs RPAREN - -> ^(TEMPLATE[$LPAREN,"TEMPLATE"] id rewriteTemplateArgs?) - ; - -/** -> ({expr})(a={...}, ...) */ -rewriteIndirectTemplateHead - : lp=LPAREN ACTION RPAREN LPAREN rewriteTemplateArgs RPAREN - -> ^(TEMPLATE[$lp,"TEMPLATE"] ACTION rewriteTemplateArgs?) - ; - -rewriteTemplateArgs - : rewriteTemplateArg (COMMA rewriteTemplateArg)* - -> ^(ARGLIST rewriteTemplateArg+) - | - ; - -rewriteTemplateArg - : id ASSIGN ACTION -> ^(ARG[$ASSIGN] id ACTION) - ; - // The name of the grammar, and indeed some other grammar elements may // come through to the parser looking like a rule reference or a token // reference, hence this rule is used to pick up whichever it is and rewrite @@ -1079,7 +906,6 @@ id @after { paraphrases.pop(); } : RULE_REF ->ID[$RULE_REF] | TOKEN_REF ->ID[$TOKEN_REF] - | TEMPLATE ->ID[$TEMPLATE] // keyword ; qid diff --git a/tool/src/org/antlr/v4/parse/ATNBuilder.g b/tool/src/org/antlr/v4/parse/ATNBuilder.g index 933967fc0..6aa4b7d00 100644 --- a/tool/src/org/antlr/v4/parse/ATNBuilder.g +++ b/tool/src/org/antlr/v4/parse/ATNBuilder.g @@ -83,8 +83,7 @@ block[GrammarAST ebnfRoot] returns [ATNFactory.Handle p] alternative returns [ATNFactory.Handle p] @init {List els = new ArrayList();} - : ^(ALT_REWRITE a=alternative .*) {$p = $a.p;} - | ^(LEXER_ALT_ACTION a=alternative .*) {$p = $a.p;} + : ^(LEXER_ALT_ACTION a=alternative .*) {$p = $a.p;} | ^(ALT EPSILON) {$p = factory.epsilon($EPSILON);} | ^(ALT (e=element {els.add($e.p);})+) {$p = factory.alt(els);} ; @@ -97,12 +96,7 @@ element returns [ATNFactory.Handle p] | SEMPRED {$p = factory.sempred((PredAST)$SEMPRED);} | ^(ACTION .) {$p = factory.action((ActionAST)$ACTION);} | ^(SEMPRED .) {$p = factory.sempred((PredAST)$SEMPRED);} - | treeSpec {$p = $treeSpec.p;} - | ^(ROOT a=astOperand) {$p = $a.p;} - | ^(BANG a=astOperand) {$p = $a.p;} | ^(NOT b=blockSet[true]) {$p = $b.p;} - | DOWN_TOKEN {$p = factory.tokenRef((DownAST)$start);} - | UP_TOKEN {$p = factory.tokenRef((UpAST)$start);} | ARG_ACTION {$p = factory.charSetLiteral($start);} ; @@ -116,15 +110,8 @@ labeledElement returns [ATNFactory.Handle p] | ^(PLUS_ASSIGN ID element) {$p = factory.listLabel($element.p);} ; -treeSpec returns [ATNFactory.Handle p] -@init {List els = new ArrayList();} - : ^(TREE_BEGIN (e=element {els.add($e.p);})+ ) - {$p = factory.tree($TREE_BEGIN, els);} - ; - subrule returns [ATNFactory.Handle p] - : ^(astBlockSuffix block[null]) {$p = $block.p;} - | ^(OPTIONAL block[$start]) {$p = $block.p;} + : ^(OPTIONAL block[$start]) {$p = $block.p;} | ^(CLOSURE block[$start]) {$p = $block.p;} | ^(POSITIVE_CLOSURE block[$start]) {$p = $block.p;} | block[null] {$p = $block.p;} @@ -142,12 +129,6 @@ setElement | ^(RANGE a=STRING_LITERAL b=STRING_LITERAL) ; -astBlockSuffix - : ROOT - | IMPLIES - | BANG - ; - atom returns [ATNFactory.Handle p] : range {$p = $range.p;} | ^(DOT ID terminal) {$p = $terminal.p;} diff --git a/tool/src/org/antlr/v4/parse/BlockSetTransformer.g b/tool/src/org/antlr/v4/parse/BlockSetTransformer.g index 95cc03d47..8818ffb77 100644 --- a/tool/src/org/antlr/v4/parse/BlockSetTransformer.g +++ b/tool/src/org/antlr/v4/parse/BlockSetTransformer.g @@ -40,18 +40,7 @@ topdown setAlt : {inContext("RULE BLOCK")}? - ( ALT {currentAlt = $start; rewriteElems.clear();} - | ALT_REWRITE {currentAlt = $start;} - { - IntervalSet s = new IntervalSet(); - s.add(RULE_REF); - s.add(STRING_LITERAL); - s.add(TOKEN_REF); - List nodes = ((GrammarAST)(currentAlt.getChild(1))).getNodesWithType(s); - for (GrammarAST n : nodes) {rewriteElems.add(n.getText());} - } - ) - + ALT {currentAlt = $start; rewriteElems.clear();} ; // (BLOCK (ALT (+ (BLOCK (ALT INT) (ALT ID))))) diff --git a/tool/src/org/antlr/v4/parse/GrammarTreeVisitor.g b/tool/src/org/antlr/v4/parse/GrammarTreeVisitor.g index e09df5e55..d24b622f3 100644 --- a/tool/src/org/antlr/v4/parse/GrammarTreeVisitor.g +++ b/tool/src/org/antlr/v4/parse/GrammarTreeVisitor.g @@ -89,16 +89,12 @@ public String currentRuleName; public GrammarAST currentOuterAltRoot; public int currentOuterAltNumber = 1; // 1..n public int rewriteEBNFLevel = 0; -public boolean inRewrite; -public boolean currentOuterAltHasRewrite; public GrammarTreeVisitor() { this(null); } public ErrorManager getErrorManager() { return null; } public void visitGrammar(GrammarAST t) { visit(t, "grammarSpec"); } -public void visitRewrite(GrammarAST t) { visit(t, "rewrite"); } -public void visitRewriteEBNF(GrammarAST t) { visit(t, "rewriteTreeEbnf"); } public void visit(GrammarAST t, String ruleName) { CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); setTreeNodeStream(nodes); @@ -148,12 +144,6 @@ public void finallyAction(ActionAST action) { } public void discoverAlt(AltAST alt) { } /** outermost alt */ public void finishAlt(AltAST alt) { } -/** outermost alt */ -public void discoverAltWithRewrite(AltAST alt) { } -/** outermost alt */ -public void finishAltWithRewrite(AltAST alt) { } -public void discoverSTRewrite(GrammarAST rew) { } -public void discoverTreeRewrite(GrammarAST rew) { } public void ruleRef(GrammarAST ref, ActionAST arg) { } public void tokenRef(TerminalAST ref) { } @@ -164,18 +154,6 @@ public void actionInAlt(ActionAST action) { } public void sempredInAlt(PredAST pred) { } public void label(GrammarAST op, GrammarAST ID, GrammarAST element) { } -public void rootOp(GrammarAST op, GrammarAST opnd) { } -public void bangOp(GrammarAST op, GrammarAST opnd) { } - -public void discoverRewrites(GrammarAST result) { } -public void finishRewrites(GrammarAST result) { } -public void rewriteTokenRef(TerminalAST ast, ActionAST arg) { } -public void rewriteTerminalOption(TerminalAST t, GrammarAST ID, GrammarAST value) { } -public void rewriteStringRef(TerminalAST ast) { } -public void rewriteRuleRef(GrammarAST ast) { } -public void rewriteLabelRef(GrammarAST ast) { } -public void rewriteAction(ActionAST ast) { } - public void traceIn(String ruleName, int ruleIndex) { System.err.println("enter "+ruleName+": "+input.LT(1)); } @@ -228,7 +206,6 @@ optionValue returns [String v] @init {$v = $start.token.getText();} : ID | STRING_LITERAL - | DOUBLE_QUOTE_STRING_LITERAL | INT ; @@ -341,12 +318,6 @@ ruleBlock ( { currentOuterAltRoot = (GrammarAST)input.LT(1); currentOuterAltNumber++; - currentOuterAltHasRewrite=false; - inRewrite=false; - currentOuterAltHasRewrite = false; - if ( currentOuterAltRoot.getType()==ALT_REWRITE ) { - currentOuterAltHasRewrite=true; - } } outerAlternative )+ @@ -355,19 +326,16 @@ ruleBlock outerAlternative @init { - if ( $start.getType()==ALT_REWRITE ) discoverAltWithRewrite((AltAST)$start.getChild(0)); - else discoverAlt((AltAST)$start); + discoverAlt((AltAST)$start); } @after { - if ( $start.getType()==ALT_REWRITE ) finishAltWithRewrite((AltAST)$start.getChild(0)); - else finishAlt((AltAST)$start); + finishAlt((AltAST)$start); } : alternative ; alternative - : ^(ALT_REWRITE alternative {inRewrite=true;} rewrite {inRewrite=false;}) - | ^(LEXER_ALT_ACTION alternative lexerAction*) + : ^(LEXER_ALT_ACTION alternative lexerAction*) | ^(ALT element+) | ^(ALT EPSILON) ; @@ -395,13 +363,8 @@ element | ^(ACTION elementOptions) {actionInAlt((ActionAST)$ACTION);} | ^(SEMPRED elementOptions) {sempredInAlt((PredAST)$SEMPRED);} - | treeSpec - | ^(ROOT astOperand) {rootOp($ROOT, $astOperand.start);} - | ^(BANG astOperand) {bangOp($BANG, $astOperand.start);} | ^(NOT blockSet) | ^(NOT block) - | DOWN_TOKEN - | UP_TOKEN | ARG_ACTION // lexer [Aa] set ; @@ -415,10 +378,6 @@ labeledElement : ^((ASSIGN|PLUS_ASSIGN) ID element) {label($start, $ID, $element.start);} ; -treeSpec - : ^(TREE_BEGIN element element+ ) // UP_TOKEN and DOWN_TOKEN in there somewhere - ; - subrule : ^(blockSuffix block) | block @@ -426,9 +385,6 @@ subrule blockSuffix : ebnfSuffix - | ROOT - | IMPLIES - | BANG ; ebnfSuffix @@ -493,96 +449,5 @@ elementOption[GrammarASTWithOptions t] : ID {elementOption(t, $ID, null);} | ^(ASSIGN id=ID v=ID) {elementOption(t, $id, $v);} | ^(ASSIGN ID v=STRING_LITERAL) {elementOption(t, $ID, $v);} - | ^(ASSIGN ID v=DOUBLE_QUOTE_STRING_LITERAL) {elementOption(t, $ID, $v);} | ^(ASSIGN ID v=ACTION) {elementOption(t, $ID, $v);} - ; - -rewrite - : {discoverRewrites($start);} predicatedRewrite* nakedRewrite {finishRewrites($start);} - ; - -predicatedRewrite - : ^(ST_RESULT SEMPRED {discoverSTRewrite($start);} rewriteAlt) - | ^(RESULT SEMPRED {discoverTreeRewrite($start);} rewriteAlt) - ; - -nakedRewrite - : ^(ST_RESULT rewriteAlt) - | ^(RESULT rewriteAlt) - ; - -rewriteAlt -@init {rewriteEBNFLevel=0;} - : rewriteTemplate - | rewriteTreeAlt - | ETC - | EPSILON - ; - -rewriteTreeAlt - : ^(REWRITE_SEQ rewriteTreeElement+) - ; - -rewriteTreeElement - : rewriteTreeAtom - | rewriteTree - | rewriteTreeEbnf - ; - -rewriteTreeAtom - : ^(TOKEN_REF rewriteElementOptions ARG_ACTION) - {rewriteTokenRef((TerminalAST)$start,(ActionAST)$ARG_ACTION);} - | ^(TOKEN_REF rewriteElementOptions) - {rewriteTokenRef((TerminalAST)$start,null);} - | ^(TOKEN_REF ARG_ACTION) - {rewriteTokenRef((TerminalAST)$start,(ActionAST)$ARG_ACTION);} - | TOKEN_REF {rewriteTokenRef((TerminalAST)$start,null);} - | RULE_REF {rewriteRuleRef($start);} - | ^(STRING_LITERAL rewriteElementOptions) - {rewriteStringRef((TerminalAST)$start);} - | STRING_LITERAL {rewriteStringRef((TerminalAST)$start);} - | LABEL {rewriteLabelRef($start);} - | ACTION {rewriteAction((ActionAST)$start);} - ; - -rewriteElementOptions - : ^(ELEMENT_OPTIONS rewriteElementOption[(TerminalAST)$start.getParent()]+) - ; - -rewriteElementOption[TerminalAST t] - : ID {rewriteTerminalOption(t, $ID, null);} - | ^(ASSIGN id=ID v=ID) {rewriteTerminalOption(t, $id, $v);} - | ^(ASSIGN ID v=STRING_LITERAL) {rewriteTerminalOption(t, $ID, $v);} - ; - -rewriteTreeEbnf - : ^(ebnfSuffix ^(REWRITE_BLOCK {rewriteEBNFLevel++;} rewriteTreeAlt {rewriteEBNFLevel--;})) - ; - -rewriteTree - : ^(TREE_BEGIN rewriteTreeAtom rewriteTreeElement* ) - ; - -rewriteTemplate - : ^(TEMPLATE rewriteTemplateArgs? DOUBLE_QUOTE_STRING_LITERAL) - | ^(TEMPLATE rewriteTemplateArgs? DOUBLE_ANGLE_STRING_LITERAL) - | rewriteTemplateRef - | rewriteIndirectTemplateHead - | ACTION - ; - -rewriteTemplateRef - : ^(TEMPLATE ID rewriteTemplateArgs?) - ; - -rewriteIndirectTemplateHead - : ^(TEMPLATE ACTION rewriteTemplateArgs?) - ; - -rewriteTemplateArgs - : ^(ARGLIST rewriteTemplateArg+) - ; - -rewriteTemplateArg - : ^(ARG ID ACTION) - ; + ; \ No newline at end of file diff --git a/tool/src/org/antlr/v4/parse/LeftRecursiveRuleAnalyzer.java b/tool/src/org/antlr/v4/parse/LeftRecursiveRuleAnalyzer.java index 25efb5b74..b6c8f44cc 100644 --- a/tool/src/org/antlr/v4/parse/LeftRecursiveRuleAnalyzer.java +++ b/tool/src/org/antlr/v4/parse/LeftRecursiveRuleAnalyzer.java @@ -324,7 +324,10 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { public static boolean hasImmediateRecursiveRuleRefs(GrammarAST t, String ruleName) { if ( t==null ) return false; GrammarAST blk = (GrammarAST)t.getFirstChildWithType(BLOCK); - for (GrammarAST rref : blk.getNodesWithType(RULE_REF)) { + if ( blk==null ) return false; + List ruleRefs = blk.getNodesWithType(RULE_REF); + if ( ruleRefs==null ) return false; + for (GrammarAST rref : ruleRefs) { if ( rref.getChildIndex()==0 && rref.getText().equals(ruleName) ) return true; } return false; diff --git a/tool/src/org/antlr/v4/parse/LeftRecursiveRuleWalker.g b/tool/src/org/antlr/v4/parse/LeftRecursiveRuleWalker.g index 460c83fc5..4b090afca 100644 --- a/tool/src/org/antlr/v4/parse/LeftRecursiveRuleWalker.g +++ b/tool/src/org/antlr/v4/parse/LeftRecursiveRuleWalker.g @@ -105,9 +105,7 @@ ruleBlock returns [boolean isLeftRec] @init{boolean lr=false; this.numAlts = $start.getChildCount();} : ^( BLOCK ( - ( o=outerAlternative[null] - | ^( r=ALT_REWRITE o=outerAlternative[(GrammarAST)$r.getChild(1)] rewrite ) - ) + o=outerAlternative[null] {if ($o.isLeftRec) $isLeftRec = true;} {currentOuterAltNumber++;} )+ @@ -162,8 +160,6 @@ recurseNoLabel : {((CommonTree)input.LT(1)).getText().equals(ruleName)}? RULE_RE token returns [GrammarAST t=null] : ^(ASSIGN ID s=token {$t = $s.t;}) | ^(PLUS_ASSIGN ID s=token {$t = $s.t;}) - | ^(ROOT s=token {$t = $s.t;}) - | ^(BANG s=token {$t = $s.t;}) | b=STRING_LITERAL {$t = $b;} | ^(b=STRING_LITERAL elementOptions) {$t = $b;} | ^(c=TOKEN_REF elementOptions) {$t = $c;} @@ -181,9 +177,7 @@ elementOption ; element - : ^(ROOT element) - | ^(BANG element) - | atom + : atom | ^(NOT element) | ^(RANGE atom atom) | ^(ASSIGN ID element) @@ -191,7 +185,6 @@ element | ^(SET setElement+) | RULE_REF | ebnf - | tree_ | ACTION | SEMPRED | EPSILON @@ -213,14 +206,9 @@ block ; alternative - : ^(ALT_REWRITE alternative rewrite) - | ^(ALT element+) + : ^(ALT element+) ; -tree_ - : ^(TREE_BEGIN element+) - ; - atom : ^(RULE_REF ARG_ACTION?) | ^(STRING_LITERAL elementOptions) @@ -230,18 +218,4 @@ atom | ^(WILDCARD elementOptions) | WILDCARD | ^(DOT ID element) - ; - -ast_suffix - : ROOT - | BANG - ; - -rewrite - : rewrite_result* - ; - -rewrite_result - : ^(ST_RESULT .*) - | ^(RESULT .*) - ; + ; \ No newline at end of file diff --git a/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java b/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java index 265c9183e..277d399b6 100644 --- a/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java +++ b/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java @@ -87,22 +87,11 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { new HashSet() { { add("tokenVocab"); - add("output"); add("rewrite"); add("ASTLabelType"); add("TokenLabelType"); add("superClass"); } }; - public static final Set legalTreeParserOptions = - new HashSet() { - { - add("tokenVocab"); - add("output"); add("ASTLabelType"); - add("superClass"); - add("filter"); - } - }; - public static final Set legalRuleOptions = new HashSet() { { @@ -117,7 +106,6 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { public static final Set legalTokenOptions = new HashSet() { { - add(TerminalAST.defaultTokenOption); add("assoc"); } }; @@ -143,8 +131,6 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { map(ANTLRParser.PARSER, ANTLRParser.PARSER); map(ANTLRParser.PARSER, ANTLRParser.COMBINED); - map(ANTLRParser.TREE, ANTLRParser.TREE); - map(ANTLRParser.COMBINED, ANTLRParser.COMBINED); } }; @@ -166,11 +152,6 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { checkGrammarName(ID.token); } - @Override - public void finishGrammar(GrammarRootAST root, GrammarAST ID) { - checkTreeFilterOptions(root); - } - @Override public void finishPrequels(GrammarAST firstPrequel) { if ( firstPrequel==null ) return; @@ -255,39 +236,6 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { // } } - @Override - public void discoverAltWithRewrite(AltAST alt) { - GrammarAST firstNode = (GrammarAST)alt.getChild(0); - checkRewriteForMultiRootAltInTreeGrammar(g.ast, - firstNode.token, - currentOuterAltNumber); - } - - @Override - public void rootOp(GrammarAST op, GrammarAST opnd) { - checkASTOps(g.ast, op, opnd); - } - - @Override - public void bangOp(GrammarAST op, GrammarAST opnd) { - checkASTOps(g.ast, op, opnd); - } - - @Override - public void discoverTreeRewrite(GrammarAST rew) { - checkRewriteOk(g.ast, rew); - } - - @Override - public void discoverSTRewrite(GrammarAST rew) { - checkRewriteOk(g.ast, rew); - } - - @Override - public void wildcardRef(GrammarAST ref) { - checkWildcardRoot(ref); - } - // Routines to do the actual work of checking issues with a grammar. // They are triggered by the visitor methods above. @@ -349,7 +297,7 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { g.tool.errMgr.grammarError(ErrorType.PARSER_RULES_NOT_ALLOWED, fileName, ruleID, ruleID.getText()); } - if ( (g.isParser()||g.isTreeGrammar()) && + if ( g.isParser() && Character.isUpperCase(ruleID.getText().charAt(0)) ) { g.tool.errMgr.grammarError(ErrorType.LEXER_RULES_NOT_ALLOWED, @@ -454,12 +402,6 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { optionID.getText()); return false; } - // example (ALT_REWRITE (ALT (ID (ELEMENT_OPTIONS Foo))) (-> (ALT ID)) - if ( !inRewrite && this.currentOuterAltHasRewrite ) { - g.tool.errMgr.grammarError(ErrorType.HETERO_ILLEGAL_IN_REWRITE_ALT, - fileName, - optionID); - } // TODO: extra checks depending on terminal kind? return true; } @@ -470,121 +412,11 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { return legalLexerOptions.contains(key); case ANTLRParser.PARSER : return legalParserOptions.contains(key); - case ANTLRParser.TREE : - return legalTreeParserOptions.contains(key); default : return legalParserOptions.contains(key); } } - /** Rules in tree grammar that use -> rewrites and are spitting out - * templates via output=template and then use rewrite=true must only - * use -> on alts that are simple nodes or trees or single rule refs - * that match either nodes or trees. - */ - void checkRewriteForMultiRootAltInTreeGrammar( - GrammarRootAST root, - Token altStart, - int alt) - { - if ( g.isTreeGrammar() && - root.getOptions()!=null && root.getOptionString("output")!=null && - root.getOptionString("output").equals("template") && - root.getOptionString("rewrite")!=null && - root.getOptionString("rewrite").equals("true") ) - { - String fileName = altStart.getInputStream().getSourceName(); - g.tool.errMgr.grammarError(ErrorType.REWRITE_FOR_MULTI_ELEMENT_ALT, - fileName, - altStart, - alt); - } - } - - void checkASTOps(GrammarRootAST root, - GrammarAST op, - GrammarAST elementRoot) - { - RuleAST rule = (RuleAST)op.getAncestor(ANTLRParser.RULE); - String ruleName = rule.getChild(0).getText(); - String fileName = elementRoot.token.getInputStream().getSourceName(); - if ( root.getOptions()==null || !root.getOptionString("output").equals("AST") ) { - g.tool.errMgr.grammarError(ErrorType.AST_OP_WITH_NON_AST_OUTPUT_OPTION, - fileName, - elementRoot.token, - op.getText()); - } - if ( root.getOptions()!=null && root.getOptionString("output")==null ) { - g.tool.errMgr.grammarError(ErrorType.REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION, - fileName, - elementRoot.token, - ruleName); - } - if ( op.hasAncestor(ANTLRParser.ALT_REWRITE) ) { - GrammarAST rew = (GrammarAST)op.getAncestor(ANTLRParser.ALT_REWRITE); - int altNum = rew.getChildIndex() + 1; // alts are 1..n - g.tool.errMgr.grammarError(ErrorType.AST_OP_IN_ALT_WITH_REWRITE, - fileName, - elementRoot.token, - ruleName, - altNum); - } - } - - void checkRewriteOk(GrammarRootAST root, GrammarAST elementRoot) { - String ruleName = currentRuleAST.getChild(0).getText(); - String fileName = elementRoot.token.getInputStream().getSourceName(); - if ( root.getOptions()!=null && root.getOptionString("output")==null ) { - g.tool.errMgr.grammarError(ErrorType.REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION, - fileName, - elementRoot.token, - ruleName); - } - } - - void checkTreeFilterOptions(GrammarRootAST root) { - if ( root.getOptions()==null ) return; - String fileName = root.token.getInputStream().getSourceName(); - String filter = root.getOptionString("filter"); - if ( g.isTreeGrammar() && filter!=null && filter.equals("true") ) { - // check for conflicting options - // filter => backtrack=true (can't be false) - // filter&&output!=AST => error - // filter&&output=AST => rewrite=true - // any deviation from valid option set is an error - String backtrack = root.getOptionString("backtrack"); - String output = root.getOptionString("output"); - String rewrite = root.getOptionString("rewrite"); - if ( backtrack!=null && !backtrack.toString().equals("true") ) { - g.tool.errMgr.grammarError(ErrorType.CONFLICTING_OPTION_IN_TREE_FILTER, - fileName, - root.token, - "backtrack", backtrack); - } - if ( output!=null && !output.equals("AST") ) { - g.tool.errMgr.grammarError(ErrorType.CONFLICTING_OPTION_IN_TREE_FILTER, - fileName, - root.token, - "output", output); - } - else if ( rewrite!=null && !rewrite.equals("true") ) { // && AST output - g.tool.errMgr.grammarError(ErrorType.CONFLICTING_OPTION_IN_TREE_FILTER, - fileName, - root.token, - "rewrite", rewrite); - } - } - } - - void checkWildcardRoot(GrammarAST wild) { - if ( wild.getChildIndex()==0 && wild.getParent().getType()==ANTLRParser.TREE_BEGIN ) { - String fileName = wild.token.getInputStream().getSourceName(); - g.tool.errMgr.grammarError(ErrorType.WILDCARD_AS_ROOT, - fileName, - wild.token); - } - } - void checkImport(Token importID) { Grammar delegate = g.getImportedGrammar(importID.getText()); if ( delegate==null ) return; diff --git a/tool/src/org/antlr/v4/semantics/RewriteRefs.java b/tool/src/org/antlr/v4/semantics/RewriteRefs.java deleted file mode 100644 index 5f7d4d7c7..000000000 --- a/tool/src/org/antlr/v4/semantics/RewriteRefs.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.semantics; - -import org.antlr.v4.parse.GrammarTreeVisitor; -import org.antlr.v4.tool.ast.*; - -import java.util.*; - -public class RewriteRefs extends GrammarTreeVisitor { - List shallow = new ArrayList(); - List deep = new ArrayList(); - int desiredShallowLevel; - - public RewriteRefs(int desiredShallowLevel) { - this.desiredShallowLevel = desiredShallowLevel; - } - - public void track(GrammarAST t) { - deep.add(t); - if ( rewriteEBNFLevel==desiredShallowLevel ) shallow.add(t); - } - - @Override - public void rewriteTokenRef(TerminalAST ast, ActionAST arg) { - track(ast); - } - - @Override - public void rewriteStringRef(TerminalAST ast) { - track(ast); - } - - @Override - public void rewriteRuleRef(GrammarAST ast) { - track(ast); - } - - @Override - public void rewriteLabelRef(GrammarAST ast) { - track(ast); - } -} diff --git a/tool/src/org/antlr/v4/semantics/SemanticPipeline.java b/tool/src/org/antlr/v4/semantics/SemanticPipeline.java index bd3abc619..99729b2da 100644 --- a/tool/src/org/antlr/v4/semantics/SemanticPipeline.java +++ b/tool/src/org/antlr/v4/semantics/SemanticPipeline.java @@ -126,9 +126,6 @@ public class SemanticPipeline { // CHECK ATTRIBUTE EXPRESSIONS FOR SEMANTIC VALIDITY AttributeChecks.checkAllAttributeExpressions(g); - symcheck.checkForUndefinedTokensInRewrite(); - - UseDefAnalyzer.checkRewriteElementsPresentOnLeftSide(g); UseDefAnalyzer.trackTokenRuleRefsInActions(g); } diff --git a/tool/src/org/antlr/v4/semantics/SymbolChecks.java b/tool/src/org/antlr/v4/semantics/SymbolChecks.java index 43d32d8f4..c945c438d 100644 --- a/tool/src/org/antlr/v4/semantics/SymbolChecks.java +++ b/tool/src/org/antlr/v4/semantics/SymbolChecks.java @@ -29,7 +29,6 @@ package org.antlr.v4.semantics; -import org.antlr.runtime.Token; import org.antlr.v4.parse.ANTLRParser; import org.antlr.v4.tool.*; import org.antlr.v4.tool.ast.GrammarAST; @@ -84,7 +83,6 @@ public class SymbolChecks { //checkRuleArgs(collector.rulerefs); checkForTokenConflicts(collector.tokenIDRefs); // sets tokenIDs checkForLabelConflicts(collector.rules); - //checkRewriteElementsPresentOnLeftSide(collector.rules); // move to after token type assignment } public void checkForRuleConflicts(List rules) { @@ -190,19 +188,6 @@ public class SymbolChecks { // } } - public void checkForUndefinedTokensInRewrite() { - // Ensure that all tokens refs on the right of -> have been defined. - for (GrammarAST elem : collector.rewriteElements) { - if ( elem.getType()==ANTLRParser.TOKEN_REF ) { - int ttype = g.getTokenType(elem.getText()); - if ( ttype == Token.INVALID_TOKEN_TYPE ) { - g.tool.errMgr.grammarError(ErrorType.UNDEFINED_TOKEN_REF_IN_REWRITE, - g.fileName, elem.token, elem.getText()); - } - } - } - } - /** Make sure a label doesn't conflict with another symbol. * Labels must not conflict with: rules, tokens, scope names, * return values, parameters, and rule-scope dynamic attributes diff --git a/tool/src/org/antlr/v4/semantics/SymbolCollector.java b/tool/src/org/antlr/v4/semantics/SymbolCollector.java index 3a1a42a43..7c6a73958 100644 --- a/tool/src/org/antlr/v4/semantics/SymbolCollector.java +++ b/tool/src/org/antlr/v4/semantics/SymbolCollector.java @@ -61,20 +61,10 @@ public class SymbolCollector extends GrammarTreeVisitor { public List tokenIDRefs = new ArrayList(); public Set strings = new HashSet(); public List tokensDefs = new ArrayList(); - public List scopes = new ArrayList(); - - /** Tracks named actions like @parser::members {...}. - * Key is scope::name, value is action ast node. - */ -// public DoubleKeyMap namedActions = -// new DoubleKeyMap(); /** Track action name node in @parser::members {...} or @members {...} */ List namedActions = new ArrayList(); - /** All labels, rule references, and token references to right of -> */ - public List rewriteElements = new ArrayList(); - // context public Rule currentRule; @@ -157,9 +147,6 @@ public class SymbolCollector extends GrammarTreeVisitor { currentRule = r; } - @Override - public void discoverAltWithRewrite(AltAST alt) { discoverAlt(alt); } - @Override public void discoverAlt(AltAST alt) { currentRule.alt[currentOuterAltNumber].ast = alt; @@ -223,28 +210,6 @@ public class SymbolCollector extends GrammarTreeVisitor { } } - @Override - public void rewriteLabelRef(GrammarAST ast) { rewriteElements.add(ast); } - - @Override - public void rewriteRuleRef(GrammarAST ast) { rewriteElements.add(ast); } - - @Override - public void rewriteStringRef(TerminalAST ast) { - rewriteElements.add(ast); - } - - @Override - public void rewriteTokenRef(TerminalAST ast, ActionAST arg) { - rewriteElements.add(ast); - if ( arg!=null ) arg.resolver = currentRule.alt[currentOuterAltNumber]; - } - - @Override - public void rewriteAction(ActionAST ast) { - ast.resolver = currentRule.alt[currentOuterAltNumber]; - } - @Override public void grammarOption(GrammarAST ID, GrammarAST valueAST) { setActionResolver(valueAST); diff --git a/tool/src/org/antlr/v4/semantics/UseDefAnalyzer.java b/tool/src/org/antlr/v4/semantics/UseDefAnalyzer.java index 05751c173..011cf64ba 100644 --- a/tool/src/org/antlr/v4/semantics/UseDefAnalyzer.java +++ b/tool/src/org/antlr/v4/semantics/UseDefAnalyzer.java @@ -34,7 +34,10 @@ import org.antlr.runtime.Token; import org.antlr.v4.parse.ANTLRParser; import org.antlr.v4.parse.ActionSplitter; import org.antlr.v4.parse.ActionSplitterListener; -import org.antlr.v4.tool.*; +import org.antlr.v4.tool.Alternative; +import org.antlr.v4.tool.Grammar; +import org.antlr.v4.tool.LexerGrammar; +import org.antlr.v4.tool.Rule; import org.antlr.v4.tool.ast.ActionAST; import org.antlr.v4.tool.ast.GrammarAST; @@ -42,31 +45,6 @@ import java.util.*; /** Look for errors and deadcode stuff */ public class UseDefAnalyzer { - public static void checkRewriteElementsPresentOnLeftSide(Grammar g) { - for (Rule r : g.rules.values()) { - for (int a=1; a<=r.numberOfAlts; a++) { - Alternative alt = r.alt[a]; - GrammarAST rewNode = alt.ast.getRewrite(); - if ( rewNode==null ) continue; - List elems = getAllRewriteElementRefs(rewNode); - for (GrammarAST e : elems) { - boolean unknownToken = e.getType()==ANTLRParser.TOKEN_REF && - g.getTokenType(e.getText()) == Token.INVALID_TOKEN_TYPE; - if ( unknownToken ) continue; // We already checked these - boolean ok = - alt.ruleRefs.containsKey(e.getText()) || - g.getTokenType(e.getText()) != Token.INVALID_TOKEN_TYPE || - alt.labelDefs.containsKey(e.getText()) || - e.getText().equals(r.name); - if ( !ok ) { // $r ok in rule r - g.tool.errMgr.grammarError(ErrorType.REWRITE_ELEMENT_NOT_PRESENT_ON_LHS, - g.fileName, e.token, e.getText()); - } - } - } - } - } - // side-effect: updates Alternative with refs in actions public static void trackTokenRuleRefsInActions(Grammar g) { for (Rule r : g.rules.values()) { @@ -107,63 +85,6 @@ public class UseDefAnalyzer { return dependent[0]; } - /** Given -> (ALT ...), return list of element refs at - * top level - */ - public static List getElementReferencesShallowInOuterAlt(GrammarAST altAST) - { - return getRewriteElementRefs(altAST, 0, false); - } - - /** Given (('?'|'*') (REWRITE_BLOCK (ALT ...))) return list of element refs at - * top level of REWRITE_BLOCK. Must see into (nested) tree structures if - * optional but not if closure (that might lead to inf loop when building tree). - */ - public static List getElementReferencesInEBNF(GrammarAST ebnfRoot, - boolean deep) - { - return getRewriteElementRefs(ebnfRoot, 1, deep); - } - - /** Get list of rule refs, token refs mentioned on left, and labels not - * referring to rule result like $e in rule e. - */ - public static List filterForRuleAndTokenRefs(Alternative alt, - List refs) - { - List elems = new ArrayList(); - if ( refs!=null ) { - for (GrammarAST ref : refs) { - boolean imaginary = - ref.getType()== ANTLRParser.TOKEN_REF && - !alt.tokenRefs.containsKey(ref.getText()); - boolean selfLabel = - ref.getType()==ANTLRParser.LABEL && - ref.getText().equals(alt.rule.name); - if ( !imaginary && !selfLabel ) elems.add(ref); - } - } - return elems; - } - - public static List getAllRewriteElementRefs(GrammarAST root) { - return getRewriteElementRefs(root, 0, true); - } - - /** Visit either ^(-> ...) or ^(('?'|'*') ...) */ - public static List getRewriteElementRefs(GrammarAST root, - int desiredShallowLevel, - boolean deep) - { - RewriteRefs collector = new RewriteRefs(desiredShallowLevel); - if ( root.getType()==ANTLRParser.RESULT ) collector.visitRewrite(root); - else collector.visitRewriteEBNF(root); -// System.out.println("from "+root.toStringTree()); -// System.out.println("shallow: "+collector.shallow); -// System.out.println("deep: "+collector.deep); - return deep ? collector.deep : collector.shallow; - } - /** Find all rules reachable from r directly or indirectly for all r in g */ public static Map> getRuleDependencies(Grammar g) { return getRuleDependencies(g, g.rules.values()); diff --git a/tool/src/org/antlr/v4/tool/Alternative.java b/tool/src/org/antlr/v4/tool/Alternative.java index 9ead730e4..f008f1e7e 100644 --- a/tool/src/org/antlr/v4/tool/Alternative.java +++ b/tool/src/org/antlr/v4/tool/Alternative.java @@ -30,7 +30,6 @@ package org.antlr.v4.tool; -import org.antlr.v4.parse.ANTLRParser; import org.antlr.v4.tool.ast.ActionAST; import org.antlr.v4.tool.ast.AltAST; import org.antlr.v4.tool.ast.GrammarAST; @@ -78,14 +77,6 @@ public class Alternative implements AttributeResolver { public Alternative(Rule r, int altNum) { this.rule = r; this.altNum = altNum; } - /** (ALT_REWRITE (ALT ...) (-> (ALT ...))); rewrite might nested in subrule */ - public boolean hasRewrite() { - return - ast.parent.getType()==ANTLRParser.ALT_REWRITE || - ast.getNodesWithType(ANTLRParser.ALT_REWRITE).size()>0; -// return ast.parent.getType()==ANTLRParser.ALT_REWRITE; - } - public boolean resolvesToToken(String x, ActionAST node) { if ( tokenRefs.get(x)!=null ) return true; LabelElementPair anyLabelDef = getAnyLabelDef(x); diff --git a/tool/src/org/antlr/v4/tool/Grammar.java b/tool/src/org/antlr/v4/tool/Grammar.java index 110755a9e..b370621f0 100644 --- a/tool/src/org/antlr/v4/tool/Grammar.java +++ b/tool/src/org/antlr/v4/tool/Grammar.java @@ -60,8 +60,7 @@ public class Grammar implements AttributeResolver { public static final Set doNotCopyOptionsToLexer = new HashSet() { { - add("output"); add("TokenLabelType"); add("ASTLabelType"); add("superClass"); - add("rewrite"); + add("TokenLabelType"); add("superClass"); } }; @@ -214,13 +213,6 @@ public class Grammar implements AttributeResolver { } protected void initTokenSymbolTables() { - if ( isTreeGrammar() ) { - typeToTokenList.setSize(Token.UP + 1); - typeToTokenList.set(Token.DOWN, "DOWN"); - typeToTokenList.set(Token.UP, "UP"); - tokenNameToTypeMap.put("DOWN", Token.DOWN); - tokenNameToTypeMap.put("UP", Token.UP); - } tokenNameToTypeMap.put("EOF", Token.EOF); } @@ -643,8 +635,6 @@ public class Grammar implements AttributeResolver { case ANTLRParser.PARSER : case ANTLRParser.COMBINED : return "parser"; - case ANTLRParser.TREE : - return "treeparser"; } return null; } @@ -661,7 +651,6 @@ public class Grammar implements AttributeResolver { public boolean isLexer() { return getType()==ANTLRParser.LEXER; } public boolean isParser() { return getType()==ANTLRParser.PARSER; } - public boolean isTreeGrammar() { return getType()==ANTLRParser.TREE; } public boolean isCombined() { return getType()==ANTLRParser.COMBINED; } public String getTypeString() { @@ -673,7 +662,6 @@ public class Grammar implements AttributeResolver { switch ( type ) { case ANTLRParser.LEXER : return "Lexer"; case ANTLRParser.PARSER : return "Parser"; - case ANTLRParser.TREE : return ""; // if combined grammar, gen Parser and Lexer will be done later // TODO: we are separate now right? case ANTLRParser.COMBINED : return "Parser"; @@ -691,11 +679,6 @@ public class Grammar implements AttributeResolver { return defaultValue; } - public boolean hasASTOption() { - String outputOption = getOptionString("output"); - return outputOption!=null && outputOption.equals("AST"); - } - /** Manually get language option from tree */ // TODO: move to general tree visitor/parser class? // TODO: don't need anymore as i set optins in parser? diff --git a/tool/src/org/antlr/v4/tool/LabelElementPair.java b/tool/src/org/antlr/v4/tool/LabelElementPair.java index 0260faa17..38452496f 100644 --- a/tool/src/org/antlr/v4/tool/LabelElementPair.java +++ b/tool/src/org/antlr/v4/tool/LabelElementPair.java @@ -64,12 +64,6 @@ public class LabelElementPair { if ( labelOp==ANTLRParser.ASSIGN ) type = LabelType.LEXER_STRING_LABEL; } } - else if ( g.isTreeGrammar() ) { - if ( element.getFirstDescendantWithType(ANTLRParser.WILDCARD)!=null ) { - if ( labelOp==ANTLRParser.ASSIGN ) type = LabelType.WILDCARD_TREE_LABEL; - else type = LabelType.WILDCARD_TREE_LIST_LABEL; - } - } } public String toString() { diff --git a/tool/src/org/antlr/v4/tool/ast/AltAST.java b/tool/src/org/antlr/v4/tool/ast/AltAST.java index 36731c4f4..5bd6953db 100644 --- a/tool/src/org/antlr/v4/tool/ast/AltAST.java +++ b/tool/src/org/antlr/v4/tool/ast/AltAST.java @@ -31,7 +31,6 @@ package org.antlr.v4.tool.ast; import org.antlr.runtime.Token; import org.antlr.runtime.tree.Tree; -import org.antlr.v4.parse.ANTLRParser; import org.antlr.v4.tool.Alternative; /** Any ALT (which can be child of ALT_REWRITE node) */ @@ -52,14 +51,6 @@ public class AltAST extends GrammarAST { public AltAST(int type) { super(type); } public AltAST(int type, Token t) { super(type, t); } - public GrammarAST getRewrite() { - // ^(ALT_REWRITE ^(ALT ...) ^(-> ...)) ?? - if ( getParent().getType() == ANTLRParser.ALT_REWRITE ) { - return (GrammarAST)getParent().getChild(1); - } - return null; - } - @Override public Tree dupNode() { return new AltAST(this); } diff --git a/tool/src/org/antlr/v4/tool/ast/TerminalAST.java b/tool/src/org/antlr/v4/tool/ast/TerminalAST.java index 306657eb2..559c550ac 100644 --- a/tool/src/org/antlr/v4/tool/ast/TerminalAST.java +++ b/tool/src/org/antlr/v4/tool/ast/TerminalAST.java @@ -33,7 +33,6 @@ import org.antlr.runtime.Token; import org.antlr.runtime.tree.Tree; public class TerminalAST extends GrammarASTWithOptions implements RuleElementAST { - public static final String defaultTokenOption = "node"; public TerminalAST(GrammarAST node) { super(node); diff --git a/tool/test/org/antlr/v4/test/BaseTest.java b/tool/test/org/antlr/v4/test/BaseTest.java index 24065cf97..95cb4a32d 100644 --- a/tool/test/org/antlr/v4/test/BaseTest.java +++ b/tool/test/org/antlr/v4/test/BaseTest.java @@ -30,7 +30,10 @@ package org.antlr.v4.test; import org.antlr.v4.Tool; -import org.antlr.v4.automata.*; +import org.antlr.v4.automata.ATNFactory; +import org.antlr.v4.automata.ATNPrinter; +import org.antlr.v4.automata.LexerATNFactory; +import org.antlr.v4.automata.ParserATNFactory; import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.misc.Utils; import org.antlr.v4.runtime.*; @@ -431,20 +434,9 @@ public abstract class BaseTest { lexerName, debug); writeFile(tmpdir, "input", input); - boolean parserBuildsTrees = - grammarStr.contains("output=AST") || - grammarStr.contains("output = AST"); - boolean parserBuildsTemplate = - grammarStr.contains("output=template") || - grammarStr.contains("output = template"); return rawExecRecognizer(parserName, - null, lexerName, startRuleName, - null, - parserBuildsTrees, - parserBuildsTemplate, - false, debug); } @@ -500,24 +492,9 @@ public abstract class BaseTest { writeFile(tmpdir, "input", input); - boolean parserBuildsTrees = - parserGrammarStr.contains("output=AST") || - parserGrammarStr.contains("output = AST"); - boolean treeParserBuildsTrees = - treeParserGrammarStr.contains("output=AST") || - treeParserGrammarStr.contains("output = AST"); - boolean parserBuildsTemplate = - parserGrammarStr.contains("output=template") || - parserGrammarStr.contains("output = template"); - return rawExecRecognizer(parserName, - treeParserName, lexerName, parserStartRuleName, - treeParserStartRuleName, - parserBuildsTrees, - parserBuildsTemplate, - treeParserBuildsTrees, debug); } @@ -546,39 +523,12 @@ public abstract class BaseTest { } protected String rawExecRecognizer(String parserName, - @Nullable String treeParserName, String lexerName, String parserStartRuleName, - @Nullable String treeParserStartRuleName, - boolean parserBuildsTrees, - boolean parserBuildsTemplate, - boolean treeParserBuildsTrees, boolean debug) { this.stderrDuringParse = null; - if ( treeParserBuildsTrees && parserBuildsTrees ) { - writeTreeAndTreeTestFile(parserName, - treeParserName, - lexerName, - parserStartRuleName, - treeParserStartRuleName, - debug); - } - else if ( parserBuildsTrees ) { - writeTreeTestFile(parserName, - treeParserName, - lexerName, - parserStartRuleName, - treeParserStartRuleName, - debug); - } - else if ( parserBuildsTemplate ) { - writeTemplateTestFile(parserName, - lexerName, - parserStartRuleName, - debug); - } - else if ( parserName==null ) { + if ( parserName==null ) { writeLexerTestFile(lexerName, false); } else { @@ -827,7 +777,6 @@ public abstract class BaseTest { void checkRuleATN(Grammar g, String ruleName, String expecting) { ParserATNFactory f = new ParserATNFactory(g); - if ( g.isTreeGrammar() ) f = new TreeParserATNFactory(g); ATN atn = f.createATN(); DOTGenerator dot = new DOTGenerator(g); @@ -1206,30 +1155,10 @@ public abstract class BaseTest { writeFile(tmpdir, "Test.java", outputFileST.render()); } - public void writeRecognizerAndCompile(String parserName, String treeParserName, String lexerName, String parserStartRuleName, String treeParserStartRuleName, boolean parserBuildsTrees, boolean parserBuildsTemplate, boolean treeParserBuildsTrees, boolean debug) { - if ( treeParserBuildsTrees && parserBuildsTrees ) { - writeTreeAndTreeTestFile(parserName, - treeParserName, - lexerName, - parserStartRuleName, - treeParserStartRuleName, - debug); - } - else if ( parserBuildsTrees ) { - writeTreeTestFile(parserName, - treeParserName, - lexerName, - parserStartRuleName, - treeParserStartRuleName, - debug); - } - else if ( parserBuildsTemplate ) { - writeTemplateTestFile(parserName, - lexerName, - parserStartRuleName, - debug); - } - else if ( parserName==null ) { + public void writeRecognizerAndCompile(String parserName, String lexerName, + String parserStartRuleName, + boolean debug) { + if ( parserName==null ) { writeLexerTestFile(lexerName, debug); } else { diff --git a/tool/test/org/antlr/v4/test/TestASTNodeStream.java b/tool/test/org/antlr/v4/test/TestASTNodeStream.java deleted file mode 100644 index c3842f1a2..000000000 --- a/tool/test/org/antlr/v4/test/TestASTNodeStream.java +++ /dev/null @@ -1,386 +0,0 @@ -/* - * [The "BSD license"] - * Copyright (c) 2010 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. - * "+UP+". 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.test; - -import org.antlr.v4.runtime.CommonToken; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.tree.*; -import org.junit.Test; - -/** Test the tree node stream. */ -public class TestASTNodeStream extends BaseTest { - public static final String DN = " "+Token.DOWN+" "; - public static final String UP = " "+Token.UP; - - /** Build new stream; let's us override to test other streams. */ - public ASTNodeStream newStream(Object t) { - return new CommonASTNodeStream(t); - } - - public String toTokenTypeString(ASTNodeStream stream) { - return ((CommonASTNodeStream)stream).toTokenTypeString(); - } - - @Test public void testSingleNode() throws Exception { - BaseAST t = new CommonAST(new CommonToken(101)); - - ASTNodeStream stream = newStream(t); - String expecting = " 101"; - String found = toNodesOnlyString(stream); - assertEquals(expecting, found); - - expecting = " 101"; - found = toTokenTypeString(stream); - assertEquals(expecting, found); - } - - @Test public void test4Nodes() throws Exception { - // ^(101 ^(102 103) 104) - BaseAST t = new CommonAST(new CommonToken(101)); - t.addChild(new CommonAST(new CommonToken(102))); - t.getChild(0).addChild(new CommonAST(new CommonToken(103))); - t.addChild(new CommonAST(new CommonToken(104))); - - ASTNodeStream stream = newStream(t); - String expecting = " 101 102 103 104"; - String found = toNodesOnlyString(stream); - assertEquals(expecting, found); - - expecting = " 101"+DN+"102"+DN+"103"+UP+" 104"+UP+""; - found = toTokenTypeString(stream); - assertEquals(expecting, found); - } - - @Test public void testList() throws Exception { - BaseAST root = new CommonAST((Token)null); - - BaseAST t = new CommonAST(new CommonToken(101)); - t.addChild(new CommonAST(new CommonToken(102))); - t.getChild(0).addChild(new CommonAST(new CommonToken(103))); - t.addChild(new CommonAST(new CommonToken(104))); - - BaseAST u = new CommonAST(new CommonToken(105)); - - root.addChild(t); - root.addChild(u); - - ASTNodeStream stream = newStream(root); - String expecting = " 101 102 103 104 105"; - String found = toNodesOnlyString(stream); - assertEquals(expecting, found); - - expecting = " 101"+DN+"102"+DN+"103"+UP+" 104"+UP+" 105"; - found = toTokenTypeString(stream); - assertEquals(expecting, found); - } - - @Test public void testFlatList() throws Exception { - BaseAST root = new CommonAST((Token)null); - - root.addChild(new CommonAST(new CommonToken(101))); - root.addChild(new CommonAST(new CommonToken(102))); - root.addChild(new CommonAST(new CommonToken(103))); - - ASTNodeStream stream = newStream(root); - String expecting = " 101 102 103"; - String found = toNodesOnlyString(stream); - assertEquals(expecting, found); - - expecting = " 101 102 103"; - found = toTokenTypeString(stream); - assertEquals(expecting, found); - } - - @Test public void testListWithOneNode() throws Exception { - BaseAST root = new CommonAST((Token)null); - - root.addChild(new CommonAST(new CommonToken(101))); - - ASTNodeStream stream = newStream(root); - String expecting = " 101"; - String found = toNodesOnlyString(stream); - assertEquals(expecting, found); - - expecting = " 101"; - found = toTokenTypeString(stream); - assertEquals(expecting, found); - } - - @Test public void testAoverB() throws Exception { - BaseAST t = new CommonAST(new CommonToken(101)); - t.addChild(new CommonAST(new CommonToken(102))); - - ASTNodeStream stream = newStream(t); - String expecting = " 101 102"; - String found = toNodesOnlyString(stream); - assertEquals(expecting, found); - - expecting = " 101"+DN+"102"+UP+""; - found = toTokenTypeString(stream); - assertEquals(expecting, found); - } - - @Test public void testLT() throws Exception { - // ^(101 ^(102 103) 104) - BaseAST t = new CommonAST(new CommonToken(101)); - t.addChild(new CommonAST(new CommonToken(102))); - t.getChild(0).addChild(new CommonAST(new CommonToken(103))); - t.addChild(new CommonAST(new CommonToken(104))); - - ASTNodeStream stream = newStream(t); - assertEquals(101, ((AST)stream.LT(1)).getType()); - assertEquals(Token.DOWN, ((AST)stream.LT(2)).getType()); - assertEquals(102, ((AST)stream.LT(3)).getType()); - assertEquals(Token.DOWN, ((AST)stream.LT(4)).getType()); - assertEquals(103, ((AST)stream.LT(5)).getType()); - assertEquals(Token.UP, ((AST)stream.LT(6)).getType()); - assertEquals(104, ((AST)stream.LT(7)).getType()); - assertEquals(Token.UP, ((AST)stream.LT(8)).getType()); - assertEquals(Token.EOF, ((AST)stream.LT(9)).getType()); - // check way ahead - assertEquals(Token.EOF, ((AST)stream.LT(100)).getType()); - } - - @Test public void testMarkRewindEntire() throws Exception { - // ^(101 ^(102 103 ^(106 107) ) 104 105) - // stream has 7 real + 6 nav nodes - // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF - BaseAST r0 = new CommonAST(new CommonToken(101)); - BaseAST r1 = new CommonAST(new CommonToken(102)); - r0.addChild(r1); - r1.addChild(new CommonAST(new CommonToken(103))); - BaseAST r2 = new CommonAST(new CommonToken(106)); - r2.addChild(new CommonAST(new CommonToken(107))); - r1.addChild(r2); - r0.addChild(new CommonAST(new CommonToken(104))); - r0.addChild(new CommonAST(new CommonToken(105))); - - ASTNodeStream stream = newStream(r0); - int m = stream.mark(); // MARK - int index = stream.index(); - for (int k=1; k<=13; k++) { // consume til end - stream.LT(1); - stream.consume(); - } - assertEquals(Token.EOF, ((AST)stream.LT(1)).getType()); - stream.seek(index); - stream.release(m); - - // consume til end again :) - for (int k=1; k<=13; k++) { // consume til end - stream.LT(1); - stream.consume(); - } - assertEquals(Token.EOF, ((AST)stream.LT(1)).getType()); - } - - @Test public void testMarkRewindInMiddle() throws Exception { - // ^(101 ^(102 103 ^(106 107) ) 104 105) - // stream has 7 real + 6 nav nodes - // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF - BaseAST r0 = new CommonAST(new CommonToken(101)); - BaseAST r1 = new CommonAST(new CommonToken(102)); - r0.addChild(r1); - r1.addChild(new CommonAST(new CommonToken(103))); - BaseAST r2 = new CommonAST(new CommonToken(106)); - r2.addChild(new CommonAST(new CommonToken(107))); - r1.addChild(r2); - r0.addChild(new CommonAST(new CommonToken(104))); - r0.addChild(new CommonAST(new CommonToken(105))); - - ASTNodeStream stream = newStream(r0); - for (int k=1; k<=7; k++) { // consume til middle - //System.out.println(((AST)stream.LT(1)).getType()); - stream.consume(); - } - assertEquals(107, ((AST)stream.LT(1)).getType()); - int m = stream.mark(); // MARK - int index = stream.index(); - stream.consume(); // consume 107 - stream.consume(); // consume UP - stream.consume(); // consume UP - stream.consume(); // consume 104 - stream.seek(index); // REWIND - stream.release(m); - stream.mark(); // keep saving nodes though - - assertEquals(107, ((AST)stream.LT(1)).getType()); - stream.consume(); - assertEquals(Token.UP, ((AST)stream.LT(1)).getType()); - stream.consume(); - assertEquals(Token.UP, ((AST)stream.LT(1)).getType()); - stream.consume(); - assertEquals(104, ((AST)stream.LT(1)).getType()); - stream.consume(); - // now we're past rewind position - assertEquals(105, ((AST)stream.LT(1)).getType()); - stream.consume(); - assertEquals(Token.UP, ((AST)stream.LT(1)).getType()); - stream.consume(); - assertEquals(Token.EOF, ((AST)stream.LT(1)).getType()); - assertEquals(Token.UP, ((AST)stream.LT(-1)).getType()); - } - - @Test public void testMarkRewindNested() throws Exception { - // ^(101 ^(102 103 ^(106 107) ) 104 105) - // stream has 7 real + 6 nav nodes - // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF - BaseAST r0 = new CommonAST(new CommonToken(101)); - BaseAST r1 = new CommonAST(new CommonToken(102)); - r0.addChild(r1); - r1.addChild(new CommonAST(new CommonToken(103))); - BaseAST r2 = new CommonAST(new CommonToken(106)); - r2.addChild(new CommonAST(new CommonToken(107))); - r1.addChild(r2); - r0.addChild(new CommonAST(new CommonToken(104))); - r0.addChild(new CommonAST(new CommonToken(105))); - - ASTNodeStream stream = newStream(r0); - int m = stream.mark(); // MARK at start - int index = stream.index(); - stream.consume(); // consume 101 - stream.consume(); // consume DN - int m2 = stream.mark(); // MARK on 102 - int index2 = stream.index(); - stream.consume(); // consume 102 - stream.consume(); // consume DN - stream.consume(); // consume 103 - stream.consume(); // consume 106 - stream.seek(index2); // REWIND to 102 - stream.release(m2); - assertEquals(102, ((AST)stream.LT(1)).getType()); - stream.consume(); - assertEquals(Token.DOWN, ((AST)stream.LT(1)).getType()); - stream.consume(); - // stop at 103 and rewind to start - stream.seek(index); // REWIND to 101 - stream.release(m); - assertEquals(101, ((AST)stream.LT(1)).getType()); - stream.consume(); - assertEquals(Token.DOWN, ((AST)stream.LT(1)).getType()); - stream.consume(); - assertEquals(102, ((AST)stream.LT(1)).getType()); - stream.consume(); - assertEquals(Token.DOWN, ((AST)stream.LT(1)).getType()); - } - - @Test public void testSeekFromStart() throws Exception { - // ^(101 ^(102 103 ^(106 107) ) 104 105) - // stream has 7 real + 6 nav nodes - // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF - BaseAST r0 = new CommonAST(new CommonToken(101)); - BaseAST r1 = new CommonAST(new CommonToken(102)); - r0.addChild(r1); - r1.addChild(new CommonAST(new CommonToken(103))); - BaseAST r2 = new CommonAST(new CommonToken(106)); - r2.addChild(new CommonAST(new CommonToken(107))); - r1.addChild(r2); - r0.addChild(new CommonAST(new CommonToken(104))); - r0.addChild(new CommonAST(new CommonToken(105))); - - ASTNodeStream stream = newStream(r0); - stream.seek(7); // seek to 107 - assertEquals(107, ((AST)stream.LT(1)).getType()); - stream.consume(); // consume 107 - stream.consume(); // consume UP - stream.consume(); // consume UP - assertEquals(104, ((AST)stream.LT(1)).getType()); - } - - @Test public void testReset() throws Exception { - // ^(101 ^(102 103 ^(106 107) ) 104 105) - // stream has 7 real + 6 nav nodes - // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF - BaseAST r0 = new CommonAST(new CommonToken(101)); - BaseAST r1 = new CommonAST(new CommonToken(102)); - r0.addChild(r1); - r1.addChild(new CommonAST(new CommonToken(103))); - BaseAST r2 = new CommonAST(new CommonToken(106)); - r2.addChild(new CommonAST(new CommonToken(107))); - r1.addChild(r2); - r0.addChild(new CommonAST(new CommonToken(104))); - r0.addChild(new CommonAST(new CommonToken(105))); - - ASTNodeStream stream = newStream(r0); - String v = toNodesOnlyString(stream); // scan all - stream.reset(); - String v2 = toNodesOnlyString(stream); // scan all - assertEquals(v, v2); - } - - @Test public void testDeepTree() throws Exception { - // ^(10 100 101 ^(20 ^(30 40 (50 (60 70)))) (80 90))) - // stream has 8 real + 10 nav nodes - int n = 9; - CommonAST[] nodes = new CommonAST[n]; - for (int i=0; i< n; i++) { - nodes[i] = new CommonAST(new CommonToken((i+1)*10)); - } - BaseAST g = nodes[0]; - BaseAST rules = nodes[1]; - BaseAST rule1 = nodes[2]; - BaseAST id = nodes[3]; - BaseAST block = nodes[4]; - BaseAST alt = nodes[5]; - BaseAST s = nodes[6]; - BaseAST rule2 = nodes[7]; - BaseAST id2 = nodes[8]; - g.addChild(new CommonAST(new CommonToken(100))); - g.addChild(new CommonAST(new CommonToken(101))); - g.addChild(rules); - rules.addChild(rule1); - rule1.addChild(id); - rule1.addChild(block); - block.addChild(alt); - alt.addChild(s); - rules.addChild(rule2); - rule2.addChild(id2); - - ASTNodeStream stream = newStream(g); - String expecting = " 10"+DN+"100 101 20"+DN+"30"+DN+"40 50"+DN+"60"+DN+"70"+UP+""+UP+""+UP+" 80"+DN+"90"+UP+""+UP+""+UP+""; - String found = toTokenTypeString(stream); - assertEquals(expecting, found); - } - - public String toNodesOnlyString(ASTNodeStream nodes) { - ASTAdaptor adaptor = nodes.getTreeAdaptor(); - StringBuffer buf = new StringBuffer(); - Object o = nodes.LT(1); - int type = adaptor.getType(o); - while ( o!=null && type!=Token.EOF ) { - if ( !(type==Token.DOWN||type==Token.UP) ) { - buf.append(" "); - buf.append(type); - } - nodes.consume(); - o = nodes.LT(1); - type = adaptor.getType(o); - } - return buf.toString(); - } -} diff --git a/tool/test/org/antlr/v4/test/TestASTOps.java b/tool/test/org/antlr/v4/test/TestASTOps.java deleted file mode 100644 index 4074b17cb..000000000 --- a/tool/test/org/antlr/v4/test/TestASTOps.java +++ /dev/null @@ -1,835 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.test; - -import org.junit.Test; - -public class TestASTOps extends BaseTest { - protected boolean debug = false; - - @Test - public void testTokenList() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : ID INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "abc 34", debug); - assertEquals("abc 34\n", found); - } - - @Test public void testTokenListInSingleAltBlock() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : (ID INT) ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "abc 34", debug); - assertEquals("abc 34\n", found); - } - - @Test public void testSimpleChoiceBlock() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : (ID{;}|INT) ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "abc", debug); - assertEquals("abc\n", found); - } - - @Test public void testSimpleRootAtOuterLevel() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : ID^ INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "abc 34", debug); - assertEquals("(abc 34)\n", found); - } - - @Test public void testSimpleRootAtOuterLevelReverse() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : INT ID^ ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "34 abc", debug); - assertEquals("(abc 34)\n", found); - } - - @Test public void testBang() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT! ID! INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34 dag 4532", debug); - assertEquals("abc 4532\n", found); - } - - @Test public void testOptionalThenRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ( ID INT )? ID^ ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a 1 b", debug); - assertEquals("(b a 1)\n", found); - } - - @Test public void testLabeledStringRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : v='void'^ ID ';' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "void foo;", debug); - assertEquals("(void foo ;)\n", found); - } - - @Test public void testWildcard() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : v='void'^ . ';' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "void foo;", debug); - assertEquals("(void foo ;)\n", found); - } - - @Test public void testWildcardRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : v='void' .^ ';' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "void foo;", debug); - assertEquals("(foo void ;)\n", found); - } - - @Test public void testWildcardRootWithLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : v='void' x=.^ ';' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "void foo;", debug); - assertEquals("(foo void ;)\n", found); - } - - @Test public void testWildcardRootWithListLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : v='void' x+=.^ ';' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "void foo;", debug); - assertEquals("(foo void ;)\n", found); - } - - @Test public void testWildcardBangWithListLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : v='void' x=.! ';' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "void foo;", debug); - assertEquals("void ;\n", found); - } - - @Test public void testRootRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID^ INT^ ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a 34 c", debug); - assertEquals("(34 a c)\n", found); - } - - @Test public void testRootRoot2() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT^ ID^ ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a 34 c", debug); - assertEquals("(c (34 a))\n", found); - } - - @Test public void testRootThenRootInLoop() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID^ (INT '*'^ ID)+ ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a 34 * b 9 * c", debug); - assertEquals("(* (* (a 34) b 9) c)\n", found); - } - - @Test public void testNestedSubrule() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : 'void' (({;}ID|INT) ID | 'null' ) ';' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "void a b;", debug); - assertEquals("void a b ;\n", found); - } - - @Test public void testInvokeRule() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : type ID ;\n" + - "type : {;}'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "int a", debug); - assertEquals("int a\n", found); - } - - @Test public void testInvokeRuleAsRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : type^ ID ;\n" + - "type : {;}'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "int a", debug); - assertEquals("(int a)\n", found); - } - - @Test public void testInvokeRuleAsRootWithLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : x=type^ ID ;\n" + - "type : {;}'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "int a", debug); - assertEquals("(int a)\n", found); - } - - @Test public void testInvokeRuleAsRootWithListLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : x+=type^ ID ;\n" + - "type : {;}'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "int a", debug); - assertEquals("(int a)\n", found); - } - - @Test public void testRuleRootInLoop() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID ('+'^ ID)* ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a+b+c+d", debug); - assertEquals("(+ (+ (+ a b) c) d)\n", found); - } - - @Test public void testRuleInvocationRuleRootInLoop() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID (op^ ID)* ;\n" + - "op : {;}'+' | '-' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a+b+c-d", debug); - assertEquals("(- (+ (+ a b) c) d)\n", found); - } - - @Test public void testTailRecursion() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "s : a ;\n" + - "a : atom ('exp'^ a)? ;\n" + - "atom : INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "s", "3 exp 4 exp 5", debug); - assertEquals("(exp 3 (exp 4 5))\n", found); - } - - @Test public void testSet() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID|INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc", debug); - assertEquals("abc\n", found); - } - - @Test public void testSetRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ('+' | '-')^ ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "+abc", debug); - assertEquals("(+ abc)\n", found); - } - - @Test - public void testSetRootWithLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : x=('+' | '-')^ ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "+abc", debug); - assertEquals("(+ abc)\n", found); - } - - @Test public void testSetAsRuleRootInLoop() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID (('+'|'-')^ ID)* ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a+b-c", debug); - assertEquals("(- (+ a b) c)\n", found); - } - - @Test public void testNotSet() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ~ID '+' INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "34+2", debug); - assertEquals("34 + 2\n", found); - } - - @Test public void testNotSetWithLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : x=~ID '+' INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "34+2", debug); - assertEquals("34 + 2\n", found); - } - - @Test public void testNotSetWithListLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : x=~ID '+' INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "34+2", debug); - assertEquals("34 + 2\n", found); - } - - @Test public void testNotSetRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ~'+'^ INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "34 55", debug); - assertEquals("(34 55)\n", found); - } - - @Test public void testNotSetRootWithLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ~'+'^ INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "34 55", debug); - assertEquals("(34 55)\n", found); - } - - @Test public void testNotSetRootWithListLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ~'+'^ INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "34 55", debug); - assertEquals("(34 55)\n", found); - } - - @Test public void testNotSetRuleRootInLoop() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : INT (~INT^ INT)* ;\n" + - "blort : '+' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "PLUS : '+';\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "3+4+5", debug); - assertEquals("(+ (+ 3 4) 5)\n", found); - } - - @Test public void testTokenLabelReuse() throws Exception { - // check for compilation problem due to multiple defines - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : id=ID id=ID {System.out.print(\"2nd id=\"+$id.text+';');} ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("2nd id=b;a b\n", found); - } - - @Test public void testTokenLabelReuse2() throws Exception { - // check for compilation problem due to multiple defines - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : id=ID id=ID^ {System.out.print(\"2nd id=\"+$id.text+';');} ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("2nd id=b;(b a)\n", found); - } - - @Test public void testTokenListLabelReuse() throws Exception { - // check for compilation problem due to multiple defines - // make sure ids has both ID tokens - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ids+=ID ids+=ID {System.out.print(\"id list=\"+$ids+';');} ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - String expecting = "id list=[[@0,0:0='a',<3>,1:0], [@2,2:2='b',<3>,1:2]];a b\n"; - assertEquals(expecting, found); - } - - @Test public void testTokenListLabelReuse2() throws Exception { - // check for compilation problem due to multiple defines - // make sure ids has both ID tokens - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ids+=ID^ ids+=ID {System.out.print(\"id list=\"+$ids+';');} ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - String expecting = "id list=[[@0,0:0='a',<3>,1:0], [@2,2:2='b',<3>,1:2]];(a b)\n"; - assertEquals(expecting, found); - } - - @Test public void testTokenListLabelRuleRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : id+=ID^ ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testTokenListLabelBang() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : id+=ID! ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("", found); - } - - @Test public void testRuleListLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : x+=b x+=b {" + - "Tree t=(Tree)$x.get(1).tree;" + - "System.out.print(\"2nd x=\"+t.toStringTree()+';');} ;\n" + - "b : ID;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("2nd x=b;a b\n", found); - } - - @Test public void testRuleListLabelRuleRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ( x+=b^ )+ {" + - "System.out.print(\"x=\"+((CommonAST)$x.get(1).tree).toStringTree()+';');} ;\n" + - "b : ID;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("x=(b a);(b a)\n", found); - } - - @Test public void testRuleListLabelBang() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : x+=b! x+=b {" + - "System.out.print(\"1st x=\"+((CommonAST)$x.get(0).tree).toStringTree()+';');} ;\n" + - "b : ID;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("1st x=a;b\n", found); - } - - @Test public void testComplicatedMelange() throws Exception { - // check for compilation problem - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : A b=B b=B c+=C c+=C D {String s = $D.text;} ;\n" + - "A : 'a' ;\n" + - "B : 'b' ;\n" + - "C : 'c' ;\n" + - "D : 'd' ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b b c c d", debug); - assertEquals("a b b c c d\n", found); - } - - @Test public void testReturnValueWithAST() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : ID b {System.out.println($b.i);} ;\n" + - "b returns [int i] : INT {$i=Integer.parseInt($INT.text);} ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "abc 34", debug); - assertEquals("34\nabc 34\n", found); - } - - @Test public void testSetLoop() throws Exception { - String grammar = - "grammar T;\n" + - "options { output=AST; }\n" + - "r : (INT|ID)+ ; \n" + - "ID : 'a'..'z' + ;\n" + - "INT : '0'..'9' +;\n" + - "WS: (' ' | '\\n' | '\\t')+ {$channel = HIDDEN;};\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "r", "abc 34 d", debug); - assertEquals("abc 34 d\n", found); - } - - @Test public void testExtraTokenInSimpleDecl() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "decl : type^ ID '='! INT ';'! ;\n" + - "type : 'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "decl", "int 34 x=1;", debug); - assertEquals("line 1:4 extraneous input '34' expecting ID\n", this.stderrDuringParse); - assertEquals("(int x 1)\n", found); // tree gets correct x and 1 tokens - } - - @Test public void testMissingIDInSimpleDecl() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "tokens {EXPR;}\n" + - "decl : type^ ID '='! INT ';'! ;\n" + - "type : 'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "decl", "int =1;", debug); - assertEquals("line 1:4 missing ID at '='\n", this.stderrDuringParse); - assertEquals("(int 1)\n", found); // tree gets invented ID token - } - - @Test public void testMissingSetInSimpleDecl() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "tokens {EXPR;}\n" + - "decl : type^ ID '='! INT ';'! ;\n" + - "type : 'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "decl", "x=1;", debug); - assertEquals("line 1:0 mismatched input 'x' expecting set null\n", this.stderrDuringParse); - assertEquals("( x 1)\n", found); // tree gets invented ID token - } - - @Test public void testMissingTokenGivesErrorNode() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : ID INT ;\n" + // follow is EOF - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "abc", debug); - assertEquals("line 1:3 missing INT at ''\n", this.stderrDuringParse); - assertEquals("abc \n", found); - } - - @Test public void testMissingTokenGivesErrorNodeInInvokedRule() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : b ;\n" + - "b : ID INT ;\n" + // follow should see EOF - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "abc", debug); - assertEquals("line 1:3 missing INT at ''\n", this.stderrDuringParse); - assertEquals("abc \n", found); - } - - @Test public void testExtraTokenGivesErrorNode() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : b c ;\n" + - "b : ID ;\n" + - "c : INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "abc ick 34", debug); - assertEquals("line 1:4 extraneous input 'ick' expecting INT\n", this.stderrDuringParse); - assertEquals("abc 34\n", found); - } - - @Test public void testMissingFirstTokenGivesErrorNode() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : ID INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "34", debug); - assertEquals("line 1:0 missing ID at '34'\n", this.stderrDuringParse); - assertEquals(" 34\n", found); - } - - @Test public void testMissingFirstTokenGivesErrorNode2() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : b c ;\n" + - "b : ID ;\n" + - "c : INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "34", debug); - // finds an error at the first token, 34, and re-syncs. - // re-synchronizing does not consume a token because 34 follows - // ref to rule b (start of c). It then matches 34 in c. - assertEquals("line 1:0 missing ID at '34'\n", this.stderrDuringParse); - assertEquals(" 34\n", found); - } - - @Test public void testNoViableAltGivesErrorNode() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : b | c ;\n" + - "b : ID ;\n" + - "c : INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "S : '*' ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "*", debug); - assertEquals("line 1:0 no viable alternative at input '*'\n", this.stderrDuringParse); - assertEquals("", found); - } - - - // S U P P O R T - - public void _test() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "abc 34", debug); - assertEquals("\n", found); - } - -} diff --git a/tool/test/org/antlr/v4/test/TestASTStructure.gunit b/tool/test/org/antlr/v4/test/TestASTStructure.gunit index 9210b7fab..741e7680d 100644 --- a/tool/test/org/antlr/v4/test/TestASTStructure.gunit +++ b/tool/test/org/antlr/v4/test/TestASTStructure.gunit @@ -17,14 +17,12 @@ grammarSpec: << parser grammar P; - options {k=2; output=AST;} tokens { A; B='33'; } @header {foo} a : A; >> -> (PARSER_GRAMMAR P - (OPTIONS (= k 2) (= output AST)) (tokens { A (= B '33')) (@ header {foo}) (RULES (RULE a (BLOCK (ALT A))))) @@ -33,14 +31,12 @@ grammarSpec: parser grammar P; @header {foo} tokens { A; B='33'; } - options {k=2; ASTLabel=a.b.c; output=AST;} a : A; >> -> (PARSER_GRAMMAR P (@ header {foo}) (tokens { A (= B '33')) - (OPTIONS (= k 2) (= ASTLabel a.b.c) (= output AST)) (RULES (RULE a (BLOCK (ALT A))))) << @@ -123,80 +119,12 @@ rule: >> -> (RULE a int i (throws a.b.c) (BLOCK (ALT A))) -block: - "( ^(A B) | ^(b C) )" -> (BLOCK (ALT ("^(" A DOWN B UP)) (ALT ("^(" b DOWN C UP))) - ebnf: "(A|B)" -> (BLOCK (ALT A) (ALT B)) "(A|B)?" -> (? (BLOCK (ALT A) (ALT B))) "(A|B)*" -> (* (BLOCK (ALT A) (ALT B))) "(A|B)+" -> (+ (BLOCK (ALT A) (ALT B))) -alternative: - "x+=ID* -> $x*" -> - (ALT_REWRITE - (ALT (* (BLOCK (ALT (+= x ID))))) - (-> (REWRITE_SEQ (* (REWRITE_BLOCK (REWRITE_SEQ x)))))) - - "A -> ..." -> (ALT_REWRITE (ALT A) (-> ...)) - "A -> " -> (ALT_REWRITE (ALT A) (-> EPSILON)) - - "A -> foo(a={x}, b={y})" -> - (ALT_REWRITE - (ALT A) - (-> (TEMPLATE foo (ARGLIST (= a {x}) (= b {y}))))) - - "A -> template(a={x}, b={y}) <>" -> - (ALT_REWRITE - (ALT A) - (-> (TEMPLATE (ARGLIST (= a {x}) (= b {y})) <>))) - - "A -> ({name})()" -> (ALT_REWRITE (ALT A) (-> (TEMPLATE {name}))) - - "A -> {expr}" -> (ALT_REWRITE (ALT A) (-> (REWRITE_SEQ {expr}))) - - << - A -> {p1}? {e1} - -> {e2} - -> - >> - -> - ( ALT_REWRITE - (ALT A) - (-> {p1}? (REWRITE_SEQ {e1})) - (-> (REWRITE_SEQ {e2})) - ) - - "A -> A" -> (ALT_REWRITE (ALT A) (-> (REWRITE_SEQ A))) - - "a -> a" -> (ALT_REWRITE (ALT a) (-> (REWRITE_SEQ a))) - - "a A X? Y* -> A a ^(TOP X)? Y*" -> - (ALT_REWRITE - (ALT a A (? (BLOCK (ALT X))) (* (BLOCK (ALT Y)))) - (-> (REWRITE_SEQ - A a - (? (REWRITE_BLOCK (REWRITE_SEQ ("^(" TOP X)))) - (* (REWRITE_BLOCK (REWRITE_SEQ Y)))))) - - "A -> A[33]" -> (ALT_REWRITE (ALT A) (-> (REWRITE_SEQ (A 33)))) - - "A -> 'int' ^(A A)*" -> - (ALT_REWRITE - (ALT A) - (-> (REWRITE_SEQ 'int' (* (REWRITE_BLOCK (REWRITE_SEQ ("^(" A A))))))) - - << - A -> {p1}? A - -> {p2}? B - -> - >> - -> - (ALT_REWRITE (ALT A) - (-> {p1}? (REWRITE_SEQ A)) - (-> {p2}? (REWRITE_SEQ B)) - (-> EPSILON)) - element: "~A" -> (~ (SET A)) "b+" -> (+ (BLOCK (ALT b))) @@ -224,6 +152,3 @@ element: "x+=b+" -> (+ (BLOCK (ALT (+= x b)))) "('*'^)*" -> (* (BLOCK (ALT (^ '*')))) "({blort} 'x')*" -> (* (BLOCK (ALT {blort} 'x'))) - "A!" -> (! A) - "A^" -> (^ A) - "x=A^" -> (= x (^ A)) diff --git a/tool/test/org/antlr/v4/test/TestASTStructure.java b/tool/test/org/antlr/v4/test/TestASTStructure.java index b8c143aa2..a14ba320d 100644 --- a/tool/test/org/antlr/v4/test/TestASTStructure.java +++ b/tool/test/org/antlr/v4/test/TestASTStructure.java @@ -22,469 +22,329 @@ public class TestASTStructure extends org.antlr.v4.gunit.gUnitBase { @Test public void test_grammarSpec2() throws Exception { // gunit test on line 18 - RuleReturnScope rstruct = (RuleReturnScope)execParser("grammarSpec", "\n parser grammar P;\n options {k=2; output=AST;}\n tokens { A; B='33'; }\n @header {foo}\n a : A;\n ", 18); + RuleReturnScope rstruct = (RuleReturnScope)execParser("grammarSpec", "\n parser grammar P;\n tokens { A; B='33'; }\n @header {foo}\n a : A;\n ", 18); Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(PARSER_GRAMMAR P (OPTIONS (= k 2) (= output AST)) (tokens { A (= B '33')) (@ header {foo}) (RULES (RULE a (BLOCK (ALT A)))))"; + Object expecting = "(PARSER_GRAMMAR P (tokens { A (= B '33')) (@ header {foo}) (RULES (RULE a (BLOCK (ALT A)))))"; assertEquals("testing rule grammarSpec", expecting, actual); } @Test public void test_grammarSpec3() throws Exception { - // gunit test on line 32 - RuleReturnScope rstruct = (RuleReturnScope)execParser("grammarSpec", "\n parser grammar P;\n @header {foo}\n tokens { A; B='33'; }\n options {k=2; ASTLabel=a.b.c; output=AST;}\n a : A;\n ", 32); + // gunit test on line 30 + RuleReturnScope rstruct = (RuleReturnScope)execParser("grammarSpec", "\n parser grammar P;\n @header {foo}\n tokens { A; B='33'; }\n a : A;\n ", 30); Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(PARSER_GRAMMAR P (@ header {foo}) (tokens { A (= B '33')) (OPTIONS (= k 2) (= ASTLabel a.b.c) (= output AST)) (RULES (RULE a (BLOCK (ALT A)))))"; + Object expecting = "(PARSER_GRAMMAR P (@ header {foo}) (tokens { A (= B '33')) (RULES (RULE a (BLOCK (ALT A)))))"; assertEquals("testing rule grammarSpec", expecting, actual); } @Test public void test_grammarSpec4() throws Exception { - // gunit test on line 46 - RuleReturnScope rstruct = (RuleReturnScope)execParser("grammarSpec", "\n parser grammar P;\n import A=B, C;\n a : A;\n ", 46); + // gunit test on line 42 + RuleReturnScope rstruct = (RuleReturnScope)execParser("grammarSpec", "\n parser grammar P;\n import A=B, C;\n a : A;\n ", 42); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(PARSER_GRAMMAR P (import (= A B) C) (RULES (RULE a (BLOCK (ALT A)))))"; assertEquals("testing rule grammarSpec", expecting, actual); } @Test public void test_delegateGrammars1() throws Exception { - // gunit test on line 57 - RuleReturnScope rstruct = (RuleReturnScope)execParser("delegateGrammars", "import A;", 57); + // gunit test on line 53 + RuleReturnScope rstruct = (RuleReturnScope)execParser("delegateGrammars", "import A;", 53); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(import A)"; assertEquals("testing rule delegateGrammars", expecting, actual); } @Test public void test_rule1() throws Exception { - // gunit test on line 60 - RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "a : A;", 60); + // gunit test on line 56 + RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "a : A;", 56); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(RULE a (BLOCK (ALT (A (ELEMENT_OPTIONS X (= Y a.b.c))))))"; assertEquals("testing rule rule", expecting, actual); } @Test public void test_rule2() throws Exception { - // gunit test on line 62 - RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "A : B+;", 62); + // gunit test on line 58 + RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "A : B+;", 58); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(RULE A (BLOCK (ALT (+ (BLOCK (ALT B))))))"; assertEquals("testing rule rule", expecting, actual); } @Test public void test_rule3() throws Exception { - // gunit test on line 64 - RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "\n public a[int i] returns [int y]\n options {backtrack=true;}\n @init {blort}\n : ID ;\n ", 64); + // gunit test on line 60 + RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "\n public a[int i] returns [int y]\n options {backtrack=true;}\n @init {blort}\n : ID ;\n ", 60); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(RULE a (RULEMODIFIERS public) int i (returns int y) (OPTIONS (= backtrack true)) (@ init {blort}) (BLOCK (ALT ID)))"; assertEquals("testing rule rule", expecting, actual); } @Test public void test_rule4() throws Exception { - // gunit test on line 79 - RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "\n a[int i] returns [int y]\n @init {blort}\n options {backtrack=true;}\n : ID;\n ", 79); + // gunit test on line 75 + RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "\n a[int i] returns [int y]\n @init {blort}\n options {backtrack=true;}\n : ID;\n ", 75); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(RULE a int i (returns int y) (@ init {blort}) (OPTIONS (= backtrack true)) (BLOCK (ALT ID)))"; assertEquals("testing rule rule", expecting, actual); } @Test public void test_rule5() throws Exception { - // gunit test on line 92 - RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "\n a : ID ;\n catch[A b] {foo}\n finally {bar}\n ", 92); + // gunit test on line 88 + RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "\n a : ID ;\n catch[A b] {foo}\n finally {bar}\n ", 88); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(RULE a (BLOCK (ALT ID)) (catch A b {foo}) (finally {bar}))"; assertEquals("testing rule rule", expecting, actual); } @Test public void test_rule6() throws Exception { - // gunit test on line 101 - RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "\n a : ID ;\n catch[A a] {foo}\n catch[B b] {fu}\n finally {bar}\n ", 101); + // gunit test on line 97 + RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "\n a : ID ;\n catch[A a] {foo}\n catch[B b] {fu}\n finally {bar}\n ", 97); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(RULE a (BLOCK (ALT ID)) (catch A a {foo}) (catch B b {fu}) (finally {bar}))"; assertEquals("testing rule rule", expecting, actual); } @Test public void test_rule7() throws Exception { - // gunit test on line 111 - RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "\n\ta[int i]\n\tlocals [int a, float b]\n\t\t:\tA\n\t\t;\n\t", 111); + // gunit test on line 107 + RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "\n\ta[int i]\n\tlocals [int a, float b]\n\t\t:\tA\n\t\t;\n\t", 107); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(RULE a int i (locals int a, float b) (BLOCK (ALT A)))"; assertEquals("testing rule rule", expecting, actual); } @Test public void test_rule8() throws Exception { - // gunit test on line 119 - RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "\n\ta[int i] throws a.b.c\n\t\t:\tA\n\t\t;\n\t", 119); + // gunit test on line 115 + RuleReturnScope rstruct = (RuleReturnScope)execParser("rule", "\n\ta[int i] throws a.b.c\n\t\t:\tA\n\t\t;\n\t", 115); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(RULE a int i (throws a.b.c) (BLOCK (ALT A)))"; assertEquals("testing rule rule", expecting, actual); - } @Test public void test_block1() throws Exception { - // gunit test on line 127 - RuleReturnScope rstruct = (RuleReturnScope)execParser("block", "( ^(A B) | ^(b C) )", 127); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(BLOCK (ALT (^( A DOWN B UP)) (ALT (^( b DOWN C UP)))"; - assertEquals("testing rule block", expecting, actual); } @Test public void test_ebnf1() throws Exception { - // gunit test on line 130 - RuleReturnScope rstruct = (RuleReturnScope)execParser("ebnf", "(A|B)", 130); + // gunit test on line 123 + RuleReturnScope rstruct = (RuleReturnScope)execParser("ebnf", "(A|B)", 123); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(BLOCK (ALT A) (ALT B))"; assertEquals("testing rule ebnf", expecting, actual); } @Test public void test_ebnf2() throws Exception { - // gunit test on line 131 - RuleReturnScope rstruct = (RuleReturnScope)execParser("ebnf", "(A|B)?", 131); + // gunit test on line 124 + RuleReturnScope rstruct = (RuleReturnScope)execParser("ebnf", "(A|B)?", 124); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(? (BLOCK (ALT A) (ALT B)))"; assertEquals("testing rule ebnf", expecting, actual); } @Test public void test_ebnf3() throws Exception { - // gunit test on line 132 - RuleReturnScope rstruct = (RuleReturnScope)execParser("ebnf", "(A|B)*", 132); + // gunit test on line 125 + RuleReturnScope rstruct = (RuleReturnScope)execParser("ebnf", "(A|B)*", 125); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(* (BLOCK (ALT A) (ALT B)))"; assertEquals("testing rule ebnf", expecting, actual); } @Test public void test_ebnf4() throws Exception { - // gunit test on line 133 - RuleReturnScope rstruct = (RuleReturnScope)execParser("ebnf", "(A|B)+", 133); + // gunit test on line 126 + RuleReturnScope rstruct = (RuleReturnScope)execParser("ebnf", "(A|B)+", 126); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(+ (BLOCK (ALT A) (ALT B)))"; assertEquals("testing rule ebnf", expecting, actual); - } @Test public void test_alternative1() throws Exception { - // gunit test on line 136 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "x+=ID* -> $x*", 136); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT (* (BLOCK (ALT (+= x ID))))) (-> (REWRITE_SEQ (* (REWRITE_BLOCK (REWRITE_SEQ x))))))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative2() throws Exception { - // gunit test on line 141 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "A -> ...", 141); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT A) (-> ...))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative3() throws Exception { - // gunit test on line 142 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "A -> ", 142); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT A) (-> EPSILON))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative4() throws Exception { - // gunit test on line 144 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "A -> foo(a={x}, b={y})", 144); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT A) (-> (TEMPLATE foo (ARGLIST (= a {x}) (= b {y})))))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative5() throws Exception { - // gunit test on line 149 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "A -> template(a={x}, b={y}) <>", 149); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT A) (-> (TEMPLATE (ARGLIST (= a {x}) (= b {y})) <>)))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative6() throws Exception { - // gunit test on line 154 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "A -> ({name})()", 154); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT A) (-> (TEMPLATE {name})))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative7() throws Exception { - // gunit test on line 156 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "A -> {expr}", 156); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT A) (-> (REWRITE_SEQ {expr})))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative8() throws Exception { - // gunit test on line 158 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "\n A -> {p1}? {e1}\n -> {e2}\n ->\n ", 158); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT A) (-> {p1}? (REWRITE_SEQ {e1})) (-> (REWRITE_SEQ {e2})))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative9() throws Exception { - // gunit test on line 170 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "A -> A", 170); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT A) (-> (REWRITE_SEQ A)))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative10() throws Exception { - // gunit test on line 172 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "a -> a", 172); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT a) (-> (REWRITE_SEQ a)))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative11() throws Exception { - // gunit test on line 174 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "a A X? Y* -> A a ^(TOP X)? Y*", 174); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT a A (? (BLOCK (ALT X))) (* (BLOCK (ALT Y)))) (-> (REWRITE_SEQ A a (? (REWRITE_BLOCK (REWRITE_SEQ (^( TOP X)))) (* (REWRITE_BLOCK (REWRITE_SEQ Y))))))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative12() throws Exception { - // gunit test on line 182 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "A -> A[33]", 182); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT A) (-> (REWRITE_SEQ (A 33))))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative13() throws Exception { - // gunit test on line 184 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "A -> 'int' ^(A A)*", 184); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT A) (-> (REWRITE_SEQ 'int' (* (REWRITE_BLOCK (REWRITE_SEQ (^( A A)))))))"; - assertEquals("testing rule alternative", expecting, actual); - } - - @Test public void test_alternative14() throws Exception { - // gunit test on line 189 - RuleReturnScope rstruct = (RuleReturnScope)execParser("alternative", "\n A -> {p1}? A\n -> {p2}? B\n ->\n ", 189); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(ALT_REWRITE (ALT A) (-> {p1}? (REWRITE_SEQ A)) (-> {p2}? (REWRITE_SEQ B)) (-> EPSILON))"; - assertEquals("testing rule alternative", expecting, actual); } @Test public void test_element1() throws Exception { - // gunit test on line 201 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "~A", 201); + // gunit test on line 129 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "~A", 129); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(~ (SET A))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element2() throws Exception { - // gunit test on line 202 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "b+", 202); + // gunit test on line 130 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "b+", 130); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(+ (BLOCK (ALT b)))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element3() throws Exception { - // gunit test on line 203 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "(b)+", 203); + // gunit test on line 131 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "(b)+", 131); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(+ (BLOCK (ALT b)))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element4() throws Exception { - // gunit test on line 204 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "b?", 204); + // gunit test on line 132 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "b?", 132); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(? (BLOCK (ALT b)))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element5() throws Exception { - // gunit test on line 205 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "(b)?", 205); + // gunit test on line 133 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "(b)?", 133); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(? (BLOCK (ALT b)))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element6() throws Exception { - // gunit test on line 206 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "(b)*", 206); + // gunit test on line 134 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "(b)*", 134); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(* (BLOCK (ALT b)))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element7() throws Exception { - // gunit test on line 207 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "b*", 207); + // gunit test on line 135 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "b*", 135); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(* (BLOCK (ALT b)))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element8() throws Exception { - // gunit test on line 208 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "'while'*", 208); + // gunit test on line 136 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "'while'*", 136); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(* (BLOCK (ALT 'while')))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element9() throws Exception { - // gunit test on line 209 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "'a'+", 209); + // gunit test on line 137 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "'a'+", 137); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(+ (BLOCK (ALT 'a')))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element10() throws Exception { - // gunit test on line 210 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "a[3]", 210); + // gunit test on line 138 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "a[3]", 138); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(a 3)"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element11() throws Exception { - // gunit test on line 211 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "'a'..'z'+", 211); + // gunit test on line 139 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "'a'..'z'+", 139); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(+ (BLOCK (ALT (.. 'a' 'z'))))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element12() throws Exception { - // gunit test on line 212 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=ID", 212); + // gunit test on line 140 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=ID", 140); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(= x ID)"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element13() throws Exception { - // gunit test on line 213 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=ID?", 213); + // gunit test on line 141 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=ID?", 141); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(? (BLOCK (ALT (= x ID))))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element14() throws Exception { - // gunit test on line 214 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=ID*", 214); + // gunit test on line 142 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=ID*", 142); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(* (BLOCK (ALT (= x ID))))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element15() throws Exception { - // gunit test on line 215 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=b", 215); + // gunit test on line 143 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=b", 143); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(= x b)"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element16() throws Exception { - // gunit test on line 216 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=(A|B)", 216); + // gunit test on line 144 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=(A|B)", 144); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(= x (BLOCK (ALT A) (ALT B)))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element17() throws Exception { - // gunit test on line 217 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=(A|B)^", 217); + // gunit test on line 145 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=(A|B)^", 145); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(= x (^ (BLOCK (ALT A) (ALT B))))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element18() throws Exception { - // gunit test on line 218 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=~(A|B)", 218); + // gunit test on line 146 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=~(A|B)", 146); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(= x (~ (SET A B)))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element19() throws Exception { - // gunit test on line 219 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x+=~(A|B)", 219); + // gunit test on line 147 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x+=~(A|B)", 147); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(+= x (~ (SET A B)))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element20() throws Exception { - // gunit test on line 220 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x+=~(A|B)+", 220); + // gunit test on line 148 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x+=~(A|B)+", 148); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(+ (BLOCK (ALT (+= x (~ (SET A B))))))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element21() throws Exception { - // gunit test on line 221 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=b+", 221); + // gunit test on line 149 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=b+", 149); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(+ (BLOCK (ALT (= x b))))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element22() throws Exception { - // gunit test on line 222 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x+=ID*", 222); + // gunit test on line 150 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x+=ID*", 150); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(* (BLOCK (ALT (+= x ID))))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element23() throws Exception { - // gunit test on line 223 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x+='int'*", 223); + // gunit test on line 151 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x+='int'*", 151); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(* (BLOCK (ALT (+= x 'int'))))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element24() throws Exception { - // gunit test on line 224 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x+=b+", 224); + // gunit test on line 152 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x+=b+", 152); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(+ (BLOCK (ALT (+= x b))))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element25() throws Exception { - // gunit test on line 225 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "('*'^)*", 225); + // gunit test on line 153 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "('*'^)*", 153); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(* (BLOCK (ALT (^ '*'))))"; assertEquals("testing rule element", expecting, actual); } @Test public void test_element26() throws Exception { - // gunit test on line 226 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "({blort} 'x')*", 226); + // gunit test on line 154 + RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "({blort} 'x')*", 154); Object actual = ((Tree)rstruct.getTree()).toStringTree(); Object expecting = "(* (BLOCK (ALT {blort} 'x')))"; assertEquals("testing rule element", expecting, actual); } - - @Test public void test_element27() throws Exception { - // gunit test on line 227 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "A!", 227); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(! A)"; - assertEquals("testing rule element", expecting, actual); - } - - @Test public void test_element28() throws Exception { - // gunit test on line 228 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "A^", 228); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(^ A)"; - assertEquals("testing rule element", expecting, actual); - } - - @Test public void test_element29() throws Exception { - // gunit test on line 229 - RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=A^", 229); - Object actual = ((Tree)rstruct.getTree()).toStringTree(); - Object expecting = "(= x (^ A))"; - assertEquals("testing rule element", expecting, actual); - } } \ No newline at end of file diff --git a/tool/test/org/antlr/v4/test/TestBasicSemanticErrors.java b/tool/test/org/antlr/v4/test/TestBasicSemanticErrors.java index e1a479dd8..9d06faa3f 100644 --- a/tool/test/org/antlr/v4/test/TestBasicSemanticErrors.java +++ b/tool/test/org/antlr/v4/test/TestBasicSemanticErrors.java @@ -36,9 +36,6 @@ public class TestBasicSemanticErrors extends BaseTest { // INPUT "grammar A;\n" + "\n" + - "options {\n" + - " output=template;\n" + - "}\n" + "\n" + "a : ID -> ID ;\n" + "\n" + @@ -51,10 +48,6 @@ public class TestBasicSemanticErrors extends BaseTest { // INPUT "tree grammar B;\n" + - "options {\n" + - "\tfilter=true;\n" + - "\toutput=template;\n" + - "}\n" + "\n" + "a : A;\n" + "\n" + @@ -97,10 +90,6 @@ public class TestBasicSemanticErrors extends BaseTest { // INPUT "tree grammar V;\n" + - "options {\n" + - " rewrite=true;\n" + - " output=template;\n" + - "}\n" + "a : A\n" + " | A B -> template() \"kjsfdkdsj\" \n" + " ;", @@ -118,16 +107,6 @@ public class TestBasicSemanticErrors extends BaseTest { "warning(47): V.g:3:8: illegal option rewrite\n" }; - static String[] C = { - "parser grammar C;\n" + - "options {output=AST;}\n" + - "tokens { A; B; C; }\n" + - "a : A -> B $a A ;", // no problem with or $a. - - "" - }; - @Test public void testA() { super.testErrors(A, false); } @Test public void testU() { super.testErrors(U, false); } - @Test public void testE() { super.testErrors(C, false); } } diff --git a/tool/test/org/antlr/v4/test/TestBufferedASTNodeStream.java b/tool/test/org/antlr/v4/test/TestBufferedASTNodeStream.java deleted file mode 100644 index 548de510b..000000000 --- a/tool/test/org/antlr/v4/test/TestBufferedASTNodeStream.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * [The "BSD license"] - * Copyright (c) 2010 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.test; - -import org.antlr.v4.runtime.CommonToken; -import org.antlr.v4.runtime.tree.*; -import org.junit.Test; - -public class TestBufferedASTNodeStream extends TestASTNodeStream { - // inherits tests; these methods make it use a new buffer - - public ASTNodeStream newStream(Object t) { - return new BufferedASTNodeStream(t); - } - - public String toTokenTypeString(ASTNodeStream stream) { - return ((BufferedASTNodeStream)stream).toTokenTypeString(); - } - - @Test public void testSeek() throws Exception { - // ^(101 ^(102 103 ^(106 107) ) 104 105) - // stream has 7 real + 6 nav nodes - // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF - BaseAST r0 = new CommonAST(new CommonToken(101)); - BaseAST r1 = new CommonAST(new CommonToken(102)); - r0.addChild(r1); - r1.addChild(new CommonAST(new CommonToken(103))); - BaseAST r2 = new CommonAST(new CommonToken(106)); - r2.addChild(new CommonAST(new CommonToken(107))); - r1.addChild(r2); - r0.addChild(new CommonAST(new CommonToken(104))); - r0.addChild(new CommonAST(new CommonToken(105))); - - ASTNodeStream stream = newStream(r0); - stream.consume(); // consume 101 - stream.consume(); // consume DN - stream.consume(); // consume 102 - stream.seek(7); // seek to 107 - assertEquals(107, ((AST)stream.LT(1)).getType()); - stream.consume(); // consume 107 - stream.consume(); // consume UP - stream.consume(); // consume UP - assertEquals(104, ((AST)stream.LT(1)).getType()); - } -} diff --git a/tool/test/org/antlr/v4/test/TestFullContextParsing.java b/tool/test/org/antlr/v4/test/TestFullContextParsing.java index 3fa196d46..6cd9f380e 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: {1..2}:[(1,1,[]), (1,2,[])],conflictingAlts={1..2}, input=abc\n", + assertEquals("line 1:0 reportAmbiguity d=0: ambigAlts={1..2}:[(1,1,[]), (1,2,[])],conflictingAlts={1..2}, input='abc'\n", this.stderrDuringParse); } @@ -77,7 +77,8 @@ public class TestFullContextParsing extends BaseTest { "s0-INT->s1\n" + "s1-ID->s2^\n"; assertEquals(expecting, result); - assertEquals("line 1:2 reportContextSensitivity d=1: [(20,1,[10])],uniqueAlt=1, input=34\n", + 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", this.stderrDuringParse); result = execParser("T.g", grammar, "TParser", "TLexer", "s", @@ -87,7 +88,8 @@ public class TestFullContextParsing extends BaseTest { "s0-INT->s1\n" + "s1-ID->s2^\n"; assertEquals(expecting, result); - assertEquals("line 1:5 reportContextSensitivity d=1: [(1,2,[])],uniqueAlt=2, input=34abc\n", + 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", this.stderrDuringParse); } @@ -114,8 +116,10 @@ public class TestFullContextParsing extends BaseTest { "s0-INT->s1\n" + "s1-ID->s2^\n"; assertEquals(expecting, result); - assertEquals("line 1:2 reportContextSensitivity d=2: [(22,1,[10])],uniqueAlt=1, input=34\n" + - "line 1:14 reportContextSensitivity d=2: [(8,2,[]), (12,2,[]), (1,2,[])],uniqueAlt=2, input=34abc\n", + 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:17 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", this.stderrDuringParse); } @@ -157,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: {1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2}, input=else\n", + assertEquals("line 1:29 reportAmbiguity d=1: ambigAlts={1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2},dipsIntoOuterContext, input='else'\n", this.stderrDuringParse); input = "{ if x then return else foo }"; @@ -177,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: {1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2}, input=else\n", + assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2},dipsIntoOuterContext, input='else'\n", this.stderrDuringParse); input = "{ if x then return else foo }"; @@ -191,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: {1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2}, input=else\n", + assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2},dipsIntoOuterContext, input='else'\n", this.stderrDuringParse); input = @@ -208,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: {1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2}, input=else\n", + assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2},dipsIntoOuterContext, input='else'\n", this.stderrDuringParse); input = @@ -225,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: {1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2}, input=else\n", + assertEquals("line 1:19 reportAmbiguity d=1: ambigAlts={1..2}:[(25,1,[]), (25,2,[],up=1)],conflictingAlts={1..2},dipsIntoOuterContext, input='else'\n", this.stderrDuringParse); } diff --git a/tool/test/org/antlr/v4/test/TestHeteroAST.java b/tool/test/org/antlr/v4/test/TestHeteroAST.java deleted file mode 100644 index 621472b56..000000000 --- a/tool/test/org/antlr/v4/test/TestHeteroAST.java +++ /dev/null @@ -1,545 +0,0 @@ -/* - * [The "BSD license"] - * Copyright (c) 2010 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.test; - -import org.junit.*; - -/** Test hetero trees in parsers and tree parsers */ -@Ignore public class TestHeteroAST extends BaseTest { - protected boolean debug = false; - - // TODO: make these work! - // PARSERS -- AUTO AST - - @Test public void testToken() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testTokenCommonAST() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testTokenWithQualifiedType() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : ID ;\n"+ // TParser.V is qualified name - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testNamedType() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - - @Test public void testTokenWithLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : x=ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testTokenWithListLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : x+=ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testTokenRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : ID^ ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testTokenRootWithListLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : x+=ID^ ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testString() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : 'begin' ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "begin", debug); - assertEquals("begin\n", found); - } - - @Test public void testStringRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : 'begin'^ ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "begin", debug); - assertEquals("begin\n", found); - } - - // PARSERS -- REWRITE AST - - @Test public void testRewriteToken() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : ID -> ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testRewriteTokenWithArgs() throws Exception { - // arg to ID[42,19,30] means you're constructing node not associated with ID - // so must pass in token manually - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {\n" + - "static class V extends CommonAST {\n" + - " public int x,y,z;\n"+ - " public V(int ttype, int x, int y, int z) { this.x=x; this.y=y; this.z=z; token=new CommonToken(ttype,\"\"); }\n" + - " public V(int ttype, Token t, int x) { token=t; this.x=x;}\n" + - " public String toString() { return (token!=null?token.getText():\"\")+\";\"+x+y+z;}\n" + - "}\n" + - "}\n"+ - "a : ID -> ID[42,19,30] ID[$ID,99] ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals(";421930 a;9900\n", found); - } - - @Test public void testRewriteTokenRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : ID INT -> ^(ID INT) ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a 2", debug); - assertEquals("(a 2)\n", found); - } - - @Test public void testRewriteString() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : 'begin' -> 'begin' ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "begin", debug); - assertEquals("begin\n", found); - } - - @Test public void testRewriteStringRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : 'begin' INT -> ^('begin' INT) ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "begin 2", debug); - assertEquals("(begin 2)\n", found); - } - - @Test public void testRewriteRuleResults() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {LIST;}\n" + - "@members {\n" + - "static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "static class W extends CommonAST {\n" + - " public W(int tokenType, String txt) { super(new CommonToken(tokenType,txt)); }\n" + - " public W(Token t) { token=t;}\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : id (',' id)* -> ^(LIST[\"LIST\"] id+);\n" + - "id : ID -> ID;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a,b,c", debug); - assertEquals("(LIST a b c)\n", found); - } - - @Test public void testCopySemanticsWithHetero() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "@members {\n" + - "static class V extends CommonAST {\n" + - " public V(Token t) { token=t;}\n" + // for 'int' - " public V(V node) { super(node); }\n\n" + // for dupNode - " public Tree dupNode() { return new V(this); }\n" + // for dup'ing type - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n" + - "a : type ID (',' ID)* ';' -> ^(type ID)+;\n" + - "type : 'int' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "int a, b, c;", debug); - assertEquals("(int a) (int b) (int c)\n", found); - } - - // TREE PARSERS -- REWRITE AST - - @Test public void testTreeParserRewriteFlatList() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP;\n"+ - "options {output=AST; ASTLabelType=CommonAST; tokenVocab=T;}\n" + - "@members {\n" + - "static class V extends CommonAST {\n" + - " public V(Object t) { super((CommonAST)t); }\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "static class W extends CommonAST {\n" + - " public W(Object t) { super((CommonAST)t); }\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : ID INT -> INT ID\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc 34"); - assertEquals("34 abc\n", found); - } - - @Test public void testTreeParserRewriteTree() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP;\n"+ - "options {output=AST; ASTLabelType=CommonAST; tokenVocab=T;}\n" + - "@members {\n" + - "static class V extends CommonAST {\n" + - " public V(Object t) { super((CommonAST)t); }\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "static class W extends CommonAST {\n" + - " public W(Object t) { super((CommonAST)t); }\n" + - " public String toString() { return token.getText()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : ID INT -> ^(INT ID)\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc 34"); - assertEquals("(34 abc)\n", found); - } - - @Test public void testTreeParserRewriteImaginary() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP;\n"+ - "options {output=AST; ASTLabelType=CommonAST; tokenVocab=T;}\n" + - "tokens { ROOT; }\n" + - "@members {\n" + - "class V extends CommonAST {\n" + - " public V(int tokenType) { super(new CommonToken(tokenType)); }\n" + - " public String toString() { return tokenNames[token.getType()]+\"\";}\n" + - "}\n" + - "}\n"+ - "a : ID -> ROOT ID\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc"); - assertEquals("ROOT abc\n", found); - } - - @Test public void testTreeParserRewriteImaginaryWithArgs() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP;\n"+ - "options {output=AST; ASTLabelType=CommonAST; tokenVocab=T;}\n" + - "tokens { ROOT; }\n" + - "@members {\n" + - "class V extends CommonAST {\n" + - " public int x;\n" + - " public V(int tokenType, int x) { super(new CommonToken(tokenType)); this.x=x;}\n" + - " public String toString() { return tokenNames[token.getType()]+\";\"+x;}\n" + - "}\n" + - "}\n"+ - "a : ID -> ROOT[42] ID\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc"); - assertEquals("ROOT;42 abc\n", found); - } - - @Test public void testTreeParserRewriteImaginaryRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP;\n"+ - "options {output=AST; ASTLabelType=CommonAST; tokenVocab=T;}\n" + - "tokens { ROOT; }\n" + - "@members {\n" + - "class V extends CommonAST {\n" + - " public V(int tokenType) { super(new CommonToken(tokenType)); }\n" + - " public String toString() { return tokenNames[token.getType()]+\"\";}\n" + - "}\n" + - "}\n"+ - "a : ID -> ^(ROOT ID)\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc"); - assertEquals("(ROOT abc)\n", found); - } - - @Test public void testTreeParserRewriteImaginaryFromReal() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP;\n"+ - "options {output=AST; ASTLabelType=CommonAST; tokenVocab=T;}\n" + - "tokens { ROOT; }\n" + - "@members {\n" + - "class V extends CommonAST {\n" + - " public V(int tokenType) { super(new CommonToken(tokenType)); }\n" + - " public V(int tokenType, Object tree) { super((CommonAST)tree); token.setType(tokenType); }\n" + - " public String toString() { return tokenNames[token.getType()]+\"@\"+token.getLine();}\n" + - "}\n" + - "}\n"+ - "a : ID -> ROOT[$ID]\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc"); - assertEquals("ROOT@1\n", found); // at line 1; shows copy of ID's stuff - } - - @Test public void testTreeParserAutoHeteroAST() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID ';' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP;\n"+ - "options {output=AST; ASTLabelType=CommonAST; tokenVocab=T;}\n" + - "tokens { ROOT; }\n" + - "@members {\n" + - "class V extends CommonAST {\n" + - " public V(CommonAST t) { super(t); }\n" + // NEEDS SPECIAL CTOR - " public String toString() { return super.toString()+\"\";}\n" + - "}\n" + - "}\n"+ - "a : ID ';'\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc;"); - assertEquals("abc ;\n", found); - } - -} diff --git a/tool/test/org/antlr/v4/test/TestLeftRecursion.java b/tool/test/org/antlr/v4/test/TestLeftRecursion.java index 889d3e938..7ccd384a1 100644 --- a/tool/test/org/antlr/v4/test/TestLeftRecursion.java +++ b/tool/test/org/antlr/v4/test/TestLeftRecursion.java @@ -39,12 +39,11 @@ public class TestLeftRecursion extends BaseTest { @Test public void testTernaryExpr() throws Exception { String grammar = "grammar T;\n" + - "options {output=AST;}\n" + "s : e EOF ;\n" + // must indicate EOF can follow or 'a' won't match - "e : e '*'^ e" + - " | e '+'^ e" + - " | e '?'^ e ':'! e" + - " | e '='^ e" + + "e : e '*' e" + + " | e '+' e" + + " | e '?' e ':' e" + + " | e '=' e" + " | ID" + " ;\n" + "ID : 'a'..'z'+ ;\n" + @@ -66,14 +65,13 @@ public class TestLeftRecursion extends BaseTest { @Test public void testDeclarationsUsingASTOperators() throws Exception { String grammar = "grammar T;\n" + - "options {output=AST;}\n" + "s : declarator EOF ;\n" + // must indicate EOF can follow "declarator\n" + - " : declarator '['^ e ']'!\n" + - " | declarator '['^ ']'!\n" + - " | declarator '('^ ')'!\n" + - " | '*'^ declarator\n" + // binds less tight than suffixes - " | '('! declarator ')'!\n" + + " : declarator '[' e ']'\n" + + " | declarator '[' ']'\n" + + " | declarator '(' ')'\n" + + " | '*' declarator\n" + // binds less tight than suffixes + " | '(' declarator ')'\n" + " | ID\n" + " ;\n" + "e : INT ;\n" + @@ -98,15 +96,14 @@ public class TestLeftRecursion extends BaseTest { @Test public void testDeclarationsUsingRewriteOperators() throws Exception { String grammar = "grammar T;\n" + - "options {output=AST;}\n" + "s : declarator EOF ;\n" + // must indicate EOF can follow "declarator\n" + - " : declarator '[' e ']' -> ^('[' declarator e)\n" + - " | declarator '[' ']' -> ^('[' declarator)\n" + - " | declarator '(' ')' -> ^('(' declarator)\n" + - " | '*' declarator -> ^('*' declarator) \n" + // binds less tight than suffixes - " | '(' declarator ')' -> declarator\n" + - " | ID -> ID\n" + + " : declarator '[' e ']'" + + " | declarator '[' ']'" + + " | declarator '(' ')'" + + " | '*' declarator" + // binds less tight than suffixes + " | '(' declarator ')'" + + " | ID" + " ;\n" + "e : INT ;\n" + "ID : 'a'..'z'+ ;\n" + @@ -130,13 +127,12 @@ public class TestLeftRecursion extends BaseTest { @Test public void testExpressionsUsingASTOperators() throws Exception { String grammar = "grammar T;\n" + - "options {output=AST;}\n" + "s : e EOF ;\n" + // must indicate EOF can follow - "e : e '.'^ ID\n" + - " | e '.'^ 'this'\n" + - " | '-'^ e\n" + - " | e '*'^ e\n" + - " | e ('+'^|'-'^) e\n" + + "e : e '.' ID\n" + + " | e '.' 'this'\n" + + " | '-' e\n" + + " | e '*' e\n" + + " | e ('+'|'-') e\n" + " | INT\n" + " | ID\n" + " ;\n" + @@ -160,50 +156,17 @@ public class TestLeftRecursion extends BaseTest { runTests(grammar, tests, "e"); } - @Test public void testExpressionsUsingRewriteOperators() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "s : e EOF ;\n" + // must indicate EOF can follow - "e : e '.' ID -> ^('.' e ID)\n" + - " | e '.' 'this' -> ^('.' e 'this')\n" + - " | '-' e -> ^('-' e)\n" + - " | e '*' b=e -> ^('*' e $b)\n" + - " | e (op='+'|op='-') b=e -> ^($op e $b)\n" + - " | INT -> INT\n" + - " | ID -> ID\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+ ;\n" + - "WS : (' '|'\\n') {skip();} ;\n"; - String[] tests = { - "a", "a", - "1", "1", - "a+1", "(+ a 1)", - "a*1", "(* a 1)", - "a.b", "(. a b)", - "a.this", "(. a this)", - "a+b*c", "(+ a (* b c))", - "a.b+1", "(+ (. a b) 1)", - "-a", "(- a)", - "-a+b", "(+ (- a) b)", - "-a.b", "(- (. a b))", - }; - runTests(grammar, tests, "e"); - } - @Test public void testExpressionAssociativity() throws Exception { String grammar = "grammar T;\n" + - "options {output=AST;}\n" + "s : e EOF ;\n" + // must indicate EOF can follow "e\n" + - " : e '.'^ ID\n" + - " | '-'^ e\n" + - " | e '^'^ e\n" + - " | e '*'^ e\n" + - " | e ('+'^|'-'^) e\n" + - " | e ('='^ |'+='^) e\n" + + " : e '.' ID\n" + + " | '-' e\n" + + " | e '' e\n" + + " | e '*' e\n" + + " | e ('+'|'-') e\n" + + " | e ('=' |'+=') e\n" + " | INT\n" + " | ID\n" + " ;\n" + @@ -237,57 +200,56 @@ public class TestLeftRecursion extends BaseTest { // this is simplified from real java String grammar = "grammar T;\n" + - "options {output=AST;}\n" + "s : e EOF ;\n" + // must indicate EOF can follow "expressionList\n" + - " : e (','! e)*\n" + + " : e (',' e)*\n" + " ;\n" + - "e : '('! e ')'!\n" + + "e : '(' e ')'\n" + " | 'this' \n" + " | 'super'\n" + " | INT\n" + " | ID\n" + - " | type '.'^ 'class'\n" + - " | e '.'^ ID\n" + - " | e '.'^ 'this'\n" + - " | e '.'^ 'super' '('^ expressionList? ')'!\n" + - " | e '.'^ 'new'^ ID '('! expressionList? ')'!\n" + - " | 'new'^ type ( '(' expressionList? ')'! | ('[' e ']'!)+)\n" + - " | e '['^ e ']'!\n" + - " | '('^ type ')'! e\n" + - " | e ('++'^ | '--'^)\n" + - " | e '('^ expressionList? ')'!\n" + - " | ('+'^|'-'^|'++'^|'--'^) e\n" + - " | ('~'^|'!'^) e\n" + - " | e ('*'^|'/'^|'%'^) e\n" + - " | e ('+'^|'-'^) e\n" + - " | e ('<<'^ | '>>>'^ | '>>'^) e\n" + - " | e ('<='^ | '>='^ | '>'^ | '<'^) e\n" + - " | e 'instanceof'^ e\n" + - " | e ('=='^ | '!='^) e\n" + - " | e '&'^ e\n" + - " | e '^'^ e\n" + - " | e '|'^ e\n" + - " | e '&&'^ e\n" + - " | e '||'^ e\n" + + " | type '.' 'class'\n" + + " | e '.' ID\n" + + " | e '.' 'this'\n" + + " | e '.' 'super' '(' expressionList? ')'\n" + + " | e '.' 'new' ID '(' expressionList? ')'\n" + + " | 'new' type ( '(' expressionList? ')' | ('[' e ']')+)\n" + + " | e '[' e ']'\n" + + " | '(' type ')' e\n" + + " | e ('++' | '--')\n" + + " | e '(' expressionList? ')'\n" + + " | ('+'|'-'|'++'|'--') e\n" + + " | ('~'|'!') e\n" + + " | e ('*'|'/'|'%') e\n" + + " | e ('+'|'-') e\n" + + " | e ('<<' | '>>>' | '>>') e\n" + + " | e ('<=' | '>=' | '>' | '<') e\n" + + " | e 'instanceof' e\n" + + " | e ('==' | '!=') e\n" + + " | e '&' e\n" + + " | e '^' e\n" + + " | e '|' e\n" + + " | e '&&' e\n" + + " | e '||' e\n" + " | e '?' e ':' e\n" + - " | e ('='^\n" + - " |'+='^\n" + - " |'-='^\n" + - " |'*='^\n" + - " |'/='^\n" + - " |'&='^\n" + - " |'|='^\n" + - " |'^='^\n" + - " |'>>='^\n" + - " |'>>>='^\n" + - " |'<<='^\n" + - " |'%='^) e\n" + + " | e ('='\n" + + " |'+='\n" + + " |'-='\n" + + " |'*='\n" + + " |'/='\n" + + " |'&='\n" + + " |'|='\n" + + " |'^='\n" + + " |'>>='\n" + + " |'>>>='\n" + + " |'<<='\n" + + " |'%=') e\n" + " ;\n" + "type: ID \n" + - " | ID '['^ ']'!\n" + + " | ID '[' ']'\n" + " | 'int'\n" + - " | 'int' '['^ ']'! \n" + + " | 'int' '[' ']' \n" + " ;\n" + "ID : ('a'..'z'|'A'..'Z'|'_'|'$')+;\n" + "INT : '0'..'9'+ ;\n" + @@ -346,11 +308,10 @@ public class TestLeftRecursion extends BaseTest { @Test public void testReturnValueAndActionsAndASTs() throws Exception { String grammar = "grammar T;\n" + - "options {output=AST;}\n" + "s : e {System.out.print(\"v=\"+$e.v+\", \");} ;\n" + "e returns [int v, List ignored]\n" + - " : e '*'^ b=e {$v *= $b.v;}\n" + - " | e '+'^ b=e {$v += $b.v;}\n" + + " : e '*' b=e {$v *= $b.v;}\n" + + " | e '+' b=e {$v += $b.v;}\n" + " | INT {$v = $INT.int;}\n" + " ;\n" + "INT : '0'..'9'+ ;\n" + @@ -364,18 +325,10 @@ public class TestLeftRecursion extends BaseTest { public void runTests(String grammar, String[] tests, String startRule) { rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer", debug); - boolean parserBuildsTrees = - grammar.indexOf("output=AST")>=0 || - grammar.indexOf("output = AST")>=0; writeRecognizerAndCompile("TParser", - null, - "TLexer", - startRule, - null, - parserBuildsTrees, - false, - false, - debug); + "TLexer", + startRule, + debug); for (int i=0; i()); + sharedParser.setErrorHandler(new BailErrorStrategy()); } } diff --git a/tool/test/org/antlr/v4/test/TestRewriteAST.java b/tool/test/org/antlr/v4/test/TestRewriteAST.java deleted file mode 100644 index bfaf5978e..000000000 --- a/tool/test/org/antlr/v4/test/TestRewriteAST.java +++ /dev/null @@ -1,1406 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.test; - -import org.junit.Test; - -public class TestRewriteAST extends BaseTest { - protected boolean debug = false; - - @Test public void testDelete() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT -> ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("", found); - } - - @Test public void testSingleToken() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID -> ID;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc", debug); - assertEquals("abc\n", found); - } - - @Test public void testSingleLabeledToken() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : x=ID -> $x;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc", debug); - assertEquals("abc\n", found); - } - - @Test public void testSingleTokenToNewNode() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID -> ID[\"x\"];\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc", debug); - assertEquals("x\n", found); - } - - @Test public void testSingleTokenToNewNodeRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID -> ^(ID[\"x\"] INT);\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc", debug); - assertEquals("(x INT)\n", found); - } - - @Test public void testSingleCharLiteral() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : 'c' -> 'c';\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "c", debug); - assertEquals("c\n", found); - } - - @Test public void testSingleStringLiteral() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : 'ick' -> 'ick';\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "ick", debug); - assertEquals("ick\n", found); - } - - @Test public void testSingleRule() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : b -> b;\n" + - "b : ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc", debug); - assertEquals("abc\n", found); - } - - @Test public void testReorderTokens() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT -> INT ID;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("34 abc\n", found); - } - - @Test public void testReorderTokenAndRule() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : b INT -> INT b;\n" + - "b : ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("34 abc\n", found); - } - - @Test public void testTokenTree() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT -> ^(INT ID);\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("(34 abc)\n", found); - } - - @Test public void testTokenTreeAfterOtherStuff() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : 'void' ID INT -> 'void' ^(INT ID);\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "void abc 34", debug); - assertEquals("void (34 abc)\n", found); - } - - @Test public void testNestedTokenTreeWithOuterLoop() throws Exception { - // verify that ID and INT both iterate over outer index variable - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {DUH;}\n" + - "a : ID INT ID INT -> ^( DUH ID ^( DUH INT) )* ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a 1 b 2", debug); - assertEquals("(DUH a (DUH 1)) (DUH b (DUH 2))\n", found); - } - - @Test public void testOptionalSingleToken() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID -> ID? ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc", debug); - assertEquals("abc\n", found); - } - - @Test public void testClosureSingleToken() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID ID -> ID* ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("a b\n", found); - } - - @Test public void testPositiveClosureSingleToken() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID ID -> ID+ ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("a b\n", found); - } - - @Test public void testOptionalSingleRule() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : b -> b?;\n" + - "b : ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc", debug); - assertEquals("abc\n", found); - } - - @Test public void testClosureSingleRule() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : b b -> b*;\n" + - "b : ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("a b\n", found); - } - - @Test public void testClosureOfLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : x+=b x+=b -> $x*;\n" + - "b : ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("a b\n", found); - } - - @Test public void testOptionalLabelNoListLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : (x=ID)? -> $x?;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testSinglePredicateT() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID -> {true}? ID -> ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc", debug); - assertEquals("abc\n", found); - } - - @Test public void testSinglePredicateF() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID -> {false}? ID -> ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc", debug); - assertEquals("", found); - } - - @Test public void testMultiplePredicate() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT -> {false}? ID\n" + - " -> {true}? INT\n" + - " -> \n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a 2", debug); - assertEquals("2\n", found); - } - - @Test public void testMultiplePredicateTrees() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT -> {false}? ^(ID INT)\n" + - " -> {true}? ^(INT ID)\n" + - " -> ID\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a 2", debug); - assertEquals("(2 a)\n", found); - } - - @Test public void testSimpleTree() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : op INT -> ^(op INT);\n" + - "op : '+'|'-' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "-34", debug); - assertEquals("(- 34)\n", found); - } - - @Test public void testSimpleTree2() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : op INT -> ^(INT op);\n" + - "op : '+'|'-' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "+ 34", debug); - assertEquals("(34 +)\n", found); - } - - - @Test public void testNestedTrees() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : 'var' (ID ':' type ';')+ -> ^('var' ^(':' ID type)*) ;\n" + - "type : 'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "var a:int; b:float;", debug); - assertEquals("(var (: a int) (: b float))\n", found); - } - - @Test public void testImaginaryTokenCopy() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {VAR;}\n" + - "a : ID (',' ID)*-> ^(VAR ID)* ;\n" + - "type : 'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a,b,c", debug); - assertEquals("(VAR a) (VAR b) (VAR c)\n", found); - } - - @Test public void testTokenUnreferencedOnLeftButDefined() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {VAR;}\n" + - "a : b -> ID ;\n" + - "b : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("ID\n", found); - } - - @Test public void testImaginaryTokenCopySetText() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {VAR;}\n" + - "a : ID (',' ID)*-> ^(VAR[\"var\"] ID)* ;\n" + - "type : 'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a,b,c", debug); - assertEquals("(var a) (var b) (var c)\n", found); - } - - @Test public void testImaginaryTokenNoCopyFromToken() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID*) ;\n" + - "type : 'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "{a b c}", debug); - assertEquals("({ a b c)\n", found); - } - - @Test public void testImaginaryTokenNoCopyFromTokenSetText() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : lc='{' ID+ '}' -> ^(BLOCK[$lc,\"block\"] ID*) ;\n" + - "type : 'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "{a b c}", debug); - assertEquals("(block a b c)\n", found); - } - - @Test public void testMixedRewriteAndAutoAST() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : b b^ ;\n" + // 2nd b matches only an INT; can make it root - "b : ID INT -> INT ID\n" + - " | INT\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a 1 2", debug); - assertEquals("(2 1 a)\n", found); - } - - @Test public void testSubruleWithRewrite() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : b b ;\n" + - "b : (ID INT -> INT ID | INT INT -> INT* )\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a 1 2 3", debug); - assertEquals("1 a 2 3\n", found); - } - - @Test public void testSubruleWithRewrite2() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {TYPE;}\n" + - "a : b b ;\n" + - "b : 'int'\n" + - " ( ID -> ^(TYPE 'int' ID)\n" + - " | ID '=' INT -> ^(TYPE 'int' ID INT)\n" + - " )\n" + - " ';'\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "int a; int b=3;", debug); - assertEquals("(TYPE int a) (TYPE int b 3)\n", found); - } - - @Test public void testNestedRewriteShutsOffAutoAST() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : b b ;\n" + - "b : ID ( ID (last=ID -> $last)* ) ';'\n" + // get last ID - " | INT\n" + // should still get auto AST construction - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b c; 42", debug); - assertEquals("c 42\n", found); - } - - @Test public void testRewriteActions() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : atom -> ^({_adaptor.create(INT,\"9\")} atom) ;\n" + - "atom : INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "3", debug); - assertEquals("(9 3)\n", found); - } - - @Test public void testRewriteActions2() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : atom -> {_adaptor.create(INT,\"9\")} atom ;\n" + - "atom : INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "3", debug); - assertEquals("9 3\n", found); - } - - @Test public void testRefToOldValue() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : (atom -> atom) (op='+' r=atom -> ^($op $a $r) )* ;\n" + - "atom : INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "3+4+5", debug); - assertEquals("(+ (+ 3 4) 5)\n", found); - } - - @Test public void testCopySemanticsForRules() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : atom -> ^(atom atom) ;\n" + // NOT CYCLE! (dup atom) - "atom : INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "3", debug); - assertEquals("(3 3)\n", found); - } - - @Test public void testCopySemanticsForRules2() throws Exception { - // copy type as a root for each invocation of (...)* in rewrite - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : type ID (',' ID)* ';' -> ^(type ID)* ;\n" + - "type : 'int' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "int a,b,c;", debug); - assertEquals("(int a) (int b) (int c)\n", found); - } - - @Test public void testCopySemanticsForRules3() throws Exception { - // copy type *and* modifier even though it's optional - // for each invocation of (...)* in rewrite - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : modifier? type ID (',' ID)* ';' -> ^(type modifier? ID)* ;\n" + - "type : 'int' ;\n" + - "modifier : 'public' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "public int a,b,c;", debug); - assertEquals("(int public a) (int public b) (int public c)\n", found); - } - - @Test public void testCopySemanticsForRules3Double() throws Exception { - // copy type *and* modifier even though it's optional - // for each invocation of (...)* in rewrite - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : modifier? type ID (',' ID)* ';' -> ^(type modifier? ID)* ^(type modifier? ID)* ;\n" + - "type : 'int' ;\n" + - "modifier : 'public' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "public int a,b,c;", debug); - assertEquals("(int public a) (int public b) (int public c) (int public a) (int public b) (int public c)\n", found); - } - - @Test public void testCopySemanticsForRules4() throws Exception { - // copy type *and* modifier even though it's optional - // for each invocation of (...)* in rewrite - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {MOD;}\n" + - "a : modifier? type ID (',' ID)* ';' -> ^(type ^(MOD modifier)? ID)* ;\n" + - "type : 'int' ;\n" + - "modifier : 'public' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "public int a,b,c;", debug); - assertEquals("(int (MOD public) a) (int (MOD public) b) (int (MOD public) c)\n", found); - } - - @Test public void testCopySemanticsLists() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {MOD;}\n" + - "a : ID (',' ID)* ';' -> ID* ID* ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a,b,c;", debug); - assertEquals("a b c a b c\n", found); - } - - @Test public void testCopyRuleLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : x=b -> $x $x;\n"+ - "b : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a a\n", found); - } - - @Test public void testCopyRuleLabel2() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : x=b -> ^($x $x);\n"+ - "b : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("(a a)\n", found); - } - - @Test public void testQueueingOfTokens() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : 'int' ID (',' ID)* ';' -> ^('int' ID*) ;\n" + - "op : '+'|'-' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "int a,b,c;", debug); - assertEquals("(int a b c)\n", found); - } - - @Test public void testCopyOfTokens() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : 'int' ID ';' -> 'int' ID 'int' ID ;\n" + - "op : '+'|'-' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "int a;", debug); - assertEquals("int a int a\n", found); - } - - @Test public void testTokenCopyInLoop() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : 'int' ID (',' ID)* ';' -> ^('int' ID)* ;\n" + - "op : '+'|'-' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "int a,b,c;", debug); - assertEquals("(int a) (int b) (int c)\n", found); - } - - @Test public void testTokenCopyInLoopAgainstTwoOthers() throws Exception { - // must smear 'int' copies across as root of multiple trees - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : 'int' ID ':' INT (',' ID ':' INT)* ';' -> ^('int' ID INT)* ;\n" + - "op : '+'|'-' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "int a:1,b:2,c:3;", debug); - assertEquals("(int a 1) (int b 2) (int c 3)\n", found); - } - - @Test public void testListRefdOneAtATime() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID+ -> ID ID ID ;\n" + // works if 3 input IDs - "op : '+'|'-' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b c", debug); - assertEquals("a b c\n", found); - } - - @Test public void testSplitListWithLabels() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {VAR;}\n"+ - "a : first=ID others+=ID* -> $first VAR $others* ;\n" + - "op : '+'|'-' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b c", debug); - assertEquals("a VAR b c\n", found); - } - - @Test public void testComplicatedMelange() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : A A b=B B b=B c+=C C c+=C D {String s=$D.text;} -> A* B* C* D ;\n" + - "type : 'int' | 'float' ;\n" + - "A : 'a' ;\n" + - "B : 'b' ;\n" + - "C : 'c' ;\n" + - "D : 'd' ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a a b b b c c c d", debug); - assertEquals("a a b b b c c c d\n", found); - } - - @Test public void testRuleLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : x=b -> $x;\n"+ - "b : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testAmbiguousRule() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID a -> a | INT ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "INT: '0'..'9'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("34\n", found); - } - - /* - @Test public void testWeirdRuleRef() throws Exception { - ErrorQueue equeue = new ErrorQueue(); - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID a -> $a | INT ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "INT: '0'..'9'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - Grammar g = new Grammar(grammar); - Tool antlr = newTool(); - antlr.addListener(equeue); - CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); - g.setCodeGenerator(generator); - generator.genRecognizer(); - - // $a is ambig; is it previous root or ref to a ref in alt? - assertEquals("unexpected errors: "+equeue, 1, equeue.errors.size()); - } - */ - - @Test public void testRuleListLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : x+=b x+=b -> $x*;\n"+ - "b : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("a b\n", found); - } - - @Test public void testRuleListLabel2() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : x+=b x+=b -> $x $x*;\n"+ - "b : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("a a b\n", found); - } - - @Test public void testOptional() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : x=b (y=b)? -> $x $y?;\n"+ - "b : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testOptional2() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : x=ID (y=b)? -> $x $y?;\n"+ - "b : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("a b\n", found); - } - - @Test public void testOptional3() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : x=ID (y=b)? -> ($x $y)?;\n"+ - "b : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("a b\n", found); - } - - @Test public void testOptional4() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : x+=ID (y=b)? -> ($x $y)?;\n"+ - "b : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("a b\n", found); - } - - @Test public void testOptional5() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : ID -> ID? ;\n"+ // match an ID to optional ID - "b : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a", debug); - assertEquals("a\n", found); - } - - @Test public void testArbitraryExprType() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : x+=b x+=b -> {new CommonAST()};\n"+ - "b : ID ;\n"+ - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - assertEquals("", found); - } - - @Test public void testSet() throws Exception { - String grammar = - "grammar T;\n" + - "options { output = AST; } \n" + - "a: (INT|ID)+ -> INT* ID* ;\n" + - "INT: '0'..'9'+;\n" + - "ID : 'a'..'z'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "2 a 34 de", debug); - assertEquals("2 34 a de\n", found); - } - - @Test public void testSet2() throws Exception { - String grammar = - "grammar T;\n" + - "options { output = AST; } \n" + - "a: (INT|ID) -> INT? ID? ;\n" + - "INT: '0'..'9'+;\n" + - "ID : 'a'..'z'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "2", debug); - assertEquals("2\n", found); - } - - @Test public void testSetWithLabel() throws Exception { - String grammar = - "grammar T;\n" + - "options { output = AST; } \n" + - "a : x=(INT|ID) -> $x ;\n" + - "INT: '0'..'9'+;\n" + - "ID : 'a'..'z'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "2", debug); - assertEquals("2\n", found); - } - - @Test public void testRewriteAction() throws Exception { - String grammar = - "grammar T; \n" + - "options { output = AST; }\n" + - "tokens { FLOAT; }\n" + - "a\n" + - " : INT -> {new CommonAST(new CommonToken(FLOAT,$INT.text+\".0\"))} \n" + - " ; \n" + - "INT : '0'..'9'+; \n" + - "WS: (' ' | '\\n' | '\\t')+ {$channel = HIDDEN;}; \n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "25", debug); - assertEquals("25.0\n", found); - } - - @Test public void testOptionalSubruleWithoutRealElements() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;} \n" + - "tokens {PARMS;} \n" + - "\n" + - "a \n" + - " : 'modulo' ID ('(' parms+ ')')? -> ^('modulo' ID ^(PARMS parms*)?) \n" + - " ; \n" + - "parms : '#'|ID; \n" + - "ID : ('a'..'z' | 'A'..'Z')+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "modulo abc (x y #)", debug); - assertEquals("(modulo abc (PARMS x y #))\n", found); - } - - // C A R D I N A L I T Y I S S U E S - - @Test public void testCardinality() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "tokens {BLOCK;}\n" + - "a : ID ID INT INT INT -> (ID INT)*;\n"+ - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+; \n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b 3 4 5", debug); - String expecting = - "org.antlr.v4.runtime.tree.RewriteCardinalityException: size==2 and out of elements"; - String found = getFirstLineOfException(); - assertEquals(expecting, found); - } - - @Test public void testCardinality2() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID+ -> ID ID ID ;\n" + // only 2 input IDs - "op : '+'|'-' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - execParser("T.g", grammar, "TParser", "TLexer", - "a", "a b", debug); - String expecting = - "org.antlr.v4.runtime.tree.RewriteCardinalityException: size==2 and out of elements"; - String found = getFirstLineOfException(); - assertEquals(expecting, found); - } - - @Test public void testCardinality3() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID? INT -> ID INT ;\n" + - "op : '+'|'-' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - execParser("T.g", grammar, "TParser", "TLexer", - "a", "3", debug); - String expecting = - "org.antlr.v4.runtime.tree.RewriteEmptyStreamException: n/a"; - String found = getFirstLineOfException(); - assertEquals(expecting, found); - } - - @Test public void testWildcard() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID c=. -> $c;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("34\n", found); - } - - @Test public void testWildcard2() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID c+=. c+=. -> $c*;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34 def", debug); - assertEquals("34 def\n", found); - } - - // E R R O R S - - /* - @Test public void testUnknownRule() throws Exception { - ErrorQueue equeue = new ErrorQueue(); - - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : INT -> ugh ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - Grammar g = new Grammar(grammar); - Tool antlr = newTool(); - antlr.addListener(equeue); - CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); - g.setCodeGenerator(generator); - generator.genRecognizer(); - - int expectedMsgID = ErrorManager.MSG_UNDEFINED_RULE_REF; - Object expectedArg = "ugh"; - Object expectedArg2 = null; - GrammarSemanticsMessage expectedMessage = - new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); - - checkError(equeue, expectedMessage); - } - - @Test public void testKnownRuleButNotInLHS() throws Exception { - ErrorQueue equeue = new ErrorQueue(); - - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : INT -> b ;\n" + - "b : 'b' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - Grammar g = new Grammar(grammar); - Tool antlr = newTool(); - antlr.addListener(equeue); - CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); - g.setCodeGenerator(generator); - generator.genRecognizer(); - - int expectedMsgID = ErrorManager.MSG_REWRITE_ELEMENT_NOT_PRESENT_ON_LHS; - Object expectedArg = "b"; - Object expectedArg2 = null; - GrammarSemanticsMessage expectedMessage = - new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); - - checkError(equeue, expectedMessage); - } - - @Test public void testUnknownToken() throws Exception { - ErrorQueue equeue = new ErrorQueue(); - - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : INT -> ICK ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - Grammar g = new Grammar(grammar); - Tool antlr = newTool(); - antlr.addListener(equeue); - CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); - g.setCodeGenerator(generator); - generator.genRecognizer(); - - int expectedMsgID = ErrorManager.MSG_UNDEFINED_TOKEN_REF_IN_REWRITE; - Object expectedArg = "ICK"; - Object expectedArg2 = null; - GrammarSemanticsMessage expectedMessage = - new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); - - checkError(equeue, expectedMessage); - } - - @Test public void testUnknownLabel() throws Exception { - ErrorQueue equeue = new ErrorQueue(); - - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : INT -> $foo ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - Grammar g = new Grammar(grammar); - Tool antlr = newTool(); - antlr.addListener(equeue); - CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); - g.setCodeGenerator(generator); - generator.genRecognizer(); - - int expectedMsgID = ErrorManager.MSG_UNDEFINED_LABEL_REF_IN_REWRITE; - Object expectedArg = "foo"; - Object expectedArg2 = null; - GrammarSemanticsMessage expectedMessage = - new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); - - checkError(equeue, expectedMessage); - } - - @Test public void testUnknownCharLiteralToken() throws Exception { - ErrorQueue equeue = new ErrorQueue(); - - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : INT -> 'a' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - Grammar g = new Grammar(grammar); - Tool antlr = newTool(); - antlr.addListener(equeue); - CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); - g.setCodeGenerator(generator); - generator.genRecognizer(); - - int expectedMsgID = ErrorManager.MSG_UNDEFINED_TOKEN_REF_IN_REWRITE; - Object expectedArg = "'a'"; - Object expectedArg2 = null; - GrammarSemanticsMessage expectedMessage = - new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); - - checkError(equeue, expectedMessage); - } - - @Test public void testUnknownStringLiteralToken() throws Exception { - ErrorQueue equeue = new ErrorQueue(); - - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : INT -> 'foo' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - Grammar g = new Grammar(grammar); - Tool antlr = newTool(); - antlr.addListener(equeue); - CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); - g.setCodeGenerator(generator); - generator.genRecognizer(); - - int expectedMsgID = ErrorManager.MSG_UNDEFINED_TOKEN_REF_IN_REWRITE; - Object expectedArg = "'foo'"; - Object expectedArg2 = null; - GrammarSemanticsMessage expectedMessage = - new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); - - checkError(equeue, expectedMessage); - } - */ - - @Test public void testExtraTokenInSimpleDecl() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "tokens {EXPR;}\n" + - "decl : type ID '=' INT ';' -> ^(EXPR type ID INT) ;\n" + - "type : 'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "decl", "int 34 x=1;", debug); - assertEquals("line 1:4 extraneous input '34' expecting ID\n", this.stderrDuringParse); - assertEquals("(EXPR int x 1)\n", found); // tree gets correct x and 1 tokens - } - - @Test public void testMissingIDInSimpleDecl() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "tokens {EXPR;}\n" + - "decl : type ID '=' INT ';' -> ^(EXPR type ID INT) ;\n" + - "type : 'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "decl", "int =1;", debug); - assertEquals("line 1:4 missing ID at '='\n", this.stderrDuringParse); - assertEquals("(EXPR int 1)\n", found); // tree gets invented ID token - } - - @Test public void testMissingSetInSimpleDecl() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "tokens {EXPR;}\n" + - "decl : type ID '=' INT ';' -> ^(EXPR type ID INT) ;\n" + - "type : 'int' | 'float' ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "decl", "x=1;", debug); - assertEquals("line 1:0 mismatched input 'x' expecting {'int', 'float'}\n", this.stderrDuringParse); - assertEquals("(EXPR x 1)\n", found); // tree gets invented ID token - } - - @Test public void testMissingTokenGivesErrorNode() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : ID INT -> ID INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "abc", debug); - assertEquals("line 1:3 missing INT at ''\n", this.stderrDuringParse); - // doesn't do in-line recovery for sets (yet?) - assertEquals("abc \n", found); - } - - @Test public void testExtraTokenGivesErrorNode() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : b c -> b c;\n" + - "b : ID -> ID ;\n" + - "c : INT -> INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "abc ick 34", debug); - assertEquals("line 1:4 extraneous input 'ick' expecting INT\n", this.stderrDuringParse); - assertEquals("abc 34\n", found); - } - - @Test public void testMissingFirstTokenGivesErrorNode() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : ID INT -> ID INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "34", debug); - assertEquals("line 1:0 missing ID at '34'\n", this.stderrDuringParse); - assertEquals(" 34\n", found); - } - - @Test public void testMissingFirstTokenGivesErrorNode2() throws Exception { - String grammar = - "grammar foo;\n" + - "options {output=AST;}\n" + - "a : b c -> b c;\n" + - "b : ID -> ID ;\n" + - "c : INT -> INT ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("foo.g", grammar, "fooParser", "fooLexer", - "a", "34", debug); - // finds an error at the first token, 34, and re-syncs. - // re-synchronizing does not consume a token because 34 follows - // ref to rule b (start of c). It then matches 34 in c. - assertEquals("line 1:0 missing ID at '34'\n", this.stderrDuringParse); - assertEquals(" 34\n", found); - } - -} diff --git a/tool/test/org/antlr/v4/test/TestRewriteTemplates.java b/tool/test/org/antlr/v4/test/TestRewriteTemplates.java deleted file mode 100644 index f2a732aef..000000000 --- a/tool/test/org/antlr/v4/test/TestRewriteTemplates.java +++ /dev/null @@ -1,284 +0,0 @@ -/* - * [The "BSD license"] - * Copyright (c) 2010 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.test; - -import org.junit.*; - -@Ignore -public class TestRewriteTemplates extends BaseTest { - protected boolean debug = false; - - @Test public void testDelete() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=template;}\n" + - "a : ID INT -> ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("", found); - } - - @Test public void testAction() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=template;}\n" + - "a : ID INT -> {new StringTemplate($ID.text)} ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("abc\n", found); - } - - @Test public void testEmbeddedLiteralConstructor() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=template;}\n" + - "a : ID INT -> {%{$ID.text}} ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("abc\n", found); - } - - @Test public void testInlineTemplate() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=template;}\n" + - "a : ID INT -> template(x={$ID},y={$INT}) <, y:;>> ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("x:abc, y:34;\n", found); - } - - @Test public void testNamedTemplate() throws Exception { - // the support code adds template group in it's output Test.java - // that defines template foo. - String grammar = - "grammar T;\n" + - "options {output=template;}\n" + - "a : ID INT -> foo(x={$ID.text},y={$INT.text}) ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("abc 34\n", found); - } - - @Test public void testIndirectTemplate() throws Exception { - // the support code adds template group in it's output Test.java - // that defines template foo. - String grammar = - "grammar T;\n" + - "options {output=template;}\n" + - "a : ID INT -> ({\"foo\"})(x={$ID.text},y={$INT.text}) ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("abc 34\n", found); - } - - @Test public void testInlineTemplateInvokingLib() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=template;}\n" + - "a : ID INT -> template(x={$ID.text},y={$INT.text}) \"\" ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("abc 34\n", found); - } - - @Test public void testPredicatedAlts() throws Exception { - // the support code adds template group in it's output Test.java - // that defines template foo. - String grammar = - "grammar T;\n" + - "options {output=template;}\n" + - "a : ID INT -> {false}? foo(x={$ID.text},y={$INT.text})\n" + - " -> foo(x={\"hi\"}, y={$ID.text})\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("hi abc\n", found); - } - - @Test public void testTemplateReturn() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=template;}\n" + - "a : b {System.out.println($b.st);} ;\n" + - "b : ID INT -> foo(x={$ID.text},y={$INT.text}) ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("abc 34\n", found); - } - - @Test public void testReturnValueWithTemplate() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=template;}\n" + - "a : b {System.out.println($b.i);} ;\n" + - "b returns [int i] : ID INT {$i=8;} ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("8\n", found); - } - - @Test public void testTemplateRefToDynamicAttributes() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=template;}\n" + - "a scope {String id;} : ID {$a::id=$ID.text;} b\n" + - " {System.out.println($b.st.toString());}\n" + - " ;\n" + - "b : INT -> foo(x={$a::id}) ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "abc 34", debug); - assertEquals("abc \n", found); - } - - // tests for rewriting templates in tree parsers - - @Test public void testSingleNode() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP;\n"+ - "options {ASTLabelType=CommonTree; output=template;}\n" + - "s : a {System.out.println($a.st);} ;\n" + - "a : ID -> template(x={$ID.text}) <<||>> ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "s", "abc"); - assertEquals("|abc|\n", found); - } - - @Test public void testSingleNodeRewriteMode() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP;\n"+ - "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" + - "s : a {System.out.println(input.getTokenStream().toString(0,0));} ;\n" + - "a : ID -> template(x={$ID.text}) <<||>> ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "s", "abc"); - assertEquals("|abc|\n", found); - } - - @Test public void testRewriteRuleAndRewriteModeOnSimpleElements() throws Exception { - String grammar = - "tree grammar TP;\n"+ - "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" + - "a: ^(A B) -> {ick}\n" + - " | y+=INT -> {ick}\n" + - " | x=ID -> {ick}\n" + - " | BLORT -> {ick}\n" + - " ;\n"; - String expecting = null; - testErrors(new String[]{grammar, expecting}, false); - } - - @Test public void testRewriteRuleAndRewriteModeIgnoreActionsPredicates() throws Exception { - String grammar = - "tree grammar TP;\n"+ - "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" + - "a: {action} {action2} x=A -> {ick}\n" + - " | {pred1}? y+=B -> {ick}\n" + - " | C {action} -> {ick}\n" + - " | {pred2}?=> z+=D -> {ick}\n" + - " | (E)=> ^(F G) -> {ick}\n" + - " ;\n"; - String expecting = null; - testErrors(new String[]{grammar, expecting}, false); - } - - @Test public void testRewriteRuleAndRewriteModeNotSimple() throws Exception { - String grammar = - "tree grammar TP;\n"+ - "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" + - "a : ID+ -> {ick}\n" + - " | INT INT -> {ick}\n" + - " ;\n"; - String expecting = null; - testErrors(new String[] {grammar, expecting}, false); - } - - @Test public void testRewriteRuleAndRewriteModeRefRule() throws Exception { - String grammar = - "tree grammar TP;\n"+ - "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" + - "a : b+ -> {ick}\n" + - " | b b A -> {ick}\n" + - " ;\n" + - "b : B ;\n"; - String expecting = null; - testErrors(new String[] {grammar, expecting}, false); - } - -} diff --git a/tool/test/org/antlr/v4/test/TestSets.java b/tool/test/org/antlr/v4/test/TestSets.java index 504793d7c..2abf50bfb 100644 --- a/tool/test/org/antlr/v4/test/TestSets.java +++ b/tool/test/org/antlr/v4/test/TestSets.java @@ -96,16 +96,6 @@ public class TestSets extends BaseTest { assertEquals("b\n", found); } - @Test public void testRuleAsSetAST() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : 'a' | 'b' |'c' ;\n"; - String found = execParser("T.g", grammar, "TParser", "TLexer", - "a", "b", debug); - assertEquals("b\n", found); - } - @Test public void testNotChar() throws Exception { String grammar = "grammar T;\n" + diff --git a/tool/test/org/antlr/v4/test/TestSymbolIssues.java b/tool/test/org/antlr/v4/test/TestSymbolIssues.java index 6cd08a0ad..2c56322d5 100644 --- a/tool/test/org/antlr/v4/test/TestSymbolIssues.java +++ b/tool/test/org/antlr/v4/test/TestSymbolIssues.java @@ -48,22 +48,6 @@ public class TestSymbolIssues extends BaseTest { "error(42): B.g:6:9: label x type mismatch with previous definition: TOKEN_LIST_LABEL!=TOKEN_LABEL\n" }; - static String[] C = { - // INPUT - "grammar C;\n"+ - "options {output=AST;}\n"+ - "a : A x=b y+=Z 'hi' -> ID A r $foo $x b $y+ 'hi' 'eh?'\n"+ - " | ID -> $x A ID // shouldn't see these refs from other alt ('cept ID)\n"+ - " ;\n"+ - "b : B ;\n"+ - "A : 'a';", - // YIELDS - "error(51): C.g:3:28: reference to rewrite element r not found to left of ->\n" + - "error(51): C.g:3:30: reference to rewrite element foo not found to left of ->\n" + - "error(51): C.g:3:49: reference to rewrite element 'eh?' not found to left of ->\n" + - "error(51): C.g:4:10: reference to rewrite element x not found to left of ->\n" - }; - static String[] D = { // INPUT "parser grammar D;\n" + @@ -101,7 +85,6 @@ public class TestSymbolIssues extends BaseTest { @Test public void testA() { super.testErrors(A, false); } @Test public void testB() { super.testErrors(B, false); } - @Test public void testC() { super.testErrors(C, false); } @Test public void testD() { super.testErrors(D, false); } @Test public void testE() { super.testErrors(E, false); } } diff --git a/tool/test/org/antlr/v4/test/TestTemplates.java b/tool/test/org/antlr/v4/test/TestTemplates.java deleted file mode 100644 index a360a9de5..000000000 --- a/tool/test/org/antlr/v4/test/TestTemplates.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * [The "BSD license"] - * Copyright (c) 2010 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.test; - -import org.junit.Test; -import org.stringtemplate.v4.ST; -import org.stringtemplate.v4.STGroup; -import org.stringtemplate.v4.STGroupString; - -/** Test templates in actions; %... shorthands */ -public class TestTemplates extends BaseTest { - private static final String LINE_SEP = System.getProperty("line.separator"); - - String template = - "simpleTemplate(inline) ::= <<\n" + - "grammar T;\n" + - "options {\n" + - " output=template;\n" + - "}\n" + - "\n" + - "a : ID {#inline##end-inline#}\n" + - " ;\n" + - "\n" + - "ID : 'a';\n" + - ">>\n"; - - @Test - public void testTemplateConstructor() throws Exception { - String action = "x = %foo(name={$ID.text});"; - String expecting = "x = templateLib.getInstanceOf(\"foo\"," + - "new STAttrMap().put(\"name\", (ID1!=null?ID1.getText():null)));"; - testActions(template, "inline", action, expecting); - } - - @Test - public void testTemplateConstructorNoArgs() throws Exception { - String action = "x = %foo();"; - String expecting = "x = templateLib.getInstanceOf(\"foo\");"; - testActions(template, "inline", action, expecting); - } - - @Test - public void testIndirectTemplateConstructor() throws Exception { - String action = "x = %({\"foo\"})(name={$ID.text});"; - String expecting = "x = templateLib.getInstanceOf(\"foo\"," + - "new STAttrMap().put(\"name\", (ID1!=null?ID1.getText():null)));"; - testActions(template, "inline", action, expecting); - } - - @Test public void testStringConstructor() throws Exception { - String action = "x = %{$ID.text};"; - String expecting = "x = new StringTemplate(templateLib,(ID1!=null?ID1.getText():null));"; - testActions(template, "inline", action, expecting); - } - - @Test public void testSetAttr() throws Exception { - String action = "%x.y = z;"; - String expecting = "(x).setAttribute(\"y\", z);"; - testActions(template, "inline", action, expecting); - } - - @Test public void testSetAttrOfExpr() throws Exception { - String action = "%{foo($ID.text).getST()}.y = z;"; - String expecting = "(foo((ID1!=null?ID1.getText():null)).getST()).setAttribute(\"y\", z);"; - testActions(template, "inline", action, expecting); - } - - @Test public void testCannotHaveSpaceBeforeDot() throws Exception { - String action = "%x .y = z;"; - String expecting = ": T.g:6:16: invalid StringTemplate % shorthand syntax: '%x'"; - STGroup grammarG = new STGroupString(template); - ST grammarST = grammarG.getInstanceOf("simpleTemplate"); - grammarST.add("inline", action); - String grammar = grammarST.render(); - testErrors(new String[] {grammar, expecting}, false); - } - - @Test public void testCannotHaveSpaceAfterDot() throws Exception { - String action = "%x. y = z;"; - String expecting = ": T.g:6:16: invalid StringTemplate % shorthand syntax: '%x.'"; - STGroup grammarG = new STGroupString(template); - ST grammarST = grammarG.getInstanceOf("simpleTemplate"); - grammarST.add("inline", action); - String grammar = grammarST.render(); - testErrors(new String[] {grammar, expecting}, false); - } - - // S U P P O R T - private void assertNoErrors(ErrorQueue equeue) { - assertTrue("unexpected errors: "+equeue, equeue.errors.size()==0); - } -} diff --git a/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java b/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java index 0add57230..db879c70c 100644 --- a/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java +++ b/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java @@ -36,9 +36,9 @@ public class TestToolSyntaxErrors extends BaseTest { "grammar A;\n" + "a ( A | B ) D ;\n" + "b : B ;", - "error(17): A.g:2:3: '(' came as a complete surprise to me while matching rule preamble\n" + - "error(17): A.g:2:11: mismatched input ')' expecting SEMI while matching a rule\n" + - "error(17): A.g:2:15: ';' came as a complete surprise to me while matching rule preamble\n", + ": 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", }; @Test public void testA() { super.testErrors(A, 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 rule element\n", + "error(17): A.g:3:0: unterminated rule (missing ';') detected at 'B :' while looking for lexer rule element\n", }; super.testErrors(pair, true); } diff --git a/tool/test/org/antlr/v4/test/TestTreeIterator.java b/tool/test/org/antlr/v4/test/TestTreeIterator.java deleted file mode 100644 index 2b1b8a0a4..000000000 --- a/tool/test/org/antlr/v4/test/TestTreeIterator.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.test; - -import org.antlr.v4.runtime.tree.*; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class TestTreeIterator { - static final String[] tokens = new String[] { - "", "", "", "", "A", "B", "C", "D", "E", "F", "G" - }; - - @Test public void testNode() { - ASTAdaptor adaptor = new CommonASTAdaptor(); - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("A"); - ASTIterator it = new ASTIterator(t); - StringBuffer buf = toString(it); - String expecting = "A EOF"; - String found = buf.toString(); - assertEquals(expecting, found); - } - - @Test public void testFlatAB() { - ASTAdaptor adaptor = new CommonASTAdaptor(); - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(nil A B)"); - ASTIterator it = new ASTIterator(t); - StringBuffer buf = toString(it); - String expecting = "nil DOWN A B UP EOF"; - String found = buf.toString(); - assertEquals(expecting, found); - } - - @Test public void testAB() { - ASTAdaptor adaptor = new CommonASTAdaptor(); - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B)"); - ASTIterator it = new ASTIterator(t); - StringBuffer buf = toString(it); - String expecting = "A DOWN B UP EOF"; - String found = buf.toString(); - assertEquals(expecting, found); - } - - @Test public void testABC() { - ASTAdaptor adaptor = new CommonASTAdaptor(); - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C)"); - ASTIterator it = new ASTIterator(t); - StringBuffer buf = toString(it); - String expecting = "A DOWN B C UP EOF"; - String found = buf.toString(); - assertEquals(expecting, found); - } - - @Test public void testVerticalList() { - ASTAdaptor adaptor = new CommonASTAdaptor(); - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A (B C))"); - ASTIterator it = new ASTIterator(t); - StringBuffer buf = toString(it); - String expecting = "A DOWN B DOWN C UP UP EOF"; - String found = buf.toString(); - assertEquals(expecting, found); - } - - @Test public void testComplex() { - ASTAdaptor adaptor = new CommonASTAdaptor(); - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A (B (C D E) F) G)"); - ASTIterator it = new ASTIterator(t); - StringBuffer buf = toString(it); - String expecting = "A DOWN B DOWN C DOWN D E UP F UP G UP EOF"; - String found = buf.toString(); - assertEquals(expecting, found); - } - - @Test public void testReset() { - ASTAdaptor adaptor = new CommonASTAdaptor(); - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A (B (C D E) F) G)"); - ASTIterator it = new ASTIterator(t); - StringBuffer buf = toString(it); - String expecting = "A DOWN B DOWN C DOWN D E UP F UP G UP EOF"; - String found = buf.toString(); - assertEquals(expecting, found); - - it.reset(); - buf = toString(it); - expecting = "A DOWN B DOWN C DOWN D E UP F UP G UP EOF"; - found = buf.toString(); - assertEquals(expecting, found); - } - - protected static StringBuffer toString(ASTIterator it) { - StringBuffer buf = new StringBuffer(); - while ( it.hasNext() ) { - CommonAST n = (CommonAST)it.next(); - buf.append(n); - if ( it.hasNext() ) buf.append(" "); - } - return buf; - } -} diff --git a/tool/test/org/antlr/v4/test/TestTreeParsing.java b/tool/test/org/antlr/v4/test/TestTreeParsing.java deleted file mode 100644 index 716217cbf..000000000 --- a/tool/test/org/antlr/v4/test/TestTreeParsing.java +++ /dev/null @@ -1,342 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.test; - -import org.junit.*; - -public class TestTreeParsing extends BaseTest { - @Test public void testFlatList() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {ASTLabelType=CommonAST;}\n" + - "a : ID INT\n" + - " {System.out.println($ID+\", \"+$INT);}\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc 34"); - assertEquals("abc, 34\n", found); - } - - @Test public void testSimpleTree() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT -> ^(ID INT);\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {ASTLabelType=CommonAST;}\n" + - "a : ^(ID INT)\n" + - " {System.out.println($ID+\", \"+$INT);}\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc 34"); - assertEquals("abc, 34\n", found); - } - - @Test public void testFlatVsTreeDecision() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : b c ;\n" + - "b : ID INT -> ^(ID INT);\n" + - "c : ID INT;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {ASTLabelType=CommonAST;}\n" + - "a : b b ;\n" + - "b : ID INT {System.out.print($ID+\" \"+$INT);}\n" + - " | ^(ID INT) {System.out.print(\"^(\"+$ID+\" \"+$INT+')');}\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "a 1 b 2"); - assertEquals("^(a 1)b 2\n", found); - } - - @Test public void testFlatVsTreeDecision2() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : b c ;\n" + - "b : ID INT+ -> ^(ID INT+);\n" + - "c : ID INT+;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {ASTLabelType=CommonAST;}\n" + - "a : b b ;\n" + - "b : ID INT+ {System.out.print($ID+\" \"+$INT);}\n" + - " | ^(x=ID (y=INT)+) {System.out.print(\"^(\"+$x+' '+$y+')');}\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", - "a 1 2 3 b 4 5"); - assertEquals("^(a 3)b 5\n", found); - } - - @Test public void testCyclicDFALookahead() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT+ PERIOD;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "SEMI : ';' ;\n"+ - "PERIOD : '.' ;\n"+ - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {tokenVocab=\"T\"; ASTLabelType=CommonAST;}\n" + - "a : ID INT+ PERIOD {System.out.print(\"alt 1\");}"+ - " | ID INT+ SEMI {System.out.print(\"alt 2\");}\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "a 1 2 3."); - assertEquals("alt 1\n", found); - } - - @Ignore - public void testTemplateOutput() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP;\n" + - "options {output=template; ASTLabelType=CommonAST;}\n" + - "s : a {System.out.println($a.st);};\n" + - "a : ID INT -> {new ST($INT.text)}\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "s", "abc 34"); - assertEquals("34\n", found); - } - - @Test public void testNullableChildList() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT? -> ^(ID INT?);\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {ASTLabelType=CommonAST;}\n" + - "a : ^(ID INT?)\n" + - " {System.out.println($ID);}\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc"); - assertEquals("abc\n", found); - } - - @Test public void testNullableChildList2() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID INT? SEMI -> ^(ID INT?) SEMI ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "SEMI : ';' ;\n"+ - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {ASTLabelType=CommonAST;}\n" + - "a : ^(ID INT?) SEMI\n" + - " {System.out.println($ID);}\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc;"); - assertEquals("abc\n", found); - } - - @Test public void testNullableChildList3() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : x=ID INT? (y=ID)? SEMI -> ^($x INT? $y?) SEMI ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "SEMI : ';' ;\n"+ - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {ASTLabelType=CommonAST;}\n" + - "a : ^(ID INT? b) SEMI\n" + - " {System.out.println($ID+\", \"+$b.text);}\n" + - " ;\n"+ - "b : ID? ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc def;"); - assertEquals("abc, def\n", found); - } - - @Test public void testActionsAfterRoot() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : x=ID INT? SEMI -> ^($x INT?) ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "SEMI : ';' ;\n"+ - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {ASTLabelType=CommonAST;}\n" + - "a @init {int x=0;} : ^(ID {x=1;} {x=2;} INT?)\n" + - " {System.out.println($ID+\", \"+x);}\n" + - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "abc;"); - assertEquals("abc, 2\n", found); - } - - @Test public void testWildcardLookahead() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID '+'^ INT;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "SEMI : ';' ;\n"+ - "PERIOD : '.' ;\n"+ - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {tokenVocab=T; ASTLabelType=CommonAST;}\n" + - "a : ^('+' . INT) {System.out.print(\"alt 1\");}"+ - " ;\n"; - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "a + 2"); - assertEquals("alt 1\n", found); - } - - @Test public void testWildcardLookahead2() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID '+'^ INT;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "SEMI : ';' ;\n"+ - "PERIOD : '.' ;\n"+ - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {tokenVocab=T; ASTLabelType=CommonAST;}\n" + - "a : ^('+' . INT) {System.out.print(\"alt 1\");}"+ - " | ^('+' . .) {System.out.print(\"alt 2\");}\n" + - " ;\n"; - - // AMBIG upon '+' DOWN INT UP etc.. but so what. - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "a + 2"); - assertEquals("alt 1\n", found); - } - - @Test public void testWildcardLookahead3() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID '+'^ INT;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "SEMI : ';' ;\n"+ - "PERIOD : '.' ;\n"+ - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {tokenVocab=T; ASTLabelType=CommonAST;}\n" + - "a : ^('+' ID INT) {System.out.print(\"alt 1\");}"+ - " | ^('+' . .) {System.out.print(\"alt 2\");}\n" + - " ;\n"; - - // AMBIG upon '+' DOWN INT UP etc.. but so what. - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "a + 2"); - assertEquals("alt 1\n", found); - } - - @Test public void testWildcardPlusLookahead() throws Exception { - String grammar = - "grammar T;\n" + - "options {output=AST;}\n" + - "a : ID '+'^ INT;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "SEMI : ';' ;\n"+ - "PERIOD : '.' ;\n"+ - "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; - - String treeGrammar = - "tree grammar TP; options {tokenVocab=T; ASTLabelType=CommonAST;}\n" + - "a : ^('+' INT INT ) {System.out.print(\"alt 1\");}"+ - " | ^('+' .+) {System.out.print(\"alt 2\");}\n" + - " ;\n"; - - // AMBIG upon '+' DOWN INT UP etc.. but so what. - - String found = execTreeParser("T.g", grammar, "TParser", "TP.g", - treeGrammar, "TP", "TLexer", "a", "a", "a + 2"); - assertEquals("alt 2\n", found); - } - -} diff --git a/tool/test/org/antlr/v4/test/TestTreeWizard.java b/tool/test/org/antlr/v4/test/TestTreeWizard.java deleted file mode 100644 index e86eb9018..000000000 --- a/tool/test/org/antlr/v4/test/TestTreeWizard.java +++ /dev/null @@ -1,397 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.test; - -import org.antlr.v4.runtime.tree.*; -import org.junit.Test; - -import java.util.*; - - -public class TestTreeWizard extends BaseTest { - protected static final String[] tokens = - new String[] {"", "", "", "", "", "A", "B", "C", "D", "E", "ID", "VAR"}; - protected static final ASTAdaptor adaptor = new CommonASTAdaptor(); - - @Test public void testSingleNode() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("ID"); - String found = t.toStringTree(); - String expecting = "ID"; - assertEquals(expecting, found); - } - - @Test public void testSingleNodeWithArg() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("ID[foo]"); - String found = t.toStringTree(); - String expecting = "foo"; - assertEquals(expecting, found); - } - - @Test public void testSingleNodeTree() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A)"); - String found = t.toStringTree(); - String expecting = "A"; - assertEquals(expecting, found); - } - - @Test public void testSingleLevelTree() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C D)"); - String found = t.toStringTree(); - String expecting = "(A B C D)"; - assertEquals(expecting, found); - } - - @Test public void testListTree() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(nil A B C)"); - String found = t.toStringTree(); - String expecting = "A B C"; - assertEquals(expecting, found); - } - - @Test public void testInvalidListTree() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("A B C"); - assertTrue(t==null); - } - - @Test public void testDoubleLevelTree() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A (B C) (B D) E)"); - String found = t.toStringTree(); - String expecting = "(A (B C) (B D) E)"; - assertEquals(expecting, found); - } - - @Test public void testSingleNodeIndex() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("ID"); - Map m = wiz.index(t); - String found = m.toString(); - String expecting = "{10=[ID]}"; - assertEquals(expecting, found); - } - - @Test public void testNoRepeatsIndex() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C D)"); - Map m = wiz.index(t); - String found = sortMapToString(m); - String expecting = "{5=[A], 6=[B], 7=[C], 8=[D]}"; - assertEquals(expecting, found); - } - - @Test public void testRepeatsIndex() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B (A C B) B D D)"); - Map m = wiz.index(t); - String found = sortMapToString(m); - String expecting = "{5=[A, A], 6=[B, B, B], 7=[C], 8=[D, D]}"; - assertEquals(expecting, found); - } - - @Test public void testNoRepeatsVisit() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C D)"); - final List elements = new ArrayList(); - wiz.visit(t, wiz.getTokenType("B"), new TreeWizard.Visitor() { - public void visit(Object t) { - elements.add(t); - } - }); - String found = elements.toString(); - String expecting = "[B]"; - assertEquals(expecting, found); - } - - @Test public void testNoRepeatsVisit2() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B (A C B) B D D)"); - final List elements = new ArrayList(); - wiz.visit(t, wiz.getTokenType("C"), - new TreeWizard.Visitor() { - public void visit(Object t) { - elements.add(t); - } - }); - String found = elements.toString(); - String expecting = "[C]"; - assertEquals(expecting, found); - } - - @Test public void testRepeatsVisit() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B (A C B) B D D)"); - final List elements = new ArrayList(); - wiz.visit(t, wiz.getTokenType("B"), - new TreeWizard.Visitor() { - public void visit(Object t) { - elements.add(t); - } - }); - String found = elements.toString(); - String expecting = "[B, B, B]"; - assertEquals(expecting, found); - } - - @Test public void testRepeatsVisit2() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B (A C B) B D D)"); - final List elements = new ArrayList(); - wiz.visit(t, wiz.getTokenType("A"), - new TreeWizard.Visitor() { - public void visit(Object t) { - elements.add(t); - } - }); - String found = elements.toString(); - String expecting = "[A, A]"; - assertEquals(expecting, found); - } - - @Test public void testRepeatsVisitWithContext() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B (A C B) B D D)"); - final List elements = new ArrayList(); - wiz.visit(t, wiz.getTokenType("B"), - new TreeWizard.ContextVisitor() { - public void visit(Object t, Object parent, int childIndex, Map labels) { - elements.add(adaptor.getText(t)+"@"+ - (parent!=null?adaptor.getText(parent):"nil")+ - "["+childIndex+"]"); - } - }); - String found = elements.toString(); - String expecting = "[B@A[0], B@A[1], B@A[2]]"; - assertEquals(expecting, found); - } - - @Test public void testRepeatsVisitWithNullParentAndContext() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B (A C B) B D D)"); - final List elements = new ArrayList(); - wiz.visit(t, wiz.getTokenType("A"), - new TreeWizard.ContextVisitor() { - public void visit(Object t, Object parent, int childIndex, Map labels) { - elements.add(adaptor.getText(t)+"@"+ - (parent!=null?adaptor.getText(parent):"nil")+ - "["+childIndex+"]"); - } - }); - String found = elements.toString(); - String expecting = "[A@nil[0], A@A[1]]"; - assertEquals(expecting, found); - } - - @Test public void testVisitPattern() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C (A B) D)"); - final List elements = new ArrayList(); - wiz.visit(t, "(A B)", - new TreeWizard.Visitor() { - public void visit(Object t) { - elements.add(t); - } - }); - String found = elements.toString(); - String expecting = "[A]"; // shouldn't match overall root, just (A B) - assertEquals(expecting, found); - } - - @Test public void testVisitPatternMultiple() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C (A B) (D (A B)))"); - final List elements = new ArrayList(); - wiz.visit(t, "(A B)", - new TreeWizard.ContextVisitor() { - public void visit(Object t, Object parent, int childIndex, Map labels) { - elements.add(adaptor.getText(t)+"@"+ - (parent!=null?adaptor.getText(parent):"nil")+ - "["+childIndex+"]"); - } - }); - String found = elements.toString(); - String expecting = "[A@A[2], A@D[0]]"; // shouldn't match overall root, just (A B) - assertEquals(expecting, found); - } - - @Test public void testVisitPatternMultipleWithLabels() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))"); - final List elements = new ArrayList(); - wiz.visit(t, "(%a:A %b:B)", - new TreeWizard.ContextVisitor() { - public void visit(Object t, Object parent, int childIndex, Map labels) { - elements.add(adaptor.getText(t)+"@"+ - (parent!=null?adaptor.getText(parent):"nil")+ - "["+childIndex+"]"+labels.get("a")+"&"+labels.get("b")); - } - }); - String found = elements.toString(); - String expecting = "[foo@A[2]foo&bar, big@D[0]big&dog]"; - assertEquals(expecting, found); - } - - @Test public void testParse() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C)"); - boolean valid = wiz.parse(t, "(A B C)"); - assertTrue(valid); - } - - @Test public void testParseSingleNode() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("A"); - boolean valid = wiz.parse(t, "A"); - assertTrue(valid); - } - - @Test public void testParseFlatTree() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(nil A B C)"); - boolean valid = wiz.parse(t, "(nil A B C)"); - assertTrue(valid); - } - - @Test public void testWildcard() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C)"); - boolean valid = wiz.parse(t, "(A . .)"); - assertTrue(valid); - } - - @Test public void testParseWithText() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B[foo] C[bar])"); - // C pattern has no text arg so despite [bar] in t, no need - // to match text--check structure only. - boolean valid = wiz.parse(t, "(A B[foo] C)"); - assertTrue(valid); - } - - @Test public void testParseWithText2() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B[T__32] (C (D E[a])))"); - // C pattern has no text arg so despite [bar] in t, no need - // to match text--check structure only. - boolean valid = wiz.parse(t, "(A B[foo] C)"); - assertEquals("(A T__32 (C (D a)))", t.toStringTree()); - } - - @Test public void testParseWithTextFails() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C)"); - boolean valid = wiz.parse(t, "(A[foo] B C)"); - assertTrue(!valid); // fails - } - - @Test public void testParseLabels() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C)"); - Map labels = new HashMap(); - boolean valid = wiz.parse(t, "(%a:A %b:B %c:C)", labels); - assertTrue(valid); - assertEquals("A", labels.get("a").toString()); - assertEquals("B", labels.get("b").toString()); - assertEquals("C", labels.get("c").toString()); - } - - @Test public void testParseWithWildcardLabels() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C)"); - Map labels = new HashMap(); - boolean valid = wiz.parse(t, "(A %b:. %c:.)", labels); - assertTrue(valid); - assertEquals("B", labels.get("b").toString()); - assertEquals("C", labels.get("c").toString()); - } - - @Test public void testParseLabelsAndTestText() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B[foo] C)"); - Map labels = new HashMap(); - boolean valid = wiz.parse(t, "(%a:A %b:B[foo] %c:C)", labels); - assertTrue(valid); - assertEquals("A", labels.get("a").toString()); - assertEquals("foo", labels.get("b").toString()); - assertEquals("C", labels.get("c").toString()); - } - - @Test public void testParseLabelsInNestedTree() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A (B C) (D E))"); - Map labels = new HashMap(); - boolean valid = wiz.parse(t, "(%a:A (%b:B %c:C) (%d:D %e:E) )", labels); - assertTrue(valid); - assertEquals("A", labels.get("a").toString()); - assertEquals("B", labels.get("b").toString()); - assertEquals("C", labels.get("c").toString()); - assertEquals("D", labels.get("d").toString()); - assertEquals("E", labels.get("e").toString()); - } - - @Test public void testEquals() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t1 = (CommonAST)wiz.create("(A B C)"); - CommonAST t2 = (CommonAST)wiz.create("(A B C)"); - boolean same = TreeWizard.equals(t1, t2, adaptor); - assertTrue(same); - } - - @Test public void testEqualsWithText() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t1 = (CommonAST)wiz.create("(A B[foo] C)"); - CommonAST t2 = (CommonAST)wiz.create("(A B[foo] C)"); - boolean same = TreeWizard.equals(t1, t2, adaptor); - assertTrue(same); - } - - @Test public void testEqualsWithMismatchedText() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t1 = (CommonAST)wiz.create("(A B[foo] C)"); - CommonAST t2 = (CommonAST)wiz.create("(A B C)"); - boolean same = TreeWizard.equals(t1, t2, adaptor); - assertTrue(!same); - } - - @Test public void testFindPattern() throws Exception { - TreeWizard wiz = new TreeWizard(adaptor, tokens); - CommonAST t = (CommonAST)wiz.create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))"); - final List subtrees = wiz.find(t, "(A B)"); - List elements = subtrees; - String found = elements.toString(); - String expecting = "[foo, big]"; - assertEquals(expecting, found); - } - -} diff --git a/tool/test/org/antlr/v4/test/TestTrees.java b/tool/test/org/antlr/v4/test/TestTrees.java deleted file mode 100644 index c72a58404..000000000 --- a/tool/test/org/antlr/v4/test/TestTrees.java +++ /dev/null @@ -1,405 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 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.test; - -import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.tree.*; -import org.junit.Test; - -public class TestTrees extends BaseTest { - ASTAdaptor adaptor = new CommonASTAdaptor(); - protected boolean debug = false; - - static class V extends CommonAST { - public int x; - public V(Token t) { this.token = t;} - public V(int ttype, int x) { this.x=x; token=new CommonToken(ttype); } - public V(int ttype, Token t, int x) { token=t; this.x=x;} - public String toString() { return (token!=null?token.getText():"")+"";} - } - - @Test public void testSingleNode() throws Exception { - CommonAST t = new CommonAST(new CommonToken(101)); - assertNull(t.parent); - assertEquals(-1, t.childIndex); - } - - @Test public void testTwoChildrenOfNilRoot() throws Exception { - CommonAST root_0 = (CommonAST)adaptor.nil(); - CommonAST t = new V(101, 2); - CommonAST u = new V(new CommonToken(102,"102")); - adaptor.addChild(root_0, t); - adaptor.addChild(root_0, u); - assertNull(root_0.parent); - assertEquals(-1, root_0.childIndex); - assertEquals(0, t.childIndex); - assertEquals(1, u.childIndex); - } - - @Test public void test4Nodes() throws Exception { - // ^(101 ^(102 103) 104) - CommonAST r0 = new CommonAST(new CommonToken(101)); - r0.addChild(new CommonAST(new CommonToken(102))); - r0.getChild(0).addChild(new CommonAST(new CommonToken(103))); - r0.addChild(new CommonAST(new CommonToken(104))); - - assertNull(r0.parent); - assertEquals(-1, r0.childIndex); - } - - @Test public void testList() throws Exception { - // ^(nil 101 102 103) - CommonAST r0 = new CommonAST((Token)null); - CommonAST c0, c1, c2; - r0.addChild(c0=new CommonAST(new CommonToken(101))); - r0.addChild(c1=new CommonAST(new CommonToken(102))); - r0.addChild(c2=new CommonAST(new CommonToken(103))); - - assertNull(r0.parent); - assertEquals(-1, r0.childIndex); - assertEquals(r0, c0.parent); - assertEquals(0, c0.childIndex); - assertEquals(r0, c1.parent); - assertEquals(1, c1.childIndex); - assertEquals(r0, c2.parent); - assertEquals(2, c2.childIndex); - } - - @Test public void testList2() throws Exception { - // Add child ^(nil 101 102 103) to root 5 - // should pull 101 102 103 directly to become 5's child list - CommonAST root = new CommonAST(new CommonToken(5)); - - // child tree - CommonAST r0 = new CommonAST((Token)null); - CommonAST c0, c1, c2; - r0.addChild(c0=new CommonAST(new CommonToken(101))); - r0.addChild(c1=new CommonAST(new CommonToken(102))); - r0.addChild(c2=new CommonAST(new CommonToken(103))); - - root.addChild(r0); - - assertNull(root.parent); - assertEquals(-1, root.childIndex); - // check children of root all point at root - assertEquals(root, c0.parent); - assertEquals(0, c0.childIndex); - assertEquals(root, c0.parent); - assertEquals(1, c1.childIndex); - assertEquals(root, c0.parent); - assertEquals(2, c2.childIndex); - } - - @Test public void testAddListToExistChildren() throws Exception { - // Add child ^(nil 101 102 103) to root ^(5 6) - // should add 101 102 103 to end of 5's child list - CommonAST root = new CommonAST(new CommonToken(5)); - root.addChild(new CommonAST(new CommonToken(6))); - - // child tree - CommonAST r0 = new CommonAST((Token)null); - CommonAST c0, c1, c2; - r0.addChild(c0=new CommonAST(new CommonToken(101))); - r0.addChild(c1=new CommonAST(new CommonToken(102))); - r0.addChild(c2=new CommonAST(new CommonToken(103))); - - root.addChild(r0); - - assertNull(root.parent); - assertEquals(-1, root.childIndex); - // check children of root all point at root - assertEquals(root, c0.parent); - assertEquals(1, c0.childIndex); - assertEquals(root, c0.parent); - assertEquals(2, c1.childIndex); - assertEquals(root, c0.parent); - assertEquals(3, c2.childIndex); - } - - @Test public void testDupTree() throws Exception { - // ^(101 ^(102 103 ^(106 107) ) 104 105) - CommonAST r0 = new CommonAST(new CommonToken(101)); - CommonAST r1 = new CommonAST(new CommonToken(102)); - r0.addChild(r1); - r1.addChild(new CommonAST(new CommonToken(103))); - BaseAST r2 = new CommonAST(new CommonToken(106)); - r2.addChild(new CommonAST(new CommonToken(107))); - r1.addChild(r2); - r0.addChild(new CommonAST(new CommonToken(104))); - r0.addChild(new CommonAST(new CommonToken(105))); - - CommonAST dup = (CommonAST)(new CommonASTAdaptor()).dupTree(r0); - - assertNull(dup.parent); - assertEquals(-1, dup.childIndex); - Trees.sanityCheckParentAndChildIndexes(dup); - } - - @Test public void testBecomeRoot() throws Exception { - // 5 becomes new root of ^(nil 101 102 103) - CommonAST newRoot = new CommonAST(new CommonToken(5)); - - CommonAST oldRoot = new CommonAST((Token)null); - oldRoot.addChild(new CommonAST(new CommonToken(101))); - oldRoot.addChild(new CommonAST(new CommonToken(102))); - oldRoot.addChild(new CommonAST(new CommonToken(103))); - - ASTAdaptor adaptor = new CommonASTAdaptor(); - adaptor.becomeRoot(newRoot, oldRoot); - Trees.sanityCheckParentAndChildIndexes(newRoot); - } - - @Test public void testBecomeRoot2() throws Exception { - // 5 becomes new root of ^(101 102 103) - CommonAST newRoot = new CommonAST(new CommonToken(5)); - - CommonAST oldRoot = new CommonAST(new CommonToken(101)); - oldRoot.addChild(new CommonAST(new CommonToken(102))); - oldRoot.addChild(new CommonAST(new CommonToken(103))); - - ASTAdaptor adaptor = new CommonASTAdaptor(); - adaptor.becomeRoot(newRoot, oldRoot); - Trees.sanityCheckParentAndChildIndexes(newRoot); - } - - @Test public void testBecomeRoot3() throws Exception { - // ^(nil 5) becomes new root of ^(nil 101 102 103) - CommonAST newRoot = new CommonAST((Token)null); - newRoot.addChild(new CommonAST(new CommonToken(5))); - - CommonAST oldRoot = new CommonAST((Token)null); - oldRoot.addChild(new CommonAST(new CommonToken(101))); - oldRoot.addChild(new CommonAST(new CommonToken(102))); - oldRoot.addChild(new CommonAST(new CommonToken(103))); - - ASTAdaptor adaptor = new CommonASTAdaptor(); - adaptor.becomeRoot(newRoot, oldRoot); - Trees.sanityCheckParentAndChildIndexes(newRoot); - } - - @Test public void testBecomeRoot5() throws Exception { - // ^(nil 5) becomes new root of ^(101 102 103) - CommonAST newRoot = new CommonAST((Token)null); - newRoot.addChild(new CommonAST(new CommonToken(5))); - - CommonAST oldRoot = new CommonAST(new CommonToken(101)); - oldRoot.addChild(new CommonAST(new CommonToken(102))); - oldRoot.addChild(new CommonAST(new CommonToken(103))); - - ASTAdaptor adaptor = new CommonASTAdaptor(); - adaptor.becomeRoot(newRoot, oldRoot); - Trees.sanityCheckParentAndChildIndexes(newRoot); - } - - @Test public void testBecomeRoot6() throws Exception { - // emulates construction of ^(5 6) - CommonAST root_0 = (CommonAST)adaptor.nil(); - CommonAST root_1 = (CommonAST)adaptor.nil(); - root_1 = (CommonAST)adaptor.becomeRoot(new CommonAST(new CommonToken(5)), root_1); - - adaptor.addChild(root_1, new CommonAST(new CommonToken(6))); - - adaptor.addChild(root_0, root_1); - - Trees.sanityCheckParentAndChildIndexes(root_0); - } - - // Test replaceChildren - - @Test public void testReplaceWithNoChildren() throws Exception { - CommonAST t = new CommonAST(new CommonToken(101)); - CommonAST newChild = new CommonAST(new CommonToken(5)); - boolean error = false; - try { - Trees.replaceChildren(t, 0, 0, newChild); - } - catch (IllegalArgumentException iae) { - error = true; - } - assertTrue(error); - } - - @Test public void testReplaceWithOneChildren() throws Exception { - // assume token type 99 and use text - CommonAST t = new CommonAST(new CommonToken(99,"a")); - CommonAST c0 = new CommonAST(new CommonToken(99, "b")); - t.addChild(c0); - - CommonAST newChild = new CommonAST(new CommonToken(99, "c")); - Trees.replaceChildren(t, 0, 0, newChild); - String expecting = "(a c)"; - assertEquals(expecting, t.toStringTree()); - Trees.sanityCheckParentAndChildIndexes(t); - } - - @Test public void testReplaceInMiddle() throws Exception { - CommonAST t = new CommonAST(new CommonToken(99, "a")); - t.addChild(new CommonAST(new CommonToken(99, "b"))); - t.addChild(new CommonAST(new CommonToken(99, "c"))); // index 1 - t.addChild(new CommonAST(new CommonToken(99, "d"))); - - CommonAST newChild = new CommonAST(new CommonToken(99,"x")); - Trees.replaceChildren(t, 1, 1, newChild); - String expecting = "(a b x d)"; - assertEquals(expecting, t.toStringTree()); - Trees.sanityCheckParentAndChildIndexes(t); - } - - @Test public void testReplaceAtLeft() throws Exception { - CommonAST t = new CommonAST(new CommonToken(99, "a")); - t.addChild(new CommonAST(new CommonToken(99, "b"))); // index 0 - t.addChild(new CommonAST(new CommonToken(99, "c"))); - t.addChild(new CommonAST(new CommonToken(99, "d"))); - - CommonAST newChild = new CommonAST(new CommonToken(99,"x")); - Trees.replaceChildren(t, 0, 0, newChild); - String expecting = "(a x c d)"; - assertEquals(expecting, t.toStringTree()); - Trees.sanityCheckParentAndChildIndexes(t); - } - - @Test public void testReplaceAtRight() throws Exception { - CommonAST t = new CommonAST(new CommonToken(99, "a")); - t.addChild(new CommonAST(new CommonToken(99, "b"))); - t.addChild(new CommonAST(new CommonToken(99, "c"))); - t.addChild(new CommonAST(new CommonToken(99, "d"))); // index 2 - - CommonAST newChild = new CommonAST(new CommonToken(99,"x")); - Trees.replaceChildren(t, 2, 2, newChild); - String expecting = "(a b c x)"; - assertEquals(expecting, t.toStringTree()); - Trees.sanityCheckParentAndChildIndexes(t); - } - - @Test public void testReplaceOneWithTwoAtLeft() throws Exception { - CommonAST t = new CommonAST(new CommonToken(99, "a")); - t.addChild(new CommonAST(new CommonToken(99, "b"))); - t.addChild(new CommonAST(new CommonToken(99, "c"))); - t.addChild(new CommonAST(new CommonToken(99, "d"))); - - CommonAST newChildren = (CommonAST)adaptor.nil(); - newChildren.addChild(new CommonAST(new CommonToken(99,"x"))); - newChildren.addChild(new CommonAST(new CommonToken(99,"y"))); - - Trees.replaceChildren(t, 0, 0, newChildren); - String expecting = "(a x y c d)"; - assertEquals(expecting, t.toStringTree()); - Trees.sanityCheckParentAndChildIndexes(t); - } - - @Test public void testReplaceOneWithTwoAtRight() throws Exception { - CommonAST t = new CommonAST(new CommonToken(99, "a")); - t.addChild(new CommonAST(new CommonToken(99, "b"))); - t.addChild(new CommonAST(new CommonToken(99, "c"))); - t.addChild(new CommonAST(new CommonToken(99, "d"))); - - CommonAST newChildren = (CommonAST)adaptor.nil(); - newChildren.addChild(new CommonAST(new CommonToken(99,"x"))); - newChildren.addChild(new CommonAST(new CommonToken(99,"y"))); - - Trees.replaceChildren(t, 2, 2, newChildren); - String expecting = "(a b c x y)"; - assertEquals(expecting, t.toStringTree()); - Trees.sanityCheckParentAndChildIndexes(t); - } - - @Test public void testReplaceOneWithTwoInMiddle() throws Exception { - CommonAST t = new CommonAST(new CommonToken(99, "a")); - t.addChild(new CommonAST(new CommonToken(99, "b"))); - t.addChild(new CommonAST(new CommonToken(99, "c"))); - t.addChild(new CommonAST(new CommonToken(99, "d"))); - - CommonAST newChildren = (CommonAST)adaptor.nil(); - newChildren.addChild(new CommonAST(new CommonToken(99,"x"))); - newChildren.addChild(new CommonAST(new CommonToken(99,"y"))); - - Trees.replaceChildren(t, 1, 1, newChildren); - String expecting = "(a b x y d)"; - assertEquals(expecting, t.toStringTree()); - Trees.sanityCheckParentAndChildIndexes(t); - } - - @Test public void testReplaceTwoWithOneAtLeft() throws Exception { - CommonAST t = new CommonAST(new CommonToken(99, "a")); - t.addChild(new CommonAST(new CommonToken(99, "b"))); - t.addChild(new CommonAST(new CommonToken(99, "c"))); - t.addChild(new CommonAST(new CommonToken(99, "d"))); - - CommonAST newChild = new CommonAST(new CommonToken(99,"x")); - - Trees.replaceChildren(t, 0, 1, newChild); - String expecting = "(a x d)"; - assertEquals(expecting, t.toStringTree()); - Trees.sanityCheckParentAndChildIndexes(t); - } - - @Test public void testReplaceTwoWithOneAtRight() throws Exception { - CommonAST t = new CommonAST(new CommonToken(99, "a")); - t.addChild(new CommonAST(new CommonToken(99, "b"))); - t.addChild(new CommonAST(new CommonToken(99, "c"))); - t.addChild(new CommonAST(new CommonToken(99, "d"))); - - CommonAST newChild = new CommonAST(new CommonToken(99,"x")); - - Trees.replaceChildren(t, 1, 2, newChild); - String expecting = "(a b x)"; - assertEquals(expecting, t.toStringTree()); - Trees.sanityCheckParentAndChildIndexes(t); - } - - @Test public void testReplaceAllWithOne() throws Exception { - CommonAST t = new CommonAST(new CommonToken(99, "a")); - t.addChild(new CommonAST(new CommonToken(99, "b"))); - t.addChild(new CommonAST(new CommonToken(99, "c"))); - t.addChild(new CommonAST(new CommonToken(99, "d"))); - - CommonAST newChild = new CommonAST(new CommonToken(99,"x")); - - Trees.replaceChildren(t, 0, 2, newChild); - String expecting = "(a x)"; - assertEquals(expecting, t.toStringTree()); - Trees.sanityCheckParentAndChildIndexes(t); - } - - @Test public void testReplaceAllWithTwo() throws Exception { - CommonAST t = new CommonAST(new CommonToken(99, "a")); - t.addChild(new CommonAST(new CommonToken(99, "b"))); - t.addChild(new CommonAST(new CommonToken(99, "c"))); - t.addChild(new CommonAST(new CommonToken(99, "d"))); - - CommonAST newChildren = (CommonAST)adaptor.nil(); - newChildren.addChild(new CommonAST(new CommonToken(99,"x"))); - newChildren.addChild(new CommonAST(new CommonToken(99,"y"))); - - Trees.replaceChildren(t, 0, 2, newChildren); - String expecting = "(a x y)"; - assertEquals(expecting, t.toStringTree()); - Trees.sanityCheckParentAndChildIndexes(t); - } -}