Use windows-1252 encoding for stdin/stdout on Windows
[factor/jcg.git] / misc / fuel / fuel-syntax.el
blob7f3e0c46f5a917614f8001a8137559a07f8750e4
1 ;;; fuel-syntax.el --- auxiliar definitions for factor code navigation.
3 ;; Copyright (C) 2008, 2009 Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
7 ;; Keywords: languages
9 ;;; Commentary:
11 ;; Auxiliar constants and functions to parse factor code.
13 ;;; Code:
15 (require 'thingatpt)
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 (skip-syntax-backward "w_()"))
24 (defsubst fuel-syntax--beginning-of-symbol-pos ()
25 (save-excursion (fuel-syntax--beginning-of-symbol) (point)))
27 (defun fuel-syntax--end-of-symbol ()
28 "Move point to the end of the current symbol."
29 (skip-syntax-forward "w_()"))
31 (defsubst fuel-syntax--end-of-symbol-pos ()
32 (save-excursion (fuel-syntax--end-of-symbol) (point)))
34 (put 'factor-symbol 'end-op 'fuel-syntax--end-of-symbol)
35 (put 'factor-symbol 'beginning-op 'fuel-syntax--beginning-of-symbol)
37 (defsubst fuel-syntax-symbol-at-point ()
38 (let ((s (substring-no-properties (thing-at-point 'factor-symbol))))
39 (and (> (length s) 0) s)))
43 ;;; Regexps galore:
45 (defconst fuel-syntax--parsing-words
46 '(":" "::" ";" "&:" "<<" "<PRIVATE" ">>"
47 "ABOUT:" "ALIAS:" "ALIEN:" "ARTICLE:"
48 "B" "BIN:"
49 "C:" "C-ENUM:" "C-STRUCT:" "C-UNION:" "CHAR:" "CONSTANT:" "call-next-method"
50 "DEFER:"
51 "ERROR:" "EXCLUDE:"
52 "f" "FORGET:" "FROM:" "FUNCTION:"
53 "GENERIC#" "GENERIC:"
54 "HELP:" "HEX:" "HOOK:"
55 "IN:" "initial:" "INSTANCE:" "INTERSECTION:"
56 "LIBRARY:"
57 "M:" "MACRO:" "MACRO::" "MAIN:" "MATH:" "MEMO:" "MEMO:" "METHOD:" "MIXIN:"
58 "OCT:"
59 "POSTPONE:" "PREDICATE:" "PRIMITIVE:" "PRIVATE>" "PROVIDE:"
60 "QUALIFIED-WITH:" "QUALIFIED:"
61 "read-only" "RENAME:" "REQUIRE:" "REQUIRES:"
62 "SINGLETON:" "SINGLETONS:" "SLOT:" "SYMBOL:" "SYMBOLS:"
63 "TUPLE:" "t" "t?" "TYPEDEF:"
64 "UNION:" "USE:" "USING:"
65 "VARS:"))
67 (defconst fuel-syntax--parsing-words-regex
68 (regexp-opt fuel-syntax--parsing-words 'words))
70 (defconst fuel-syntax--bracers
71 '("B" "BV" "C" "CS" "H" "T" "V" "W"))
73 (defconst fuel-syntax--brace-words-regex
74 (format "%s{" (regexp-opt fuel-syntax--bracers t)))
76 (defconst fuel-syntax--declaration-words
77 '("flushable" "foldable" "inline" "parsing" "recursive" "delimiter"))
79 (defconst fuel-syntax--declaration-words-regex
80 (regexp-opt fuel-syntax--declaration-words 'words))
82 (defsubst fuel-syntax--second-word-regex (prefixes)
83 (format "%s +\\([^ \r\n]+\\)" (regexp-opt prefixes t)))
85 (defconst fuel-syntax--method-definition-regex
86 "^M: +\\([^ ]+\\) +\\([^ ]+\\)")
88 (defconst fuel-syntax--integer-regex
89 "\\_<-?[0-9]+\\_>")
91 (defconst fuel-syntax--raw-float-regex
92 "[0-9]*\\.[0-9]*\\([eE][+-]?[0-9]+\\)?")
94 (defconst fuel-syntax--float-regex
95 (format "\\_<-?%s\\_>" fuel-syntax--raw-float-regex))
97 (defconst fuel-syntax--number-regex
98 (format "\\([0-9]+\\|%s\\)" fuel-syntax--raw-float-regex))
100 (defconst fuel-syntax--ratio-regex
101 (format "\\_<[+-]?%s/-?%s\\_>"
102 fuel-syntax--number-regex
103 fuel-syntax--number-regex))
105 (defconst fuel-syntax--bad-string-regex
106 "\\_<\"[^>]\\([^\"\n]\\|\\\\\"\\)*\n")
108 (defconst fuel-syntax--word-definition-regex
109 (fuel-syntax--second-word-regex
110 '(":" "::" "GENERIC:" "DEFER:" "HOOK:" "MAIN:" "MATH:" "POSTPONE:"
111 "SYMBOL:" "RENAME:")))
113 (defconst fuel-syntax--alias-definition-regex
114 "^ALIAS: +\\(\\_<.+?\\_>\\) +\\(\\_<.+?\\_>\\)")
116 (defconst fuel-syntax--vocab-ref-regexp
117 (fuel-syntax--second-word-regex
118 '("IN:" "USE:" "FROM:" "EXCLUDE:" "QUALIFIED:" "QUALIFIED-WITH:")))
120 (defconst fuel-syntax--int-constant-def-regex
121 (fuel-syntax--second-word-regex '("ALIEN:" "CHAR:" "BIN:" "HEX:" "OCT:")))
123 (defconst fuel-syntax--type-definition-regex
124 (fuel-syntax--second-word-regex
125 '("C-STRUCT:" "C-UNION:" "MIXIN:" "TUPLE:" "SINGLETON:" "UNION:")))
127 (defconst fuel-syntax--tuple-decl-regex
128 "^TUPLE: +\\([^ \n]+\\) +< +\\([^ \n]+\\)\\_>")
130 (defconst fuel-syntax--constructor-regex "<[^ >]+>")
132 (defconst fuel-syntax--getter-regex "\\(^\\|\\_<\\)[^ ]+?>>\\_>")
133 (defconst fuel-syntax--setter-regex "\\_<>>.+?\\_>")
135 (defconst fuel-syntax--symbol-definition-regex
136 (fuel-syntax--second-word-regex '("&:" "SYMBOL:" "VAR:")))
138 (defconst fuel-syntax--stack-effect-regex
139 "\\( ( .* )\\)\\|\\( (( .* ))\\)")
141 (defconst fuel-syntax--using-lines-regex "^USING: +\\([^;]+\\);")
143 (defconst fuel-syntax--use-line-regex "^USE: +\\(.*\\)$")
145 (defconst fuel-syntax--current-vocab-regex "^IN: +\\([^ \r\n\f]+\\)")
147 (defconst fuel-syntax--sub-vocab-regex "^<\\([^ \n]+\\) *$")
149 (defconst fuel-syntax--alien-function-regex
150 "\\_<FUNCTION: \\(\\w+\\) \\(\\w+\\)")
152 (defconst fuel-syntax--indent-def-starts '("" ":"
153 "C-ENUM" "C-STRUCT" "C-UNION"
154 "FROM" "FUNCTION:"
155 "INTERSECTION:"
156 "M" "MACRO" "MACRO:"
157 "MEMO" "MEMO:" "METHOD"
158 "PREDICATE" "PRIMITIVE"
159 "UNION"))
161 (defconst fuel-syntax--no-indent-def-starts '("SINGLETONS"
162 "SYMBOLS"
163 "TUPLE"
164 "VARS"))
166 (defconst fuel-syntax--indent-def-start-regex
167 (format "^\\(%s:\\)\\( \\|\n\\)" (regexp-opt fuel-syntax--indent-def-starts)))
169 (defconst fuel-syntax--no-indent-def-start-regex
170 (format "^\\(%s:\\) " (regexp-opt fuel-syntax--no-indent-def-starts)))
172 (defconst fuel-syntax--definition-start-regex
173 (format "^\\(%s:\\) " (regexp-opt (append fuel-syntax--no-indent-def-starts
174 fuel-syntax--indent-def-starts))))
176 (defconst fuel-syntax--definition-end-regex
177 (format "\\(\\(^\\| +\\);\\( *%s\\)*\\($\\| +\\)\\)"
178 fuel-syntax--declaration-words-regex))
180 (defconst fuel-syntax--single-liner-regex
181 (regexp-opt '("ABOUT:"
182 "ARTICLE:"
183 "ALIAS:"
184 "CONSTANT:" "C:"
185 "DEFER:"
186 "FORGET:"
187 "GENERIC:" "GENERIC#"
188 "HELP:" "HEX:" "HOOK:"
189 "IN:" "INSTANCE:"
190 "LIBRARY:"
191 "MAIN:" "MATH:" "MIXIN:"
192 "OCT:"
193 "POSTPONE:" "PRIVATE>" "<PRIVATE"
194 "QUALIFIED-WITH:" "QUALIFIED:"
195 "RENAME:"
196 "SINGLETON:" "SLOT:" "SYMBOL:"
197 "TYPEDEF:"
198 "USE:"
199 "VAR:")))
201 (defconst fuel-syntax--begin-of-def-regex
202 (format "^USING: \\|\\(%s\\)\\|\\(^%s .*\\)"
203 fuel-syntax--definition-start-regex
204 fuel-syntax--single-liner-regex))
206 (defconst fuel-syntax--end-of-def-line-regex
207 (format "^.*%s" fuel-syntax--definition-end-regex))
209 (defconst fuel-syntax--end-of-def-regex
210 (format "\\(%s\\)\\|\\(^%s .*\\)"
211 fuel-syntax--end-of-def-line-regex
212 fuel-syntax--single-liner-regex))
214 (defconst fuel-syntax--defun-signature-regex
215 (format "\\(%s\\|%s\\)"
216 (format ":[^ ]* [^ ]+\\(%s\\)*" fuel-syntax--stack-effect-regex)
217 "M[^:]*: [^ ]+ [^ ]+"))
219 (defconst fuel-syntax--constructor-decl-regex
220 "\\_<C: +\\(\\w+\\) +\\(\\w+\\)\\( .*\\)?$")
222 (defconst fuel-syntax--typedef-regex
223 "\\_<TYPEDEF: +\\(\\w+\\) +\\(\\w+\\)\\( .*\\)?$")
225 (defconst fuel-syntax--rename-regex
226 "\\_<RENAME: +\\(\\w+\\) +\\(\\w+\\) +=> +\\(\\w+\\)\\( .*\\)?$")
229 ;;; Factor syntax table
231 (setq fuel-syntax--syntax-table
232 (let ((table (make-syntax-table)))
233 ;; Default is word constituent
234 (dotimes (i 256)
235 (modify-syntax-entry i "w" table))
237 ;; Whitespace (TAB is not whitespace)
238 (modify-syntax-entry ?\f " " table)
239 (modify-syntax-entry ?\r " " table)
240 (modify-syntax-entry ?\ " " table)
241 (modify-syntax-entry ?\n " " table)
243 ;; Char quote
244 (modify-syntax-entry ?\\ "/" table)
246 table))
248 (defconst fuel-syntax--syntactic-keywords
249 `(;; CHARs:
250 ("\\(CHAR:\\|POSTPONE:\\|\\\\\\) \\(.\\)\\( \\|$\\)" (2 "w"))
251 ;; Comments:
252 ("\\_<\\(#?!\\) .*\\(\n\\|$\\)" (1 "<") (2 ">"))
253 ("\\_<\\(#?!\\)\\(\n\\|$\\)" (1 "<") (2 ">"))
254 (" \\((\\)( \\([^\n]*\\) )\\()\\)\\( \\|\n\\)" (1 "<b") (2 "w") (3 ">b"))
255 (" \\((\\) \\([^\n]*\\) \\()\\)\\( \\|\n\\)" (1 "<b") (2 "w") (3 ">b"))
256 ;; Strings
257 ("\\( \\|^\\)\\(DLL\\|P\\|SBUF\\)\\(\"\\)[^\n\r\f]*?\\(\"\\)\\( \\|\n\\)"
258 (3 "\"") (4 "\""))
259 ("\\(\"\\)[^\n\r\f]*?\\(\"\\)\\( \\|\n\\|$\\)" (1 "\"") (2 "\""))
260 ("\\_<<\\(\"\\)\\_>" (1 "<b"))
261 ("\\_<\\(\"\\)>\\_>" (1 ">b"))
262 ;; Multiline constructs
263 ("\\_<\\(U\\)SING: \\(;\\)" (1 "<b") (2 ">b"))
264 ("\\_<USING:\\( \\)" (1 "<b"))
265 ("\\_<\\(C\\)-ENUM: \\(;\\)" (1 "<b") (2 ">b"))
266 ("\\_<C-ENUM:\\( \\|\n\\)" (1 "<b"))
267 ("\\_<TUPLE: +\\w+? +< +\\w+? *\\( \\|\n\\)\\([^;]\\|$\\)" (1 "<b"))
268 ("\\_<\\(TUPLE\\|SYMBOLS\\|VARS\\): +\\w+? *\\( \\|\n\\)\\([^;<\n]\\|\\_>\\)"
269 (2 "<b"))
270 ("\\(\n\\| \\);\\_>" (1 ">b"))
271 ;; Let and lambda:
272 ("\\_<\\(!(\\) .* \\()\\)" (1 "<") (2 ">"))
273 ("\\(\\[\\)\\(let\\|wlet\\|let\\*\\)\\( \\|$\\)" (1 "(]"))
274 ("\\(\\[\\)\\(|\\) +[^|]* \\(|\\)" (1 "(]") (2 "(|") (3 ")|"))
275 (" \\(|\\) " (1 "(|"))
276 (" \\(|\\)$" (1 ")"))
277 ;; Opening brace words:
278 ("\\_<\\w*\\({\\)\\_>" (1 "(}"))
279 ("\\_<\\(}\\)\\_>" (1 "){"))
280 ;; Parenthesis:
281 ("\\_<\\((\\)\\_>" (1 "()"))
282 ("\\_<\\()\\)\\_>" (1 ")("))
283 ;; Quotations:
284 ("\\_<'\\(\\[\\)\\_>" (1 "(]")) ; fried
285 ("\\_<\\(\\[\\)\\_>" (1 "(]"))
286 ("\\_<\\(\\]\\)\\_>" (1 ")["))))
289 ;;; Source code analysis:
291 (defsubst fuel-syntax--brackets-depth ()
292 (nth 0 (syntax-ppss)))
294 (defsubst fuel-syntax--brackets-start ()
295 (nth 1 (syntax-ppss)))
297 (defun fuel-syntax--brackets-end ()
298 (save-excursion
299 (goto-char (fuel-syntax--brackets-start))
300 (condition-case nil
301 (progn (forward-sexp)
302 (1- (point)))
303 (error -1))))
305 (defsubst fuel-syntax--indentation-at (pos)
306 (save-excursion (goto-char pos) (current-indentation)))
308 (defsubst fuel-syntax--increased-indentation (&optional i)
309 (+ (or i (current-indentation)) factor-indent-width))
310 (defsubst fuel-syntax--decreased-indentation (&optional i)
311 (- (or i (current-indentation)) factor-indent-width))
313 (defsubst fuel-syntax--at-begin-of-def ()
314 (looking-at fuel-syntax--begin-of-def-regex))
316 (defsubst fuel-syntax--at-begin-of-indent-def ()
317 (looking-at fuel-syntax--indent-def-start-regex))
319 (defsubst fuel-syntax--at-end-of-def ()
320 (looking-at fuel-syntax--end-of-def-regex))
322 (defsubst fuel-syntax--looking-at-emptiness ()
323 (looking-at "^[ ]*$\\|$"))
325 (defsubst fuel-syntax--is-last-char (pos)
326 (save-excursion
327 (goto-char (1+ pos))
328 (fuel-syntax--looking-at-emptiness)))
330 (defsubst fuel-syntax--line-offset (pos)
331 (- pos (save-excursion
332 (goto-char pos)
333 (beginning-of-line)
334 (point))))
336 (defun fuel-syntax--previous-non-blank ()
337 (forward-line -1)
338 (while (and (not (bobp)) (fuel-syntax--looking-at-emptiness))
339 (forward-line -1)))
341 (defun fuel-syntax--beginning-of-block-pos ()
342 (save-excursion
343 (if (> (fuel-syntax--brackets-depth) 0)
344 (fuel-syntax--brackets-start)
345 (fuel-syntax--beginning-of-defun)
346 (point))))
348 (defun fuel-syntax--at-setter-line ()
349 (save-excursion
350 (beginning-of-line)
351 (when (re-search-forward fuel-syntax--setter-regex
352 (line-end-position)
354 (let* ((to (match-beginning 0))
355 (from (fuel-syntax--beginning-of-block-pos)))
356 (goto-char from)
357 (let ((depth (fuel-syntax--brackets-depth)))
358 (and (or (re-search-forward fuel-syntax--constructor-regex to t)
359 (re-search-forward fuel-syntax--setter-regex to t))
360 (= depth (fuel-syntax--brackets-depth))))))))
362 (defun fuel-syntax--at-constructor-line ()
363 (save-excursion
364 (beginning-of-line)
365 (re-search-forward fuel-syntax--constructor-regex (line-end-position) t)))
367 (defsubst fuel-syntax--at-using ()
368 (looking-at fuel-syntax--using-lines-regex))
370 (defun fuel-syntax--in-using ()
371 (let ((p (point)))
372 (save-excursion
373 (and (re-search-backward "^USING: " nil t)
374 (re-search-forward " ;" nil t)
375 (< p (match-end 0))))))
377 (defsubst fuel-syntax--beginning-of-defun (&optional times)
378 (re-search-backward fuel-syntax--begin-of-def-regex nil t times))
380 (defsubst fuel-syntax--end-of-defun ()
381 (re-search-forward fuel-syntax--end-of-def-regex nil t))
383 (defsubst fuel-syntax--end-of-defun-pos ()
384 (save-excursion
385 (re-search-forward fuel-syntax--end-of-def-regex nil t)
386 (point)))
388 (defun fuel-syntax--beginning-of-body ()
389 (let ((p (point)))
390 (and (fuel-syntax--beginning-of-defun)
391 (re-search-forward fuel-syntax--defun-signature-regex p t)
392 (not (re-search-forward fuel-syntax--end-of-def-regex p t)))))
394 (defun fuel-syntax--beginning-of-sexp ()
395 (if (> (fuel-syntax--brackets-depth) 0)
396 (goto-char (fuel-syntax--brackets-start))
397 (fuel-syntax--beginning-of-body)))
399 (defsubst fuel-syntax--beginning-of-sexp-pos ()
400 (save-excursion (fuel-syntax--beginning-of-sexp) (point)))
403 ;;; USING/IN:
405 (make-variable-buffer-local
406 (defvar fuel-syntax--current-vocab-function 'fuel-syntax--find-in))
408 (defsubst fuel-syntax--current-vocab ()
409 (funcall fuel-syntax--current-vocab-function))
411 (defun fuel-syntax--find-in ()
412 (save-excursion
413 (when (re-search-backward fuel-syntax--current-vocab-regex nil t)
414 (match-string-no-properties 1))))
416 (make-variable-buffer-local
417 (defvar fuel-syntax--usings-function 'fuel-syntax--find-usings))
419 (defsubst fuel-syntax--usings ()
420 (funcall fuel-syntax--usings-function))
422 (defun fuel-syntax--file-has-private ()
423 (save-excursion
424 (goto-char (point-min))
425 (and (re-search-forward "\\_<<PRIVATE\\_>" nil t)
426 (re-search-forward "\\_<PRIVATE>\\_>" nil t))))
428 (defun fuel-syntax--find-usings (&optional no-private)
429 (save-excursion
430 (let ((usings))
431 (goto-char (point-max))
432 (while (re-search-backward fuel-syntax--using-lines-regex nil t)
433 (dolist (u (split-string (match-string-no-properties 1) nil t))
434 (push u usings)))
435 (when (and (not no-private) (fuel-syntax--file-has-private))
436 (goto-char (point-max))
437 (push (concat (fuel-syntax--find-in) ".private") usings))
438 usings)))
441 (provide 'fuel-syntax)
442 ;;; fuel-syntax.el ends here