3 ;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;;; This library is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Lesser General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 3 of the License, or (at your option) any later version.
10 ;;; This library is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Lesser General Public License for more details.
15 ;;; You should have received a copy of the GNU Lesser General Public
16 ;;; License along with this library; if not, write to the Free Software
17 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 (define-module (language elisp lexer)
22 #:use-module (ice-9 regex)
23 #:export (get-lexer get-lexer/1))
25 ;;; This is the lexical analyzer for the elisp reader. It is
26 ;;; hand-written instead of using some generator. I think this is the
27 ;;; best solution because of all that fancy escape sequence handling and
30 ;;; Characters are handled internally as integers representing their
31 ;;; code value. This is necessary because elisp allows a lot of fancy
32 ;;; modifiers that set certain high-range bits and the resulting values
33 ;;; would not fit into a real Scheme character range. Additionally,
34 ;;; elisp wants characters as integers, so we just do the right thing...
36 ;;; TODO: #@count comments
38 ;;; Report an error from the lexer (that is, invalid input given).
40 (define (lexer-error port msg . args)
41 (apply error msg args))
43 ;;; In a character, set a given bit. This is just some bit-wise or'ing
44 ;;; on the characters integer code and converting back to character.
46 (define (set-char-bit chr bit)
47 (logior chr (ash 1 bit)))
49 ;;; Check if a character equals some other. This is just like char=?
50 ;;; except that the tested one could be EOF in which case it simply
53 (define (is-char? tested should-be)
54 (and (not (eof-object? tested))
55 (char=? tested should-be)))
57 ;;; For a character (as integer code), find the real character it
58 ;;; represents or #\nul if out of range. This is used to work with
59 ;;; Scheme character functions like char-numeric?.
61 (define (real-character chr)
66 ;;; Return the control modified version of a character. This is not
67 ;;; just setting a modifier bit, because ASCII conrol characters must be
68 ;;; handled as such, and in elisp C-? is the delete character for
69 ;;; historical reasons. Otherwise, we set bit 26.
71 (define (add-control chr)
72 (let ((real (real-character chr)))
73 (if (char-alphabetic? real)
74 (- (char->integer (char-upcase real)) (char->integer #\@))
78 (else (set-char-bit chr 26))))))
80 ;;; Parse a charcode given in some base, basically octal or hexadecimal
81 ;;; are needed. A requested number of digits can be given (#f means it
82 ;;; does not matter and arbitrary many are allowed), and additionally
83 ;;; early return allowed (if fewer valid digits are found). These
84 ;;; options are all we need to handle the \u, \U, \x and \ddd (octal
85 ;;; digits) escape sequences.
87 (define (charcode-escape port base digits early-return)
88 (let iterate ((result 0)
90 (if (and digits (>= procdigs digits))
92 (let* ((cur (read-char port))
95 (- (char->integer cur) (char->integer #\0)))
96 ((char-alphabetic? cur)
97 (let ((code (- (char->integer (char-upcase cur))
98 (char->integer #\A))))
103 (valid (and value (< value base))))
105 (if (or (not digits) early-return)
107 (unread-char cur port)
110 "invalid digit in escape-code"
113 (iterate (+ (* result base) value) (1+ procdigs)))))))
115 ;;; Read a character and process escape-sequences when necessary. The
116 ;;; special in-string argument defines if this character is part of a
117 ;;; string literal or a single character literal, the difference being
118 ;;; that in strings the meta modifier sets bit 7, while it is bit 27 for
121 (define basic-escape-codes
133 (define (get-character port in-string)
134 (let ((meta-bits `((#\A . 22)
138 (#\M . ,(if in-string 7 27))))
139 (cur (read-char port)))
141 ;; Handle an escape-sequence.
142 (let* ((escaped (read-char port))
143 (esc-code (assq-ref basic-escape-codes escaped))
144 (meta (assq-ref meta-bits escaped)))
146 ;; Meta-check must be before esc-code check because \s- must
147 ;; be recognized as the super-meta modifier if a - follows.
148 ;; If not, it will be caught as \s -> space escape code.
149 ((and meta (is-char? (peek-char port) #\-))
150 (if (not (char=? (read-char port) #\-))
151 (error "expected - after control sequence"))
152 (set-char-bit (get-character port in-string) meta))
153 ;; One of the basic control character escape names?
155 ;; Handle \ddd octal code if it is one.
156 ((and (char>=? escaped #\0) (char<? escaped #\8))
158 (unread-char escaped port)
159 (charcode-escape port 8 3 #t)))
160 ;; Check for some escape-codes directly or otherwise use the
161 ;; escaped character literally.
164 ((#\^) (add-control (get-character port in-string)))
166 (if (is-char? (peek-char port) #\-)
168 (if (not (char=? (read-char port) #\-))
169 (error "expected - after control sequence"))
170 (add-control (get-character port in-string)))
172 ((#\x) (charcode-escape port 16 #f #t))
173 ((#\u) (charcode-escape port 16 4 #f))
174 ((#\U) (charcode-escape port 16 8 #f))
175 (else (char->integer escaped))))))
176 ;; No escape-sequence, just the literal character. But remember
177 ;; to get the code instead!
178 (char->integer cur))))
180 ;;; Read a symbol or number from a port until something follows that
181 ;;; marks the start of a new token (like whitespace or parentheses).
182 ;;; The data read is returned as a string for further conversion to the
183 ;;; correct type, but we also return what this is
184 ;;; (integer/float/symbol). If any escaped character is found, it must
185 ;;; be a symbol. Otherwise we at the end check the result-string
186 ;;; against regular expressions to determine if it is possibly an
187 ;;; integer or a float.
189 (define integer-regex (make-regexp "^[+-]?[0-9]+\\.?$"))
193 "^[+-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)(e[+-]?[0-9]+)?$"))
195 ;;; A dot is also allowed literally, only a single dort alone is parsed
196 ;;; as the 'dot' terminal for dotted lists.
198 (define no-escape-punctuation (string->char-set "-+=*/_~!@$%^&:<>{}?."))
200 (define (get-symbol-or-number port)
201 (let iterate ((result-chars '())
203 (let* ((c (read-char port))
205 (let ((result (list->string
206 (reverse result-chars))))
209 ((and (not had-escape)
210 (regexp-exec integer-regex result))
212 ((and (not had-escape)
213 (regexp-exec float-regex result))
217 (need-no-escape? (lambda (c)
218 (or (char-numeric? c)
221 no-escape-punctuation
224 ((eof-object? c) (finish))
225 ((need-no-escape? c) (iterate (cons c result-chars) had-escape))
226 ((char=? c #\\) (iterate (cons (read-char port) result-chars) #t))
231 ;;; Parse a circular structure marker without the leading # (which was
232 ;;; already read and recognized), that is, a number as identifier and
233 ;;; then either = or #.
235 (define (get-circular-marker port)
238 (let iterate ((result 0))
239 (let ((cur (read-char port)))
240 (if (char-numeric? cur)
241 (let ((val (- (char->integer cur) (char->integer #\0))))
242 (iterate (+ (* result 10) val)))
243 (values result cur)))))
246 ((#\#) `(circular-ref . ,id))
247 ((#\=) `(circular-def . ,id))
248 (else (lexer-error port
249 "invalid circular marker character"
252 ;;; Main lexer routine, which is given a port and does look for the next
256 (let ((return (let ((file (if (file-port? port)
259 (line (1+ (port-line port)))
260 (column (1+ (port-column port))))
261 (lambda (token value)
262 (let ((obj (cons token value)))
263 (set-source-property! obj 'filename file)
264 (set-source-property! obj 'line line)
265 (set-source-property! obj 'column column)
267 ;; Read afterwards so the source-properties are correct above
268 ;; and actually point to the very character to be read.
269 (c (read-char port)))
271 ;; End of input must be specially marked to the parser.
272 ((eof-object? c) (return 'eof c))
273 ;; Whitespace, just skip it.
274 ((char-whitespace? c) (lex port))
275 ;; The dot is only the one for dotted lists if followed by
276 ;; whitespace. Otherwise it is considered part of a number of
279 (char-whitespace? (peek-char port)))
281 ;; Continue checking for literal character values.
284 ;; A line comment, skip until end-of-line is found.
287 (let ((cur (read-char port)))
288 (if (or (eof-object? cur) (char=? cur #\newline))
291 ;; A character literal.
293 (return 'character (get-character port #f)))
294 ;; A literal string. This is mainly a sequence of characters
295 ;; just as in the character literals, the only difference is
296 ;; that escaped newline and space are to be completely ignored
297 ;; and that meta-escapes set bit 7 rather than bit 27.
299 (let iterate ((result-chars '()))
300 (let ((cur (read-char port)))
303 (return 'string (list->string (reverse result-chars))))
305 (let ((escaped (read-char port)))
308 (iterate result-chars))
310 (unread-char escaped port)
311 (unread-char cur port)
313 (cons (integer->char (get-character port #t))
315 (else (iterate (cons cur result-chars)))))))
317 (let ((c (read-char port)))
319 ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
321 (let ((mark (get-circular-marker port)))
322 (return (car mark) (cdr mark))))
324 (return 'function #f)))))
325 ;; Parentheses and other special-meaning single characters.
326 ((#\() (return 'paren-open #f))
327 ((#\)) (return 'paren-close #f))
328 ((#\[) (return 'square-open #f))
329 ((#\]) (return 'square-close #f))
330 ((#\') (return 'quote #f))
331 ((#\`) (return 'backquote #f))
332 ;; Unquote and unquote-splicing.
334 (if (is-char? (peek-char port) #\@)
335 (if (not (char=? (read-char port) #\@))
336 (error "expected @ in unquote-splicing")
337 (return 'unquote-splicing #f))
338 (return 'unquote #f)))
339 ;; Remaining are numbers and symbols. Process input until next
340 ;; whitespace is found, and see if it looks like a number
341 ;; (float/integer) or symbol and return accordingly.
345 (lambda () (get-symbol-or-number port))
349 ;; str could be empty if the first character is already
350 ;; something not allowed in a symbol (and not escaped)!
351 ;; Take care about that, it is an error because that
352 ;; character should have been handled elsewhere or is
353 ;; invalid in the input.
354 (if (zero? (string-length str))
356 ;; Take it out so the REPL might not get into an
357 ;; infinite loop with further reading attempts.
359 (error "invalid character in input" c))
360 (return 'symbol (string->symbol str))))
362 ;; In elisp, something like "1." is an integer, while
363 ;; string->number returns an inexact real. Thus we need
364 ;; a conversion here, but it should always result in an
368 (let ((num (inexact->exact (string->number str))))
369 (if (not (integer? num))
370 (error "expected integer" str num))
373 (return 'float (let ((num (string->number str)))
375 (error "expected inexact float"
379 (else (error "wrong number/symbol type" type)))))))))))
381 ;;; Build a lexer thunk for a port. This is the exported routine which
382 ;;; can be used to create a lexer for the parser to use.
384 (define (get-lexer port)
385 (lambda () (lex port)))
387 ;;; Build a special lexer that will only read enough for one expression
388 ;;; and then always return end-of-input. If we find one of the quotation
389 ;;; stuff, one more expression is needed in any case.
391 (define (get-lexer/1 port)
392 (let ((lex (get-lexer port))
397 (cons 'eof ((@ (ice-9 binary-ports) eof-object)))
401 ((paren-open square-open)
402 (set! paren-level (1+ paren-level)))
403 ((paren-close square-close)
404 (set! paren-level (1- paren-level)))
405 ((quote backquote unquote unquote-splicing circular-def)
406 (set! quotation #t)))
407 (if (and (not quotation) (<= paren-level 0))