mirror of
https://git.in.rschanz.org/ryan77627/guix.git
synced 2024-11-07 07:26:13 -05:00
diagnostics: Add a procedural variant of diagnostic procedures.
Callers can pass 'report-error', 'warning', etc. to 'apply'. * guix/diagnostics.scm (trivial-format-string?): New procedure, moved from... (highlight-argument): ... here. (define-diagnostic): Add 'identifier?' clause. (emit-diagnostic): New procedure.
This commit is contained in:
parent
efe037fc5c
commit
860f3d7749
1 changed files with 35 additions and 13 deletions
|
@ -57,10 +57,6 @@ (define-module (guix diagnostics)
|
||||||
;;;
|
;;;
|
||||||
;;; Code:
|
;;; Code:
|
||||||
|
|
||||||
(define-syntax highlight-argument
|
|
||||||
(lambda (s)
|
|
||||||
"Given FMT and ARG, expand ARG to a call that highlights it, provided FMT
|
|
||||||
is a trivial format string."
|
|
||||||
(define (trivial-format-string? fmt)
|
(define (trivial-format-string? fmt)
|
||||||
(define len
|
(define len
|
||||||
(string-length fmt))
|
(string-length fmt))
|
||||||
|
@ -73,6 +69,10 @@ (define len
|
||||||
((#\a #\A #\%) (loop (+ tilde 2)))
|
((#\a #\A #\%) (loop (+ tilde 2)))
|
||||||
(else #f)))))))
|
(else #f)))))))
|
||||||
|
|
||||||
|
(define-syntax highlight-argument
|
||||||
|
(lambda (s)
|
||||||
|
"Given FMT and ARG, expand ARG to a call that highlights it, provided FMT
|
||||||
|
is a trivial format string."
|
||||||
;; Be conservative: limit format argument highlighting to cases where the
|
;; Be conservative: limit format argument highlighting to cases where the
|
||||||
;; format string contains nothing but ~a escapes. If it contained ~s
|
;; format string contains nothing but ~a escapes. If it contained ~s
|
||||||
;; escapes, this strategy wouldn't work.
|
;; escapes, this strategy wouldn't work.
|
||||||
|
@ -132,7 +132,15 @@ (define-syntax name
|
||||||
args (... ...))
|
args (... ...))
|
||||||
(free-identifier=? #'N-underscore #'N_)
|
(free-identifier=? #'N-underscore #'N_)
|
||||||
#'(name #f (N-underscore singular plural n)
|
#'(name #f (N-underscore singular plural n)
|
||||||
args (... ...)))))))))
|
args (... ...)))
|
||||||
|
(id
|
||||||
|
(identifier? #'id)
|
||||||
|
;; Run-time variant.
|
||||||
|
#'(lambda (location fmt . args)
|
||||||
|
(emit-diagnostic fmt args
|
||||||
|
#:location location
|
||||||
|
#:prefix prefix
|
||||||
|
#:colors colors)))))))))
|
||||||
|
|
||||||
;; XXX: This doesn't work well for right-to-left languages.
|
;; XXX: This doesn't work well for right-to-left languages.
|
||||||
;; TRANSLATORS: The goal is to emit "warning:" followed by a short phrase;
|
;; TRANSLATORS: The goal is to emit "warning:" followed by a short phrase;
|
||||||
|
@ -147,6 +155,20 @@ (define-syntax-rule (leave args ...)
|
||||||
(report-error args ...)
|
(report-error args ...)
|
||||||
(exit 1)))
|
(exit 1)))
|
||||||
|
|
||||||
|
(define* (emit-diagnostic fmt args
|
||||||
|
#:key location (colors (color)) (prefix ""))
|
||||||
|
"Report diagnostic message FMT with the given ARGS and the specified
|
||||||
|
LOCATION, COLORS, and PREFIX.
|
||||||
|
|
||||||
|
This procedure is used as a last resort when the format string is not known at
|
||||||
|
macro-expansion time."
|
||||||
|
(print-diagnostic-prefix (gettext prefix %gettext-domain)
|
||||||
|
location #:colors colors)
|
||||||
|
(apply format (guix-warning-port) fmt
|
||||||
|
(if (trivial-format-string? fmt)
|
||||||
|
(map %highlight-argument args)
|
||||||
|
args)))
|
||||||
|
|
||||||
(define %warning-color (color BOLD MAGENTA))
|
(define %warning-color (color BOLD MAGENTA))
|
||||||
(define %info-color (color BOLD))
|
(define %info-color (color BOLD))
|
||||||
(define %error-color (color BOLD RED))
|
(define %error-color (color BOLD RED))
|
||||||
|
|
Loading…
Reference in a new issue