unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 3b9f21cde37d1f999b96c224c23a5ae3bf2f0535 19149 bytes (raw)
name: test/src/syntax-tests.el 	 # note: path name is non-authoritative(*)

  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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
 
;;; syntax-tests.el --- tests for syntax.c functions -*- lexical-binding: t -*-

;; Copyright (C) 2017-2022 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'ert)
(require 'ert-x)
(require 'cl-lib)

(ert-deftest parse-partial-sexp-continue-over-comment-marker ()
  "Continue a parse that stopped in the middle of a comment marker."
  (with-temp-buffer
    (let ((table (make-syntax-table)))
      (modify-syntax-entry ?/ ". 124")
      (modify-syntax-entry ?* ". 23b")
      (set-syntax-table table))
    (insert "/*C*/\nX")
    (goto-char (point-min))
    (let* ((pointC (progn (search-forward "C") (1- (point))))
           (preC (1- pointC))
           (pointX (progn (search-forward "X") (1- (point))))
           (aftC (+ 2 pointC))
           (ppsC (parse-partial-sexp (point-min) pointC))
           (pps-preC (parse-partial-sexp (point-min) preC))
           (pps-aftC (parse-partial-sexp (point-min) aftC))
           (ppsX (parse-partial-sexp (point-min) pointX)))
      ;; C should be inside comment.
      (should (= (nth 0 ppsC) 0))
      (should (eq (nth 4 ppsC) t))
      (should (= (nth 8 ppsC) (- pointC 2)))
      ;; X should not be in comment or list.
      (should (= (nth 0 ppsX) 0))
      (should-not (nth 4 ppsX))
      ;; Try using OLDSTATE.
      (should (equal (parse-partial-sexp preC pointC nil nil pps-preC)
                     ppsC))
      (should (equal (parse-partial-sexp pointC aftC nil nil ppsC)
                     pps-aftC))
      (should (equal (parse-partial-sexp preC aftC nil nil pps-preC)
                     pps-aftC))
      (should (equal (parse-partial-sexp aftC pointX nil nil pps-aftC)
                     ppsX)))))

(ert-deftest syntax-class-character-test ()
  (cl-loop for char across " .w_()'\"$\\/<>@!|"
           for i from 0
           do (should (= char (syntax-class-to-char i)))
           when (string-to-syntax (string char))
           do (should (= char (syntax-class-to-char
                               (car (string-to-syntax (string char)))))))
  (should-error (syntax-class-to-char -1))
  (should-error (syntax-class-to-char 200)))

(ert-deftest parse-partial-sexp-paren-comments ()
  "Test syntax parsing with paren comment markers.
Specifically, where the first character of the comment marker is
also has open paren syntax (see Bug#24870)."
  (with-temp-buffer
    (let ((table (make-syntax-table)))
      (modify-syntax-entry ?\{  "(}1nb" table)
      (modify-syntax-entry ?\}  "){4nb" table)
      (modify-syntax-entry ?-  ". 123" table)
      (set-syntax-table table))
    (insert "{-C-}\nX")
    (goto-char (point-min))
    (let* ((pointC (progn (search-forward "C") (1- (point))))
           (pointX (progn (search-forward "X") (1- (point))))
           (ppsC (parse-partial-sexp (point-min) pointC))
           (ppsX (parse-partial-sexp (point-min) pointX)))
      ;; C should be inside nestable comment, not list.
      (should (= (nth 0 ppsC) 0))
      (should (= (nth 4 ppsC) 1))
      (should (= (nth 8 ppsC) (- pointC 2)))
      ;; X should not be in comment or list.
      (should (= (nth 0 ppsX) 0))
      (should-not (nth 4 ppsX))
      ;; Try using OLDSTATE.
      (should (equal (parse-partial-sexp pointC pointX nil nil ppsC)
                     ppsX)))))


;;; Commentary:
;; The next bit tests the handling of comments in syntax.c, in
;; particular the functions `forward-comment' and `scan-lists' and
;; `parse-partial-sexp' (in so far as they relate to comments).

;; It is intended to enhance this bit to test nested comments
;; (2020-10-01).

;; This bit uses the data file syntax-resources/syntax-comments.txt.

(defun syntax-comments-point (n forw)
  "Return the buffer offset corresponding to the \"label\" N.
N is a decimal number which appears in the data file, usually
twice, as \"labels\".  It can also be a negative number or zero.
FORW is t when we're using the label at BOL, nil for the one at EOL.

If the label N doesn't exist in the current buffer, an exception
is thrown.

When FORW is t and N positive, we return the position after the
first occurrence of label N at BOL in the data file.  With FORW
nil, we return the position before the last occurrence of the
label at EOL in the data file.

When N is negative, we return instead the position of the end of
line that the -N label is on.  When it is zero, we return POINT."
  (if (zerop n)
      (point)
    (let ((str (format "%d" (abs n))))
      (save-excursion
	(if forw
	    (progn
	      (goto-char (point-min))
	      (re-search-forward
	       (concat "^\\(" str "\\)\\([^0-9\n]\\|$\\)"))
	      (if (< n 0)
		  (progn (end-of-line) (point))
		(match-end 1)))
	  (goto-char (point-max))
	  (re-search-backward
	   (concat "\\(^\\|[^0-9]\\)\\(" str "\\)$"))
	  (if (< n 0)
	      (progn (end-of-line) (point))
	    (match-beginning 2)))))))

(defun syntax-comments-midpoint (n)
  "Return the buffer offset corresponding to the \"label\" N.
N is a positive decimal number which should appear in the buffer
exactly once.  The label need not be at the beginning or end of a
line.

The return value is the position just before the label.

If the label N doesn't exist in the current buffer, an exception
is thrown."
  (let ((str (format "%d" n)))
    (save-excursion
      (goto-char (point-min))
      (re-search-forward
       (concat "\\(^\\|[^0-9]\\)\\(" str "\\)\\([^0-9\n]\\|$\\)"))
      (match-beginning 2))))

(eval-and-compile
  (defvar syntax-comments-section))

(defmacro syntax-comments (-type- -dir- res start &optional stop)
  "Create an ERT test to test (forward-comment 1/-1).
The test uses a fixed name data file, which it visits.  It calls
entry and exit functions to set up and tear down syntax entries
for comment characters.  The test is given a name based on the
global variable `syntax-comments-section', the direction of
movement and the value of START.

-TYPE- (unquoted) is a symbol from whose name the entry and exit
function names are derived by appending \"-in\" and \"-out\".

-DIR- (unquoted) is `forward' or `backward', the direction
`forward-comment' is attempted.

RES, t or nil, is the expected result from `forward-comment'.

START and STOP are decimal numbers corresponding to labels in the
data file marking the start and expected stop positions.  See
`syntax-comments-point' for a precise specification.  If STOP is
missing or nil, the value of START is assumed for it."
  (declare (debug t))
  (let ((forw
	 (cond
	  ((eq -dir- 'forward) t)
	  ((eq -dir- 'backward) nil)
	  (t (error "Invalid -dir- argument \"%s\" to `syntax-comments'" -dir-))))
	(start-str (format "%d" (abs start)))
	(type -type-))
    `(ert-deftest ,(intern (concat "syntax-comments-"
				   syntax-comments-section
				   (if forw "-f" "-b") start-str))
	 ()
       (with-current-buffer
	   (find-file
            ,(ert-resource-file "syntax-comments.txt"))
	 (,(intern (concat (symbol-name type) "-in")))
	 (goto-char (syntax-comments-point ,start ,forw))
	 (let ((stop (syntax-comments-point ,(or stop start) ,(not forw))))
	   (should (eq (forward-comment ,(if forw 1 -1)) ,res))
	   (should (eq (point) stop)))
	 (,(intern (concat (symbol-name type) "-out")))))))

(defmacro syntax-br-comments (-type- -dir- res -start- &optional stop)
  "Create an ERT test to test (scan-lists <position> 1/-1 0).
This is to test the interface between scan-lists and the internal
comment routines in syntax.c.

The test uses a fixed name data file, which it visits.  It calls
entry and exit functions to set up and tear down syntax entries
for comment and paren characters.  The test is given a name based
on the global variable `syntax-comments-section', the direction
of movement and the value of -START-.

-TYPE- (unquoted) is a symbol from whose name the entry and exit
function names are derived by appending \"-in\" and \"-out\".

-DIR- (unquoted) is `forward' or `backward', the direction
`scan-lists' is attempted.

RES is t if `scan-lists' is expected to return, nil if it is
expected to raise a `scan-error' exception.

-START- and STOP are decimal numbers corresponding to labels in the
data file marking the start and expected stop positions.  See
`syntax-comments-point' for a precise specification.  If STOP is
missing or nil, the value of -START- is assumed for it."
  (declare (debug t))
  (let* ((forw
	  (cond
	   ((eq -dir- 'forward) t)
	   ((eq -dir- 'backward) nil)
	   (t (error "Invalid -dir- argument \"%s\" to `syntax-br-comments'" -dir-))))
         (start -start-)
	 (start-str (format "%d" (abs start)))
	 (type -type-))
    `(ert-deftest ,(intern (concat "syntax-br-comments-"
				   syntax-comments-section
				   (if forw "-f" "-b") start-str))
	 ()
       (with-current-buffer
	   (find-file
            ,(ert-resource-file "syntax-comments.txt"))
	 (,(intern (concat (symbol-name type) "-in")))
         (let ((start-pos (syntax-comments-point ,start ,forw))
               ,@(if res
                     `((stop-pos (syntax-comments-point
                                  ,(or stop start) ,(not forw))))))
           ,(if res
                `(should
                  (eq (scan-lists start-pos ,(if forw 1 -1) 0)
                      stop-pos))
              `(should-error (scan-lists start-pos ,(if forw 1 -1) 0)
                             :type 'scan-error)))
	 (,(intern (concat (symbol-name type) "-out")))))))

(defmacro syntax-pps-comments (-type- -start- open close &optional -stop-)
  "Create an ERT test to test `parse-partial-sexp' with comments.
This is to test the interface between `parse-partial-sexp' and
the internal comment routines in syntax.c.

The test uses a fixed name data file, which it visits.  It calls
entry and exit functions to set up and tear down syntax entries
for comment and paren characters.  The test is given a name based
on the global variable `syntax-comments-section', and the value
of -START-.

The generated test calls `parse-partial-sexp' three times, the
first two with COMMENTSTOP set to `syntax-table' so as to stop
after the start and end of the comment.  The third call is
expected to stop at the brace/paren matching the one where the
test started.

-TYPE- (unquoted) is a symbol from whose name the entry and exit
function names are derived by appending \"-in\" and \"-out\".

-START- and -STOP- are decimal numbers corresponding to labels in
the data file marking the start and expected stop positions.  See
`syntax-comments-point' for a precise specification.  If -STOP-
is missing or nil, the value of -START- is assumed for it.

OPEN and CLOSE are decimal numbers corresponding to labels in the
data file marking just after the comment opener and closer where
the `parse-partial-sexp's are expected to stop.  See
`syntax-comments-midpoint' for a precise specification."
  (declare (debug t))
  (let* ((type -type-)
         (start -start-)
         (start-str (format "%d" start))
         (stop (or -stop- start)))
    `(ert-deftest ,(intern (concat "syntax-pps-comments-"
                                   syntax-comments-section
                                   "-" start-str))
         ()
       (with-current-buffer
           (find-file
            ,(ert-resource-file "syntax-comments.txt"))
         (,(intern (concat (symbol-name type) "-in")))
         (let ((start-pos (syntax-comments-point ,start t))
               (open-pos (syntax-comments-midpoint ,open))
               (close-pos (syntax-comments-midpoint ,close))
               (stop-pos (syntax-comments-point ,stop nil))
               s)
           (setq s (parse-partial-sexp
                    start-pos (point-max) 0 nil nil 'syntax-table))
           (should (eq (point) open-pos))
           (setq s (parse-partial-sexp
                    (point) (point-max) 0 nil s 'syntax-table))
           (should (eq (point) close-pos))
           (setq s (parse-partial-sexp (point) (point-max) 0 nil s))
           (should (eq (point) stop-pos)))
         (,(intern (concat (symbol-name type) "-out")))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; "Pascal" style comments - single character delimiters, the closing
;; delimiter not being newline.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun {-in ()
  (setq parse-sexp-ignore-comments t)
  (setq comment-end-can-be-escaped nil)
  (modify-syntax-entry ?{ "<")
  (modify-syntax-entry ?} ">"))
(defun {-out ()
  (modify-syntax-entry ?{ "(}")
  (modify-syntax-entry ?} "){"))
(eval-and-compile
  (setq syntax-comments-section "pascal"))

(syntax-comments { forward nil 20 0)
(syntax-comments { backward nil 20 0)
(syntax-comments { forward t 21)
(syntax-comments { backward t 21)
(syntax-comments { forward t 22)
(syntax-comments { backward t 22)

(syntax-comments { forward t 23)
(syntax-comments { backward t 23)
(syntax-comments { forward t 24)
(syntax-comments { backward t 24)
(syntax-comments { forward t 26)
(syntax-comments { backward t 26)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; "Lisp" style comments - single character opening delimiters on line
;; comments.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun \;-in ()
  (setq parse-sexp-ignore-comments t)
  (setq comment-end-can-be-escaped nil)
  (modify-syntax-entry ?\n ">")
  (modify-syntax-entry ?\; "<")
  (modify-syntax-entry ?{ ".")
  (modify-syntax-entry ?} "."))
(defun \;-out ()
  (modify-syntax-entry ?\n " ")
  (modify-syntax-entry ?\; ".")
  (modify-syntax-entry ?{ "(}")
  (modify-syntax-entry ?} "){"))
(eval-and-compile
  (setq syntax-comments-section "lisp"))

(syntax-comments \; backward nil 30 30)
(syntax-comments \; forward t 31)
(syntax-comments \; backward t 31)
(syntax-comments \; forward t 32)
(syntax-comments \; backward t 32)
(syntax-comments \; forward t 33)
(syntax-comments \; backward t 33)

;; "Lisp" style comments inside lists.
(syntax-br-comments \; backward nil 40)
(syntax-br-comments \; forward t 41)
(syntax-br-comments \; backward t 41)
(syntax-br-comments \; forward t 42)
(syntax-br-comments \; backward t 42)
(syntax-br-comments \; forward nil 43)

;; "Lisp" style comments parsed by `parse-partial-sexp'.
(syntax-pps-comments \; 41 90 91)
(syntax-pps-comments \; 42 92 93)
(syntax-pps-comments \; 43 94 95 -999)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; "Lisp" style nested comments: between delimiters #|  |#.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun \#|-in ()
  (setq parse-sexp-ignore-comments t)
  (modify-syntax-entry ?# ". 14")
  (modify-syntax-entry ?| ". 23n")
  (modify-syntax-entry ?\; "< b")
  (modify-syntax-entry ?\n "> b"))
(defun \#|-out ()
  (modify-syntax-entry ?# ".")
  (modify-syntax-entry ?| ".")
  (modify-syntax-entry ?\; ".")
  (modify-syntax-entry ?\n " "))
(eval-and-compile
  (setq syntax-comments-section "lisp-n"))

(syntax-comments \#| forward nil 100 0)
(syntax-comments \#| backward nil 100 0)
(syntax-comments \#| forward nil 101 -999)
(syntax-comments \#| forward t 102)
(syntax-comments \#| backward t 102)

(syntax-comments \#| forward t 103)
(syntax-comments \#| backward t 103)
(syntax-comments \#| forward t 104)
(syntax-comments \#| backward t 104)

(syntax-comments \#| forward nil 105 -999)
(syntax-comments \#| backward t 105)
(syntax-comments \#| forward t 106)
(syntax-comments \#| backward t 106)
(syntax-comments \#| forward t 107)
(syntax-comments \#| backward t 107)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Mixed "Lisp" style (nested and unnested) comments.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(syntax-comments \#| forward t 110)
(syntax-comments \#| backward t 110)
(syntax-comments \#| forward t 111)
(syntax-comments \#| backward t 111)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Emacs 27 "C" style comments - `comment-end-can-be-escaped' is non-nil.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun /*-in ()
  (setq parse-sexp-ignore-comments t)
  (setq comment-end-can-be-escaped t)
  (modify-syntax-entry ?/ ". 124b")
  (modify-syntax-entry ?* ". 23")
  (modify-syntax-entry ?\n "> b"))
(defun /*-out ()
  (setq comment-end-can-be-escaped nil)
  (modify-syntax-entry ?/ ".")
  (modify-syntax-entry ?* ".")
  (modify-syntax-entry ?\n " "))
(eval-and-compile
  (setq syntax-comments-section "c"))

(syntax-comments /* forward t 1)
(syntax-comments /* backward t 1)
(syntax-comments /* forward t 2)
(syntax-comments /* backward t 2)
(syntax-comments /* forward t 3)
(syntax-comments /* backward t 3)

(syntax-comments /* forward t 4)
(syntax-comments /* backward t 4)
(syntax-comments /* forward t 5 6)
(syntax-comments /* backward nil 5 0)
(syntax-comments /* forward nil 6 0)
(syntax-comments /* backward t 6 5)

(syntax-comments /* forward t 7 8)
(syntax-comments /* backward nil 7 0)
(syntax-comments /* forward nil 8 0)
(syntax-comments /* backward t 8 7)
(syntax-comments /* forward t 9)
(syntax-comments /* backward t 9)

(syntax-comments /* forward nil 10 0)
(syntax-comments /* backward nil 10 0)
(syntax-comments /* forward t 11)
(syntax-comments /* backward t 11)

(syntax-comments /* forward t 13 14)
(syntax-comments /* backward nil 13 -14)
(syntax-comments /* forward t 15)
(syntax-comments /* backward t 15)

;; Emacs 27 "C" style comments inside brace lists.
(syntax-br-comments /* forward t 50)
(syntax-br-comments /* backward t 50)
(syntax-br-comments /* forward t 51)
(syntax-br-comments /* backward t 51)
(syntax-br-comments /* forward t 52)
(syntax-br-comments /* backward t 52)

(syntax-br-comments /* forward t 53)
(syntax-br-comments /* backward t 53)
(syntax-br-comments /* forward t 54 20)
(syntax-br-comments /* backward t 54)
(syntax-br-comments /* forward t 55)
(syntax-br-comments /* backward t 55)

(syntax-br-comments /* forward t 56 58)
(syntax-br-comments /* backward t 58 56)
(syntax-br-comments /* backward nil 59)
(syntax-br-comments /* forward t 60)
(syntax-br-comments /* backward t 60)

;; Emacs 27 "C" style comments parsed by `parse-partial-sexp'.
(syntax-pps-comments /* 50 70 71)
(syntax-pps-comments /* 52 72 73)
(syntax-pps-comments /* 54 74 55 20)
(syntax-pps-comments /* 56 76 77 58)
(syntax-pps-comments /* 60 78 79)

(ert-deftest test-from-to-parse-partial-sexp ()
  (with-temp-buffer
    (insert "foo")
    (should (parse-partial-sexp 1 1))
    (should-error (parse-partial-sexp 2 1))))

;;; syntax-tests.el ends here

debug log:

solving 3b9f21cde3 ...
found 3b9f21cde3 in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).