Fix deserialization of shogi promotions.
[tagua/yd.git] / data / scripts / hllib.lua
blob0be99745c37163469ef6f383312813fc74be7a62
1 -------------------------------------------------------------------------------
2 -- Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 -- (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 -------------------------------------------------------------------------------
11 Text = {
12 create_from_hline = function(hline, from, to)
13 local o = {
14 line = hline,
15 from = from,
16 to = to
18 setmetatable(o, Text[".object_meta_table"])
19 return o
20 end,
21 [".getter_table"] = {
22 text = function(self)
23 return self.line:mid(self.from, self.to)
24 end
26 [".method_table"] = {
27 dump = function(self)
28 self.line:dump()
29 end,
30 clone = function(self)
31 local hline = self.line:extract(self.from, self.to)
32 return Text.create_from_hline(hline, 0, hline.length)
33 end
35 [".meta_table"] = {
36 __call = function(prototype, str)
37 local hline = HLine(str)
38 return prototype.create_from_hline(hline, 0, hline.length)
39 end
41 [".object_meta_table"] = {
42 __index = function(self, property)
43 local h = Text[".getter_table"][property]
44 if h ~= nil then
45 return h(self)
46 else
47 h = Text[".method_table"][property]
48 if h ~= nil then
49 return h
50 else
51 return nil
52 end
53 end
54 end,
56 __newindex = function(self, property, value)
57 self.line["set_" .. property](self.line, self.from, self.to, value)
58 end,
60 __call = function(self, from, to)
61 if (to > self.to) then
62 error("Index " .. to .. " is too large")
63 end
64 return Text.create_from_hline(self.line, from + self.from, to + self.from)
65 end,
67 __add = function(self, other)
68 local hline = self:clone().line:append(other.line, other.from, other.to)
69 return Text.create_from_hline(hline, 0, hline.length)
70 end
73 setmetatable(Text, Text[".meta_table"])
75 __patterns__ = { }
77 on = {}
78 highlight = {}
80 setmetatable(on, {
81 __newindex = function(self, re, h)
82 local event = {
83 pattern = re,
84 handler = h
86 table.insert(__patterns__, event)
87 end,
88 __call = function(self, re, h)
89 self[re] = h
90 end
93 setmetatable(highlight, {
94 __newindex = function(self, re, fmt)
95 local function handler()
96 local function set_property(prop)
97 if fmt[prop] ~= nil then
98 match[prop] = fmt[prop]
99 end
101 set_property("bold")
102 set_property("italic")
103 set_property("color")
104 return line
106 table.insert(__patterns__, { pattern = re, handler = handler})
107 end,
109 __call = function(self, re, fmt)
110 self[re] = fmt
114 function filter(re)
115 table.insert(__patterns__, { pattern = re, handler = function() end})
118 function __run_event__(event, str, match, ref, state)
119 env = getfenv(event.handler)
120 env.line = Text(str)
121 env.match = env.line(match.from, match.to)
122 env.ref = {}
123 for i, r in ipairs(ref) do
124 env.ref[i] = env.line(ref[i].from, ref[i].to)
126 for k, v in pairs(state) do
127 env[k] = v
129 setfenv(event.handler, env)
131 local result = event.handler()
133 if result ~= nil then
134 return result