Last active
December 9, 2016 06:45
-
-
Save colstrom/26fad5fd29646258f64dff63e22b5eef to your computer and use it in GitHub Desktop.
sonar-lexer.fish: a lexer for fish, written in fish
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name = sonar-lexer | |
| author = Chris Olstrom | |
| license = MIT | |
| provides = fish/functions/sonar-lexer | |
| requires | |
| fish/builtin/and | |
| fish/builtin/begin | |
| fish/builtin/builtin | |
| fish/builtin/command | |
| fish/builtin/commandline | |
| fish/builtin/else | |
| fish/builtin/end | |
| fish/builtin/for | |
| fish/builtin/function | |
| fish/builtin/functions | |
| fish/builtin/if | |
| fish/builtin/printf | |
| fish/builtin/read | |
| fish/builtin/set | |
| fish/builtin/string | |
| fish/builtin/while | |
| fish/functions/isatty | |
| fish/functions/sonar-lexer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sonar-lexer | |
| if isatty stdin | |
| for token in (commandline --tokenize --input "$argv") | |
| set --local unmatched | |
| set --query unmatched | |
| and begin | |
| if string match --quiet -- '\*' "$token" | |
| printf "%s\t%s\n" t_asterisk "$token" | |
| set --erase unmatched | |
| end | |
| end | |
| set --query unmatched | |
| and begin | |
| if string match --quiet -- ';' "$token" | |
| printf "%s\t%s\n" t_semicolon "$token" | |
| set --erase unmatched | |
| end | |
| end | |
| set --query unmatched | |
| and begin | |
| if string match --quiet -- '$*' "$token" | |
| printf "%s\t%s\n" t_variable "$token" | |
| set --erase unmatched | |
| end | |
| end | |
| set --query unmatched | |
| and begin | |
| if string match --quiet -- '{*}' "$token" | |
| printf "%s\t%s\n" t_brace_exp "$token" | |
| set --erase unmatched | |
| end | |
| end | |
| set --query unmatched | |
| and begin | |
| if builtin --names | string match --quiet -- "$token" | |
| printf "%s\t%s\n" t_builtin "$token" | |
| set --erase unmatched | |
| end | |
| end | |
| set --query unmatched | |
| and begin | |
| if functions --query -- "$token" | |
| printf "%s\t%s\n" t_function "$token" | |
| set --erase unmatched | |
| end | |
| end | |
| set --query unmatched | |
| and begin | |
| if command --search -- "$token" >/dev/null | |
| printf "%s\t%s\n" t_command "$token" | |
| set --erase unmatched | |
| end | |
| end | |
| set --query unmatched | |
| and begin | |
| if string match --quiet --regex -- '^--?[^[:space:]]+' "$token" | |
| printf "%s\t%s\n" t_option "$token" | |
| set --erase unmatched | |
| end | |
| end | |
| set --query unmatched | |
| and begin | |
| if string match --quiet -- "(*)" "$token" | |
| sonar-lexer (string replace --regex '^\(' '' (string replace --regex '\)$' '' -- "$token")) | |
| set --erase unmatched | |
| end | |
| end | |
| set --query unmatched | |
| and printf "%s\t%s\n" t_string "$token" | |
| end | |
| else | |
| while read line | |
| sonar-lexer "$line" | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment