Skip to content

Commit e2d4b38

Browse files
committed
Setup add method on types to append such as Comment + Comment
1 parent c6450ce commit e2d4b38

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

fluent/resource.lua

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ local dedent
88

99
local FluentNode = class({
1010
discardable = false,
11-
mergable = false,
11+
appendable = false,
12+
commentable = false,
1213

1314
_init = function (self, node)
1415
for key, value in pairs(node) do
@@ -33,6 +34,22 @@ local FluentNode = class({
3334
for k, v in pairs(self) do ast[k] = v end
3435
ast.identifier = nil
3536
return ast
37+
end,
38+
39+
append = function (self, node)
40+
if type(self.__add) == "function"
41+
and self.appendable
42+
and node.appendable
43+
and self:is_a(node:is_a())
44+
then
45+
return self + node
46+
else
47+
return false
48+
end
49+
end,
50+
51+
attachcomment = function (self, node)
52+
-- if
3653
end
3754

3855
})
@@ -60,6 +77,7 @@ node_types.Junk = class({
6077
})
6178

6279
node_types.Message = class({
80+
commentable = true,
6381
_base = FluentNode,
6482
_init = function (self, node)
6583
self:super(node)
@@ -121,7 +139,7 @@ node_types.Pattern = class({
121139
-- end
122140

123141
node_types.TextElement = class({
124-
mergable = true,
142+
appendable = true,
125143
_base = FluentNode,
126144
_init = function (self, node)
127145
self:super(node)
@@ -134,27 +152,32 @@ node_types.PatternElement = function (node)
134152
end
135153

136154
node_types.Comment = class({
137-
mergable = true,
155+
appendable = true,
138156
_base = FluentNode,
139157
_init = function (self, node)
140158
self:super(node)
159+
end,
160+
__add = function (self, node)
161+
self.content = (self.content or "") .. "\n" .. (node.content or "")
141162
end
142163
})
143164

144165
node_types.GroupComment = class({
145-
mergable = true,
166+
appendable = true,
146167
_base = FluentNode,
147168
_init = function (self, node)
148169
self:super(node)
149-
end
170+
end,
171+
__add = node_types.Comment.__add
150172
})
151173

152174
node_types.ResourceComment = class({
153-
mergable = true,
175+
appendable = true,
154176
_base = FluentNode,
155177
_init = function (self, node)
156178
self:super(node)
157-
end
179+
end,
180+
__add = node_types.Comment.__add
158181
})
159182

160183
node_types.Attribute = class({
@@ -209,24 +232,21 @@ local FluentResource = class({
209232
local stash = function (node)
210233
if not _stash then
211234
_stash = node
212-
elseif _stash:is_a(node:is_a()) then
213-
-- TODO: move to _add meta method
214-
_stash.content = (_stash.content or "") .. "\n" .. (node.content or "")
215-
else
235+
elseif not _stash:append(node) then
216236
flush()
217237
_stash = node
218238
end
219239
end
220240
-- TODO: eliminate double iteration by looking ahead?
221241
local elements = tablex.imap(node_to_class, ast)
222242
for _, node in ipairs(elements) do
223-
if node.mergable then
243+
if node.appendable then
224244
stash(node)
225245
elseif node:is_a(node_types.blank_block) then
226246
if not node.discardable then
227247
flush()
228248
end
229-
elseif node:is_a(node_types.Message) or node:is_a(node_types.Term) then
249+
elseif node.commentable then
230250
if _stash then
231251
if _stash.type ~= "Comment" then
232252
flush()

0 commit comments

Comments
 (0)