1
0
Fork 0
mirror of https://github.com/Oreolek/ink-instead.git synced 2024-05-11 13:38:24 +03:00

paragraphs and comments

This commit is contained in:
premek 2016-11-22 00:56:32 +01:00
parent 90f1e41100
commit 8fe40c413d
2 changed files with 63 additions and 73 deletions

100
peg.lua
View file

@ -1,54 +1,5 @@
function table_print (tt, done)
done = done or {}
if type(tt) == "table" then
local sb = {}
for key, value in pairs (tt) do
table.insert(sb, " ") -- indent it
if type (value) == "table" and not done [value] then
done [value] = true
table.insert(sb, "{");
table.insert(sb, table_print (value, done))
table.insert(sb, " ") -- indent it
table.insert(sb, "} ");
elseif "number" == type(key) then
table.insert(sb, string.format("%s,", tostring(value)))
else
table.insert(sb, string.format(
"%s = %s,", tostring (key), tostring(value)))
end
end
return table.concat(sb)
else
return tt .. "\n"
end
end
function to_string( tbl )
if "nil" == type( tbl ) then
return tostring(nil)
elseif "table" == type( tbl ) then
return table_print(tbl)
elseif "string" == type( tbl ) then
return tbl
else
return tostring(tbl)
end
end
local lpeg = require("lpeg")
local white = lpeg.S(" \t\r\n") ^ 0
local integer = white * lpeg.R("09") ^ 1 / tonumber
local muldiv = white * lpeg.C(lpeg.S("/*"))
local addsub = white * lpeg.C(lpeg.S("+-"))
require "util"
local lpeg = require "lpeg"
local function node(p)
return p / function(left, op, right)
@ -56,32 +7,35 @@ local function node(p)
end
end
local lst = function(fold, new)
println(fold, new)
if typeof(fold) ~= "table" then return {new}
else table.insert(fold, new); return fold; end
end
local sp = lpeg.S" \t" ^0
local wh = lpeg.S" \n\t" ^0
local wh = lpeg.S" \t\r\n" ^0
local nl = lpeg.S"\r\n" ^1
local text = sp * (lpeg.R("az", "AZ")^1 * sp)^1
local para = wh * lpeg.C(text) * wh
local textLine = (lpeg.P(1)-nl)^1
local para = lpeg.C(textLine)
local commOL = '//' * sp * text
local commML = '/*' * wh * text * wh * '*/'
local comm = commOL + commML
local exp = para + comm
local any = lpeg.Ct(exp^1) * -1
local ink = any
--local id = lpeg.R("az", "AZ")^1 * sp)^1
print(to_string(ink:match("hello\nworld\n//comment")))
local commOL = '//' * sp * lpeg.C((lpeg.P(1)-nl)^0) -- TODO comment that does not start at the line beginning
local commML = '/*' * wh * lpeg.C((lpeg.P(1)-'*/')^0) * '*/'
local comm = (commOL + commML) / function (i) return "comment:"..i; end
local line = wh * (comm + para) * wh
local prog = ((wh * lpeg.Ct((line*wh)^0)) )* -1
local ink = prog
print(to_string(ink:match(" asd\naa")))
print(to_string(ink:match(" // asd a\ta\naa")))
print(to_string(ink:match("/* \tas \n\n \tda\n */")))
print(to_string(ink:match("// \n \tsome text\n// and some comment\n\n\t\n")))
print(to_string(ink:match(" \n \t/* \ndemment \n\t comment\n\n\t */ \n")))
print(to_string(ink:match(" \t\t \t ")))
print(to_string(ink:match("\n\n \t\n\t \t \n")))
print(to_string(ink:match("hello\nworld\n//comme nt\ntest\n/*demm*/")))
print(to_string(ink:match(" \nFc oh\n")))
print(to_string(ink:match(" \nF\tc \n ooo")))
print(to_string(ink:match(" \nFc \n")))
print(to_string(ink:match("\"What do you make of this?\" she asked. \n\n// Something unprintable...\n\n\"I couldn't possibly comment,\" I replied.\n/*\n ... or an unlimited block of text\n*/")))

36
util.lua Normal file
View file

@ -0,0 +1,36 @@
function table_print (tt, done)
done = done or {}
if type(tt) == "table" then
local sb = {}
for key, value in pairs (tt) do
table.insert(sb, " ") -- indent it
if type (value) == "table" and not done [value] then
done [value] = true
table.insert(sb, "{");
table.insert(sb, table_print (value, done))
table.insert(sb, " ") -- indent it
table.insert(sb, "} ");
elseif "number" == type(key) then
table.insert(sb, string.format("%s,", tostring(value)))
else
table.insert(sb, string.format(
"%s = %s,", tostring (key), tostring(value)))
end
end
return table.concat(sb)
else
return tt .. "\n"
end
end
function to_string( tbl )
if "nil" == type( tbl ) then
return tostring(nil)
elseif "table" == type( tbl ) then
return table_print(tbl)
elseif "string" == type( tbl ) then
return tbl
else
return tostring(tbl)
end
end