java类org.antlr.v4.runtime.BaseErrorListener的实例源码

Walker.java 文件源码 项目:elasticsearch_my 阅读 30 收藏 0 点赞 0 评论 0
private void setupPicky(PainlessParser parser) {
    // Diagnostic listener invokes syntaxError on other listeners for ambiguity issues,
    parser.addErrorListener(new DiagnosticErrorListener(true));
    // a second listener to fail the test when the above happens.
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(final Recognizer<?,?> recognizer, final Object offendingSymbol, final int line,
                                final int charPositionInLine, final String msg, final RecognitionException e) {
            throw new AssertionError("line: " + line + ", offset: " + charPositionInLine +
                ", symbol:" + offendingSymbol + " " + msg);
        }
    });

    // Enable exact ambiguity detection (costly). we enable exact since its the default for
    // DiagnosticErrorListener, life is too short to think about what 'inexact ambiguity' might mean.
    parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
}
OboParser.java 文件源码 项目:ontolib 阅读 24 收藏 0 点赞 0 评论 0
private void parseInputStream(CharStream inputStream, OboParseResultListener listener) {
  final OboLexer l = new OboLexer(inputStream);
  final Antlr4OboParser p = new Antlr4OboParser(new CommonTokenStream(l));

  p.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);
    }
  });

  if (debug) {
    p.addErrorListener(new DiagnosticErrorListener());
  }

  p.addParseListener(new OboParserListener(listener));

  p.oboFile();
}
OboParser.java 文件源码 项目:boqa 阅读 26 收藏 0 点赞 0 评论 0
private void parseInputStream(CharStream inputStream, OboParseResultListener listener) {
  final OboLexer l = new OboLexer(inputStream);
  final Antlr4OboParser p = new Antlr4OboParser(new CommonTokenStream(l));

  p.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);
    }
  });

  if (debug) {
    p.addErrorListener(new DiagnosticErrorListener());
  }

  p.addParseListener(new OboParserListener(listener));

  p.oboFile();
}
CoqFTParser.java 文件源码 项目:exterminator 阅读 23 收藏 0 点赞 0 评论 0
public static void registerErrorListener(final CoqFTParser parser) {
    parser.removeErrorListeners();
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer,
                Object offendingSymbol,
                int line,
                int charPositionInLine,
                String msg,
                RecognitionException e) {
            throw new CoqSyntaxException(parser,
                    (Token)offendingSymbol, line, charPositionInLine, msg,
                    e);
        }
    });
}
ScoreTranslatorTest.java 文件源码 项目:fix-orchestra 阅读 19 收藏 0 点赞 0 评论 0
@Test
public void testExampleFieldCondition() throws Exception {
  ScoreLexer l = new ScoreLexer(CharStreams.fromString(expression));
  ScoreParser p = new ScoreParser(new CommonTokenStream(l));
  p.addErrorListener(new BaseErrorListener() {
    @Override
    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
        int charPositionInLine, String msg, RecognitionException e) {
      throw new IllegalStateException(String.format(
          "Failed to parse at line %d position %d due to %s", line, charPositionInLine, msg), e);
    }
  });
  ScoreTranslator visitor = new ScoreTranslator();
  AnyExpressionContext ctx = p.anyExpression();
  String text = visitor.visitAnyExpression(ctx);
  System.out.println(text);
}
DslExpressionTest.java 文件源码 项目:fix-orchestra 阅读 21 收藏 0 点赞 0 评论 0
@Test
public void testExampleFieldCondition() throws Exception {
  ScoreLexer l = new ScoreLexer(CharStreams.fromString(fieldCondition));
  ScoreParser p = new ScoreParser(new CommonTokenStream(l));
  p.addErrorListener(new BaseErrorListener() {
    @Override
    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
        int charPositionInLine, String msg, RecognitionException e) {
      throw new IllegalStateException(String.format(
          "Failed to parse at line %d position %d due to %s", line, charPositionInLine, msg), e);
    }
  });
  ScoreBaseVisitor<Object> visitor = new ScoreBaseVisitor<>();
  AnyExpressionContext ctx = p.anyExpression();
  Object expression = visitor.visitAnyExpression(ctx);
  //System.out.println(expression.getClass().getSimpleName());
}
Compiler.java 文件源码 项目:vba-interpreter 阅读 24 收藏 0 点赞 0 评论 0
public Statement compileExpression(String expr, MethodDecl method, ModuleInstance module) throws CompileException {
    VbaLexer lexer = new VbaLexer(new org.antlr.v4.runtime.ANTLRInputStream(expr));

    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    VbaParser parser = new VbaParser(tokenStream);
    parser.setBuildParseTree(true);
    parser.addErrorListener(new BaseErrorListener() {

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
                String msg, RecognitionException e) {
            // errors.add(new CompileException(new SourceLocation(file, line, charPositionInLine, 0),
            // CompileException.SYNTAX_ERROR, msg, ((CommonToken) offendingSymbol).getText()));
            System.err.println(msg);
        }

    });

    EvalStmtContext eval = parser.evalStmt();
    ParserRuleContext c = (ParserRuleContext) eval.getChild(0);
    if (c instanceof ValueStmtContext) {
        return this.compileValueStatement((ValueStmtContext) c, method).getStatement();
    } else {
        return new BlockCompiler(method, this).compileBlockStatement(c);
    }
}
Elide.java 文件源码 项目:elide 阅读 20 收藏 0 点赞 0 评论 0
/**
 * Compile request to AST.
 *
 * @param path request
 * @return AST parse tree
 */
