org.antlr.v4.runtime.Recognizer Java Examples

The following examples show how to use org.antlr.v4.runtime.Recognizer. 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: DistkvSqlErrorListener.java    From distkv with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
                        int line, int charPositionInLine, String msg, RecognitionException e) {

  String sourceName = recognizer.getInputStream().getSourceName();
  if (!sourceName.isEmpty()) {
    sourceName = String.format("%s:%d:%d: ", sourceName, line, charPositionInLine);
  }
  // TODO(qwang): This exception should be refined.
  if (recognizer.getState() == 128) {
    throw new DistkvException("X010",
          sourceName + "line " + line + ":" + charPositionInLine + " " + msg);
  } else {
    throw new DistkvException("X020",
          sourceName + "line " + line + ":" + charPositionInLine + " " + msg);
  }
}
 
Example #2
Source File: QueryTest.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void simpleCommentsWork() throws Exception {
    String queryString = "select * where { ?s ?p ?o }#?s ?p ?o }";
    String expectedQuery = "select * where { ?s ?p ?o }";
    Sparql11Parser parser = Query.getParser(queryString);
    TokenStream tokens = parser.getTokenStream();
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e);
        }
    });

    parser.query();
    assertEquals(expectedQuery, tokens.getText());
}
 
Example #3
Source File: Tool.java    From bookish with MIT License 6 votes vote down vote up
public String translateString(Translator trans, String markdown, String startRule) throws Exception {
	CharStream input = CharStreams.fromString(markdown);
	BookishLexer lexer = new BookishLexer(input);
	CommonTokenStream tokens = new CommonTokenStream(lexer);
	BookishParser parser = new BookishParser(tokens,null, 0);
	parser.removeErrorListeners();
	parser.addErrorListener(new ConsoleErrorListener() {
		@Override
		public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
			msg = "Parsing author string: "+msg;
			super.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
		}
	});
	Method startMethod = BookishParser.class.getMethod(startRule, (Class[])null);
	ParseTree doctree = (ParseTree)startMethod.invoke(parser, (Object[])null);

	OutputModelObject omo = trans.visit(doctree); // get single chapter

	ModelConverter converter = new ModelConverter(trans.templates);
	ST outputST = converter.walk(omo);
	return outputST.render();
}
 
Example #4
Source File: JQLChecker.java    From kripton with Apache License 2.0 6 votes vote down vote up
/**
 * Prepare parser.
 *
 * @param jqlContext
 *            the jql context
 * @param jql
 *            the jql
 * @return the pair
 */
protected Pair<ParserRuleContext, CommonTokenStream> prepareParser(final JQLContext jqlContext, final String jql) {
	JqlLexer lexer = new JqlLexer(CharStreams.fromString(jql));
	CommonTokenStream tokens = new CommonTokenStream(lexer);
	JqlParser parser = new JqlParser(tokens);

	parser.removeErrorListeners();
	parser.addErrorListener(new JQLBaseErrorListener() {
		@Override
		public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
				int charPositionInLine, String msg, RecognitionException e) {
			AssertKripton.assertTrue(false,
					jqlContext.getContextDescription() + ": unespected char at pos %s of SQL '%s'",
					charPositionInLine, jql);
		}
	});

	ParserRuleContext context = parser.parse();
	return new Pair<>(context, tokens);
}
 
Example #5
Source File: CustomErrorListener.java    From systemds with Apache License 2.0 6 votes vote down vote up
/**
 * Syntax error occurred. Add the error to the list of parse issues.
 */
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
		String msg, RecognitionException e) {
	parseIssues.add(new ParseIssue(line, charPositionInLine, msg, currentFileName, ParseIssueType.SYNTAX_ERROR));
	try {
		setAtLeastOneError(true);
		// Print error messages with file name
		if (currentFileName == null)
			log.error("line " + line + ":" + charPositionInLine + " " + msg);
		else {
			String fileName = currentFileName;
			log.error(fileName + " line " + line + ":" + charPositionInLine + " " + msg);
		}
	} catch (Exception e1) {
		log.error("ERROR: while customizing error message:" + e1);
	}
}
 
Example #6
Source File: QueryTest.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void insensitiveToCase() throws Exception {
    String queryString = "select * WHERE { ?x ?Y ?z }";
    String queryNoSpaces = "select*WHERE{?x?Y?z}";
    Sparql11Parser parser = Query.getParser(queryString);
    TokenStream tokens = parser.getTokenStream();
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e);
        }
    });

    assertEquals(queryNoSpaces, parser.query().selectQuery().getText());
    assertEquals(queryString, tokens.getText());
}
 
Example #7
Source File: CustomErrorListener.java    From systemds with Apache License 2.0 6 votes vote down vote up
/**
 * Syntax error occurred. Add the error to the list of parse issues.
 */
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
		String msg, RecognitionException e) {
	parseIssues.add(new ParseIssue(line, charPositionInLine, msg, currentFileName, ParseIssueType.SYNTAX_ERROR));
	try {
		setAtLeastOneError(true);
		// Print error messages with file name
		if (currentFileName == null)
			log.error("line " + line + ":" + charPositionInLine + " " + msg);
		else {
			String fileName = currentFileName;
			log.error(fileName + " line " + line + ":" + charPositionInLine + " " + msg);
		}
	} catch (Exception e1) {
		log.error("ERROR: while customizing error message:" + e1);
	}
}
 
Example #8
Source File: LogInstruction.java    From onedev with MIT License 6 votes vote down vote up
public static InstructionContext parse(String instructionString) {
	CharStream is = CharStreams.fromString(instructionString); 
	LogInstructionLexer lexer = new LogInstructionLexer(is);
	lexer.removeErrorListeners();
	lexer.addErrorListener(new BaseErrorListener() {

		@Override
		public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
				int charPositionInLine, String msg, RecognitionException e) {
			throw new RuntimeException("Malformed log instruction");
		}
		
	});
	CommonTokenStream tokens = new CommonTokenStream(lexer);
	LogInstructionParser parser = new LogInstructionParser(tokens);
	parser.removeErrorListeners();
	parser.setErrorHandler(new BailErrorStrategy());
	return parser.instruction();
}
 
Example #9
Source File: CQLErrorListener.java    From PoseidonX with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
    String msg, RecognitionException e)
{
    if (parserException != null)
    {
        return;
    }
    
    String errorSymbol = getOffendingSymbol(recognizer, offendingSymbol, charPositionInLine);
    parserException =
        new ParseException(
            ErrorCode.SEMANTICANALYZE_PARSE_ERROR,
            "You have an error in your CQL syntax; check the manual that corresponds to your Streaming version for the right syntax to use near '"
                + errorSymbol + "' at line " + line + ":" + charPositionInLine);
    LOG.error(parserException.getMessage(), parserException);
}
 
Example #10
Source File: QueryTest.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void replaceDatasetClause() throws Exception {
    InputStream query = getClass().getResourceAsStream("/example2.rq");
    Sparql11Parser parser = Query.getParser(streamToString(query));
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e);
        }
    });

    Sparql11Parser.QueryContext queryContext = parser.query();

    // Rewrite the dataset clause
    TokenStreamRewriter rewriter = new TokenStreamRewriter(parser.getTokenStream());
    ParseTreeWalker walker = new ParseTreeWalker();
    DatasetListener listener = new DatasetListener(rewriter);
    walker.walk(listener, queryContext);

    // Test result
    String newText = rewriter.getText();
    parser = Query.getParser(newText);
    String datasetText = parser.query().selectQuery().datasetClause().get(0).getText();
    assertEquals(DATASET_REPLACEMENT, datasetText);
}
 
Example #11
Source File: CQLErrorListener.java    From PoseidonX with Apache License 2.0 5 votes vote down vote up
/**
 * 获取错误单词总入口
 */
private String getOffendingSymbol(Recognizer<?, ?> recognizer, Object offendingSymbol, int charPositionInLine)
{
    if (null != offendingSymbol)
    {
        return getOffendingSymbolWithHint(recognizer, offendingSymbol);
    }
    
    String inputSentence = recognizer.getInputStream().toString();
    return Strings.isNullOrEmpty(inputSentence) ? "" : getOffendingSymbolWithoutHint(inputSentence, charPositionInLine);
}
 
Example #12
Source File: BatfishParserErrorListener.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public void syntaxError(
    Recognizer<?, ?> recognizer,
    Object offendingSymbol,
    int line,
    int charPositionInLine,
    String msg,
    RecognitionException e) {
  BatfishParser parser = _combinedParser.getParser();
  syntaxError(parser.getContext(), offendingSymbol, line, charPositionInLine, msg);
}
 
Example #13
Source File: GyroErrorListener.java    From gyro with Apache License 2.0 5 votes vote down vote up
@Override
public void syntaxError(
    Recognizer<?, ?> recognizer,
    Object symbol,
    int line,
    int column,
    String message,
    RecognitionException error) {

    syntaxErrors.add(symbol instanceof Token
        ? new SyntaxError(stream, message, (Token) symbol)
        : new SyntaxError(stream, message, line - 1, column));
}
 
