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

The following examples show how to use org.antlr.v4.runtime.atn.ATN. 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: AtnManager.java    From groovy with Apache License 2.0 6 votes vote down vote up
public ATN checkAndClear() {
    if (!shouldClearDfaCache()) {
        return atn;
    }

    if (0 != counter.incrementAndGet() % DFA_CACHE_THRESHOLD) {
        return atn;
    }

    WRITE_LOCK.lock();
    try {
        atn.clearDFA();
    } finally {
        WRITE_LOCK.unlock();
    }

    return atn;
}
 
Example #2
Source File: ATNOptimizer.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static void optimizeStates(ATN atn) {
//		System.out.println(atn.states);
		List<ATNState> compressed = new ArrayList<ATNState>();
		int i = 0; // new state number
		for (ATNState s : atn.states) {
			if ( s!=null ) {
				compressed.add(s);
				s.stateNumber = i; // reset state number as we shift to new position
				i++;
			}
		}
//		System.out.println(compressed);
//		System.out.println("ATN optimizer removed " + (atn.states.size() - compressed.size()) + " null states.");
		atn.states.clear();
		atn.states.addAll(compressed);
	}
 
Example #3
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public ATN createATN() {
	_createATN(g.rules.values());
	assert atn.maxTokenType == g.getMaxTokenType();
       addRuleFollowLinks();
	addEOFTransitionToStartRules();
	ATNOptimizer.optimize(g, atn);

	for (Triple<Rule, ATNState, ATNState> pair : preventEpsilonClosureBlocks) {
		LL1Analyzer analyzer = new LL1Analyzer(atn);
		if (analyzer.LOOK(pair.b, pair.c, null).contains(org.antlr.v4.runtime.Token.EPSILON)) {
			ErrorType errorType = pair.a instanceof LeftRecursiveRule ? ErrorType.EPSILON_LR_FOLLOW : ErrorType.EPSILON_CLOSURE;
			g.tool.errMgr.grammarError(errorType, g.fileName, ((GrammarAST)pair.a.ast.getChild(0)).getToken(), pair.a.name);
		}
	}

	optionalCheck:
	for (Triple<Rule, ATNState, ATNState> pair : preventEpsilonOptionalBlocks) {
		int bypassCount = 0;
		for (int i = 0; i < pair.b.getNumberOfTransitions(); i++) {
			ATNState startState = pair.b.transition(i).target;
			if (startState == pair.c) {
				bypassCount++;
				continue;
			}

			LL1Analyzer analyzer = new LL1Analyzer(atn);
			if (analyzer.LOOK(startState, pair.c, null).contains(org.antlr.v4.runtime.Token.EPSILON)) {
				g.tool.errMgr.grammarError(ErrorType.EPSILON_OPTIONAL, g.fileName, ((GrammarAST)pair.a.ast.getChild(0)).getToken(), pair.a.name);
				continue optionalCheck;
			}
		}

		if (bypassCount != 1) {
			throw new UnsupportedOperationException("Expected optional block with exactly 1 bypass alternative.");
		}
	}

	return atn;
}
 
Example #4
Source File: SoqlParser.java    From components with Apache License 2.0 5 votes vote down vote up
public final AnywordContext anyword() throws RecognitionException {
	AnywordContext _localctx = new AnywordContext(_ctx, getState());
	enterRule(_localctx, 22, RULE_anyword);
	try {
		int _alt;
		enterOuterAlt(_localctx, 1);
		{
			setState(86);
			_errHandler.sync(this);
			_alt = 1;
			do {
				switch (_alt) {
				case 1: {
					{
						setState(85);
						match(ANYCHAR);
					}
				}
					break;
				default:
					throw new NoViableAltException(this);
				}
				setState(88);
				_errHandler.sync(this);
				_alt = getInterpreter().adaptivePredict(_input, 8, _ctx);
			} while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
		}
	} catch (RecognitionException re) {
		_localctx.exception = re;
		_errHandler.reportError(this, re);
		_errHandler.recover(this, re);
	} finally {
		exitRule();
	}
	return _localctx;
}
 