public static ParseTree parse(String path) {
    String normalizedPath = Paths.get(path).normalize().toString().replace(File.separatorChar, '/');
    if (normalizedPath.startsWith("/")) {
        normalizedPath = normalizedPath.substring(1);
    }
    ANTLRInputStream is = new ANTLRInputStream(normalizedPath);
    CoreLexer lexer = new CoreLexer(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 ParseCancellationException(msg, e);
        }
    });
    CoreParser parser = new CoreParser(new CommonTokenStream(lexer));
    parser.setErrorHandler(new BailErrorStrategy());
    return parser.start();
}
JsonApiParser.java 文件源码 项目:elide 阅读 26 收藏 0 点赞 0 评论 0
/**
 * Compile request to AST.
 * @param path request
 * @return AST
 */
public static ParseTree parse(String path) {
    ANTLRInputStream is = new ANTLRInputStream(path);
    CoreLexer lexer = new CoreLexer(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 ParseCancellationException(e);
        }
    });
    CoreParser parser = new CoreParser(new CommonTokenStream(lexer));
    parser.setErrorHandler(new BailErrorStrategy());
    return parser.start();
}
EntityPermissions.java 文件源码 项目:elide 阅读 22 收藏 0 点赞 0 评论 0
public static ParseTree parseExpression(String expression) {
    ANTLRInputStream is = new ANTLRInputStream(expression);
    ExpressionLexer lexer = new ExpressionLexer(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 ParseCancellationException(msg, e);
        }
    });
    ExpressionParser parser = new ExpressionParser(new CommonTokenStream(lexer));
    parser.setErrorHandler(new BailErrorStrategy());
    lexer.reset();
    return parser.start();
}
ExpectedParseTreeGenerator.java 文件源码 项目:contribution 阅读 18 收藏 0 点赞 0 评论 0
private static ParseTree parseJavadocFromFile(File file)
    throws IOException {
    final String content = Files.toString(file, Charsets.UTF_8);
    final InputStream in = new ByteArrayInputStream(content.getBytes(Charsets.UTF_8));

    final ANTLRInputStream input = new ANTLRInputStream(in);
    final JavadocLexer lexer = new JavadocLexer(input);
    lexer.removeErrorListeners();

    final BaseErrorListener errorListener = new FailOnErrorListener();
    lexer.addErrorListener(errorListener);

    final CommonTokenStream tokens = new CommonTokenStream(lexer);

    final JavadocParser parser = new JavadocParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);

    return parser.javadoc();
}
GmlTracer.java 文件源码 项目:gml-tracer 阅读 20 收藏 0 点赞 0 评论 0
public static void main(final String[] args) throws Exception {
    final FileInputStream fileInputStream = new FileInputStream(args[0]);
    final GMLLexer gmlLexer = new GMLLexer(new ANTLRInputStream(fileInputStream));
    final GMLParser gmlParser = new GMLParser(new CommonTokenStream(gmlLexer));
    gmlParser.addErrorListener(new BaseErrorListener() {

        @Override
        public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line,
                final int charPositionInLine, final String message, final RecognitionException exception) {
            throw new RuntimeException(message);
        }

    });
    final GMLExtractor gmlExtractor = new GMLExtractor(gmlParser);
    final GMLInterpreter gmlInterpreter = new GMLInterpreter(gmlExtractor);
    gmlInterpreter.interpret();
}
GMLInterpreterTest.java 文件源码 项目:gml-tracer 阅读 23 收藏 0 点赞 0 评论 0
@Test
public void twelveFactorial() throws IOException {
    final GMLLexer gmlLexer = new GMLLexer(new ANTLRInputStream(getClass().getResourceAsStream("fact.gml")));
    final GMLParser gmlParser = new GMLParser(new CommonTokenStream(gmlLexer));
    gmlParser.addErrorListener(new BaseErrorListener() {

        @Override
        public void syntaxError(final Recognizer<?, ?> theRecognizer, final Object theOffendingSymbol, final int theLine,
                final int theCharPositionInLine, final String theMsg, final RecognitionException theE) {
            Assert.fail(theMsg);
        }

    });
    final GMLExtractor gmlExtractor = new GMLExtractor(gmlParser);
    final GMLInterpreter gmlInterpreter = new GMLInterpreter(gmlExtractor);
    final Stack<Token> tokenStack = gmlInterpreter.interpret();
    Assert.assertEquals(tokenStack.size(), 1);
    final NumberToken result = (NumberToken) tokenStack.pop();
    Assert.assertEquals(result.getValue(), 479001600d);
}
GMLExtractorTest.java 文件源码 项目:gml-tracer 阅读 21 收藏 0 点赞 0 评论 0
@Test(dataProvider = "dataProvider")
public void gmlExtractorTest(final String fileName, final int expectedTokenCount) throws IOException {
    final GMLLexer gmlLexer = new GMLLexer(new ANTLRInputStream(getClass().getResourceAsStream(fileName)));
    final GMLParser gmlParser = new GMLParser(new CommonTokenStream(gmlLexer));
    gmlParser.addErrorListener(new BaseErrorListener() {

        @Override
        public void syntaxError(final Recognizer<?, ?> theRecognizer, final Object theOffendingSymbol, final int theLine,
                final int theCharPositionInLine, final String theMsg, final RecognitionException theE) {
            Assert.fail(theMsg);
        }

    });
    final GMLExtractor gmlExtractor = new GMLExtractor(gmlParser);
    final List<Token> tokens = gmlExtractor.extract();
    LOGGER.info(tokens.toString());
    Assert.assertEquals(tokens.size(), expectedTokenCount);
}
SaltProcessor.java 文件源码 项目:salt9000 阅读 19 收藏 0 点赞 0 评论 0
/** Parse data or input file and initialize the parse tree.   */
private void parseInput() {
    if (data == null)
        loadInputFile();
    if (data == null)
        throw new IllegalArgumentException("Unable to load input file or data is missing.");
    ANTLRInputStream input = new ANTLRInputStream(data);
    SaltLexer lexer = new SaltLexer(input);
    lexer.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> arg0, Object arg1, int arg2,
                int arg3, String arg4, RecognitionException arg5) {
            throw new RuntimeException(arg5);
        }
    });
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SaltParser parser = new SaltParser(tokens);
    tree = parser.document();
    if (parser.getNumberOfSyntaxErrors() > 0) {
        System.out.println("Syntax error in file " + inputFile);
        return;
    }
}
SaltTest.java 文件源码 项目:salt9000 阅读 22 收藏 0 点赞 0 评论 0
/**
 * Parse the string passed as parameter returning the string representing the structure of the UI
 * described by the parameter.
 * @param document
 * @return
 * @throws Exception
 */
