123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 | #!/usr/bin/env nomsu -V8.0.0
###
This is a tool to find syntax trees matching a pattern. In the pattern, "*"
or a variable matches any subtree, and "**" matches all of an action's
remaining arguments (see the "tree_search" library).
nomsu -t find [flags] "* squared" file1 file2...
Flags:
-l List only the names of the files with matches
--literal="$x $y" Make these variables only match themselves
Output:
<filename>:<line number>:
<matching lines>
use "filesystem"
use "consolecolor"
use "commandline"
use "tree_search"
command line program with $args:
unless $args.extras.1:
usage "nomsu -t find [-l] [--literal=\"$x $y\"] <pattern> files..."
$pattern = (pattern $args.extras.1 with literals $args.literal)
for $filename in (files to search ($args.extras from 2 to -1)):
{$file, $tree, $error} = (read and parse $filename)
if $error:
say (red "\$filename failed to parse:\n\$error")
do next $filename
$results = {}
for $t in recursive $tree:
if ($t matches $pattern):
$line_num = ($file, line number at $t.source.start)
$source = (source lines of $t)
$indent = ($source, matching "^ *")
$source = ($source, from (#$indent + 1), with "\n\$indent" -> "\n")
add {.line = $line_num, .text = "\(blue "\$filename:\$line_num:")\n\$source"} to $results
for $sub in $t:
if ($sub is syntax tree):
recurse $t on $sub
if $args.l:
if (#$results > 0):
say $filename
..else:
sort $results by $ -> $.line
for $ in $results:
say $.text
|