Java Code Examples for org.antlr.v4.runtime.atn.ATNState#addTransition()

The following examples show how to use org.antlr.v4.runtime.atn.ATNState#addTransition() . 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: LexerATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** For a lexer, a string is a sequence of char to match.  That is,
 *  "fog" is treated as 'f' 'o' 'g' not as a single transition in
 *  the DFA.  Machine== o-'f'->o-'o'->o-'g'->o and has n+1 states
 *  for n characters.
 */
@Override
public Handle stringLiteral(TerminalAST stringLiteralAST) {
	String chars = stringLiteralAST.getText();
	chars = CharSupport.getStringFromGrammarStringLiteral(chars);
	int n = chars.length();
	ATNState left = newState(stringLiteralAST);
	ATNState prev = left;
	ATNState right = null;
	for (int i=0; i<n; i++) {
		right = newState(stringLiteralAST);
		prev.addTransition(new AtomTransition(right, chars.charAt(i)));
		prev = right;
	}
	stringLiteralAST.atnState = left;
	return new Handle(left, right);
}
 
Example 2
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Handle _ruleRef(GrammarAST node) {
	Rule r = g.getRule(node.getText());
	if ( r==null ) {
		g.tool.errMgr.grammarError(ErrorType.INTERNAL_ERROR, g.fileName, node.getToken(), "Rule "+node.getText()+" undefined");
		return null;
	}
	RuleStartState start = atn.ruleToStartState[r.index];
	ATNState left = newState(node);
	ATNState right = newState(node);
	int precedence = 0;
	if (((GrammarASTWithOptions)node).getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME) != null) {
		precedence = Integer.parseInt(((GrammarASTWithOptions)node).getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME));
	}
	RuleTransition call = new RuleTransition(start, r.index, precedence, right);
	left.addTransition(call);

	node.atnState = left;
	return new Handle(left, right);
}
 
Example 3
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Build what amounts to an epsilon transition with a semantic
 *  predicate action.  The {@code pred} is a pointer into the AST of
 *  the {@link ANTLRParser#SEMPRED} token.
 */

@Override
public Handle sempred(PredAST pred) {
	//System.out.println("sempred: "+ pred);
	ATNState left = newState(pred);
	ATNState right = newState(pred);

	AbstractPredicateTransition p;
	if (pred.getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME) != null) {
		int precedence = Integer.parseInt(pred.getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME));
		p = new PrecedencePredicateTransition(right, precedence);
	}
	else {
		boolean isCtxDependent = UseDefAnalyzer.actionIsContextDependent(pred);
		p = new PredicateTransition(right, currentRule.index, g.sempreds.get(pred), isCtxDependent);
	}

	left.addTransition(p);
	pred.atnState = left;
	return new Handle(left, right);
}
 
Example 4
Source File: LexerATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected Handle action(GrammarAST node, LexerAction lexerAction) {
	ATNState left = newState(node);
	ATNState right = newState(node);
	boolean isCtxDependent = false;
	int lexerActionIndex = getLexerActionIndex(lexerAction);
	ActionTransition a =
		new ActionTransition(right, currentRule.index, lexerActionIndex, isCtxDependent);
	left.addTransition(a);
	node.atnState = left;
	Handle h = new Handle(left, right);
	return h;
}
 
Example 5
Source File: LexerATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Handle range(GrammarAST a, GrammarAST b) {
	ATNState left = newState(a);
	ATNState right = newState(b);
	int t1 = CharSupport.getCharValueFromGrammarCharLiteral(a.getText());
	int t2 = CharSupport.getCharValueFromGrammarCharLiteral(b.getText());
	left.addTransition(new  RangeTransition(right, t1, t2));
	a.atnState = left;
	b.atnState = left;
	return new Handle(left, right);
}
 
Example 6
Source File: LexerATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** [Aa\t \u1234a-z\]\-] char sets */
@Override
public Handle charSetLiteral(GrammarAST charSetAST) {
	ATNState left = newState(charSetAST);
	ATNState right = newState(charSetAST);
	IntervalSet set = getSetFromCharSetLiteral(charSetAST);
	left.addTransition(new SetTransition(right, set));
	charSetAST.atnState = left;
	return new Handle(left, right);
}
 
Example 7
Source File: LexerATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Handle tokenRef(TerminalAST node) {
	// Ref to EOF in lexer yields char transition on -1
	if ( node.getText().equals("EOF") ) {
		ATNState left = newState(node);
		ATNState right = newState(node);
		left.addTransition(new AtomTransition(right, IntStream.EOF));
		return new Handle(left, right);
	}
	return _ruleRef(node);
}
 
Example 8
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** From label {@code A} build graph {@code o-A->o}. */

	@Override
	public Handle tokenRef(TerminalAST node) {
		ATNState left = newState(node);
		ATNState right = newState(node);
		int ttype = g.getTokenType(node.getText());
		left.addTransition(new AtomTransition(right, ttype));
		node.atnState = left;
		return new Handle(left, right);
	}
 
Example 9
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Build an atom with all possible values in its label. */

	@Override
	public Handle wildcard(GrammarAST node) {
		ATNState left = newState(node);
		ATNState right = newState(node);
		left.addTransition(new WildcardTransition(right));
		node.atnState = left;
		return new Handle(left, right);
	}
 
Example 10
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Add an EOF transition to any rule end ATNState that points to nothing
    *  (i.e., for all those rules not invoked by another rule).  These
    *  are start symbols then.
 *
 *  Return the number of grammar entry points; i.e., how many rules are
 *  not invoked by another rule (they can only be invoked from outside).
 *  These are the start rules.
    */
public int addEOFTransitionToStartRules() {
	int n = 0;
	ATNState eofTarget = newState(null); // one unique EOF target for all rules
	for (Rule r : g.rules.values()) {
		ATNState stop = atn.ruleToStopState[r.index];
		if ( stop.getNumberOfTransitions()>0 ) continue;
		n++;
		Transition t = new AtomTransition(eofTarget, Token.EOF);
		stop.addTransition(t);
	}
	return n;
}
 
Example 11
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected void epsilon(ATNState a, ATNState b, boolean prepend) {
	if ( a!=null ) {
		int index = prepend ? 0 : a.getNumberOfTransitions();
		a.addTransition(index, new EpsilonTransition(b));
	}
}