Skip to content

Instantly share code, notes, and snippets.

@srcreigh
Created September 4, 2021 02:24
Show Gist options
  • Save srcreigh/f341b2adaa0fe37c241fdf15f37bdd2c to your computer and use it in GitHub Desktop.
Save srcreigh/f341b2adaa0fe37c241fdf15f37bdd2c to your computer and use it in GitHub Desktop.
Display multiple syntax errors in Racket
#lang racket/base
(require racket/list)
(provide report-all-errors
save-error-message)
;; Taken from Typed Racket codebase tc-util.rkt
(define-struct err (msg stx) #:prefab)
(define delayed-errors null)
(define (take-errors!)
(define errs (remove-duplicates (reverse delayed-errors)))
(set! delayed-errors null)
errs)
(define (save-error-message msg #:stx stx . vs)
(set! delayed-errors (cons (make-err (apply format msg vs)
(list stx))
delayed-errors)))
(define (raise-typecheck-error msg stxs)
(if (null? (cdr stxs))
(raise-syntax-error (string->symbol "Type Checker") msg (car stxs))
(raise-syntax-error (string->symbol "Type Checker") msg #f #f stxs)))
(define (report-all-errors)
(define errs (take-errors!))
(for/list ([e (in-list errs)])
(with-handlers ([exn:fail:syntax?
(λ (e) ((error-display-handler) (exn-message e) e))])
(raise-typecheck-error (err-msg e) (err-stx e))))
(raise-typecheck-error (format "Summary: ~a errors encountered"
(length errs))
(apply append (map err-stx errs))))
#lang racket/base
(require (for-syntax racket/base syntax/parse "errors.rkt"))
(define-syntax (err stx)
(syntax-parse stx
[(_ ...)
(save-error-message "This is an error." #:stx stx)
#'(void)]))
(define-syntax (run-typechecking stx)
(syntax-parse stx
[(_ ...)
(report-all-errors)
#'(void)]))
;; The following two lines will be highlighted as
;; errors in Dr Racket and Emacs racket-xp-mode.
(err first error)
(err (+ "hello" 2))
(run-typechecking)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment