1 ;;; wisi-parse.el --- Wisi parser
3 ;; Copyright (C) 2013, 2014 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;; An extended LALR parser, that handles shift/reduce and
24 ;; reduce/reduce conflicts by spawning parallel parsers to follow each
30 (require 'semantic/wisent)
32 ;; WORKAROUND: for some reason, this condition doesn't work in batch mode!
33 ;; (when (and (= emacs-major-version 24)
34 ;; (= emacs-minor-version 2))
35 (require 'wisi-compat-24.2)
38 (defvar wisi-parse-max-parallel 15
39 "Maximum number of parallel parsers for acceptable performance.
40 If a file needs more than this, it's probably an indication that
41 the grammar is excessively redundant.")
43 (defvar wisi-parse-max-parallel-current (cons 0 0)
44 "Cons (count . point); Maximum number of parallel parsers used in most recent parse,
45 point at which that max was spawned.")
49 0 : normal - ignore parse errors, for indenting new code
50 1 : report parse errors (for running tests)
51 2 : show parse states, position point at parse errors, debug-on-error works in parser
52 3 : also show top 10 items of parser stack.")
54 (cl-defstruct (wisi-parser-state
56 label ;; integer identifying parser for debug
59 ;; 'shift - need new token
60 ;; 'reduce - need reduce
61 ;; 'accept - parsing completed
62 ;; 'error - failed, error not reported yet
65 ;; 'pending-shift, 'pending-reduce - newly created parser; see wisi-parse
68 ;; Each stack item takes two slots: (token-symbol token-text (token-start . token-end)), state
69 ;; token-text is nil for nonterminals.
70 ;; this is _not_ the same as the wisent-parse stack; that leaves out token-symbol.
75 ;; list of (action-symbol stack-fragment)
78 (defun wisi-error-msg (message &rest args)
79 (let ((line (line-number-at-pos))
80 (col (- (point) (line-beginning-position))))
83 (file-name-nondirectory (buffer-name)) ;; buffer-file-name is sometimes nil here!?
85 (apply 'format message args))))
87 (defvar wisi-parse-error nil)
88 (put 'wisi-parse-error
90 '(error wisi-parse-error))
91 (put 'wisi-parse-error
95 (defvar-local wisi-cache-max 0
96 "Maximimum position in buffer where wisi-cache text properties are valid.")
98 (defun wisi-parse (automaton lexer)
99 "Parse current buffer from bob using the automaton specified in AUTOMATON.
101 - AUTOMATON is the parse table generated by `wisi-compile-grammar'.
103 - LEXER is a function with no argument called by the parser to
104 obtain the next token from the current buffer after point, as a
105 list (symbol text start . end), where `symbol' is the terminal
106 symbol, `text' is the token string, `start . end' is the range
108 (let* ((actions (aref automaton 0))
109 (gotos (aref automaton 1))
110 (parser-states ;; vector of parallel parser states
112 (make-wisi-parser-state
115 :stack (make-vector wisent-parse-max-stack-size nil)
118 (active-parser-count 1)
119 active-parser-count-prev
125 (goto-char (point-min))
126 (aset (wisi-parser-state-stack (aref parser-states 0)) 0 0)
128 (setq token (funcall lexer))
129 (setq wisi-parse-max-parallel-current (cons 0 0))
131 (while (not (eq active 'accept))
132 (setq active-parser-count-prev active-parser-count)
133 (setq some-pending nil)
134 (dotimes (parser-index (length parser-states))
135 (when (eq active (wisi-parser-state-active (aref parser-states parser-index)))
136 (let* ((parser-state (aref parser-states parser-index))
137 (result (wisi-parse-1 token parser-state (> active-parser-count 1) actions gotos)))
139 ;; spawn a new parser
140 (when (= active-parser-count wisi-parse-max-parallel)
141 (signal 'wisi-parse-error
142 (let ((state (aref (wisi-parser-state-stack parser-state)
143 (wisi-parser-state-sp parser-state))))
144 (wisi-error-msg (concat "too many parallel parsers required in grammar state %d;"
145 " simplify grammar, or increase `wisi-parse-max-parallel'"
148 (let ((j (wisi-free-parser parser-states)))
151 ;; Add to parser-states; the new parser won't be executed
152 ;; again in this parser-index loop.
153 (setq parser-states (vconcat parser-states (vector nil)))
154 (setq j (1- (length parser-states))))
156 ;; The new parser won't be executed again in this
157 ;; parser-index loop; nothing to do.
160 ;; Don't let the new parser execute again in this
161 ;; parser-index loop.
162 (setq some-pending t)
163 (setf (wisi-parser-state-active result)
164 (cl-case (wisi-parser-state-active result)
165 (shift 'pending-shift)
166 (reduce 'pending-reduce)
169 (setq active-parser-count (1+ active-parser-count))
170 (when (> active-parser-count (car wisi-parse-max-parallel-current))
171 (setq wisi-parse-max-parallel-current (cons active-parser-count (point))))
172 (setf (wisi-parser-state-label result) j)
173 (aset parser-states j result))
174 (when (> wisi-debug 1)
175 (message "spawn parser (%d active)" active-parser-count)))
177 (when (eq 'error (wisi-parser-state-active parser-state))
178 (setq active-parser-count (1- active-parser-count))
179 (when (> wisi-debug 1)
180 (message "terminate parser (%d active)" active-parser-count))
181 (cl-case active-parser-count
184 ((= active-parser-count-prev 1)
185 ;; We were not in a parallel parse; report the error.
186 (let ((state (aref (wisi-parser-state-stack parser-state)
187 (wisi-parser-state-sp parser-state))))
188 (signal 'wisi-parse-error
189 (wisi-error-msg "syntax error in grammar state %d; unexpected %s, expecting one of %s"
192 (mapcar 'car (aref actions state))))
195 ;; Report errors from all parsers that failed on this token.
197 (dotimes (_ (length parser-states))
198 (let* ((parser-state (aref parser-states parser-index))
199 (state (aref (wisi-parser-state-stack parser-state)
200 (wisi-parser-state-sp parser-state))))
201 (when (eq 'error (wisi-parser-state-active parser-state))
206 "syntax error in grammar state %d; unexpected %s, expecting one of %s"
209 (mapcar 'car (aref actions state)))))
211 (signal 'wisi-parse-error msg)))
215 (setf (wisi-parser-state-active parser-state) nil); Don't save error for later.
216 (wisi-execute-pending (wisi-parser-state-pending
217 (aref parser-states (wisi-active-parser parser-states))))
218 (setf (wisi-parser-state-pending
219 (aref parser-states (wisi-active-parser parser-states)))
222 ;; We were in a parallel parse, and this parser
223 ;; failed; mark it inactive, don't save error for
225 (setf (wisi-parser-state-active parser-state) nil)
230 ;; Change pending-* parsers to *.
231 (dotimes (parser-index (length parser-states))
233 ((eq (wisi-parser-state-active (aref parser-states parser-index)) 'pending-shift)
234 (setf (wisi-parser-state-active (aref parser-states parser-index)) 'shift))
235 ((eq (wisi-parser-state-active (aref parser-states parser-index)) 'pending-reduce)
236 (setf (wisi-parser-state-active (aref parser-states parser-index)) 'reduce))
239 (setq active (wisi-parsers-active parser-states active-parser-count))
240 (when (eq active 'shift)
241 (when (> active-parser-count 1)
242 (setq active-parser-count (wisi-parse-elim-identical parser-states active-parser-count)))
244 (setq token (funcall lexer)))
246 (when (> active-parser-count 1)
247 (error "ambiguous parse result"))))
249 (defun wisi-parsers-active-index (parser-states)
250 ;; only called when active-parser-count = 1
253 (while (and (not result)
254 (< i (length parser-states)))
255 (when (wisi-parser-state-active (aref parser-states i))
260 (defun wisi-parsers-active (parser-states active-count)
261 "Return the type of parser cycle to execute.
262 PARSER-STATES[*].active is the last action a parser took. If it
263 was 'shift, that parser used the input token, and should not be
264 executed again until another input token is available, after all
265 parsers have shifted the current token or terminated.
269 'accept : all PARSER-STATES have active set to nil or 'accept -
272 'shift : all PARSER-STATES have active set to nil, 'accept, or
273 'shift - get a new token, execute 'shift parsers.
275 'reduce : some PARSER-STATES have active set to 'reduce - no new
276 token, execute 'reduce parsers."
282 (while (and (not result)
283 (< i (length parser-states)))
284 (setq active (wisi-parser-state-active (aref parser-states i)))
286 ((eq active 'shift) (setq shift-count (1+ shift-count)))
287 ((eq active 'reduce) (setq result 'reduce))
288 ((eq active 'accept) (setq accept-count (1+ accept-count)))
294 ((= accept-count active-count)
296 ((= (+ shift-count accept-count) active-count)
299 ;; all parsers in error state; should not get here
300 (error "all parsers in error state; programmer error"))
303 (defun wisi-free-parser (parser-states)
304 "Return index to a non-active parser in PARSER-STATES, -1 if there is none."
307 (while (and (not result)
308 (< i (length parser-states)))
309 (when (not (wisi-parser-state-active (aref parser-states i)))
312 (if result result -1)))
314 (defun wisi-active-parser (parser-states)
315 "Return index to the first active parser in PARSER-STATES."
318 (while (and (not result)
319 (< i (length parser-states)))
320 (when (wisi-parser-state-active (aref parser-states i))
324 (error "no active parsers"))
327 (defun wisi-parse-elim-identical (parser-states active-parser-count)
328 "Check for parsers in PARSER-STATES that have reached identical states eliminate one.
329 Return new ACTIVE-PARSER-COUNT. Assumes all parsers have active
330 nil, 'shift, or 'accept."
331 ;; parser-states passed by reference; active-parser-count by copy
332 ;; see test/ada_mode-slices.adb for example
333 (dotimes (parser-i (1- (length parser-states)))
334 (when (wisi-parser-state-active (aref parser-states parser-i))
335 (dotimes (parser-j (- (length parser-states) parser-i 1))
336 (when (wisi-parser-state-active (aref parser-states (+ parser-i parser-j 1)))
337 (when (eq (wisi-parser-state-sp (aref parser-states parser-i))
338 (wisi-parser-state-sp (aref parser-states (+ parser-i parser-j 1))))
340 (dotimes (stack-i (wisi-parser-state-sp (aref parser-states parser-i)))
344 (equal (aref (wisi-parser-state-stack (aref parser-states parser-i)) stack-i)
345 (aref (wisi-parser-state-stack (aref parser-states (+ parser-i parser-j 1))) stack-i)))))
347 ;; parser stacks are identical
348 (setq active-parser-count (1- active-parser-count))
349 (when (> wisi-debug 1)
350 (message "terminate identical parser %d (%d active)"
351 (+ parser-i parser-j 1) active-parser-count))
352 (when (= active-parser-count 1)
353 ;; the actions for the two parsers are not
354 ;; identical, but either is good enough for
355 ;; indentation and navigation, so we just do one.
356 (when (> wisi-debug 1) (message "executing actions for %d" (+ parser-i parser-j 1)))
357 (wisi-execute-pending (wisi-parser-state-pending (aref parser-states (+ parser-i parser-j 1))))
358 (setf (wisi-parser-state-pending (aref parser-states (+ parser-i parser-j 1))) nil)
360 ;; clear pending of other parser so it can be reused
361 (setf (wisi-parser-state-pending (aref parser-states parser-i)) nil))
363 (setf (wisi-parser-state-active (aref parser-states (+ parser-i parser-j 1))) nil))
368 (defun wisi-parse-max-pos (tokens)
369 "Return max position in tokens, or point if tokens nil."
370 (let ((result (if tokens 0 (point))))
373 (when (cl-cdddr token)
374 (setq result (max (cl-cdddr token) result))))
379 (defun wisi-parse-exec-action (func tokens)
380 "Execute action if all tokens past wisi-cache-max."
381 ;; We don't execute actions if all tokens are before wisi-cache-max,
382 ;; because later actions can update existing caches, and if the
383 ;; parse fails that won't happen. It also saves time.
384 (if (>= (wisi-parse-max-pos tokens) wisi-cache-max)
386 (funcall func tokens)
388 (when (> wisi-debug 1)
389 (message "... action skipped"))
392 (defun wisi-execute-pending (pending)
394 (when (> wisi-debug 1) (message "%s" (car pending)))
396 (let ((func-args (pop pending)))
397 (wisi-parse-exec-action (car func-args) (cadr func-args)))
400 (defun wisi-parse-1 (token parser-state pendingp actions gotos)
401 "Perform one shift or reduce on PARSER-STATE.
402 If PENDINGP, push actions onto PARSER-STATE.pending; otherwise execute them.
403 See `wisi-parse' for full details.
404 Return nil or new parser (a wisi-parse-state struct)."
405 (let* ((state (aref (wisi-parser-state-stack parser-state)
406 (wisi-parser-state-sp parser-state)))
407 (parse-action (wisent-parse-action (car token) (aref actions state)))
410 (when (> wisi-debug 1)
414 ;; put top 10 stack items
415 (let* ((count (min 20 (wisi-parser-state-sp parser-state)))
416 (msg (make-vector (+ 1 count) nil)))
418 (aset msg (- count i)
419 (aref (wisi-parser-state-stack parser-state) (- (wisi-parser-state-sp parser-state) i)))
421 (message "%d: %s: %d: %s"
422 (wisi-parser-state-label parser-state)
423 (wisi-parser-state-active parser-state)
424 (wisi-parser-state-sp parser-state)
426 (message " %d: %s: %s" state token parse-action))
427 (message "%d: %d: %s: %s" (wisi-parser-state-label parser-state) state token parse-action)))
429 (when (and (listp parse-action)
430 (not (symbolp (car parse-action))))
431 ;; Conflict; spawn a new parser.
432 (setq new-parser-state
433 (make-wisi-parser-state
435 :stack (vconcat (wisi-parser-state-stack parser-state))
436 :sp (wisi-parser-state-sp parser-state)
437 :pending (wisi-parser-state-pending parser-state)))
439 (wisi-parse-2 (cadr parse-action) token new-parser-state t gotos)
441 (setq parse-action (car parse-action))
445 (wisi-parse-2 parse-action token parser-state pendingp gotos)
449 (defun wisi-parse-2 (action token parser-state pendingp gotos)
450 "Execute parser ACTION (must not be a conflict).
454 (setf (wisi-parser-state-active parser-state) 'accept))
457 (setf (wisi-parser-state-active parser-state) 'error))
460 ;; Shift token and new state (= action) onto stack
461 (let ((stack (wisi-parser-state-stack parser-state)); reference
462 (sp (wisi-parser-state-sp parser-state))); copy
464 (aset stack (1- sp) token)
465 (aset stack sp action)
466 (setf (wisi-parser-state-sp parser-state) sp))
467 (setf (wisi-parser-state-active parser-state) 'shift))
470 (wisi-parse-reduce action parser-state pendingp gotos)
471 (setf (wisi-parser-state-active parser-state) 'reduce))
474 (defun wisi-nonterm-bounds (stack i j)
475 "Return a pair (START . END), the buffer region for a nonterminal.
476 STACK is the parser stack. I and J are the indices in STACK of
477 the first and last tokens of the nonterminal."
478 (let ((start (cl-caddr (aref stack i)))
479 (end (cl-cdddr (aref stack j))))
480 (while (and (or (not start) (not end))
484 ;; item i is an empty production
485 (setq start (cl-caddr (aref stack (setq i (+ i 2))))))
488 ;; item j is an empty production
489 (setq end (cl-cdddr (aref stack (setq j (- j 2))))))
492 (and start end (cons start end))))
494 (defun wisi-parse-reduce (action parser-state pendingp gotos)
495 "Reduce PARSER-STATE.stack, and execute or pend ACTION."
496 (let* ((stack (wisi-parser-state-stack parser-state)); reference
497 (sp (wisi-parser-state-sp parser-state)); copy
498 (token-count (or (nth 2 action) 0))
499 (nonterm (nth 0 action))
500 (nonterm-region (when (> token-count 0)
501 (wisi-nonterm-bounds stack (- sp (* 2 (1- token-count)) 1) (1- sp))))
502 (post-reduce-state (aref stack (- sp (* 2 token-count))))
503 (new-state (cdr (assoc nonterm (aref gotos post-reduce-state))))
505 (when (not new-state)
506 (error "no goto for %s %d" nonterm post-reduce-state))
507 (if (= 1 token-count)
508 (setq tokens (list (aref stack (1- sp))))
509 (dotimes (i token-count)
510 (push (aref stack (- sp (* 2 i) 1)) tokens)))
511 (setq sp (+ 2 (- sp (* 2 token-count))))
512 (aset stack (1- sp) (cons nonterm (cons nil nonterm-region)))
513 (aset stack sp new-state)
514 (setf (wisi-parser-state-sp parser-state) sp)
516 (if (wisi-parser-state-pending parser-state)
517 (setf (wisi-parser-state-pending parser-state)
518 (append (wisi-parser-state-pending parser-state)
519 (list (list (nth 1 action) tokens))))
520 (setf (wisi-parser-state-pending parser-state)
521 (list (list (nth 1 action) tokens))))
524 (wisi-parse-exec-action (nth 1 action) tokens)
528 (provide 'wisi-parse)
529 ;;; wisi-parse.el ends here