say where a reader error was met
Every later stage's error says file:line, and the reader's said only what; an unclosed list, met at the end of the file, now names the line it opened on as well.
This commit is contained in:
28
reader.scm
28
reader.scm
@@ -16,6 +16,7 @@
|
||||
scheme
|
||||
(scheme base) ; make-parameter
|
||||
(chicken base)
|
||||
(chicken condition)
|
||||
(chicken irregex)
|
||||
(chicken module)
|
||||
(chicken pathname)
|
||||
@@ -99,11 +100,11 @@
|
||||
((char=? c #\()
|
||||
(let ((line (current-line)))
|
||||
(get-ch port)
|
||||
(stamp (read-list port close-paren) line)))
|
||||
(stamp (read-list port close-paren line) line)))
|
||||
((char=? c #\[)
|
||||
(let ((line (current-line)))
|
||||
(get-ch port)
|
||||
(stamp (cons '¤ (read-list port close-bracket)) line)))
|
||||
(stamp (cons '¤ (read-list port close-bracket line)) line)))
|
||||
((char=? c #\)) (get-ch port) close-paren)
|
||||
((char=? c #\]) (get-ch port) close-bracket)
|
||||
((char=? c #\;)
|
||||
@@ -148,11 +149,14 @@
|
||||
|
||||
;;; Read list elements up to close-paren or close-bracket,
|
||||
;;; honoring dotted-pair notation (a b . c)
|
||||
(define (read-list port closer)
|
||||
(define (read-list port closer opened)
|
||||
(let loop ((acc (list)))
|
||||
(let ((tok (next-token port)))
|
||||
(cond
|
||||
((eof-object? tok) (error "Unexpected end of input inside list"))
|
||||
((eof-object? tok)
|
||||
(error (string-append
|
||||
"Unexpected end of input inside a list opened on line "
|
||||
(number->string opened))))
|
||||
((eq? tok close-paren)
|
||||
(if (eq? closer close-paren)
|
||||
(reverse acc)
|
||||
@@ -357,7 +361,7 @@
|
||||
((or (char=? c #\t) (char=? c #\T)) (read-bool port #t))
|
||||
((or (char=? c #\f) (char=? c #\F)) (read-bool port #f))
|
||||
((char=? c #\\) (read-char-lit port))
|
||||
((char=? c #\() (list->vector (read-list port close-paren)))
|
||||
((char=? c #\() (list->vector (read-list port close-paren (current-line))))
|
||||
((memv c '(#\x #\X)) (read-radix port 16))
|
||||
((memv c '(#\b #\B)) (read-radix port 2))
|
||||
((memv c '(#\o #\O)) (read-radix port 8))
|
||||
@@ -546,9 +550,19 @@
|
||||
(else (skip-block-comment port depth))))))
|
||||
|
||||
;;; Read every top-level form from `port'. Locations are recorded by
|
||||
;;; next-token, for every form rather than only these
|
||||
;;; next-token, for every form rather than only these. An error says
|
||||
;;; where it was met, the way a later stage's says where its form was
|
||||
;;; written: file:line: what
|
||||
(define (parse-all port)
|
||||
(parameterize ((current-line 1))
|
||||
(handle-exceptions e
|
||||
(let ((message ((condition-property-accessor 'exn 'message #f) e)))
|
||||
(if message
|
||||
(apply error
|
||||
(string-append (current-source-file) ":"
|
||||
(number->string (current-line)) ": " message)
|
||||
((condition-property-accessor 'exn 'arguments (list)) e))
|
||||
(abort e)))
|
||||
(let loop ((acc (list)))
|
||||
(skip-whitespace port)
|
||||
(let ((tok (next-token port)))
|
||||
@@ -560,7 +574,7 @@
|
||||
((eq? tok dot-token)
|
||||
(error "Unexpected . at top level"))
|
||||
(else
|
||||
(loop (cons tok acc))))))))
|
||||
(loop (cons tok acc)))))))))
|
||||
|
||||
;;; Entry point: read all forms from a file, or from the current input
|
||||
;;; port when the source is 'stdin
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
(import (chicken condition)
|
||||
(chicken irregex)
|
||||
(chicken port)
|
||||
reader)
|
||||
|
||||
@@ -10,12 +11,18 @@
|
||||
(with-input-from-string string
|
||||
(lambda () (read-raw-forms 'stdin))))))))
|
||||
|
||||
(define (read-error string)
|
||||
"The message of the error reading STRING raises, or #f if it reads."
|
||||
(define (read-error-at string)
|
||||
"The message of the error reading STRING raises, where included, or
|
||||
#f if it reads."
|
||||
(handle-exceptions e ((condition-property-accessor 'exn 'message) e)
|
||||
(with-input-from-string string (lambda () (read-raw-forms 'stdin)))
|
||||
#f))
|
||||
|
||||
(define (read-error string)
|
||||
"The message of the error reading STRING raises, or #f if it reads."
|
||||
(let ((message (read-error-at string)))
|
||||
(and message (irregex-replace "^[^:]*:[0-9]+: " message ""))))
|
||||
|
||||
(define-syntax reader-test
|
||||
(syntax-rules ()
|
||||
((reader-test result string)
|
||||
@@ -123,6 +130,15 @@
|
||||
(test "Latin-1 is no exception" #t (string? (read-error "#\\é")))
|
||||
(reader-test '(#\a #\~ #\space) "#\\a #\\~ #\\space")
|
||||
|
||||
;; A reader error said what and not where, which the error of every
|
||||
;; later stage does
|
||||
(test "a reader error says where"
|
||||
"stdin:2: unknown escape"
|
||||
(read-error-at "(a)\n\"\\q\""))
|
||||
(test "an unclosed list, where it opened"
|
||||
"stdin:3: Unexpected end of input inside a list opened on line 2"
|
||||
(read-error-at "(a)\n(b\n (c)"))
|
||||
|
||||
;; `;' comments are preserved as (comment "...") forms
|
||||
(reader-test '((comment " hi")) "; hi")
|
||||
(reader-test '((comment ";; Prototypes")) ";;; Prototypes")
|
||||
|
||||
Reference in New Issue
Block a user