code / nomsu

Lines6.6K Lua5.1K PEG1.3K make117
2 others 83
Markdown60 Bourne Again Shell23
(42 lines)
1 #!/usr/bin/env nomsu -V7.0.0
2 ###
3 Auto-format Nomsu code. Usage:
4 nomsu -t format [-i] file1 file2...
6 If the "-i" flag is used, the file will be edited in-place.
7 If the "-q" flag is used and an error occurs, the original file will be printed.
8 If no files are passed in, this will read from stdin.
10 use "filesystem"
11 use "commandline"
13 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 command line program with $args:
16 $filenames = $args.extras
17 if (#$filenames == 0):
18 say ("
19 Warning: reading from stdin (ctrl-d to abort). To avoid this message, use nomsu -t format -
20 ")
21 $filenames = ["stdin"]
23 for $filename in $filenames:
24 $file = (read file $filename)
25 unless $file:
26 fail "File does not exist: \$filename"
27 $leading_indent = ($file, matching "^\n*([ ]*)")
28 $code = (NomsuCode from (Source $filename 1 #$file) $file)
29 try:
30 $tree = ($code parsed)
31 $formatted = ($tree as nomsu, text, indented $leading_indent)
32 ..if it fails with $msg:
33 if $args.q:
34 $formatted = $file
35 ..else:
36 say $msg
37 do next $filename
39 if $args.i:
40 write $formatted to file $filename
41 ..else:
42 say $formatted inline