-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
590 lines (505 loc) · 16.4 KB
/
parser.go
File metadata and controls
590 lines (505 loc) · 16.4 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
package xterm
// Ported from xterm.js src/common/parser/EscapeSequenceParser.ts.
// Synchronous VT500-compatible escape sequence parser.
import "reflect"
// Handler types for the escape sequence parser.
type (
// PrintHandler handles printable character ranges.
PrintHandler func(data []uint32, start, end int)
// ExecuteHandler handles C0/C1 control codes.
ExecuteHandler func()
// CsiHandler handles CSI sequences. Returns true to stop handler chain bubbling.
CsiHandler func(params *Params) bool
// EscHandler handles ESC sequences. Returns true to stop handler chain bubbling.
EscHandler func() bool
// ExecuteFallbackHandler is called when no execute handler matches.
ExecuteFallbackHandler func(code uint32)
// CsiFallbackHandler is called when no CSI handler matches.
CsiFallbackHandler func(ident int, params *Params)
// EscFallbackHandler is called when no ESC handler matches.
EscFallbackHandler func(ident int)
// PrintFallbackHandler is called when no print handler is set.
// Same signature as PrintHandler.
PrintFallbackHandler = PrintHandler
// ErrorHandler processes parsing errors. Returns a (possibly modified) ParsingState.
// Set abort=true in the returned state to stop parsing.
ErrorHandler func(state ParsingState) ParsingState
)
// ParsingState describes the parser state at the point of an error.
type ParsingState struct {
Position int
Code uint32
CurrentState ParserState
Collect int
Params *Params
Abort bool
}
// FunctionIdentifier identifies a CSI or ESC function by its prefix, intermediates, and final character.
type FunctionIdentifier struct {
Prefix byte // 0 for none, or '<', '>', '?', '!'
Intermediates string
Final byte
}
// EscapeSequenceParser is a synchronous VT500-compatible escape sequence parser.
type EscapeSequenceParser struct {
initialState ParserState
currentState ParserState
precedingJoinState int
transitions *TransitionTable
params *Params
collect int
printHandler PrintHandler
printHandlerFb PrintFallbackHandler
executeHandlers [128]ExecuteHandler
executeHandlerFb ExecuteFallbackHandler
csiHandlers map[int][]CsiHandler
csiHandlerFb CsiFallbackHandler
escHandlers map[int][]EscHandler
escHandlerFb EscFallbackHandler
oscParser *OscParser
dcsParser *DcsParser
apcParser *ApcParser
errorHandler ErrorHandler
errorHandlerFb ErrorHandler
}
// NewEscapeSequenceParser creates a new parser with the VT500 transition table.
func NewEscapeSequenceParser() *EscapeSequenceParser {
return NewEscapeSequenceParserWithTable(VT500TransitionTable)
}
// NewEscapeSequenceParserWithTable creates a new parser with a custom transition table.
func NewEscapeSequenceParserWithTable(table *TransitionTable) *EscapeSequenceParser {
defaultErrorHandler := func(state ParsingState) ParsingState {
return state
}
p := &EscapeSequenceParser{
initialState: ParserStateGround,
currentState: ParserStateGround,
transitions: table,
params: DefaultParams(),
csiHandlers: make(map[int][]CsiHandler),
escHandlers: make(map[int][]EscHandler),
oscParser: NewOscParser(),
dcsParser: NewDcsParser(),
apcParser: NewApcParser(),
printHandler: func(data []uint32, start, end int) {},
printHandlerFb: func(data []uint32, start, end int) {},
executeHandlerFb: func(code uint32) {},
csiHandlerFb: func(ident int, params *Params) {},
escHandlerFb: func(ident int) {},
errorHandler: defaultErrorHandler,
errorHandlerFb: defaultErrorHandler,
}
p.params.AddParam(0) // ZDM
return p
}
// identifier computes the numeric identifier for a FunctionIdentifier.
func (p *EscapeSequenceParser) identifier(id FunctionIdentifier) int {
var collect int
if id.Prefix != 0 {
collect = int(id.Prefix)
}
for i := range len(id.Intermediates) {
collect = collect<<8 | int(id.Intermediates[i])
}
return collect<<8 | int(id.Final)
}
// IdentToString converts a numeric identifier back to a human-readable string.
func IdentToString(ident int) string {
var res []byte
for ident != 0 {
res = append([]byte{byte(ident & 0xFF)}, res...)
ident >>= 8
}
return string(res)
}
// --- Print handler ---
// SetPrintHandler sets the handler for printable character ranges.
func (p *EscapeSequenceParser) SetPrintHandler(handler PrintHandler) {
p.printHandler = handler
}
// ClearPrintHandler resets the print handler to the fallback.
func (p *EscapeSequenceParser) ClearPrintHandler() {
p.printHandler = p.printHandlerFb
}
// SetPrintHandlerFallback sets the fallback print handler.
func (p *EscapeSequenceParser) SetPrintHandlerFallback(handler PrintFallbackHandler) {
p.printHandlerFb = handler
}
// --- Execute handlers ---
// RegisterExecuteHandler registers a handler for a specific control code.
// Returns a Disposable that removes the handler.
func (p *EscapeSequenceParser) RegisterExecuteHandler(code byte, handler ExecuteHandler) Disposable {
p.executeHandlers[code] = handler
return toDisposable(func() {
p.executeHandlers[code] = nil
})
}
// ClearExecuteHandler removes the handler for a specific control code.
func (p *EscapeSequenceParser) ClearExecuteHandler(code byte) {
p.executeHandlers[code] = nil
}
// SetExecuteHandlerFallback sets the fallback execute handler.
func (p *EscapeSequenceParser) SetExecuteHandlerFallback(handler ExecuteFallbackHandler) {
p.executeHandlerFb = handler
}
// --- CSI handlers ---
// RegisterCsiHandler registers a handler for a CSI function identifier.
// Returns a Disposable that removes the handler.
func (p *EscapeSequenceParser) RegisterCsiHandler(id FunctionIdentifier, handler CsiHandler) Disposable {
ident := p.identifier(id)
p.csiHandlers[ident] = append(p.csiHandlers[ident], handler)
hPtr := reflect.ValueOf(handler).Pointer()
return toDisposable(func() {
list := p.csiHandlers[ident]
for i := len(list) - 1; i >= 0; i-- {
if reflect.ValueOf(list[i]).Pointer() == hPtr {
p.csiHandlers[ident] = append(list[:i], list[i+1:]...)
return
}
}
})
}
// ClearCsiHandler removes all handlers for a CSI function identifier.
func (p *EscapeSequenceParser) ClearCsiHandler(id FunctionIdentifier) {
delete(p.csiHandlers, p.identifier(id))
}
// SetCsiHandlerFallback sets the fallback CSI handler.
func (p *EscapeSequenceParser) SetCsiHandlerFallback(handler CsiFallbackHandler) {
p.csiHandlerFb = handler
}
// --- ESC handlers ---
// RegisterEscHandler registers a handler for an ESC function identifier.
// Returns a Disposable that removes the handler.
func (p *EscapeSequenceParser) RegisterEscHandler(id FunctionIdentifier, handler EscHandler) Disposable {
ident := p.identifier(id)
p.escHandlers[ident] = append(p.escHandlers[ident], handler)
hPtr := reflect.ValueOf(handler).Pointer()
return toDisposable(func() {
list := p.escHandlers[ident]
for i := len(list) - 1; i >= 0; i-- {
if reflect.ValueOf(list[i]).Pointer() == hPtr {
p.escHandlers[ident] = append(list[:i], list[i+1:]...)
return
}
}
})
}
// ClearEscHandler removes all handlers for an ESC function identifier.
func (p *EscapeSequenceParser) ClearEscHandler(id FunctionIdentifier) {
delete(p.escHandlers, p.identifier(id))
}
// SetEscHandlerFallback sets the fallback ESC handler.
func (p *EscapeSequenceParser) SetEscHandlerFallback(handler EscFallbackHandler) {
p.escHandlerFb = handler
}
// --- Sub-parser delegation ---
// RegisterDcsHandler registers a DCS handler.
func (p *EscapeSequenceParser) RegisterDcsHandler(id FunctionIdentifier, handler DcsHandler) Disposable {
return p.dcsParser.RegisterHandler(p.identifier(id), handler)
}
// ClearDcsHandler removes all DCS handlers for the identifier.
func (p *EscapeSequenceParser) ClearDcsHandler(id FunctionIdentifier) {
p.dcsParser.ClearHandler(p.identifier(id))
}
// SetDcsHandlerFallback sets the DCS fallback handler.
func (p *EscapeSequenceParser) SetDcsHandlerFallback(handler DcsFallbackHandler) {
p.dcsParser.SetHandlerFallback(handler)
}
// RegisterOscHandler registers an OSC handler.
func (p *EscapeSequenceParser) RegisterOscHandler(ident int, handler OscHandler) Disposable {
return p.oscParser.RegisterHandler(ident, handler)
}
// ClearOscHandler removes all OSC handlers for the identifier.
func (p *EscapeSequenceParser) ClearOscHandler(ident int) {
p.oscParser.ClearHandler(ident)
}
// SetOscHandlerFallback sets the OSC fallback handler.
func (p *EscapeSequenceParser) SetOscHandlerFallback(handler OscFallbackHandler) {
p.oscParser.SetHandlerFallback(handler)
}
// RegisterApcHandler registers an APC handler.
func (p *EscapeSequenceParser) RegisterApcHandler(id FunctionIdentifier, handler ApcHandler) Disposable {
id.Prefix = 0 // APC does not support prefix byte
return p.apcParser.RegisterHandler(p.identifier(id), handler)
}
// ClearApcHandler removes all APC handlers for the identifier.
func (p *EscapeSequenceParser) ClearApcHandler(id FunctionIdentifier) {
id.Prefix = 0 // APC does not support prefix byte
p.apcParser.ClearHandler(p.identifier(id))
}
// SetApcHandlerFallback sets the APC fallback handler.
func (p *EscapeSequenceParser) SetApcHandlerFallback(handler ApcFallbackHandler) {
p.apcParser.SetHandlerFallback(handler)
}
// --- Error handler ---
// SetErrorHandler sets the error handler.
func (p *EscapeSequenceParser) SetErrorHandler(handler ErrorHandler) {
p.errorHandler = handler
}
// ClearErrorHandler resets the error handler to the default.
func (p *EscapeSequenceParser) ClearErrorHandler() {
p.errorHandler = p.errorHandlerFb
}
// --- State access ---
// CurrentState returns the current parser state.
func (p *EscapeSequenceParser) CurrentState() ParserState {
return p.currentState
}
// PrecedingJoinState returns the preceding grapheme join state.
func (p *EscapeSequenceParser) PrecedingJoinState() int {
return p.precedingJoinState
}
// SetPrecedingJoinState sets the preceding grapheme join state.
func (p *EscapeSequenceParser) SetPrecedingJoinState(state int) {
p.precedingJoinState = state
}
// --- Reset ---
// Reset resets the parser to its initial state.
func (p *EscapeSequenceParser) Reset() {
p.currentState = p.initialState
p.oscParser.Reset()
p.dcsParser.Reset()
p.apcParser.Reset()
p.params.ResetZdm()
p.collect = 0
p.precedingJoinState = 0
}
// Dispose cleans up all parser resources.
func (p *EscapeSequenceParser) Dispose() {
p.oscParser.Dispose()
p.dcsParser.Dispose()
p.apcParser.Dispose()
}
// --- Parse ---
// Parse processes UTF-32 codepoints in data[0:length].
// This is the main parse loop, fully synchronous (no async/promise support).
func (p *EscapeSequenceParser) Parse(data []uint32, length int) {
var code uint32
var transition uint16
table := p.transitions.table
for i := 0; i < length; i++ {
code = data[i]
// EXE fast-path: common control bytes (0x00-0x17) in non-payload states
// bypass the transition table entirely and dispatch directly.
if code < 0x18 && p.currentState <= ParserStateCSIIgnore {
if p.executeHandlers[code] != nil {
p.executeHandlers[code]()
} else {
p.executeHandlerFb(code)
}
p.precedingJoinState = 0
continue
}
// CSI fast-path: when ESC [ is detected in a non-string state, parse
// params and final byte in a tight loop without table lookups.
if code == 0x1b && p.currentState < ParserStateOSCString &&
i+2 < length && data[i+1] == 0x5b {
p.params.ResetZdm()
p.collect = 0
k := i + 2
ch := data[k]
// Prefix byte (< = > ?)
if ch >= 0x3c && ch <= 0x3f {
p.collect = int(ch)
k++
}
csiDone := false
for ; k < length; k++ {
ch = data[k]
if ch >= 0x30 && ch <= 0x39 {
p.params.AddDigit(int32(ch) - 48)
} else if ch == 0x3b {
p.params.AddParam(0) // ZDM
} else if ch == 0x3a {
p.params.AddSubParam(-1)
} else if ch >= 0x40 && ch <= 0x7e {
// Final byte — dispatch CSI handler
ident := p.collect<<8 | int(ch)
handlers := p.csiHandlers[ident]
j := len(handlers) - 1
for ; j >= 0; j-- {
if handlers[j](p.params) {
break
}
}
if j < 0 {
p.csiHandlerFb(ident, p.params)
}
p.precedingJoinState = 0
i = k
p.currentState = ParserStateGround
csiDone = true
break
} else {
// Intermediate byte or unexpected — fall back to table-driven path
break
}
}
if !csiDone {
// Ran out of data or hit an intermediate; let the table-driven
// path continue from the CSI_PARAM state.
i = k - 1
p.currentState = ParserStateCSIParam
}
continue
}
// Map non-ASCII printable to the 0xA0 slot
if code < 0xa0 {
transition = table[int(p.currentState)<<tableIndexStateShift|int(code)]
} else {
transition = table[int(p.currentState)<<tableIndexStateShift|nonASCIIPrintable]
}
switch ParserAction(transition >> tableTransitionActionShift) {
case ParserActionPrint:
// Read ahead for contiguous printable range (loop unrolling)
j := i + 1
for ; j < length; j++ {
code = data[j]
if code < 0x20 || (code > 0x7e && code < nonASCIIPrintable) {
break
}
}
p.printHandler(data, i, j)
i = j - 1
case ParserActionExecute:
if code < 128 && p.executeHandlers[code] != nil {
p.executeHandlers[code]()
} else {
p.executeHandlerFb(code)
}
p.precedingJoinState = 0
case ParserActionIgnore:
// do nothing
case ParserActionError:
inject := p.errorHandler(ParsingState{
Position: i,
Code: code,
CurrentState: p.currentState,
Collect: p.collect,
Params: p.params,
Abort: false,
})
if inject.Abort {
return
}
case ParserActionCSIDispatch:
// Dispatch to CSI handlers
ident := p.collect<<8 | int(code)
handlers := p.csiHandlers[ident]
j := len(handlers) - 1
for ; j >= 0; j-- {
if handlers[j](p.params) {
break
}
}
if j < 0 {
p.csiHandlerFb(ident, p.params)
}
p.precedingJoinState = 0
case ParserActionParam:
// Inner loop: digits (0x30-0x39), ';' (0x3b), ':' (0x3a)
for {
switch code {
case 0x3b:
p.params.AddParam(0) // ZDM
case 0x3a:
p.params.AddSubParam(-1)
default: // 0x30-0x39
p.params.AddDigit(int32(code) - 48)
}
i++
if i >= length {
break
}
code = data[i]
if code < 0x30 || code > 0x3b {
break
}
}
i--
case ParserActionCollect:
p.collect = p.collect<<8 | int(code)
case ParserActionESCDispatch:
ident := p.collect<<8 | int(code)
handlers := p.escHandlers[ident]
j := len(handlers) - 1
for ; j >= 0; j-- {
if handlers[j]() {
break
}
}
if j < 0 {
p.escHandlerFb(ident)
}
p.precedingJoinState = 0
case ParserActionClear:
p.params.ResetZdm()
p.collect = 0
case ParserActionDCSHook:
p.dcsParser.Hook(p.collect<<8|int(code), p.params)
case ParserActionDCSPut:
// Inner loop: exit on 0x18, 0x1a, 0x1b, 0x7f, 0x80-0x9f
j := i + 1
for ; j < length; j++ {
code = data[j]
if code == 0x18 || code == 0x1a || code == 0x1b || (code > 0x7f && code < nonASCIIPrintable) {
break
}
}
p.dcsParser.Put(data, i, j)
i = j - 1
case ParserActionDCSUnhook:
p.dcsParser.Unhook(code != 0x18 && code != 0x1a)
if code == 0x1b {
transition |= uint16(ParserStateEscape)
}
p.params.ResetZdm()
p.collect = 0
p.precedingJoinState = 0
case ParserActionOSCStart:
p.oscParser.Start()
case ParserActionOSCPut:
// Inner loop: 0x20 (SP) included, 0x7F (DEL) included
j := i + 1
for ; j < length; j++ {
code = data[j]
if code < 0x20 || (code > 0x7f && code < nonASCIIPrintable) {
break
}
}
p.oscParser.Put(data, i, j)
i = j - 1
case ParserActionOSCEnd:
p.oscParser.End(code != 0x18 && code != 0x1a)
if code == 0x1b {
transition |= uint16(ParserStateEscape)
}
p.params.ResetZdm()
p.collect = 0
p.precedingJoinState = 0
case ParserActionAPCStart:
p.apcParser.Start(p.collect<<8 | int(code))
case ParserActionAPCPut:
// Inner loop: allow 0x08-0x0d, 0x20-0x7e, non-ASCII printable
j := i + 1
for ; j < length; j++ {
code = data[j]
if (code >= 0x20 && code < 0x7f) || (code >= 0x08 && code < 0x0e) || code >= nonASCIIPrintable {
continue
}
break
}
p.apcParser.Put(data, i, j)
i = j - 1
case ParserActionAPCEnd:
p.apcParser.End(code != 0x18 && code != 0x1a)
if code == 0x1b {
transition |= uint16(ParserStateEscape)
}
p.params.ResetZdm()
p.collect = 0
p.precedingJoinState = 0
}
p.currentState = ParserState(transition & tableTransitionStateMask)
}
}