Skip to content

Commit 8f0dc77

Browse files
committed
Correct the basic token search when around a comment
1 parent 4446ba9 commit 8f0dc77

File tree

2 files changed

+91
-18
lines changed

2 files changed

+91
-18
lines changed

sysver-bnf.el

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
(syntax-state-next (if (not (eobp))
6262
(save-excursion
6363
(syntax-ppss (1+ start-pnt)))
64-
(syntax-class)))) ; it is expected a list so recycle the previous one
64+
syntax-state))) ; it is expected a list so recycle the previous one
6565

6666
(cond
6767
;; the token search must stop when reaching the end of the buffer
@@ -71,7 +71,7 @@
7171
;; state to skip it
7272
((nth 4 syntax-state)
7373
(goto-char (nth 8 syntax-state)) ; to the comment start to easily skip it
74-
;; move forward skipping comments and with characters
74+
;; move forward skipping comments and space characters
7575
(forward-comment (point-max))
7676
SYNTOK-SPACING)
7777

@@ -93,8 +93,8 @@
9393
(forward-comment (point-max))
9494

9595
(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
96+
;; we just skipped a space-comments group, hence the spacing-token is returned, the caller of this
97+
;; function will refine the search
9898
SYNTOK-SPACING
9999
(buffer-substring-no-properties
100100
(point)
@@ -104,25 +104,58 @@
104104
(zerop (skip-syntax-forward ")")))
105105
(skip-syntax-forward "w_'")
106106
(point))
107-
(t (point)))
108-
)))))))
107+
(t (point))))))))))
109108
(defun sysver-basic-backward-token ()
110109
"Default backward search token function based on the syntax classes."
111110

112111
;; NOTE: all explanations and comments are the same as for the `sysver-basic-forward-token'
113112
(progn
114-
(let ((start-pnt (point)))
115-
(forward-comment (- (point)))
116-
117-
(if (< (point) start-pnt)
118-
SYNTOK-SPACING
119-
(buffer-substring-no-properties
120-
(point)
121-
(progn (if (and (zerop (skip-syntax-backward "."))
122-
(zerop (skip-syntax-backward "("))
123-
(zerop (skip-syntax-backward ")")))
124-
(skip-syntax-backward "w_'"))
125-
(point)))))))
113+
(let* ((start-pnt (point))
114+
(syntax-state (syntax-ppss start-pnt))
115+
(syntax-state-prev (if (not (bobp))
116+
(save-excursion
117+
(syntax-ppss (1- start-pnt)))
118+
syntax-state)))
119+
120+
(cond
121+
;; the token search must stop when reaching the start of the buffer
122+
((bobp) nil)
123+
124+
;; inside a comment
125+
((nth 4 syntax-state)
126+
;; move to the comment beginning as "forward-comment" does not work from inside it
127+
(goto-char (nth 8 syntax-state))
128+
;; move backward skipping comments and space characters
129+
(forward-comment (- (point)))
130+
SYNTOK-SPACING)
131+
132+
;; FIXME: the string case fails to leave a string context when the current string contains
133+
;; the escaped apexes.
134+
135+
;; When inside of a string or just before a string opener character, a string-token shall be
136+
;; returned.
137+
((or (nth 3 syntax-state) (nth 3 syntax-state-prev))
138+
;; skip all the characters inside the string (^ negate the class to be skipped)
139+
(skip-syntax-backward "\"") ; move inside the string when placed just before its opener
140+
(skip-syntax-backward "^\"") ; reach the string closer
141+
(skip-syntax-backward "\"") ; move after the string closer
142+
SYNTOK-STRING)
143+
144+
;; elsewhere
145+
(t
146+
(forward-comment (- (point)))
147+
148+
(if (< (point) start-pnt)
149+
SYNTOK-SPACING
150+
(buffer-substring-no-properties
151+
(point)
152+
(cond
153+
((and (zerop (skip-syntax-backward "."))
154+
(zerop (skip-syntax-backward "("))
155+
(zerop (skip-syntax-backward ")")))
156+
(skip-syntax-backward "w_'")
157+
(point))
158+
(t (point))))))))))
126159

127160
;; Through the token search, the indentation can overcome the BNF grammar limits and
128161
;; return "synthetic" tokens to ease the grammar definition.

test/sysver-token-test.el

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,43 @@
6868
(dolist (exp-tok exp-tokens-reverse)
6969
(message "token: %s" exp-tok))
7070
(should (equal (sysver-basic-backward-token) (pop exp-tokens-reverse)))))))))
71+
72+
(ert-deftest sysver-test-gen-syntok-space ()
73+
"Test the behavior of the basic token search functions, when:
74+
75+
- right before a comment delimiter
76+
- in the middle of a comment"
77+
78+
(let ((tok-search-functions '(sysver-basic-forward-token
79+
sysver-basic-backward-token))
80+
(buffer-extremes-list '(eobp bobp))
81+
search-func
82+
buf-limit-p)
83+
84+
(while tok-search-functions
85+
86+
;; pop test symbols
87+
(fset 'search-func (pop tok-search-functions))
88+
(fset 'buf-limit-p (pop buffer-extremes-list))
89+
90+
(with-temp-buffer
91+
(sysver-utc-environment
92+
;; test string
93+
" // a long comment with fancy ch@ract3rs "
94+
95+
;; no parameters to be tested
96+
()
97+
98+
;; verify steps
99+
(
100+
;; before a comment
101+
(info-msg "testing the `%s' function before a comment delimiter" (symbol-function 'search-func))
102+
(skip-syntax-forward " ")
103+
(should (equal (search-func) SYNTOK-SPACING))
104+
(should (buf-limit-p))
105+
106+
;; in the middle of a comment
107+
(info-msg "testing the `%s' function in the middle of a comment" (symbol-function 'search-func))
108+
(goto-char (/ (buffer-size) 2))
109+
(should (equal (search-func) SYNTOK-SPACING))
110+
(should (buf-limit-p))))))))

0 commit comments

Comments
 (0)