org.antlr.v4.runtime.Parser Java Examples

The following examples show how to use org.antlr.v4.runtime.Parser. 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: ANTLRv4GrammarParser.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected ParseTree parse(Parser parser, IElementType root) {
	int startRule;
	if (root instanceof IFileElementType) {
		startRule = ANTLRv4Parser.RULE_grammarSpec;
	}
	else if (root == ANTLRv4TokenTypes.TOKEN_ELEMENT_TYPES.get(ANTLRv4Lexer.TOKEN_REF)
		|| root == ANTLRv4TokenTypes.TOKEN_ELEMENT_TYPES.get(ANTLRv4Lexer.RULE_REF)) {
		startRule = ANTLRv4Parser.RULE_atom;
	}
	else {
		startRule = Token.INVALID_TYPE;
	}

	switch (startRule) {
	case ANTLRv4Parser.RULE_grammarSpec:
		return ((ANTLRv4Parser) parser).grammarSpec();

	case ANTLRv4Parser.RULE_atom:
		return ((ANTLRv4Parser) parser).atom();

	default:
		String ruleName = ANTLRv4Parser.ruleNames[startRule];
		throw new UnsupportedOperationException(String.format("cannot start parsing using root element %s", root));
	}
}
 
Example #2
Source File: FWPolicyErrorStrategy.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Make sure we don't attempt to recover inline; if the parser successfully
 * recovers, it won't throw an exception.
 */
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
    InputMismatchException e = new InputMismatchException(recognizer);

    String policies = recognizer.getInputStream().getText();
    StringTokenizer tk = new StringTokenizer(policies, ";");
    String policy = "";
    int idx = 0;
    while (tk.hasMoreElements()) {
        policy = (String) tk.nextElement();
        idx += policy.length();
        if (idx >= e.getOffendingToken().getStartIndex()) {
            break;
        }
    }

    String message = Messages.get(Messages.DEFAULT_LOCALE,
            "error_invalid_firewallconfig", new Object[] {
                    e.getOffendingToken().getText(), policy });
    throw new RuntimeException(message);
}
 
Example #3
Source File: BeetlAntlrErrorStrategy.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void reportUnwantedToken(@NotNull Parser recognizer)
{
	if (inErrorRecoveryMode(recognizer))
	{
		return;
	}

	beginErrorCondition(recognizer);

	Token t = recognizer.getCurrentToken();
	String tokenName = getTokenErrorDisplay(t);
	IntervalSet expecting = getExpectedTokens(recognizer);
	String msg = "多余输入 " + tokenName + " 期望 " + expecting.toString(recognizer.getTokenNames());
	BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg);
	//		exception.token = this.getGrammarToken(t);
	exception.pushToken(this.getGrammarToken(t));
	throw exception;
}
 
Example #4
Source File: CQLErrorStrategy.java    From PoseidonX with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void reportNoViableAlternative(@NotNull Parser recognizer, @NotNull NoViableAltException e)
{
    TokenStream tokens = recognizer.getInputStream();
    String input;
    if (tokens instanceof TokenStream)
    {
        if (e.getStartToken().getType() == Token.EOF)
            input = "<EOF>";
        else
            input = getText(tokens, e.getStartToken(), e.getOffendingToken());
    }
    else
    {
        input = "<unknown input>";
    }
    String msg = "no viable alternative at input " + escapeWSAndQuote(input);
    recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}
 
Example #5
Source File: BeetlAntlrErrorStrategy.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/** Make sure we don't attempt to recover inline; if the parser
	* successfully recovers, it won't throw an exception.
	*/
	@Override
	public Token recoverInline(Parser recognizer) throws RecognitionException
	{
		// SINGLE TOKEN DELETION
		Token matchedSymbol = singleTokenDeletion(recognizer);
		if (matchedSymbol != null)
		{
			// we have deleted the extra token.
			// now, move past ttype token as if all were ok
			recognizer.consume();
			return matchedSymbol;
		}

		// SINGLE TOKEN INSERTION
		if (singleTokenInsertion(recognizer))
		{
			return getMissingSymbol(recognizer);
		}
		
//		BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR);
//		exception.pushToken(this.getGrammarToken(recognizer.getCurrentToken()));
//		throw exception;
		throw new InputMismatchException(recognizer);
	}
 
