]> code.delx.au - gnu-emacs-elpa/blob - packages/wisi/wisi-parse.el
4497b39ae45b34678e28af05a66aaca5fa3e8cee
[gnu-emacs-elpa] / packages / wisi / wisi-parse.el
1 ;;; wisi-parse.el --- Wisi parser
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
19
20
21 ;;; Commentary:
22
23 ;; An extended LALR parser, that handles shift/reduce and
24 ;; reduce/reduce conflicts by spawning parallel parsers to follow each
25 ;; path.
26
27 ;;; Code:
28
29 (require 'semantic/wisent)
30 (eval-when-compile (require 'cl-lib))
31
32 (cl-defstruct (wisi-parser-state
33 (:copier nil))
34 label ;; integer identifying parser for debug
35
36 active
37 ;; 'shift - need new token
38 ;; 'reduce - need reduce
39 ;; 'accept - parsing completed
40 ;; 'error - failed, error not reported yet
41 ;; nil - terminated
42 ;;
43 ;; 'pending-shift, 'pending-reduce - newly created parser; see wisi-parse
44
45 stack
46 ;; Each stack item takes two slots: (token-symbol token-text (token-start . token-end)), state
47 ;; token-text is nil for nonterminals.
48 ;; this is _not_ the same as the wisent-parse stack; that leaves out token-symbol.
49
50 sp ;; stack pointer
51
52 pending
53 ;; list of (action-symbol stack-fragment)
54 )
55
56 (defun wisi-error-msg (message &rest args)
57 (let ((line (line-number-at-pos))
58 (col (- (point) (line-beginning-position))))
59 (format
60 "%s:%d:%d: %s"
61 (file-name-nondirectory (buffer-name)) ;; buffer-file-name is sometimes nil here!?
62 line col
63 (apply 'format message args))))
64
65 (defvar wisi-parse-error nil)
66 (put 'wisi-parse-error
67 'error-conditions
68 '(error wisi-parse-error))
69 (put 'wisi-parse-error
70 'error-message
71 "wisi parse error")
72
73 (defvar wisi-parse-max-parallel 15
74 "Maximum number of parallel parsers for acceptable performance.
75 If a file needs more than this, it's probably an indication that
76 the grammar is excessively redundant.")
77
78 (defvar wisi-debug)
79
80 (defun wisi-parse (automaton lexer)
81 "Parse input using the automaton specified in AUTOMATON.
82
83 - AUTOMATON is the parse table generated by `wisi-compile-grammar'.
84
85 - LEXER is a function with no argument called by the parser to
86 obtain the next token in input, as a list (symbol text start
87 . end), where `symbol' is the terminal symbol, `text' is the
88 token string, `start . end' is the range in the buffer."
89 (let* ((actions (aref automaton 0))
90 (gotos (aref automaton 1))
91 (parser-states ;; vector of parallel parser states
92 (vector
93 (make-wisi-parser-state
94 :label 0
95 :active 'shift
96 :stack (make-vector wisent-parse-max-stack-size nil)
97 ;; FIXME: better error message when stack overflows, so
98 ;; user can set wisent-parse-max-stack-size in file-local
99 ;; vars.
100 :sp 0
101 :pending nil)))
102 (active-parser-count 1)
103 active-parser-count-prev
104 (active 'shift)
105 (token (funcall lexer))
106 some-pending)
107
108 (aset (wisi-parser-state-stack (aref parser-states 0)) 0 0) ;; Initial state
109
110 (while (not (eq active 'accept))
111 (setq active-parser-count-prev active-parser-count)
112 (setq some-pending nil)
113 (dotimes (parser-index (length parser-states))
114 (when (eq active (wisi-parser-state-active (aref parser-states parser-index)))
115 (let* ((parser-state (aref parser-states parser-index))
116 (result (wisi-parse-1 token parser-state (> active-parser-count 1) actions gotos)))
117 (when result
118 ;; spawn a new parser
119 (when (= active-parser-count wisi-parse-max-parallel)
120 (signal 'wisi-parse-error
121 (wisi-error-msg (concat "too many parallel parsers required;"
122 " simplify grammar, or increase `wisi-parse-max-parallel'"))))
123 (let ((j (wisi-free-parser parser-states)))
124 (cond
125 ((= j -1)
126 ;; Add to parser-states; the new parser won't be executed
127 ;; again in this parser-index loop.
128 (setq parser-states (vconcat parser-states (vector nil)))
129 (setq j (1- (length parser-states))))
130 ((< j parser-index)
131 ;; The new parser won't be executed again in this
132 ;; parser-index loop; nothing to do.
133 )
134 (t
135 ;; Don't let the new parser execute again in this
136 ;; parser-index loop.
137 (setq some-pending t)
138 (setf (wisi-parser-state-active result)
139 (cl-case (wisi-parser-state-active result)
140 (shift 'pending-shift)
141 (reduce 'pending-reduce)
142 )))
143 )
144 (setq active-parser-count (1+ active-parser-count))
145 (setf (wisi-parser-state-label result) j)
146 (aset parser-states j result))
147 (when (> wisi-debug 1)
148 (message "spawn parser (%d active)" active-parser-count)))
149
150 (when (eq 'error (wisi-parser-state-active parser-state))
151 (setq active-parser-count (1- active-parser-count))
152 (when (> wisi-debug 1)
153 (message "terminate parser (%d active)" active-parser-count))
154 (cl-case active-parser-count
155 (0
156 (cond
157 ((= active-parser-count-prev 1)
158 ;; We were not in a parallel parse; report the error.
159 (let ((state (aref (wisi-parser-state-stack parser-state)
160 (wisi-parser-state-sp parser-state))))
161 (signal 'wisi-parse-error
162 (wisi-error-msg "syntax error in grammar state %d; unexpected %s, expecting one of %s"
163 state
164 (nth 1 token)
165 (mapcar 'car (aref actions state))))
166 ))
167 (t
168 ;; Report errors from all parsers that failed on this token.
169 (let ((msg))
170 (dotimes (_ (length parser-states))
171 (let* ((parser-state (aref parser-states parser-index))
172 (state (aref (wisi-parser-state-stack parser-state)
173 (wisi-parser-state-sp parser-state))))
174 (when (eq 'error (wisi-parser-state-active parser-state))
175 (setq msg
176 (concat msg
177 (when msg "\n")
178 (wisi-error-msg
179 "syntax error in grammar state %d; unexpected %s, expecting one of %s"
180 state
181 (nth 1 token)
182 (mapcar 'car (aref actions state)))))
183 )))
184 (signal 'wisi-parse-error msg)))
185 ))
186
187 (1
188 (setf (wisi-parser-state-active parser-state) nil); Don't save error for later.
189 (wisi-execute-pending (wisi-parser-state-pending
190 (aref parser-states (wisi-active-parser parser-states))))
191 (setf (wisi-parser-state-pending
192 (aref parser-states (wisi-active-parser parser-states)))
193 nil))
194 (t
195 ;; We were in a parallel parse, and this parser
196 ;; failed; mark it inactive, don't save error for
197 ;; later.
198 (setf (wisi-parser-state-active parser-state) nil)
199 )))
200 )));; end dotimes
201
202 (when some-pending
203 ;; Change pending-* parsers to *.
204 (dotimes (parser-index (length parser-states))
205 (cond
206 ((eq (wisi-parser-state-active (aref parser-states parser-index)) 'pending-shift)
207 (setf (wisi-parser-state-active (aref parser-states parser-index)) 'shift))
208 ((eq (wisi-parser-state-active (aref parser-states parser-index)) 'pending-reduce)
209 (setf (wisi-parser-state-active (aref parser-states parser-index)) 'reduce))
210 )))
211
212 (setq active (wisi-parsers-active parser-states active-parser-count))
213 (when (eq active 'shift)
214 (when (> active-parser-count 1)
215 (setq active-parser-count (wisi-parse-elim-identical parser-states active-parser-count)))
216 (setq token (funcall lexer)))
217 )
218 (when (> active-parser-count 1)
219 (error "ambiguous parse result"))))
220
221 (defun wisi-parsers-active (parser-states active-count)
222 "Return the type of parser cycle to execute.
223 PARSER-STATES[*].active is the last action a parser took. If it
224 was 'shift, that parser used the input token, and should not be
225 executed again until another input token is available, after all
226 parsers have shifted the current token or terminated.
227
228 'accept : all PARSER-STATES have active set to nil or 'accept -
229 done parsing
230
231 'shift : all PARSER-STATES have active set to nil, 'accept, or
232 'shift - get a new token, execute 'shift parsers.
233
234 'reduce : some PARSER-STATES have active set to 'reduce - no new
235 token, execute 'reduce parsers."
236 (let ((result nil)
237 (i 0)
238 (shift-count 0)
239 (accept-count 0)
240 active)
241 (while (and (not result)
242 (< i (length parser-states)))
243 (setq active (wisi-parser-state-active (aref parser-states i)))
244 (cond
245 ((eq active 'shift) (setq shift-count (1+ shift-count)))
246 ((eq active 'reduce) (setq result 'reduce))
247 ((eq active 'accept) (setq accept-count (1+ accept-count)))
248 )
249 (setq i (1+ i)))
250
251 (cond
252 (result )
253 ((= accept-count active-count)
254 'accept)
255 ((= (+ shift-count accept-count) active-count)
256 'shift)
257 (t (error "unexpected result in wisi-parsers-active"))
258 )))
259
260 (defun wisi-free-parser (parser-states)
261 "Return index to a non-active parser in PARSER-STATES, -1 if there is none."
262 (let ((result nil)
263 (i 0))
264 (while (and (not result)
265 (< i (length parser-states)))
266 (when (not (wisi-parser-state-active (aref parser-states i)))
267 (setq result i))
268 (setq i (1+ i)))
269 (if result result -1)))
270
271 (defun wisi-active-parser (parser-states)
272 "Return index to the first active parser in PARSER-STATES."
273 (let ((result nil)
274 (i 0))
275 (while (and (not result)
276 (< i (length parser-states)))
277 (when (wisi-parser-state-active (aref parser-states i))
278 (setq result i))
279 (setq i (1+ i)))
280 (unless result
281 (error "no active parsers"))
282 result))
283
284 (defun wisi-parse-elim-identical (parser-states active-parser-count)
285 "Check for parsers in PARSER-STATES that have reached identical states eliminate one.
286 Return new ACTIVE-PARSER-COUNT. Assumes all parsers have active
287 nil, 'shift, or 'accept."
288 ;; parser-states passed by reference; active-parser-count by copy
289 ;; see test/ada_mode-slices.adb for example
290 (dotimes (parser-i (1- (length parser-states)))
291 (when (wisi-parser-state-active (aref parser-states parser-i))
292 (dotimes (parser-j (- (length parser-states) parser-i 1))
293 (when (wisi-parser-state-active (aref parser-states (+ parser-i parser-j 1)))
294 (when (eq (wisi-parser-state-sp (aref parser-states parser-i))
295 (wisi-parser-state-sp (aref parser-states (+ parser-i parser-j 1))))
296 (let ((compare t))
297 (dotimes (stack-i (wisi-parser-state-sp (aref parser-states parser-i)))
298 (setq
299 compare
300 (and compare
301 (equal (aref (wisi-parser-state-stack (aref parser-states parser-i)) stack-i)
302 (aref (wisi-parser-state-stack (aref parser-states (+ parser-i parser-j 1))) stack-i)))))
303 (when compare
304 ;; parser stacks are identical
305 (setq active-parser-count (1- active-parser-count))
306 (when (> wisi-debug 1)
307 (message "terminate identical parser %d (%d active)"
308 (+ parser-i parser-j 1) active-parser-count))
309 (when (= active-parser-count 1)
310 ;; the actions for the two parsers are not
311 ;; identical, but either is good enough for
312 ;; indentation and navigation, so we just do one.
313 (when (> wisi-debug 1) (message "executing actions for %d" (+ parser-i parser-j 1)))
314 (wisi-execute-pending (wisi-parser-state-pending (aref parser-states (+ parser-i parser-j 1))))
315 (setf (wisi-parser-state-pending (aref parser-states (+ parser-i parser-j 1))) nil)
316
317 ;; clear pending of other parser so it can be reused
318 (setf (wisi-parser-state-pending (aref parser-states parser-i)) nil))
319
320 (setf (wisi-parser-state-active (aref parser-states (+ parser-i parser-j 1))) nil))
321 )))
322 )))
323 active-parser-count)
324
325 (defun wisi-execute-pending (pending)
326 (while pending
327 (when (> wisi-debug 1) (message "%s" (car pending)))
328 (apply (pop pending))))
329
330 (defun wisi-parse-1 (token parser-state pendingp actions gotos)
331 "Perform one shift or reduce on PARSER-STATE.
332 If PENDINGP, push actions onto PARSER-STATE.pending; otherwise execute them.
333 See `wisi-parse' for full details.
334 Return nil or new parser (a wisi-parse-state struct)."
335 (let* ((state (aref (wisi-parser-state-stack parser-state)
336 (wisi-parser-state-sp parser-state)))
337 (parse-action (wisent-parse-action (car token) (aref actions state)))
338 new-parser-state)
339
340 (when (> wisi-debug 1)
341 ;; output trace info
342 (if (> wisi-debug 2)
343 (progn
344 ;; put top 10 stack items
345 (let* ((count (min 20 (wisi-parser-state-sp parser-state)))
346 (msg (make-vector (+ 1 count) nil)))
347 (dotimes (i count)
348 (aset msg (- count i)
349 (aref (wisi-parser-state-stack parser-state) (- (wisi-parser-state-sp parser-state) i)))
350 )
351 (message "%d: %s: %d: %s"
352 (wisi-parser-state-label parser-state)
353 (wisi-parser-state-active parser-state)
354 (wisi-parser-state-sp parser-state)
355 msg))
356 (message " %d: %s: %s" state token parse-action))
357 (message "%d: %d: %s: %s" (wisi-parser-state-label parser-state) state token parse-action)))
358
359 (when (and (listp parse-action)
360 (not (symbolp (car parse-action))))
361 ;; Conflict; spawn a new parser.
362 (setq new-parser-state
363 (make-wisi-parser-state
364 :active nil
365 :stack (vconcat (wisi-parser-state-stack parser-state))
366 :sp (wisi-parser-state-sp parser-state)
367 :pending (wisi-parser-state-pending parser-state)))
368
369 (wisi-parse-2 (cadr parse-action) token new-parser-state t gotos)
370 (setq pendingp t)
371 (setq parse-action (car parse-action))
372 );; when
373
374 ;; current parser
375 (wisi-parse-2 parse-action token parser-state pendingp gotos)
376
377 new-parser-state))
378
379 (defun wisi-parse-2 (action token parser-state pendingp gotos)
380 "Execute parser ACTION (must not be a conflict).
381 Return nil."
382 (cond
383 ((eq action 'accept)
384 (setf (wisi-parser-state-active parser-state) 'accept))
385
386 ((eq action 'error)
387 (setf (wisi-parser-state-active parser-state) 'error))
388
389 ((natnump action)
390 ;; Shift token and new state (= action) onto stack
391 (let ((stack (wisi-parser-state-stack parser-state)); reference
392 (sp (wisi-parser-state-sp parser-state))); copy
393 (setq sp (+ sp 2))
394 (aset stack (1- sp) token)
395 (aset stack sp action)
396 (setf (wisi-parser-state-sp parser-state) sp))
397 (setf (wisi-parser-state-active parser-state) 'shift))
398
399 (t
400 (wisi-parse-reduce action parser-state pendingp gotos)
401 (setf (wisi-parser-state-active parser-state) 'reduce))
402 ))
403
404 (defun wisi-nonterm-bounds (stack i j)
405 "Return a pair (START . END), the buffer region for a nonterminal.
406 STACK is the parser stack. I and J are the indices in STACK of
407 the first and last tokens of the nonterminal."
408 (let ((start (cl-caddr (aref stack i)))
409 (end (cl-cdddr (aref stack j))))
410 (while (and (or (not start) (not end))
411 (/= i j))
412 (cond
413 ((not start)
414 ;; item i is an empty production
415 (setq start (cl-caddr (aref stack (setq i (+ i 2))))))
416
417 ((not end)
418 ;; item j is an empty production
419 (setq end (cl-cdddr (aref stack (setq j (- j 2))))))
420
421 (t (setq i j))))
422 (and start end (cons start end))))
423
424 (defun wisi-parse-reduce (action parser-state pendingp gotos)
425 "Reduce PARSER-STATE.stack, and execute or pend ACTION."
426 (let* ((stack (wisi-parser-state-stack parser-state)); reference
427 (sp (wisi-parser-state-sp parser-state)); copy
428 (token-count (or (nth 2 action) 0))
429 (nonterm (nth 0 action))
430 (nonterm-region (when (> token-count 0)
431 (wisi-nonterm-bounds stack (- sp (* 2 (1- token-count)) 1) (1- sp))))
432 (post-reduce-state (aref stack (- sp (* 2 token-count))))
433 (new-state (cdr (assoc nonterm (aref gotos post-reduce-state))))
434 tokens)
435 (when (not new-state)
436 (error "no goto for %s %d" nonterm post-reduce-state))
437 (if (= 1 token-count)
438 (setq tokens (list (aref stack (1- sp))))
439 (dotimes (i token-count)
440 (push (aref stack (- sp (* 2 i) 1)) tokens)))
441 (setq sp (+ 2 (- sp (* 2 token-count))))
442 (aset stack (1- sp) (cons nonterm (cons nil nonterm-region)))
443 (aset stack sp new-state)
444 (setf (wisi-parser-state-sp parser-state) sp)
445 (if pendingp
446 (if (wisi-parser-state-pending parser-state)
447 (setf (wisi-parser-state-pending parser-state)
448 (append (wisi-parser-state-pending parser-state)
449 (list (list (nth 1 action) tokens))))
450 (setf (wisi-parser-state-pending parser-state)
451 (list (list (nth 1 action) tokens))))
452 (funcall (nth 1 action) tokens))
453 ))
454
455 (provide 'wisi-parse)
456 ;;; wisi-parse.el ends here