Search in sources :

Example 56 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project metron by apache.

the class BaseStellarProcessor method compile.

/**
 * Parses and evaluates the given Stellar expression, {@code rule}.
 * @param rule The Stellar expression to parse and evaluate.
 * @return The Expression, which can be reevaluated without reparsing in different Contexts and Resolvers.
 */
public static StellarCompiler.Expression compile(final String rule) {
    if (rule == null || isEmpty(rule.trim())) {
        return null;
    }
    ANTLRInputStream input = new ANTLRInputStream(rule);
    StellarLexer lexer = new StellarLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(new ErrorListener());
    TokenStream tokens = new CommonTokenStream(lexer);
    StellarParser parser = new StellarParser(tokens);
    StellarCompiler treeBuilder = new StellarCompiler(ArithmeticEvaluator.INSTANCE, NumberLiteralEvaluator.INSTANCE, ComparisonExpressionWithOperatorEvaluator.INSTANCE);
    parser.addParseListener(treeBuilder);
    parser.removeErrorListeners();
    parser.addErrorListener(new ErrorListener());
    parser.transformation();
    return treeBuilder.getExpression();
}
Also used : ErrorListener(org.apache.metron.stellar.dsl.ErrorListener) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TokenStream(org.antlr.v4.runtime.TokenStream) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) StellarLexer(org.apache.metron.stellar.common.generated.StellarLexer) StellarParser(org.apache.metron.stellar.common.generated.StellarParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 57 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project grakn by graknlabs.

the class Autocomplete method getTokens.

/**
 * @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();
}
Also used : BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) GraqlLexer(ai.grakn.graql.internal.antlr.GraqlLexer)

Example 58 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project drools by kiegroup.

the class FEELParser method parse.

public static FEEL_1_1Parser parse(FEELEventListenersManager eventsManager, String source, Map<String, Type> inputVariableTypes, Map<String, Object> inputVariables, Collection<FEELFunction> additionalFunctions, List<FEELProfile> profiles) {
    ANTLRInputStream input = new ANTLRInputStream(source);
    FEEL_1_1Lexer lexer = new FEEL_1_1Lexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    FEEL_1_1Parser parser = new FEEL_1_1Parser(tokens);
    ParserHelper parserHelper = new ParserHelper(eventsManager);
    additionalFunctions.forEach(f -> parserHelper.getSymbolTable().getBuiltInScope().define(f.getSymbol()));
    profiles.stream().filter(KieExtendedFEELProfile.class::isInstance).forEach(dc -> parserHelper.setFeatDMN12EnhancedForLoopEnabled(true));
    parser.setHelper(parserHelper);
    parser.setErrorHandler(new FEELErrorHandler());
    // removes the error listener that prints to the console
    parser.removeErrorListeners();
    parser.addErrorListener(new FEELParserErrorListener(eventsManager));
    // pre-loads the parser with symbols
    defineVariables(inputVariableTypes, inputVariables, parser);
    return parser;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 59 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project drools by kiegroup.

the class FEELParser method checkVariableName.

public static List<FEELEvent> checkVariableName(String source) {
    if (source == null || source.isEmpty()) {
        return Collections.singletonList(new SyntaxErrorEvent(FEELEvent.Severity.ERROR, Msg.createMessage(Msg.INVALID_VARIABLE_NAME_EMPTY), null, 0, 0, null));
    }
    ANTLRInputStream input = new ANTLRInputStream(source);
    FEEL_1_1Lexer lexer = new FEEL_1_1Lexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    FEEL_1_1Parser parser = new FEEL_1_1Parser(tokens);
    parser.setHelper(new ParserHelper());
    parser.setErrorHandler(new FEELErrorHandler());
    FEELParserErrorListener errorChecker = new FEELParserErrorListener(null);
    // removes the error listener that prints to the console
    parser.removeErrorListeners();
    parser.addErrorListener(errorChecker);
    FEEL_1_1Parser.NameDefinitionContext nameDef = parser.nameDefinition();
    if (!errorChecker.hasErrors() && nameDef != null && source.trim().equals(parser.getHelper().getOriginalText(nameDef))) {
        return Collections.emptyList();
    }
    return errorChecker.getErrors();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) SyntaxErrorEvent(org.kie.dmn.feel.runtime.events.SyntaxErrorEvent) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 60 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project incubator-systemml by apache.

the class DMLParserWrapper method doParse.

/**
 * This function is supposed to be called directly only from DmlSyntacticValidator when it encounters 'import'
 * @param fileName script file name
 * @param dmlScript script file contents
 * @param sourceNamespace namespace from source statement
 * @param argVals script arguments
 * @return dml program, or null if at least one error
 */
public DMLProgram doParse(String fileName, String dmlScript, String sourceNamespace, Map<String, String> argVals) {
    DMLProgram dmlPgm = null;
    ANTLRInputStream in;
    try {
        if (dmlScript == null) {
            dmlScript = readDMLScript(fileName, LOG);
        }
        InputStream stream = new ByteArrayInputStream(dmlScript.getBytes());
        in = new ANTLRInputStream(stream);
    } catch (FileNotFoundException e) {
        throw new ParseException("Cannot find file/resource: " + fileName, e);
    } catch (IOException e) {
        throw new ParseException("Cannot open file: " + fileName, e);
    } catch (LanguageException e) {
        throw new ParseException(e.getMessage(), e);
    }
    ProgramrootContext ast = null;
    CustomErrorListener errorListener = new CustomErrorListener();
    try {
        DmlLexer lexer = new DmlLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        DmlParser antlr4Parser = new DmlParser(tokens);
        // For now no optimization, since it is not able to parse integer value.
        boolean tryOptimizedParsing = false;
        if (tryOptimizedParsing) {
            // Try faster and simpler SLL
            antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            antlr4Parser.removeErrorListeners();
            antlr4Parser.setErrorHandler(new BailErrorStrategy());
            try {
                ast = antlr4Parser.programroot();
            // If successful, no need to try out full LL(*) ... SLL was enough
            } catch (ParseCancellationException ex) {
                // Error occurred, so now try full LL(*) for better error messages
                tokens.reset();
                antlr4Parser.reset();
                if (fileName != null) {
                    errorListener.setCurrentFileName(fileName);
                } else {
                    errorListener.setCurrentFileName("MAIN_SCRIPT");
                }
                // Set our custom error listener
                antlr4Parser.addErrorListener(errorListener);
                antlr4Parser.setErrorHandler(new DefaultErrorStrategy());
                antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.LL);
                ast = antlr4Parser.programroot();
            }
        } else {
            // Set our custom error listener
            antlr4Parser.removeErrorListeners();
            antlr4Parser.addErrorListener(errorListener);
            errorListener.setCurrentFileName(fileName);
            // Now do the parsing
            ast = antlr4Parser.programroot();
        }
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot parse the program:" + fileName, e);
    }
    // Now convert the parse tree into DMLProgram
    // Do syntactic validation while converting
    ParseTree tree = ast;
    // And also do syntactic validation
    ParseTreeWalker walker = new ParseTreeWalker();
    // Get list of function definitions which take precedence over built-in functions if same name
    DmlPreprocessor prep = new DmlPreprocessor(errorListener);
    walker.walk(prep, tree);
    // Syntactic validation
    DmlSyntacticValidator validator = new DmlSyntacticValidator(errorListener, argVals, sourceNamespace, prep.getFunctionDefs());
    walker.walk(validator, tree);
    errorListener.unsetCurrentFileName();
    this.parseIssues = errorListener.getParseIssues();
    this.atLeastOneWarning = errorListener.isAtLeastOneWarning();
    this.atLeastOneError = errorListener.isAtLeastOneError();
    if (atLeastOneError) {
        throw new ParseException(parseIssues, dmlScript);
    }
    if (atLeastOneWarning) {
        LOG.warn(CustomErrorListener.generateParseIssuesMessage(dmlScript, parseIssues));
    }
    dmlPgm = createDMLProgram(ast, sourceNamespace);
    return dmlPgm;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CustomErrorListener(org.apache.sysml.parser.common.CustomErrorListener) ByteArrayInputStream(java.io.ByteArrayInputStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) FileNotFoundException(java.io.FileNotFoundException) ProgramrootContext(org.apache.sysml.parser.dml.DmlParser.ProgramrootContext) IOException(java.io.IOException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.sysml.parser.ParseException) LanguageException(org.apache.sysml.parser.LanguageException) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) DMLProgram(org.apache.sysml.parser.DMLProgram) ParseException(org.apache.sysml.parser.ParseException) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Aggregations

ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)110 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)86 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)59 Test (org.junit.Test)59 LexerGrammar (org.antlr.v4.tool.LexerGrammar)52 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)43 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 CharStream (org.antlr.v4.runtime.CharStream)15 ParseTree (org.antlr.v4.runtime.tree.ParseTree)15 Token (org.antlr.v4.runtime.Token)12 TokenStream (org.antlr.v4.runtime.TokenStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 StringReader (java.io.StringReader)7 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)7 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)7 BufferedTokenStream (org.antlr.v4.runtime.BufferedTokenStream)6 IntegerList (org.antlr.v4.runtime.misc.IntegerList)6 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)5