Skip to content

Commit 19e6974

Browse files
committed
Abstract syntax-ppss
1 parent ea029a0 commit 19e6974

File tree

2 files changed

+114
-45
lines changed

2 files changed

+114
-45
lines changed

swift-mode-indent.el

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,10 +1510,11 @@ Return nil otherwise."
15101510
"Return t if the point is inside an incomplete comment.
15111511
15121512
Return nil otherwise."
1513-
(and (nth 4 (syntax-ppss))
1514-
(save-excursion
1515-
(goto-char (nth 8 (syntax-ppss)))
1516-
(not (forward-comment 1)))))
1513+
(let ((chunk (swift-mode:chunk-after)))
1514+
(and (swift-mode:chunk:comment-p chunk)
1515+
(save-excursion
1516+
(goto-char (swift-mode:chunk:start chunk))
1517+
(not (forward-comment 1))))))
15171518

15181519
(defun swift-mode:indent-new-comment-line (&optional soft)
15191520
"Break the line at the point and indent the new line.
@@ -1523,10 +1524,8 @@ multiline comment, close the previous comment and start new one if
15231524
`comment-multi-line' is nil.
15241525
See `indent-new-comment-line' for SOFT."
15251526
(interactive)
1526-
(let* ((parser-state (syntax-ppss))
1527-
(is-inside-comment (nth 4 parser-state))
1528-
(is-single-line-comment (eq (nth 7 parser-state) 1))
1529-
(comment-beginning-position (nth 8 parser-state))
1527+
(let* ((chunk (swift-mode:chunk-after))
1528+
(comment-beginning-position (swift-mode:chunk:start chunk))
15301529
(space-after-asterisk
15311530
(if swift-mode:insert-space-after-asterisk-in-comment " " ""))
15321531
(default-line-prefix
@@ -1537,20 +1536,20 @@ See `indent-new-comment-line' for SOFT."
15371536
(if soft (insert-and-inherit ?\n) (newline 1))
15381537
(delete-horizontal-space)
15391538

1540-
(when is-inside-comment
1539+
(when (swift-mode:chunk:comment-p chunk)
15411540
(insert-before-markers-and-inherit
15421541
(cond
1543-
(is-single-line-comment
1542+
((swift-mode:chunk:single-line-comment-p chunk)
15441543
(save-excursion
15451544
(goto-char comment-beginning-position)
1546-
(looking-at "/+\\s *")
1545+
(looking-at "/+\\(\\s *\\)")
15471546
(match-string-no-properties 0)))
15481547

15491548
(comment-multi-line
15501549
(save-excursion
1551-
(beginning-of-line)
1550+
(forward-line 0)
15521551
(forward-char -1)
1553-
(beginning-of-line)
1552+
(forward-line 0)
15541553
(if (<= (point) comment-beginning-position)
15551554
;; The cursor was on the 2nd line of the comment.
15561555
;; Uses default prefix.
@@ -1576,7 +1575,7 @@ See `indent-new-comment-line' for SOFT."
15761575

15771576
;; Closes incomplete multiline comment.
15781577
(when (and swift-mode:auto-close-multiline-comment
1579-
(not is-single-line-comment)
1578+
(swift-mode:chunk:multiline-comment-p chunk)
15801579
(swift-mode:incomplete-comment-p))
15811580
(save-excursion
15821581
(end-of-line)
@@ -1592,7 +1591,7 @@ See `indent-new-comment-line' for SOFT."
15921591
((and
15931592
swift-mode:prepend-asterisk-to-comment-line
15941593
(= last-command-event ?*)
1595-
(nth 4 (syntax-ppss))
1594+
(swift-mode:chunk:comment-p (swift-mode:chunk-after))
15961595
(save-excursion (backward-char) (skip-syntax-backward " ") (bolp)))
15971596
(when swift-mode:insert-space-after-asterisk-in-comment
15981597
(insert-before-markers-and-inherit " "))
@@ -1602,10 +1601,10 @@ See `indent-new-comment-line' for SOFT."
16021601
((and
16031602
(= last-command-event ?/)
16041603
swift-mode:fix-comment-close
1605-
(nth 4 (syntax-ppss))
1604+
(swift-mode:chunk:comment-p (swift-mode:chunk-after))
16061605
(save-excursion
16071606
(let ((pos (point)))
1608-
(beginning-of-line)
1607+
(forward-line 0)
16091608
(and
16101609
(looking-at "^\\s *\\*\\s +/")
16111610
(eq (match-end 0) pos)

swift-mode-lexer.el

Lines changed: 98 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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
748745
type `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
959957
type `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
12381240
If 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

Comments
 (0)