41d674106565ced8e32486112c9f84dd6a09b5c4
[nit.git] / src / parser / parser_work.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Internal algorithm and data structures for the Nit parser
16 module parser_work
17
18 intrude import parser_prod
19
20 # State of the parser automata as stored in the parser stack.
21 private class State
22 # The internal state number
23 var state: Int
24
25 # The node stored with the state in the stack
26 var nodes: nullable Object
27 end
28
29 # The parser of the Nit language.
30 class Parser
31 super TablesCapable
32 # Associated lexer
33 var lexer: Lexer
34
35 # Stack of pushed states and productions
36 private var stack = new Array[State]
37
38 # Position in the stack
39 private var stack_pos: Int = -1
40
41 init
42 do
43 build_reduce_table
44 end
45
46 # Do a transition in the automata
47 private fun go_to(index: Int): Int
48 do
49 var state = state
50 var low = 1
51 var high = parser_goto(index, 0) - 1
52
53 while low <= high do
54 var middle = (low + high) / 2
55 var subindex = middle * 2 + 1 # +1 because parser_goto(index, 0) is the length
56
57 var goal = parser_goto(index, subindex)
58 if state < goal then
59 high = middle - 1
60 else if state > goal then
61 low = middle + 1
62 else
63 return parser_goto(index, subindex+1)
64 end
65 end
66
67 return parser_goto(index, 2) # Default value
68 end
69
70 # Push someting in the state stack
71 private fun push(numstate: Int, list_node: nullable Object)
72 do
73 var pos = _stack_pos + 1
74 _stack_pos = pos
75 if pos < _stack.length then
76 var state = _stack[pos]
77 state._state = numstate
78 state._nodes = list_node
79 else
80 _stack.push(new State(numstate, list_node))
81 end
82 end
83
84 # The current state
85 private fun state: Int
86 do
87 return _stack[_stack_pos]._state
88 end
89
90 # Pop something from the stack state
91 private fun pop: nullable Object
92 do
93 var res = _stack[_stack_pos]._nodes
94 _stack_pos = _stack_pos -1
95 return res
96 end
97
98 # Build and return a full AST.
99 fun parse: Start
100 do
101 push(0, null)
102
103 var lexer = _lexer
104 loop
105 var token = lexer.peek
106 if token isa AError then
107 return new Start(null, token)
108 end
109
110 var state = self.state
111 var index = token.parser_index
112 var action_type = parser_action(state, 2)
113 var action_value = parser_action(state, 3)
114
115 var low = 1
116 var high = parser_action(state, 0) - 1
117
118 while low <= high do
119 var middle = (low + high) / 2
120 var subindex = middle * 3 + 1 # +1 because parser_action(state, 0) is the length
121
122 var goal = parser_action(state, subindex)
123 if index < goal then
124 high = middle - 1
125 else if index > goal then
126 low = middle + 1
127 else
128 action_type = parser_action(state, subindex+1)
129 action_value = parser_action(state, subindex+2)
130 break
131 end
132 end
133
134 if action_type == 0 then # SHIFT
135 push(action_value, lexer.next)
136 else if action_type == 1 then # REDUCE
137 _reduce_table[action_value].action(self)
138 else if action_type == 2 then # ACCEPT
139 var node2 = lexer.next
140 assert node2 isa EOF
141 var node1 = pop
142 assert node1 isa AModule
143 var node = new Start(node1, node2)
144 (new ComputeProdLocationVisitor).enter_visit(node)
145 return node
146 else if action_type == 3 then # ERROR
147 # skip injected tokens
148 while not isset token._location do token = lexer.next
149 var node2 = new AParserError.init_parser_error("Syntax error: unexpected {token}.", token.location, token)
150 var node = new Start(null, node2)
151 return node
152 end
153 end
154 end
155
156 private var reduce_table: Array[ReduceAction] is noinit
157 private fun build_reduce_table is abstract
158 end
159
160 redef class Prod
161 # Location on the first token after the start of a production
162 # So outside the production for epsilon production
163 var first_location: nullable Location
164
165 # Join the text of all visited tokens
166 fun collect_text: String
167 do
168 var v = new TextCollectorVisitor
169 v.enter_visit(self)
170 assert v.text != ""
171 return v.text
172 end
173 end
174
175 # Find location of production nodes
176 # Uses existing token locations to infer location of productions.
177 private class ComputeProdLocationVisitor
178 super Visitor
179 # Currently visited productions that need a first token
180 var need_first_prods = new Array[Prod]
181
182 # Already visited epsilon productions that waits something after them
183 var need_after_epsilons = new Array[Prod]
184
185 # Location of the last visited token in the current production
186 var last_location: nullable Location = null
187
188 redef fun visit(n: ANode)
189 do
190 if n isa Token then
191 if not isset n._location then return
192 var loc = n._location
193 _last_location = loc
194
195 # Add a first token to productions that need one
196 if not _need_first_prods.is_empty then
197 for no in _need_first_prods do
198 no._first_location = loc
199 end
200 _need_first_prods.clear
201 end
202
203 # Find location for already visited epsilon production that need one
204 if not _need_after_epsilons.is_empty then
205 var loco = new Location(loc.file, loc.line_start, loc.line_start, loc.column_start, loc.column_start)
206 for no in _need_after_epsilons do
207 no.location = loco
208 end
209 _need_after_epsilons.clear
210 end
211 else
212 assert n isa Prod
213 _need_first_prods.add(n)
214
215 n.visit_all(self)
216
217 var startl = n._first_location
218 if startl != null then
219 # Non-epsilon production
220 var endl = _last_location
221 assert endl != null
222
223 n.location = new Location(startl.file, startl.line_start, endl.line_end, startl.column_start, endl.column_end)
224
225 if not _need_after_epsilons.is_empty then
226 var loc = new Location(endl.file, endl.line_end, endl.line_end, endl.column_end, endl.column_end)
227 for no in _need_after_epsilons do
228 # Epsilon production that finishes the current non-epsilon production
229 no.location = loc
230 end
231 _need_after_epsilons.clear
232 end
233 else
234 # Epsilon production in the middle or that finishes a parent non-epsilon production
235 _need_after_epsilons.add(n)
236 end
237 end
238 end
239 end
240
241 private class TextCollectorVisitor
242 super Visitor
243 var text: String = ""
244 redef fun visit(n)
245 do
246 if n isa Token then text += n.text
247 n.visit_all(self)
248 end
249 end
250
251
252 # Each reduce action has its own class, this one is the root of the hierarchy.
253 private abstract class ReduceAction
254 fun action(p: Parser) is abstract
255 fun concat(l1, l2 : Array[Object]): Array[Object]
256 do
257 if l1.is_empty then return l2
258 l1.append(l2)
259 return l1
260 end
261 var goto: Int
262 end