org.antlr.v4.runtime.atn.ATNDeserializer Java Examples

The following examples show how to use org.antlr.v4.runtime.atn.ATNDeserializer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GrammarParserInterpreter.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** 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;
}
 
Example #2
Source File: Grammar.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public LexerInterpreter createLexerInterpreter(CharStream input) {
	if (this.isParser()) {
		throw new IllegalStateException("A lexer interpreter can only be created for a lexer or combined grammar.");
	}

	if (this.isCombined()) {
		return implicitLexer.createLexerInterpreter(input);
	}

	char[] serializedAtn = ATNSerializer.getSerializedAsChars(atn);
	ATN deserialized = new ATNDeserializer().deserialize(serializedAtn);
	return new LexerInterpreter(fileName, getVocabulary(), Arrays.asList(getRuleNames()), ((LexerGrammar)this).modes.keySet(), deserialized, input);
}
 
Example #3
Source File: Grammar.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** @since 4.5.1 */
public GrammarParserInterpreter createGrammarParserInterpreter(TokenStream tokenStream) {
	if (this.isLexer()) {
		throw new IllegalStateException("A parser interpreter can only be created for a parser or combined grammar.");
	}
	char[] serializedAtn = ATNSerializer.getSerializedAsChars(atn);
	ATN deserialized = new ATNDeserializer().deserialize(serializedAtn);
	return new GrammarParserInterpreter(this, deserialized, tokenStream);
}
 
Example #4
Source File: Grammar.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ParserInterpreter createParserInterpreter(TokenStream tokenStream) {
	if (this.isLexer()) {
		throw new IllegalStateException("A parser interpreter can only be created for a parser or combined grammar.");
	}

	char[] serializedAtn = ATNSerializer.getSerializedAsChars(atn);
	ATN deserialized = new ATNDeserializer().deserialize(serializedAtn);
	return new ParserInterpreter(fileName, getVocabulary(), Arrays.asList(getRuleNames()), deserialized, tokenStream);
}
 
Example #5
Source File: PreviewParser.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public PreviewParser(Grammar g, TokenStream input) {
	this(g, new ATNDeserializer().deserialize(ATNSerializer.getSerializedAsChars(g.getATN())), input);
}