Java 类org.antlr.v4.runtime.BaseErrorListener 实例源码
项目:elasticsearch_my
文件:Walker.java
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);
}
项目:ontolib
文件:OboParser.java
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();
}
项目:boqa
文件:OboParser.java
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();
}
项目:exterminator
文件:CoqFTParser.java
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);
}
});
}
项目:fix-orchestra
文件:ScoreTranslatorTest.java
@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);
}
项目:fix-orchestra
文件:DslExpressionTest.java
@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());
}
项目:vba-interpreter
文件:Compiler.java
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
文件:Elide.java
/**
* 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();
}
项目:elide
文件:JsonApiParser.java
/**
* 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();
}
项目:elide
文件:EntityPermissions.java
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();
}
项目:contribution
文件:ExpectedParseTreeGenerator.java
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();
}
项目:gml-tracer
文件:GmlTracer.java
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();
}
项目:gml-tracer
文件:GMLInterpreterTest.java
@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);
}
项目:gml-tracer
文件:GMLExtractorTest.java
@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);
}
项目:salt9000
文件:SaltProcessor.java
/** 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;
}
}
项目:salt9000
文件:SaltTest.java
/**
* 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);
}
项目:Ultrastructure
文件:WorkspacePresentation.java
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;
}
项目:Ultrastructure
文件:TestParse.java
@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();
}
项目:Ultrastructure
文件:Spa.java
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();
}
项目:ontolib
文件:Antlr4OboParserTestBase.java
/**
* 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;
}
项目:gitplex-mit
文件:ProjectCommitsPage.java
@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;
}
}
项目:boqa
文件:Antlr4OboParserTestBase.java
/**
* 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;
}
项目:grakn
文件:Autocomplete.java
/**
* @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();
}
项目:djigger
文件:OQLFilterBuilder.java
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();
}
项目:djigger
文件:OQLMongoDBBuilder.java
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();
}
项目:monsoon
文件:StringTemplate.java
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;
}
项目:monsoon
文件:CollectdTags.java
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;
}
项目:fix-orchestra
文件:ScoreVisitorImplTest.java
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;
}
项目:saral
文件:SaralCompilationUnitParser.java
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);
}
项目:step
文件:OQLFilterBuilder.java
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();
}
项目:step
文件:OQLMongoDBBuilder.java
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();
}
项目:MPL
文件:TargetSelector.java
public static @Nullable TargetSelector parse(String value, MplSource source,
MplCompilerContext context) {
checkNotNull(value, "value == null!");
checkNotNull(source, "source == null!");
checkNotNull(context, "context == null!");
ANTLRInputStream input = new ANTLRInputStream(value);
TargetSelectorLexer lexer = new TargetSelectorLexer(input);
TokenStream tokens = new CommonTokenStream(lexer);
TargetSelectorParser parser = new TargetSelectorParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object token, int line,
int charPositionInLine, String message, RecognitionException cause) {
context.addError(new CompilerException(source, message));
}
});
SelectorContext ctx = parser.selector();
if (context.getErrors().isEmpty()) {
TargetSelectorListenerImpl listener = new TargetSelectorListenerImpl();
new ParseTreeWalker().walk(listener, ctx);
return listener.getResult();
}
return null;
}
项目:xtext-ide
文件:FetchCompilerError.java
protected CommonTokenStream lex(final String input, final int[] ids, final String[] strings, final String[] errors) throws IOException {
lexer = new BoaLexer(new ANTLRInputStream(new StringReader(input)));
lexer.removeErrorListeners();
lexer.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) {
error("lexer", (BoaLexer)recognizer, offendingSymbol, line, charPositionInLine, 1, msg, e);
}
});
final CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
return tokens;
}
项目:jooby
文件:DocParser.java
private static ANTLRErrorListener errorListener(final Logger log, Path file) {
return new BaseErrorListener() {
@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol,
final int line,
final int charPositionInLine, final String msg, final RecognitionException e) {
log.debug("{}:{}:{} {}", file, line, charPositionInLine, msg);
}
};
}
项目:protoc
文件:Generator.java
public void parse(InputStream stream, ProtoParserBaseVisitor<Void> visitor, boolean trace) throws IOException {
final ANTLRInputStream input = new ANTLRInputStream( stream );
final ProtoParserLexer lexer = new ProtoParserLexer( input );
final CommonTokenStream tokens = new CommonTokenStream( lexer );
final ProtoParserParser parser = new ProtoParserParser( tokens );
final List<String> parseErrros = new LinkedList<String>();
parser.addErrorListener( new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer,
Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e) {
List<String> stack = ( (ProtoParserParser) recognizer ).getRuleInvocationStack();
Collections.reverse( stack );
String parseError = String.format( "line %s:%s at %s: error=%s", line, charPositionInLine, offendingSymbol, msg );
LOGGER.warn( "rule stack: " + stack );
LOGGER.warn( parseError );
parseErrros.add( parseError );
}
} );
if ( failOnParseErrors && !parseErrros.isEmpty() ) {
for ( String err : parseErrros ) {
LOGGER.error( err );
}
throw new IllegalStateException( "schema has errors" );
}
parser.setTrace( trace );
ProtoContext protoContext = parser.proto();
visitor.visit( protoContext );
}
项目:monsoon
文件:PathMatcher.java
/**
* Read path matcher from reader.
*
* @param reader A reader supplying the input of a path expression.
* @return A PathMatcher corresponding to the parsed input
* from the reader.
* @throws IOException on IO errors from the reader.
* @throws
* com.groupon.lex.metrics.PathMatcher.ParseException
* on invalid path expression.
*/
public static PathMatcher valueOf(Reader reader) throws IOException, ParseException {
class DescriptiveErrorListener extends BaseErrorListener {
public List<String> errors = new ArrayList<>();
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine,
String msg, org.antlr.v4.runtime.RecognitionException e) {
LOG.log(Level.INFO, "Parse error: {0}:{1} -> {2}", new Object[]{line, charPositionInLine, msg});
errors.add(String.format("%d:%d: %s", line, charPositionInLine, msg));
}
}
final DescriptiveErrorListener error_listener = new DescriptiveErrorListener();
final PathMatcherLexer lexer = new PathMatcherLexer(CharStreams.fromReader(reader));
lexer.removeErrorListeners();
lexer.addErrorListener(error_listener);
final PathMatcherGrammar parser = new PathMatcherGrammar(new BufferedTokenStream(lexer));
parser.removeErrorListeners();
parser.addErrorListener(error_listener);
final PathMatcherGrammar.ExprContext expr;
try {
expr = parser.expr();
} catch (Exception ex) {
LOG.log(Level.SEVERE, "parser yielded exceptional return", ex);
if (!error_listener.errors.isEmpty())
throw new ParseException(error_listener.errors, ex);
else
throw ex;
}
if (!error_listener.errors.isEmpty()) {
if (expr.exception != null)
throw new ParseException(error_listener.errors, expr.exception);
throw new ParseException(error_listener.errors);
} else if (expr.exception != null) {
throw new ParseException(expr.exception);
}
return expr.s;
}
项目:monsoon
文件:TimeSeriesMetricExpression.java
/**
* Read expression from reader.
*
* @param reader A reader supplying the input of an expression.
* @return A TimeSeriesMetricExpression corresponding to the parsed input
* from the reader.
* @throws IOException on IO errors from the reader.
* @throws
* com.groupon.lex.metrics.timeseries.TimeSeriesMetricExpression.ParseException
* on invalid expression.
*/
public static TimeSeriesMetricExpression valueOf(Reader reader) throws IOException, ParseException {
final Logger LOG = Logger.getLogger(TimeSeriesMetricExpression.class.getName());
class DescriptiveErrorListener extends BaseErrorListener {
public List<String> errors = new ArrayList<>();
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine,
String msg, org.antlr.v4.runtime.RecognitionException e) {
LOG.log(Level.INFO, "Parse error: {0}:{1} -> {2}", new Object[]{line, charPositionInLine, msg});
errors.add(String.format("%d:%d: %s", line, charPositionInLine, msg));
}
}
final DescriptiveErrorListener error_listener = new DescriptiveErrorListener();
final ExpressionLexer lexer = new ExpressionLexer(CharStreams.fromReader(reader));
lexer.removeErrorListeners();
lexer.addErrorListener(error_listener);
final Expression parser = new Expression(new BufferedTokenStream(lexer));
parser.removeErrorListeners();
parser.addErrorListener(error_listener);
final Expression.ExprContext expr;
try {
expr = parser.expr();
} catch (Exception ex) {
LOG.log(Level.SEVERE, "parser yielded exceptional return", ex);
if (!error_listener.errors.isEmpty())
throw new ParseException(error_listener.errors, ex);
else
throw ex;
}
if (!error_listener.errors.isEmpty()) {
if (expr.exception != null)
throw new ParseException(error_listener.errors, expr.exception);
throw new ParseException(error_listener.errors);
} else if (expr.exception != null) {
throw new ParseException(expr.exception);
}
return expr.s;
}
项目:abnffuzzer
文件:Fuzzer.java
/**
* Create a new {@code Fuzzer} by reading ABNF rules from a reader.
*
* @param rules
* ANBF rules
* @throws IOException
* if the rules can't be read
*/
@SuppressWarnings("serial")
public Fuzzer(final Reader rules) throws IOException {
final AbnfLexer l = new AbnfLexer(new ANTLRInputStream(rules));
final CommonTokenStream tokens = new CommonTokenStream(l);
final AbnfParser p = new AbnfParser(tokens);
p.setBuildParseTree(true);
p.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 IllegalStateException(
"failed to parse at line " + line + " due to " + msg,
e);
}
});
final ParseTree tree = p.rulelist();
ruleList = Collections.unmodifiableMap(new RuleList() {
{
for (int i = 0; i < tree.getChildCount(); i++) {
final ParseTree child = tree.getChild(i);
if (child instanceof Rule_Context
&& child.getChildCount() == 3) {
// rule definition
final ParseTree name = child.getChild(0);
if (!(name instanceof TerminalNode)) {
throw new IllegalArgumentException();
}
final ParseTree equalSign = child.getChild(1);
if (!(equalSign instanceof TerminalNode)
|| !"=".equals(equalSign.toString())) {
throw new IllegalArgumentException();
}
final ParseTree elements = child.getChild(2);
if (!(elements instanceof ElementsContext)) {
throw new IllegalArgumentException();
}
put(name.toString(),
new Rule((ElementsContext) elements));
}
}
}
});
}
项目:eo
文件:Program.java
/**
* Compile it to Java and save.
* @throws IOException If fails
*/
public void compile() throws IOException {
final String[] lines = new TextOf(this.input).asString().split("\n");
final ANTLRErrorListener errors = new BaseErrorListener() {
// @checkstyle ParameterNumberCheck (10 lines)
@Override
public void syntaxError(final Recognizer<?, ?> recognizer,
final Object symbol, final int line,
final int position, final String msg,
final RecognitionException error) {
throw new CompileException(
String.format(
"[%d:%d] %s: \"%s\"",
line, position, msg, lines[line - 1]
),
error
);
}
};
final ProgramLexer lexer = new ProgramLexer(
CharStreams.fromStream(this.input.stream())
);
lexer.removeErrorListeners();
lexer.addErrorListener(errors);
final ProgramParser parser = new ProgramParser(
new CommonTokenStream(lexer)
);
parser.removeErrorListeners();
parser.addErrorListener(errors);
final Tree tree = parser.program().ret;
new IoCheckedScalar<>(
new And(
tree.java().entrySet(),
path -> {
new LengthOf(
new TeeInput(
path.getValue(),
this.target.apply(path.getKey())
)
).value();
}
)
).value();
}
项目:moar
文件:RegexCompiler.java
public static Regex compile(String regexStr) {
StringBuilder additionalMessage = new StringBuilder();
RegexParser parser = regexParser( regexStr );
parser.getErrorListeners().clear();
parser.addErrorListener(
new BaseErrorListener() {
@Override
public void syntaxError(
Recognizer<?, ?> recognizer,
Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e) {
additionalMessage.append( "SyntaxEception in Regex: \"" )
.append( regexStr )
.append( "\": " )
.append( msg );
if ( offendingSymbol instanceof CommonToken ) {
CommonToken token = (CommonToken) offendingSymbol;
if ( token.getText().equals( "*" ) || token.getText().equals( "+" ) || token.getText()
.equals( "?" ) ) {
additionalMessage.append( ", dangling metacharacter: '" )
.append( ((CommonToken) offendingSymbol).getText() )
.append( "' at line " )
.append( token.getLine() )
.append( ", pos " )
.append( token.getCharPositionInLine() );
}
}
}
}
);
RegexParser.RegexContext regexTree = parser.regex();
if ( parser.getNumberOfSyntaxErrors() > 0 ) {
throw new IllegalArgumentException( "malformed regex found : " + regexStr + "\n" + additionalMessage.toString() );
}
ParseTreeWalker walker = new ParseTreeWalker();
RegexGroupNameListener nameListener = new RegexGroupNameListener();
walker.walk( nameListener, regexTree );
RegexTreeListener listener = new RegexTreeListener( nameListener.getGroupNames() );
walker.walk( listener, regexTree );
return listener.finalRegex();
}