From b6d78421e8eadd128d336bba1430153e9d367c19 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sun, 2 Feb 2020 21:30:47 +0800 Subject: [PATCH 1/4] fix issue #406, and add escape new line support. --- moonscript/parse.lua | 2 +- moonscript/parse.moon | 2 +- moonscript/parse/literals.lua | 3 ++- moonscript/parse/literals.moon | 4 +++- moonscript/util.lua | 2 +- moonscript/util.moon | 2 +- spec/inputs/syntax.moon | 13 +++++++++++++ spec/outputs/syntax.lua | 11 ++++++++++- 8 files changed, 32 insertions(+), 7 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index da6731e7..8fdbef3e 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -127,7 +127,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) WithExp = Ct(ExpList) * Assign ^ -1 / format_assign, With = key("with") * DisableDo * ensure(WithExp, PopDo) * key("do") ^ -1 * Body / mark("with"), Switch = key("switch") * DisableDo * ensure(Exp, PopDo) * key("do") ^ -1 * Space ^ -1 * Break * SwitchBlock / mark("switch"), - SwitchBlock = EmptyLine ^ 0 * Advance * Ct(SwitchCase * (Break ^ 1 * SwitchCase) ^ 0 * (Break ^ 1 * SwitchElse) ^ -1) * PopIndent, + SwitchBlock = EmptyLine ^ 0 * Advance * Ct(SwitchCase * (SpaceBreak ^ 1 * SwitchCase) ^ 0 * (SpaceBreak ^ 1 * SwitchElse) ^ -1) * PopIndent, SwitchCase = key("when") * Ct(ExpList) * key("then") ^ -1 * Body / mark("case"), SwitchElse = key("else") * Body / mark("else"), IfCond = Exp * Assign ^ -1 / format_single_assign, diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 7fd5de6f..e788cb1b 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -145,7 +145,7 @@ build_grammar = wrap_env debug_grammar, (root) -> Switch: key"switch" * DisableDo * ensure(Exp, PopDo) * key"do"^-1 * Space^-1 * Break * SwitchBlock / mark"switch" - SwitchBlock: EmptyLine^0 * Advance * Ct(SwitchCase * (Break^1 * SwitchCase)^0 * (Break^1 * SwitchElse)^-1) * PopIndent + SwitchBlock: EmptyLine^0 * Advance * Ct(SwitchCase * (SpaceBreak^1 * SwitchCase)^0 * (SpaceBreak ^1 * SwitchElse)^-1) * PopIndent SwitchCase: key"when" * Ct(ExpList) * key"then"^-1 * Body / mark"case" SwitchElse: key"else" * Body / mark"else" diff --git a/moonscript/parse/literals.lua b/moonscript/parse/literals.lua index 1ad5aeb5..bc6c57e8 100644 --- a/moonscript/parse/literals.lua +++ b/moonscript/parse/literals.lua @@ -14,7 +14,8 @@ local plain_space = S(" \t") ^ 0 local Break = P("\r") ^ -1 * P("\n") local Stop = Break + -1 local Comment = P("--") * (1 - S("\r\n")) ^ 0 * L(Stop) -local Space = plain_space * Comment ^ -1 +local escape_newline = P("\\") * plain_space * Comment ^ -1 * Break +local Space = (S(" \t") + escape_newline) ^ 0 * Comment ^ -1 local SomeSpace = S(" \t") ^ 1 * Comment ^ -1 local SpaceBreak = Space * Break local EmptyLine = SpaceBreak diff --git a/moonscript/parse/literals.moon b/moonscript/parse/literals.moon index 5a4980f3..28a873fe 100644 --- a/moonscript/parse/literals.moon +++ b/moonscript/parse/literals.moon @@ -12,7 +12,8 @@ Break = P"\r"^-1 * P"\n" Stop = Break + -1 Comment = P"--" * (1 - S"\r\n")^0 * L(Stop) -Space = plain_space * Comment^-1 +escape_newline = P"\\" * plain_space * Comment^-1 * Break +Space = (S" \t" + escape_newline)^0 * Comment^-1 SomeSpace = S" \t"^1 * Comment^-1 SpaceBreak = Space * Break @@ -36,3 +37,4 @@ safe_module "moonscript.parse.literals", { :White, :Break, :Stop, :Comment, :Space, :SomeSpace, :SpaceBreak, :EmptyLine, :AlphaNum, :Name, :Num, :Shebang } + diff --git a/moonscript/util.lua b/moonscript/util.lua index bde0d576..ceee01da 100644 --- a/moonscript/util.lua +++ b/moonscript/util.lua @@ -40,7 +40,7 @@ pos_to_line = function(str, pos) end local trim trim = function(str) - return str:match("^%s*(.-)%s*$") + return str:match("^[%s\\]*(.-)%s*$") end local get_line get_line = function(str, line_num) diff --git a/moonscript/util.moon b/moonscript/util.moon index ea7c8a5b..d3efc338 100644 --- a/moonscript/util.moon +++ b/moonscript/util.moon @@ -33,7 +33,7 @@ pos_to_line = (str, pos) -> line trim = (str) -> - str\match "^%s*(.-)%s*$" + str\match "^[%s\\]*(.-)%s*$" get_line = (str, line_num) -> -- todo: this returns an extra blank line at the end diff --git a/spec/inputs/syntax.moon b/spec/inputs/syntax.moon index 854f6297..40e726e9 100644 --- a/spec/inputs/syntax.moon +++ b/spec/inputs/syntax.moon @@ -270,3 +270,16 @@ z = a- b -- cooool + +str = strA \ -- comment 1 + .. strB \ -- comment 2 + .. strC + +-> + a,b, \ + c,d, \ + e,f + +with obj + print \ + \func! diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index fef1a739..879f0f73 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -257,4 +257,13 @@ x = x and "hello" z = a - b z = a(-b) z = a - b -z = a - b \ No newline at end of file +z = a - b +local str = strA .. strB .. strC +_ = function() + return a, b, c, d, e, f +end +do + local _with_0 = obj + print(_with_0:func()) + return _with_0 +end From cb34d9bceca9077012db2e018896a78117138d66 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sun, 2 Feb 2020 22:53:24 +0800 Subject: [PATCH 2/4] Update syntax.lua remove new line? From fe7056e7eca4665c1e6c0ae28d9db531ef5c67c8 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sun, 2 Feb 2020 23:45:12 +0800 Subject: [PATCH 3/4] Update syntax.lua From aebb0a9fc9372f563bbb7ed902c40ee7fd23c750 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sun, 2 Feb 2020 23:49:31 +0800 Subject: [PATCH 4/4] remove eol at the file end. --- spec/outputs/syntax.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index 879f0f73..13107463 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -266,4 +266,4 @@ do local _with_0 = obj print(_with_0:func()) return _with_0 -end +end \ No newline at end of file