ExpressionStringParser { public Term parse(InputStream source) { ExpressionLexer lexer; try { lexer = new ExpressionLexer(CharStreams.fromStream(source)); } catch (IOException e) { throw new RuntimeException(e); } ExpressionStringListener listener = new ExpressionStringListener(); new ParseTreeWalker().walk(listener, new ExpressionParser(new CommonTokenStream(lexer)).expressionString()); return listener.getRoot(); }  Term parse(InputStream source);  }
@Test void parseSimple() { Term term = SUT.parse(IOUtils.toInputStream("payload.lengthInBytes+4", Charset.defaultCharset())); assertThat(term, not(nullValue())); assertThat(term, instanceOf(BinaryTerm.class)); BinaryTerm binaryTerm = (BinaryTerm) term; assertVariableLiteral( binaryTerm.getA(), "payload", null, lengthInBytesVariableLiteral -> assertVariableLiteral( lengthInBytesVariableLiteral, "lengthInBytes" ) ); assertNumericLiteral(binaryTerm.getB(), 4L); assertThat(binaryTerm.getOperation(), is("+")); }
@Test void parseDoubleCast() { Term term = SUT.parse(IOUtils.toInputStream("CAST(CAST(parameter,S7ParameterUserData).items(hurz)[0],S7ParameterUserDataItemCPUFunctions).cpuFunctionType", Charset.defaultCharset())); assertVariableLiteral( term, "CAST", outerCast -> { assertThat(outerCast, hasSize(2)); assertVariableLiteral( outerCast.get(0), "CAST", innerCast -> { assertThat(innerCast, hasSize(2)); assertVariableLiteral( innerCast.get(0), "parameter" ); assertVariableLiteral( innerCast.get(1), "S7ParameterUserData" ); }, items -> { assertVariableLiteral( items, "items", hurzes -> { assertThat(hurzes, hasSize(1)); assertVariableLiteral( hurzes.get(0), "hurz" ); }, null, 0 ); }); assertVariableLiteral( outerCast.get(1), "S7ParameterUserDataItemCPUFunctions" ); }, variableLiteral -> { assertVariableLiteral( variableLiteral, "cpuFunctionType" ); } ); }
@Test void parseCast() { Term term = SUT.parse(IOUtils.toInputStream("CAST(parameter,S7ParameterUserData).items(hurz)[0]", Charset.defaultCharset())); assertVariableLiteral( term, "CAST", terms -> { assertThat(terms, hasSize(2)); assertVariableLiteral( terms.get(0), "parameter" ); assertVariableLiteral( terms.get(1), "S7ParameterUserData" ); }, variableLiteral -> { assertVariableLiteral( variableLiteral, "items", terms -> { assertThat(terms, hasSize(1)); assertVariableLiteral( terms.get(0), "hurz" ); }, null, 0 ); } ); }