forked from thejerf/css
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.go
More file actions
731 lines (660 loc) · 17.7 KB
/
scanner.go
File metadata and controls
731 lines (660 loc) · 17.7 KB
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
// Copyright as given in CONTRIBUTORS
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package css
import (
"strings"
"unicode/utf8"
)
// --------------------------------------------------------------------
// Character classification helpers
// --------------------------------------------------------------------
func isDigitByte(c byte) bool {
return c >= '0' && c <= '9'
}
func isNmStartByte(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'
}
func isNmCharByte(c byte) bool {
return isNmStartByte(c) || isDigitByte(c) || c == '-'
}
// isUpperHex returns true for digits and uppercase A-F only.
// Used for UnicodeRange which per spec accepts only uppercase hex.
func isUpperHex(c byte) bool {
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')
}
// startsWithFold checks if s starts with prefix, case-insensitive (ASCII only).
func startsWithFold(s, prefix string) bool {
if len(s) < len(prefix) {
return false
}
for i := 0; i < len(prefix); i++ {
a, b := s[i], prefix[i]
if a != b {
if a >= 'A' && a <= 'Z' {
a += 'a' - 'A'
}
if b >= 'A' && b <= 'Z' {
b += 'a' - 'A'
}
if a != b {
return false
}
}
}
return true
}
// --------------------------------------------------------------------
// Scanner
// --------------------------------------------------------------------
// New returns a new CSS scanner for the given input.
func New(input string) *Scanner {
// Normalize newlines. Only allocate if the input contains \r.
if strings.ContainsRune(input, '\r') {
input = strings.ReplaceAll(input, "\r\n", "\n")
}
return &Scanner{
input: input,
row: 1,
col: 1,
}
}
// Scanner scans an input and emits tokens following the CSS3 specification.
type Scanner struct {
input string
pos int
row int
col int
err *Token
}
// --------------------------------------------------------------------
// Scan length helpers
//
// These return byte lengths without modifying scanner state.
// The offset parameter is relative to s.pos.
// --------------------------------------------------------------------
// scanEscapeLen returns the byte length of an escape sequence starting at
// s.input[s.pos+offset] (which should be a backslash). Returns 0 if not
// a valid escape.
func (s *Scanner) scanEscapeLen(offset int) int {
pos := s.pos + offset
if pos >= len(s.input) || s.input[pos] != '\\' {
return 0
}
pos++
if pos >= len(s.input) {
return 0 // lone backslash
}
c := s.input[pos]
if isHexChar(c) {
// Hex escape: 1-6 hex digits, optional single trailing whitespace.
pos++
for i := 1; i < 6 && pos < len(s.input) && isHexChar(s.input[pos]); i++ {
pos++
}
if pos < len(s.input) && isWhitespace(s.input[pos]) {
pos++
}
return pos - (s.pos + offset)
}
// Literal escape: any char in U+0020..U+007E or nonascii.
if c >= 0x80 {
_, w := utf8.DecodeRuneInString(s.input[pos:])
return 1 + w
}
if c >= 0x20 && c <= 0x7e {
return 2
}
return 0
}
// scanNameLen returns the byte length of consecutive nmchar characters
// starting at s.input[s.pos+offset]. nmchar = [a-zA-Z0-9_-] | nonascii | escape.
func (s *Scanner) scanNameLen(offset int) int {
pos := s.pos + offset
start := pos
for pos < len(s.input) {
c := s.input[pos]
if isNmCharByte(c) {
pos++
} else if c >= 0x80 {
_, w := utf8.DecodeRuneInString(s.input[pos:])
pos += w
} else if c == '\\' {
n := s.scanEscapeLen(pos - s.pos)
if n == 0 {
break
}
pos += n
} else {
break
}
}
return pos - start
}
// scanIdentLen returns the byte length of an identifier starting at
// s.input[s.pos+offset]. Supports CSS3 custom properties (--name).
// Returns 0 if no valid ident found.
func (s *Scanner) scanIdentLen(offset int) int {
pos := s.pos + offset
start := pos
if pos >= len(s.input) {
return 0
}
// Case 1: --{nmchar}+ (custom properties, requires at least one nmchar
// so that "-->" is not consumed as ident).
if pos+1 < len(s.input) && s.input[pos] == '-' && s.input[pos+1] == '-' {
pos += 2
n := s.scanNameLen(pos - s.pos)
if n == 0 {
return 0
}
return 2 + n
}
// Case 2: -?{nmstart}{nmchar}*
if s.input[pos] == '-' {
pos++
if pos >= len(s.input) {
return 0
}
}
c := s.input[pos]
if isNmStartByte(c) {
pos++
} else if c >= 0x80 {
_, w := utf8.DecodeRuneInString(s.input[pos:])
pos += w
} else if c == '\\' {
n := s.scanEscapeLen(pos - s.pos)
if n == 0 {
return 0
}
pos += n
} else {
return 0
}
pos += s.scanNameLen(pos - s.pos)
return pos - start
}
// scanNumLen returns the byte length of a number (with optional sign)
// starting at s.input[s.pos+offset]. Returns 0 if no valid number found.
func (s *Scanner) scanNumLen(offset int) int {
pos := s.pos + offset
start := pos
if pos >= len(s.input) {
return 0
}
// Optional sign
if s.input[pos] == '+' || s.input[pos] == '-' {
pos++
}
// Integer part
hasDigits := false
for pos < len(s.input) && isDigitByte(s.input[pos]) {
pos++
hasDigits = true
}
// Decimal part
if pos < len(s.input) && s.input[pos] == '.' {
if pos+1 < len(s.input) && isDigitByte(s.input[pos+1]) {
pos++ // consume dot
for pos < len(s.input) && isDigitByte(s.input[pos]) {
pos++
}
return pos - start
}
if !hasDigits {
return 0
}
return pos - start
}
if !hasDigits {
return 0
}
return pos - start
}
// scanStringLen returns the byte length of a quoted string (including quotes)
// starting at s.input[s.pos+offset], and whether the scan was successful.
func (s *Scanner) scanStringLen(offset int) (int, bool) {
pos := s.pos + offset
if pos >= len(s.input) {
return 0, false
}
quote := s.input[pos]
if quote != '"' && quote != '\'' {
return 0, false
}
pos++
for pos < len(s.input) {
c := s.input[pos]
if c == quote {
return pos + 1 - (s.pos + offset), true
}
if c == '\\' {
pos++
if pos >= len(s.input) {
return 0, false
}
nc := s.input[pos]
if isHexChar(nc) {
// Hex escape: up to 6 hex digits + optional whitespace.
pos++
for i := 1; i < 6 && pos < len(s.input) && isHexChar(s.input[pos]); i++ {
pos++
}
if pos < len(s.input) && isWhitespace(s.input[pos]) {
pos++
}
} else if nc == '\n' || nc == '\f' {
pos++
} else if nc == '\r' {
pos++
if pos < len(s.input) && s.input[pos] == '\n' {
pos++
}
} else if nc >= 0x80 {
_, w := utf8.DecodeRuneInString(s.input[pos:])
pos += w
} else {
pos++
}
continue
}
if c == '\n' || c == '\r' || c == '\f' {
return 0, false // unescaped newline terminates string (error)
}
if c >= 0x80 {
_, w := utf8.DecodeRuneInString(s.input[pos:])
pos += w
} else {
pos++
}
}
return 0, false // unclosed string
}
// scanCommentLen returns the byte length of a /* ... */ comment starting at
// s.pos, and whether the scan was successful.
func (s *Scanner) scanCommentLen() (int, bool) {
pos := s.pos
if pos+1 >= len(s.input) || s.input[pos] != '/' || s.input[pos+1] != '*' {
return 0, false
}
pos += 2
for pos+1 < len(s.input) {
if s.input[pos] == '*' && s.input[pos+1] == '/' {
return pos + 2 - s.pos, true
}
pos++
}
return 0, false // unclosed comment
}
// scanWhitespaceLen returns the byte length of consecutive whitespace
// starting at s.input[s.pos+offset].
func (s *Scanner) scanWhitespaceLen(offset int) int {
pos := s.pos + offset
start := pos
for pos < len(s.input) && isWhitespace(s.input[pos]) {
pos++
}
return pos - start
}
// scanUnicodeRangeLen returns the byte length of a unicode range token
// starting at s.pos. Format: U+hex{1,6}(-hex{1,6})? or U+[hex?]{1,6}.
// Uses uppercase hex only, matching CSS spec. Returns 0 if invalid.
func (s *Scanner) scanUnicodeRangeLen() int {
pos := s.pos
if pos+2 >= len(s.input) {
return 0
}
if (s.input[pos] != 'U' && s.input[pos] != 'u') || s.input[pos+1] != '+' {
return 0
}
pos += 2
if pos >= len(s.input) || (!isUpperHex(s.input[pos]) && s.input[pos] != '?') {
return 0
}
// Consume hex digits and ? marks (up to 6 total).
count := 0
hasQuestion := false
for count < 6 && pos < len(s.input) {
c := s.input[pos]
if isUpperHex(c) && !hasQuestion {
pos++
count++
} else if c == '?' {
pos++
count++
hasQuestion = true
} else {
break
}
}
// If we had question marks, no range suffix allowed.
if hasQuestion {
return pos - s.pos
}
// Optional range: -hex{1,6}
if pos < len(s.input) && s.input[pos] == '-' {
rangeStart := pos
pos++
rangeCount := 0
for rangeCount < 6 && pos < len(s.input) && isUpperHex(s.input[pos]) {
pos++
rangeCount++
}
if rangeCount == 0 {
pos = rangeStart // no hex digits after -, back up
}
}
return pos - s.pos
}
// scanFuncBodyLen scans the body of a url(), local(), format(), or tech()
// function. prefixLen is the byte length of the keyword+( prefix (relative
// to s.pos). Returns total byte length and success.
func (s *Scanner) scanFuncBodyLen(prefixLen int) (int, bool) {
pos := s.pos + prefixLen
// Skip leading whitespace.
for pos < len(s.input) && isWhitespace(s.input[pos]) {
pos++
}
if pos >= len(s.input) {
return 0, false
}
// Try quoted string first.
stringMatched := false
if s.input[pos] == '"' || s.input[pos] == '\'' {
n, ok := s.scanStringLen(pos - s.pos)
if ok {
pos += n
stringMatched = true
}
}
if !stringMatched {
// Scan unquoted urlchars. Rewind to after prefix + whitespace.
pos = s.pos + prefixLen
for pos < len(s.input) && isWhitespace(s.input[pos]) {
pos++
}
for pos < len(s.input) {
c := s.input[pos]
if c == ')' {
break
}
// Whitespace (except tab) ends urlchar content.
if c == ' ' || c == '\n' || c == '\r' || c == '\f' {
break
}
// ASCII urlchar check: tab, !, #..~ (excludes " 0x22 and ) 0x29
// which are caught above).
if c == '\t' || c == '!' || (c >= '#' && c <= '~') {
pos++
continue
}
// Escape sequence inside URL.
if c == '\\' {
n := s.scanEscapeLen(pos - s.pos)
if n > 0 {
pos += n
continue
}
break
}
// Non-ASCII: valid urlchar.
if c >= 0x80 {
_, w := utf8.DecodeRuneInString(s.input[pos:])
pos += w
continue
}
break
}
}
// Skip trailing whitespace.
for pos < len(s.input) && isWhitespace(s.input[pos]) {
pos++
}
// Expect closing paren.
if pos < len(s.input) && s.input[pos] == ')' {
return pos + 1 - s.pos, true
}
return 0, false
}
// --------------------------------------------------------------------
// Token production
// --------------------------------------------------------------------
// Next returns the next token from the input.
//
// At the end of the input the token type is EOF.
//
// If the input can't be tokenized the token type is Error. This occurs
// in case of unclosed quotation marks or comments.
func (s *Scanner) Next() *Token {
if s.err != nil {
return s.err
}
if s.pos >= len(s.input) {
s.err = &Token{EOF, "", s.row, s.col}
return s.err
}
if s.pos == 0 {
// Test BOM only once, at the beginning of the file.
if strings.HasPrefix(s.input, "\uFEFF") {
return s.emitSimple(BOM, "\uFEFF")
}
}
input := s.input[s.pos:]
switch input[0] {
case '\t', '\n', '\f', '\r', ' ':
n := s.scanWhitespaceLen(0)
return s.emitToken(S, input[:n])
case '"', '\'':
n, ok := s.scanStringLen(0)
if ok {
return s.emitToken(String, input[:n])
}
s.err = &Token{Error, "unclosed quotation mark", s.row, s.col}
return s.err
case '#':
n := s.scanNameLen(1)
if n > 0 {
return s.emitToken(Hash, input[:1+n])
}
return s.emitSimple(Delim, "#")
case '.':
if len(input) > 1 && isDigitByte(input[1]) {
return s.scanNumericToken()
}
return s.emitSimple(Delim, ".")
case '@':
n := s.scanIdentLen(1)
if n > 0 {
return s.emitSimple(AtKeyword, input[:1+n])
}
return s.emitSimple(Delim, "@")
case '+':
if len(input) > 1 && isDigitByte(input[1]) {
return s.scanNumericToken()
}
if len(input) > 2 && input[1] == '.' && isDigitByte(input[2]) {
return s.scanNumericToken()
}
return s.emitSimple(Delim, "+")
case '-':
// Negative number: -42, -.5
if len(input) > 1 && isDigitByte(input[1]) {
return s.scanNumericToken()
}
if len(input) > 2 && input[1] == '.' && isDigitByte(input[2]) {
return s.scanNumericToken()
}
// CDC: -->
if len(input) >= 3 && input[1] == '-' && input[2] == '>' {
return s.emitSimple(CDC, "-->")
}
// Ident or custom property: -webkit, --my-var
n := s.scanIdentLen(0)
if n > 0 {
return s.scanIdentLikeToken(n)
}
return s.emitSimple(Delim, "-")
case '/':
if len(input) > 1 && input[1] == '*' {
n, ok := s.scanCommentLen()
if ok {
return s.emitToken(Comment, input[:n])
}
s.err = &Token{Error, "unclosed comment", s.row, s.col}
return s.err
}
return s.emitSimple(Delim, "/")
case '\\':
// Start of escape → ident.
if s.scanEscapeLen(0) > 0 {
n := s.scanIdentLen(0)
if n > 0 {
return s.scanIdentLikeToken(n)
}
}
return s.emitSimple(Delim, "\\")
case '~':
return s.emitPrefixOrChar(Includes, "~=")
case '|':
return s.emitPrefixOrChar(DashMatch, "|=")
case '^':
return s.emitPrefixOrChar(PrefixMatch, "^=")
case '$':
return s.emitPrefixOrChar(SuffixMatch, "$=")
case '*':
return s.emitPrefixOrChar(SubstringMatch, "*=")
case '<':
return s.emitPrefixOrChar(CDO, "<!--")
case ':', ',', ';', '%', '&', '=', '>', '(', ')', '[', ']', '{', '}':
return s.emitSimple(Delim, string(input[0]))
}
c := input[0]
// Digit → numeric token.
if isDigitByte(c) {
return s.scanNumericToken()
}
// Unicode range: U+xxxx (uppercase hex only per spec).
if (c == 'U' || c == 'u') && len(input) > 2 && input[1] == '+' &&
(isUpperHex(input[2]) || input[2] == '?') {
n := s.scanUnicodeRangeLen()
if n > 0 {
return s.emitToken(UnicodeRange, input[:n])
}
}
// Ident-like tokens: ident, function, url(), local(), format(), tech().
if isNmStartByte(c) || c >= 0x80 {
n := s.scanIdentLen(0)
if n > 0 {
return s.scanIdentLikeToken(n)
}
}
// Fallback: single-character delimiter.
r, width := utf8.DecodeRuneInString(input)
token := &Token{Delim, string(r), s.row, s.col}
s.col += width
s.pos += width
return token
}
// scanNumericToken scans a Number, Percentage, or Dimension token.
func (s *Scanner) scanNumericToken() *Token {
input := s.input[s.pos:]
numLen := s.scanNumLen(0)
if numLen == 0 {
// Shouldn't happen if called correctly; emit as delimiter.
r, width := utf8.DecodeRuneInString(input)
token := &Token{Delim, string(r), s.row, s.col}
s.col += width
s.pos += width
return token
}
// Check for percentage.
if s.pos+numLen < len(s.input) && s.input[s.pos+numLen] == '%' {
return s.emitToken(Percentage, input[:numLen+1])
}
// Check for dimension (number followed by ident unit).
identLen := s.scanIdentLen(numLen)
if identLen > 0 {
return s.emitToken(Dimension, input[:numLen+identLen])
}
return s.emitToken(Number, input[:numLen])
}
// scanIdentLikeToken scans an Ident, Function, URI, Local, Format, or Tech
// token. identLen is the pre-computed byte length of the identifier portion.
func (s *Scanner) scanIdentLikeToken(identLen int) *Token {
input := s.input[s.pos:]
// Check if followed by '(' → function or special function.
if s.pos+identLen < len(s.input) && s.input[s.pos+identLen] == '(' {
name := input[:identLen]
prefixLen := identLen + 1 // ident + opening paren
// Special functions (case-insensitive): url(), local(), format(), tech().
if identLen == 3 && startsWithFold(name, "url") {
if n, ok := s.scanFuncBodyLen(prefixLen); ok {
return s.emitToken(URI, input[:n])
}
}
if identLen == 5 && startsWithFold(name, "local") {
if n, ok := s.scanFuncBodyLen(prefixLen); ok {
return s.emitToken(Local, input[:n])
}
}
if identLen == 6 && startsWithFold(name, "format") {
if n, ok := s.scanFuncBodyLen(prefixLen); ok {
return s.emitToken(Format, input[:n])
}
}
if identLen == 4 && startsWithFold(name, "tech") {
if n, ok := s.scanFuncBodyLen(prefixLen); ok {
return s.emitToken(Tech, input[:n])
}
}
// Generic function.
return s.emitToken(Function, input[:prefixLen])
}
return s.emitToken(Ident, input[:identLen])
}
// --------------------------------------------------------------------
// Position tracking and token emission
// --------------------------------------------------------------------
// updatePosition updates input coordinates based on the consumed text.
func (s *Scanner) updatePosition(text string) {
width := utf8.RuneCountInString(text)
lines := strings.Count(text, "\n")
s.row += lines
if lines == 0 {
s.col += width
} else {
s.col = utf8.RuneCountInString(text[strings.LastIndex(text, "\n"):])
}
s.pos += len(text) // while col is a rune index, pos is a byte index
}
// emitToken returns a Token for the string v and updates the scanner position.
func (s *Scanner) emitToken(t Type, v string) *Token {
token := &Token{t, v, s.row, s.col}
s.updatePosition(v)
token.normalize()
return token
}
// emitSimple returns a Token for the string v and updates the scanner
// position in a simplified manner.
//
// The string is known to have only ASCII characters and to not have a newline.
func (s *Scanner) emitSimple(t Type, v string) *Token {
token := &Token{t, v, s.row, s.col}
s.col += len(v)
s.pos += len(v)
token.normalize()
return token
}
// emitPrefixOrChar returns a Token for type t if the current position
// matches the given prefix. Otherwise it returns a Char token using the
// first character from the prefix.
//
// The prefix is known to have only ASCII characters and to not have a newline.
func (s *Scanner) emitPrefixOrChar(t Type, prefix string) *Token {
if strings.HasPrefix(s.input[s.pos:], prefix) {
return s.emitSimple(t, prefix)
}
return s.emitSimple(Delim, string(prefix[0]))
}