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

The following examples show how to use org.antlr.v4.tool.ast.GrammarAST#getType() . 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
public void label(GrammarAST op, GrammarAST ID, GrammarAST element) {
	switch (element.getType()) {
	// token atoms
	case TOKEN_REF:
	case STRING_LITERAL:
	case RANGE:
	// token sets
	case SET:
	case NOT:
	// rule atoms
	case RULE_REF:
	case WILDCARD:
		return;

	default:
		String fileName = ID.token.getInputStream().getSourceName();
		g.tool.errMgr.grammarError(ErrorType.LABEL_BLOCK_NOT_A_SET, fileName, ID.token, ID.getText());
		break;
	}
}
 
Example 2
Source File: BasicSemanticChecks.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void discoverLexerRule(RuleAST rule, GrammarAST ID, List<GrammarAST> modifiers,
							  GrammarAST block)
{
	checkInvalidRuleDef(ID.token);

	if (modifiers != null) {
		for (GrammarAST tree : modifiers) {
			if (tree.getType() == ANTLRParser.FRAGMENT) {
				inFragmentRule = true;
			}
		}
	}

	if (!inFragmentRule) {
		nonFragmentRuleCount++;
	}
}
 
Example 3
Source File: ParserFactory.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Choice getComplexEBNFBlock(GrammarAST ebnfRoot, List<CodeBlockForAlt> alts) {
	int ebnf = 0;
	if ( ebnfRoot!=null ) ebnf = ebnfRoot.getType();
	Choice c = null;
	switch ( ebnf ) {
		case ANTLRParser.OPTIONAL :
			c = new OptionalBlock(this, ebnfRoot, alts);
			break;
		case ANTLRParser.CLOSURE :
			c = new StarBlock(this, ebnfRoot, alts);
			break;
		case ANTLRParser.POSITIVE_CLOSURE :
			c = new PlusBlock(this, ebnfRoot, alts);
			break;
	}
	return c;
}
 
Example 4
Source File: ParserFactory.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Choice getLL1EBNFBlock(GrammarAST ebnfRoot, List<CodeBlockForAlt> alts) {
	int ebnf = 0;
	if ( ebnfRoot!=null ) ebnf = ebnfRoot.getType();
	Choice c = null;
	switch ( ebnf ) {
		case ANTLRParser.OPTIONAL :
			if ( alts.size()==1 ) c = new LL1OptionalBlockSingleAlt(this, ebnfRoot, alts);
			else c = new LL1OptionalBlock(this, ebnfRoot, alts);
			break;
		case ANTLRParser.CLOSURE :
			if ( alts.size()==1 ) c = new LL1StarBlockSingleAlt(this, ebnfRoot, alts);
			else c = getComplexEBNFBlock(ebnfRoot, alts);
			break;
		case ANTLRParser.POSITIVE_CLOSURE :
			if ( alts.size()==1 ) c = new LL1PlusBlockSingleAlt(this, ebnfRoot, alts);
			else c = getComplexEBNFBlock(ebnfRoot, alts);
			break;
	}
	return c;
}
 
Example 5
Source File: ParserFactory.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Choice getEBNFBlock(GrammarAST ebnfRoot, List<CodeBlockForAlt> alts) {
	if (!g.tool.force_atn) {
		int decision;
		if ( ebnfRoot.getType()==ANTLRParser.POSITIVE_CLOSURE ) {
			decision = ((PlusLoopbackState)ebnfRoot.atnState).decision;
		}
		else if ( ebnfRoot.getType()==ANTLRParser.CLOSURE ) {
			decision = ((StarLoopEntryState)ebnfRoot.atnState).decision;
		}
		else {
			decision = ((DecisionState)ebnfRoot.atnState).decision;
		}

		if ( AnalysisPipeline.disjoint(g.decisionLOOK.get(decision)) ) {
			return getLL1EBNFBlock(ebnfRoot, alts);
		}
	}

	return getComplexEBNFBlock(ebnfRoot, alts);
}
 
