Merge branch 'master' into comment-cache
[emacs.git] / lisp / cedet / semantic / debug.el
blob5c793e44aa99c03658d6eabc365e63bd02f2b102
1 ;;; semantic/debug.el --- Language Debugger framework
3 ;; Copyright (C) 2003-2005, 2008-2017 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; To provide better support for debugging parsers, this framework
25 ;; provides the interface for debugging. The work of parsing and
26 ;; controlling and stepping through the parsing work must be implemented
27 ;; by the parser.
29 ;; Fortunately, the nature of language support files means that the parser
30 ;; may not need to be instrumented first.
32 ;; The debugger uses EIEIO objects. One object controls the user
33 ;; interface, including stepping, data-view, queries. A second
34 ;; object implemented here represents the parser itself. A third represents
35 ;; a parser independent frame which knows how to highlight the parser buffer.
36 ;; Each parser must implement the interface and override any methods as needed.
39 (eval-when-compile (require 'cl))
40 (require 'semantic)
41 (require 'eieio)
42 (require 'cl-generic)
43 (eval-when-compile (require 'semantic/find))
45 ;;; Code:
47 ;;;###autoload
48 (defvar semantic-debug-parser-source nil
49 "For any buffer, the file name (no path) of the parser.
50 This would be a parser for a specific language, not the source
51 to one of the parser generators.")
52 ;;;###autoload
53 (make-variable-buffer-local 'semantic-debug-parser-source)
55 ;;;###autoload
56 (defvar semantic-debug-parser-class nil
57 "Class to create when building a debug parser object.")
58 ;;;###autoload
59 (make-variable-buffer-local 'semantic-debug-parser-class)
61 ;;;###autoload
62 (defvar semantic-debug-parser-debugger-source nil
63 "Location of the debug parser class.")
64 ;;;###autoload
65 (make-variable-buffer-local 'semantic-debug-parser-source)
67 (defvar semantic-debug-enabled nil
68 "Non-nil when debugging a parser.")
70 ;;; Variables used during a debug session.
71 (defvar semantic-debug-current-interface nil
72 "The debugger interface currently active for this buffer.")
74 (defvar semantic-debug-current-parser nil
75 "The parser current active for this buffer.")
77 ;;; User Interface Portion
79 (defclass semantic-debug-interface ()
80 ((parser-buffer :initarg :parser-buffer
81 :type buffer
82 :documentation
83 "The buffer containing the parser we are debugging.")
84 (parser-local-map :initarg :parser-local-map
85 :type keymap
86 :documentation
87 "The local keymap originally in the PARSER buffer.")
88 (parser-location :type marker
89 :documentation
90 "A marker representing where we are in the parser buffer.")
91 (source-buffer :initarg :source-buffer
92 :type buffer
93 :documentation
94 "The buffer containing the source we are parsing.
95 The :parser-buffer defines a parser that can parse the text in the
96 :source-buffer.")
97 (source-local-map :initarg :source-local-map
98 :type keymap
99 :documentation
100 "The local keymap originally in the SOURCE buffer.")
101 (source-location :type marker
102 :documentation
103 "A marker representing where we are in the parser buffer.")
104 (data-buffer :initarg :data-buffer
105 :type buffer
106 :documentation
107 "Buffer being used to display some useful data.
108 These buffers are brought into view when layout occurs.")
109 (current-frame :type semantic-debug-frame
110 :documentation
111 "The currently displayed frame.")
112 (overlays :type list
113 :initarg nil
114 :initform nil
115 :documentation
116 "Any active overlays being used to show the debug position.")
118 "Controls action when in `semantic-debug-mode'")
120 ;; Methods
121 (cl-defmethod semantic-debug-set-frame ((iface semantic-debug-interface) frame)
122 "Set the current frame on IFACE to FRAME."
123 (if frame
124 (oset iface current-frame frame)
125 (slot-makeunbound iface 'current-frame)))
127 (cl-defmethod semantic-debug-set-parser-location ((iface semantic-debug-interface) point)
128 "Set the parser location in IFACE to POINT."
129 (with-current-buffer (oref iface parser-buffer)
130 (if (not (slot-boundp iface 'parser-location))
131 (oset iface parser-location (make-marker)))
132 (move-marker (oref iface parser-location) point))
135 (cl-defmethod semantic-debug-set-source-location ((iface semantic-debug-interface) point)
136 "Set the source location in IFACE to POINT."
137 (with-current-buffer (oref iface source-buffer)
138 (if (not (slot-boundp iface 'source-location))
139 (oset iface source-location (make-marker)))
140 (move-marker (oref iface source-location) point))
143 (cl-defmethod semantic-debug-interface-layout ((iface semantic-debug-interface))
144 "Layout windows in the current frame to facilitate debugging."
145 (delete-other-windows)
146 ;; Deal with the data buffer
147 (when (slot-boundp iface 'data-buffer)
148 (let ((lines (/ (frame-height (selected-frame)) 3))
149 (cnt (with-current-buffer (oref iface data-buffer)
150 (count-lines (point-min) (point-max))))
152 ;; Set the number of lines to 1/3, or the size of the data buffer.
153 (if (< cnt lines) (setq cnt lines))
155 (split-window-vertically cnt)
156 (switch-to-buffer (oref iface data-buffer))
158 (other-window 1))
159 ;; Parser
160 (switch-to-buffer (oref iface parser-buffer))
161 (when (slot-boundp iface 'parser-location)
162 (goto-char (oref iface parser-location)))
163 (split-window-vertically)
164 (other-window 1)
165 ;; Source
166 (switch-to-buffer (oref iface source-buffer))
167 (when (slot-boundp iface 'source-location)
168 (goto-char (oref iface source-location)))
171 (cl-defmethod semantic-debug-highlight-lexical-token ((iface semantic-debug-interface) token)
172 "For IFACE, highlight TOKEN in the source buffer .
173 TOKEN is a lexical token."
174 (set-buffer (oref iface :source-buffer))
176 (object-add-to-list iface 'overlays
177 (semantic-lex-highlight-token token))
179 (semantic-debug-set-source-location iface (semantic-lex-token-start token))
182 (cl-defmethod semantic-debug-highlight-rule ((iface semantic-debug-interface) nonterm &optional rule match)
183 "For IFACE, highlight NONTERM in the parser buffer.
184 NONTERM is the name of the rule currently being processed that shows up
185 as a nonterminal (or tag) in the source buffer.
186 If RULE and MATCH indices are specified, highlight those also."
187 (set-buffer (oref iface :parser-buffer))
189 (let* ((rules (semantic-find-tags-by-class 'nonterminal (current-buffer)))
190 (nt (semantic-find-first-tag-by-name nonterm rules))
191 (o nil)
193 (when nt
194 ;; I know it is the first symbol appearing in the body of this token.
195 (goto-char (semantic-tag-start nt))
197 (setq o (semantic-make-overlay (point) (progn (forward-sexp 1) (point))))
198 (semantic-overlay-put o 'face 'highlight)
200 (object-add-to-list iface 'overlays o)
202 (semantic-debug-set-parser-location iface (semantic-overlay-start o))
204 (when (and rule match)
206 ;; Rule, an int, is the rule inside the nonterminal we are following.
207 (re-search-forward ":\\s-*")
208 (while (/= 0 rule)
209 (re-search-forward "^\\s-*|\\s-*")
210 (setq rule (1- rule)))
212 ;; Now find the match inside the rule
213 (while (/= 0 match)
214 (forward-sexp 1)
215 (skip-chars-forward " \t")
216 (setq match (1- match)))
218 ;; Now highlight the thingy we find there.
219 (setq o (semantic-make-overlay (point) (progn (forward-sexp 1) (point))))
220 (semantic-overlay-put o 'face 'highlight)
222 (object-add-to-list iface 'overlays o)
224 ;; If we have a match for a sub-rule, have the parser position
225 ;; move so we can see it in the output window for very long rules.
226 (semantic-debug-set-parser-location iface (semantic-overlay-start o))
228 ))))
230 (cl-defmethod semantic-debug-unhighlight ((iface semantic-debug-interface))
231 "Remove all debugging overlays."
232 (mapc 'semantic-overlay-delete (oref iface overlays))
233 (oset iface overlays nil))
235 ;; Call from the parser at a breakpoint
236 (defvar semantic-debug-user-command nil
237 "The command the user is requesting.")
239 (defun semantic-debug-break (frame)
240 "Stop parsing now at FRAME.
241 FRAME is an object that represents the parser's view of the
242 current state of the world.
243 This function enters a recursive edit. It returns
244 on an `exit-recursive-edit', or if someone uses one
245 of the `semantic-debug-mode' commands.
246 It returns the command specified. Parsers need to take action
247 on different types of return values."
248 (save-window-excursion
249 ;; Set up displaying information
250 (semantic-debug-mode t)
251 (unwind-protect
252 (progn
253 (semantic-debug-frame-highlight frame)
254 (semantic-debug-interface-layout semantic-debug-current-interface)
255 (condition-case nil
256 ;; Enter recursive edit... wait for user command.
257 (recursive-edit)
258 (error nil)))
259 (semantic-debug-unhighlight semantic-debug-current-interface)
260 (semantic-debug-mode nil))
261 ;; Find the requested user state. Do something.
262 (let ((returnstate semantic-debug-user-command))
263 (setq semantic-debug-user-command nil)
264 returnstate)
267 ;;; Frame
269 ;; A frame can represent the state at a break point.
270 (defclass semantic-debug-frame ()
273 "One frame representation.")
275 (cl-defmethod semantic-debug-frame-highlight ((frame semantic-debug-frame))
276 "Highlight one parser frame."
280 (cl-defmethod semantic-debug-frame-info ((frame semantic-debug-frame))
281 "Display info about this one parser frame."
285 ;;; Major Mode
287 (defvar semantic-debug-mode-map
288 (let ((km (make-sparse-keymap)))
289 (define-key km "n" 'semantic-debug-next)
290 (define-key km " " 'semantic-debug-next)
291 (define-key km "s" 'semantic-debug-step)
292 (define-key km "u" 'semantic-debug-up)
293 (define-key km "d" 'semantic-debug-down)
294 (define-key km "f" 'semantic-debug-fail-match)
295 (define-key km "h" 'semantic-debug-print-state)
296 (define-key km "s" 'semantic-debug-jump-to-source)
297 (define-key km "p" 'semantic-debug-jump-to-parser)
298 (define-key km "q" 'semantic-debug-quit)
299 (define-key km "a" 'semantic-debug-abort)
300 (define-key km "g" 'semantic-debug-go)
301 (define-key km "b" 'semantic-debug-set-breakpoint)
302 ;; Some boring bindings.
303 (define-key km "e" 'eval-expression)
306 "Keymap used when in semantic-debug-node.")
308 (defun semantic-debug-mode (onoff)
309 "Turn `semantic-debug-mode' on and off.
310 Argument ONOFF is non-nil when we are entering debug mode.
311 \\{semantic-debug-mode-map}"
312 (let ((iface semantic-debug-current-interface))
313 (if onoff
314 ;; Turn it on
315 (with-current-buffer (oref iface parser-buffer)
316 ;; Install our map onto this buffer
317 (use-local-map semantic-debug-mode-map)
318 ;; Make the buffer read only
319 (setq buffer-read-only t)
321 (set-buffer (oref iface source-buffer))
322 ;; Use our map in the source buffer also
323 (use-local-map semantic-debug-mode-map)
324 ;; Make the buffer read only
325 (setq buffer-read-only t)
326 ;; Hooks
327 (run-hooks 'semantic-debug-mode-hook)
329 ;; Restore old mode information
330 (with-current-buffer
331 (oref semantic-debug-current-interface parser-buffer)
332 (use-local-map
333 (oref semantic-debug-current-interface parser-local-map))
334 (setq buffer-read-only nil)
336 (with-current-buffer
337 (oref semantic-debug-current-interface source-buffer)
338 (use-local-map
339 (oref semantic-debug-current-interface source-local-map))
340 (setq buffer-read-only nil)
342 (run-hooks 'semantic-debug-exit-hook)
345 ;;;###autoload
346 (defun semantic-debug ()
347 "Parse the current buffer and run in debug mode."
348 (interactive)
349 (if semantic-debug-current-interface
350 (error "You are already in a debug session"))
351 (if (not semantic-debug-parser-class)
352 (error "This major mode does not support parser debugging"))
353 ;; Clear the cache to force a full reparse.
354 (semantic-clear-toplevel-cache)
355 ;; Load in the debugger for this file.
356 (when semantic-debug-parser-debugger-source
357 (require semantic-debug-parser-debugger-source))
358 ;; Do the parse
359 (let ((semantic-debug-enabled t)
360 ;; Create an interface
361 (semantic-debug-current-interface
362 (let ((parserb (semantic-debug-find-parser-source)))
363 (semantic-debug-interface
364 "Debug Interface"
365 :parser-buffer parserb
366 :parser-local-map (with-current-buffer parserb
367 (current-local-map))
368 :source-buffer (current-buffer)
369 :source-local-map (current-local-map)
371 ;; Create a parser debug interface
372 (semantic-debug-current-parser
373 (funcall semantic-debug-parser-class "parser"))
375 ;; We could recurse into a parser while debugging.
376 ;; Is that a problem?
377 (semantic-fetch-tags)
378 ;; We should turn the auto-parser back on, but don't do it for
379 ;; now until the debugger is working well.
382 (defun semantic-debug-find-parser-source ()
383 "Return a buffer containing the parser source file for the current buffer.
384 The parser needs to be on the load path, or this routine returns nil."
385 (if (not semantic-debug-parser-source)
386 (error "No parser is associated with this buffer"))
387 (let ((parser (locate-library semantic-debug-parser-source t)))
388 (if parser
389 (find-file-noselect parser)
390 (error "Cannot find parser source. It should be on the load-path"))))
392 ;;; Debugger commands
394 (defun semantic-debug-next ()
395 "Perform one parser operation.
396 In the recursive parser, this steps past one match rule.
397 In other parsers, this may be just like `semantic-debug-step'."
398 (interactive)
399 (let ((parser semantic-debug-current-parser))
400 (semantic-debug-parser-next parser)
401 (exit-recursive-edit)
405 (defun semantic-debug-step ()
406 "Perform one parser operation."
407 (interactive)
408 (let ((parser semantic-debug-current-parser))
409 (semantic-debug-parser-step parser)
410 (exit-recursive-edit)
414 (defun semantic-debug-up ()
415 "Move highlighting representation up one level."
416 (interactive)
417 (message "Not implemented yet.")
420 (defun semantic-debug-down ()
421 "Move highlighting representation down one level."
422 (interactive)
423 (message "Not implemented yet.")
426 (defun semantic-debug-fail-match ()
427 "Artificially fail the current match."
428 (interactive)
429 (let ((parser semantic-debug-current-parser))
430 (semantic-debug-parser-fail parser)
431 (exit-recursive-edit)
435 (defun semantic-debug-print-state ()
436 "Show interesting parser state."
437 (interactive)
438 (let ((parser semantic-debug-current-parser))
439 (semantic-debug-parser-print-state parser)
443 (defun semantic-debug-jump-to-source ()
444 "Move cursor to the source code being parsed at the current lexical token."
445 (interactive)
446 (let* ((interface semantic-debug-current-interface)
447 (buf (oref interface source-buffer)))
448 (if (get-buffer-window buf)
449 (progn
450 (select-frame (window-frame (get-buffer-window buf)))
451 (select-window (get-buffer-window buf)))
452 ;; Technically, this should do a window layout operation
453 (switch-to-buffer buf))
457 (defun semantic-debug-jump-to-parser ()
458 "Move cursor to the parser being debugged."
459 (interactive)
460 (let* ((interface semantic-debug-current-interface)
461 (buf (oref interface parser-buffer)))
462 (if (get-buffer-window buf)
463 (progn
464 (select-frame (window-frame (get-buffer-window buf)))
465 (select-window (get-buffer-window buf)))
466 ;; Technically, this should do a window layout operation
467 (switch-to-buffer buf))
471 (defun semantic-debug-quit ()
472 "Exit debug mode, blowing all stack, and leaving the parse incomplete.
473 Do not update any tokens already parsed."
474 (interactive)
475 (let ((parser semantic-debug-current-parser))
476 (semantic-debug-parser-quit parser)
477 (exit-recursive-edit)
481 (defun semantic-debug-abort ()
482 "Abort one level of debug mode, blowing all stack."
483 (interactive)
484 (let ((parser semantic-debug-current-parser))
485 (semantic-debug-parser-abort parser)
486 (exit-recursive-edit)
490 (defun semantic-debug-go ()
491 "Continue parsing till finish or breakpoint."
492 (interactive)
493 (let ((parser semantic-debug-current-parser))
494 (semantic-debug-parser-go parser)
495 (exit-recursive-edit)
499 (defun semantic-debug-set-breakpoint ()
500 "Set a breakpoint at the current rule location."
501 (interactive)
502 (let ((parser semantic-debug-current-parser)
503 ;; Get the location as semantic tokens.
504 (location (semantic-current-tag))
506 (if location
507 (semantic-debug-parser-break parser location)
508 (error "Not on a rule"))
513 ;;; Debugger superclass
515 (defclass semantic-debug-parser ()
518 "Represents a parser and its state.
519 When implementing the debug parser you can add extra functionality
520 by overriding one of the command methods. Be sure to use
521 `call-next-method' so that the debug command is saved, and passed
522 down to your parser later."
523 :abstract t)
525 (cl-defmethod semantic-debug-parser-next ((parser semantic-debug-parser))
526 "Execute next for this PARSER."
527 (setq semantic-debug-user-command 'next)
530 (cl-defmethod semantic-debug-parser-step ((parser semantic-debug-parser))
531 "Execute a step for this PARSER."
532 (setq semantic-debug-user-command 'step)
535 (cl-defmethod semantic-debug-parser-go ((parser semantic-debug-parser))
536 "Continue execution in this PARSER until the next breakpoint."
537 (setq semantic-debug-user-command 'go)
540 (cl-defmethod semantic-debug-parser-fail ((parser semantic-debug-parser))
541 "Continue execution in this PARSER until the next breakpoint."
542 (setq semantic-debug-user-command 'fail)
545 (cl-defmethod semantic-debug-parser-quit ((parser semantic-debug-parser))
546 "Continue execution in this PARSER until the next breakpoint."
547 (setq semantic-debug-user-command 'quit)
550 (cl-defmethod semantic-debug-parser-abort ((parser semantic-debug-parser))
551 "Continue execution in this PARSER until the next breakpoint."
552 (setq semantic-debug-user-command 'abort)
555 (cl-defmethod semantic-debug-parser-print-state ((parser semantic-debug-parser))
556 "Print state for this PARSER at the current breakpoint."
557 (with-slots (current-frame) semantic-debug-current-interface
558 (when current-frame
559 (semantic-debug-frame-info current-frame)
562 (cl-defmethod semantic-debug-parser-break ((parser semantic-debug-parser))
563 "Set a breakpoint for this PARSER."
566 ;; Stack stuff
567 (cl-defmethod semantic-debug-parser-frames ((parser semantic-debug-parser))
568 "Return a list of frames for the current parser.
569 A frame is of the form:
570 ( .. .what ? .. )
572 (error "Parser has not implemented frame values")
576 (provide 'semantic/debug)
578 ;; Local variables:
579 ;; generated-autoload-file: "loaddefs.el"
580 ;; generated-autoload-load-name: "semantic/debug"
581 ;; End:
583 ;;; semantic/debug.el ends here