Example #14
Source File: CQLErrorListener.java    From PoseidonX with Apache License 2.0 5 votes vote down vote up
/**
 * 在语法解析器可以定位到错误单词的基础下获取错误单词
 */
private String getOffendingSymbolWithHint(Recognizer<?, ?> recognizer, Object offendingSymbol)
{
    Token token = (Token)offendingSymbol;
    String tokenText = token.getText();
    if (tokenText.equals(SYMBOL_EOF))
    {
        List<Token> allTokens = ((org.antlr.v4.runtime.CommonTokenStream)recognizer.getInputStream()).getTokens();
        int tokensCount = allTokens.size();
        return (tokensCount < MIN_SIZE_FOR_TOKENS) ? "" : allTokens.get(tokensCount - MIN_SIZE_FOR_TOKENS)
            .getText();
    }
    return tokenText;
}
 
Example #15
Source File: QueryTest.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void parseSimpleQuery() throws Exception {
    InputStream query = getClass().getResourceAsStream("/example1.rq");
    Sparql11Parser parser = Query.getParser(streamToString(query));
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e);
        }
    });
    parser.query();
}
 
Example #16
Source File: DocCommentParserTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
        int line, int charPositionInLine, String msg,
        RecognitionException e) throws ParseCancellationException
{
    throw new ParseCancellationException(msg);
}
 
Example #17
Source File: SiddhiErrorListener.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
                        int line, int charPositionInLine,
                        String msg, RecognitionException e) {

    throw new SiddhiParserException("Syntax error in SiddhiQL, " + msg + ".", new int[]{line, 0},
            new int[]{line, charPositionInLine});
}
 
Example #18
Source File: StringTemplate.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static StringTemplate fromString(String pattern) {
    class DescriptiveErrorListener extends BaseErrorListener {
        public List<String> errors = new ArrayList<>();

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
                                int line, int charPositionInLine,
                                String msg, RecognitionException e) {
            errors.add(String.format("%d:%d: %s", line, charPositionInLine, msg));
        }
    }

    final DescriptiveErrorListener error_listener = new DescriptiveErrorListener();

    final StringSubstitutionLexer lexer = new StringSubstitutionLexer(CharStreams.fromString(pattern));
    lexer.removeErrorListeners();
    lexer.addErrorListener(error_listener);
    final StringSubstitutionParser parser = new StringSubstitutionParser(new UnbufferedTokenStream(lexer));
    parser.removeErrorListeners();
    parser.addErrorListener(error_listener);

    parser.setErrorHandler(new BailErrorStrategy());
    final StringSubstitutionParser.ExprContext result = parser.expr();
    if (result.exception != null)
        throw new IllegalArgumentException("errors during parsing: " + pattern, result.exception);
    else if (!error_listener.errors.isEmpty())
        throw new IllegalArgumentException("syntax errors during parsing:\n" + String.join("\n", error_listener.errors.stream().map(s -> "  " + s).collect(Collectors.toList())));
    return result.s;
}
 
Example #19
Source File: ErrorListener.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public void syntaxError(
    Recognizer<?, ?> recognizer,
    Object offendingSymbol,
    int line,
    int position,
    String message,
    RecognitionException e) throws GWQLParseException {
  throw new GWQLParseException(line, position, message.replace(" K_", " "));
}
 
Example #20
Source File: JsErrorManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void syntaxError(
        final Recognizer<?, ?> recognizer,
        final Object offendingSymbol,
        final int line,
        final int charPositionInLine,
        final String msg,
        final RecognitionException e) {
    if (recognizer instanceof Parser) {
        //Recognizer can be either Parser or Lexer
        List<String> stack = ((Parser) recognizer).getRuleInvocationStack();
        Collections.reverse(stack);
    }
    addParserError(new AntlrParserError(msg, line, charPositionInLine, offendingSymbol));
}
 
Example #21
Source File: SchemaExprParser.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                        int charPositionInLine, String msg, RecognitionException e) {
  StringBuilder builder = new StringBuilder();
  builder.append("Line [").append(line).append("]");
  builder.append(", position [").append(charPositionInLine).append("]");
  if (offendingSymbol != null) {
    builder.append(", offending symbol ").append(offendingSymbol);
  }
  if (msg != null) {
    builder.append(": ").append(msg);
  }
  throw new SchemaParsingException(builder.toString());
}
 
Example #22
Source File: ScriptDecisionTableErrorListener.java    From urule with Apache License 2.0 5 votes vote down vote up
@Override
public void syntaxError(Recognizer<?, ?> recognizer,Object offendingSymbol, 
		int line, int charPositionInLine,
		String msg, RecognitionException e) {
	if(sb==null){
		sb=new StringBuffer();
	}
	sb.append("["+offendingSymbol+"] is invalid:"+msg);
	sb.append("\r\n");
}
 
