C# Engine Parser


      Remarks

    * Parser is translated from java version
    * C# behavior must be coded in java version
    * Differences with Java version
          o using instead of import
          o is instead of instanceof
          o typeof(...) instead of .class


      EBNF

//==========================================================
// The JRules IRL parser
//==========================================================

//---------------------------------------------------------------------------
// Reserved words of the rule language
//---------------------------------------------------------------------------

TOKEN :
  
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 
  | 

//---------------------------------------------------------------------------
// Some constants
//---------------------------------------------------------------------------

TOKEN :
     |  | 

//========================================================== 
//   The rules
//==========================================================

Identifier :
  
  | "assert"
  | "insert"
  | "retract"
  | "update"
  | "modify"
  | "apply"
  | "execute"
  | "rule"
  | "packet"
  | "priority"
  | "property"
  | "ruleset"
  | "using"
  | "setup"
  | "when"
  | "then"
  | "where"
  | "from"
  | "in"
  | "while"
  | "foreach"
  | "for" 
  | "as"
  | "function"
  | "occursin"
  | "before"
  | "after"
  | "$"
  | "functiontask"
  | "ruletask"
  | "flowtask"
  | "body"
  | "initialaction"
  | "finalaction"
  | "firing"
  | "firinglimit"
  | "ordering"
  | "select"
  | "dynamicselect"
  | "filter"
  | "agendafilter"
  | "out"
  | "inout"
  | "completionflag"
  | "default"
  | "iterator"
  | "algorithm"
  | "matchedclasses"
  | "wait"
  | "is"

ExtendedIdentifier :
  ( Identifier ( "." Identifier )* )

PossiblePackageName :
  ( PossiblePacketName | "event" )

PossiblePacketName :
  Identifier
  | "return"
  | "break"
  | "continue"
  | "if"
  | "else"
  | "new"
  | "instanceof"
  | "timeof"
  | "not"
  | "exists"
  | "collect"
  | "logical"
  | "bind"
  | "var"
  | "until"
  | "timeout"
  | "evaluate"
  | "throw"
  | "try"
  | "catch"
  | "finally"
  | "isunknown"
  | "isknown"

Variable :
  ( Identifier |  |  )

//---------------------------------------------------------------------------
// Literals of the language
//---------------------------------------------------------------------------

Literal :
  
  | 
  | 
  | 
  | "true"
  | "false"
  | "null"

SignedLiteral :
  ( "+" | "-" ) ?
  ( 
    | 
    | 
    | 
    | "true"
    | "false"
    | "null" )

SignedNumberExpression :
  ( "+" | "-" ) (  |  )

//---------------------------------------------------------------------------
// The type names
//   Simple type name is used to specify simple type, eventually using
//   package name. They are used in the import statement. type name is
//   used for a general type, including array type.
//---------------------------------------------------------------------------

SimpleTypeName :
  Identifier ( "." Identifier )*

SimplePackageTypeName :
  PossiblePackageName ( "." PossiblePackageName )*

TypeName :
  SimpleTypeName ( "[" "]"  )*

//---------------------------------------------------------------------------
// Utility methods: parser entry points
//---------------------------------------------------------------------------

ParseRuleDefinition :
  "rule" ExtendedIdentifier "{" RuleBodyDefinition "}" (";" )?  

//---------------------------------------------------------------------------
// The ruleset definition
//   A ruleset begins with some imports statements (can be absent),
//   Then an initialization rule (optional),
//   Then some rule definitions.
//   Then EOF.
//---------------------------------------------------------------------------

RulesetDefinition :
  ( ImportDefinition )*
  ( PropertyDefinition )? 
  ( SetupDefinition )? 
      (  FunctionDefinition
       | RuleDefinition
       | TaskDefinition
       | ErrorManagement
      )*
  

ErrorManagement :
    Identifier

//---------------------------------------------------------------------------
// The taskset definition
//   Then some task definitions.
//   Then EOF.
//---------------------------------------------------------------------------

TasksetDefinition :
  ( ImportDefinition )*
  ( PropertyDefinition )?
  ( SetupDefinition )?
  ( TaskDefinition )*
  

//---------------------------------------------------------------------------
// Imports
//---------------------------------------------------------------------------

ImportDefinition :
  ProtectedImportDefinition

ProtectedImportDefinition :
  ( "using" ) SimplePackageTypeName [ "." "*" ] ";"

