summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2020-07-25 17:54:20 +0200
committerLudovic Courtès <ludo@gnu.org>2020-07-25 19:11:36 +0200
commit860f3d77495aad0061c4ee9b6de73d6fe9fc40e9 (patch)
tree9e876dc8a9c11588229f766fd471d6423b2ff7cf
parentefe037fc5cc3134bbc3ef4e36b49a3f788921b68 (diff)
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.
-rw-r--r--guix/diagnostics.scm48
1 files changed, 35 insertions, 13 deletions
diff --git a/guix/diagnostics.scm b/guix/diagnostics.scm
index 3096d384d81..3b536d8e96d 100644
--- a/guix/diagnostics.scm
+++ b/guix/diagnostics.scm
@@ -57,22 +57,22 @@
57;;; 57;;;
58;;; Code: 58;;; Code:
59 59
60(define (trivial-format-string? fmt)
61 (define len
62 (string-length fmt))
63
64 (let loop ((start 0))
65 (or (>= (+ 1 start) len)
66 (let ((tilde (string-index fmt #\~ start)))
67 (or (not tilde)
68 (case (string-ref fmt (+ tilde 1))
69 ((#\a #\A #\%) (loop (+ tilde 2)))
70 (else #f)))))))
71
60(define-syntax highlight-argument 72(define-syntax highlight-argument
61 (lambda (s) 73 (lambda (s)
62 "Given FMT and ARG, expand ARG to a call that highlights it, provided FMT 74 "Given FMT and ARG, expand ARG to a call that highlights it, provided FMT
63is a trivial format string." 75is a trivial format string."
64 (define (trivial-format-string? fmt)
65 (define len
66 (string-length fmt))
67
68 (let loop ((start 0))
69 (or (>= (+ 1 start) len)
70 (let ((tilde (string-index fmt #\~ start)))
71 (or (not tilde)
72 (case (string-ref fmt (+ tilde 1))
73 ((#\a #\A #\%) (loop (+ tilde 2)))
74 (else #f)))))))
75
76 ;; Be conservative: limit format argument highlighting to cases where the 76 ;; Be conservative: limit format argument highlighting to cases where the
77 ;; format string contains nothing but ~a escapes. If it contained ~s 77 ;; format string contains nothing but ~a escapes. If it contained ~s
78 ;; escapes, this strategy wouldn't work. 78 ;; escapes, this strategy wouldn't work.
@@ -132,7 +132,15 @@ messages."
132 args (... ...)) 132 args (... ...))
133 (free-identifier=? #'N-underscore #'N_) 133 (free-identifier=? #'N-underscore #'N_)
134 #'(name #f (N-underscore singular plural n) 134 #'(name #f (N-underscore singular plural n)
135 args (... ...))))))))) 135 args (... ...)))
136 (id
137 (identifier? #'id)
138 ;; Run-time variant.
139 #'(lambda (location fmt . args)
140 (emit-diagnostic fmt args
141 #:location location
142 #:prefix prefix
143 #:colors colors)))))))))
136 144
137;; XXX: This doesn't work well for right-to-left languages. 145;; XXX: This doesn't work well for right-to-left languages.
138;; TRANSLATORS: The goal is to emit "warning:" followed by a short phrase; 146;; TRANSLATORS: The goal is to emit "warning:" followed by a short phrase;
@@ -147,6 +155,20 @@ messages."
147 (report-error args ...) 155 (report-error args ...)
148 (exit 1))) 156 (exit 1)))
149 157
158(define* (emit-diagnostic fmt args
159 #:key location (colors (color)) (prefix ""))
160 "Report diagnostic message FMT with the given ARGS and the specified
161LOCATION, COLORS, and PREFIX.
162
163This procedure is used as a last resort when the format string is not known at
164macro-expansion time."
165 (print-diagnostic-prefix (gettext prefix %gettext-domain)
166 location #:colors colors)
167 (apply format (guix-warning-port) fmt
168 (if (trivial-format-string? fmt)
169 (map %highlight-argument args)
170 args)))
171
150(define %warning-color (color BOLD MAGENTA)) 172(define %warning-color (color BOLD MAGENTA))
151(define %info-color (color BOLD)) 173(define %info-color (color BOLD))
152(define %error-color (color BOLD RED)) 174(define %error-color (color BOLD RED))