I wrote some function that could replace the function read of common lisp
(defun my-read (stream &rest args) (declare (ignore args)) (funcall (my-get-macro-character (read-char stream)))) Is there a way to use this function as default reader?
You can't redefine the built in functions1, but you can define a package that shadows cl:read and defines a new function my:read, so that when you use that package, it looks like it's the default read function. E.g., something like this:
CL-USER> (defpackage #:my-package (:use "COMMON-LISP") (:shadow #:read) (:export #:read)) ;=> #<PACKAGE "MY-PACKAGE"> CL-USER> (defun my-package:read (&rest args) (declare (ignore args)) 42) ;=> MY-PACKAGE:READ CL-USER> (defpackage #:another-package (:use #:my-package "COMMON-LISP") (:shadowing-import-from #:my-package #:read)) ;=> #<PACKAGE "ANOTHER-PACKAGE"> CL-USER> (in-package #:another-package) ;=> #<PACKAGE "ANOTHER-PACKAGE"> ANOTHER-PACKAGE> (read) ;=> 42 (in-package #:my-package) at the top of that file.One approach is to use set-macro-character on all "valid" input characters in a readtable. (This is okay if you only accept ASCII input, but I don't know if it would be practical for full Unicode.)
Something like this:
(defun replace-default-read-behavior (rt fn) (loop for c across " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" do (set-macro-character c fn t rt))) (defun my-parser (stream char) (format t "custom read: ~A~A" char (read-line stream))) (defun my-get-macro-character (char) (declare (ignore char)) #'my-parser) (defun my-read (stream char) (funcall (my-get-macro-character char) stream char)) (defvar *my-readtable* (copy-readtable ())) (replace-default-read-behavior *my-readtable* #'my-read) (let ((*readtable* *my-readtable*)) (read-from-string "foo")) custom read: foo ; printed NIL ; returned 3 my-read to ingest the entire input you can loosen the requirement to say just the first character of input has to be one you expect. That's still not satisfying, but I don't know of a better way to do it.
loadorcompile-filemight have the reader inlined, or they might refer to internal reader definitions, e.g. to bypass the optional/key argument handling overhead.