Property :
  "property" SimpleTypeName "="
  ( SignedLiteral | TypeName ( "("  ")" )? ";" )

//---------------------------------------------------------------------------
// The ruleset properties
//---------------------------------------------------------------------------

PropertyDefinition :
  "ruleset" SimpleTypeName
  ( ";" | "{" ( RulesetSettings )* "}" ( ";" )? )

RulesetSettings :
  Property
  | RulesetVariableDeclaration
  | ClassInstancesDeclaration
  | HasherDeclaration

ClassInstancesDeclaration :
  "instances" "(" TypeName ")" "=" ( InstancesList | Expression ";" )

InstancesList :
  "{" ( "}"  | ( Expression ( "," Expression )* "}" ) ) ( ";" )?

HasherDeclaration :
  "hasher" "(" TypeName Variable ")" "=" Expression ";"  

RulesetVariableDeclaration :
  ( "in"  | "inout" ) TypeName Variable ";" 
  | [ "out" ] TypeName Variable [ "=" ExpressionAndArray ] ";"

//---------------------------------------------------------------------------
// The setup rule (or initialization rule)
//   Is used to initialize the execution context. This is a rule without
//   conditions (with only actions). The actions are executed as soon
//   as the context is created.
//---------------------------------------------------------------------------

SetupDefinition :
  ProtectedSetupDefinition

ProtectedSetupDefinition :
  "setup" "{" ( ActionStatement )* "}" ( ";" )?

//---------------------------------------------------------------------------
// The rule definition
//   See the next grammar for the rule definition syntax.
//---------------------------------------------------------------------------

RuleDefinition :
  ProtectedRuleDefinition

ProtectedRuleDefinition :
  "rule" ExtendedIdentifier "{" RuleBodyDefinition "}" ( ";" )?

//---------------------------------------------------------------------------
// The function definition
//   See the next grammar for the function definition syntax.
//---------------------------------------------------------------------------

FunctionDefinition :
  ProtectedFunctionDefinition

ProtectedFunctionDefinition :
  "function" TypeName ExtendedIdentifier
  FunctionArgsDefinition "{" ( ActionStatement )* "}" ( "; ")?

FunctionArgsDefinition :
  "(" ( ")" | ( FunctionArgDefinition ( "," FunctionArgDefinition )* ")" )

FunctionArgDefinition :
  TypeName Variable

//---------------------------------------------------------------------------
// Rule body
//---------------------------------------------------------------------------

RuleBodyDefinition :
  ( RuleParameterAssignments )* RuleConditions RuleActions

RuleParameterAssignments :
  "packet" "=" PossiblePacketName ";" 
  | "priority" "=" Expression ";"
  | Property

//---------------------------------------------------------------------------
// The task definition
//---------------------------------------------------------------------------

TaskDefinition :
  ProtectedRuleTaskDefinition
  | ProtectedFunctionTaskDefinition
  | ProtectedFlowTaskDefinition

ProtectedRuleTaskDefinition :
  "ruletask" ExtendedIdentifier
  "{" RuleTaskBodyDefinition "}" ( ";" )?

ProtectedFunctionTaskDefinition :
  "functiontask" ExtendedIdentifier
  "{" FunctionTaskBodyDefinition "}" ( ";" )?

ProtectedFlowTaskDefinition :
  "flowtask" ExtendedIdentifier
  "{" FlowTaskBodyDefinition "}" ( ";" )?

//---------------------------------------------------------------------------
// Task body
//---------------------------------------------------------------------------

RuleTaskBodyDefinition :
  ( RuleTaskParameterAssignments )*

FunctionTaskBodyDefinition :
  ( FunctionTaskParameterAssignments )*

FlowTaskBodyDefinition :
  ( FlowTaskParameterAssignments )*

RuleTaskParameterAssignments :
  "body"  
   ( "=" ( "select" | "dynamicselect" )
       "(" [ Variable ]
       ")"  TaskActions EndRuleTaskSelector
      |
       ("=")? "{" ( RuleList "}" | "}" ) ( ";" )?
      )       
     |
      ( "ordering" "=" Identifier ";" )
    )
   | ( "firing" "=" Identifier ";" )
   | ( "firinglimit" "=" ( "+" | "-" )?  ";" )
   | ( "agendafilter" "=" 
       ( "filter" "(" Variable ")" TaskActions ( ";" )? | Expression ";" )
     )
   | ( "iterator" "=" Expression ";" )
   | ( "algorithm" "=" Identifier ";" )
   | ( "matchedclasses" 
      ( "=" Expression ";" | ( "=" )? "{" MatchOnList "}" ( ";" )? )
     )
   | CommonTaskParameterAssignments