private String parse(String document) throws Exception {
    ANTLRInputStream input = new ANTLRInputStream(document);
    SaltLexer lexer = new SaltLexer(input);
    // The lexer should not recover errors.
    lexer.addErrorListener(new BaseErrorListener() {

        @Override
        public void syntaxError(Recognizer<?, ?> arg0, Object arg1, int arg2,
                int arg3, String arg4, RecognitionException arg5) {
            throw new RuntimeException(arg5);
        }

    });
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SaltParser parser = new SaltParser(tokens);
    ParseTree tree = parser.document();
    if (parser.getNumberOfSyntaxErrors() > 0) {
        throw new SyntaxException("Syntax error into the document: " + document);
    }
    //
    SaltTextVisitor visitor = new SaltTextVisitor(parser);
    return visitor.visit(tree);
}
WorkspacePresentation.java 文件源码 项目:Ultrastructure 阅读 18 收藏 0 点赞 0 评论 0
public static WorkspaceContext getWorkspaceContext(InputStream source) throws IOException {
    WorkspaceLexer l = new WorkspaceLexer(CharStreams.fromStream(source));
    WorkspaceParser p = new WorkspaceParser(new CommonTokenStream(l));
    p.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);
        }
    });
    WorkspaceContext ctx = p.workspace();
    return ctx;
}
TestParse.java 文件源码 项目:Ultrastructure 阅读 33 收藏 0 点赞 0 评论 0
@Test
public void testParse() throws Exception {
    WorkspaceLexer l = new WorkspaceLexer(CharStreams.fromStream(getClass().getResourceAsStream("/thing.wsp")));
    WorkspaceParser p = new WorkspaceParser(new CommonTokenStream(l));
    p.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);
        }
    });
    p.workspace();
}
Spa.java 文件源码 项目:Ultrastructure 阅读 20 收藏 0 点赞 0 评论 0
public static Spa manifest(String resource) throws IOException {
    SpaLexer l = new SpaLexer(CharStreams.fromStream(Utils.resolveResource(Spa.class,
                                                                           resource)));
    SpaParser p = new SpaParser(new CommonTokenStream(l));
    p.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);
        }
    });
    SpaContext spa = p.spa();
    SpaImporter importer = new SpaImporter();
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(importer, spa);
    return importer.getSpa();
}
Antlr4OboParserTestBase.java 文件源码 项目:ontolib 阅读 26 收藏 0 点赞 0 评论 0
/**
 * Build and return {@link Antlr4OboParser} for a given <code>text</code>.
 *
 * @param text String with the text to parse.
 * @param mode Name of the mode to use.
 * @return {@link Antlr4OboParser}, readily setup for parsing the OBO file.
 */
