diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2019-01-15 17:48:14 -0800 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2019-01-15 17:48:35 -0800 |
| commit | 5c9674880206f66c109adc1bedf8f786166f125f (patch) | |
| tree | 30d95a1aacd2b30f8cb060846cbe9efd09e31aca | |
| parent | f6fe028f1786d7e582623fc26df916fcd05c2e31 (diff) | |
Fix for formatting indented code with the format tool.
| -rwxr-xr-x | lib/tools/format.nom | 8 | ||||
| -rw-r--r-- | nomsu.2.peg | 6 | ||||
| -rw-r--r-- | nomsu.3.peg | 6 | ||||
| -rw-r--r-- | nomsu.4.peg | 6 | ||||
| -rw-r--r-- | nomsu.5.peg | 6 | ||||
| -rw-r--r-- | nomsu.6.peg | 7 | ||||
| -rw-r--r-- | nomsu_decompiler.lua | 3 | ||||
| -rw-r--r-- | nomsu_decompiler.moon | 1 |
8 files changed, 6 insertions, 37 deletions
diff --git a/lib/tools/format.nom b/lib/tools/format.nom index eee1316..c2e6a1c 100755 --- a/lib/tools/format.nom +++ b/lib/tools/format.nom @@ -24,19 +24,19 @@ command line program with $args: $file = (read file $filename) unless $file: fail "File does not exist: \$filename" - $leading_indent = ($file, matching "\n*([ ]*)") + $leading_indent = ($file, matching "^\n*([ ]*)") $code = (NomsuCode from ($Source $filename 1 (size of $file)) $file) try: $tree = ($code parsed) ..if it fails with $msg: + $tree = (nil) if $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 $tree: + $formatted = ($tree as nomsu, text, indented $leading_indent) if $formatted: if $args.i: diff --git a/nomsu.2.peg b/nomsu.2.peg index b6d1687..64db7fa 100644 --- a/nomsu.2.peg +++ b/nomsu.2.peg @@ -9,22 +9,16 @@ file <- shebang <- "#!" (!"nomsu" [^%nl])* "nomsu" ws+ "-V" ws* {:version: [0-9.]+ :} [^%nl]* file_chunks (FileChunks) <- - {:curr_indent: ' '* :} shebang? comment? blank_lines? (top_block (nl_nodent section_division top_block)*) blank_lines? ws* unexpected_chunk? - {:curr_indent: %nil :} top_block (Block) <- - {:curr_indent: ' '* :} comment? blank_lines? statement (nl_nodent statement)* - {:curr_indent: %nil :} empty_block (Block) <- - {:curr_indent: ' '* :} comment? blank_lines? - {:curr_indent: %nil :} nodent <- (unexpected_indent [^%nl]* / =curr_indent) indent <- =curr_indent " " diff --git a/nomsu.3.peg b/nomsu.3.peg index d0b2dc9..655ed11 100644 --- a/nomsu.3.peg +++ b/nomsu.3.peg @@ -9,22 +9,16 @@ file <- shebang <- "#!" (!"nomsu" [^%nl])* "nomsu" ws+ "-V" ws* {:version: [0-9.]+ :} [^%nl]* file_chunks (FileChunks) <- - {:curr_indent: ' '* :} shebang? comment? blank_lines? (top_block (nl_nodent section_division top_block)*) blank_lines? ws* unexpected_chunk? - {:curr_indent: %nil :} top_block (Block) <- - {:curr_indent: ' '* :} comment? blank_lines? statement (nl_nodent statement)* - {:curr_indent: %nil :} empty_block (Block) <- - {:curr_indent: ' '* :} comment? blank_lines? - {:curr_indent: %nil :} nodent <- (unexpected_indent [^%nl]* / =curr_indent) indent <- =curr_indent " " diff --git a/nomsu.4.peg b/nomsu.4.peg index bcfc972..873638a 100644 --- a/nomsu.4.peg +++ b/nomsu.4.peg @@ -11,22 +11,16 @@ shebang <- "#!" (!"nomsu" [^%nl])* "nomsu" ws+ "-V" ws* [0-9.]+ [^%nl]* (%nl / ! eof <- !. file_chunks (FileChunks) <- - {:curr_indent: ' '* :} {:shebang: shebang :}? (top_block (nl_nodent section_division top_block)*) blank_lines? ws* unexpected_chunk? - {:curr_indent: %nil :} top_block (Block) <- - {:curr_indent: ' '* :} comment? blank_lines? statement (nl_nodent statement)* - {:curr_indent: %nil :} empty_block (Block) <- - {:curr_indent: ' '* :} comment? blank_lines? - {:curr_indent: %nil :} nodent <- (unexpected_indent [^%nl]* / =curr_indent) indent <- {~ =curr_indent (ws / (%tab -> ' '))+ ~} diff --git a/nomsu.5.peg b/nomsu.5.peg index 772ca75..751e033 100644 --- a/nomsu.5.peg +++ b/nomsu.5.peg @@ -11,22 +11,16 @@ shebang <- "#!" (!"nomsu" [^%nl])* "nomsu" ws+ "-V" ws* [0-9.]+ [^%nl]* (%nl / ! eof <- !. file_chunks (FileChunks) <- - {:curr_indent: ' '* :} {:shebang: shebang :}? (top_block (nl_nodent section_division top_block)*) blank_lines? ws* unexpected_chunk? - {:curr_indent: %nil :} top_block (Block) <- - {:curr_indent: ' '* :} comment? blank_lines? statement (nl_nodent statement)* - {:curr_indent: %nil :} empty_block (Block) <- - {:curr_indent: ' '* :} comment? blank_lines? - {:curr_indent: %nil :} nodent <- (unexpected_indent [^%nl]* / =curr_indent) indent <- {~ =curr_indent (ws / (%tab -> ' '))+ ~} diff --git a/nomsu.6.peg b/nomsu.6.peg index 9a9393f..39b0629 100644 --- a/nomsu.6.peg +++ b/nomsu.6.peg @@ -1,20 +1,19 @@ --- Nomsu version 5 +-- Nomsu version 6 file <- {:curr_indent: ' '* :} (((methodchain / action / expression / inline_block) eol !.) / file_chunks / comment? blank_lines?) {:curr_indent: %nil :} + (unexpected_code .+)? !. shebang <- "#!" (!"nomsu" [^%nl])* "nomsu" ws+ "-V" ws* [0-9.]+ [^%nl]* (%nl / !.) file_chunks (FileChunks) <- - {:curr_indent: ' '* :} {:shebang: shebang :}? (top_block (nl_nodent section_division top_block)*) blank_lines? ws* unexpected_chunk? - {:curr_indent: %nil :} section_division <- ("~")^+3 eol @@ -32,9 +31,7 @@ comment (Comment) <- top_block (Block) <- - {:curr_indent: ' '* :} comment? blank_lines? statement (nl_nodent statement)* - {:curr_indent: %nil :} inline_block (Block) <- ":" ws* (inline_statement (ws* ";" ws* inline_statement)*)? diff --git a/nomsu_decompiler.lua b/nomsu_decompiler.lua index 9224f25..164470f 100644 --- a/nomsu_decompiler.lua +++ b/nomsu_decompiler.lua @@ -352,9 +352,6 @@ tree_to_nomsu = function(tree) nomsu:add(tree_to_nomsu(chunk)) end end - if not (nomsu:match("\n$")) then - nomsu:add('\n') - end return nomsu elseif "Action" == _exp_0 then local next_space = "" diff --git a/nomsu_decompiler.moon b/nomsu_decompiler.moon index b88a766..297a518 100644 --- a/nomsu_decompiler.moon +++ b/nomsu_decompiler.moon @@ -251,7 +251,6 @@ tree_to_nomsu = (tree)-> else nomsu\add tree_to_nomsu(chunk) - nomsu\add('\n') unless nomsu\match("\n$") return nomsu when "Action" |