EndRuleTaskSelector :
  ["in" AdditiveExpression ] ( ";" )?

RuleList :
  ExtendedIdentifier ( "," ExtendedIdentifier )*

MatchOnList :
  TypeName ( "," TypeName )*

FunctionTaskParameterAssignments :
  "body" ( "=" )? TaskActions ( ";" )?
  | CommonTaskParameterAssignments

TaskActions :
  "{" ( ActionStatement )* "}"

FlowTaskParameterAssignments :
  "body" ( "=" )? CommonTaskParameterAssignments

CommonTaskParameterAssignments :
  Property
  | InitialFinalAction
  | "completionflag" "=" Expression ";"

FlowDefinition :
  "{" ( "}" | FlowStatement ( FlowStatement )* "}" )

FlowStatement :
  TaskStatement
  | WhileFlowStatement
  | SwitchFlowStatement
  | IfFlowStatement
  | ForkFlowStatement
  | GotoStatement
  | BreakOrContinueFlowStatement

BreakOrContinueFlowStatement :
  ( "break" | "continue" ) ";"

GotoStatement :
  "goto" Identifier ";"

TaskStatement :
  Identifier ":" ExtendedIdentifier ";"
  | ExtendedIdentifier end = ";"     

IfFlowStatement :
  [ Identifier ":" ] "if" "(" TestExpression ")" Branch [ "else" Branch ]

SwitchFlowStatement :
  [ Identifier ":" ] "switch" "(" Expression ")"
  "{" ( ( "case"   ":" | "default" ":" ) Branch )* "}"   

WhileFlowStatement :
  ( "while" "(" | Identifier ":" "while" "(" )
  TestExpression() ")" Branch

BracedBranch :
  "{" ( "}" | ( FlowStatement ) ( FlowStatement )* "}" )

Branch :
  ( FlowStatement ) | BracedBranch

ForkFlowStatement :
  [ Identifier ":" ] "fork" BracedBranch ( "&&" BracedBranch )* ( ";" )?

InitialFinalAction :
  ( "initialaction" | "finalaction" ) ( "=" )? TaskActions ( ";" )?

//---------------------------------------------------------------------------
// Conditions
//---------------------------------------------------------------------------

RuleConditions :
  "when" "{" FirstPattern ( LhsPattern )* "}" 

//---------------------------------------------------------------------------
// Condition part
//---------------------------------------------------------------------------

FirstPattern :
  NotPattern
  | ExistsPattern
  | ( Variable ":" )? ( ClassPattern | CollectPattern )

LhsPattern :
  FirstPattern | EvaluatePattern | WaitPattern

ClassPattern :
  ClassCondition

CollectPattern :
  "collect" [ "(" [ Expression ] ")" ]
  ClassCondition [ "where" "(" TestAssignmentSuite ")" ] ";" 

NotPattern :
  "not" ClassCondition ";"

ExistsPattern :
  "exists" ClassCondition ";"

EvaluatePattern :
  "evaluate"  "(" TestAssignmentSuite ")" ";"

WaitPattern :
  ( Variable ":" )? "wait" ("logical")? ( "until"? Expression )?
  ( ";" | "{" ( FirstPattern )* "}" )

//---------------------------------------------------------------------------
// Class Condition
//---------------------------------------------------------------------------

ClassCondition :
  [ "event" ] TypeName "(" 
  [ TestAssignmentSuite ] ")" [ ( "from" | "in" ) AdditiveExpression ]

//---------------------------------------------------------------------------
// Tests
//---------------------------------------------------------------------------

TestAssignmentSuite :
  TestAssignmentSet ( ";" TestAssignmentSet )* [ ";" ]

//---------------------------------------------------------------------------
// Decomposition of tests and assignment (combination of OR, AND)
//---------------------------------------------------------------------------

TestAssignmentSet :
  OrTestAssignmentSet

OrTestAssignmentSet :
  AndTestAssignmentSet ( "||" AndTestAssignmentSet )*

AndTestAssignmentSet :
  TestAssignment ( "&&" TestAssignment )*

TestAssignment :
  VariableAssignment
  | BooleanExpression
  | "(" TestAssignmentSet ")"
  | "!" TestAssignment

