1 ;;; fuel-syntax.el --- auxiliar definitions for factor code navigation.
3 ;; Copyright (C) 2008 Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
11 ;; Auxiliar constants and functions to parse factor code.
18 ;;; Thing-at-point support for factor symbols:
20 (defun fuel-syntax--beginning-of-symbol ()
21 "Move point to the beginning of the current symbol."
22 (while (eq (char-before) ?:) (backward-char))
23 (skip-syntax-backward "w_"))
25 (defun fuel-syntax--end-of-symbol ()
26 "Move point to the end of the current symbol."
27 (skip-syntax-forward "w_")
28 (while (looking-at ":") (forward-char)))
30 (put 'factor-symbol 'end-op 'fuel-syntax--end-of-symbol)
31 (put 'factor-symbol 'beginning-op 'fuel-syntax--beginning-of-symbol)
33 (defsubst fuel-syntax-symbol-at-point ()
34 (let ((s (substring-no-properties (thing-at-point 'factor-symbol))))
35 (and (> (length s) 0) s)))
40 (defconst fuel-syntax--parsing-words
41 '("{" "}" "^:" "^::" ";" "<<" "<PRIVATE" ">>"
42 "BIN:" "BV{" "B{" "C:" "C-STRUCT:" "C-UNION:" "CHAR:" "CS{" "C{"
43 "DEFER:" "ERROR:" "EXCLUDE:" "FORGET:"
44 "GENERIC#" "GENERIC:" "HEX:" "HOOK:" "H{"
45 "IN:" "INSTANCE:" "INTERSECTION:"
46 "M:" "MACRO:" "MACRO::" "MAIN:" "MATH:" "METHOD:" "MIXIN:"
47 "OCT:" "POSTPONE:" "PREDICATE:" "PRIMITIVE:" "PRIVATE>" "PROVIDE:"
48 "REQUIRE:" "REQUIRES:" "SINGLETON:" "SLOT:" "SYMBOL:" "SYMBOLS:"
49 "TUPLE:" "T{" "t\\??" "TYPEDEF:"
50 "UNION:" "USE:" "USING:" "V{" "VARS:" "W{"))
52 (defconst fuel-syntax--parsing-words-ext-regex
53 (regexp-opt '("B" "call-next-method" "delimiter" "f" "initial:" "read-only")
56 (defconst fuel-syntax--declaration-words
57 '("flushable" "foldable" "inline" "parsing" "recursive"))
59 (defconst fuel-syntax--declaration-words-regex
60 (regexp-opt fuel-syntax--declaration-words 'words))
62 (defsubst fuel-syntax--second-word-regex (prefixes)
63 (format "^%s +\\([^ \r\n]+\\)" (regexp-opt prefixes t)))
65 (defconst fuel-syntax--method-definition-regex
66 "^M: +\\([^ ]+\\) +\\([^ ]+\\)")
68 (defconst fuel-syntax--word-definition-regex
69 (fuel-syntax--second-word-regex '(":" "::" "GENERIC:")))
71 (defconst fuel-syntax--type-definition-regex
72 (fuel-syntax--second-word-regex '("TUPLE:" "SINGLETON:")))
74 (defconst fuel-syntax--parent-type-regex "^TUPLE: +[^ ]+ +< +\\([^ ]+\\)")
76 (defconst fuel-syntax--constructor-regex "<[^ >]+>")
78 (defconst fuel-syntax--setter-regex "\\W>>[^ ]+\\b")
80 (defconst fuel-syntax--symbol-definition-regex
81 (fuel-syntax--second-word-regex '("SYMBOL:" "VAR:")))
83 (defconst fuel-syntax--stack-effect-regex " ( .* )")
85 (defconst fuel-syntax--using-lines-regex "^USING: +\\([^;]+\\);")
87 (defconst fuel-syntax--use-line-regex "^USE: +\\(.*\\)$")
89 (defconst fuel-syntax--current-vocab-regex "^IN: +\\([^ \r\n\f]+\\)")
91 (defconst fuel-syntax--sub-vocab-regex "^<\\([^ \n]+\\) *$")
93 (defconst fuel-syntax--definition-starters-regex
94 (regexp-opt '("VARS" "TUPLE" "MACRO" "MACRO:" "M" ":" "")))
96 (defconst fuel-syntax--definition-start-regex
97 (format "^\\(%s:\\) " fuel-syntax--definition-starters-regex))
99 (defconst fuel-syntax--definition-end-regex
100 (format "\\(\\(^\\| +\\);\\( +%s\\)*\\($\\| +\\)\\)"
101 fuel-syntax--declaration-words-regex))
103 (defconst fuel-syntax--single-liner-regex
104 (format "^%s" (regexp-opt '("DEFER:" "GENERIC:" "IN:"
105 "PRIVATE>" "<PRIVATE"
106 "SINGLETON:" "SYMBOL:" "USE:" "VAR:"))))
108 (defconst fuel-syntax--begin-of-def-regex
109 (format "^USING: \\|\\(%s\\)\\|\\(%s .*\\)"
110 fuel-syntax--definition-start-regex
111 fuel-syntax--single-liner-regex))
113 (defconst fuel-syntax--end-of-def-line-regex
114 (format "^.*%s" fuel-syntax--definition-end-regex))
116 (defconst fuel-syntax--end-of-def-regex
117 (format "\\(%s\\)\\|\\(%s .*\\)"
118 fuel-syntax--end-of-def-line-regex
119 fuel-syntax--single-liner-regex))
121 ;;; Factor syntax table
123 (defvar fuel-syntax--syntax-table
125 (table (make-syntax-table)))
126 ;; Default is atom-constituent
128 (modify-syntax-entry i "_ " table)
134 (modify-syntax-entry i "w " table)
138 (modify-syntax-entry i "w " table)
142 (modify-syntax-entry i "w " table)
146 (modify-syntax-entry ?\t " " table)
147 (modify-syntax-entry ?\f " " table)
148 (modify-syntax-entry ?\r " " table)
149 (modify-syntax-entry ? " " table)
152 (modify-syntax-entry ?\n ">" table)
155 (modify-syntax-entry ?\[ "(] " table)
156 (modify-syntax-entry ?\] ")[ " table)
157 (modify-syntax-entry ?{ "(} " table)
158 (modify-syntax-entry ?} "){ " table)
160 (modify-syntax-entry ?\( "()" table)
161 (modify-syntax-entry ?\) ")(" table)
164 (modify-syntax-entry ?\" "\"" table)
165 (modify-syntax-entry ?\\ "/" table)
167 "Syntax table used while in Factor mode.")
169 (defconst fuel-syntax--syntactic-keywords
170 `(("\\(#!\\)" (1 "<"))
173 ("\\(!(\\) .* \\()\\)" (1 "<") (2 ">"))
174 ("\\([[({]\\)\\([^ \"\n]\\)" (1 "_") (2 "_"))
175 ("\\([^ \"\n]\\)\\([])}]\\)" (1 "_") (2 "_"))))
178 ;;; Source code analysis:
180 (defsubst fuel-syntax--brackets-depth ()
181 (nth 0 (syntax-ppss)))
183 (defsubst fuel-syntax--brackets-start ()
184 (nth 1 (syntax-ppss)))
186 (defun fuel-syntax--brackets-end ()
188 (goto-char (fuel-syntax--brackets-start))
190 (progn (forward-sexp)
194 (defsubst fuel-syntax--indentation-at (pos)
195 (save-excursion (goto-char pos) (current-indentation)))
197 (defsubst fuel-syntax--increased-indentation (&optional i)
198 (+ (or i (current-indentation)) factor-indent-width))
199 (defsubst fuel-syntax--decreased-indentation (&optional i)
200 (- (or i (current-indentation)) factor-indent-width))
202 (defsubst fuel-syntax--at-begin-of-def ()
203 (looking-at fuel-syntax--begin-of-def-regex))
205 (defsubst fuel-syntax--at-end-of-def ()
206 (looking-at fuel-syntax--end-of-def-regex))
208 (defsubst fuel-syntax--looking-at-emptiness ()
209 (looking-at "^[ \t]*$"))
211 (defun fuel-syntax--at-setter-line ()
214 (if (not (fuel-syntax--looking-at-emptiness))
215 (re-search-forward fuel-syntax--setter-regex (line-end-position) t)
217 (or (fuel-syntax--at-constructor-line)
218 (fuel-syntax--at-setter-line)))))
220 (defun fuel-syntax--at-constructor-line ()
223 (re-search-forward fuel-syntax--constructor-regex (line-end-position) t)))
225 (defsubst fuel-syntax--at-using ()
226 (looking-at fuel-syntax--using-lines-regex))
228 (defsubst fuel-syntax--beginning-of-defun (&optional times)
229 (re-search-backward fuel-syntax--begin-of-def-regex nil t times))
231 (defsubst fuel-syntax--end-of-defun ()
232 (re-search-forward fuel-syntax--end-of-def-regex nil t))
237 (make-variable-buffer-local
238 (defvar fuel-syntax--current-vocab nil))
240 (make-variable-buffer-local
241 (defvar fuel-syntax--usings nil))
243 (defun fuel-syntax--current-vocab ()
246 (when (re-search-backward fuel-syntax--current-vocab-regex nil t)
247 (setq fuel-syntax--current-vocab (match-string-no-properties 1))
250 (let ((pp (save-excursion
251 (when (re-search-backward fuel-syntax--sub-vocab-regex ip t)
253 (when (and pp (> pp ip))
254 (let ((sub (match-string-no-properties 1)))
255 (unless (save-excursion (search-backward (format "%s>" sub) pp t))
256 (setq fuel-syntax--current-vocab
257 (format "%s.%s" fuel-syntax--current-vocab (downcase sub)))))))))
258 fuel-syntax--current-vocab)
260 (defun fuel-syntax--usings-update ()
262 (setq fuel-syntax--usings (list (fuel-syntax--current-vocab)))
263 (while (re-search-backward fuel-syntax--using-lines-regex nil t)
264 (dolist (u (split-string (match-string-no-properties 1) nil t))
265 (push u fuel-syntax--usings)))
266 fuel-syntax--usings))
268 (defsubst fuel-syntax--usings-update-hook ()
269 (fuel-syntax--usings-update)
272 (defun fuel-syntax--enable-usings ()
273 (add-hook 'before-save-hook 'fuel-syntax--usings-update-hook nil t)
274 (fuel-syntax--usings-update))
276 (defsubst fuel-syntax--usings ()
277 (or fuel-syntax--usings (fuel-syntax--usings-update)))
280 (provide 'fuel-syntax)
281 ;;; fuel-syntax.el ends here