Skip to content

Commit

Permalink
Introduce SIEL expression support
Browse files Browse the repository at this point in the history
Add new synapse path type called SIEL to evaluate complex expressions
which are not supported in either JSONPath or XPATH.
Fixes wso2/product-micro-integrator/issues/3749
  • Loading branch information
GDLMadushanka committed Oct 31, 2024
1 parent 695642e commit 0222047
Show file tree
Hide file tree
Showing 34 changed files with 3,899 additions and 8 deletions.
42 changes: 42 additions & 0 deletions modules/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,44 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<executions>
<execution>
<id>antlr</id>
<phase>generate-sources</phase>
<goals>
<goal>antlr4</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/antlr4</sourceDirectory>
<arguments>
<argument>-visitor</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<!-- Build Helper Maven Plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/antlr4</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

<!-- Attach a JAR with the test classes so that we can reuse them in other
modules (see http://maven.apache.org/guides/mini/guide-attached-tests.html). -->
Expand Down Expand Up @@ -506,5 +544,9 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
lexer grammar ExpressionLexer;

JSONPATH_FUNCTIONS: 'contains ' | 'in' | 'nin' | 'subsetof' | 'size' | 'empty' | 'empty true' | 'empty false' | '=~';

// Tokens for identifiers, operators, and keywords
VAR: 'var';
PAYLOAD: 'payload' | '$';
HEADERS: 'headers';
CONFIG: 'config';
ATTRIBUTES: 'attributes';
AXIS2: 'axis2';
SYNAPSE: 'synapse';
REGISTRY: 'registry';
SECRET: 'secret';
BASE64ENCODE: 'base64encode';
BASE64DECODE: 'base64decode';
URLENCODE: 'urlEncode';
URLDECODE: 'urlDecode';
NOW: 'now';
TODAY: 'today';
FORMATDATE: 'formatDate';
ISNUMBER: 'isNumber';
ISSTRING: 'isString';
ISARRAY: 'isArray';
ISOBJECT: 'isObject';
ROUND: 'round';
INTEGER: 'integer';
FLOAT: 'float';
STRING: 'string';
BOOLEAN: 'boolean';
ABS: 'abs';
FLOOR: 'floor';
CEIL: 'ceil';
SQRT: 'sqrt';
LOG: 'log';
POW: 'pow';
LENGTH: 'length';
TOUPPER: 'toUpper';
TOLOWER: 'toLower';
SUBSTRING: 'subString';
STARTSWITH: 'startsWith';
ENDSWITH: 'endsWith';
CONTAINS: 'contains';
TRIM: 'trim';
REPLACE: 'replace';
SPLIT: 'split';
AND: 'and' | '&&';
OR: 'or' | '||';
NOT: 'not' | '!';

DOUBLE_DOT : '..';
ASTERISK : '*';

// Operators
PLUS: '+';
MINUS: '-';
DIV: '/';
MODULO: '%';
EQ: '==';
NEQ: '!=';
GT: '>';
LT: '<';
GTE: '>=';
LTE: '<=';

// Delimiters
LPAREN: '(';
RPAREN: ')';
LBRACKET: '[';
RBRACKET: ']';
DOT: '.';
COMMA: ',';
COLON: ':';
QUOTE: '"' | '\'';

// Literals
BOOLEAN_LITERAL: 'true' | 'false';
NUMBER: '-'? [0-9]+ ('.' [0-9]+)?;


STRING_LITERAL : ('"' (ESC | ~["\\])* '"' | '\'' (ESC | ~['\\])* '\'');


fragment ESC
: '\\' [btnfr"'\\/] // Basic escape sequences
| UNICODE_ESC
| OCTAL_ESC
;
fragment UNICODE_ESC
: '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
fragment OCTAL_ESC
: '\\' [0-3]? [0-7] [0-7]
;
fragment HEX_DIGIT
: [0-9a-fA-F]
;
NULL_LITERAL
: 'null' // Define null as a recognized keyword
;
// Identifiers
ID: [a-zA-Z_][a-zA-Z_0-9]*;
GETPROPERTY: 'getProperty';
// Special symbols for JSONPath filter expressions
QUESTION: '?';
AT: '@';
// Whitespace
WS: [ \t\n\r]+ -> skip;
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
parser grammar ExpressionParser;

options {
tokenVocab = ExpressionLexer;
}

expression
: comparisonExpression
| conditionalExpression
| EOF
;

conditionalExpression
: comparisonExpression (QUESTION expression COLON expression)?
;

comparisonExpression
: logicalExpression ( (GT | LT | GTE | LTE | EQ | NEQ) logicalExpression )*
| logicalExpression (EQ | NEQ) NULL_LITERAL // Allow comparison to null
;

logicalExpression
: arithmeticExpression (AND logicalExpression | OR logicalExpression)?
;

arithmeticExpression
: term ( (PLUS | MINUS) term )*
;

term
: factor ( (ASTERISK | DIV | MODULO) factor )*
;

factor
: literal
| functionCall
| variableAccess
| payloadAccess
| headerAccess
| configAccess
| attributeAccess
| LPAREN expression RPAREN
;

configAccess
: CONFIG DOT headerName
;

headerAccess
: HEADERS DOT headerName
;

headerName
: ID // Standard identifier
| STRING_LITERAL // Quoted string literal to allow spaces
;

attributeAccess
: ATTRIBUTES (DOT AXIS2 DOT propertyName // Access syntax for attributes.axis2.property
| DOT SYNAPSE DOT propertyName) // Access syntax for attributes.synapse.property
;

propertyName
: ID // Standard identifier
| STRING_LITERAL // Quoted string literal to allow spaces
;

literal
: arrayLiteral
| BOOLEAN_LITERAL
| NUMBER
| STRING_LITERAL
| NULL_LITERAL
;

variableAccess
: VAR ( DOT ID // Dot notation: var.variableName
| (DOT)? LBRACKET STRING_LITERAL RBRACKET // Bracket notation: var["variableName"]
)
( (DOUBLE_DOT ASTERISK // Handles recursive descent and wildcard, like var.variableName..*
| DOT ASTERISK // Handles wildcard after dot notation, like var.variableName.*
| DOT ID
| LBRACKET arrayIndex RBRACKET // Handles array access, like var.variableName[...]
| DOT LBRACKET arrayIndex RBRACKET // Handles dot followed by bracket notation, like var.variableName.["property"]
)*
)
;

arrayLiteral
: LBRACKET (expression (COMMA expression)*)? RBRACKET // Array with zero or more literals, separated by commas
;

payloadAccess
: PAYLOAD ( (DOUBLE_DOT ASTERISK
| DOUBLE_DOT ID
| DOT ID
| LBRACKET arrayIndex RBRACKET
| DOT LBRACKET arrayIndex RBRACKET
| DOT ASTERISK)*
| DOUBLE_DOT ID (LBRACKET arrayIndex RBRACKET)? )
;

arrayIndex
: NUMBER
| STRING_LITERAL
| expression
| multipleArrayIndices
| sliceArrayIndex
| expression ( (PLUS | MINUS | MULT | DIV ) expression)*
| ASTERISK
| QUESTION? filterExpression
;

multipleArrayIndices
: expression (COMMA expression)+
;

sliceArrayIndex
: signedExpressions? COLON signedExpressions? (COLON signedExpressions?)?
;

signedExpressions
: MINUS? expression
;

filterExpression
: (filterComponent)+
;

filterComponent
: variableAccess
| payloadAccess
| stringOrOperator
| headerAccess
| configAccess
| attributeAccess
| functionCall
;

stringOrOperator
: QUESTION | AT | JSONPATH_FUNCTIONS| STRING_LITERAL |NUMBER | BOOLEAN_LITERAL | ID | GT | LT | GTE | LTE | EQ | NEQ
| PLUS | MINUS | MULT | DIV | LPAREN | RPAREN | DOT | COMMA | COLON | WS | AND | OR | NOT | ASTERISK
;



functionCall
: LENGTH LPAREN expression RPAREN
| TOUPPER LPAREN expression RPAREN
| TOLOWER LPAREN expression RPAREN
| SUBSTRING LPAREN expression COMMA expression (COMMA expression)? RPAREN
| STARTSWITH LPAREN expression COMMA expression RPAREN
| ENDSWITH LPAREN expression COMMA expression RPAREN
| CONTAINS LPAREN expression COMMA expression RPAREN
| TRIM LPAREN expression RPAREN
| REPLACE LPAREN expression COMMA expression COMMA expression RPAREN
| SPLIT LPAREN expression COMMA expression RPAREN
| ABS LPAREN expression RPAREN
| FLOOR LPAREN expression RPAREN
| CEIL LPAREN expression RPAREN
| SQRT LPAREN expression RPAREN
| LOG LPAREN expression RPAREN
| POW LPAREN expression COMMA expression RPAREN
| REGISTRY LPAREN expression RPAREN (DOT GETPROPERTY LPAREN expression RPAREN)?
| SECRET LPAREN expression RPAREN
| BASE64ENCODE LPAREN expression (COMMA expression)? RPAREN
| BASE64DECODE LPAREN expression RPAREN
| URLENCODE LPAREN expression (COMMA expression)? RPAREN
| URLDECODE LPAREN expression RPAREN
| ISNUMBER LPAREN expression RPAREN
| ISSTRING LPAREN expression RPAREN
| ISARRAY LPAREN expression RPAREN
| ISOBJECT LPAREN expression RPAREN
| NOW LPAREN RPAREN
| TODAY LPAREN STRING_LITERAL RPAREN
| FORMATDATE LPAREN expression COMMA STRING_LITERAL RPAREN
| ROUND LPAREN expression RPAREN
| INTEGER LPAREN expression RPAREN
| FLOAT LPAREN expression RPAREN
| STRING LPAREN expression RPAREN
| BOOLEAN LPAREN expression RPAREN
;
Loading

0 comments on commit 0222047

Please sign in to comment.