protected Antlr4OboParser buildParser(String text, String mode) {
  final CodePointCharStream inputStream = CharStreams.fromString(text);
  final OboLexer l = new OboLexer(inputStream);

  for (int i = 0; i < l.getModeNames().length; ++i) {
    if (mode.equals(l.getModeNames()[i])) {
      l.mode(i);
    }
  }

  Antlr4OboParser p = new Antlr4OboParser(new CommonTokenStream(l));

  p.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);
    }
  });

  p.addErrorListener(new DiagnosticErrorListener());

  p.addParseListener(outerListener);

  return p;
}
ProjectCommitsPage.java 文件源码 项目:gitplex-mit 阅读 19 收藏 0 点赞 0 评论 0
@Nullable
private QueryContext parse(@Nullable String query) {
    if (query != null) {
        ANTLRInputStream is = new ANTLRInputStream(query); 
        CommitQueryLexer lexer = new CommitQueryLexer(is);
        lexer.removeErrorListeners();
        lexer.addErrorListener(new BaseErrorListener() {

            @Override
            public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                    int charPositionInLine, String msg, RecognitionException e) {
                if (e != null) {
                    logger.error("Error lexing commit query", e);
                } else if (msg != null) {
                    logger.error("Error lexing commit query: " + msg);
                }
                throw new RuntimeException("Malformed commit query");
            }

        });
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        CommitQueryParser parser = new CommitQueryParser(tokens);
        parser.removeErrorListeners();
        parser.setErrorHandler(new BailErrorStrategy());
        return parser.query();
    } else {
        return null;
    }
}
Antlr4OboParserTestBase.java 文件源码 项目:boqa 阅读 22 收藏 0 点赞 0 评论 0
/**
 * Build and return {@link Antlr4OboParser} for a given <code>text</code>.
 *
 * @param text String with the text to parse.
 * @param mode Name of the mode to use.
 * @return {@link Antlr4OboParser}, readily setup for parsing the OBO file.
 */
