1 : %glr-parser
2 : %pure-parser
3 : %locations
4 : %error-verbose
5 : %parse-param { yyscan_t scanner }
6 : %parse-param { void * data }
7 : %lex-param { yyscan_t scanner }
8 :
9 : %{
10 : #include <stdio.h>
11 : #include <stdlib.h>
12 : #include <string.h>
13 : #include <aterm2.h>
14 :
15 : #include "parser-tab.h"
16 : #include "lexer-tab.h"
17 :
18 : typedef ATerm Expr;
19 : typedef ATerm Pos;
20 :
21 : #include "nixexpr-ast.hh"
22 :
23 : void setParseResult(void * data, ATerm t);
24 : void parseError(void * data, char * error, int line, int column);
25 : ATerm absParsedPath(void * data, ATerm t);
26 : ATerm fixAttrs(int recursive, ATermList as);
27 : const char * getPath(void * data);
28 :
29 : void yyerror(YYLTYPE * loc, yyscan_t scanner, void * data, char * s)
30 0 : {
31 0 : parseError(data, s, loc->first_line, loc->first_column);
32 : }
33 :
34 : ATerm toATerm(const char * s)
35 147 : {
36 147 : return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s, 0, ATtrue));
37 : }
38 :
39 : static Pos makeCurPos(YYLTYPE * loc, void * data)
40 141 : {
41 141 : return makePos(toATerm(getPath(data)),
42 : loc->first_line, loc->first_column);
43 : }
44 :
45 : #define CUR_POS makeCurPos(yylocp, data)
46 :
47 : %}
48 :
49 : %union {
50 : ATerm t;
51 : ATermList ts;
52 : }
53 :
54 : %type <t> start expr expr_function expr_if expr_op
55 : %type <t> expr_app expr_select expr_simple bind inheritsrc formal
56 : %type <ts> binds ids expr_list formals
57 : %token <t> ID INT STR PATH URI
58 : %token IF THEN ELSE ASSERT WITH LET REC INHERIT EQ NEQ AND OR IMPL
59 :
60 : %nonassoc IMPL
61 : %left OR
62 : %left AND
63 : %nonassoc EQ NEQ
64 : %right UPDATE
65 : %left NEG
66 : %left '+'
67 : %nonassoc '?'
68 : %nonassoc '~'
69 :
70 : %%
71 :
72 23 : start: expr { setParseResult(data, $1); };
73 23 :
74 : expr: expr_function;
75 :
76 : expr_function
77 : : '{' formals '}' ':' expr_function
78 6 : { $$ = makeFunction($2, $5, CUR_POS); }
79 6 : | ID ':' expr_function
80 10 : { $$ = makeFunction1($1, $3, CUR_POS); }
81 10 : | ASSERT expr ';' expr_function
82 7 : { $$ = makeAssert($2, $4, CUR_POS); }
83 7 : | WITH expr ';' expr_function
84 0 : { $$ = makeWith($2, $4, CUR_POS); }
85 0 : | expr_if
86 : ;
87 :
88 : expr_if
89 : : IF expr THEN expr ELSE expr
90 8 : { $$ = makeIf($2, $4, $6); }
91 8 : | expr_op
92 : ;
93 :
94 : expr_op
95 0 : : '!' expr_op %prec NEG { $$ = makeOpNot($2); }
96 0 : | expr_op EQ expr_op { $$ = makeOpEq($1, $3); }
97 2 : | expr_op NEQ expr_op { $$ = makeOpNEq($1, $3); }
98 7 : | expr_op AND expr_op { $$ = makeOpAnd($1, $3); }
99 4 : | expr_op OR expr_op { $$ = makeOpOr($1, $3); }
100 1 : | expr_op IMPL expr_op { $$ = makeOpImpl($1, $3); }
101 7 : | expr_op UPDATE expr_op { $$ = makeOpUpdate($1, $3); }
102 0 : | expr_op '~' expr_op { $$ = makeSubPath($1, $3); }
103 0 : | expr_op '?' ID { $$ = makeOpHasAttr($1, $3); }
104 0 : | expr_op '+' expr_op { $$ = makeOpPlus($1, $3); }
105 7 : | expr_app
106 : ;
107 :
108 : expr_app
109 : : expr_app expr_select
110 49 : { $$ = makeCall($1, $2); }
111 49 : | expr_select { $$ = $1; }
112 207 : ;
113 :
114 : expr_select
115 : : expr_select '.' ID
116 11 : { $$ = makeSelect($1, $3); }
117 11 : | expr_simple { $$ = $1; }
118 323 : ;
119 :
120 : expr_simple
121 119 : : ID { $$ = makeVar($1); }
122 119 : | INT { $$ = makeInt(ATgetInt((ATermInt) $1)); }
123 9 : | STR { $$ = makeStr($1); }
124 98 : | PATH { $$ = makePath(absParsedPath(data, $1)); }
125 19 : | URI { $$ = makeUri($1); }
126 8 : | '(' expr ')' { $$ = $2; }
127 9 : /* Let expressions `let {..., body = ...}' are just desugared
128 : into `(rec {..., body = ...}).body'. */
129 : | LET '{' binds '}'
130 6 : { $$ = makeSelect(fixAttrs(1, $3), toATerm("body")); }
131 6 : | REC '{' binds '}'
132 2 : { $$ = fixAttrs(1, $3); }
133 2 : | '{' binds '}'
134 24 : { $$ = fixAttrs(0, $2); }
135 24 : | '[' expr_list ']' { $$ = makeList($2); }
136 29 : ;
137 :
138 : binds
139 118 : : binds bind { $$ = ATinsert($1, $2); }
140 118 : | { $$ = ATempty; }
141 32 : ;
142 :
143 : bind
144 : : ID '=' expr ';'
145 112 : { $$ = makeBind($1, $3, CUR_POS); }
146 112 : | INHERIT inheritsrc ids ';'
147 6 : { $$ = makeInherit($2, $3, CUR_POS); }
148 6 : ;
149 :
150 : inheritsrc
151 1 : : '(' expr ')' { $$ = $2; }
152 1 : | { $$ = makeScope(); }
153 5 : ;
154 :
155 22 : ids: ids ID { $$ = ATinsert($1, $2); } | { $$ = ATempty; };
156 22 :
157 : expr_list
158 67 : : expr_select expr_list { $$ = ATinsert($2, $1); }
159 67 : /* yes, this is right-recursive, but it doesn't matter since
160 : otherwise we would need ATreverse which requires unbounded
161 : stack space */
162 29 : | { $$ = ATempty; }
163 29 : ;
164 :
165 : formals
166 16 : : formal ',' formals { $$ = ATinsert($3, $1); } /* idem - right recursive */
167 16 : | formal { $$ = ATinsert(ATempty, $1); }
168 6 : ;
169 :
170 : formal
171 11 : : ID { $$ = makeNoDefFormal($1); }
172 11 : | ID '?' expr { $$ = makeDefFormal($1, $3); }
173 2191 : ;
174 :
175 : %%
|