You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

910 lines
38 KiB

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