cabal.scm 39 KB

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