Example #6
Source File: DescriptiveErrorStrategy.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void recover(Parser recognizer, RecognitionException e) {
    for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) {
        context.exception = e;
    }

    if (PredictionMode.LL.equals(recognizer.getInterpreter().getPredictionMode())) {
        if (e instanceof NoViableAltException) {
            this.reportNoViableAlternative(recognizer, (NoViableAltException) e);
        } else if (e instanceof InputMismatchException) {
            this.reportInputMismatch(recognizer, (InputMismatchException) e);
        } else if (e instanceof FailedPredicateException) {
            this.reportFailedPredicate(recognizer, (FailedPredicateException) e);
        }
    }

    throw new ParseCancellationException(e);
}
 
Example #7
Source File: ANTLRParseTreeToPSIConverter.java    From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public ANTLRParseTreeToPSIConverter(Language language, Parser parser, PsiBuilder builder) {
	this.language = language;
	this.builder = builder;

	this.tokenElementTypes = PSIElementTypeFactory.getTokenIElementTypes(language);
	this.ruleElementTypes = PSIElementTypeFactory.getRuleIElementTypes(language);

	for (ANTLRErrorListener listener : parser.getErrorListeners()) {
		if (listener instanceof SyntaxErrorListener) {
			syntaxErrors = ((SyntaxErrorListener)listener).getSyntaxErrors();
			for (SyntaxError error : syntaxErrors) {
				// record first error per token
				int StartIndex = error.getOffendingSymbol().getStartIndex();
				if ( !tokenToErrorMap.containsKey(StartIndex) ) {
					tokenToErrorMap.put(StartIndex, error);
				}
			}
		}
	}
}
 
Example #8
Source File: GyroErrorStrategyTest.java    From gyro with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void beforeEach() {
    strategy = spy(GyroErrorStrategy.INSTANCE);
    recognizer = mock(Parser.class);
    token = mock(Token.class);
    set = mock(IntervalSet.class);
}
 
Example #9
Source File: SQLParserExecutor.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private ParseASTNode twoPhaseParse() {
    SQLParser sqlParser = SQLParserFactory.newInstance(databaseTypeName, sql);
    try {
        ((Parser) sqlParser).setErrorHandler(new BailErrorStrategy());
        ((Parser) sqlParser).getInterpreter().setPredictionMode(PredictionMode.SLL);
        return (ParseASTNode) sqlParser.parse();
    } catch (final ParseCancellationException ex) {
        ((Parser) sqlParser).reset();
        ((Parser) sqlParser).setErrorHandler(new DefaultErrorStrategy());
        ((Parser) sqlParser).getInterpreter().setPredictionMode(PredictionMode.LL);
        return (ParseASTNode) sqlParser.parse();
    }
}
 
Example #10
Source File: BeetlAntlrErrorStrategy.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void recover(Parser recognizer, RecognitionException e)
{

	//不会执行到此处,因为在report部分就抛出异常了
	super.recover(recognizer, e);
}
 
Example #11
Source File: BeetlAntlrErrorStrategy.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void reportError(Parser recognizer, RecognitionException e)
{
	// if we've already reported an error and have not matched a token
	// yet successfully, don't report any errors.
	if (inErrorRecoveryMode(recognizer))
	{
		//			System.err.print("[SPURIOUS] ");
		return; // don't report spurious errors
	}
	beginErrorCondition(recognizer);
	if (e instanceof NoViableAltException)
	{
		reportNoViableAlternative(recognizer, (NoViableAltException) e);
	}
	else if (e instanceof InputMismatchException)
	{
		reportInputMismatch(recognizer, (InputMismatchException) e);
	}
	else if (e instanceof FailedPredicateException)
	{
		reportFailedPredicate(recognizer, (FailedPredicateException) e);
	}
	else
	{
		//			System.err.println("unknown recognition error type: " + e.getClass().getName());
		BeetlException exception = new BeetlException(BeetlException.PARSER_UNKNOW_ERROR, e.getClass().getName(), e);
		//			exception.token = this.getGrammarToken(e.getOffendingToken());
		exception.pushToken(this.getGrammarToken(e.getOffendingToken()));

		throw exception;
	}
}
 
