forked from rgburke/grv
/
config_parse.go
811 lines (670 loc) · 22.8 KB
/
config_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
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
package main
import (
"bytes"
"errors"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"unicode"
slice "github.com/bradfitz/slice"
)
const (
setCommand = "set"
themeCommand = "theme"
mapCommand = "map"
unmapCommand = "unmap"
quitCommand = "q"
addtabCommand = "addtab"
removetabCommand = "rmtab"
addviewCommand = "addview"
vsplitCommand = "vsplit"
hsplitCommand = "hsplit"
splitCommand = "split"
gitCommand = "git"
gitInteractiveCommand = "giti"
helpCommand = "help"
defCommand = "def"
undefCommand = "undef"
evalkeysCommand = "evalkeys"
sleepCommand = "sleep"
)
const (
openingBrace = "{"
closingBrace = "}"
)
var isIdentifier = regexp.MustCompile(`[[:alnum:]]+`).MatchString
var commentTokens = map[ConfigTokenType]bool{
CtkComment: true,
}
var whiteSpaceTokens = map[ConfigTokenType]bool{
CtkWhiteSpace: true,
CtkComment: true,
}
var whiteSpaceAndTerminatorTokens = map[ConfigTokenType]bool{
CtkWhiteSpace: true,
CtkComment: true,
CtkTerminator: true,
}
type commandConstructor func(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error)
// ConfigCommand represents a config command
type ConfigCommand interface {
configCommand()
}
// SetCommand contains state for setting a config variable to a value
type SetCommand struct {
variable *ConfigToken
value *ConfigToken
}
func (setCommand *SetCommand) configCommand() {}
// ThemeCommand contains state for setting a components values for on a theme
type ThemeCommand struct {
name *ConfigToken
component *ConfigToken
bgcolor *ConfigToken
fgcolor *ConfigToken
}
func (themeCommand *ThemeCommand) configCommand() {}
// MapCommand contains state for mapping a key sequence to another
type MapCommand struct {
view *ConfigToken
from *ConfigToken
to *ConfigToken
}
func (mapCommand *MapCommand) configCommand() {}
// UnmapCommand contains state for unmapping a key sequence
type UnmapCommand struct {
view *ConfigToken
from *ConfigToken
}
func (unmapCommand *UnmapCommand) configCommand() {}
// QuitCommand represents the command to quit grv
type QuitCommand struct{}
func (quitCommand *QuitCommand) configCommand() {}
// NewTabCommand represents the command to create a new tab
type NewTabCommand struct {
tabName *ConfigToken
}
func (newTabCommand *NewTabCommand) configCommand() {}
// RemoveTabCommand represents the command to remove the currently active tab
type RemoveTabCommand struct{}
func (removeTabCommand *RemoveTabCommand) configCommand() {}
// AddViewCommand represents the command to add a new view
// to the currently active view
type AddViewCommand struct {
view *ConfigToken
args []*ConfigToken
}
func (addViewCommand *AddViewCommand) configCommand() {}
// SplitViewCommand represents the command split the currently
// active view with a new view
type SplitViewCommand struct {
orientation ContainerOrientation
view *ConfigToken
args []*ConfigToken
}
func (splitViewCommand *SplitViewCommand) configCommand() {}
// GitCommand represents a git command
type GitCommand struct {
interactive bool
args []*ConfigToken
}
func (gitCommand *GitCommand) configCommand() {}
// HelpCommand represents the command to show the help view
type HelpCommand struct {
searchTerm string
}
func (helpCommand *HelpCommand) configCommand() {}
// ShellCommand represents a shell command
type ShellCommand struct {
command *ConfigToken
}
func (shellCommand *ShellCommand) configCommand() {}
// DefCommand represents a function definition command
type DefCommand struct {
commandName string
functionBody string
}
func (defCommand *DefCommand) configCommand() {}
// UndefCommand represents the command to undefine a command
type UndefCommand struct {
commandName *ConfigToken
}
func (undefCommand *UndefCommand) configCommand() {}
// CustomCommand represents an invocation of a user defined command
type CustomCommand struct {
commandName string
args []string
}
func (customCommand *CustomCommand) configCommand() {}
// EvalKeysCommand represents a key evaluation command
type EvalKeysCommand struct {
keys string
}
func (evalKeysCommand *EvalKeysCommand) configCommand() {}
// SleepCommand represents a command to sleep
type SleepCommand struct {
sleepSeconds float64
}
func (sleepCommand *SleepCommand) configCommand() {}
type commandHelpGenerator func(config Config) []*HelpSection
type commandCustomParser func(parser *ConfigParser) (tokens []*ConfigToken, err error)
type commandDescriptor struct {
tokenTypes []ConfigTokenType
constructor commandConstructor
commandHelpGenerator commandHelpGenerator
customParser commandCustomParser
userDefined bool
}
// DefineCustomCommand allows a custom command to be parsed
func DefineCustomCommand(commandName string) (err error) {
if existingDescriptor, ok := commandDescriptors[commandName]; ok && !existingDescriptor.userDefined {
return fmt.Errorf("Cannot override built in command %v", commandName)
}
commandDescriptors[commandName] = &commandDescriptor{
customParser: parseVarArgsCommand(),
constructor: customCommandConstructor,
userDefined: true,
}
return
}
// UndefineCustomCommand invalidates a custom command
func UndefineCustomCommand(commandName string) (err error) {
if existingDescriptor, ok := commandDescriptors[commandName]; ok && !existingDescriptor.userDefined {
return fmt.Errorf("Cannot undefine built in command %v", commandName)
}
delete(commandDescriptors, commandName)
return
}
var commandDescriptors = map[string]*commandDescriptor{
setCommand: {
tokenTypes: []ConfigTokenType{CtkWord, CtkWord},
constructor: setCommandConstructor,
commandHelpGenerator: GenerateSetCommandHelpSections,
},
themeCommand: {
tokenTypes: []ConfigTokenType{CtkOption, CtkWord, CtkOption, CtkWord, CtkOption, CtkWord, CtkOption, CtkWord},
constructor: themeCommandConstructor,
commandHelpGenerator: GenerateThemeCommandHelpSections,
},
mapCommand: {
tokenTypes: []ConfigTokenType{CtkWord, CtkWord, CtkWord | CtkShellCommand},
constructor: mapCommandConstructor,
commandHelpGenerator: GenerateMapCommandHelpSections,
},
unmapCommand: {
tokenTypes: []ConfigTokenType{CtkWord, CtkWord},
constructor: unmapCommandConstructor,
commandHelpGenerator: GenerateUnmapCommandHelpSections,
},
quitCommand: {
constructor: quitCommandConstructor,
commandHelpGenerator: GenerateQuitCommandHelpSections,
},
addtabCommand: {
tokenTypes: []ConfigTokenType{CtkWord},
constructor: newTabCommandConstructor,
commandHelpGenerator: GenerateAddTabCommandHelpSections,
},
removetabCommand: {
constructor: newRemoveTabCommandConstructor,
commandHelpGenerator: GenerateRmTabCommandHelpSections,
},
addviewCommand: {
customParser: parseVarArgsCommand(),
constructor: addViewCommandConstructor,
commandHelpGenerator: GenerateAddViewCommandHelpSections,
},
vsplitCommand: {
customParser: parseVarArgsCommand(),
constructor: splitViewCommandConstructor,
commandHelpGenerator: GenerateVSplitCommandHelpSections,
},
hsplitCommand: {
customParser: parseVarArgsCommand(),
constructor: splitViewCommandConstructor,
commandHelpGenerator: GenerateHSplitCommandHelpSections,
},
splitCommand: {
customParser: parseVarArgsCommand(),
constructor: splitViewCommandConstructor,
commandHelpGenerator: GenerateSplitCommandHelpSections,
},
gitCommand: {
customParser: parseVarArgsCommand(),
constructor: gitCommandConstructor,
commandHelpGenerator: GenerateGitCommandHelpSections,
},
gitInteractiveCommand: {
customParser: parseVarArgsCommand(),
constructor: gitCommandConstructor,
commandHelpGenerator: GenerateGitiCommandHelpSections,
},
helpCommand: {
customParser: parseVarArgsCommand(),
constructor: helpCommandConstructor,
commandHelpGenerator: GenerateHelpCommandHelpSections,
},
defCommand: {
customParser: parseDefCommand,
constructor: defCommandConstructor,
commandHelpGenerator: GenerateDefCommandHelpSections,
},
undefCommand: {
tokenTypes: []ConfigTokenType{CtkWord},
constructor: undefCommandConstructor,
commandHelpGenerator: GenerateUndefCommandHelpSections,
},
evalkeysCommand: {
customParser: parseVarArgsParserGenerator(false),
constructor: evalKeysCommandConstructor,
commandHelpGenerator: GenerateEvalKeysCommandHelpSections,
},
sleepCommand: {
tokenTypes: []ConfigTokenType{CtkWord},
constructor: sleepCommandConstructor,
commandHelpGenerator: GenerateSleepCommandHelpSections,
},
}
// GenerateConfigCommandHelpSections generates help documentation for all configuration commands
func GenerateConfigCommandHelpSections(config Config) (helpSections []*HelpSection) {
helpSections = append(helpSections, &HelpSection{
title: HelpSectionText{text: "Configuration Commands"},
description: []HelpSectionText{
{text: "The behaviour of GRV can be customised through the use of commands specified in a configuration file"},
{text: "GRV will look for the following configuration files on start up:"},
{},
{text: " - $XDG_CONFIG_HOME/grv/grvrc", themeComponentID: CmpHelpViewSectionCodeBlock},
{text: " - $HOME/.config/grv/grvrc", themeComponentID: CmpHelpViewSectionCodeBlock},
{},
{text: "GRV will attempt to process the first file which exists."},
{text: "Commands can also be specified within GRV using the command prompt :"},
{},
{text: "Below are the set of configuration commands supported:"},
},
})
commands := []string{}
for command := range commandDescriptors {
commands = append(commands, command)
}
slice.Sort(commands, func(i, j int) bool {
return commands[i] < commands[j]
})
for _, command := range commands {
commandDescriptor := commandDescriptors[command]
if commandDescriptor.commandHelpGenerator != nil {
helpSections = append(helpSections, commandDescriptor.commandHelpGenerator(config)...)
}
}
return
}
// ConfigParser is a component capable of parsing config into commands
type ConfigParser struct {
scanner *ConfigScanner
inputSource string
}
// NewConfigParser creates a new ConfigParser which will read input from the provided reader
func NewConfigParser(reader io.Reader, inputSource string) *ConfigParser {
return &ConfigParser{
scanner: NewConfigScanner(reader),
inputSource: inputSource,
}
}
// Parse returns the next command from the input stream
// eof is set to true if the end of the input stream has been reached
func (parser *ConfigParser) Parse() (command ConfigCommand, eof bool, err error) {
var token *ConfigToken
for {
token, err = parser.scan()
if err != nil {
return
}
switch token.tokenType {
case CtkWord:
command, eof, err = parser.parseCommand(token)
case CtkShellCommand:
command = parser.shellCommand(token)
case CtkTerminator:
continue
case CtkEOF:
eof = true
case CtkOption:
err = parser.generateParseError(token, "Unexpected Option \"%v\"", token.value)
case CtkInvalid:
err = parser.generateParseError(token, "Syntax Error")
default:
err = parser.generateParseError(token, "Unexpected token \"%v\"", token.value)
}
break
}
if err != nil {
parser.discardTokensUntilNextCommand()
}
return
}
// InputSource returns the text description of the input source
func (parser *ConfigParser) InputSource() string {
return parser.inputSource
}
func (parser *ConfigParser) scanAndIgnore(ignoreTokens map[ConfigTokenType]bool) (token *ConfigToken, err error) {
for {
token, err = parser.scanner.Scan()
if err != nil {
return
} else if _, ignore := ignoreTokens[token.tokenType]; !ignore {
break
}
}
return
}
func (parser *ConfigParser) scan() (token *ConfigToken, err error) {
return parser.scanAndIgnore(whiteSpaceTokens)
}
func (parser *ConfigParser) scanIgnoringTerminators() (token *ConfigToken, err error) {
return parser.scanAndIgnore(whiteSpaceAndTerminatorTokens)
}
func (parser *ConfigParser) scanRaw() (token *ConfigToken, err error) {
return parser.scanner.Scan()
}
func (parser *ConfigParser) generateParseError(token *ConfigToken, errorMessage string, args ...interface{}) error {
return generateConfigError(parser.inputSource, token, errorMessage, args...)
}
func generateConfigError(inputSource string, token *ConfigToken, errorMessage string, args ...interface{}) error {
var buffer bytes.Buffer
if inputSource != "" {
buffer.WriteString(inputSource)
buffer.WriteRune(':')
buffer.WriteString(fmt.Sprintf("%v:%v ", token.startPos.line, token.startPos.col))
}
buffer.WriteString(fmt.Sprintf(errorMessage, args...))
if token.err != nil {
buffer.WriteString(": ")
buffer.WriteString(token.err.Error())
}
return errors.New(buffer.String())
}
func (parser *ConfigParser) discardTokensUntilNextCommand() {
for {
token, err := parser.scan()
if err != nil ||
token.tokenType == CtkTerminator ||
token.tokenType == CtkEOF {
return
}
}
}
func (parser *ConfigParser) parseCommand(commandToken *ConfigToken) (command ConfigCommand, eof bool, err error) {
commandDescriptor, ok := commandDescriptors[commandToken.value]
if !ok {
err = parser.generateParseError(commandToken, "Invalid command \"%v\"", commandToken.value)
return
}
var tokens []*ConfigToken
if commandDescriptor.customParser != nil {
if tokens, err = commandDescriptor.customParser(parser); err != nil {
return
}
} else {
for i := 0; i < len(commandDescriptor.tokenTypes); i++ {
var token *ConfigToken
token, err = parser.scan()
expectedConfigTokenType := commandDescriptor.tokenTypes[i]
switch {
case err != nil:
return
case token.err != nil:
err = parser.generateParseError(token, "Syntax Error when parsing %v command", commandToken.value)
return
case token.tokenType == CtkEOF:
err = parser.generateParseError(token, "Unexpected EOF when parsing %v command", commandToken.value)
eof = true
return
case (token.tokenType & expectedConfigTokenType) == 0:
err = parser.generateParseError(token, "Invalid argument for %v command: Expected %v but got %v: \"%v\"",
commandToken.value, ConfigTokenName(expectedConfigTokenType), ConfigTokenName(token.tokenType), token.value)
return
}
tokens = append(tokens, token)
}
}
command, err = commandDescriptor.constructor(parser, commandToken, tokens)
return
}
func parseVarArgsCommand() commandCustomParser {
return parseVarArgsParserGenerator(true)
}
func parseVarArgsParserGenerator(ignoreWhitespace bool) commandCustomParser {
var ignoreTokens map[ConfigTokenType]bool
if ignoreWhitespace {
ignoreTokens = whiteSpaceTokens
} else {
ignoreTokens = commentTokens
}
return func(parser *ConfigParser) (tokens []*ConfigToken, err error) {
OuterLoop:
for {
var token *ConfigToken
token, err = parser.scanAndIgnore(ignoreTokens)
switch {
case err != nil:
return
case token.err != nil:
err = parser.generateParseError(token, "Syntax Error")
return
case token.tokenType == CtkEOF:
break OuterLoop
case token.tokenType == CtkTerminator:
break OuterLoop
}
tokens = append(tokens, token)
}
return
}
}
func parseDefCommand(parser *ConfigParser) (tokens []*ConfigToken, err error) {
commandNameToken, err := parser.scanIgnoringTerminators()
if err != nil {
return
} else if err = commandNameToken.err; err != nil {
return
} else if commandNameToken.tokenType != CtkWord {
return tokens, parser.generateParseError(commandNameToken, "Expected function name but found %v", commandNameToken.value)
} else if !isIdentifier(commandNameToken.value) {
return tokens, parser.generateParseError(commandNameToken, "Invalid function identifier %v", commandNameToken.value)
}
tokens = append(tokens, commandNameToken)
openingBraceToken, err := parser.scanIgnoringTerminators()
if err != nil {
return
} else if err = openingBraceToken.err; err != nil {
return
} else if openingBraceToken.tokenType != CtkWord || openingBraceToken.rawValue != openingBrace {
return tokens, parser.generateParseError(openingBraceToken, "Expected %v but found %v", openingBrace, openingBraceToken.value)
}
tokens = append(tokens, openingBraceToken)
closingBracesRemaining := 1
wordsSinceTerminator := 0
for closingBracesRemaining > 0 {
var token *ConfigToken
if token, err = parser.scanRaw(); err != nil {
return
} else if err = token.err; err != nil {
return
}
tokens = append(tokens, token)
switch token.tokenType {
case CtkEOF:
return nil, parser.generateParseError(token, "Expected %v but reached EOF", closingBrace)
case CtkWord:
if token.value == defCommand && wordsSinceTerminator == 0 {
closingBracesRemaining++
} else if token.rawValue == closingBrace {
closingBracesRemaining--
}
wordsSinceTerminator++
case CtkTerminator:
wordsSinceTerminator = 0
}
}
return
}
func (parser *ConfigParser) shellCommand(commandToken *ConfigToken) *ShellCommand {
return &ShellCommand{
command: commandToken,
}
}
func setCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error) {
return &SetCommand{
variable: tokens[0],
value: tokens[1],
}, nil
}
func themeCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error) {
themeCommand := &ThemeCommand{}
optionSetters := map[string]func(*ConfigToken){
"--name": func(name *ConfigToken) { themeCommand.name = name },
"--component": func(component *ConfigToken) { themeCommand.component = component },
"--bgcolor": func(bgcolor *ConfigToken) { themeCommand.bgcolor = bgcolor },
"--fgcolor": func(fgcolor *ConfigToken) { themeCommand.fgcolor = fgcolor },
}
for i := 0; i+1 < len(tokens); i += 2 {
optionToken := tokens[i]
valueToken := tokens[i+1]
optionSetter, ok := optionSetters[optionToken.value]
if !ok {
return nil, parser.generateParseError(optionToken, "Invalid option for theme command: \"%v\"", optionToken.value)
}
optionSetter(valueToken)
}
return themeCommand, nil
}
func mapCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error) {
return &MapCommand{
view: tokens[0],
from: tokens[1],
to: tokens[2],
}, nil
}
func unmapCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error) {
return &UnmapCommand{
view: tokens[0],
from: tokens[1],
}, nil
}
func quitCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error) {
return &QuitCommand{}, nil
}
func newTabCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error) {
return &NewTabCommand{
tabName: tokens[0],
}, nil
}
func newRemoveTabCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error) {
return &RemoveTabCommand{}, nil
}
func addViewCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error) {
if len(tokens) < 1 {
addViewCommand := commandToken.value
return nil, parser.generateParseError(commandToken, "Invalid %[1]v command. Usage: %[1]v [VIEW] [ARGS...]", addViewCommand)
}
return &AddViewCommand{
view: tokens[0],
args: tokens[1:],
}, nil
}
func splitViewCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error) {
splitViewCommand := commandToken.value
if len(tokens) < 1 {
return nil, parser.generateParseError(commandToken, "Invalid %[1]v command. Usage: %[1]v [VIEW] [ARGS...]", splitViewCommand)
}
var orientation ContainerOrientation
switch splitViewCommand {
case splitCommand:
orientation = CoDynamic
case hsplitCommand:
orientation = CoHorizontal
case vsplitCommand:
orientation = CoVertical
default:
return nil, parser.generateParseError(commandToken, "Unrecognised command: %v", splitViewCommand)
}
return &SplitViewCommand{
orientation: orientation,
view: tokens[0],
args: tokens[1:],
}, nil
}
func gitCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error) {
return &GitCommand{
interactive: commandToken.value == gitInteractiveCommand,
args: tokens,
}, nil
}
func helpCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error) {
var searchTerm string
if len(tokens) > 0 {
searchTerm = tokens[0].value
}
return &HelpCommand{
searchTerm: searchTerm,
}, nil
}
func defCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (configCommand ConfigCommand, err error) {
if len(tokens) < 4 {
err = parser.generateParseError(commandToken, "Too few tokens (%v) for function definition", len(tokens))
return
}
commandName := tokens[0].value
var functionBodyBuffer bytes.Buffer
for i := 2; i < len(tokens)-1; i++ {
functionBodyBuffer.WriteString(tokens[i].rawValue)
}
functionBody := functionBodyBuffer.String()
configCommand = &DefCommand{
commandName: commandName,
functionBody: functionBody,
}
return
}
func undefCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (configCommand ConfigCommand, err error) {
return &UndefCommand{
commandName: tokens[0],
}, nil
}
func customCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (configCommand ConfigCommand, err error) {
var args []string
for _, token := range tokens {
args = append(args, token.value)
}
return &CustomCommand{
commandName: commandToken.value,
args: args,
}, nil
}
func evalKeysCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (configCommand ConfigCommand, err error) {
if len(tokens) == 0 {
return nil, parser.generateParseError(commandToken, "No keys specified for %v command", evalkeysCommand)
}
var buffer bytes.Buffer
for _, token := range tokens {
buffer.WriteString(token.value)
}
keys := strings.TrimLeftFunc(buffer.String(), unicode.IsSpace)
return &EvalKeysCommand{
keys: keys,
}, nil
}
func sleepCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (configCommand ConfigCommand, err error) {
if len(tokens) < 1 {
return nil, parser.generateParseError(commandToken, "No sleep time specified")
}
sleepToken := tokens[0]
sleepSeconds, err := strconv.ParseFloat(sleepToken.value, 64)
if err != nil || sleepSeconds <= 0.0 {
return nil, parser.generateParseError(sleepToken, "Invalid sleep time: %v. Must be a positive integer", sleepToken.value)
}
return &SleepCommand{
sleepSeconds: sleepSeconds,
}, nil
}