Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce SIEL expression support #2227

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,118 @@
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';
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,193 @@
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)? )
;

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

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) | registryAccess )?
| 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
| ARRAY LPAREN expression RPAREN
| XPATH LPAREN expression RPAREN
;
Loading
Loading