Example #12
Source File: BatfishANTLRErrorStrategy.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
protected void beginErrorCondition(Parser parser) {
  if (inErrorRecoveryMode(parser)) {
    return;
  }
  _parserStateAtRecovery = parser.getRuleContext().toString(Arrays.asList(parser.getRuleNames()));
  super.beginErrorCondition(parser);
}
 
Example #13
Source File: ErrorStrategyAdaptor.java    From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** By default ANTLR makes the start/stop -1/-1 for invalid tokens
 *  which is reasonable but here we want to highlight the
 *  current position indicating that is where we lack a token.
 *  if no input, highlight at position 0.
 */
protected Token getMissingSymbol(Parser recognizer) {
	Token missingSymbol = super.getMissingSymbol(recognizer);
	// alter the default missing symbol.
	if ( missingSymbol instanceof CommonToken) {
		int start, stop;
		Token current = recognizer.getCurrentToken();
		start = current.getStartIndex();
		stop = current.getStopIndex();
		((CommonToken) missingSymbol).setStartIndex(start);
		((CommonToken) missingSymbol).setStopIndex(stop);
	}
	return missingSymbol;
}
 
Example #14
Source File: UniquifyRuleRefs.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
	PsiElement el = MyActionUtils.getSelectedPsiElement(e);
	if ( el==null ) return;

	final String ruleName = el.getText();

	final PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
	if ( psiFile==null ) return;

	final Project project = e.getProject();

	Editor editor = e.getData(PlatformDataKeys.EDITOR);
	if ( editor==null ) return;
	final Document doc = editor.getDocument();

	String grammarText = psiFile.getText();
	ParsingResult results = ParsingUtils.parseANTLRGrammar(grammarText);
	Parser parser = results.parser;
	ParseTree tree = results.tree;

	// find all parser and lexer rule refs
	final List<TerminalNode> rrefNodes = RefactorUtils.getAllRuleRefNodes(parser, tree, ruleName);
	if ( rrefNodes==null ) return;

	// find rule def
	final TerminalNode ruleDefNameNode = RefactorUtils.getRuleDefNameNode(parser, tree, ruleName);
	if ( ruleDefNameNode==null ) return;

	// alter rule refs and dup rules
	WriteCommandAction setTextAction = new WriteCommandAction(project) {
		@Override
		protected void run(final Result result) throws Throwable {
			// do in a single action so undo works in one go
			dupRuleAndMakeRefsUnique(doc, ruleName, rrefNodes);
		}
	};
	setTextAction.execute();
}
 
Example #15
Source File: CompileErrorStrategy.java    From BigDataScript with Apache License 2.0 5 votes vote down vote up
/** Instead of recovering from pendingException {@code e}, re-throw it wrapped
 *  in a {@link ParseCancellationException} so it is not caught by the
 *  rule function catches.  Use {@link Exception#getCause()} to get the
 *  original {@link RecognitionException}.
 */
@Override
public void recover(Parser recognizer, RecognitionException e) {
	// Add a compiler error message
	String message = "Cannot parse input, near '" + e.getOffendingToken().getText() + "'";
	CompilerMessage cm = new CompilerMessage(e.getOffendingToken().getInputStream().getSourceName(), e.getOffendingToken().getLine(), -1, message, MessageType.ERROR);
	CompilerMessages.get().add(cm);

	// Add pendingException to all contexts
	for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent())
		context.exception = e;

	throw new ParseCancellationException(e);
}
 
