diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2018-12-30 19:04:34 -0800 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2018-12-30 19:04:45 -0800 |
| commit | 8a3c32408733a2f5e14f8a2dbafa3f980b2f73a1 (patch) | |
| tree | 68f1e8a8b956c33ed24cc7a6a369fd97b8849321 /tools/find.nom | |
| parent | 359152da1772ce501609edd8f84b4985ed3e42f2 (diff) | |
Update to new syntax.
Diffstat (limited to 'tools/find.nom')
| -rwxr-xr-x | tools/find.nom | 74 |
1 files changed, 29 insertions, 45 deletions
diff --git a/tools/find.nom b/tools/find.nom index 4d0c29e..d2678d2 100755 --- a/tools/find.nom +++ b/tools/find.nom @@ -1,20 +1,20 @@ -#!/usr/bin/env nomsu -V5.12.12.8 +#!/usr/bin/env nomsu -V6.12.12.8 # 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... - + + nomsu -t find [flags] "* squared" file1 file2... + Flags: - -l List only the names of the files with matches - --wildcard=<wildcard> Specify a custom wildcard (in case you need to - match an action with a "*" in the name) - + -l List only the names of the files with matches + --wildcard=<wildcard> Specify a custom wildcard (in case you need to + match an action with a "*" in the name) + Output: - <filename>:<line number>: - <matching lines> + <filename>:<line number>: + <matching lines> use "lib/os.nom" use "lib/consolecolor.nom" @@ -25,48 +25,32 @@ $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 = ($pattern, with "\$wildcard\$wildcard" -> "$multi_wildcard") +$pattern = ($pattern, with $wildcard -> "$wildcard") $pattern_tree = ($pattern parsed) - ($tree matches $patt) means: when: - (not ($tree is syntax tree)): - return (no) - - (($patt.type == "Var") and ($patt.1 == "wildcard")): - return (yes) - - ($tree.type != $patt.type): - return (no) - + (not ($tree is syntax tree)): return (no) + (($patt.type == "Var") and ($patt.1 == "wildcard")): return (yes) + ($tree.type != $patt.type): return (no) ($tree.type == "Action"): - if (($tree|get stub) != ($patt|get stub)): - return (no) - + if (($tree, get stub) != ($patt, get stub)): return (no) + for $ in 1 to (#$patt): if ($patt.$ is syntax tree): - if ($patt.$ == \$multi_wildcard): - return (yes) - - unless ($tree.$ matches $patt.$): - return (no) + if ($patt.$ == \$multi_wildcard): return (yes) + unless ($tree.$ matches $patt.$): return (no) ..else: - unless ($tree.$ == $patt.$): - return (no) - - if ((#$tree) != (#$patt)): - return (no) - + unless ($tree.$ == $patt.$): return (no) + + if ((#$tree) != (#$patt)): return (no) return (yes) - -$filenames = ($(COMMAND LINE ARGS).extras|from 2 to -1) +$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): +for $filename in $filenames: $file = (read file $filename) unless $file: barf "File does not exist: \$filename" @@ -83,15 +67,15 @@ for $filename in ($filenames): $results = [] for $t in recursive $tree: if ($t matches $pattern_tree): - $line_num = ($file|line number at $t.source.start) - $results| - add {line: $line_num, text: "\(blue "\$filename:\$line_num:")\n\(source lines of $t)"} + $line_num = ($file, line number at $t.source.start) + $results, + add {.line = $line_num, .text = "\(blue "\$filename:\$line_num:")\n\(source lines of $t)"} for $sub in $t: 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: |
