Search in sources :

Example 1 with StellarLexer

use of org.apache.metron.stellar.common.generated.StellarLexer 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)

Aggregations

ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 TokenStream (org.antlr.v4.runtime.TokenStream)1 StellarLexer (org.apache.metron.stellar.common.generated.StellarLexer)1 StellarParser (org.apache.metron.stellar.common.generated.StellarParser)1 ErrorListener (org.apache.metron.stellar.dsl.ErrorListener)1