gitstack

grindlemire/go-tui code browser

6.6 KB Go 282 lines 2026-06-03 · d15bb9f raw
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
package tuigen

import (
	"unicode/utf8"
)

// ReadGoExpr reads a Go expression inside {}, handling nested braces.
// This is called by the parser when it encounters { in an attribute or content context.
// Note: The opening '{' has already been consumed by the lexer when producing TokenLBrace,
// so we start reading from the current position (after the '{').
func (l *Lexer) ReadGoExpr() Token {
	l.startToken()

	// The '{' was already consumed when TokenLBrace was created,
	// so we start with braceDepth=1 and read from current position.
	startPos := l.pos
	braceDepth := 1
	parenDepth := 0
	bracketDepth := 0

	for braceDepth > 0 && l.ch != 0 {
		switch l.ch {
		case '{':
			braceDepth++
		case '}':
			braceDepth--
		case '(':
			parenDepth++
		case ')':
			parenDepth--
		case '[':
			bracketDepth++
		case ']':
			bracketDepth--
		case '"':
			l.skipStringInExpr()
			continue
		case '`':
			l.skipRawStringInExpr()
			continue
		case '\'':
			l.skipCharInExpr()
			continue
		}

		if braceDepth > 0 {
			l.readChar()
		}
	}

	if braceDepth != 0 {
		l.errors.AddError(l.position(), "unterminated Go expression: unmatched '{'")
		return l.makeToken(TokenError, l.source[startPos:l.pos])
	}

	if parenDepth != 0 {
		l.errors.AddErrorf(l.position(), "unterminated Go expression: unmatched parentheses")
	}
	if bracketDepth != 0 {
		l.errors.AddErrorf(l.position(), "unterminated Go expression: unmatched brackets")
	}

	expr := l.source[startPos:l.pos]
	l.readChar() // consume closing }

	return l.makeToken(TokenGoExpr, expr)
}

// skipStringInExpr skips a string literal inside a Go expression.
func (l *Lexer) skipStringInExpr() {
	l.readChar() // consume opening "
	for l.ch != '"' && l.ch != 0 {
		if l.ch == '\\' {
			l.readChar() // skip escape
		}
		l.readChar()
	}
	if l.ch == '"' {
		l.readChar() // consume closing "
	}
}

// skipRawStringInExpr skips a raw string literal inside a Go expression.
func (l *Lexer) skipRawStringInExpr() {
	l.readChar() // consume opening `
	for l.ch != '`' && l.ch != 0 {
		l.readChar()
	}
	if l.ch == '`' {
		l.readChar() // consume closing `
	}
}

// skipCharInExpr skips a character literal inside a Go expression.
func (l *Lexer) skipCharInExpr() {
	l.readChar() // consume opening '
	if l.ch == '\\' {
		l.readChar() // skip escape
	}
	l.readChar() // skip character
	if l.ch == '\'' {
		l.readChar() // consume closing '
	}
}

// PeekToken returns the next token without consuming it.
// This is used by the parser to look ahead.
func (l *Lexer) PeekToken() Token {
	saved := l.SaveState()
	tok := l.Next()
	l.RestoreState(saved)
	return tok
}

// ReadBalancedBraces reads content inside balanced braces, handling nested braces,
// strings, raw strings, and character literals. The opening '{' should NOT have been
// consumed yet - this method reads from the current position expecting to see '{'.
// Returns the content between braces (excluding the braces themselves).
func (l *Lexer) ReadBalancedBraces() (string, error) {
	// Expect opening brace
	if l.ch != '{' {
		return "", NewError(l.position(), "expected '{' at start of balanced braces")
	}
	l.readChar() // consume opening {

	startPos := l.pos
	braceDepth := 1

	for braceDepth > 0 && l.ch != 0 {
		switch l.ch {
		case '{':
			braceDepth++
			l.readChar()
		case '}':
			braceDepth--
			if braceDepth > 0 {
				l.readChar()
			}
		case '"':
			l.skipStringInExpr()
		case '`':
			l.skipRawStringInExpr()
		case '\'':
			l.skipCharInExpr()
		default:
			l.readChar()
		}
	}

	if braceDepth != 0 {
		return "", NewError(l.position(), "unterminated braces: unmatched '{'")
	}

	content := l.source[startPos:l.pos]
	l.readChar() // consume closing }

	return content, nil
}

