A fast implementation of the Nix expression language
Revisão | 476522045be45336529417d240954a5b0e91ff66 (tree) |
---|---|
Hora | 2024-06-14 11:29:28 |
Autor | Corbin <cds@corb...> |
Commiter | Corbin |
parser: Make string QLs left-recursive.
And, with this, there's no longer any good explanation for why we cannot
match formals and binds at the same top-level.
@@ -180,16 +180,17 @@ class AppBox(EBox): | ||
180 | 180 | compiler.mkApp() |
181 | 181 | |
182 | 182 | class StrQLBox(EBox): |
183 | - def __init__(self, init, pairs): | |
184 | - self.init = init | |
183 | + def __init__(self, pairs, tail): | |
185 | 184 | self.pairs = pairs |
185 | + self.tail = tail | |
186 | 186 | def pretty(self): |
187 | - rv = [self.init] | |
188 | - for expr, piece in self.pairs: | |
187 | + rv = [] | |
188 | + for piece, expr in self.pairs: | |
189 | + rv.append(piece) | |
189 | 190 | rv.append("${ ") |
190 | 191 | rv.append(expr.pretty()) |
191 | 192 | rv.append(" }") |
192 | - rv.append(piece) | |
193 | + rv.append(self.tail) | |
193 | 194 | return '"%s"' % "".join(rv) |
194 | 195 | # def compile(self, compiler): |
195 | 196 | # l = [heap.HeapStr(self.init)] |
@@ -439,23 +440,22 @@ def exprSimpleInt(p): return IntBox(int(p[0].getstr())) | ||
439 | 440 | @pg.production("expr_simple : STRING") |
440 | 441 | def exprQuoted(p): return StrBox(trimBox(p[0], 1, -1)) |
441 | 442 | |
442 | -@pg.production("expr_simple : STRING_INIT string_ql") | |
443 | -def stringQL(p): | |
444 | - s = trimBox(p[0], 1, -2) | |
445 | - incomplete = p[1] | |
446 | - assert isinstance(incomplete, IncompleteQL) | |
447 | - return StrQLBox(s, incomplete.pairs) | |
443 | +@pg.production("string_ql : STRING_INIT expr") | |
444 | +def stringQL(p): return IncompleteQL([(trimBox(p[0], 1, -2), p[1])]) | |
448 | 445 | |
449 | -# XXX should be left-recursive? Requires refactoring all string_ql rules | |
450 | -@pg.production("string_ql : expr STRING_PIECE string_ql") | |
446 | +@pg.production("string_ql : string_ql STRING_PIECE expr") | |
451 | 447 | def stringQLPiece(p): |
452 | - incomplete = p[2] | |
448 | + incomplete = p[0] | |
453 | 449 | assert isinstance(incomplete, IncompleteQL) |
454 | - pairs = [(p[0], trimBox(p[1], 1, -2))] + incomplete.pairs | |
450 | + pairs = incomplete.pairs + [(trimBox(p[1], 1, -2), p[2])] | |
455 | 451 | return IncompleteQL(pairs) |
456 | 452 | |
457 | -@pg.production("string_ql : expr STRING_END") | |
458 | -def stringQLEnd(p): return IncompleteQL([(p[0], trimBox(p[1], 1, -1))]) | |
453 | +@pg.production("expr_simple : string_ql STRING_END") | |
454 | +def stringQLEnd(p): | |
455 | + incomplete = p[0] | |
456 | + assert isinstance(incomplete, IncompleteQL) | |
457 | + s = trimBox(p[1], 1, -1) | |
458 | + return StrQLBox(incomplete.pairs, s) | |
459 | 459 | |
460 | 460 | @pg.production("expr_simple : URI") |
461 | 461 | def exprURI(p): return StrBox(p[0].getstr()) |