Example #23
Source File: SyntaxErrorListener.java    From urule with Apache License 2.0 5 votes vote down vote up
@Override
public void syntaxError(Recognizer<?, ?> recognizer,
		Object offendingSymbol, int line, int charPositionInLine,
		String msg, RecognitionException e) {
	this.reportor.addError(line, charPositionInLine, offendingSymbol, msg);
	//super.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
}
 
Example #24
Source File: ParserErrorListener.java    From karate with MIT License 5 votes vote down vote up
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int position, String message, RecognitionException e) {
    logger.error("syntax error: {}", message);
    this.message = message;
    this.line = line;
    this.position = position;
    this.offendingSymbol = offendingSymbol;
}
 
Example #25
Source File: YangErrorListener.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@SuppressWarnings("checkstyle:parameterName")
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line,
        final int charPositionInLine, final String msg, final RecognitionException e) {
    LOG.debug("Syntax error in {} at {}:{}: {}", source, line, charPositionInLine, msg, e);
    exceptions.add(new YangSyntaxErrorException(source, line, charPositionInLine, msg, e));
}
 
Example #26
Source File: AntlrConditionParser.java    From styx with Apache License 2.0 5 votes vote down vote up
@Override
public Condition parse(String condition) {
    ConditionParser parser = new ConditionParser(
            new CommonTokenStream(new ConditionLexer(new ANTLRInputStream(condition))));
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            throw new DslSyntaxError(line, charPositionInLine, msg, e);
        }
    });

    return new AntlrCondition(expressionVisitor.visit(parser.expression()));
}
 
Example #27
Source File: CollectdTags.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Map<String, Any2<String, Number>> parse(String pattern) {
    class DescriptiveErrorListener extends BaseErrorListener {
        public List<String> errors = new ArrayList<>();

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
                                int line, int charPositionInLine,
                                String msg, RecognitionException e) {
            errors.add(String.format("%d:%d: %s", line, charPositionInLine, msg));
        }
    }

    final DescriptiveErrorListener error_listener = new DescriptiveErrorListener();

    final CollectdTagsLexer lexer = new CollectdTagsLexer(new ANTLRInputStream(pattern));
    lexer.removeErrorListeners();
    lexer.addErrorListener(error_listener);
    final CollectdTagsParser parser = new CollectdTagsParser(new UnbufferedTokenStream(lexer));
    parser.removeErrorListeners();
    parser.addErrorListener(error_listener);

    parser.setErrorHandler(new BailErrorStrategy());
    final CollectdTagsParser.ExprContext result = parser.expr();
    if (result.exception != null)
        throw new IllegalArgumentException("errors during parsing: " + pattern, result.exception);
    else if (!error_listener.errors.isEmpty())
        throw new IllegalArgumentException("syntax errors during parsing:\n" + String.join("\n", error_listener.errors.stream().map(s -> "  " + s).collect(Collectors.toList())));
    return result.result;
}
 
Example #28
Source File: ExpressionErrorListener.java    From ureport with Apache License 2.0 5 votes vote down vote up
@Override
public void syntaxError(Recognizer<?, ?> recognizer,Object offendingSymbol, 
		int line, int charPositionInLine,
		String msg, RecognitionException e) {
	if(sb==null){
		sb=new StringBuffer();
	}
	sb.append("["+offendingSymbol+"] is invalid:"+msg);
	sb.append("\r\n");
}
 
Example #29
Source File: ParserSupport.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
                        int line, int charPositionInLine,
                        String msg, org.antlr.v4.runtime.RecognitionException e) {
    LOG.log(Level.INFO, "Configuration error: {0}:{1} -> {2}", new Object[]{line, charPositionInLine, msg});
    errors.add(String.format("%d:%d: %s", line, charPositionInLine, msg));
}
 
Example #30
Source File: MatcherAction.java    From yauaa with Apache License 2.0 5 votes vote down vote up
@Override
public void syntaxError(
        Recognizer<?, ?> recognizer,
        Object offendingSymbol,
        int line,
        int charPositionInLine,
        String msg,
        RecognitionException e) {
    LOG.error("Syntax error");
    LOG.error("Source {}: {}", matcher.getMatcherSourceLocation(), matchExpression);
    LOG.error("Message: {}", msg);
    throw new InvalidParserConfigurationException("Syntax error \"" + msg + "\" caused by \"" + matchExpression + "\".");
}