// ReadUntilBrace reads raw source from the current position until a '{' is encountered.
// Used for capturing if conditions and for iterables as raw Go code.
// Does not consume the '{'.
func (l *Lexer) ReadUntilBrace() string {
	l.skipWhitespaceAndCollectComments()
	startPos := l.pos

	for l.ch != '{' && l.ch != 0 && l.ch != '\n' {
		l.readChar()
	}

	return l.source[startPos:l.pos]
}

// ReadBalancedBracesFrom reads balanced brace content starting from a given source position.
// The startPos should point to the opening '{'. Returns the content between braces
// (excluding the braces themselves) and updates the lexer position to after the closing '}'.
// This is used by the parser when it has a TokenLBrace and needs to capture raw Go code.
func (l *Lexer) ReadBalancedBracesFrom(startPos int) (string, error) {
	// Validate startPos points to '{'
	if startPos < 0 || startPos >= len(l.source) || l.source[startPos] != '{' {
		return "", NewError(l.position(), "invalid start position for balanced braces")
	}

	// Scan forward from startPos+1 to find matching '}'
	contentStart := startPos + 1
	pos := contentStart
	braceDepth := 1

	for pos < len(l.source) && braceDepth > 0 {
		ch := l.source[pos]

		switch ch {
		case '{':
			braceDepth++
			pos++
		case '}':
			braceDepth--
			if braceDepth > 0 {
				pos++
			}
		case '"':
			// Skip string literal
			pos++
			for pos < len(l.source) && l.source[pos] != '"' {
				if l.source[pos] == '\\' && pos+1 < len(l.source) {
					pos += 2 // skip escape sequence
				} else {
					pos++
				}
			}
			if pos < len(l.source) {
				pos++ // skip closing "
			}
		case '`':
			// Skip raw string literal
			pos++
			for pos < len(l.source) && l.source[pos] != '`' {
				pos++
			}
			if pos < len(l.source) {
				pos++ // skip closing `
			}
		case '\'':
			// Skip character literal
			pos++
			if pos < len(l.source) && l.source[pos] == '\\' {
				pos += 2 // skip escape
			} else if pos < len(l.source) {
				pos++ // skip char
			}
			if pos < len(l.source) && l.source[pos] == '\'' {
				pos++ // skip closing '
			}
		default:
			pos++
		}
	}

	if braceDepth != 0 {
		return "", NewError(l.position(), "unterminated braces: unmatched '{'")
	}

	content := l.source[contentStart:pos]

	// Update lexer position to after closing '}'
	l.pos = pos
	l.readPos = pos + 1
	if l.readPos <= len(l.source) {
		r, _ := utf8.DecodeRuneInString(l.source[pos:])
		l.ch = r
	} else {
		l.ch = 0
	}

	// Calculate correct line/column from the start of the source.
	// We need to recalculate from scratch because the lexer may have peeked ahead
	// and l.column could be in an inconsistent state.
	lineStart := 0
	lineNum := 1
	for i := range startPos {
		if l.source[i] == '\n' {
			lineStart = i + 1
			lineNum++
		}
	}

	// Scan from lineStart to pos to get correct line and column
	l.line = lineNum
	l.column = 1
	for i := lineStart; i < pos && i < len(l.source); i++ {
		if l.source[i] == '\n' {
			l.line++
			l.column = 1
		} else {
			l.column++
		}
	}

	l.readChar() // advance past '}' and update column

	return content, nil
}