1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-12 06:41:13 +02:00

throw SyntaxError on bad syntax

* module/language/ecmascript/parse.scm (syntax-error):
* module/language/ecmascript/tokenize.scm (syntax-error): Throw an error
  on bad syntax.
This commit is contained in:
Andy Wingo 2009-02-22 11:20:15 +01:00
parent af6c20b731
commit 0b229e81a7
2 changed files with 26 additions and 20 deletions

View file

@ -24,11 +24,14 @@
#:use-module (language ecmascript tokenize) #:use-module (language ecmascript tokenize)
#:export (read-ecmascript read-ecmascript/1 parse-ecmascript)) #:export (read-ecmascript read-ecmascript/1 parse-ecmascript))
(define (syntax-error message . args)
(apply throw 'SyntaxError message args))
(define (read-ecmascript port) (define (read-ecmascript port)
(parse-ecmascript (make-tokenizer port) pk)) (parse-ecmascript (make-tokenizer port) syntax-error))
(define (read-ecmascript/1 port) (define (read-ecmascript/1 port)
(parse-ecmascript (make-tokenizer/1 port) pk)) (parse-ecmascript (make-tokenizer/1 port) syntax-error))
(define *eof-object* (define *eof-object*
(call-with-input-string "" read-char)) (call-with-input-string "" read-char))

View file

@ -24,13 +24,16 @@
#:use-module ((srfi srfi-1) #:select (unfold-right)) #:use-module ((srfi srfi-1) #:select (unfold-right))
#:export (next-token make-tokenizer make-tokenizer/1 tokenize tokenize/1)) #:export (next-token make-tokenizer make-tokenizer/1 tokenize tokenize/1))
(define (syntax-error message . args)
(apply throw 'SyntaxError message args))
;; taken from SSAX, sorta ;; taken from SSAX, sorta
(define (read-until delims port) (define (read-until delims port)
(if (eof-object? (peek-char port)) (if (eof-object? (peek-char port))
(error "EOF while reading a token") (syntax-error "EOF while reading a token")
(let ((token (read-delimited delims port 'peek))) (let ((token (read-delimited delims port 'peek)))
(if (eof-object? (peek-char port)) (if (eof-object? (peek-char port))
(error "EOF while reading a token") (syntax-error "EOF while reading a token")
token)))) token))))
(define (char-hex? c) (define (char-hex? c)
@ -61,7 +64,7 @@
(read-char port) (read-char port)
(let lp ((c (read-char port))) (let lp ((c (read-char port)))
(cond (cond
((eof-object? c) (error "EOF while in multi-line comment")) ((eof-object? c) (syntax-error "EOF while in multi-line comment"))
((char=? c #\*) ((char=? c #\*)
(if (eqv? (peek-char port) #\/) (if (eqv? (peek-char port) #\/)
(begin (begin
@ -101,7 +104,7 @@
(lp (read-until terms port) (lp (read-until terms port)
(string-append head str (string #\\ echar))))) (string-append head str (string #\\ echar)))))
(else (else
(error "regexp literals may not contain newlines" str))))))) (syntax-error "regexp literals may not contain newlines" str)))))))
(define (read-string port) (define (read-string port)
(let ((c (read-char port))) (let ((c (read-char port)))
@ -120,7 +123,7 @@
(let ((next (peek-char port))) (let ((next (peek-char port)))
(cond ((eof-object? next) #\nul) (cond ((eof-object? next) #\nul)
((char-numeric? next) ((char-numeric? next)
(error "octal escape sequences are not supported")) (syntax-error "octal escape sequences are not supported"))
(else #\nul)))) (else #\nul))))
((#\x) ((#\x)
(let* ((a (read-char port)) (let* ((a (read-char port))
@ -129,9 +132,9 @@
((and (char-hex? a) (char-hex? b)) ((and (char-hex? a) (char-hex? b))
(integer->char (+ (* 16 (hex->number a)) (hex->number b)))) (integer->char (+ (* 16 (hex->number a)) (hex->number b))))
(else (else
(error "bad hex character escape" a b))))) (syntax-error "bad hex character escape" a b)))))
((#\u) ((#\u)
(error "unicode not supported")) (syntax-error "unicode not supported"))
(else (else
c)))) c))))
(let lp ((str (read-until terms port))) (let lp ((str (read-until terms port)))
@ -146,7 +149,7 @@
(lp (string-append str (string echar) (lp (string-append str (string echar)
(read-until terms port))))) (read-until terms port)))))
(else (else
(error "string literals may not contain newlines" str)))))))) (syntax-error "string literals may not contain newlines" str))))))))
(define *keywords* (define *keywords*
'(("break" . break) '(("break" . break)
@ -224,7 +227,7 @@
(cond ((assoc-ref *keywords* word) (cond ((assoc-ref *keywords* word)
=> (lambda (x) `(,x . #f))) => (lambda (x) `(,x . #f)))
((assoc-ref *future-reserved-words* word) ((assoc-ref *future-reserved-words* word)
(error "word is reserved for the future, dude." word)) (syntax-error "word is reserved for the future, dude." word))
(else `(Identifier . ,(string->symbol word))))) (else `(Identifier . ,(string->symbol word)))))
(begin (read-char port) (begin (read-char port)
(lp (peek-char port) (cons c chars)))))) (lp (peek-char port) (cons c chars))))))
@ -240,7 +243,7 @@
(read-char port) (read-char port)
(let ((c (peek-char port))) (let ((c (peek-char port)))
(if (not (char-hex? c)) (if (not (char-hex? c))
(error "bad digit reading hexadecimal number" c)) (syntax-error "bad digit reading hexadecimal number" c))
(let lp ((c c) (acc 0)) (let lp ((c c) (acc 0))
(cond ((char-hex? c) (cond ((char-hex? c)
(read-char port) (read-char port)
@ -253,7 +256,7 @@
(cond ((eof-object? c) acc) (cond ((eof-object? c) acc)
((char-numeric? c) ((char-numeric? c)
(if (or (char=? c #\8) (char=? c #\9)) (if (or (char=? c #\8) (char=? c #\9))
(error "invalid digit in octal sequence" c)) (syntax-error "invalid digit in octal sequence" c))
(read-char port) (read-char port)
(lp (peek-char port) (lp (peek-char port)
(+ (* 8 acc) (digit->number c)))) (+ (* 8 acc) (digit->number c))))
@ -270,12 +273,12 @@
((or (char=? c1 #\e) (char=? c1 #\E)) ((or (char=? c1 #\e) (char=? c1 #\E))
(read-char port) (read-char port)
(let ((add (let ((c (peek-char port))) (let ((add (let ((c (peek-char port)))
(cond ((eof-object? c) (error "error reading exponent: EOF")) (cond ((eof-object? c) (syntax-error "error reading exponent: EOF"))
((char=? c #\+) (read-char port) +) ((char=? c #\+) (read-char port) +)
((char=? c #\-) (read-char port) -) ((char=? c #\-) (read-char port) -)
((char-numeric? c) +) ((char-numeric? c) +)
(else (error "error reading exponent: non-digit" (else (syntax-error "error reading exponent: non-digit"
c)))))) c))))))
(let lp ((c (peek-char port)) (e 0)) (let lp ((c (peek-char port)) (e 0))
(cond ((and (not (eof-object? c)) (char-numeric? c)) (cond ((and (not (eof-object? c)) (char-numeric? c))
(read-char port) (read-char port)
@ -375,7 +378,7 @@
(candidate (candidate
`(,candidate . #f)) `(,candidate . #f))
(else (else
(error "bad syntax: character not allowed" c))))))) (syntax-error "bad syntax: character not allowed" c)))))))
(define (next-token port div?) (define (next-token port div?)
(let ((c (peek-char port))) (let ((c (peek-char port)))
@ -431,19 +434,19 @@
((rparen) ((rparen)
(if (and (pair? stack) (eq? (car stack) 'lparen)) (if (and (pair? stack) (eq? (car stack) 'lparen))
(set! stack (cdr stack)) (set! stack (cdr stack))
(error "unexpected right parenthesis"))) (syntax-error "unexpected right parenthesis")))
((lbracket) ((lbracket)
(set! stack (cons 'lbracket stack))) (set! stack (cons 'lbracket stack)))
((rbracket) ((rbracket)
(if (and (pair? stack) (eq? (car stack) 'lbracket)) (if (and (pair? stack) (eq? (car stack) 'lbracket))
(set! stack (cdr stack)) (set! stack (cdr stack))
(error "unexpected right bracket" stack))) (syntax-error "unexpected right bracket" stack)))
((lbrace) ((lbrace)
(set! stack (cons 'lbrace stack))) (set! stack (cons 'lbrace stack)))
((rbrace) ((rbrace)
(if (and (pair? stack) (eq? (car stack) 'lbrace)) (if (and (pair? stack) (eq? (car stack) 'lbrace))
(set! stack (cdr stack)) (set! stack (cdr stack))
(error "unexpected right brace" stack))) (syntax-error "unexpected right brace" stack)))
((semicolon) ((semicolon)
(set! eoi? (null? stack)))) (set! eoi? (null? stack))))
(set! div? (and (pair? tok) (eq? (car tok) 'identifier))) (set! div? (and (pair? tok) (eq? (car tok) 'identifier)))