protected Antlr4OboParser buildParser(String text, String mode) {
  final CodePointCharStream inputStream = CharStreams.fromString(text);
  final OboLexer l = new OboLexer(inputStream);

  for (int i = 0; i < l.getModeNames().length; ++i) {
    if (mode.equals(l.getModeNames()[i])) {
      l.mode(i);
    }
  }

  Antlr4OboParser p = new Antlr4OboParser(new CommonTokenStream(l));

  p.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);
    }
  });

  p.addErrorListener(new DiagnosticErrorListener());

  p.addParseListener(outerListener);

  return p;
}
Autocomplete.java 文件源码 项目:grakn 阅读 23 收藏 0 点赞 0 评论 0
/**
 * @param query a graql query
 * @return a list of tokens from running the lexer on the query
 */
private static List<? extends Token> getTokens(String query) {
    ANTLRInputStream input = new ANTLRInputStream(query);
    GraqlLexer lexer = new GraqlLexer(input);

    // Ignore syntax errors
    lexer.removeErrorListeners();
    lexer.addErrorListener(new BaseErrorListener());

    return lexer.getAllTokens();
}
OQLFilterBuilder.java 文件源码 项目:djigger 阅读 21 收藏 0 点赞 0 评论 0
private static ParseContext parse(String expression) {
    OQLLexer lexer = new OQLLexer(new ANTLRInputStream(expression));
    OQLParser parser = new OQLParser(new CommonTokenStream(lexer));
    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);
        }
    });
    return parser.parse();

}
OQLMongoDBBuilder.java 文件源码 项目:djigger 阅读 19 收藏 0 点赞 0 评论 0
private static ParseContext parse(String expression) {
    OQLLexer lexer = new OQLLexer(new ANTLRInputStream(expression));
    OQLParser parser = new OQLParser(new CommonTokenStream(lexer));
    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);
        }
    });
    return parser.parse();

}
StringTemplate.java 文件源码 项目:monsoon 阅读 22 收藏 0 点赞 0 评论 0
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;
}
CollectdTags.java 文件源码 项目:monsoon 阅读 17 收藏 0 点赞 0 评论 0
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;
}
ScoreVisitorImplTest.java 文件源码 项目:fix-orchestra 阅读 20 收藏 0 点赞 0 评论 0
private ScoreParser parse(String expression) throws IOException {
  ScoreLexer l = new ScoreLexer(CharStreams.fromString(expression));
  ScoreParser p = new ScoreParser(new CommonTokenStream(l));
  p.addErrorListener(new BaseErrorListener() {
    @Override
    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
        int charPositionInLine, String msg, RecognitionException e) {
      throw new IllegalStateException(String.format(
          "Failed to parse at line %d position %d due to %s", line, charPositionInLine, msg), e);
    }
  });
  return p;
}
SaralCompilationUnitParser.java 文件源码 项目:saral 阅读 18 收藏 0 点赞 0 评论 0
public Init getCompilationUnit(File preprocessedTempFile, String className) throws IOException{
    String fileAbsPath = preprocessedTempFile.getAbsolutePath();

    CharStream charStream = new ANTLRFileStream(fileAbsPath);
    SaralLexer saralLexer = new SaralLexer(charStream);
    CommonTokenStream commonTokenStream  = new CommonTokenStream(saralLexer);
    SaralParser saralParser = new SaralParser(commonTokenStream);

    BaseErrorListener errorListener = new SaralTreeWalkErrorListener();
    saralParser.addErrorListener(errorListener);

    InitVisitor initVisitor = new InitVisitor(className);
    return saralParser.init().accept(initVisitor);
}
OQLFilterBuilder.java 文件源码 项目:step 阅读 18 收藏 0 点赞 0 评论 0
private static ParseContext parse(String expression) {
    OQLLexer lexer = new OQLLexer(new ANTLRInputStream(expression));
    OQLParser parser = new OQLParser(new CommonTokenStream(lexer));
    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);
        }
    });
    return parser.parse();

}


问题


面经


文章

微信
公众号

扫码关注公众号