Example #16
Source File: KsqlParserErrorStrategy.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
protected void reportUnwantedToken(Parser recognizer) {
  if (!this.inErrorRecoveryMode(recognizer)) {
    this.beginErrorCondition(recognizer);
    Token t = recognizer.getCurrentToken();
    String tokenName = this.getTokenErrorDisplay(t);
    IntervalSet expecting = this.getExpectedTokens(recognizer);
    String msg =
        "extraneous input " + tokenName + " expecting "
        + expecting.toString(recognizer.getVocabulary());
    recognizer.notifyErrorListeners(t, msg, (RecognitionException) null);
  }
}
 
Example #17
Source File: ParseTreePrettyPrinter.java    From batfish with Apache License 2.0 5 votes vote down vote up
private ParseTreePrettyPrinter(
    ParserRuleContext ctx, BatfishCombinedParser<?, ?> combinedParser, boolean printLineNumbers) {
  Parser grammar = combinedParser.getParser();
  List<String> ruleNames = Arrays.asList(grammar.getRuleNames());
  _vocabulary = grammar.getVocabulary();
  _combinedParser = combinedParser;
  _ruleNames = ruleNames;
  _ctx = ctx;
  _ptSentences = new ParseTreeSentences();
  _printLineNumbers = printLineNumbers;
  _indent = 0;
}
 
Example #18
Source File: BeetlAntlrErrorStrategy.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void reportMissingToken(@NotNull Parser recognizer)
	{
		if (inErrorRecoveryMode(recognizer))
		{
			return;
		}

		beginErrorCondition(recognizer);

//		Token t = recognizer.getCurrentToken();
		Token t = recognizer.getTokenStream().LT(-1);
		IntervalSet expecting = getExpectedTokens(recognizer);
		String expect = expecting.toString(recognizer.getTokenNames());
		if(expects.containsKey(expect)){
			expect = expects.get(expect);
		}
		if(expect.equals("'>>'")){
			expect = "'模板的占位结束符号'";
		}
		
		
		String tokenStr = getTokenErrorDisplay(t);
		String msg = null;
		if(expect.equals("'}'")&&tokenStr.equals("'>>'")) {
			 msg = "试图在第"+t.getLine()+"行未找到 '{' 匹配的结束符号 '}'";
		}else {
			//常规情况
			 msg = "缺少输入 " + expect + " 在 " + tokenStr+" 后面";
		}
		
		

		BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg);
		exception.pushToken(this.getGrammarToken(t));
		throw exception;
	}
 
Example #19
Source File: GyroErrorStrategy.java    From gyro with Apache License 2.0 5 votes vote down vote up
@Override
protected void reportUnwantedToken(Parser recognizer) {
    if (inErrorRecoveryMode(recognizer)) {
        return;
    }

    beginErrorCondition(recognizer);
    recognizer.notifyErrorListeners(recognizer.getCurrentToken(), "Extra input", null);
}
 
Example #20
Source File: FWPolicyErrorStrategy.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Instead of recovering from exception e, rethrow it wrapped in a generic
 * RuntimeException so it is not caught by the rule function catches.
 * Exception e is the "cause" of the RuntimeException.
 */

@Override
public void recover(Parser recognizer, RecognitionException e) {
    String message = Messages.get(Messages.DEFAULT_LOCALE,
            "error_invalid_firewallconfig", new Object[] {
                    e.getOffendingToken().getText(),
                    e.getInputStream().toString() });

    throw new RuntimeException(message);
}
 
Example #21
Source File: DefaultANTLRErrorListener.java    From yauaa with Apache License 2.0 5 votes vote down vote up
@Override
default void reportAmbiguity(
    Parser recognizer,
    DFA dfa,
    int startIndex,
    int stopIndex,
    boolean exact,
    BitSet ambigAlts,
    ATNConfigSet configs) {
    // Ignore this always.
}
 
Example #22
Source File: BeetlAntlrErrorStrategy.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void reportInputMismatch(@NotNull Parser recognizer, @NotNull InputMismatchException e)
{
	Token t1 = recognizer.getInputStream().LT(-1);
	String msg = "缺少输入在 " + getTokenErrorDisplay(t1) + " 后面, 期望 "
			+ e.getExpectedTokens().toString(recognizer.getTokenNames());
	BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg, e);
	//		exception.token = this.getGrammarToken(e.getOffendingToken());
	exception.pushToken(this.getGrammarToken(t1));

	throw exception;

}
 
