-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.go
More file actions
197 lines (172 loc) · 4.69 KB
/
tokenizer.go
File metadata and controls
197 lines (172 loc) · 4.69 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
package dotenv
import (
"fmt"
"regexp"
"strings"
)
// Tokenizer handles lexical analysis of .env content
type Tokenizer struct {
content string
pos int
line int
col int
length int
exportMode bool // whether to handle "export KEY=value" syntax
}
// NewTokenizer creates a new tokenizer for the given content
func NewTokenizer(content string) *Tokenizer {
return &Tokenizer{
content: content,
pos: 0,
line: 1,
col: 1,
length: len(content),
exportMode: true, // enable export handling by default
}
}
// peek returns the character at the current position without advancing
func (t *Tokenizer) peek() byte {
if t.pos >= t.length {
return 0
}
return t.content[t.pos]
}
// peekNext returns the character at the next position without advancing
func (t *Tokenizer) peekNext() byte {
if t.pos+1 >= t.length {
return 0
}
return t.content[t.pos+1]
}
// advance moves to the next character
func (t *Tokenizer) advance() byte {
if t.pos >= t.length {
return 0
}
ch := t.content[t.pos]
t.pos++
if ch == '\n' {
t.line++
t.col = 1
} else {
t.col++
}
return ch
}
// skipWhitespace skips spaces and tabs (but not newlines)
func (t *Tokenizer) skipWhitespace() {
for t.pos < t.length && (t.peek() == ' ' || t.peek() == '\t') {
t.advance()
}
}
// skipToNextLine skips to the next line
func (t *Tokenizer) skipToNextLine() {
for t.pos < t.length && t.peek() != '\n' {
t.advance()
}
if t.pos < t.length {
t.advance() // consume the newline
}
}
// isValidKeyChar checks if character is valid in a key name
func isValidKeyChar(ch byte) bool {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
(ch >= '0' && ch <= '9') || ch == '_'
}
// isValidKeyStart checks if character is valid at the start of a key name
func isValidKeyStart(ch byte) bool {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || ch == '_'
}
// parseKey parses a key (identifier)
func (t *Tokenizer) parseKey() (string, error) {
start := t.pos
// First character must be letter or underscore
if !isValidKeyStart(t.peek()) {
return "", fmt.Errorf("invalid key name at line %d: keys must start with letter or underscore", t.line)
}
for t.pos < t.length && isValidKeyChar(t.peek()) {
t.advance()
}
return t.content[start:t.pos], nil
}
// parseUnquotedValue parses an unquoted value until comment or newline
func (t *Tokenizer) parseUnquotedValue() (string, bool) {
var result strings.Builder
hasComment := false
for t.pos < t.length {
ch := t.peek()
if ch == '\n' || ch == '\r' {
break
}
if ch == '#' {
hasComment = true
break
}
result.WriteByte(t.advance())
}
// Only trim trailing whitespace, preserve leading whitespace
value := strings.TrimRight(result.String(), " \t")
return value, hasComment
} // parseQuotedValue parses a quoted value (single or double quotes)
func (t *Tokenizer) parseQuotedValue(quote byte) (string, error) {
var result strings.Builder
t.advance() // consume opening quote
for t.pos < t.length {
ch := t.peek()
if ch == quote {
t.advance() // consume closing quote
return result.String(), nil
}
if ch == '\\' && quote == '"' {
// Handle escapes only in double quotes
t.advance() // consume backslash
if t.pos >= t.length {
return "", fmt.Errorf("unexpected end of file after escape at line %d", t.line)
}
escaped := t.advance()
switch escaped {
case 'n':
result.WriteByte('\n')
case 't':
result.WriteByte('\t')
case 'r':
result.WriteByte('\r')
case '\\':
result.WriteByte('\\')
case '"':
result.WriteByte('"')
case '\'':
result.WriteByte('\'')
default:
// For unknown escapes, include both backslash and character
result.WriteByte('\\')
result.WriteByte(escaped)
}
} else {
result.WriteByte(t.advance())
}
}
return "", fmt.Errorf("unterminated quoted string at line %d", t.line)
}
// expandVariables expands ${VAR} and $VAR patterns in the value
func expandVariables(value string, env map[string]string) string {
// First handle ${VAR} format
varPattern := regexp.MustCompile(`\$\{([A-Za-z_][A-Za-z0-9_]*)\}`)
result := varPattern.ReplaceAllStringFunc(value, func(match string) string {
varName := match[2 : len(match)-1] // remove ${ and }
if val, exists := env[varName]; exists {
return val
}
return match // keep original if not found
})
// Then handle $VAR format (but not if already inside ${})
simplePattern := regexp.MustCompile(`\$([A-Za-z_][A-Za-z0-9_]*)`)
result = simplePattern.ReplaceAllStringFunc(result, func(match string) string {
varName := match[1:] // remove $
if val, exists := env[varName]; exists {
return val
}
return match // keep original if not found
})
return result
}