Example #5
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 #6
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 #7
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 #8
Source File: SoqlParser.java    From components with Apache License 2.0 5 votes vote down vote up
public final ObjectPrefixContext objectPrefix() throws RecognitionException {
	ObjectPrefixContext _localctx = new ObjectPrefixContext(_ctx, getState());
	enterRule(_localctx, 16, RULE_objectPrefix);
	try {
		int _alt;
		enterOuterAlt(_localctx, 1);
		{
			setState(73);
			_errHandler.sync(this);
			_alt = 1;
			do {
				switch (_alt) {
				case 1: {
					{
						setState(71);
						match(NAME);
						setState(72);
						match(DOT);
					}
				}
					break;
				default:
					throw new NoViableAltException(this);
				}
				setState(75);
				_errHandler.sync(this);
				_alt = getInterpreter().adaptivePredict(_input, 7, _ctx);
			} while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
		}
	} catch (RecognitionException re) {
		_localctx.exception = re;
		_errHandler.reportError(this, re);
		_errHandler.recover(this, re);
	} finally {
		exitRule();
	}
	return _localctx;
}
 
Example #9
Source File: SoqlParser.java    From components with Apache License 2.0 5 votes vote down vote up
public final FieldListContext fieldList() throws RecognitionException {
	FieldListContext _localctx = new FieldListContext(_ctx, getState());
	enterRule(_localctx, 8, RULE_fieldList);
	try {
		int _alt;
		enterOuterAlt(_localctx, 1);
		{
			setState(45);
			field();
			setState(50);
			_errHandler.sync(this);
			_alt = getInterpreter().adaptivePredict(_input, 3, _ctx);
			while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
				if (_alt == 1) {
					{
						{
							setState(46);
							match(COMMA);
							setState(47);
							field();
						}
					}
				}
				setState(52);
				_errHandler.sync(this);
				_alt = getInterpreter().adaptivePredict(_input, 3, _ctx);
			}
		}
	} catch (RecognitionException re) {
		_localctx.exception = re;
		_errHandler.reportError(this, re);
		_errHandler.recover(this, re);
	} finally {
		exitRule();
	}
	return _localctx;
}
 
Example #10
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ParserATNFactory(Grammar g) {
	if (g == null) {
		throw new NullPointerException("g");
	}

	this.g = g;

	ATNType atnType = g instanceof LexerGrammar ? ATNType.LEXER : ATNType.PARSER;
	int maxTokenType = g.getMaxTokenType();
	this.atn = new ATN(atnType, maxTokenType);
}
 
Example #11
Source File: GrammarParserInterpreter.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public GrammarParserInterpreter(Grammar g,
								String grammarFileName,
								Vocabulary vocabulary,
								Collection<String> ruleNames,
								ATN atn,
								TokenStream input) {
	super(grammarFileName, vocabulary, ruleNames, atn, input);
	this.g = g;
}
 
Example #12
Source File: SerializedATN.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public SerializedATN(OutputModelFactory factory, ATN atn) {
		super(factory);
		IntegerList data = ATNSerializer.getSerialized(atn);
		serialized = new ArrayList<String>(data.size());
		for (int c : data.toArray()) {
			String encoded = factory.getGenerator().getTarget().encodeIntAsCharEscape(c == -1 ? Character.MAX_VALUE : c);
			serialized.add(encoded);
		}
//		System.out.println(ATNSerializer.getDecoded(factory.getGrammar(), atn));
	}
 
Example #13
Source File: GrammarParserInterpreter.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public GrammarParserInterpreter(Grammar g, ATN atn, TokenStream input) {
	super(g.fileName, g.getVocabulary(),
		  Arrays.asList(g.getRuleNames()),
		  atn, // must run ATN through serializer to set some state flags
		  input);
	this.g = g;
	decisionStatesThatSetOuterAltNumInContext = findOuterMostDecisionStates();
	stateToAltsMap = new int[g.atn.states.size()][];
}
 
Example #14
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 #15
Source File: AntlrATNCacheFields.java    From presto with Apache License 2.0 5 votes vote down vote up
private static DFA[] createDecisionToDFA(ATN atn)
{
    DFA[] decisionToDFA = new DFA[atn.getNumberOfDecisions()];
    for (int i = 0; i < decisionToDFA.length; i++) {
        decisionToDFA[i] = new DFA(atn.getDecisionState(i), i);
    }
    return decisionToDFA;
}
 
Example #16
Source File: Grammar.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ATN getATN() {
	if ( atn==null ) {
		ParserATNFactory factory = new ParserATNFactory(this);
		atn = factory.createATN();
	}
	return atn;
}
 
Example #17
Source File: EditorConfigParser.java    From editorconfig-netbeans with MIT License 4 votes vote down vote up
@Override
public ATN getATN() {
  return _ATN;
}
 
Example #18
Source File: PositionAdjustingLexerATNSimulator.java    From beakerx with Apache License 2.0 4 votes vote down vote up
public PositionAdjustingLexerATNSimulator(Lexer recog, ATN atn, DFA[] decisionToDFA, PredictionContextCache sharedContextCache) {
    super(recog, atn, decisionToDFA, sharedContextCache);
}
 
Example #19
Source File: XpathParser.java    From JsoupXpath with Apache License 2.0 4 votes vote down vote up
@Override
public ATN getATN() { return _ATN; }
 
Example #20
Source File: XpathLexer.java    From JsoupXpath with Apache License 2.0 4 votes vote down vote up
@Override
public ATN getATN() { return _ATN; }
 
Example #21
Source File: EditorConfigLexer.java    From editorconfig-netbeans with MIT License 4 votes vote down vote up
@Override
public ATN getATN() {
  return _ATN;
}
 
Example #22
Source File: JaybirdSqlParser.java    From jaybird with GNU Lesser General Public License v2.1 4 votes vote down vote up
public final MergeStatementContext mergeStatement() throws RecognitionException {
	MergeStatementContext _localctx = new MergeStatementContext(_ctx, getState());
	enterRule(_localctx, 10, RULE_mergeStatement);
	int _la;
	try {
		int _alt;
		enterOuterAlt(_localctx, 1);
		{
		setState(108);
		match(MERGE);
		setState(109);
		match(INTO);
		setState(110);
		tableName();
		setState(114);
		_errHandler.sync(this);
		_alt = getInterpreter().adaptivePredict(_input,14,_ctx);
		while ( _alt!=1 && _alt!= ATN.INVALID_ALT_NUMBER ) {
			if ( _alt==1+1 ) {
				{
				{
				setState(111);
				matchWildcard();
				}
				} 
			}
			setState(116);
			_errHandler.sync(this);
			_alt = getInterpreter().adaptivePredict(_input,14,_ctx);
		}
		setState(118);
		_errHandler.sync(this);
		_la = _input.LA(1);
		if (_la==RETURNING) {
			{
			setState(117);
			returningClause();
			}
		}

		setState(121);
		_errHandler.sync(this);
		_la = _input.LA(1);
		if (_la==T__0) {
			{
			setState(120);
			match(T__0);
			}
		}


		                statementModel.setStatementType(JaybirdStatementModel.MERGE_TYPE);
		            
		}
	}
	catch (RecognitionException re) {
		_localctx.exception = re;
		_errHandler.reportError(this, re);
		_errHandler.recover(this, re);
	}
	finally {
		exitRule();
	}
	return _localctx;
}
 
Example #23
Source File: AtnManager.java    From groovy with Apache License 2.0 4 votes vote down vote up
public AtnWrapper(ATN atn) {
    this.atn = atn;
}
 
Example #24
Source File: SoqlParser.java    From components with Apache License 2.0 4 votes vote down vote up
@Override
public ATN getATN() {
	return _ATN;
}
 
Example #25
Source File: XGBoostModelParser.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public ATN getATN() {
    return _ATN;
}
 
Example #26
Source File: XGBoostModelLexer.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public ATN getATN() {
    return _ATN;
}
 
Example #27
Source File: CQLCFLexer.java    From chronix.server with Apache License 2.0 4 votes vote down vote up
@Override
public ATN getATN() {
    return _ATN;
}
 
Example #28
Source File: CQLCFParser.java    From chronix.server with Apache License 2.0 4 votes vote down vote up
@Override
public ATN getATN() {
    return _ATN;
}
 
Example #29
Source File: GroovyLangLexer.java    From groovy with Apache License 2.0 4 votes vote down vote up
public PositionAdjustingLexerATNSimulator(Lexer recog, ATN atn) {
    super(recog, atn);
}
 
Example #30
Source File: LeftRecursionDetector.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public LeftRecursionDetector(Grammar g, ATN atn) {
	this.g = g;
	this.atn = atn;
}