Example 6
Source File: SemanticPipeline.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void assignTokenTypes(Grammar g, List<GrammarAST> tokensDefs,
					  List<GrammarAST> tokenIDs, List<GrammarAST> terminals)
{
	//Grammar G = g.getOutermostGrammar(); // put in root, even if imported

	// create token types for tokens { A, B, C } ALIASES
	for (GrammarAST alias : tokensDefs) {
		if (g.getTokenType(alias.getText()) != Token.INVALID_TYPE) {
			g.tool.errMgr.grammarError(ErrorType.TOKEN_NAME_REASSIGNMENT, g.fileName, alias.token, alias.getText());
		}

		g.defineTokenName(alias.getText());
	}

	// DEFINE TOKEN TYPES FOR TOKEN REFS LIKE ID, INT
	for (GrammarAST idAST : tokenIDs) {
		if (g.getTokenType(idAST.getText()) == Token.INVALID_TYPE) {
			g.tool.errMgr.grammarError(ErrorType.IMPLICIT_TOKEN_DEFINITION, g.fileName, idAST.token, idAST.getText());
		}

		g.defineTokenName(idAST.getText());
	}

	// VERIFY TOKEN TYPES FOR STRING LITERAL REFS LIKE 'while', ';'
	for (GrammarAST termAST : terminals) {
		if (termAST.getType() != ANTLRParser.STRING_LITERAL) {
			continue;
		}

		if (g.getTokenType(termAST.getText()) == Token.INVALID_TYPE) {
			g.tool.errMgr.grammarError(ErrorType.IMPLICIT_STRING_DEFINITION, g.fileName, termAST.token, termAST.getText());
		}
	}

	g.tool.log("semantics", "tokens="+g.tokenNameToTypeMap);
       g.tool.log("semantics", "strings="+g.stringLiteralToTypeMap);
}
 
Example 7
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 8
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 9
Source File: ElementFrequenciesVisitor.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void exitLexerSubrule(GrammarAST tree) {
	if (tree.getType() == CLOSURE || tree.getType() == POSITIVE_CLOSURE) {
		for (Map.Entry<String, MutableInt> entry : frequencies.peek().entrySet()) {
			entry.getValue().v = 2;
		}
	}
}
 
Example 10
Source File: ElementFrequenciesVisitor.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void exitSubrule(GrammarAST tree) {
	if (tree.getType() == CLOSURE || tree.getType() == POSITIVE_CLOSURE) {
		for (Map.Entry<String, MutableInt> entry : frequencies.peek().entrySet()) {
			entry.getValue().v = 2;
		}
	}
}
 
Example 11
Source File: RuleFunction.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public List<Decl> getDeclForAltElement(GrammarAST t, String refLabelName, boolean needList) {
	List<Decl> decls = new ArrayList<Decl>();
	if ( t.getType()==RULE_REF ) {
		Rule rref = factory.getGrammar().getRule(t.getText());
		String ctxName = factory.getGenerator().getTarget()
						 .getRuleFunctionContextStructName(rref);
		if ( needList) {
			if(factory.getGenerator().getTarget().supportsOverloadedMethods())
				decls.add( new ContextRuleListGetterDecl(factory, refLabelName, ctxName) );
			decls.add( new ContextRuleListIndexedGetterDecl(factory, refLabelName, ctxName) );
		}
		else {
			decls.add( new ContextRuleGetterDecl(factory, refLabelName, ctxName) );
		}
	}
	else {
		if ( needList ) {
			if(factory.getGenerator().getTarget().supportsOverloadedMethods())
				decls.add( new ContextTokenListGetterDecl(factory, refLabelName) );
			decls.add( new ContextTokenListIndexedGetterDecl(factory, refLabelName) );
		}
		else {
			decls.add( new ContextTokenGetterDecl(factory, refLabelName) );
		}
	}
	return decls;
}
 
Example 12
Source File: LeftRecursiveRuleFunction.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public LeftRecursiveRuleFunction(OutputModelFactory factory, LeftRecursiveRule r) {
	super(factory, r);

	CodeGenerator gen = factory.getGenerator();
	// Since we delete x=lr, we have to manually add decls for all labels
	// on left-recur refs to proper structs
	for (Pair<GrammarAST,String> pair : r.leftRecursiveRuleRefLabels) {
		GrammarAST idAST = pair.a;
		String altLabel = pair.b;
		String label = idAST.getText();
		GrammarAST rrefAST = (GrammarAST)idAST.getParent().getChild(1);
		if ( rrefAST.getType() == ANTLRParser.RULE_REF ) {
			Rule targetRule = factory.getGrammar().getRule(rrefAST.getText());
			String ctxName = gen.getTarget().getRuleFunctionContextStructName(targetRule);
			RuleContextDecl d;
			if (idAST.getParent().getType() == ANTLRParser.ASSIGN) {
				d = new RuleContextDecl(factory, label, ctxName);
			}
			else {
				d = new RuleContextListDecl(factory, label, ctxName);
			}

			StructDecl struct = ruleCtx;
			if ( altLabelCtxs!=null ) {
				StructDecl s = altLabelCtxs.get(altLabel);
				if ( s!=null ) struct = s; // if alt label, use subctx
			}
			struct.addDecl(d); // stick in overall rule's ctx
		}
	}
}
 
