cabal.scm 40 KB

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