diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2018-12-18 19:23:20 -0800 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2018-12-18 19:24:37 -0800 |
| commit | 94740a9b414a0fd9af70acb9b8bf3b9576b537e1 (patch) | |
| tree | d0b18232b285d3f5216dce195fb06b6f3685b070 /tools | |
| parent | ad09f002e84d0e317c9563784b829f6f0102e994 (diff) | |
Improved command line interface and robustness of tools.
Diffstat (limited to 'tools')
| -rwxr-xr-x | tools/autoformat.nom | 17 | ||||
| -rwxr-xr-x | tools/find.nom | 42 | ||||
| -rwxr-xr-x | tools/parse.nom | 2 | ||||
| -rwxr-xr-x | tools/test.nom | 6 | ||||
| -rwxr-xr-x | tools/upgrade.nom | 11 |
5 files changed, 49 insertions, 29 deletions
diff --git a/tools/autoformat.nom b/tools/autoformat.nom index 76d1d27..d878a02 100755 --- a/tools/autoformat.nom +++ b/tools/autoformat.nom @@ -1,16 +1,21 @@ #!/usr/bin/env nomsu -V5.12.12.8 # Auto-format Nomsu code. Usage: - nomsu tools/autoformat.nom [-i] file1 file2 directory1 ... - If the first argument is "-i", modifications will be performed in-place. Otherwise, - the formatted code will be printed. + nomsu -t autoformat [-i] file1 file2... + + If the "-i" flag is used, the file will be edited in-place. + If no files are passed in, this will read from stdin. use "lib/os.nom" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -$args = (command line args) -for $filename in $args.extra_args: +$filenames = $(COMMAND LINE ARGS).extras +if ((#$filenames) == 0): + say "Warning: reading from stdin (ctrl-d to abort). To avoid this message, use nomsu -t autoformat -" + $filenames = ["stdin"] + +for $filename in $filenames: $file = (read file $filename) unless $file: barf "File does not exist: \$filename" @@ -20,7 +25,7 @@ for $filename in $args.extra_args: $formatted = " \$leading_indent\((($tree as nomsu)|text)|with "\n" -> "\n\$leading_indent")" - if $args."-i": + if $(COMMAND LINE ARGS)."-i": write $formatted to file $filename ..else: say $formatted inline diff --git a/tools/find.nom b/tools/find.nom index 84f69b4..aeb535d 100755 --- a/tools/find.nom +++ b/tools/find.nom @@ -1,6 +1,9 @@ #!/usr/bin/env nomsu -V5.12.12.8 # - This is a tool to find syntax trees matching a pattern. ("*" is a wildcard) + This is a tool to find syntax trees matching a pattern. "*" is a wildcard + that will match any subtree, and "**" is a wildcard that will match any + 0 or more subtrees. "**" is greedy, so extra arguments after it will + not match. nomsu -t find [flags] "* squared" file1 file2... @@ -18,12 +21,21 @@ use "lib/consolecolor.nom" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -$wildcard = ((command line args)."--wildcard" or "%*") -$pattern = ((command line args).extra_args.1|with $wildcard -> "$wildcard") +$wildcard = ($(COMMAND LINE ARGS)."--wildcard" or "%*") +$pattern = $(COMMAND LINE ARGS).extras.1 +if (any of [not $pattern, $pattern == "*", $pattern == "**"]): + barf "Usage: nomsu -t find [-l] [--wildcard=<wildcard>] <pattern>, where <pattern> is valid Nomsu code" + +$pattern = ($pattern|with "\$wildcard\$wildcard" -> "$multi_wildcard") +$pattern = ($pattern|with $wildcard -> "$wildcard") $pattern_tree = ($pattern parsed) +say $pattern_tree ($tree matches $patt) means: when: + (not ($tree is syntax tree)): + return (no) + (($patt.type == "Var") and ($patt.1 == "wildcard")): return (yes) @@ -33,25 +45,29 @@ $pattern_tree = ($pattern parsed) ($tree.type == "Action"): if (($tree|get stub) != ($patt|get stub)): return (no) - ..else: - if ((#$tree) < (#$patt)): - return (no) - - ((#$tree) != (#$patt)): - return (no) for $ in 1 to (#$patt): if ($patt.$ is syntax tree): + if ($patt.$ == \$multi_wildcard): + return (yes) + unless ($tree.$ matches $patt.$): return (no) ..else: unless ($tree.$ == $patt.$): return (no) + if ((#$tree) != (#$patt)): + return (no) + return (yes) -for $ in 2 to (size of (command line args).extra_args): - $filename = (command line args).extra_args.$ +$filenames = ($(COMMAND LINE ARGS).extras|from 2 to -1) +if ((#$filenames) == 0): + say "Warning: searching stdin (ctrl-d to abort). To avoid this message, use nomsu -t find -" + $filenames = ["stdin"] + +for $filename in ($filenames): $file = (read file $filename) unless $file: barf "File does not exist: \$filename" @@ -63,7 +79,7 @@ for $ in 2 to (size of (command line args).extra_args): $tree = (nil) unless $tree: - do next $ + do next $filename $results = [] for $t in recursive $tree: @@ -76,7 +92,7 @@ for $ in 2 to (size of (command line args).extra_args): if ($sub is syntax tree): recurse $t on $sub - if ((command line args)."-l"): + if ($(COMMAND LINE ARGS)."-l"): if ((#$results) > 0): say $filename ..else: diff --git a/tools/parse.nom b/tools/parse.nom index e6768a6..1005983 100755 --- a/tools/parse.nom +++ b/tools/parse.nom @@ -36,7 +36,7 @@ externally (print tree $t at indent $indent) means: else: say "\$indent \(quote $arg)" -for $filename in (command line args).extra_args: +for $filename in $(COMMAND LINE ARGS).extras: $file = (read file $filename) unless $file: barf "File does not exist: \$filename" diff --git a/tools/test.nom b/tools/test.nom index 5b36010..7b5842c 100755 --- a/tools/test.nom +++ b/tools/test.nom @@ -9,9 +9,9 @@ use "lib/consolecolor.nom" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Make sure all the files get run -for $filename in (command line args).extra_args: use $filename -$tests = {: for $s = $t in (tests): add (=lua "Source:from_string(\$s)") = $t} -for $filename in (command line args).extra_args: +for $filename in $(COMMAND LINE ARGS).extras: use $filename +$tests = {: for $s = $t in $TESTS: add (=lua "Source:from_string(\$s)") = $t} +for $filename in $(COMMAND LINE ARGS).extras: $file = (read file $filename) $version = ($file|matching "#![^\n]* nomsu %-V[ ]*([^\n]*)") $file_tests = [] diff --git a/tools/upgrade.nom b/tools/upgrade.nom index 9244203..96a1312 100755 --- a/tools/upgrade.nom +++ b/tools/upgrade.nom @@ -11,12 +11,11 @@ use "lib/consolecolor.nom" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -$args = (command line args) -$inplace = ($args."-i" or $args."--inplace") -$start_version = $args."--upgrade-from" -$version = ($args."--upgrade-to" or (Nomsu version)) -$test = ($args."-t" or $args."--test") -for $filename in $args.extra_args: +$inplace = ($(COMMAND LINE ARGS)."-i" or $(COMMAND LINE ARGS)."--inplace") +$start_version = $(COMMAND LINE ARGS)."--upgrade-from" +$version = ($(COMMAND LINE ARGS)."--upgrade-to" or (Nomsu version)) +$test = ($(COMMAND LINE ARGS)."-t" or $(COMMAND LINE ARGS)."--test") +for $filename in $(COMMAND LINE ARGS).extras: $file = (read file $filename) unless $file: barf "File does not exist: \$filename" |
