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
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
|
import options
import strformat
import strutils
import macros
import tables
import unicode
import encoding/decoderstream
import html/entity
import html/parseerror
import html/tags
import utils/opt
import utils/radixtree
import utils/twtstr
# Tokenizer
type
Tokenizer* = object
state*: TokenizerState
rstate: TokenizerState
tmp: string
code: int
tok: Token
laststart: Token
attrn: string
attrv: string
attr: bool
hasnonhtml*: bool
onParseError: proc(e: ParseError)
decoder: DecoderStream
sbuf: seq[Rune]
sbuf_i: int
eof_i: int
TokenType* = enum
DOCTYPE, START_TAG, END_TAG, COMMENT, CHARACTER, CHARACTER_ASCII, EOF
TokenizerState* = enum
DATA, CHARACTER_REFERENCE, TAG_OPEN, RCDATA, RCDATA_LESS_THAN_SIGN,
RAWTEXT, RAWTEXT_LESS_THAN_SIGN, SCRIPT_DATA, SCRIPT_DATA_LESS_THAN_SIGN,
PLAINTEXT, MARKUP_DECLARATION_OPEN, END_TAG_OPEN, BOGUS_COMMENT, TAG_NAME,
BEFORE_ATTRIBUTE_NAME, RCDATA_END_TAG_OPEN, RCDATA_END_TAG_NAME,
RAWTEXT_END_TAG_OPEN, RAWTEXT_END_TAG_NAME, SELF_CLOSING_START_TAG,
SCRIPT_DATA_END_TAG_OPEN, SCRIPT_DATA_ESCAPE_START,
SCRIPT_DATA_END_TAG_NAME, SCRIPT_DATA_ESCAPE_START_DASH,
SCRIPT_DATA_ESCAPED_DASH_DASH, SCRIPT_DATA_ESCAPED,
SCRIPT_DATA_ESCAPED_DASH, SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN,
SCRIPT_DATA_ESCAPED_END_TAG_OPEN, SCRIPT_DATA_DOUBLE_ESCAPE_START,
SCRIPT_DATA_ESCAPED_END_TAG_NAME, SCRIPT_DATA_DOUBLE_ESCAPED,
SCRIPT_DATA_DOUBLE_ESCAPED_DASH, SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN,
SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH, SCRIPT_DATA_DOUBLE_ESCAPE_END,
AFTER_ATTRIBUTE_NAME, ATTRIBUTE_NAME, BEFORE_ATTRIBUTE_VALUE,
ATTRIBUTE_VALUE_DOUBLE_QUOTED, ATTRIBUTE_VALUE_SINGLE_QUOTED,
ATTRIBUTE_VALUE_UNQUOTED, AFTER_ATTRIBUTE_VALUE_QUOTED, COMMENT_START,
CDATA_SECTION, COMMENT_START_DASH, COMMENT, COMMENT_END,
COMMENT_LESS_THAN_SIGN, COMMENT_END_DASH, COMMENT_LESS_THAN_SIGN_BANG,
COMMENT_LESS_THAN_SIGN_BANG_DASH, COMMENT_LESS_THAN_SIGN_BANG_DASH_DASH,
COMMENT_END_BANG, DOCTYPE, BEFORE_DOCTYPE_NAME, DOCTYPE_NAME,
AFTER_DOCTYPE_NAME, AFTER_DOCTYPE_PUBLIC_KEYWORD,
AFTER_DOCTYPE_SYSTEM_KEYWORD, BOGUS_DOCTYPE,
BEFORE_DOCTYPE_PUBLIC_IDENTIFIER, DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED,
DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED, AFTER_DOCTYPE_PUBLIC_IDENTIFIER,
BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS,
DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED,
DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED, BEFORE_DOCTYPE_SYSTEM_IDENTIFIER,
AFTER_DOCTYPE_SYSTEM_IDENTIFIER, CDATA_SECTION_BRACKET, CDATA_SECTION_END,
NAMED_CHARACTER_REFERENCE, NUMERIC_CHARACTER_REFERENCE,
AMBIGUOUS_AMPERSAND_STATE, HEXADECIMAL_CHARACTER_REFERENCE_START,
DECIMAL_CHARACTER_REFERENCE_START, HEXADECIMAL_CHARACTER_REFERENCE,
DECIMAL_CHARACTER_REFERENCE, NUMERIC_CHARACTER_REFERENCE_END
Token* = ref object
case t*: TokenType
of DOCTYPE:
name*: Option[string]
pubid*: Option[string]
sysid*: Option[string]
quirks*: bool
of START_TAG, END_TAG:
tagname*: string
tagtype*: TagType
selfclosing*: bool
attrs*: Table[string, string]
of CHARACTER:
r*: Rune
of CHARACTER_ASCII:
c*: char
of COMMENT:
data*: string
of EOF: discard
func `$`*(tok: Token): string =
case tok.t
of DOCTYPE: fmt"{tok.t} {tok.name} {tok.pubid} {tok.sysid} {tok.quirks}"
of START_TAG, END_TAG: fmt"{tok.t} {tok.tagname} {tok.selfclosing} {tok.attrs}"
of CHARACTER: fmt"{tok.t} {tok.r}"
of CHARACTER_ASCII: fmt"{tok.t} {tok.c}"
of COMMENT: fmt"{tok.t} {tok.data}"
of EOF: fmt"{tok.t}"
const bufLen = 1024 # * 4096 bytes
const copyBufLen = 16 # * 64 bytes
proc readn(t: var Tokenizer) =
let l = t.sbuf.len
t.sbuf.setLen(bufLen)
let n = t.decoder.readData(addr t.sbuf[l], (bufLen - l) * sizeof(Rune))
t.sbuf.setLen(l + n div sizeof(Rune))
if t.decoder.atEnd:
t.eof_i = t.sbuf.len
proc newTokenizer*(s: DecoderStream, onParseError: proc(e: ParseError)): Tokenizer =
var t = Tokenizer(
decoder: s,
sbuf: newSeqOfCap[Rune](bufLen),
eof_i: -1,
sbuf_i: 0,
onParseError: onParseError
)
t.readn()
return t
proc newTokenizer*(s: string): Tokenizer =
let rs = s.toRunes()
var t = Tokenizer(
sbuf: rs,
eof_i: rs.len,
sbuf_i: 0
)
return t
func atEof(t: Tokenizer): bool =
t.eof_i != -1 and t.sbuf_i >= t.eof_i
proc checkBufLen(t: var Tokenizer) =
if t.sbuf_i >= min(bufLen - copyBufLen, t.sbuf.len):
for i in t.sbuf_i ..< t.sbuf.len:
t.sbuf[i - t.sbuf_i] = t.sbuf[i]
t.sbuf.setLen(t.sbuf.len - t.sbuf_i)
t.sbuf_i = 0
if t.sbuf.len < bufLen:
t.readn()
proc consume(t: var Tokenizer): Rune =
t.checkBufLen()
## Normalize newlines (\r\n -> \n, single \r -> \n)
if t.sbuf[t.sbuf_i] == Rune('\r'):
inc t.sbuf_i
t.checkBufLen()
if t.atEof or t.sbuf[t.sbuf_i] != Rune('\n'):
# \r
result = Rune('\n')
return
# else, \r\n so just return the \n
result = t.sbuf[t.sbuf_i]
inc t.sbuf_i
proc reconsume(t: var Tokenizer) =
dec t.sbuf_i
iterator tokenize*(tokenizer: var Tokenizer): Token =
var tokqueue: seq[Token]
var running = true
template emit(tok: Token) =
if tok.t == START_TAG:
tokenizer.laststart = tok
if tok.t in {START_TAG, END_TAG}:
tok.tagtype = tagType(tok.tagname)
tokqueue.add(tok)
template emit(tok: TokenType) = emit Token(t: tok)
template emit(rn: Rune) = emit Token(t: CHARACTER, r: rn)
template emit(ch: char) = emit Token(t: CHARACTER_ASCII, c: ch)
template emit_eof =
emit EOF
running = false
template emit_tok =
if tokenizer.attr:
tokenizer.tok.attrs[tokenizer.attrn] = tokenizer.attrv
emit tokenizer.tok
template emit_current =
if is_eof:
emit_eof
elif c in Ascii:
emit c
else:
emit r
template emit_replacement = emit Rune(0xFFFD)
template switch_state(s: TokenizerState) =
tokenizer.state = s
template switch_state_return(s: TokenizerState) =
tokenizer.rstate = tokenizer.state
tokenizer.state = s
template reconsume_in(s: TokenizerState) =
tokenizer.reconsume()
switch_state s
template parse_error(error: untyped) =
if tokenizer.onParseError != nil:
tokenizer.onParseError(error)
template is_appropriate_end_tag_token(): bool =
tokenizer.laststart != nil and tokenizer.laststart.tagname == tokenizer.tok.tagname
template start_new_attribute =
if tokenizer.attr:
tokenizer.tok.attrs[tokenizer.attrn] = tokenizer.attrv
tokenizer.attrn = ""
tokenizer.attrv = ""
tokenizer.attr = true
template leave_attribute_name_state =
if tokenizer.attrn in tokenizer.tok.attrs:
tokenizer.attr = false
template append_to_current_attr_value(c: typed) =
if tokenizer.attr:
tokenizer.attrv &= c
template peek_str(s: string): bool =
# WARNING: will break on strings with copyBufLen + 4 bytes
# WARNING: only works with ascii
assert s.len < copyBufLen - 4 and s.len > 0
if tokenizer.eof_i != -1 and tokenizer.sbuf_i + s.len >= tokenizer.eof_i:
false
else:
var b = true
for i in 0 ..< s.len:
let c = tokenizer.sbuf[tokenizer.sbuf_i + i]
if not c.isAscii() or cast[char](c) != s[i]:
b = false
break
b
template peek_str_nocase(s: string): bool =
# WARNING: will break on strings with copyBufLen + 4 bytes
# WARNING: only works with UPPER CASE ascii
assert s.len < copyBufLen - 4 and s.len > 0
if tokenizer.eof_i != -1 and tokenizer.sbuf_i + s.len >= tokenizer.eof_i:
false
else:
var b = true
for i in 0 ..< s.len:
let c = tokenizer.sbuf[tokenizer.sbuf_i + i]
if not c.isAscii() or cast[char](c).toUpperAscii() != s[i]:
b = false
break
b
template peek_char(): char =
let r = tokenizer.sbuf[tokenizer.sbuf_i]
if r.isAscii():
cast[char](r)
else:
char(128)
template consume_and_discard(n: int) = #TODO optimize
var i = 0
while i < n:
discard tokenizer.consume()
inc i
template consumed_as_an_attribute(): bool =
tokenizer.rstate in {ATTRIBUTE_VALUE_DOUBLE_QUOTED, ATTRIBUTE_VALUE_SINGLE_QUOTED, ATTRIBUTE_VALUE_UNQUOTED}
template emit_tmp() =
var i = 0
while i < tokenizer.tmp.len:
if tokenizer.tmp[i] in Ascii:
emit tokenizer.tmp[i]
inc i
else:
var r: Rune
fastRuneAt(tokenizer.tmp, i, r)
emit r
template flush_code_points_consumed_as_a_character_reference() =
if consumed_as_an_attribute:
append_to_current_attr_value tokenizer.tmp
else:
emit_tmp
template new_token(t: Token) =
if tokenizer.attr:
tokenizer.attr = false
tokenizer.tok = t
# Fake EOF as an actual character. Also replace anything_else with the else
# branch.
macro stateMachine(states: varargs[untyped]): untyped =
var maincase = newNimNode(nnkCaseStmt).add(quote do: tokenizer.state)
for state in states:
if state.kind == nnkOfBranch:
var mainstmtlist: NimNode
var mainstmtlist_i = -1
for i in 0 ..< state.len:
if state[i].kind == nnkStmtList:
mainstmtlist = state[i]
mainstmtlist_i = i
break
if mainstmtlist[0].kind == nnkIdent and mainstmtlist[0].strVal == "ignore_eof":
maincase.add(state)
continue
var hasanythingelse = false
if mainstmtlist[0].kind == nnkIdent and mainstmtlist[0].strVal == "has_anything_else":
hasanythingelse = true
let childcase = findChild(mainstmtlist, it.kind == nnkCaseStmt)
var haseof = false
var eofstmts: NimNode
var elsestmts: NimNode
for i in countdown(childcase.len-1, 0):
let childof = childcase[i]
if childof.kind == nnkOfBranch:
for j in countdown(childof.len-1, 0):
if childof[j].kind == nnkIdent and childof[j].strVal == "eof":
haseof = true
eofstmts = childof.findChild(it.kind == nnkStmtList)
if childof.findChild(it.kind == nnkIdent and it.strVal != "eof") != nil:
childof.del(j)
else:
childcase.del(i)
elif childof.kind == nnkElse:
elsestmts = childof.findChild(it.kind == nnkStmtList)
if not haseof:
eofstmts = elsestmts
if hasanythingelse:
let fake_anything_else = quote do:
template anything_else =
`elsestmts`
mainstmtlist.insert(0, fake_anything_else)
let eofstmtlist = quote do:
if is_eof:
`eofstmts`
else:
`mainstmtlist`
state[mainstmtlist_i] = eofstmtlist
maincase.add(state)
result = newNimNode(nnkStmtList)
result.add(maincase)
template ignore_eof = discard # does nothing
template has_anything_else = discard # does nothing
const null = char(0)
while running:
#eprint tokenizer.state #debug
let is_eof = tokenizer.atEof # set eof here, otherwise we would exit at the last character
let r = if not is_eof:
tokenizer.consume()
else:
# avoid consuming eof...
Rune(null)
let c = if r.isAscii(): cast[char](r) else: char(128)
stateMachine: # => case tokenizer.state
of DATA:
case c
of '&': switch_state_return CHARACTER_REFERENCE
of '<': switch_state TAG_OPEN
of null:
parse_error UNEXPECTED_NULL_CHARACTER
emit_current
of eof: emit_eof
else: emit_current
of RCDATA:
case c
of '&': switch_state_return CHARACTER_REFERENCE
of '<': switch_state RCDATA_LESS_THAN_SIGN
of null: parse_error UNEXPECTED_NULL_CHARACTER
of eof: emit_eof
else: emit_current
of RAWTEXT:
case c
of '<': switch_state RAWTEXT_LESS_THAN_SIGN
of null:
parse_error UNEXPECTED_NULL_CHARACTER
emit_replacement
of eof: emit_eof
else: emit_current
of SCRIPT_DATA:
case c
of '<': switch_state SCRIPT_DATA_LESS_THAN_SIGN
of null:
parse_error UNEXPECTED_NULL_CHARACTER
emit_replacement
of eof: emit_eof
else: emit_current
of PLAINTEXT:
case c
of null:
parse_error UNEXPECTED_NULL_CHARACTER
emit_replacement
of eof: emit_eof
else: emit_current
of TAG_OPEN:
case c
of '!': switch_state MARKUP_DECLARATION_OPEN
of '/': switch_state END_TAG_OPEN
of AsciiAlpha:
new_token Token(t: START_TAG)
reconsume_in TAG_NAME
of '?':
parse_error UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME
new_token Token(t: COMMENT)
reconsume_in BOGUS_COMMENT
of eof:
parse_error EOF_BEFORE_TAG_NAME
emit '<'
emit_eof
else:
parse_error INVALID_FIRST_CHARACTER_OF_TAG_NAME
emit '<'
reconsume_in DATA
of END_TAG_OPEN:
case c
of AsciiAlpha:
new_token Token(t: END_TAG)
reconsume_in TAG_NAME
of '>':
parse_error MISSING_END_TAG_NAME
switch_state DATA
of eof:
parse_error EOF_BEFORE_TAG_NAME
emit '<'
emit '/'
emit_eof
else:
parse_error INVALID_FIRST_CHARACTER_OF_TAG_NAME
new_token Token(t: COMMENT)
reconsume_in BOGUS_COMMENT
of TAG_NAME:
case c
of AsciiWhitespace: switch_state BEFORE_ATTRIBUTE_NAME
of '/': switch_state SELF_CLOSING_START_TAG
of '>':
switch_state DATA
emit_tok
of AsciiUpperAlpha: tokenizer.tok.tagname &= c.tolower()
of null:
parse_error UNEXPECTED_NULL_CHARACTER
tokenizer.tok.tagname &= Rune(0xFFFD)
of eof:
parse_error EOF_IN_TAG
emit_eof
else: tokenizer.tok.tagname &= r
of RCDATA_LESS_THAN_SIGN:
case c
of '/':
tokenizer.tmp = ""
switch_state RCDATA_END_TAG_OPEN
else:
emit '<'
reconsume_in RCDATA
of RCDATA_END_TAG_OPEN:
case c
of AsciiAlpha:
new_token Token(t: END_TAG)
reconsume_in RCDATA_END_TAG_NAME
else:
emit '<'
emit '/'
reconsume_in RCDATA
of RCDATA_END_TAG_NAME:
has_anything_else
case c
of AsciiWhitespace:
if is_appropriate_end_tag_token:
switch_state BEFORE_ATTRIBUTE_NAME
else:
anything_else
of '/':
if is_appropriate_end_tag_token:
switch_state SELF_CLOSING_START_TAG
else:
anything_else
of '>':
if is_appropriate_end_tag_token:
switch_state DATA
emit_tok
else:
anything_else
of AsciiAlpha: # note: merged upper & lower
tokenizer.tok.tagname &= c.tolower()
tokenizer.tmp &= c
else:
new_token nil #TODO
emit '<'
emit '/'
emit_tmp
reconsume_in RCDATA
of RAWTEXT_LESS_THAN_SIGN:
case c
of '/':
tokenizer.tmp = ""
switch_state RAWTEXT_END_TAG_OPEN
else:
emit '<'
reconsume_in RAWTEXT
of RAWTEXT_END_TAG_OPEN:
case c
of AsciiAlpha:
new_token Token(t: END_TAG)
reconsume_in RAWTEXT_END_TAG_NAME
else:
emit '<'
emit '/'
reconsume_in RAWTEXT
of RAWTEXT_END_TAG_NAME:
has_anything_else
case c
of AsciiWhitespace:
if is_appropriate_end_tag_token:
switch_state BEFORE_ATTRIBUTE_NAME
else:
anything_else
of '/':
if is_appropriate_end_tag_token:
switch_state SELF_CLOSING_START_TAG
else:
anything_else
of '>':
if is_appropriate_end_tag_token:
switch_state DATA
emit_tok
else:
anything_else
of AsciiAlpha: # note: merged upper & lower
tokenizer.tok.tagname &= c.tolower()
tokenizer.tmp &= c
else:
new_token nil #TODO
emit '<'
emit '/'
for r in tokenizer.tmp.runes:
emit r
reconsume_in RAWTEXT
of SCRIPT_DATA_LESS_THAN_SIGN:
case c
of '/':
tokenizer.tmp = ""
switch_state SCRIPT_DATA_END_TAG_OPEN
of '!':
switch_state SCRIPT_DATA_ESCAPE_START
emit '<'
emit '!'
else:
emit '<'
reconsume_in SCRIPT_DATA
of SCRIPT_DATA_END_TAG_OPEN:
case c
of AsciiAlpha:
new_token Token(t: END_TAG)
reconsume_in SCRIPT_DATA_END_TAG_NAME
else:
emit '<'
emit '/'
reconsume_in SCRIPT_DATA
of SCRIPT_DATA_END_TAG_NAME:
has_anything_else
case c
of AsciiWhitespace:
if is_appropriate_end_tag_token:
switch_state BEFORE_ATTRIBUTE_NAME
else:
anything_else
of '/':
if is_appropriate_end_tag_token:
switch_state SELF_CLOSING_START_TAG
else:
anything_else
of '>':
if is_appropriate_end_tag_token:
switch_state DATA
emit_tok
else:
anything_else
of AsciiAlpha: # note: merged upper & lower
tokenizer.tok.tagname &= c.tolower()
tokenizer.tmp &= c
else:
emit '<'
emit '/'
emit_tmp
reconsume_in SCRIPT_DATA
of SCRIPT_DATA_ESCAPE_START:
case c
of '-':
switch_state SCRIPT_DATA_ESCAPE_START_DASH
emit '-'
else:
reconsume_in SCRIPT_DATA
of SCRIPT_DATA_ESCAPE_START_DASH:
case c
of '-':
switch_state SCRIPT_DATA_ESCAPED_DASH_DASH
emit '-'
else:
reconsume_in SCRIPT_DATA
of SCRIPT_DATA_ESCAPED:
case c
of '-':
switch_state SCRIPT_DATA_ESCAPED_DASH
emit '-'
of '<':
switch_state SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN
of null:
parse_error UNEXPECTED_NULL_CHARACTER
emit_replacement
of eof:
parse_error EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT
emit_eof
else:
emit_current
of SCRIPT_DATA_ESCAPED_DASH:
case c
of '-':
switch_state SCRIPT_DATA_ESCAPED_DASH_DASH
emit '-'
of '<':
switch_state SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN
of null:
parse_error UNEXPECTED_NULL_CHARACTER
switch_state SCRIPT_DATA_ESCAPED
of eof:
parse_error EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT
emit_eof
else:
switch_state SCRIPT_DATA_ESCAPED
emit_current
of SCRIPT_DATA_ESCAPED_DASH_DASH:
case c
of '-':
emit '-'
of '<':
switch_state SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN
of '>':
switch_state SCRIPT_DATA
emit '>'
of null:
parse_error UNEXPECTED_NULL_CHARACTER
switch_state SCRIPT_DATA_ESCAPED
of eof:
parse_error EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT
emit_eof
else:
switch_state SCRIPT_DATA_ESCAPED
emit_current
of SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN:
case c
of '/':
tokenizer.tmp = ""
switch_state SCRIPT_DATA_ESCAPED_END_TAG_OPEN
of AsciiAlpha:
tokenizer.tmp = ""
emit '<'
reconsume_in SCRIPT_DATA_DOUBLE_ESCAPE_START
else:
emit '<'
reconsume_in SCRIPT_DATA_ESCAPED
of SCRIPT_DATA_ESCAPED_END_TAG_OPEN:
case c
of AsciiAlpha:
new_token Token(t: START_TAG)
reconsume_in SCRIPT_DATA_ESCAPED_END_TAG_NAME
else:
emit '<'
emit '/'
reconsume_in SCRIPT_DATA_ESCAPED
of SCRIPT_DATA_ESCAPED_END_TAG_NAME:
has_anything_else
case c
of AsciiWhitespace:
if is_appropriate_end_tag_token:
switch_state BEFORE_ATTRIBUTE_NAME
else:
anything_else
of '/':
if is_appropriate_end_tag_token:
switch_state SELF_CLOSING_START_TAG
else:
anything_else
of '>':
if is_appropriate_end_tag_token:
switch_state DATA
else:
anything_else
of AsciiAlpha:
tokenizer.tok.tagname &= c.tolower()
tokenizer.tmp &= c
else:
emit '<'
emit '/'
emit_tmp
reconsume_in SCRIPT_DATA_ESCAPED
of SCRIPT_DATA_DOUBLE_ESCAPE_START:
case c
of AsciiWhitespace, '/', '>':
if tokenizer.tmp == "script":
switch_state SCRIPT_DATA_DOUBLE_ESCAPED
else:
switch_state SCRIPT_DATA_ESCAPED
emit_current
of AsciiAlpha: # note: merged upper & lower
tokenizer.tmp &= c.tolower()
emit_current
else: reconsume_in SCRIPT_DATA_ESCAPED
of SCRIPT_DATA_DOUBLE_ESCAPED:
case c
of '-':
switch_state SCRIPT_DATA_DOUBLE_ESCAPED_DASH
emit '-'
of '<':
switch_state SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN
emit '<'
of null:
parse_error UNEXPECTED_NULL_CHARACTER
emit_replacement
of eof:
parse_error EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT
emit_eof
else: emit_current
of SCRIPT_DATA_DOUBLE_ESCAPED_DASH:
case c
of '-':
switch_state SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH
emit '-'
of '<':
switch_state SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN
emit '<'
of null:
parse_error UNEXPECTED_NULL_CHARACTER
switch_state SCRIPT_DATA_DOUBLE_ESCAPED
emit_replacement
of eof:
parse_error EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT
emit_eof
else:
switch_state SCRIPT_DATA_DOUBLE_ESCAPED
emit_current
of SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH:
case c
of '-': emit '-'
of '<':
switch_state SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN
emit '<'
of '>':
switch_state SCRIPT_DATA
emit '>'
of null:
parse_error UNEXPECTED_NULL_CHARACTER
switch_state SCRIPT_DATA_DOUBLE_ESCAPED
emit_replacement
of eof:
parse_error EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT
emit_eof
else: switch_state SCRIPT_DATA_DOUBLE_ESCAPED
of SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN:
case c
of '/':
tokenizer.tmp = ""
switch_state SCRIPT_DATA_DOUBLE_ESCAPE_END
emit '/'
else: reconsume_in SCRIPT_DATA_DOUBLE_ESCAPED
of SCRIPT_DATA_DOUBLE_ESCAPE_END:
case c
of AsciiWhitespace, '/', '>':
if tokenizer.tmp == "script":
switch_state SCRIPT_DATA_ESCAPED
else:
switch_state SCRIPT_DATA_DOUBLE_ESCAPED
emit_current
of AsciiAlpha: # note: merged upper & lower
tokenizer.tmp &= c.tolower()
emit_current
else:
reconsume_in SCRIPT_DATA_DOUBLE_ESCAPED
of BEFORE_ATTRIBUTE_NAME:
case c
of AsciiWhitespace: discard
of '/', '>', eof: reconsume_in AFTER_ATTRIBUTE_NAME
of '=':
parse_error UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME
start_new_attribute
switch_state ATTRIBUTE_NAME
else:
start_new_attribute
reconsume_in ATTRIBUTE_NAME
of ATTRIBUTE_NAME:
has_anything_else
case c
of AsciiWhitespace, '/', '>', eof:
leave_attribute_name_state
reconsume_in AFTER_ATTRIBUTE_NAME
of '=':
leave_attribute_name_state
switch_state BEFORE_ATTRIBUTE_VALUE
of AsciiUpperAlpha:
tokenizer.attrn &= c.tolower()
of null:
parse_error UNEXPECTED_NULL_CHARACTER
tokenizer.attrn &= Rune(0xFFFD)
of '"', '\'', '<':
parse_error UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME
anything_else
else:
tokenizer.attrn &= r
of AFTER_ATTRIBUTE_NAME:
case c
of AsciiWhitespace: discard
of '/': switch_state SELF_CLOSING_START_TAG
of '=': switch_state BEFORE_ATTRIBUTE_VALUE
of '>':
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_TAG
emit_eof
else:
start_new_attribute
reconsume_in ATTRIBUTE_NAME
of BEFORE_ATTRIBUTE_VALUE:
case c
of AsciiWhitespace: discard
of '"': switch_state ATTRIBUTE_VALUE_DOUBLE_QUOTED
of '\'': switch_state ATTRIBUTE_VALUE_SINGLE_QUOTED
of '>':
parse_error MISSING_ATTRIBUTE_VALUE
switch_state DATA
emit '>'
else: reconsume_in ATTRIBUTE_VALUE_UNQUOTED
of ATTRIBUTE_VALUE_DOUBLE_QUOTED:
case c
of '"': switch_state AFTER_ATTRIBUTE_VALUE_QUOTED
of '&': switch_state_return CHARACTER_REFERENCE
of null:
parse_error UNEXPECTED_NULL_CHARACTER
append_to_current_attr_value Rune(0xFFFD)
of eof:
parse_error EOF_IN_TAG
emit_eof
else: append_to_current_attr_value r
of ATTRIBUTE_VALUE_SINGLE_QUOTED:
case c
of '\'': switch_state AFTER_ATTRIBUTE_VALUE_QUOTED
of '&': switch_state_return CHARACTER_REFERENCE
of null:
parse_error UNEXPECTED_NULL_CHARACTER
append_to_current_attr_value Rune(0xFFFD)
of eof:
parse_error EOF_IN_TAG
emit_eof
else: append_to_current_attr_value r
of ATTRIBUTE_VALUE_UNQUOTED:
case c
of AsciiWhitespace: switch_state BEFORE_ATTRIBUTE_NAME
of '&': switch_state_return CHARACTER_REFERENCE
of '>':
switch_state DATA
emit_tok
of null:
parse_error UNEXPECTED_NULL_CHARACTER
append_to_current_attr_value Rune(0xFFFD)
of '"', '\'', '<', '=', '`':
parse_error UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE
append_to_current_attr_value c
of eof:
parse_error EOF_IN_TAG
emit_eof
else: append_to_current_attr_value r
of AFTER_ATTRIBUTE_VALUE_QUOTED:
case c
of AsciiWhitespace:
switch_state BEFORE_ATTRIBUTE_NAME
of '/':
switch_state SELF_CLOSING_START_TAG
of '>':
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_TAG
emit_eof
else:
parse_error MISSING_WHITESPACE_BETWEEN_ATTRIBUTES
reconsume_in BEFORE_ATTRIBUTE_NAME
of SELF_CLOSING_START_TAG:
case c
of '>':
tokenizer.tok.selfclosing = true
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_TAG
emit_eof
else:
parse_error UNEXPECTED_SOLIDUS_IN_TAG
reconsume_in BEFORE_ATTRIBUTE_NAME
of BOGUS_COMMENT:
assert tokenizer.tok.t == COMMENT
case c
of '>':
switch_state DATA
emit_tok
of eof:
emit_tok
emit_eof
of null: parse_error UNEXPECTED_NULL_CHARACTER
else: tokenizer.tok.data &= r
of MARKUP_DECLARATION_OPEN: # note: rewritten to fit case model as we consume a char anyway
has_anything_else
case c
of '-':
if peek_char == '-':
new_token Token(t: COMMENT)
tokenizer.state = COMMENT_START
consume_and_discard 1
else: anything_else
of 'D', 'd':
if peek_str_nocase("OCTYPE"):
consume_and_discard "OCTYPE".len
switch_state DOCTYPE
else: anything_else
of '[':
if peek_str("CDATA["):
consume_and_discard "CDATA[".len
if tokenizer.hasnonhtml:
switch_state CDATA_SECTION
else:
parse_error CDATA_IN_HTML_CONTENT
new_token Token(t: COMMENT, data: "[CDATA[")
switch_state BOGUS_COMMENT
else: anything_else
else:
parse_error INCORRECTLY_OPENED_COMMENT
new_token Token(t: COMMENT)
reconsume_in BOGUS_COMMENT
of COMMENT_START:
case c
of '-': switch_state COMMENT_START_DASH
of '>':
parse_error ABRUPT_CLOSING_OF_EMPTY_COMMENT
switch_state DATA
emit_tok
else: reconsume_in COMMENT
of COMMENT_START_DASH:
case c
of '-': switch_state COMMENT_END
of '>':
parse_error ABRUPT_CLOSING_OF_EMPTY_COMMENT
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_COMMENT
emit_tok
emit_eof
else:
tokenizer.tok.data &= '-'
reconsume_in COMMENT
of COMMENT:
case c
of '<':
tokenizer.tok.data &= c
switch_state COMMENT_LESS_THAN_SIGN
of '-': switch_state COMMENT_END_DASH
of null:
parse_error UNEXPECTED_NULL_CHARACTER
tokenizer.tok.data &= Rune(0xFFFD)
of eof:
parse_error EOF_IN_COMMENT
emit_tok
emit_eof
else: tokenizer.tok.data &= r
of COMMENT_LESS_THAN_SIGN:
case c
of '!':
tokenizer.tok.data &= c
switch_state COMMENT_LESS_THAN_SIGN_BANG
of '<': tokenizer.tok.data &= c
else: reconsume_in COMMENT
of COMMENT_LESS_THAN_SIGN_BANG:
case c
of '-': switch_state COMMENT_LESS_THAN_SIGN_BANG_DASH
else: reconsume_in COMMENT
of COMMENT_LESS_THAN_SIGN_BANG_DASH:
case c
of '-': switch_state COMMENT_LESS_THAN_SIGN_BANG_DASH_DASH
else: reconsume_in COMMENT_END_DASH
of COMMENT_LESS_THAN_SIGN_BANG_DASH_DASH:
case c
of '>', eof: reconsume_in COMMENT_END
else:
parse_error NESTED_COMMENT
reconsume_in COMMENT_END
of COMMENT_END_DASH:
case c
of '-': switch_state COMMENT_END
of eof:
parse_error EOF_IN_COMMENT
emit_tok
emit_eof
else:
tokenizer.tok.data &= '-'
reconsume_in COMMENT
of COMMENT_END:
case c
of '>': switch_state DATA
of '!': switch_state COMMENT_END_BANG
of '-': tokenizer.tok.data &= '-'
of eof:
parse_error EOF_IN_COMMENT
emit_tok
emit_eof
else:
tokenizer.tok.data &= "--"
reconsume_in COMMENT
of COMMENT_END_BANG:
case c
of '-':
tokenizer.tok.data &= "--!"
switch_state COMMENT_END_DASH
of '>':
parse_error INCORRECTLY_CLOSED_COMMENT
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_COMMENT
emit_tok
emit_eof
else:
tokenizer.tok.data &= "--!"
reconsume_in COMMENT
of DOCTYPE:
case c
of AsciiWhitespace: switch_state BEFORE_DOCTYPE_NAME
of '>': reconsume_in BEFORE_DOCTYPE_NAME
of eof:
parse_error EOF_IN_DOCTYPE
new_token Token(t: DOCTYPE, quirks: true)
emit_tok
emit_eof
else:
parse_error MISSING_WHITESPACE_BEFORE_DOCTYPE_NAME
reconsume_in BEFORE_DOCTYPE_NAME
of BEFORE_DOCTYPE_NAME:
case c
of AsciiWhitespace: discard
of AsciiUpperAlpha:
new_token Token(t: DOCTYPE, name: some($c.tolower()))
switch_state DOCTYPE_NAME
of null:
parse_error UNEXPECTED_NULL_CHARACTER
new_token Token(t: DOCTYPE, name: some($Rune(0xFFFD)))
of '>':
parse_error MISSING_DOCTYPE_NAME
new_token Token(t: DOCTYPE, quirks: true)
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_DOCTYPE
new_token Token(t: DOCTYPE, quirks: true)
emit_tok
emit_eof
else:
new_token Token(t: DOCTYPE, name: some($r))
switch_state DOCTYPE_NAME
of DOCTYPE_NAME:
case c
of AsciiWhitespace: switch_state AFTER_DOCTYPE_NAME
of '>':
switch_state DATA
emit_tok
of AsciiUpperAlpha:
tokenizer.tok.name.get &= c.tolower()
of null:
parse_error UNEXPECTED_NULL_CHARACTER
tokenizer.tok.name.get &= Rune(0xFFFD)
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
else:
tokenizer.tok.name.get &= r
of AFTER_DOCTYPE_NAME: # note: rewritten to fit case model as we consume a char anyway
has_anything_else
case c
of AsciiWhitespace: discard
of '>':
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
of 'p', 'P':
if peek_str("UBLIC"):
consume_and_discard "UBLIC".len
switch_state AFTER_DOCTYPE_PUBLIC_KEYWORD
else:
anything_else
of 's', 'S':
if peek_str("YSTEM"):
consume_and_discard "YSTEM".len
switch_state AFTER_DOCTYPE_SYSTEM_KEYWORD
else:
anything_else
else:
parse_error INVALID_CHARACTER_SEQUENCE_AFTER_DOCTYPE_NAME
tokenizer.tok.quirks = true
reconsume_in BOGUS_DOCTYPE
of AFTER_DOCTYPE_PUBLIC_KEYWORD:
case c
of AsciiWhitespace: switch_state BEFORE_DOCTYPE_PUBLIC_IDENTIFIER
of '"':
parse_error MISSING_WHITESPACE_AFTER_DOCTYPE_PUBLIC_KEYWORD
tokenizer.tok.pubid = some("")
switch_state DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED
of '>':
parse_error MISSING_DOCTYPE_PUBLIC_IDENTIFIER
tokenizer.tok.quirks = true
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
else:
parse_error MISSING_QUOTE_BEFORE_DOCTYPE_PUBLIC_IDENTIFIER
tokenizer.tok.quirks = true
reconsume_in BOGUS_DOCTYPE
of BEFORE_DOCTYPE_PUBLIC_IDENTIFIER:
case c
of AsciiWhitespace: discard
of '"':
tokenizer.tok.pubid = some("")
switch_state DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED
of '\'':
tokenizer.tok.pubid = some("")
switch_state DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED
of '>':
parse_error MISSING_DOCTYPE_PUBLIC_IDENTIFIER
tokenizer.tok.quirks = true
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
else:
parse_error MISSING_QUOTE_BEFORE_DOCTYPE_PUBLIC_IDENTIFIER
tokenizer.tok.quirks = true
reconsume_in BOGUS_DOCTYPE
of DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED:
case c
of '"': switch_state AFTER_DOCTYPE_PUBLIC_IDENTIFIER
of null:
parse_error UNEXPECTED_NULL_CHARACTER
tokenizer.tok.pubid.get &= Rune(0xFFFD)
of '>':
parse_error ABRUPT_DOCTYPE_PUBLIC_IDENTIFIER
tokenizer.tok.quirks = true
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
else:
tokenizer.tok.pubid.get &= r
of DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED:
case c
of '\'': switch_state AFTER_DOCTYPE_PUBLIC_IDENTIFIER
of null:
parse_error UNEXPECTED_NULL_CHARACTER
tokenizer.tok.pubid.get &= Rune(0xFFFD)
of '>':
parse_error ABRUPT_DOCTYPE_PUBLIC_IDENTIFIER
tokenizer.tok.quirks = true
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
else:
tokenizer.tok.pubid.get &= r
of AFTER_DOCTYPE_PUBLIC_IDENTIFIER:
case c
of AsciiWhitespace: switch_state BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS
of '>':
switch_state DATA
emit_tok
of '"':
parse_error MISSING_WHITESPACE_BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS
tokenizer.tok.sysid = some("")
switch_state DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED
of '\'':
parse_error MISSING_WHITESPACE_BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS
tokenizer.tok.sysid = some("")
switch_state DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
else:
parse_error MISSING_QUOTE_BEFORE_DOCTYPE_SYSTEM_IDENTIFIER
tokenizer.tok.quirks = true
reconsume_in BOGUS_DOCTYPE
of BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS:
case c
of AsciiWhitespace: discard
of '>':
switch_state DATA
emit_tok
of '"':
tokenizer.tok.sysid = some("")
switch_state DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED
of '\'':
tokenizer.tok.sysid = some("")
switch_state DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
else:
parse_error MISSING_QUOTE_BEFORE_DOCTYPE_SYSTEM_IDENTIFIER
tokenizer.tok.quirks = true
reconsume_in BOGUS_DOCTYPE
of AFTER_DOCTYPE_SYSTEM_KEYWORD:
case c
of AsciiWhitespace: switch_state BEFORE_DOCTYPE_SYSTEM_IDENTIFIER
of '"':
parse_error MISSING_WHITESPACE_AFTER_DOCTYPE_SYSTEM_KEYWORD
tokenizer.tok.sysid = some("")
switch_state DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED
of '\'':
parse_error MISSING_WHITESPACE_AFTER_DOCTYPE_SYSTEM_KEYWORD
tokenizer.tok.sysid = some("")
switch_state DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED
of '>':
parse_error MISSING_DOCTYPE_SYSTEM_IDENTIFIER
tokenizer.tok.quirks = true
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
else:
parse_error MISSING_QUOTE_BEFORE_DOCTYPE_SYSTEM_IDENTIFIER
tokenizer.tok.quirks = true
reconsume_in BOGUS_DOCTYPE
of BEFORE_DOCTYPE_SYSTEM_IDENTIFIER:
case c
of AsciiWhitespace: discard
of '"':
tokenizer.tok.pubid = some("")
switch_state DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED
of '\'':
tokenizer.tok.pubid = some("")
switch_state DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED
of '>':
parse_error MISSING_DOCTYPE_SYSTEM_IDENTIFIER
tokenizer.tok.quirks = true
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
else:
parse_error MISSING_QUOTE_BEFORE_DOCTYPE_SYSTEM_IDENTIFIER
tokenizer.tok.quirks = true
reconsume_in BOGUS_DOCTYPE
of DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED:
case c
of '"': switch_state AFTER_DOCTYPE_SYSTEM_IDENTIFIER
of null:
parse_error UNEXPECTED_NULL_CHARACTER
tokenizer.tok.sysid.get &= Rune(0xFFFD)
of '>':
parse_error ABRUPT_DOCTYPE_SYSTEM_IDENTIFIER
tokenizer.tok.quirks = true
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
else:
tokenizer.tok.sysid.get &= r
of DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED:
case c
of '\'': switch_state AFTER_DOCTYPE_SYSTEM_IDENTIFIER
of null:
parse_error UNEXPECTED_NULL_CHARACTER
tokenizer.tok.sysid.get &= Rune(0xFFFD)
of '>':
parse_error ABRUPT_DOCTYPE_SYSTEM_IDENTIFIER
tokenizer.tok.quirks = true
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
else:
tokenizer.tok.sysid.get &= r
of AFTER_DOCTYPE_SYSTEM_IDENTIFIER:
case c
of AsciiWhitespace: discard
of '>':
switch_state DATA
emit_tok
of eof:
parse_error EOF_IN_DOCTYPE
tokenizer.tok.quirks = true
emit_tok
emit_eof
else:
parse_error UNEXPECTED_CHARACTER_AFTER_DOCTYPE_SYSTEM_IDENTIFIER
reconsume_in BOGUS_DOCTYPE
of BOGUS_DOCTYPE:
case c
of '>':
switch_state DATA
emit_tok
of null: parse_error UNEXPECTED_NULL_CHARACTER
of eof:
emit_tok
emit_eof
else: discard
of CDATA_SECTION:
case c
of ']': switch_state CDATA_SECTION_BRACKET
of eof:
parse_error EOF_IN_CDATA
emit_eof
else:
emit_current
of CDATA_SECTION_BRACKET:
case c
of ']': switch_state CDATA_SECTION_END
of '>': switch_state DATA
else:
emit ']'
reconsume_in CDATA_SECTION
of CDATA_SECTION_END:
case c
of ']': emit ']'
of '>': switch_state DATA
else:
emit ']'
emit ']'
reconsume_in CDATA_SECTION
of CHARACTER_REFERENCE:
tokenizer.tmp = "&"
case c
of AsciiAlpha: reconsume_in NAMED_CHARACTER_REFERENCE
of '#':
tokenizer.tmp &= '#'
switch_state NUMERIC_CHARACTER_REFERENCE
else:
flush_code_points_consumed_as_a_character_reference
reconsume_in tokenizer.rstate
of NAMED_CHARACTER_REFERENCE:
ignore_eof # we check for eof ourselves
tokenizer.reconsume()
when nimvm:
eprint "Cannot evaluate character references at compile time"
else:
var tokenizerp = addr tokenizer
var lasti = 0
let value = entityMap.find(proc(s: var string): bool =
if tokenizerp[].atEof:
return false
let rs = $tokenizerp[].consume()
lasti = tokenizerp[].tmp.len
tokenizerp[].tmp &= rs
s &= rs
return true
)
tokenizer.reconsume()
tokenizer.tmp.setLen(lasti)
if value.isOk:
if consumed_as_an_attribute and tokenizer.tmp[^1] != ';' and peek_char in {'='} + AsciiAlpha:
flush_code_points_consumed_as_a_character_reference
switch_state tokenizer.rstate
else:
if tokenizer.tmp[^1] != ';':
parse_error MISSING_SEMICOLON_AFTER_CHARACTER_REFERENCE
tokenizer.tmp = value.get
flush_code_points_consumed_as_a_character_reference
switch_state tokenizer.rstate
else:
flush_code_points_consumed_as_a_character_reference
switch_state AMBIGUOUS_AMPERSAND_STATE
of AMBIGUOUS_AMPERSAND_STATE:
case c
of AsciiAlpha:
if consumed_as_an_attribute:
append_to_current_attr_value c
else:
emit_current
of ';':
parse_error UNKNOWN_NAMED_CHARACTER_REFERENCE
reconsume_in tokenizer.rstate
else: reconsume_in tokenizer.rstate
of NUMERIC_CHARACTER_REFERENCE:
tokenizer.code = 0
case c
of 'x', 'X':
tokenizer.tmp &= c
switch_state HEXADECIMAL_CHARACTER_REFERENCE_START
else: reconsume_in DECIMAL_CHARACTER_REFERENCE_START
of HEXADECIMAL_CHARACTER_REFERENCE_START:
case c
of AsciiHexDigit: reconsume_in HEXADECIMAL_CHARACTER_REFERENCE
else:
parse_error ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE
flush_code_points_consumed_as_a_character_reference
reconsume_in tokenizer.rstate
of DECIMAL_CHARACTER_REFERENCE_START:
case c
of AsciiDigit: reconsume_in DECIMAL_CHARACTER_REFERENCE
else:
parse_error ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE
flush_code_points_consumed_as_a_character_reference
reconsume_in tokenizer.rstate
of HEXADECIMAL_CHARACTER_REFERENCE:
case c
of AsciiHexDigit: # note: merged digit, upper hex, lower hex
tokenizer.code *= 0x10
tokenizer.code += hexValue(c)
of ';': switch_state NUMERIC_CHARACTER_REFERENCE_END
else:
parse_error MISSING_SEMICOLON_AFTER_CHARACTER_REFERENCE
reconsume_in NUMERIC_CHARACTER_REFERENCE_END
of DECIMAL_CHARACTER_REFERENCE:
case c
of AsciiDigit:
tokenizer.code *= 10
tokenizer.code += decValue(c)
of ';': switch_state NUMERIC_CHARACTER_REFERENCE_END
else:
parse_error MISSING_SEMICOLON_AFTER_CHARACTER_REFERENCE
reconsume_in NUMERIC_CHARACTER_REFERENCE_END
of NUMERIC_CHARACTER_REFERENCE_END:
ignore_eof # we reconsume anyway
case tokenizer.code
of 0x00:
parse_error NULL_CHARACTER_REFERENCE
tokenizer.code = 0xFFFD
elif tokenizer.code > 0x10FFFF:
parse_error CHARACTER_REFERENCE_OUTSIDE_UNICODE_RANGE
tokenizer.code = 0xFFFD
elif Rune(tokenizer.code).isSurrogate():
parse_error SURROGATE_CHARACTER_REFERENCE
tokenizer.code = 0xFFFD
elif Rune(tokenizer.code).isNonCharacter():
parse_error NONCHARACTER_CHARACTER_REFERENCE
# do nothing
elif tokenizer.code in 0..255 and char(tokenizer.code) in ((Controls - AsciiWhitespace) + {chr(0x0D)}):
const ControlMapTable = [
(0x80, 0x20AC), (0x82, 0x201A), (0x83, 0x0192), (0x84, 0x201E),
(0x85, 0x2026), (0x86, 0x2020), (0x87, 0x2021), (0x88, 0x02C6),
(0x89, 0x2030), (0x8A, 0x0160), (0x8B, 0x2039), (0x8C, 0x0152),
(0x8E, 0x017D), (0x91, 0x2018), (0x92, 0x2019), (0x93, 0x201C),
(0x94, 0x201D), (0x95, 0x2022), (0x96, 0x2013), (0x97, 0x2014),
(0x98, 0x02DC), (0x99, 0x2122), (0x9A, 0x0161), (0x9B, 0x203A),
(0x9C, 0x0153), (0x9E, 0x017E), (0x9F, 0x0178),
].toTable()
if ControlMapTable.hasKey(tokenizer.code):
tokenizer.code = ControlMapTable[tokenizer.code]
tokenizer.tmp = $Rune(tokenizer.code)
flush_code_points_consumed_as_a_character_reference #TODO optimize so we flush directly
reconsume_in tokenizer.rstate # we unnecessarily consumed once so reconsume
for tok in tokqueue:
yield tok
tokqueue.setLen(0)
|