38b360386de86d15618d01431c2fe6358bcb3da4
[nit.git] / src / parser / xss / parser.xss
1 $ // This file is part of NIT ( http://www.nitlanguage.org ).
2 $ //
3 $ // Copyright 2008 Jean Privat <jean@pryen.org>
4 $ // Based on algorithms developped for ( http://www.sablecc.org/ ).
5 $ //
6 $ // Licensed under the Apache License, Version 2.0 (the "License");
7 $ // you may not use this file except in compliance with the License.
8 $ // You may obtain a copy of the License at
9 $ //
10 $ //     http://www.apache.org/licenses/LICENSE-2.0
11 $ //
12 $ // Unless required by applicable law or agreed to in writing, software
13 $ // distributed under the License is distributed on an "AS IS" BASIS,
14 $ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 $ // See the License for the specific language governing permissions and
16 $ // limitations under the License.
17
18 $ template make_parser()
19
20 # State of the parser automata as stored in the parser stack.
21 private class State
22         # The internal state number
23         readable writable var _state: Int
24
25         # The node stored with the state in the stack
26         readable writable var _nodes: nullable Object
27
28         init(state: Int, nodes: nullable Object)
29         do
30                 _state = state
31                 _nodes = nodes
32         end
33 end
34
35 class Parser
36 special ParserTable
37         # Associated lexer
38         var _lexer: Lexer
39
40         # Stack of pushed states and productions
41         var _stack: Array[State]
42
43         # Position in the stack
44         var _stack_pos: Int
45
46         # Create a new parser based on a given lexer
47         init(lexer: Lexer)
48         do
49                 _lexer = lexer
50                 _stack = new Array[State]
51                 _stack_pos = -1
52                 build_goto_table
53                 build_action_table
54                 build_reduce_table
55         end
56
57         # Do a transition in the automata
58         private fun go_to(index: Int): Int
59         do
60                 var state = state
61                 var table = _goto_table[index]
62                 var low = 1
63                 var high = table.length/2 - 1
64
65                 while low <= high do
66                         var middle = (low + high) / 2
67                         var subindex = middle * 2
68
69                         if state < table[subindex] then
70                                 high = middle - 1
71                         else if state > table[subindex] then
72                                 low = middle + 1
73                         else
74                                 return table[subindex + 1]
75                         end
76                 end
77
78                 return table[1] # Default value
79         end
80
81         # Push someting in the state stack
82         private fun push(numstate: Int, list_node: nullable Object)
83         do
84                 var pos = _stack_pos + 1
85                 _stack_pos = pos
86                 if pos < _stack.length then
87                         var state = _stack[pos]
88                         state.state = numstate
89                         state.nodes = list_node
90                 else
91                         _stack.push(new State(numstate, list_node))
92                 end
93         end
94
95         # The current state
96         private fun state: Int
97         do
98                 return _stack[_stack_pos].state
99         end
100
101         # Pop something from the stack state
102         private fun pop: nullable Object
103         do
104                 var res = _stack[_stack_pos].nodes
105                 _stack_pos = _stack_pos -1
106                 return res
107         end
108
109         # Build and return a full AST.
110         fun parse: Start
111         do
112                 push(0, null)
113
114                 var lexer = _lexer
115                 while true do
116                         var token = lexer.peek
117                         var last_pos = token.location.column_start
118                         var last_line = token.location.line_start
119
120                         if token isa PError then
121                                 return new Start(null, token)
122                         end
123
124                         var index = token.parser_index
125                         var table = _action_table[state]
126                         var action_type = table[1]
127                         var action_value = table[2]
128
129                         var low = 1
130                         var high = table.length/3 - 1
131
132                         while low <= high do
133                                 var middle = (low + high) / 2
134                                 var subindex = middle * 3
135
136                                 if index < table[subindex] then
137                                         high = middle - 1
138                                 else if index > table[subindex] then
139                                         low = middle + 1
140                                 else
141                                         action_type = table[subindex + 1]
142                                         action_value = table[subindex + 2]
143                                         high = low -1 # break
144                                 end
145                         end
146
147                         if action_type == 0 then # SHIFT
148                                 push(action_value, lexer.next)
149                         else if action_type == 1 then # REDUCE
150                                 _reduce_table[action_value].action(self)
151                         else if action_type == 2 then # ACCEPT
152                                 var node2 = lexer.next
153                                 assert node2 isa EOF
154                                 var node1 = pop
155                                 assert node1 isa ${/parser/prods/prod/@ename}
156                                 var node = new Start(node1, node2)
157                                 (new ComputeProdLocationVisitor).enter_visit(node)
158                                 return node
159                         else if action_type == 3 then # ERROR
160                                 var location = new Location(lexer.filename, last_line, last_line, last_pos, last_pos)
161                                 var node2 = new PError.init_error(error_messages[errors[action_value]],location)
162                                 var node = new Start(null, node2)
163                                 return node
164                         end
165                 end
166                 abort
167         end
168
169         var _reduce_table: Array[ReduceAction]
170         private fun build_reduce_table
171         do
172                 _reduce_table = new Array[ReduceAction].with_items(
173 $ foreach {rules/rule}
174                         new ReduceAction@index[-sep ','-]
175 $ end foreach
176                 )
177         end
178 end
179
180 redef class Prod
181         # Location on the first token after the start of a production
182         # So outside the production for epilon production
183         var _first_location: nullable Location
184
185         # Location of the last token before the end of a production
186         # So outside the production for epilon production
187         var _last_location: nullable Location
188 end
189
190 # Find location of production nodes
191 # Uses existing token locations to infer location of productions.
192 private class ComputeProdLocationVisitor
193 special Visitor
194         # Currenlty visited productions that need a first token
195         var _need_first_prods: Array[Prod] = new Array[Prod]
196
197         # Already visited epsilon productions that waits something after them
198         var _need_after_epsilons: Array[Prod] = new Array[Prod]
199
200         # Already visited epsilon production that waits something before them
201         var _need_before_epsilons: Array[Prod] = new Array[Prod]
202
203         # Location of the last visited token in the current production
204         var _last_location: nullable Location = null
205
206         redef fun visit(n: nullable PNode)
207         do
208                 if n == null then
209                         return
210                 else if n isa Token then
211                         var loc = n.location
212                         _last_location = loc
213
214                         # Add a first token to productions that need one
215                         for no in _need_first_prods do
216                                 no._first_location = loc
217                         end
218                         _need_first_prods.clear
219
220                         # Find location for already visited epsilon production that need one
221                         for no in _need_after_epsilons do
222                                 # Epsilon production that is in the middle of a non-epsilon production
223                                 # The epsilon production has both a token before and after it
224                                 var endl = loc
225                                 var startl = no._last_location
226                                 no.location = new Location(endl.file, startl.line_end, endl.line_start, startl.column_end, endl.column_start)
227                         end
228                         _need_after_epsilons.clear
229                 else
230                         assert n isa Prod
231                         _need_first_prods.add(n)
232
233                         var old_last = _last_location
234                         _last_location = null
235                         n.visit_all(self)
236                         var endl = _last_location
237                         if endl == null then _last_location = old_last
238
239                         n._last_location = endl
240                         var startl = n._first_location
241                         if startl != null then
242                                 # Non-epsilon production
243                                 assert endl != null
244
245                                 n.location = new Location(startl.file, startl.line_start, endl.line_end, startl.column_start, endl.column_end)
246
247                                 for no in _need_before_epsilons do
248                                         # Epsilon production that starts the current non-epsilon production
249                                         #var startl = n.location
250                                         no.location = new Location(startl.file, startl.line_start, startl.line_start, startl.column_start, startl.column_start)
251                                 end
252                                 _need_before_epsilons.clear
253
254                                 for no in _need_after_epsilons do
255                                         # Epsilon production that finishes the current non-epsilon production
256                                         #var endl = n.location
257                                         no.location = new Location(endl.file, endl.line_end, endl.line_end, endl.column_end, endl.column_end)
258                                 end
259                                 _need_after_epsilons.clear
260                         else
261                                 # No first token means epsilon production (or "throw all my tokens" production)
262                                 # So, it must be located it later
263                                 if endl == null then
264                                         # Epsilon production that starts a parent non-epsilon production
265                                         _need_before_epsilons.add(n)
266                                 else
267                                         # Epsilon production in the middle or that finishes a parent non-epsilon production
268                                         _need_after_epsilons.add(n)
269                                 end
270                         end
271                 end
272         end
273
274         init do end
275 end
276
277 # Each reduca action has its own class, this one is the root of the hierarchy.
278 private abstract class ReduceAction
279         fun action(p: Parser) is abstract
280 end
281
282 $ foreach {rules/rule}
283 private class ReduceAction@index
284 special ReduceAction
285         redef fun action(p: Parser)
286         do
287                                         var node_list: nullable Object = null
288 $   foreach {action}
289 $   choose
290 $     when {@cmd='POP'}
291                                         var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = p.pop
292 $     end
293 $     when {@cmd='FETCHLIST'}
294                                         var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = ${translate(@from,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
295                                         assert ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} isa Array[Object]
296 $     end
297 $     when {@cmd='FETCHNODE'}
298                                         var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = ${translate(@from,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
299                                         assert ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} isa nullable @etype
300 $     end
301 $     when {@cmd='ADDNODE'}
302                                         if ${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} != null then
303                                                 ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}.add(${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")})
304                                         end
305 $     end
306 $     when {@cmd='ADDLIST'}
307 #                                       if ${translate(@fromlist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} != null then
308                                                 if ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}.is_empty then
309                                                         ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = ${translate(@fromlist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
310                                                 else
311                                                         ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}.append(${translate(@fromlist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")})
312                                                 end
313 #                                       end
314 $     end
315 $     when {@cmd='MAKELIST'}
316                                         var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = new Array[Object]
317 $     end
318 $     when {@cmd='MAKENODE'}
319                                         var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}: nullable @etype = new @etype.init_${translate(@etype,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}(
320 $       foreach {arg}
321 $           if @null
322                                                 null[-sep ','-]
323 $           else
324                                                 ${translate(.,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}[-sep ','-]
325 $           end
326 $       end foreach
327                                         )
328 $     end
329 $     when {@cmd='RETURNNODE'}
330 $       if @null
331                                         node_list = null
332 $       else
333                                         node_list = ${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
334 $       end
335 $     end
336 $     when {@cmd='RETURNLIST'}
337                                         node_list = ${translate(@list,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
338 $     end
339 $   end choose
340 $   end foreach
341                                         p.push(p.go_to(@leftside), node_list)
342         end
343 init do end
344 end
345 $ end foreach
346 $ end template
347
348 $ template make_parser_tables()
349 # Parser that build a full AST
350 abstract class ParserTable
351         var _action_table: Array[Array[Int]]
352         private fun build_action_table
353         do
354                 _action_table = once [
355 $ foreach {parser_data/action_table/row}
356                         action_table_row${position()}[-sep ','-]
357 $ end foreach
358                 ]
359         end
360
361 $ foreach {parser_data/action_table/row}
362         private fun action_table_row${position()}: Array[Int]
363         do
364                 return [
365 $   foreach {action}
366                                 @from, @action, @to[-sep ','-]
367 $   end foreach
368                         ]
369         end
370 $ end foreach
371
372         var _goto_table: Array[Array[Int]]
373         private fun build_goto_table
374         do
375                 _goto_table = once [
376 $ foreach {parser_data/goto_table/row}
377                         [
378 $   foreach {goto}
379                                 @from, @to[-sep ','-]
380 $   end foreach
381                         ][-sep ','-]
382 $ end foreach
383                 ]
384         end
385
386         private fun error_messages: Array[String]
387         do
388                 return once [
389 $ foreach {parser_data/error_messages/msg}
390                         "${sablecc:string2escaped_unicode(.)}"[-sep ','-]
391 $ end
392                 ]
393         end
394
395         private fun errors: Array[Int]
396         do
397                 return once [
398                         [-foreach {parser_data/errors/i}-]${.}[-sep ','-][-end-]
399                 ]
400         end
401
402         init do end
403 end
404 $ end template