aboutsummaryrefslogtreecommitdiff
path: root/src/guile/skribilo/reader.scm
blob: 7cc96f623f443a1db34d87fb7ff225199dba8fcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
;;; reader.scm  --  Skribilo's front-end (aka. reader) interface.
;;; -*- coding: iso-8859-1 -*-
;;;
;;; Copyright 2005, 2009, 2016 Ludovic Court�s <ludo@gnu.org>
;;;
;;;
;;; This file is part of Skribilo.
;;;
;;; Skribilo is free software: you can redistribute it and/or modify
;;; it 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.
;;;
;;; Skribilo is distributed in the hope that it will be useful,
;;; but 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
;;; along with Skribilo.  If not, see <http://www.gnu.org/licenses/>.

(define-module (skribilo reader)
  #:use-module (srfi srfi-9)  ;; records
  #:use-module (srfi srfi-39) ;; parameter objects
  #:use-module (skribilo condition)
  #:autoload   (srfi srfi-34) (raise)
  #:use-module (srfi srfi-35)
  #:export (%make-reader lookup-reader make-reader
	   *document-reader*
           reader:name
           reader:version
           reader:make

	   &reader-search-error reader-search-error?
	   reader-search-error:reader

           define-reader define-public-reader reader?))

;;; Author:  Ludovic Court�s
;;;
;;; Commentary:
;;;
;;; This module contains Skribilo's front-end (aka. ``reader'') interface.
;;; Skribilo's default reader is `(skribilo reader skribe)' which provides a
;;; reader for the Skribe syntax.
;;;
;;; Code:

(define-record-type <reader>
  (%make-reader name version make)
  reader?
  (name      reader:name      reader:set-name!)    ;; a symbol
  (version   reader:version   reader:set-version!) ;; a string
  (make      reader:make      reader:set-make!))   ;; a one-argument proc
                                                   ;; that returns a reader
                                                   ;; proc

(define-macro (define-reader name version make-proc)
  `(define reader-specification
     (%make-reader (quote ,name) ,version ,make-proc)))

(define-macro (define-public-reader name version make-proc)
  `(define-reader ,name ,version ,make-proc))


;;; Error condition.

(define-condition-type &reader-search-error &skribilo-error
  reader-search-error?
  (reader reader-search-error:reader))



;;; The mechanism below is inspired by Guile-VM code written by K. Nishida.

(define (lookup-reader name)
  "Look for a reader named @var{name} (a symbol) in the @code{(skribilo
reader)} module hierarchy.  If no such reader was found, an error is
raised."
  (let ((m (false-if-exception
	    (resolve-module `(skribilo reader ,name)))))
    (if (and (module? m)
	     (module-bound? m 'reader-specification))
	(module-ref m 'reader-specification)
	(raise (condition (&reader-search-error (reader name)))))))

(define (make-reader name)
  "Look for reader @var{name} and instantiate it."
  (let* ((spec (lookup-reader name))
         (make (reader:make spec)))
    (make)))


;;; Current document reader.

(define *document-reader* (make-parameter #f))


;;; reader.scm ends here