private String getFunctionName(BaseNode name) { String functionName = null; if ( name instanceof NameRefNode ) { // simple name functionName = name.getText(); } else if (name instanceof QualifiedNameNode) { QualifiedNameNode qn = (QualifiedNameNode) name; functionName = qn.getParts().stream().map( p -> p.getText() ).collect( Collectors.joining( " ") ); } return functionName; }
public UnaryTestNode( String op, BaseNode value ) { super(); setText( op+" "+value.getText() ); this.operator = UnaryOperator.determineOperator( op ); this.value = value; }
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()); } }
private Object fetchValue(Object o) throws IllegalAccessException, InvocationTargetException { if ( name instanceof NameRefNode ) { o = EvalHelper.getValue( o, name.getText() ); } else if ( name instanceof QualifiedNameNode ) { for ( NameRefNode nr : ((QualifiedNameNode) name).getParts() ) { o = EvalHelper.getValue( o, nr.getText() ); } } return o; }
@Test public void testIfExpression() { String inputExpression = "if applicant.age < 18 then \"declined\" else \"accepted\""; BaseNode ifBase = parse( inputExpression ); assertThat( ifBase, is( instanceOf( IfExpressionNode.class ) ) ); assertThat( ifBase.getText(), is( inputExpression ) ); assertThat( ifBase.getResultType(), is( BuiltInType.STRING ) ); IfExpressionNode ifExpr = (IfExpressionNode) ifBase; assertThat( ifExpr.getCondition().getText(), is( "applicant.age < 18" ) ); assertThat( ifExpr.getThenExpression().getText(), is( "\"declined\"" ) ); assertThat( ifExpr.getElseExpression().getText(), is( "\"accepted\"" ) ); }
@Test public void testPathExpression() { String inputExpression = "[ 10, 15 ].size"; BaseNode pathBase = parse( inputExpression ); assertThat( pathBase, is( instanceOf( PathExpressionNode.class ) ) ); assertThat( pathBase.getText(), is( inputExpression ) ); PathExpressionNode pathExpr = (PathExpressionNode) pathBase; assertThat( pathExpr.getExpression(), is( instanceOf( ListNode.class ) ) ); assertThat( pathExpr.getExpression().getText(), is( "10, 15" ) ); assertThat( pathExpr.getName(), is( instanceOf( NameRefNode.class ) ) ); assertThat( pathExpr.getName().getText(), is( "size" ) ); }
@Test public void testFilterExpression() { String inputExpression = "[ {x:1, y:2}, {x:2, y:3} ][ x=1 ]"; BaseNode filterBase = parse( inputExpression ); assertThat( filterBase, is( instanceOf( FilterExpressionNode.class ) ) ); assertThat( filterBase.getText(), is( inputExpression ) ); FilterExpressionNode filter = (FilterExpressionNode) filterBase; assertThat( filter.getExpression(), is( instanceOf( ListNode.class ) ) ); assertThat( filter.getExpression().getText(), is( "{x:1, y:2}, {x:2, y:3}" ) ); assertThat( filter.getFilter(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( filter.getFilter().getText(), is( "x=1" ) ); }
@Test public void testEmptyContext() { String inputExpression = "{}"; BaseNode context = parse( inputExpression ); assertThat( context, is( instanceOf( ContextNode.class ) ) ); assertThat( context.getText(), is( inputExpression ) ); ContextNode ctx = (ContextNode) context; assertThat( ctx.getEntries(), is( empty() )); }
@Test public void testFunctionInvocationEmptyParams() { String inputExpression = "my.test.Function()"; BaseNode functionBase = parse( inputExpression ); assertThat( functionBase, is( instanceOf( FunctionInvocationNode.class ) ) ); assertThat( functionBase.getText(), is( inputExpression ) ); FunctionInvocationNode function = (FunctionInvocationNode) functionBase; assertThat( function.getName(), is( instanceOf( QualifiedNameNode.class ) ) ); assertThat( function.getName().getText(), is( "my.test.Function" ) ); assertThat( function.getParams(), is( instanceOf( ListNode.class ) ) ); assertThat( function.getParams().getElements(), is( empty() ) ); }
@Test public void testParensWithLiteral() { String inputExpression = "(10.5 )"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( NumberNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( number.getText(), is( "10.5" ) ); }
@Test public void testEmptyList() { String inputExpression = "[]"; BaseNode list = parse( inputExpression ); assertThat( list, is( instanceOf( ListNode.class ) ) ); assertThat( list.getResultType(), is( BuiltInType.LIST ) ); assertThat( list.getText(), is( inputExpression ) ); ListNode ln = (ListNode) list; assertThat( ln.getElements(), is( empty() )); }
@Test public void testLogicalNegation() { String inputExpression = "not ( true )"; BaseNode neg = parse( inputExpression ); assertThat( neg, is( instanceOf( FunctionInvocationNode.class ) ) ); assertThat( neg.getResultType(), is( BuiltInType.UNKNOWN ) ); assertThat( neg.getText(), is( "not ( true )" ) ); FunctionInvocationNode not = (FunctionInvocationNode) neg; assertThat( not.getParams().getElements().get( 0 ), is( instanceOf( BooleanNode.class ) ) ); assertThat( not.getParams().getElements().get( 0 ).getResultType(), is( BuiltInType.BOOLEAN ) ); assertThat( not.getParams().getElements().get( 0 ).getText(), is( "true" ) ); }
@Test public void testStringLiteral() { String inputExpression = "\"some string\""; BaseNode stringLit = parse( inputExpression ); assertThat( stringLit, is( instanceOf( StringNode.class ) ) ); assertThat( stringLit.getResultType(), is( BuiltInType.STRING ) ); assertLocation( inputExpression, stringLit ); assertThat(stringLit.getText(), is(inputExpression)); }
private void assertLocation(String inputExpression, BaseNode number) { assertThat( number.getText(), is( inputExpression ) ); assertThat( number.getStartChar(), is( 0 ) ); assertThat( number.getStartLine(), is( 1 ) ); assertThat( number.getStartColumn(), is( 0 ) ); assertThat( number.getEndChar(), is( inputExpression.length() - 1 ) ); assertThat( number.getEndLine(), is( 1 ) ); assertThat( number.getEndColumn(), is( inputExpression.length() ) ); }
@Test public void testNegativeIntegerLiteral() { String inputExpression = "-10"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( SignedUnaryNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); SignedUnaryNode sun = (SignedUnaryNode) number; assertThat( sun.getSign(), is( SignedUnaryNode.Sign.NEGATIVE ) ); assertThat( sun.getExpression(), is( instanceOf( NumberNode.class ) ) ); assertThat( sun.getExpression().getText(), is( "10" ) ); }
@Test public void testPositiveFloatLiteral() { String inputExpression = "+10.5"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( SignedUnaryNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); SignedUnaryNode sun = (SignedUnaryNode) number; assertThat( sun.getSign(), is( SignedUnaryNode.Sign.POSITIVE ) ); assertThat( sun.getExpression(), is( instanceOf( NumberNode.class ) ) ); assertThat( sun.getExpression().getText(), is( "10.5" ) ); }
@Test public void testPositiveIntegerLiteral() { String inputExpression = "+10"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( SignedUnaryNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); SignedUnaryNode sun = (SignedUnaryNode) number; assertThat( sun.getSign(), is( SignedUnaryNode.Sign.POSITIVE ) ); assertThat( sun.getExpression(), is( instanceOf( NumberNode.class ) ) ); assertThat( sun.getExpression().getText(), is( "10" ) ); }
@Test public void testNegativeFloatLiteral() { String inputExpression = "-10.5"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( SignedUnaryNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); SignedUnaryNode sun = (SignedUnaryNode) number; assertThat( sun.getSign(), is( SignedUnaryNode.Sign.NEGATIVE ) ); assertThat( sun.getExpression(), is( instanceOf( NumberNode.class ) ) ); assertThat( sun.getExpression().getText(), is( "10.5" ) ); }
public static DirectCompilerResult declaration(FunctionDefNode n, MethodCallExpr list, Expression fnBody) { LambdaExpr lambda = Expressions.lambda(fnBody); String fnName = Constants.functionName(n.getBody().getText()); DirectCompilerResult r = DirectCompilerResult.of( Functions.internal(list, new NameExpr(fnName)), BuiltInType.FUNCTION); r.addFieldDesclaration(Constants.function(fnName, lambda)); return r; }
@Override public DirectCompilerResult visit(FilterExpressionNode n) { DirectCompilerResult expr = n.getExpression().accept(this); DirectCompilerResult filter = n.getFilter().accept(this); Expressions.NamedLambda lambda = Expressions.namedLambda(filter.getExpression(), n.getFilter().getText()); DirectCompilerResult r = DirectCompilerResult.of( Expressions.filter(expr.getExpression(), lambda.name()), // here we could still try to infer the result type, but presently use ANY BuiltInType.UNKNOWN).withFD(expr).withFD(filter); r.addFieldDesclaration(lambda.field()); return r; }