-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
3f7d461
commit f79e0df
Showing
38 changed files
with
4,364 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
modules/core/src/main/antlr4/org/apache/synapse/util/synapse_path/ExpressionLexer.g4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
182 changes: 182 additions & 0 deletions
182
modules/core/src/main/antlr4/org/apache/synapse/util/synapse_path/ExpressionParser.g4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
; |
Oops, something went wrong.