forked from k1complete/elixir-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelixir-smie.el
202 lines (184 loc) · 8.93 KB
/
elixir-smie.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
(require 'smie)
(progn
(setq elixir-syntax-class-names nil)
(defmacro elixir-smie-define-regexp-opt (name &rest table)
`(elixir-smie-define-regexp ,name (regexp-opt (list ,@table))))
(defmacro elixir-smie-define-regexp (name regexp &optional flag)
(let ((regex-name (intern (format "elixir-smie-%s" name))))
`(progn
(defconst ,regex-name
,regexp)
(pushnew `(,',regex-name . ,(upcase (symbol-name ',name))) elixir-syntax-class-names))))
(elixir-smie-define-regexp-opt op
"<<<" ">>>" "^^^" "~~~" "&&&" "|||" ; op3
"===" "!==" ; comp3
"==" "!=" "<=" ">=" ; comp2
"{}" "[]" ; container2
"<" ">" ; comp1
"+" "-" "*" "/" "=" "|" "!" "^" "@" ; op1
"&&" "||" "<>" "++" "--" "**" "//" "::" "<-" ".." "/>" "=~" ; op2 (minus ->)
)
(elixir-smie-define-regexp dot "\\.")
(elixir-smie-define-regexp comma ",")
(elixir-smie-define-regexp -> "->")
(elixir-smie-define-regexp << "<<")
(elixir-smie-define-regexp >> ">>")
(elixir-smie-define-regexp-opt parens "(" ")" "{" "}" "[" "]" "<<" ">>"))
(defvar elixir-tokenizer-syntax-table (let ((table (copy-syntax-table elixir-mode-syntax-table)))
(modify-syntax-entry ?\n "." table)
table))
(setq elixir-smie-block-intro-keywords '(do else catch after rescue -> COMMA))
(defun elixir-smie-next-token-no-lookaround (forwardp nested)
;; First, skip comments but determine if newline-as-whitespace is
;; significant:
(with-syntax-table (if (and (not nested)
(member
(intern
(save-excursion
(elixir-smie-next-token-no-lookaround nil t)))
elixir-smie-block-intro-keywords))
elixir-mode-syntax-table
elixir-tokenizer-syntax-table)
(if forwardp
(forward-comment (point-max))
(forward-comment (- (point)))))
(let* ((found-token-class (find-if
(lambda (class-def)
(let ((regex (symbol-value (car class-def))))
(if forwardp
(looking-at regex)
(looking-back regex nil t))))
elixir-syntax-class-names))
(maybe-token (cond ((member (if forwardp
(following-char)
(preceding-char))
'(?\n ?\;))
(if forwardp
(forward-comment (point-max))
(forward-comment (- (point))))
";")
(found-token-class
(goto-char (if forwardp
(match-end 0)
(match-beginning 0)))
(if (string= "PARENS" (cdr found-token-class))
(buffer-substring-no-properties (match-beginning 0) (match-end 0))
(cdr found-token-class)))
((when (= ?\" (char-syntax (if forwardp
(following-char)
(preceding-char))))
(if forwardp
(forward-sexp)
(backward-sexp))
"STRING")))))
(or maybe-token
(downcase
(buffer-substring-no-properties
(point)
(if forwardp
(progn (skip-syntax-forward "'w_")
(point))
(progn (skip-syntax-backward "'w_")
(point))))))))
(defun elixir-smie-next-token (forwardp)
;; When reading match statements (the ones with expr -> statements),
;; we need to drop non-; delimiters so the parser knows when a
;; match statement ends and another begins, so scan around point to
;; see if there are any -> within the current block's scope.
;; If the current token is a ";", scan forward to see if the current
;; potential statement contains a "->". If so, scan back to find a
;; "do". If there is a -> there, emit a match-statement-delimiter
;; instead of the ";".
(let ((current-token (elixir-smie-next-token-no-lookaround forwardp nil)))
(if (and (string= ";" current-token)
;; Scan ahead:
(let ((level 0)
token)
(save-excursion
(block nil
(while (and (not (= (point) (point-max))) (not (string= "" token)) (not (string= ";" token)))
(setq token (elixir-smie-next-token-no-lookaround t nil))
(cond ((and (= level 0) (string= "->" token))
(return t))
((find token '("do" "fn") :test 'string=)
(incf level))
((string= token "end")
(decf level)))))))
;; Scan behind:
(let (token)
(save-excursion
(block nil
(while (and (not (= (point) (point-min))) (not (string= "" token)) (not (string= "do" token)) (not (string= "fn" token)))
(setq token (elixir-smie-next-token-no-lookaround nil nil))
(when (string= "->" token)
(return t)))
(when (or (string= token "do"))
t)))))
"MATCH-STATEMENT-DELIMITER"
current-token)))
(defun elixir-smie-forward-token ()
(elixir-smie-next-token t))
(defun elixir-smie-backward-token ()
(elixir-smie-next-token nil))
(setq elixir-smie-grammar
(smie-prec2->grammar
(smie-bnf->prec2
'((id)
(statements
(statement)
(statement ";" statements))
(statement
("if" non-block-expr "do" statements "else" statements "end")
("if" non-block-expr "do" statements "end")
("if" non-block-expr "COMMA" "do:" statement)
("if" non-block-expr "COMMA" "do:" statement "COMMA" "else:" statement)
("try" "do" statements "after" statements "end")
("try" "do" statements "catch" match-statements "end")
("try" "do" statements "end")
("case" non-block-expr "do" match-statements "end")
("fn" match-statement "end")
("function" "do" match-statements "end")
(non-block-expr "do" statements "end")
(expr)
)
(non-block-expr
(non-block-expr "OP" non-block-expr)
(non-block-expr "DOT" non-block-expr)
(non-block-expr "COMMA" non-block-expr)
("(" statements ")")
("{" statements "}")
("[" statements "]")
("STRING"))
(match-statements
(match-statement "MATCH-STATEMENT-DELIMITER" match-statements)
(match-statement))
(match-statement
(non-block-expr "->" statements)))
'((assoc "DOT") (assoc "if") (assoc "do:") (assoc "else:") (assoc "COMMA") (assoc "OP") (assoc "->" ";")))))
(defvar elixir-smie-indent-basic 2)
(defun verbose-elixir-smie-rules (kind token)
(let ((value (elixir-smie-rules kind token)))
(message "%s '%s'; s:%s p:%s == %s" kind token (ignore-errors (smie-rule-sibling-p)) (ignore-errors smie--parent) value)
value))
(defun elixir-smie-rules (kind token)
(pcase (cons kind token)
(`(:elem . basic) elixir-smie-indent-basic)
(`(:after . "->")
(when (smie-rule-hanging-p)
elixir-smie-indent-basic))
(`(,_ . ,(or `"COMMA")) (smie-rule-separator kind))
(`(:after . "=") elixir-smie-indent-basic)
(`(:after . ,(or `"do"))
elixir-smie-indent-basic)
(`(:list-intro . ,(or `"do"))
t)))
(define-minor-mode elixir-smie-mode
"SMIE-based indentation and syntax for Elixir"
nil nil nil nil
(set (make-local-variable 'comment-start) "# ")
(set (make-local-variable 'comment-end) "")
(smie-setup elixir-smie-grammar 'elixir-smie-rules ; 'verbose-elixir-smie-rules
:forward-token 'elixir-smie-forward-token
:backward-token 'elixir-smie-backward-token))
(define-key elixir-mode-map (kbd "C-M-d") 'smie-down-list)
(provide 'elixir-smie)