[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/haskell-tng-mode 64ad4a8 057/385: refactored to centralise
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/haskell-tng-mode 64ad4a8 057/385: refactored to centralise state |
Date: |
Tue, 5 Oct 2021 23:59:01 -0400 (EDT) |
branch: elpa/haskell-tng-mode
commit 64ad4a8cb290dcacce3805cdf6af1119dc405896
Author: Tseen She <ts33n.sh3@gmail.com>
Commit: Tseen She <ts33n.sh3@gmail.com>
refactored to centralise state
---
haskell-tng-smie.el | 108 +++++++++++++++++++++++++++++-----------------------
1 file changed, 60 insertions(+), 48 deletions(-)
diff --git a/haskell-tng-smie.el b/haskell-tng-smie.el
index addeff0..ee080a3 100644
--- a/haskell-tng-smie.el
+++ b/haskell-tng-smie.el
@@ -30,17 +30,16 @@
(require 'haskell-tng-font-lock)
;; FIXME: massive hack. Holds an ordered list of (position . level) that close
-;; an inferred layout block. This could be turned into a (cached) function call
-;; plus some state in wldo-state.
+;; an inferred layout block. Convert into a (cached) function call to calculate
+;; the relevant WLDOs for a given point.
(defvar-local haskell-tng-smie:wldos nil)
-;; FIXME: massive hack. State of previous lexeme. Unsure how to remove this.
-;; Ideally we would be able to return multiple tokens to SMIE and we wouldn't
-;; need this.
+;; State: a list of tokens to return at the current point ending with `t' as an
+;; indicator that all virtual tokens have been processed. `nil' means to
proceed
+;; as normal.
;;
-;; TODO: refactor so this stores the list of tokens to return at the current
-;; point, and some information allowing cache invalidation.
-(defvar-local haskell-tng-smie:wldo-state nil)
+;; FIXME cache invalidation
+(defvar-local haskell-tng-smie:multi nil)
;; Function to scan forward for the next token.
;;
@@ -52,48 +51,61 @@
;; https://www.gnu.org/software/emacs/manual/html_mono/elisp.html#SMIE-Lexer
(defun haskell-tng-smie:forward-token ()
(interactive) ;; for testing
- (forward-comment (point-max))
- (if (eobp)
- "}"
- (let ((case-fold-search nil)
+ (forward-comment (point-max)) ;; TODO: move to after virtual token generation
+ (cond
+ ;; TODO: remove this hack
+ ((eobp)
+ "}")
+
+ ;; reading from state
+ ((stringp (car haskell-tng-smie:multi))
+ (pop haskell-tng-smie:multi))
+
+ (t
+ (let ((done-multi (pop haskell-tng-smie:multi))
+ (case-fold-search nil)
(syntax (char-syntax (char-after)))
- (wldo-state haskell-tng-smie:wldo-state)
(offside (car haskell-tng-smie:wldos)))
- (setq haskell-tng-smie:wldo-state nil)
- (cond
- ;; layout
- ((and (eq wldo-state 'start) (not (looking-at "{")))
- (push (haskell-tng:layout-close-and-level) haskell-tng-smie:wldos)
- (setq haskell-tng-smie:wldo-state 'middle)
- "{")
- ((when-let (close (car offside))
- (= (point) close))
- (pop haskell-tng-smie:wldos)
- "}")
- ((when-let (level (cdr offside))
- (and
- (= (current-column) level)
- (not (eq wldo-state 'middle))))
- (setq haskell-tng-smie:wldo-state 'middle)
- ";")
-
- ;; parens
- ((member syntax '(?\( ?\) ?\" ?$)) nil)
-
- ;; layout detection
- ((looking-at (rx word-start (| "where" "let" "do" "of") word-end))
- (setq haskell-tng-smie:wldo-state 'start)
- (haskell-tng-smie:last-match))
-
- ;; regexps
- ((or
- ;; known identifiers
- (looking-at haskell-tng:regexp:reserved)
- ;; symbols
- (looking-at (rx (+ (| (syntax word) (syntax symbol)))))
- ;; whatever the current syntax class is
- (looking-at (rx-to-string `(+ (syntax ,syntax)))))
- (haskell-tng-smie:last-match))))))
+ (cl-flet ((virtual-end () (= (point) (car offside)))
+ (virtual-semicolon () (= (current-column) (cdr offside))))
+ (cond
+ ;; layout
+ ((and offside
+ (not done-multi)
+ (or (virtual-end) (virtual-semicolon)))
+ (setq haskell-tng-smie:multi '(t))
+ (while (and offside (virtual-end))
+ (push "}" haskell-tng-smie:multi)
+ (pop haskell-tng-smie:wldos)
+ (setq offside (car haskell-tng-smie:wldos)))
+ (when (and offside (virtual-semicolon))
+ (setq haskell-tng-smie:multi
+ (-insert-at (- (length haskell-tng-smie:multi) 1)
+ ";" haskell-tng-smie:multi)))
+ (pop haskell-tng-smie:multi))
+
+ ;; parens
+ ((member syntax '(?\( ?\) ?\" ?$)) nil)
+
+ ;; layout detection
+ ((looking-at (rx word-start (| "where" "let" "do" "of") word-end))
+ (save-match-data
+ (forward-word)
+ (forward-comment (point-max))
+ (when (not (looking-at "{"))
+ (push (haskell-tng:layout-close-and-level)
haskell-tng-smie:wldos)
+ (setq haskell-tng-smie:multi '("{" t))))
+ (haskell-tng-smie:last-match))
+
+ ;; regexps
+ ((or
+ ;; known identifiers
+ (looking-at haskell-tng:regexp:reserved)
+ ;; symbols
+ (looking-at (rx (+ (| (syntax word) (syntax symbol)))))
+ ;; whatever the current syntax class is
+ (looking-at (rx-to-string `(+ (syntax ,syntax)))))
+ (haskell-tng-smie:last-match))))))))
(defun haskell-tng:layout-of-next-token ()
(save-excursion
- [nongnu] elpa/haskell-tng-mode 8e1a225 068/385: sexp tests, (continued)
- [nongnu] elpa/haskell-tng-mode 8e1a225 068/385: sexp tests, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 269be91 072/385: revert broken grammar rules, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 3194e62 074/385: stefan to the rescue, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode b690037 081/385: comment-* support, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 502cc26 085/385: document a failure mode, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 08f924c 088/385: simplify the grammar rules, better s-exps, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 71cf945 048/385: lexer test based on Haskell2010, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 4d6bbfc 050/385: feedback from Stefan, improving lexing, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 96609e4 052/385: thoughts on layout inference, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 06b357c 054/385: hacky closing braces, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 64ad4a8 057/385: refactored to centralise state,
ELPA Syncer <=
- [nongnu] elpa/haskell-tng-mode 0ac5a2f 059/385: copyright years and move the test assertions, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode a6bb27e 061/385: [ci skip] layout algorithm implemented and tested, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 7d2863e 065/385: tests for SMIE state invalidation, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 41a29dd 066/385: backward lexer, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode c48e7a5 069/385: starting to transcribe the expression table, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 1f1110a 073/385: transcribe the grammar rules, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 01789b1 075/385: y u no haskell-mode?, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode b8f3e3f 079/385: back out incomplete grammar rules, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 9e19b2b 080/385: double down on simpler grammar, ELPA Syncer, 2021/10/06
- [nongnu] elpa/haskell-tng-mode 6e7a24f 083/385: lexer identifies conid / varid, ELPA Syncer, 2021/10/06