about summary refs log tree commit diff
path: root/overlays/patches/emacs/overlay/repos/fromElisp/default.nix
blob: b412736bdd19b8b269a6c28e3744547cd8eb2fcc (plain) (blame)
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
# WARNING: This file was automatically imported from
# https://github.com/talyz/fromElisp. Don't make any changes to it
# locally - they will be discarded on update!

{ pkgs ? import <nixpkgs> {}
, commentMaxLength ? 300
, stringMaxLength ? 3000
, characterMaxLength ? 50
, integerMaxLength ? 50
, floatMaxLength ? 50
, boolVectorMaxLength ? 50
, symbolMaxLength ? 50
, orgModeBabelCodeBlockHeaderMaxLength ? 200
, orgModeBabelCodeBlockArgMaxLength ? 30
}:

with pkgs.lib;
with builtins;

let

  # Create a matcher from a regex string and maximum length. A
  # matcher takes a string and returns the first match produced by
  # running its regex on it, or null if the match is unsuccessful,
  # but only as far in as specified by maxLength.
  mkMatcher = regex: maxLength:
    string:
      let
        substr = substring 0 maxLength string;
        matched = match regex substr;
      in
        if matched != null then head matched else null;

  removeStrings = stringsToRemove: string:
    let
      len = length stringsToRemove;
      listOfNullStrings = genList (const "") len;
    in
      replaceStrings stringsToRemove listOfNullStrings string;

  # Split a string of elisp into individual tokens and add useful
  # metadata.
  tokenizeElisp' = { elisp, startLineNumber ? 1 }:
    let
      # These are the only characters that can not be unescaped in a
      # symbol name. We match the inverse of these to get the actual
      # symbol characters and use them to differentiate between
      # symbols and tokens that could potentially look like symbols,
      # such as numbers. Due to the leading bracket, this has to be
      # placed _first_ inside a bracket expression.
      notInSymbol = '']["'`,#;\\()[:space:][:cntrl:]'';

      matchComment = mkMatcher "(;[^\n]*).*" commentMaxLength;

      matchString = mkMatcher ''("([^"\\]|\\.)*").*'' stringMaxLength;

      matchCharacter = mkMatcher ''([?]((\\[sSHMAC]-)|\\\^)*(([^][\\()]|\\[][\\()])|\\[^^SHMACNuUx0-7]|\\[uU][[:digit:]a-fA-F]+|\\x[[:digit:]a-fA-F]*|\\[0-7]{1,3}|\\N\{[^}]+}))([${notInSymbol}?]|$).*'' characterMaxLength;

      matchNonBase10Integer = mkMatcher ''(#([BbOoXx]|[[:digit:]]{1,2}r)[[:digit:]a-fA-F]+)([${notInSymbol}]|$).*'' integerMaxLength;

      matchInteger = mkMatcher ''([+-]?[[:digit:]]+[.]?)([${notInSymbol}]|$).*'' integerMaxLength;

      matchBoolVector = mkMatcher ''(#&[[:digit:]]+"([^"\\]|\\.)*").*'' boolVectorMaxLength;

      matchFloat = mkMatcher ''([+-]?([[:digit:]]*[.][[:digit:]]+|([[:digit:]]*[.])?[[:digit:]]+e([+-]?[[:digit:]]+|[+](INF|NaN))))([${notInSymbol}]|$).*'' floatMaxLength;

      matchDot = mkMatcher ''([.])([${notInSymbol}]|$).*'' 2;

      # Symbols can contain pretty much any characters - the general
      # rule is that if nothing else matches, it's a symbol, so we
      # should be pretty generous here and match for symbols last. See
      # https://www.gnu.org/software/emacs/manual/html_node/elisp/Symbol-Type.html
      matchSymbol =
        let
          symbolChar = ''([^${notInSymbol}]|\\.)'';
        in mkMatcher ''(${symbolChar}+)([${notInSymbol}]|$).*'' symbolMaxLength;

      maxTokenLength = foldl' max 0 [
        commentMaxLength
        stringMaxLength
        characterMaxLength
        integerMaxLength
        floatMaxLength
        boolVectorMaxLength
        symbolMaxLength
      ];

      # Fold over all the characters in a string, checking for
      # matching tokens.
      #
      # The implementation is a bit obtuse, for optimization reasons:
      # nix doesn't have tail-call optimization, thus a strict fold,
      # which should essentially force a limited version of tco when
      # iterating a list, is our best alternative.
      #
      # The string read from is split into a list of its constituent
      # characters, which is then folded over. Each character is then
      # used to determine a likely matching regex "matcher" to run on
      # the string, starting at the position of the aforementioned
      # character. When an appropriate matcher has been found and run
      # successfully on the string, its result is added to
      # `state.acc`, a list of all matched tokens. The length of the
      # matched token is determined and passed on to the following
      # iteration through `state.skip`. If `state.skip` is positive,
      # nothing will be done in the current iteration, except
      # decrementing `state.skip` for the next one: this skips the
      # characters we've already matched. At each iteration,
      # `state.pos` is also incremented, to keep track of the current
      # string position.
      #
      # The order of the matches is significant - matchSymbol will,
      # for example, also match numbers and characters, so we check
      # for symbols last.
      readToken = state: char:
        let
          rest = substring state.pos maxTokenLength elisp;
          comment = matchComment rest;
          character = matchCharacter rest;
          nonBase10Integer = matchNonBase10Integer rest;
          integer = matchInteger rest;
          float = matchFloat rest;
          function = matchFunction rest;
          boolVector = matchBoolVector rest;
          string = matchString rest;
          dot = matchDot rest;
          symbol = matchSymbol rest;
        in
          if state.skip > 0 then
            state // {
              pos = state.pos + 1;
              skip = state.skip - 1;
              line = if char == "\n" then state.line + 1 else state.line;
            }
          else if char == "\n" then
            let
              mod = state.line / 1000;
              newState = {
                pos = state.pos + 1;
                line = state.line + 1;
                inherit mod;
              };
            in
              state // (
                # Force evaluation of old state every 1000 lines. Nix
                # doesn't have a modulo builtin, so we have to save
                # the result of an integer division and compare
                # between runs.
                if mod > state.mod then
                  seq state.acc newState
                else
                  newState
              )
          else if elem char [ " " "\t" "\r" ] then
            state // {
              pos = state.pos + 1;
              inherit (state) line;
            }
          else if char == ";" then
            if comment != null then
              state // {
                pos = state.pos + 1;
                skip = (stringLength comment) - 1;
              }
            else throw "Unrecognized token on line ${toString state.line}: ${rest}"
          else if char == "(" then
            state // {
              acc = state.acc ++ [{ type = "openParen"; value = "("; inherit (state) line; }];
              pos = state.pos + 1;
            }
          else if char == ")" then
            state // {
              acc = state.acc ++ [{ type = "closeParen"; value = ")"; inherit (state) line; }];
              pos = state.pos + 1;
            }
          else if char == "[" then
            state // {
              acc = state.acc ++ [{ type = "openBracket"; value = "["; inherit (state) line; }];
              pos = state.pos + 1;
            }
          else if char == "]" then
            state // {
              acc = state.acc ++ [{ type = "closeBracket"; value = "]"; inherit (state) line; }];
              pos = state.pos + 1;
            }
          else if char == "'" then
            state // {
              acc = state.acc ++ [{ type = "quote"; value = "'"; inherit (state) line; }];
              pos = state.pos + 1;
            }
          else if char == ''"'' then
            if string != null then
              state // {
                acc = state.acc ++ [{ type = "string"; value = string; inherit (state) line; }];
                pos = state.pos + 1;
                skip = (stringLength string) - 1;
              }
            else throw "Unrecognized token on line ${toString state.line}: ${rest}"
          else if char == "#" then
            let nextChar = substring 1 1 rest;
            in
              if nextChar == "'" then
                state // {
                  acc = state.acc ++ [{ type = "function"; value = "#'"; inherit (state) line; }];
                  pos = state.pos + 1;
                  skip = 1;
                }
              else if nextChar == "&" then
                if boolVector != null then
                  state // {
                    acc = state.acc ++ [{ type = "boolVector"; value = boolVector; inherit (state) line; }];
                    pos = state.pos + 1;
                    skip = (stringLength boolVector) - 1;
                  }
                else throw "Unrecognized token on line ${toString state.line}: ${rest}"
              else if nextChar == "s" then
                if substring 2 1 rest == "(" then
                  state // {
                    acc = state.acc ++ [{ type = "record"; value = "#s"; inherit (state) line; }];
                    pos = state.pos + 1;
                    skip = 1;
                  }
                else throw "List must follow #s in record on line ${toString state.line}: ${rest}"
              else if nextChar == "[" then
                state // {
                  acc = state.acc ++ [{ type = "byteCode"; value = "#"; inherit (state) line; }];
                  pos = state.pos + 1;
                }
              else if nonBase10Integer != null then
                state // {
                  acc = state.acc ++ [{ type = "nonBase10Integer"; value = nonBase10Integer; inherit (state) line; }];
                  pos = state.pos + 1;
                  skip = (stringLength nonBase10Integer) - 1;
                }
              else throw "Unrecognized token on line ${toString state.line}: ${rest}"
          else if elem char [ "+" "-" "." "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" ] then
            if integer != null then
              state // {
                acc = state.acc ++ [{ type = "integer"; value = integer; inherit (state) line; }];
                pos = state.pos + 1;
                skip = (stringLength integer) - 1;
              }
            else if float != null then
              state // {
                acc = state.acc ++ [{ type = "float"; value = float; inherit (state) line; }];
                pos = state.pos + 1;
                skip = (stringLength float) - 1;
              }
            else if dot != null then
              state // {
                acc = state.acc ++ [{ type = "dot"; value = dot; inherit (state) line; }];
                pos = state.pos + 1;
                skip = (stringLength dot) - 1;
              }
            else if symbol != null then
              state // {
                acc = state.acc ++ [{ type = "symbol"; value = symbol; inherit (state) line; }];
                pos = state.pos + 1;
                skip = (stringLength symbol) - 1;
              }
            else throw "Unrecognized token on line ${toString state.line}: ${rest}"
          else if char == "?" then
            if character != null then
              state // {
                acc = state.acc ++ [{ type = "character"; value = character; inherit (state) line; }];
                pos = state.pos + 1;
                skip = (stringLength character) - 1;
              }
            else throw "Unrecognized token on line ${toString state.line}: ${rest}"
          else if char == "`" then
            state // {
              acc = state.acc ++ [{ type = "backquote"; value = "`"; inherit (state) line; }];
              pos = state.pos + 1;
            }
          else if char == "," then
            if substring 1 1 rest == "@" then
              state // {
                acc = state.acc ++ [{ type = "slice"; value = ",@"; inherit (state) line; }];
                skip = 1;
                pos = state.pos + 1;
              }
            else
              state // {
                acc = state.acc ++ [{ type = "expand"; value = ","; inherit (state) line; }];
                pos = state.pos + 1;
              }
          else if symbol != null then
            state // {
              acc = state.acc ++ [{ type = "symbol"; value = symbol; inherit (state) line; }];
              pos = state.pos + 1;
              skip = (stringLength symbol) - 1;
            }
          else
            throw "Unrecognized token on line ${toString state.line}: ${rest}";
    in (builtins.foldl' readToken { acc = []; pos = 0; skip = 0; line = startLineNumber; mod = 0; } (stringToCharacters elisp)).acc;

  tokenizeElisp = elisp:
    tokenizeElisp' { inherit elisp; };

  # Produce an AST from a list of tokens produced by `tokenizeElisp`.
  parseElisp' = tokens:
    let
      # Convert literal value tokens in a flat list to their
      # corresponding nix representation.
      parseValues = tokens:
        map (token:
          if token.type == "string" then
            token // {
              value = substring 1 (stringLength token.value - 2) token.value;
            }
          else if token.type == "integer" then
            token // {
              value = fromJSON (removeStrings ["+" "."] token.value);
            }
          else if token.type == "symbol" && token.value == "t" then
            token // {
              value = true;
            }
          else if token.type == "float" then
            let
              initial = head (match "([+-]?([[:digit:]]*[.])?[[:digit:]]+(e([+-]?[[:digit:]]+|[+](INF|NaN)))?)" token.value);
              isSpecial = (match "(.+(e[+](INF|NaN)))" initial) != null;
              withoutPlus = removeStrings ["+"] initial;
              withPrefix =
                if substring 0 1 withoutPlus == "." then
                  "0" + withoutPlus
                else if substring 0 2 withoutPlus == "-." then
                  "-0" + removeStrings ["-"] withoutPlus
                else
                  withoutPlus;
            in
              if !isSpecial && withPrefix != null then
                token // {
                  value = fromJSON withPrefix;
                }
              else
                token
          else
            token
        ) tokens;

      # Convert pairs of opening and closing tokens to their
      # respective collection types, i.e. lists and vectors. Also,
      # normalize the forms of nil, which can be written as either
      # `nil` or `()`, to empty lists.
      #
      # For performance reasons, this is implemented as a fold over
      # the list of tokens, rather than as a recursive function. To
      # keep track of list depth when sublists are parsed, a list,
      # `state.acc`, is used as a stack. When entering a sublist, an
      # empty list is pushed to `state.acc`, and items in the sublist
      # are subsequently added to this list. When exiting the list,
      # `state.acc` is popped and the completed list is added to the
      # new head of `state.acc`, i.e. the outer list, which we were
      # parsing before entering the sublist.
      #
      # Evaluation of old state is forced with `seq` in a few places,
      # because nix otherwise keeps it around, eventually resulting in
      # a stack overflow.
      parseCollections = tokens:
        let
          parseToken = state: token:
            let
              openColl = if token.type == "openParen" then "list" else if token.type == "openBracket" then "vector" else null;
              closeColl = if token.type == "closeParen" then "list" else if token.type == "closeBracket" then "vector" else null;
            in
              if openColl != null then
                state // {
                  acc = [ [] ] ++ seq (head state.acc) state.acc;
                  inColl = [ openColl ] ++ state.inColl;
                  depth = state.depth + 1;
                  line = [ token.line ] ++ state.line;
                }
              else if closeColl != null then
                if (head state.inColl) == closeColl then
                  let
                    outerColl = elemAt state.acc 1;
                    currColl = {
                      type = closeColl;
                      value = head state.acc;
                      line = head state.line;
                      inherit (state) depth;
                    };
                    rest = tail (tail state.acc);
                  in
                    state // seq state.acc {
                      acc = [ (outerColl ++ [ currColl ]) ] ++ rest;
                      inColl = tail state.inColl;
                      depth = state.depth - 1;
                      line = tail state.line;
                    }
                else
                  throw "Unmatched ${token.type} on line ${toString token.line}"
              else if token.type == "symbol" && token.value == "nil" then
                let
                  currColl = head state.acc;
                  rest = tail state.acc;
                  emptyList = {
                    type = "list";
                    depth = state.depth + 1;
                    value = [];
                  };
                in
                  state // seq currColl { acc = [ (currColl ++ [ emptyList ]) ] ++ rest; }
              else
                let
                  currColl = head state.acc;
                  rest = tail state.acc;
                in
                  state // seq currColl { acc = [ (currColl ++ [ token ]) ] ++ rest; };
        in
          head (builtins.foldl' parseToken { acc = [ [] ]; inColl = [ null ]; depth = -1; line = []; } tokens).acc;

      # Handle dotted pair notation, a syntax where the car and cdr
      # are represented explicitly. See
      # https://www.gnu.org/software/emacs/manual/html_node/elisp/Dotted-Pair-Notation.html#Dotted-Pair-Notation
      # for more info.
      #
      # This mainly entails handling lists that are the cdrs of a
      # dotted pairs, concatenating the lexically distinct lists into
      # the logical list they actually represent.
      #
      # For example:
      # (a . (b . (c . nil))) -> (a b c)
      parseDots = tokens:
        let
          parseToken = state: token:
            if token.type == "dot" then
              if state.inList then
                state // {
                  dotted = true;
                  depthReduction = state.depthReduction + 1;
                }
              else
                throw ''"Dotted pair notation"-dot outside list on line ${toString token.line}''
            else if isList token.value then
              let
                collectionContents = foldl' parseToken {
                  acc = [];
                  dotted = false;
                  inList = token.type == "list";
                  inherit (state) depthReduction;
                } token.value;
              in
                state // {
                  acc = state.acc ++ (
                    if state.dotted then
                      collectionContents.acc
                    else
                      [
                        (token // {
                          value = collectionContents.acc;
                          depth = token.depth - state.depthReduction;
                        })
                      ]
                  );
                  dotted = false;
                }
            else
              state // {
                acc = state.acc ++ [token];
              };
        in
          (foldl' parseToken { acc = []; dotted = false; inList = false; depthReduction = 0; } tokens).acc;

      parseQuotes = tokens:
        let
          parseToken = state: token':
            let
              token =
                if isList token'.value then
                  token' // {
                    value = (foldl' parseToken { acc = []; quotes = []; } token'.value).acc;
                  }
                else
                  token';
            in
              if elem token.type [ "quote" "expand" "slice" "backquote" "function" "record" "byteCode" ] then
                state // {
                  quotes = [ token ] ++ state.quotes;
                }
              else if state.quotes != [] then
                let
                  quote = value: token:
                    token // {
                      inherit value;
                    };
                  quotedValue = foldl' quote token state.quotes;
                in
                  state // {
                    acc = state.acc ++ [ quotedValue ];
                    quotes = [];
                  }
              else
                state // {
                  acc = state.acc ++ [ token ];
                };
        in
          (foldl' parseToken { acc = []; quotes = []; } tokens).acc;
    in
      parseQuotes (parseDots (parseCollections (parseValues tokens)));

  parseElisp = elisp:
    parseElisp' (tokenizeElisp elisp);

  fromElisp' = ast:
    let
      readObject = object:
        if isList object.value then
          map readObject object.value
        else if object.type == "quote" then
          ["quote" (readObject object.value)]
        else if object.type == "backquote" then
          ["`" (readObject object.value)]
        else if object.type == "expand" then
          ["," (readObject object.value)]
        else if object.type == "slice" then
          [",@" (readObject object.value)]
        else if object.type == "function" then
          ["#'" (readObject object.value)]
        else if object.type == "byteCode" then
          ["#"] ++ (readObject object.value)
        else if object.type == "record" then
          ["#s"] ++ (readObject object.value)
        else
          object.value;
    in
      map readObject ast;

  fromElisp = elisp:
    fromElisp' (parseElisp elisp);

  # Parse an Org mode babel text and return a list of all code blocks
  # with metadata.
  #
  # The general operation is similar to tokenizeElisp', so check its
  # documentation for a more in-depth description.
  #
  # As in tokenizeElisp', the string read from is split into a list of
  # its constituent characters, which is then folded over. Each
  # character is then used to determine whether we should try to run a
  # match for a `#+begin_src` header or `#+end_src` footer, starting
  # at the position of the aforementioned character. These matches
  # should only be attempted if the current character is `#` and the
  # line has nothing but whitespace before it (noted by
  # `state.leadingWhitespace`).
  #
  # When an appropriate match for a header has been found, its
  # arguments are further parsed and the result is put into the code
  # block's `flags` attribute. The subsequent characters are added to
  # the code block's `body` attribute, until a footer is successfully
  # matched and the block is added to the list of parsed blocks,
  # `state.acc`.
  parseOrgModeBabel = text:
    let
      matchBeginCodeBlock = mkMatcher "(#[+][bB][eE][gG][iI][nN]_[sS][rR][cC])([[:space:]]+).*" orgModeBabelCodeBlockHeaderMaxLength;
      matchHeader = mkMatcher "(#[+][hH][eE][aA][dD][eE][rR][sS]?:)([[:space:]]+).*" orgModeBabelCodeBlockHeaderMaxLength;
      matchEndCodeBlock = mkMatcher "(#[+][eE][nN][dD]_[sS][rR][cC][^\n]*).*" orgModeBabelCodeBlockHeaderMaxLength;

      matchBeginCodeBlockLang = match "([[:blank:]]*)([[:alnum:]][[:alnum:]-]*).*";
      matchBeginCodeBlockFlags = mkMatcher "([^\n]*[\n]).*" orgModeBabelCodeBlockHeaderMaxLength;

      parseToken = state: char:
        let
          rest = substring state.pos orgModeBabelCodeBlockHeaderMaxLength text;
          beginCodeBlock = matchBeginCodeBlock rest;
          header = matchHeader rest;
          endCodeBlock = matchEndCodeBlock rest;
          language = matchBeginCodeBlockLang rest;
          flags = matchBeginCodeBlockFlags rest;

          force = expr: seq state.pos (seq state.line expr);
        in
          if state.skip > 0 then
            state // force {
              pos = state.pos + 1;
              skip = state.skip - 1;
              line = if char == "\n" then state.line + 1 else state.line;
              leadingWhitespace = char == "\n" || (state.leadingWhitespace && elem char [ " " "\t" "\r" ]);
            }
          else if char == "#" && state.leadingWhitespace && !state.readBody && beginCodeBlock != null then
            state // {
              pos = state.pos + 1;
              skip = (stringLength beginCodeBlock) - 1;
              leadingWhitespace = false;
              readLanguage = true;
            }
          else if char == "#" && state.leadingWhitespace && !state.readBody && header != null then
            state // {
              pos = state.pos + 1;
              skip = (stringLength header) - 1;
              leadingWhitespace = false;
              readFlags = true;
            }
          else if state.readLanguage then
            if language != null then
              state // {
                block = state.block // {
                  language = elemAt language 1;
                };
                pos = state.pos + 1;
                skip = (foldl' (total: string: total + (stringLength string)) 0 language) - 1;
                leadingWhitespace = false;
                readLanguage = false;
                readFlags = true;
                readBody = true;
              }
            else throw "Language missing or invalid for code block on line ${toString state.line}!"
          else if state.readFlags then
            if flags != null then
              let
                parseFlag = state: item:
                  let
                    prefix = if isString item then substring 0 1 item else null;
                  in
                    if elem prefix [ ":" "-" "+" ] then
                      state // {
                        acc = state.acc // { ${item} = true; };
                        flag = item;
                      }
                    else if state.flag != null then
                      state // {
                        acc = state.acc // { ${state.flag} = item; };
                        flag = null;
                      }
                    else
                      state;
              in
                state // {
                  block = state.block // {
                    flags =
                      (foldl'
                        parseFlag
                        { acc = state.block.flags;
                          flag = null;
                          inherit (state) line;
                        }
                        (fromElisp flags)).acc;
                    startLineNumber = state.line + 1;
                  };
                  pos = state.pos + 1;
                  skip = (stringLength flags) - 1;
                  line = if char == "\n" then state.line + 1 else state.line;
                  leadingWhitespace = char == "\n";
                  readFlags = false;
                }
            else throw "Arguments malformed for code block on line ${toString state.line}!"
          else if char == "#" && state.leadingWhitespace && endCodeBlock != null then
            state // {
              acc = state.acc ++ [ state.block ];
              block = {
                language = null;
                body = "";
                flags = {};
              };
              pos = state.pos + 1;
              skip = (stringLength endCodeBlock) - 1;
              leadingWhitespace = false;
              readBody = false;
            }
          else if state.readBody then
            let
              mod = state.pos / 100;
              newState = {
                block = state.block // {
                  body = state.block.body + char;
                };
                inherit mod;
                pos = state.pos + 1;
                line = if char == "\n" then state.line + 1 else state.line;
                leadingWhitespace = char == "\n" || (state.leadingWhitespace && elem char [ " " "\t" "\r" ]);
              };
            in
              if mod > state.mod then
                state // seq state.block.body (force newState)
              else
                state // newState
          else
            state // force {
              pos = state.pos + 1;
              line = if char == "\n" then state.line + 1 else state.line;
              leadingWhitespace = char == "\n" || (state.leadingWhitespace && elem char [ " " "\t" "\r" ]);
            };
    in
      (foldl'
        parseToken
        { acc = [];
          mod = 0;
          pos = 0;
          skip = 0;
          line = 1;
          block = {
            language = null;
            body = "";
            flags = {};
          };
          leadingWhitespace = true;
          readLanguage = false;
          readFlags = false;
          readBody = false;
        }
        (stringToCharacters text)).acc;

  # Run tokenizeElisp' on all Elisp code blocks (with `:tangle yes`
  # set) from an Org mode babel text. If the block doesn't have a
  # `tangle` attribute, it's determined by `defaultArgs`.
  tokenizeOrgModeBabelElisp' = defaultArgs: text:
    let
      codeBlocks =
        filter
          (block:
            let
              tangle = toLower (block.flags.":tangle" or defaultArgs.":tangle" or "no");
              language = toLower block.language;
            in
              elem language [ "elisp" "emacs-lisp" ]
                && elem tangle [ "yes" ''"yes"'' ])
          (parseOrgModeBabel text);
      in
        foldl'
          (result: codeBlock:
            result ++ (tokenizeElisp' {
              elisp = codeBlock.body;
              inherit (codeBlock) startLineNumber;
            })
          )
          []
          codeBlocks;

  tokenizeOrgModeBabelElisp =
    tokenizeOrgModeBabelElisp' {
      ":tangle" = "no";
    };

  parseOrgModeBabelElisp' = defaultArgs: text:
    parseElisp' (tokenizeOrgModeBabelElisp' defaultArgs text);

  parseOrgModeBabelElisp = text:
    parseElisp' (tokenizeOrgModeBabelElisp text);

  fromOrgModeBabelElisp' = defaultArgs: text:
    fromElisp' (parseOrgModeBabelElisp' defaultArgs text);

  fromOrgModeBabelElisp = text:
    fromElisp' (parseOrgModeBabelElisp text);

in
{
  inherit tokenizeElisp parseElisp fromElisp;
  inherit tokenizeElisp' parseElisp' fromElisp';
  inherit tokenizeOrgModeBabelElisp parseOrgModeBabelElisp fromOrgModeBabelElisp;
  inherit tokenizeOrgModeBabelElisp' parseOrgModeBabelElisp' fromOrgModeBabelElisp';
}