1 ;;; ECMAScript for Guile
3 ;; Copyright (C) 2009, 2010, 2011 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 ecmascript tokenize)
22 #:use-module (ice-9 rdelim)
23 #:use-module ((srfi srfi-1) #:select (unfold-right))
24 #:use-module (system base lalr)
25 #:export (next-token make-tokenizer make-tokenizer/1 tokenize tokenize/1))
27 (define (syntax-error what loc form . args)
28 (throw 'syntax-error #f what
29 (and=> loc source-location->source-properties)
32 (define (port-source-location port)
33 (make-source-location (port-filename port)
36 (false-if-exception (ftell port))
39 ;; taken from SSAX, sorta
40 (define (read-until delims port loc)
41 (if (eof-object? (peek-char port))
42 (syntax-error "EOF while reading a token" loc #f)
43 (let ((token (read-delimited delims port 'peek)))
44 (if (eof-object? (peek-char port))
45 (syntax-error "EOF while reading a token" loc token)
49 (and (not (eof-object? c))
51 (memv c '(#\a #\b #\c #\d #\e #\f))
52 (memv c '(#\A #\B #\C #\D #\E #\F)))))
54 (define (digit->number c)
55 (- (char->integer c) (char->integer #\0)))
57 (define (hex->number c)
60 (+ 10 (- (char->integer (char-downcase c)) (char->integer #\a)))))
62 (define (read-slash port loc div?)
68 ;; hmm. error if we're not looking for a div? ?
69 (make-lexical-token '/ loc #f))
72 (next-token port div?))
75 (let lp ((c (read-char port)))
78 (syntax-error "EOF while in multi-line comment" loc #f))
80 (if (eqv? (peek-char port) #\/)
83 (next-token port div?))
84 (lp (read-char port))))
86 (lp (read-char port))))))
89 ((#\=) (read-char port) (make-lexical-token '/= loc #f))
90 (else (make-lexical-token '/ loc #f))))
92 (read-regexp port loc)))))
94 (define (read-regexp port loc)
95 ;; first slash already read
96 (let ((terms (string #\/ #\\ #\nl #\cr)))
97 (let lp ((str (read-until terms port loc)) (head ""))
98 (let ((terminator (peek-char port)))
100 ((char=? terminator #\/)
103 (let lp ((c (peek-char port)) (flags '()))
104 (if (or (eof-object? c)
105 (not (or (char-alphabetic? c)
109 (make-lexical-token 'RegexpLiteral loc
110 (cons (string-append head str)
112 (begin (read-char port)
113 (lp (peek-char port) (cons c flags))))))
114 ((char=? terminator #\\)
116 (let ((echar (read-char port)))
117 (lp (read-until terms port loc)
118 (string-append head str (string #\\ echar)))))
120 (syntax-error "regexp literals may not contain newlines"
123 (define (read-string port loc)
124 (let ((c (read-char port)))
125 (let ((terms (string c #\\ #\nl #\cr)))
126 (define (read-escape port)
127 (let ((c (read-char port)))
137 (let ((next (peek-char port)))
139 ((eof-object? next) #\nul)
140 ((char-numeric? next)
141 (syntax-error "octal escape sequences are not supported"
145 (let* ((a (read-char port))
146 (b (read-char port)))
148 ((and (char-hex? a) (char-hex? b))
149 (integer->char (+ (* 16 (hex->number a)) (hex->number b))))
151 (syntax-error "bad hex character escape" loc (string a b))))))
153 (let* ((a (read-char port))
156 (d (read-char port)))
157 (integer->char (string->number (string a b c d) 16))))
160 (let lp ((str (read-until terms port loc)))
161 (let ((terminator (peek-char port)))
163 ((char=? terminator c)
165 (make-lexical-token 'StringLiteral loc str))
166 ((char=? terminator #\\)
168 (let ((echar (read-escape port)))
169 (lp (string-append str (string echar)
170 (read-until terms port loc)))))
172 (syntax-error "string literals may not contain newlines"
181 ("finally" . finally)
188 ("continue" . continue)
189 ("function" . function)
192 ("default" . default)
199 ("instanceof" . instanceof)
202 ;; these aren't exactly keywords, but hey
207 (define *future-reserved-words*
208 '(("abstract" . abstract)
212 ("boolean" . boolean)
214 ("interface" . interface)
217 ("extends" . extends)
223 ("synchronized" . synchronized)
226 ("package" . package)
230 ("private" . private)
231 ("transient" . transient)
232 ("debugger" . debugger)
233 ("implements" . implements)
234 ("protected" . protected)
235 ("volatile" . volatile)
238 ("public" . public)))
240 (define (read-identifier port loc)
241 (let lp ((c (peek-char port)) (chars '()))
242 (if (or (eof-object? c)
243 (not (or (char-alphabetic? c)
247 (let ((word (list->string (reverse chars))))
248 (cond ((assoc-ref *keywords* word)
249 => (lambda (x) (make-lexical-token x loc #f)))
250 ((assoc-ref *future-reserved-words* word)
251 (syntax-error "word is reserved for the future, dude."
253 (else (make-lexical-token 'Identifier loc
254 (string->symbol word)))))
255 (begin (read-char port)
256 (lp (peek-char port) (cons c chars))))))
258 (define (read-numeric port loc)
259 (let* ((c0 (if (char=? (peek-char port) #\.)
262 (c1 (peek-char port)))
264 ((eof-object? c1) (digit->number c0))
265 ((and (char=? c0 #\0) (or (char=? c1 #\x) (char=? c1 #\X)))
267 (let ((c (peek-char port)))
268 (if (not (char-hex? c))
269 (syntax-error "bad digit reading hexadecimal number"
271 (let lp ((c c) (acc 0))
275 (+ (* 16 acc) (hex->number c))))
278 ((and (char=? c0 #\0) (char-numeric? c1))
279 (let lp ((c c1) (acc 0))
280 (cond ((eof-object? c) acc)
282 (if (or (char=? c #\8) (char=? c #\9))
283 (syntax-error "invalid digit in octal sequence"
287 (+ (* 8 acc) (digit->number c))))
291 (let lp ((c1 c1) (acc (digit->number c0)))
293 ((eof-object? c1) acc)
297 (+ (* 10 acc) (digit->number c1))))
298 ((or (char=? c1 #\e) (char=? c1 #\E))
300 (let ((add (let ((c (peek-char port)))
301 (cond ((eof-object? c)
302 (syntax-error "error reading exponent: EOF"
304 ((char=? c #\+) (read-char port) +)
305 ((char=? c #\-) (read-char port) -)
306 ((char-numeric? c) +)
308 (syntax-error "error reading exponent: non-digit"
310 (let lp ((c (peek-char port)) (e 0))
311 (cond ((and (not (eof-object? c)) (char-numeric? c))
313 (lp (peek-char port) (add (* 10 e) (digit->number c))))
315 (* (if (negative? e) (* acc 1.0) acc) (expt 10 e)))))))
318 (let lp2 ((c (peek-char port)) (dec 0.0) (n -1))
319 (cond ((and (not (eof-object? c)) (char-numeric? c))
321 (lp2 (peek-char port)
322 (+ dec (* (digit->number c) (expt 10 n)))
325 ;; loop back to catch an exponential part
326 (lp c (+ acc dec))))))
330 (define *punctuation*
378 (define *div-punctuation*
382 ;; node ::= (char (symbol | #f) node*)
383 (define read-punctuation
384 (let ((punc-tree (let lp ((nodes '()) (puncs *punctuation*))
387 ((assv-ref nodes (string-ref (caar puncs) 0))
388 => (lambda (node-tail)
389 (if (= (string-length (caar puncs)) 1)
390 (set-car! node-tail (cdar puncs))
393 `((,(substring (caar puncs) 1)
395 (lp nodes (cdr puncs))))
397 (lp (cons (list (string-ref (caar puncs) 0) #f) nodes)
400 (let lp ((c (peek-char port)) (tree punc-tree) (candidate #f))
403 => (lambda (node-tail)
405 (lp (peek-char port) (cdr node-tail) (car node-tail))))
407 (make-lexical-token candidate loc #f))
409 (syntax-error "bad syntax: character not allowed" loc c)))))))
411 (define (next-token port div?)
412 (let ((c (peek-char port))
413 (loc (port-source-location port)))
415 ((#\ht #\vt #\np #\space #\x00A0) ; whitespace
417 (next-token port div?))
418 ((#\newline #\cr) ; line break
420 (next-token port div?))
422 ;; division, single comment, double comment, or regexp
423 (read-slash port loc div?))
424 ((#\" #\') ; string literal
425 (read-string port loc))
430 ((or (char-alphabetic? c)
433 ;; reserved word or identifier
434 (read-identifier port loc))
436 ;; numeric -- also accept . FIXME, requires lookahead
437 (make-lexical-token 'NumericLiteral loc (read-numeric port loc)))
440 (read-punctuation port loc)))))))
442 (define (make-tokenizer port)
445 (let ((tok (next-token port div?)))
446 (set! div? (and (lexical-token? tok)
447 (let ((cat (lexical-token-category tok)))
448 (or (eq? cat 'Identifier)
449 (eq? cat 'NumericLiteral)
450 (eq? cat 'StringLiteral)))))
453 (define (make-tokenizer/1 port)
460 (let ((tok (next-token port div?)))
461 (case (if (lexical-token? tok) (lexical-token-category tok) tok)
463 (set! stack (cons tok stack)))
465 (if (and (pair? stack)
466 (eq? (lexical-token-category (car stack)) 'lparen))
467 (set! stack (cdr stack))
468 (syntax-error "unexpected right parenthesis"
469 (lexical-token-source tok)
472 (set! stack (cons tok stack)))
474 (if (and (pair? stack)
475 (eq? (lexical-token-category (car stack)) 'lbracket))
476 (set! stack (cdr stack))
477 (syntax-error "unexpected right bracket"
478 (lexical-token-source tok)
481 (set! stack (cons tok stack)))
483 (if (and (pair? stack)
484 (eq? (lexical-token-category (car stack)) 'lbrace))
485 (set! stack (cdr stack))
486 (syntax-error "unexpected right brace"
487 (lexical-token-source tok)
490 (set! eoi? (null? stack))))
491 (set! div? (and (lexical-token? tok)
492 (let ((cat (lexical-token-category tok)))
493 (or (eq? cat 'Identifier)
494 (eq? cat 'NumericLiteral)
495 (eq? cat 'StringLiteral)))))
498 (define (tokenize port)
499 (let ((next (make-tokenizer port)))
504 (lp (cons tok out)))))))
506 (define (tokenize/1 port)
507 (let ((next (make-tokenizer/1 port)))
512 (lp (cons tok out)))))))