aboutsummaryrefslogtreecommitdiff
path: root/tools/find.nom
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-12-18 18:49:15 -0800
committerBruce Hill <bruce@bruce-hill.com>2018-12-18 18:49:15 -0800
commitad09f002e84d0e317c9563784b829f6f0102e994 (patch)
tree66016fcd813bcdbe5864b624b3b1f8083b0642bf /tools/find.nom
parent046767e240a9abef1837ccfd16bf185491c7e0fb (diff)
Renamed find_action to find because it finds anything now.
Diffstat (limited to 'tools/find.nom')
-rwxr-xr-xtools/find.nom85
1 files changed, 85 insertions, 0 deletions
diff --git a/tools/find.nom b/tools/find.nom
new file mode 100755
index 0000000..84f69b4
--- /dev/null
+++ b/tools/find.nom
@@ -0,0 +1,85 @@
+#!/usr/bin/env nomsu -V5.12.12.8
+#
+ This is a tool to find syntax trees matching a pattern. ("*" is a wildcard)
+
+ 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)
+
+ Output:
+ <filename>:<line number>:
+ <matching lines>
+
+use "lib/os.nom"
+use "lib/consolecolor.nom"
+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+$wildcard = ((command line args)."--wildcard" or "%*")
+$pattern = ((command line args).extra_args.1|with $wildcard -> "$wildcard")
+$pattern_tree = ($pattern parsed)
+
+($tree matches $patt) means:
+ when:
+ (($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)
+ ..else:
+ if ((#$tree) < (#$patt)):
+ return (no)
+
+ ((#$tree) != (#$patt)):
+ return (no)
+
+ for $ in 1 to (#$patt):
+ if ($patt.$ is syntax tree):
+ unless ($tree.$ matches $patt.$):
+ return (no)
+ ..else:
+ unless ($tree.$ == $patt.$):
+ return (no)
+
+ return (yes)
+
+for $ in 2 to (size of (command line args).extra_args):
+ $filename = (command line args).extra_args.$
+ $file = (read file $filename)
+ unless $file:
+ barf "File does not exist: \$filename"
+ $code = (NomsuCode from ($Source $filename 1 (size of $file)) $file)
+ try:
+ $tree = ($code parsed)
+ ..and if it barfs $msg:
+ say (red "\$filename failed to parse:\n\$msg")
+ $tree = (nil)
+
+ unless $tree:
+ do next $
+
+ $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)"}
+
+ for $sub in $t:
+ if ($sub is syntax tree):
+ recurse $t on $sub
+
+ if ((command line args)."-l"):
+ if ((#$results) > 0):
+ say $filename
+ ..else:
+ sort $results by $ -> $.line
+ for $ in $results:
+ say $.text