Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local rules = {
- {"%-%-%[(=*)%[.-%]%1%]","COMMENT"};
- {"%-%-.-\n","COMMENT"};
- {"['\"]","START"};
- {"%[(=*)%[.-%]%1%]","LONGSTRING"};
- {"[^%[-'\"]+","OTHER"};
- }
- local function parse(source)
- local result,i = {},1
- local function parser()
- if i == #source + 1 then return end
- for k,v in pairs(rules) do
- local a,b = source:find(v[1],i)
- if a == i then i = b+1
- return v[2],source:sub(a,b),a
- end
- end error("No rule found at pos "..i,0)
- end
- for typ,part,start in parser do
- --print(typ,part,start)
- if typ == "START" then local st = start+1
- local a,b = source:find("\\*"..part,st)
- while a do st = b + 1
- if (b-a)%2==0 then a=nil break end
- a,b = source:find("\\*"..part,st)
- end
- if b and not a then i = b+1
- local str = source:sub(start,b)
- -- print"hi" will make 'str' be "hi" (including quotes)
- table.insert(result,"(sandboxstring"..str..")")
- else
- error("Couldn't find closing quote thingy starting at pos "..start,0)
- end
- elseif typ == "LONGSTRING" then
- table.insert(result,"(sandboxstring"..part..")")
- elseif typ == "COMMENT" then
- -- ignore? idk. Could parse javadocs or whatever for fun
- else
- table.insert(result,part)
- end
- end
- return table.concat(result)
- end
- local code = parse[==========[
- print"pls"
- print'hi'
- -- comment that'll get ignored
- print[==[
- long
- bananas
- ]==]
- --[[
- This one gets ignored too, yeuy!
- --]]
- print[[not-so-long bananas with " and ' quotes and [=[this thing]=] ]]
- print("Ah putting ' in here works fine")
- print("Putting \" with backslash works fine too")
- ]==========]
- print(code)
- assert(loadstring(code))
- --[[ START OF RESULT
- print(sandboxstring"pls")
- print(sandboxstring'hi')
- print(sandboxstring[==[
- long
- bananas
- ]==])
- print(sandboxstring[[not-so-long bananas with " and ' quotes and [=[this thing]=] ]])
- print((sandboxstring"Ah putting ' in here works fine"))
- print((sandboxstring"Putting \" with backslash works fine too"))
- --]] END OF RESULT
Add Comment
Please, Sign In to add comment