//---------------------------------------------------------------------------
// Assignments
//---------------------------------------------------------------------------

VariableAssignment :
  Variable ":" ( AdditiveExpression | "("  Expression ")" )
  ("&" BooleanArguments )*

//---------------------------------------------------------------------------
// Tests
//---------------------------------------------------------------------------

BooleanExpression :
  ( AdditiveExpression | "(" Expression ")" )
  [ BooleanArguments ( "&" BooleanArguments )* ]

BooleanArguments :
  TypeTestArguments
  | AsExpression 
  | TemporalTestArguments 
  | ValueTestArguments 
  | UnknownTestArguments 
  | KnownTestArguments

UnknownTestArguments :
  "isunknown"

KnownTestArguments :
  "isknown"

TypeTestArguments :
  ( "is" ) TypeName

ValueTestArguments :
  ValueTestArguments1 | ValueTestArguments2

ListExpression :
  ListValues

ListValues :
  "{" ( Expression | ListValues ) "," ( Expression | ListValues ) )* "}" 

ValueTestArguments1 :
  [ "!" ] "in" ListExpression

ValueTestArguments2 :
  ( PredefinedPredicate | "!" "in" | Identifier ) 
  ( AdditiveExpression | "(" Expression ")" )

PredefinedPredicate :
  "==" | "!=" | "<" | ">" | "<=" | ">=" 

TemporalTestOperator :
  "occursin" | "before" | "after"

TemporalTestArguments :
  UnaryTemporalTestArguments | BinaryTemporalTestArguments

UnaryTemporalTestArguments :
  "occursin" TimeInterval

BinaryTemporalTestArguments :
  ( "before" | "after" ) [ TimeInterval ] AdditiveExpression

TimeInterval :
  "[" ( "$" |  Expression ) ","  ( "$" |  Expression ) "]"

//---------------------------------------------------------------------------
// Expressions
//---------------------------------------------------------------------------

ExpressionEof :
  Expression 

Expression :
  TestExpression

AdditiveExpression :
  MultiplicativeExpression ( ( "+" | "-" ) MultiplicativeExpression )*

MultiplicativeExpression :
  UnaryExpression ( ( "*" | "/" | "%" ) UnaryExpression )*

UnaryExpression :
  SignedNumberExpression 
  | ( "+" | "-" | "!" | "++" | "--" ) UnaryExpression
  | UnaryExpressionNotPlusMinus

UnaryExpressionNotPlusMinus :
  CastExpression
  | TimeofExpression
  | PrimaryExpression [ ( "++" | "--" ) ]

CastExpression :
  "(" TypeName ")" UnaryExpression

TimeofExpression :
  "timeof" "(" Expression ")" 

PrimaryExpression :
   
  |
   TypeofExpression
  |
   PrimaryPrefix ( PrimarySuffix )*

TypeofExpression : 
  "typeof" "(" TypeName ")"

PrimaryPrefix :
  Literal
  | Name
  | AllocationExpression
  | "(" Expression ")"

AllocationExpression :
  "new" SimpleTypeName ( Arguments | ArrayDims [ ListValues ] )

ExpressionAndArray :
  Expression | ListValues

ArrayDims :
  "["  [ Expression ] "]" ( "[" [ Expression ] "]" )*

PrimarySuffix :
  "." (Identifier |  |  )
  | ArrayArguments
  | Arguments

ArrayArguments :
  "[" [ ArgumentList ] "]"

Arguments :
  "(" [ ArgumentList ] ")"

ArgumentList :
  Expression ( "," Expression )*

Name :
  Variable ( "." Identifier )*

//---------------------------------------------------------------------------
// Rule Actions
//---------------------------------------------------------------------------

RuleActions :
  "then" "{" ( ActionStatement )* ( TimeOutStatement )* "}" 
  ( "else" "{" ( ActionStatement )* ( TimeOutStatement )* "}" )?

ActionStatement :
  
  | SimpleStatement
  | AssertStatement
  | RetractStatement
  | UpdateStatement
  | ModifyStatement
  | ApplyStatement
  | IfStatement
  | ExecuteStatement
  | WhileStatement
  | ForeachStatement
  | ForStatement
  | ReturnStatement
  | BreakStatement
  | ContinueStatement
  | ThrowStatement
  | TryStatement
  | BindStatement

TryStatement :
  "try" TryStatements CatchFinally

