-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
626 lines (593 loc) · 13.9 KB
/
parse.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
package inputrc
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
// Parser is a inputrc parser.
type Parser struct {
haltOnErr bool
strict bool
name string
app string
term string
mode string
keymap string
line int
conds []bool
errs []error
}
// New creates a new inputrc parser.
func New(opts ...Option) *Parser {
// build parser state
p := &Parser{
line: 1,
}
for _, o := range opts {
o(p)
}
return p
}
// Parse parses inputrc data from the reader, passing sets and binding keys to
// h based on the configured options.
func (p *Parser) Parse(r io.Reader, h Handler) error {
var err error
// reset parser state
p.keymap, p.line, p.conds, p.errs = "emacs", 1, append(p.conds[:0], true), p.errs[:0]
// scan file by lines
var line []rune
var i, end int
s := bufio.NewScanner(r)
for ; s.Scan(); p.line++ {
line = []rune(s.Text())
end = len(line)
if i = findNonSpace(line, 0, end); i == end {
continue
}
// skip blank/comment
switch line[i] {
case 0, '\r', '\n', '#':
continue
}
// next
if err = p.next(h, line, i, end); err != nil {
p.errs = append(p.errs, err)
if p.haltOnErr {
return err
}
}
}
if err = s.Err(); err != nil {
p.errs = append(p.errs, err)
return err
}
return nil
}
// Errs returns the parse errors encountered.
func (p *Parser) Errs() []error {
return p.errs
}
// next handles the next statement.
func (p *Parser) next(h Handler, r []rune, i, end int) error {
a, b, tok, err := p.readNext(r, i, end)
if err != nil {
return err
}
switch tok {
case tokenBind, tokenBindMacro:
return p.doBind(h, a, b, tok == tokenBindMacro)
case tokenSet:
return p.doSet(h, a, b)
case tokenConstruct:
return p.do(h, a, b)
}
return nil
}
// readNext reads the next statement.
func (p *Parser) readNext(r []rune, i, end int) (string, string, token, error) {
i = findNonSpace(r, i, end)
switch {
case r[i] == 's' && grab(r, i+1, end) == 'e' && grab(r, i+2, end) == 't' && unicode.IsSpace(grab(r, i+3, end)):
// read set
return p.readSymbols(r, i+4, end, tokenSet, true)
case r[i] == '$':
// read construct
return p.readSymbols(r, i, end, tokenConstruct, false)
}
// read key seq
var seq string
if r[i] == '"' || r[i] == '\'' {
start, ok := i, false
if i, ok = findStringEnd(r, i, end); !ok {
return "", "", tokenNone, &ParseError{
Name: p.name,
Line: p.line,
Text: string(r[start:]),
Err: ErrBindMissingClosingQuote,
}
}
seq = unescapeRunes(r, start+1, i-1)
} else {
var err error
if seq, i, err = decodeKey(r, i, end); err != nil {
return "", "", tokenNone, &ParseError{
Name: p.name,
Line: p.line,
Text: string(r),
Err: err,
}
}
}
// NOTE: this is technically different than the actual readline
// implementation, as it doesn't allow whitespace, but silently fails (ie
// does not bind a key) if a space follows the key declaration. made a
// decision to instead return an error if the : is missing in all cases.
// seek :
for ; i < end && r[i] != ':'; i++ {
}
if i == end || r[i] != ':' {
return "", "", tokenNone, &ParseError{
Name: p.name,
Line: p.line,
Text: string(r),
Err: ErrMissingColon,
}
}
// seek non space
if i = findNonSpace(r, i+1, end); i == end || r[i] == '#' {
return seq, "", tokenNone, nil
}
// seek
if r[i] == '"' || r[i] == '\'' {
start, ok := i, false
if i, ok = findStringEnd(r, i, end); !ok {
return "", "", tokenNone, &ParseError{
Name: p.name,
Line: p.line,
Text: string(r[start:]),
Err: ErrMacroMissingClosingQuote,
}
}
return seq, unescapeRunes(r, start+1, i-1), tokenBindMacro, nil
}
return seq, string(r[i:findEnd(r, i, end)]), tokenBind, nil
}
// readSymbols reads the next two symbols.
func (p *Parser) readSymbols(r []rune, i, end int, tok token, allowStrings bool) (string, string, token, error) {
start := findNonSpace(r, i, end)
i = findEnd(r, start, end)
a := string(r[start:i])
start = findNonSpace(r, i, end)
var ok bool
if c := grab(r, start, end); allowStrings || c == '"' || c == '\'' {
var pos int
if pos, ok = findStringEnd(r, start, end); ok {
i = pos
}
}
if !allowStrings || !ok {
i = findEnd(r, start, end)
}
return a, string(r[start:i]), tok, nil
}
// doBind handles a bind.
func (p *Parser) doBind(h Handler, sequence, action string, macro bool) error {
if !p.conds[len(p.conds)-1] {
return nil
}
return h.Bind(p.keymap, sequence, action, macro)
}
// doSet handles a set.
func (p *Parser) doSet(h Handler, name, value string) error {
if !p.conds[len(p.conds)-1] {
return nil
}
switch name {
case "keymap":
if p.strict {
switch value {
// see: man readline
// see: https://unix.stackexchange.com/questions/303479/what-are-readlines-modes-keymaps-and-their-default-bindings
case "emacs", "emacs-standard", "emacs-meta", "emacs-ctlx",
"vi", "vi-move", "vi-command", "vi-insert":
default:
return &ParseError{
Name: p.name,
Line: p.line,
Text: value,
Err: ErrInvalidKeymap,
}
}
}
p.keymap = value
return nil
case "editing-mode":
switch value {
case "emacs", "vi":
default:
return &ParseError{
Name: p.name,
Line: p.line,
Text: value,
Err: ErrInvalidEditingMode,
}
}
return h.Set(name, value)
}
if v := h.Get(name); v != nil {
// defined in vars, so pass to set only as that type
var z interface{}
switch v.(type) {
case bool:
z = strings.ToLower(value) == "on" || value == "1"
case string:
z = value
case int:
i, err := strconv.Atoi(value)
if err != nil {
return err
}
z = i
default:
panic(fmt.Sprintf("unsupported type %T", v))
}
return h.Set(name, z)
}
// not set, so try to convert to usable value
if i, err := strconv.Atoi(value); err == nil {
return h.Set(name, i)
}
switch strings.ToLower(value) {
case "off":
return h.Set(name, false)
case "on":
return h.Set(name, true)
}
return h.Set(name, value)
}
// do handles a construct.
func (p *Parser) do(h Handler, a, b string) error {
switch a {
case "$if":
var eval bool
switch {
case strings.HasPrefix(b, "mode="):
eval = strings.TrimPrefix(b, "mode=") == p.mode
case strings.HasPrefix(b, "term="):
eval = strings.TrimPrefix(b, "term=") == p.term
default:
eval = strings.ToLower(b) == p.app
}
p.conds = append(p.conds, eval)
return nil
case "$else":
if len(p.conds) == 1 {
return &ParseError{
Name: p.name,
Line: p.line,
Text: "$else",
Err: ErrElseWithoutMatchingIf,
}
}
p.conds[len(p.conds)-1] = !p.conds[len(p.conds)-1]
return nil
case "$endif":
if len(p.conds) == 1 {
return &ParseError{
Name: p.name,
Line: p.line,
Text: "$endif",
Err: ErrEndifWithoutMatchingIf,
}
}
p.conds = p.conds[:len(p.conds)-1]
return nil
case "$include":
if !p.conds[len(p.conds)-1] {
return nil
}
buf, err := h.ReadFile(b)
switch {
case err != nil && errors.Is(err, os.ErrNotExist):
return nil
case err != nil:
return err
}
return Parse(bytes.NewReader(buf), h, WithName(b), WithApp(p.app), WithTerm(p.term), WithMode(p.mode))
}
if !p.conds[len(p.conds)-1] {
return nil
}
// delegate unknown construct
if err := h.Do(a, b); err != nil {
return &ParseError{
Name: p.name,
Line: p.line,
Text: a + " " + b,
Err: err,
}
}
return nil
}
// Option is a parser option.
type Option func(*Parser)
// WithHaltOnErr is a parser option to halt on every encountered error.
func WithHaltOnErr(haltOnErr bool) Option {
return func(p *Parser) {
p.haltOnErr = haltOnErr
}
}
// WithStrict is a parser option to set strict keymap parsing.
func WithStrict(strict bool) Option {
return func(p *Parser) {
p.strict = strict
}
}
// WithName is a parser option to set the file name.
func WithName(name string) Option {
return func(p *Parser) {
p.name = name
}
}
// WithApp is a parser option to set the app name.
func WithApp(app string) Option {
return func(p *Parser) {
p.app = app
}
}
// WithTerm is a parser option to set the term name.
func WithTerm(term string) Option {
return func(p *Parser) {
p.term = term
}
}
// WithMode is a parser option to set the mode name.
func WithMode(mode string) Option {
return func(p *Parser) {
p.mode = mode
}
}
// ParseError is a parse error.
type ParseError struct {
Name string
Line int
Text string
Err error
}
// Error satisfies the error interface.
func (err *ParseError) Error() string {
var s string
if err.Name != "" {
s = " " + err.Name + ":"
}
return fmt.Sprintf("inputrc:%s line %d: %s: %v", s, err.Line, err.Text, err.Err)
}
// Unwrap satisfies the errors.Unwrap call.
func (err *ParseError) Unwrap() error {
return err.Err
}
// token is a inputrc line token.
type token int
// inputrc line tokens.
const (
tokenNone token = iota
tokenBind
tokenBindMacro
tokenSet
tokenConstruct
)
// String satisfies the fmt.Stringer interface.
func (tok token) String() string {
switch tok {
case tokenNone:
return "none"
case tokenBind:
return "bind"
case tokenBindMacro:
return "bind-macro"
case tokenSet:
return "set"
case tokenConstruct:
return "construct"
}
return fmt.Sprintf("token(%d)", tok)
}
// findNonSpace finds first non space rune in r, returning end if not found.
func findNonSpace(r []rune, i, end int) int {
for ; i < end && unicode.IsSpace(r[i]); i++ {
}
return i
}
// findEnd finds end of the current symbol (position of next #, space, or line
// end), returning end if not found.
func findEnd(r []rune, i, end int) int {
for c := grab(r, i+1, end); i < end && c != '#' && !unicode.IsSpace(c) && !unicode.IsControl(c); i++ {
c = grab(r, i+1, end)
}
return i
}
// findStringEnd finds end of the string, returning end if not found.
func findStringEnd(r []rune, i, end int) (int, bool) {
quote, c := r[i], rune(0)
for i++; i < end; i++ {
switch c = r[i]; {
case c == '\\':
i++
continue
case c == quote:
return i + 1, true
}
}
return i, false
}
// grab returns r[i] when i < end, 0 otherwise.
func grab(r []rune, i, end int) rune {
if i < end {
return r[i]
}
return 0
}
// decodeKey decodes named key sequence.
func decodeKey(r []rune, i, end int) (string, int, error) {
// seek end of sequence
start := i
for c := grab(r, i+1, end); i < end && c != ':' && c != '#' && !unicode.IsSpace(c) && !unicode.IsControl(c); i++ {
c = grab(r, i+1, end)
}
s := strings.ToLower(string(r[start:i]))
meta, control := false, false
for i := strings.Index(s, "-"); i != -1; i = strings.Index(s, "-") {
switch s[:i] {
case "control", "ctrl", "c":
control = true
case "meta", "m":
meta = true
default:
return "", i, ErrUnknownModifier
}
s = s[i+1:]
}
var c rune
switch s {
case "":
return "", i, nil
case "delete", "del", "rubout":
c = Delete
case "escape", "esc":
c = Esc
case "newline", "linefeed", "lfd":
c = Newline
case "return", "ret":
c = Return
case "tab":
c = Tab
case "space", "spc":
c = Space
case "formfeed", "ffd":
c = Formfeed
case "vertical", "vrt":
c = Vertical
default:
c, _ = utf8.DecodeRuneInString(s)
}
switch {
case control && meta:
return string([]rune{Esc, Encontrol(c)}), i, nil
case control:
c = Encontrol(c)
case meta:
c = Enmeta(c)
}
return string(c), i, nil
}
// unescapeRunes decodes escaped string sequence.
func unescapeRunes(r []rune, i, end int) string {
if end-i <= 1 {
return string(r)
}
var s []rune
var c0, c1, c2, c3, c4, c5 rune
for ; i < end; i++ {
if c0 = r[i]; c0 == '\\' {
c1, c2, c3, c4, c5 = grab(r, i+1, end), grab(r, i+2, end), grab(r, i+3, end), grab(r, i+4, end), grab(r, i+5, end)
switch {
case c1 == 'a': // \a alert (bell)
s = append(s, Alert)
i += 1
case c1 == 'b': // \b backspace
s = append(s, Backspace)
i += 1
case c1 == 'd': // \d delete
s = append(s, Delete)
i += 1
case c1 == 'e': // \e escape
s = append(s, Esc)
i += 1
case c1 == 'f': // \f form feed
s = append(s, Formfeed)
i += 1
case c1 == 'n': // \n new line
s = append(s, Newline)
i += 1
case c1 == 'r': // \r carriage return
s = append(s, Return)
i += 1
case c1 == 't': // \t tab
s = append(s, Tab)
i += 1
case c1 == 'v': // \v vertical
s = append(s, Vertical)
i += 1
case c1 == '\\', c1 == '"', c1 == '\'': // \\ \" \' literal
s = append(s, c1)
i += 1
case c1 == 'x' && hexDigit(c2) && hexDigit(c3): // \xHH hex
s = append(s, hexVal(c2)<<4|hexVal(c3))
i += 2
case c1 == 'x' && hexDigit(c2): // \xH hex
s = append(s, hexVal(c2))
i += 1
case octDigit(c1) && octDigit(c2) && octDigit(c3): // \nnn octal
s = append(s, (c1-'0')<<6|(c2-'0')<<3|(c3-'0'))
i += 3
case octDigit(c1) && octDigit(c2): // \nn octal
s = append(s, (c1-'0')<<3|(c2-'0'))
i += 2
case octDigit(c1): // \n octal
s = append(s, c1-'0')
i += 1
case ((c1 == 'C' && c4 == 'M') || (c1 == 'M' && c4 == 'C')) && c2 == '-' && c3 == '\\' && c5 == '-':
// \C-\M- or \M-\C- control meta prefix
if c6 := grab(r, i+6, end); c6 != 0 {
s = append(s, Esc, Encontrol(c6))
i += 6
} else {
i += 5
}
case c1 == 'C' && c2 == '-' && c3 == '?': // \C-? control + ?
s = append(s, Delete)
i += 3
case c1 == 'C' && c2 == '-': // \C- control prefix
s = append(s, Encontrol(c3))
i += 3
case c1 == 'M' && c2 == '-' && c3 == 0: // \M- isolated meta
s = append(s, Esc)
i += 2
case c1 == 'M' && c2 == '-': // \M- meta prefix
s = append(s, Enmeta(c3))
i += 3
default:
s = append(s, c1)
i += 1
}
continue
}
s = append(s, c0)
}
return string(s)
}
// octDigit returns true when r is 0-7.
func octDigit(r rune) bool {
return '0' <= r && r <= '7'
}
// hexDigit returns true when r is 0-9A-Fa-f.
func hexDigit(r rune) bool {
return '0' <= r && r <= '9' || 'A' <= r && r <= 'F' || 'a' <= r && r <= 'f'
}
// hexVal converts a rune to its hex value.
func hexVal(r rune) rune {
switch {
case 'a' <= r && r <= 'f':
return r - 'a' + 10
case 'A' <= r && r <= 'F':
return r - 'A' + 10
}
return r - '0'
}