forked from microsoft/typescript-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregexpchecker.go
More file actions
1378 lines (1284 loc) · 42 KB
/
regexpchecker.go
File metadata and controls
1378 lines (1284 loc) · 42 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
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
package regexpchecker
import (
"fmt"
"maps"
"slices"
"strings"
"unicode/utf8"
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/diagnostics"
"github.com/microsoft/typescript-go/internal/scanner"
"github.com/microsoft/typescript-go/internal/stringutil"
)
// regExpFlags represents regexp flags (e.g., 'g', 'i', 'm', etc.)
type regExpFlags uint32
const (
regExpFlagsNone regExpFlags = 0
regExpFlagsGlobal regExpFlags = 1 << 0 // g
regExpFlagsIgnoreCase regExpFlags = 1 << 1 // i
regExpFlagsMultiline regExpFlags = 1 << 2 // m
regExpFlagsDotAll regExpFlags = 1 << 3 // s
regExpFlagsUnicode regExpFlags = 1 << 4 // u
regExpFlagsSticky regExpFlags = 1 << 5 // y
regExpFlagsHasIndices regExpFlags = 1 << 6 // d
regExpFlagsUnicodeSets regExpFlags = 1 << 7 // v
regExpFlagsModifiers regExpFlags = regExpFlagsIgnoreCase | regExpFlagsMultiline | regExpFlagsDotAll
regExpFlagsAnyUnicodeMode regExpFlags = regExpFlagsUnicode | regExpFlagsUnicodeSets
)
var charCodeToRegExpFlag = map[rune]regExpFlags{
'd': regExpFlagsHasIndices,
'g': regExpFlagsGlobal,
'i': regExpFlagsIgnoreCase,
'm': regExpFlagsMultiline,
's': regExpFlagsDotAll,
'u': regExpFlagsUnicode,
'v': regExpFlagsUnicodeSets,
'y': regExpFlagsSticky,
}
// regExpValidator is used to validate regular expressions
type regExpValidator struct {
text string
pos int
end int
languageVersion core.ScriptTarget
languageVariant core.LanguageVariant
onError scanner.ErrorCallback
regExpFlags regExpFlags
annexB bool
unicodeSetsMode bool
anyUnicodeMode bool
anyUnicodeModeOrNonAnnexB bool
hasNamedCapturingGroups bool
numberOfCapturingGroups int
groupSpecifiers map[string]bool
groupNameReferences []namedReference
decimalEscapes []decimalEscape
disjunctionsScopesStack []disjunctionsScope
topDisjunctionsScope disjunctionsScope
mayContainStrings bool
isCharacterComplement bool
tokenValue string
surrogateState *surrogatePairState // For non-Unicode mode: tracks partial surrogate pair
}
type disjunction struct {
groupName string // Not a named capturing group if empty
}
type disjunctionsScope struct {
disjunctions []disjunction
currentAlternativeIndex int
}
// surrogatePairState tracks when we're in the middle of emitting a surrogate pair
// in non-Unicode mode (where literal characters >= U+10000 must be split into two UTF-16 code units)
type surrogatePairState struct {
lowSurrogate rune // The low surrogate value to return next
utf8Size int // Size of the UTF-8 character to advance past
}
type namedReference struct {
pos int
end int
name string
}
type decimalEscape struct {
pos int
end int
value int
}
func Check(
node *ast.RegularExpressionLiteral,
sourceFile *ast.SourceFile,
languageVersion core.ScriptTarget,
onError scanner.ErrorCallback,
) {
text := node.Text
v := ®ExpValidator{
text: text,
languageVersion: languageVersion,
languageVariant: sourceFile.LanguageVariant,
onError: onError,
}
// Similar to the original scanRegularExpressionWorker, but since we are outside the scanner,
// we need to rescan for some information that the scanner previously calculated.
bodyEnd := strings.LastIndexByte(text, '/')
if bodyEnd <= 0 {
panic("regexpchecker: regex must have closing '/' (scanner should have validated)")
}
v.pos = bodyEnd + 1
v.end = len(text)
v.regExpFlags = v.scanFlags(regExpFlagsNone, false)
v.pos = 1
v.end = bodyEnd
v.unicodeSetsMode = v.regExpFlags®ExpFlagsUnicodeSets != 0
v.anyUnicodeMode = v.regExpFlags®ExpFlagsAnyUnicodeMode != 0
v.annexB = true
v.anyUnicodeModeOrNonAnnexB = v.anyUnicodeMode || !v.annexB
v.hasNamedCapturingGroups = v.detectNamedCapturingGroups()
v.scanDisjunction(false)
v.validateGroupReferences()
v.validateDecimalEscapes()
}
// detectNamedCapturingGroups performs a quick scan of the pattern to detect
// if it contains any named capturing groups (?<name>...). This is needed because
// the presence of named groups changes the interpretation of \k escapes:
// - Without named groups: \k is an identity escape (matches literal 'k')
// - With named groups: \k must be followed by <name> or it's a syntax error
// This matches the behavior in scanner.ts's reScanSlashToken.
func (v *regExpValidator) detectNamedCapturingGroups() bool {
inEscape := false
inCharacterClass := false
text := v.text[v.pos:v.end]
for i, ch := range text {
if inEscape {
inEscape = false
continue
}
// Only check ASCII characters for the pattern (?<
if ch >= utf8.RuneSelf {
continue
}
if ch == '\\' {
inEscape = true
} else if ch == '[' {
inCharacterClass = true
} else if ch == ']' {
inCharacterClass = false
} else if !inCharacterClass &&
ch == '(' &&
i+3 < len(text) &&
text[i+1] == '?' &&
text[i+2] == '<' &&
text[i+3] != '=' &&
text[i+3] != '!' {
// Found (?< that's not (?<= or (?<! - this is a named capturing group
return true
}
}
return false
}
func (v *regExpValidator) charAndSize() (rune, int) {
if v.pos >= v.end {
return 0, 0
}
// Simple ASCII fast path
if ch := v.text[v.pos]; ch < utf8.RuneSelf {
return rune(ch), 1
}
// Decode multi-byte UTF-8 character
r, size := utf8.DecodeRuneInString(v.text[v.pos:])
return r, size
}
func (v *regExpValidator) charAtOffset(offset int) rune {
if v.pos+offset >= v.end {
return 0
}
// Simple ASCII fast path
if ch := v.text[v.pos+offset]; ch < utf8.RuneSelf {
return rune(ch)
}
// Decode multi-byte UTF-8 character
r, _ := utf8.DecodeRuneInString(v.text[v.pos+offset:])
return r
}
func (v *regExpValidator) error(message *diagnostics.Message, start, length int, args ...any) {
v.onError(message, start, length, args...)
}
func (v *regExpValidator) checkRegularExpressionFlagAvailability(flag regExpFlags, size int) {
var availableFrom core.ScriptTarget
// TODO: Use LanguageFeatureMinimumTarget
switch flag {
case regExpFlagsHasIndices:
availableFrom = core.ScriptTargetES2022
case regExpFlagsDotAll:
availableFrom = core.ScriptTargetES2018
case regExpFlagsUnicodeSets:
availableFrom = core.ScriptTargetES2024
default:
return
}
if v.languageVersion < availableFrom {
// !!! Old compiler lowercases these names.
v.error(diagnostics.This_regular_expression_flag_is_only_available_when_targeting_0_or_later, v.pos, size, strings.ToLower(availableFrom.String()))
}
}
func (v *regExpValidator) scanDisjunction(isInGroup bool) {
v.topDisjunctionsScope = disjunctionsScope{}
for {
v.scanAlternative(isInGroup)
if v.charAtOffset(0) != '|' {
return
}
v.pos++
v.topDisjunctionsScope.currentAlternativeIndex = len(v.topDisjunctionsScope.disjunctions)
}
}
func (v *regExpValidator) scanAlternative(isInGroup bool) {
isPreviousTermQuantifiable := false
for {
start := v.pos
ch := v.charAtOffset(0)
switch ch {
case 0:
return
case '^', '$':
v.pos++
isPreviousTermQuantifiable = false
case '\\':
v.pos++
switch v.charAtOffset(0) {
case 'b', 'B':
v.pos++
isPreviousTermQuantifiable = false
default:
v.scanAtomEscape()
isPreviousTermQuantifiable = true
}
case '(':
v.pos++
var groupName string
if v.charAtOffset(0) == '?' {
v.pos++
switch v.charAtOffset(0) {
case '=', '!':
v.pos++
// In Annex B, `(?=Disjunction)` and `(?!Disjunction)` are quantifiable
isPreviousTermQuantifiable = !v.anyUnicodeModeOrNonAnnexB
case '<':
groupNameStart := v.pos
v.pos++
switch v.charAtOffset(0) {
case '=', '!':
v.pos++
isPreviousTermQuantifiable = false
default:
v.scanGroupName(false)
groupName = v.tokenValue
v.scanExpectedChar('>')
// TODO: Move to LanguageFeatureMinimumTarget.RegularExpressionNamedCapturingGroups
if v.languageVersion < core.ScriptTargetES2018 {
v.error(diagnostics.Named_capturing_groups_are_only_available_when_targeting_ES2018_or_later, groupNameStart, v.pos-groupNameStart)
}
v.numberOfCapturingGroups++
isPreviousTermQuantifiable = true
}
default:
start := v.pos
setFlags := v.scanPatternModifiers(regExpFlagsNone)
if v.charAtOffset(0) == '-' {
v.pos++
v.scanPatternModifiers(setFlags)
if v.pos == start+1 {
v.error(diagnostics.Subpattern_flags_must_be_present_when_there_is_a_minus_sign, start, v.pos-start)
}
}
v.scanExpectedChar(':')
isPreviousTermQuantifiable = true
}
} else {
v.numberOfCapturingGroups++
isPreviousTermQuantifiable = true
}
disjunction := disjunction{groupName}
v.topDisjunctionsScope.disjunctions = append(v.topDisjunctionsScope.disjunctions, disjunction)
oldTopDisjunctionsScope := v.topDisjunctionsScope
oldDisjunctionsScopesStack := v.disjunctionsScopesStack
v.disjunctionsScopesStack = append(v.disjunctionsScopesStack, v.topDisjunctionsScope)
v.scanDisjunction(true)
oldTopDisjunctionsScope.disjunctions = append(oldTopDisjunctionsScope.disjunctions, v.topDisjunctionsScope.disjunctions...)
v.topDisjunctionsScope = oldTopDisjunctionsScope
v.disjunctionsScopesStack = oldDisjunctionsScopesStack
v.scanExpectedChar(')')
case '{':
v.pos++
digitsStart := v.pos
v.scanDigits()
minVal := v.tokenValue
if !v.anyUnicodeModeOrNonAnnexB && minVal == "" {
isPreviousTermQuantifiable = true
break
}
if v.charAtOffset(0) == ',' {
v.pos++
v.scanDigits()
maxVal := v.tokenValue
if minVal == "" {
if maxVal != "" || v.charAtOffset(0) == '}' {
v.error(diagnostics.Incomplete_quantifier_Digit_expected, digitsStart, 0)
} else {
v.error(diagnostics.Unexpected_0_Did_you_mean_to_escape_it_with_backslash, start, 1, string(ch))
isPreviousTermQuantifiable = true
break
}
} else if maxVal != "" && (v.anyUnicodeModeOrNonAnnexB || v.charAtOffset(0) == '}') {
minInt := parseDecimalValue(minVal, 0, len(minVal))
maxInt := parseDecimalValue(maxVal, 0, len(maxVal))
if minInt > maxInt {
v.error(diagnostics.Numbers_out_of_order_in_quantifier, digitsStart, v.pos-digitsStart)
}
}
} else if minVal == "" {
if v.anyUnicodeModeOrNonAnnexB {
v.error(diagnostics.Unexpected_0_Did_you_mean_to_escape_it_with_backslash, start, 1, string(ch))
}
isPreviousTermQuantifiable = true
break
}
if v.charAtOffset(0) != '}' {
if v.anyUnicodeModeOrNonAnnexB {
v.error(diagnostics.X_0_expected, v.pos, 0, "}")
v.pos--
} else {
isPreviousTermQuantifiable = true
break
}
}
fallthrough
case '*', '+', '?':
v.pos++
if v.charAtOffset(0) == '?' {
// Non-greedy
v.pos++
}
if !isPreviousTermQuantifiable {
v.error(diagnostics.There_is_nothing_available_for_repetition, start, v.pos-start)
}
isPreviousTermQuantifiable = false
case '.':
v.pos++
isPreviousTermQuantifiable = true
case '[':
v.pos++
if v.unicodeSetsMode {
v.scanClassSetExpression()
} else {
v.scanClassRanges()
}
v.scanExpectedChar(']')
isPreviousTermQuantifiable = true
case ')':
if isInGroup {
return
}
fallthrough
case ']', '}':
if v.anyUnicodeModeOrNonAnnexB || ch == ')' {
v.error(diagnostics.Unexpected_0_Did_you_mean_to_escape_it_with_backslash, v.pos, 1, string(ch))
}
v.pos++
isPreviousTermQuantifiable = true
case '/', '|':
return
default:
v.scanSourceCharacter()
isPreviousTermQuantifiable = true
}
}
}
func (v *regExpValidator) validateGroupReferences() {
for _, ref := range v.groupNameReferences {
if !v.groupSpecifiers[ref.name] {
v.error(diagnostics.There_is_no_capturing_group_named_0_in_this_regular_expression, ref.pos, ref.end-ref.pos, ref.name)
// Provide spelling suggestions
if len(v.groupSpecifiers) > 0 {
// Convert map keys to slice
candidates := make([]string, 0, len(v.groupSpecifiers))
candidates = slices.AppendSeq(candidates, maps.Keys(v.groupSpecifiers))
suggestion := core.GetSpellingSuggestion(ref.name, candidates, core.Identity[string])
if suggestion != "" {
v.error(diagnostics.Did_you_mean_0, ref.pos, ref.end-ref.pos, suggestion)
}
}
}
}
}
func (v *regExpValidator) validateDecimalEscapes() {
for _, escape := range v.decimalEscapes {
if escape.value > v.numberOfCapturingGroups {
if v.numberOfCapturingGroups > 0 {
v.error(diagnostics.This_backreference_refers_to_a_group_that_does_not_exist_There_are_only_0_capturing_groups_in_this_regular_expression, escape.pos, escape.end-escape.pos, v.numberOfCapturingGroups)
} else {
v.error(diagnostics.This_backreference_refers_to_a_group_that_does_not_exist_There_are_no_capturing_groups_in_this_regular_expression, escape.pos, escape.end-escape.pos)
}
}
}
}
func (v *regExpValidator) scanDigits() {
start := v.pos
for v.pos < v.end && stringutil.IsDigit(v.charAtOffset(0)) {
v.pos++
}
v.tokenValue = v.text[start:v.pos]
}
func (v *regExpValidator) scanExpectedChar(expected rune) {
if v.charAtOffset(0) == expected {
v.pos++
} else {
v.error(diagnostics.X_0_expected, v.pos, 0, string(expected))
}
}
// scanFlags scans regexp flags and validates them.
// If checkModifiers is true, only allows modifier flags (i, m, s).
func (v *regExpValidator) scanFlags(currFlags regExpFlags, checkModifiers bool) regExpFlags {
for {
ch, size := v.charAndSize()
if ch == 0 || !scanner.IsIdentifierPart(ch) {
break
}
flag, ok := charCodeToRegExpFlag[ch]
if !ok {
v.error(diagnostics.Unknown_regular_expression_flag, v.pos, size)
} else if currFlags&flag != 0 {
v.error(diagnostics.Duplicate_regular_expression_flag, v.pos, size)
} else if !checkModifiers && (currFlags|flag)®ExpFlagsAnyUnicodeMode == regExpFlagsAnyUnicodeMode {
v.error(diagnostics.The_Unicode_u_flag_and_the_Unicode_Sets_v_flag_cannot_be_set_simultaneously, v.pos, size)
} else if checkModifiers && flag®ExpFlagsModifiers == 0 {
v.error(diagnostics.This_regular_expression_flag_cannot_be_toggled_within_a_subpattern, v.pos, size)
} else {
currFlags |= flag
v.checkRegularExpressionFlagAvailability(flag, size)
}
v.pos += size
}
return currFlags
}
func (v *regExpValidator) scanPatternModifiers(currFlags regExpFlags) regExpFlags {
return v.scanFlags(currFlags, true)
}
func (v *regExpValidator) scanAtomEscape() {
switch v.charAtOffset(0) {
case 'k':
v.pos++
if v.charAtOffset(0) == '<' {
v.pos++
v.scanGroupName(true)
v.scanExpectedChar('>')
} else if v.anyUnicodeModeOrNonAnnexB || v.hasNamedCapturingGroups {
v.error(diagnostics.X_k_must_be_followed_by_a_capturing_group_name_enclosed_in_angle_brackets, v.pos-2, 2)
}
case 'q':
if v.unicodeSetsMode {
v.pos++
v.error(diagnostics.X_q_is_only_available_inside_character_class, v.pos-2, 2)
break
}
fallthrough
default:
if !v.scanCharacterClassEscape() && !v.scanDecimalEscape() {
v.scanCharacterEscape(true)
}
}
}
func (v *regExpValidator) scanDecimalEscape() bool {
ch := v.charAtOffset(0)
if ch >= '1' && ch <= '9' {
start := v.pos
v.scanDigits()
value := parseDecimalValue(v.tokenValue, 0, len(v.tokenValue))
v.decimalEscapes = append(v.decimalEscapes, decimalEscape{pos: start, end: v.pos, value: value})
return true
}
return false
}
func (v *regExpValidator) scanCharacterClassEscape() bool {
ch := v.charAtOffset(0)
isCharacterComplement := false
switch ch {
case 'd', 'D', 's', 'S', 'w', 'W':
v.pos++
return true
case 'P':
isCharacterComplement = true
fallthrough
case 'p':
v.pos++
if v.charAtOffset(0) == '{' {
v.pos++
v.scanUnicodePropertyValueExpression(isCharacterComplement)
} else {
if v.anyUnicodeModeOrNonAnnexB {
v.error(diagnostics.X_0_must_be_followed_by_a_Unicode_property_value_expression_enclosed_in_braces, v.pos-2, 2, string(ch))
} else {
v.pos--
}
}
return true
}
return false
}
func (v *regExpValidator) scanUnicodePropertyValueExpression(isCharacterComplement bool) {
// start is at the first character after '{', so start-3 points to '\' before 'p' or 'P'
start := v.pos - 3
propertyNameOrValueStart := v.pos
v.scanWordCharacters(v.charAtOffset(0))
propertyNameOrValue := v.tokenValue
if v.charAtOffset(0) == '=' {
// property=value syntax
propertyNameValid := true
if v.pos == propertyNameOrValueStart {
v.error(diagnostics.Expected_a_Unicode_property_name, propertyNameOrValueStart, 0)
propertyNameValid = false
} else if !isValidNonBinaryUnicodePropertyName(propertyNameOrValue) {
v.error(diagnostics.Unknown_Unicode_property_name, propertyNameOrValueStart, v.pos-propertyNameOrValueStart)
// Provide spelling suggestion
candidates := make([]string, 0, len(nonBinaryUnicodePropertyNames))
candidates = slices.AppendSeq(candidates, maps.Keys(nonBinaryUnicodePropertyNames))
suggestion := core.GetSpellingSuggestion(propertyNameOrValue, candidates, core.Identity[string])
if suggestion != "" {
v.error(diagnostics.Did_you_mean_0, propertyNameOrValueStart, v.pos-propertyNameOrValueStart, suggestion)
}
propertyNameValid = false
}
v.pos++
propertyValueStart := v.pos
v.scanWordCharacters(v.charAtOffset(0))
propertyValue := v.tokenValue
if v.pos == propertyValueStart {
v.error(diagnostics.Expected_a_Unicode_property_value, propertyValueStart, 0)
} else if propertyNameValid && !isValidUnicodeProperty(propertyNameOrValue, propertyValue) {
v.error(diagnostics.Unknown_Unicode_property_value, propertyValueStart, v.pos-propertyValueStart)
// Provide spelling suggestion based on the property name
canonicalName := nonBinaryUnicodePropertyNames[propertyNameOrValue]
var candidates []string
switch canonicalName {
case "General_Category":
candidates = generalCategoryValues.KeysSlice()
case "Script", "Script_Extensions":
candidates = scriptValues.KeysSlice()
}
if len(candidates) > 0 {
suggestion := core.GetSpellingSuggestion(propertyValue, candidates, core.Identity[string])
if suggestion != "" {
v.error(diagnostics.Did_you_mean_0, propertyValueStart, v.pos-propertyValueStart, suggestion)
}
}
}
} else {
// property name alone
if v.pos == propertyNameOrValueStart {
v.error(diagnostics.Expected_a_Unicode_property_name_or_value, propertyNameOrValueStart, 0)
} else if binaryUnicodePropertiesOfStrings.Has(propertyNameOrValue) {
// Properties that match more than one character (strings)
if !v.unicodeSetsMode {
v.error(diagnostics.Any_Unicode_property_that_would_possibly_match_more_than_a_single_character_is_only_available_when_the_Unicode_Sets_v_flag_is_set, propertyNameOrValueStart, v.pos-propertyNameOrValueStart)
} else if isCharacterComplement {
v.error(diagnostics.Anything_that_would_possibly_match_more_than_a_single_character_is_invalid_inside_a_negated_character_class, propertyNameOrValueStart, v.pos-propertyNameOrValueStart)
} else {
v.mayContainStrings = true
}
} else if !isValidUnicodePropertyName(propertyNameOrValue) {
v.error(diagnostics.Unknown_Unicode_property_name_or_value, propertyNameOrValueStart, v.pos-propertyNameOrValueStart)
// Provide spelling suggestion from general category values, binary properties, and binary properties of strings
candidates := make([]string, 0, generalCategoryValues.Len()+binaryUnicodeProperties.Len()+binaryUnicodePropertiesOfStrings.Len())
candidates = slices.AppendSeq(candidates, maps.Keys(generalCategoryValues.M))
candidates = slices.AppendSeq(candidates, maps.Keys(binaryUnicodeProperties.M))
candidates = slices.AppendSeq(candidates, maps.Keys(binaryUnicodePropertiesOfStrings.M))
suggestion := core.GetSpellingSuggestion(propertyNameOrValue, candidates, core.Identity)
if suggestion != "" {
v.error(diagnostics.Did_you_mean_0, propertyNameOrValueStart, v.pos-propertyNameOrValueStart, suggestion)
}
}
}
// Scan the expected closing brace
v.scanExpectedChar('}')
// Report the "only available when unicode mode" error AFTER validation
if !v.anyUnicodeMode {
v.error(diagnostics.Unicode_property_value_expressions_are_only_available_when_the_Unicode_u_flag_or_the_Unicode_Sets_v_flag_is_set, start, v.pos-start)
}
}
func (v *regExpValidator) scanWordCharacters(ch rune) {
start := v.pos
if ch != 0 && scanner.IsWordCharacter(ch) {
v.pos++
for v.pos < v.end {
ch = v.charAtOffset(0)
if scanner.IsWordCharacter(ch) {
v.pos++
} else {
break
}
}
}
v.tokenValue = v.text[start:v.pos]
}
func (v *regExpValidator) scanIdentifier(ch rune) {
start := v.pos
if ch != 0 && scanner.IsIdentifierStart(ch) {
v.pos++
for v.pos < v.end {
ch = v.charAtOffset(0)
if scanner.IsIdentifierPart(ch) {
v.pos++
} else {
break
}
}
}
v.tokenValue = v.text[start:v.pos]
}
func (v *regExpValidator) scanCharacterEscape(atomEscape bool) string {
ch := v.charAtOffset(0)
switch ch {
case 0:
v.error(diagnostics.Undetermined_character_escape, v.pos-1, 1)
return "\\"
case 'c':
v.pos++
ch = v.charAtOffset(0)
if stringutil.IsASCIILetter(ch) {
v.pos++
return string(ch & 0x1f)
}
if v.anyUnicodeModeOrNonAnnexB {
v.error(diagnostics.X_c_must_be_followed_by_an_ASCII_letter, v.pos-2, 2)
} else {
v.pos--
return "\\"
}
return string(ch)
case '^', '$', '/', '\\', '.', '*', '+', '?', '(', ')', '[', ']', '{', '}', '|':
v.pos++
return string(ch)
default:
return v.scanEscapeSequence(atomEscape)
}
}
func (v *regExpValidator) scanEscapeSequence(atomEscape bool) string {
// start points to the backslash (before the escape character)
start := v.pos - 1
ch := v.charAtOffset(0)
if ch == 0 {
v.error(diagnostics.Unexpected_end_of_text, start, 1)
return ""
}
v.pos++
switch ch {
case '0':
// '\0' - null character, but check if followed by digit
if v.pos >= v.end || !stringutil.IsDigit(v.charAtOffset(0)) {
return "\x00"
}
// This is an octal escape starting with \0
// falls through to handle as octal
if stringutil.IsOctalDigit(v.charAtOffset(0)) {
v.pos++
}
fallthrough
case '1', '2', '3':
// Can be up to 3 octal digits
if v.pos < v.end && stringutil.IsOctalDigit(v.charAtOffset(0)) {
v.pos++
}
fallthrough
case '4', '5', '6', '7':
// Can be 1 or 2 octal digits (already consumed one above for 1-3)
if v.pos < v.end && stringutil.IsOctalDigit(v.charAtOffset(0)) {
v.pos++
}
// Always report errors for octal escapes in regexp mode
code := parseOctalValue(v.text, start+1, v.pos)
hexCode := fmt.Sprintf("\\x%02x", code)
if !atomEscape && ch != '0' {
v.error(diagnostics.Octal_escape_sequences_and_backreferences_are_not_allowed_in_a_character_class_If_this_was_intended_as_an_escape_sequence_use_the_syntax_0_instead, start, v.pos-start, hexCode)
} else {
v.error(diagnostics.Octal_escape_sequences_are_not_allowed_Use_the_syntax_0, start, v.pos-start, hexCode)
}
return string(ch)
case '8', '9':
// Invalid decimal escapes - always report in regexp mode
if !atomEscape {
v.error(diagnostics.Decimal_escape_sequences_and_backreferences_are_not_allowed_in_a_character_class, start, v.pos-start)
} else {
v.error(diagnostics.Escape_sequence_0_is_not_allowed, start, v.pos-start, v.text[start:v.pos])
}
return string(ch)
case 'b':
return "\b"
case 't':
return "\t"
case 'n':
return "\n"
case 'v':
return "\v"
case 'f':
return "\f"
case 'r':
return "\r"
case 'x':
// Hex escape '\xDD'
hexStart := v.pos
for range 2 {
if v.pos >= v.end || !stringutil.IsHexDigit(v.charAtOffset(0)) {
v.error(diagnostics.Hexadecimal_digit_expected, v.pos, 0)
return v.text[start:v.pos]
}
v.pos++
}
code := parseHexValue(v.text, hexStart, v.pos)
return string(rune(code))
case 'u':
// Unicode escape '\uDDDD' or '\u{DDDDDD}'
if v.charAtOffset(0) == '{' {
// Extended unicode escape \u{DDDDDD}
v.pos++
hexStart := v.pos
hasDigits := false
for v.pos < v.end && stringutil.IsHexDigit(v.charAtOffset(0)) {
hasDigits = true
v.pos++
}
if !hasDigits {
v.error(diagnostics.Hexadecimal_digit_expected, hexStart, 0)
return v.text[start:v.pos]
}
if v.charAtOffset(0) == '}' {
v.pos++
} else {
v.error(diagnostics.Unterminated_Unicode_escape_sequence, v.pos, 0)
return v.text[start:v.pos]
}
// Parse hex value (-1 to skip closing brace)
code := parseHexValue(v.text, hexStart, v.pos-1)
// Validate the code point is within valid Unicode range
if code > 0x10FFFF {
v.error(diagnostics.An_extended_Unicode_escape_value_must_be_between_0x0_and_0x10FFFF_inclusive, hexStart, v.pos-1-hexStart)
}
if !v.anyUnicodeMode {
v.error(diagnostics.Unicode_escape_sequences_are_only_available_when_the_Unicode_u_flag_or_the_Unicode_Sets_v_flag_is_set, start, v.pos-start)
}
return string(rune(code))
} else {
// Standard unicode escape '\uDDDD'
hexStart := v.pos
for range 4 {
if v.pos >= v.end || !stringutil.IsHexDigit(v.charAtOffset(0)) {
v.error(diagnostics.Hexadecimal_digit_expected, v.pos, 0)
return v.text[start:v.pos]
}
v.pos++
}
code := parseHexValue(v.text, hexStart, v.pos)
// For surrogates, we need to preserve the actual value since string(rune(surrogate))
// converts to 0xFFFD. We encode the surrogate as UTF-16BE bytes.
var escapedValueString string
if isSurrogate(rune(code)) {
// Surrogate - encode as 2-byte sequence (UTF-16BE)
escapedValueString = encodeSurrogate(rune(code))
} else {
escapedValueString = string(rune(code))
}
// In Unicode mode, check for surrogate pairs
if v.anyUnicodeMode && isHighSurrogate(rune(code)) &&
v.pos+6 <= v.end && v.charAtOffset(0) == '\\' && v.charAtOffset(1) == 'u' {
// High surrogate followed by potential low surrogate
nextStart := v.pos
nextPos := v.pos + 2
validNext := true
for range 4 {
if nextPos >= v.end || !stringutil.IsHexDigit(rune(v.text[nextPos])) {
validNext = false
break
}
nextPos++
}
if validNext {
// Parse the next escape
nextCode := parseHexValue(v.text, nextStart+2, nextPos)
// Check if it's a low surrogate
if isLowSurrogate(rune(nextCode)) {
// Combine surrogates into a single code point
combinedCodePoint := combineSurrogatePair(rune(code), rune(nextCode))
v.pos = nextPos
return string(combinedCodePoint)
}
}
}
return escapedValueString
}
default:
// Identity escape or invalid escape
// Report error if:
// - In any Unicode mode, OR
// - In regexp mode, not Annex B, and ch is an identifier part
if v.anyUnicodeMode || (v.anyUnicodeModeOrNonAnnexB && scanner.IsIdentifierPart(ch)) {
v.error(diagnostics.This_character_cannot_be_escaped_in_a_regular_expression, start, v.pos-start)
}
return string(ch)
}
}
// parses octal digits from text and returns the integer value
func parseOctalValue(text string, start, end int) int {
code := 0
for i := start; i < end; i++ {
code = code*8 + int(text[i]-'0')
}
return code
}
// parses decimal digits from text and returns the integer value
func parseDecimalValue(text string, start, end int) int {
code := 0
for i := start; i < end; i++ {
code = code*10 + int(text[i]-'0')
}
return code
}
// parseHexValue parses hexadecimal digits from text and returns the integer value
func parseHexValue(text string, start, end int) int {
code := 0
for i := start; i < end; i++ {
digit := text[i]
if digit >= '0' && digit <= '9' {
code = code*16 + int(digit-'0')
} else if digit >= 'a' && digit <= 'f' {
code = code*16 + int(digit-'a'+10)
} else if digit >= 'A' && digit <= 'F' {
code = code*16 + int(digit-'A'+10)
}
}
return code
}
func (v *regExpValidator) scanGroupName(isReference bool) {
tokenStart := v.pos
v.scanIdentifier(v.charAtOffset(0))
if v.pos == tokenStart {
v.error(diagnostics.Expected_a_capturing_group_name, v.pos, 0)
}
if isReference {
v.groupNameReferences = append(v.groupNameReferences, namedReference{pos: tokenStart, end: v.pos, name: v.tokenValue})
} else {
if v.tokenValue != "" {
// Check for duplicate names in scope
outer:
for _, scope := range append(v.disjunctionsScopesStack, v.topDisjunctionsScope) {
for i := scope.currentAlternativeIndex; i < len(scope.disjunctions); i++ {
if scope.disjunctions[i].groupName == v.tokenValue {
v.error(diagnostics.Named_capturing_groups_with_the_same_name_must_be_mutually_exclusive_to_each_other, tokenStart, v.pos-tokenStart)
break outer
}
}
}
}
if v.groupSpecifiers == nil {
v.groupSpecifiers = make(map[string]bool)
}
v.groupSpecifiers[v.tokenValue] = true
}
}
// scanSourceCharacter scans and returns a single "character" from the source.
// In Unicode mode (u or v flags), returns complete Unicode code points.
// In non-Unicode mode, mimics JavaScript's UTF-16 behavior where literal characters
// >= U+10000 are treated as surrogate pairs and consumed across two sequential calls.
func (v *regExpValidator) scanSourceCharacter() string {
// Check if we have a pending low surrogate from the previous call
if v.surrogateState != nil {
low := v.surrogateState.lowSurrogate
size := v.surrogateState.utf8Size
v.surrogateState = nil
v.pos += size
// Return the low surrogate encoded as UTF-16BE
return encodeSurrogate(low)
}
// Decode the next UTF-8 character from the source
r, s := v.charAndSize()
if v.anyUnicodeMode || r < supplementaryMin {
// In Unicode mode, or for BMP characters, consume and return normally
v.pos += s
return v.text[v.pos-s : v.pos]
}
// In non-Unicode mode with a supplementary character (>= U+10000):
// JavaScript represents these as surrogate pairs (two UTF-16 code units).
// Return the high surrogate now and save the low surrogate for the next call.
high, low := splitToSurrogatePair(r)
v.surrogateState = &surrogatePairState{
lowSurrogate: low,
utf8Size: s,
}
return encodeSurrogate(high)
}
// ClassRanges ::= ClassAtom ('-' ClassAtom)?
// Scans character class content like [a-z] or [^0-9].
// Follows ECMAScript regexp grammar
func (v *regExpValidator) scanClassRanges() {
isNegated := v.charAtOffset(0) == '^'
if isNegated {
v.pos++
}
oldIsCharacterComplement := v.isCharacterComplement
v.isCharacterComplement = isNegated
defer func() {
v.isCharacterComplement = oldIsCharacterComplement
}()
for {
ch := v.charAtOffset(0)
if v.isClassContentExit(ch) {
return
}
atomStart := v.pos
atom := v.scanClassAtom()
if v.charAtOffset(0) == '-' {
v.pos++
if v.isClassContentExit(v.charAtOffset(0)) {
return
}
// Check if min side of range is a character class escape
if atom == "" && v.anyUnicodeModeOrNonAnnexB {
v.error(diagnostics.A_character_class_range_must_not_be_bounded_by_another_character_class, atomStart, v.pos-1-atomStart)
}
rangeEndStart := v.pos
rangeEnd := v.scanClassAtom()
// Check if max side of range is a character class escape
if rangeEnd == "" && v.anyUnicodeModeOrNonAnnexB {
v.error(diagnostics.A_character_class_range_must_not_be_bounded_by_another_character_class, rangeEndStart, v.pos-rangeEndStart)
}
// Check range order
if atom != "" && rangeEnd != "" {
minCodePoint := decodeCodePoint(atom)
maxCodePoint := decodeCodePoint(rangeEnd)