Java Code Examples for org.antlr.v4.tool.Grammar#isTokenName()

The following examples show how to use org.antlr.v4.tool.Grammar#isTokenName() . 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
void checkInvalidRuleDef(Token ruleID) {
	String fileName = null;
	if ( ruleID.getInputStream()!=null ) {
		fileName = ruleID.getInputStream().getSourceName();
	}
	if ( g.isLexer() && Character.isLowerCase(ruleID.getText().charAt(0)) ) {
		g.tool.errMgr.grammarError(ErrorType.PARSER_RULES_NOT_ALLOWED,
								   fileName, ruleID, ruleID.getText());
	}
	if ( g.isParser() &&
		Grammar.isTokenName(ruleID.getText()) )
	{
		g.tool.errMgr.grammarError(ErrorType.LEXER_RULES_NOT_ALLOWED,
								   fileName, ruleID, ruleID.getText());
	}
}
 
Example 2
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static TerminalNode getRuleDefNameNode(Parser parser, ParseTree tree, String ruleName) {
	Collection<ParseTree> ruleDefRuleNodes;
	if ( Grammar.isTokenName(ruleName) ) {
		ruleDefRuleNodes = XPath.findAll(tree, "//lexerRule/TOKEN_REF", parser);
	}
	else {
		ruleDefRuleNodes = XPath.findAll(tree, "//parserRuleSpec/RULE_REF", parser);
	}
	for (ParseTree node : ruleDefRuleNodes) {
		String r = node.getText(); // always a TerminalNode; just get rule name of this def
		if ( r.equals(ruleName) ) {
			return (TerminalNode)node;
		}
	}
	return null;
}
 
Example 3
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static List<TerminalNode> getAllRuleRefNodes(Parser parser, ParseTree tree, String ruleName) {
	List<TerminalNode> nodes = new ArrayList<TerminalNode>();
	Collection<ParseTree> ruleRefs;
	if ( Grammar.isTokenName(ruleName) ) {
		ruleRefs = XPath.findAll(tree, "//lexerRuleBlock//TOKEN_REF", parser);
	}
	else {
		ruleRefs = XPath.findAll(tree, "//ruleBlock//RULE_REF", parser);
	}
	for (ParseTree node : ruleRefs) {
		TerminalNode terminal = (TerminalNode)node;
		Token rrefToken = terminal.getSymbol();
		String r = rrefToken.getText();
		if ( r.equals(ruleName) ) {
			nodes.add(terminal);
		}
	}
	if ( nodes.size()==0 ) return null;
	return nodes;
}
 
Example 4
Source File: BasicSemanticChecks.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void checkTokenDefinition(Token tokenID) {
	String fileName = tokenID.getInputStream().getSourceName();
	if ( !Grammar.isTokenName(tokenID.getText()) ) {
		g.tool.errMgr.grammarError(ErrorType.TOKEN_NAMES_MUST_START_UPPER,
								   fileName,
								   tokenID,
								   tokenID.getText());
	}
}
 
Example 5
Source File: RuleAST.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public boolean isLexerRule() {
	String name = getRuleName();
	return name!=null && Grammar.isTokenName(name);
}
 
Example 6
Source File: SemanticPipeline.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
void assignLexerTokenTypes(Grammar g, List<GrammarAST> tokensDefs) {
	Grammar G = g.getOutermostGrammar(); // put in root, even if imported
	for (GrammarAST def : tokensDefs) {
		// tokens { id (',' id)* } so must check IDs not TOKEN_REF
		if ( Grammar.isTokenName(def.getText()) ) {
			G.defineTokenName(def.getText());
		}
	}

	/* Define token types for nonfragment rules which do not include a 'type(...)'
	 * or 'more' lexer command.
	 */
	for (Rule r : g.rules.values()) {
		if ( !r.isFragment() && !hasTypeOrMoreCommand(r) ) {
			G.defineTokenName(r.name);
		}
	}

	// FOR ALL X : 'xxx'; RULES, DEFINE 'xxx' AS TYPE X
	List<Pair<GrammarAST,GrammarAST>> litAliases =
		Grammar.getStringLiteralAliasesFromLexerRules(g.ast);
	Set<String> conflictingLiterals = new HashSet<String>();
	if ( litAliases!=null ) {
		for (Pair<GrammarAST,GrammarAST> pair : litAliases) {
			GrammarAST nameAST = pair.a;
			GrammarAST litAST = pair.b;
			if ( !G.stringLiteralToTypeMap.containsKey(litAST.getText()) ) {
				G.defineTokenAlias(nameAST.getText(), litAST.getText());
			}
			else {
				// oops two literal defs in two rules (within or across modes).
				conflictingLiterals.add(litAST.getText());
			}
		}
		for (String lit : conflictingLiterals) {
			// Remove literal if repeated across rules so it's not
			// found by parser grammar.
			Integer value = G.stringLiteralToTypeMap.remove(lit);
			if (value != null && value > 0 && value < G.typeToStringLiteralList.size() && lit.equals(G.typeToStringLiteralList.get(value))) {
				G.typeToStringLiteralList.set(value, null);
			}
		}
	}

}