Search in sources :

Example 1 with ANTLRErrorListener

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;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Expression(com.sri.ai.expresso.api.Expression) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 2 with ANTLRErrorListener

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;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Expression(com.sri.ai.expresso.api.Expression) HOGMLexer(com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer) HOGMParser(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 3 with ANTLRErrorListener

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;
}
Also used : Attribute(net.htmlparser.jericho.Attribute) ArrayList(java.util.ArrayList) ArrayErrorListener(cfml.parsing.reporting.ArrayErrorListener) ANTLRErrorListener(org.antlr.v4.runtime.ANTLRErrorListener) ParseException(cfml.parsing.reporting.ParseException) IOException(java.io.IOException) CFLintScanException(com.cflint.exception.CFLintScanException) CFExpression(cfml.parsing.cfscript.CFExpression) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 CFExpression (cfml.parsing.cfscript.CFExpression)1 ArrayErrorListener (cfml.parsing.reporting.ArrayErrorListener)1 ParseException (cfml.parsing.reporting.ParseException)1 CFLintScanException (com.cflint.exception.CFLintScanException)1 HOGMLexer (com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer)1 HOGMParser (com.sri.ai.praise.model.v1.hogm.antlr.HOGMParser)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Attribute (net.htmlparser.jericho.Attribute)1 ANTLRErrorListener (org.antlr.v4.runtime.ANTLRErrorListener)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1