Antlr - dennisholee/notes GitHub Wiki

grammar EntitlementRule;

// Parser Rules
rule
    : expression EOF
    ;

expression
    : orExpression
    ;

orExpression
    : andExpression (OR andExpression)*
    ;

andExpression
    : primaryExpression (AND primaryExpression)*
    ;

primaryExpression
    : NOT? (comparisonExpression | LPAREN expression RPAREN)
    ;

comparisonExpression
    : objectPath
    | term comparisonOperator term
    | term HAS term
    ;

term
    : objectPath
    | literal
    ;

objectPath
    : IDENTIFIER ('.' IDENTIFIER)*
    ;

comparisonOperator
    : EQ | NEQ | GT | LT | GTE | LTE | IS | EQUALS | AT_LEAST | AT_MOST
    ;

literal
    : NUMBER
    | STRING
    | BOOLEAN
    | NULL
    ;

// Keywords and operators
AND: 'AND' | 'and' ;
OR: 'OR' | 'or' ;
NOT: 'NOT' | 'not' ;
IS: 'is' ;
EQUALS: 'equals' ;
AT_LEAST: 'at' WS+ 'least' ;
AT_MOST: 'at' WS+ 'most' ;
HAS: 'has' ;

// Lexer Rules
IDENTIFIER: [a-zA-Z_][a-zA-Z0-9_]* ;
NUMBER: [0-9]+ ('.' [0-9]+)? ;
STRING: '"' ~["\r\n]* '"' ;
BOOLEAN: 'true' | 'false' ;
NULL: 'null' ;

// Basic operators
EQ: '=' | '==' ;
NEQ: '!=' ;
GT: '>' | 'gt' ;
LT: '<' | 'lt' ;
GTE: '>=' ;
LTE: '<=' ;

// Parentheses
LPAREN: '(' ;
RPAREN: ')' ;

// Skip whitespace and comments
WS: [ \t\r\n]+ -> skip ;
COMMENT: '//' ~[\r\n]* -> skip ;
BLOCK_COMMENT: '/*' .*? '*/' -> skip ;