123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- local ptable
- ptable = function(t,d)
- local str = ""
- d = d or 0
- for k,v in pairs(t) do
- str = str .. string.rep(" ", d) .. tostring(k) .. " = " .. tostring(v) .. "\n"
- if type(v) == "table" then str = str .. ptable(v, d+1) end
- end
- return str
- end
-
- local split
- split = function(str)
- --print("split<"..tostring(str)..">")
- local ast = {}
- local prev = 0
- local cur = str:find(" ", 0, true)
- local ret
- while cur do
- ret = str:sub(prev + 1, cur - 1)
- if ret and #ret > 0 then
- table.insert(ast, ret)
- end
- prev = cur
- cur = str:find(" ", prev + 1, true)
- end
- ret = str:sub(prev + 1)
- if ret and #ret > 0 then
- table.insert(ast, ret)
- end
- --print("split's result:")
- --print(ptable(ast))
- if #ast == 1 then
- --print("split '" .. str .. "' returns '" .. ast[1] .. "'")
- return ast[1]
- elseif #ast > 1 then
- --print("split '" .. str .. "' returns '" .. table.concat(ast, ",") .. "'")
- return ast
- end
- end
-
- local brace
- brace = function(str)
- --print("brace<"..tostring(str)..">")
- local ast = {}
- local open, close = str:find("(", 0, true), str:find(")", 0, true)
- local ret
- while open and close and open < close do
- -- we want what came before the next level starts
- ret = split(str:sub(1, open - 1))
- if ret then
- table.insert(ast, ret)
- end
- -- we want the object formed from the next level
- ret, str = brace(str:sub(open + 1))
- if ret then
- table.insert(ast, ret)
- end
- -- and now we need to find what is next
- open, close = str:find("(", 0, true), str:find(")", 0, true)
- end
- if close then
- -- we want what came before our close
- ret = split(str:sub(1, close - 1))
- if ret then
- table.insert(ast, ret)
- end
- --print("CLOSING \""..str:sub(close + 1).."\"")
- -- return our result + what is still unparsed
- return ast, str:sub(close + 1)
- end
- -- if there is anything left, add it (we should be on the top level only here)
- ret = split(str)
- --print("STR->RET", str, ret)
- if ret then
- table.insert(ast, ret)
- end
- --print("brace's result:")
- --print(ptable(ast))
- if #ast == 1 then
- return ast[1]
- elseif #ast > 1 then
- return ast
- end
- end
-
- local a = "(echo (: 'value' ($ name)))"
- ast = brace(a)
- print(ptable(ast))
- print(ast[2][1][1])
- --print(ast)
-
- --(
- -- 1echo
- -- 2(
- -- 1:
- -- 2'value'
- -- 3(
- -- 1$
- -- 2name
- -- )
- -- )
- --)
|