diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index e967172..fdbae61 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -13,7 +13,7 @@ immediately end stubs = repr(stubs); if #stubs > 80 then - lua:append("\\n ",stubs,",\\n "); + lua:append("\n ",stubs,",\n "); else lua:append(stubs,", "); end @@ -30,7 +30,7 @@ immediately body_lua:convert_to_statements("return "); body_lua:remove_free_vars(unpack(args)); body_lua:declare_locals(); - lua:append(")\\n ", body_lua, "\\nend);"); + lua:append(")\n ", body_lua, "\nend);"); return lua; end); @@ -45,7 +45,7 @@ immediately end stubs = repr(stubs); if #stubs > 80 then - lua:append("\\n ",stubs,",\\n "); + lua:append("\n ",stubs,",\n "); else lua:append(stubs,", "); end @@ -62,7 +62,7 @@ immediately body_lua:convert_to_statements("return "); body_lua:remove_free_vars(unpack(args)); body_lua:declare_locals(); - lua:append(")\\n ", body_lua, "\\nend);") + lua:append(")\n ", body_lua, "\nend);") return lua; # Macro to make nomsu macros @@ -76,7 +76,7 @@ immediately end stubs = repr(stubs); if #stubs > 80 then - lua:append("\\n ",stubs,",\\n "); + lua:append("\n ",stubs,",\n "); else lua:append(stubs,", "); end diff --git a/core/operators.nom b/core/operators.nom index a9deec1..480a75f 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -123,7 +123,7 @@ immediately vars[i] = tostring(target_lua); end \%lua:remove_free_vars(vars); - \%lua:prepend("local ", lhs, " = ", rhs, ";\\n"); + \%lua:prepend("local ", lhs, " = ", rhs, ";\n"); return Lua ".." do diff --git a/nomsu.lua b/nomsu.lua index 0c1ccbb..0730724 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -165,9 +165,9 @@ do local text_loc = lpeg.userdata.source_code.source:sub(pos, pos) line_no = text_loc:get_line_number() src = FILE_CACHE[text_loc.filename] - local prev_line = src:sub(LINE_STARTS[src][line_no - 1] or 1, LINE_STARTS[src][line_no] - 1) - local err_line = src:sub(LINE_STARTS[src][line_no], (LINE_STARTS[src][line_no + 1] or 0) - 1) - local next_line = src:sub(LINE_STARTS[src][line_no + 1] or -1, (LINE_STARTS[src][line_no + 2] or 0) - 1) + local prev_line = src:sub(LINE_STARTS[src][line_no - 1] or 1, LINE_STARTS[src][line_no] - 2) + local err_line = src:sub(LINE_STARTS[src][line_no], (LINE_STARTS[src][line_no + 1] or 0) - 2) + local next_line = src:sub(LINE_STARTS[src][line_no + 1] or -1, (LINE_STARTS[src][line_no + 2] or 0) - 2) local pointer = ("-"):rep(pos - LINE_STARTS[src][line_no]) .. "^" err_msg = (err_msg or "Parse error") .. " in " .. tostring(lpeg.userdata.source_code.source.filename) .. " on line " .. tostring(line_no) .. ":\n" err_msg = err_msg .. "\n" .. tostring(prev_line) .. "\n" .. tostring(err_line) .. "\n" .. tostring(pointer) .. "\n" .. tostring(next_line) .. "\n" diff --git a/nomsu.moon b/nomsu.moon index 9441414..7e94ecd 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -35,7 +35,6 @@ debug_getinfo = debug.getinfo -- Do a pass on all actions to enforce parameters-are-nouns heuristic -- Maybe do some sort of lazy definitions of actions that defer until they're used in code -- Add a ((%x foo %y) where {x:"asdf", y:"fdsa"}) compile-time action for substitution --- Allow plain text backslash like: "\n" in indented text without requiring "\\n" -- Maybe support some kind of regex action definitions like "foo %first (and %next)*"? -- Re-implement nomsu-to-lua comment translation? @@ -155,9 +154,9 @@ NOMSU_DEFS = with {} text_loc = lpeg.userdata.source_code.source\sub(pos,pos) line_no = text_loc\get_line_number! src = FILE_CACHE[text_loc.filename] - prev_line = src\sub(LINE_STARTS[src][line_no-1] or 1, LINE_STARTS[src][line_no]-1) - err_line = src\sub(LINE_STARTS[src][line_no], (LINE_STARTS[src][line_no+1] or 0)-1) - next_line = src\sub(LINE_STARTS[src][line_no+1] or -1, (LINE_STARTS[src][line_no+2] or 0)-1) + prev_line = src\sub(LINE_STARTS[src][line_no-1] or 1, LINE_STARTS[src][line_no]-2) + err_line = src\sub(LINE_STARTS[src][line_no], (LINE_STARTS[src][line_no+1] or 0)-2) + next_line = src\sub(LINE_STARTS[src][line_no+1] or -1, (LINE_STARTS[src][line_no+2] or 0)-2) pointer = ("-")\rep(pos-LINE_STARTS[src][line_no]) .. "^" err_msg = (err_msg or "Parse error").." in #{lpeg.userdata.source_code.source.filename} on line #{line_no}:\n" err_msg ..="\n#{prev_line}\n#{err_line}\n#{pointer}\n#{next_line}\n" diff --git a/nomsu.peg b/nomsu.peg index b1c1f50..731a83c 100644 --- a/nomsu.peg +++ b/nomsu.peg @@ -11,20 +11,20 @@ statement: action / expression inline_statement: inline_action / inline_expression inline_block (Block): - {| inline_statement (";" inline_statement)+ |} + {| inline_statement (%ws* ";" %ws* inline_statement)+ |} -> Tuple block (Block): {| statement (nodent statement)+ |} -> Tuple inline_nomsu (Nomsu): "\" noindex_inline_expression indented_nomsu (Nomsu): - "\" (noindex_inline_expression / (":" %ws* (inline_action / inline_expression) eol) / indented_expression) + "\" (noindex_inline_expression / (":" %ws* (inline_block / inline_action / inline_expression) eol) / indented_expression) index_chain (IndexChain): {| noindex_inline_expression ("." ((({} ({|{%operator / (!number plain_word)}|} -> Tuple) {}) -> Text) / noindex_inline_expression))+ |} -> Tuple noindex_inline_expression: number / variable / inline_text / inline_list / inline_dict / inline_nomsu - / ("(" %ws* (inline_action / inline_expression) %ws* ")") + / ("(" %ws* (inline_block / inline_action / inline_expression) %ws* ")") inline_expression: index_chain / noindex_inline_expression @@ -36,12 +36,12 @@ indented_expression: / block (dedent / (("" -> "Error while parsing indented expression") => error))) ) expression: - inline_expression / (":" %ws* (inline_action / inline_expression) eol) / indented_expression + inline_expression / (":" %ws* (inline_block / inline_action / inline_expression) eol) / indented_expression -- Function calls need at least one word in them inline_action (Action): {| (inline_expression %ws*)* word (%ws* (inline_expression / word))* - (%ws* ":" %ws* (inline_action / inline_expression))?|} -> Tuple + (%ws* ":" %ws* (inline_block / inline_action / inline_expression))?|} -> Tuple action (Action): {| (expression (dotdot / %ws*))* word ((dotdot / %ws*) (expression / word))* |} -> Tuple @@ -58,20 +58,17 @@ indented_text (Text): '".."' eol %nl ({| {~ (%nl*) (%indent -> "") ~} ({~ - (("\\" -> "\") / (("\" eol %nl+ %nodent "..") -> "") - / (%nl+ {~ %nodent -> "" ~}) / [^%nl\])+ + (("\\" -> "\") / (("\" nodent "..") -> "")/ (%nl+ {~ %nodent -> "" ~}) / [^%nl\] / (!text_interpolation "\"))+ ~} / text_interpolation)* |} -> Tuple) (((!.) &%dedent) / (&(%nl %dedent)) / (("" -> "Error while parsing Text") => error)) inline_text_interpolation: "\" ( variable / inline_list / inline_dict / inline_text - / ("(" %ws* (inline_action / inline_expression) %ws* ")") + / ("(" %ws* (inline_block / inline_action / inline_expression) %ws* ")") ) text_interpolation: inline_text_interpolation / - ("\" - (block_comment / line_comment / indented_expression)? - nodent "..") + ("\" indented_expression nodent "..") number (Number): (("-"? (([0-9]+ "." [0-9]+) / ("." [0-9]+) / ([0-9]+)))-> tonumber) @@ -90,7 +87,7 @@ indented_list (List): list_line: ((action / expression) !comma) / (inline_list_item (comma list_line?)?) -inline_list_item: inline_action / inline_expression +inline_list_item: inline_block / inline_action / inline_expression inline_dict (Dict): !('{..}') @@ -104,7 +101,7 @@ dict_line: ((dict_key %ws* ":" %ws* (action / expression)) -> DictEntry !comma) / (inline_dict_item (comma dict_line?)?) inline_dict_item: - (dict_key %ws* ":" %ws* (inline_action / inline_expression)) -> DictEntry + (dict_key %ws* ":" %ws* (inline_block / inline_action / inline_expression)) -> DictEntry dict_key: (({} ({|{%operator / (!number plain_word)}|} -> Tuple) {}) -> Text) / inline_expression diff --git a/tests/text.nom b/tests/text.nom index 004a630..72eebac 100644 --- a/tests/text.nom +++ b/tests/text.nom @@ -22,3 +22,34 @@ immediately "\(%)世界" assume ((%こんにちは と言う) = "こんにちは世界") or barf "Unicode doesn't work" + +%s <- ".." + one two\nthree\ + ..four +assume (%s = "one two\\nthreefour") +%s <- ".." + list:\[..] + 1,2,3 + .. +assume (%s = "list:{1, 2, 3}") + +assume + ".." + foo = \ + 1 + 2 + ..! + ..= "foo = 3!" + +assume + ".." + one\"\n"two + ..= "one\ntwo" + +assume + ".." + no\ # Comment + #comment + #.. + block comment + ..gap + ..= "nogap"