aboutsummaryrefslogtreecommitdiff
path: root/tools/autoformat.nom
blob: 76d1d27c05d5769c635ec57ef4add6f7240d5729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env nomsu -V5.12.12.8
#
    Auto-format Nomsu code. Usage:
    nomsu tools/autoformat.nom [-i] file1 file2 directory1 ...
    If the first argument is "-i", modifications will be performed in-place. Otherwise,
    the formatted code will be printed.
    
use "lib/os.nom"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$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")"
    
    if $args."-i":
        write $formatted to file $filename
    ..else:
        say $formatted inline