@@ -221,19 +221,16 @@ Intended for `syntax-propertize-function'."
221221 nil
222222 swift-mode:matching-parenthesis
223223 nil ))
224- (let* ((parser-state (syntax-ppss start))
225- (inside-string (nth 3 parser-state))
226- (comment-nesting (nth 4 parser-state))
227- (comment-beginning-position (nth 8 parser-state)))
224+ (let* ((chunk (swift-mode:chunk-after (syntax-ppss start))))
228225 (cond
229- ((eq inside- string t )
226+ ((swift-mode:chunk:multiline- string-p chunk )
230227 (swift-mode:syntax-propertize:end-of-multiline-string end))
231228
232- (inside- string
229+ ((swift-mode:chunk:single-line- string-p chunk)
233230 (swift-mode:syntax-propertize:end-of-single-line-string end))
234231
235- (comment-nesting
236- (goto-char comment-beginning-position )
232+ ((swift-mode:chunk: comment-p chunk)
233+ (goto-char (swift-mode:chunk:start chunk) )
237234 (forward-comment (point-max )))))
238235
239236 (swift-mode:syntax-propertize:scan end 0 ))
@@ -748,8 +745,9 @@ Return the token object. If no more tokens available, return a token with
748745type `out-of-buffer' "
749746
750747 (let ((pos (point )))
751- (when (nth 4 (syntax-ppss ))
752- (goto-char (nth 8 (syntax-ppss ))))
748+ (let ((chunk (swift-mode:chunk-after)))
749+ (when (swift-mode:chunk:comment-p chunk)
750+ (goto-char (swift-mode:chunk:start chunk))))
753751 (forward-comment (point-max ))
754752 (cond
755753 ; ; Outside of buffer
@@ -959,8 +957,9 @@ Return the token object. If no more tokens available, return a token with
959957type `out-of-buffer' ."
960958
961959 (let ((pos (point )))
962- (when (nth 4 (syntax-ppss ))
963- (goto-char (nth 8 (syntax-ppss ))))
960+ (let ((chunk (swift-mode:chunk-after)))
961+ (when (swift-mode:chunk:comment-p chunk)
962+ (goto-char (swift-mode:chunk:start chunk))))
964963 (forward-comment (- (point )))
965964 (cond
966965 ; ; Outside of buffer
@@ -1226,28 +1225,99 @@ Assuming the cursor is on a string."
12261225
12271226(defun swift-mode:goto-non-comment-bol ()
12281227 " Back to the beginning of line that is not inside a comment."
1229- (beginning-of-line )
1230- (while (nth 4 (syntax-ppss ))
1231- ; ; The cursor is in a comment. Backs to the beginning of the comment.
1232- (goto-char (nth 8 (syntax-ppss )))
1233- (beginning-of-line )))
1228+ (forward-line 0 )
1229+ (let (chunk)
1230+ (while (progn
1231+ (setq chunk (swift-mode:chunk-after))
1232+ (swift-mode:chunk:comment-p chunk))
1233+ ; ; The cursor is in a comment. Backs to the beginning of the comment.
1234+ (goto-char (swift-mode:chunk:start chunk))
1235+ (forward-line 0 ))))
12341236
12351237(defun swift-mode:goto-non-comment-eol ()
12361238 " Proceed to the end of line that is not inside a comment.
12371239
12381240If this line ends with a single-line comment, goto just before the comment."
12391241 (end-of-line )
1240- (while (nth 4 (syntax-ppss ))
1241- ; ; The cursor is in a comment.
1242- (if (eq (nth 4 (syntax-ppss )) t )
1243- ; ; This ia a single-line comment
1244- ; ; Back to the beginning of the comment.
1245- (goto-char (nth 8 (syntax-ppss )))
1246- ; ; This is a multiline comment
1247- ; ; Proceed to the end of the comment.
1248- (goto-char (nth 8 (syntax-ppss )))
1249- (forward-comment 1 )
1250- (end-of-line ))))
1242+ (let (chunk)
1243+ (while (progn
1244+ (setq chunk (swift-mode:chunk-after))
1245+ (swift-mode:chunk:comment-p chunk))
1246+ ; ; The cursor is in a comment.
1247+ (if (swift-mode:chunk:single-line-comment-p chunk)
1248+ ; ; This ia a single-line comment
1249+ ; ; Back to the beginning of the comment.
1250+ (goto-char (swift-mode:chunk:start chunk))
1251+ ; ; This is a multiline comment
1252+ ; ; Proceed to the end of the comment.
1253+ (goto-char (swift-mode:chunk:start chunk))
1254+ (forward-comment 1 )
1255+ (end-of-line )))))
1256+
1257+ ; ;; Comment or string chunks
1258+
1259+ ; ; A chunk is either a string-chunk or a comment.
1260+ ; ; It have the type and the start position.
1261+
1262+ (defun swift-mode:chunk (type start )
1263+ " Return a new chunk with TYPE and START position."
1264+ (list type start))
1265+
1266+ (defun swift-mode:chunk:type (chunk )
1267+ " Return the type of the CHUNK."
1268+ (nth 0 chunk))
1269+
1270+ (defun swift-mode:chunk:start (chunk )
1271+ " Return the start position of the CHUNK."
1272+ (nth 1 chunk))
1273+
1274+ (defun swift-mode:chunk:comment-p (chunk )
1275+ " Return non-nil if the CHUNK is a comment."
1276+ (memq (swift-mode:chunk:type chunk) '(single-line-comment multiline-comment)))
1277+
1278+ (defun swift-mode:chunk:string-p (chunk )
1279+ " Return non-nil if the CHUNK is a string."
1280+ (memq (swift-mode:chunk:type chunk) '(single-line-string multiline-string)))
1281+
1282+ (defun swift-mode:chunk:single-line-comment-p (chunk )
1283+ " Return non-nil if the CHUNK is a single-line comment."
1284+ (eq (swift-mode:chunk:type chunk) 'single-line-comment ))
1285+
1286+ (defun swift-mode:chunk:multiline-comment-p (chunk )
1287+ " Return non-nil if the CHUNK is a multiline comment."
1288+ (eq (swift-mode:chunk:type chunk) 'multiline-comment ))
1289+
1290+ (defun swift-mode:chunk:single-line-string-p (chunk )
1291+ " Return non-nil if the CHUNK is a single-line string."
1292+ (eq (swift-mode:chunk:type chunk) 'single-line-string ))
1293+
1294+ (defun swift-mode:chunk:multiline-string-p (chunk )
1295+ " Return non-nil if the CHUNK is a multiline string."
1296+ (eq (swift-mode:chunk:type chunk) 'multiline-string ))
1297+
1298+ (defun swift-mode:chunk-after (&optional parser-state )
1299+ " Return the chunk at the cursor.
1300+
1301+ If the cursor is outside of strings and comments, return nil.
1302+
1303+ If PARSER-STATE is given, it is used instead of (syntax-ppss)."
1304+ (unless parser-state
1305+ (setq parser-state (syntax-ppss )))
1306+ (cond
1307+ ((eq (nth 3 parser-state) t )
1308+ (swift-mode:chunk 'multiline-string (nth 8 parser-state)))
1309+ ((nth 3 parser-state)
1310+ (swift-mode:chunk 'single-line-string (nth 8 parser-state)))
1311+ ((eq (nth 4 parser-state) t )
1312+ (swift-mode:chunk 'single-line-comment (nth 8 parser-state)))
1313+ ((nth 4 parser-state)
1314+ (swift-mode:chunk 'multiline-comment (nth 8 parser-state)))
1315+ ((and (eq (char-before ) ?/ ) (eq (char-after ) ?/ ))
1316+ (swift-mode:chunk 'single-line-comment (1- (point ))))
1317+ ((and (eq (char-before ) ?/ ) (eq (char-after ) ?* ))
1318+ (swift-mode:chunk 'multiline-comment (1- (point ))))
1319+ (t
1320+ nil )))
12511321
12521322(provide 'swift-mode-lexer )
12531323
0 commit comments