File: gnuplot-debug-context.el

package info (click to toggle)
gnuplot-mode 1%3A0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,520 kB
  • sloc: lisp: 5,689; makefile: 26
file content (115 lines) | stat: -rw-r--r-- 3,771 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
;; debugging utilities for the gnuplot-mode context matcher -*- lexical-binding: t -*-

(require 'gnuplot-test-context) ; for gnuplot-simplify-tokens

(defun gnuplot-unload ()
  (interactive)
  (mapatoms
   (lambda (sym)
     (when (string-match
            "gnuplot"
            (symbol-name sym))
       (unintern sym obarray)))))

(defun gnuplot-reload (&optional context)
  (interactive "p")
  (condition-case nil
      (gnuplot-unload)
    (error nil))
  (require 'gnuplot)
  (when context
    (if (= context 16)
        (require 'gnuplot-debug-context))
    (require 'gnuplot-context)))

(defsubst gnuplot-recompile ()
  (save-current-buffer
    (save-window-excursion
      (find-file "gnuplot-context.el")
      (delete-file "gnuplot-context.elc")
      (emacs-lisp-byte-compile)
      (load-file "gnuplot-context.elc"))))

(defun gnuplot-nodebug ()
  (interactive)
  (when (featurep 'gnuplot-debug-context)
    (let ((savef (symbol-function 'gnuplot-debug-on)))
      (unload-feature 'gnuplot-debug-context)
      (fset 'gnuplot-debug-on savef)))
  (gnuplot-recompile))

(defun gnuplot-debug-on ()
  (interactive)
  (unless (featurep 'gnuplot-debug-context)
    (load-library "gnuplot-debug-context"))
  (gnuplot-recompile))

(defmacro gnuplot-context--with-trace-buffer (&rest body)
  `(with-current-buffer (get-buffer-create "gnuplot-context-trace")
     ,@body))

(defmacro gnuplot-context--debug (&rest args)
  `(progn ,@args))

(defmacro gnuplot-context--trace (&rest args)
  `(gnuplot-context--with-trace-buffer (insert (format ,@args))))

(defun gnuplot-backtrace (stack)
  (if stack
      (gnuplot-context--with-trace-buffer
       (insert "\n-- * backtrace: * --\n")
       (dolist (x stack)
         (insert (format "%s\n"
                         (if (eq (car x) 'return)
                             x
                           (list (car x) (cadr x)
                                 (gnuplot-simplify-tokens (cl-caddr x)))))))
       (insert "-- end backtrace  --\n"))))

(defun gnuplot-dump-backtrack (backtrack)
  (if backtrack
      (gnuplot-context--with-trace-buffer
       (insert "\n-- * backtrack records: * --\n")
       (dolist (x backtrack)
         (insert (format "%s\t%s\n" (cl-caddr x) (gnuplot-simplify-tokens (cadr x)))))
       (insert "-- end backtrack records  --\n\n"))))

(defun gnuplot-dump-progress (progress)
  (if progress
      (gnuplot-context--with-trace-buffer
       (insert "\n-- * progress records: * --\n")
       (dolist (x progress)
         (insert (format "%s\t%s\n" (car x) (gnuplot-simplify-tokens (cdr x)))))
       (insert "-- end progress records  --\n\n"))))

(defun gnuplot-dump-code (&optional inst)
  (interactive)
  (let ((inst (or inst gnuplot-context--compiled-grammar)))
    (gnuplot-context--with-trace-buffer
     (insert "\n-- * compiled code: * --\n")
     (dotimes (i (length inst))
       (insert (format "%s\t%s\n" i (aref inst i))))
     (insert "--  end compiled code --\n\n")
     (pop-to-buffer (current-buffer)))))

(defun gnuplot-dump-captures ()
  (interactive)
  (if gnuplot-context--captures
      (gnuplot-context--with-trace-buffer
       (insert "\n-- * capture groups: * --\n")
       (cl-loop for c on gnuplot-context--captures
                do
                (let ((name (caar c))
                      (gnuplot-context--captures c))
                  (insert (format "%s\t%s\n"
                                  name
                                  (mapconcat 'gnuplot-context--token-id
                                             (gnuplot-context--capture-group name)
                                             " ")))))
       (insert "-- end capture groups  --\n\n"))))

(provide 'gnuplot-debug-context)

(gnuplot-debug-on)

;;; gnuplot-debug-context.el ends here