Example 13
Source File: Grammar.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static Map<Integer, Interval> getStateToGrammarRegionMap(GrammarRootAST ast, IntervalSet grammarTokenTypes) {
	Map<Integer, Interval> stateToGrammarRegionMap = new HashMap<Integer, Interval>();
	if ( ast==null ) return stateToGrammarRegionMap;

	List<GrammarAST> nodes = ast.getNodesWithType(grammarTokenTypes);
	for (GrammarAST n : nodes) {
		if (n.atnState != null) {
			Interval tokenRegion = Interval.of(n.getTokenStartIndex(), n.getTokenStopIndex());
			org.antlr.runtime.tree.Tree ruleNode = null;
			// RULEs, BLOCKs of transformed recursive rules point to original token interval
			switch ( n.getType() ) {
				case ANTLRParser.RULE :
					ruleNode = n;
					break;
				case ANTLRParser.BLOCK :
				case ANTLRParser.CLOSURE :
					ruleNode = n.getAncestor(ANTLRParser.RULE);
					break;
			}
			if ( ruleNode instanceof RuleAST ) {
				String ruleName = ((RuleAST) ruleNode).getRuleName();
				Rule r = ast.g.getRule(ruleName);
				if ( r instanceof LeftRecursiveRule ) {
					RuleAST originalAST = ((LeftRecursiveRule) r).getOriginalAST();
					tokenRegion = Interval.of(originalAST.getTokenStartIndex(), originalAST.getTokenStopIndex());
				}
			}
			stateToGrammarRegionMap.put(n.atnState.stateNumber, tokenRegion);
		}
	}
	return stateToGrammarRegionMap;
}
 
Example 14
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 15
Source File: SymbolCollector.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void label(GrammarAST op, GrammarAST ID, GrammarAST element) {
	LabelElementPair lp = new LabelElementPair(g, ID, element, op.getType());
	currentRule.alt[currentOuterAltNumber].labelDefs.map(ID.getText(), lp);
}
 