Example #23
Source File: DefaultANTLRErrorListener.java    From yauaa with Apache License 2.0 5 votes vote down vote up
@Override
default void reportAttemptingFullContext(
    Parser recognizer,
    DFA dfa,
    int startIndex,
    int stopIndex,
    BitSet conflictingAlts,
    ATNConfigSet configs) {
    // Ignore this always.
}
 
Example #24
Source File: BailSyntaxErrorStrategy.java    From gdl with Apache License 2.0 5 votes vote down vote up
/**
 *  Make sure we don't attempt to recover inline; if the parser
 *  successfully recovers, it won't throw an exception.
 *  Again, the {@link DefaultErrorStrategy#recoverInline(Parser)} gets executed
 *  to print the wrong syntax
 */
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
  super.recoverInline(recognizer);
  InputMismatchException e = new InputMismatchException(recognizer);
  for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) {
    context.exception = e;
  }
  throw new ParseCancellationException(e);
}
 
Example #25
Source File: DefaultANTLRErrorListener.java    From yauaa with Apache License 2.0 5 votes vote down vote up
@Override
default void reportContextSensitivity(
    Parser recognizer,
    DFA dfa,
    int startIndex,
    int stopIndex,
    int prediction,
    ATNConfigSet configs) {
    // Ignore this always.
}
 
Example #26
Source File: ADLErrorListener.java    From archie with Apache License 2.0 5 votes vote down vote up
@Override
public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) {
    String input = recognizer.getInputStream().getText(new Interval(startIndex, stopIndex));
    String warning = String.format("FULL AMBIGUITY: %d-%d, exact: %b, input: %s", startIndex, stopIndex, exact, input);
    logger.debug(warning);
    errors.addWarning(warning);
}
 
Example #27
Source File: GrammarParserInterpreter.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
		public void recover(Parser recognizer, RecognitionException e) {
			int errIndex = recognizer.getInputStream().index();
			if ( firstErrorTokenIndex == -1 ) {
				firstErrorTokenIndex = errIndex; // latch
			}
//			System.err.println("recover: error at " + errIndex);
			TokenStream input = recognizer.getInputStream();
			if ( input.index()<input.size()-1 ) { // don't consume() eof
				recognizer.consume(); // just kill this bad token and let it continue.
			}
		}
 
Example #28
Source File: BeetlAntlrErrorStrategy.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void reportFailedPredicate(@NotNull Parser recognizer, @NotNull FailedPredicateException e)
{
	String ruleName = recognizer.getRuleNames()[recognizer.getContext().getRuleIndex()];
	BeetlException exception = new BeetlParserException(BeetlException.PARSER_PREDICATE_ERROR, ruleName, e);
	//		exception.token = this.getGrammarToken(e.getOffendingToken());
	exception.pushToken(this.getGrammarToken(e.getOffendingToken()));

	throw exception;
}
 
Example #29
Source File: CompileErrorStrategy.java    From BigDataScript with Apache License 2.0 5 votes vote down vote up
/** Make sure we don't attempt to recover inline; if the parser
 *  successfully recovers, it won't throw an pendingException.
 */
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
	InputMismatchException e = new InputMismatchException(recognizer);

	String message = "Cannot parse input, near '" + e.getOffendingToken().getText() + "'";
	CompilerMessage cm = new CompilerMessage(e.getOffendingToken().getInputStream().getSourceName(), e.getOffendingToken().getLine(), -1, message, MessageType.ERROR);
	CompilerMessages.get().add(cm);

	// Add pendingException to all contexts
	for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent())
		context.exception = e;

	throw new ParseCancellationException(e);
}
 
Example #30
Source File: DescriptiveErrorStrategy.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Token recoverInline(Parser recognizer)
        throws RecognitionException {

    this.recover(recognizer, new InputMismatchException(recognizer)); // stop parsing
    return null;
}