Grammar and Keywords
The grammar of a constraint expression, from its characters to its operators, and the words it reserves. The grammar uses ANTLR4 notation.
Lexical Grammar
A token is the longest text one of these rules reads.
A quoted word of the syntactic grammar is a reserved word, which no NAME and no UNIT_WORD reads.
WS : [\p{White_Space}]+ -> skip ;
NUMBER : DECIMAL | HEX ;
QUANTITY : DECIMAL ' '? ( UNIT_WORD | STRING ) ;
STRING : '\'' ( ~['\\] | ESCAPE )* '\''
| '"' ( ~["\\] | ESCAPE )* '"' ;
VARIABLE : '$' [A-Za-z0-9_]* ;
ANNOTATION : '_' [A-Za-z0-9_]+ ;
NAME : [A-Za-z] [A-Za-z0-9_]* ;
fragment DIGITS : [0-9] ( '_'? [0-9] )* ;
fragment HEX_DIGITS : [0-9A-Fa-f] ( '_'? [0-9A-Fa-f] )* ;
fragment DECIMAL : ( '0' | [1-9] ( '_'? [0-9] )* ) ( '.' DIGITS )? ( [eE] [+-]? DIGITS )? ;
fragment HEX : '0' [xX] HEX_DIGITS ;
fragment UNIT_WORD : [\p{Alphabetic}] [\p{Alphabetic}\p{N}_]* ;
fragment ESCAPE : '\\' ( [\\'"/ntr] | 'u' [0-9A-Fa-f] [0-9A-Fa-f] [0-9A-Fa-f] [0-9A-Fa-f] ) ;
-
A
_groups digits only between two of them,1_000, a number reads with every_removed. -
A unit word takes Unicode letters and digits, so
µsandkΩare unit words, a name ASCII ones only. Anewith digits behind it is an exponent,1e3sis the number1e3and the units. -
A unit that is not a unit word is written as a string,
50 '%', see Units. -
The escapes of a string are listed under Literals.
-
$alone is the context,$instancethe instance,$$the configuration root and%the parent, see Context. -
The operators and punctuation are listed under Operators and Punctuation.
Syntactic Grammar
expression : orExpression ( '?' expression ':' expression )? ;
orExpression : andExpression ( ( '||' | 'or' ) andExpression )* ;
andExpression : comparisonExpression ( ( '&&' | 'and' ) comparisonExpression )* ;
comparisonExpression : coalesceExpression ( comparisonOperator coalesceExpression )* ;
comparisonOperator : '==' | '=' | '!=' | '<' | '<=' | '>' | '>=' | 'in' ;
coalesceExpression : additiveExpression ( ( '??' | '?:' ) coalesceExpression )? ;
additiveExpression : multiplicativeExpression ( ( '+' | '-' ) multiplicativeExpression )* ;
multiplicativeExpression : unaryExpression ( ( '*' | '/' | '%' ) unaryExpression )* ;
unaryExpression : ( '-' | '!' ) unaryExpression | postfixExpression ;
postfixExpression : primaryExpression ( '.' step | '[' expression ']' )* ;
step : NAME | reserved | ANNOTATION | '%' | '(' block ')' ;
primaryExpression : QUANTITY | NUMBER | STRING | 'true' | 'false' | NAME | ANNOTATION
| '$$' | '%' | VARIABLE | call | '(' block ')' | array ;
block : ( VARIABLE ':=' expression ';' )* expression ;
call : VARIABLE '(' ( argument ( ',' argument )* )? ')' ;
argument : lambda | expression ;
lambda : VARIABLE '=>' expression
| 'function' '(' VARIABLE ')' '{' expression '}' ;
array : '[' ( item ( ',' item )* )? ']' ;
item : expression ( '..' expression )? ;
reserved : 'in' | 'and' | 'or' | 'true' | 'false' | 'function' | 'null' ;
-
A
?and its:group from the right,a ? b : c ? d : eisa ? b : (c ? d : e). -
A
-takes no second sign and no hex number,--xand-0x10are definition errors. -
( )holding one expression groups it, so an index after it picks from the whole list:(tasks.subtasks)[0].( )after a.maps the expression over the elements,tasks.(runtime / period). -
[ ]after a step filters its elements by a condition or picks one by an integer literal,tasks[0], a negative one counting from the end. -
A lambda is an argument of
$every,$some,$single,$filterand$noOverlaponly, its body runs to the end of the argument. -
A range
a..bstands in an array only and takes integer literal bounds,[1..4]. -
A
:=binding stands in a block only, before the last expression.
The precedence of the operators:
From the tightest to the loosest binding, the operators of one level group from left to right, ??, ?: and ? : from right to left, a chain of && or || as a balanced tree, which reads its operands from left to right and gives the same result.
A float result rounds after each operation in that grouping, so a + b + c rounds a + b first.
| Operators | Example |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Reserved Words
A reserved word is no property name and no unit.
The property of such a name reads as a step on the context, $.in.
| Word | Meaning |
|---|---|
|
Membership, |
|
Logical and, |
|
Logical or, |
|
A boolean literal. A property of this name reads as |
|
A boolean literal. A property of this name reads as |
|
JSONata’s lambda form, |
|
Not in the constraint language, an absent value is tested with |
Operators and Punctuation
| Token | Meaning |
|---|---|
|
A path step, |
|
A range in an array, |
|
Separates array items and call arguments. |
|
Ends a binding in a block. |
|
A block or a group, after a |
|
An array, after a step a filter or an index, |
|
The body of a |
|
Binds a variable in a block, |
|
The conditional, |
|
|
|
|
|
A lambda, |
|
Arithmetic, |
|
The remainder between two values, |
|
Logical not. |
|
Logical and, or, also spelled |
|
Equality, |
|
Order. |