Skip to content

Commit

Permalink
Fix infinite loop in swift-mode:syntax-propertize:scan
Browse files Browse the repository at this point in the history
Fix #192
  • Loading branch information
taku0 committed Jan 11, 2025
1 parent ab189d6 commit 2c0b2b7
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions swift-mode-lexer.el
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ Intended for `syntax-propertize-function'."
((swift-mode:chunk:comment-p chunk)
(goto-char (swift-mode:chunk:start chunk))
(setq comment-start (point))
(forward-comment 1)
(swift-mode:forward-comment 1 end)
(put-text-property comment-start (point) 'swift-mode:comment t))))
(swift-mode:syntax-propertize:scan end 0))

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

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

(defun swift-mode:forward-comment (count &optional end)
"Move point after spaces and COUNT comments.
If END is given, do not move beyond END.
COUNT must be positive.
Multiline comments can be nested.
If the comment is not closed, move to END if given, or `point-max' if it
is nil.
This function doesn't depend on the syntax tables, so that it works in
`swift-mode:syntax-propertize'."
(while (< 0 count)
(skip-chars-forward "\s\t\n")
(cond
;; Single line comment
((and (eq (char-after) ?/)
(eq (char-after (1+ (point))) ?/))
(forward-line))

;; Multiline comment
((and (eq (char-after) ?/)
(eq (char-after (1+ (point))) ?*))
(swift-mode:forward-multiline-comment (or end (point-max))))

;; Not a comment
(t
(setq count 1)))
(setq count (1- count))))

(defun swift-mode:forward-multiline-comment (end)
"Move point after a multiline comment.
Assuming the point is just before a multiline comment.
Do not move beyond END.
Multiline comments can be nested.
If the comment is not closed, move to END."
(forward-char 2)
(let ((pattern (mapconcat #'regexp-quote '("/*" "*/") "\\|"))
(nesting-level 1))
(while (and (< 0 nesting-level)
(< (point) end)
(search-forward-regexp pattern end t))
(if (eq (char-before) ?*)
(setq nesting-level (1+ nesting-level))
(setq nesting-level (1- nesting-level))))
(when (or (and (not (zerop nesting-level))
(< (point) end))
(< end (point)))
(goto-char end))))

(defun swift-mode:put-syntax-multiline-property (start end)
"Put `syntax-multiline` text propery from START to END.
Expand Down

0 comments on commit 2c0b2b7

Please sign in to comment.