-
Notifications
You must be signed in to change notification settings - Fork 2
/
fish.sublime-syntax
1694 lines (1443 loc) · 72.3 KB
/
fish.sublime-syntax
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%YAML 1.2
---
# http://www.sublimetext.com/docs/3/syntax.html
name: Fish
file_extensions:
- fish
first_line_match: ^#!.*\b(fish)\b
scope: source.shell.fish
# Style guide:
# - Everything in fish is either a command call, a comment, or the empty, unscoped space between. The appearance of a comment prevents anything else being read as a command call on that line. The appearance of a backslash at the end of the line allows the user to temporarily leave the command call and start adding full line comments, before resuming the command call on the next line that starts without any comment character. As such, the following three names are mutually exclusive in the base environment: meta.function-call, comment.line, and constant.character.escape.newline. However, meta.function-call is allowed to overlap with comment.line or constant.character.escape.newline when two layers of command calls are active, such as in blocks and command substitution. Additionally, an escaped newline can also appear midway through any unquoted string, and in such a case it *will* overlap with the meta.function-call scope, but only because it is also in the meta.string.unquoted scope. These sorts of escaped newlines cannot be used to insert full line comments, as the parameter they interrupted will resume at the start of the next line and if that character is a '#' it will be treated literally
# - fish recognises three types of strings: single-quoted, double-quoted, and unquoted. As such, every character of a command call besides whitespace and control operators should have one and only one of the following names: string.quoted.single, string.quoted.double, or meta.string.unquoted. We consider the actual quote characters around a quoted string to be of the same type as the string they enclose, so they highlight usefully. We don't consider whitespace to be an unquoted string.
# - There should never be an unquoted string scope inside of a quoted string, though this leads to a corner case with the numbers inside an index expansion because index expansion can appear after a variable inside or outside of quotes, so we never try to apply any sort of string scope to the indices specifically; we just wrap up the whole variable and index expansion in one big meta.string.unquoted scope when it appears outside of strings, even though there may be whitespace in it and we did just say we shouldn't put meta.string.unquoted on whitespace.
# - We want the meta.function-call scope to extend across the entirety of every command call, including the whitespace. The official recommendation is that for example "meta.function.php meta.function.parameters.php" should never occur, so we take care to analyse every element and decide what sort of "meta.function-call.<type>.<subtype>" name it should have, so that "meta.function-call" is common to every element, but only appears once on every element.
# - fish functions (defined with a call to the `function` builtin) cannot begin with a hyphen, however commands (any executable files on the user's $PATH) can begin with a hyphen and are executed without trouble. To this end, we never forbid a command call from beginning with a hyphen.
# - fish performs an extra level of parsing we can never hope to replicate: any builtin command can be constructed of arbitrarily quoted and unquoted strings and may also contain escaped newlines. We can't parse that in any reasonable way, and it's highly unlikely users ever do it anyway, so we don't even attempt it.
# - keyword.control.conditional is applied to all control keywords that are actually words (if, else, end, function, etc.)
# - support.function is applied to any fish builtins that we have to treat uniquely, however variable.function is used for everything else including other shell builtins.
# - constant.character.escape is used for the backslash at the end of a line which causes line continuation because technically that backslash is "escaping" the literal newline which fish otherwise treats as an operator. In this sense, the backslash is indeed an escape character.
# - constant.character.escape is used for \x?? and \0?? escape codes rather than constant.numeric.{hex,octal} because even though the user inputs a numeric value in hex or octal, the codes serve the purpose of making characters not numbers.
# - The convention used here for the order of operator characters in expressions is:
# - '\s' (includes newline)
# - newline '\n', the implied newline of ')', the implied newline of '#', ';', '&'
# - '(' and ')'
# - '|', '<|', '>>|', '3>|', etc
# - '<', '>', '^'
# - '-'
# - Note the comment character '#' is special only at start of line or preceded by whitespace. Within a parameter it parses as a literal character, so it only works sometimes. This makes it a "weak" operator.
# Use [A-Za-z0-9_]+ for variable names
variables:
# Separator whitespace - Whitespace that isn't the literal newline
# Separates words in a command call
ws_sep: '[^\n\S]'
# Strong newline set - No need to be preceded by whitespace
# Terminates pipelines
nl_s: \n\) # \n)
# Weak newline set - Some chars must be preceded by whitespace
nl_w: '{{nl_s}}#' # \n)#
# Strong redirection - No need to be preceded by whitespace
# Controls data flow for a single command
redir_s: <>
# (Redirection into file and redirection into pipe use '&' slightly differently...grumble)
_redir_base: (?:[0-9]+)?(?:[{{redir_s}}]|>>)|\^\^?
# Weak redirection - Some chars must be preceded by whitespace
# Controls data flow for a single command
redir_w: '{{_redir_base}}|&>>?'
# Strong pipe - No need to be preceded by whitespace
# Separates commands in a pipeline
pipe_s: \|
# Weak pipe (may be redirection into pipe) - Some chars must be preceded by whitespace
pipe_w: (?:{{_redir_base}}|&)?{{pipe_s}}
# Strong operator set - No need to be preceded by whitespace
# Terminates pipelines
op_s: ;&
# Weak operator set - Some chars must be preceded by whitespace
# Terminates pipelines
op_w: '[{{nl_w}}]|;|&(?![|>])'
# Weak operator set, including pipe
# Terminates pipelines or separates commands in a pipeline
op_w_pipe: '{{op_w}}|{{pipe_w}}'
# Weak operator set, including redirection
# Terminates pipelines or controls data flow for a single command
op_w_redir: '{{op_w}}|{{redir_w}}'
# Weak operator set, including pipe and redirection
# Interrupts commands in some way
op_w_pipe_redir: '{{op_w}}|{{pipe_w}}|{{redir_w}}'
# Parameter separators
param_sep: '{{ws_sep}}{{nl_s}}{{op_s}}{{pipe_s}}{{redir_s}}' # \s);&|<>
# Integer number
int: '[+-]?[0-9]+'
# Real (floating-point) number
real: (?:{{int}}\.?[0-9]*|[+-]?[0-9]*\.?[0-9]+)
# Valid characters in a variable name identifier
id_var: \w
contexts:
main:
# Pick up '#' and "\\\n" before command-call sees them
# Here (and within line-continuation) should be the only places that capture a comment, as the '#' is treated as a special character whenever it could end a line and used as the end of the command call
- include: comment
- include: line-continuation
# The first command of a pipeline can't begin with a close parenthesis or be "end". We match this so exclusively early because the base pipeline scope will end immediately if either is seen by its lookahead
# TODO: In an ideal world, command-call-standard would be performing this match because fish considers the strings which follow as parameters, not as new functions. We couldn't do that in a tmLanguage
- match: \)|end
push:
- meta_scope: invalid.illegal.function-call.fish
- match: (?=[{{param_sep}}])
pop: true
# The first character of a pipeline can't be an '&', and the base pipeline scope won't be able to mark it as invalid so we have to do so here
- match: \&
scope: invalid.illegal.function-call.fish
# Base pipeline: goes up until a definitive end (typical control operators, or a comment to finish the line) or the sequences that could be an end if we're actually inside the main context right now (')' and "end")
- match: (?=\S)
push:
- match: (\n)|(;)|(&)|(?=\)|#|end)
captures:
# Redirection uses the meta.function-call.operator prefix, so we have it here too despite it being redundant
1: meta.function-call.operator.control.newline.fish
2: meta.function-call.operator.control.semicolon.fish keyword.operator.control.fish
3: meta.function-call.operator.control.ampersand.fish keyword.operator.control.fish
pop: true
- include: pipeline
separator-whitespace:
# Fundamental structure used to separate elements of a command call; it's any whitespace that isn't a newline, practically meaning spaces or tabs
- match: '{{ws_sep}}+'
scope: meta.function-call.fish
parameter:
# The fish docs for `complete` define the fundamental units passed to a command as "parameters", where those which start with a hyphen are "options" and those which don't are "arguments"
# Parameters are here defined as a sequence of non-operator characters separated by unescaped and unscoped whitespace
# Order important because the parameter-argument rule doesn't exclude things that look like options
- include: parameter-option
- include: parameter-argument
parameter-common:
# The typical substitutions, expansions, and escapes allowed anywhere in a parameter
- include: command-substitution
# Give variable expansion the unquoted string scope, since if it appears inside a string it gets the quoted string scope and we should mirror that
- match: (?=\$)
push:
- meta_scope: meta.string.unquoted.fish
- match: (?!\$)
pop: true
- include: variable-expansion
# Parameters are otherwise made of strings, either quoted or unquoted. The string-unquoted context handles character escapes and brace expansion
- include: string
parameter-option:
# For optimum usefulness to the user, only option parameters receive the variable.parameter scope
# Long option (parameter starting with two hyphens)
- match: (?=--)
push:
- meta_scope: meta.function-call.parameter.option.long.fish
- match: (?=[{{param_sep}}])
pop: true
- match: --
scope:
punctuation.definition.option.long.begin.fish
meta.string.unquoted.fish
push:
- meta_scope: variable.parameter.fish
- match: (?=[{{param_sep}}=])
pop: true
# We mimic the parameter-common context but use a tweaked unquoted string pattern which excludes '='
- include: command-substitution
- match: (?=\$)
push:
- meta_scope: meta.string.unquoted.fish
- match: (?!\$)
pop: true
- include: variable-expansion
- include: string-quoted
- match: (?!['"])
push:
- meta_scope: meta.string.unquoted.fish
- match: (?=[{{param_sep}}('"$=])
pop: true
- include: string-unquoted-patterns
# Consume the '=' and then use standard parameter patterns as well as numerics
- match: =
scope:
variable.parameter.fish
punctuation.definition.option.long.separator.fish
meta.string.unquoted.fish
push:
- match: (?=[{{param_sep}}])
pop: true
- match: '{{real}}(?=$|[{{param_sep}}])'
scope:
meta.string.unquoted.fish
constant.numeric.fish
- include: parameter-common
# Short option (parameter starting with one hyphen)
- match: -(?=[^{{param_sep}}])
scope:
punctuation.definition.option.short.fish
meta.string.unquoted.fish
push:
- meta_scope:
meta.function-call.parameter.option.short.fish
variable.parameter.fish
- match: (?=[{{param_sep}}])
pop: true
- include: parameter-common
parameter-argument:
# Arguments are a type of parameter never treated like options by the command they are passed to
# This context must be entirely standalone because it is used in scopes where parameters starting with hyphens are explicitly interpreted as arguments rather than as options
- match: (?![{{param_sep}}^])
# Begin if we do not precede whitespace or an operator
push:
# End if we precede whitespace or operators (excluding stderr redirect '^' due to a fish quirk)
- match: (?=[{{param_sep}}])
pop: true
# Job and process expansions only occur if the '%' is at the front of the argument, and continue for the entire argument
# Job expansion if the whole argument is an integer
- match: (\%)[0-9]+(?=$|[{{param_sep}}])
captures:
0: meta.function-call.parameter.argument.job-expansion.fish meta.string.unquoted.fish
1: punctuation.definition.job.fish
# Special process expansions. By a convention that I'm making up, scope them as a type of variable
- match: (\%)(self)(?=$|[{{param_sep}}])
captures:
0: meta.function-call.parameter.argument.process-expansion.self.fish meta.string.unquoted.fish
1: punctuation.definition.process.fish
2: variable.language.fish
- match: (\%)(last)(?=$|[{{param_sep}}])
captures:
0: meta.function-call.parameter.argument.process-expansion.last.fish meta.string.unquoted.fish
1: punctuation.definition.process.fish
2: variable.language.fish
# Normal process expansion
- match: \%
scope:
meta.string.unquoted.fish
punctuation.definition.process.fish
push:
- meta_scope: meta.function-call.parameter.argument.process-expansion.other.fish
- match: (?=[{{param_sep}}])
pop: true
- include: parameter-common
# Treat a sequence of integers (with possible sign and decimal separator) as a standalone constant. Don't do this in the string-unquoted-patterns context, so that we can ensure it is a string solely of numbers
- match: '{{real}}(?=$|[{{param_sep}}])'
scope:
meta.function-call.parameter.argument.numeric.fish
meta.string.unquoted.fish
constant.numeric.fish
# This scope can be used by plugins to locate arguments which don't *start* with command substitution or variable expansion and may directly resolve to file paths. Of course, they could have command substitution or variable expansion further on in them (and we do include those contexts), but looking ahead for that to avoid using this particular scope name is nontrivial
- match: (?![\s($])
push:
- meta_scope: meta.function-call.parameter.argument.path.fish
- match: (?=[{{param_sep}}])
pop: true
# Home directory expansion only occurs if the '~' is at the front of the argument, so check it first
- match: \~
scope:
meta.string.unquoted.fish
keyword.operator.tilde.fish
push:
- match: (?=[{{param_sep}}])
pop: true
- include: parameter-common
- include: parameter-common
# Use standard parameter patterns for whatever doesn't match the above
- match: (?!\s)
push:
- meta_scope: meta.function-call.parameter.argument.fish
- match: (?=[{{param_sep}}])
pop: true
- include: parameter-common
pipeline:
# The pipeline context is nonterminating, meaning that it will not consume a *valid* control operator ('\n', ')', '#', ';', or '&'). It *will* consume control operators that appear in invalid locations
# Check for environment setting that precedes the decorators
- include: command-call-environment
- include: separator-whitespace
- include: line-continuation
# This meta command acts as a unary operator on the command to the right, but it is only allowed at the beginning of a pipeline! It can't come after a pipe, so it isn't in the more general command-call-meta context. If the command is followed by any form of execution that just prints help text (ie, with an option or with an explicit or implicit newline), then don't open the scope
# Within fish 3.0, these two commands are "decorators" and intentionally apply their logic to an entire "job conjunction" which we call a logic pipeline
- match: (?=(and|or)\b(?!\s*[{{nl_w}};-]))
push:
- match: (?={{op_w}})
pop: true
- match: (and|or)\b(?!\s*[{{nl_s}};-])
scope:
meta.function-call.name.fish
keyword.operator.word.fish
meta.string.unquoted.fish
push:
- match: (?!{{ws_sep}}|\&|\\\n)
pop: true
- include: separator-whitespace
# If the command is followed by an "&", then that's invalid
- match: \&
scope: invalid.illegal.function-call.fish
- include: line-continuation
# If the command is followed by redirection, then the redirection is illegal. We let that be marked up by the pipeline context
- include: pipeline
# The pipeline-segment context is recursive
- include: pipeline-segment
pipeline-segment:
# The pipeline-segment context is agnostic of where it appears in an overall pipeline, but it does perform checks on what can appear after pipe/logic operators (if they appear) before it will recurse into itself
# Match operators (background, pipe, redirect, and logic) which cannot start a pipeline because they must be consumed within or after a pipeline
# This is done in a weird way so that we can mark '&' as invalid instead of letting it naturally close the pipeline scope and be marked as valid
- match: (?=[&{{pipe_s}}]|{{redir_w}})
push:
# End at newline, ensure no operators or redirection after next whitespace
- match: (?=\n)|(?!\s*(?:{{op_w_pipe_redir}}))
pop: true
- match: '[&{{pipe_s}}]|{{redir_w}}'
scope: invalid.illegal.function-call.fish
# Match commands illegal in the main context
- match: (?:case|else|end)(?=[{{param_sep}}])
scope: invalid.illegal.function-call.fish
# A pipeline which may be one command call or multiple command calls linked by pipe operators ('|', '2>|', etc) or logic operators ('&&' and '||')
- match: (?=[^\s#])
push:
# The pipeline terminates at the first encounter of any control operator, but not '&&'
- match: (?={{op_w}})(?!&&)
pop: true
# Match the command of a pipeline segment
# Ensure no whitespace, comments, background, piping
- match: (?![\s#&]|{{pipe_w}})
push:
-
- match: (?={{op_w_pipe}})
pop: true
- include: command-call-meta
- include: command-call-standard
-
- include: command-call-environment
- include: separator-whitespace
- include: line-continuation
- match: ''
pop: true
# Look for piping/logic which may lead to a second command, and if it does recurse into the pipeline-segment context again
# TODO: Use the scope stack to better control the unlimited number of optional newlines and comments that can follow a pipe/logic operation
- match: (?=&&|\|\||{{pipe_w}})
push:
- match: (?={{op_w}})(?!&&)
pop: true
# Check for a malformed pipeline segment, however since fish 3.0 there is allowed to be a newline or comment after these
# If logic operator would be followed by a control operator mark the logic operator invalid
- match: (?:&&|\|\|)(?=\s*[\){{op_s}}{{pipe_s}}])
scope: invalid.illegal.operator.fish
# If pipe would be followed by a control operator mark the pipe invalid
- match: '{{pipe_w}}(?=\s*[\){{op_s}}])'
scope: invalid.illegal.operator.fish
# Say the piping/logic is okay; the tests at the start of pipeline-segment will highlight any invalid commands (eg, redirection)
# A pipeline is permitted to continue on the next line with whitespace and unescaped newlines in between (though we can only catch one newline)
- match: (?=&&|\|\||{{pipe_w}})
set:
-
- match: (?={{op_w}})
pop: true
# After piping/logic can't be an "and" or "or" that is taking another command as its parameters. This regex should resemble the match which seeks a valid "and"/"or" in the pipeline context, since the only difference is that here the same matched text is instead invalid. We can be a little simpler though, and not worry about marking an '&' as invalid since the word will be instead
- match: (and|or)\b(?!\s*[\n;-])
scope: invalid.illegal.function-call.fish
# Match the next command and any piping/logic which follows it, and so on
- include: pipeline-segment
- pipeline-segment-between
- pipeline-segment-piping
pipeline-segment-piping:
- match: (&&)|(\|\|)
captures:
1: meta.function-call.operator.control.double-ampersand.fish keyword.operator.control.fish
2: meta.function-call.operator.control.double-bar.fish keyword.operator.control.fish
pop: true
- match: ''
set:
- include: redirection-to-pipe-lhs+op
- match: ''
pop: true
pipeline-segment-between:
# This rule lists elements that we can consume indefinitely between the piping and the next pipeline-segment (command call) which follows it
- include: separator-whitespace
- include: line-continuation
- include: comment
- match: \n
scope: meta.function-call.operator.control.newline.ignored.fish
# Matching '' would pop this rule at the end of the first line, which is less than what we want. Looking ahead for a character forces Sublime to keep feeding us lines until we hit something that isn't consumed above
- match: (?=.)
pop: true
pipe-common:
- match: '{{pipe_s}}'
scope: keyword.operator.pipe.fish
pop: true
command-call-environment:
# Must start with a valid variable name identifier
- match: (?=[{{id_var}}]+=)
push:
-
- meta_scope: meta.function-call.environment.fish
- match: (?=[{{param_sep}}])
pop: true
# Match the rest of the environment set, but mark it invalid if the command call is about to end
- match: (?:\S+?|\(.*\))(?=\s*[\n{{op_s}}{{pipe_s}}{{redir_s}}])
scope: invalid.illegal.function-call.fish
pop: true
- include: parameter-common
-
# Consume the '=' but mark it invalid if the command call is about to end
- match: =(?={{op_w_pipe_redir}})
scope: invalid.illegal.function-call.fish
pop: true
# I chose that a valid '=' would be keyword.operator because, unlike the '=' in a long option, this one is actually parsed by the shell and affects its behaviour before the command is even called. It seems disingenuous not to call it a keyword, even though it only has any power in this exact position (since an '=' after the command name is just a literal)
- match: =
scope: keyword.operator.assignment.fish
pop: true
-
# Get the variable name identifier, which must be an unquoted string
- meta_scope:
variable.parameter.fish
meta.string.unquoted.fish
- match: (?=\=)
pop: true
command-call-meta:
# These builtins take another command as parameters, hence, meta commands. However, they act as regular commands if they take an option, are backgrounded/piped, or in some cases are redirected - the command-call-standard context picks them up in that case
# These three meta commands force the parameter to behave as a standard command. They stop when their subcommand is piped. Notably, if the command is followed by redirection, then the redirection is legal and the command just prints its help text, so in that case we avoid opening this scope so that the command is caught elsewhere as a standard command
# Check no control operation or option after whitespace
# Within fish, these three commands are referred to as "decorators"
- match: (?:builtin|command|exec)\b(?!\s*(?:{{op_w_pipe_redir}}|-))
scope:
meta.function-call.name.fish
support.function.fish
meta.string.unquoted.fish
push:
- match: (?={{op_w_pipe}})
pop: true
- include: separator-whitespace
- include: line-continuation
- include: command-call-standard
# (fish 3.1) This meta command is different, because it *must* accept a valid function or command name as the arguments, and rejects options or attempts to execute it alone
# TODO: "time" is allowed after a logic pipe (eg "&&") but not a regular pipe ('|'), and currently this implementation allows it to be in either location. Something to fix up when you overhaul piping
- match: 'time\b'
scope:
meta.function-call.name.fish
support.function.fish
meta.string.unquoted.fish
push:
- match: (?={{op_w_pipe}})
pop: true
- include: separator-whitespace
- include: line-continuation
- include: command-call-standard
# This meta command acts as a unary operator on the command to the right, which can also be a meta command. There is no restriction on where in a pipeline this command may appear. If the command is followed by any form of execution that just prints help text (ie, with an option or with an explicit or implicit newline), then don't open the scope
# Note that both forms require whitespace separation from the next command
- match: (?=(not|!){{ws_sep}}(?!\s*[{{nl_w}};-]))
push:
- match: (?={{op_w}})
pop: true
- match: ((not)|(!))(?={{ws_sep}}(?!\s*[{{nl_w}};-]))
# Still scoping '!' as a "word" operator, because it isn't as strong as a control operator. It has to be followed by whitespace, ie, it has to be a word!
captures:
1: meta.function-call.name.fish
2: keyword.operator.word.not.fish meta.string.unquoted.fish
3: keyword.operator.word.bang.fish meta.string.unquoted.fish
push:
- match: (?!{{ws_sep}}|\&|\\\n)
pop: true
- include: separator-whitespace
# If the command is followed by an "&" that's invalid and we need to pick it up here
- match: \&
scope: invalid.illegal.function-call.fish
- include: line-continuation
# Redirection is illegal after the meta command, which is taken care of by the pipeline context
- include: pipeline
command-call-standard:
# Check if the command is any other legal command, ie, a standard command
# Look for the alternate form of test, which uses a matching pair of '[' ']'
- match: \[(?=[\s{{redir_s}}]|\\\n)
scope:
meta.function-call.name.fish
support.function.test.begin.fish
meta.string.unquoted.fish
set:
-
# After the closing ']' further parameters are illegal, but redirection is okay (for no good reason)
- match: (?={{op_w_pipe}})
pop: true
- include: separator-whitespace
- include: line-continuation
- include: redirection
- match: (?!{{op_w_pipe}}).
scope:
meta.function-call.fish
invalid.illegal.parameter.fish
-
- match: \]
scope:
meta.function-call.name.fish
support.function.test.end.fish
meta.string.unquoted.fish
pop: true
- match: '[{{nl_s}}{{op_s}}{{pipe_s}}].*'
scope: invalid.illegal.function-call.fish
pop: true
- include: separator-whitespace
- include: line-continuation
- include: redirection
- include: parameter
# A complete command comprising a name element and optional parameter/redirection/comment elements
- match: (?=\S)
set:
-
- match: (?={{op_w_pipe}})
pop: true
# Allowed elements (now forcibly using arguments, no more options)
- include: separator-whitespace
- include: line-continuation
- include: redirection
- include: parameter-argument
-
- match: (?={{op_w_pipe}})
pop: true
# End of options (parameter of just two hyphens)
- match: --(?=[{{param_sep}}])
scope:
meta.function-call.parameter.option.end.fish
variable.parameter.fish
punctuation.definition.option.end.fish
meta.string.unquoted.fish
pop: true
# Allowed elements (including options)
- include: separator-whitespace
- include: line-continuation
- include: redirection
- include: parameter
-
# A name or block element. If a block is found, everything up to the `end` command is captured here
- match: (?=[{{param_sep}}])
pop: true
- include: command-call-standard-block
- include: command-call-standard-name
-
# A command name can't begin with a process expansion operator (however the variable expansion operator '$' is allowed)
- match: \%[^{{param_sep}}]*
scope: invalid.illegal.function-call.fish
- match: ''
pop: true
command-call-standard-name:
# Look for loop/function control commands. We perform no checking on the validity of their scope (because only allowing them in the correct scope won't work if they are used within if-blocks) or parameters (because fish does that during execution not parsing)
- match: (?:break|continue|return)(?=[{{param_sep}}])
scope:
meta.function-call.name.fish
keyword.control.conditional.fish
meta.string.unquoted.fish
# A generic name element
- match: (?!\s)
push:
- match: (?=[{{param_sep}}])
pop: true
# A command name can't contain a command substitution. We match the whole line if no closing parenthesis is found, or until the end of the command name if the command substitution gets closed
# fish would match the whole command name invalid if there was a command substitution anywhere in it, but we can't look ahead that effectively
- match: (?=\()
push:
- meta_scope: invalid.illegal.function-call.fish
- match: (?=[{{param_sep}}])
pop: true
- match: \(
push:
- match: \)|(?=[\n{{op_s}}{{pipe_s}}{{redir_s}}])
pop: true
# A command can't start with a closing brace (an unbalanced expansion). I think this is the level at which we would like to match ')' as well, but since it is a much stronger operator it appears in a lot of lookaheads before this point and we struggle to capture it
- match: \}[^{{param_sep}}]*
scope: invalid.illegal.function-call.fish
# Otherwise, treat the element as a fraction of a name made of arbitrary strings (which breaks at an escaped newline)
- match: (?!\s)
push:
- meta_scope:
meta.function-call.name.fish
variable.function.fish
- match: (?=[{{param_sep}}(])
pop: true
# Give variable expansion the unquoted string scope
- match: (?=\$)
push:
- meta_scope: meta.string.unquoted.fish
- match: (?!\$)
pop: true
- include: variable-expansion
- include: string
command-call-standard-block:
# Block commands cannot be backgrounded, piped, or redirected
- match: (begin|while|if|for|switch|function)\s*([&{{pipe_s}}{{redir_s}}]+)
captures:
1: meta.function-call.name.fish variable.function.fish meta.string.unquoted.fish
2: invalid.illegal.operator.fish
# The begin command uniquely cannot be the last command in a command substitution
- match: (begin)\s*(\))
captures:
1: meta.function-call.name.fish variable.function.fish meta.string.unquoted.fish
2: invalid.illegal.operator.fish
# The begin command can be alone on a line or followed by any command that doesn't start with a '-'. If a '-' is seen it shouldn't be treated as a block
- match: begin(?=\s*$|\s*[\n;]|\s+[^\s-])
scope:
meta.function-call.name.fish
keyword.control.conditional.fish
meta.string.unquoted.fish
push:
- meta_scope: meta.block.begin.fish
- match: end(?=$|[{{param_sep}}])
scope:
meta.function-call.name.fish
keyword.control.conditional.fish
meta.string.unquoted.fish
pop: true
- include: main
# If the command name is followed by a valid string (not anything that closes the scope or a string starting with a '-') then open the scope
- match: (?=while\s+[^{{nl_w}};-])
push:
- meta_scope: meta.block.while.fish
- match: end(?=$|[{{param_sep}}])
scope:
meta.function-call.name.fish
keyword.control.conditional.fish
meta.string.unquoted.fish
pop: true
# Capture the command name we know is there, include a single instance of a pipeline, and end when an operator is seen
- match: while
scope:
meta.function-call.name.fish
keyword.control.conditional.fish
meta.string.unquoted.fish
push:
- match: (?=[{{op_w}}])
pop: true
- include: line-continuation
- include: pipeline
# Capture the operator we know is there, include the main context, and end when an `end` command is seen. The main context handles any invalid operators
- match: (\n)|(;)|(?=[#)&])
captures:
1: meta.function-call.operator.control.newline.fish
2: meta.function-call.operator.control.semicolon.fish keyword.operator.control.fish
push:
- match: (?=end(?:$|[{{param_sep}}]))
pop: true
- include: main
# If the command name is followed by a valid string (not anything that closes the scope or a string starting with a '-') then open the scope
- match: (?=if\s+[^{{nl_w}};-])
push:
- meta_scope: meta.block.if.fish
- match: end(?=$|[{{param_sep}}])
scope:
meta.function-call.name.fish
keyword.control.conditional.fish
meta.string.unquoted.fish
pop: true
# Note that this internal scope does not treat the closing parenthesis as a control operator, because a command substitution can't end in the middle of a block
- include: command-call-standard-block-if-internal
# If the command name is followed by a valid string (not anything that closes the scope or a string starting with a '-') then open the scope
- match: (?=for\s+[^{{nl_w}};-])
push:
- meta_scope: meta.block.for-in.fish
- match: end(?=$|[{{param_sep}}])
scope:
meta.function-call.name.fish
keyword.control.conditional.fish
meta.string.unquoted.fish
pop: true
# Capture the command name we know is there, include a single instance of a parameter (the varname), and end when the whitespace after the varname is captured
- match: (for)(\s+)
captures:
1: meta.function-call.name.fish keyword.control.conditional.fish meta.string.unquoted.fish
2: meta.function-call.fish
push:
- match: \s+
scope: meta.function-call.fish
pop: true
# Manually define this parameter, because it must be something that evaluates to a valid variable name. That means that we need to include most parameter patterns, but rather than blindly accepting all unquoted strings we will only accept word characters
- match: (?![{{param_sep}}])
push:
- meta_scope: meta.function-call.parameter.argument.fish
- match: (?=[{{param_sep}}])
pop: true
- include: line-continuation # Eats whitespace on next line!
# Subset of the parameter-common rule
- include: command-substitution
- match: (?=\$)
push:
- meta_scope: meta.string.unquoted.fish
- match: (?!\$)
pop: true
- include: variable-expansion
- include: string-quoted
# The only unquoted string should be a valid variable name identifier
- match: '{{id_var}}'
scope: meta.string.unquoted.fish
# If we get to here, we have an illegal character for variables
- match: .
scope: invalid.illegal.string.fish
# Capture any operators
- match: \S+
scope: invalid.illegal.operator.fish
# Line continuation is allowed between the varname and "in"
- include: line-continuation
# Capture the command name which might be there, include an arbitrary number of arguments, and end when the control operator is seen
- match: in(?=\s)
scope:
meta.function-call.name.fish
keyword.control.conditional.fish
meta.string.unquoted.fish
push:
- match: (?={{op_w}}|{{pipe_s}})
pop: true
- include: separator-whitespace
- include: line-continuation
- include: parameter-argument
# Capture the operator we know is there, include the main context, and end when an `end` command is seen. The main context handles any invalid operators
- match: (\n)|(;)|(?=[)#])
captures:
1: meta.function-call.operator.control.newline.fish
2: meta.function-call.operator.control.semicolon.fish keyword.operator.control.fish
push:
- match: (?=end(?:$|[{{param_sep}}]))
pop: true
- include: main
# Anything else is invalid
- match: \S+?
scope: invalid.illegal.function-call.fish
- include: separator-whitespace
# If the command name is followed by a valid string (not anything that closes the scope or a string starting with a '-') then open the scope
- match: (?=switch\s+[^{{nl_w}};-])
push:
- meta_scope: meta.block.switch.fish
- match: end(?=$|[{{param_sep}}])
scope:
meta.function-call.name.fish
keyword.control.conditional.fish
meta.string.unquoted.fish
pop: true
# Match the valid part of the switch statement, then look for an invalid part
- match: (?=switch)
push:
- match: (?=[{{nl_w}};])
pop: true
# Capture the command name we know is there, include a single instance of an argument (the value), and end when whitespace or a control operator is seen
- match: (switch)(\s+)
captures:
1: meta.function-call.name.fish keyword.control.conditional.fish meta.string.unquoted.fish
2: meta.function-call.fish
push:
- match: (?=[{{param_sep}}])
pop: true
- include: line-continuation # Eats whitespace on next line!
- include: parameter-argument
# Capture anything that an argument explicitly rejects, which is mostly operators
- match: \S+
scope: invalid.illegal.operator.fish
# Capture whitespace which might be there, match any other strings as invalid, and end when a valid control operator is seen
- match: \s+
scope: meta.function-call.fish
push:
- match: (?=[{{nl_w}};])
pop: true
- match: \S+?
scope: invalid.illegal.string.fish
# Capture the operator we know is there, include the main context, and end when an `end` command is seen. The main context handles any invalid operators
- match: (\n)|(;)|(?=[#)])
captures:
1: meta.function-call.operator.control.newline.fish
2: meta.function-call.operator.control.semicolon.fish keyword.operator.control.fish
push:
- match: (?=end(?:$|[{{param_sep}}]))
pop: true
# Capture the command name which might be there, include an arbitrary number of arguments, and end when the control operator is captured. The main context further down handles any invalid operators used to end the scope
- match: case(?=[{{ws_sep}}{{nl_s}}{{op_s}}])
scope:
meta.function-call.name.fish
keyword.control.conditional.fish
meta.string.unquoted.fish
push:
- match: (\n)|(;)|(?=[)#&{{pipe_s}}]|{{redir_w}})
captures:
1: meta.function-call.operator.control.newline.fish
2: meta.function-call.operator.control.semicolon.fish keyword.operator.control.fish
pop: true
- include: separator-whitespace
- include: line-continuation
- include: parameter-argument
- include: main
# If the command name is followed by a valid string (not anything that closes the scope or a string starting with a '-') then open the scope
- match: (?=function\s+[^{{nl_w}};-])
push:
- meta_scope: meta.block.function.fish
- match: end(?=$|[{{param_sep}}])
scope:
meta.function-call.name.fish
keyword.control.conditional.fish
meta.string.unquoted.fish
pop: true
# Match the defined name of the function statement, then look for further parameters
- match: (?=function)
push:
- match: (?={{op_w_pipe_redir}})
pop: true
# Capture the command name we know is there, include a single instance of a parameter (the value), and end when whitespace or a control operator is seen
- match: (function)\s+
captures:
0: meta.function-call.name.fish
1: keyword.control.conditional.fish meta.string.unquoted.fish
push:
- match: (?=[{{param_sep}}])
pop: true
- include: line-continuation # Eats whitespace on next line!
# Illegal control characters are rejected via ?={{param_sep}} and marked invalid by main scope inclusion further down
# Start when an escaped newline isn't present, and end when whitespace or an operator is seen
- match: (?!\\\n)
push:
- match: (?=[{{param_sep}}])
pop: true
# This is all the usual things a parameter is allowed to contain
- match: (?![{{ws_sep}}{{nl_s}}])
push:
- meta_scope:
meta.function-call.parameter.argument.fish
entity.name.function.fish
- match: (?=[{{param_sep}}])
pop: true
- include: parameter-common
# Capture whitespace which might be there, then match anything normal for a command call, except redirections!
- match: (?={{ws_sep}})
push:
- match: (?={{op_w_pipe_redir}})
pop: true
- include: separator-whitespace
- include: line-continuation
- include: parameter
# Capture the operator we know is there, include the main context, and end when an `end` command is seen. The main context handles any invalid operators
- match: (\n)|(;)|(?=[)#&{{pipe_s}}]|{{redir_w}})
captures:
1: meta.function-call.operator.control.newline.fish
2: meta.function-call.operator.control.semicolon.fish keyword.operator.control.fish
push:
- match: (?=end(?:$|[{{param_sep}}]))
pop: true
- include: main
command-call-standard-block-if-internal:
# The acceptable structure internal to an if-end block can be represented recursively because anonymous scopes nest silently. If an `else` without a following `if` is seen, then further `else` commands will be correctly marked as invalid. This is a lot of work just to get that extra little bit of functionality :)
# Capture an `if` and the command up to the control operator, then capture from the control operator indefinitely
- match: (?=if(?:\s*\n|\s+[^\s;]))
push:
- match: (?=end(?:$|[{{param_sep}}]))
pop: true
# Match the command name we know is there, include a single instance of a pipeline, and end when a control operator is seen
- match: if
scope:
meta.function-call.name.fish
keyword.control.conditional.fish
meta.string.unquoted.fish
push:
- match: (?=[\n#{{op_s}}])
pop: true
- include: line-continuation
- include: pipeline
# Match the operator we know is there, then include the main context or an `else` structure. The main context handles any invalid operators
- match: (\n)|(;)|(?=[#&])
captures:
1: meta.function-call.operator.control.newline.fish
2: meta.function-call.operator.control.semicolon.fish keyword.operator.control.fish
push:
- match: '(?=end(?:$|[{{param_sep}}]))'
pop: true
# Capture an `else` up to the control operator or the start of an `if` structure, then match from the control operator indefinitely or match an `if` structure
- match: (?=else\s*[\s;])
push:
- match: (?=end(?:$|[{{param_sep}}]))