~ehmry/dhall-nim

ref: staging dhall-nim/src/dhall/parse.nim -rw-r--r-- 28.5 KiB
b4903660Emery Hemingway Add main proc to parser 8 months ago
                                                                                
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
import terms

import bigints, npeg, npeg/lib/uri, npeg/lib/utf8

import algorithm, math, options, parseutils, strutils, tables, unicode

func isHex(s: string): bool {.inline.} =
  s.len > 2 and s[0] == '0' and s[1] == 'x'

func parseBigInt(s: string): BigInt =
  if s.isHex:
    initBigInt(s[2..s.high], 16)
  else:
    initBigInt(s, 10)

type
  Frame = tuple[term: Term, pos: int]
  Stack = seq[Frame]

template backtrack(n = 1) =
  stack.setLen(stack.len-n)
  fail()

func flattenOperator(stack: var Stack; kind: OpKind; n: int) =
  assert(n < stack.len)
  let off = stack.high-n
  for i in off+1..stack.high:
    stack[off].term = Term(kind: tOp, op: kind,
        opL: stack[off].term,
        opR: stack[i].term)
  stack.setLen(stack.len-n)

template flattenOperator(stack: Stack; kind: OpKind) =
  let n = capture.len-1
  if n > 0:
    flattenOperator(stack, kind, n)

template peek(): var Term = stack[stack.high].term
template pop(): Term = pop(stack).term
template push(t: Term) = stack.add((t, capture[0].si))

template appendTextLiteral(s: string) =
  # TODO: optimize!
  #[
  if stack.len > 0 and
      peek().kind == tTextChunk and
          peek().textExpr.isNil:
    peek().textPrefix.add s
  else:
  ]#
    push Term(kind: tTextChunk, textPrefix: s)

func joinTextChunks(stack: var Stack; pos: int) =
  var n: int
  while n < stack.len and
      stack[stack.high-n].pos > pos and
      stack[stack.high-n].term.kind == tTextChunk:
    inc n
  let t = Term(kind: tTextLiteral,
      textChunks: newSeqOfCap[Term](n), textSuffix: "")
  let chunkOff = stack.len-n
  var tmp = ""
  for i in 0..<n:
    let tc = stack[chunkOff+i].term
    tmp.add(tc.textPrefix)
    if not tc.textExpr.isNil:
      t.textChunks.add Term(kind: tTextChunk,
        textPrefix: move tmp, textExpr: tc.textExpr)
  t.textSuffix = move tmp
  stack.setLen(succ chunkOff)
  stack[chunkOff].term = t

type IndentParser = tuple[indent: string, tailLine: bool]

# TODO: break windows compatiblity in release builds

func parse(ip: var IndentParser; s: string) =
  ## Determine the common indentation level of ``s``.
  var i: int
  if not ip.tailLine:
    i = s.skipWhile({'\r', '\n'}, 0)
      # Skip empty lines.
    i.inc(s.parseWhile(ip.indent, {'\t', ' '}, i))
      # Take the leading whitespace of the first non-empty line.
    ip.tailLine = true
  while i < s.len:
    let lineLen = s.skipUntil({'\r', '\n'}, i)
    i.inc(lineLen)
    i.inc(s.skipWhile({'\r', '\n'}, i))
      # Skip empty lines.
    let remain = s.len-i
    if 0 < remain and remain < ip.indent.len:
      ip.indent.setLen(remain)
    for j in 0..ip.indent.high:
      # Drop characters from indentation
      # not matching successive lines.
      if i+j < s.len and ip.indent[j] != s[i+j]:
        ip.indent.setLen(j)
        break
    i.inc(ip.indent.len)

func dedent(headLine: var bool; s: var string; n: int) =
  ## Remove ``n`` leading whitespace characters from every line.
  assert(n > 0)
  var j: int
  var i = if headLine: n else: 0
  headLine = false
  while i < s.len:
    while i < s.len and s[i] notin {'\r', '\n'}:
      s[j] = s[i]
      inc j
      inc i
    while i < s.len and s[i] in {'\r', '\n'}:
      if s[i] != '\r':
        s[j] = '\n'
        inc j
      inc i
    inc i, n
  s.setLen(j)