Example 16
Source File: LeftRecursiveRuleTransformer.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/** Return true if successful */
	public boolean translateLeftRecursiveRule(GrammarRootAST ast,
											  LeftRecursiveRule r,
											  String language)
	{
		//tool.log("grammar", ruleAST.toStringTree());
		GrammarAST prevRuleAST = r.ast;
		String ruleName = prevRuleAST.getChild(0).getText();
		LeftRecursiveRuleAnalyzer leftRecursiveRuleWalker =
			new LeftRecursiveRuleAnalyzer(prevRuleAST, tool, ruleName, language);
		boolean isLeftRec;
		try {
//			System.out.println("TESTING ---------------\n"+
//							   leftRecursiveRuleWalker.text(ruleAST));
			isLeftRec = leftRecursiveRuleWalker.rec_rule();
		}
		catch (RecognitionException re) {
			isLeftRec = false; // didn't match; oh well
		}
		if ( !isLeftRec ) return false;

		// replace old rule's AST; first create text of altered rule
		GrammarAST RULES = (GrammarAST)ast.getFirstChildWithType(ANTLRParser.RULES);
		String newRuleText = leftRecursiveRuleWalker.getArtificialOpPrecRule();
//		System.out.println("created: "+newRuleText);
		// now parse within the context of the grammar that originally created
		// the AST we are transforming. This could be an imported grammar so
		// we cannot just reference this.g because the role might come from
		// the imported grammar and not the root grammar (this.g)
		RuleAST t = parseArtificialRule(prevRuleAST.g, newRuleText);

		// reuse the name token from the original AST since it refers to the proper source location in the original grammar
		((GrammarAST)t.getChild(0)).token = ((GrammarAST)prevRuleAST.getChild(0)).getToken();

		// update grammar AST and set rule's AST.
		RULES.setChild(prevRuleAST.getChildIndex(), t);
		r.ast = t;

		// Reduce sets in newly created rule tree
		GrammarTransformPipeline transform = new GrammarTransformPipeline(g, g.tool);
		transform.reduceBlocksToSets(r.ast);
		transform.expandParameterizedLoops(r.ast);

		// Rerun semantic checks on the new rule
		RuleCollector ruleCollector = new RuleCollector(g);
		ruleCollector.visit(t, "rule");
		BasicSemanticChecks basics = new BasicSemanticChecks(g, ruleCollector);
		// disable the assoc element option checks because they are already
		// handled for the pre-transformed rule.
		basics.checkAssocElementOption = false;
		basics.visit(t, "rule");

		// track recursive alt info for codegen
		r.recPrimaryAlts = new ArrayList<LeftRecursiveRuleAltInfo>();
		r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.prefixAndOtherAlts);
		if (r.recPrimaryAlts.isEmpty()) {
			tool.errMgr.grammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST)r.ast.getChild(0)).getToken(), r.name);
		}

		r.recOpAlts = new OrderedHashMap<Integer, LeftRecursiveRuleAltInfo>();
		r.recOpAlts.putAll(leftRecursiveRuleWalker.binaryAlts);
		r.recOpAlts.putAll(leftRecursiveRuleWalker.ternaryAlts);
		r.recOpAlts.putAll(leftRecursiveRuleWalker.suffixAlts);

		// walk alt info records and set their altAST to point to appropriate ALT subtree
		// from freshly created AST
		setAltASTPointers(r, t);

		// update Rule to just one alt and add prec alt
		ActionAST arg = (ActionAST)r.ast.getFirstChildWithType(ANTLRParser.ARG_ACTION);
		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[1]; // todo: isn't this Rule or something?
		}

		// define labels on recursive rule refs we delete; they don't point to nodes of course
		// these are so $label in action translation works
		for (Pair<GrammarAST,String> pair : leftRecursiveRuleWalker.leftRecursiveRuleRefLabels) {
			GrammarAST labelNode = pair.a;
			GrammarAST labelOpNode = (GrammarAST)labelNode.getParent();
			GrammarAST elementNode = (GrammarAST)labelOpNode.getChild(1);
			LabelElementPair lp = new LabelElementPair(g, labelNode, elementNode, labelOpNode.getType());
			r.alt[1].labelDefs.map(labelNode.getText(), lp);
		}
		// copy to rule from walker
		r.leftRecursiveRuleRefLabels = leftRecursiveRuleWalker.leftRecursiveRuleRefLabels;

		tool.log("grammar", "added: "+t.toStringTree());
		return true;
	}
 
Example 17
Source File: BasicSemanticChecks.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/** Check option is appropriate for grammar, rule, subrule */
boolean checkOptions(GrammarAST parent,
					 Token optionID,
					 GrammarAST valueAST)
{
	boolean ok = true;
	if ( parent.getType()==ANTLRParser.BLOCK ) {
		if ( g.isLexer() && !Grammar.LexerBlockOptions.contains(optionID.getText()) ) { // block
			g.tool.errMgr.grammarError(ErrorType.ILLEGAL_OPTION,
									   g.fileName,
									   optionID,
									   optionID.getText());
			ok = false;
		}
		if ( !g.isLexer() && !Grammar.ParserBlockOptions.contains(optionID.getText()) ) { // block
			g.tool.errMgr.grammarError(ErrorType.ILLEGAL_OPTION,
									   g.fileName,
									   optionID,
									   optionID.getText());
			ok = false;
		}
	}
	else if ( parent.getType()==ANTLRParser.RULE ) {
		if ( !Grammar.ruleOptions.contains(optionID.getText()) ) { // rule
			g.tool.errMgr.grammarError(ErrorType.ILLEGAL_OPTION,
									   g.fileName,
									   optionID,
									   optionID.getText());
			ok = false;
		}
	}
	else if ( parent.getType()==ANTLRParser.GRAMMAR &&
			  !legalGrammarOption(optionID.getText()) ) { // grammar
		g.tool.errMgr.grammarError(ErrorType.ILLEGAL_OPTION,
								   g.fileName,
								   optionID,
								   optionID.getText());
		ok = false;
	}

	return ok;
}