private String toStringTree(@NotNull final Tree t, @Nullable final List<String> ruleNames) { if (t.getChildCount() == 0) { return Utils.escapeWhitespace(getNodeText(t, ruleNames), true); } StringBuilder buf = new StringBuilder(); buf.append(" ( "); String s = Utils.escapeWhitespace(getNodeText(t, ruleNames), true); buf.append(s); buf.append(' '); for (int i = 0; i < t.getChildCount(); i++) { if (i > 0) { buf.append(' '); } buf.append(toStringTree(t.getChild(i), ruleNames)); } buf.append(" ) "); return buf.toString(); }
private String getNodeText(@NotNull final Tree t, @Nullable final List<String> ruleNames) { if (ruleNames != null) { if (t instanceof RuleNode) { int ruleIndex = ((RuleNode) t).getRuleContext().getRuleIndex(); String ruleName = ruleNames.get(ruleIndex); return ruleName; } else if (t instanceof ErrorNode) { return "<" + t.toString() + ">"; } else if (t instanceof TerminalNode) { Token symbol = ((TerminalNode) t).getSymbol(); if (symbol != null) { String s = symbol.getText(); return "'" + s + "'"; } } } // no recog for rule names Object payload = t.getPayload(); if (payload instanceof Token) { return ((Token) payload).getText(); } return t.getPayload().toString(); }
public String toString(@Nullable List<String> ruleNames, @Nullable RuleContext stop) { StringBuilder buf = new StringBuilder(); RuleContext p = this; buf.append("["); while (p != null && p != stop) { if (ruleNames == null) { if (!p.isEmpty()) { buf.append(p.invokingState); } } else { int ruleIndex = p.getRuleIndex(); String ruleName = ruleIndex >= 0 && ruleIndex < ruleNames.size() ? ruleNames.get(ruleIndex) : Integer.toString(ruleIndex); buf.append(ruleName); } if (p.parent != null && (ruleNames != null || !p.parent.isEmpty())) { buf.append(" "); } p = p.parent; } buf.append("]"); return buf.toString(); }
public FailedPredicateException(@NotNull Parser recognizer, @Nullable String predicate, @Nullable String message) { super(formatMessage(predicate, message), recognizer, recognizer.getInputStream(), recognizer._ctx); ATNState s = recognizer.getInterpreter().atn.states.get(recognizer.getState()); AbstractPredicateTransition trans = (AbstractPredicateTransition)s.transition(0); if (trans instanceof PredicateTransition) { this.ruleIndex = ((PredicateTransition)trans).ruleIndex; this.predicateIndex = ((PredicateTransition)trans).predIndex; } else { this.ruleIndex = 0; this.predicateIndex = 0; } this.predicate = predicate; this.setOffendingToken(recognizer.getCurrentToken()); }
/** Print out a whole tree in LISP form. {@link #getNodeText} is used on the * node payloads to get the text for the nodes. Detect * parse trees and extract data appropriately. */ public static String toStringTree(@NotNull Tree t, @Nullable List<String> ruleNames) { String s = Utils.escapeWhitespace(getNodeText(t, ruleNames), false); if ( t.getChildCount()==0 ) return s; StringBuilder buf = new StringBuilder(); buf.append("("); s = Utils.escapeWhitespace(getNodeText(t, ruleNames), false); buf.append(s); buf.append(' '); for (int i = 0; i<t.getChildCount(); i++) { if ( i>0 ) buf.append(' '); buf.append(toStringTree(t.getChild(i), ruleNames)); } buf.append(")"); return buf.toString(); }
public static String getNodeText(@NotNull Tree t, @Nullable List<String> ruleNames) { if ( ruleNames!=null ) { if ( t instanceof RuleNode ) { int ruleIndex = ((RuleNode)t).getRuleContext().getRuleIndex(); String ruleName = ruleNames.get(ruleIndex); return ruleName; } else if ( t instanceof ErrorNode) { return t.toString(); } else if ( t instanceof TerminalNode) { Token symbol = ((TerminalNode)t).getSymbol(); if (symbol != null) { String s = symbol.getText(); return s; } } } // no recog for rule names Object payload = t.getPayload(); if ( payload instanceof Token ) { return ((Token)payload).getText(); } return t.getPayload().toString(); }
/** * Constructs a new instance of {@link ParseTreeMatch} from the specified * parse tree and pattern. * * @param tree The parse tree to match against the pattern. * @param pattern The parse tree pattern. * @param labels A mapping from label names to collections of * {@link ParseTree} objects located by the tree pattern matching process. * @param mismatchedNode The first node which failed to match the tree * pattern during the matching process. * * @exception IllegalArgumentException if {@code tree} is {@code null} * @exception IllegalArgumentException if {@code pattern} is {@code null} * @exception IllegalArgumentException if {@code labels} is {@code null} */ public ParseTreeMatch(@NotNull ParseTree tree, @NotNull ParseTreePattern pattern, @NotNull MultiMap<String, ParseTree> labels, @Nullable ParseTree mismatchedNode) { if (tree == null) { throw new IllegalArgumentException("tree cannot be null"); } if (pattern == null) { throw new IllegalArgumentException("pattern cannot be null"); } if (labels == null) { throw new IllegalArgumentException("labels cannot be null"); } this.tree = tree; this.pattern = pattern; this.labels = labels; this.mismatchedNode = mismatchedNode; }
@NotNull protected ATNConfigSet computeStartState(@NotNull ATNState p, @Nullable RuleContext ctx, boolean fullCtx) { // always at least the implicit call to start rule PredictionContext initialContext = PredictionContext.fromRuleContext(atn, ctx); ATNConfigSet configs = new ATNConfigSet(fullCtx); for (int i=0; i<p.getNumberOfTransitions(); i++) { ATNState target = p.transition(i).target; ATNConfig c = new ATNConfig(target, i+1, initialContext); Set<ATNConfig> closureBusy = new HashSet<ATNConfig>(); closure(c, configs, closureBusy, true, fullCtx, false); } return configs; }
/** * Calculates the SLL(1) expected lookahead set for each outgoing transition * of an {@link ATNState}. The returned array has one element for each * outgoing transition in {@code s}. If the closure from transition * <em>i</em> leads to a semantic predicate before matching a symbol, the * element at index <em>i</em> of the result will be {@code null}. * * @param s the ATN state * @return the expected symbols for each outgoing transition of {@code s}. */ @Nullable public IntervalSet[] getDecisionLookahead(@Nullable ATNState s) { // System.out.println("LOOK("+s.stateNumber+")"); if ( s==null ) { return null; } IntervalSet[] look = new IntervalSet[s.getNumberOfTransitions()]; for (int alt = 0; alt < s.getNumberOfTransitions(); alt++) { look[alt] = new IntervalSet(); Set<ATNConfig> lookBusy = new HashSet<ATNConfig>(); boolean seeThruPreds = false; // fail to get lookahead upon pred _LOOK(s.transition(alt).target, null, PredictionContext.EMPTY, look[alt], lookBusy, new BitSet(), seeThruPreds, false); // Wipe out lookahead for this alternative if we found nothing // or we had a predicate when we !seeThruPreds if ( look[alt].size()==0 || look[alt].contains(HIT_PRED) ) { look[alt] = null; } } return look; }
@Override protected void reportAmbiguity(@NotNull DFA dfa, DFAState D, int startIndex, int stopIndex, boolean exact, @Nullable BitSet ambigAlts, @NotNull ATNConfigSet configs) { int prediction; if ( ambigAlts!=null ) { prediction = ambigAlts.nextSetBit(0); } else { prediction = configs.getAlts().nextSetBit(0); } if ( configs.fullCtx && prediction != conflictingAltResolvedBySLL ) { // Even though this is an ambiguity we are reporting, we can // still detect some context sensitivities. Both SLL and LL // are showing a conflict, hence an ambiguity, but if they resolve // to different minimum alternatives we have also identified a // context sensitivity. decisions[currentDecision].contextSensitivities.add( new ContextSensitivityInfo(currentDecision, configs, _input, startIndex, stopIndex) ); } decisions[currentDecision].ambiguities.add( new AmbiguityInfo(currentDecision, configs, _input, startIndex, stopIndex, configs.fullCtx) ); super.reportAmbiguity(dfa, D, startIndex, stopIndex, exact, ambigAlts, configs); }
public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) { mErrMsg = "at line " + line + ":" + charPositionInLine + " " + msg; throw e; }
@Override public void reportAmbiguity(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, boolean exact, @Nullable BitSet ambigAlts, @NotNull ATNConfigSet configs) { }
@Override public void reportAttemptingFullContext(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @Nullable BitSet conflictingAlts, @NotNull ATNConfigSet configs) { }
/** * {@inheritDoc} * <p/> * The method is overridden to throw an exception if the parsed input has an invalid syntax. */ @Override public void syntaxError(Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, int charPositionInLine, String msg, @Nullable RecognitionException e) { String errorMessage = msg; if (e instanceof InputMismatchException && msg.endsWith(" expecting VALUE")) { errorMessage += ". Please make sure that all values are surrounded by double quotes."; } throw new IllegalArgumentException(errorMessage); }
private static String getNodeText(@NotNull Tree t, @Nullable List<String> ruleNames, @Nullable List<String> tokenNames) { if (t instanceof RuleNode) { int ruleIndex = ((RuleNode) t).getRuleContext().getRuleIndex(); return ruleNames.get(ruleIndex); } if (t instanceof ErrorNode) { return t.toString(); } if (t instanceof TerminalNode) { Token symbol = ((TerminalNode) t).getSymbol(); if (symbol != null) { return tokenNames.get(symbol.getType()); } } // no recog for rule names Object payload = t.getPayload(); if (payload instanceof Token) { return ((Token) payload).getText(); } return t.getPayload().toString(); }
/** Save this tree in a postscript file */ public void save(@Nullable Parser parser, String fileName) throws IOException, PrintException { List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null; save(ruleNames, fileName); }
public static String getPS(Tree t, @Nullable List<String> ruleNames, String fontName, int fontSize) { TreePostScriptGenerator psgen = new TreePostScriptGenerator(ruleNames, t, fontName, fontSize); return psgen.getPS(); }
public static void writePS(Tree t, @Nullable List<String> ruleNames, String fileName, String fontName, int fontSize) throws IOException { String ps = getPS(t, ruleNames, fontName, fontSize); FileWriter f = new FileWriter(fileName); BufferedWriter bw = new BufferedWriter(f); try { bw.write(ps); } finally { bw.close(); } }
public TreePostScriptGenerator(@Nullable List<String> ruleNames, Tree root, String fontName, int fontSize) { this.root = root; setTreeTextProvider(new TreeViewer.DefaultTreeTextProvider(ruleNames)); doc = new PostScriptDocument(fontName, fontSize); boolean compareNodeIdentities = true; this.treeLayout = new TreeLayout<Tree>(new TreeLayoutAdaptor(root), new VariableExtentProvide(), new DefaultConfiguration<Tree>(gapBetweenLevels, gapBetweenNodes, Configuration.Location.Bottom), compareNodeIdentities); }
@NotNull private static String formatMessage(@Nullable String predicate, @Nullable String message) { if (message != null) { return message; } return String.format(Locale.getDefault(), "failed predicate: {%s}?", predicate); }
public LexerNoViableAltException(@Nullable Lexer lexer, @NotNull CharStream input, int startIndex, @Nullable ATNConfigSet deadEndConfigs) { super(lexer, input, null); this.startIndex = startIndex; this.deadEndConfigs = deadEndConfigs; }
@Override public void reportAmbiguity(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, boolean exact, @Nullable BitSet ambigAlts, @NotNull ATNConfigSet configs) { for (ANTLRErrorListener listener : delegates) { listener.reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs); } }
@Override public void reportAttemptingFullContext(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @Nullable BitSet conflictingAlts, @NotNull ATNConfigSet configs) { for (ANTLRErrorListener listener : delegates) { listener.reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs); } }
public NoViableAltException(@NotNull Parser recognizer, @NotNull TokenStream input, @NotNull Token startToken, @NotNull Token offendingToken, @Nullable ATNConfigSet deadEndConfigs, @NotNull ParserRuleContext ctx) { super(recognizer, input, ctx); this.deadEndConfigs = deadEndConfigs; this.startToken = startToken; this.setOffendingToken(offendingToken); }
@Override public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) { }
public RecognitionException(@Nullable Recognizer<?, ?> recognizer, @Nullable IntStream input, @Nullable ParserRuleContext ctx) { this.recognizer = recognizer; this.input = input; this.ctx = ctx; if ( recognizer!=null ) this.offendingState = recognizer.getState(); }
/** Save this tree in a postscript file using a particular font name and size */ public void save(@Nullable List<String> ruleNames, String fileName, String fontName, int fontSize) throws IOException { Trees.writePS(this, ruleNames, fileName, fontName, fontSize); }
/** * Builds a complete metric name, of the form prefix.resource.metric * * @param request The broker request containing all the information * @param metricName The metric name to register * @return The complete metric name */ private String buildMetricName(@Nullable BrokerRequest request, String metricName) { if (request != null && request.getQuerySource() != null && request.getQuerySource().getTableName() != null) { return _metricPrefix + getTableName(request.getQuerySource().getTableName()) + "." + metricName; } else { return _metricPrefix + "unknown." + metricName; } }
@Override public void reportAttemptingFullContext(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, @Nullable BitSet conflictingAlts, @NotNull ATNConfigSet configs) { String format = "reportAttemptingFullContext d=%s, input='%s'"; String decision = getDecisionDescription(recognizer, dfa); String text = recognizer.getTokenStream().getText(Interval.of(startIndex, stopIndex)); String message = String.format(format, decision, text); recognizer.notifyErrorListeners(message); }
/** * Computes the set of conflicting or ambiguous alternatives from a * configuration set, if that information was not already provided by the * parser. * * @param reportedAlts The set of conflicting or ambiguous alternatives, as * reported by the parser. * @param configs The conflicting or ambiguous configuration set. * @return Returns {@code reportedAlts} if it is not {@code null}, otherwise * returns the set of alternatives represented in {@code configs}. */ @NotNull protected BitSet getConflictingAlts(@Nullable BitSet reportedAlts, @NotNull ATNConfigSet configs) { if (reportedAlts != null) { return reportedAlts; } BitSet result = new BitSet(); for (ATNConfig config : configs) { result.set(config.alt); } return result; }
public RootView(@NotNull String name, @Nullable String subclass, @NotNull java.util.Set<Attribute> attributes, @NotNull List<Child> children) { view = new View(name, attributes, children); this.subclass = subclass; this.bindClass = findBindClass(); if (this.bindClass == null) { verifyNoBindings(view); } }
public ATNConfig(@NotNull ATNState state, int alt, @Nullable PredictionContext context, @NotNull SemanticContext semanticContext) { this.state = state; this.alt = alt; this.context = context; this.semanticContext = semanticContext; }
public String toString(@Nullable Recognizer<?, ?> recog, boolean showAlt) { StringBuilder buf = new StringBuilder(); // if ( state.ruleIndex>=0 ) { // if ( recog!=null ) buf.append(recog.getRuleNames()[state.ruleIndex]+":"); // else buf.append(state.ruleIndex+":"); // } buf.append('('); buf.append(state); if ( showAlt ) { buf.append(","); buf.append(alt); } if ( context!=null ) { buf.append(",["); buf.append(context.toString()); buf.append("]"); } if ( semanticContext!=null && semanticContext != SemanticContext.NONE ) { buf.append(","); buf.append(semanticContext); } if ( reachesIntoOuterContext>0 ) { buf.append(",up=").append(reachesIntoOuterContext); } buf.append(')'); return buf.toString(); }
public ParserATNSimulator(@Nullable Parser parser, @NotNull ATN atn, @NotNull DFA[] decisionToDFA, @NotNull PredictionContextCache sharedContextCache) { super(atn, sharedContextCache); this.parser = parser; this.decisionToDFA = decisionToDFA; // DOTGenerator dot = new DOTGenerator(null); // System.out.println(dot.getDOT(atn.rules.get(0), parser.getRuleNames())); // System.out.println(dot.getDOT(atn.rules.get(1), parser.getRuleNames())); }
@Nullable protected ATNState getReachableTarget(@NotNull Transition trans, int ttype) { if (trans.matches(ttype, 0, atn.maxTokenType)) { return trans.target; } return null; }
public void notifyErrorListeners(@NotNull Token offendingToken, @NotNull String msg, @Nullable RecognitionException e) { _syntaxErrors++; int line = -1; int charPositionInLine = -1; line = offendingToken.getLine(); charPositionInLine = offendingToken.getCharPositionInLine(); ANTLRErrorListener listener = getErrorListenerDispatch(); listener.syntaxError(this, offendingToken, line, charPositionInLine, msg, e); }
/** If context sensitive parsing, we know it's ambiguity not conflict */ protected void reportAmbiguity(@NotNull DFA dfa, DFAState D, int startIndex, int stopIndex, boolean exact, @Nullable BitSet ambigAlts, @NotNull ATNConfigSet configs) { if ( debug || retry_debug ) { Interval interval = Interval.of(startIndex, stopIndex); System.out.println("reportAmbiguity "+ ambigAlts+":"+configs+ ", input="+parser.getTokenStream().getText(interval)); } if ( parser!=null ) parser.getErrorListenerDispatch().reportAmbiguity(parser, dfa, startIndex, stopIndex, exact, ambigAlts, configs); }
public ATNDeserializer(@Nullable ATNDeserializationOptions deserializationOptions) { if (deserializationOptions == null) { deserializationOptions = ATNDeserializationOptions.getDefaultOptions(); } this.deserializationOptions = deserializationOptions; }
@Override protected void reportAttemptingFullContext(@NotNull DFA dfa, @Nullable BitSet conflictingAlts, @NotNull ATNConfigSet configs, int startIndex, int stopIndex) { if ( conflictingAlts!=null ) { conflictingAltResolvedBySLL = conflictingAlts.nextSetBit(0); } else { conflictingAltResolvedBySLL = configs.getAlts().nextSetBit(0); } decisions[currentDecision].LL_Fallback++; super.reportAttemptingFullContext(dfa, conflictingAlts, configs, startIndex, stopIndex); }