Skip to content

Commit

Permalink
Correct span offsets in error messages
Browse files Browse the repository at this point in the history
- add constructors for secondary notes
- move source-related generics to coalton-impl/source (source-available-p, source-name, source-stream)
- rewrite fundep parser
- add span-relative error message options to cursor
- track last consumed element in cursor
- break up source-aware condition reporting function
  • Loading branch information
jbouwman committed Oct 8, 2024
1 parent 67339e2 commit c8e97c2
Show file tree
Hide file tree
Showing 22 changed files with 1,018 additions and 612 deletions.
10 changes: 5 additions & 5 deletions src/analysis/analysis.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
(let ((missing (find-non-matching-value (list (list pattern)) 1 env)))
(unless (eq t missing)
(tc-error "Non-exhaustive match"
(source:note pattern "missing case ~w"
(print-pattern (first missing)))))))
(source:secondary-note pattern "missing case ~w"
(print-pattern (first missing)))))))

(defun analyze-translation-unit (translation-unit env)
"Perform analysis passes on TRANSLATION-UNIT, potentially producing errors or warnings."
Expand All @@ -47,9 +47,9 @@
(cons (source:note node "non-exhaustive match")
(when (first exhaustive-or-missing)
(list
(source:note node "missing case ~w"
(print-pattern
(first exhaustive-or-missing))))))))
(source:secondary-note node "missing case ~w"
(print-pattern
(first exhaustive-or-missing))))))))
(loop :for pattern :in patterns
:unless (useful-pattern-p patterns pattern env) :do
(source:warn "Useless match case"
Expand Down
6 changes: 3 additions & 3 deletions src/doc/markdown.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@
:do (incf index)))

(defun line-number (backend source offset)
(let ((line-offsets (gethash (source-error:source-name source)
(let ((line-offsets (gethash (source:source-name source)
(slot-value backend 'file-line-offsets))))
(unless line-offsets
(with-open-stream (stream (source-error:source-stream source))
(with-open-stream (stream (source:source-stream source))
(setf line-offsets (find-line-offsets stream)))
(setf (gethash (source-error:source-name source)
(setf (gethash (source:source-name source)
(slot-value backend 'file-line-offsets))
line-offsets))
(labels ((%find (lo hi)
Expand Down
7 changes: 3 additions & 4 deletions src/doc/model.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,15 @@
(source:location-source location))))

(defun source-available-p (object)
(let ((source (source object)))
(and source
(not (null (source-error:source-name source))))))
(and (source object)
(source:source-available-p (source object))))

(defun source-location-href (object)
(when (source-available-p object)
(format nil "~a/~a"
*remote*
(remove-prefix (ensure-suffix #\/ *local*)
(source-error:source-name (source object))))))
(source:source-name (source object))))))

(defun source-span (object)
(source:location-span (object-location object)))
Expand Down
3 changes: 1 addition & 2 deletions src/entry.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
(:shadow
#:compile)
(:local-nicknames
(#:se #:source-error)
(#:settings #:coalton-impl/settings)
(#:util #:coalton-impl/util)
(#:parser #:coalton-impl/parser)
Expand Down Expand Up @@ -221,7 +220,7 @@
(defun compile-to-lisp (source output)
"Read Coalton source from SOURCE and write Lisp source to OUTPUT. NAME may be the filename related to the input stream."
(declare (optimize (debug 3)))
(with-open-stream (stream (se:source-stream source))
(with-open-stream (stream (source:source-stream source))
(parser:with-reader-context stream
(with-environment-updates updates
(let* ((program (parser:read-program stream source ':file))
Expand Down
12 changes: 9 additions & 3 deletions src/parser/base.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#:parse-error)
(:local-nicknames
(#:cst #:concrete-syntax-tree)
(#:se #:source-error)
(#:source #:coalton-impl/source)
(#:util #:coalton-impl/util))
(:export
Expand All @@ -23,6 +22,7 @@
#:parse-list ; FUNCTION
#:parse-error
#:note
#:secondary-note
#:note-end
#:help
#:form-location))
Expand Down Expand Up @@ -95,13 +95,13 @@
;;; (note SOURCE CST2 "Related: ~A" ARG3)
;;; ... )

(define-condition parse-error (se:source-base-error)
(define-condition parse-error (source:source-error)
()
(:documentation "A condition indicating a syntax error in Coalton source code."))

(defun parse-error (message &rest notes)
"Signal PARSE-ERROR with provided MESSAGE and source NOTES."
(error 'parse-error :err (source:make-source-error ':error message notes)))
(error 'parse-error :message message :notes notes))

(defun ensure-span (spanning)
"Is SPANNING is a span, return it unchanged; if it is a cst node, return the node's span."
Expand All @@ -115,6 +115,12 @@
(apply #'source:note (source:make-location source (ensure-span locatable))
format-string format-args))

(defun secondary-note (source locatable format-string &rest format-args)
"Make a source note using SOURCE and CST:SOURCE as location."
(declare (type string format-string))
(apply #'source:secondary-note (source:make-location source (ensure-span locatable))
format-string format-args))

(defun note-end (source locatable format-string &rest format-args)
"Make a source note using SOURCE and the location immediately following CST:SOURCE as location."
(apply #'source:note
Expand Down
Loading

0 comments on commit c8e97c2

Please sign in to comment.