Skip to content

Commit 35c2646

Browse files
committed
Use idiomatic Lua and math methods so Message * Comment make comment a child of message
1 parent e2d4b38 commit 35c2646

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

fluent/resource.lua

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ local dedent
99
local FluentNode = class({
1010
discardable = false,
1111
appendable = false,
12-
commentable = false,
1312

1413
_init = function (self, node)
1514
for key, value in pairs(node) do
@@ -48,8 +47,14 @@ local FluentNode = class({
4847
end
4948
end,
5049

51-
attachcomment = function (self, node)
52-
-- if
50+
attach = function (self, node)
51+
if node and
52+
type(node.__mul) == "function"
53+
then
54+
return self * node
55+
else
56+
return false
57+
end
5358
end
5459

5560
})
@@ -77,7 +82,6 @@ node_types.Junk = class({
7782
})
7883

7984
node_types.Message = class({
80-
commentable = true,
8185
_base = FluentNode,
8286
_init = function (self, node)
8387
self:super(node)
@@ -159,6 +163,10 @@ node_types.Comment = class({
159163
end,
160164
__add = function (self, node)
161165
self.content = (self.content or "") .. "\n" .. (node.content or "")
166+
end,
167+
__mul = function (self, node)
168+
self.comment = node
169+
return self
162170
end
163171
})
164172

@@ -220,14 +228,17 @@ end
220228

221229
local FluentResource = class({
222230
type = "Resource",
223-
idmap = {},
231+
index = {},
224232

225233
_init = function (self, ast)
226234
self.body = {}
227235
local _stash = nil
228236
local flush = function ()
229-
if _stash then table.insert(self.body, _stash) end
237+
if _stash then
238+
self:insert(_stash)
239+
end
230240
_stash = nil
241+
return #self.body
231242
end
232243
local stash = function (node)
233244
if not _stash then
@@ -246,30 +257,26 @@ local FluentResource = class({
246257
if not node.discardable then
247258
flush()
248259
end
249-
elseif node.commentable then
250-
if _stash then
251-
if _stash.type ~= "Comment" then
252-
flush()
253-
else
254-
node.comment = _stash
255-
_stash = nil
256-
end
257-
end
258-
table.insert(self.body, node)
259-
if node:is_a(node_types.Message) then
260-
self.idmap[node.identifier] = #self.body
261-
end
260+
elseif node:attach(_stash) and flush() then
261+
self:insert(node)
262262
else
263263
flush()
264-
table.insert(self.body, node)
264+
self:insert(node)
265265
end
266266
end
267267
flush()
268268
-- self:catch(function (self, identifier) return self:get_message(identifier) end)
269269
end,
270270

271+
insert = function (self, node)
272+
table.insert(self.body, node)
273+
if node:is_a(node_types.Message) then
274+
self.index[node.identifier] = #self.body
275+
end
276+
end,
277+
271278
get_message = function (self, identifier)
272-
return self.idmap[identifier] and self.body[self.idmap[identifier]]
279+
return self.index[identifier] and self.body[self.index[identifier]]
273280
end,
274281

275282
dump_ast = function (self)

0 commit comments

Comments
 (0)