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

The following examples show how to use org.antlr.v4.tool.Grammar#getRule() . 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: SymbolChecks.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void checkRuleArgs(Grammar g, List<GrammarAST> rulerefs) {
	if ( rulerefs==null ) return;
	for (GrammarAST ref : rulerefs) {
		String ruleName = ref.getText();
		Rule r = g.getRule(ruleName);
		GrammarAST arg = (GrammarAST)ref.getFirstChildWithType(ANTLRParser.ARG_ACTION);
		if ( arg!=null && (r==null || r.args==null) ) {
			errMgr.grammarError(ErrorType.RULE_HAS_NO_ARGS,
									  g.fileName, ref.token, ruleName);

		}
		else if ( arg==null && (r!=null&&r.args!=null) ) {
			errMgr.grammarError(ErrorType.MISSING_RULE_ARGS,
									  g.fileName, ref.token, ruleName);
		}
	}
}
 
Example 2
Source File: SymbolChecks.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void checkForQualifiedRuleIssues(Grammar g, List<GrammarAST> qualifiedRuleRefs) {
	for (GrammarAST dot : qualifiedRuleRefs) {
		GrammarAST grammar = (GrammarAST)dot.getChild(0);
		GrammarAST rule = (GrammarAST)dot.getChild(1);
           g.tool.log("semantics", grammar.getText()+"."+rule.getText());
		Grammar delegate = g.getImportedGrammar(grammar.getText());
		if ( delegate==null ) {
			errMgr.grammarError(ErrorType.NO_SUCH_GRAMMAR_SCOPE,
									  g.fileName, grammar.token, grammar.getText(),
									  rule.getText());
		}
		else {
			if ( g.getRule(grammar.getText(), rule.getText())==null ) {
				errMgr.grammarError(ErrorType.NO_SUCH_RULE_IN_SCOPE,
										  g.fileName, rule.token, grammar.getText(),
										  rule.getText());
			}
		}
	}
}
 
Example 3
Source File: ProfilerPanel.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static String getSemanticContextDisplayString(PredicateEvalInfo pred,
                                                     PreviewState previewState,
                                                     SemanticContext semctx,
                                                     int alt,
                                                     boolean result) {
	Grammar g = previewState.g;
	String semanticContextDisplayString = g.getSemanticContextDisplayString(semctx);
	if ( semctx instanceof SemanticContext.PrecedencePredicate ) {
		int ruleIndex = previewState.parsingResult.parser.getATN().decisionToState.get(pred.decision).ruleIndex;
		Rule rule = g.getRule(ruleIndex);
		int precedence = ((SemanticContext.PrecedencePredicate) semctx).precedence;
		// precedence = n - originalAlt + 1, So:
		int originalAlt = rule.getOriginalNumberOfAlts()-precedence+1;
		alt = originalAlt;
	}
	return semanticContextDisplayString+" => alt "+alt+" is "+result;
}
 
Example 4
Source File: ParsingUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static ParsingResult parseText(Grammar g,
										  LexerGrammar lg,
										  String startRuleName,
										  final VirtualFile grammarFile,
										  SyntaxErrorListener syntaxErrorListener,
										  TokenStream tokens,
										  int startIndex) {
		if ( g==null || lg==null ) {
			ANTLRv4PluginController.LOG.info("parseText can't parse: missing lexer or parser no Grammar object for " +
											 (grammarFile != null ? grammarFile.getName() : "<unknown file>"));
			return null;
		}

		String grammarFileName = g.fileName;
		if (!new File(grammarFileName).exists()) {
			ANTLRv4PluginController.LOG.info("parseText grammar doesn't exist "+grammarFileName);
			return null;
		}

		if ( g==BAD_PARSER_GRAMMAR || lg==BAD_LEXER_GRAMMAR ) {
			return null;
		}

		tokens.seek(startIndex);

		PreviewParser parser = new PreviewParser(g, tokens);
		parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
		parser.setProfile(true);

		parser.removeErrorListeners();
		parser.addErrorListener(syntaxErrorListener);

		Rule start = g.getRule(startRuleName);
		if ( start==null ) {
			return null; // can't find start rule
		}
//		System.out.println("parse test ----------------------------");
		ParseTree t = parser.parse(start.index);

		if ( t!=null ) {
			return new ParsingResult(parser, t, syntaxErrorListener);
		}
		return null;
	}