Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
Address review comments
  • Loading branch information
GDLMadushanka committed Nov 13, 2024
1 parent f79e0df commit 94f735f
Show file tree
Hide file tree
Showing 34 changed files with 424 additions and 451 deletions.
Original file line number Diff line number Diff line change
@@ -1,58 +1,18 @@
lexer grammar ExpressionLexer;

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

JSONPATH_FUNCTIONS: 'length()' | 'size()' | 'min()' | 'max()' | 'avg()' | 'sum()' | 'stddev()' | 'keys()' | 'first()' | 'last()';

// 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' | '!';
NOT: '!';

DOUBLE_DOT : '..';
ASTERISK : '*';
Expand Down Expand Up @@ -110,8 +70,8 @@ NULL_LITERAL
;
// Identifiers
GETPROPERTY: 'getProperty';
ID: [a-zA-Z_][a-zA-Z_0-9]*;
// Special symbols for JSONPath filter expressions
QUESTION: '?';
AT: '@';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
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
;

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 ID propertyName)
;

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

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

jsonPathExpression
:( DOT JSONPATH_FUNCTIONS
|DOUBLE_DOT ASTERISK
| DOUBLE_DOT ID
| DOT ID
| (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 | ASTERISK | DIV ) expression)*
| ASTERISK
| QUESTION? filterExpression
;

multipleArrayIndices
: expression (COMMA expression)+
;

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

signedExpressions
: MINUS? expression
;

filterExpression
: (filterComponent)+
;

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

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


functionCall
: ID LPAREN (expression (COMMA expression)*)? RPAREN functionCallSuffix?
;

functionCallSuffix
: DOT ID LPAREN (expression (COMMA expression)*)? RPAREN // Method chaining
| jsonPathExpression
;
Loading

0 comments on commit 94f735f

Please sign in to comment.