Added file to scanner and scanner-error for error reporting.

Pascal J. Bourguignon [2015-07-23 15:43]
Added file to scanner and scanner-error for error reporting.
Filename
common-lisp/parser/generate-scanner.lisp
common-lisp/parser/packages.lisp
common-lisp/parser/scanner.lisp
diff --git a/common-lisp/parser/generate-scanner.lisp b/common-lisp/parser/generate-scanner.lisp
index 0e44e0a..8538408 100644
--- a/common-lisp/parser/generate-scanner.lisp
+++ b/common-lisp/parser/generate-scanner.lisp
@@ -53,10 +53,14 @@

 (defmethod print-scanner-error ((err scanner-error) stream)
   (declare (stepper disable))
-  (format stream
-          "~&~@[~A:~]~D:~D: ~?~%"
+  (format stream "~&~@[~A:~]~D:~D: ~?~%"
           (let ((source (scanner-source (scanner-error-scanner err))))
-            (unless (stringp source) (ignore-errors (pathname source))))
+            (typecase source
+              (file-stream  (or (ignore-errors (pathname source))
+                                (scanner-error-file err)))
+              (peek-stream  (or (ignore-errors (pathname (peek-stream-stream source)))
+                                (scanner-error-file err)))
+              (otherwise    (scanner-error-file err))))
           (scanner-error-line err)
           (scanner-error-column err)
           (scanner-error-format-control err)
@@ -130,6 +134,7 @@
                    (scanner-column scanner))
         (scan-next-token scanner))
       (error 'unexpected-token-error
+             :file   (scanner-file scanner)
              :line   (scanner-line scanner)
              :column (scanner-column scanner)
              :scanner scanner
@@ -251,6 +256,7 @@
                  ;; Else we have an error:
                  (t
                   (error 'scanner-error-invalid-character
+                         :file   (scanner-file   scanner)
                          :line   (scanner-line   scanner)
                          :column (scanner-column scanner)
                          :state  (scanner-state  scanner)
diff --git a/common-lisp/parser/packages.lisp b/common-lisp/parser/packages.lisp
index fee621b..05a69cd 100644
--- a/common-lisp/parser/packages.lisp
+++ b/common-lisp/parser/packages.lisp
@@ -50,7 +50,7 @@
    "*SPACE*"
    ;; SCANNER:
    "SCANNER" "SCANNER-CURRENT-TOKEN"
-   "SCANNER-SOURCE" "SCANNER-LINE" "SCANNER-COLUMN" "SCANNER-STATE"
+   "SCANNER-SOURCE" "SCANNER-FILE" "SCANNER-LINE" "SCANNER-COLUMN" "SCANNER-STATE"
    "SCANNER-SPACES" "SCANNER-TAB-WIDTH"
    "SCANNER-TOKEN-KIND-PACKAGE"
    "SCANNER-CURRENT-TOKEN"
@@ -69,7 +69,7 @@
    "GENERATE-SCANNER"
    "ADVANCE-LINE"  "READLINE"
    "SCANNER-END-OF-SOURCE-P" "SCANNER-END-OF-LINE-P"
-   "ACCEPT" "PRINT-SCANNER-ERROR" "PRINT-PARSER-ERROR" )
+   "ACCEPT" "PRINT-SCANNER-ERROR" "PRINT-PARSER-ERROR")
   (:documentation
    "
 An abstract scanner class.
diff --git a/common-lisp/parser/scanner.lisp b/common-lisp/parser/scanner.lisp
index b893b0d..b933895 100644
--- a/common-lisp/parser/scanner.lisp
+++ b/common-lisp/parser/scanner.lisp
@@ -111,6 +111,8 @@
 ;; they can change in the scanner object between the condition
 ;; creation and its handling.

+(defgeneric scanner-error-file (error)
+  (:documentation "The file in which the scanner error was detected."))
 (defgeneric scanner-error-line (error)
   (:documentation "The line on which the scanner error was detected."))
 (defgeneric scanner-error-column (error)
@@ -130,15 +132,17 @@
   (:documentation "The invalid character that made the scanner error."))

 (define-condition scanner-error (simple-error)
-  ((line             :initarg :line             :initform 1   :reader scanner-error-line
+  ((file             :initarg :file             :initform "<stdin>"             :reader scanner-error-file
+                     :documentation "The path of the file.")
+   (line             :initarg :line             :initform 1                     :reader scanner-error-line
                      :documentation "The number of the line. First line is line number 1.")
-   (column           :initarg :column           :initform 1   :reader scanner-error-column
+   (column           :initarg :column           :initform 1                     :reader scanner-error-column
                      :documentation "The number of the column.  First column is column number 1.")
-   (state            :initarg :state            :initform 0   :reader scanner-error-state)
-   (current-token    :initarg :current-token    :initform nil :reader scanner-error-current-token)
-   (scanner          :initarg :scanner                        :reader scanner-error-scanner)
-   (format-control   :initarg :format-control   :initform ""  :reader scanner-error-format-control)
-   (format-arguments :initarg :format-arguments :initform '() :reader scanner-error-format-arguments))
+   (state            :initarg :state            :initform 0                     :reader scanner-error-state)
+   (current-token    :initarg :current-token    :initform nil                   :reader scanner-error-current-token)
+   (scanner          :initarg :scanner                                          :reader scanner-error-scanner)
+   (format-control   :initarg :format-control   :initform ""                    :reader scanner-error-format-control)
+   (format-arguments :initarg :format-arguments :initform '()                   :reader scanner-error-format-arguments))
   (:documentation "A scanner error."))


@@ -149,6 +153,8 @@

 (defgeneric scanner-source (scanner)
   (:documentation "The source can be a PEEK-STREAM, a STREAM, or a STRING."))
+(defgeneric scanner-file (scanner)
+  (:documentation "The namestring of the current file being scanned."))
 (defgeneric scanner-line (scanner)
   (:documentation "The number of the current line."))
 (defgeneric scanner-column (scanner)
@@ -171,6 +177,11 @@
                          :reader        scanner-stream
                          :documentation "The source is wrapped into this PEEK-STREAM.
 Subclasses may use scanner-stream to read from the source.")
+   (file                 :initarg       :file
+                         :accessor      scanner-file
+                         :type          string
+                         :initform      "<stdin>"
+                         :documentation "The namestring of the current file.")
    (line                 :initarg       :line
                          :accessor      scanner-line
                          :type          integer
ViewGit