From b0997a7dbffb636b8209055ed788fe1360aef25d Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 12 Jun 2018 18:16:34 -0700 Subject: Minor optimizations to indent/dedent/nodent. --- nomsu.moon | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'nomsu.moon') diff --git a/nomsu.moon b/nomsu.moon index 5aaf683..cab3719 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -140,22 +140,24 @@ NOMSU_DEFS = with {} .ident_char = R("az","AZ","09") + P("_") + .utf8_char -- If the line begins with #indent+4 spaces, the pattern matches *those* spaces - -- and adds them to the stack (not any more). + -- and adds them to the current indent (not any more). .indent = Cmt Carg(1), (start, userdata)=> - if #match(@, "^[ ]*", start) >= userdata.indent + 4 - userdata.indent += 4 - return start + userdata.indent - -- If the number of leading space characters is <= the number of space on the top of the - -- stack minus 4, this pattern matches and pops off the top of the stack exactly once. + indented = userdata.indent..' ' + if sub(@, start, start+#indented-1) == indented + userdata.indent = indented + return start + #indented + -- If the number of leading space characters is <= the number of spaces in the current + -- indent minus 4, this pattern matches and decrements the current indent exactly once. .dedent = Cmt Carg(1), (start, userdata)=> - if #match(@, "^[ ]*", start) <= userdata.indent - 4 - userdata.indent -= 4 + dedented = sub(userdata.indent, 1, -5) + if #match(@, "^[ ]*", start) <= #dedented + userdata.indent = dedented return start - -- If the number of leading space characters is >= the number on the top of the - -- stack, this pattern matches and does not modify the stack. + -- If the number of leading space characters is >= the number of spaces in the current + -- indent, this pattern matches and does not modify the indent. .nodent = Cmt Carg(1), (start, userdata)=> - if #match(@, "^[ ]*", start) >= userdata.indent - return start + userdata.indent + if sub(@, start, start+#userdata.indent-1) == userdata.indent + return start + #userdata.indent .userdata = Carg(1) @@ -318,7 +320,7 @@ class NomsuCompiler parse: (nomsu_code)=> assert(type(nomsu_code) != 'string') userdata = { - source_code:nomsu_code, indent: 0, errors: {}, + source_code:nomsu_code, indent: "", errors: {}, source: nomsu_code.source, } tree = NOMSU_PATTERN\match(tostring(nomsu_code), nil, userdata) -- cgit v1.2.3