Skip to content

Commit 2c0b2b7

Browse files
committed
Fix infinite loop in swift-mode:syntax-propertize:scan
Fix #192
1 parent ab189d6 commit 2c0b2b7

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

swift-mode-lexer.el

+58-2
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ Intended for `syntax-propertize-function'."
251251
((swift-mode:chunk:comment-p chunk)
252252
(goto-char (swift-mode:chunk:start chunk))
253253
(setq comment-start (point))
254-
(forward-comment 1)
254+
(swift-mode:forward-comment 1 end)
255255
(put-text-property comment-start (point) 'swift-mode:comment t))))
256256
(swift-mode:syntax-propertize:scan end 0))
257257

@@ -304,7 +304,7 @@ stops where the level becomes zero."
304304
;; Comments
305305
((memq (char-after) '(?/ ?*))
306306
(goto-char start)
307-
(forward-comment 1)
307+
(swift-mode:forward-comment 1 end)
308308
(put-text-property start (point) 'swift-mode:comment t))
309309

310310
;; Operators
@@ -323,6 +323,62 @@ stops where the level becomes zero."
323323
(goto-char end))
324324
found-matching-parenthesis))
325325

326+
(defun swift-mode:forward-comment (count &optional end)
327+
"Move point after spaces and COUNT comments.
328+
329+
If END is given, do not move beyond END.
330+
331+
COUNT must be positive.
332+
333+
Multiline comments can be nested.
334+
335+
If the comment is not closed, move to END if given, or `point-max' if it
336+
is nil.
337+
338+
This function doesn't depend on the syntax tables, so that it works in
339+
`swift-mode:syntax-propertize'."
340+
(while (< 0 count)
341+
(skip-chars-forward "\s\t\n")
342+
(cond
343+
;; Single line comment
344+
((and (eq (char-after) ?/)
345+
(eq (char-after (1+ (point))) ?/))
346+
(forward-line))
347+
348+
;; Multiline comment
349+
((and (eq (char-after) ?/)
350+
(eq (char-after (1+ (point))) ?*))
351+
(swift-mode:forward-multiline-comment (or end (point-max))))
352+
353+
;; Not a comment
354+
(t
355+
(setq count 1)))
356+
(setq count (1- count))))
357+
358+
(defun swift-mode:forward-multiline-comment (end)
359+
"Move point after a multiline comment.
360+
361+
Assuming the point is just before a multiline comment.
362+
363+
Do not move beyond END.
364+
365+
Multiline comments can be nested.
366+
367+
If the comment is not closed, move to END."
368+
(forward-char 2)
369+
(let ((pattern (mapconcat #'regexp-quote '("/*" "*/") "\\|"))
370+
(nesting-level 1))
371+
(while (and (< 0 nesting-level)
372+
(< (point) end)
373+
(search-forward-regexp pattern end t))
374+
(if (eq (char-before) ?*)
375+
(setq nesting-level (1+ nesting-level))
376+
(setq nesting-level (1- nesting-level))))
377+
(when (or (and (not (zerop nesting-level))
378+
(< (point) end))
379+
(< end (point)))
380+
(goto-char end))))
381+
326382
(defun swift-mode:put-syntax-multiline-property (start end)
327383
"Put `syntax-multiline` text propery from START to END.
328384

0 commit comments

Comments
 (0)