Search in sources :

Example 1 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project presto by prestodb.

the class TypeCalculation method calculateLiteralValue.

public static Long calculateLiteralValue(String calculation, Map<String, Long> inputs) {
    try {
        ParserRuleContext tree = parseTypeCalculation(calculation);
        CalculateTypeVisitor visitor = new CalculateTypeVisitor(inputs);
        BigInteger result = visitor.visit(tree);
        return result.longValueExact();
    } catch (StackOverflowError e) {
        throw new ParsingException("Type calculation is too large (stack overflow while parsing)");
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParsingException(com.facebook.presto.sql.parser.ParsingException) BigInteger(java.math.BigInteger)

Example 2 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class LL1Analyzer method getDecisionLookahead.

/**
	 * 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}.
	 */
public IntervalSet[] getDecisionLookahead(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>();
        // fail to get lookahead upon pred
        boolean seeThruPreds = false;
        _LOOK(s.transition(alt).target, null, PredictionContext.EMPTY, look[alt], lookBusy, new BitSet(), seeThruPreds, false);
        // or we had a predicate when we !seeThruPreds
        if (look[alt].size() == 0 || look[alt].contains(HIT_PRED)) {
            look[alt] = null;
        }
    }
    return look;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) BitSet(java.util.BitSet) HashSet(java.util.HashSet)

Example 3 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class LL1Analyzer method LOOK.

/**
	 * Compute set of tokens that can follow {@code s} in the ATN in the
	 * specified {@code ctx}.
	 *
	 * <p>If {@code ctx} is {@code null} and the end of the rule containing
	 * {@code s} is reached, {@link Token#EPSILON} is added to the result set.
	 * If {@code ctx} is not {@code null} and the end of the outermost rule is
	 * reached, {@link Token#EOF} is added to the result set.</p>
	 *
	 * @param s the ATN state
	 * @param stopState the ATN state to stop at. This can be a
	 * {@link BlockEndState} to detect epsilon paths through a closure.
	 * @param ctx the complete parser context, or {@code null} if the context
	 * should be ignored
	 *
	 * @return The set of tokens that can follow {@code s} in the ATN in the
	 * specified {@code ctx}.
	 */
public IntervalSet LOOK(ATNState s, ATNState stopState, RuleContext ctx) {
    IntervalSet r = new IntervalSet();
    // ignore preds; get all lookahead
    boolean seeThruPreds = true;
    PredictionContext lookContext = ctx != null ? PredictionContext.fromRuleContext(s.atn, ctx) : null;
    _LOOK(s, stopState, lookContext, r, new HashSet<ATNConfig>(), new BitSet(), seeThruPreds, true);
    return r;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) BitSet(java.util.BitSet)

Example 4 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class ParseTreePattern method findAll.

/**
	 * Find all nodes using XPath and then try to match those subtrees against
	 * this tree pattern.
	 *
	 * @param tree The {@link ParseTree} to match against this pattern.
	 * @param xpath An expression matching the nodes
	 *
	 * @return A collection of {@link ParseTreeMatch} objects describing the
	 * successful matches. Unsuccessful matches are omitted from the result,
	 * regardless of the reason for the failure.
	 */
public List<ParseTreeMatch> findAll(ParseTree tree, String xpath) {
    Collection<ParseTree> subtrees = XPath.findAll(tree, xpath, matcher.getParser());
    List<ParseTreeMatch> matches = new ArrayList<ParseTreeMatch>();
    for (ParseTree t : subtrees) {
        ParseTreeMatch match = match(t);
        if (match.succeeded()) {
            matches.add(match);
        }
    }
    return matches;
}
Also used : ArrayList(java.util.ArrayList) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 5 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class Parser method getATNWithBypassAlts.

/**
	 * The ATN with bypass alternatives is expensive to create so we create it
	 * lazily.
	 *
	 * @throws UnsupportedOperationException if the current parser does not
	 * implement the {@link #getSerializedATN()} method.
	 */
public ATN getATNWithBypassAlts() {
    String serializedAtn = getSerializedATN();
    if (serializedAtn == null) {
        throw new UnsupportedOperationException("The current parser does not support an ATN with bypass alternatives.");
    }
    synchronized (bypassAltsAtnCache) {
        ATN result = bypassAltsAtnCache.get(serializedAtn);
        if (result == null) {
            ATNDeserializationOptions deserializationOptions = new ATNDeserializationOptions();
            deserializationOptions.setGenerateRuleBypassTransitions(true);
            result = new ATNDeserializer(deserializationOptions).deserialize(serializedAtn.toCharArray());
            bypassAltsAtnCache.put(serializedAtn, result);
        }
        return result;
    }
}
Also used : ATNDeserializer(org.antlr.v4.runtime.atn.ATNDeserializer) ATNDeserializationOptions(org.antlr.v4.runtime.atn.ATNDeserializationOptions) ATN(org.antlr.v4.runtime.atn.ATN)

Aggregations

Test (org.junit.Test)131 LexerGrammar (org.antlr.v4.tool.LexerGrammar)78 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)52 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)49 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)44 ATN (org.antlr.v4.runtime.atn.ATN)44 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)38 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)38 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)38 ParseTree (org.antlr.v4.runtime.tree.ParseTree)29 ATNState (org.antlr.v4.runtime.atn.ATNState)11 Grammar (org.antlr.v4.tool.Grammar)10 STGroupString (org.stringtemplate.v4.STGroupString)10 ByteBuffer (java.nio.ByteBuffer)8 IntBuffer (java.nio.IntBuffer)8 UTF8CodePointDecoder (org.antlr.v4.runtime.UTF8CodePointDecoder)8 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)8 DOTGenerator (org.antlr.v4.tool.DOTGenerator)8 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 Rule (org.antlr.v4.tool.Rule)7