use of org.antlr.v4.runtime.ANTLRErrorListener in project aic-expresso by aic-sri-international.
the class AntlrGrinderParserWrapper method parse.
@Override
public Expression parse(String string, Parser.ErrorListener parserEerrorListener) {
Expression result = null;
try {
AntlrErrorListener antlrErrorListener = new AntlrErrorListener(parserEerrorListener);
ANTLRInputStream input = new ANTLRInputStream(string);
AntlrGrinderLexer lexer = new AntlrGrinderLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
AntlrGrinderParser parser = new AntlrGrinderParser(tokens);
lexer.removeErrorListeners();
parser.removeErrorListeners();
lexer.addErrorListener(antlrErrorListener);
parser.addErrorListener(antlrErrorListener);
ParseTree tree = parser.expression();
boolean eof = parser.getInputStream().LA(1) == Recognizer.EOF;
if (!antlrErrorListener.errorsDetected) {
if (!eof) {
System.err.println("Unable to parse the complete input expression: " + input);
} else {
lexer.removeErrorListeners();
parser.removeErrorListeners();
ExpressionVisitor expressionVisitor = new ExpressionVisitor(getRandomPredicatesSignatures());
result = expressionVisitor.visit(tree);
}
}
} catch (RecognitionException re) {
re.printStackTrace();
} catch (RuntimeException re) {
re.printStackTrace();
}
return result;
}
use of org.antlr.v4.runtime.ANTLRErrorListener in project aic-praise by aic-sri-international.
the class HOGMParserWrapper method parse.
//
// PRIVATE
//
private Expression parse(String string, Parser.ErrorListener errorListener, ParseTreeRetriever parseTreeRetriever) throws RecognitionException, UnableToParseAllTheInputError, HOGModelException {
Expression result = null;
AntlrErrorListener antlrErrorListener = new AntlrErrorListener(errorListener);
ANTLRInputStream input = new ANTLRInputStream(string);
HOGMLexer lexer = new HOGMLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
HOGMParser parser = new HOGMParser(tokens);
lexer.removeErrorListeners();
parser.removeErrorListeners();
lexer.addErrorListener(antlrErrorListener);
parser.addErrorListener(antlrErrorListener);
ParseTree tree = parseTreeRetriever.retrieve(parser);
boolean eofReached = parser.getInputStream().LA(1) == Recognizer.EOF;
if (!antlrErrorListener.errorsDetected) {
if (!eofReached) {
throw new UnableToParseAllTheInputError();
} else {
lexer.removeErrorListeners();
parser.removeErrorListeners();
HOGModelVisitor hogmModelVisitor = new HOGModelVisitor();
result = hogmModelVisitor.visit(tree);
}
}
return result;
}
use of org.antlr.v4.runtime.ANTLRErrorListener in project CFLint by cflint.
the class CFLint method unpackTagExpressions.
private Map<String, CFExpression> unpackTagExpressions(final Element elem) {
// Use LinkedHashMap to preserve the order
final Map<String, CFExpression> expressions = new LinkedHashMap<>();
if (!elem.getName().toLowerCase().startsWith("cf") || elem.getAttributes() == null) {
return expressions;
}
// variable attribute
for (final Attribute attr : elem.getAttributes()) {
if (detectScript(attr)) {
// Try wrapping the expression in single or double quotes for
// parsing.
final List<String> literalChar = attr.getValue().contains("'") ? Arrays.asList("\"", "'") : Arrays.asList("'", "\"");
try {
final List<String> errors = new ArrayList<>();
final ANTLRErrorListener errorReporter = new ArrayErrorListener(errors);
final CFExpression exp = cfmlParser.parseCFMLExpression(literalChar.get(0) + attr.getValue() + literalChar.get(0), errorReporter);
if (errors.isEmpty()) {
expressions.put(attr.getName().toLowerCase(), exp);
continue;
}
} catch (final Exception e) {
}
// Try other quotes before reporting a failure
try {
final CFExpression exp = cfmlParser.parseCFMLExpression(literalChar.get(1) + attr.getValue() + literalChar.get(1), this);
expressions.put(attr.getName().toLowerCase(), exp);
} catch (final Exception e2) {
System.err.println("Error in parsing : " + attr.getValue() + " on tag " + elem.getName());
}
} else if (tagInfo.isExpressionAttribute(elem, attr.getName())) {
try {
final CFExpression exp = cfmlParser.parseCFMLExpression(attr.getValue(), this);
expressions.put(attr.getName().toLowerCase(), exp);
} catch (final Exception e2) {
System.err.println("Error in parsing : " + attr.getValue() + " on tag " + elem.getName());
}
}
}
return expressions;
}
Aggregations