aboutsummaryrefslogtreecommitdiff
path: root/src/guile/skribilo/lib.scm
blob: ef18bda705edc9998a5b9cc519860e055c0eb6e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
;;; lib.scm -- Utilities.                 -*- coding: iso-8859-1 -*-
;;;
;;; Copyright 2005, 2007, 2009, 2012, 2013, 2016, 2020  Ludovic Court�s <ludo@gnu.org>
;;; Copyright 2003, 2004  Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;
;;;
;;; This file is part of Skribilo.
;;;
;;; Skribilo is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Skribilo is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Skribilo.  If not, see <http://www.gnu.org/licenses/>.

(define-module (skribilo lib)
  #:use-module (skribilo utils syntax)
  #:export (skribe-ast-error skribe-error
           skribe-type-error
           warning/loc
           skribe-warning
           skribe-warning/ast
           skribe-message

	   type-name

           new define-markup define-simple-markup
           define-simple-container define-processor-markup

           &invocation-location)

  ;; Re-exported because used in `define-markup'.
  #:re-export  (invocation-location)

  #:use-module (skribilo ast)

  ;; useful for `new' to work well with <language>
  #:autoload   (skribilo source)   (<language>)

  #:use-module (skribilo parameters)
  #:use-module (skribilo location)

  #:use-module (srfi srfi-1)
  #:use-module (oop goops))


(skribilo-module-syntax)


;;;
;;; NEW
;;;

(define-macro (new class . parameters)
  ;; Thanks to the trick below, modules don't need to import `(oop goops)'
  ;; and `(skribilo ast)' in order to make use of `new'.
  (let ((class-name (symbol-append '< class '>)))
    `((@ (oop goops) make) (@@ (skribilo lib) ,class-name)
      ,@(concatenate (map (lambda (x)
                            `(,(symbol->keyword (car x)) ,(cadr x)))
                          parameters)))))

;;;
;;; DEFINE-MARKUP
;;;

