-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathNix.bnf
217 lines (195 loc) · 7.56 KB
/
Nix.bnf
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
{
parserClass="org.nixos.idea.lang.NixParser"
parserUtilClass="org.nixos.idea.psi.impl.NixParserUtil"
extends="com.intellij.extapi.psi.ASTWrapperPsiElement"
psiClassPrefix="Nix"
psiImplClassSuffix="Impl"
psiPackage="org.nixos.idea.psi"
psiImplPackage="org.nixos.idea.psi.impl"
elementTypeHolderClass="org.nixos.idea.psi.NixTypes"
elementTypeClass="org.nixos.idea.psi.NixElementType"
tokenTypeClass="org.nixos.idea.psi.NixTokenType"
// do not record error reporting information in recover rules and missing_semi
consumeTokenMethod(".*_recover|missing_semi")="consumeTokenFast"
// do not record error reporting information in expression parsing,
// no one really needs to know that + - * / are expected at any offset
consumeTokenMethod("expr_op.*")="consumeTokenFast"
tokens = [
// This list does not contain all tokens. This list only defines the debug
// names for tokens which have a distinct text representation. Other tokens
// are implicitly declared through their usage.
IF = 'if'
THEN = 'then'
ELSE = 'else'
ASSERT = 'assert'
WITH = 'with'
LET = 'let'
IN = 'in'
REC = 'rec'
INHERIT = 'inherit'
OR_KW = 'or'
ELLIPSIS = '...'
ASSIGN = '='
COLON = ':'
SEMI = ';'
COMMA = ','
AT = '@'
LPAREN = '('
RPAREN = ')'
LCURLY = '{'
RCURLY = '}'
LBRAC = '['
RBRAC = ']'
DOLLAR = '$'
DOT = '.'
HAS = '?'
NOT = '!'
TIMES = '*'
DIVIDE = '/'
PLUS = '+'
MINUS = '-'
LT = '<'
GT = '>'
CONCAT = '++'
UPDATE = '//'
LEQ = '<='
GEQ = '>='
EQ = '=='
NEQ = '!='
AND = '&&'
OR = '||'
IMPL = '->'
// This two tokens must be declared here because they are not used by the
// parser and therefore not declared anywhere else in the file.
SCOMMENT = 'SCOMMENT'
MCOMMENT = 'MCOMMENT'
]
}
nixFile ::= expr
;{ extends("expr_.*")=expr }
expr ::= <<parseNonBindValue expr0>>
private expr0 ::=
expr_assert
| expr_if
| expr_let
| expr_with
| expr_lambda
| expr_op
expr_assert ::= ASSERT expr SEMI expr { pin=1 }
expr_if ::= IF expr then else { pin=1 }
private then ::= THEN expr { pin=1 }
private else ::= ELSE expr { pin=1 }
expr_let ::= LET !LCURLY recover_let (bind recover_let)* IN expr { pin=2 }
expr_with ::= WITH expr SEMI expr { pin=1 }
private recover_let ::= { recoverWhile=let_recover }
private let_recover ::= braces_recover !(ASSERT | SEMI | IF | THEN | ELSE | LET | IN | WITH) !bind
expr_lambda ::= lambda_params !missing_semi COLON expr { pin=3 }
private lambda_params ::= ID [ !missing_semi AT param_set ] | param_set [ !missing_semi AT ID ]
param_set ::= LCURLY [ ( param COMMA )* ( ELLIPSIS | param ) ] recover_param_set RCURLY { pin=1 }
param ::= ID [ param_has ] { pin=2 recoverWhile=param_recover }
private param_has ::= HAS expr { pin=1 }
private param_recover ::= curly_recover !COMMA
private recover_param_set ::= { recoverWhile=curly_recover }
// Note that the rules for expr_op.* use a special processing mode of
// Grammar-Kit. Left recursion would not be possible otherwise.
// https://github.com/JetBrains/Grammar-Kit/blob/master/HOWTO.md#24-compact-expression-parsing-with-priorities
// https://intellij-support.jetbrains.com/hc/en-us/community/posts/360001258300-What-s-the-alternative-to-left-recursion-in-GrammarKit-#community_comment_360000201199
;{ extends("expr_op_.*")=expr_op }
expr_op ::=
expr_op_implication
| expr_op_or
| expr_op_and
| group_eq
| group_cmp
| expr_op_update
| expr_op_not
| group_add
| group_mul
| expr_op_concat
| expr_op_has
| expr_op_neg
| expr_op_base
private group_eq ::= expr_op_eq | expr_op_ne
private group_cmp ::= expr_op_lt | expr_op_le | expr_op_gt | expr_op_ge
private group_add ::= expr_op_plus | expr_op_minus
private group_mul ::= expr_op_mul | expr_op_div
expr_op_implication ::= expr_op IMPL expr_op
expr_op_or ::= expr_op OR expr_op
expr_op_and ::= expr_op AND expr_op
expr_op_eq ::= expr_op EQ expr_op
expr_op_ne ::= expr_op NEQ expr_op
expr_op_lt ::= expr_op LT expr_op
expr_op_le ::= expr_op LEQ expr_op
expr_op_gt ::= expr_op GT expr_op
expr_op_ge ::= expr_op GEQ expr_op
expr_op_update ::= expr_op UPDATE expr_op { rightAssociative=true }
expr_op_not ::= NOT expr_op
expr_op_plus ::= expr_op PLUS expr_op
expr_op_minus ::= expr_op MINUS expr_op
expr_op_mul ::= expr_op TIMES expr_op
expr_op_div ::= expr_op DIVIDE expr_op
expr_op_concat ::= expr_op CONCAT expr_op { rightAssociative=true }
expr_op_has ::= expr_op HAS attr_path
expr_op_neg ::= MINUS expr_op
expr_op_base ::= expr_app
// Grammar-Kit cannot handle "expr_app ::= expr_app expr_select_or_legacy" or
// equivalent rules. As a workaround, we use this rule which will only create
// one AST node for a series of function calls.
expr_app ::= expr_select ( !missing_semi expr_select ) *
expr_select ::= expr_simple [ !missing_semi ( select_attr | legacy_app_or )]
private select_attr ::= DOT attr_path [ select_default ] { pin=1 }
private select_default ::= OR_KW expr_select { pin=1 }
// Uses 'upper' instead of a choice with '|' as workaround for
// https://github.com/JetBrains/Grammar-Kit/issues/257
upper legacy_app_or ::= OR_KW { extends=expr_app }
;{ extends("identifier|literal|string|parens|set|list|legacy_let")=expr_simple }
expr_simple ::=
identifier
| literal
| string
| parens
| set
| list
| legacy_let
identifier ::= ID
literal ::= INT | FLOAT | PATH | HPATH | SPATH | URI
parens ::= LPAREN expr recover_parens RPAREN { pin=1 }
set ::= [ REC ] LCURLY recover_set (bind recover_set)* RCURLY { pin=2 }
list ::= LBRAC recover_list (expr_select recover_list)* RBRAC { pin=1 }
legacy_let ::= LET LCURLY recover_set (bind recover_set)* RCURLY { pin=2 }
private recover_parens ::= { recoverWhile=paren_recover }
private recover_set ::= { recoverWhile=set_recover }
private recover_list ::= { recoverWhile=list_recover }
private set_recover ::= curly_recover !bind
private list_recover ::= brac_recover !expr_select
;{ extends(".*_string")="string" }
string ::= std_string | ind_string
std_string ::= STRING_OPEN string_part* STRING_CLOSE { pin=1 }
ind_string ::= IND_STRING_OPEN string_part* IND_STRING_CLOSE { pin=1 }
;{ extends("string_text|antiquotation")=string_part }
string_part ::= string_text | antiquotation { recoverWhile=string_part_recover }
string_text ::= STR | IND_STR
antiquotation ::= DOLLAR LCURLY expr recover_antiquotation RCURLY { pin=1 }
private recover_antiquotation ::= { recoverWhile=curly_recover }
private string_part_recover ::= !(STR | IND_STR | DOLLAR | STRING_CLOSE | IND_STRING_CLOSE)
;{ extends("bind_attr|bind_inherit")=bind }
bind ::= bind_attr | bind_inherit
bind_attr ::= attr_path ASSIGN bind_value SEMI { pin=2 }
bind_value ::= <<parseBindValue expr0>> { elementType=expr }
bind_inherit ::= INHERIT [ LPAREN expr RPAREN ] attr* SEMI { pin=1 }
// Is used in various rules just to provide a better error message when a
// semicolon is missing. Must always be used with `!`.
private missing_semi ::= <<parseIsBindValue>> ( RCURLY | IN | bind )
;{ extends(".*_attr")=attr }
attr ::= std_attr | string_attr
std_attr ::= ID | OR_KW
string_attr ::= std_string | antiquotation
attr_path ::= attr ( DOT attr )*
// The lexer uses curly braces to determine its state. To avoid inconsistencies
// between the parser and lexer (i.e. the lexer sees a string where the parser
// wants to read an expression), the error recovery for curly braces is very
// strict while no other error recovery will consume curly braces.
private braces_recover ::= !(RPAREN | RCURLY | RBRAC)
private paren_recover ::= !(RPAREN | RCURLY)
private curly_recover ::= !RCURLY
private brac_recover ::= !(RBRAC | RCURLY)