From 75b7207ed918324fa3ef5fbc5541b72c284f3eba Mon Sep 17 00:00:00 2001 From: theFox6 Date: Tue, 11 Feb 2020 17:08:37 +0100 Subject: [PATCH] added comments "//" and "##" --- init.lua | 93 +++++++++++++++++++++++++++++++++++++++++++++-------- runTest.lua | 4 +-- test.venus | 13 +++++++- 3 files changed, 94 insertions(+), 16 deletions(-) diff --git a/init.lua b/init.lua index c6867c8..97ee298 100644 --- a/init.lua +++ b/init.lua @@ -1,43 +1,108 @@ local parser = {} -local elements = "(%s*%g+)" +local elements = { + names = "(%a%w+)", + spaces = "(%s+)", + special = "[%%%(%)%{%}%;%,]", + strings = "[\"']", + special_combined = "([%+%-%*/%^#=~<>%[%]%:.][%+%-/#=%[%]:%.]?[%.]?)", +} + +--FIXME: allow multiple paterns +local function optmatch(str,pat) + local cutstr = str + return function() + if not cutstr then return end + local spos,epos = cutstr:find(pat) + local match + local found = (spos == 1) + if found then + match = cutstr:sub(spos,epos) + cutstr = cutstr:sub(epos+1) + --print("f",match,cutstr,pat) + elseif not spos then + match = cutstr + cutstr = nil + --print("n",match,pat) + else + match = cutstr:sub(1,spos-1) + cutstr = cutstr:sub(spos) + --print("p",match,cutstr,pat) + end + return match, found + end +end + +function parser.test_optmatch() + assert(optmatch("123","123")() == "123") + assert(optmatch("123","321")() == "123") + assert(optmatch("123","1")() == "1") + assert(optmatch("123","2")() == "1") +end local function parse_element(el,pc) - local space = el:match("%s*") - local word = el:sub(#space+1) if pc.foreach == 2 then pc.foreach = 3 - return space.."pairs("..word + return "pairs("..el end - if word == "foreach" then + if el == "foreach" then pc.foreach = 1 - return space .. "for" - elseif word == "for" then + return "for" + elseif el == "for" then pc.foreach = 0 - elseif word == "in" then + elseif el == "in" then if pc.foreach == 1 then pc.foreach = 2 end - elseif word == "do" then + elseif el == "do" then if pc.foreach == 3 then return ")" .. el end + elseif el == '"' or el == "'" then + if not pc.instring then + pc.instring = el + elseif pc.instring == el then + pc.instring = false + end + elseif el == "[[" then + if not pc.instring then + pc.instring = el + end + elseif el == "]]" then + if pc.instring == "[[" then + pc.instring = false + end + elseif el == "//" or el=="##" then + if not pc.instring then + return "--" + end end - --print(el) + --print(el,pc.instring and "in string" or "") return el end local function parse_line(l,pc) local pl = "" - for w in l:gmatch(elements) do - pl = pl .. parse_element(w,pc) + local i = 0 + for sp,s in optmatch(l,elements.spaces) do + if s then + pl = pl .. sp + else + for sc in optmatch(sp,elements.special_combined) do + for ss in optmatch(sc,elements.special) do + for st in optmatch(ss,elements.strings) do + pl = pl .. parse_element(st,pc) + end + end + end + end end return pl end function parser.loadvenus(file) local fc = "" - local pc = {opencurly = {}} + local pc = {instring == false, opencurly = {}} for l in io.lines(file) do fc = fc .. parse_line(l,pc) .. "\n" end @@ -52,4 +117,6 @@ function parser.dovenus(file) return ff() end +parser.optmatch = optmatch + return parser diff --git a/runTest.lua b/runTest.lua index f8c1834..db139e9 100644 --- a/runTest.lua +++ b/runTest.lua @@ -1,3 +1,3 @@ -local p = dofile("src/init.lua") +local p = dofile("init.lua") -p.dovenus("src/test.venus") +p.dovenus("test.venus") diff --git a/test.venus b/test.venus index fed8311..ee70bf7 100644 --- a/test.venus +++ b/test.venus @@ -18,9 +18,15 @@ foreach i,el in testt do print(i.." = "..el) end ---[[ coming soon +// comment +## comment //comment +assert("//"=="/".."/") +-- comment +assert([[ +##]]=="#".."#","comment within [[string]] falsely detected") +--[[ coming soon fn a() print("function") end @@ -33,6 +39,11 @@ j = j + 2 i++ j += 2 +function dec(n) + n-- + return n -- not a decrement +end + function t() { print("hi") }