TryStatements :
  "{" (ActionStatement)* "}" 

CatchFinally :
  Finally | ( ( Catches )+ ( Finally )? )

Catches :
  CatchClause ( CatchClause )*

CatchClause :
  "catch" "(" FunctionArgDefinition ")" "{" ( ActionStatement )* "}"

Finally :
  "finally" "{" ( ActionStatement )* "}"

ThrowStatement :
  "throw" Expression ";" 

ReturnStatement :
  "return" ( Expression )? ";"

BreakStatement :
  "break" ";"

ContinueStatement :
  "continue" ";"

BindStatement :
  ( "bind" | "var" ) Variable "=" ExpressionAndArray ";"
  | TypeName Variable ( "=" ExpressionAndArray )? ";"

AssertStatement :
  ("assert" | "insert")
  ( [ "logical" ] AssertedObjectAndInitStatements | "event" AssertedEventAndInitStatements )

AssertedObjectAndInitStatements :
  ( SimpleTypeName [ Arguments ] | "(" Expression ")" ) AssertInitStatements

AssertedEventAndInitStatements :
  SimpleTypeName [ Arguments ] AssertInitStatements
  | "(" Expression ")" ( AssertInitStatements | AssertedObjectAndInitStatements )

AssertInitStatements :
  "{" SimpleStatementBlock "}" [ ";" ] | ";"    

RetractStatement :
  "retract" PrimaryExpression ";"

UpdateStatement :
  "update" PrimaryExpression ";"

ModifyStatement :
  "modify" PrimaryExpression "{" SimpleStatementBlock "}" [ ";" ]

ApplyStatement :
  "apply" PrimaryExpression "{" SimpleStatementBlock "}" [ ";" ]

ExecuteStatement :
  "execute" ( ActionStatement | "{" ( ActionStatement )* "}" [ ";" ] )

IfStatement :
  "if" IfTest IfBlockStatements [ IfElseStatements ]

IfTest :
  "(" TestExpression ")" 

IfBlockStatements :
  ActionStatement() | "{" ( ActionStatement )* "}"

IfElseStatements :
  "else"  IfBlockStatements

WhileStatement :
  "while" "(" TestExpression ")"
  ( ActionStatement | "{" ( ActionStatement )* "}" [ ";" ] )

ForeachStatement :
  "foreach" ForeachInitialisation BlockStatements

ForeachInitialisation :
  "(" ForeachBlockExpression ")" 

ForeachBlockExpression :
  TypeName Variable "in" Expression

ForStatement :
  "for" ForInitialisation BlockStatements

ForInitialisation :
  "(" [ ForBlockExpression ] ";" [ TestExpression ] ";" [ ForBlockExpression ] ")"

BlockStatements :
  ActionStatement | "{"  ( ActionStatement )* "}" [ ";" ]

ForBlockExpression :
  ( ForBindStatement | ForSimpleStatement )
  ( "," ( ForBindStatement | ForSimpleStatement) )*

ForSimpleStatement :
  UnaryExpression [AssignmentOperator ExpressionAndArray]

ForBindStatement :
  TypeName Variable "=" Expression
  | ( "bind" | "var" ) Variable "=" Expression

TimeOutStatement :
  "timeout" Variable
  ( ActionStatement | "{" (ActionStatement)* "}" [";"] )

StatementBlockEof :
  ( ActionStatement )* 

SimpleStatementBlock :
  ( SimpleStatement | BindStatement )*

SimpleStatementItem :
  SimpleStatement | BindStatement

SimpleStatement :
  UnaryExpression [ AssignmentOperator Expression ] ";"

SimpleStatementLookAhead :
  UnaryExpression [ AssignmentOperator Expression ] ";"

AsLookAhead :
  "as" TypeName

AsExpression :
  "as" TypeName

AssignmentOperator :
  "=" | "*=" | "/=" | "%=" | "+=" | "-=" 

//---------------------------------------------------------------------------
// Conventional test expressions to be used in RHS
//---------------------------------------------------------------------------

TestExpression :
  AndTestExpression ("||" AndTestExpression )*

AndTestExpression :
  OtherTestExpression ("&&" OtherTestExpression )*

OtherTestExpression :
  SimpleTestExpression
  | "(" TestExpression ")"
  | "!" OtherTestExpression

SimpleTestExpression :
  AdditiveExpression [ BooleanArguments ]

-- Alexandre FAU  - 22 Apr 2004