1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
local DefaultSource = { start = 0, finish = 0 }
local mt = {}
mt.__index = mt
mt.type = 'value'
mt._type = 'any'
function mt:setValue(source, value)
self._value = value
end
function mt:getValue()
return self._value
end
function mt:inference(tp)
if tp == '...' then
error('Value type cant be ...')
end
if self._type == 'any' and tp ~= 'nil' then
self._type = tp
end
end
function mt:getType()
return self._type
end
function mt:createField(name, source)
local field = {
type = 'field',
key = name,
source = source or DefaultSource,
}
if not self._child then
self._child = {}
end
local uri = source and source.uri or ''
if not self._child[uri] then
self._child[uri] = {}
end
self._child[uri][name] = field
self:inference('table')
return field
end
function mt:rawGetField(name, source)
local uri = source and source.uri or ''
local field
if self._child then
if self._child[uri] then
field = self._child[uri][name]
end
if not field then
for _, childs in pairs(self._child) do
field = childs[name]
if field then
break
end
end
end
end
return field
end
function mt:getField(name, source)
local field = self:rawGetField(name, source)
if not field then
field = self:createField(name, source)
end
return field
end
function mt:eachField(callback)
if not self._child then
return
end
local mark = {}
for _, childs in pairs(self._child) do
for name, field in pairs(childs) do
if not mark[name] then
mark[name] = true
callback(name, field)
end
end
end
end
function mt:addInfo(tp, source)
if source and not source.start then
error('Miss start: ' .. table.dump(source))
end
if not self._info then
self._info = {}
end
local uri = source and source.uri or ''
if not self._info[uri] then
self._info[uri] = {}
end
self._info[uri][#self._info[uri]+1] = {
type = tp,
source = source or DefaultSource,
}
return self
end
function mt:eachInfo(callback)
if not self._info then
return
end
for _, infos in pairs(self._info) do
for i = 1, #infos do
callback(infos[i])
end
end
end
return function (tp, source, value)
if tp == '...' then
error('Value type cant be ...')
end
-- TODO lib里的多类型
if type(tp) == 'table' then
tp = tp[1]
end
local self = setmetatable({
source = source or DefaultSource,
_type = tp,
}, mt)
if value ~= nil then
self:setValue(source, value)
end
return self
end
|