aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-06-12 18:16:34 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-06-12 18:16:44 -0700
commitb0997a7dbffb636b8209055ed788fe1360aef25d (patch)
tree5fb8aafeafa65c012d2abe53246f513fc2864f50
parent3c510e4ee5027aa5191e994bdf0a080a32a4b082 (diff)
Minor optimizations to indent/dedent/nodent.
-rw-r--r--nomsu.lua18
-rwxr-xr-xnomsu.moon28
2 files changed, 25 insertions, 21 deletions
diff --git a/nomsu.lua b/nomsu.lua
index 4f61c29..4aa57ff 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -172,20 +172,22 @@ do
_with_0.utf8_char = (R("\194\223") * R("\128\191") + R("\224\239") * R("\128\191") * R("\128\191") + R("\240\244") * R("\128\191") * R("\128\191") * R("\128\191"))
_with_0.ident_char = R("az", "AZ", "09") + P("_") + _with_0.utf8_char
_with_0.indent = Cmt(Carg(1), function(self, start, userdata)
- if #match(self, "^[ ]*", start) >= userdata.indent + 4 then
- userdata.indent = userdata.indent + 4
- return start + userdata.indent
+ local indented = userdata.indent .. ' '
+ if sub(self, start, start + #indented - 1) == indented then
+ userdata.indent = indented
+ return start + #indented
end
end)
_with_0.dedent = Cmt(Carg(1), function(self, start, userdata)
- if #match(self, "^[ ]*", start) <= userdata.indent - 4 then
- userdata.indent = userdata.indent - 4
+ local dedented = sub(userdata.indent, 1, -5)
+ if #match(self, "^[ ]*", start) <= #dedented then
+ userdata.indent = dedented
return start
end
end)
_with_0.nodent = Cmt(Carg(1), function(self, start, userdata)
- if #match(self, "^[ ]*", start) >= userdata.indent then
- return start + userdata.indent
+ if sub(self, start, start + #userdata.indent - 1) == userdata.indent then
+ return start + #userdata.indent
end
end)
_with_0.userdata = Carg(1)
@@ -311,7 +313,7 @@ do
assert(type(nomsu_code) ~= 'string')
local userdata = {
source_code = nomsu_code,
- indent = 0,
+ indent = "",
errors = { },
source = nomsu_code.source
}
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)