1
0
forked from alex-eg/sex

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 e0ecde6517
commit 4fa8a021de
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)
@@ -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,21 +550,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