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();
}
java类org.antlr.v4.runtime.BaseErrorListener的实例源码
OQLMongoDBBuilder.java 文件源码
项目:step
阅读 18
收藏 0
点赞 0
评论 0
TargetSelector.java 文件源码
项目:MPL
阅读 22
收藏 0
点赞 0
评论 0
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;
}
FetchCompilerError.java 文件源码
项目:xtext-ide
阅读 19
收藏 0
点赞 0
评论 0
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;
}
DocParser.java 文件源码
项目:jooby
阅读 18
收藏 0
点赞 0
评论 0
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);
}
};
}
Generator.java 文件源码
项目:protoc
阅读 27
收藏 0
点赞 0
评论 0
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 );
}
PathMatcher.java 文件源码
项目:monsoon
阅读 24
收藏 0
点赞 0
评论 0
/**
* 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;
}
TimeSeriesMetricExpression.java 文件源码
项目:monsoon
阅读 18
收藏 0
点赞 0
评论 0
/**
* 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;
}
Fuzzer.java 文件源码
项目:abnffuzzer
阅读 21
收藏 0
点赞 0
评论 0
/**
* 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));
}
}
}
});
}
Program.java 文件源码
项目:eo
阅读 24
收藏 0
点赞 0
评论 0
/**
* 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();
}
RegexCompiler.java 文件源码
项目:moar
阅读 15
收藏 0
点赞 0
评论 0
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();
}