compilationUnit ::= packageStatement? ( statement ";"? )*

packageStatement ::= "package" qualifiedIdentifier ";"?

statement ::= importStatement
    |  globalStatement
    |  declare
    |  rule
    |  ruleAttribute
    |  function
    |  query

importStatement ::= "import" ("function"|"static")? qualifiedIdentifier ("." "*")?

globalStatement ::= "global" type ID

declare ::= "declare" (entryPointDeclaration | windowDeclaration | enumDeclaration | typeDeclaration) "end"

entryPointDeclaration ::= "entry-point" stringId annotation* "end"

windowDeclaration ::= "window" ID annotation* lhsPatternBind "end"

enumDeclaration ::= "enum" qualifiedIdentifier annotation* enumerative+ field* "end"

typeDeclaration ::= "type" qualifiedIdentifier ("extends" qualifiedIdentifier)? annotation* field* "end"

enumerative ::= ID ( "(" expression ("," expression)* ")" )?

field ::= label fieldType (EQUALS_ASSIGN conditionalExpr)? annotation* ";"?

function ::= "function" type? ID parameters block

parameters ::= "(" ( parameter ( "," parameter )* )? ")"

parameter ::= type? ID ("[" "]")*

query ::= "query" stringId parameters? annotation* lhsExpression "end"

rule ::= "rule" stringId ("extends" stringId)? annotation* attributes? lhs? rhs

stringId ::= ( ID | stringLiteral )

attributes ::= ("attributes" ":"?)? attribute ( ","? attribute )*

attribute ::=
      salience
    | enabled
    |
    (
          "no-loop"
        | "auto-focus"
        | "lock-on-active"
        | "refract"
    )
    booleanLiteral?
    |
    (
          "agenda-group"
        | "activation-group"
        | "ruleflow-group"
        | "date-effective"
        | "date-expires"
        | "dialect"
    )
    stringLiteral
    | "calendars" stringLiteral ( "," stringLiteral )*
    | "timer" ( decimal | chunk )
    | "duration" ( decimal | chunk )

salience ::= "salience" conditionalExpr

enabled ::= "enabled" conditionalExpr

booleanAttribute ::= attributeKey (booleanLiteral)?

stringAttribute ::= attributeKey stringLiteral

stringListAttribute ::= attributeKey stringLiteral ( "," stringLiteral )*

lhs ::= "when" ":"? lhsExpression

lhsExpression ::= lhsOr*

lhsOr ::= "(" "or" lhsAnd+ ")" | lhsAnd ("or" lhsAnd)*

lhsAnd ::= "(" "and" lhsUnary+ ")" | lhsUnary ("and" lhsUnary)*

lhsUnary ::=
( lhsExists
| lhsNot
| lhsEval
| lhsForall
| lhsAccumulate
| "(" lhsOr ")"
| lhsPatternBind
)
";"?

lhsExists ::= "exists" ( "(" lhsOr ")" | lhsPatternBind )

lhsNot ::= "not" ( "(" lhsOr ")" | lhsPatternBind )

lhsForall ::= "forall" "(" lhsPatternBind+ ")"

lhsEval ::= "eval" "(" conditionalExpr ")"

lhsParen ::= "(" lhsOr ")"

lhsPatternBind ::= label? ( "(" lhsPattern ("or" lhsPattern)* ")" | lhsPattern )

lhsAccumulate ::= "accumulate" "(" lhsAnd (","|";") accumulateFunctionBinding ("," accumulateFunctionBinding)* (";" constraints)? ")" ";"?

lhsPattern ::= "?"? qualifiedIdentifier "(" positionalConstraints? constraints? ")" ("over" patternFilter)? ("from" patternSource)?

label ::= ID ":"

positionalConstraints ::= constraint ("," constraint)* ";"

constraints ::= constraint ("," constraint)*

constraint ::= conditionalOrExpr

patternFilter ::= "over" filterDef

filterDef ::= label ID "(" parameters ")"

patternSource ::= "from" ( fromAccumulate | fromCollect | fromEntryPoint | fromWindow | fromExpression )

fromExpression ::= conditionalOrExpr

fromEntryPoint ::= "entry-point" stringId

fromWindow ::= "window" ID

fromCollect ::= "collect" "(" lhsPatternBind ")"

fromAccumulate ::= "accumulate" "(" lhsAnd "," ( "init" chunk "," "action" chunk "," ( "reverse" chunk ",")? "result" chunk | accumulateFunction ) ")"

accumulateFunctionBinding ::= label accumulateFunction

accumulateFunction ::= label? ID parameters

parameters ::= "(" (conditionalExpr ("," conditionalExpr)* )? ")"

rhs ::= "then" rhsStatement* "end"

rhsStatement ::= javaStatement | mvelStatement | modifyStatement | updateStatement | retractStatement | deleteStatement

modifyStatement ::= "modify" parExpression "{" ( expression ( "," expression )* )? "}"

updateStatement ::= "update" "(" expression ")"

retractStatement ::= "retract" "(" expression ")"

deleteStatement ::= "delete" "(" expression ")"

annotation ::= "@" ID (elementValuePairs | chunk )?

elementValuePairs ::= "(" elementValuePair ("," elementValuePair)* ")"

elementValuePair ::= ID "=" elementValue

elementValue ::= elementValueArrayInitializer | conditionalExpr

elementValueArrayInitializer ::= "{" (elementValue ("," elementValue )*)? "}"

type ::= ID typeArguments? ( "." ID typeArguments? )* ("[" "]")*

typeArguments ::= "<" typeArgument ("," typeArgument)* ">"

typeArgument ::= "?" (( "extends" | "super" ) type )? | type

qualifiedIdentifier ::= ID ( "." ID )*

chunk ::= "(" .* ")"

block ::= "{" .* "}"

ID ::= identifierStart identifierPart*

stringId ::= '"' ID '"'

identifierStart ::= letter | "_" | "$"

identifierPart ::= identifierStart | digit

assignmentOperator ::= "=" | "+=" | "-=" | "*=" | "/=" | "&=" | "|=" | "^=" | "%=" | "<<=" | ">>>=" | ">>="

operator ::= "==" | "!=" | relationalOperator

relationalOperator ::= "<=" | ">=" | "<" | ">" | ( "not"? ID squareArguments )

expression ::= conditionalExpr ( assignmentOperator expression )?

conditionalExpr ::= conditionalOrExpr ternaryExpression?

ternaryExpression ::= "?" expression ":" expression

conditionalOrExpr ::= conditionalAndExpression ( "||" conditionalAndExpression )*

conditionalAndExpression ::= inclusiveOrExpression ( "&&" inclusiveOrExpression )*

inclusiveOrExpression ::= exclusiveOrExpression ( "|" exclusiveOrExpression )*

exclusiveOrExpression ::= andExpression ( "^" andExpression )*

andExpression ::= equalityExpression ( "&" equalityExpression )*

equalityExpression ::= instanceOfExpression ( ( "==" | "!=" ) instanceOfExpression )*

instanceOfExpression ::= inExpression ( "instanceof" type )?

inExpression ::= relationalExpression ( "not"? "in" "(" expression ( "," expression )* ")" )?

relationalExpression ::= shiftExpression orRestriction*

orRestriction ::= andRestriction ( "||" andRestriction )*

andRestriction ::= singleRestriction ( "&&" singleRestriction )*

singleRestriction ::= ( operator shiftExpression ) | "(" orRestriction ")"

shiftExpression ::= additiveExpression ( shiftOp additiveExpression )*

shiftOp ::= "<<" | ">>>" | ">>"

additiveExpression ::= multiplicativeExpression ( ("+" | "-") multiplicativeExpression )*

multiplicativeExpression ::= unaryExpression ( ("/" | "%" | "*") unaryExpression )*

unaryExpression ::= ( "+" unaryExpression ) | ( "-" unaryExpression ) | ( "++" primary ) | ( "--" primary ) | unaryExpressionNotPlusMinus

