public ContextNode(ParserRuleContext ctx, ListNode list) { super( ctx ); for( BaseNode node : list.getElements() ) { ContextEntryNode entry = (ContextEntryNode) node; entries.add( entry ); parsedResultType.addField(entry.getName().getText(), entry.getResultType()); } }
@Override public BaseNode visitContextEntries(FEEL_1_1Parser.ContextEntriesContext ctx) { List<BaseNode> nodes = new ArrayList<>(); scopeHelper.pushScope(); for ( FEEL_1_1Parser.ContextEntryContext c : ctx.contextEntry() ) { ContextEntryNode visited = (ContextEntryNode) visit( c ); // forced cast similarly to visitFunctionDefinition() method if (visited != null) { nodes.add( visited ); scopeHelper.addType( visited.getName().getText() , visited.getResultType() ); } } scopeHelper.popScope(); return ASTBuilderFactory.newListNode( ctx, nodes ); }
@Test public void testNestedContexts2() { String inputExpression = "{ an applicant : { " + " home address : {" + " street name: \"broadway st\"," + " city : \"New York\" " + " } " + " },\n " + " street : an applicant.home address.street name \n" + "}"; BaseNode ctxbase = parse( inputExpression ); assertThat( ctxbase, is( instanceOf( ContextNode.class ) ) ); assertThat( ctxbase.getText(), is( inputExpression ) ); ContextNode ctx = (ContextNode) ctxbase; assertThat( ctx.getEntries().size(), is( 2 ) ); ContextEntryNode entry = ctx.getEntries().get( 1 ); assertThat( entry.getName(), is( instanceOf( NameDefNode.class ) ) ); assertThat( entry.getResultType(), is( BuiltInType.STRING ) ); NameDefNode name = (NameDefNode) entry.getName(); assertThat( name.getText(), is("street") ); assertThat( entry.getValue(), is( instanceOf( QualifiedNameNode.class ) ) ); QualifiedNameNode qnn = (QualifiedNameNode) entry.getValue(); assertThat( qnn.getParts().get( 0 ).getText(), is("an applicant") ); assertThat( qnn.getParts().get( 1 ).getText(), is("home address") ); assertThat( qnn.getParts().get( 2 ).getText(), is("street name") ); }
assertThat(nameNode.getText(), is("\"a string key\"")); // Reference String literal test, BaseNode#getText() return the FEEL equivalent expression, in this case quoted. assertThat( entry.getValue(), is( instanceOf( NumberNode.class ) ) ); assertThat( entry.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( entry.getValue().getText(), is("10") ); assertThat( entry.getName().getText(), is("a non-string key") ); assertThat( entry.getValue(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( entry.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( entry.getValue().getText(), is( "foo+bar" ) ); assertThat( entry.getName().getText(), is("a key.with + /' odd chars") ); assertThat( entry.getValue(), is( instanceOf( RangeNode.class ) ) ); assertThat( entry.getResultType(), is( BuiltInType.RANGE ) ); assertThat( entry.getValue().getText(), is( "[10..50]" ) );
assertThat( name.getText(), is("a value") ); assertThat( entry.getValue(), is( instanceOf( NumberNode.class ) ) ); assertThat( entry.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( entry.getValue().getText(), is("10") ); assertThat( applicant.getEntries().size(), is( 5 ) ); assertThat( applicant.getEntries().get( 0 ).getName().getText(), is("first name") ); assertThat( applicant.getEntries().get( 0 ).getResultType(), is( BuiltInType.STRING ) ); assertThat( applicant.getEntries().get( 1 ).getName().getText(), is("last + name") ); assertThat( applicant.getEntries().get( 1 ).getResultType(), is( BuiltInType.STRING ) ); assertThat( applicant.getEntries().get( 2 ).getName().getText(), is("full name") ); assertThat( applicant.getEntries().get( 2 ).getResultType(), is( BuiltInType.STRING ) ); assertThat( applicant.getEntries().get( 3 ).getName().getText(), is("address") ); assertThat( applicant.getEntries().get( 3 ).getValue(), is( instanceOf( ContextNode.class ) ) ); assertThat( address.getEntries().size(), is( 2 ) ); assertThat( address.getEntries().get( 0 ).getName().getText(), is("street") ); assertThat( address.getEntries().get( 0 ).getResultType(), is( BuiltInType.STRING ) ); assertThat( address.getEntries().get( 1 ).getName().getText(), is("city") ); assertThat( address.getEntries().get( 0 ).getResultType(), is( BuiltInType.STRING ) );
assertThat( entry.getName().getText(), is("a variable with in keyword") ); assertThat( entry.getValue(), is( instanceOf( NumberNode.class ) ) ); assertThat( entry.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( entry.getValue().getText(), is("10") ); assertThat( entry.getName().getText(), is("another variable") ); assertThat( entry.getValue(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( entry.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( entry.getValue().getText(), is( "a variable with in keyword + 20" ) ); assertThat( entry.getName().getText(), is("another in variable") ); assertThat( entry.getValue(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( entry.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( entry.getValue().getText(), is( "an external in variable / 2" ) );