Skip to content

Instantly share code, notes, and snippets.

@colstrom
Last active December 9, 2016 06:45
Show Gist options
  • Select an option

  • Save colstrom/26fad5fd29646258f64dff63e22b5eef to your computer and use it in GitHub Desktop.

Select an option

Save colstrom/26fad5fd29646258f64dff63e22b5eef to your computer and use it in GitHub Desktop.
sonar-lexer.fish: a lexer for fish, written in fish
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
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