Changes

Jump to navigation Jump to search
190 bytes added ,  10:02, 24 February 2009
Reformated code
local nodes = { }
function nodes.B(x) command("black" ,x) end function nodes.W(x) command("white" ,x) end
local function action(what,data)
end
local space = lpeg.S(' \r\n')^1 local lower = lpeg.R("az") local upper = lpeg.R("AZ") local letter = lower + upper local position = letter^2
local left = lpeg.P("(")
local right = lpeg.P(")")
local none = 1- (left + right)
local node = (lpeg.P(";") * lpeg.C(lpeg.S("BW")) * lpeg.P("[") * lpeg.C(position) * lpeg.P("]"))/action
local branch = lpeg.P { left/start * (lpeg.V(1) + node + space)^0 * right/stop }
local parser = (branch + node + space)^0
function thirddata.sgf.parsea(str)
end
local node = (lpeg.P(";") * lpeg.C(lpeg.S("BW")) * lpeg.P("[") * lpeg.C(position) * lpeg.P("]"))/action
local branch = lpeg.P { left * (lpeg.V(1) + (none^1/nest) + space)^0 * right }
local parser = (branch + node + space)^0
function thirddata.sgf.parseb(str)
\starttexdefinition sgf!start #1
\par \advance\leftskipby +2em
\stoptexdefinition
\starttexdefinition sgf!stop #1
\par \advance\leftskipby -2em
\stoptexdefinition
\starttexdefinition sgf!node #1
Node: #1\par
\stoptexdefinition
\starttexdefinition sgf!white #1
White: #1\par
\stoptexdefinition
\starttexdefinition sgf!black #1
Black: #1\par
\stoptexdefinition
local nodes = { }
function nodes.B (x) command("black" ,x) end function nodes.W (x) command("white" ,x) end function nodes.AW(x) command("addwhite" ,x) end function nodes.C (x) command("comment" ,x) end
local function action(what,data)
local a = nodes[what]
if a then
for w in string.gmatch(data, "%b[]") do a(string.sub(w,2,-2))
end
else
end
local function nodecontent(str)
tex.sprint(tex.ctxcatcodes,string.format("\\csname sgf!node\\endcsname{%s}",string.sub(str,2)))
end
local space = lpeg.S(' \r\n')^1 local lcletter = lpeg.R("az") local ucletter = lpeg.R("AZ") local letter = lcletter + ucletter
local propindent = ucletter^1
local property = lpeg.C(propindent) * lpeg.C{ (lpeg.P("[") * (1 - lpeg.S"[]")^0 * lpeg.P("]"))^1} / action
local function nest(str) tex.sprint(tex.ctxcatcodes,string.format("\\parsesgf{%s}",string.sub(str,2,-2))) end
local node = lpeg.P{ ";" * (propindent * (lpeg.P("[") * (1 - lpeg.S"[]")^0 * lpeg.P("]"))^1)^1} / nodecontent local branch = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" } / nest
local parser = (branch + node + property + space)^0
function thirddata.sgf.parse(str) parser:match(str) end
end
\starttexdefinition parsesgf #1
\par \advance\leftskipby +2em \sgfflush{#1} \par \advance\leftskipby -2em
\stoptexdefinition
\starttexdefinition sgf!node #1
\par \sgfflush{#1}
\stoptexdefinition
\starttexdefinition sgf!white #1
White: #1\quad
\stoptexdefinition
\starttexdefinition sgf!black #1
Black: #1\quad
\stoptexdefinition
\starttexdefinition sgf!addwhite #1
Add White: #1\quad
\stoptexdefinition
\starttexdefinition sgf!comment #1
Comment: #1\quad
\stoptexdefinition
* http://www.inf.puc-rio.br/~roberto/lpeg.html
* http://www.red-bean.com/sgf/
gardener
110

edits

Navigation menu