aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-07-20 20:13:01 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-07-20 20:13:08 -0700
commit2577c4511e4bdf88fab3bc534dc90ef5cee5227c (patch)
treec40826eeac1272dc304124e57c54cb3a8a6e8684
parent385beb4998615d03568f64040bef9761f5cdcbf2 (diff)
Improving nomsu codegen.
-rw-r--r--nomsu_compiler.lua23
-rw-r--r--nomsu_compiler.moon22
-rw-r--r--parser.lua2
-rw-r--r--parser.moon2
4 files changed, 26 insertions, 23 deletions
diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua
index b51605b..f46e75b 100644
--- a/nomsu_compiler.lua
+++ b/nomsu_compiler.lua
@@ -807,15 +807,14 @@ do
end
return nomsu
elseif "Text" == _exp_0 then
- local make_text
- make_text = function(tree)
- local nomsu = NomsuCode(tree.source)
+ local add_text
+ add_text = function(nomsu, tree)
for i, bit in ipairs(tree) do
if type(bit) == 'string' then
- bit = Parser.inline_escape(bit)
- nomsu:append(bit)
+ local escaped = Parser.inline_escape(bit)
+ nomsu:append(Parser.inline_escape(bit))
elseif bit.type == "Text" then
- nomsu:append(make_text(bit))
+ add_text(nomsu, bit)
else
local interp_nomsu = recurse(bit, nomsu)
if bit.type ~= "Var" and bit.type ~= "List" and bit.type ~= "Dict" then
@@ -829,9 +828,10 @@ do
check(len, nomsu, tree)
end
end
- return nomsu
end
- return NomsuCode(tree.source, '"', make_text(tree), '"')
+ local nomsu = NomsuCode(tree.source)
+ add_text(nomsu, tree)
+ return NomsuCode(tree.source, '"', nomsu, '"')
elseif "List" == _exp_0 then
local nomsu = NomsuCode(tree.source, "[")
for i, item in ipairs(tree) do
@@ -1109,6 +1109,7 @@ do
nomsu:append(pop_comments(tree.source.stop, '\n'))
return NomsuCode(tree.source, ":\n ", nomsu)
elseif "Text" == _exp_0 then
+ local max_line = math.floor(1.5 * MAX_LINE)
local add_text
add_text = function(nomsu, tree)
for i, bit in ipairs(tree) do
@@ -1118,12 +1119,12 @@ do
for j, line in ipairs(bit_lines) do
if j > 1 then
nomsu:append("\n")
- elseif #line > 10 and nomsu:trailing_line_len() > MAX_LINE then
+ elseif #line > 10 and nomsu:trailing_line_len() > max_line then
nomsu:append("\\\n..")
end
while #line > 0 do
- local space = MAX_LINE - nomsu:trailing_line_len()
- local split = find(line, " ", space, true)
+ local space = max_line - nomsu:trailing_line_len()
+ local split = find(line, "[%p%s]", space)
if not split or split > space + 10 then
split = space + 10
end
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon
index 302027c..5e68709 100644
--- a/nomsu_compiler.moon
+++ b/nomsu_compiler.moon
@@ -525,14 +525,13 @@ with NomsuCompiler
return nomsu
when "Text"
- make_text = (tree)->
- nomsu = NomsuCode(tree.source)
+ add_text = (nomsu, tree)->
for i, bit in ipairs tree
if type(bit) == 'string'
- bit = Parser.inline_escape(bit)
- nomsu\append bit
+ escaped = Parser.inline_escape(bit)
+ nomsu\append Parser.inline_escape(bit)
elseif bit.type == "Text"
- nomsu\append(make_text(bit))
+ add_text(nomsu, bit)
else
interp_nomsu = recurse(bit, nomsu)
if bit.type != "Var" and bit.type != "List" and bit.type != "Dict"
@@ -541,8 +540,9 @@ with NomsuCompiler
interp_nomsu\parenthesize!
nomsu\append "\\", interp_nomsu
check(len, nomsu, tree) if check
- return nomsu
- return NomsuCode(tree.source, '"', make_text(tree), '"')
+ nomsu = NomsuCode(tree.source)
+ add_text(nomsu, tree)
+ return NomsuCode(tree.source, '"', nomsu, '"')
when "List"
nomsu = NomsuCode(tree.source, "[")
@@ -720,6 +720,8 @@ with NomsuCompiler
return NomsuCode(tree.source, ":\n ", nomsu)
when "Text"
+ -- Multi-line text has more generous wrap margins
+ max_line = math.floor(1.5*MAX_LINE)
add_text = (nomsu, tree)->
for i, bit in ipairs tree
if type(bit) == 'string'
@@ -728,12 +730,12 @@ with NomsuCompiler
for j, line in ipairs bit_lines
if j > 1
nomsu\append "\n"
- elseif #line > 10 and nomsu\trailing_line_len! > MAX_LINE
+ elseif #line > 10 and nomsu\trailing_line_len! > max_line
nomsu\append "\\\n.."
while #line > 0
- space = MAX_LINE - nomsu\trailing_line_len!
- split = find(line, " ", space, true)
+ space = max_line - nomsu\trailing_line_len!
+ split = find(line, "[%p%s]", space)
if not split or split > space + 10
split = space + 10
if #line - split < 10
diff --git a/parser.lua b/parser.lua
index b5fb258..327424f 100644
--- a/parser.lua
+++ b/parser.lua
@@ -234,7 +234,7 @@ end
Parser.is_identifier = function(s)
return not not (NOMSU_DEFS.ident_char ^ 1 * -1):match(s)
end
-local inline_escaper = re.compile("{~ (%utf8_char / ('\\' -> '\\\\') / [ -~] / ('\n' -> '\\n') / ('\t' -> '\\t') / ('\b' -> '\\b') / ('\a' -> '\\a') / ('\v' -> '\\v') / ('\f' -> '\\f') / ('\r' -> '\\r') / ('\"' -> '\\\"') / (. -> escape))* ~}", {
+local inline_escaper = re.compile("{~ (%utf8_char / ('\"' -> '\\\"') / ('\n' -> '\\n') / ('\t' -> '\\t') / ('\b' -> '\\b') / ('\a' -> '\\a') / ('\v' -> '\\v') / ('\f' -> '\\f') / ('\r' -> '\\r') / ('\\' -> '\\\\') / ([^ -~] -> escape) / .)* ~}", {
utf8_char = NOMSU_DEFS.utf8_char,
escape = (function(self)
return ("\\%03d"):format(self:byte())
diff --git a/parser.moon b/parser.moon
index 1998ae2..749c09a 100644
--- a/parser.moon
+++ b/parser.moon
@@ -144,7 +144,7 @@ Parser.is_operator = (s)->
Parser.is_identifier = (s)->
return not not (NOMSU_DEFS.ident_char^1 * -1)\match(s)
-inline_escaper = re.compile "{~ (%utf8_char / ('\\' -> '\\\\') / [ -~] / ('\n' -> '\\n') / ('\t' -> '\\t') / ('\b' -> '\\b') / ('\a' -> '\\a') / ('\v' -> '\\v') / ('\f' -> '\\f') / ('\r' -> '\\r') / ('\"' -> '\\\"') / (. -> escape))* ~}", {utf8_char: NOMSU_DEFS.utf8_char, escape:(=> ("\\%03d")\format(@byte!))}
+inline_escaper = re.compile "{~ (%utf8_char / ('\"' -> '\\\"') / ('\n' -> '\\n') / ('\t' -> '\\t') / ('\b' -> '\\b') / ('\a' -> '\\a') / ('\v' -> '\\v') / ('\f' -> '\\f') / ('\r' -> '\\r') / ('\\' -> '\\\\') / ([^ -~] -> escape) / .)* ~}", {utf8_char: NOMSU_DEFS.utf8_char, escape:(=> ("\\%03d")\format(@byte!))}
Parser.inline_escape = (s)->
return inline_escaper\match(s)
escaper = re.compile "{~ (%utf8_char / ('\\' -> '\\\\') / [\n\r\t -~] / (. -> escape))* ~}", {utf8_char: NOMSU_DEFS.utf8_char, escape:(=> ("\\%03d")\format(@byte!))}