@@ -22,39 +22,33 @@ local FluentNode = class({
2222 end
2323 end
2424 end
25- if (node [1 ] and # node > 0 ) then
26- self .elements = {}
27- tablex .insertvalues (self .elements , tablex .imap (node_to_type , node ))
25+ tablex .foreachi (node , function (n ) self :insert (node_to_type (n )) end )
26+ end ,
27+
28+ insert = function (self , node )
29+ if type (node ) ~= " table" then return nil end
30+ if node :is_a (node_types .Identifier ) then
31+ self .id = node
32+ elseif node :is_a (node_types .Pattern ) then
33+ self .value = node
34+ else
35+ if not self .elements then self .elements = {} end
36+ table.insert (self .elements , node )
2837 end
2938 end ,
3039
3140 dump_ast = function (self )
3241 local ast = { type = self .type }
3342 for k , v in pairs (self ) do ast [k ] = v end
34- ast .identifier = nil
3543 return ast
3644 end ,
3745
3846 append = function (self , node )
39- if type (self .__add ) == " function"
40- and self .appendable
41- and node .appendable
42- and self :is_a (node :is_a ())
43- then
44- return self + node
45- else
46- return false
47- end
47+ return node and type (node .__add ) == " function" and self + node
4848 end ,
4949
5050 attach = function (self , node )
51- if node and
52- type (node .__mul ) == " function"
53- then
54- return node * self
55- else
56- return false
57- end
51+ return node and type (node .__mul ) == " function" and self * node
5852 end
5953
6054 })
@@ -84,17 +78,6 @@ node_types.Message = class({
8478 _base = FluentNode ,
8579 _init = function (self , node )
8680 self :super (node )
87- for key , value in ipairs (self .elements ) do
88- if value :is_a (node_types .Identifier ) then
89- self .identifier = value .name
90- self .id = value
91- self .elements [key ] = nil
92- elseif value :is_a (node_types .Pattern ) then
93- self .value = value
94- self .elements [key ] = nil
95- end
96- end
97- if # self .elements == 0 then self .elements = nil end
9881 self .attributes = {}
9982 end ,
10083 format = function (self , parameters )
@@ -145,24 +128,63 @@ node_types.TextElement = class({
145128 appendable = true ,
146129 _base = FluentNode ,
147130 _init = function (self , node )
131+ node .id = " TextElement"
148132 self :super (node )
149133 end
150134 })
151135
136+ node_types .Placeable = class ({
137+ appendable = true ,
138+ _base = FluentNode ,
139+ _init = function (self , node )
140+ node .id = " Placeable"
141+ self :super (node )
142+ if node .expression then
143+ self .expression = node_to_type (node .expression [1 ])
144+ end
145+ end
146+ })
147+
152148node_types .PatternElement = function (node )
153- node .id = " TextElement"
154- return node_types .TextElement (node )
149+ if node .value then
150+ return node_types .TextElement (node )
151+ else
152+ return node_types .Placeable (node )
153+ end
155154end
156155
156+ node_types .StringLiteral = class ({
157+ _base = FluentNode ,
158+ _init = function (self , node )
159+ self :super (node )
160+ end
161+ })
162+
163+ node_types .NumberLiteral = class ({
164+ _base = FluentNode ,
165+ _init = function (self , node )
166+ self :super (node )
167+ end
168+ })
169+
170+ node_types .VariableReference = class ({
171+ _base = FluentNode ,
172+ _init = function (self , node )
173+ self :super (node )
174+ end
175+ })
176+
157177node_types .Comment = class ({
158178 appendable = true ,
159179 _base = FluentNode ,
160180 _init = function (self , node )
161181 self :super (node )
162182 end ,
163183 __add = function (self , node )
164- self .content = (self .content or " " ) .. " \n " .. (node .content or " " )
165- return self
184+ if self :is_a (node :is_a ()) and self .appendable and node .appendable then
185+ self .content = (self .content or " " ) .. " \n " .. (node .content or " " )
186+ return self
187+ end
166188 end ,
167189 __mul = function (self , node )
168190 if self :is_a (node_types .Message ) then
@@ -208,8 +230,9 @@ node_types.CommentLine = function(node)
208230end
209231
210232node_to_type = function (node )
211- if type (node .id ) ~= " string" then return nil end
212- return node_types [node .id ](node )
233+ if type (node ) == " table" and type (node .id ) == " string" then
234+ return node_types [node .id ](node )
235+ end
213236end
214237
215238dedent = function (content )
@@ -273,7 +296,7 @@ local FluentResource = class({
273296 insert = function (self , node )
274297 table.insert (self .body , node )
275298 if node :is_a (node_types .Message ) then
276- self .index [node .identifier ] = # self .body
299+ self .index [node .id . name ] = # self .body
277300 end
278301 end ,
279302
@@ -288,6 +311,7 @@ local FluentResource = class({
288311 end ,
289312
290313 __add = function (self , resource )
314+ if not self :is_a (resource :is_a ()) then error (" Cannot merge unlike types" ) end
291315 for _ , node in ipairs (resource .body ) do
292316 self :insert (node )
293317 end
0 commit comments