(define (dsssl->guile-formals args)
  ;; When using `(ice-9 optargs)', the `:rest' argument can only appear last,
  ;; which is not what Skribe/DSSSL expect'.  In addition, all keyword
  ;; arguments are allowed (hence `:allow-other-keys'); they are then checked
  ;; by `verify'.  This procedure shuffles ARGS accordingly.

  (let loop ((args          args)
             (result        '())
             (rest-arg      '())
             (has-keywords? #f))
    (cond ((null? args)
           (let ((result (if has-keywords?
                             (cons :allow-other-keys result)
                             result)))
             (append (reverse result) rest-arg)))

          ((list? args)
           (let ((is-rest-arg? (eq? (car args) :rest))
                 (is-keyword?  (eq? (car args) :key)))
             (if is-rest-arg?
                 (loop (cddr args)
                       result
                       (list (car args) (cadr args))
                       (or has-keywords? is-keyword?))
                 (loop (cdr args)
                       (cons (car args) result)
                       rest-arg
                       (or has-keywords? is-keyword?)))))

          ((pair? args)
           (loop '()
                 (cons (car args) result)
                 (list :rest (cdr args))
                 has-keywords?)))))

;; `define-markup' is similar to Guile's `lambda*', with DSSSL
;; keyword style, and a couple other differences handled by
;; `dsssl->guile-formals'.

;; On Guile 2.0, `define-markup' generates a macro for the markup, such
;; that the macro captures its invocation source location using
;; `current-source-location'.

(define-syntax-parameter &invocation-location
  (identifier-syntax #f))

(define-syntax define-markup
  (lambda (s)
    (syntax-case s ()
      ;; Note: Use a dotted pair for formals, to allow for dotted forms
      ;; like: `(define-markup (foo x . rest) ...)'.
      ((_ (name . formals) body ...)
       (let ((formals  (map (lambda (s)
                              (datum->syntax #'formals s))
                            (dsssl->guile-formals (syntax->datum #'formals))))
             (internal (symbol-append '% (syntax->datum #'name)
                                      '-internal)))
         (with-syntax ((internal/loc (datum->syntax #'name internal)))
           #`(begin
               (define* (internal/loc loc #,@formals)
                 (syntax-parameterize ((&invocation-location
                                        (identifier-syntax loc)))
                   body ...))
               (define-syntax name
                 (lambda (s)
                   (syntax-case s ()
                     ((_ . args)
                      #'(let ((loc (source-properties->location
                                    (current-source-location))))
                          (internal/loc loc . args)))
                     (_
                      #'(lambda args
                          (let ((loc (source-properties->location
                                      (current-source-location))))
                            (apply internal/loc loc args)))))))
               internal/loc                     ; mark it as used
               (export name))))))))


;;;
;;; DEFINE-SIMPLE-MARKUP
;;;
(define-macro (define-simple-markup markup)
  `(define-markup (,markup :rest opts :key ident class loc)
     (new markup
	  (markup ',markup)
	  (ident (or ident (symbol->string
			    (gensym ,(symbol->string markup)))))
	  (loc (or loc &invocation-location))
	  (class class)
	  (required-options '())
	  (options (the-options opts :ident :class :loc))
	  (body (the-body opts)))))


;;;
;;; DEFINE-SIMPLE-CONTAINER
;;;
(define-macro (define-simple-container markup)
   `(define-markup (,markup :rest opts :key ident class loc)
       (new container
	  (markup ',markup)
	  (ident (or ident (symbol->string
			    (gensym ,(symbol->string markup)))))
	  (loc (or loc &invocation-location))
	  (class class)
	  (required-options '())
	  (options (the-options opts :ident :class :loc))
	  (body (the-body opts)))))


;;;
;;; DEFINE-PROCESSOR-MARKUP
;;;
(define-macro (define-processor-markup proc)
  `(define-markup (,proc #:rest opts :key loc)
     (new processor
          (loc     (or loc &invocation-location))
	  (engine  (find-engine ',proc))
	  (body    (the-body opts))
	  (options (the-options opts)))))



;;;
;;; TYPE-NAME
;;;
(define (type-name obj)
  (cond ((string? obj)  "string")
	((ast? obj)     "ast")
	((list? obj)    "list")
	((pair? obj)    "pair")
	((number? obj)  "number")
	((char? obj)    "character")
	((keyword? obj) "keyword")
	(else           (with-output-to-string
			  (lambda () (write obj))))))

;;;
;;; SKRIBE-ERROR
;;;
(define (skribe-ast-error proc msg obj)
  (let ((l     (ast-loc obj))
	(shape (if (markup? obj) (markup-markup obj) obj)))
    (if (location? l)
	(error (format #f "~a:~a: ~a: ~a ~s" (location-file l)
		       (location-line l) proc msg shape))
	(error (format #f "~a: ~a ~s " proc msg shape)))))

(define (skribe-error proc msg obj)
  (if (ast? obj)
      (skribe-ast-error proc msg obj)
      (error (format #f "~a: ~a ~s" proc msg obj))))


;;;
;;; SKRIBE-TYPE-ERROR
;;;
(define (skribe-type-error proc msg obj etype)
  (skribe-error proc (format #f "~a ~s (~a expected)" msg obj etype) #f))


;;;
;;; SKRIBE-WARNING  &  SKRIBE-WARNING/AST
;;;
(define (%skribe-warn level file line col lst)
  (let ((port (current-error-port)))
    (when (and file line col)
      (format port "~a:~a:~a: " file line col))
    (display "warning: " port)
    (for-each (lambda (x) (format port "~a " x)) lst)
    (newline port)))


(define (skribe-warning level . obj)
  (if (>= (*warning*) level)
      (%skribe-warn level #f #f #f obj)))


(define (warning/loc level loc . obj)
  (if (>= (*warning*) level)
      (if (location? loc)
          (%skribe-warn level (location-file loc) (location-line loc)
                        (location-column loc) obj)
          (%skribe-warn level #f #f #f obj))))

(define (skribe-warning/ast level ast . obj)
  (apply warning/loc level
         (and (ast? ast) (ast-location ast))
         obj))

;;;
;;; SKRIBE-MESSAGE
;;;
(define (skribe-message fmt . obj)
  (when (> (*verbose*) 0)
    (apply format (current-error-port) fmt obj)))

;;; lib.scm ends here