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 µs and kΩ are unit words, a name ASCII ones only. An e with digits behind it is an exponent, 1e3s is the number 1e3 and the unit s.

  • 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, $instance the 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 : e is a ? b : (c ? d : e).

  • A - takes no second sign and no hex number, --x and -0x10 are 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, $filter and $noOverlap only, its body runs to the end of the argument.

  • A range a..b stands 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

( ), $f( )

(a || b) && c

., [ ]

-a.b is -(a.b)

!, -

!a == b is (!a) == b, -a * b is (-a) * b

*, /, %

a - b * c is a - (b * c)

+, -

a - b - c is (a - b) - c, a + b + c + d is ((a + b) + c) + d

??, ?:

x ?? 0 > 5 is (x ?? 0) > 5, x ?? y + 1 is x ?? (y + 1), x ?? y ?: z is x ?? (y ?: z)

==, !=, <, <=, >, >=, in

a < b == c is (a < b) == c

&&, and

a || b && c is a || (b && c)

||, or

a || b ? c : d is (a || b) ? c : d

c ? a : b

a ? b : c ? d : e is a ? b : (c ? d : e)

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

in

Membership, mode in ['CLASSIC', 'FD']. A property of this name reads as $.in.

and

Logical and, &&. A property of this name reads as $.and.

or

Logical or, ||. A property of this name reads as $.or.

true

A boolean literal. A property of this name reads as $.true.

false

A boolean literal. A property of this name reads as $.false.

function

JSONata’s lambda form, function($t){ $t > 0 }. A property of this name reads as $.function.

null

Not in the constraint language, an absent value is tested with $exists. A property of this name reads as $.null.

Operators and Punctuation

Token Meaning

.

A path step, tasks.priority.

..

A range in an array, [1..4].

,

Separates array items and call arguments.

;

Ends a binding in a block.

( )

A block or a group, after a . a mapped step, around call arguments and the parameter of function.

[ ]

An array, after a step a filter or an index, tasks[priority > 10], tasks[0].

{ }

The body of a function lambda.

:=

Binds a variable in a block, ($n := $count(tasks); $n > 0).

? :

The conditional, c ? a : b.

??

a ?? b takes a when it has a value, else b.

?:

a ?: b takes a unless it is absent, false, zero or '', else b.

=>

A lambda, $t => $t.period > 0.

+ - * /

Arithmetic, - also negates.

%

The remainder between two values, start % 4, else the parent, %.period.

!

Logical not.

&& ||

Logical and, or, also spelled and, or.

== = !=

Equality, = the same as ==.

< <= > >=

Order.