summaryrefslogtreecommitdiff
path: root/tokenizer.fun
blob: 5cca2036bdd3213f1799d84e3a0e45fc60d88211 (plain)
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
functor Tokenizer(structure H: HASHTABLE; structure S: STREAM)
    : TOKENIZER =
struct

  structure S = S

  datatype intType = ItDec | ItOct | ItHex
  datatype intSfx = IsNone | IsU | IsL | IsUL | IsLL | IsULL
  datatype floatSfx = FsNone | FsF | FsL

  datatype numConst =
    IntConst of intType * string * intSfx |
    FloatConst of string * floatSfx

  datatype token =
    Invalid |
    EOS |
    NewLine |

    Num of numConst |

    Id of string |
    CharConst of string * int |
    StringConst of string |

    kwBreak |
    kwCase |
    kwChar |
    kwConst |
    kwContinue |
    kwDefault |
    kwDouble |
    kwElse |
    kwEnum |
    kwExtern |
    kwFloat |
    kwFor |
    kwGoto |
    kwInt |
    kwLong |
    kwRegister |
    kwReturn |
    kwShort |
    kwSigned |
    kwSizeof |
    kwStruct |
    kwSwitch |
    kwTypedef |
    kwUnion |
    kwUnsigned |
    kwVoid |
    kwVolatile |

    LParen |
    RParen |
    LBracket |
    RBracket |
    LBrace |
    RBrace |

    QuestionMark |
    Colon |
    Coma |
    Semicolon |

    Arrow |
    Plus |
    DoublePlus|
    Minus |
    DoubleMinus |
    Ampersand |
    Asterisk |
    Slash |
    Tilde |
    ExclMark |
    Percent |
    DoubleGreater |
    DoubleLess |
    Greater |
    Less |
    EqualSign |
    LessEqualSign |
    GreaterEqualSign |
    DoubleEqualSign |
    ExclMarkEqualSign |
    Cap |
    VerticalBar |
    DoubleAmpersand |
    DoubleVerticalBar |

    AsteriskEqualSign |
    SlashEqualSign |
    PercentEqualSign |
    PlusEqualSign |
    MinusEqualSign |
    DoubleLessEqualSign |
    DoubleGreaterEqualSign |
    AmpersandEqualSign |
    CapEqualSign |
    VerticalBarEqualSign |

    Hash |
    DoubleHash |

    Dot |
    DoubleDot |
    TripleDot |

    CommentStart |

    CppInclude |
    CppDefine |
    CppUndef |
    CppIf |
    CppIfdef |
    CppIfndef |
    CppElse |
    CppElif |
    CppEndif |
    CppWarning |
    CppError |
    CppPragma

  val kwPrefix  = #"@"
  val cppPrefix = #"$"

  type fullToken = S.pos * token

  datatype tkErrorAuxInfo = TkiEOF | TkiDx of int | TkiStart

  exception TkError of tkErrorAuxInfo * string
  exception TkErrorAug of S.ppos * string

  exception ExpectedCppDir (* handled in postprocess *)

  exception FsmTableIsTooSmall

  (* Unreachable (should be) *)
  exception TokenWithoutRepr
  exception SuffixWithoutRepr

  val tokenRepr =
  let
    fun & repr = str kwPrefix ^ repr
    fun % repr = str cppPrefix ^ repr
  in
    [
      (NewLine, "NewLine"),

      (kwBreak, &"break"),
      (kwCase, &"case"),
      (kwChar, &"char"),
      (kwConst, &"const"),
      (kwContinue, &"continue"),
      (kwDefault, &"default"),
      (kwDouble, &"double"),
      (kwElse, &"else"),
      (kwEnum, &"enum"),
      (kwExtern, &"extern"),
      (kwFloat, &"float"),
      (kwFor, &"for"),
      (kwGoto, &"goto"),
      (kwInt, &"int"),
      (kwLong, &"long"),
      (kwRegister, &"register"),
      (kwReturn, &"return"),
      (kwShort, &"short"),
      (kwSigned, &"signed"),
      (kwSizeof, &"sizeof"),
      (kwStruct, &"struct"),
      (kwSwitch, &"switch"),
      (kwTypedef, &"typedef"),
      (kwUnion, &"union"),
      (kwUnsigned, &"unsigned"),
      (kwVoid, &"void"),
      (kwVolatile, &"volatile"),

      (LParen, "("),
      (RParen, ")"),
      (LBracket, "["),
      (RBracket, "]"),
      (LBrace, "{"),
      (RBrace, "}"),

      (QuestionMark, "?"),
      (Colon, ":"),
      (Coma, ","),
      (Semicolon, ";"),

      (Arrow, "->"),
      (Plus, "+"),
      (DoublePlus, "++"),
      (Minus, "-"),
      (DoubleMinus, "--"),
      (Ampersand, "&"),
      (Asterisk, "*"),
      (Slash, "/"),
      (Tilde, "~"),
      (ExclMark, "!"),
      (Percent, "%"),
      (DoubleLess, "<<"),
      (DoubleGreater, ">>"),
      (Less, "<"),
      (Greater, ">"),
      (EqualSign, "="),
      (LessEqualSign, "<="),
      (GreaterEqualSign, ">="),
      (DoubleEqualSign, "=="),
      (ExclMarkEqualSign, "!="),
      (Cap, "^"),
      (VerticalBar, "|"),
      (DoubleAmpersand, "&&"),
      (DoubleVerticalBar, "||"),

      (AsteriskEqualSign, "*="),
      (SlashEqualSign, "/="),
      (PercentEqualSign, "%="),
      (PlusEqualSign, "+="),
      (MinusEqualSign, "-="),
      (DoubleLessEqualSign, "<<="),
      (DoubleGreaterEqualSign, ">>="),
      (AmpersandEqualSign, "&="),
      (CapEqualSign, "^="),
      (VerticalBarEqualSign, "|="),

      (Hash, "#"),
      (DoubleHash, "##"),

      (Dot, "."),
      (DoubleDot, ".."),
      (TripleDot, "..."),

      (CommentStart, "/*"),

      (CppInclude, %"include"),
      (CppDefine, %"define"),
      (CppUndef, %"undef"),
      (CppIf, %"if"),
      (CppIfdef, %"ifdef"),
      (CppIfndef, %"ifndef"),
      (CppElse, %"else"),
      (CppElif, %"elif"),
      (CppEndif, %"endif"),
      (CppWarning, %"warning"),
      (CppError, %"error"),
      (CppPragma, %"pragma")
  ]
  end

  val intSuffixRepr = [
    (IsNone, ""),
    (IsU, "u"),
    (IsL, "l"),
    (IsUL, "ul"),
    (IsLL, "ll"),
    (IsULL, "ull")
  ]

  val floatSuffixRepr = [
    (FsNone, ""),
    (FsF, "f"),
    (FsL, "l")
  ]

  fun getSfxRepr sfx buf onError =
    case List.find (fn (sfx', _) => sfx' = sfx) buf of
      NONE => onError ()
    | SOME (_, repr) => repr

  fun getSfxReprSimple sfx buf =
    getSfxRepr sfx buf (fn () => raise SuffixWithoutRepr)

  val token2str = fn
      Id s => "id:" ^ s
    | Num (IntConst (it, str, sfx)) =>
      let
        val intType =
          case it of
            ItDec => ""
          | ItOct => "0"
          | ItHex => "0x"
      in
        intType ^ str ^ "`" ^ getSfxReprSimple sfx intSuffixRepr ^ "`"
      end
    | Num (FloatConst (str, sfx)) =>
        str ^ "`" ^ getSfxReprSimple sfx floatSuffixRepr ^ "`"
    | CharConst (repr, _) => repr
    | StringConst s =>
        "\"" ^ s ^ "\""
    | v =>
      case List.find (fn (x, _) => x = v) tokenRepr of
      SOME (_, repr) => repr
      | NONE => raise TokenWithoutRepr

  fun printToken tk = print $ token2str tk

  fun isIdStart c = Char.isAlpha c orelse c = #"_"
  fun isIdBody c = Char.isAlphaNum c orelse c = #"_"

  fun isOctal c = ord c >= ord #"0" andalso ord c < ord #"8"
  val isDigit = Char.isDigit
  val isHexDigit = Char.isHexDigit

  fun isPrintable c = Char.isPrint c andalso c <> #" "

  (* FSM for parsing symbols *)

  val maxStates = 51

  fun fsmInsert (nextState, buf) curState tk (c :: cs) =
  let
    open Array

    val (_, row) = sub (buf, curState)
    val potNextState = sub (row, ord c)
  in
    if potNextState <> ~1 then
      fsmInsert (nextState, buf) potNextState tk cs
    else (
      update (row, ord c, !nextState);
      nextState := !nextState + 1;
      fsmInsert (nextState, buf) (!nextState - 1) tk cs
    )
  end
   | fsmInsert (_, buf) curState tk [] =
  let
    open Array
    val (_, row) = sub (buf, curState)
  in
    update (buf, curState, (tk, row))
  end

  fun isStartForFsmGen () =
  let
    open Array

    val lookupTable = array (128, false)

    fun firstChr s = ord $ String.sub (s, 0)
    val () = List.app
      (fn (_, repr) => update (lookupTable, firstChr repr, true))
          $ List.filter
              (fn (tk, repr) =>
                let
                  val c = String.sub (repr, 0)
                in
                  c <> kwPrefix andalso c <> cppPrefix
                    andalso tk <> NewLine
                end)
              tokenRepr

  in
    fn c => sub (lookupTable, ord c)
  end

  val isStartForFsm = isStartForFsmGen ()

  fun fsmTableCreate () =
  let
    open Array

    val T as (nextState, buf) =
      (ref 1, array (maxStates, (Invalid, array (128, ~1))))
    val r = ref 1

    fun filterNeeded [] acc = acc
      | filterNeeded ((T as (_, repr)) :: tks) acc =
          filterNeeded tks
              (if isStartForFsm $ String.sub (repr, 0) then
                 T :: acc
               else
                acc)

    val tokenRepr = filterNeeded tokenRepr []

    fun fsmInsert' T curState tk repr = fsmInsert T curState tk repr handle
      Subscript => raise FsmTableIsTooSmall
  in
    while !r < length buf do (
      update (buf, !r, (Invalid, array(128, ~1)));
      r := !r + 1
    );

    List.app (fn (v, p) => fsmInsert' T 0 v $ explode p) tokenRepr;
    if !nextState <> maxStates then
      printLn $ "note: Fsm table size can be smaller: "
          ^ Int.toString (!nextState) ^ " is enough"
    else ();
    T
  end

  (* Unused right now
  fun printTable (nextState, buf) =
  let
    fun printRow i row =
      if i = length row then
        print "\n"
      else
        let
          val state = sub (row, i)
        in
          if state = ~1 then
            ()
          else
            print ((str (chr i)) ^ ": " ^ (Int.toString state) ^ ", ");
            printRow (i + 1) row
        end

    fun print' rowNum buf =
      if rowNum = !nextState then
        ()
      else
        let
          val (tk, row) = sub (buf, rowNum)
        in
          print ((token2string tk) ^ ": ");
          printRow 0 row;
          print' (rowNum + 1) buf
        end
  in
    print ("NextState: " ^ Int.toString (!nextState) ^ "\n");
    print' 0 buf;
    print "\n"
  end
  *)

  val fsmTable = lazy fsmTableCreate

  fun fsmEat stream =
  let
    open Array
    val pos = S.getPos stream

    fun get curState stream =
    let
      val (c, stream) = S.getchar stream
    in
      case c of
        NONE => (#1 $ sub (#2 $ fsmTable (), curState), stream)
      | SOME c =>
          let
            val (tk, row) = sub (#2 $ fsmTable (), curState)
            val nextState = sub (row, ord c)
          in
            if nextState = ~1 then
              (tk, S.ungetc stream)
            else
              get nextState stream
          end
    end
  in
    (fn (tk, stream) => ((pos, tk), stream)) $ get 0 stream
  end

  fun tkError2aug stream (dx, msg) =
  let
    val (id, pos) = S.getPosAfterCharRead stream
    val pos = S.pos2ppos (id, pos + dx) stream
  in
    TkErrorAug (pos, msg)
  end

  fun parserWrapper stream parser acc =
  let
    val stream = S.ungetc stream
    val P as (_, startOff) = S.getPos stream

    fun parse' stream acc = let
      val (c, stream) = S.getchar stream

      val (acc, tk, stream) = parser acc (stream, startOff) c handle
          TkError (TkiDx dx, msg) => raise tkError2aug stream (dx, msg)
        | TkError (TkiStart, msg) =>
            let
              val startPos = S.pos2ppos P stream
            in
              raise TkErrorAug (startPos, msg)
            end
        | TkError (TkiEOF, msg) =>
            let
              val pos = S.pposWithoutCol $ S.pos2ppos P stream
            in
              raise TkErrorAug (pos, msg)
            end
    in
      case tk of
        NONE => parse' stream acc
      | _ => (valOf tk, stream)
    end

    val (tk, stream) = parse' stream acc
  in
    ((P, tk): fullToken, stream)
  end

  fun finishSeqRead startOff stream =
  let
    val (_, endOff) = S.getPos stream
    val s = S.getSubstr startOff endOff stream
  in
    s
  end

  fun keywordHashtableGen () =
  let
    val table = H.create 128
    val () =
      List.app
        (fn (tk, repr) =>
            if String.sub (repr, 0) = kwPrefix then
              H.insert table (String.extract (repr, 1, NONE)) tk
            else
              ())
        tokenRepr
  in
    table
  end

  val keywordHashtable = lazy keywordHashtableGen

  fun findKeyword str =
    case H.lookup (keywordHashtable ()) str of
      NONE => Id str
    | SOME tk => tk

  fun idParser () (stream, startOff) c =
  let
    fun finalize stream =
    let
      val s = finishSeqRead startOff stream
      val tk = findKeyword s
    in
      ((), SOME tk, stream)
    end
  in
    case c of
    NONE => finalize stream
    | SOME c =>
        if isIdBody c then
          ((), NONE, stream)
        else
          finalize (S.ungetc stream)
  end

  datatype intMode = ImDec | ImOct | ImInvalidOct | ImHex
  datatype floatMode = FmDot | FmExp

  datatype npState = NpInit | IntMode of intMode | FloatMode of floatMode

  fun getLongestSeq acc pred stream =
  let
    val (c, stream') = S.getchar stream
  in
    if isSome c andalso pred $ valOf c then
      getLongestSeq (valOf c :: acc) pred stream'
    else
      (implode $ rev acc, stream)
  end

  fun skipDigitSeq off stream =
  let
    val (res, stream) = getLongestSeq [] isDigit stream
  in
    if res = "" then
      raise TkError (TkiDx off, "expected digit")
    else
      (String.size res, stream)
  end

  fun getSuffixCommon buf off stream =
  let
    val (sfx, stream) = getLongestSeq [] Char.isAlpha stream
    val sfx = String.map (fn c => Char.toLower c) sfx
    val sfx =
      case List.find (fn (_, repr) => repr = sfx) buf of
        NONE => raise TkError (TkiDx off, "unknown suffix")
      | SOME (sfx, _) => sfx
  in
    (sfx, stream)
  end

  val getIntSuffix = getSuffixCommon intSuffixRepr 0
  fun getFloatSuffix off = getSuffixCommon floatSuffixRepr off

  (*
   * It seems that there is an ambiguity in C:
   * gcc/clang/cparser consider 0xe-3 as a fp constant and reject it.
   * Right now we do not, but it may be reconsidered.
   *)
  fun numParser NpInit (stream, _) (SOME c) =
    if c = #"0" then
      let
        val (c, stream') = S.getchar stream
      in
        case c of
          NONE =>
            (NpInit, SOME $ Num $ IntConst (ItOct, "", IsNone), stream')
        | SOME c =>
          if Char.toLower c = #"x" then
            (IntMode ImHex, NONE, stream')
          else
            (IntMode ImOct, NONE, stream)
      end
    else
      (IntMode ImDec, NONE, stream)
    | numParser NpInit _ NONE = raise Unreachable
    | numParser (IntMode mode) (stream, startOff) c =
    let
      val (pred, res, offset) =
        case mode of
          ImDec => (isDigit, ItDec, 0)
        | ImOct => (isOctal, ItOct, 1)
        | ImInvalidOct => (isDigit, ItOct, 1)
        | ImHex => (isHexDigit, ItHex, 2)

      fun checkAndRaise m msg =
        if mode = m then raise TkError (TkiStart, msg) else ()

      fun finish () =
      let
        val () = checkAndRaise ImInvalidOct "invalid octal constant"
        val stream = S.ungetc stream
        val str = finishSeqRead (startOff + offset) stream
        val (sfx, stream) = getIntSuffix stream
      in
        (IntMode mode, SOME $ Num $ IntConst (res, str, sfx), stream)
      end
    in
      case c of
        NONE => finish ()
      | SOME c =>
          if pred c then
            (IntMode mode, NONE, stream)
          else if c = #"." then
            (FloatMode FmDot, NONE, stream)
          else if Char.toLower c = #"e" then
            (checkAndRaise ImHex
                "floating constant can not come with 0x prefix";
            (FloatMode FmExp, NONE, stream))
          else
            finish ()
    end
    | numParser (FloatMode FmDot) (stream, startOff) _ =
    let
      val (len, stream) = skipDigitSeq 0 $ S.ungetc stream

      val (c, stream') = S.getchar stream

      fun finish () =
      let
        val str = finishSeqRead startOff stream
        val (sfx, stream) = getFloatSuffix len stream
      in
        (FloatMode FmDot, SOME $ Num $ FloatConst (str, sfx), stream)
      end
    in
      case c of
        NONE => finish ()
      | SOME c =>
          if Char.toLower c = #"e" then
            (FloatMode FmExp, NONE, stream')
          else
            finish ()
    end
    | numParser (FloatMode FmExp) (stream, startOff) c =
    let
      val (off, stream) =
        if c = NONE then
          raise TkError (TkiDx 0, "expected digit")
        else if valOf c <> #"+" andalso valOf c <> #"-" then
          (0, S.ungetc stream)
        else
          (~1, stream)

      val (len, stream) = skipDigitSeq off stream
      val str = finishSeqRead startOff stream
      val (sfx, stream) = getFloatSuffix len stream
    in
      (FloatMode FmDot, SOME $ Num $ FloatConst (str, sfx), stream)
    end

  fun chrIntVal c =
    if isDigit c then
      ord c - ord #"0"
    else if ord c >= ord #"a" andalso ord c <= ord #"z" then
      ord c - ord #"a" + 10
    else if ord c >= ord #"A" andalso ord c <= ord #"Z" then
      ord c - ord #"A" + 10
    else
      raise Unreachable

  fun parseOctalSeq stream c =
  let
    fun follow stream acc count =
      if count = 3 then
        (SOME $ chr acc, stream)
      else
        let
          val (c, stream) = S.getchar stream
        in
          case c of
            NONE => (SOME $ chr acc, stream)
          | SOME c =>
              if isOctal c then
                follow stream (acc * 8 + chrIntVal c) (count + 1)
              else
                (SOME $ chr acc, S.ungetc stream)
        end
  in
    follow stream (chrIntVal c) 1
  end

  fun parseHexSeq stream =
  let
    fun follow stream acc count =
      let
        val (c, stream) = S.getchar stream

        val noHex = TkError (TkiDx 0, "\\x without hex digits")
      in
        case c of
          NONE =>
              if count = 0 then
                raise noHex
              else
                (SOME $ chr acc, stream)
        | SOME c =>
            if isHexDigit c then
              if count = 2 then
                raise TkError (TkiDx 2, "hex sequence out of range")
              else
                follow stream (acc * 16 + chrIntVal c) (count + 1)
            else
              if count = 0 then
                raise noHex
              else
                (SOME $ chr acc, S.ungetc stream)
      end
  in
    follow stream 0 0
  end

  fun eatEscSeq stream =
  let
    fun raiseErr0 msg = raise TkError (TkiDx 0, msg)

    val (c, stream) = S.getchar stream
    val c =
      case c of
        NONE => raiseErr0 "unfinished escape sequence"
      | SOME c => c

    fun & c = (SOME c, stream)
  in
    case c of
      #"'" => & #"'"
    | #"\"" => & #"\""
    | #"?" => & #"?"
    | #"\\" => & #"\\"
    | #"a" => & #"\a"
    | #"b" => & #"\b"
    | #"f" => & #"\f"
    | #"n" => & #"\n"
    | #"r" => & #"\r"
    | #"t" => & #"\t"
    | #"v" => & #"\v"
    | #"\n" => (NONE, stream)
    | #"x" => parseHexSeq stream
    | c =>
      if isOctal c then
        parseOctalSeq stream c
      else
        raiseErr0 "unknown escape sequence"
  end

  datatype SeqParseState = SeqInit | SeqStart | SeqValue of int | SeqTerm

  datatype seqParseMode = SpmChr | SpmStr

  fun seqBound SpmChr = #"'"
    | seqBound SpmStr = #"\""

  fun seqExnConv mode (TkError (v, msg)) =
  let
    val bound = if mode = SpmChr then "'" else "\""
    val msg =
      String.translate (fn c => if c = #"%" then bound else str c) msg
  in
    TkError (v, msg)
  end
    | seqExnConv _ _ = raise Unreachable

  fun unfinishedSeq SpmChr = "unfinished character constant"
    | unfinishedSeq SpmStr = "unfinished string literal"

  fun seqParser mode SeqInit (stream, _) (SOME c) =
    if seqBound mode = c then
      (SeqStart, NONE, stream)
    else
      raise Unreachable
    | seqParser mode SeqStart (stream, _) (SOME c) =
      if c <> seqBound mode then
        let
          val (c, stream) =
              if c <> #"\\" then (SOME c, stream) else eatEscSeq stream
        in
          if c = NONE then
            (SeqStart, NONE, stream)
          else
            (SeqValue (ord $ Option.valOf c), NONE, stream)
        end
      else if mode = SpmStr then
          (SeqTerm, SOME $ StringConst "", stream)
      else
        raise seqExnConv SpmChr $ TkError (TkiDx 0, "expected value after %")
    | seqParser mode (SeqValue v) (stream, startOff) (SOME c) =
      if seqBound mode = c then
        let
          fun term s v =
            if mode = SpmChr then
              CharConst (s, v)
            else
              StringConst $ String.extract (s, 1, SOME $ String.size s - 2)
        in
          (SeqTerm, SOME $ term (finishSeqRead startOff stream) v, stream)
        end
      else if mode = SpmStr then
        let
          val (_, stream) =
            if c <> #"\\" then (SOME c, stream) else eatEscSeq stream
        in
          (SeqValue v, NONE, stream)
        end
      else
        raise seqExnConv SpmChr $ TkError (TkiDx 0, "expected % after value")
    | seqParser _ SeqTerm _ (SOME _) =
      raise Unreachable
    | seqParser mode state (_, _) NONE =
      raise case state of
        SeqInit => Unreachable
      | SeqStart => seqExnConv mode $ TkError (TkiStart, unfinishedSeq mode)
      | SeqValue _ =>
          seqExnConv mode $ TkError (TkiStart, unfinishedSeq mode)
      | SeqTerm => Unreachable

  val charParser = seqParser SpmChr
  val strParser = seqParser SpmStr

  fun formCppDir (Id s) =
  let
    open String
  in
    case List.find
      (fn (_, repr) =>
          sub (repr, 0) = cppPrefix andalso
              extract (repr, 1, NONE) = s)
      tokenRepr
    of
      SOME (tk, _) => tk
    | NONE => raise ExpectedCppDir
  end
    | formCppDir kwElse = CppElse
    | formCppDir _ = raise ExpectedCppDir

  fun handleCppDir tk prevPos stream =
  let
    val (pos, tk') = tk
  in
    (prevPos, formCppDir tk') handle
        ExpectedCppDir =>
           raise TkErrorAug (S.pos2ppos pos stream,
            "expected preprocessor directive")
  end

  fun unexpectedCharRaise stream c =
  let
    val pos = S.pos2ppos (S.getPosAfterCharRead stream) stream
    val repr =
      if isPrintable c then
        str c
      else
        "<" ^ Int.toString (ord c) ^ ">"
  in
        raise TkErrorAug (pos, "unexpected character " ^ repr)
  end

  fun skipComment stream pos =
  let
    fun skip prevIsAsterisk stream =
    let
      val (c, stream) =
        case S.getchar stream of
          (NONE, _) =>
            let
              val pos = S.pos2ppos pos stream
            in
                raise TkErrorAug (pos, "unfinished comment")
            end
       | (SOME c, stream) => (c, stream)
    in
      if prevIsAsterisk andalso c = #"/" then
        stream
      else
        skip (c = #"*") stream
    end
  in
    skip false stream
  end

  fun handleBackslash stream =
  let
    val (c, stream) = S.getchar stream

    val raiseErr = fn () =>
      let
          val pos = S.getPosAfterCharRead stream
          val pos = S.pos2ppos pos stream
      in
        raise TkErrorAug (pos, "expected \\n after backslash")
      end
  in
    case c of
      SOME c =>
        if c = #"\n" then
          stream
        else
          raiseErr ()
    | NONE => raiseErr ()
  end

  fun processSymbol stream =
  let
    val (T as (p, tk), stream) = fsmEat $ S.ungetc stream
  in
    case tk of
      CommentStart => getToken $ skipComment stream p
    | DoubleDot => (SOME (p, Dot), S.ungetc stream)
    | Hash =>
        if S.isFirstOnLine p stream then
          let
            val (tk, stream) = getToken stream
          in
            case tk of
              NONE =>
                raise TkErrorAug (S.pos2ppos p stream,
                    "unfinished preprecessor directive")
            | SOME tk =>
                (SOME $ handleCppDir tk p stream, stream)
          end
        else
          (SOME T, stream)
    | _ => (SOME T, stream)
  end

  and getToken stream =
  let
    val (c, stream) = S.getchar stream

    fun @-> parser acc =
      (fn (tk, s) => (SOME tk, s)) $ parserWrapper stream parser acc
  in
    case c of
      NONE => (NONE, stream)
    | SOME c =>
      if c = #"\n" then
        (SOME (S.getPosAfterCharRead stream, NewLine), stream)
      else if Char.isSpace c then
        getToken stream
      else if isIdStart c then
        @-> idParser ()
      else if isDigit c then
        @-> numParser NpInit
      else if c = #"'" then
        @-> charParser SeqInit
      else if c = #"\"" then
        @-> strParser SeqInit
      else if isStartForFsm c then
        processSymbol stream
      else if c = #"\\" then
        getToken $ handleBackslash stream
      else
        unexpectedCharRaise stream c
  end

  fun tokenize stream =
  let
    fun aux acc stream =
    let
      val (tk, stream) = getToken stream
    in
      case tk of
        NONE => rev acc
      | SOME tk => aux (tk :: acc) stream
    end
  in
    aux [] stream
  end

  fun debugPrint tkl fname =
  let
    fun print' line _ ((_, NewLine) :: tks) =
      print' (line + 1) true tks
      | print' line firstOnLine ((_, tk) :: tks) = (
        if firstOnLine then (
          print "\n";
          printLn $ fname ^ ":" ^ Int.toString line;
          print "\t")
        else
          ();
        printToken tk;
        print " ";
        print' line false tks
      )
      | print' _ _ [] = ()
  in
    print' 1 true tkl;
    print "\n"
  end
end