From 5fca236834b77764c548d7ce049dbbbc1065c68f Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 31 Aug 2017 12:44:41 -0400 Subject: [PATCH] birth: Include pos and window in lexing error Analog to prelude.js's `Parser#_error'. * build-aux/bootstrap/birth.scm (parse-error): Add procedure. (lex): Use it for string error. --- build-aux/bootstrap/birth.scm | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/build-aux/bootstrap/birth.scm b/build-aux/bootstrap/birth.scm index 90b9b7f..d991042 100644 --- a/build-aux/bootstrap/birth.scm +++ b/build-aux/bootstrap/birth.scm @@ -85,7 +85,8 @@ ;; quotes, which can be escaped with a backslash (("\"") (let ((str (js:match-regexp "^\"(|.*?[^\\\\])\"" trim))) - (or str (error "missing closing string delimiter" str)) + (or str (parse-error + src pos "missing closing string delimiter")) ;; a string token consists of the entire string ;; including quotes as its lexeme, but its value will ;; be the value of the string without quotes due to @@ -100,6 +101,17 @@ (token "symbol" symbol trim newpos)))))))) +;; Throw an error with a window of surrounding source code. +;; +;; The "window" is simply ten characters to the left and right of the +;; first character of the source input SRC that resulted in the error. +;; It's a little more than useless. +(define (parse-error src pos msg) + (let ((window (substring src (- pos 10) (+ pos 10)))) + (error (string-append msg " (pos " pos "): " window) + src))) + + ;; Produce a token and recurse. ;; ;; The token will be concatenated with the result of the mutually