nomsu/tools/autoformat.nom

27 lines
895 B
Plaintext
Raw Normal View History

2018-12-14 20:21:03 -08:00
#!/usr/bin/env nomsu -V5.12.12.8
2018-07-23 15:54:27 -07:00
#
Auto-format Nomsu code. Usage:
2018-11-19 17:37:37 -08:00
nomsu tools/autoformat.nom [-i] file1 file2 directory1 ...
2018-07-23 15:54:27 -07:00
If the first argument is "-i", modifications will be performed in-place. Otherwise,
the formatted code will be printed.
use "lib/os.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2018-12-14 20:21:03 -08:00
$args = (command line args)
for $filename in $args.extra_args:
$file = (read file $filename)
unless $file:
barf "File does not exist: \$filename"
$leading_indent = ($file|matching "[\n]*([ ]*)")
$code = (NomsuCode from ($Source $filename 1 (size of $file)) $file)
$tree = ($code parsed)
$formatted = "
\$leading_indent\((($tree as nomsu)|text)|with "\n" -> "\n\$leading_indent")"
2018-11-19 17:37:37 -08:00
2018-12-14 20:21:03 -08:00
if $args."-i":
write $formatted to file $filename
..else:
2018-12-14 20:21:03 -08:00
say $formatted inline