Search in sources :

Example 1 with ATNDeserializer

use of org.antlr.v4.runtime.atn.ATNDeserializer 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)

Example 2 with ATNDeserializer

use of org.antlr.v4.runtime.atn.ATNDeserializer in project antlr4 by antlr.

the class GrammarParserInterpreter method deriveTempParserInterpreter.

/** Derive a new parser from an old one that has knowledge of the grammar.
	 *  The Grammar object is used to correctly compute outer alternative
	 *  numbers for parse tree nodes. A parser of the same type is created
	 *  for subclasses of {@link ParserInterpreter}.
	 */
public static ParserInterpreter deriveTempParserInterpreter(Grammar g, Parser originalParser, TokenStream tokens) {
    ParserInterpreter parser;
    if (originalParser instanceof ParserInterpreter) {
        Class<? extends ParserInterpreter> c = originalParser.getClass().asSubclass(ParserInterpreter.class);
        try {
            Constructor<? extends ParserInterpreter> ctor = c.getConstructor(Grammar.class, ATN.class, TokenStream.class);
            parser = ctor.newInstance(g, originalParser.getATN(), originalParser.getTokenStream());
        } catch (Exception e) {
            throw new IllegalArgumentException("can't create parser to match incoming " + originalParser.getClass().getSimpleName(), e);
        }
    } else {
        // must've been a generated parser
        char[] serializedAtn = ATNSerializer.getSerializedAsChars(originalParser.getATN());
        ATN deserialized = new ATNDeserializer().deserialize(serializedAtn);
        parser = new ParserInterpreter(originalParser.getGrammarFileName(), originalParser.getVocabulary(), Arrays.asList(originalParser.getRuleNames()), deserialized, tokens);
    }
    parser.setInputStream(tokens);
    // Make sure that we don't get any error messages from using this temporary parser
    parser.setErrorHandler(new BailErrorStrategy());
    parser.removeErrorListeners();
    parser.removeParseListeners();
    parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
    return parser;
}
Also used : ATNDeserializer(org.antlr.v4.runtime.atn.ATNDeserializer) ParserInterpreter(org.antlr.v4.runtime.ParserInterpreter) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) ATN(org.antlr.v4.runtime.atn.ATN) InputMismatchException(org.antlr.v4.runtime.InputMismatchException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 3 with ATNDeserializer

use of org.antlr.v4.runtime.atn.ATNDeserializer in project antlr4 by antlr.

the class BaseCppTest method createATN.

protected ATN createATN(Grammar g, boolean useSerializer) {
    if (g.atn == null) {
        semanticProcess(g);
        assertEquals(0, g.tool.getNumErrors());
        ParserATNFactory f;
        if (g.isLexer()) {
            f = new LexerATNFactory((LexerGrammar) g);
        } else {
            f = new ParserATNFactory(g);
        }
        g.atn = f.createATN();
        assertEquals(0, g.tool.getNumErrors());
    }
    ATN atn = g.atn;
    if (useSerializer) {
        char[] serialized = ATNSerializer.getSerializedAsChars(atn);
        return new ATNDeserializer().deserialize(serialized);
    }
    return atn;
}
Also used : ATNDeserializer(org.antlr.v4.runtime.atn.ATNDeserializer) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory)

Example 4 with ATNDeserializer

use of org.antlr.v4.runtime.atn.ATNDeserializer in project antlr4 by antlr.

the class BaseGoTest method createATN.

protected ATN createATN(Grammar g, boolean useSerializer) {
    if (g.atn == null) {
        semanticProcess(g);
        assertEquals(0, g.tool.getNumErrors());
        ParserATNFactory f;
        if (g.isLexer()) {
            f = new LexerATNFactory((LexerGrammar) g);
        } else {
            f = new ParserATNFactory(g);
        }
        g.atn = f.createATN();
        assertEquals(0, g.tool.getNumErrors());
    }
    ATN atn = g.atn;
    if (useSerializer) {
        char[] serialized = ATNSerializer.getSerializedAsChars(atn);
        return new ATNDeserializer().deserialize(serialized);
    }
    return atn;
}
Also used : ATNDeserializer(org.antlr.v4.runtime.atn.ATNDeserializer) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory)

Example 5 with ATNDeserializer

use of org.antlr.v4.runtime.atn.ATNDeserializer in project antlr4 by antlr.

the class TestATNDeserialization method checkDeserializationIsStable.

protected void checkDeserializationIsStable(Grammar g) {
    ATN atn = createATN(g, false);
    char[] data = Utils.toCharArray(ATNSerializer.getSerialized(atn));
    String atnData = ATNSerializer.getDecoded(atn, Arrays.asList(g.getTokenNames()));
    ATN atn2 = new ATNDeserializer().deserialize(data);
    String atn2Data = ATNSerializer.getDecoded(atn2, Arrays.asList(g.getTokenNames()));
    assertEquals(atnData, atn2Data);
}
Also used : ATNDeserializer(org.antlr.v4.runtime.atn.ATNDeserializer) ATN(org.antlr.v4.runtime.atn.ATN)

Aggregations

ATN (org.antlr.v4.runtime.atn.ATN)12 ATNDeserializer (org.antlr.v4.runtime.atn.ATNDeserializer)12 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)6 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)6 LexerGrammar (org.antlr.v4.tool.LexerGrammar)6 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)2 ArrayList (java.util.ArrayList)1 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)1 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)1 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 ATNDeserializationOptions (org.antlr.v4.runtime.atn.ATNDeserializationOptions)1