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:
50
reader.scm
50
reader.scm
@@ -16,6 +16,7 @@
|
|||||||
scheme
|
scheme
|
||||||
(scheme base) ; make-parameter
|
(scheme base) ; make-parameter
|
||||||
(chicken base)
|
(chicken base)
|
||||||
|
(chicken condition)
|
||||||
(chicken irregex)
|
(chicken irregex)
|
||||||
(chicken module)
|
(chicken module)
|
||||||
(chicken pathname)
|
(chicken pathname)
|
||||||
@@ -99,11 +100,11 @@
|
|||||||
((char=? c #\()
|
((char=? c #\()
|
||||||
(let ((line (current-line)))
|
(let ((line (current-line)))
|
||||||
(get-ch port)
|
(get-ch port)
|
||||||
(stamp (read-list port close-paren) line)))
|
(stamp (read-list port close-paren line) line)))
|
||||||
((char=? c #\[)
|
((char=? c #\[)
|
||||||
(let ((line (current-line)))
|
(let ((line (current-line)))
|
||||||
(get-ch port)
|
(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-paren)
|
||||||
((char=? c #\]) (get-ch port) close-bracket)
|
((char=? c #\]) (get-ch port) close-bracket)
|
||||||
((char=? c #\;)
|
((char=? c #\;)
|
||||||
@@ -148,11 +149,14 @@
|
|||||||
|
|
||||||
;;; Read list elements up to close-paren or close-bracket,
|
;;; Read list elements up to close-paren or close-bracket,
|
||||||
;;; honoring dotted-pair notation (a b . c)
|
;;; honoring dotted-pair notation (a b . c)
|
||||||
(define (read-list port closer)
|
(define (read-list port closer opened)
|
||||||
(let loop ((acc (list)))
|
(let loop ((acc (list)))
|
||||||
(let ((tok (next-token port)))
|
(let ((tok (next-token port)))
|
||||||
(cond
|
(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)
|
((eq? tok close-paren)
|
||||||
(if (eq? closer close-paren)
|
(if (eq? closer close-paren)
|
||||||
(reverse acc)
|
(reverse acc)
|
||||||
@@ -357,7 +361,7 @@
|
|||||||
((or (char=? c #\t) (char=? c #\T)) (read-bool port #t))
|
((or (char=? c #\t) (char=? c #\T)) (read-bool port #t))
|
||||||
((or (char=? c #\f) (char=? c #\F)) (read-bool port #f))
|
((or (char=? c #\f) (char=? c #\F)) (read-bool port #f))
|
||||||
((char=? c #\\) (read-char-lit port))
|
((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 '(#\x #\X)) (read-radix port 16))
|
||||||
((memv c '(#\b #\B)) (read-radix port 2))
|
((memv c '(#\b #\B)) (read-radix port 2))
|
||||||
((memv c '(#\o #\O)) (read-radix port 8))
|
((memv c '(#\o #\O)) (read-radix port 8))
|
||||||
@@ -546,21 +550,31 @@
|
|||||||
(else (skip-block-comment port depth))))))
|
(else (skip-block-comment port depth))))))
|
||||||
|
|
||||||
;;; Read every top-level form from `port'. Locations are recorded by
|
;;; 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)
|
(define (parse-all port)
|
||||||
(parameterize ((current-line 1))
|
(parameterize ((current-line 1))
|
||||||
(let loop ((acc (list)))
|
(handle-exceptions e
|
||||||
(skip-whitespace port)
|
(let ((message ((condition-property-accessor 'exn 'message #f) e)))
|
||||||
(let ((tok (next-token port)))
|
(if message
|
||||||
(cond
|
(apply error
|
||||||
((eof-object? tok) (reverse acc))
|
(string-append (current-source-file) ":"
|
||||||
((or (eq? tok close-paren)
|
(number->string (current-line)) ": " message)
|
||||||
(eq? tok close-bracket))
|
((condition-property-accessor 'exn 'arguments (list)) e))
|
||||||
(error "Unmatched closing bracket at top level"))
|
(abort e)))
|
||||||
((eq? tok dot-token)
|
(let loop ((acc (list)))
|
||||||
(error "Unexpected . at top level"))
|
(skip-whitespace port)
|
||||||
(else
|
(let ((tok (next-token port)))
|
||||||
(loop (cons tok acc))))))))
|
(cond
|
||||||
|
((eof-object? tok) (reverse acc))
|
||||||
|
((or (eq? tok close-paren)
|
||||||
|
(eq? tok close-bracket))
|
||||||
|
(error "Unmatched closing bracket at top level"))
|
||||||
|
((eq? tok dot-token)
|
||||||
|
(error "Unexpected . at top level"))
|
||||||
|
(else
|
||||||
|
(loop (cons tok acc)))))))))
|
||||||
|
|
||||||
;;; Entry point: read all forms from a file, or from the current input
|
;;; Entry point: read all forms from a file, or from the current input
|
||||||
;;; port when the source is 'stdin
|
;;; port when the source is 'stdin
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
(import (chicken condition)
|
(import (chicken condition)
|
||||||
|
(chicken irregex)
|
||||||
(chicken port)
|
(chicken port)
|
||||||
reader)
|
reader)
|
||||||
|
|
||||||
@@ -10,12 +11,18 @@
|
|||||||
(with-input-from-string string
|
(with-input-from-string string
|
||||||
(lambda () (read-raw-forms 'stdin))))))))
|
(lambda () (read-raw-forms 'stdin))))))))
|
||||||
|
|
||||||
(define (read-error string)
|
(define (read-error-at string)
|
||||||
"The message of the error reading STRING raises, or #f if it reads."
|
"The message of the error reading STRING raises, where included, or
|
||||||
|
#f if it reads."
|
||||||
(handle-exceptions e ((condition-property-accessor 'exn 'message) e)
|
(handle-exceptions e ((condition-property-accessor 'exn 'message) e)
|
||||||
(with-input-from-string string (lambda () (read-raw-forms 'stdin)))
|
(with-input-from-string string (lambda () (read-raw-forms 'stdin)))
|
||||||
#f))
|
#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
|
(define-syntax reader-test
|
||||||
(syntax-rules ()
|
(syntax-rules ()
|
||||||
((reader-test result string)
|
((reader-test result string)
|
||||||
@@ -123,6 +130,15 @@
|
|||||||
(test "Latin-1 is no exception" #t (string? (read-error "#\\é")))
|
(test "Latin-1 is no exception" #t (string? (read-error "#\\é")))
|
||||||
(reader-test '(#\a #\~ #\space) "#\\a #\\~ #\\space")
|
(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
|
;; `;' comments are preserved as (comment "...") forms
|
||||||
(reader-test '((comment " hi")) "; hi")
|
(reader-test '((comment " hi")) "; hi")
|
||||||
(reader-test '((comment ";; Prototypes")) ";;; Prototypes")
|
(reader-test '((comment ";; Prototypes")) ";;; Prototypes")
|
||||||
|
|||||||
Reference in New Issue
Block a user