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.
19 $ template make_parser()
21 # State of the parser automata as stored in the parser stack.
23 # The internal state number
24 readable writable attr _state: Int
26 # The node stored with the state in the stack
27 readable writable attr _nodes: Object
29 init(state: Int, nodes: Object)
41 # Stack of pushed states and productions
42 attr _stack: Array[State]
44 # Position in the stack
47 # Create a new parser based on a given lexer
51 _stack = new Array[State]
58 # Do a transition in the automata
59 private meth go_to(index: Int): Int
62 var table = _goto_table[index]
64 var high = table.length/2 - 1
67 var middle = (low + high) / 2
68 var subindex = middle * 2
70 if state < table[subindex] then
72 else if state > table[subindex] then
75 return table[subindex + 1]
79 return table[1] # Default value
82 # Push someting in the state stack
83 private meth push(numstate: Int, list_node: Object)
85 var pos = _stack_pos + 1
87 if pos < _stack.length then
88 var state = _stack[pos]
89 state.state = numstate
90 state.nodes = list_node
92 _stack.push(new State(numstate, list_node))
97 private meth state: Int
99 return _stack[_stack_pos].state
102 # Pop something from the stack state
103 private meth pop: Object
105 var res = _stack[_stack_pos].nodes
106 _stack_pos = _stack_pos -1
110 # Build and return a full AST.
115 var ign: List[Token] = null
118 var token = lexer.peek
119 var last_pos = token.pos
120 var last_line = token.line
122 if token isa PError then
123 assert token isa PError
124 return new Start(null, token)
127 var index = token.parser_index
128 var table = _action_table[state]
129 var action_type = table[1]
130 var action_value = table[2]
133 var high = table.length/3 - 1
136 var middle = (low + high) / 2
137 var subindex = middle * 3
139 if index < table[subindex] then
141 else if index > table[subindex] then
144 action_type = table[subindex + 1]
145 action_value = table[subindex + 2]
146 high = low -1 # break
150 if action_type == 0 then # SHIFT
151 push(action_value, lexer.next)
152 else if action_type == 1 then # REDUCE
153 _reduce_table[action_value].action(self)
154 else if action_type == 2 then # ACCEPT
155 var node2 = lexer.next
158 assert node1 isa ${/parser/prods/prod/@ename}
159 var node = new Start(node1, node2)
160 (new SearchTokensVisitor).visit(node)
162 else if action_type == 3 then # ERROR
163 var node2 = new PError.init_error(lexer.filename, last_line, last_pos, error_messages[errors[action_value]])
164 var node = new Start(null, node2)
171 attr _reduce_table: Array[ReduceAction]
172 private meth build_reduce_table
174 _reduce_table = new Array[ReduceAction].with_items(
175 $ foreach {rules/rule}
176 new ReduceAction@index[-sep ','-]
182 # Find first and last tokens of production nodes
183 private class SearchTokensVisitor
185 attr _untokenned_nodes: Array[Prod]
186 attr _last_token: Token
187 redef meth visit(n: PNode)
192 for no in _untokenned_nodes do
195 _untokenned_nodes.clear
198 _untokenned_nodes.add(n)
200 n.last_token = _last_token
205 _untokenned_nodes = new Array[Prod]
209 # Each reduca action has its own class, this one is the root of the hierarchy.
210 private abstract class ReduceAction
211 meth action(p: Parser) is abstract
214 $ foreach {rules/rule}
215 private class ReduceAction@index
217 redef meth action(p: Parser)
219 var node_list: Object = null
223 var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = p.pop
225 $ when {@cmd='FETCHLIST'}
226 var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = ${translate(@from,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
227 assert ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} isa Array[Object]
229 $ when {@cmd='FETCHNODE'}
230 var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = ${translate(@from,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
231 assert ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} isa @etype
233 $ when {@cmd='ADDNODE'}
234 if ${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} != null then
235 ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}.add(${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")})
238 $ when {@cmd='ADDLIST'}
239 if ${translate(@fromlist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} != null then
240 if ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}.is_empty then
241 ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = ${translate(@fromlist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
243 ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}.append(${translate(@fromlist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")})
247 $ when {@cmd='MAKELIST'}
248 var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = new Array[Object]
250 $ when {@cmd='MAKENODE'}
251 var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = new @etype.init_${translate(@etype,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}(
256 ${translate(.,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}[-sep ','-]
261 $ when {@cmd='RETURNNODE'}
265 node_list = ${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
268 $ when {@cmd='RETURNLIST'}
269 node_list = ${translate(@list,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
273 p.push(p.go_to(@leftside), node_list)
280 $ template make_parser_tables()
281 # Parser that build a full AST
282 abstract class ParserTable
283 attr _action_table: Array[Array[Int]] = null
284 private meth build_action_table
286 _action_table = once [
287 $ foreach {parser_data/action_table/row}
288 action_table_row${position()}[-sep ','-]
293 $ foreach {parser_data/action_table/row}
294 private meth action_table_row${position()}: Array[Int]
298 @from, @action, @to [-sep ','-]
304 attr _goto_table: Array[Array[Int]] = null
305 private meth build_goto_table
308 $ foreach {parser_data/goto_table/row}
311 @from, @to [-sep ','-]
318 private meth error_messages: Array[String]
321 $ foreach {parser_data/error_messages/msg}
322 "${sablecc:string2escaped_unicode(.)}" [-sep ','-]
327 private meth errors: Array[Int]
330 [-foreach {parser_data/errors/i}-]${.} [-sep ','-] [-end-]