import: hackage: Add recognition of 'true' and 'false' symbols.
[guix.git] / guix / import / cabal.scm
blob8d84e0907756383d4135ccba3aa11b6f74bbee22
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix import cabal)
20   #:use-module (ice-9 match)
21   #:use-module (ice-9 regex)
22   #:use-module (ice-9 rdelim)
23   #:use-module (ice-9 receive)
24   #:use-module (srfi srfi-26)
25   #:use-module (srfi srfi-34)
26   #:use-module (srfi srfi-35)
27   #:use-module (srfi srfi-11)
28   #:use-module (srfi srfi-1)
29   #:use-module (srfi srfi-9)
30   #:use-module (srfi srfi-9 gnu)
31   #:use-module (system base lalr)
32   #:use-module (rnrs enums)
33   #:export (read-cabal
34             eval-cabal
35             
36             cabal-package?
37             cabal-package-name
38             cabal-package-version
39             cabal-package-license
40             cabal-package-home-page
41             cabal-package-source-repository
42             cabal-package-synopsis
43             cabal-package-description
44             cabal-package-executables
45             cabal-package-library
46             cabal-package-test-suites
47             cabal-package-flags
48             cabal-package-eval-environment
50             cabal-source-repository?
51             cabal-source-repository-use-case
52             cabal-source-repository-type
53             cabal-source-repository-location
55             cabal-flag?
56             cabal-flag-name
57             cabal-flag-description
58             cabal-flag-default
59             cabal-flag-manual
61             cabal-dependency?
62             cabal-dependency-name
63             cabal-dependency-version
65             cabal-executable?
66             cabal-executable-name
67             cabal-executable-dependencies
69             cabal-library?
70             cabal-library-dependencies
72             cabal-test-suite?
73             cabal-test-suite-name
74             cabal-test-suite-dependencies))
76 ;; Part 1:
78 ;; Functions used to read a Cabal file.
80 ;; Comment:
82 ;; The use of virtual closing braces VCCURLY and some lexer functions were
83 ;; inspired from http://hackage.haskell.org/package/haskell-src
85 ;; Object containing information about the structure of a block: (i) delimited
86 ;; by braces or by indentation, (ii) minimum indentation.
87 (define-record-type  <parse-context>
88   (make-parse-context mode indentation)
89   parse-context?
90   (mode parse-context-mode)                ; 'layout or 'no-layout
91   (indentation parse-context-indentation)) ; #f for 'no-layout
93 ;; <parse-context> mode set universe
94 (define-enumeration context (layout no-layout) make-context)
96 (define (make-stack)
97   "Creates a simple stack closure.  Actions on the generated stack are
98 requested by calling it with one of the following symbols as the first
99 argument: 'empty?, 'push!, 'top, 'pop! and 'clear!.  The action 'push! is the
100 only one requiring a second argument corresponding to the object to be added
101 to the stack."
102   (let ((stack '()))
103     (lambda (msg . args)
104       (cond ((eqv? msg 'empty?) (null? stack))
105             ((eqv? msg 'push!) (set! stack (cons (first args) stack)))
106             ((eqv? msg 'top) (if (null? stack) '() (first stack)))
107             ((eqv? msg 'pop!) (match stack
108                                 ((e r ...) (set! stack (cdr stack)) e)
109                                 (_ #f)))
110             ((eqv? msg 'clear!) (set! stack '()))
111             (else #f)))))
113 ;; Stack to track the structure of nested blocks and simple interface
114 (define context-stack (make-parameter (make-stack)))
116 (define (context-stack-empty?) ((context-stack) 'empty?))
118 (define (context-stack-push! e) ((context-stack) 'push! e))
120 (define (context-stack-top) ((context-stack) 'top))
122 (define (context-stack-pop!) ((context-stack) 'pop!))
124 (define (context-stack-clear!) ((context-stack) 'clear!))
126 ;; Indentation of the line being parsed.
127 (define current-indentation (make-parameter 0))
129 ;; Signal to reprocess the beginning of line, in case we need to close more
130 ;; than one indentation level.
131 (define check-bol? (make-parameter #f))
133 ;; Name of the file being parsed. Used in error messages.
134 (define cabal-file-name (make-parameter "unknowk"))
136 ;; Specify the grammar of a Cabal file and generate a suitable syntax analyser.
137 (define (make-cabal-parser)
138   "Generate a parser for Cabal files."
139   (lalr-parser
140    ;; --- token definitions
141    (CCURLY VCCURLY OPAREN CPAREN TEST ID VERSION RELATION TRUE FALSE
142            (right: IF FLAG EXEC TEST-SUITE SOURCE-REPO BENCHMARK LIB OCURLY)
143            (left: OR)
144            (left: PROPERTY AND)
145            (right: ELSE NOT))
146    ;; --- rules
147    (body        (properties sections)   : (append $1 $2))
148    (sections    (sections flags)        : (append $1 $2)
149                 (sections source-repo)  : (append $1 (list $2))
150                 (sections executables)  : (append $1 $2)
151                 (sections test-suites)  : (append $1 $2)
152                 (sections benchmarks)   : (append $1 $2)
153                 (sections lib-sec)      : (append $1 (list $2))
154                 ()                      : '())
155    (flags       (flags flag-sec)        : (append $1 (list $2))
156                 (flag-sec)              : (list $1))
157    (flag-sec    (FLAG OCURLY properties CCURLY) : `(section flag ,$1 ,$3)
158                 (FLAG open properties close)    : `(section flag ,$1 ,$3)
159                 (FLAG)                          : `(section flag ,$1 '()))
160    (source-repo (SOURCE-REPO OCURLY properties CCURLY)
161                 : `(section source-repository ,$1 ,$3)
162                 (SOURCE-REPO open properties close)
163                 : `(section source-repository ,$1 ,$3))
164    (properties  (properties PROPERTY)   : (append $1 (list $2))
165                 (PROPERTY)              : (list $1))
166    (executables (executables exec-sec)  : (append $1 (list $2))
167                 (exec-sec)              : (list $1))
168    (exec-sec    (EXEC OCURLY exprs CCURLY) : `(section executable ,$1 ,$3)
169                 (EXEC open exprs close)    : `(section executable ,$1 ,$3))
170    (test-suites (test-suites ts-sec)    : (append $1 (list $2))
171                 (ts-sec)                : (list $1))
172    (ts-sec      (TEST-SUITE OCURLY exprs CCURLY) : `(section test-suite ,$1 ,$3)
173                 (TEST-SUITE open exprs close)    : `(section test-suite ,$1 ,$3))
174    (benchmarks  (benchmarks bm-sec)     : (append $1 (list $2))
175                 (bm-sec)                : (list $1))
176    (bm-sec      (BENCHMARK OCURLY exprs CCURLY) : `(section benchmark ,$1 ,$3)
177                 (BENCHMARK open exprs close)    : `(section benchmark ,$1 ,$3))
178    (lib-sec     (LIB OCURLY exprs CCURLY) : `(section library ,$3)
179                 (LIB open exprs close)    : `(section library ,$3))
180    (exprs       (exprs PROPERTY)         : (append $1 (list $2))
181                 (PROPERTY)               : (list $1)
182                 (exprs if-then-else)     : (append $1 (list $2))
183                 (if-then-else)           : (list $1)
184                 (exprs if-then)          : (append $1 (list $2))
185                 (if-then)                : (list $1))
186    (if-then-else (IF tests OCURLY exprs CCURLY ELSE OCURLY exprs CCURLY)
187                  : `(if ,$2 ,$4 ,$8)
188                  (IF tests open exprs close ELSE OCURLY exprs CCURLY)
189                  : `(if ,$2 ,$4 ,$8)
190                  ;; The 'open' token after 'tests' is shifted after an 'exprs'
191                  ;; is found.  This is because, instead of 'exprs' a 'OCURLY'
192                  ;; token is a valid alternative.  For this reason, 'open'
193                  ;; pushes a <parse-context> with a line indentation equal to
194                  ;; the indentation of 'exprs'.
195                  ;;
196                  ;; Differently from this, without the rule above this
197                  ;; comment, when an 'ELSE' token is found, the 'open' token
198                  ;; following the 'ELSE' would be shifted immediately, before
199                  ;; the 'exprs' is found (because there are no other valid
200                  ;; tokens).  The 'open' would therefore create a
201                  ;; <parse-context> with the indentation of 'ELSE' and not
202                  ;; 'exprs', creating an inconsistency.  We therefore allow
203                  ;; mixed style conditionals.
204                  (IF tests open exprs close ELSE open exprs close)
205                  : `(if ,$2 ,$4 ,$8))
206    (if-then     (IF tests OCURLY exprs CCURLY) : `(if ,$2 ,$4 ())
207                 (IF tests open exprs close)    : `(if ,$2 ,$4 ()))
208    (tests       (TEST OPAREN ID CPAREN)        : `(,$1 ,$3)
209                 (TRUE)                         : 'true
210                 (FALSE)                        : 'false
211                 (TEST OPAREN ID RELATION VERSION CPAREN)
212                 : `(,$1 ,(string-append $3 " " $4 " " $5))
213                 (TEST OPAREN ID RELATION VERSION AND RELATION VERSION CPAREN)
214                 : `(and (,$1 ,(string-append $3 " " $4 " " $5))
215                         (,$1 ,(string-append $3 " " $7 " " $8)))
216                (NOT tests)                     : `(not ,$2)
217                (tests AND tests)               : `(and ,$1 ,$3)
218                (tests OR tests)                : `(or ,$1 ,$3)
219                (OPAREN tests CPAREN)           : $2)
220    (open       () : (context-stack-push!
221                                    (make-parse-context (context layout)
222                                                        (current-indentation))))
223    (close      (VCCURLY))))
225 (define (peek-next-line-indent port)
226   "This function can be called when the next character on PORT is #\newline
227 and returns the indentation of the line starting after the #\newline
228 character.  Discard (and consume) empty and comment lines."
229   (let ((initial-newline (string (read-char port))))
230     (let loop ((char (peek-char port))
231                (word ""))
232       (cond ((eqv? char #\newline) (read-char port)
233              (loop (peek-char port) ""))
234             ((or (eqv? char #\space) (eqv? char #\tab))
235              (let ((c (read-char port)))
236                (loop (peek-char port) (string-append word (string c)))))
237             ((comment-line port char) (loop (peek-char port) ""))
238             (else
239              (let ((len (string-length word)))
240                (unread-string (string-append initial-newline word) port)
241                len))))))
243 (define* (read-value port value min-indent #:optional (separator " "))
244   "The next character on PORT must be #\newline.  Append to VALUE the
245 following lines with indentation larger than MIN-INDENT."
246   (let loop ((val (string-trim-both value))
247              (x (peek-next-line-indent port)))
248     (if (> x min-indent)
249         (begin
250           (read-char port) ; consume #\newline
251           (loop (string-append
252                  val (if (string-null? val) "" separator)
253                  (string-trim-both (read-delimited "\n" port 'peek)))
254                 (peek-next-line-indent port)))
255         val)))
257 (define (lex-white-space port bol)
258   "Consume white spaces and comment lines on PORT.  If a new line is started return #t,
259 otherwise return BOL (beginning-of-line)."
260   (let loop ((c (peek-char port))
261              (bol bol))
262     (cond
263      ((and (not (eof-object? c))
264            (or (char=? c #\space) (char=? c #\tab)))
265       (read-char port)
266       (loop (peek-char port) bol))
267      ((and (not (eof-object? c)) (char=? c #\newline))
268       (read-char port)
269       (loop (peek-char port) #t))
270      ((comment-line port c)
271       (lex-white-space port bol))
272      (else
273       bol))))
275 (define (lex-bol port)
276   "Process the beginning of a line on PORT: update current-indentation and
277 check the end of an indentation based context."
278   (let ((loc (make-source-location (cabal-file-name) (port-line port)
279                                    (port-column port) -1 -1)))
280     (current-indentation (source-location-column loc))
281     (case (get-offside port)
282       ((less-than)
283        (check-bol? #t) ; need to check if closing more than 1 indent level.
284        (unless (context-stack-empty?) (context-stack-pop!))
285        (make-lexical-token 'VCCURLY loc #f))
286       (else
287        (lex-token port)))))
289 (define (bol? port) (or (check-bol?) (= (port-column port) 0)))
291 (define (comment-line port c)
292   "If PORT starts with a comment line, consume it up to, but not including
293 #\newline.  C is the next character on PORT."
294   (cond ((and (not (eof-object? c)) (char=? c #\-))
295          (read-char port)
296          (let ((c2 (peek-char port)))
297            (if (char=? c2 #\-)
298                (read-delimited "\n" port 'peek)
299                (begin (unread-char c port) #f))))
300         (else #f)))
302 (define-enumeration ordering (less-than equal greater-than) make-ordering)
304 (define (get-offside port)
305   "In an indentation based context return the symbol 'greater-than, 'equal or
306 'less-than to signal if the current column number on PORT is greater-, equal-,
307 or less-than the indentation of the current context."
308   (let ((x (port-column port)))
309     (match (context-stack-top)
310       (($ <parse-context> 'layout indentation)
311        (cond
312         ((> x indentation) (ordering greater-than))
313         ((= x indentation) (ordering equal))
314         (else (ordering less-than))))
315       (_ (ordering greater-than)))))
317 ;; (Semi-)Predicates for individual tokens.
319 (define (is-relation? c)
320   (and (char? c) (any (cut char=? c <>) '(#\< #\> #\=))))
322 (define* (make-rx-matcher pat #:optional (flag #f))
323   "Compile PAT into a regular expression with FLAG and creates a function
324 matching a string against the created regexp."
325   (let ((rx (if flag
326                 (make-regexp pat flag)
327                 (make-regexp pat))))
328     (cut regexp-exec rx <>)))
330 (define is-property (make-rx-matcher "([a-z0-9-]+):[ \t]*(\\w?.*)$"
331                                      regexp/icase))
333 (define is-flag (make-rx-matcher "^flag +([a-z0-9_-]+)"
334                                  regexp/icase))
336 (define is-src-repo
337   (make-rx-matcher "^source-repository +([a-z0-9_-]+)"
338                    regexp/icase))
340 (define is-exec (make-rx-matcher "^executable +([a-z0-9_-]+)"
341                                  regexp/icase))
343 (define is-test-suite (make-rx-matcher "^test-suite +([a-z0-9_-]+)"
344                                        regexp/icase))
346 (define is-benchmark (make-rx-matcher "^benchmark +([a-z0-9_-]+)"
347                                       regexp/icase))
349 (define is-lib (make-rx-matcher "^library *" regexp/icase))
351 (define is-else (make-rx-matcher "^else" regexp/icase))
353 (define (is-if s) (string-ci=? s "if"))
355 (define (is-true s) (string-ci=? s "true"))
357 (define (is-false s) (string-ci=? s "false"))
359 (define (is-and s) (string=? s "&&"))
361 (define (is-or s) (string=? s "||"))
363 (define (is-id s)
364   (let ((cabal-reserved-words
365          '("if" "else" "library" "flag" "executable" "test-suite"
366            "source-repository" "benchmark")))
367     (and (every (cut string-ci<> s <>) cabal-reserved-words)
368          (not (char=? (last (string->list s)) #\:)))))
370 (define (is-test s port)
371   (let ((tests-rx (make-regexp "os|arch|flag|impl"))
372         (c (peek-char port)))
373     (and (regexp-exec tests-rx s) (char=? #\( c))))
375 ;; Lexers for individual tokens.
377 (define (lex-relation loc port)
378   (make-lexical-token 'RELATION loc (read-while is-relation? port)))
380 (define (lex-version loc port)
381   (make-lexical-token 'VERSION loc
382                       (read-while char-numeric? port
383                                   (cut char=? #\. <>) char-numeric?)))
385 (define* (read-while is? port #:optional
386                      (is-if-followed-by? (lambda (c) #f))
387                      (is-allowed-follower? (lambda (c) #f)))
388   "Read from PORT as long as: (i) either the read character satisfies the
389 predicate IS?, or (ii) it satisfies the predicate IS-IF-FOLLOWED-BY? and the
390 character immediately following it satisfies IS-ALLOWED-FOLLOWER?.  Returns a
391 string with the read characters."
392   (let loop ((c (peek-char port))
393              (res '()))
394     (cond ((and (not (eof-object? c)) (is? c))
395            (let ((c (read-char port)))
396              (loop (peek-char port) (append res (list c)))))
397           ((and (not (eof-object? c)) (is-if-followed-by? c))
398            (let ((c (read-char port))
399                  (c2 (peek-char port)))
400              (if (and (not (eof-object? c2)) (is-allowed-follower? c2))
401                  (loop c2 (append res (list c)))
402                  (begin (unread-char c) (list->string res)))))
403           (else (list->string res)))))
405 (define (lex-property k-v-rx-res loc port)
406   (let ((key (string-downcase (match:substring k-v-rx-res 1)))
407         (value (match:substring k-v-rx-res 2)))
408     (make-lexical-token
409      'PROPERTY loc
410      (list key `(,(read-value port value (current-indentation)))))))
412 (define (lex-rx-res rx-res token loc)
413   (let ((name (string-downcase (match:substring rx-res 1))))
414     (make-lexical-token token loc name)))
416 (define (lex-flag flag-rx-res loc) (lex-rx-res flag-rx-res 'FLAG loc))
418 (define (lex-src-repo src-repo-rx-res loc)
419   (lex-rx-res src-repo-rx-res 'SOURCE-REPO loc))
421 (define (lex-exec exec-rx-res loc) (lex-rx-res exec-rx-res 'EXEC loc))
423 (define (lex-test-suite ts-rx-res loc) (lex-rx-res ts-rx-res 'TEST-SUITE loc))
425 (define (lex-benchmark bm-rx-res loc) (lex-rx-res bm-rx-res 'BENCHMARK loc))
427 (define (lex-lib loc) (make-lexical-token 'LIB loc #f))
429 (define (lex-else loc) (make-lexical-token 'ELSE loc #f))
431 (define (lex-if loc) (make-lexical-token 'IF loc #f))
433 (define (lex-true loc) (make-lexical-token 'TRUE loc #t))
435 (define (lex-false loc) (make-lexical-token 'FALSE loc #f))
437 (define (lex-and loc) (make-lexical-token 'AND loc #f))
439 (define (lex-or loc) (make-lexical-token 'OR loc #f))
441 (define (lex-id w loc) (make-lexical-token 'ID loc w))
443 (define (lex-test w loc) (make-lexical-token 'TEST loc (string->symbol w)))
445 ;; Lexer for tokens recognizable by single char.
447 (define* (is-ref-char->token ref-char next-char token loc port
448                          #:optional (hook-fn #f))
449   "If the next character NEXT-CHAR on PORT is REF-CHAR, then read it,
450 execute HOOK-FN if it isn't #f and return a lexical token of type TOKEN with
451 location information LOC."
452   (cond ((char=? next-char ref-char)
453          (read-char port)
454          (when hook-fn (hook-fn))
455          (make-lexical-token token loc (string next-char)))
456         (else #f)))
458 (define (is-ocurly->token c loc port)
459   (is-ref-char->token #\{ c 'OCURLY loc port
460                   (lambda ()
461                     (context-stack-push! (make-parse-context
462                                           (context no-layout) #f)))))
464 (define (is-ccurly->token c loc port)
465   (is-ref-char->token #\} c 'CCURLY loc port (lambda () (context-stack-pop!))))
467 (define (is-oparen->token c loc port)
468   (is-ref-char->token #\( c 'OPAREN loc port))
470 (define (is-cparen->token c loc port)
471   (is-ref-char->token #\) c 'CPAREN loc port))
473 (define (is-not->token c loc port)
474   (is-ref-char->token #\! c 'NOT loc port))
476 (define (is-version? c) (char-numeric? c))
478 ;; Main lexer functions
480 (define (lex-single-char port loc)
481   "Process tokens which can be recognised by peeking the next character on
482 PORT.  If no token can be recognized return #f.  LOC is the current port
483 location."
484   (let* ((c (peek-char port)))
485     (cond ((eof-object? c) (read-char port) '*eoi*)
486           ((is-ocurly->token c loc port))
487           ((is-ccurly->token c loc port))
488           ((is-oparen->token c loc port))
489           ((is-cparen->token c loc port))
490           ((is-not->token c loc port))
491           ((is-version? c) (lex-version loc port))
492           ((is-relation? c) (lex-relation loc port))
493           (else
494            #f))))
496 (define (lex-word port loc)
497   "Process tokens which can be recognized by reading the next word form PORT.
498 LOC is the current port location."
499   (let* ((w (read-delimited " ()\t\n" port 'peek)))
500     (cond ((is-if w) (lex-if loc))
501           ((is-test w port) (lex-test w loc))
502           ((is-true w) (lex-true loc))
503           ((is-false w) (lex-false loc))
504           ((is-and w) (lex-and loc))
505           ((is-or w) (lex-or loc))
506           ((is-id w) (lex-id w loc))
507           (else (unread-string w port) #f))))
509 (define (lex-line port loc)
510   "Process tokens which can be recognised by reading a line from PORT.  LOC is
511 the current port location."
512   (let* ((s (read-delimited "\n{}" port 'peek)))
513     (cond
514      ((is-property s) => (cut lex-property <> loc port))
515      ((is-flag s) => (cut lex-flag <> loc))
516      ((is-src-repo s) => (cut lex-src-repo <> loc))
517      ((is-exec s) => (cut lex-exec <> loc))
518      ((is-test-suite s) => (cut lex-test-suite <> loc))
519      ((is-benchmark s) => (cut lex-benchmark <> loc))
520      ((is-lib s) (lex-lib loc))
521      ((is-else s) (lex-else loc))
522      (else
523       #f))))
525 (define (lex-token port)
526   (let* ((loc (make-source-location (cabal-file-name) (port-line port)
527                                     (port-column port) -1 -1)))
528     (or (lex-single-char port loc) (lex-word port loc) (lex-line port loc))))
530 ;; Lexer- and error-function generators
532 (define (errorp)
533   "Generates the lexer error function."
534   (let ((p (current-error-port)))
535     (lambda (message . args)
536       (format p "~a" message)
537       (if (and (pair? args) (lexical-token? (car args)))
538           (let* ((token (car args))
539                  (source (lexical-token-source token))
540                  (line (source-location-line source))
541                  (column (source-location-column source)))
542             (format p "~a " (or (lexical-token-value token)
543                                  (lexical-token-category token)))
544             (when (and (number? line) (number? column))
545               (format p "(at line ~a, column ~a)" (1+ line) column)))
546           (for-each display args))
547       (format p "~%"))))
549 (define (make-lexer port)
550   "Generate the Cabal lexical analyser reading from PORT."
551   (let ((p port))
552     (lambda ()
553       (let ((bol (lex-white-space p (bol? p))))
554         (check-bol? #f)
555         (if bol (lex-bol p) (lex-token p))))))
557 (define* (read-cabal #:optional (port (current-input-port))
558                      (file-name #f))
559   "Read a Cabal file from PORT.  FILE-NAME is a string used in error messages.
560 If #f use the function 'port-filename' to obtain it."
561   (let ((cabal-parser (make-cabal-parser)))
562     (parameterize ((cabal-file-name
563                     (or file-name (port-filename port) "standard input"))
564                    (current-indentation 0)
565                    (check-bol? #f)
566                    (context-stack (make-stack)))
567       (cabal-parser (make-lexer port) (errorp)))))
569 ;; Part 2:
571 ;; Evaluate the S-expression returned by 'read-cabal'.
573 ;; This defines the object and interface that we provide to access the Cabal
574 ;; file information.  Note that this does not include all the pieces of
575 ;; information of the Cabal file, but only the ones we currently are
576 ;; interested in.
577 (define-record-type <cabal-package>
578   (make-cabal-package name version license home-page source-repository
579                       synopsis description
580                       executables lib test-suites
581                       flags eval-environment)
582   cabal-package?
583   (name   cabal-package-name)
584   (version cabal-package-version)
585   (license cabal-package-license)
586   (home-page cabal-package-home-page)
587   (source-repository cabal-package-source-repository)
588   (synopsis cabal-package-synopsis)
589   (description cabal-package-description)
590   (executables cabal-package-executables)
591   (lib cabal-package-library) ; 'library' is a Scheme keyword
592   (test-suites cabal-package-test-suites)
593   (flags cabal-package-flags)
594   (eval-environment cabal-package-eval-environment)) ; alist
596 (set-record-type-printer! <cabal-package>
597                           (lambda (package port)
598                             (format port "#<cabal-package ~a-~a>"
599                                       (cabal-package-name package)
600                                       (cabal-package-version package))))
602 (define-record-type <cabal-source-repository>
603   (make-cabal-source-repository use-case type location)
604   cabal-source-repository?
605   (use-case cabal-source-repository-use-case)
606   (type cabal-source-repository-type)
607   (location cabal-source-repository-location))
609 ;; We need to be able to distinguish the value of a flag from the Scheme #t
610 ;; and #f values.
611 (define-record-type <cabal-flag>
612   (make-cabal-flag name description default manual)
613   cabal-flag?
614   (name cabal-flag-name)
615   (description cabal-flag-description)
616   (default cabal-flag-default) ; 'true or 'false
617   (manual cabal-flag-manual))  ; 'true or 'false
619 (set-record-type-printer! <cabal-flag>
620                           (lambda (package port)
621                             (format port "#<cabal-flag ~a default:~a>"
622                                       (cabal-flag-name package)
623                                       (cabal-flag-default package))))
625 (define-record-type <cabal-dependency>
626   (make-cabal-dependency name version)
627   cabal-dependency?
628   (name cabal-dependency-name)
629   (version cabal-dependency-version))
631 (define-record-type <cabal-executable>
632   (make-cabal-executable name dependencies)
633   cabal-executable?
634   (name cabal-executable-name)
635   (dependencies cabal-executable-dependencies)) ; list of <cabal-dependency>
637 (define-record-type <cabal-library>
638   (make-cabal-library dependencies)
639   cabal-library?
640   (dependencies cabal-library-dependencies)) ; list of <cabal-dependency>
642 (define-record-type <cabal-test-suite>
643   (make-cabal-test-suite name dependencies)
644   cabal-test-suite?
645   (name cabal-test-suite-name)
646   (dependencies cabal-test-suite-dependencies)) ; list of <cabal-dependency>
648 (define (cabal-flags->alist flag-list)
649     "Retrun an alist associating the flag name to its default value from a
650 list of <cabal-flag> objects."
651   (map (lambda (flag) (cons (cabal-flag-name flag) (cabal-flag-default flag)))
652        flag-list))
654 (define (eval-cabal cabal-sexp env)
655   "Given the CABAL-SEXP produced by 'read-cabal', evaluate all conditionals
656 and return a 'cabal-package' object.  The values of all tests can be
657 overwritten by specifying the desired value in ENV.  ENV must be an alist.
658 The accepted keys are: \"os\", \"arch\", \"impl\" and a name of a flag.  The
659 value associated with a flag has to be either \"true\" or \"false\".  The
660 value associated with other keys has to conform to the Cabal file format
661 definition."
662   (define (os name)
663     (let ((env-os (or (assoc-ref env "os") "linux")))
664       (string-match env-os name)))
665   
666   (define (arch name)
667     (let ((env-arch (or (assoc-ref env "arch") "x86_64")))
668       (string-match env-arch name)))
670   (define (comp-name+version haskell)
671     "Extract the compiler name and version from the string HASKELL."
672     (let* ((matcher-fn (make-rx-matcher "([a-zA-Z0-9_]+)-([0-9.]+)"))
673            (name (or (and=> (matcher-fn haskell) (cut match:substring <> 1))
674                      haskell))
675            (version (and=> (matcher-fn haskell) (cut match:substring <> 2))))
676       (values name version)))
678   (define (comp-spec-name+op+version spec)
679     "Extract the compiler specification from SPEC.  Return the compiler name,
680 the ordering operation and the version."
681     (let* ((with-ver-matcher-fn (make-rx-matcher
682                                  "([a-zA-Z0-9_-]+) *([<>=]+) *([0-9.]+) *"))
683            (without-ver-matcher-fn (make-rx-matcher "([a-zA-Z0-9_-]+)"))
684            (name (or (and=> (with-ver-matcher-fn spec)
685                             (cut match:substring <> 1))
686                      (match:substring (without-ver-matcher-fn spec) 1)))
687            (operator (and=> (with-ver-matcher-fn spec)
688                             (cut match:substring <> 2)))
689            (version (and=> (with-ver-matcher-fn spec)
690                            (cut match:substring <> 3))))
691       (values name operator version)))
692   
693   (define (impl haskell)
694     (let*-values (((comp-name comp-ver)
695                    (comp-name+version (or (assoc-ref env "impl") "ghc")))
696                   ((spec-name spec-op spec-ver)
697                    (comp-spec-name+op+version haskell)))
698       (if (and spec-ver comp-ver)
699           (eval-string
700            (string-append "(string" spec-op " \"" comp-name "\""
701                           " \"" spec-name "-" spec-ver "\")"))
702           (string-match spec-name comp-name))))
703   
704   (define (cabal-flags)
705     (make-cabal-section cabal-sexp 'flag))
706   
707   (define (flag name)
708     (let ((value (or (assoc-ref env name)
709                      (assoc-ref (cabal-flags->alist (cabal-flags)) name))))
710       (if (eq? value 'false) #f #t)))
711   
712   (define (eval sexp)
713     (match sexp
714       (() '())
715       ;; nested 'if'
716       ((('if predicate true-group false-group) rest ...)
717        (append (if (eval predicate)
718                    (eval true-group)
719                    (eval false-group))
720                (eval rest)))
721       (('if predicate true-group false-group)
722        (if (eval predicate)
723            (eval true-group)
724            (eval false-group)))
725       (('flag name) (flag name))
726       (('os name) (os name))
727       (('arch name) (arch name))
728       (('impl name) (impl name))
729       ('true #t)
730       ('false #f)
731       (('not name) (not (eval name)))
732       ;; 'and' and 'or' aren't functions, thus we can't use apply
733       (('and args ...) (fold (lambda (e s) (and e s)) #t (eval args)))
734       (('or args ...) (fold (lambda (e s) (or e s)) #f (eval args)))
735       ;; no need to evaluate flag parameters
736       (('section 'flag name parameters)
737        (list 'section 'flag name parameters))
738       ;; library does not have a name parameter
739       (('section 'library parameters)
740        (list 'section 'library (eval parameters)))
741       (('section type name parameters)
742        (list 'section type name (eval parameters)))
743       (((? string? name) values)
744        (list name values))
745       ((element rest ...)
746        (cons (eval element) (eval rest)))
747       (_ (raise (condition
748                  (&message (message "Failed to evaluate Cabal file. \
749 See the manual for limitations.")))))))
751   (define (cabal-evaluated-sexp->package evaluated-sexp)
752     (let* ((name (lookup-join evaluated-sexp "name"))
753            (version (lookup-join evaluated-sexp "version"))
754            (license (lookup-join evaluated-sexp "license"))
755            (home-page (lookup-join evaluated-sexp "homepage"))
756            (home-page-or-hackage
757             (if (string-null? home-page)
758                 (string-append "http://hackage.haskell.org/package/" name)
759                 home-page))
760            (source-repository (make-cabal-section evaluated-sexp
761                                                   'source-repository))
762            (synopsis (lookup-join evaluated-sexp "synopsis"))
763            (description (lookup-join evaluated-sexp "description"))
764            (executables (make-cabal-section evaluated-sexp 'executable))
765            (lib (make-cabal-section evaluated-sexp 'library))
766            (test-suites (make-cabal-section evaluated-sexp 'test-suite))
767            (flags (make-cabal-section evaluated-sexp 'flag))
768            (eval-environment '()))
769       (make-cabal-package name version license home-page-or-hackage
770                           source-repository synopsis description executables lib
771                           test-suites flags eval-environment)))
773   ((compose cabal-evaluated-sexp->package eval) cabal-sexp))
775 (define (make-cabal-section sexp section-type)
776   "Given an SEXP as produced by 'read-cabal', produce a list of objects
777 pertaining to SECTION-TYPE sections.  SECTION-TYPE must be one of:
778 'executable, 'flag, 'test-suite, 'source-repository or 'library."
779   (filter-map (cut match <>
780                    (('section (? (cut equal? <> section-type)) name parameters)
781                     (case section-type
782                       ((test-suite) (make-cabal-test-suite
783                                       name (dependencies parameters)))
784                       ((executable) (make-cabal-executable
785                                       name (dependencies parameters)))
786                       ((source-repository) (make-cabal-source-repository
787                                             name
788                                             (lookup-join parameters "type")
789                                             (lookup-join parameters "location")))
790                       ((flag)
791                        (let* ((default (lookup-join parameters "default"))
792                               (default-true-or-false
793                                 (if (and default (string-ci=? "false" default))
794                                     'false
795                                     'true))
796                               (description (lookup-join parameters "description"))
797                               (manual (lookup-join parameters "manual"))
798                               (manual-true-or-false
799                                (if (and manual (string-ci=? "true" manual))
800                                    'true
801                                    'false)))
802                          (make-cabal-flag name description
803                                           default-true-or-false
804                                           manual-true-or-false)))
805                       (else #f)))
806                    (('section (? (cut equal? <> section-type) lib) parameters)
807                     (make-cabal-library (dependencies parameters)))
808                    (_ #f))
809               sexp))
811 (define* (lookup-join key-values-list key #:optional (delimiter " "))
812   "Lookup and joint all values pertaining to keys of value KEY in
813 KEY-VALUES-LIST.  The optional DELIMITER is used to specify a delimiter string
814 to be added between the values found in different key/value pairs."
815   (string-join 
816    (filter-map (cut match <> 
817                     (((? (lambda(x) (equal? x key))) value)
818                      (string-join value delimiter))
819                     (_ #f))
820                key-values-list)
821    delimiter))
823 (define dependency-name-version-rx
824   (make-regexp "([a-zA-Z0-9_-]+) *(.*)"))
826 (define (dependencies key-values-list)
827   "Return a list of 'cabal-dependency' objects for the dependencies found in
828 KEY-VALUES-LIST."
829   (let ((deps (string-tokenize (lookup-join key-values-list "build-depends" ",")
830                                (char-set-complement (char-set #\,)))))
831     (map (lambda (d)
832            (let ((rx-result (regexp-exec dependency-name-version-rx d)))
833              (make-cabal-dependency
834               (match:substring rx-result 1)
835               (match:substring rx-result 2))))
836          deps)))
838 ;;; cabal.scm ends here