aboutsummaryrefslogtreecommitdiff
path: root/tools/find.nom
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-12-18 19:23:20 -0800
committerBruce Hill <bruce@bruce-hill.com>2018-12-18 19:24:37 -0800
commit94740a9b414a0fd9af70acb9b8bf3b9576b537e1 (patch)
treed0b18232b285d3f5216dce195fb06b6f3685b070 /tools/find.nom
parentad09f002e84d0e317c9563784b829f6f0102e994 (diff)
Improved command line interface and robustness of tools.
Diffstat (limited to 'tools/find.nom')
-rwxr-xr-xtools/find.nom42
1 files changed, 29 insertions, 13 deletions
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: