aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-05-30 12:06:44 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-05-30 12:07:08 -0700
commite5d9879a7972fa09e8c9e8f88642bbcc27e0c5a1 (patch)
treef4eedaffb7f87be9fcc6a89db0deff5d1b233eb7
parenta01e7d27bd3c17ef69c984866b1870b69689e9f9 (diff)
Cleaned up patterns a little.
-rw-r--r--nomsu.lua24
-rwxr-xr-xnomsu.moon24
-rw-r--r--nomsu.peg7
3 files changed, 21 insertions, 34 deletions
diff --git a/nomsu.lua b/nomsu.lua
index 7778abd..394af49 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -123,7 +123,6 @@ LINE_STARTS = setmetatable({ }, {
return line_starts
end
})
-local LUA_METADATA = { }
do
local STRING_METATABLE = getmetatable("")
STRING_METATABLE.__add = function(self, other)
@@ -148,10 +147,6 @@ do
_with_0.nl = P("\r") ^ -1 * P("\n")
_with_0.ws = S(" \t")
_with_0.tonumber = tonumber
- _with_0.print = function(src, pos, msg)
- print(msg, pos, repr(src:sub(math.max(0, pos - 16), math.max(0, pos - 1)) .. "|" .. src:sub(pos, pos + 16)))
- return true
- end
local string_escapes = {
n = "\n",
t = "\t",
@@ -170,7 +165,6 @@ do
end)
_with_0.escaped_char = _with_0.escaped_char + ((P("\\") * C(S("ntbavfr"))) / string_escapes)
_with_0.operator_char = S("'~`!@$^&*-+=|<>?/")
- _with_0.operator = _with_0.operator_char ^ 1
_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)
@@ -251,7 +245,7 @@ end
local NomsuCompiler
do
local _class_0
- local stub_defs, stub_pattern, var_pattern, _nomsu_chunk_counter, _running_files, MAX_LINE, math_expression
+ local stub_pattern, var_pattern, _nomsu_chunk_counter, _running_files, MAX_LINE, math_expression
local _base_0 = {
define_action = function(self, signature, fn, is_compile_action)
if is_compile_action == nil then
@@ -1351,14 +1345,16 @@ do
})
_base_0.__class = _class_0
local self = _class_0
- stub_defs = {
- space = (P(' ') + P('\n..')) ^ 0,
- word = (NOMSU_DEFS.ident_char ^ 1 + NOMSU_DEFS.operator),
- varname = (R('az', 'AZ', '09') + P('_') + NOMSU_DEFS.utf8_char + (-P("'") * NOMSU_DEFS.operator)) ^ 0
- }
- stub_pattern = re.compile([=[ {~ (%space->'') (('%' (%varname->'')) / %word)? ((%space->' ') (('%' (%varname->'')) / %word))* (%space->'') ~}
+ local stub_defs
+ do
+ stub_defs = {
+ word = (-R("09") * NOMSU_DEFS.ident_char ^ 1) + NOMSU_DEFS.operator_char ^ 1,
+ varname = (NOMSU_DEFS.ident_char ^ 1 * ((-P("'") * NOMSU_DEFS.operator_char ^ 1) + NOMSU_DEFS.ident_char ^ 1) ^ 0) ^ -1
+ }
+ end
+ stub_pattern = re.compile([=[ {~ ([ ]*->'') (('%' (%varname->'')) / %word)? (([ ]*->' ') (('%' (%varname->'')) / %word))* ([ ]*->'') ~}
]=], stub_defs)
- var_pattern = re.compile("{| %space ((('%' {%varname}) / %word) %space)+ |}", stub_defs)
+ var_pattern = re.compile("{| [ ]* ((('%' {%varname}) / %word) [ ]*)+ |}", stub_defs)
_nomsu_chunk_counter = 0
_running_files = { }
MAX_LINE = 80
diff --git a/nomsu.moon b/nomsu.moon
index 7562cee..c162246 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -109,11 +109,6 @@ LINE_STARTS = setmetatable {}, {
return line_starts
}
--- Map from unique nomsu chunkname to:
--- lua_to_nomsu, nomsu_to_lua, lua_sources, nomsu_sources,
--- nomsu_filename, nomsu_file, lua_filename, lua_file
-LUA_METADATA = {}
-
-- Use + operator for string coercive concatenation (note: "asdf" + 3 == "asdf3")
-- Use [] for accessing string characters, or s[{3,4}] for s:sub(3,4)
-- Note: This globally affects all strings in this instance of Lua!
@@ -133,16 +128,12 @@ NOMSU_DEFS = with {}
.nl = P("\r")^-1 * P("\n")
.ws = S(" \t")
.tonumber = tonumber
- .print = (src,pos,msg)->
- print(msg, pos, repr(src\sub(math.max(0,pos-16),math.max(0,pos-1)).."|"..src\sub(pos,pos+16)))
- return true
string_escapes = n:"\n", t:"\t", b:"\b", a:"\a", v:"\v", f:"\f", r:"\r"
digit, hex = R('09'), R('09','af','AF')
.escaped_char = (P("\\")*S("xX")*C(hex*hex)) / => string.char(tonumber(@, 16))
.escaped_char += (P("\\")*C(digit*(digit^-2))) / => string.char(tonumber @)
.escaped_char += (P("\\")*C(S("ntbavfr"))) / string_escapes
.operator_char = S("'~`!@$^&*-+=|<>?/")
- .operator = .operator_char^1
.utf8_char = (
R("\194\223")*R("\128\191") +
R("\224\239")*R("\128\191")*R("\128\191") +
@@ -285,15 +276,16 @@ class NomsuCompiler
@environment.Types = Types
@initialize_core!
- stub_defs = {
- space:(P(' ') + P('\n..'))^0
- word:(NOMSU_DEFS.ident_char^1 + NOMSU_DEFS.operator)
- varname:(R('az','AZ','09') + P('_') + NOMSU_DEFS.utf8_char + (-P("'") * NOMSU_DEFS.operator))^0
- }
+ local stub_defs
+ with NOMSU_DEFS
+ stub_defs = {
+ word: (-R("09") * .ident_char^1) + .operator_char^1
+ varname: (.ident_char^1 * ((-P("'") * .operator_char^1) + .ident_char^1)^0)^-1
+ }
stub_pattern = re.compile [=[
- {~ (%space->'') (('%' (%varname->'')) / %word)? ((%space->' ') (('%' (%varname->'')) / %word))* (%space->'') ~}
+ {~ ([ ]*->'') (('%' (%varname->'')) / %word)? (([ ]*->' ') (('%' (%varname->'')) / %word))* ([ ]*->'') ~}
]=], stub_defs
- var_pattern = re.compile "{| %space ((('%' {%varname}) / %word) %space)+ |}", stub_defs
+ var_pattern = re.compile "{| [ ]* ((('%' {%varname}) / %word) [ ]*)+ |}", stub_defs
define_action: (signature, fn, is_compile_action=false)=>
if type(fn) != 'function'
error("Not a function: #{repr fn}")
diff --git a/nomsu.peg b/nomsu.peg
index b705de5..f260f86 100644
--- a/nomsu.peg
+++ b/nomsu.peg
@@ -52,9 +52,9 @@ inline_action (Action):
action (Action):
{| (expression (dotdot? %ws*))* word ((dotdot? %ws*) (expression / word))* |}
-word: { %operator / (!number plain_word) }
+word: { %operator_char+ / (!number %ident_char+) }
-text_word (Text): {| {%operator / (!number plain_word)} |}
+text_word (Text): {| word |}
inline_text (Text):
!('".."' eol)
@@ -90,7 +90,7 @@ number (Number): (("-"? (([0-9]+ "." [0-9]+) / ("." [0-9]+) / ([0-9]+)))-> tonum
-- Variables can be nameless (i.e. just %) and can't contain operators like apostrophe
-- which is a hack to allow %'s to parse as "%" and "' s" separately
-variable (Var): "%" { (plain_word ((!"'" %operator) / plain_word)*)? }
+variable (Var): "%" { (%ident_char+ ((!"'" %operator_char+) / %ident_char+)*)? }
inline_list (List):
!('[..]')
@@ -135,4 +135,3 @@ dedent: eol (%nl ignored_line)* (((!.) %dedent) / (&(%nl %dedent)))
non_dedent_error: (!dedent .)* eol (%nl ignored_line)* (!. / &%nl)
comma: %ws* "," %ws*
dotdot: nodent ".."
-plain_word: ([a-zA-Z0-9_-] / %utf8_char)+