1 ;;; geiser-syntax.el -- utilities for parsing scheme syntax
3 ;; Copyright (C) 2009, 2010, 2011, 2012 Jose Antonio Ortega Ruiz
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the Modified BSD License. You should
7 ;; have received a copy of the license along with this program. If
8 ;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
10 ;; Start date: Sun Feb 08, 2009 15:03
14 (require 'geiser-impl
)
15 (require 'geiser-popup
)
16 (require 'geiser-base
)
20 (eval-when-compile (require 'cl
))
25 (defmacro geiser-syntax--scheme-indent
(&rest pairs
)
26 `(progn ,@(mapcar (lambda (p)
27 `(put ',(car p
) 'scheme-indent-function
',(cadr p
)))
30 (geiser-syntax--scheme-indent
63 (parameterize-break 1)
71 (syntax-id-rules defun
)
85 (with-error-to-port 1)
89 ;;; Extra syntax keywords
90 (defconst geiser-syntax--keywords
91 '(("\\[\\(else\\)\\>" .
1)
92 ("(\\(parameterize\\)\\>" .
1)))
94 (font-lock-add-keywords 'scheme-mode geiser-syntax--keywords
)
96 (geiser-impl--define-caller geiser-syntax--impl-kws keywords
()
97 "A variable (or thunk returning a value) giving additional,
98 implementation-specific entries for font-lock-keywords.")
100 (defun geiser-syntax--add-kws ()
101 (when (not (and (boundp 'quack-mode
) quack-mode
))
102 (let ((kw (geiser-syntax--impl-kws geiser-impl--implementation
)))
103 (when kw
(font-lock-add-keywords nil kw
)))))
106 ;;; A simple scheme reader
108 (defvar geiser-syntax--read
/buffer-limit nil
)
110 (defsubst geiser-syntax--read
/eos
()
112 (and geiser-syntax--read
/buffer-limit
113 (<= geiser-syntax--read
/buffer-limit
(point)))))
115 (defsubst geiser-syntax--read
/next-char
()
116 (unless (geiser-syntax--read/eos
)
120 (defsubst geiser-syntax--read
/token
(token)
121 (geiser-syntax--read/next-char
)
122 (if (listp token
) token
(list token
)))
124 (defsubst geiser-syntax--read
/elisp
()
125 (ignore-errors (read (current-buffer))))
127 (defun geiser-syntax--read/symbol
()
128 (with-syntax-table scheme-mode-syntax-table
129 (when (re-search-forward "\\(\\sw\\|\\s_\\)+" nil t
)
130 (make-symbol (match-string-no-properties 0)))))
132 (defun geiser-syntax--read/matching
(open close
)
135 (while (and (> count
0)
136 (geiser-syntax--read/next-char
))
137 (cond ((looking-at-p open
) (setq count
(1+ count
)))
138 ((looking-at-p close
) (setq count
(1- count
)))))
139 (buffer-substring-no-properties p
(point))))
141 (defsubst geiser-syntax--read
/unprintable
()
142 (geiser-syntax--read/token
143 (cons 'unprintable
(geiser-syntax--read/matching
"<" ">"))))
145 (defun geiser-syntax--read/skip-comment
()
146 (while (and (geiser-syntax--read/next-char
)
147 (nth 8 (syntax-ppss))))
148 (geiser-syntax--read/next-token
))
150 (defun geiser-syntax--read/next-token
()
151 (skip-syntax-forward "->")
152 (if (geiser-syntax--read/eos
) '(eob)
154 (?\
; (geiser-syntax--read/skip-comment))
155 ((?\
( ?\
[) (geiser-syntax--read/token
'lparen
))
156 ((?\
) ?\
]) (geiser-syntax--read/token
'rparen
))
157 (?.
(if (memq (car (syntax-after (1+ (point)))) '(0 11 12))
158 (geiser-syntax--read/token
'dot
)
159 (cons 'atom
(geiser-syntax--read/elisp
))))
160 (?\
# (case (geiser-syntax--read/next-char
)
162 (?|
(geiser-syntax--read/skip-comment
))
163 (?
: (if (geiser-syntax--read/next-char
)
164 (cons 'kwd
(geiser-syntax--read/symbol
))
166 (?
\\ (cons 'char
(geiser-syntax--read/elisp
)))
167 (?\
( (geiser-syntax--read/token
'vectorb
))
168 (?\
< (geiser-syntax--read/unprintable
))
169 ((?
' ?
` ?
,) (geiser-syntax--read/next-token
))
170 (t (let ((tok (geiser-syntax--read/symbol
)))
171 (cond ((equal (symbol-name tok
) "t") '(boolean .
:t
))
172 ((equal (symbol-name tok
) "f") '(boolean .
:f
))
173 (tok (cons 'atom tok
))
174 (t (geiser-syntax--read/next-token
)))))))
175 (?
\' (geiser-syntax--read/token
'(quote . quote
)))
176 (?\
` (geiser-syntax--read/token
177 `(backquote .
,backquote-backquote-symbol
)))
178 (?
, (if (eq (geiser-syntax--read/next-char
) ?
@)
179 (geiser-syntax--read/token
180 `(splice .
,backquote-splice-symbol
))
181 `(unquote .
,backquote-unquote-symbol
)))
182 (?
\" (cons 'string
(geiser-syntax--read/elisp
)))
183 (t (cons 'atom
(geiser-syntax--read/symbol
))))))
185 (defsubst geiser-syntax--read
/match
(&rest tks
)
186 (let ((token (geiser-syntax--read/next-token
)))
187 (if (memq (car token
) tks
) token
188 (error "Unexpected token: %s" token
))))
190 (defsubst geiser-syntax--read
/skip-until
(&rest tks
)
192 (while (and (not (memq (car token
) tks
))
193 (not (eq (car token
) 'eob
)))
194 (setq token
(geiser-syntax--read/next-token
)))
197 (defsubst geiser-syntax--read
/try
(&rest tks
)
199 (tk (ignore-errors (apply 'geiser-syntax--read
/match tks
))))
200 (unless tk
(goto-char p
))
203 (defun geiser-syntax--read/list
()
204 (cond ((geiser-syntax--read/try
'dot
)
205 (let ((tail (geiser-syntax--read)))
206 (geiser-syntax--read/skip-until
'eob
'rparen
)
208 ((geiser-syntax--read/try
'rparen
'eob
) nil
)
209 (t (cons (geiser-syntax--read)
210 (geiser-syntax--read/list
)))))
212 (defun geiser-syntax--read ()
213 (let ((token (geiser-syntax--read/next-token
))
214 (max-lisp-eval-depth (max max-lisp-eval-depth
3000)))
217 (lparen (geiser-syntax--read/list
))
218 (vectorb (apply 'vector
(geiser-syntax--read/list
)))
219 ((quote backquote unquote splice
) (list (cdr token
)
220 (geiser-syntax--read)))
221 (kwd (make-symbol (format ":%s" (cdr token
))))
222 (unprintable (format "#<%s>" (cdr token
)))
223 ((char string atom
) (cdr token
))
224 (boolean (cdr token
))
225 (t (error "Reading scheme syntax: unexpected token: %s" token
)))))
227 (defun geiser-syntax--read-from-string (string &optional start end
)
228 (when (stringp string
)
229 (let* ((start (or start
0))
230 (end (or end
(length string
)))
231 (max-lisp-eval-depth (max max-lisp-eval-depth
(- end start
))))
233 (save-excursion (insert string
))
234 (cons (ignore-errors (geiser-syntax--read)) (point))))))
236 (defsubst geiser-syntax--form-after-point
(&optional boundary
)
237 (let ((geiser-syntax--read/buffer-limit
(and (numberp boundary
) boundary
)))
238 (save-excursion (values (geiser-syntax--read) (point)))))
240 (defun geiser-syntax--mapconcat (fun lst sep
)
241 (cond ((null lst
) "")
242 ((not (listp lst
)) (format ".%s%s" sep
(funcall fun lst
)))
243 ((null (cdr lst
)) (format "%s" (funcall fun
(car lst
))))
245 (funcall fun
(car lst
))
247 (geiser-syntax--mapconcat fun
(cdr lst
) sep
)))))
249 (defun geiser-syntax--display (a)
250 (cond ((null a
) "()")
253 ((geiser-syntax--keywordp a
) (format "#%s" a
))
254 ((symbolp a
) (format "%s" a
))
255 ((equal a
"...") "...")
256 ((stringp a
) (format "%S" a
))
257 ((and (listp a
) (symbolp (car a
))
258 (equal (symbol-name (car a
)) "quote"))
259 (format "'%s" (geiser-syntax--display (cadr a
))))
262 (geiser-syntax--mapconcat 'geiser-syntax--display a
" ")))
263 (t (format "%s" a
))))
268 (defsubst geiser-syntax--symbol-at-point
()
269 (and (not (nth 8 (syntax-ppss)))
270 (car (geiser-syntax--read-from-string (thing-at-point 'symbol
)))))
272 (defsubst geiser-syntax--skip-comment
/string
()
273 (let ((pos (nth 8 (syntax-ppss))))
274 (goto-char (or pos
(point)))
277 (defsubst geiser-syntax--nesting-level
()
278 (or (nth 0 (syntax-ppss)) 0))
280 (defsubst geiser-syntax--pair-length
(p)
281 (if (cdr (last p
)) (1+ (safe-length p
)) (length p
)))
283 (defun geiser-syntax--shallow-form (boundary)
284 (when (looking-at-p "\\s(")
289 (while (< (point) boundary
)
290 (skip-syntax-forward "-<>")
291 (when (<= (point) boundary
)
293 (let ((s (thing-at-point 'symbol
)))
294 (unless (equal "." s
)
295 (push (car (geiser-syntax--read-from-string s
)) elems
))))))
298 (defsubst geiser-syntax--keywordp
(s)
299 (and s
(symbolp s
) (string-match "^:.+" (symbol-name s
))))
301 (defsubst geiser-syntax--symbol-eq
(s0 s1
)
302 (and (symbolp s0
) (symbolp s1
) (equal (symbol-name s0
) (symbol-name s1
))))
304 (defun geiser-syntax--scan-sexps (&optional begin
)
305 (let* ((fst (geiser-syntax--symbol-at-point))
306 (smth (or fst
(not (looking-at-p "[\s \s)\s>\s<\n]"))))
307 (path (and fst
`((,fst
0)))))
309 (while (not (zerop (geiser-syntax--nesting-level)))
310 (let ((boundary (point)))
311 (geiser-syntax--skip-comment/string
)
313 (let ((form (geiser-syntax--shallow-form boundary
)))
314 (when (and (listp form
) (car form
) (symbolp (car form
)))
315 (let* ((len (geiser-syntax--pair-length form
))
316 (pos (if smth
(1- len
) (progn (setq smth t
) len
)))
317 (prev (and (> pos
1) (nth (1- pos
) form
)))
318 (prev (and (geiser-syntax--keywordp prev
)
320 (push `(,(car form
) ,pos
,@prev
) path
)))))))
322 (cons (substring-no-properties (format "%s" (car e
))) (cdr e
)))
325 (defsubst geiser-syntax--binding-form-p
(bfs sbfs f
)
327 (let ((f (symbol-name f
)))
328 (or (member f
'("define" "define*" "define-syntax"
329 "syntax-rules" "lambda" "case-lambda"
330 "let" "let*" "let-values" "let*-values"
331 "letrec" "letrec*" "parameterize"))
335 (defsubst geiser-syntax--binding-form
*-p
(sbfs f
)
337 (let ((f (symbol-name f
)))
338 (or (member f
'("let*" "let*-values" "letrec" "letrec*"))
341 (defsubst geiser-syntax--if-symbol
(x) (and (symbolp x
) x
))
342 (defsubst geiser-syntax--if-list
(x) (and (listp x
) x
))
344 (defsubst geiser-syntax--normalize
(vars)
346 (let ((i (if (listp i
) (car i
) i
)))
347 (and (symbolp i
) (symbol-name i
))))
350 (defun geiser-syntax--linearize (form)
351 (cond ((not (listp form
)) (list form
))
353 (t (cons (car form
) (geiser-syntax--linearize (cdr form
))))))
355 (defun geiser-syntax--scan-locals (bfs sbfs form nesting locals
)
356 (if (or (null form
) (not (listp form
)))
357 (geiser-syntax--normalize locals
)
358 (if (not (geiser-syntax--binding-form-p bfs sbfs
(car form
)))
359 (geiser-syntax--scan-locals bfs sbfs
362 (let* ((head (car form
))
363 (name (geiser-syntax--if-symbol (cadr form
)))
364 (names (if name
(geiser-syntax--if-list (caddr form
))
365 (geiser-syntax--if-list (cadr form
))))
367 (geiser-syntax--binding-form-p bfs sbfs
(car names
))))
368 (rest (if (and name
(not bns
)) (cdddr form
) (cddr form
)))
369 (use-names (and (or rest
371 (geiser-syntax--binding-form*-p sbfs head
))
373 (when name
(push name locals
))
374 (when (geiser-syntax--symbol-eq head
'case-lambda
)
375 (dolist (n (and (> nesting
0) (caar (last form
))))
376 (when n
(push n locals
)))
377 (setq rest
(and (> nesting
0) (cdr form
)))
378 (setq use-names nil
))
379 (when (geiser-syntax--symbol-eq head
'syntax-rules
)
380 (dolist (n (and (> nesting
0) (cdaar (last form
))))
381 (when n
(push n locals
)))
382 (setq rest
(and (> nesting
0) (cdr form
))))
384 (dolist (n (geiser-syntax--linearize names
))
385 (let ((xs (if (and (listp n
) (listp (car n
))) (car n
) (list n
))))
386 (dolist (x xs
) (when x
(push x locals
))))))
387 (dolist (f (butlast rest
))
389 (geiser-syntax--symbol-eq (car f
) 'define
)
391 (push (cadr f
) locals
)))
392 (geiser-syntax--scan-locals bfs sbfs
393 (car (last (or rest names
)))
397 (defun geiser-syntax--locals-around-point (bfs sbfs
)
398 (when (eq major-mode
'scheme-mode
)
400 (let ((sym (unless (geiser-syntax--skip-comment/string
)
401 (thing-at-point 'symbol
))))
402 (skip-syntax-forward "->")
403 (let ((boundary (point))
404 (nesting (geiser-syntax--nesting-level)))
405 (while (not (zerop (geiser-syntax--nesting-level)))
407 (multiple-value-bind (form end
)
408 (geiser-syntax--form-after-point boundary
)
410 (geiser-syntax--scan-locals bfs
417 ;;; Fontify strings as Scheme code:
419 (defun geiser-syntax--font-lock-buffer ()
420 (let ((name " *geiser font lock*"))
421 (or (get-buffer name
)
422 (let ((buffer (get-buffer-create name
)))
424 (let ((geiser-default-implementation
425 (or geiser-default-implementation
426 (car geiser-active-implementations
))))
430 (defun geiser-syntax--scheme-str (str)
432 (set-buffer (geiser-syntax--font-lock-buffer))
435 (let ((font-lock-verbose nil
)) (font-lock-fontify-buffer))
439 (provide 'geiser-syntax
)