cabal.scm 41 KB

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