Java Code Examples for org.antlr.v4.tool.ast.GrammarAST#getChildCount()

The following examples show how to use org.antlr.v4.tool.ast.GrammarAST#getChildCount() . 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: BasicSemanticChecks.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
protected void exitMode(GrammarAST tree) {
	if (nonFragmentRuleCount == 0) {
		Token token = tree.getToken();
		String name = "?";
		if (tree.getChildCount() > 0) {
			name = tree.getChild(0).getText();
			if (name == null || name.isEmpty()) {
				name = "?";
			}

			token = ((GrammarAST)tree.getChild(0)).getToken();
		}

		g.tool.errMgr.grammarError(ErrorType.MODE_WITHOUT_RULES, g.fileName, token, name, g);
	}
}
 
Example 2
Source File: Tool.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Manually get option node from tree; return null if no defined. */
public static GrammarAST findOptionValueAST(GrammarRootAST root, String option) {
	GrammarAST options = (GrammarAST)root.getFirstChildWithType(ANTLRParser.OPTIONS);
	if ( options!=null && options.getChildCount() > 0 ) {
		for (Object o : options.getChildren()) {
			GrammarAST c = (GrammarAST)o;
			if ( c.getType() == ANTLRParser.ASSIGN &&
				 c.getChild(0).getText().equals(option) )
			{
				return (GrammarAST)c.getChild(1);
			}
		}
	}
	return null;
}
 
Example 3
Source File: Grammar.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right))))
 *  set option assoc=right in TOKEN_REF.
 */
public static void setNodeOptions(GrammarAST node, GrammarAST options) {
	if ( options==null ) return;
	GrammarASTWithOptions t = (GrammarASTWithOptions)node;
	if ( t.getChildCount()==0 || options.getChildCount()==0 ) return;
	for (Object o : options.getChildren()) {
		GrammarAST c = (GrammarAST)o;
		if ( c.getType()==ANTLRParser.ASSIGN ) {
			t.setOption(c.getChild(0).getText(), (GrammarAST)c.getChild(1));
		}
		else {
			t.setOption(c.getText(), null); // no arg such as ID<VarNodeType>
		}
	}
}
 
Example 4
Source File: RuleCollector.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void discoverLexerRule(RuleAST rule, GrammarAST ID, List<GrammarAST> modifiers,
							  GrammarAST block)
{
	int numAlts = block.getChildCount();
	Rule r = new Rule(g, ID.getText(), rule, numAlts);
	r.mode = currentModeName;
	if ( !modifiers.isEmpty() ) r.modifiers = modifiers;
	rules.put(r.name, r);
}
 
Example 5
Source File: BasicSemanticChecks.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void checkNumRules(GrammarAST rulesNode) {
	if ( rulesNode.getChildCount()==0 ) {
		GrammarAST root = (GrammarAST)rulesNode.getParent();
		GrammarAST IDNode = (GrammarAST)root.getChild(0);
		g.tool.errMgr.grammarError(ErrorType.NO_RULES, g.fileName,
				null, IDNode.getText(), g);
	}
}
 
Example 6
Source File: SemanticPipeline.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
boolean hasTypeOrMoreCommand(Rule r) {
	GrammarAST ast = r.ast;
	if (ast == null) {
		return false;
	}

	GrammarAST altActionAst = (GrammarAST)ast.getFirstDescendantWithType(ANTLRParser.LEXER_ALT_ACTION);
	if (altActionAst == null) {
		// the rule isn't followed by any commands
		return false;
	}

	// first child is the alt itself, subsequent are the actions
	for (int i = 1; i < altActionAst.getChildCount(); i++) {
		GrammarAST node = (GrammarAST)altActionAst.getChild(i);
		if (node.getType() == ANTLRParser.LEXER_ACTION_CALL) {
			if ("type".equals(node.getChild(0).getText())) {
				return true;
			}
		}
		else if ("more".equals(node.getText())) {
			return true;
		}
	}

	return false;
}
 
Example 7
Source File: RuleCollector.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void discoverRule(RuleAST rule, GrammarAST ID,
						 List<GrammarAST> modifiers, ActionAST arg,
						 ActionAST returns, GrammarAST thrws,
						 GrammarAST options, ActionAST locals,
						 List<GrammarAST> actions,
						 GrammarAST block)
{
	int numAlts = block.getChildCount();
	Rule r;
	if ( LeftRecursiveRuleAnalyzer.hasImmediateRecursiveRuleRefs(rule, ID.getText()) ) {
		r = new LeftRecursiveRule(g, ID.getText(), rule);
	}
	else {
		r = new Rule(g, ID.getText(), rule, numAlts);
	}
	rules.put(r.name, r);

	if ( arg!=null ) {
		r.args = ScopeParser.parseTypedArgList(arg, arg.getText(), g);
		r.args.type = AttributeDict.DictType.ARG;
		r.args.ast = arg;
		arg.resolver = r.alt[currentOuterAltNumber];
	}

	if ( returns!=null ) {
		r.retvals = ScopeParser.parseTypedArgList(returns, returns.getText(), g);
		r.retvals.type = AttributeDict.DictType.RET;
		r.retvals.ast = returns;
	}

	if ( locals!=null ) {
		r.locals = ScopeParser.parseTypedArgList(locals, locals.getText(), g);
		r.locals.type = AttributeDict.DictType.LOCAL;
		r.locals.ast = locals;
	}

	for (GrammarAST a : actions) {
		// a = ^(AT ID ACTION)
		ActionAST action = (ActionAST) a.getChild(1);
		r.namedActions.put(a.getChild(0).getText(), action);
		action.resolver = r;
	}
}