-
Notifications
You must be signed in to change notification settings - Fork 14
/
tokenizer.lisp
1635 lines (1541 loc) · 71.9 KB
/
tokenizer.lisp
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
;;;; HTML5 parser for Common Lisp
;;;;
;;;; Copyright (C) 2012 Thomas Bakketun <[email protected]>
;;;; Copyright (C) 2012 Asgeir Bjørlykke <[email protected]>
;;;; Copyright (C) 2012 Mathias Hellevang
;;;; Copyright (C) 2012 Stian Sletner <[email protected]>
;;;;
;;;; This library is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Lesser General Public License as published
;;;; by the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this library. If not, see <http://www.gnu.org/licenses/>.
(in-package :html5-parser)
(defclass html-tokenizer ()
((stream :initarg :stream :reader tokenizer-stream)
(cdata-switch-helper :initarg :cdata-switch-helper
:initform (constantly nil))
(lowercase-element-name :initform t)
(lowercase-attr-name :initform t)
(escape-flag :initform nil)
(last-four-chars :initform nil)
(state :initform :data-state :accessor tokenizer-state)
(escape :initform nil)
(current-token :initform nil)
(token-queue :initform nil)
(temporary-buffer :initform nil)))
(defun make-html-tokenizer (source &key encoding cdata-switch-helper)
(make-instance 'html-tokenizer
:stream (make-html-input-stream source :override-encoding encoding)
:cdata-switch-helper cdata-switch-helper))
(defun map-tokens (tokenizer function)
"Return next token or NIL on eof"
(with-slots (token-queue stream) tokenizer
(loop while (run-state tokenizer) do
(setf token-queue (nreverse token-queue))
(loop while (html5-stream-errors stream)
do (funcall function (list :type :parse-error :data (pop (html5-stream-errors stream)))))
(loop while token-queue
do (funcall function (pop token-queue))))))
(defun run-state (tokenizer)
(run-state* tokenizer (slot-value tokenizer 'state)))
(defgeneric run-state* (tokenizer state))
(defmacro defstate (state (&rest slots) &body body)
`(defmethod run-state* (self (state (eql ,state)))
(with-slots (,@slots) self
(block nil
,@body
t))))
(defun push-token (self token)
(with-slots (token-queue) self
(push token token-queue)))
(defun make-growable-string (&optional (init ""))
"Make an adjustable string with a fill pointer.
Given INIT, a string, return an adjustable version of it with the fill
pointer at the end."
(let ((string
(make-array (max 5 (length init))
:element-type 'character
:adjustable t
:fill-pointer (length init))))
(when init
(replace string init))
string))
(defun nconcat (string &rest data)
"Destructively concatenate DATA, string designators, to STRING."
(declare (optimize speed))
(unless (array-has-fill-pointer-p string)
(setf string (make-growable-string string)))
(labels ((conc (string x)
(typecase x
(character
(vector-push-extend x string))
(string
(let ((len (length x)))
(loop for c across x do
(vector-push-extend c string len))))
(symbol (conc string (string x))))))
(dolist (x data string)
(conc string x))))
(define-modify-macro nconcatf (&rest data) nconcat)
(defun push-token* (self type &rest data)
"Push a token with :type type and :data the a string concatenation of data"
(push-token self (list :type type
:data (apply #'nconcat (make-growable-string) data))))
(defun add-attribute (token name)
(setf (getf token :data) (append (getf token :data)
(list (cons (make-growable-string (string name))
(make-growable-string))))))
(defun add-to-attr-name (token &rest data)
(setf (caar (last (getf token :data)))
(apply #'nconcat
(caar (last (getf token :data)))
data)))
(defun add-to-attr-value (token &rest data)
(setf (cdar (last (getf token :data)))
(apply #'nconcat
(cdar (last (getf token :data)))
data)))
(defun add-to (token indicator &rest data)
(setf (getf token indicator)
(apply #'nconcat
(getf token indicator)
data)))
(defun consume-number-entity (self is-hex)
"This function returns either U+FFFD or the character based on the
decimal or hexadecimal representation. It also discards \";\" if present.
If not present a token (:type :parse-error) is emitted.
"
(with-slots (stream) self
(let ((allowed +digits+)
(radix 10)
(char-stack)
(c)
(char-as-int)
(char))
(when is-hex
(setf allowed +hex-digits+)
(setf radix 16))
;; Consume all the characters that are in range while making sure we
;; don't hit an EOF.
(setf c (html5-stream-char stream))
(loop while (and (find c allowed) (not (eql c +eof+))) do
(push c char-stack)
(setf c (html5-stream-char stream)))
;; Convert the set of characters consumed to an int.
(setf char-as-int (parse-integer (coerce (nreverse char-stack) 'string) :radix radix))
;; Certain characters get replaced with others
(cond ((find char-as-int +replacement-characters+)
(setf char (getf +replacement-characters+ char-as-int))
(push-token self `(:type :parse-error
:data :illegal-codepoint-for-numeric-entity
:datavars '(:char-as-int ,char-as-int))))
((or (<= #xD800 char-as-int #xDFFF)
(> char-as-int #x10FFFF))
(setf char #\uFFFD)
(push-token self `(:type :parse-error
:data :illegal-codepoint-for-numeric-entity
:datavars '(:char-as-int ,char-as-int))))
(t
;; Python comment: Should speed up this check somehow (e.g. move the set to a constant)
(when (or (<= #x0001 char-as-int #x0008)
(<= #x000E char-as-int #x001F)
(<= #x007F char-as-int #x009F)
(<= #xFDD0 char-as-int #xFDEF)
(find char-as-int
#(#x000B #xFFFE #xFFFF #x1FFFE
#x1FFFF #x2FFFE #x2FFFF #x3FFFE
#x3FFFF #x4FFFE #x4FFFF #x5FFFE
#x5FFFF #x6FFFE #x6FFFF #x7FFFE
#x7FFFF #x8FFFE #x8FFFF #x9FFFE
#x9FFFF #xAFFFE #xAFFFF #xBFFFE
#xBFFFF #xCFFFE #xCFFFF #xDFFFE
#xDFFFF #xEFFFE #xEFFFF #xFFFFE
#xFFFFF #x10FFFE #x10FFFF)))
(push-token self `(:type :parse-error
:data :illegal-codepoint-for-numeric-entity
:datavars '(:char-as-int ,char-as-int))))
;; Assume char-code-limit >= 1114112
(setf char (code-char char-as-int))))
;; Discard the ; if present. Otherwise, put it back on the queue and
;; invoke parseError on parser.
(unless (eql c #\;)
(push-token self `(:type :parse-error :data :numeric-entity-without-semicolon))
(html5-stream-unget stream c))
(string char))))
(defun consume-entity (self &key allowed-char from-attribute)
(with-slots (stream current-token) self
(let ((output "&")
(stack (list (html5-stream-char stream))))
(cond ((or (find (car stack) +space-characters+)
(find (car stack) '(+eof+ #\< #\&))
(and allowed-char (eql allowed-char (car stack))))
(html5-stream-unget stream (car stack)))
((eql (car stack) #\#)
(push (html5-stream-char stream) stack)
(let ((is-hex (find (car stack) "xX")))
(when is-hex
(push (html5-stream-char stream) stack))
(cond ((find (car stack) (if is-hex +hex-digits+ +digits+))
(html5-stream-unget stream (car stack))
(setf output (consume-number-entity self is-hex)))
(t
(push-token self '(:type :parse-error :data :expected-numeric-entity))
(html5-stream-unget stream (pop stack))
(when is-hex
(html5-stream-unget stream (pop stack)))
(html5-stream-unget stream (pop stack))))))
(t
;; Consume the maximum number of characters possible, with the
;; consumed characters matching one of the identifiers in the first
;; column of the named character references table
;; (in a case-sensitive manner).
(let ((entity)
(match-at 0))
(loop with node = *entities-tree*
for char = (car stack) then (car (push (html5-stream-char stream)
stack))
for next-node = (assoc char node)
while next-node
do (when (second next-node)
(setf entity (second next-node))
(setf match-at (length stack)))
do (setf node (cddr next-node)))
(let ((next-char))
;; Unconsume those characters that are not part of the match
;; This unconsumes everything if there where no match
(loop until (= (length stack) match-at) do
(setf next-char (car stack))
(html5-stream-unget stream (pop stack)))
(cond ((not entity)
;; If no match can be made, then no characters are consumed, and nothing is returned.
;; Is this always a parse error really?
(push-token self '(:type :parse-error :data :expected-named-entity)))
((and from-attribute
(not (eql #\; (car stack)))
(or (eql next-char #\=)
(find next-char +digits+)
(ascii-letter-p next-char)))
; Is this a parse error really?
(push-token self '(:type :parse-error :data :bogus))
(setf output (concatenate 'string "&" (reverse stack))))
(t
(unless (eql #\; (car stack))
(push-token self '(:type :parse-error
:data :named-entity-without-semicolon)))
(setf output entity)))))))
(cond (from-attribute
(add-to-attr-value current-token output))
(t
(push-token* self (if (find (char output 0) +space-characters+)
:space-characters
:characters)
output))))))
(defun process-entity-in-attribute (self &key allowed-char)
(consume-entity self :allowed-char allowed-char :from-attribute t))
(defun emit-current-token (self)
"This method is a generic handler for emitting the tags. It also sets
the state to :data because that's what's needed after a token has been
emitted.
"
(with-slots (current-token state lowercase-element-name) self
(let ((token current-token))
;; Add token to the queue to be yielded
(when (find (getf token :type) +tag-token-types+)
(when lowercase-element-name
(setf (getf token :name) (ascii-upper-2-lower (getf token :name))))
(when (eql (getf token :type) :end-tag)
(when (getf token :data)
(push-token self '(:type :parse-error :data :attributes-in-end-tag)))
(when (getf token :self-closing)
(push-token self '(:type :parse-error :data :self-closing-flag-on-end-tag)))))
(push-token self token)
(setf state :data-state))))
;;;
;;; Below are the various tokenizer states worked out.
;;;
(defstate :data-state (stream state)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\&)
(setf state :entity-data-state))
((eql data #\<)
(setf state :tag-open-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(push-token* self :characters #\u0000))
((eql data +eof+)
;; Tokenization ends.
(return nil))
((find data +space-characters+)
;; Directly after emitting a token you switch back to the "data
;; state". At that point spaceCharacters are important so they are
;; emitted separately.
(push-token* self :space-characters
data
(html5-stream-chars-until stream +space-characters+ t))
;; No need to update lastFourChars here, since the first space will
;; have already been appended to lastFourChars and will have broken
;; any <!-- or --> sequences
)
(t
(push-token* self :characters
data
(html5-stream-chars-until stream '(#\& #\< #\u0000)))))))
(defstate :entity-data-state (state)
(consume-entity self)
(setf state :data-state))
(defstate :rcdata-state (stream state)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\&)
(setf state :character-reference-in-rcdata))
((eql data #\<)
(setf state :rcdata-less-than-sign-state))
((eql data +eof+)
;; Tokenization ends.
(return nil))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(push-token* self :characters #\uFFFD))
((find data +space-characters+)
;; Directly after emitting a token you switch back to the "data
;; state". At that point spaceCharacters are important so they are
;; emitted separately.
(push-token* self :space-characters
data
(html5-stream-chars-until stream +space-characters+ t))
;; No need to update lastFourChars here, since the first space will
;; have already been appended to lastFourChars and will have broken
;; any <!-- or --> sequences
)
(t
(push-token* self :characters
data
(html5-stream-chars-until stream '(#\& #\<)))))))
(defstate :character-reference-in-rcdata (state)
(consume-entity self)
(setf state :rcdata-state))
(defstate :rawtext-state (stream state)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\<)
(setf state :rawtext-less-than-sign-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(push-token* self :characters #\uFFFD))
((eql data +eof+)
;; Tokenization ends.
(return nil))
(t
(push-token* self :characters
data
(html5-stream-chars-until stream '(#\< #\u0000)))))))
(defstate :script-data-state (stream state)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\<)
(setf state :script-data-less-than-sign-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(push-token* self :characters #\uFFFD))
((eql data +eof+)
;; Tokenization ends.
(return nil))
(t
(push-token* self :characters
data
(html5-stream-chars-until stream '(#\< #\u0000)))))))
(defstate :plaintext-state (stream)
(let ((data (html5-stream-char stream)))
(cond ((eql data +eof+)
;; Tokenization ends.
(return nil))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(push-token* self :characters #\uFFFD))
(t
(push-token* self :characters
data
(html5-stream-chars-until stream '(#\u0000)))))))
(defstate :tag-open-state (stream state current-token)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\!)
(setf state :markup-declaration-open-state))
((eql data #\/)
(setf state :close-tag-open-state))
((ascii-letter-p data)
(setf current-token (list :type :start-tag
:name (make-array 1 :element-type 'character
:initial-element data
:fill-pointer t
:adjustable t)
:data '()
:self-closing nil
:self-closing-acknowledged nil))
(setf state :tag-name-state))
((eql data #\>)
;; XXX In theory it could be something besides a tag name. But
;; do we really care?
(push-token self '(:type :parse-error :data :expected-tag-name-but-got-right-bracket))
(push-token* self :characters "<>")
(setf state :data-state))
((eql data #\?)
;; XXX In theory it could be something besides a tag name. But
;; do we really care?
(push-token self '(:type :parse-error :data :expected-tag-name-but-got-question-mark))
(html5-stream-unget stream data)
(setf state :bogus-comment-state))
(t
;; XXX
(push-token self '(:type :parse-error :data :expected-tag-name))
(push-token* self :characters "<")
(html5-stream-unget stream data)
(setf state :data-state)))))
(defstate :close-tag-open-state
(stream state current-token)
(let ((data (html5-stream-char stream)))
(cond ((ascii-letter-p data)
(setf current-token (list :type :end-tag
:name (make-array 1 :element-type 'character
:initial-element data
:fill-pointer t
:adjustable t)
:data '()
:self-closing nil))
(setf state :tag-name-state))
((eql data #\>)
(push-token self '(:type :parse-error :data :expected-closing-tag-but-got-right-bracket))
(setf state :data-state))
((eql data +eof+)
(push-token self '(:type :parse-error :data :expected-closing-tag-but-got-eof))
(push-token* self :characters "</")
(setf state :data-state))
(t
;; XXX data can be _'_...
(push-token self `(:type :parse-error :data :expected-closing-tag-but-got-char
:datavars (:data ,data)))
(html5-stream-unget stream data)
(setf state :bogus-comment-state))))
t)
(defstate :tag-name-state (stream state current-token)
(let ((data (html5-stream-char stream)))
(cond ((find data +space-characters+)
(setf state :before-attribute-name-state))
((eql data #\>)
(emit-current-token self))
((eql data +eof+)
(push-token self '(:type :parse-error :data :eof-in-tag-name))
(setf state :data-state))
((eql data #\/)
(setf state :self-closing-start-tag-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(vector-push-extend #\uFFFD (getf current-token :name)))
(t
(vector-push-extend data (getf current-token :name))
;; (Don't use charsUntil here, because tag names are
;; very short and it's faster to not do anything fancy)
))))
(defstate :rcdata-less-than-sign-state (stream state temporary-buffer)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\/)
(setf temporary-buffer (make-growable-string))
(setf state :rcdata-end-tag-open-state))
(t
(push-token* self :characters "<")
(html5-stream-unget stream data)
(setf state :rcdata-state)))))
(defstate :rcdata-end-tag-open-state (stream state temporary-buffer)
(let ((data (html5-stream-char stream)))
(cond ((ascii-letter-p data)
(nconcatf temporary-buffer (string data))
(setf state :rcdata-end-tag-name-state))
(t
(push-token* self :characters "</")
(html5-stream-unget stream data)
(setf state :rcdata-state)))))
(defstate :rcdata-end-tag-name-state (stream state temporary-buffer current-token)
(let ((appropriate (and current-token
(string-equal (getf current-token :name)
temporary-buffer)))
(data (html5-stream-char stream)))
(cond ((and (find data +space-characters+)
appropriate)
(setf current-token (list :type :end-tag
:name temporary-buffer
:data '()
:self-closing nil))
(setf state :before-attribute-name-state))
((and (eql data #\/)
appropriate)
(setf current-token (list :type :end-tag
:name temporary-buffer
:data '()
:self-closing nil))
(setf state :self-closing-start-tag-state))
((and (eql data #\>)
appropriate)
(setf current-token (list :type :end-tag
:name temporary-buffer
:data '()
:self-closing nil))
(emit-current-token self)
(setf state :data-state))
((ascii-letter-p data)
(nconcatf temporary-buffer data))
(t
(push-token* self :characters "</" temporary-buffer)
(html5-stream-unget stream data)
(setf state :rcdata-state)))))
(defstate :rawtext-less-than-sign-state (stream state temporary-buffer)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\/)
(setf temporary-buffer (make-growable-string))
(setf state :rawtext-end-tag-open-state))
(t
(push-token* self :characters "<")
(html5-stream-unget stream data)
(setf state :rawtext-state)))))
(defstate :rawtext-end-tag-open-state (stream state temporary-buffer)
(let ((data (html5-stream-char stream)))
(cond ((ascii-letter-p data)
(nconcatf temporary-buffer (string data))
(setf state :rawtext-end-tag-name-state))
(t
(push-token* self :characters "</")
(html5-stream-unget stream data)
(setf state :rawtext-state)))))
(defstate :rawtext-end-tag-name-state (stream state temporary-buffer current-token)
(let ((appropriate (and current-token
(string-equal (getf current-token :name)
temporary-buffer)))
(data (html5-stream-char stream)))
(cond ((and (find data +space-characters+)
appropriate)
(setf current-token (list :type :end-tag
:name temporary-buffer
:data '()
:self-closing nil))
(setf state :before-attribute-name-state))
((and (eql data #\/)
appropriate)
(setf current-token (list :type :end-tag
:name temporary-buffer
:data '()
:self-closing nil))
(setf state :self-closing-start-tag-state))
((and (eql data #\>)
appropriate)
(setf current-token (list :type :end-tag
:name temporary-buffer
:data '()
:self-closing nil))
(emit-current-token self)
(setf state :data-state))
((ascii-letter-p data)
(nconcatf temporary-buffer data))
(t
(push-token* self :characters "</" temporary-buffer)
(html5-stream-unget stream data)
(setf state :rawtext-state)))))
(defstate :script-data-less-than-sign-state (stream state temporary-buffer)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\/)
(setf temporary-buffer (make-growable-string))
(setf state :script-data-end-tag-open-state))
((eql data #\!)
(push-token* self :characters "<!")
(setf state :script-data-escape-start-state))
(t
(push-token* self :characters "<")
(html5-stream-unget stream data)
(setf state :script-data-state)))))
(defstate :script-data-end-tag-open-state (stream state temporary-buffer)
(let ((data (html5-stream-char stream)))
(cond ((ascii-letter-p data)
(nconcatf temporary-buffer data)
(setf state :script-data-end-tag-name-state))
(t
(push-token* self :characters "</")
(html5-stream-unget stream data)
(setf state :script-data-state)))))
(defstate :script-data-end-tag-name-state (stream state temporary-buffer current-token)
(let ((appropriate (and current-token
(string-equal (getf current-token :name)
temporary-buffer)))
(data (html5-stream-char stream)))
(cond ((and (find data +space-characters+)
appropriate)
(setf current-token (list :type :end-tag
:name temporary-buffer
:data '()
:self-closing nil))
(setf state :before-attribute-name-state))
((and (eql data #\/)
appropriate)
(setf current-token (list :type :end-tag
:name temporary-buffer
:data '()
:self-closing nil))
(setf state :self-closing-start-tag-state))
((and (eql data #\>)
appropriate)
(setf current-token (list :type :end-tag
:name temporary-buffer
:data '()
:self-closing nil))
(emit-current-token self)
(setf state :data-state))
((ascii-letter-p data)
(nconcatf temporary-buffer data))
(t
(push-token* self :characters "</" temporary-buffer)
(html5-stream-unget stream data)
(setf state :script-data-state)))))
(defstate :script-data-escape-start-state (stream state)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\-)
(push-token* self :characters "-")
(setf state :script-data-escape-start-dash-state))
(t
(html5-stream-unget stream data)
(setf state :script-data-state)))))
(defstate :script-data-escape-start-dash-state (stream state)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\-)
(push-token* self :characters "-")
(setf state :script-data-escaped-dash-dash-state))
(t
(html5-stream-unget stream data)
(setf state :script-data-state)))))
(defstate :script-data-escaped-state (stream state)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\-)
(push-token* self :characters "-")
(setf state :script-data-escaped-dash-state))
((eql data #\<)
(setf state :script-data-escaped-less-than-sign-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(push-token* self :characters #\uFFFD))
((eql data +eof+)
(setf state :data-state))
(t
(push-token* self :characters data (html5-stream-chars-until stream '(#\< #\- #\u0000)))))))
(defstate :script-data-escaped-dash-state (stream state)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\-)
(push-token* self :characters "-")
(setf state :script-data-escaped-dash-dash-state))
((eql data #\<)
(setf state :script-data-escaped-less-than-sign-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(push-token* self :characters #\uFFFD)
(setf state :script-data-escaped-state))
((eql data +eof+)
(setf state :data-state))
(t
(push-token* self :characters data (html5-stream-chars-until stream '(#\< #\- #\u0000)))
(setf state :script-data-escaped-state)))))
(defstate :script-data-escaped-dash-dash-state (stream state)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\-)
(push-token* self :characters "-"))
((eql data #\<)
(setf state :script-data-escaped-less-than-sign-state))
((eql data #\>)
(push-token* self :characters ">")
(setf state :script-data-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(push-token* self :characters #\uFFFD)
(setf state :script-data-escaped-state))
((eql data +eof+)
(setf state :data-state))
(t
(push-token* self :characters data (html5-stream-chars-until stream '(#\< #\- #\u0000)))
(setf state :script-data-escaped-state)))))
(defstate :script-data-escaped-less-than-sign-state (stream state temporary-buffer)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\/)
(setf temporary-buffer (make-growable-string))
(setf state :script-data-escaped-end-tag-open-state))
((ascii-letter-p data)
(push-token* self :characters "<" data)
(setf temporary-buffer (ascii-upper-2-lower (string data)))
(setf state :script-data-double-escape-start-state))
(t
(push-token* self :characters "<")
(html5-stream-unget stream data)
(setf state :script-data-escaped-state)))))
(defstate :script-data-escaped-end-tag-open-state (stream state temporary-buffer)
(let ((data (html5-stream-char stream)))
(cond ((ascii-letter-p data)
(setf temporary-buffer (string data))
(setf state :script-data-escaped-end-tag-name-state))
(t
(push-token* self :characters "</")
(html5-stream-unget stream data)
(setf state :script-data-escaped-state)))))
(defstate :script-data-escaped-end-tag-name-state (stream state temporary-buffer current-token)
(let ((appropriate (and current-token
(string-equal (getf current-token :name)
temporary-buffer)))
(data (html5-stream-char stream)))
(cond ((and (find data +space-characters+)
appropriate)
(setf current-token (list :type :end-tag
:name temporary-buffer
:data '()
:self-closing nil))
(setf state :before-attribute-name-state))
((and (eql data #\/)
appropriate)
(setf current-token (list :type :end-tag
:name temporary-buffer
:data '()
:self-closing nil))
(setf state :self-closing-start-tag-state))
((and (eql data #\>)
appropriate)
(setf current-token (list :type :end-tag
:name temporary-buffer
:data '()
:self-closing nil))
(emit-current-token self)
(setf state :data-state))
((ascii-letter-p data)
(nconcatf temporary-buffer data))
(t
(push-token* self :characters "</" temporary-buffer)
(html5-stream-unget stream data)
(setf state :script-data-escaped-state)))))
(defstate :script-data-double-escape-start-state (stream state temporary-buffer)
(let ((data (html5-stream-char stream)))
(cond ((or (find data +space-characters+)
(find data '(#\/ #\>)))
(push-token* self :characters data)
(if (string= (string-downcase temporary-buffer) "script")
(setf state :script-data-double-escaped-state)
(setf state :script-data-escaped-state)))
((ascii-letter-p data)
(push-token* self :characters data)
(nconcatf temporary-buffer (string data)))
(t
(html5-stream-unget stream data)
(setf state :script-data-escaped-state)))))
(defstate :script-data-double-escaped-state (stream state)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\-)
(push-token* self :characters "-")
(setf state :script-data-double-escaped-dash-state))
((eql data #\<)
(push-token* self :characters "<")
(setf state :script-data-double-escaped-less-than-sign-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(push-token* self :characters #\uFFFD))
((eql data +eof+)
(push-token self '(:type :parse-error :data :eof-in-script-in-script))
(setf state :data-state))
(t
(push-token* self :characters data)))))
(defstate :script-data-double-escaped-dash-state (stream state)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\-)
(push-token* self :characters "-")
(setf state :script-data-double-escaped-dash-dash-state))
((eql data #\<)
(push-token* self :characters "<")
(setf state :script-data-double-escaped-less-than-sign-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(push-token* self :characters #\uFFFD)
(setf state :script-data-double-escaped-state))
((eql data +eof+)
(push-token self '(:type :parse-error :data :eof-in-script-in-script))
(setf state :data-state))
(t
(push-token* self :characters data)
(setf state :script-data-double-escaped-state)))))
;; FIXME: Incorrectly named in Python code: scriptDataDoubleEscapedDashState (same the one above)
(defstate :script-data-double-escaped-dash-dash-state (stream state)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\-)
(push-token* self :characters "-")
(setf state :script-data-double-escaped-dash-dash-state))
((eql data #\<)
(push-token* self :characters "<")
(setf state :script-data-double-escaped-less-than-sign-state))
((eql data #\>)
(push-token* self :characters ">")
(setf state :script-data-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(push-token* self :characters #\uFFFD)
(setf state :script-data-double-escaped-state))
((eql data +eof+)
(push-token self '(:type :parse-error :data :eof-in-script-in-script))
(setf state :data-state))
(t
(push-token* self :characters data)
(setf state :script-data-double-escaped-state)))))
(defstate :script-data-double-escaped-less-than-sign-state (stream state temporary-buffer)
(let ((data (html5-stream-char stream)))
(cond ((eql data #\/)
(push-token* self :characters "/")
(setf temporary-buffer (make-growable-string))
(setf state :script-data-double-escape-end-state))
(t
(html5-stream-unget stream data)
(setf state :script-data-double-escaped-state)))))
(defstate :script-data-double-escape-end-state (stream state temporary-buffer)
(let ((data (html5-stream-char stream)))
(cond ((or (find data +space-characters+)
(find data '(#\/ #\>)))
(push-token* self :characters data)
(if (string= (string-downcase temporary-buffer) "script")
(setf state :script-data-escaped-state)
(setf state :script-data-double-escaped-state)))
((ascii-letter-p data)
(push-token* self :characters data)
(nconcatf temporary-buffer data))
(t
(html5-stream-unget stream data)
(setf state :script-data-double-escaped-state)))))
(defstate :before-attribute-name-state (stream state current-token)
(let ((data (html5-stream-char stream)))
(cond ((find data +space-characters+)
(html5-stream-chars-until stream +space-characters+ t))
((ascii-letter-p data)
(add-attribute current-token data)
(setf state :attribute-name-state))
((eql data #\>)
(emit-current-token self))
((eql data #\/)
(setf state :self-closing-start-tag-state))
((find data '(#\' #\" #\= #\<))
(push-token self '(:type :parse-error :data :invalid-character-in-attribute-name))
(add-attribute current-token data)
(setf state :attribute-name-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(add-attribute current-token #\uFFFD)
(setf state :attribute-name-state))
((eql data +eof+)
(push-token self '(:type :parse-error :data :expected-attribute-name-but-got-eof))
(setf state :data-state))
(t
(add-attribute current-token data)
(setf state :attribute-name-state)))))
(defstate :attribute-name-state (stream state current-token lowercase-attr-name)
(let ((data (html5-stream-char stream))
(leaving-this-state t)
(emit-token nil))
(cond ((eql data #\=)
(setf state :before-attribute-value-state))
((ascii-letter-p data)
(add-to-attr-name current-token data
(html5-stream-chars-until stream +ascii-letters+ t))
(setf leaving-this-state nil))
((eql data #\>)
;; XXX If we emit here the attributes are converted to a dict
;; without being checked and when the code below runs we error
;; because data is a dict not a list
(setf emit-token t))
((find data +space-characters+)
(setf state :after-attribute-name-state))
((eql data #\/)
(setf state :self-closing-start-tag-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(add-to-attr-name current-token #\uFFFD)
(setf leaving-this-state nil))
((find data '(#\' #\" #\<))
(push-token self '(:type :parse-error :data :invalid-character-in-attribute-name))
(add-to-attr-name current-token data)
(setf leaving-this-state nil))
((eql data +eof+)
(push-token self '(:type :parse-error :data :eof-in-attribute-name))
(setf state :data-state))
(t
(add-to-attr-name current-token data)
(setf leaving-this-state nil)))
(when leaving-this-state
;; Attributes are not dropped at this stage. That happens when the
;; start tag token is emitted so values can still be safely appended
;; to attributes, but we do want to report the parse error in time.
(when lowercase-attr-name
(setf (caar (last (getf current-token :data)))
(ascii-upper-2-lower (caar (last (getf current-token :data))))))
(loop for (name . value) in (butlast (getf current-token :data)) do
(when (string= (caar (last (getf current-token :data))) name)
(push-token self '(:type :parse-error :data :duplicate-attribute))
(return)))
;; XXX Fix for above XXX
(when emit-token
(emit-current-token self)))))
(defstate :after-attribute-name-state (stream state current-token)
(let ((data (html5-stream-char stream)))
(cond ((find data +space-characters+)
(html5-stream-chars-until stream +space-characters+ t))
((eql data #\=)
(setf state :before-attribute-value-state))
((eql data #\>)
(emit-current-token self))
((ascii-letter-p data)
(add-attribute current-token data)
(setf state :attribute-name-state))
((eql data #\/)
(setf state :self-closing-start-tag-state))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(add-attribute current-token #\uFFFD)
(setf state :attribute-name-state))
((find data '(#\' #\" #\<))
(push-token self '(:type :parse-error :data :invalid-character-after-attribute-name))
(add-attribute current-token data)
(setf state :attribute-name-state))
((eql data +eof+)
(push-token self '(:type :parse-error :data :expected-end-of-tag-but-got-eof))
(setf state :data-state))
(t
(add-attribute current-token data)
(setf state :attribute-name-state)))))
(defstate :before-attribute-value-state (stream state current-token)
(let ((data (html5-stream-char stream)))
(cond ((find data +space-characters+)
(html5-stream-chars-until stream +space-characters+ t))
((eql data #\")
(setf state :attribute-value-double-quoted-state))
((eql data #\&)
(setf state :attribute-value-un-quoted-state)
(html5-stream-unget stream data))
((eql data #\')
(setf state :attribute-value-single-quoted-state))
((eql data #\>)
(push-token self '(:type :parse-error :data :expected-attribute-value-but-got-right-bracket))
(emit-current-token self))
((eql data #\u0000)
(push-token self '(:type :parse-error :data :invalid-codepoint))
(add-to-attr-value current-token #\uFFFD)
(setf state :attribute-value-un-quoted-state))
((find data '(#\= #\< #\`))
(push-token self '(:type :parse-error :data :equals-in-unquoted-attribute-value))
(add-to-attr-value current-token data)
(setf state :attribute-value-un-quoted-state))
((eql data +eof+)
(push-token self '(:type :parse-error :data :expected-attribute-value-but-got-eof))
(setf state :data-state))
(t
(add-to-attr-value current-token data)