-
Notifications
You must be signed in to change notification settings - Fork 8
/
OpenFGALexer.g4
163 lines (134 loc) · 3.12 KB
/
OpenFGALexer.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
lexer grammar OpenFGALexer;
tokens {
COLON,
COMMA,
LESS,
GREATER,
LBRACKET,
RBRACKET,
LPAREN,
RPAREN,
WHITESPACE,
IDENTIFIER
}
HASH: '#';
COLON: ':';
COMMA: ',';
AND: 'and';
OR: 'or';
BUT_NOT: 'but not';
FROM: 'from';
MODULE: 'module';
MODEL: 'model';
SCHEMA: 'schema';
SCHEMA_VERSION: DIGIT+'.'DIGIT+;
EXTEND: 'extend';
TYPE: 'type';
CONDITION: 'condition' -> pushMode(CONDITION_DEF);
RELATIONS: 'relations';
RELATION: 'relation';
DEFINE: 'define';
KEYWORD_WITH: 'with';
// CEL Lexer tokens, slightly modified source:
// https://github.com/google/cel-go/blob/32ac6133c6b8eca8bb76e17e6ad50a1eb757778a/parser/gen/CEL.g4
EQUALS: '==';
NOT_EQUALS: '!=';
IN: 'in';
LESS: '<';
LESS_EQUALS: '<=';
GREATER_EQUALS: '>=';
GREATER: '>';
LOGICAL_AND: '&&';
LOGICAL_OR: '||';
LBRACKET: '[';
RPRACKET: ']';
LBRACE: '{';
RBRACE: '}';
LPAREN: '(';
RPAREN: ')';
DOT: '.';
MINUS: '-';
EXCLAM: '!';
QUESTIONMARK: '?';
PLUS: '+';
STAR: '*';
SLASH: '/';
PERCENT: '%';
CEL_TRUE: 'true';
CEL_FALSE: 'false';
NUL: 'null';
fragment BACKSLASH: '\\';
fragment LETTER: 'A' ..'Z' | 'a' ..'z';
fragment DIGIT: '0' ..'9';
fragment EXPONENT: ('e' | 'E') ( '+' | '-')? DIGIT+;
fragment HEXDIGIT: ('0' ..'9' | 'a' ..'f' | 'A' ..'F');
fragment RAW: 'r' | 'R';
fragment ESC_SEQ:
ESC_CHAR_SEQ
| ESC_BYTE_SEQ
| ESC_UNI_SEQ
| ESC_OCT_SEQ;
fragment ESC_CHAR_SEQ:
BACKSLASH (
'a'
| 'b'
| 'f'
| 'n'
| 'r'
| 't'
| 'v'
| '"'
| '\''
| '\\'
| '?'
| '`'
);
fragment ESC_OCT_SEQ:
BACKSLASH ('0' ..'3') ('0' ..'7') ('0' ..'7');
fragment ESC_BYTE_SEQ: BACKSLASH ( 'x' | 'X') HEXDIGIT HEXDIGIT;
fragment ESC_UNI_SEQ:
BACKSLASH 'u' HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT
| BACKSLASH 'U' HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT;
WHITESPACE: ( '\t' | ' ' | '\u000C')+;
CEL_COMMENT: '//' (~'\n')* -> channel(HIDDEN);
NUM_FLOAT: (
DIGIT+ ('.' DIGIT+) EXPONENT?
| DIGIT+ EXPONENT
| '.' DIGIT+ EXPONENT?
);
NUM_INT: ( DIGIT+ | '0x' HEXDIGIT+);
NUM_UINT: DIGIT+ ( 'u' | 'U') | '0x' HEXDIGIT+ ( 'u' | 'U');
STRING:
'"' (ESC_SEQ | ~('\\' | '"' | '\n' | '\r'))* '"'
| '\'' (ESC_SEQ | ~('\\' | '\'' | '\n' | '\r'))* '\''
| '"""' (ESC_SEQ | ~('\\'))*? '"""'
| '\'\'\'' (ESC_SEQ | ~('\\'))*? '\'\'\''
| RAW '"' ~('"' | '\n' | '\r')* '"'
| RAW '\'' ~('\'' | '\n' | '\r')* '\''
| RAW '"""' .*? '"""'
| RAW '\'\'\'' .*? '\'\'\'';
BYTES: ('b' | 'B') STRING;
IDENTIFIER: (LETTER | '_') (LETTER | DIGIT | '_' | MINUS)*;
// NOTE: MINUS is not allowed in CEL, but allowed in FGA, CEL will be revalidated after
// END CEL GRAMMAR
NEWLINE:
WHITESPACE? ('\r'? '\n' | '\r' | '\f') WHITESPACE? NEWLINE?;
mode CONDITION_DEF;
CONDITION_DEF_END: RPAREN -> type(RPAREN), popMode;
CONDITION_PARAM_CONTAINER: 'map' | 'list';
CONDITION_PARAM_TYPE:
'bool'
| 'string'
| 'int'
| 'uint'
| 'double'
| 'duration'
| 'timestamp'
| 'ipaddress';
CONDITION_PARAM_TYPE_LESS: LESS -> type(LESS);
CONDITION_PARAM_TYPE_GREATER: GREATER -> type(GREATER);
CONDITION_OPEN: LPAREN -> type(LPAREN);
CONDITION_COLON: COLON -> type(COLON);
CONDITION_COMMA: COMMA -> type(COMMA);
CONDITION_WS: WHITESPACE -> type(WHITESPACE);
CONDITION_NAME: IDENTIFIER -> type(IDENTIFIER);