unaryExpressionNotPlusMinus ::= ( "~" unaryExpression ) | ( "!" unaryExpression ) | castExpression |
        ID ( ":" | ":=" ) primary selector* ( "++" | "--" )?

castExpression ::= ( "(" primitiveType ")" unaryExpression ) | ( "(" type ")" unaryExpressionNotPlusMinus )

primitiveType ::= "boolean" | "char" | "byte" | "short" | "int" | "long" | "float" | "double"

primary ::= parExpression |
        nonWildcardTypeArguments ( explicitGenericInvocationSuffix | ( "this" arguments ) ) |
        literal |
        "super" superSuffix |
        "new" creator |
        primitiveType ( "[" "]" )* "." "class" |
        inlineMapExpression |
        inlineListExpression |
        ID ( "." ID )* identifierSuffix

inlineListExpression ::= "[" expressionList? "]"

inlineMapExpression ::= "[" mapExpressionList "]"

mapExpressionList ::= mapEntry ( "," mapEntry )*

mapEntry ::= expression ":" expression

parExpression ::= "(" expression ")"

identifierSuffix ::= ( "[" "]" )* "." "class" | "[" expression "]" | arguments

creator ::= nonWildcardTypeArguments? createdName ( arrayCreatorRest | classCreatorRest )

createdName ::= ( ID typeArguments? ( "." ID typeArguments? )* ) | primitiveType

innerCreator ::= ID classCreatorRest

arrayCreatorRest ::= "[" ( "]" ( "[" "]" )* arrayInitializer ) | ( expression "]" ( "[" expression "]" )* ( "[" "]" )* )

variableInitializer ::= arrayInitializer | expression

arrayInitializer ::= "{" ( variableInitializer ( "," variableInitializer )* ","? )? "}"

classCreatorRest ::= arguments

explicitGenericInvocation ::= nonWildcardTypeArguments arguments

nonWildcardTypeArguments ::= "<" typeList ">"

explicitGenericInvocationSuffix ::= ( "super" superSuffix ) | ID arguments

selector ::= ( "." "super" superSuffix ) |
        ( "." "new" nonWildcardTypeArguments? innerCreator ) |
        ( "." ID arguments? ) |
        ( "[" expression "]" )

superSuffix ::= arguments | ( "." ID arguments? )

squareArguments ::= "[" expressionList? "]"

arguments ::= "(" expressionList? ")"

expressionList ::= expression ( "," expression )*

decimal	::= digit+ integerTypeSuffix?

literal ::= intLiteral | realLiteral | booleanLiteral | stringLiteral | "null"

booleanLiteral ::= "true" | "false"

stringLiteral ::= '"' .* '"'

intLiteral ::= ( "+" | "-" )? ( Digit+ | "0x" ( hexDigit+ ) )

realLiteral ::= ( "+" | "-" )? ( ( ( fraction exponent? | digit+ exponent ) realTypeSuffix? ) | Digit+ realTypeSuffix )

fraction ::= digit* "." digit+

exponent ::= ( "e" | "E" ) ( "+" | "-" ) digit+

realTypeSuffix ::= "f" | "F" | "d" | "D"

integerTypeSuffix ::= "l" | "L" | "I"

digit ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

hexDigit ::= digit | "A" | "B" | "C" | "D" | "E" | "F" | "a" | "b" | "c" | "d" | "e" | "f"

letter ::= "A" | "B" | "C" | "D" | "E" | "F" | "G"
         | "H" | "I" | "J" | "K" | "L" | "M" | "N"
         | "O" | "P" | "Q" | "R" | "S" | "T" | "U"
         | "V" | "W" | "X" | "Y" | "Z"
         | "a" | "b" | "c" | "d" | "e" | "f" | "g"
         | "h" | "i" | "j" | "k" | "l" | "m" | "n"
         | "o" | "p" | "q" | "r" | "s" | "t" | "u"
         | "v" | "w" | "x" | "y" | "z"
