1 $ // This file is part of NIT ( http://www.nitlanguage.org ).
3 $ // Copyright 2008 Jean Privat <jean@pryen.org>
4 $ // Based on algorithms developped for ( http://www.sablecc.org/ ).
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
10 $ // http://www.apache.org/licenses/LICENSE-2.0
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.
18 $ template make_parser()
20 # State of the parser automata as stored in the parser stack.
22 # The internal state number
23 readable writable var _state: Int
25 # The node stored with the state in the stack
26 readable writable var _nodes: nullable Object
28 init(state: Int, nodes: nullable Object)
40 # Stack of pushed states and productions
41 var _stack: Array[State]
43 # Position in the stack
46 # Create a new parser based on a given lexer
50 _stack = new Array[State]
55 # Do a transition in the automata
56 private fun go_to(index: Int): Int
60 var high = parser_goto(index, 0) - 1
63 var middle = (low + high) / 2
64 var subindex = middle * 2 + 1 # +1 because parser_goto(index, 0) is the length
66 var goal = parser_goto(index, subindex)
69 else if state > goal then
72 return parser_goto(index, subindex+1)
76 return parser_goto(index, 2) # Default value
79 # Push someting in the state stack
80 private fun push(numstate: Int, list_node: nullable Object)
82 var pos = _stack_pos + 1
84 if pos < _stack.length then
85 var state = _stack[pos]
86 state.state = numstate
87 state.nodes = list_node
89 _stack.push(new State(numstate, list_node))
94 private fun state: Int
96 return _stack[_stack_pos].state
99 # Pop something from the stack state
100 private fun pop: nullable Object
102 var res = _stack[_stack_pos].nodes
103 _stack_pos = _stack_pos -1
107 # Build and return a full AST.
114 var token = lexer.peek
115 if token isa PError then
116 return new Start(null, token)
119 var index = token.parser_index
120 var action_type = parser_action(state, 2)
121 var action_value = parser_action(state, 3)
124 var high = parser_action(state, 0) - 1
127 var middle = (low + high) / 2
128 var subindex = middle * 3 + 1 # +1 because parser_action(state, 0) is the length
130 var goal = parser_action(state, subindex)
133 else if index > goal then
136 action_type = parser_action(state, subindex+1)
137 action_value = parser_action(state, subindex+2)
142 if action_type == 0 then # SHIFT
143 push(action_value, lexer.next)
144 else if action_type == 1 then # REDUCE
145 _reduce_table[action_value].action(self)
146 else if action_type == 2 then # ACCEPT
147 var node2 = lexer.next
150 assert node1 isa ${/parser/prods/prod/@ename}
151 var node = new Start(node1, node2)
152 (new ComputeProdLocationVisitor).enter_visit(node)
154 else if action_type == 3 then # ERROR
155 var node2 = new PError.init_error("Syntax error: unexpected token.", token.location)
156 var node = new Start(null, node2)
162 var _reduce_table: Array[ReduceAction]
163 private fun build_reduce_table
165 _reduce_table = new Array[ReduceAction].with_items(
166 $ foreach {rules/rule}
167 new ReduceAction@index(@leftside)[-sep ','-]
174 # Location on the first token after the start of a production
175 # So outside the production for epilon production
176 var _first_location: nullable Location
178 # Location of the last token before the end of a production
179 # So outside the production for epilon production
180 var _last_location: nullable Location
183 # Find location of production nodes
184 # Uses existing token locations to infer location of productions.
185 private class ComputeProdLocationVisitor
187 # Currenlty visited productions that need a first token
188 var _need_first_prods: Array[Prod] = new Array[Prod]
190 # Already visited epsilon productions that waits something after them
191 var _need_after_epsilons: Array[Prod] = new Array[Prod]
193 # Already visited epsilon production that waits something before them
194 var _need_before_epsilons: Array[Prod] = new Array[Prod]
196 # Location of the last visited token in the current production
197 var _last_location: nullable Location = null
199 redef fun visit(n: nullable PNode)
203 else if n isa Token then
207 # Add a first token to productions that need one
208 for no in _need_first_prods do
209 no._first_location = loc
211 _need_first_prods.clear
213 # Find location for already visited epsilon production that need one
214 for no in _need_after_epsilons do
215 # Epsilon production that is in the middle of a non-epsilon production
216 # The epsilon production has both a token before and after it
218 var startl = no._last_location
219 no.location = new Location(endl.file, startl.line_end, endl.line_start, startl.column_end, endl.column_start)
221 _need_after_epsilons.clear
224 _need_first_prods.add(n)
226 var old_last = _last_location
227 _last_location = null
229 var endl = _last_location
230 if endl == null then _last_location = old_last
232 n._last_location = endl
233 var startl = n._first_location
234 if startl != null then
235 # Non-epsilon production
238 n.location = new Location(startl.file, startl.line_start, endl.line_end, startl.column_start, endl.column_end)
240 for no in _need_before_epsilons do
241 # Epsilon production that starts the current non-epsilon production
242 #var startl = n.location
243 no.location = new Location(startl.file, startl.line_start, startl.line_start, startl.column_start, startl.column_start)
245 _need_before_epsilons.clear
247 for no in _need_after_epsilons do
248 # Epsilon production that finishes the current non-epsilon production
249 #var endl = n.location
250 no.location = new Location(endl.file, endl.line_end, endl.line_end, endl.column_end, endl.column_end)
252 _need_after_epsilons.clear
254 # No first token means epsilon production (or "throw all my tokens" production)
255 # So, it must be located it later
257 # Epsilon production that starts a parent non-epsilon production
258 _need_before_epsilons.add(n)
260 # Epsilon production in the middle or that finishes a parent non-epsilon production
261 _need_after_epsilons.add(n)
270 # Each reduca action has its own class, this one is the root of the hierarchy.
271 private abstract class ReduceAction
272 fun action(p: Parser) is abstract
273 fun concat(l1, l2 : Array[Object]): Array[Object]
275 if l1.is_empty then return l2
281 $ foreach {rules/rule}
282 private class ReduceAction@index
284 redef fun action(p: Parser)
286 var node_list: nullable Object = null
290 var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = p.pop
292 $ when {@cmd='FETCHLIST'}
293 var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = ${translate(@from,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
294 assert ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} isa Array[Object]
296 $ when {@cmd='FETCHNODE'}
297 var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = ${translate(@from,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
298 assert ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} isa nullable @etype
300 $ when {@cmd='ADDNODE'}
301 if ${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} != null then
302 ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}.add(${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")})
305 $ when {@cmd='ADDLIST'}
306 ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = concat(${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}, ${translate(@fromlist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")})
308 $ when {@cmd='MAKELIST'}
309 var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = new Array[Object]
311 $ when {@cmd='MAKENODE'}
312 var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}: nullable @etype = new @etype.init_${translate(@etype,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}(
317 ${translate(.,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}[-sep ','-]
322 $ when {@cmd='RETURNNODE'}
326 node_list = ${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
329 $ when {@cmd='RETURNLIST'}
330 node_list = ${translate(@list,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
334 p.push(p.go_to(_goto), node_list)
337 init(g: Int) do _goto = g
342 $ template make_parser_table()
343 $ foreach {parser_data/action_table/row}
344 static int parser_action_row${position()}[] = {
347 @from, @action, @to[-sep ','-]
352 const int* const parser_action_table[] = {
353 $ foreach {parser_data/action_table/row}
354 parser_action_row${position()}[-sep ','-]
358 $ foreach {parser_data/goto_table/row}
359 static int parser_goto_row${position()}[] = {
362 @from, @to[-sep ','-]
367 const int* const parser_goto_table[] = {
368 $ foreach {parser_data/goto_table/row}
369 parser_goto_row${position()}[-sep ','-]