aboutsummaryrefslogtreecommitdiff
path: root/tools/format.nom
blob: df896357c3432df4f7fce62a5a9c8b0b65c0dc16 (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env nomsu -V6.13.12.8
#
    Auto-format Nomsu code. Usage:
    nomsu -t format [-i] file1 file2...
    
    If the "-i" flag is used, the file will be edited in-place.
    If the "-q" flag is used and an error occurs, the original file will be printed.
    If no files are passed in, this will read from stdin.
    
use "lib/os.nom"

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

$filenames = $(COMMAND LINE ARGS).extras
if ((#$filenames) == 0):
    say ("
        Warning: reading from stdin (ctrl-d to abort). To avoid this message, use nomsu -t format -
    ")
    $filenames = ["stdin"]

for $filename in $filenames:
    $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)
    try:
        $tree = ($code parsed)
    ..and if it barfs $msg:
        if $(COMMAND LINE ARGS).q:
            $formatted = $file
        ..else:
            say $msg
    
    if ($tree and (not $formatted)):
        $formatted =
            "\$leading_indent\($tree as nomsu, text, with "\n" -> "\n\$leading_indent")"
    
    if $formatted:
        if $(COMMAND LINE ARGS).i:
            write $formatted to file $filename
        ..else:
            say $formatted inline