-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathparser.go
More file actions
789 lines (684 loc) · 18.3 KB
/
Copy pathparser.go
File metadata and controls
789 lines (684 loc) · 18.3 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
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
package parser
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/charmbracelet/vhs/lexer"
"github.com/charmbracelet/vhs/token"
)
// NewError returns a new parser.Error with the given token and message.
func NewError(token token.Token, msg string) Error {
return Error{
Token: token,
Msg: msg,
}
}
// CommandType is a type that represents a command.
type CommandType token.Type
// CommandTypes is a list of the available commands that can be executed.
var CommandTypes = []CommandType{ //nolint: deadcode
token.BACKSPACE,
token.DELETE,
token.INSERT,
token.CTRL,
token.ALT,
token.DOWN,
token.ENTER,
token.ESCAPE,
token.ILLEGAL,
token.LEFT,
token.PAGE_UP,
token.PAGE_DOWN,
token.RIGHT,
token.SET,
token.OUTPUT,
token.SLEEP,
token.SPACE,
token.HIDE,
token.REQUIRE,
token.SHOW,
token.TAB,
token.TYPE,
token.UP,
token.WAIT,
token.SOURCE,
token.SCREENSHOT,
token.COPY,
token.PASTE,
token.ENV,
}
// String returns the string representation of the command.
func (c CommandType) String() string { return token.ToCamel(string(c)) }
// Command represents a command with options and arguments.
type Command struct {
Type CommandType
Options string
Args string
Source string
}
// String returns the string representation of the command.
// This includes the options and arguments of the command.
func (c Command) String() string {
if c.Options != "" {
return fmt.Sprintf("%s %s %s", c.Type, c.Options, c.Args)
}
return fmt.Sprintf("%s %s", c.Type, c.Options)
}
// Error represents an error with parsing a tape file.
// It tracks the token causing the error and a human readable error message.
type Error struct {
Token token.Token
Msg string
}
// String returns a human readable error message printing the token line number
// and message.
func (e Error) String() string {
return fmt.Sprintf("%2d:%-2d │ %s", e.Token.Line, e.Token.Column, e.Msg)
}
// Error implements the error interface.
func (e Error) Error() string {
return e.String()
}
// Parser is the structure that manages the parsing of tokens.
type Parser struct {
l *lexer.Lexer
errors []Error
cur token.Token
peek token.Token
}
// New returns a new Parser.
func New(l *lexer.Lexer) *Parser {
p := &Parser{l: l, errors: []Error{}}
// Read two tokens, so cur and peek are both set.
p.nextToken()
p.nextToken()
return p
}
// Parse takes an input string provided by the lexer and parses it into a
// list of commands.
func (p *Parser) Parse() []Command {
cmds := []Command{}
for p.cur.Type != token.EOF {
if p.cur.Type == token.COMMENT {
p.nextToken()
continue
}
cmds = append(cmds, p.parseCommand()...)
p.nextToken()
}
return cmds
}
// parseCommand parses a command.
func (p *Parser) parseCommand() []Command {
switch p.cur.Type {
case token.SPACE,
token.BACKSPACE,
token.DELETE,
token.INSERT,
token.ENTER,
token.ESCAPE,
token.TAB,
token.DOWN,
token.LEFT,
token.RIGHT,
token.UP,
token.PAGE_UP,
token.PAGE_DOWN:
return []Command{p.parseKeypress(p.cur.Type)}
case token.SET:
return []Command{p.parseSet()}
case token.OUTPUT:
return []Command{p.parseOutput()}
case token.SLEEP:
return []Command{p.parseSleep()}
case token.TYPE:
return []Command{p.parseType()}
case token.CTRL:
return []Command{p.parseCtrl()}
case token.ALT:
return []Command{p.parseAlt()}
case token.SHIFT:
return []Command{p.parseShift()}
case token.HIDE:
return []Command{p.parseHide()}
case token.REQUIRE:
return []Command{p.parseRequire()}
case token.SHOW:
return []Command{p.parseShow()}
case token.WAIT:
return []Command{p.parseWait()}
case token.SOURCE:
return p.parseSource()
case token.SCREENSHOT:
return []Command{p.parseScreenshot()}
case token.COPY:
return []Command{p.parseCopy()}
case token.PASTE:
return []Command{p.parsePaste()}
case token.ENV:
return []Command{p.parseEnv()}
default:
p.errors = append(p.errors, NewError(p.cur, "Invalid command: "+p.cur.Literal))
return []Command{{Type: token.ILLEGAL}}
}
}
func (p *Parser) parseWait() Command {
cmd := Command{Type: token.WAIT}
if p.peek.Type == token.PLUS {
p.nextToken()
if p.peek.Type != token.STRING || (p.peek.Literal != "Line" && p.peek.Literal != "Screen") {
p.errors = append(p.errors, NewError(p.peek, "Wait+ expects Line or Screen"))
return cmd
}
cmd.Args = p.peek.Literal
p.nextToken()
} else {
cmd.Args = "Line"
}
cmd.Options = p.parseSpeed()
if cmd.Options != "" {
dur, _ := time.ParseDuration(cmd.Options)
if dur <= 0 {
p.errors = append(p.errors, NewError(p.peek, "Wait expects positive duration"))
return cmd
}
}
if p.peek.Type != token.REGEX {
// fallback to default
return cmd
}
p.nextToken()
if _, err := regexp.Compile(p.cur.Literal); err != nil {
p.errors = append(p.errors, NewError(p.cur, fmt.Sprintf("Invalid regular expression '%s': %v", p.cur.Literal, err)))
return cmd
}
cmd.Args += " " + p.cur.Literal
return cmd
}
// parseSpeed parses a typing speed indication.
//
// i.e. @<time>
//
// This is optional (defaults to 100ms), thus skips (rather than error-ing)
// if the typing speed is not specified.
func (p *Parser) parseSpeed() string {
if p.peek.Type == token.AT {
p.nextToken()
return p.parseTime()
}
return ""
}
// parseRepeat parses an optional repeat count for a command.
//
// i.e. Backspace [count]
//
// This is optional (defaults to 1), thus skips (rather than error-ing)
// if the repeat count is not specified.
func (p *Parser) parseRepeat() string {
if p.peek.Type == token.NUMBER {
count := p.peek.Literal
p.nextToken()
return count
}
return "1"
}
// parseTime parses a time argument.
//
// <number>[ms]
func (p *Parser) parseTime() string {
var t string
if p.peek.Type == token.NUMBER {
t = p.peek.Literal
p.nextToken()
} else {
p.errors = append(p.errors, NewError(p.cur, "Expected time after "+p.cur.Literal))
return ""
}
// Allow TypingSpeed to have bare units (e.g. 50ms, 100ms)
if p.peek.Type == token.MILLISECONDS || p.peek.Type == token.SECONDS || p.peek.Type == token.MINUTES {
t += p.peek.Literal
p.nextToken()
} else {
t += "s"
}
return t
}
// parseCtrl parses a control command.
// A control command takes one or multiples characters and/or modifiers to type while ctrl is held down.
//
// Ctrl[+Alt][+Shift]+<char>
// E.g:
// Ctrl+Shift+O
// Ctrl+Alt+Shift+P
func (p *Parser) parseCtrl() Command {
var args []string
inModifierChain := true
for p.peek.Type == token.PLUS {
p.nextToken()
peek := p.peek
// Get key from keywords and check if it's a valid modifier
if k := token.Keywords[peek.Literal]; token.IsModifier(k) {
if !inModifierChain {
p.errors = append(p.errors, NewError(p.cur, "Modifiers must come before other characters"))
// Clear args so the error is returned
args = nil
continue
}
args = append(args, peek.Literal)
p.nextToken()
continue
}
inModifierChain = false
// Add key argument.
switch {
case peek.Type == token.ENTER,
peek.Type == token.SPACE,
peek.Type == token.BACKSPACE,
peek.Type == token.MINUS,
peek.Type == token.AT,
peek.Type == token.LEFT_BRACKET,
peek.Type == token.RIGHT_BRACKET,
peek.Type == token.CARET,
peek.Type == token.BACKSLASH,
peek.Type == token.STRING && len(peek.Literal) == 1:
args = append(args, peek.Literal)
default:
// Key arguments with len > 1 are not valid
p.errors = append(p.errors,
NewError(p.cur, "Not a valid modifier"),
NewError(p.cur, "Invalid control argument: "+p.cur.Literal))
}
p.nextToken()
}
if len(args) == 0 {
p.errors = append(p.errors, NewError(p.cur, "Expected control character with args, got "+p.cur.Literal))
}
ctrlArgs := strings.Join(args, " ")
return Command{Type: token.CTRL, Args: ctrlArgs}
}
// parseAlt parses an alt command.
// An alt command takes a character to type while the modifier is held down.
//
// Alt+<character>
func (p *Parser) parseAlt() Command {
if p.peek.Type == token.PLUS {
p.nextToken()
if p.peek.Type == token.STRING ||
p.peek.Type == token.ENTER ||
p.peek.Type == token.LEFT_BRACKET ||
p.peek.Type == token.RIGHT_BRACKET ||
p.peek.Type == token.TAB {
c := p.peek.Literal
p.nextToken()
return Command{Type: token.ALT, Args: c}
}
}
p.errors = append(p.errors, NewError(p.cur, "Expected alt character, got "+p.cur.Literal))
return Command{Type: token.ALT}
}
// parseShift parses a shift command.
// A shift command takes one character and types while shift is held down.
//
// Shift+<char>
// E.g.
// Shift+A
// Shift+Tab
// Shift+Enter
func (p *Parser) parseShift() Command {
if p.peek.Type == token.PLUS {
p.nextToken()
if p.peek.Type == token.STRING ||
p.peek.Type == token.ENTER ||
p.peek.Type == token.LEFT_BRACKET ||
p.peek.Type == token.RIGHT_BRACKET ||
p.peek.Type == token.TAB {
c := p.peek.Literal
p.nextToken()
return Command{Type: token.SHIFT, Args: c}
}
}
p.errors = append(p.errors, NewError(p.cur, "Expected shift character, got "+p.cur.Literal))
return Command{Type: token.SHIFT}
}
// parseKeypress parses a repeatable and time adjustable keypress command.
// A keypress command takes an optional typing speed and optional count.
//
// Key[@<time>] [count]
func (p *Parser) parseKeypress(ct token.Type) Command {
cmd := Command{Type: CommandType(ct)}
cmd.Options = p.parseSpeed()
cmd.Args = p.parseRepeat()
return cmd
}
// parseOutput parses an output command.
// An output command takes a file path to which to output.
//
// Output <path>
func (p *Parser) parseOutput() Command {
cmd := Command{Type: token.OUTPUT}
if p.peek.Type != token.STRING {
p.errors = append(p.errors, NewError(p.cur, "Expected file path after output"))
return cmd
}
ext := filepath.Ext(p.peek.Literal)
if ext != "" {
cmd.Options = ext
} else {
cmd.Options = ".png"
if !strings.HasSuffix(p.peek.Literal, "/") {
p.errors = append(p.errors, NewError(p.peek, "Expected folder with trailing slash"))
}
}
cmd.Args = p.peek.Literal
p.nextToken()
return cmd
}
// parseSet parses a set command.
// A set command takes a setting name and a value.
//
// Set <setting> <value>
func (p *Parser) parseSet() Command {
cmd := Command{Type: token.SET}
if token.IsSetting(p.peek.Type) {
cmd.Options = p.peek.Literal
} else {
p.errors = append(p.errors, NewError(p.peek, "Unknown setting: "+p.peek.Literal))
}
p.nextToken()
switch p.cur.Type {
case token.WAIT_TIMEOUT:
cmd.Args = p.parseTime()
case token.WAIT_PATTERN:
cmd.Args = p.peek.Literal
_, err := regexp.Compile(p.peek.Literal)
if err != nil {
p.errors = append(p.errors, NewError(p.peek, "Invalid regexp pattern: "+p.peek.Literal))
}
p.nextToken()
case token.LOOP_OFFSET:
cmd.Args = p.peek.Literal
p.nextToken()
// Allow LoopOffset without '%'
// Set LoopOffset 20
cmd.Args += "%"
if p.peek.Type == token.PERCENT {
p.nextToken()
}
case token.TYPING_SPEED:
cmd.Args = p.peek.Literal
p.nextToken()
// Allow TypingSpeed to have bare units (e.g. 10ms)
// Set TypingSpeed 10ms
if p.peek.Type == token.MILLISECONDS ||
p.peek.Type == token.SECONDS {
cmd.Args += p.peek.Literal
p.nextToken()
} else if cmd.Options == "TypingSpeed" {
cmd.Args += "s"
}
case token.WINDOW_BAR:
cmd.Args = p.peek.Literal
p.nextToken()
windowBar := p.cur.Literal
if !isValidWindowBar(windowBar) {
p.errors = append(
p.errors,
NewError(p.cur, windowBar+" is not a valid bar style."),
)
}
case token.MARGIN_FILL:
cmd.Args = p.peek.Literal
p.nextToken()
marginFill := p.cur.Literal
// Check if margin color is a valid hex string
if strings.HasPrefix(marginFill, "#") {
_, err := strconv.ParseUint(marginFill[1:], 16, 64)
if err != nil || len(marginFill) != 7 {
p.errors = append(
p.errors,
NewError(
p.cur,
"\""+marginFill+"\" is not a valid color.",
),
)
}
}
case token.CURSOR_BLINK:
cmd.Args = p.peek.Literal
p.nextToken()
if p.cur.Type != token.BOOLEAN {
p.errors = append(
p.errors,
NewError(p.cur, "expected boolean value."),
)
}
default:
cmd.Args = p.peek.Literal
p.nextToken()
}
return cmd
}
// parseSleep parses a sleep command.
// A sleep command takes a time for how long to sleep.
//
// Sleep <time>
func (p *Parser) parseSleep() Command {
cmd := Command{Type: token.SLEEP}
cmd.Args = p.parseTime()
return cmd
}
// parseHide parses a Hide command.
//
// Hide
func (p *Parser) parseHide() Command {
cmd := Command{Type: token.HIDE}
return cmd
}
// parseRequire parses a Require command.
//
// Require
func (p *Parser) parseRequire() Command {
cmd := Command{Type: token.REQUIRE}
if p.peek.Type != token.STRING {
p.errors = append(p.errors, NewError(p.peek, p.cur.Literal+" expects one string"))
}
cmd.Args = p.peek.Literal
p.nextToken()
return cmd
}
// parseShow parses a Show command.
//
// Show
func (p *Parser) parseShow() Command {
cmd := Command{Type: token.SHOW}
return cmd
}
// parseType parses a type command.
// A type command takes a string to type.
//
// Type "string"
func (p *Parser) parseType() Command {
cmd := Command{Type: token.TYPE}
cmd.Options = p.parseSpeed()
if p.peek.Type != token.STRING {
p.errors = append(p.errors, NewError(p.peek, p.cur.Literal+" expects string"))
}
for p.peek.Type == token.STRING {
p.nextToken()
cmd.Args += p.cur.Literal
// If the next token is a string, add a space between them.
// Since tokens must be separated by a whitespace, this is most likely
// what the user intended.
//
// Although it is possible that there may be multiple spaces / tabs between
// the tokens, however if the user was intending to type multiple spaces
// they would need to use a string literal.
if p.peek.Type == token.STRING {
cmd.Args += " "
}
}
return cmd
}
// parseCopy parses a copy command
// A copy command takes a string to the clipboard
//
// Copy "string"
func (p *Parser) parseCopy() Command {
cmd := Command{Type: token.COPY}
if p.peek.Type != token.STRING {
p.errors = append(p.errors, NewError(p.peek, p.cur.Literal+" expects string"))
}
for p.peek.Type == token.STRING {
p.nextToken()
cmd.Args += p.cur.Literal
// If the next token is a string, add a space between them.
// Since tokens must be separated by a whitespace, this is most likely
// what the user intended.
//
// Although it is possible that there may be multiple spaces / tabs between
// the tokens, however if the user was intending to type multiple spaces
// they would need to use a string literal.
if p.peek.Type == token.STRING {
cmd.Args += " "
}
}
return cmd
}
// parsePaste parses paste command
// Paste Command the string from the clipboard buffer.
//
// Paste
func (p *Parser) parsePaste() Command {
cmd := Command{Type: token.PASTE}
return cmd
}
// parseEnv parses Env command
// Env command takes in a key-value pair which is set.
//
// Env key "value"
func (p *Parser) parseEnv() Command {
cmd := Command{Type: token.ENV}
cmd.Options = p.peek.Literal
p.nextToken()
if p.peek.Type != token.STRING {
p.errors = append(p.errors, NewError(p.peek, p.cur.Literal+" expects string"))
}
cmd.Args = p.peek.Literal
p.nextToken()
return cmd
}
// parseSource parses source command.
// Source command takes a tape path to include in current tape.
//
// Source <path>
func (p *Parser) parseSource() []Command {
cmd := Command{Type: token.SOURCE}
if p.peek.Type != token.STRING {
p.errors = append(p.errors, NewError(p.cur, "Expected path after Source"))
p.nextToken()
return []Command{cmd}
}
srcPath := p.peek.Literal
// Check if path has .tape extension
ext := filepath.Ext(srcPath)
if ext != ".tape" {
p.errors = append(p.errors, NewError(p.peek, "Expected file with .tape extension"))
p.nextToken()
return []Command{cmd}
}
// Check if tape exist
if _, err := os.Stat(srcPath); os.IsNotExist(err) {
notFoundErr := fmt.Sprintf("File %s not found", srcPath)
p.errors = append(p.errors, NewError(p.peek, notFoundErr))
p.nextToken()
return []Command{cmd}
}
// Check if source tape contains nested Source command
d, err := os.ReadFile(srcPath)
if err != nil {
readErr := fmt.Sprintf("Unable to read file: %s", srcPath)
p.errors = append(p.errors, NewError(p.peek, readErr))
p.nextToken()
return []Command{cmd}
}
srcTape := string(d)
// Check source tape is NOT empty
if len(srcTape) == 0 {
readErr := fmt.Sprintf("Source tape: %s is empty", srcPath)
p.errors = append(p.errors, NewError(p.peek, readErr))
p.nextToken()
return []Command{cmd}
}
srcLexer := lexer.New(srcTape)
srcParser := New(srcLexer)
// Check not nested source
srcCmds := srcParser.Parse()
for _, cmd := range srcCmds {
if cmd.Type == token.SOURCE {
p.errors = append(p.errors, NewError(p.peek, "Nested Source detected"))
p.nextToken()
return []Command{cmd}
}
}
// Check src errors
srcErrors := srcParser.Errors()
if len(srcErrors) > 0 {
p.errors = append(p.errors, NewError(p.peek, fmt.Sprintf("%s has %d errors", srcPath, len(srcErrors))))
p.nextToken()
return []Command{cmd}
}
filtered := make([]Command, 0)
for _, srcCmd := range srcCmds {
// Output have to be avoid in order to not overwrite output of the original tape.
if srcCmd.Type == token.SOURCE ||
srcCmd.Type == token.OUTPUT {
continue
}
filtered = append(filtered, srcCmd)
}
p.nextToken()
return filtered
}
// parseScreenshot parses screenshot command.
// Screenshot command takes a file path for storing screenshot.
//
// Screenshot <path>
func (p *Parser) parseScreenshot() Command {
cmd := Command{Type: token.SCREENSHOT}
if p.peek.Type != token.STRING {
p.errors = append(p.errors, NewError(p.cur, "Expected path after Screenshot"))
p.nextToken()
return cmd
}
path := p.peek.Literal
// Check if path has .png, .txt, or .ansi extension
ext := filepath.Ext(path)
if ext != ".png" && ext != ".txt" && ext != ".ansi" {
p.errors = append(p.errors, NewError(p.peek, "Expected file with .png, .txt, or .ansi extension"))
p.nextToken()
return cmd
}
cmd.Args = path
p.nextToken()
return cmd
}
// Errors returns any errors that occurred during parsing.
func (p *Parser) Errors() []Error {
return p.errors
}
// nextToken gets the next token from the lexer
// and updates the parser tokens accordingly.
func (p *Parser) nextToken() {
p.cur = p.peek
p.peek = p.l.NextToken()
}
// Check if a given windowbar type is valid.
func isValidWindowBar(w string) bool {
return w == "" ||
w == "Colorful" || w == "ColorfulRight" ||
w == "Rings" || w == "RingsRight"
}