Skip to content

Commit 4446ba9

Browse files
committed
Initial test for the re-factored forward search token function
1 parent 62ef3b3 commit 4446ba9

File tree

3 files changed

+67
-23
lines changed

3 files changed

+67
-23
lines changed

sysver-bnf.el

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
(defvar SYNTOK-PARAMS-START-LIST "SYNTOK-PARAMS-START-LIST")
4040
(defvar SYNTOK-ASSIGN-SEMICOLON-CLOSER "SYNTOK-ASSIGN-SEMICOLON-CLOSER")
4141
(defvar SYNTOK-SPACING "SYNTOK-SPACING")
42+
(defvar SYNTOK-STRING "SYNTOK-STRING")
4243

4344
;; token search functions
4445

@@ -54,25 +55,57 @@
5455
;; modify the default forward token function such that it returns a token even for
5556
;; parenthesis characters "(" ")"
5657
(progn
57-
(let ((start-pnt (point)))
58-
;; return the whole string as a token
59-
;; when inside a comment the `forward-comment' returns always `nil', moreover the strings
60-
;; shall be given or a string the
61-
(forward-comment (point-max))
62-
63-
(if (> (point) start-pnt)
64-
;; we just skipped a space-comments group, hence the general inter-token is returned, the
65-
;; caller of this function will refine the search
66-
SYNTOK-SPACING
67-
(buffer-substring-no-properties
68-
(point)
69-
(cond
70-
((and (zerop (skip-syntax-forward "."))
71-
(zerop (skip-syntax-forward "("))
72-
(zerop (skip-syntax-forward ")")))))
73-
(progn (if
74-
(skip-syntax-forward "w_'"))
75-
(point)))))))
58+
(let* ((start-pnt (point))
59+
(syntax-state (syntax-ppss start-pnt))
60+
;; do not try to get the state of the incremented point when at the end of the buffer
61+
(syntax-state-next (if (not (eobp))
62+
(save-excursion
63+
(syntax-ppss (1+ start-pnt)))
64+
(syntax-class)))) ; it is expected a list so recycle the previous one
65+
66+
(cond
67+
;; the token search must stop when reaching the end of the buffer
68+
((eobp) nil)
69+
70+
;; when inside a comment the `forward-comment' returns always `nil', hence use the syntax
71+
;; state to skip it
72+
((nth 4 syntax-state)
73+
(goto-char (nth 8 syntax-state)) ; to the comment start to easily skip it
74+
;; move forward skipping comments and with characters
75+
(forward-comment (point-max))
76+
SYNTOK-SPACING)
77+
78+
;; FIXME: the string case fails to leave a string context when the current string contains
79+
;; the escaped apexes.
80+
81+
;; When inside of a string or just before a string opener character, a string-token shall be
82+
;; returned.
83+
((or (nth 3 syntax-state) (nth 3 syntax-state-next))
84+
;; skip all the characters inside the string (^ negate the class to be skipped)
85+
(skip-syntax-forward "\"") ; move inside the string when placed just before its opener
86+
(skip-syntax-forward "^\"") ; reach the string closer
87+
(skip-syntax-forward "\"") ; move after the string closer
88+
SYNTOK-STRING)
89+
90+
;; as not inside a comment or string search the next token based on the syntax class (after
91+
;; skipping comments and white spaces)
92+
(t
93+
(forward-comment (point-max))
94+
95+
(if (> (point) start-pnt)
96+
;; we just skipped a space-comments group, hence the general inter-token is returned, the
97+
;; caller of this function will refine the search
98+
SYNTOK-SPACING
99+
(buffer-substring-no-properties
100+
(point)
101+
(cond
102+
((and (zerop (skip-syntax-forward "."))
103+
(zerop (skip-syntax-forward "("))
104+
(zerop (skip-syntax-forward ")")))
105+
(skip-syntax-forward "w_'")
106+
(point))
107+
(t (point)))
108+
)))))))
76109
(defun sysver-basic-backward-token ()
77110
"Default backward search token function based on the syntax classes."
78111

test/sysver-common.el

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
;;; sysver-test-common.el --- general definitions for various UTCs (Unit-Test-Case)
22

3+
;; NOTE: could not find a way to step-through a macro, there is a way to let edebug how to instrument your macro
4+
;; via a debug-declaration, but it's too complex.
5+
;; The easy work around is to expand the macro by placing the cursor right before the macro-call and execute the
6+
;; command: emacs-lisp-macroexpand
7+
;; After the debug is over, the buffer should be reverted to collapse the macro again.
38
(defmacro sysver-utc-environment (test-string setup-to-test test-body)
49
"Freshly reload the `sysver' major mode and setup the environment for an unit-test-case (UTC).
510
611
The TEST-STRING is a string to be inserted into the current buffer.
712
The SETUP-TO-TEST should be an unquoted list of statements to setup the feature under test, while
813
the TEST-BODY is an unquoted list of statements to verify the tested feature."
14+
(declare (debug (form sexp sexp)))
915

1016
`(progn
1117
;; reload sysver

test/sysver-token-test.el

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
(require 'ert)
44
(require 'sysver)
55
(require 'sysver-common)
6+
(require 'cl-lib)
67

78
(ert-deftest sysver-test-default-token-search ()
89
"Test the default token search behavior"
@@ -18,12 +19,16 @@
1819
"// a commment to test the synthetic-token generation"))
1920

2021
;; list of all expected tokens: the REAL-TOKENS list interleaved with SYNTOK-SPACING tokens
21-
(exp-tokens (let (full-tok-list '())
22+
(exp-tokens (let ((full-tok-list '()))
23+
;; interleave the expected tokens list with SYNTOK-SPACING
2224
(dolist (token real-tokens full-tok-list)
2325
(setq full-tok-list (append full-tok-list `(,token ,SYNTOK-SPACING))))
24-
;; Refine the output list as the last comment shall be matched together with the previous
25-
;; space character (hence the obtained list less the last tokens).
26-
(setq full-tok-list (butlast full-tok-list 2))))
26+
;; Refine the output list:
27+
;; - the string shall be returned as a SYNTOK-STRING token
28+
;; - the last comment shall be part of the previous space, hence remove the last 2 tokens
29+
(setq full-tok-list (append (cl-subseq full-tok-list 0 4)
30+
`(,SYNTOK-STRING)
31+
(cl-subseq full-tok-list 5 (- (length full-tok-list) 2))))))
2732

2833
;; turn the REAL-TOKENS list into a space-interleaved-list
2934
(current-string (mapconcat 'identity real-tokens " "))

0 commit comments

Comments
 (0)