Debian Codegraph @

parsing/kdl/internal/tokenizer.go

raw symbols SCIP
1 package internal
2
3 import (
4 "fmt"
5 "strings"
6 "unicode"
7 "unicode/utf8"
8 )
9
10 // Version represents the KDL specification version.
11 type Version int
12
13 const (
14 VersionUnknown Version = 0
15 Version1 Version = 1
16 Version2 Version = 2
17 )
18
19 // TokenType represents the type of a KDL token.
20 type TokenType int
21
22 const (
23 TokenEOF TokenType = iota
24 TokenNewline
25 TokenIdentifier
26 TokenQuotedString
27 TokenRawString
28 TokenMultiLineString
29 TokenInteger
30 TokenFloat
31 TokenHexInt
32 TokenOctalInt
33 TokenBinaryInt
34 TokenTrue
35 TokenFalse
36 TokenNull
37 TokenInf
38 TokenNegInf
39 TokenNaN
40 TokenOpenBrace
41 TokenCloseBrace
42 TokenOpenParen
43 TokenCloseParen
44 TokenEquals
45 TokenSemicolon
46 TokenSlashDash
47 )
48
49 // Token represents a single KDL token.
50 type Token struct {
51 Type TokenType
52 Value string
53 Line int
54 Col int
55 }
56
57 // Tokenizer is a KDL tokenizer supporting both v1 and v2.
58 type Tokenizer struct {
59 input []rune
60 pos int
61 line int
62 col int
63 Version Version
64 peeked *Token
65 }
66
67 // NewTokenizer creates a new tokenizer for the given input.
68 func NewTokenizer(input string) *Tokenizer {
69 return &Tokenizer{
70 input: []rune(input),
71 pos: 0,
72 line: 1,
73 col: 1,
74 Version: VersionUnknown,
75 }
76 }
77
78 func (t *Tokenizer) peek() rune {
79 if t.pos >= len(t.input) {
80 return -1
81 }
82 return t.input[t.pos]
83 }
84
85 func (t *Tokenizer) peekAt(offset int) rune {
86 p := t.pos + offset
87 if p >= len(t.input) {
88 return -1
89 }
90 return t.input[p]
91 }
92
93 func (t *Tokenizer) advance() rune {
94 if t.pos >= len(t.input) {
95 return -1
96 }
97 ch := t.input[t.pos]
98 t.pos++
99 if ch == '\n' {
100 t.line++
101 t.col = 1
102 } else {
103 t.col++
104 }
105 return ch
106 }
107
108 func (t *Tokenizer) error(msg string) error {
109 return fmt.Errorf("kdl: %s at line %d, col %d", msg, t.line, t.col)
110 }
111
112 // PeekToken returns the next token without consuming it.
113 func (t *Tokenizer) PeekToken() (Token, error) {
114 if t.peeked != nil {
115 return *t.peeked, nil
116 }
117 tok, err := t.NextToken()
118 if err != nil {
119 return Token{}, err
120 }
121 t.peeked = &tok
122 return tok, nil
123 }
124
125 // NextToken returns the next token from the input.
126 func (t *Tokenizer) NextToken() (Token, error) {
127 if t.peeked != nil {
128 tok := *t.peeked
129 t.peeked = nil
130 return tok, nil
131 }
132 return t.readToken()
133 }
134
135 func (t *Tokenizer) readToken() (Token, error) {
136 t.skipWhitespaceAndComments()
137
138 if t.pos >= len(t.input) {
139 return Token{Type: TokenEOF, Line: t.line, Col: t.col}, nil
140 }
141
142 ch := t.peek()
143
144 // Newline
145 if ch == '\n' || ch == '\r' {
146 return t.readNewline()
147 }
148
149 // Semicolon (node terminator)
150 if ch == ';' {
151 tok := Token{Type: TokenSemicolon, Value: ";", Line: t.line, Col: t.col}
152 t.advance()
153 return tok, nil
154 }
155
156 // Braces
157 if ch == '{' {
158 tok := Token{Type: TokenOpenBrace, Value: "{", Line: t.line, Col: t.col}
159 t.advance()
160 return tok, nil
161 }
162 if ch == '}' {
163 tok := Token{Type: TokenCloseBrace, Value: "}", Line: t.line, Col: t.col}
164 t.advance()
165 return tok, nil
166 }
167
168 // Parens (type annotations)
169 if ch == '(' {
170 tok := Token{Type: TokenOpenParen, Value: "(", Line: t.line, Col: t.col}
171 t.advance()
172 return tok, nil
173 }
174 if ch == ')' {
175 tok := Token{Type: TokenCloseParen, Value: ")", Line: t.line, Col: t.col}
176 t.advance()
177 return tok, nil
178 }
179
180 // Equals
181 if ch == '=' {
182 tok := Token{Type: TokenEquals, Value: "=", Line: t.line, Col: t.col}
183 t.advance()
184 return tok, nil
185 }
186
187 // Slash-dash
188 if ch == '/' && t.peekAt(1) == '-' {
189 tok := Token{Type: TokenSlashDash, Value: "/-", Line: t.line, Col: t.col}
190 t.advance()
191 t.advance()
192 return tok, nil
193 }
194
195 // Line continuation (backslash followed by newline)
196 if ch == '\\' {
197 next := t.peekAt(1)
198 if next == '\n' || next == '\r' {
199 t.advance() // consume backslash
200 t.skipNewline()
201 t.skipWhitespaceAndComments()
202 return t.readToken()
203 }
204 }
205
206 // v2 keywords starting with #
207 if ch == '#' {
208 return t.readHash()
209 }
210
211 // v1 raw strings: r"..." or r#"..."#
212 if ch == 'r' && t.peekAt(1) == '"' {
213 return t.readV1RawString()
214 }
215 if ch == 'r' && t.peekAt(1) == '#' {
216 return t.readV1RawString()
217 }
218
219 // Quoted strings
220 if ch == '"' {
221 return t.readQuotedString()
222 }
223
224 // Numbers
225 if ch == '-' || ch == '+' || (ch >= '0' && ch <= '9') {
226 return t.readNumber()
227 }
228
229 // Bare identifiers
230 if isIdentStart(ch) {
231 return t.readIdentifier()
232 }
233
234 return Token{}, t.error(fmt.Sprintf("unexpected character: %c", ch))
235 }
236
237 func (t *Tokenizer) readNewline() (Token, error) {
238 tok := Token{Type: TokenNewline, Value: "\n", Line: t.line, Col: t.col}
239 t.skipNewline()
240 return tok, nil
241 }
242
243 func (t *Tokenizer) skipNewline() {
244 switch t.peek() {
245 case '\r':
246 t.advance()
247 if t.peek() == '\n' {
248 t.advance()
249 }
250 case '\n':
251 t.advance()
252 }
253 }
254
255 func (t *Tokenizer) skipWhitespaceAndComments() {
256 for t.pos < len(t.input) {
257 ch := t.peek()
258
259 // Regular whitespace (not newlines — those are tokens)
260 if ch == ' ' || ch == '\t' || ch == '\u00A0' || ch == '\uFEFF' ||
261 (ch > 0x2000 && ch <= 0x200A) || ch == 0x202F || ch == 0x205F || ch == 0x3000 {
262 t.advance()
263 continue
264 }
265
266 // Line comments
267 if ch == '/' && t.peekAt(1) == '/' {
268 t.advance()
269 t.advance()
270 for t.pos < len(t.input) && t.peek() != '\n' && t.peek() != '\r' {
271 t.advance()
272 }
273 continue
274 }
275
276 // Block comments (nestable)
277 if ch == '/' && t.peekAt(1) == '*' {
278 t.advance()
279 t.advance()
280 depth := 1
281 for t.pos < len(t.input) && depth > 0 {
282 if t.peek() == '/' && t.peekAt(1) == '*' {
283 t.advance()
284 t.advance()
285 depth++
286 } else if t.peek() == '*' && t.peekAt(1) == '/' {
287 t.advance()
288 t.advance()
289 depth--
290 } else {
291 t.advance()
292 }
293 }
294 continue
295 }
296
297 // Line continuation
298 if ch == '\\' {
299 next := t.peekAt(1)
300 if next == '\n' || next == '\r' {
301 t.advance() // consume backslash
302 t.skipNewline()
303 continue
304 }
305 }
306
307 break
308 }
309 }
310
311 // readHash handles # prefixed tokens in v2 (and auto-detects version).
312 func (t *Tokenizer) readHash() (Token, error) {
313 line, col := t.line, t.col
314
315 // Count leading hashes for raw strings
316 hashCount := 0
317 p := t.pos
318 for p < len(t.input) && t.input[p] == '#' {
319 hashCount++
320 p++
321 }
322
323 // v2 raw string: #"..."# or ##"..."##, etc.
324 if p < len(t.input) && t.input[p] == '"' {
325 t.setVersion(Version2)
326 return t.readV2RawString(hashCount)
327 }
328
329 // v2 keywords: #true, #false, #null, #inf, #-inf, #nan
330 if hashCount == 1 {
331 t.advance() // consume #
332 remaining := string(t.input[t.pos:])
333
334 keywords := []struct {
335 text string
336 tokType TokenType
337 }{
338 {"true", TokenTrue},
339 {"false", TokenFalse},
340 {"null", TokenNull},
341 {"inf", TokenInf},
342 {"nan", TokenNaN},
343 {"-inf", TokenNegInf},
344 }
345
346 for _, kw := range keywords {
347 if strings.HasPrefix(remaining, kw.text) {
348 // Make sure it's not part of a longer identifier
349 endPos := t.pos + len(kw.text)
350 if endPos >= len(t.input) || !isIdentChar(t.input[endPos]) {
351 for range kw.text {
352 t.advance()
353 }
354 t.setVersion(Version2)
355 return Token{Type: kw.tokType, Value: "#" + kw.text, Line: line, Col: col}, nil
356 }
357 }
358 }
359
360 return Token{}, t.error("unexpected # character")
361 }
362
363 return Token{}, t.error("unexpected # character")
364 }
365
366 func (t *Tokenizer) readV1RawString() (Token, error) {
367 line, col := t.line, t.col
368 t.advance() // consume 'r'
369
370 if t.peek() == '#' {
371 // r#"..."# or r##"..."## etc.
372 hashCount := 0
373 for t.peek() == '#' {
374 hashCount++
375 t.advance()
376 }
377 if t.peek() != '"' {
378 return Token{}, t.error("expected '\"' after r and hashes")
379 }
380 t.advance() // consume opening "
381
382 var sb strings.Builder
383 for {
384 if t.pos >= len(t.input) {
385 return Token{}, t.error("unterminated raw string")
386 }
387 ch := t.advance()
388 if ch == '"' {
389 // Check for matching hashes
390 matched := 0
391 for matched < hashCount && t.peek() == '#' {
392 matched++
393 t.advance()
394 }
395 if matched == hashCount {
396 t.setVersion(Version1)
397 return Token{Type: TokenRawString, Value: sb.String(), Line: line, Col: col}, nil
398 }
399 sb.WriteRune('"')
400 for i := 0; i < matched; i++ {
401 sb.WriteRune('#')
402 }
403 } else {
404 sb.WriteRune(ch)
405 }
406 }
407 }
408
409 // r"..."
410 if t.peek() != '"' {
411 // Not a raw string, it's an identifier starting with 'r'
412 t.pos--
413 t.col--
414 return t.readIdentifier()
415 }
416 t.advance() // consume opening "
417
418 var sb strings.Builder
419 for {
420 if t.pos >= len(t.input) {
421 return Token{}, t.error("unterminated raw string")
422 }
423 ch := t.advance()
424 if ch == '"' {
425 t.setVersion(Version1)
426 return Token{Type: TokenRawString, Value: sb.String(), Line: line, Col: col}, nil
427 }
428 sb.WriteRune(ch)
429 }
430 }
431
432 func (t *Tokenizer) readV2RawString(hashCount int) (Token, error) {
433 line, col := t.line, t.col
434
435 // Consume the hashes
436 for i := 0; i < hashCount; i++ {
437 t.advance()
438 }
439
440 // Check for multi-line raw string: #"""..."""#
441 if t.peek() == '"' && t.peekAt(1) == '"' && t.peekAt(2) == '"' {
442 t.advance() // first "
443 t.advance() // second "
444 t.advance() // third "
445
446 // Must be followed by newline
447 if t.peek() == '\r' || t.peek() == '\n' {
448 t.skipNewline()
449 }
450
451 return t.readMultiLineRawContent(hashCount, line, col)
452 }
453
454 // Single-line raw string: #"..."#
455 t.advance() // consume opening "
456
457 var sb strings.Builder
458 for {
459 if t.pos >= len(t.input) {
460 return Token{}, t.error("unterminated raw string")
461 }
462 ch := t.advance()
463 if ch == '"' {
464 // Check for matching closing hashes
465 matched := 0
466 for matched < hashCount && t.peek() == '#' {
467 matched++
468 t.advance()
469 }
470 if matched == hashCount {
471 return Token{Type: TokenRawString, Value: sb.String(), Line: line, Col: col}, nil
472 }
473 sb.WriteRune('"')
474 for i := 0; i < matched; i++ {
475 sb.WriteRune('#')
476 }
477 } else {
478 sb.WriteRune(ch)
479 }
480 }
481 }
482
483 func (t *Tokenizer) readMultiLineRawContent(hashCount int, line, col int) (Token, error) {
484 closing := `"""` + strings.Repeat("#", hashCount)
485
486 var sb strings.Builder
487 for {
488 if t.pos >= len(t.input) {
489 return Token{}, t.error("unterminated multi-line raw string")
490 }
491 ch := t.peek()
492 if ch == '"' {
493 // Try to match closing sequence
494 match := true
495 for i, c := range closing {
496 if t.peekAt(i) != c {
497 match = false
498 break
499 }
500 }
501 if match {
502 for range closing {
503 t.advance()
504 }
505 content := dedentMultiLineString(sb.String())
506 return Token{Type: TokenMultiLineString, Value: content, Line: line, Col: col}, nil
507 }
508 }
509 sb.WriteRune(t.advance())
510 }
511 }
512
513 func (t *Tokenizer) readQuotedString() (Token, error) {
514 line, col := t.line, t.col
515 t.advance() // consume opening "
516
517 // Check for multi-line string: """
518 if t.peek() == '"' && t.peekAt(1) == '"' {
519 t.advance() // second "
520 t.advance() // third "
521
522 // Must be followed by newline
523 if t.peek() == '\r' || t.peek() == '\n' {
524 t.skipNewline()
525 } else {
526 return Token{}, t.error("multi-line string opening must be followed by newline")
527 }
528
529 t.setVersion(Version2)
530 return t.readMultiLineString(line, col)
531 }
532
533 var sb strings.Builder
534 for {
535 if t.pos >= len(t.input) {
536 return Token{}, t.error("unterminated string")
537 }
538 ch := t.advance()
539 switch ch {
540 case '"':
541 return Token{Type: TokenQuotedString, Value: sb.String(), Line: line, Col: col}, nil
542 case '\\':
543 escaped, err := t.readEscape()
544 if err != nil {
545 return Token{}, err
546 }
547 sb.WriteString(escaped)
548 case '\n', '\r':
549 return Token{}, t.error("unexpected newline in string (use multi-line string or \\n)")
550 default:
551 sb.WriteRune(ch)
552 }
553 }
554 }
555
556 func (t *Tokenizer) readMultiLineString(line, col int) (Token, error) {
557 var sb strings.Builder
558 for {
559 if t.pos >= len(t.input) {
560 return Token{}, t.error("unterminated multi-line string")
561 }
562 ch := t.peek()
563 if ch == '"' && t.peekAt(1) == '"' && t.peekAt(2) == '"' {
564 t.advance()
565 t.advance()
566 t.advance()
567 content := dedentMultiLineString(sb.String())
568 return Token{Type: TokenMultiLineString, Value: content, Line: line, Col: col}, nil
569 }
570 ch = t.advance()
571 if ch == '\\' {
572 escaped, err := t.readEscape()
573 if err != nil {
574 return Token{}, err
575 }
576 sb.WriteString(escaped)
577 } else {
578 sb.WriteRune(ch)
579 }
580 }
581 }
582
583 // dedentMultiLineString removes common leading whitespace from multi-line strings.
584 // The last line's whitespace determines the indentation to strip.
585 func dedentMultiLineString(s string) string {
586 lines := strings.Split(s, "\n")
587
588 if len(lines) == 0 {
589 return ""
590 }
591
592 // The last line (before closing """) determines the indent
593 lastLine := lines[len(lines)-1]
594 indent := ""
595 for _, ch := range lastLine {
596 if ch == ' ' || ch == '\t' {
597 indent += string(ch)
598 } else {
599 break
600 }
601 }
602
603 // Strip that indent from all lines, and remove the last line if it's only whitespace
604 var result []string
605 for i, line := range lines {
606 if i == len(lines)-1 {
607 // Last line: only include if it has content after indent
608 stripped := strings.TrimPrefix(line, indent)
609 if stripped != "" {
610 result = append(result, stripped)
611 }
612 continue
613 }
614 if line == "" {
615 result = append(result, "")
616 } else {
617 result = append(result, strings.TrimPrefix(line, indent))
618 }
619 }
620
621 return strings.Join(result, "\n")
622 }
623
624 func (t *Tokenizer) readEscape() (string, error) {
625 if t.pos >= len(t.input) {
626 return "", t.error("unterminated escape sequence")
627 }
628 ch := t.advance()
629 switch ch {
630 case 'n':
631 return "\n", nil
632 case 'r':
633 return "\r", nil
634 case 't':
635 return "\t", nil
636 case '\\':
637 return "\\", nil
638 case '"':
639 return "\"", nil
640 case 'b':
641 return "\b", nil
642 case 'f':
643 return "\f", nil
644 case 's':
645 // v2 escape for space
646 t.setVersion(Version2)
647 return " ", nil
648 case 'u':
649 return t.readUnicodeEscape()
650 default:
651 return "", t.error(fmt.Sprintf("unknown escape sequence: \\%c", ch))
652 }
653 }
654
655 func (t *Tokenizer) readUnicodeEscape() (string, error) {
656 if t.pos >= len(t.input) || t.peek() != '{' {
657 return "", t.error("expected '{' in unicode escape")
658 }
659 t.advance() // consume {
660
661 var hexStr strings.Builder
662 for t.pos < len(t.input) && t.peek() != '}' {
663 hexStr.WriteRune(t.advance())
664 }
665 if t.pos >= len(t.input) {
666 return "", t.error("unterminated unicode escape")
667 }
668 t.advance() // consume }
669
670 var codePoint int
671 _, err := fmt.Sscanf(hexStr.String(), "%x", &codePoint)
672 if err != nil {
673 return "", t.error(fmt.Sprintf("invalid unicode escape: %s", hexStr.String()))
674 }
675
676 if !utf8.ValidRune(rune(codePoint)) {
677 return "", t.error(fmt.Sprintf("invalid unicode code point: U+%04X", codePoint))
678 }
679
680 return string(rune(codePoint)), nil
681 }
682
683 func (t *Tokenizer) readNumber() (Token, error) {
684 line, col := t.line, t.col
685 var sb strings.Builder
686
687 // Optional sign
688 if t.peek() == '-' || t.peek() == '+' {
689 sb.WriteRune(t.advance())
690 }
691
692 // Check if this is actually an identifier (e.g., just "-" followed by a letter, or bare sign)
693 if sb.Len() > 0 && (t.pos >= len(t.input) || !isDigit(t.peek())) {
694 // It's a bare identifier like "-tag", "--flag", "+.", or just "+" / "-"
695 if t.pos < len(t.input) && isIdentChar(t.peek()) {
696 for t.pos < len(t.input) && isIdentChar(t.peek()) {
697 sb.WriteRune(t.advance())
698 }
699 }
700 // A sign alone or sign+ident chars is a bare identifier
701 return Token{Type: TokenIdentifier, Value: sb.String(), Line: line, Col: col}, nil
702 }
703
704 if t.pos >= len(t.input) {
705 // Bare sign at EOF is an identifier
706 if sb.Len() > 0 {
707 return Token{Type: TokenIdentifier, Value: sb.String(), Line: line, Col: col}, nil
708 }
709 return Token{}, t.error("unexpected end of input in number")
710 }
711
712 // Check for hex, octal, binary
713 if t.peek() == '0' && t.pos+1 < len(t.input) {
714 next := t.peekAt(1)
715 switch next {
716 case 'x', 'X':
717 return t.readHexNumber(sb.String(), line, col)
718 case 'o', 'O':
719 return t.readOctalNumber(sb.String(), line, col)
720 case 'b', 'B':
721 return t.readBinaryNumber(sb.String(), line, col)
722 }
723 }
724
725 // Decimal integer or float
726 isFloat := false
727 hasE := false
728
729 for t.pos < len(t.input) {
730 ch := t.peek()
731 if ch == '_' {
732 t.advance() // skip underscores in numbers
733 continue
734 }
735 if isDigit(ch) {
736 sb.WriteRune(t.advance())
737 } else if ch == '.' && !isFloat && !hasE {
738 isFloat = true
739 sb.WriteRune(t.advance())
740 } else if (ch == 'e' || ch == 'E') && !hasE {
741 isFloat = true
742 hasE = true
743 sb.WriteRune(t.advance())
744 if t.pos < len(t.input) && (t.peek() == '+' || t.peek() == '-') {
745 sb.WriteRune(t.advance())
746 }
747 } else {
748 break
749 }
750 }
751
752 if isFloat {
753 return Token{Type: TokenFloat, Value: sb.String(), Line: line, Col: col}, nil
754 }
755 return Token{Type: TokenInteger, Value: sb.String(), Line: line, Col: col}, nil
756 }
757
758 func (t *Tokenizer) readHexNumber(prefix string, line, col int) (Token, error) {
759 var sb strings.Builder
760 sb.WriteString(prefix)
761 sb.WriteRune(t.advance()) // 0
762 sb.WriteRune(t.advance()) // x
763
764 for t.pos < len(t.input) {
765 ch := t.peek()
766 if ch == '_' {
767 t.advance()
768 continue
769 }
770 if isHexDigit(ch) {
771 sb.WriteRune(t.advance())
772 } else {
773 break
774 }
775 }
776 return Token{Type: TokenHexInt, Value: sb.String(), Line: line, Col: col}, nil
777 }
778
779 func (t *Tokenizer) readOctalNumber(prefix string, line, col int) (Token, error) {
780 var sb strings.Builder
781 sb.WriteString(prefix)
782 sb.WriteRune(t.advance()) // 0
783 sb.WriteRune(t.advance()) // o
784
785 for t.pos < len(t.input) {
786 ch := t.peek()
787 if ch == '_' {
788 t.advance()
789 continue
790 }
791 if ch >= '0' && ch <= '7' {
792 sb.WriteRune(t.advance())
793 } else {
794 break
795 }
796 }
797 return Token{Type: TokenOctalInt, Value: sb.String(), Line: line, Col: col}, nil
798 }
799
800 func (t *Tokenizer) readBinaryNumber(prefix string, line, col int) (Token, error) {
801 var sb strings.Builder
802 sb.WriteString(prefix)
803 sb.WriteRune(t.advance()) // 0
804 sb.WriteRune(t.advance()) // b
805
806 for t.pos < len(t.input) {
807 ch := t.peek()
808 if ch == '_' {
809 t.advance()
810 continue
811 }
812 if ch == '0' || ch == '1' {
813 sb.WriteRune(t.advance())
814 } else {
815 break
816 }
817 }
818 return Token{Type: TokenBinaryInt, Value: sb.String(), Line: line, Col: col}, nil
819 }
820
821 func (t *Tokenizer) readIdentifier() (Token, error) {
822 line, col := t.line, t.col
823 var sb strings.Builder
824
825 for t.pos < len(t.input) && isIdentChar(t.peek()) {
826 sb.WriteRune(t.advance())
827 }
828
829 val := sb.String()
830
831 // v1 keywords
832 switch val {
833 case "true":
834 t.setVersion(Version1)
835 return Token{Type: TokenTrue, Value: val, Line: line, Col: col}, nil
836 case "false":
837 t.setVersion(Version1)
838 return Token{Type: TokenFalse, Value: val, Line: line, Col: col}, nil
839 case "null":
840 t.setVersion(Version1)
841 return Token{Type: TokenNull, Value: val, Line: line, Col: col}, nil
842 case "inf":
843 return Token{Type: TokenInf, Value: val, Line: line, Col: col}, nil
844 case "-inf":
845 return Token{Type: TokenNegInf, Value: val, Line: line, Col: col}, nil
846 case "nan":
847 return Token{Type: TokenNaN, Value: val, Line: line, Col: col}, nil
848 }
849
850 return Token{Type: TokenIdentifier, Value: val, Line: line, Col: col}, nil
851 }
852
853 func (t *Tokenizer) setVersion(v Version) {
854 if t.Version == VersionUnknown {
855 t.Version = v
856 }
857 }
858
859 // isIdentStart returns true if ch can start a bare identifier.
860 func isIdentStart(ch rune) bool {
861 if ch <= 0 {
862 return false
863 }
864 // Cannot start with a digit, sign, or various special chars
865 if isDigit(ch) {
866 return false
867 }
868 return isIdentChar(ch)
869 }
870
871 // isIdentChar returns true if ch can be part of a bare identifier.
872 func isIdentChar(ch rune) bool {
873 if ch <= 0 {
874 return false
875 }
876 // Disallowed in identifiers
877 switch ch {
878 case '\\', '/', '(', ')', '{', '}', '<', '>', ';', '[', ']', '=', ',', '"':
879 return false
880 }
881 // No whitespace
882 if unicode.IsSpace(ch) {
883 return false
884 }
885 // No control characters
886 if unicode.IsControl(ch) {
887 return false
888 }
889 return true
890 }
891
892 func isDigit(ch rune) bool {
893 return ch >= '0' && ch <= '9'
894 }
895
896 func isHexDigit(ch rune) bool {
897 return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')
898 }