-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathOpenFGAParser.g4
105 lines (90 loc) · 3.26 KB
/
OpenFGAParser.g4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
parser grammar OpenFGAParser;
options { tokenVocab=OpenFGALexer; }
main: WHITESPACE? NEWLINE? (modelHeader | moduleHeader) NEWLINE? typeDefs NEWLINE? conditions NEWLINE? EOF;
// Model Header
modelHeader: (multiLineComment NEWLINE)? MODEL NEWLINE SCHEMA WHITESPACE schemaVersion=SCHEMA_VERSION WHITESPACE?;
// Module Header
moduleHeader: (multiLineComment NEWLINE)? MODULE WHITESPACE moduleName=identifier WHITESPACE?;
// Type Definitions
typeDefs: typeDef*;
typeDef: (NEWLINE multiLineComment)? NEWLINE (EXTEND WHITESPACE)? TYPE WHITESPACE typeName=identifier (NEWLINE RELATIONS relationDeclaration+)?;
// Relation definitions
relationDeclaration: (NEWLINE multiLineComment)? NEWLINE DEFINE WHITESPACE relationName WHITESPACE? COLON WHITESPACE? (relationDef);
relationName: identifier;
relationDef: (relationDefDirectAssignment | relationDefGrouping | relationRecurse) (relationDefPartials)?;
relationDefNoDirect: (relationDefGrouping | relationRecurseNoDirect) (relationDefPartials)?;
relationDefPartials:
(WHITESPACE OR WHITESPACE (relationDefGrouping | relationRecurseNoDirect))+
| (WHITESPACE AND WHITESPACE (relationDefGrouping | relationRecurseNoDirect))+
| (WHITESPACE BUT_NOT WHITESPACE (relationDefGrouping | relationRecurseNoDirect));
relationDefGrouping: relationDefRewrite;
relationRecurse:
LPAREN WHITESPACE* (
relationDef |
relationRecurseNoDirect
) WHITESPACE* RPAREN;
relationRecurseNoDirect:
LPAREN WHITESPACE* (
relationDefNoDirect |
relationRecurseNoDirect
) WHITESPACE* RPAREN;
relationDefDirectAssignment: LBRACKET WHITESPACE? relationDefTypeRestriction WHITESPACE? (COMMA WHITESPACE? relationDefTypeRestriction WHITESPACE?)* RPRACKET;
relationDefRewrite: rewriteComputedusersetName=identifier (WHITESPACE FROM WHITESPACE rewriteTuplesetName=identifier)?;
relationDefTypeRestriction: NEWLINE? (
relationDefTypeRestrictionBase
| (relationDefTypeRestrictionBase WHITESPACE KEYWORD_WITH WHITESPACE conditionName)
) NEWLINE?;
relationDefTypeRestrictionBase: relationDefTypeRestrictionType=identifier
((COLON relationDefTypeRestrictionWildcard=STAR)
| (HASH relationDefTypeRestrictionRelation=identifier))?;
// Conditions
conditions: condition*;
condition: (NEWLINE multiLineComment)? NEWLINE
CONDITION WHITESPACE conditionName WHITESPACE?
LPAREN WHITESPACE? conditionParameter WHITESPACE? (COMMA WHITESPACE? conditionParameter WHITESPACE?)* NEWLINE? RPAREN WHITESPACE?
LBRACE NEWLINE? WHITESPACE?
conditionExpression
NEWLINE? RBRACE;
conditionName: IDENTIFIER;
conditionParameter: NEWLINE? parameterName WHITESPACE? COLON WHITESPACE? parameterType;
parameterName: IDENTIFIER;
parameterType: CONDITION_PARAM_TYPE | (CONDITION_PARAM_CONTAINER LESS CONDITION_PARAM_TYPE GREATER);
multiLineComment: HASH (~NEWLINE)* (NEWLINE multiLineComment)?;
identifier: MODEL | SCHEMA | TYPE | RELATION | IDENTIFIER | MODULE | EXTEND;
conditionExpression: ((
IDENTIFIER |
EQUALS |
NOT_EQUALS |
IN |
LESS |
LESS_EQUALS |
GREATER_EQUALS |
GREATER |
LOGICAL_AND |
LOGICAL_OR |
LBRACKET |
RPRACKET |
LBRACE |
LPAREN |
RPAREN |
DOT |
MINUS |
EXCLAM |
QUESTIONMARK |
PLUS |
STAR |
SLASH |
PERCENT |
CEL_TRUE |
CEL_FALSE |
NUL |
WHITESPACE |
CEL_COMMENT |
NUM_FLOAT |
NUM_INT |
NUM_UINT |
STRING |
BYTES |
NEWLINE |
WHITESPACE
)|~(RBRACE))*;