From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: ludovic.courtes@laas.fr (Ludovic =?iso-8859-1?Q?Court=E8s?=) Newsgroups: gmane.lisp.guile.devel Subject: [PATCH] Per-module reader Date: Tue, 13 Sep 2005 14:51:32 +0200 Organization: LAAS-CNRS Message-ID: <87u0gp9lm3.fsf@laas.fr> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: sea.gmane.org 1126617641 12357 80.91.229.2 (13 Sep 2005 13:20:41 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 13 Sep 2005 13:20:41 +0000 (UTC) Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Tue Sep 13 15:20:32 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1EFAhx-0005nf-Gx for guile-devel@m.gmane.org; Tue, 13 Sep 2005 15:20:22 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1EFAhv-0006rO-4I for guile-devel@m.gmane.org; Tue, 13 Sep 2005 09:20:19 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1EFAhE-0006af-F8 for guile-devel@gnu.org; Tue, 13 Sep 2005 09:19:36 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1EFAhD-0006aG-Ko for guile-devel@gnu.org; Tue, 13 Sep 2005 09:19:36 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1EFAXF-0003An-8W for guile-devel@gnu.org; Tue, 13 Sep 2005 09:09:17 -0400 Original-Received: from [140.93.0.15] (helo=laas.laas.fr) by monty-python.gnu.org with esmtp (TLS-1.0:DHE_RSA_3DES_EDE_CBC_SHA:24) (Exim 4.34) id 1EFAGI-0008Al-Nc for guile-devel@gnu.org; Tue, 13 Sep 2005 08:51:47 -0400 Original-Received: by laas.laas.fr (8.13.1/8.13.1) with SMTP id j8DCpgjk007162; Tue, 13 Sep 2005 14:51:44 +0200 (CEST) Original-To: guile-devel@gnu.org X-URL: http://www.laas.fr/~lcourtes/ X-Revolutionary-Date: 27 Fructidor an 213 de la =?iso-8859-1?Q?R=E9volutio?= =?iso-8859-1?Q?n?= X-PGP-Key-ID: 0xEB1F5364 X-PGP-Key: http://www.laas.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 821D 815D 902A 7EAB 5CEE D120 7FBA 3D4F EB1F 5364 X-OS: powerpc-unknown-linux-gnu User-Agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux) X-Spam-Score: 0 () X-Scanned-By: MIMEDefang at CNRS-LAAS X-MIME-Autoconverted: from 8bit to quoted-printable by laas.laas.fr id j8DCpgjk007162 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:5221 Archived-At: Hi, The patch below adds a `#:reader' keyword to `define-module' that allows modules to specify the reader that should be used to interpret them: (define-module (the-module) #:reader the-reader-for-my-favorite-syntax) This way, module implementors can decide to use whatever syntax variant they prefer, while not having any side-effect on the code being read from other files or modules. For illustration, here is an example that makes use of my `guile-reader' thing: --8<---------------cut here---------------start------------->8--- (define-module (module-with-reader) #:reader (let* ((elisp-char-tr (make-token-reader #\? (token-reader-procedure (standard-token-reader 'character))))) (make-reader (cons elisp-char-tr (map standard-token-reader '(whitespace sexp string number colon-keyword semicolon-comment symbol-upper-case symbol-lower-case quote-quasiquote-unquote))))) #:use-module (system reader)) (define-public (return-a-colon-keyword) :a-keyword!) (define-public (return-an-elisp-character) ?a) --8<---------------cut here---------------end--------------->8--- For the module user, things are completely transparent: $ guile -L . guile> (read-options)=20 (keywords #f positions) guile> (use-modules (module-with-reader)) guile> (return-a-colon-keyword) #:a-keyword! guile> (return-an-elisp-character) #\a guile> (module-reader (resolve-module '(module-with-reader))) # guile> (module-reader (current-module)) # The patch has a slight impact on `boot-9.scm' and `load.c'. The procedure `primitive-load' (which is called by `load-module') is modified in order to switch to the current module's reader as soon as the current module changes. Feedback welcome! ;-) Thanks, Ludovic. ChangeLog entry for `libguile': 2005-10-13 Ludovic Court=E8s * load.c (scm_primitive_load): Switch to the current module's reader when the current module changes. * module.h (scm_module_index_reader): New macro. (SCM_MODULE_READER): New macro. ChangeLog entry for `ice-9': 2005-10-13 Ludovic Court=E8s * boot-9.scm (module-type): New field `reader'. Updated callers of `module-constructor'. (module-reader): New procedure. (set-module-reader!): New procedure. (process-define-module): Handle the `#:reader' keyword. Index: ice-9/boot-9.scm =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvsroot/guile/guile/guile-core/ice-9/boot-9.scm,v retrieving revision 1.351 diff -u -B -b -p -r1.351 boot-9.scm --- ice-9/boot-9.scm 31 Jul 2005 23:36:50 -0000 1.351 +++ ice-9/boot-9.scm 13 Sep 2005 12:28:08 -0000 @@ -1185,7 +1185,8 @@ (make-record-type 'module '(obarray uses binder eval-closure transformer name kind duplicates-handlers duplicates-interface - observers weak-observers observer-id) + observers weak-observers observer-id + reader) %print-module)) =20 ;; make-module &opt size uses binder @@ -1221,7 +1222,8 @@ uses binder #f #f #f #f #f #f '() (make-weak-value-hash-table 31) - 0))) + 0 + read))) =20 ;; We can't pass this as an argument to module-constructor, ;; because we need it to close over a pointer to the module @@ -1247,6 +1249,8 @@ (define set-module-name! (record-modifier module-type 'name)) (define module-kind (record-accessor module-type 'kind)) (define set-module-kind! (record-modifier module-type 'kind)) +(define module-reader (record-accessor module-type 'reader)) +(define set-module-reader! (record-modifier module-type 'reader)) (define module-duplicates-handlers (record-accessor module-type 'duplicates-handlers)) (define set-module-duplicates-handlers! @@ -2042,10 +2046,22 @@ (call-with-deferred-observers (lambda () (module-use-interfaces! module (reverse reversed-interfaces)) + ;; Evaluate the `#:reader' argument in the context of the module + ;; being defined. + (set-module-reader! module + (eval (module-reader module) module)) (module-export! module exports) (module-replace! module replacements) (module-re-export! module re-exports))) (case (car kws) + ((#:reader) + ;; The argument to `#:reader' will be evaluated eventually. + (set-module-reader! module (cadr kws)) + (loop (cddr kws) + reversed-interfaces + exports + re-exports + replacements)) ((#:use-module #:use-syntax) (or (pair? (cdr kws)) (unrecognized kws)) @@ -2138,7 +2154,7 @@ (set-car! (memq a (module-uses module)) i) (module-local-variable i sym)))))) (module-constructor (make-hash-table 0) '() b #f #f name 'autoload #= f #f - '() (make-weak-value-hash-table 31) 0))) + '() (make-weak-value-hash-table 31) 0 read))) =20 ;;; {Compiled module} =20 Index: libguile/load.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvsroot/guile/guile/guile-core/libguile/load.c,v retrieving revision 1.86 diff -u -B -b -p -r1.86 load.c --- libguile/load.c 23 May 2005 19:57:20 -0000 1.86 +++ libguile/load.c 13 Sep 2005 12:28:08 -0000 @@ -82,15 +82,29 @@ SCM_DEFINE (scm_primitive_load, "primiti scm_call_1 (hook, filename); =20 { /* scope */ + SCM module =3D SCM_BOOL_F, reader =3D SCM_BOOL_F; SCM port =3D scm_open_file (filename, scm_from_locale_string ("r")); scm_frame_begin (SCM_F_FRAME_REWINDABLE); scm_i_frame_current_load_port (port); =20 while (1) { - SCM form =3D scm_read (port); + SCM form; + + if (scm_current_module () !=3D module) + { + module =3D scm_current_module (); + reader =3D SCM_MODULE_READER (module); + } + + if (reader =3D=3D SCM_BOOL_F) + form =3D scm_read (port); + else + form =3D scm_call_1 (reader, port); + if (SCM_EOF_OBJECT_P (form)) break; + scm_primitive_eval_x (form); } =20 Index: libguile/modules.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvsroot/guile/guile/guile-core/libguile/modules.h,v retrieving revision 1.28 diff -u -B -b -p -r1.28 modules.h --- libguile/modules.h 31 Jul 2005 23:36:14 -0000 1.28 +++ libguile/modules.h 13 Sep 2005 12:28:08 -0000 @@ -45,6 +45,7 @@ SCM_API scm_t_bits scm_module_tag; #define scm_module_index_binder 2 #define scm_module_index_eval_closure 3 #define scm_module_index_transformer 4 +#define scm_module_index_reader 12 =20 #define SCM_MODULE_OBARRAY(module) \ SCM_PACK (SCM_STRUCT_DATA (module) [scm_module_index_obarray]) @@ -56,6 +57,8 @@ SCM_API scm_t_bits scm_module_tag; SCM_PACK (SCM_STRUCT_DATA (module)[scm_module_index_eval_closure]) #define SCM_MODULE_TRANSFORMER(module) \ SCM_PACK (SCM_STRUCT_DATA (module)[scm_module_index_transformer]) +#define SCM_MODULE_READER(module) \ + SCM_PACK (SCM_STRUCT_DATA (module)[scm_module_index_reader]) =20 SCM_API scm_t_bits scm_tc16_eval_closure; =20 _______________________________________________ Guile-devel mailing list Guile-devel@gnu.org http://lists.gnu.org/mailman/listinfo/guile-devel