cabal.scm 38 KB

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