You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2019/06/11 14:28:06 UTC
[plc4x] branch feature/code-gen updated: - Added a arithmetic
expression parser
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch feature/code-gen
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/feature/code-gen by this push:
new 56a750e - Added a arithmetic expression parser
56a750e is described below
commit 56a750ec842fc97462fa513ac7467aa9eeebce5b
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Tue Jun 11 16:27:56 2019 +0200
- Added a arithmetic expression parser
---
.../language/expressions/terms/BinaryTerm.java | 46 ++++
.../language/expressions/terms/BooleanLiteral.java | 34 +++
.../plc4x/language/expressions/terms/Literal.java | 23 ++
.../language/expressions/terms/NullLiteral.java | 27 +++
.../language/expressions/terms/NumericLiteral.java | 34 +++
.../language/expressions/terms/StringLiteral.java | 34 +++
.../plc4x/language/expressions/terms/Term.java | 24 ++
.../language/expressions/terms/TernaryTerm.java | 52 +++++
.../language/expressions/terms/UnaryTerm.java | 40 ++++
.../expressions/terms/VariableLiteral.java | 52 +++++
.../resources/templates/java/pojo-template.ftlh | 2 +-
sandbox/code-generation/plc4x-maven-plugin/pom.xml | 5 +
.../codegenerator/parser/expression/Expression.g4 | 198 +++++-----------
.../expression/ExpressionStringListener.java | 260 +++++++++++++++++++++
.../expression/ExpressionStringParser.java | 51 ++++
.../parser/ManualExpressionParserTest.java | 37 +++
16 files changed, 780 insertions(+), 139 deletions(-)
diff --git a/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/BinaryTerm.java b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/BinaryTerm.java
new file mode 100644
index 0000000..4ee54a2
--- /dev/null
+++ b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/BinaryTerm.java
@@ -0,0 +1,46 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.language.expressions.terms;
+
+public class BinaryTerm implements Term {
+
+ private final Term a;
+ private final Term b;
+ private final String operation;
+
+ public BinaryTerm(Term a, Term b, String operation) {
+ this.a = a;
+ this.b = b;
+ this.operation = operation;
+ }
+
+ public Term getA() {
+ return a;
+ }
+
+ public Term getB() {
+ return b;
+ }
+
+ public String getOperation() {
+ return operation;
+ }
+
+}
diff --git a/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/BooleanLiteral.java b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/BooleanLiteral.java
new file mode 100644
index 0000000..fe918dd
--- /dev/null
+++ b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/BooleanLiteral.java
@@ -0,0 +1,34 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.language.expressions.terms;
+
+public class BooleanLiteral implements Literal {
+
+ private final boolean value;
+
+ public BooleanLiteral(boolean value) {
+ this.value = value;
+ }
+
+ public boolean getValue() {
+ return value;
+ }
+
+}
diff --git a/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/Literal.java b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/Literal.java
new file mode 100644
index 0000000..4b8eff8
--- /dev/null
+++ b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/Literal.java
@@ -0,0 +1,23 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.language.expressions.terms;
+
+public interface Literal extends Term {
+}
diff --git a/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/NullLiteral.java b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/NullLiteral.java
new file mode 100644
index 0000000..788f80e
--- /dev/null
+++ b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/NullLiteral.java
@@ -0,0 +1,27 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.language.expressions.terms;
+
+public class NullLiteral implements Literal {
+
+ public NullLiteral() {
+ }
+
+}
diff --git a/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/NumericLiteral.java b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/NumericLiteral.java
new file mode 100644
index 0000000..062509a
--- /dev/null
+++ b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/NumericLiteral.java
@@ -0,0 +1,34 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.language.expressions.terms;
+
+public class NumericLiteral implements Literal {
+
+ private Number number;
+
+ public NumericLiteral(Number number) {
+ this.number = number;
+ }
+
+ public Number getNumber() {
+ return number;
+ }
+
+}
diff --git a/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/StringLiteral.java b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/StringLiteral.java
new file mode 100644
index 0000000..a6a40b2
--- /dev/null
+++ b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/StringLiteral.java
@@ -0,0 +1,34 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.language.expressions.terms;
+
+public class StringLiteral implements Literal {
+
+ private final String value;
+
+ public StringLiteral(String value) {
+ this.value = value;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+}
diff --git a/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/Term.java b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/Term.java
new file mode 100644
index 0000000..4858be7
--- /dev/null
+++ b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/Term.java
@@ -0,0 +1,24 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.language.expressions.terms;
+
+public interface Term {
+
+}
diff --git a/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/TernaryTerm.java b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/TernaryTerm.java
new file mode 100644
index 0000000..7a34f3d
--- /dev/null
+++ b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/TernaryTerm.java
@@ -0,0 +1,52 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.language.expressions.terms;
+
+public class TernaryTerm implements Term {
+
+ private final Term a;
+ private final Term b;
+ private final Term c;
+ private final String operation;
+
+ public TernaryTerm(Term a, Term b, Term c, String operation) {
+ this.a = a;
+ this.b = b;
+ this.c = c;
+ this.operation = operation;
+ }
+
+ public Term getA() {
+ return a;
+ }
+
+ public Term getB() {
+ return b;
+ }
+
+ public Term getC() {
+ return c;
+ }
+
+ public String getOperation() {
+ return operation;
+ }
+
+}
diff --git a/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/UnaryTerm.java b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/UnaryTerm.java
new file mode 100644
index 0000000..529c041
--- /dev/null
+++ b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/UnaryTerm.java
@@ -0,0 +1,40 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.language.expressions.terms;
+
+public class UnaryTerm implements Term {
+
+ private final Term a;
+ private final String operation;
+
+ public UnaryTerm(Term a, String operation) {
+ this.a = a;
+ this.operation = operation;
+ }
+
+ public Term getA() {
+ return a;
+ }
+
+ public String getOperation() {
+ return operation;
+ }
+
+}
diff --git a/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/VariableLiteral.java b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/VariableLiteral.java
new file mode 100644
index 0000000..cf2e330
--- /dev/null
+++ b/sandbox/code-generation/language-base/src/main/java/org/apache/plc4x/language/expressions/terms/VariableLiteral.java
@@ -0,0 +1,52 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.language.expressions.terms;
+
+public class VariableLiteral implements Literal {
+
+ public static final int NO_INDEX = -1;
+
+ private final String name;
+ private final int index;
+ private final VariableLiteral child;
+
+ public VariableLiteral(String name, int index, VariableLiteral child) {
+ this.name = name;
+ this.index = index;
+ this.child = child;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getIndex() {
+ return index;
+ }
+
+ public VariableLiteral getChild() {
+ return child;
+ }
+
+ public boolean isIndexed() {
+ return index != NO_INDEX;
+ }
+
+}
diff --git a/sandbox/code-generation/language-template-java/src/main/resources/templates/java/pojo-template.ftlh b/sandbox/code-generation/language-template-java/src/main/resources/templates/java/pojo-template.ftlh
index 45bdf89..3548b27 100644
--- a/sandbox/code-generation/language-template-java/src/main/resources/templates/java/pojo-template.ftlh
+++ b/sandbox/code-generation/language-template-java/src/main/resources/templates/java/pojo-template.ftlh
@@ -127,7 +127,7 @@ public<#if type.abstract> abstract</#if> class ${typeName}<#if type.parentType??
// Array field
<#if helper.isSimpleType(field.type)>
- lengthInBits += ${field.type.size} * ${field.name}.length;
+ lengthInBits += ${field.type.size} * ${field.name}.length;
<#else>
for(SizeAware element : ${field.name}) {
lengthInBits += element.getLengthInBytes() * 8;
diff --git a/sandbox/code-generation/plc4x-maven-plugin/pom.xml b/sandbox/code-generation/plc4x-maven-plugin/pom.xml
index 93454e3..343de65 100644
--- a/sandbox/code-generation/plc4x-maven-plugin/pom.xml
+++ b/sandbox/code-generation/plc4x-maven-plugin/pom.xml
@@ -60,6 +60,11 @@
<artifactId>antlr4-runtime</artifactId>
<version>${antlr.version}</version>
</dependency>
+ <dependency>
+ <groupId>commons-beanutils</groupId>
+ <artifactId>commons-beanutils</artifactId>
+ <version>1.9.3</version>
+ </dependency>
<dependency>
<groupId>org.apache.maven</groupId>
diff --git a/sandbox/code-generation/plc4x-maven-plugin/src/main/antlr4/org/apache/plc4x/codegenerator/parser/expression/Expression.g4 b/sandbox/code-generation/plc4x-maven-plugin/src/main/antlr4/org/apache/plc4x/codegenerator/parser/expression/Expression.g4
index f1a7721..9e022f3 100644
--- a/sandbox/code-generation/plc4x-maven-plugin/src/main/antlr4/org/apache/plc4x/codegenerator/parser/expression/Expression.g4
+++ b/sandbox/code-generation/plc4x-maven-plugin/src/main/antlr4/org/apache/plc4x/codegenerator/parser/expression/Expression.g4
@@ -1,143 +1,65 @@
grammar Expression;
-/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
-*/
expressionString
- : a=expression EOF
- ;
+ : expression EOF
+ ;
expression
- : boolExpr
- ;
-
-boolExpr
- : left=sumExpr (op=AND|OR|LT|LTEQ|GT|GTEQ|EQ|NOTEQ right=boolExpr)?
- ;
-
-sumExpr
- : left=productExpr (op=SUB|ADD right=sumExpr)?
- ;
-
-productExpr
- : left=expExpr (op=DIV|MULT right=productExpr)?
- ;
-
-expExpr
- : base=unaryOperation (op=EXP exponent=expExpr)?
- ;
-
-unaryOperation
- : a=operand
- | NOT e=expression
- | SUB e=expression
- | LPAREN e=expression RPAREN
- ;
-
-operand
- : l=literal
- | v=VARIABLE
- | f=functionExpr
- ;
-
-functionExpr
- : f=FUNCNAME LPAREN (a=arguments)? RPAREN
- ;
-
-arguments
- : a=expression (COMMA b=arguments)?
- ;
-
-literal
- : numeric=NUMBER
- | string=STRING
- | boolan=TRUE|FALSE
- ;
-
-STRING
- :
- '"' STRING_EXPRESSION* '"'
- |
- '\'' STRING_EXPRESSION* '\''
- ;
-
-STRING_EXPRESSION
- : ESCAPE_SEQUENCE
- | ~'\\'
- ;
-
-TRUE
- : ('t'|'T')('r'|'R')('u'|'U')('e'|'E')
- ;
-
-FALSE
- : ('f'|'F')('a'|'A')('l'|'L')('s'|'S')('e'|'E')
- ;
-
-
-NOTEQ : '!=';
-LTEQ : '<=';
-GTEQ : '>=';
-AND : '&&';
-OR : '||';
-NOT : '!';
-EQ : '==';
-LT : '<';
-GT : '>';
-
-EXP : '^';
-MULT : '*';
-DIV : '/';
-ADD : '+';
-SUB : '-';
-
-LPAREN : '(';
-RPAREN : ')';
-COMMA : ',';
-
-VARIABLE
- : '[' ~('[' | ']')+ ']'
- ;
-FUNCNAME
- : (LETTER)+
- ;
-NUMBER
- : (DIGIT)+ ('.' (DIGIT)+)?
- ;
-WHITESPACE
- : (' ' | '\n' | '\t' | '\r')+ -> skip
- ;
-
-fragment
-LETTER
- : ('a'..'z') | ('A'..'Z')
- ;
-
-fragment
-DIGIT
- : ('0'..'9')
- ;
-
-fragment
-ESCAPE_SEQUENCE
- : '\\' 't'
- | '\\' 'n'
- | '\\' '"'
- | '\\' '\''
- | '\\' '\\'
- ;
+ : '-' expression #unaryMinusExpression
+ | '!' expression #notExpression
+ | <assoc=right> expression '^' expression #powerExpression
+ | expression op=( '*' | '/' | '%' ) expression #multExpression
+ | expression op=( '+' | '-' ) expression #addExpression
+ | expression op=( '>=' | '<=' | '>' | '<' ) expression #compExpression
+ | expression op=( '==' | '!=' ) expression #eqExpression
+ | expression '&&' expression #andExpression
+ | expression '||' expression #orExpression
+ | expression '?' expression ':' expression #ifExpression
+ | Number #numberExpression
+ | Bool #boolExpression
+ | Null #nullExpression
+ | identifierSegment #identifierExpression
+ | String indexes? #stringExpression
+ | '(' expression ')' indexes? #expressionExpression
+ ;
+
+identifierSegment
+ : name=Identifier index=indexes? ('.' rest=identifierSegment)?
+ ;
+
+indexes
+ : ( '[' expression ']' )+
+ ;
+
+Null : 'null';
+
+Bool
+ : 'true'
+ | 'false'
+ ;
+
+Number
+ : Int ( '.' Digit* )?
+ ;
+
+Identifier
+ : [a-zA-Z_] [a-zA-Z_0-9]*
+ ;
+
+String
+ : ["] ( ~["\r\n\\] | '\\' ~[\r\n] )* ["]
+ | ['] ( ~['\r\n\\] | '\\' ~[\r\n] )* [']
+ ;
+
+Space
+ : [ \t\r\n\u000C] -> skip
+ ;
+
+fragment Int
+ : [1-9] Digit*
+ | '0'
+ ;
+
+fragment Digit
+ : [0-9]
+ ;
\ No newline at end of file
diff --git a/sandbox/code-generation/plc4x-maven-plugin/src/main/java/org/apache/plc4x/plugins/codegenerator/expression/ExpressionStringListener.java b/sandbox/code-generation/plc4x-maven-plugin/src/main/java/org/apache/plc4x/plugins/codegenerator/expression/ExpressionStringListener.java
new file mode 100644
index 0000000..444c507
--- /dev/null
+++ b/sandbox/code-generation/plc4x-maven-plugin/src/main/java/org/apache/plc4x/plugins/codegenerator/expression/ExpressionStringListener.java
@@ -0,0 +1,260 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.plugins.codegenerator.expression;
+
+import org.apache.plc4x.codegenerator.parser.expression.ExpressionBaseListener;
+import org.apache.plc4x.codegenerator.parser.expression.ExpressionParser;
+import org.apache.plc4x.language.expressions.terms.*;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Stack;
+
+public class ExpressionStringListener extends ExpressionBaseListener {
+
+ private Stack<List<Term>> parserContexts;
+
+ private Term root;
+
+ public Term getRoot() {
+ return root;
+ }
+
+ @Override
+ public void enterExpressionString(ExpressionParser.ExpressionStringContext ctx) {
+ parserContexts = new Stack<>();
+ parserContexts.push(new LinkedList<>());
+ }
+
+ @Override
+ public void exitExpressionString(ExpressionParser.ExpressionStringContext ctx) {
+ List<Term> roots = parserContexts.pop();
+ if(roots.size() != 1) {
+ throw new RuntimeException("Expression can only contain one root term.");
+ }
+ root = roots.get(0);
+ }
+
+ /////////////////////////////////////////////////////////////////////////////////////////
+ // Literals
+ /////////////////////////////////////////////////////////////////////////////////////////
+
+ @Override
+ public void exitNullExpression(ExpressionParser.NullExpressionContext ctx) {
+ parserContexts.peek().add(new NullLiteral());
+ }
+
+ @Override
+ public void exitBoolExpression(ExpressionParser.BoolExpressionContext ctx) {
+ parserContexts.peek().add(new BooleanLiteral(Boolean.valueOf(ctx.getText())));
+ }
+
+ @Override
+ public void exitNumberExpression(ExpressionParser.NumberExpressionContext ctx) {
+ String strValue = ctx.Number().getText();
+ if(strValue.contains(".")) {
+ parserContexts.peek().add(new NumericLiteral(Double.valueOf(strValue)));
+ } else {
+ parserContexts.peek().add(new NumericLiteral(Long.valueOf(strValue)));
+ }
+ }
+
+ @Override
+ public void exitStringExpression(ExpressionParser.StringExpressionContext ctx) {
+ parserContexts.peek().add(new StringLiteral(ctx.getText()));
+ }
+
+ @Override
+ public void exitIdentifierExpression(ExpressionParser.IdentifierExpressionContext ctx) {
+ parserContexts.peek().add(getVariableLiteral(ctx.identifierSegment()));
+ }
+
+ /////////////////////////////////////////////////////////////////////////////////////////
+ // Unary Terms
+ /////////////////////////////////////////////////////////////////////////////////////////
+
+ @Override
+ public void enterNotExpression(ExpressionParser.NotExpressionContext ctx) {
+ parserContexts.push(new LinkedList<>());
+ }
+
+ @Override
+ public void exitNotExpression(ExpressionParser.NotExpressionContext ctx) {
+ UnaryTerm ut = getUnaryTerm("!", parserContexts.pop());
+ parserContexts.peek().add(ut);
+ }
+
+ @Override
+ public void enterUnaryMinusExpression(ExpressionParser.UnaryMinusExpressionContext ctx) {
+ parserContexts.push(new LinkedList<>());
+ }
+
+ @Override
+ public void exitUnaryMinusExpression(ExpressionParser.UnaryMinusExpressionContext ctx) {
+ UnaryTerm ut = getUnaryTerm("-", parserContexts.pop());
+ parserContexts.peek().add(ut);
+ }
+
+ @Override
+ public void enterExpressionExpression(ExpressionParser.ExpressionExpressionContext ctx) {
+ parserContexts.push(new LinkedList<>());
+ }
+
+ @Override
+ public void exitExpressionExpression(ExpressionParser.ExpressionExpressionContext ctx) {
+ UnaryTerm ut = getUnaryTerm("()", parserContexts.pop());
+ parserContexts.peek().add(ut);
+ }
+
+ /////////////////////////////////////////////////////////////////////////////////////////
+ // Binary Terms
+ /////////////////////////////////////////////////////////////////////////////////////////
+
+ @Override
+ public void enterOrExpression(ExpressionParser.OrExpressionContext ctx) {
+ parserContexts.push(new LinkedList<>());
+ }
+
+ @Override
+ public void exitOrExpression(ExpressionParser.OrExpressionContext ctx) {
+ BinaryTerm bt = getBinaryTerm("||", parserContexts.pop());
+ parserContexts.peek().add(bt);
+ }
+
+ @Override
+ public void enterPowerExpression(ExpressionParser.PowerExpressionContext ctx) {
+ parserContexts.push(new LinkedList<>());
+ }
+
+ @Override
+ public void exitPowerExpression(ExpressionParser.PowerExpressionContext ctx) {
+ BinaryTerm bt = getBinaryTerm("^", parserContexts.pop());
+ parserContexts.peek().add(bt);
+ }
+
+ @Override
+ public void enterEqExpression(ExpressionParser.EqExpressionContext ctx) {
+ parserContexts.push(new LinkedList<>());
+ }
+
+ @Override
+ public void exitEqExpression(ExpressionParser.EqExpressionContext ctx) {
+ BinaryTerm bt = getBinaryTerm(ctx.op.getText(), parserContexts.pop());
+ parserContexts.peek().add(bt);
+ }
+
+ @Override
+ public void enterAndExpression(ExpressionParser.AndExpressionContext ctx) {
+ parserContexts.push(new LinkedList<>());
+ }
+
+ @Override
+ public void exitAndExpression(ExpressionParser.AndExpressionContext ctx) {
+ BinaryTerm bt = getBinaryTerm("&&", parserContexts.pop());
+ parserContexts.peek().add(bt);
+ }
+
+ @Override
+ public void enterAddExpression(ExpressionParser.AddExpressionContext ctx) {
+ parserContexts.push(new LinkedList<>());
+ }
+
+ @Override
+ public void exitAddExpression(ExpressionParser.AddExpressionContext ctx) {
+ BinaryTerm bt = getBinaryTerm(ctx.op.getText(), parserContexts.pop());
+ parserContexts.peek().add(bt);
+ }
+
+ @Override
+ public void enterCompExpression(ExpressionParser.CompExpressionContext ctx) {
+ parserContexts.push(new LinkedList<>());
+ }
+
+ @Override
+ public void exitCompExpression(ExpressionParser.CompExpressionContext ctx) {
+ BinaryTerm bt = getBinaryTerm(ctx.op.getText(), parserContexts.pop());
+ parserContexts.peek().add(bt);
+ }
+
+ @Override
+ public void enterMultExpression(ExpressionParser.MultExpressionContext ctx) {
+ parserContexts.push(new LinkedList<>());
+ }
+
+ @Override
+ public void exitMultExpression(ExpressionParser.MultExpressionContext ctx) {
+ BinaryTerm bt = getBinaryTerm(ctx.op.getText(), parserContexts.pop());
+ parserContexts.peek().add(bt);
+ }
+
+ /////////////////////////////////////////////////////////////////////////////////////////
+ // Ternary Terms
+ /////////////////////////////////////////////////////////////////////////////////////////
+
+ @Override
+ public void enterIfExpression(ExpressionParser.IfExpressionContext ctx) {
+ parserContexts.push(new LinkedList<>());
+ }
+
+ @Override
+ public void exitIfExpression(ExpressionParser.IfExpressionContext ctx) {
+ TernaryTerm tt = getTernaryTerm("if", parserContexts.pop());
+ parserContexts.peek().add(tt);
+ }
+
+ /////////////////////////////////////////////////////////////////////////////////////////
+ // Helpers
+ /////////////////////////////////////////////////////////////////////////////////////////
+
+ private VariableLiteral getVariableLiteral(ExpressionParser.IdentifierSegmentContext ctx) {
+ String name = ctx.name.getText();
+ int index = (ctx.index != null) ? Integer.valueOf(ctx.index.getText()) : VariableLiteral.NO_INDEX;
+ VariableLiteral child = (ctx.rest != null) ? getVariableLiteral(ctx.rest) : null;
+ return new VariableLiteral(name, index, child);
+ }
+
+ private UnaryTerm getUnaryTerm(String op, List<Term> terms) {
+ if(terms.size() != 1) {
+ throw new RuntimeException(op + " should be a unary operation");
+ }
+ Term a = terms.get(0);
+ return new UnaryTerm(a, op);
+ }
+
+ private BinaryTerm getBinaryTerm(String op, List<Term> terms) {
+ if(terms.size() != 2) {
+ throw new RuntimeException(op + " should be a binary operation");
+ }
+ Term a = terms.get(0);
+ Term b = terms.get(1);
+ return new BinaryTerm(a, b, op);
+ }
+
+ private TernaryTerm getTernaryTerm(String op, List<Term> terms) {
+ if(terms.size() != 3) {
+ throw new RuntimeException(op + " should be a ternary operation");
+ }
+ Term a = terms.get(0);
+ Term b = terms.get(1);
+ Term c = terms.get(1);
+ return new TernaryTerm(a, b, c, op);
+ }
+
+}
diff --git a/sandbox/code-generation/plc4x-maven-plugin/src/main/java/org/apache/plc4x/plugins/codegenerator/expression/ExpressionStringParser.java b/sandbox/code-generation/plc4x-maven-plugin/src/main/java/org/apache/plc4x/plugins/codegenerator/expression/ExpressionStringParser.java
new file mode 100644
index 0000000..18d8d69
--- /dev/null
+++ b/sandbox/code-generation/plc4x-maven-plugin/src/main/java/org/apache/plc4x/plugins/codegenerator/expression/ExpressionStringParser.java
@@ -0,0 +1,51 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.plugins.codegenerator.expression;
+
+import org.antlr.v4.runtime.CharStreams;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.tree.ParseTree;
+import org.antlr.v4.runtime.tree.ParseTreeWalker;
+import org.apache.plc4x.codegenerator.parser.expression.ExpressionLexer;
+import org.apache.plc4x.codegenerator.parser.expression.ExpressionParser;
+import org.apache.plc4x.language.expressions.terms.Term;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class ExpressionStringParser {
+
+
+ public Term parse(InputStream source) {
+ try {
+ ExpressionLexer lexer = new ExpressionLexer(CharStreams.fromStream(source));
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
+ ExpressionParser parser = new ExpressionParser(tokens);
+ ParseTree tree = parser.expressionString();
+ ParseTreeWalker walker = new ParseTreeWalker();
+ ExpressionStringListener listener = new ExpressionStringListener();
+ walker.walk(listener, tree);
+ return listener.getRoot();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
diff --git a/sandbox/code-generation/plc4x-maven-plugin/src/test/java/org/apache/plc4x/plugins/codegenerator/parser/ManualExpressionParserTest.java b/sandbox/code-generation/plc4x-maven-plugin/src/test/java/org/apache/plc4x/plugins/codegenerator/parser/ManualExpressionParserTest.java
new file mode 100644
index 0000000..0aa3412
--- /dev/null
+++ b/sandbox/code-generation/plc4x-maven-plugin/src/test/java/org/apache/plc4x/plugins/codegenerator/parser/ManualExpressionParserTest.java
@@ -0,0 +1,37 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.plugins.codegenerator.parser;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.plc4x.language.expressions.terms.Term;
+import org.apache.plc4x.plugins.codegenerator.expression.ExpressionStringParser;
+
+import java.io.InputStream;
+
+public class ManualExpressionParserTest {
+
+ public static void main(String[] args) {
+ InputStream inputStream = IOUtils.toInputStream("lengthInBytes - (payload.lengthInBytes + 1)");
+ ExpressionStringParser parser = new ExpressionStringParser();
+ Term term = parser.parse(inputStream);
+ System.out.println(term);
+ }
+
+}