-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzero_alloc_tokenizer.go
1297 lines (1101 loc) · 34.7 KB
/
zero_alloc_tokenizer.go
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 twig
import (
"fmt"
"strings"
"sync"
"unsafe"
)
const (
// Common HTML/Twig strings to pre-cache
maxCacheableLength = 64 // Only cache strings shorter than this to avoid memory bloat
// Common HTML tags
stringDiv = "div"
stringSpan = "span"
stringP = "p"
stringA = "a"
stringImg = "img"
stringHref = "href"
stringClass = "class"
stringId = "id"
stringStyle = "style"
// Common Twig syntax
stringIf = "if"
stringFor = "for"
stringEnd = "end"
stringEndif = "endif"
stringEndfor = "endfor"
stringElse = "else"
stringBlock = "block"
stringSet = "set"
stringInclude = "include"
stringExtends = "extends"
stringMacro = "macro"
// Common operators
stringEquals = "=="
stringNotEquals = "!="
stringAnd = "and"
stringOr = "or"
stringNot = "not"
stringIn = "in"
stringIs = "is"
)
// GlobalStringCache provides a centralized cache for string interning
type GlobalStringCache struct {
sync.RWMutex
strings map[string]string
}
var (
// Singleton instance of the global string cache
globalCache = newGlobalStringCache()
)
// TagType represents the type of tag found
type TagType int
const (
TAG_NONE TagType = iota
TAG_VAR
TAG_VAR_TRIM
TAG_BLOCK
TAG_BLOCK_TRIM
TAG_COMMENT
)
// TagLocation represents the location of a tag in a template
type TagLocation struct {
Type TagType // Type of tag
Position int // Position in source
Length int // Length of opening tag
}
// ZeroAllocTokenizer is an allocation-free tokenizer
// It uses a pre-allocated token buffer for all token operations
type ZeroAllocTokenizer struct {
tokenBuffer []Token // Pre-allocated buffer of tokens
source string // Source string being tokenized
position int // Current position in source
line int // Current line
result []Token // Slice of actually used tokens
tempStrings []string // String constants that we can reuse
}
// This array contains commonly used strings in tokenization to avoid allocations
var commonStrings = []string{
// Common twig words and operators
"if", "else", "elseif", "endif", "for", "endfor", "in",
"block", "endblock", "extends", "include", "with", "set",
"macro", "endmacro", "import", "from", "as", "do",
// Common operators
"+", "-", "*", "/", "=", "==", "!=", ">", "<", ">=", "<=",
"and", "or", "not", "~", "%", "?", ":", "??",
// Common punctuation
"(", ")", "[", "]", "{", "}", ".", ",", "|", ";",
// Common literals
"true", "false", "null",
// Empty string
"",
}
// TokenizerPooled holds a set of resources for zero-allocation tokenization
type TokenizerPooled struct {
tokenizer ZeroAllocTokenizer
used bool
}
// TokenizerPool is a pool of tokenizer resources
var tokenizerPool = sync.Pool{
New: func() interface{} {
// Create a pre-allocated tokenizer with reasonable defaults
return &TokenizerPooled{
tokenizer: ZeroAllocTokenizer{
tokenBuffer: make([]Token, 0, 256), // Buffer for tokens
tempStrings: append([]string{}, commonStrings...),
result: nil,
},
used: false,
}
},
}
// GetTokenizer gets a tokenizer from the pool
func GetTokenizer(source string, capacityHint int) *ZeroAllocTokenizer {
pooled := tokenizerPool.Get().(*TokenizerPooled)
// Reset the tokenizer
tokenizer := &pooled.tokenizer
tokenizer.source = source
tokenizer.position = 0
tokenizer.line = 1
// Ensure token buffer has enough capacity
neededCapacity := capacityHint
if neededCapacity <= 0 {
// Estimate capacity based on source length
neededCapacity = len(source) / 10
if neededCapacity < 32 {
neededCapacity = 32
}
}
// Resize token buffer if needed
if cap(tokenizer.tokenBuffer) < neededCapacity {
tokenizer.tokenBuffer = make([]Token, 0, neededCapacity)
} else {
tokenizer.tokenBuffer = tokenizer.tokenBuffer[:0]
}
// Reset result
tokenizer.result = nil
// Mark as used
pooled.used = true
return tokenizer
}
// ReleaseTokenizer returns a tokenizer to the pool
func ReleaseTokenizer(tokenizer *ZeroAllocTokenizer) {
// Get the parent pooled struct
pooled := (*TokenizerPooled)(unsafe.Pointer(
uintptr(unsafe.Pointer(tokenizer)) - unsafe.Offsetof(TokenizerPooled{}.tokenizer)))
// Only return to pool if it's used
if pooled.used {
// Mark as not used and clear references that might prevent GC
pooled.used = false
tokenizer.source = ""
tokenizer.result = nil
// Return to pool
tokenizerPool.Put(pooled)
}
}
// AddToken adds a token to the buffer
func (t *ZeroAllocTokenizer) AddToken(tokenType int, value string, line int) {
// Create a token
var token Token
token.Type = tokenType
token.Value = value
token.Line = line
// Add to buffer
t.tokenBuffer = append(t.tokenBuffer, token)
}
// GetStringConstant checks if a string exists in our constants and returns
// the canonical version to avoid allocation
func (t *ZeroAllocTokenizer) GetStringConstant(s string) string {
// First check common strings
for _, constant := range t.tempStrings {
if constant == s {
return constant
}
}
// Add to temp strings if it's a short string that might be reused
if len(s) <= 20 {
t.tempStrings = append(t.tempStrings, s)
}
return s
}
// TokenizeExpression tokenizes an expression string with zero allocations
func (t *ZeroAllocTokenizer) TokenizeExpression(expr string) []Token {
// Save current position and set new source context
savedSource := t.source
savedPosition := t.position
savedLine := t.line
t.source = expr
t.position = 0
startTokenCount := len(t.tokenBuffer)
var inString bool
var stringDelimiter byte
var stringStart int
for t.position < len(t.source) {
c := t.source[t.position]
// Handle string literals
if (c == '"' || c == '\'') && (t.position == 0 || t.source[t.position-1] != '\\') {
if inString && c == stringDelimiter {
// End of string, add the string token
value := t.source[stringStart:t.position]
t.AddToken(TOKEN_STRING, value, t.line)
inString = false
} else if !inString {
// Start of string
inString = true
stringDelimiter = c
stringStart = t.position + 1
}
t.position++
continue
}
// Skip chars inside strings
if inString {
t.position++
continue
}
// Handle operators (includes multi-char operators like ==, !=, etc.)
if isOperator(c) {
op := string(c)
t.position++
// Check for two-character operators
if t.position < len(t.source) {
nextChar := t.source[t.position]
twoCharOp := string([]byte{c, nextChar})
// Check common two-char operators
if (c == '=' && nextChar == '=') ||
(c == '!' && nextChar == '=') ||
(c == '>' && nextChar == '=') ||
(c == '<' && nextChar == '=') ||
(c == '&' && nextChar == '&') ||
(c == '|' && nextChar == '|') ||
(c == '?' && nextChar == '?') {
op = twoCharOp
t.position++
}
}
// Use constant version of the operator string if possible
op = t.GetStringConstant(op)
t.AddToken(TOKEN_OPERATOR, op, t.line)
continue
}
// Handle punctuation
if isPunctuation(c) {
// Use constant version of punctuation
punct := t.GetStringConstant(string(c))
t.AddToken(TOKEN_PUNCTUATION, punct, t.line)
t.position++
continue
}
// Skip whitespace
if isWhitespace(c) {
t.position++
if c == '\n' {
t.line++
}
continue
}
// Handle identifiers, literals, etc.
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' {
// Start of an identifier
start := t.position
// Find the end
t.position++
for t.position < len(t.source) &&
((t.source[t.position] >= 'a' && t.source[t.position] <= 'z') ||
(t.source[t.position] >= 'A' && t.source[t.position] <= 'Z') ||
(t.source[t.position] >= '0' && t.source[t.position] <= '9') ||
t.source[t.position] == '_') {
t.position++
}
// Extract the identifier
identifier := t.source[start:t.position]
// Try to use a canonical string
identifier = t.GetStringConstant(identifier)
// Keywords/literals get special token types
if identifier == "true" || identifier == "false" || identifier == "null" {
t.AddToken(TOKEN_NAME, identifier, t.line)
} else {
t.AddToken(TOKEN_NAME, identifier, t.line)
}
continue
}
// Handle numbers
if (c >= '0' && c <= '9') || (c == '-' && t.position+1 < len(t.source) && t.source[t.position+1] >= '0' && t.source[t.position+1] <= '9') {
start := t.position
// Skip the negative sign if present
if c == '-' {
t.position++
}
// Consume digits
for t.position < len(t.source) && t.source[t.position] >= '0' && t.source[t.position] <= '9' {
t.position++
}
// Handle decimal point
if t.position < len(t.source) && t.source[t.position] == '.' {
t.position++
// Consume fractional digits
for t.position < len(t.source) && t.source[t.position] >= '0' && t.source[t.position] <= '9' {
t.position++
}
}
// Add the number token
t.AddToken(TOKEN_NUMBER, t.source[start:t.position], t.line)
continue
}
// Unrecognized character
t.position++
}
// Create slice of tokens
tokens := t.tokenBuffer[startTokenCount:]
// Restore original context
t.source = savedSource
t.position = savedPosition
t.line = savedLine
return tokens
}
// TokenizeHtmlPreserving performs full tokenization of a template with HTML preservation
func (t *ZeroAllocTokenizer) TokenizeHtmlPreserving() ([]Token, error) {
// Reset position and line
t.position = 0
t.line = 1
// Clear token buffer
t.tokenBuffer = t.tokenBuffer[:0]
tagPatterns := [5]string{"{{-", "{{", "{%-", "{%", "{#"}
tagTypes := [5]int{TOKEN_VAR_START_TRIM, TOKEN_VAR_START, TOKEN_BLOCK_START_TRIM, TOKEN_BLOCK_START, TOKEN_COMMENT_START}
tagLengths := [5]int{3, 2, 3, 2, 2}
for t.position < len(t.source) {
// Find the next tag
nextTagPos := -1
tagType := -1
tagLength := 0
// Check for all possible tag patterns
// This loop avoids allocations by manually checking prefixes
remainingSource := t.source[t.position:]
for i := 0; i < 5; i++ {
pattern := tagPatterns[i]
if len(remainingSource) >= len(pattern) &&
remainingSource[:len(pattern)] == pattern {
// Tag found at current position
nextTagPos = t.position
tagType = tagTypes[i]
tagLength = tagLengths[i]
break
}
// If not found at current position, find it in the remainder
patternPos := strings.Index(remainingSource, pattern)
if patternPos != -1 {
pos := t.position + patternPos
if nextTagPos == -1 || pos < nextTagPos {
nextTagPos = pos
tagType = tagTypes[i]
tagLength = tagLengths[i]
}
}
}
// Check if the tag is escaped
if nextTagPos != -1 && nextTagPos > 0 && t.source[nextTagPos-1] == '\\' {
// Add text up to the backslash
if nextTagPos-1 > t.position {
preText := t.source[t.position : nextTagPos-1]
t.AddToken(TOKEN_TEXT, preText, t.line)
t.line += countNewlines(preText)
}
// Add the tag as literal text (without the backslash)
// Find which pattern was matched
for i := 0; i < 5; i++ {
if tagType == tagTypes[i] {
t.AddToken(TOKEN_TEXT, tagPatterns[i], t.line)
break
}
}
// Move past this tag
t.position = nextTagPos + tagLength
continue
}
// No more tags found - add the rest as TEXT
if nextTagPos == -1 {
if t.position < len(t.source) {
remainingText := t.source[t.position:]
t.AddToken(TOKEN_TEXT, remainingText, t.line)
t.line += countNewlines(remainingText)
}
break
}
// Add text before the tag
if nextTagPos > t.position {
textContent := t.source[t.position:nextTagPos]
t.AddToken(TOKEN_TEXT, textContent, t.line)
t.line += countNewlines(textContent)
}
// Add the tag start token
t.AddToken(tagType, "", t.line)
// Move past opening tag
t.position = nextTagPos + tagLength
// Find matching end tag
var endTag string
var endTagType int
var endTagLength int
if tagType == TOKEN_VAR_START || tagType == TOKEN_VAR_START_TRIM {
// Look for "}}" or "-}}"
endPos1 := strings.Index(t.source[t.position:], "}}")
endPos2 := strings.Index(t.source[t.position:], "-}}")
if endPos1 != -1 && (endPos2 == -1 || endPos1 < endPos2) {
endTag = "}}"
endTagType = TOKEN_VAR_END
endTagLength = 2
} else if endPos2 != -1 {
endTag = "-}}"
endTagType = TOKEN_VAR_END_TRIM
endTagLength = 3
} else {
return nil, fmt.Errorf("unclosed variable tag at line %d", t.line)
}
} else if tagType == TOKEN_BLOCK_START || tagType == TOKEN_BLOCK_START_TRIM {
// Look for "%}" or "-%}"
endPos1 := strings.Index(t.source[t.position:], "%}")
endPos2 := strings.Index(t.source[t.position:], "-%}")
if endPos1 != -1 && (endPos2 == -1 || endPos1 < endPos2) {
endTag = "%}"
endTagType = TOKEN_BLOCK_END
endTagLength = 2
} else if endPos2 != -1 {
endTag = "-%}"
endTagType = TOKEN_BLOCK_END_TRIM
endTagLength = 3
} else {
return nil, fmt.Errorf("unclosed block tag at line %d", t.line)
}
} else if tagType == TOKEN_COMMENT_START {
// Look for "#}"
endPos := strings.Index(t.source[t.position:], "#}")
if endPos == -1 {
return nil, fmt.Errorf("unclosed comment at line %d", t.line)
}
endTag = "#}"
endTagType = TOKEN_COMMENT_END
endTagLength = 2
}
// Find position of the end tag
endPos := strings.Index(t.source[t.position:], endTag)
if endPos == -1 {
return nil, fmt.Errorf("unclosed tag at line %d", t.line)
}
// Get content between tags
tagContent := t.source[t.position : t.position+endPos]
t.line += countNewlines(tagContent)
// Process tag content based on type
if tagType == TOKEN_COMMENT_START {
// Store comments as TEXT tokens
if len(tagContent) > 0 {
t.AddToken(TOKEN_TEXT, tagContent, t.line)
}
} else {
// For variable and block tags, tokenize the content
tagContent = strings.TrimSpace(tagContent)
if tagType == TOKEN_BLOCK_START || tagType == TOKEN_BLOCK_START_TRIM {
// Process block tags with specialized tokenization
t.processBlockTag(tagContent)
} else {
// Process variable tags with optimized tokenization
if len(tagContent) > 0 {
if !strings.ContainsAny(tagContent, ".|[](){}\"',+-*/=!<>%&^~") {
// Simple variable name
identifier := t.GetStringConstant(tagContent)
t.AddToken(TOKEN_NAME, identifier, t.line)
} else {
// Complex expression
t.TokenizeExpression(tagContent)
}
}
}
}
// Add the end tag token
t.AddToken(endTagType, "", t.line)
// Move past the end tag
t.position = t.position + endPos + endTagLength
}
// Add EOF token
t.AddToken(TOKEN_EOF, "", t.line)
// Save the token buffer to result
t.result = t.tokenBuffer
return t.result, nil
}
// processBlockTag handles specialized block tag tokenization
func (t *ZeroAllocTokenizer) processBlockTag(content string) {
// Extract the tag name
spacePos := strings.IndexByte(content, ' ')
var blockName string
var blockContent string
if spacePos == -1 {
// No space found, the whole content is the tag name
blockName = content
blockContent = ""
} else {
blockName = content[:spacePos]
blockContent = strings.TrimSpace(content[spacePos+1:])
}
// Use canonical string for block name
blockName = t.GetStringConstant(blockName)
t.AddToken(TOKEN_NAME, blockName, t.line)
// If there's no content, we're done
if blockContent == "" {
return
}
// Process based on block type
switch blockName {
case "if", "elseif":
// For conditional blocks, tokenize expression
t.TokenizeExpression(blockContent)
case "for":
// Process for loop with iterator(s) and collection
inPos := strings.Index(strings.ToLower(blockContent), " in ")
if inPos != -1 {
iterators := strings.TrimSpace(blockContent[:inPos])
collection := strings.TrimSpace(blockContent[inPos+4:])
// Handle key, value iterator syntax
if strings.Contains(iterators, ",") {
iterParts := strings.SplitN(iterators, ",", 2)
if len(iterParts) == 2 {
// Process iterator variables
keyVar := t.GetStringConstant(strings.TrimSpace(iterParts[0]))
valueVar := t.GetStringConstant(strings.TrimSpace(iterParts[1]))
t.AddToken(TOKEN_NAME, keyVar, t.line)
t.AddToken(TOKEN_PUNCTUATION, ",", t.line)
t.AddToken(TOKEN_NAME, valueVar, t.line)
}
} else {
// Single iterator
iterator := t.GetStringConstant(iterators)
t.AddToken(TOKEN_NAME, iterator, t.line)
}
// Add 'in' keyword
t.AddToken(TOKEN_NAME, "in", t.line)
// Process collection expression
t.TokenizeExpression(collection)
} else {
// Fallback for malformed for loops
t.AddToken(TOKEN_NAME, blockContent, t.line)
}
case "set":
// Handle variable assignment
assignPos := strings.Index(blockContent, "=")
if assignPos != -1 {
varName := strings.TrimSpace(blockContent[:assignPos])
value := strings.TrimSpace(blockContent[assignPos+1:])
// Add the variable name token
varName = t.GetStringConstant(varName)
t.AddToken(TOKEN_NAME, varName, t.line)
// Add the assignment operator
t.AddToken(TOKEN_OPERATOR, "=", t.line)
// Tokenize the value expression
t.TokenizeExpression(value)
} else {
// Simple set without assignment
blockContent = t.GetStringConstant(blockContent)
t.AddToken(TOKEN_NAME, blockContent, t.line)
}
case "do":
// Handle variable assignment similar to set tag
assignPos := strings.Index(blockContent, "=")
if assignPos != -1 {
varName := strings.TrimSpace(blockContent[:assignPos])
value := strings.TrimSpace(blockContent[assignPos+1:])
// Check if varName is valid (should be a variable name)
// In Twig, variable names must start with a letter or underscore
if len(varName) > 0 && (isCharAlpha(varName[0]) || varName[0] == '_') {
// Add the variable name token
varName = t.GetStringConstant(varName)
t.AddToken(TOKEN_NAME, varName, t.line)
// Add the assignment operator
t.AddToken(TOKEN_OPERATOR, "=", t.line)
// Tokenize the value expression
if len(value) > 0 {
t.TokenizeExpression(value)
} else {
// Empty value after =, which is invalid
// Add an error token to trigger proper parser error
t.AddToken(TOKEN_EOF, "ERROR_MISSING_VALUE", t.line)
}
} else {
// Invalid variable name (like a number or operator)
// Just tokenize as expressions to produce an error in the parser
t.TokenizeExpression(varName)
t.AddToken(TOKEN_OPERATOR, "=", t.line)
t.TokenizeExpression(value)
}
} else {
// No assignment, just an expression to evaluate
t.TokenizeExpression(blockContent)
}
case "include":
// Handle include with template path and optional context
withPos := strings.Index(strings.ToLower(blockContent), " with ")
if withPos != -1 {
templatePath := strings.TrimSpace(blockContent[:withPos])
contextExpr := strings.TrimSpace(blockContent[withPos+6:])
// Process template path
t.tokenizeTemplatePath(templatePath)
// Add 'with' keyword
t.AddToken(TOKEN_NAME, "with", t.line)
// Process context expression as object
if strings.HasPrefix(contextExpr, "{") && strings.HasSuffix(contextExpr, "}") {
// Context is an object literal
t.AddToken(TOKEN_PUNCTUATION, "{", t.line)
objectContent := contextExpr[1 : len(contextExpr)-1]
t.tokenizeObjectContents(objectContent)
t.AddToken(TOKEN_PUNCTUATION, "}", t.line)
} else {
// Context is a variable or expression
t.TokenizeExpression(contextExpr)
}
} else {
// Just a template path
t.tokenizeTemplatePath(blockContent)
}
case "extends":
// Handle extends tag (similar to include template path)
t.tokenizeTemplatePath(blockContent)
case "from":
// Handle from tag which has a special format:
// {% from "template.twig" import macro1, macro2 as alias %}
importPos := strings.Index(strings.ToLower(blockContent), " import ")
if importPos != -1 {
// Extract template path and macros list
templatePath := strings.TrimSpace(blockContent[:importPos])
macrosStr := strings.TrimSpace(blockContent[importPos+8:]) // 8 = len(" import ")
// Process template path
t.tokenizeTemplatePath(templatePath)
// Add 'import' keyword
t.AddToken(TOKEN_NAME, "import", t.line)
// Process macro imports
macros := strings.Split(macrosStr, ",")
for i, macro := range macros {
macro = strings.TrimSpace(macro)
// Check for "as" alias
asPos := strings.Index(strings.ToLower(macro), " as ")
if asPos != -1 {
// Extract macro name and alias
macroName := strings.TrimSpace(macro[:asPos])
alias := strings.TrimSpace(macro[asPos+4:])
// Add macro name
macroName = t.GetStringConstant(macroName)
t.AddToken(TOKEN_NAME, macroName, t.line)
// Add 'as' keyword
t.AddToken(TOKEN_NAME, "as", t.line)
// Add alias
alias = t.GetStringConstant(alias)
t.AddToken(TOKEN_NAME, alias, t.line)
} else {
// Just the macro name
macro = t.GetStringConstant(macro)
t.AddToken(TOKEN_NAME, macro, t.line)
}
// Add comma if not the last macro
if i < len(macros)-1 {
t.AddToken(TOKEN_PUNCTUATION, ",", t.line)
}
}
} else {
// Malformed from tag, just tokenize as expression
t.TokenizeExpression(blockContent)
}
case "import":
// Handle import tag which allows importing entire templates
// {% import "template.twig" as alias %}
asPos := strings.Index(strings.ToLower(blockContent), " as ")
if asPos != -1 {
// Extract template path and alias
templatePath := strings.TrimSpace(blockContent[:asPos])
alias := strings.TrimSpace(blockContent[asPos+4:])
// Process template path
t.tokenizeTemplatePath(templatePath)
// Add 'as' keyword
t.AddToken(TOKEN_NAME, "as", t.line)
// Add alias
alias = t.GetStringConstant(alias)
t.AddToken(TOKEN_NAME, alias, t.line)
} else {
// Simple import without alias
t.TokenizeExpression(blockContent)
}
default:
// Other block types - tokenize as expression
t.TokenizeExpression(blockContent)
}
}
// Helper methods for specialized tag tokenization
// tokenizeTemplatePath handles template paths in extends/include tags
func (t *ZeroAllocTokenizer) tokenizeTemplatePath(path string) {
path = strings.TrimSpace(path)
// If it's a quoted string
if (strings.HasPrefix(path, "\"") && strings.HasSuffix(path, "\"")) ||
(strings.HasPrefix(path, "'") && strings.HasSuffix(path, "'")) {
// Extract content without quotes
content := path[1 : len(path)-1]
t.AddToken(TOKEN_STRING, content, t.line)
} else {
// Otherwise tokenize as expression
t.TokenizeExpression(path)
}
}
// isCharAlpha checks if a byte is an alphabetic character
func isCharAlpha(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}
// tokenizeObjectContents handles object literal contents
func (t *ZeroAllocTokenizer) tokenizeObjectContents(content string) {
// Track state for nested structures
inString := false
stringDelim := byte(0)
inObject := 0
inArray := 0
start := 0
colonPos := -1
for i := 0; i <= len(content); i++ {
// At end of string or at a comma at the top level
atEnd := i == len(content)
isComma := !atEnd && content[i] == ','
// Process key-value pair when we find a comma or reach the end
if (isComma || atEnd) && inObject == 0 && inArray == 0 && !inString {
if colonPos != -1 {
// We have a key-value pair
keyStr := strings.TrimSpace(content[start:colonPos])
valueStr := strings.TrimSpace(content[colonPos+1 : i])
// Process key
if (len(keyStr) >= 2 && keyStr[0] == '"' && keyStr[len(keyStr)-1] == '"') ||
(len(keyStr) >= 2 && keyStr[0] == '\'' && keyStr[len(keyStr)-1] == '\'') {
// Quoted key
t.AddToken(TOKEN_STRING, keyStr[1:len(keyStr)-1], t.line)
} else {
// Unquoted key
keyStr = t.GetStringConstant(keyStr)
t.AddToken(TOKEN_NAME, keyStr, t.line)
}
// Add colon
t.AddToken(TOKEN_PUNCTUATION, ":", t.line)
// Process value
t.TokenizeExpression(valueStr)
// Add comma if needed
if isComma && i < len(content)-1 {
t.AddToken(TOKEN_PUNCTUATION, ",", t.line)
}
// Reset for next pair
start = i + 1
colonPos = -1
}
continue
}
// Skip end of string case
if atEnd {
continue
}
// Current character
c := content[i]
// Handle string literals
if (c == '"' || c == '\'') && (i == 0 || content[i-1] != '\\') {
if inString && c == stringDelim {
inString = false
} else if !inString {
inString = true
stringDelim = c
}
continue
}
// Skip processing inside strings
if inString {
continue
}
// Handle object and array nesting
if c == '{' {
inObject++
} else if c == '}' {
inObject--
} else if c == '[' {
inArray++
} else if c == ']' {
inArray--
}
// Track colon position for key-value separator
if c == ':' && inObject == 0 && inArray == 0 && colonPos == -1 {
colonPos = i
}
}
}
// ApplyWhitespaceControl applies whitespace control to the tokenized result
func (t *ZeroAllocTokenizer) ApplyWhitespaceControl() {
tokens := t.result
for i := 0; i < len(tokens); i++ {
token := tokens[i]
// Handle opening tags that trim whitespace before them
if token.Type == TOKEN_VAR_START_TRIM || token.Type == TOKEN_BLOCK_START_TRIM {
// If there's a text token before this, trim its trailing whitespace
if i > 0 && tokens[i-1].Type == TOKEN_TEXT {
tokens[i-1].Value = trimTrailingWhitespace(tokens[i-1].Value)
}
}
// Handle closing tags that trim whitespace after them
if token.Type == TOKEN_VAR_END_TRIM || token.Type == TOKEN_BLOCK_END_TRIM {
// If there's a text token after this, trim its leading whitespace
if i+1 < len(tokens) && tokens[i+1].Type == TOKEN_TEXT {
tokens[i+1].Value = trimLeadingWhitespace(tokens[i+1].Value)
}
}
}
}
// The following functions implement string interning and tag detection from the optimized implementations
// newGlobalStringCache creates a new global string cache with pre-populated common strings
func newGlobalStringCache() *GlobalStringCache {
cache := &GlobalStringCache{
strings: make(map[string]string, 64), // Pre-allocate capacity
}
// Pre-populate with common strings
commonStrings := []string{
stringDiv, stringSpan, stringP, stringA, stringImg,
stringHref, stringClass, stringId, stringStyle,
stringIf, stringFor, stringEnd, stringEndif, stringEndfor,
stringElse, stringBlock, stringSet, stringInclude, stringExtends,
stringMacro, stringEquals, stringNotEquals, stringAnd,
stringOr, stringNot, stringIn, stringIs,
// Add empty string as well
"",
}
for _, s := range commonStrings {
cache.strings[s] = s
}
return cache
}
// Intern returns an interned version of the input string
// For strings that are already in the cache, the cached version is returned
// Otherwise, the input string is added to the cache and returned
func Intern(s string) string {
// Fast path for very common strings to avoid lock contention
switch s {
case stringDiv, stringSpan, stringP, stringA, stringImg,
stringIf, stringFor, stringEnd, stringEndif, stringEndfor,
stringElse, "":
return s
}
// Don't intern strings that are too long
if len(s) > maxCacheableLength {
return s
}
// Use read lock for lookup first (less contention)
globalCache.RLock()
cached, exists := globalCache.strings[s]
globalCache.RUnlock()