const parser = peg("final_expression", stack: Stack):

  final_expression <- complete_expression * !1

  complete_expression <- whsp * expression * whsp

  expression <-
      lamba_expression |
      if_expression |
      let_bindings |
      forall_expression |
      arrow_expression |
      with_expression |
      merge_annotated_expression |
      empty_list_literal |
      toMap_annotated_expression |
      assert_expression |
      annotated_expression

  lamba_expression <- lambda * whsp * '(' * whsp * nonreserved_label * whsp *
      ':' * whsp1 * expression * whsp * ')' * whsp * arrow * whsp * expression:
    push Term(kind: tLambda,
        funcLabel: $1, funcBody: pop(), funcType: pop())

  if_expression <- If * whsp1 * expression * whsp * Then * whsp1 * expression *
      whsp * Else * whsp1 * expression:
    push Term(kind: tIf, ifFalse: pop(), ifTrue: pop(),
        ifCond: pop())

  let_bindings <- +let_binding * In * whsp1 * expression:
    var n: int
    for i in countDown(stack.high.pred, 0):
      if stack[i].term.kind != tLetBinding:
        break
      inc n
    var t = Term(kind: tLet, letBinds: newSeq[Term](n), letBody: pop())
    for i in 0..<n:
      t.letBinds[i] = stack[stack.len-n+i].term
    if t.letBody.kind == tLet:
      t.letBinds = t.letBinds & t.letBody.letBinds
      t.letBody = t.letBody.letBody
    stack.setLen(stack.len-n)
    push t

  forall_expression <- forall * whsp * '(' * whsp * (>'_' | nonreserved_label) * whsp * ':' * whsp1 * expression * whsp * ')' * whsp * arrow * whsp * expression:
    push Term(kind: tPi,
        funcBody: pop(),
        funcType: pop(),
        funcLabel: $1)

  toMap_annotated_expression <- toMap_expression * ?(whsp * >':' * whsp1 * application_expression):
    if capture.len != 2:
      backtrack()
    stack[pred stack.high].term.toMapAnn = some pop()

  Import <- import_hashed * ?(whsp * As * whsp1 * >(Text | Location)):
    case capture.len
    of 1: discard
    of 2:
      peek().importKind = case $1
        of "Text": iText
        of "Location": iLocation
        else: fail()
    else: fail()

  assert_expression <- Assert * whsp * ':' * whsp1 * expression:
    push Term(kind: tAssert, assertAnn: pop())

  annotated_expression <- operator_expression * ?(whsp * >':' * whsp1 * expression):
    case capture.len
    of 1: discard
    of 2:
      push Term(kind: tAnnotation,
          annAnn: pop(), annExpr: pop())
    else: fail()

  let_binding <- Let * whsp1 * nonreserved_label * whsp *
      ?( >':' * whsp1 * expression * whsp) * '=' * whsp * expression * whsp:
    case capture.len
    of 2:
      push Term(kind: tLetBinding,
          letKey: $1, letVal: pop())
    of 3:
      push Term(kind: tLetBinding,
          letKey: $1, letVal: pop(), letAnn: some pop())
    else: fail()

  arrow_expression <- operator_expression * ?(whsp * >arrow * whsp * expression):
    # TODO: bypass operator_expression?
    if capture.len != 2:
      backtrack()
    push Term(kind: tPi,
        funcLabel: "_", funcBody: pop(), funcType: pop())

  with_clause <- any_label_or_some * *(whsp * '.' * whsp * any_label_or_some) *
      whsp * '=' * whsp * operator_expression:
    var fields = newSeq[string](capture.len-1)
    for i in 1..<capture.len:
      fields[pred i] = capture[i].s
    let t = Term(kind: tWith, withFields: fields, withUpdate: pop())
    push(t)

  with_expression <- import_expression * *(whsp1 * >with * whsp1 * with_clause):
    if capture.len == 1:
      backtrack()
    let pos = capture[0].si
    var stackOff = stack.high.pred
    while stack[stackOff].pos > pos and stack[stackOff].term.kind == tWith:
      dec stackOff
    var expr = stack[stackOff].term
    for i in stackOff.succ..stack.high:
      var next = move stack[i].term
      next.withExpr = expr
      expr = next
    stack.setLen(stackOff)
    push(expr)

  merge_annotated_expression <- merge_expression * ?(whsp * >':' * whsp1 *
      application_expression):
    if capture.len != 2:
      backtrack()
    stack[pred stack.high].term.mergeAnn = some pop()

  empty_list_literal <- '[' * whsp * ?(',' * whsp) * ']' * whsp * ':' * whsp1 * application_expression:
    var listType = pop()
    if listType.kind == tApp and
        listType.appFun.kind == tBuiltin and
            listType.appFun.builtin == bList:
      push Term(kind: tList, listType: some(listType.appArg))
    else:
      push Term(kind: tEmptyList, emptyListType: listType)

  single_quote_continue <- interpolation * single_quote_continue |
      escaped_quote_pair * single_quote_continue |
      escaped_interpolation * single_quote_continue |
      "''" |
      single_quote_char * single_quote_continue

  double_quote_chunk <- interpolation | double_quote_escaping | double_quote_char

  double_quote_escaping <- '\\' * double_quote_escaped

  double_quote_char <- +({'\x20'..'\x21'} | '\x23' | {'\x25'..'\x5b'} | {'\x5d'..'\x7a'} | valid_non_ascii) | '$' | {'\x7b'..'\x7e'}:
    appendTextLiteral($0)

  double_quote_escaped <- double_quote_escaped_char | double_quote_escaped_unicode

  double_quote_escaped_char <- '"' | '$' | '\\' | '/' | 'b' | 'f' | 'n' | 'r' | 't':
    case $0
    of "b": appendTextLiteral("\b")
    of "f": appendTextLiteral("\f")
    of "n": appendTextLiteral("\n")
    of "r": appendTextLiteral("\r")
    of "t": appendTextLiteral("\t")
    else: appendTextLiteral($0)

  double_quote_escaped_unicode <- 'u' * unicode_escape:
    var r: uint32
    validate(parseHex($1, r) == len($1))
    appendTextLiteral(Rune(r).toUtf8)

  unicode_escape <- >unbraced_escape | ('{' * >braced_escape * '}')

  unicode_suffix <- ((Digit | {'A'..'E'}) * Xdigit[3]) | ('F' * Xdigit[2] * (Digit | {'A'..'D'}))

  unbraced_escape <- ((Digit | {'A'..'C'}) * Xdigit[3]) |
      ('D' * {'0'..'7'} * Xdigit[2]) |
      ('E' * Xdigit[3]) |
      ('F' * Xdigit[2] * (Digit | {'A'..'D'}))

  braced_codepoint <- (({'1'..'9'} | {'A'..'F'} | "10") * unicode_suffix) | unbraced_escape | Xdigit[1..3]

  braced_escape <- *'0' * braced_codepoint

  escaped_quote_pair <- "'''":
    appendTextLiteral("''")

  escaped_interpolation <- "''${":
    appendTextLiteral("${")

  single_quote_char <- Print | valid_non_ascii | tab | end_of_line:
    appendTextLiteral($0)

  interpolation <- "${" * complete_expression * '}':
    let textExpr = pop()
    if stack.len > 0 and
        peek().kind == tTextChunk and
            peek().textExpr.isNil:
      peek().textExpr = textExpr
    else:
      push Term(kind: tTextChunk, textExpr: textExpr)

  pos_or_neg <- '+' | '-'

  exponent <- 'e' * ?pos_or_neg * +Digit

  numeric_double_literal <- ?pos_or_neg * +Digit * (('.' * +Digit * ?exponent) | exponent):
    var t = Term(kind: tDoubleLiteral)
    if parseBiggestFloat($0, t.double) > 0 and
        classify(t.double) in {fcNormal, fcZero, fcNegZero}:
      push t
    else:
      validate(false)

  minus_infinity_literal <- '-' * Infinity:
    push Term(kind: tDoubleLiteral, double: system.NegInf)

  plus_infinity_literal <- Infinity:
    push Term(kind: tDoubleLiteral, double: system.Inf)

  NaN_literal <- NaN:
    push Term(kind: tDoubleLiteral, double: system.NaN)

  import_type <- missing | local | http | env

  path <- +path_component:
    let t = Term(kind: tImport,
        importElements: newSeq[string](capture.len-1))
    for i in 1..<capture.len:
      t.importElements[pred i] = capture[i].s
    push t

  path_component <- '/' * ( >unquoted_path_component | ('"' * >quoted_path_component * '"') )

  quoted_path_component <- +quoted_path_character

  quoted_path_character <- '\x20' | '\x21' | {'\x23'..'\x2e'} | {'\x30'..'\x7e'} | valid_non_ascii

  unquoted_path_component <- +path_character

  path_character <- '\x21' |
    {'\x24'..'\x27'} |
    {'\x2a'..'\x2b'} |
    {'\x2d'..'\x2e'} |
    {'\x30'..'\x3b'} |
    '\x3d' |
    {'\x40'..'\x5a'} |
    {'\x5e'..'\x7a'} |
    '\x7c' |
    '\x7e'

  local <- parent_path | here_path | home_path | absolute_path

  absolute_path <- path:
    peek().importScheme = iAbs

  here_path <- '.' * path:
    peek().importScheme = iHere

  parent_path <- ".." * path:
    peek().importScheme = iParent

  home_path <- '~' * path:
    peek().importScheme = iHome

  scheme <- "http" * ?'s'

  query <- *(uri.pchar | "/" | "|" | "?")

  http_raw <- >scheme * "://" * ( > uri.authority * >uri.path) * ?('?' * >query):
    let t = Term(kind: tImport,
        importScheme: case $1
            of "http": iHttp
            of "https": iHttps
            else: fail())
    if $3 != "":
      t.importElements = split($3, Rune('/'))
      t.importElements[0] = $2
    else:
      t.importElements = @[$2, ""]
    if capture.len > 4:
      t.importQuery = some $4
    push t

  http <- http_raw * ?(whsp * >Using * whsp1 * import_expression):
    case capture.len
    of 1: discard
    of 2:
      assert(stack.len > 1)
      stack[pred stack.high].term.importHeaders = some pop()
    else: fail()

  env <- "env:" * (bash_environment_variable | posix_environment_variable)

  missing <- "missing" * !simple_label_next_char:
    push Term(kind: tImport, importScheme: iMiss)

  bash_environment_variable <- >( (Alpha | '_') * *(Alnum | '_') ):
    push Term(kind: tImport,
        importScheme: iEnv, importElements: @[$1])

  posix_environment_variable <- '"' * >( +posix_environment_variable_character ) * '"':
    let s = $1
    if s[s.high] == '\\': fail()
    var ev = newStringOfCap(s.len)
    var i = 0
    while i < s.high:
      if s[i] == '\\':
        case s[i+1]:
        of '"': ev.add('"')
        of '\\': ev.add('\\')
        of 'a': ev.add('\x07')
        of 'b': ev.add('\x08')
        of 'f': ev.add('\x0c')
        of 'n': ev.add('\x0a')
        of 'r': ev.add('\x0d')
        of 't': ev.add('\x09')
        of 'v': ev.add('\x0b')
        else: fail()
        inc(i, 2)
      else:
        ev.add(s[i])
        inc(i)
    ev.add(s[s.high])
    push Term(kind: tImport,
        importScheme: iEnv, importElements: @[ev])

  posix_environment_variable_character <- ('\\' *
      ('"' | '\\' | 'a' | 'b' | 'f' | 'n' | 'r' | 't' | 'v')) |
      {'\x20'..'\x21'} |
      {'\x23'..'\x3c'} |
      {'\x3e'..'\x5b'} |
      {'\x5d'..'\x7e'}

  hash <- "sha256:" * >Xdigit[64]:
    var check = newSeq[byte](32)
    for i in 0..31:
      validate(parseHex($1, check[i], 2*i, 2) == 2)
    peek().importCheck = check

  import_hashed <- import_type * ?(whsp1 * hash)

  not_equal_expression <- application_expression * *(whsp * >"!=" * whsp * application_expression):
    stack.flattenOperator(opBoolInequality)

  equal_expression <- not_equal_expression * *(whsp * >"==" * whsp * not_equal_expression):
    stack.flattenOperator(opBoolEquality)

  times_expression <- equal_expression * *(whsp * >'*' * whsp * equal_expression):
    stack.flattenOperator(opNaturalMultiplication)

  combine_types_expression <- times_expression * *(whsp * >combine_types * whsp * times_expression):
    stack.flattenOperator(opRecordTypeMerge)

  prefer_expression <- combine_types_expression * *(whsp * >prefer * whsp * combine_types_expression):
    stack.flattenOperator(opRecordBiasedMerge)

  combine_expression <- prefer_expression * *(whsp * >combine * whsp * prefer_expression):
    stack.flattenOperator(opRecordRecursiveMerge)

  and_expression <- combine_expression * *(whsp * >"&&" * whsp * combine_expression):
    stack.flattenOperator(opBoolAnd)

  list_append_expression <- and_expression * *(whsp * >'#' * whsp * and_expression):
    stack.flattenOperator(opListAppend)

  text_append_expression <- list_append_expression * *(whsp * >"++" * whsp * list_append_expression):
    stack.flattenOperator(opTextAppend)

  plus_expression <- text_append_expression * *(whsp * >'+' * whsp1 * text_append_expression):
    stack.flattenOperator(opNaturalAdd)

  or_expression <- plus_expression * *(whsp * >"||" * whsp * plus_expression):
    stack.flattenOperator(opBoolOr)

  import_alt_expression <- or_expression * *(whsp * >'?' * whsp1 * or_expression):
    stack.flattenOperator(opAlternateImport)

  equivalence_expression <- import_alt_expression * *(whsp * >equivalence * whsp * import_alt_expression):
    stack.flattenOperator(opEquivalience)

  operator_expression <- equivalence_expression

  first_application_expression <-
      merge_expression |
      Some_expression |
      toMap_expression |
      import_expression

  application_expression <- first_application_expression * *(whsp1 * >import_expression):
    if capture.len > 1:
      let stackOff = stack.high - (capture.len - 1)
      var app = stack[stackOff].term
      for i in stackOff+1..stack.high:
        app = Term(kind: tApp, appFun: app, appArg: stack[i].term)
      stack.setLen(stackOff+1)
      stack[stackOff].term = app

  merge_expression <- merge * whsp1 * import_expression * whsp1 * import_expression:
    push Term(kind: tMerge,
        mergeUnion: pop(), mergeHandler: pop())

  Some_expression <- Some * whsp1 * import_expression:
    push Term(kind: tSome, someVal: pop())

  toMap_expression <- toMap * whsp1 * import_expression:
    push Term(kind: tToMap, toMapBody: pop())

  primitive_expression <-
      double_literal |
      natural_literal |
      integer_literal |
      text_literal |
      bool_literal |
      record_literal |
      record_type |
      union_type |
      non_empty_list_literal |
      identifier |
      ('(' * complete_expression * ')')

  selector_expression <- primitive_expression * *(whsp * '.' * whsp * selector)
    # NOTE: Backtrack when parsing the `*("." ...)`.  The reason why is that you
    # can't tell from parsing just the period whether "foo." will become "foo.bar"
    # (i.e. accessing field `bar` of the record `foo`) or `foo./bar` (i.e. applying
    # the function `foo` to the relative path `./bar`)

  completion_expression <- selector_expression * ?( whsp * >complete * whsp * selector_expression ):
    if capture.len == 2:
      push Term(kind: tOp, op: opComplete, opR: pop(), opL: pop())

  import_expression <- Import | completion_expression

  false_literal <- False * !simple_label_next_char:
    push Term(kind: tBoolLiteral, bool: false)

  true_literal <- True * !simple_label_next_char:
    push Term(kind: tBoolLiteral, bool: true)

  bool_literal <- false_literal | true_literal

  double_literal <- numeric_double_literal |
      minus_infinity_literal | plus_infinity_literal | NaN_literal

  integer_literal <- >pos_or_neg * >natural:
    var t = Term(kind: tIntegerLiteral, integer: parseBigInt($2))
    if $1 == "-": t.integer.flags = {Negative}
    push t

  natural_literal <- >natural:
    push Term(kind: tNaturalLiteral, natural: parseBigInt($1))

  text_literal <- double_quote_literal | single_quote_literal

  double_quote_literal <- '"' * *double_quote_chunk * '"':
    joinTextChunks(stack, capture[0].si)

  single_quote_literal <- "''" * end_of_line * single_quote_continue:
    joinTextChunks(stack, capture[0].si)
    # Longest common whitespace indentation
    # is stripped from single quote literals.
    let literal = peek()
    if not literal.textSuffix.endsWith('\n'):
      # Only if the closing '' has leading whitespace.
      var ip: IndentParser
      for tc in literal.textChunks:
        ip.parse(tc.textPrefix)
      ip.parse(literal.textSuffix)
      if 0 < ip.indent.len:
        var headLine = true
        for tc in literal.textChunks.mitems:
          dedent(headLine, tc.textPrefix, ip.indent.len)
        dedent(headLine, literal.textSuffix, ip.indent.len)

  identifier <- variable | identifier_builtin

  identifier_builtin <- builtin:
    push Term(kind: tBuiltin, builtin: parseBuiltin($0))

  variable <- nonreserved_label * ?(whsp * '@' * whsp * >natural):
    var t = Term(kind: tVar, varName: $1)
    if capture.len == 3:
      let n = if isHex($2):
          parseHex($2, t.varIndex)
        else:
          parseInt($2, t.varIndex)
      validate(n == len($2))
    push t

  natural <- ("0x" * +Xdigit) | (!'0' * {'1'..'9'} * *Digit) | ('0' * !Digit)

  selector <- label_selector | fields_selector | type_selector

  label_selector <- any_label:
    for i in 1..<capture.len:
      push Term(kind: tField, fieldRecord: pop(), fieldName: capture[i].s)

  labels <- '{' * whsp * ?( ',' * whsp ) * ?( any_label_or_some * whsp * *(',' * whsp * any_label_or_some * whsp) * ?( ',' * whsp )) * '}'

  fields_selector <- labels:
    var t = Term(kind: tProject,
        projectRecord: pop(),
        projectNames: newSeq[string](pred capture.len))
    for i in 1..<capture.len:
      t.projectNames[pred i] = capture[i].s
    push t

  type_selector <- '(' * whsp * expression * whsp * ')':
    push Term(kind: tProjectType,
        projectTypeSelector: pop(),
        projectTypeRecord: pop())

  record_type <- '{' * whsp * ?( ',' * whsp ) * (non_empty_record_type | empty_record_type) * whsp * '}'

  record_literal <- '{' * whsp * ?( ',' * whsp ) * (non_empty_record_literal | empty_record_literal) * whsp * '}'

  empty_record_literal <- '=' * ?( whsp * ',' ):
    push Term(kind: tRecordLiteral, table: initTable[string, Term](2))

  empty_record_type <- 0:
    push Term(kind: tRecordType, table: initTable[string, Term](2))

  non_empty_record_type <- record_type_entry * *(whsp * ',' * whsp * record_type_entry) * ?( whsp * ',' ):
    let pos = capture[0].si
    var n: int
    while n < stack.len and
        stack[stack.high-n].pos >= pos and
        stack[stack.high-n].term.kind == tRecordBinding:
      inc n
    var t = Term(kind: tRecordType,
        table: initTable[string, Term](nextPowerOfTwo n))
    for i in stack.len-n..stack.high:
      if t.table.hasKey stack[i].term.recKey:
        t.table[stack[i].term.recKey] = Term(kind: tOp,
            op: opRecordTypeMerge,
            opL: t.table[stack[i].term.recKey],
            opR: stack[i].term.recVal)
      else:
        t.table.add(stack[i].term.recKey, stack[i].term.recVal)
    stack.setLen(stack.len-n)
    push t

  record_type_entry <- any_label_or_some * whsp * ':' * whsp1 * expression:
    push Term(kind: tRecordBinding, recKey: $1, recVal: pop())

  non_empty_record_literal <- record_literal_entry * *(whsp * >',' * whsp * record_literal_entry) * ?( whsp * ',' ):
    let pos = capture[0].si
    var n: int
    while n < stack.len and
        stack[stack.high-n].pos >= pos and
        stack[stack.high-n].term.kind == tRecordBinding:
      inc n
    var t = Term(kind: tRecordLiteral,
        table: initTable[string, Term](nextPowerOfTwo n))
    for i in stack.len-n..stack.high:
      if t.table.hasKey stack[i].term.recKey:
        t.table[stack[i].term.recKey] = Term(kind: tOp,
            op: opRecordRecursiveMerge,
            opL: t.table[stack[i].term.recKey],
            opR: stack[i].term.recVal)
      else:
        t.table.add(stack[i].term.recKey, stack[i].term.recVal)
    stack.setLen(stack.len-n)
    push t

  record_literal_entry <- record_literal_normal_entry | record_literal_punned_entry

  record_literal_normal_entry <- any_label_or_some * *(whsp * '.' * whsp * any_label_or_some) * whsp * '=' * whsp * expression:
    var t = pop()
    for i in countDown(capture.len.pred, 2):
      t = Term(kind: tRecordLiteral,
          table: toTable [(capture[i].s, t)])
    push Term(kind: tRecordBinding, recKey: $1, recVal: t)

  record_literal_punned_entry <- any_label_or_some * !(whsp * ':') * 0:
    var t = Term(kind: tVar, varName: $1)
    push Term(kind: tRecordBinding, recKey: $1, recVal: t)

  union_type <- '<' * whsp * ?( '|' * whsp ) * (non_empty_union_type | empty_union_type) * whsp * '>'

  empty_union_type <- 0:
    push Term(kind: tUnionType, table: initTable[string, Term](2))

  non_empty_union_type <- union_type_entry * *(whsp * >'|' * whsp * union_type_entry) * ?( whsp * '|' ):
    let pos = capture[0].si
    var n: int
    while n < stack.len and
        stack[stack.high-n].pos >= pos and
        stack[stack.high-n].term.kind == tRecordBinding:
      inc n
    var t = Term(kind: tUnionType,
        table: initTable[string, Term](nextPowerOfTwo n))
    for i in stack.len-n..stack.high:
      validate(not t.table.hasKey(stack[i].term.recKey))
      t.table.add(stack[i].term.recKey, stack[i].term.recVal)
    stack.setLen(stack.len-n)
    push t

  union_type_entry <- any_label_or_some * ?(whsp * >':' * whsp1 * expression):
    let t = Term(kind: tRecordBinding,
        recKey: $1,
        recVal: case capture.len
      of 2: nil
      of 3: pop()
      else: fail())
    push(t)

  non_empty_list_literal <- '[' * whsp * ?( ',' * whsp ) * expression * whsp * *(',' * whsp * expression * whsp) * ?( ',' * whsp ) * ']':
    let pos = capture[0].si
    var n: int
    while n < stack.len and
        stack[stack.high-n].pos > pos:
      inc n
    let
      off = stack.len-n
      t = Term(kind: tList, list: newSeq[Term](n))
    for i in 0..<n:
      t.list[i] = stack[off+i].term
    stack[off].term = t
    stack.setLen(succ off)

  nonreserved_label <- >(builtin * +simple_label_next_char) | (!builtin * label)

  any_label_or_some <- any_label | >Some

  any_label <- label

  label <- quoted_label | simple_label

  quoted_label <- '`' * >(*quoted_label_char) * '`'

  quoted_label_char <- {'\x20'..'\x5f'} | {'\x61'..'\x7e'}
    # omit the ` char

  simple_label_first_char <- Alpha | '_'
  simple_label_next_char <- Alnum | '-' | '/' | '_'
  simple_label <- >( (keyword * +simple_label_next_char) |
      (!keyword * simple_label_first_char * *simple_label_next_char))

  If <- "if"
  Then <- "then"
  Else <- "else"
  Let <- "let"
  In <- "in"
  As <- "as"
  Using <- "using"
  merge <- "merge"
  Infinity <- "Infinity"
  NaN <- "NaN"
  Some <- "Some"
  toMap <- "toMap"
  Assert <- "assert"
  forall <- "∀" | "forall"
  with <- "with"

  keyword <- If | Then | Else | Let | In | Using | "missing" | Assert | As | Infinity | NaN | merge | Some | toMap | forall | with

  Optional <- "Optional"
  Text <- "Text"
  List <- "List"
  Location <- "Location"

  Bool <- "Bool"
  True <- "True"
  False <- "False"
  None <- "None"
  Natural <- "Natural"
  Integer <- "Integer"
  Double <- "Double"
  Type <- "Type"
  Kind <- "Kind"
  Sort <- "Sort"
  Natural_fold <- "Natural/fold"
  Natural_build <- "Natural/build"
  Natural_isZero <- "Natural/isZero"
  Natural_even <- "Natural/even"
  Natural_odd <- "Natural/odd"
  Natural_toInteger <- "Natural/toInteger"
  Natural_show <- "Natural/show"
  Natural_subtract <- "Natural/subtract"
  Integer_toDouble <- "Integer/toDouble"
  Integer_show <- "Integer/show"
  Integer_negate <- "Integer/negate"
  Integer_clamp <- "Integer/clamp"
  Double_show <- "Double/show"
  List_build <- "List/build"
  List_fold <- "List/fold"
  List_length <- "List/length"
  List_head <- "List/head"
  List_last <- "List/last"
  List_indexed <- "List/indexed"
  List_reverse <- "List/reverse"
  Text_show <- "Text/show"
  Text_replace <- "Text/replace"

  combine <- "∧" | "/\\"
  combine_types <- "⩓" | "//\\\\"
  equivalence <- "≡" | "==="
  prefer <- "⫽" | "//"
  lambda <- "λ" | '\\'
  arrow <- "→" | "->"
  complete <- "::"

  list_builtin <- List_build | List_fold | List_length | List_head | List_last | List_indexed | List_reverse | List

  natural_builtin <- Natural_fold | Natural_build | Natural_isZero | Natural_even | Natural_odd | Natural_toInteger | Natural_show | Natural_subtract | Natural

  integer_builtin <- Integer_toDouble | Integer_show | Integer_negate | Integer_clamp | Integer

  builtin <- list_builtin | natural_builtin | integer_builtin | Double_show | Text_show | Text_replace | Bool | True | False | Optional | None | Double | Text | Type | Kind | Sort
  tab <- '\t'

  end_of_line <- '\n' | "\r\n" # fuck off windows

  line_comment <- "--" * *not_end_of_line * end_of_line

  not_end_of_line <- Print | valid_non_ascii | tab

  block_comment_continue <-
    "-}" | (block_comment * block_comment_continue) | (block_comment_char * block_comment_continue)


  block_comment <- "{-" * block_comment_continue

  block_comment_char <-
    Print | valid_non_ascii | tab | end_of_line

  whsp <- *whitespace_chunk

  whsp1 <- +whitespace_chunk

  whitespace_chunk <- ' ' | tab | end_of_line | line_comment | block_comment

  valid_non_ascii <- >utf8.any:
    ## This rule matches all characters that are not:
    ## * not ASCII
    ## * not part of a surrogate pair
    ## * not a "non-character"
    template exclude(a, b: int32) =
      let r = runeAt($1, 0)
      validate(not (cast[Rune](a) <=% r and r <=% cast[Rune](b)))
    exclude(0x0, 0x7f)
    exclude(0xd800, 0xdfff)
    exclude(0xfffe, 0xffff)
    exclude(0x1fffe, 0x1ffff)
    exclude(0x2fffe, 0x2ffff)
    exclude(0x3fffe, 0x3ffff)
    exclude(0x4fffe, 0x4ffff)
    exclude(0x5fffe, 0x5ffff)
    exclude(0x6fffe, 0x6ffff)
    exclude(0x7fffe, 0x7ffff)
    exclude(0x8fffe, 0x8ffff)
    exclude(0x9fffe, 0x9ffff)
    exclude(0xafffe, 0xaffff)
    exclude(0xbfffe, 0xbffff)
    exclude(0xcfffe, 0xcffff)
    exclude(0xdfffe, 0xdffff)
    exclude(0xefffe, 0xeffff)
    exclude(0xffffe, 0xfffff)
    exclude(0x10fffe, 0x10ffff)

proc parseDhall*(code: string): Term {.gcsafe.} =
  if code == "":
    # The code is misssing.
    return newMissing()
  var stack = newSeqOfCap[Frame](32)
    # TODO: how big does this stack grow?
  let match = parser.match(code, stack)
  if not match.ok:
    raise newException(ValueError, "failed to parse Dhall expression")
  assert(stack.len == 1, "parser did not backtrack during match")
  pop()

when isMainModule:
  import ./binary, ./render, ./resolution
  import std/nimprof, std/times
  let buf = stdin.readAll
  if buf != "":
    let
      a = cpuTime()
      term = parseDhall(buf)
      b = cpuTime()
    echo "parse time: ", b - a
    let
      c = cpuTime()
      bin = term.encode
      d = cpuTime()
    echo "encode time: ", d - c
    let
      e = cpuTime()
      dec = bin.decodeDhall
      f = cpuTIme()
    echo "decode time: ", f - e
    stdout.write $term.semanticHash, "\n"