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 Nov 8, 2024
1 parent 3f7d461 commit f79e0df
Show file tree
Hide file tree
Showing 38 changed files with 4,364 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
target/

## various other file types we don't want to have in the repository:
.DS_Store
.DS_Store
/modules/core/src/main/antlr4/org/apache/synapse/util/synapse_path/gen/
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,120 @@
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' | 'attr';
AXIS2: 'axis2';
SYNAPSE: 'synapse';
QUERY_PARAM: 'queryParams';
URI_PARAM: 'uriParams';
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';
OBJECT: 'object';
ARRAY: 'array';
XPATH: 'xpath';
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';
EXISTS: 'exists';
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
GETPROPERTY: 'getProperty';
ID: [a-zA-Z_][a-zA-Z_0-9]*;
// 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 propertyName
;

headerAccess
: HEADERS propertyName
;

attributeAccess
: ATTRIBUTES (DOT AXIS2 propertyName
| DOT SYNAPSE propertyName
| DOT QUERY_PARAM propertyName
| DOT URI_PARAM propertyName)
;

propertyName
: DOT ID
| (DOT)? LBRACKET STRING_LITERAL RBRACKET
;

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

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


variableAccess
: VAR ( DOT ID
| DOT STRING_LITERAL
| (DOT)? LBRACKET STRING_LITERAL RBRACKET // Bracket notation: var["variableName"]
)
( jsonPathExpression )?
;

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

payloadAccess
: PAYLOAD ( jsonPathExpression)?
;

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) | jsonPathExpression )?
| 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
| EXISTS LPAREN expression RPAREN
| OBJECT LPAREN expression RPAREN (jsonPathExpression)?
| ARRAY LPAREN expression RPAREN (LBRACKET arrayIndex RBRACKET)?
| XPATH LPAREN expression RPAREN
| NOT LPAREN expression RPAREN
;
Loading

0 comments on commit f79e0df

Please sign in to comment.