2013-01-05 18:47:50 -05:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
|
|
|
|
;;; Copyright © 2012 Ludovic Courtès <ludo@gnu.org>
|
2012-06-01 17:08:32 -04:00
|
|
|
|
;;;
|
2013-01-05 18:47:50 -05:00
|
|
|
|
;;; This file is part of GNU Guix.
|
2012-06-01 17:08:32 -04:00
|
|
|
|
;;;
|
2013-01-05 18:47:50 -05:00
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
2012-06-01 17:08:32 -04:00
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
2013-01-05 18:47:50 -05:00
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
2012-06-01 17:08:32 -04:00
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
2013-01-05 18:47:50 -05:00
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
2012-06-01 17:08:32 -04:00
|
|
|
|
|
|
|
|
|
(define-module (guix utils)
|
2012-11-03 16:07:52 -04:00
|
|
|
|
#:use-module (guix config)
|
2012-06-01 17:29:55 -04:00
|
|
|
|
#:use-module (srfi srfi-1)
|
2012-06-27 17:40:12 -04:00
|
|
|
|
#:use-module (srfi srfi-9)
|
2012-06-01 17:29:55 -04:00
|
|
|
|
#:use-module (srfi srfi-26)
|
2012-06-07 17:15:00 -04:00
|
|
|
|
#:use-module (srfi srfi-39)
|
2012-06-01 17:08:32 -04:00
|
|
|
|
#:use-module (srfi srfi-60)
|
|
|
|
|
#:use-module (rnrs bytevectors)
|
2012-06-28 16:57:40 -04:00
|
|
|
|
#:use-module ((rnrs io ports) #:select (put-bytevector))
|
2012-06-09 09:25:51 -04:00
|
|
|
|
#:use-module (ice-9 vlist)
|
2012-06-01 17:29:55 -04:00
|
|
|
|
#:use-module (ice-9 format)
|
2012-06-07 17:15:00 -04:00
|
|
|
|
#:autoload (ice-9 popen) (open-pipe*)
|
|
|
|
|
#:autoload (ice-9 rdelim) (read-line)
|
2012-06-11 15:50:17 -04:00
|
|
|
|
#:use-module (ice-9 regex)
|
2012-06-27 17:40:12 -04:00
|
|
|
|
#:use-module (ice-9 match)
|
2012-07-01 11:32:03 -04:00
|
|
|
|
#:use-module (ice-9 format)
|
2012-06-29 16:58:27 -04:00
|
|
|
|
#:autoload (system foreign) (pointer->procedure)
|
Move base32 code to (guix base32).
* guix/utils.scm (bytevector-quintet-ref, bytevector-quintet-ref-right,
bytevector-quintet-length, bytevector-quintet-fold,
bytevector-quintet-fold-right, make-bytevector->base32-string,
%nix-base32-chars, %rfc4648-base32-chars, bytevector->base32-string,
bytevector->nix-base32-string, bytevector-quintet-set!,
bytevector-quintet-set-right!, base32-string-unfold,
base32-string-unfold-right, make-base32-string->bytevector,
base32-string->bytevector, nix-base32-string->bytevector): Move to...
* guix/base32.scm: ... here. New file.
* tests/utils.scm (%nix-hash, "bytevector->base32-string",
"base32-string->bytevector", "nix-base32-string->bytevector", "sha256
& bytevector->base32-string"): Move to...
* tests/base32.scm: ... here. New file
* guix-download.in, guix/derivations.scm, guix/packages.scm,
guix/snix.scm, tests/builders.scm, tests/derivations.scm: Adjust
accordingly.
* guix.scm (%public-modules): Add `base32'.
2012-11-11 16:33:28 -05:00
|
|
|
|
#:export (bytevector->base16-string
|
2012-06-09 10:34:18 -04:00
|
|
|
|
base16-string->bytevector
|
2012-06-07 17:15:00 -04:00
|
|
|
|
sha256
|
|
|
|
|
|
|
|
|
|
%nixpkgs-directory
|
|
|
|
|
nixpkgs-derivation
|
2012-07-11 11:26:43 -04:00
|
|
|
|
nixpkgs-derivation*
|
2012-06-07 17:15:00 -04:00
|
|
|
|
|
2012-06-27 17:40:12 -04:00
|
|
|
|
define-record-type*
|
2012-06-28 16:38:09 -04:00
|
|
|
|
compile-time-value
|
2012-06-11 15:50:17 -04:00
|
|
|
|
memoize
|
2012-11-05 17:46:55 -05:00
|
|
|
|
default-keyword-arguments
|
|
|
|
|
substitute-keyword-arguments
|
2012-09-01 13:21:06 -04:00
|
|
|
|
|
2012-11-19 17:02:59 -05:00
|
|
|
|
<location>
|
2012-09-01 13:21:06 -04:00
|
|
|
|
location
|
|
|
|
|
location?
|
|
|
|
|
location-file
|
|
|
|
|
location-line
|
|
|
|
|
location-column
|
|
|
|
|
source-properties->location
|
|
|
|
|
|
2012-06-11 15:50:17 -04:00
|
|
|
|
gnu-triplet->nix-system
|
2012-11-03 20:29:18 -04:00
|
|
|
|
%current-system
|
|
|
|
|
package-name->name+version))
|
2012-06-01 17:08:32 -04:00
|
|
|
|
|
2012-06-28 16:57:40 -04:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Compile-time computations.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define-syntax compile-time-value
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
"Evaluate the given expression at compile time. The expression must
|
|
|
|
|
evaluate to a simple datum."
|
|
|
|
|
((_ exp)
|
|
|
|
|
(let-syntax ((v (lambda (s)
|
|
|
|
|
(let ((val exp))
|
|
|
|
|
(syntax-case s ()
|
|
|
|
|
(_ #`'#,(datum->syntax s val)))))))
|
|
|
|
|
v))))
|
|
|
|
|
|
2012-06-01 17:29:55 -04:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Base 16.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define (bytevector->base16-string bv)
|
|
|
|
|
"Return the hexadecimal representation of BV's contents."
|
|
|
|
|
(define len
|
|
|
|
|
(bytevector-length bv))
|
|
|
|
|
|
|
|
|
|
(let-syntax ((base16-chars (lambda (s)
|
|
|
|
|
(syntax-case s ()
|
|
|
|
|
(_
|
|
|
|
|
(let ((v (list->vector
|
|
|
|
|
(unfold (cut > <> 255)
|
|
|
|
|
(lambda (n)
|
|
|
|
|
(format #f "~2,'0x" n))
|
|
|
|
|
1+
|
|
|
|
|
0))))
|
|
|
|
|
v))))))
|
|
|
|
|
(define chars base16-chars)
|
|
|
|
|
(let loop ((i 0)
|
|
|
|
|
(r '()))
|
|
|
|
|
(if (= i len)
|
|
|
|
|
(string-concatenate-reverse r)
|
|
|
|
|
(loop (+ 1 i)
|
|
|
|
|
(cons (vector-ref chars (bytevector-u8-ref bv i)) r))))))
|
|
|
|
|
|
2012-06-09 10:34:18 -04:00
|
|
|
|
(define base16-string->bytevector
|
|
|
|
|
(let ((chars->value (fold (lambda (i r)
|
|
|
|
|
(vhash-consv (string-ref (number->string i 16)
|
|
|
|
|
0)
|
|
|
|
|
i r))
|
|
|
|
|
vlist-null
|
|
|
|
|
(iota 16))))
|
|
|
|
|
(lambda (s)
|
|
|
|
|
"Return the bytevector whose hexadecimal representation is string S."
|
|
|
|
|
(define bv
|
|
|
|
|
(make-bytevector (quotient (string-length s) 2) 0))
|
|
|
|
|
|
|
|
|
|
(string-fold (lambda (chr i)
|
|
|
|
|
(let ((j (quotient i 2))
|
|
|
|
|
(v (and=> (vhash-assv chr chars->value) cdr)))
|
|
|
|
|
(if v
|
|
|
|
|
(if (zero? (logand i 1))
|
|
|
|
|
(bytevector-u8-set! bv j
|
|
|
|
|
(arithmetic-shift v 4))
|
|
|
|
|
(let ((w (bytevector-u8-ref bv j)))
|
|
|
|
|
(bytevector-u8-set! bv j (logior v w))))
|
|
|
|
|
(error "invalid hexadecimal character" chr)))
|
|
|
|
|
(+ i 1))
|
|
|
|
|
0
|
|
|
|
|
s)
|
|
|
|
|
bv)))
|
|
|
|
|
|
2012-06-01 17:29:55 -04:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Hash.
|
|
|
|
|
;;;
|
|
|
|
|
|
2012-06-29 16:58:27 -04:00
|
|
|
|
(define sha256
|
2012-11-03 16:43:30 -04:00
|
|
|
|
(let ((hash (pointer->procedure void
|
|
|
|
|
(dynamic-func "gcry_md_hash_buffer"
|
|
|
|
|
(dynamic-link %libgcrypt))
|
|
|
|
|
`(,int * * ,size_t)))
|
|
|
|
|
(sha256 8)) ; GCRY_MD_SHA256, as of 1.5.0
|
2012-06-29 16:58:27 -04:00
|
|
|
|
(lambda (bv)
|
|
|
|
|
"Return the SHA256 of BV as a bytevector."
|
2012-11-03 16:43:30 -04:00
|
|
|
|
(let ((digest (make-bytevector (/ 256 8))))
|
|
|
|
|
(hash sha256 (bytevector->pointer digest)
|
|
|
|
|
(bytevector->pointer bv) (bytevector-length bv))
|
|
|
|
|
digest))))
|
2012-06-07 17:15:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Nixpkgs.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define %nixpkgs-directory
|
2012-06-28 16:38:09 -04:00
|
|
|
|
(make-parameter
|
|
|
|
|
;; Capture the build-time value of $NIXPKGS.
|
2012-12-10 16:53:28 -05:00
|
|
|
|
(or %nixpkgs
|
|
|
|
|
(and=> (getenv "NIXPKGS")
|
|
|
|
|
(lambda (val)
|
|
|
|
|
;; Bail out when passed an empty string, otherwise
|
|
|
|
|
;; `nix-instantiate' will sit there and attempt to read
|
|
|
|
|
;; from its standard input.
|
|
|
|
|
(if (string=? val "")
|
|
|
|
|
#f
|
|
|
|
|
val))))))
|
2012-06-07 17:15:00 -04:00
|
|
|
|
|
2012-10-25 17:42:06 -04:00
|
|
|
|
(define* (nixpkgs-derivation attribute #:optional (system (%current-system)))
|
2012-06-07 17:15:00 -04:00
|
|
|
|
"Return the derivation path of ATTRIBUTE in Nixpkgs."
|
2012-08-14 12:36:01 -04:00
|
|
|
|
(let* ((p (open-pipe* OPEN_READ (or (getenv "NIX_INSTANTIATE")
|
2012-11-03 16:07:52 -04:00
|
|
|
|
%nix-instantiate)
|
2012-10-25 17:42:06 -04:00
|
|
|
|
"-A" attribute (%nixpkgs-directory)
|
|
|
|
|
"--argstr" "system" system))
|
2012-06-07 17:15:00 -04:00
|
|
|
|
(l (read-line p))
|
|
|
|
|
(s (close-pipe p)))
|
|
|
|
|
(and (zero? (status:exit-val s))
|
|
|
|
|
(not (eof-object? l))
|
|
|
|
|
l)))
|
|
|
|
|
|
2012-07-11 11:26:43 -04:00
|
|
|
|
(define-syntax-rule (nixpkgs-derivation* attribute)
|
|
|
|
|
"Evaluate the given Nixpkgs derivation at compile-time."
|
|
|
|
|
(compile-time-value (nixpkgs-derivation attribute)))
|
|
|
|
|
|
2012-06-07 17:15:00 -04:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Miscellaneous.
|
|
|
|
|
;;;
|
|
|
|
|
|
2012-06-27 17:40:12 -04:00
|
|
|
|
(define-syntax define-record-type*
|
|
|
|
|
(lambda (s)
|
|
|
|
|
"Define the given record type such that an additional \"syntactic
|
|
|
|
|
constructor\" is defined, which allows instances to be constructed with named
|
|
|
|
|
field initializers, à la SRFI-35, as well as default values."
|
2012-08-29 18:18:50 -04:00
|
|
|
|
(define (make-syntactic-constructor type name ctor fields defaults)
|
|
|
|
|
"Make the syntactic constructor NAME for TYPE, that calls CTOR, and
|
|
|
|
|
expects all of FIELDS to be initialized. DEFAULTS is the list of
|
|
|
|
|
FIELD/DEFAULT-VALUE tuples."
|
|
|
|
|
(with-syntax ((type type)
|
|
|
|
|
(name name)
|
2012-06-27 17:40:12 -04:00
|
|
|
|
(ctor ctor)
|
|
|
|
|
(expected fields)
|
|
|
|
|
(defaults defaults))
|
2012-08-29 18:18:50 -04:00
|
|
|
|
#`(define-syntax name
|
2012-06-27 17:40:12 -04:00
|
|
|
|
(lambda (s)
|
2012-08-29 18:18:50 -04:00
|
|
|
|
(define (record-inheritance orig-record field+value)
|
|
|
|
|
;; Produce code that returns a record identical to
|
|
|
|
|
;; ORIG-RECORD, except that values for the FIELD+VALUE alist
|
|
|
|
|
;; prevail.
|
|
|
|
|
(define (field-inherited-value f)
|
|
|
|
|
(and=> (find (lambda (x)
|
|
|
|
|
(eq? f (car (syntax->datum x))))
|
|
|
|
|
field+value)
|
|
|
|
|
car))
|
|
|
|
|
|
|
|
|
|
#`(make-struct type 0
|
|
|
|
|
#,@(map (lambda (field index)
|
|
|
|
|
(or (field-inherited-value field)
|
|
|
|
|
#`(struct-ref #,orig-record
|
|
|
|
|
#,index)))
|
|
|
|
|
'expected
|
|
|
|
|
(iota (length 'expected)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(syntax-case s (inherit #,@fields)
|
|
|
|
|
((_ (inherit orig-record) (field value) (... ...))
|
|
|
|
|
#`(letrec* ((field value) (... ...))
|
|
|
|
|
#,(record-inheritance #'orig-record
|
|
|
|
|
#'((field value) (... ...)))))
|
2012-06-27 17:40:12 -04:00
|
|
|
|
((_ (field value) (... ...))
|
2012-07-01 11:32:03 -04:00
|
|
|
|
(let ((fields (map syntax->datum #'(field (... ...))))
|
|
|
|
|
(dflt (map (match-lambda
|
|
|
|
|
((f v)
|
|
|
|
|
(list (syntax->datum f) v)))
|
|
|
|
|
#'defaults)))
|
|
|
|
|
|
2012-08-29 18:18:50 -04:00
|
|
|
|
(define (field-value f)
|
|
|
|
|
(or (and=> (find (lambda (x)
|
|
|
|
|
(eq? f (car (syntax->datum x))))
|
|
|
|
|
#'((field value) (... ...)))
|
|
|
|
|
car)
|
|
|
|
|
(car (assoc-ref dflt (syntax->datum f)))))
|
2012-06-27 17:40:12 -04:00
|
|
|
|
|
2012-07-01 11:32:03 -04:00
|
|
|
|
(let-syntax ((error*
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
((_ fmt args (... ...))
|
|
|
|
|
(syntax-violation 'name
|
|
|
|
|
(format #f fmt args
|
|
|
|
|
(... ...))
|
|
|
|
|
s)))))
|
|
|
|
|
(let ((fields (append fields (map car dflt))))
|
|
|
|
|
(cond ((lset= eq? fields 'expected)
|
2012-07-01 11:32:03 -04:00
|
|
|
|
#`(letrec* ((field value) (... ...))
|
|
|
|
|
(ctor #,@(map field-value 'expected))))
|
2012-07-01 11:32:03 -04:00
|
|
|
|
((pair? (lset-difference eq? fields 'expected))
|
|
|
|
|
(error* "extraneous field initializers ~a"
|
|
|
|
|
(lset-difference eq? fields 'expected)))
|
|
|
|
|
(else
|
|
|
|
|
(error* "missing field initializers ~a"
|
|
|
|
|
(lset-difference eq? 'expected
|
|
|
|
|
fields)))))))))))))
|
2012-06-27 17:40:12 -04:00
|
|
|
|
|
|
|
|
|
(define (field-default-value s)
|
|
|
|
|
(syntax-case s (default)
|
|
|
|
|
((field (default val) _ ...)
|
|
|
|
|
(list #'field #'val))
|
|
|
|
|
((field _ options ...)
|
|
|
|
|
(field-default-value #'(field options ...)))
|
|
|
|
|
(_ #f)))
|
|
|
|
|
|
|
|
|
|
(syntax-case s ()
|
|
|
|
|
((_ type syntactic-ctor ctor pred
|
|
|
|
|
(field get options ...) ...)
|
|
|
|
|
#`(begin
|
|
|
|
|
(define-record-type type
|
|
|
|
|
(ctor field ...)
|
|
|
|
|
pred
|
|
|
|
|
(field get) ...)
|
2012-08-29 18:18:50 -04:00
|
|
|
|
#,(make-syntactic-constructor #'type #'syntactic-ctor #'ctor
|
2012-06-27 17:40:12 -04:00
|
|
|
|
#'(field ...)
|
|
|
|
|
(filter-map field-default-value
|
|
|
|
|
#'((field options ...)
|
|
|
|
|
...))))))))
|
|
|
|
|
|
2012-06-07 17:15:00 -04:00
|
|
|
|
(define (memoize proc)
|
|
|
|
|
"Return a memoizing version of PROC."
|
|
|
|
|
(let ((cache (make-hash-table)))
|
|
|
|
|
(lambda args
|
|
|
|
|
(let ((results (hash-ref cache args)))
|
|
|
|
|
(if results
|
|
|
|
|
(apply values results)
|
|
|
|
|
(let ((results (call-with-values (lambda ()
|
|
|
|
|
(apply proc args))
|
|
|
|
|
list)))
|
|
|
|
|
(hash-set! cache args results)
|
|
|
|
|
(apply values results)))))))
|
2012-06-11 15:50:17 -04:00
|
|
|
|
|
2012-11-05 17:46:55 -05:00
|
|
|
|
(define (default-keyword-arguments args defaults)
|
|
|
|
|
"Return ARGS augmented with any keyword/value from DEFAULTS for
|
|
|
|
|
keywords not already present in ARGS."
|
|
|
|
|
(let loop ((defaults defaults)
|
|
|
|
|
(args args))
|
|
|
|
|
(match defaults
|
|
|
|
|
((kw value rest ...)
|
|
|
|
|
(loop rest
|
|
|
|
|
(if (assoc-ref kw args)
|
|
|
|
|
args
|
|
|
|
|
(cons* kw value args))))
|
|
|
|
|
(()
|
|
|
|
|
args))))
|
|
|
|
|
|
|
|
|
|
(define-syntax substitute-keyword-arguments
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
"Return a new list of arguments where the value for keyword arg KW is
|
|
|
|
|
replaced by EXP. EXP is evaluated in a context where VAR is boud to the
|
|
|
|
|
previous value of the keyword argument."
|
|
|
|
|
((_ original-args ((kw var) exp) ...)
|
|
|
|
|
(let loop ((args original-args)
|
|
|
|
|
(before '()))
|
|
|
|
|
(match args
|
|
|
|
|
((kw var rest (... ...))
|
|
|
|
|
(loop rest (cons* exp kw before)))
|
|
|
|
|
...
|
|
|
|
|
((x rest (... ...))
|
|
|
|
|
(loop rest (cons x before)))
|
|
|
|
|
(()
|
|
|
|
|
(reverse before)))))))
|
|
|
|
|
|
2012-06-11 15:50:17 -04:00
|
|
|
|
(define (gnu-triplet->nix-system triplet)
|
|
|
|
|
"Return the Nix system type corresponding to TRIPLET, a GNU triplet as
|
|
|
|
|
returned by `config.guess'."
|
|
|
|
|
(let ((triplet (cond ((string-match "^i[345]86-(.*)$" triplet)
|
|
|
|
|
=>
|
|
|
|
|
(lambda (m)
|
|
|
|
|
(string-append "i686-" (match:substring m 1))))
|
|
|
|
|
(else triplet))))
|
|
|
|
|
(cond ((string-match "^([^-]+)-([^-]+-)?linux-gnu.*" triplet)
|
|
|
|
|
=>
|
|
|
|
|
(lambda (m)
|
|
|
|
|
;; Nix omits `-gnu' for GNU/Linux.
|
|
|
|
|
(string-append (match:substring m 1) "-linux")))
|
|
|
|
|
((string-match "^([^-]+)-([^-]+-)?([[:alpha:]]+)([0-9]+\\.?)*$" triplet)
|
|
|
|
|
=>
|
|
|
|
|
(lambda (m)
|
|
|
|
|
;; Nix strip the version number from names such as `gnu0.3',
|
|
|
|
|
;; `darwin10.2.0', etc., and always strips the vendor part.
|
|
|
|
|
(string-append (match:substring m 1) "-"
|
|
|
|
|
(match:substring m 3))))
|
|
|
|
|
(else triplet))))
|
|
|
|
|
|
|
|
|
|
(define %current-system
|
|
|
|
|
;; System type as expected by Nix, usually ARCHITECTURE-KERNEL.
|
2012-12-05 17:02:47 -05:00
|
|
|
|
;; By default, this is equal to (gnu-triplet->nix-system %host-type).
|
|
|
|
|
(make-parameter %system))
|
2012-09-01 13:21:06 -04:00
|
|
|
|
|
2012-11-03 20:29:18 -04:00
|
|
|
|
(define (package-name->name+version name)
|
|
|
|
|
"Given NAME, a package name like \"foo-0.9.1b\", return two values:
|
|
|
|
|
\"foo\" and \"0.9.1b\". When the version part is unavailable, NAME and
|
|
|
|
|
#f are returned. The first hyphen followed by a digit is considered to
|
|
|
|
|
introduce the version part."
|
|
|
|
|
;; See also `DrvName' in Nix.
|
|
|
|
|
|
|
|
|
|
(define number?
|
|
|
|
|
(cut char-set-contains? char-set:digit <>))
|
|
|
|
|
|
|
|
|
|
(let loop ((chars (string->list name))
|
|
|
|
|
(prefix '()))
|
|
|
|
|
(match chars
|
|
|
|
|
(()
|
|
|
|
|
(values name #f))
|
|
|
|
|
((#\- (? number? n) rest ...)
|
|
|
|
|
(values (list->string (reverse prefix))
|
|
|
|
|
(list->string (cons n rest))))
|
|
|
|
|
((head tail ...)
|
|
|
|
|
(loop tail (cons head prefix))))))
|
|
|
|
|
|
2012-09-01 13:21:06 -04:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Source location.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
;; A source location.
|
|
|
|
|
(define-record-type <location>
|
|
|
|
|
(make-location file line column)
|
|
|
|
|
location?
|
|
|
|
|
(file location-file) ; file name
|
|
|
|
|
(line location-line) ; 1-indexed line
|
|
|
|
|
(column location-column)) ; 0-indexed column
|
|
|
|
|
|
|
|
|
|
(define location
|
|
|
|
|
(memoize
|
|
|
|
|
(lambda (file line column)
|
|
|
|
|
"Return the <location> object for the given FILE, LINE, and COLUMN."
|
|
|
|
|
(and line column file
|
|
|
|
|
(make-location file line column)))))
|
|
|
|
|
|
|
|
|
|
(define (source-properties->location loc)
|
|
|
|
|
"Return a location object based on the info in LOC, an alist as returned
|
|
|
|
|
by Guile's `source-properties', `frame-source', `current-source-location',
|
|
|
|
|
etc."
|
|
|
|
|
(let ((file (assq-ref loc 'filename))
|
|
|
|
|
(line (assq-ref loc 'line))
|
|
|
|
|
(col (assq-ref loc 'column)))
|
2012-10-27 08:53:47 -04:00
|
|
|
|
;; In accordance with the GCS, start line and column numbers at 1.
|
|
|
|
|
(location file (and line (+ line 1)) (and col (+ col 1)))))
|