public static boolean isVariableNameValid( String source ) { return checkVariableName( source ).isEmpty(); }
/** * Either namePart is a string of digits, or it must be a valid name itself */ public static boolean isVariableNamePartValid( String namePart, Scope scope ) { if ( DIGITS_PATTERN.matcher(namePart).matches() ) { return true; } if ( REUSABLE_KEYWORDS.contains(namePart) ) { return scope.followUp(namePart, true); } return isVariableNameValid(namePart); }
public boolean followUp(Token t, boolean isPredict) { boolean dynamicResolutionResult = isDynamicResolution() && FEELParser.isVariableNamePartValid( t.getText(), currentScope ); boolean follow = dynamicResolutionResult || this.currentScope.followUp( t.getText(), isPredict ); // in case isPredict == false, will need to followUp in the currentScope, so that the TokenTree currentNode is updated as per expectations, // this is because the `follow` variable above, in the case of short-circuited on `dynamicResolutionResult`, // would skip performing any necessary update in the second part of the || predicate if (dynamicResolutionResult && !isPredict) { this.currentScope.followUp(t.getText(), isPredict); } return follow; }
@Test public void testVariableNameCantStartWithKeyword() { String var = "for keyword is an invalid start for a variable name"; assertThat( FEELParser.isVariableNameValid( var ), is( false ) ); assertThat( FEELParser.checkVariableName( var ).get( 0 ).getMessage(), is( Msg.createMessage(Msg.INVALID_VARIABLE_NAME_START, "keyword", "for") ) ); }
private BaseNode parse(String input, Map<String, Type> inputTypes) { FEEL_1_1Parser parser = FEELParser.parse(null, input, inputTypes, Collections.emptyMap(), Collections.emptyList(), Collections.emptyList()); ParseTree tree = parser.expression(); ASTBuilderVisitor v = new ASTBuilderVisitor(inputTypes); BaseNode expr = v.visit( tree ); return expr; }
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(FEELv12Profile.class::isInstance).forEach(dc -> { parserHelper.setFeatDMN12EnhancedForLoopEnabled(true); parserHelper.setFeatDMN12weekday(true); }); parser.setHelper(parserHelper); parser.setErrorHandler( new FEELErrorHandler() ); parser.removeErrorListeners(); // removes the error listener that prints to the console parser.addErrorListener( new FEELParserErrorListener( eventsManager ) ); // pre-loads the parser with symbols defineVariables( inputVariableTypes, inputVariables, parser ); return parser; }
@Test public void testVariableNameWithInvalidCharacterPercent() { String var = "?_873./-'%+*valid"; assertThat( FEELParser.isVariableNameValid( var ), is( false ) ); assertThat( FEELParser.checkVariableName( var ).get( 0 ).getMessage(), is( Msg.createMessage(Msg.INVALID_VARIABLE_NAME, "character", "%") ) ); }
private CompiledFEELExpression parse(String input, Map<String, Type> inputTypes) { FEEL_1_1Parser parser = FEELParser.parse(null, input, inputTypes, Collections.emptyMap(), Collections.emptyList(), Collections.emptyList()); ParseTree tree = parser.compilation_unit(); ASTBuilderVisitor v = new ASTBuilderVisitor(inputTypes); BaseNode node = v.visit(tree); DirectCompilerResult directResult = node.accept(new ASTCompilerVisitor()); Expression expr = directResult.getExpression(); CompiledFEELExpression cu = new CompilerBytecodeLoader().makeFromJPExpression(input, expr, directResult.getFieldDeclarations()); return cu; }
private static void defineVariables(Map<String, Type> inputVariableTypes, Map<String, Object> inputVariables, FEEL_1_1Parser parser) { inputVariableTypes.forEach( (name, type) -> { parser.getHelper().defineVariable( name, type ); if (type.getName() != null) { parser.getHelper().getSymbolTable().getGlobalScope().define(new BuiltInTypeSymbol(type.getName(), type)); } } ); inputVariables.forEach( (name, value) -> { parser.getHelper().defineVariable( name ); if( value instanceof Map ) { try { parser.getHelper().pushName( name ); parser.getHelper().pushScope(); defineVariables( Collections.EMPTY_MAP, (Map<String, Object>) value, parser ); } finally { parser.getHelper().popScope(); parser.getHelper().popName(); } } } ); }
@Test public void testVariableNameInvalidStartCharacter() { String var = "5variable can't start with a number"; assertThat( FEELParser.isVariableNameValid( var ), is( false ) ); assertThat( FEELParser.checkVariableName( var ).get( 0 ).getMessage(), is( Msg.createMessage(Msg.INVALID_VARIABLE_NAME_START, "character", "5") ) ); }
ProcessedFEELUnit(String expression, CompilerContext ctx, List<FEELProfile> profiles) { this.expression = expression; this.packageName = generateRandomPackage(); FEELEventListenersManager eventsManager = new FEELEventListenersManager(); eventsManager.addListeners(ctx.getListeners()); eventsManager.addListener(errorListener); this.parser = FEELParser.parse( eventsManager, expression, ctx.getInputVariableTypes(), ctx.getInputVariables(), ctx.getFEELFunctions(), profiles); }
@Test public void testVariableNameWithValidCharacters() { String var = "?_873./-'+*valid"; assertThat( FEELParser.isVariableNameValid( var ), is( true ) ); }
private CompiledFEELUnaryTests parse(String input, Map<String, Type> inputTypes, FEELEventListenersManager mgr, CompiledFEELSupport.SyntaxErrorListener listener) { FEEL_1_1Parser parser = FEELParser.parse(mgr, input, inputTypes, Collections.emptyMap(), Collections.emptyList(), Collections.emptyList()); ParseTree tree = parser.unaryTestsRoot(); DirectCompilerResult directResult; if (listener.isError()) { directResult = CompiledFEELSupport.compiledErrorUnaryTest(listener.event().getMessage()); } else { ASTBuilderVisitor v = new ASTBuilderVisitor(inputTypes); BaseNode node = v.visit(tree); BaseNode transformed = node.accept(new ASTUnaryTestTransform()).node(); directResult = transformed.accept(new ASTCompilerVisitor()); } Expression expr = directResult.getExpression(); CompiledFEELUnaryTests cu = new CompilerBytecodeLoader().makeFromJPUnaryTestsExpression(input, expr, directResult.getFieldDeclarations()); return cu; }
@Test public void testVariableName() { String var = "valid variable name"; assertThat( FEELParser.isVariableNameValid( var ), is( true ) ); }