From 3aeeb2b2779970e1d26fdd48f2805f4eb30663f5 Mon Sep 17 00:00:00 2001 From: parrt Date: Mon, 2 Jan 2012 18:36:59 -0800 Subject: [PATCH] rename BaseREcognizer as Parser [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9781] --- .../antlr/v4/runtime/ANTLRErrorStrategy.java | 22 +- .../antlr/v4/runtime/BailErrorStrategy.java | 6 +- .../org/antlr/v4/runtime/BaseRecognizer.java | 473 ----------------- .../v4/runtime/DefaultErrorStrategy.java | 44 +- .../v4/runtime/DiagnosticErrorStrategy.java | 8 +- .../v4/runtime/FailedPredicateException.java | 4 +- .../v4/runtime/InputMismatchException.java | 2 +- .../v4/runtime/NoViableAltException.java | 4 +- .../Java/src/org/antlr/v4/runtime/Parser.java | 479 +++++++++++++++++- .../antlr/v4/runtime/ParserRuleContext.java | 2 +- .../v4/runtime/RecognitionException.java | 4 +- .../src/org/antlr/v4/runtime/RuleContext.java | 8 +- .../v4/runtime/atn/ParserATNPathFinder.java | 4 +- .../v4/runtime/atn/v2ParserATNSimulator.java | 4 +- .../src/org/antlr/v4/runtime/tree/Trees.java | 14 +- .../tree/gui/TreePostScriptGenerator.java | 6 +- .../antlr/v4/runtime/tree/gui/TreeViewer.java | 10 +- .../org/antlr/v4/test/TestPerformance.java | 4 +- 18 files changed, 524 insertions(+), 574 deletions(-) delete mode 100644 runtime/Java/src/org/antlr/v4/runtime/BaseRecognizer.java diff --git a/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java index 86ca2845c..5d07747c5 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java @@ -32,7 +32,7 @@ import org.antlr.v4.runtime.misc.OrderedHashSet; */ public interface ANTLRErrorStrategy { /** Report any kind of RecognitionException. */ - void reportError(@NotNull BaseRecognizer recognizer, + void reportError(@NotNull Parser 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. */ - Token recoverInline(@NotNull BaseRecognizer recognizer) + Token recoverInline(@NotNull Parser 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 Parser 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 Parser 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 Parser 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 Parser 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 Parser 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 Parser recognizer, DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, @NotNull OrderedHashSet configs); @@ -135,7 +135,7 @@ public interface ANTLRErrorStrategy { // @NotNull OrderedHashSet configs); - void reportAttemptingFullContext(@NotNull BaseRecognizer recognizer, + void reportAttemptingFullContext(@NotNull Parser 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 Parser 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 Parser 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 a0be4fc7f..6e0f6ae31 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/BailErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/BailErrorStrategy.java @@ -39,7 +39,7 @@ public class BailErrorStrategy extends DefaultErrorStrategy { * RuntimeException. */ @Override - public void recover(BaseRecognizer recognizer, RecognitionException e) { + public void recover(Parser 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 Token recoverInline(BaseRecognizer recognizer) + public Token recoverInline(Parser 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(Parser recognizer) { } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/BaseRecognizer.java b/runtime/Java/src/org/antlr/v4/runtime/BaseRecognizer.java deleted file mode 100644 index e84d3c1d4..000000000 --- a/runtime/Java/src/org/antlr/v4/runtime/BaseRecognizer.java +++ /dev/null @@ -1,473 +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.*; -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.ParseTreeListener; - -import java.util.ArrayList; -import java.util.List; - -/** A generic recognizer that can handle recognizers generated from - * parser and tree grammars. This is all the parsing - * support code essentially; most of it is error recovery stuff and - * backtracking. - * - * TODO: rename / reorg with parser - */ -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 boolean buildParseTrees; - protected boolean traceATNStates; - - /** If the listener is non-null, trigger enter and exit rule events - * *during* the parse. This is typically done only when not building - * parse trees for later visiting. We either trigger events during - * the parse or during tree walks later. Both could be done. - * Not intended for tree parsing but would work. - */ - protected ParseTreeListener _listener; - - /** Did the recognizer encounter a syntax error? Track how many. */ - protected int syntaxErrors = 0; - - public BaseRecognizer(IntStream input) { - setInputStream(input); - } - - /** reset the parser's state */ - public void reset() { - if ( getInputStream()!=null ) getInputStream().seek(0); - _errHandler.endErrorCondition(this); - _ctx = null; - syntaxErrors = 0; - ATNSimulator interpreter = getInterpreter(); - if (interpreter != null) { - interpreter.reset(); - } - } - - /** Match current input symbol against ttype. Attempt - * single token insertion or deletion error recovery. If - * that fails, throw MismatchedTokenException. - */ - public Token match(int ttype) throws RecognitionException { -// System.out.println("match "+((TokenStream)input).LT(1)+" vs expected "+ttype); - Token currentSymbol = getCurrentToken(); - if ( getInputStream().LA(1)==ttype ) { - _errHandler.endErrorCondition(this); - consume(); - } - else { - currentSymbol = _errHandler.recoverInline(this); - if ( buildParseTrees && currentSymbol instanceof Token && - ((Token)currentSymbol).getTokenIndex()==-1 ) - { - // we must have conjured up a new token during single token insertion - // if it's not the current symbol - _ctx.addErrorNode((Token)currentSymbol); - } - } - return currentSymbol; - } - - /** Track the RuleContext objects during the parse and hook them up - * using the children list so that it forms a parse tree. - * The RuleContext returned from the start rule represents the root - * of the parse tree. - * - * To built parse trees, all we have to do is put a hook in setState() - * and enterRule(). In setState(), we add tokens to the current context - * as children. By the time we get to enterRule(), we are already - * in an invoked rule so we add this context as a child of the parent - * (invoking) context. Simple and effective. - * - * Note that if we are not building parse trees, rule contexts - * only point upwards. When a rule exits, it returns the context - * but that gets garbage collected if nobody holds a reference. - * It points upwards but nobody points at it. - * - * When we build parse trees, we are adding all of these contexts to - * somebody's children list. Contexts are then not candidates - * for garbage collection. - */ - public void setBuildParseTree(boolean buildParseTrees) { - this.buildParseTrees = buildParseTrees; - } - - public boolean getBuildParseTree() { - return buildParseTrees; - } - - public void setTraceATNStates(boolean traceATNStates) { - this.traceATNStates = traceATNStates; - } - - public boolean getTraceATNStates() { - return traceATNStates; - } - - public ParseTreeListener getListener() { - return _listener; - } - - public void setListener(ParseTreeListener listener) { - this._listener = listener; - } - - /** Get number of recognition errors (lexer, parser, tree parser). Each - * recognizer tracks its own number. So parser and lexer each have - * separate count. Does not count the spurious errors found between - * an error and next valid token match - * - * See also reportError() - */ - public int getNumberOfSyntaxErrors() { - return syntaxErrors; - } - - @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(); - if ( input instanceof TokenStream ) { - return ((TokenStream)input).toString(start,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. - */ - public Token getCurrentToken() { - return _input.LT(1); - } - - public void notifyListeners(String msg) { - notifyListeners(getCurrentToken(), msg, null); - } - - public void notifyListeners(Token offendingToken, String msg, - @Nullable RecognitionException e) - { - int line = -1; - int charPositionInLine = -1; - if (offendingToken instanceof Token) { - line = ((Token) offendingToken).getLine(); - charPositionInLine = ((Token) offendingToken).getCharPositionInLine(); - } - ANTLRErrorListener[] listeners = getListeners(); - if ( listeners.length == 0 ) { - System.err.println("line "+line+":"+charPositionInLine+" "+msg); - return; - } - for (ANTLRErrorListener pl : listeners) { - pl.error(this, offendingToken, line, charPositionInLine, msg, e); - } - } - - 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 ) { - ParserRuleContext parent = (ParserRuleContext)_ctx.parent; - parent.removeLastChild(); - if ( parent!=null ) parent.addChild(localctx); - } - _ctx = localctx; - _ctx.altNum = altNum; - } - - /** Consume the current symbol and return it. E.g., given the following - * input with A being the current lookahead symbol: - * - * A B - * ^ - * - * this function moves the cursor to B and returns A. - * - * If the parser is creating parse trees, the current symbol - * would also be added as a child to the current context (node). - * - * Trigger listener events if there's a listener. - */ - public Token consume() { - Token o = getCurrentToken(); - getInputStream().consume(); - if ( buildParseTrees ) { - // TODO: tree parsers? - if ( _errHandler.inErrorRecoveryMode(this) ) { -// System.out.println("consume in error recovery mode for "+o); - _ctx.addErrorNode((Token) o); - } - else _ctx.addChild((Token)o); - } - if ( _listener != null) _listener.visitTerminal(o); - return o; - } - - protected void addContextToParseTree() { - ParserRuleContext parent = (ParserRuleContext)_ctx.parent; - // add current context to parent if we have a parent - if ( parent!=null ) { - parent.addChild(_ctx); - } - } - - /** 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 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; - } - return null; - } - - public ParserRuleContext getContext() { - return _ctx; - } - - public boolean inContext(String context) { - // TODO: useful in parser? - return false; - } - - public boolean isExpectedToken(int symbol) { -// return getInterpreter().atn.nextTokens(_ctx); - ATN atn = getInterpreter().atn; - ParserRuleContext ctx = _ctx; - ATNState s = atn.states.get(ctx.s); - IntervalSet following = atn.nextTokens(s); - if (following.contains(symbol)) { - return true; - } -// System.out.println("following "+s+"="+following); - if ( !following.contains(Token.EPSILON) ) return false; - - while ( ctx!=null && ctx.invokingState>=0 && following.contains(Token.EPSILON) ) { - ATNState invokingState = atn.states.get(ctx.invokingState); - RuleTransition rt = (RuleTransition)invokingState.transition(0); - following = atn.nextTokens(rt.followState); - if (following.contains(symbol)) { - return true; - } - - ctx = (ParserRuleContext)ctx.parent; - } - - if ( following.contains(Token.EPSILON) && symbol == Token.EOF ) { - return true; - } - - return false; - } - - /** Compute the set of valid tokens reachable from the current - * position in the parse. - */ - public IntervalSet getExpectedTokens() { - ATN atn = getInterpreter().atn; - ParserRuleContext ctx = _ctx; - ATNState s = atn.states.get(ctx.s); - IntervalSet following = atn.nextTokens(s); -// System.out.println("following "+s+"="+following); - if ( !following.contains(Token.EPSILON) ) return following; - IntervalSet expected = new IntervalSet(); - expected.addAll(following); - expected.remove(Token.EPSILON); - while ( ctx!=null && ctx.invokingState>=0 && following.contains(Token.EPSILON) ) { - ATNState invokingState = atn.states.get(ctx.invokingState); - RuleTransition rt = (RuleTransition)invokingState.transition(0); - following = atn.nextTokens(rt.followState); - expected.addAll(following); - expected.remove(Token.EPSILON); - ctx = (ParserRuleContext)ctx.parent; - } - if ( following.contains(Token.EPSILON) ) { - expected.add(Token.EOF); - } - return expected; - } - - public IntervalSet getExpectedTokensWithinCurrentRule() { - ATN atn = getInterpreter().atn; - ATNState s = atn.states.get(_ctx.s); - return atn.nextTokens(s); - } - -// /** Compute the set of valid tokens reachable from the current -// * position in the parse. -// */ -// public IntervalSet nextTokens(@NotNull RuleContext ctx) { -// ATN atn = getInterpreter().atn; -// ATNState s = atn.states.get(ctx.s); -// if ( s == null ) return null; -// return atn.nextTokens(s, ctx); -// } - - /** Return List of the rule names in your parser instance - * leading up to a call to the current rule. You could override if - * you want more details such as the file/line info of where - * in the ATN a rule is invoked. - * - * This is very useful for error messages. - */ - public List getRuleInvocationStack() { - String[] ruleNames = getRuleNames(); - List stack = new ArrayList(); - RuleContext p = _ctx; - while ( p!=null ) { - // compute what follows who invoked us - stack.add(ruleNames[p.getRuleIndex()]); - p = p.parent; - } - return stack; - } - - /** For debugging and other purposes, might want the grammar name. - * Have ANTLR generate an implementation for this method. - */ - public String getGrammarFileName() { - return null; - } - - /** For debugging and other purposes */ - public List getDFAStrings() { - List s = new ArrayList(); - for (int d = 0; d < _interp.decisionToDFA.length; d++) { - DFA dfa = _interp.decisionToDFA[d]; - s.add( dfa.toString(getTokenNames()) ); - } - return s; - } - - /** For debugging and other purposes */ - public void dumpDFA() { - boolean seenOne = false; - for (int d = 0; d < _interp.decisionToDFA.length; d++) { - DFA dfa = _interp.decisionToDFA[d]; - if ( dfa!=null ) { - if ( seenOne ) System.out.println(); - System.out.println("Decision " + dfa.decision + ":"); - System.out.print(dfa.toString(getTokenNames())); - seenOne = true; - } - } - } - - public String getSourceName() { - return _input.getSourceName(); - } - - /** A convenience method for use most often with template rewrites. - * Convert a List to List - */ - public List toStrings(List tokens) { - if ( tokens==null ) return null; - List strings = new ArrayList(tokens.size()); - for (int i=0; i configs) { } @Override - public void reportAttemptingFullContext(@NotNull BaseRecognizer recognizer, + public void reportAttemptingFullContext(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull OrderedHashSet configs) @@ -565,13 +565,13 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy { } @Override - public void reportContextSensitivity(@NotNull BaseRecognizer recognizer, @NotNull DFA dfa, + public void reportContextSensitivity(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull OrderedHashSet configs) { } @Override - public void reportInsufficientPredicates(@NotNull BaseRecognizer recognizer, + public void reportInsufficientPredicates(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, diff --git a/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java index 11404cbb9..afb357ff0 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java @@ -41,7 +41,7 @@ import java.util.Arrays; public class DiagnosticErrorStrategy extends DefaultErrorStrategy { @Override - public void reportAmbiguity(@NotNull BaseRecognizer recognizer, + public void reportAmbiguity(@NotNull Parser recognizer, DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, @NotNull OrderedHashSet configs) { @@ -50,7 +50,7 @@ public class DiagnosticErrorStrategy extends DefaultErrorStrategy { } @Override - public void reportAttemptingFullContext(@NotNull BaseRecognizer recognizer, + public void reportAttemptingFullContext(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @NotNull OrderedHashSet configs) @@ -60,7 +60,7 @@ public class DiagnosticErrorStrategy extends DefaultErrorStrategy { } @Override - public void reportContextSensitivity(@NotNull BaseRecognizer recognizer, @NotNull DFA dfa, + public void reportContextSensitivity(@NotNull Parser 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 { } @Override - public void reportInsufficientPredicates(@NotNull BaseRecognizer recognizer, + public void reportInsufficientPredicates(@NotNull Parser 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 ada98738e..ff6cc5576 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java @@ -42,11 +42,11 @@ public class FailedPredicateException extends RecognitionException { public int predIndex; public String msg; - public FailedPredicateException(BaseRecognizer recognizer) { + public FailedPredicateException(Parser recognizer) { this(recognizer, null); } - public FailedPredicateException(BaseRecognizer recognizer, @Nullable String msg) { + public FailedPredicateException(Parser 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); diff --git a/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java b/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java index 3d478af40..df9ebcc8d 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java @@ -4,7 +4,7 @@ package org.antlr.v4.runtime; * when the current input does not match the expected token or tree node. */ public class InputMismatchException extends RecognitionException { - public InputMismatchException(BaseRecognizer recognizer) { + public InputMismatchException(Parser recognizer) { super(recognizer, recognizer.getInputStream(), recognizer._ctx); 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 694c637a3..d68fc5849 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java @@ -45,7 +45,7 @@ public class NoViableAltException extends RecognitionException { */ public Token startToken; - public NoViableAltException(BaseRecognizer recognizer) { // LL(1) error + public NoViableAltException(Parser recognizer) { // LL(1) error this(recognizer,recognizer.getInputStream(), recognizer.getCurrentToken(), recognizer.getCurrentToken(), @@ -53,7 +53,7 @@ public class NoViableAltException extends RecognitionException { recognizer._ctx); } - public NoViableAltException(BaseRecognizer recognizer, + public NoViableAltException(Parser recognizer, SymbolStream input, Token startToken, Token offendingToken, diff --git a/runtime/Java/src/org/antlr/v4/runtime/Parser.java b/runtime/Java/src/org/antlr/v4/runtime/Parser.java index e132ca21e..348f9b424 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Parser.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Parser.java @@ -1,44 +1,467 @@ /* [The "BSD license"] - Copyright (c) 2011 Terence Parr - All rights reserved. + 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: + 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. + 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. + 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.*; +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.ParseTreeListener; -/** A parser for TokenStreams. "parser grammars" result in a subclass - * of this. - */ -public class Parser extends BaseRecognizer { - public Parser(TokenStream input) { - super(input); +import java.util.ArrayList; +import java.util.List; + +/** This is all the parsing support code essentially; most of it is error recovery stuff. */ +public abstract class Parser 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 boolean buildParseTrees; + protected boolean traceATNStates; + + /** If the listener is non-null, trigger enter and exit rule events + * *during* the parse. This is typically done only when not building + * parse trees for later visiting. We either trigger events during + * the parse or during tree walks later. Both could be done. + * Not intended for tree parsing but would work. + */ + protected ParseTreeListener _listener; + + /** Did the recognizer encounter a syntax error? Track how many. */ + protected int syntaxErrors = 0; + + public Parser(IntStream input) { + setInputStream(input); + } + + /** reset the parser's state */ + public void reset() { + if ( getInputStream()!=null ) getInputStream().seek(0); + _errHandler.endErrorCondition(this); + _ctx = null; + syntaxErrors = 0; + ATNSimulator interpreter = getInterpreter(); + if (interpreter != null) { + interpreter.reset(); + } + } + + /** Match current input symbol against ttype. Attempt + * single token insertion or deletion error recovery. If + * that fails, throw MismatchedTokenException. + */ + public Token match(int ttype) throws RecognitionException { +// System.out.println("match "+((TokenStream)input).LT(1)+" vs expected "+ttype); + Token currentSymbol = getCurrentToken(); + if ( getInputStream().LA(1)==ttype ) { + _errHandler.endErrorCondition(this); + consume(); + } + else { + currentSymbol = _errHandler.recoverInline(this); + if ( buildParseTrees && currentSymbol instanceof Token && + ((Token)currentSymbol).getTokenIndex()==-1 ) + { + // we must have conjured up a new token during single token insertion + // if it's not the current symbol + _ctx.addErrorNode((Token)currentSymbol); + } + } + return currentSymbol; + } + + /** Track the RuleContext objects during the parse and hook them up + * using the children list so that it forms a parse tree. + * The RuleContext returned from the start rule represents the root + * of the parse tree. + * + * To built parse trees, all we have to do is put a hook in setState() + * and enterRule(). In setState(), we add tokens to the current context + * as children. By the time we get to enterRule(), we are already + * in an invoked rule so we add this context as a child of the parent + * (invoking) context. Simple and effective. + * + * Note that if we are not building parse trees, rule contexts + * only point upwards. When a rule exits, it returns the context + * but that gets garbage collected if nobody holds a reference. + * It points upwards but nobody points at it. + * + * When we build parse trees, we are adding all of these contexts to + * somebody's children list. Contexts are then not candidates + * for garbage collection. + */ + public void setBuildParseTree(boolean buildParseTrees) { + this.buildParseTrees = buildParseTrees; + } + + public boolean getBuildParseTree() { + return buildParseTrees; + } + + public void setTraceATNStates(boolean traceATNStates) { + this.traceATNStates = traceATNStates; + } + + public boolean getTraceATNStates() { + return traceATNStates; + } + + public ParseTreeListener getListener() { + return _listener; } + public void setListener(ParseTreeListener listener) { + this._listener = listener; + } + + /** Get number of recognition errors (lexer, parser, tree parser). Each + * recognizer tracks its own number. So parser and lexer each have + * separate count. Does not count the spurious errors found between + * an error and next valid token match + * + * See also reportError() + */ + public int getNumberOfSyntaxErrors() { + return syntaxErrors; + } + @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(); + if ( input instanceof TokenStream ) { + return ((TokenStream)input).toString(start,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. + */ + public Token getCurrentToken() { + return _input.LT(1); + } + + public void notifyListeners(String msg) { + notifyListeners(getCurrentToken(), msg, null); + } + + public void notifyListeners(Token offendingToken, String msg, + @Nullable RecognitionException e) + { + int line = -1; + int charPositionInLine = -1; + if (offendingToken instanceof Token) { + line = ((Token) offendingToken).getLine(); + charPositionInLine = ((Token) offendingToken).getCharPositionInLine(); + } + ANTLRErrorListener[] listeners = getListeners(); + if ( listeners.length == 0 ) { + System.err.println("line "+line+":"+charPositionInLine+" "+msg); + return; + } + for (ANTLRErrorListener pl : listeners) { + pl.error(this, offendingToken, line, charPositionInLine, msg, e); + } + } + + 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 ) { + ParserRuleContext parent = (ParserRuleContext)_ctx.parent; + parent.removeLastChild(); + if ( parent!=null ) parent.addChild(localctx); + } + _ctx = localctx; + _ctx.altNum = altNum; + } + + /** Consume the current symbol and return it. E.g., given the following + * input with A being the current lookahead symbol: + * + * A B + * ^ + * + * this function moves the cursor to B and returns A. + * + * If the parser is creating parse trees, the current symbol + * would also be added as a child to the current context (node). + * + * Trigger listener events if there's a listener. + */ + public Token consume() { + Token o = getCurrentToken(); + getInputStream().consume(); + if ( buildParseTrees ) { + // TODO: tree parsers? + if ( _errHandler.inErrorRecoveryMode(this) ) { +// System.out.println("consume in error recovery mode for "+o); + _ctx.addErrorNode((Token) o); + } + else _ctx.addChild((Token)o); + } + if ( _listener != null) _listener.visitTerminal(o); + return o; + } + + protected void addContextToParseTree() { + ParserRuleContext parent = (ParserRuleContext)_ctx.parent; + // add current context to parent if we have a parent + if ( parent!=null ) { + parent.addChild(_ctx); + } + } + + /** 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 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; + } + return null; + } + + public ParserRuleContext getContext() { + return _ctx; + } + + public boolean inContext(String context) { + // TODO: useful in parser? + return false; + } + + public boolean isExpectedToken(int symbol) { +// return getInterpreter().atn.nextTokens(_ctx); + ATN atn = getInterpreter().atn; + ParserRuleContext ctx = _ctx; + ATNState s = atn.states.get(ctx.s); + IntervalSet following = atn.nextTokens(s); + if (following.contains(symbol)) { + return true; + } +// System.out.println("following "+s+"="+following); + if ( !following.contains(Token.EPSILON) ) return false; + + while ( ctx!=null && ctx.invokingState>=0 && following.contains(Token.EPSILON) ) { + ATNState invokingState = atn.states.get(ctx.invokingState); + RuleTransition rt = (RuleTransition)invokingState.transition(0); + following = atn.nextTokens(rt.followState); + if (following.contains(symbol)) { + return true; + } + + ctx = (ParserRuleContext)ctx.parent; + } + + if ( following.contains(Token.EPSILON) && symbol == Token.EOF ) { + return true; + } + + return false; + } + + /** Compute the set of valid tokens reachable from the current + * position in the parse. + */ + public IntervalSet getExpectedTokens() { + ATN atn = getInterpreter().atn; + ParserRuleContext ctx = _ctx; + ATNState s = atn.states.get(ctx.s); + IntervalSet following = atn.nextTokens(s); +// System.out.println("following "+s+"="+following); + if ( !following.contains(Token.EPSILON) ) return following; + IntervalSet expected = new IntervalSet(); + expected.addAll(following); + expected.remove(Token.EPSILON); + while ( ctx!=null && ctx.invokingState>=0 && following.contains(Token.EPSILON) ) { + ATNState invokingState = atn.states.get(ctx.invokingState); + RuleTransition rt = (RuleTransition)invokingState.transition(0); + following = atn.nextTokens(rt.followState); + expected.addAll(following); + expected.remove(Token.EPSILON); + ctx = (ParserRuleContext)ctx.parent; + } + if ( following.contains(Token.EPSILON) ) { + expected.add(Token.EOF); + } + return expected; + } + + public IntervalSet getExpectedTokensWithinCurrentRule() { + ATN atn = getInterpreter().atn; + ATNState s = atn.states.get(_ctx.s); + return atn.nextTokens(s); + } + +// /** Compute the set of valid tokens reachable from the current +// * position in the parse. +// */ +// public IntervalSet nextTokens(@NotNull RuleContext ctx) { +// ATN atn = getInterpreter().atn; +// ATNState s = atn.states.get(ctx.s); +// if ( s == null ) return null; +// return atn.nextTokens(s, ctx); +// } + + /** Return List of the rule names in your parser instance + * leading up to a call to the current rule. You could override if + * you want more details such as the file/line info of where + * in the ATN a rule is invoked. + * + * This is very useful for error messages. + */ + public List getRuleInvocationStack() { + String[] ruleNames = getRuleNames(); + List stack = new ArrayList(); + RuleContext p = _ctx; + while ( p!=null ) { + // compute what follows who invoked us + stack.add(ruleNames[p.getRuleIndex()]); + p = p.parent; + } + return stack; + } + + /** For debugging and other purposes, might want the grammar name. + * Have ANTLR generate an implementation for this method. + */ + public String getGrammarFileName() { + return null; + } + + /** For debugging and other purposes */ + public List getDFAStrings() { + List s = new ArrayList(); + for (int d = 0; d < _interp.decisionToDFA.length; d++) { + DFA dfa = _interp.decisionToDFA[d]; + s.add( dfa.toString(getTokenNames()) ); + } + return s; + } + + /** For debugging and other purposes */ + public void dumpDFA() { + boolean seenOne = false; + for (int d = 0; d < _interp.decisionToDFA.length; d++) { + DFA dfa = _interp.decisionToDFA[d]; + if ( dfa!=null ) { + if ( seenOne ) System.out.println(); + System.out.println("Decision " + dfa.decision + ":"); + System.out.print(dfa.toString(getTokenNames())); + seenOne = true; + } + } + } + public String getSourceName() { return _input.getSourceName(); } + + /** A convenience method for use most often with template rewrites. + * Convert a List to List + */ + public List toStrings(List tokens) { + if ( tokens==null ) return null; + List strings = new ArrayList(tokens.size()); + for (int i=0; i extends RuleContext { } /** Used for rule context info debugging during runtime, not so much for ATN debugging */ - public String toInfoString(BaseRecognizer recognizer) { + public String toInfoString(Parser 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 aa7a76648..294abea92 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java @@ -79,8 +79,8 @@ public class RecognitionException extends RuntimeException { public IntervalSet getExpectedTokens() { // TODO: do we really need this type check? - if ( recognizer!=null && recognizer instanceof BaseRecognizer ) { - return ((BaseRecognizer) recognizer).getExpectedTokens(); + if ( recognizer!=null && recognizer instanceof Parser) { + return ((Parser) recognizer).getExpectedTokens(); } return null; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java index 878b2c35a..2d0113178 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(Parser parser) { TreeViewer viewer = new TreeViewer(parser, this); viewer.open(); } - public void save(BaseRecognizer parser, String fileName) + public void save(Parser parser, String fileName) throws IOException, PrintException { Trees.writePS(this, parser, fileName); } - public void save(BaseRecognizer parser, String fileName, + public void save(Parser 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(Parser recog) { return Trees.toStringTree(this, recog); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNPathFinder.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNPathFinder.java index afdc54e38..f21e95b04 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNPathFinder.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNPathFinder.java @@ -29,7 +29,7 @@ package org.antlr.v4.runtime.atn; -import org.antlr.v4.runtime.BaseRecognizer; +import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.RuleContext; import org.antlr.v4.runtime.TokenStream; @@ -44,7 +44,7 @@ import java.util.List; import java.util.Set; public class ParserATNPathFinder extends v2ParserATNSimulator { - public ParserATNPathFinder(@Nullable BaseRecognizer parser, @NotNull ATN atn) { + public ParserATNPathFinder(@Nullable Parser parser, @NotNull ATN atn) { super(parser, atn); } 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 8f8cba718..1d8edf2b4 100755 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/v2ParserATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/v2ParserATNSimulator.java @@ -236,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 Parser parser; @NotNull public final DFA[] decisionToDFA; @@ -246,7 +246,7 @@ public class v2ParserATNSimulator extends ATNSimulator { this(null, atn); } - public v2ParserATNSimulator(@Nullable BaseRecognizer parser, @NotNull ATN atn) { + public v2ParserATNSimulator(@Nullable Parser parser, @NotNull ATN atn) { super(atn); this.parser = parser; // ctxToDFAs = new HashMap(); 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 425875174..f7b20c985 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java @@ -29,7 +29,7 @@ package org.antlr.v4.runtime.tree; -import org.antlr.v4.runtime.BaseRecognizer; +import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.tree.gui.TreePostScriptGenerator; @@ -44,7 +44,7 @@ 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, Parser recog, String fontName, int fontSize) { TreePostScriptGenerator psgen = @@ -52,11 +52,11 @@ public class Trees { return psgen.getPS(); } - public static String getPS(Tree t, BaseRecognizer recog) { + public static String getPS(Tree t, Parser recog) { return getPS(t, recog, "Helvetica", 11); } - public static void writePS(Tree t, BaseRecognizer recog, + public static void writePS(Tree t, Parser recog, String fileName, String fontName, int fontSize) throws IOException @@ -68,7 +68,7 @@ public class Trees { bw.close(); } - public static void writePS(Tree t, BaseRecognizer recog, String fileName) + public static void writePS(Tree t, Parser recog, String fileName) throws IOException { writePS(t, recog, fileName, "Helvetica", 11); @@ -78,7 +78,7 @@ 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, Parser recog) { if ( t.getChildCount()==0 ) return getNodeText(t, recog); StringBuilder buf = new StringBuilder(); buf.append("("); @@ -92,7 +92,7 @@ public class Trees { return buf.toString(); } - public static String getNodeText(Tree t, BaseRecognizer recog) { + public static String getNodeText(Tree t, Parser recog) { if ( recog!=null ) { if ( t instanceof ParseTree.RuleNode ) { int ruleIndex = ((ParseTree.RuleNode)t).getRuleContext().getRuleIndex(); diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.java index 94dd525fb..9c77719e3 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.java @@ -34,7 +34,7 @@ import org.abego.treelayout.NodeExtentProvider; import org.abego.treelayout.TreeForTreeLayout; import org.abego.treelayout.TreeLayout; import org.abego.treelayout.util.DefaultConfiguration; -import org.antlr.v4.runtime.BaseRecognizer; +import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.Tree; @@ -71,11 +71,11 @@ public class TreePostScriptGenerator { protected PostScriptDocument doc; - public TreePostScriptGenerator(BaseRecognizer parser, Tree root) { + public TreePostScriptGenerator(Parser parser, Tree root) { this(parser, root, "CourierNew", 11); } - public TreePostScriptGenerator(BaseRecognizer parser, Tree root, + public TreePostScriptGenerator(Parser parser, Tree root, String fontName, int fontSize) { this.root = root; 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 c5a1d7f12..a7dc0644a 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 @@ -33,7 +33,7 @@ import org.abego.treelayout.NodeExtentProvider; import org.abego.treelayout.TreeForTreeLayout; import org.abego.treelayout.TreeLayout; import org.abego.treelayout.util.DefaultConfiguration; -import org.antlr.v4.runtime.BaseRecognizer; +import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.Tree; import org.antlr.v4.runtime.tree.Trees; @@ -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; + Parser parser; - public DefaultTreeTextProvider(BaseRecognizer parser) { + public DefaultTreeTextProvider(Parser 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 Parser parser; - public TreeViewer(BaseRecognizer parser, Tree tree) { + public TreeViewer(Parser parser, Tree tree) { this.parser = parser; setTreeTextProvider(new DefaultTreeTextProvider(parser)); boolean useIdentity = true; // compare node identity diff --git a/tool/test/org/antlr/v4/test/TestPerformance.java b/tool/test/org/antlr/v4/test/TestPerformance.java index 52fd274e3..8867bc0ad 100644 --- a/tool/test/org/antlr/v4/test/TestPerformance.java +++ b/tool/test/org/antlr/v4/test/TestPerformance.java @@ -38,12 +38,12 @@ public class TestPerformance extends BaseTest { private static final boolean RUN_PARSER = true; /** True to use {@link BailErrorStrategy}, False to use {@link DefaultErrorStrategy} */ private static final boolean BAIL_ON_ERROR = false; - /** This value is passed to {@link BaseRecognizer#setBuildParseTree}. */ + /** This value is passed to {@link org.antlr.v4.runtime.Parser#setBuildParseTree}. */ private static final boolean BUILD_PARSE_TREES = false; /** * Use ParseTreeWalker.DEFAULT.walk with the BlankJavaParserListener to show parse tree walking overhead. * If {@link #BUILD_PARSE_TREES} is false, the listener will instead be called during the parsing process via - * {@link BaseRecognizer#setListener}. + * {@link org.antlr.v4.runtime.Parser#setListener}. */ private static final boolean BLANK_LISTENER = false;