-
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
695642e
commit 0222047
Showing
34 changed files
with
3,899 additions
and
8 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
114 changes: 114 additions & 0 deletions
114
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,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; |
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 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 | ||
; |
Oops, something went wrong.