fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerMark private functions that don't need localizationa53db7256

master
2.2 KiB64 linesraw
1# localization: skip(private)
2function __fish_tokenizer_state --description "Print the state of the tokenizer at the end of the given string"
3    # Go through the token char-by-char in a state machine.
4    # The states are:
5    # - normal - no quoting is active (the starting state)
6    # - single - open single-quote
7    # - double - open double
8    # - escaped - open \\ - the next character is non-special
9    # - single-escaped - open \\ inside single-quotes
10    # - double-escaped - open \\ inside double-quotes
11
12    argparse --min-args 1 --max-args 1 i/initial-state= -- $argv
13    or return 1
14
15    set -l state normal
16    if set -q _flag_initial_state
17        set state $_flag_initial_state
18    end
19
20    for char in (string split -- "" $argv[1])
21        switch $char
22            case "'" # single-quote
23                switch $state
24                    case normal single-escaped
25                        set state single
26                    case single
27                        set state normal
28                end
29            case '"' # double-quote
30                switch $state
31                    case normal double-escaped
32                        set state double
33                    case double
34                        set state normal
35                end
36            case \\ # backslash escapes the next character
37                switch $state
38                    case double
39                        set state double-escaped
40                    case double-escaped
41                        set state double
42                    case single
43                        set state single-escaped
44                    case single-escaped
45                        set state single
46                    case normal
47                        set state escaped
48                    case escaped
49                        set state normal
50                end
51            case "*" # Any other character
52                switch $state
53                    case escaped
54                        set state normal
55                    case single-escaped
56                        set state single
57                    case double-escaped
58                        set state double
59                end
60        end
61    end
62
63    echo $state
64end