branch: elpa/gnuplot
commit 049f953c6ff922179979b8466ea1d5fd1ba51869
Author: Jonathan Oddie <[email protected]>
Commit: Jonathan Oddie <[email protected]>
Use absolute locations in all jump/call/choice/commit instructions.
---
gnuplot-context.el | 71 ++++++++++++++++++++++++++----------------------
gnuplot-debug-context.el | 5 ++++
gnuplot-test-context.el | 26 ++++++------------
3 files changed, 52 insertions(+), 50 deletions(-)
diff --git a/gnuplot-context.el b/gnuplot-context.el
index e40d72a..69b431f 100644
--- a/gnuplot-context.el
+++ b/gnuplot-context.el
@@ -411,9 +411,10 @@ These have to be compiled from the Gnuplot source tree
using
(eval-when-compile
- ;; Compile a single pattern into a list of instructions. Leaves any
- ;; calls to other rules as symbolic instructions (call SYMBOL); these
- ;; are resolved into offsets by `gnuplot-compile-grammar', below.
+ ;; Compile a single pattern into a list of instructions. Leaves
+ ;; calls to other rules as symbolic instructions (call SYMBOL) and
+ ;; jumps, commits etc. as relative offsets; these are resolved into
+ ;; absolute locations by `gnuplot-compile-grammar', below.
(defun gnuplot-compile-pattern (pat)
(cond
;; Strings match a single token literally
@@ -620,7 +621,7 @@ These have to be compiled from the Gnuplot source tree using
(setf (aref object-code i) '(return)
i (1+ i))))
- ;; Resolve symbolic jumps
+ ;; Resolve symbolic and relative jumps
(let ((pattern-name nil))
(dotimes (i (length object-code))
(let ((inst (aref object-code i)))
@@ -628,16 +629,25 @@ These have to be compiled from the Gnuplot source tree
using
((label)
(setq pattern-name (cadr inst)))
- ((jump call)
- (if (symbolp (cadr inst))
- (let* ((name (cadr inst))
- (location (gethash name name->offset)))
- (if (not location)
- (error
- (concat "gnuplot-compile-grammar: "
- "No rule found for symbol `%s' in pattern
`%s'")
- name pattern-name))
- (setcdr inst `(,location t ,name)))))))))
+ ((jump call choice commit)
+ (cond
+ ((symbolp (cadr inst))
+ (let* ((name (cadr inst))
+ (location (gethash name name->offset)))
+ (if (not location)
+ (error
+ (concat "gnuplot-compile-grammar: "
+ "No rule found for symbol `%s' in pattern
`%s'")
+ name pattern-name))
+ (setcdr inst `(,location ,name))))
+
+ ((numberp (cadr inst))
+ (let* ((offset (cadr inst))
+ (location (+ offset i)))
+ (setcdr inst `(,location))))
+
+ (t
+ (error "gnuplot-compile-grammar: bad instruction %s"
inst))))))))
object-code))))
;;; The grammar.
@@ -1657,7 +1667,7 @@ Set by `gnuplot-match-pattern'. See also
`gnuplot-info-at-point'.")
Each entry is of the form (NAME BEGIN END), where NAME is the
name specified in the (capture NAME PATTERN) form in the
-`gnuplop-compiled-grammar' source, BEGIN is the tail of the token
+`gnuplot-compiled-grammar' source, BEGIN is the tail of the token
list beginning the capture group, and END is the tail of the
token list just after the end of the capture group.")
@@ -1770,20 +1780,17 @@ there."
(fail)
(advance)))
- ;; (jump OFFSET FIXED): jump to instruction at PC + OFFSET,
- ;; or directly to OFFSET if FIXED is non-nil
+ ;; (jump LOCATION): jump to instruction at LOCATION
((jump)
- (let ((offset (cadr inst))
- (fixed (caddr inst)))
- (setq jump (if fixed offset (+ pc offset)))))
+ (let ((location (cadr inst)))
+ (setq jump location)))
- ;; (call OFFSET FIXED): push the next instruction as a
- ;; return location and jump like (jump), above
+ ;; (call LOCATION): push the next instruction as a
+ ;; return location and jump
((call)
- (let ((offset (cadr inst))
- (fixed (caddr inst)))
+ (let ((location (cadr inst)))
(push `(return ,(+ pc 1)) stack)
- (setq jump (if fixed offset (+ pc offset)))))
+ (setq jump location)))
;; (return): return to address at topmost RETURN record on
;; stack, or stop matching and return if stack is empty
@@ -1799,22 +1806,22 @@ there."
(r-pc (cadr r)))
(setq jump r-pc))))
- ;; (choice OFFSET): push PC + OFFSET onto the stack of
+ ;; (choice LOCATION): push LOCATION onto the stack of
;; backtracking points and continue at next instruction
((choice)
- (let ((offset (cadr inst)))
- (push `(,stack ,tokens ,(+ pc offset) ,gnuplot-captures
+ (let ((location (cadr inst)))
+ (push `(,stack ,tokens ,location ,gnuplot-captures
,progress)
backtrack)))
- ;; (commit OFFSET): discard most recent backtrack point
- ;; and jump to PC + OFFSET
+ ;; (commit LOCATION): discard most recent backtrack point
+ ;; and jump to LOCATION
((commit)
- (let ((offset (cadr inst)))
+ (let ((location (cadr inst)))
(if (not backtrack)
(error "no more backtrack points in commit"))
(pop backtrack)
- (setq jump (+ pc offset))))
+ (setq jump location)))
;; (fail): force this match to fail, going back to most
;; recent backtrack point
diff --git a/gnuplot-debug-context.el b/gnuplot-debug-context.el
index 780b3b7..4f4dda1 100644
--- a/gnuplot-debug-context.el
+++ b/gnuplot-debug-context.el
@@ -1,3 +1,8 @@
+;;
+;; debugging utilities for the gnuplot-mode context matcher
+;;
+
+(require 'gnuplot-test-context) ; for gnuplot-simplify-tokens
(defun gnuplot-unload ()
(interactive)
diff --git a/gnuplot-test-context.el b/gnuplot-test-context.el
index df4e83f..58c6723 100644
--- a/gnuplot-test-context.el
+++ b/gnuplot-test-context.el
@@ -41,10 +41,10 @@
(intern (gnuplot-token-id token)))))
tokens))
-;; compile a single pattern to vector form
+;; compile a single pattern to usable form
(eval-when-compile
(defun gnuplot-compile-pattern-1 (pattern)
- (vconcat (gnuplot-compile-pattern pattern) [(return)])))
+ (gnuplot-compile-grammar `((rule ,pattern)) 'rule)))
;; match a string
(defun gnuplot-match-string (string rule)
@@ -194,10 +194,6 @@
("," :none)
("]" :none)))
-(deftest gnuplot-parenthesized-expression ()
- (should-match parenthesized-expression
- ("(sum = sum + $2, sum/2)")))
-
(deftest gnuplot-function-call ()
(should-match function-call
"abs(2)"
@@ -232,6 +228,11 @@
("x=y=3, garbage" '(\, garbage))
("f(a) = y(x) = 5")))
+;; parenthesized exprs (including assignments)
+(deftest gnuplot-parenthesized-expression ()
+ (should-match parenthesized-expression
+ ("(sum = sum + $2, sum/2)")))
+
;; axis ranges
(deftest gnuplot-axis-range ()
(should-match axis-range
@@ -299,25 +300,15 @@
(deftest gnuplot-plot-command ()
(should-match plot-command
("plot sin(x) with impulses")
-
("plot x w points, x**2")
-
("plot [ ] [-2:5] tan(x), 'data.1' with l")
-
("plot 'leastsq.dat' w i")
-
("plot 'exper.dat' w lines, 'exper.dat' notitle w errorbars")
-
("plot sin(x) with linesp lt 1 pt 3, cos(x) with linesp lt 1 pt 4")
-
("plot 'data' with points pointtype 3 pointsize 2")
-
("plot 'data' using 1:2:4 with points pt 5 pointsize variable")
-
("plot 'd1' t \"good\" w l lt 2 lw 3, 'd2' t \"bad\" w l lt 2 lw 1")
-
("plot x*x with filledcurve closed, 40 with filledcurve y1=10")
-
("plot x*x, (x>=-5 && x<=5 ? 40 : 1/0) with filledcurve y1=10 lt 8")))
;;; set cntrparam
@@ -332,8 +323,7 @@
("cntrparam levels 10")
("cntrparam levels incremental 100,50")))
-
-
+
;;
;; test by parsing all the demos
;;