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:
2026-10-19 10:23:59 +03:00
parent 1e1f7bbce8
commit 4a54e6773c
2 changed files with 50 additions and 20 deletions

View File

@@ -16,6 +16,7 @@
scheme
(scheme base) ; make-parameter
(chicken base)
(chicken condition)
(chicken irregex)
(chicken module)
(chicken pathname)
@@ -102,11 +103,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 #\;)
@@ -151,11 +152,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)
@@ -360,7 +364,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))
@@ -549,21 +553,31 @@
(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))
(let loop ((acc (list)))
(skip-whitespace port)
(let ((tok (next-token port)))
(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))))))))
(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)))
(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
;;; port when the source is 'stdin

View File

@@ -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")