Update copyright year to 2015
[emacs.git] / lisp / cedet / semantic / debug.el
blob9168af59dd4471180b59ed9a5aafee9c234c637e
1 ;;; semantic/debug.el --- Language Debugger framework
3 ;; Copyright (C) 2003-2005, 2008-2015 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 (eval-when-compile (require 'semantic/find))
44 ;;; Code:
46 ;;;###autoload
47 (defvar semantic-debug-parser-source nil
48 "For any buffer, the file name (no path) of the parser.
49 This would be a parser for a specific language, not the source
50 to one of the parser generators.")
51 ;;;###autoload
52 (make-variable-buffer-local 'semantic-debug-parser-source)
54 ;;;###autoload
55 (defvar semantic-debug-parser-class nil
56 "Class to create when building a debug parser object.")
57 ;;;###autoload
58 (make-variable-buffer-local 'semantic-debug-parser-class)
60 ;;;###autoload
61 (defvar semantic-debug-parser-debugger-source nil
62 "Location of the debug parser class.")
63 ;;;###autoload
64 (make-variable-buffer-local 'semantic-debug-parser-source)
66 (defvar semantic-debug-enabled nil
67 "Non-nil when debugging a parser.")
69 ;;; Variables used during a debug session.
70 (defvar semantic-debug-current-interface nil
71 "The debugger interface currently active for this buffer.")
73 (defvar semantic-debug-current-parser nil
74 "The parser current active for this buffer.")
76 ;;; User Interface Portion
78 (defclass semantic-debug-interface ()
79 ((parser-buffer :initarg :parser-buffer
80 :type buffer
81 :documentation
82 "The buffer containing the parser we are debugging.")
83 (parser-local-map :initarg :parser-local-map
84 :type keymap
85 :documentation
86 "The local keymap originally in the PARSER buffer.")
87 (parser-location :type marker
88 :documentation
89 "A marker representing where we are in the parser buffer.")
90 (source-buffer :initarg :source-buffer
91 :type buffer
92 :documentation
93 "The buffer containing the source we are parsing.
94 The :parser-buffer defines a parser that can parse the text in the
95 :source-buffer.")
96 (source-local-map :initarg :source-local-map
97 :type keymap
98 :documentation
99 "The local keymap originally in the SOURCE buffer.")
100 (source-location :type marker
101 :documentation
102 "A marker representing where we are in the parser buffer.")
103 (data-buffer :initarg :data-buffer
104 :type buffer
105 :documentation
106 "Buffer being used to display some useful data.
107 These buffers are brought into view when layout occurs.")
108 (current-frame :type semantic-debug-frame
109 :documentation
110 "The currently displayed frame.")
111 (overlays :type list
112 :initarg nil
113 :initform nil
114 :documentation
115 "Any active overlays being used to show the debug position.")
117 "Controls action when in `semantic-debug-mode'")
119 ;; Methods
120 (defmethod semantic-debug-set-frame ((iface semantic-debug-interface) frame)
121 "Set the current frame on IFACE to FRAME."
122 (if frame
123 (oset iface current-frame frame)
124 (slot-makeunbound iface 'current-frame)))
126 (defmethod semantic-debug-set-parser-location ((iface semantic-debug-interface) point)
127 "Set the parser location in IFACE to POINT."
128 (with-current-buffer (oref iface parser-buffer)
129 (if (not (slot-boundp iface 'parser-location))
130 (oset iface parser-location (make-marker)))
131 (move-marker (oref iface parser-location) point))
134 (defmethod semantic-debug-set-source-location ((iface semantic-debug-interface) point)
135 "Set the source location in IFACE to POINT."
136 (with-current-buffer (oref iface source-buffer)
137 (if (not (slot-boundp iface 'source-location))
138 (oset iface source-location (make-marker)))
139 (move-marker (oref iface source-location) point))
142 (defmethod semantic-debug-interface-layout ((iface semantic-debug-interface))
143 "Layout windows in the current frame to facilitate debugging."
144 (delete-other-windows)
145 ;; Deal with the data buffer
146 (when (slot-boundp iface 'data-buffer)
147 (let ((lines (/ (frame-height (selected-frame)) 3))
148 (cnt (with-current-buffer (oref iface data-buffer)
149 (count-lines (point-min) (point-max))))
151 ;; Set the number of lines to 1/3, or the size of the data buffer.
152 (if (< cnt lines) (setq cnt lines))
154 (split-window-vertically cnt)
155 (switch-to-buffer (oref iface data-buffer))
157 (other-window 1))
158 ;; Parser
159 (switch-to-buffer (oref iface parser-buffer))
160 (when (slot-boundp iface 'parser-location)
161 (goto-char (oref iface parser-location)))
162 (split-window-vertically)
163 (other-window 1)
164 ;; Source
165 (switch-to-buffer (oref iface source-buffer))
166 (when (slot-boundp iface 'source-location)
167 (goto-char (oref iface source-location)))
170 (defmethod semantic-debug-highlight-lexical-token ((iface semantic-debug-interface) token)
171 "For IFACE, highlight TOKEN in the source buffer .
172 TOKEN is a lexical token."
173 (set-buffer (oref iface :source-buffer))
175 (object-add-to-list iface 'overlays
176 (semantic-lex-highlight-token token))
178 (semantic-debug-set-source-location iface (semantic-lex-token-start token))
181 (defmethod semantic-debug-highlight-rule ((iface semantic-debug-interface) nonterm &optional rule match)
182 "For IFACE, highlight NONTERM in the parser buffer.
183 NONTERM is the name of the rule currently being processed that shows up
184 as a nonterminal (or tag) in the source buffer.
185 If RULE and MATCH indices are specified, highlight those also."
186 (set-buffer (oref iface :parser-buffer))
188 (let* ((rules (semantic-find-tags-by-class 'nonterminal (current-buffer)))
189 (nt (semantic-find-first-tag-by-name nonterm rules))
190 (o nil)
192 (when nt
193 ;; I know it is the first symbol appearing in the body of this token.
194 (goto-char (semantic-tag-start nt))
196 (setq o (semantic-make-overlay (point) (progn (forward-sexp 1) (point))))
197 (semantic-overlay-put o 'face 'highlight)
199 (object-add-to-list iface 'overlays o)
201 (semantic-debug-set-parser-location iface (semantic-overlay-start o))
203 (when (and rule match)
205 ;; Rule, an int, is the rule inside the nonterminal we are following.
206 (re-search-forward ":\\s-*")
207 (while (/= 0 rule)
208 (re-search-forward "^\\s-*|\\s-*")
209 (setq rule (1- rule)))
211 ;; Now find the match inside the rule
212 (while (/= 0 match)
213 (forward-sexp 1)
214 (skip-chars-forward " \t")
215 (setq match (1- match)))
217 ;; Now highlight the thingy we find there.
218 (setq o (semantic-make-overlay (point) (progn (forward-sexp 1) (point))))
219 (semantic-overlay-put o 'face 'highlight)
221 (object-add-to-list iface 'overlays o)
223 ;; If we have a match for a sub-rule, have the parser position
224 ;; move so we can see it in the output window for very long rules.
225 (semantic-debug-set-parser-location iface (semantic-overlay-start o))
227 ))))
229 (defmethod semantic-debug-unhighlight ((iface semantic-debug-interface))
230 "Remove all debugging overlays."
231 (mapc 'semantic-overlay-delete (oref iface overlays))
232 (oset iface overlays nil))
234 ;; Call from the parser at a breakpoint
235 (defvar semantic-debug-user-command nil
236 "The command the user is requesting.")
238 (defun semantic-debug-break (frame)
239 "Stop parsing now at FRAME.
240 FRAME is an object that represents the parser's view of the
241 current state of the world.
242 This function enters a recursive edit. It returns
243 on an `exit-recursive-edit', or if someone uses one
244 of the `semantic-debug-mode' commands.
245 It returns the command specified. Parsers need to take action
246 on different types of return values."
247 (save-window-excursion
248 ;; Set up displaying information
249 (semantic-debug-mode t)
250 (unwind-protect
251 (progn
252 (semantic-debug-frame-highlight frame)
253 (semantic-debug-interface-layout semantic-debug-current-interface)
254 (condition-case nil
255 ;; Enter recursive edit... wait for user command.
256 (recursive-edit)
257 (error nil)))
258 (semantic-debug-unhighlight semantic-debug-current-interface)
259 (semantic-debug-mode nil))
260 ;; Find the requested user state. Do something.
261 (let ((returnstate semantic-debug-user-command))
262 (setq semantic-debug-user-command nil)
263 returnstate)
266 ;;; Frame
268 ;; A frame can represent the state at a break point.
269 (defclass semantic-debug-frame ()
272 "One frame representation.")
274 (defmethod semantic-debug-frame-highlight ((frame semantic-debug-frame))
275 "Highlight one parser frame."
279 (defmethod semantic-debug-frame-info ((frame semantic-debug-frame))
280 "Display info about this one parser frame."
284 ;;; Major Mode
286 (defvar semantic-debug-mode-map
287 (let ((km (make-sparse-keymap)))
288 (define-key km "n" 'semantic-debug-next)
289 (define-key km " " 'semantic-debug-next)
290 (define-key km "s" 'semantic-debug-step)
291 (define-key km "u" 'semantic-debug-up)
292 (define-key km "d" 'semantic-debug-down)
293 (define-key km "f" 'semantic-debug-fail-match)
294 (define-key km "h" 'semantic-debug-print-state)
295 (define-key km "s" 'semantic-debug-jump-to-source)
296 (define-key km "p" 'semantic-debug-jump-to-parser)
297 (define-key km "q" 'semantic-debug-quit)
298 (define-key km "a" 'semantic-debug-abort)
299 (define-key km "g" 'semantic-debug-go)
300 (define-key km "b" 'semantic-debug-set-breakpoint)
301 ;; Some boring bindings.
302 (define-key km "e" 'eval-expression)
305 "Keymap used when in semantic-debug-node.")
307 (defun semantic-debug-mode (onoff)
308 "Turn `semantic-debug-mode' on and off.
309 Argument ONOFF is non-nil when we are entering debug mode.
310 \\{semantic-debug-mode-map}"
311 (let ((iface semantic-debug-current-interface))
312 (if onoff
313 ;; Turn it on
314 (with-current-buffer (oref iface parser-buffer)
315 ;; Install our map onto this buffer
316 (use-local-map semantic-debug-mode-map)
317 ;; Make the buffer read only
318 (setq buffer-read-only t)
320 (set-buffer (oref iface source-buffer))
321 ;; Use our map in the source buffer also
322 (use-local-map semantic-debug-mode-map)
323 ;; Make the buffer read only
324 (setq buffer-read-only t)
325 ;; Hooks
326 (run-hooks 'semantic-debug-mode-hook)
328 ;; Restore old mode information
329 (with-current-buffer
330 (oref semantic-debug-current-interface parser-buffer)
331 (use-local-map
332 (oref semantic-debug-current-interface parser-local-map))
333 (setq buffer-read-only nil)
335 (with-current-buffer
336 (oref semantic-debug-current-interface source-buffer)
337 (use-local-map
338 (oref semantic-debug-current-interface source-local-map))
339 (setq buffer-read-only nil)
341 (run-hooks 'semantic-debug-exit-hook)
344 ;;;###autoload
345 (defun semantic-debug ()
346 "Parse the current buffer and run in debug mode."
347 (interactive)
348 (if semantic-debug-current-interface
349 (error "You are already in a debug session"))
350 (if (not semantic-debug-parser-class)
351 (error "This major mode does not support parser debugging"))
352 ;; Clear the cache to force a full reparse.
353 (semantic-clear-toplevel-cache)
354 ;; Load in the debugger for this file.
355 (when semantic-debug-parser-debugger-source
356 (require semantic-debug-parser-debugger-source))
357 ;; Do the parse
358 (let ((semantic-debug-enabled t)
359 ;; Create an interface
360 (semantic-debug-current-interface
361 (let ((parserb (semantic-debug-find-parser-source)))
362 (semantic-debug-interface
363 "Debug Interface"
364 :parser-buffer parserb
365 :parser-local-map (with-current-buffer parserb
366 (current-local-map))
367 :source-buffer (current-buffer)
368 :source-local-map (current-local-map)
370 ;; Create a parser debug interface
371 (semantic-debug-current-parser
372 (funcall semantic-debug-parser-class "parser"))
374 ;; We could recurse into a parser while debugging.
375 ;; Is that a problem?
376 (semantic-fetch-tags)
377 ;; We should turn the auto-parser back on, but don't do it for
378 ;; now until the debugger is working well.
381 (defun semantic-debug-find-parser-source ()
382 "Return a buffer containing the parser source file for the current buffer.
383 The parser needs to be on the load path, or this routine returns nil."
384 (if (not semantic-debug-parser-source)
385 (error "No parser is associated with this buffer"))
386 (let ((parser (locate-library semantic-debug-parser-source t)))
387 (if parser
388 (find-file-noselect parser)
389 (error "Cannot find parser source. It should be on the load-path"))))
391 ;;; Debugger commands
393 (defun semantic-debug-next ()
394 "Perform one parser operation.
395 In the recursive parser, this steps past one match rule.
396 In other parsers, this may be just like `semantic-debug-step'."
397 (interactive)
398 (let ((parser semantic-debug-current-parser))
399 (semantic-debug-parser-next parser)
400 (exit-recursive-edit)
404 (defun semantic-debug-step ()
405 "Perform one parser operation."
406 (interactive)
407 (let ((parser semantic-debug-current-parser))
408 (semantic-debug-parser-step parser)
409 (exit-recursive-edit)
413 (defun semantic-debug-up ()
414 "Move highlighting representation up one level."
415 (interactive)
416 (message "Not implemented yet.")
419 (defun semantic-debug-down ()
420 "Move highlighting representation down one level."
421 (interactive)
422 (message "Not implemented yet.")
425 (defun semantic-debug-fail-match ()
426 "Artificially fail the current match."
427 (interactive)
428 (let ((parser semantic-debug-current-parser))
429 (semantic-debug-parser-fail parser)
430 (exit-recursive-edit)
434 (defun semantic-debug-print-state ()
435 "Show interesting parser state."
436 (interactive)
437 (let ((parser semantic-debug-current-parser))
438 (semantic-debug-parser-print-state parser)
442 (defun semantic-debug-jump-to-source ()
443 "Move cursor to the source code being parsed at the current lexical token."
444 (interactive)
445 (let* ((interface semantic-debug-current-interface)
446 (buf (oref interface source-buffer)))
447 (if (get-buffer-window buf)
448 (progn
449 (select-frame (window-frame (get-buffer-window buf)))
450 (select-window (get-buffer-window buf)))
451 ;; Technically, this should do a window layout operation
452 (switch-to-buffer buf))
456 (defun semantic-debug-jump-to-parser ()
457 "Move cursor to the parser being debugged."
458 (interactive)
459 (let* ((interface semantic-debug-current-interface)
460 (buf (oref interface parser-buffer)))
461 (if (get-buffer-window buf)
462 (progn
463 (select-frame (window-frame (get-buffer-window buf)))
464 (select-window (get-buffer-window buf)))
465 ;; Technically, this should do a window layout operation
466 (switch-to-buffer buf))
470 (defun semantic-debug-quit ()
471 "Exit debug mode, blowing all stack, and leaving the parse incomplete.
472 Do not update any tokens already parsed."
473 (interactive)
474 (let ((parser semantic-debug-current-parser))
475 (semantic-debug-parser-quit parser)
476 (exit-recursive-edit)
480 (defun semantic-debug-abort ()
481 "Abort one level of debug mode, blowing all stack."
482 (interactive)
483 (let ((parser semantic-debug-current-parser))
484 (semantic-debug-parser-abort parser)
485 (exit-recursive-edit)
489 (defun semantic-debug-go ()
490 "Continue parsing till finish or breakpoint."
491 (interactive)
492 (let ((parser semantic-debug-current-parser))
493 (semantic-debug-parser-go parser)
494 (exit-recursive-edit)
498 (defun semantic-debug-set-breakpoint ()
499 "Set a breakpoint at the current rule location."
500 (interactive)
501 (let ((parser semantic-debug-current-parser)
502 ;; Get the location as semantic tokens.
503 (location (semantic-current-tag))
505 (if location
506 (semantic-debug-parser-break parser location)
507 (error "Not on a rule"))
512 ;;; Debugger superclass
514 (defclass semantic-debug-parser ()
517 "Represents a parser and its state.
518 When implementing the debug parser you can add extra functionality
519 by overriding one of the command methods. Be sure to use
520 `call-next-method' so that the debug command is saved, and passed
521 down to your parser later."
522 :abstract t)
524 (defmethod semantic-debug-parser-next ((parser semantic-debug-parser))
525 "Execute next for this PARSER."
526 (setq semantic-debug-user-command 'next)
529 (defmethod semantic-debug-parser-step ((parser semantic-debug-parser))
530 "Execute a step for this PARSER."
531 (setq semantic-debug-user-command 'step)
534 (defmethod semantic-debug-parser-go ((parser semantic-debug-parser))
535 "Continue execution in this PARSER until the next breakpoint."
536 (setq semantic-debug-user-command 'go)
539 (defmethod semantic-debug-parser-fail ((parser semantic-debug-parser))
540 "Continue execution in this PARSER until the next breakpoint."
541 (setq semantic-debug-user-command 'fail)
544 (defmethod semantic-debug-parser-quit ((parser semantic-debug-parser))
545 "Continue execution in this PARSER until the next breakpoint."
546 (setq semantic-debug-user-command 'quit)
549 (defmethod semantic-debug-parser-abort ((parser semantic-debug-parser))
550 "Continue execution in this PARSER until the next breakpoint."
551 (setq semantic-debug-user-command 'abort)
554 (defmethod semantic-debug-parser-print-state ((parser semantic-debug-parser))
555 "Print state for this PARSER at the current breakpoint."
556 (with-slots (current-frame) semantic-debug-current-interface
557 (when current-frame
558 (semantic-debug-frame-info current-frame)
561 (defmethod semantic-debug-parser-break ((parser semantic-debug-parser))
562 "Set a breakpoint for this PARSER."
565 ;; Stack stuff
566 (defmethod semantic-debug-parser-frames ((parser semantic-debug-parser))
567 "Return a list of frames for the current parser.
568 A frame is of the form:
569 ( .. .what ? .. )
571 (error "Parser has not implemented frame values")
575 (provide 'semantic/debug)
577 ;; Local variables:
578 ;; generated-autoload-file: "loaddefs.el"
579 ;; generated-autoload-load-name: "semantic/debug"
580 ;; End:
582 ;;; semantic/debug.el ends here