| 1 | # Named Readtables Manual |
|---|
| 2 | |
|---|
| 3 | ###### \[in package EDITOR-HINTS.NAMED-READTABLES\] |
|---|
| 4 | ## named-readtables ASDF System Details |
|---|
| 5 | |
|---|
| 6 | - Version: 0.9 |
|---|
| 7 | - Description: Library that creates a namespace for named readtable |
|---|
| 8 | akin to the namespace of packages. |
|---|
| 9 | - Licence: BSD, see LICENSE |
|---|
| 10 | - Author: Tobias C. Rittweiler <trittweiler@common-lisp.net> |
|---|
| 11 | - Maintainer: Gábor Melis |
|---|
| 12 | - Mailto: [mega@retes.hu](mailto:mega@retes.hu) |
|---|
| 13 | |
|---|
| 14 | ## Introduction |
|---|
| 15 | |
|---|
| 16 | Named-Readtables is a library that provides a namespace for |
|---|
| 17 | readtables akin to the already-existing namespace of packages. In |
|---|
| 18 | particular: |
|---|
| 19 | |
|---|
| 20 | - you can associate readtables with names, and retrieve |
|---|
| 21 | readtables by names; |
|---|
| 22 | |
|---|
| 23 | - you can associate source files with readtable names, and be |
|---|
| 24 | sure that the right readtable is active when compiling/loading |
|---|
| 25 | the file; |
|---|
| 26 | |
|---|
| 27 | - similiarly, your development environment now has a chance to |
|---|
| 28 | automatically determine what readtable should be active while |
|---|
| 29 | processing source forms on interactive commands. (E.g. think of |
|---|
| 30 | `C-c C-c` in Slime (yet to be done)) |
|---|
| 31 | |
|---|
| 32 | It follows that Named-Readtables is a facility for using readtables in |
|---|
| 33 | a localized way. |
|---|
| 34 | |
|---|
| 35 | Additionally, it also attempts to become a facility for using |
|---|
| 36 | readtables in a *modular* way. In particular: |
|---|
| 37 | |
|---|
| 38 | - it provides a macro to specify the content of a readtable at a |
|---|
| 39 | glance; |
|---|
| 40 | |
|---|
| 41 | - it makes it possible to use multiple inheritance between readtables. |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | ### Links |
|---|
| 45 | |
|---|
| 46 | Here is the [official repository][named-readtables-repo] and the |
|---|
| 47 | [HTML documentation][named-readtables-doc] for the latest version. |
|---|
| 48 | |
|---|
| 49 | [named-readtables-repo]: https://github.com/melisgl/named-readtables |
|---|
| 50 | |
|---|
| 51 | [named-readtables-doc]: http://melisgl.github.io/mgl-pax-world/named-readtables-manual.html |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | ### Acknowledgements |
|---|
| 55 | |
|---|
| 56 | Thanks to Robert Goldman for making me want to write this library. |
|---|
| 57 | |
|---|
| 58 | Thanks to Stephen Compall, Ariel Badichi, David Lichteblau, Bart |
|---|
| 59 | Botta, David Crawford, and Pascal Costanza for being early adopters, |
|---|
| 60 | providing comments and bugfixes. |
|---|
| 61 | |
|---|
| 62 | ## Overview |
|---|
| 63 | |
|---|
| 64 | ### Notes on the API |
|---|
| 65 | |
|---|
| 66 | The API heavily imitates the API of packages. This has the nice |
|---|
| 67 | property that any experienced Common Lisper will take it up without |
|---|
| 68 | effort. |
|---|
| 69 | |
|---|
| 70 | DEFREADTABLE - DEFPACKAGE |
|---|
| 71 | |
|---|
| 72 | IN-READTABLE - IN-PACKAGE |
|---|
| 73 | |
|---|
| 74 | MERGE-READTABLES-INTO - USE-PACKAGE |
|---|
| 75 | |
|---|
| 76 | MAKE-READTABLE - MAKE-PACKAGE |
|---|
| 77 | |
|---|
| 78 | UNREGISTER-READTABLE - DELETE-PACKAGE |
|---|
| 79 | |
|---|
| 80 | RENAME-READTABLE - RENAME-PACKAGE |
|---|
| 81 | |
|---|
| 82 | FIND-READTABLE - FIND-PACKAGE |
|---|
| 83 | |
|---|
| 84 | READTABLE-NAME - PACKAGE-NAME |
|---|
| 85 | |
|---|
| 86 | LIST-ALL-NAMED-READTABLES - LIST-ALL-PACKAGES |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | ### Important API idiosyncrasies |
|---|
| 90 | |
|---|
| 91 | There are three major differences between the API of Named-Readtables, |
|---|
| 92 | and the API of packages. |
|---|
| 93 | |
|---|
| 94 | 1. Readtable names are symbols not strings. |
|---|
| 95 | |
|---|
| 96 | Time has shown that the fact that packages are named by strings |
|---|
| 97 | causes severe headache because of the potential of package names |
|---|
| 98 | colliding with each other. |
|---|
| 99 | |
|---|
| 100 | Hence, readtables are named by symbols lest to make the |
|---|
| 101 | situation worse than it already is. Consequently, readtables |
|---|
| 102 | named `CL-ORACLE:SQL-SYNTAX` and `CL-MYSQL:SQL-SYNTAX` can |
|---|
| 103 | happily coexist next to each other. Or, taken to an extreme, |
|---|
| 104 | `SCHEME:SYNTAX` and `ELISP:SYNTAX`. |
|---|
| 105 | |
|---|
| 106 | If, for example to duly signify the importance of your cool |
|---|
| 107 | readtable hack, you really think it deserves a global name, you |
|---|
| 108 | can always resort to keywords. |
|---|
| 109 | |
|---|
| 110 | 2. The inheritance is resolved statically, not dynamically. |
|---|
| 111 | |
|---|
| 112 | A package that uses another package will have access to all the |
|---|
| 113 | other package's exported symbols, even to those that will be |
|---|
| 114 | added after its definition. I.e. the inheritance is resolved at |
|---|
| 115 | run-time, that is dynamically. |
|---|
| 116 | |
|---|
| 117 | Unfortunately, we cannot do the same for readtables in a |
|---|
| 118 | portable manner. |
|---|
| 119 | |
|---|
| 120 | Therefore, we do not talk about "using" another readtable but |
|---|
| 121 | about "merging" the other readtable's definition into the |
|---|
| 122 | readtable we are going to define. I.e. the inheritance is |
|---|
| 123 | resolved once at definition time, that is statically. |
|---|
| 124 | |
|---|
| 125 | (Such merging can more or less be implemented portably albeit at |
|---|
| 126 | a certain cost. Most of the time, this cost manifests itself at |
|---|
| 127 | the time a readtable is defined, i.e. once at compile-time, so |
|---|
| 128 | it may not bother you. Nonetheless, we provide extra support for |
|---|
| 129 | Sbcl, ClozureCL, and AllegroCL at the moment. Patches for your |
|---|
| 130 | implementation of choice are welcome, of course.) |
|---|
| 131 | |
|---|
| 132 | 3. DEFREADTABLE does not have compile-time effects. |
|---|
| 133 | |
|---|
| 134 | If you define a package via DEFPACKAGE, you can make that |
|---|
| 135 | package the currently active package for the subsequent |
|---|
| 136 | compilation of the same file via IN-PACKAGE. The same is, |
|---|
| 137 | however, not true for DEFREADTABLE and IN-READTABLE for the |
|---|
| 138 | following reason: |
|---|
| 139 | |
|---|
| 140 | It's unlikely that the need for special reader-macros arises for |
|---|
| 141 | a problem which can be solved in just one file. Most often, |
|---|
| 142 | you're going to define the reader macro functions, and set up |
|---|
| 143 | the corresponding readtable in an extra file. |
|---|
| 144 | |
|---|
| 145 | If DEFREADTABLE had compile-time effects, you'd have to wrap |
|---|
| 146 | each definition of a reader-macro function in an EVAL-WHEN to |
|---|
| 147 | make its definition available at compile-time. Because that's |
|---|
| 148 | simply not the common case, DEFREADTABLE does not have a |
|---|
| 149 | compile-time effect. |
|---|
| 150 | |
|---|
| 151 | If you want to use a readtable within the same file as its |
|---|
| 152 | definition, wrap the DEFREADTABLE and the reader-macro function |
|---|
| 153 | definitions in an explicit EVAL-WHEN. |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | ### Preregistered Readtables |
|---|
| 157 | |
|---|
| 158 | - NIL, :STANDARD, and :COMMON-LISP designate the |
|---|
| 159 | *standard readtable*. |
|---|
| 160 | |
|---|
| 161 | - :MODERN designates a *case-preserving* *standard-readtable*. |
|---|
| 162 | |
|---|
| 163 | - :CURRENT designates the *current readtable*. |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | ### Examples |
|---|
| 167 | |
|---|
| 168 | ```commonlisp |
|---|
| 169 | (defreadtable elisp:syntax |
|---|
| 170 | (:merge :standard) |
|---|
| 171 | (:macro-char #\? #'elisp::read-character-literal t) |
|---|
| 172 | (:macro-char #\[ #'elisp::read-vector-literal t) |
|---|
| 173 | ... |
|---|
| 174 | (:case :preserve)) |
|---|
| 175 | |
|---|
| 176 | (defreadtable scheme:syntax |
|---|
| 177 | (:merge :standard) |
|---|
| 178 | (:macro-char #\[ #'(lambda (stream char) |
|---|
| 179 | (read-delimited-list #\] stream))) |
|---|
| 180 | (:macro-char #\# :dispatch) |
|---|
| 181 | (:dispatch-macro-char #\# #\t #'scheme::read-#t) |
|---|
| 182 | (:dispatch-macro-char #\# #\f #'scheme::read-#f) |
|---|
| 183 | ... |
|---|
| 184 | (:case :preserve)) |
|---|
| 185 | |
|---|
| 186 | (in-readtable elisp:syntax) |
|---|
| 187 | |
|---|
| 188 | ... |
|---|
| 189 | |
|---|
| 190 | (in-readtable scheme:syntax) |
|---|
| 191 | |
|---|
| 192 | ... |
|---|
| 193 | ``` |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | ## Reference |
|---|
| 197 | |
|---|
| 198 | - [macro] DEFREADTABLE NAME &BODY OPTIONS |
|---|
| 199 | |
|---|
| 200 | Define a new named readtable, whose name is given by the symbol NAME. |
|---|
| 201 | Or, if a readtable is already registered under that name, redefine |
|---|
| 202 | that one. |
|---|
| 203 | |
|---|
| 204 | The readtable can be populated using the following OPTIONS: |
|---|
| 205 | |
|---|
| 206 | - `(:MERGE READTABLE-DESIGNATORS+)` |
|---|
| 207 | |
|---|
| 208 | Merge the readtables designated into the new readtable being |
|---|
| 209 | defined as per MERGE-READTABLES-INTO. |
|---|
| 210 | |
|---|
| 211 | If no :MERGE clause is given, an empty readtable is used. See |
|---|
| 212 | MAKE-READTABLE. |
|---|
| 213 | |
|---|
| 214 | - `(:FUSE READTABLE-DESIGNATORS+)` |
|---|
| 215 | |
|---|
| 216 | Like :MERGE except: |
|---|
| 217 | |
|---|
| 218 | Error conditions of type READER-MACRO-CONFLICT that are signaled |
|---|
| 219 | during the merge operation will be silently *continued*. It |
|---|
| 220 | follows that reader macros in earlier entries will be |
|---|
| 221 | overwritten by later ones. For backward compatibility, :FUZE is |
|---|
| 222 | accepted as an alias of :FUSE. |
|---|
| 223 | |
|---|
| 224 | - `(:DISPATCH-MACRO-CHAR MACRO-CHAR SUB-CHAR FUNCTION)` |
|---|
| 225 | |
|---|
| 226 | Define a new sub character `SUB-CHAR` for the dispatching macro |
|---|
| 227 | character `MACRO-CHAR`, per SET-DISPATCH-MACRO-CHARACTER. You |
|---|
| 228 | probably have to define `MACRO-CHAR` as a dispatching macro |
|---|
| 229 | character by the following option first. |
|---|
| 230 | |
|---|
| 231 | - `(:MACRO-CHAR MACRO-CHAR FUNCTION [NON-TERMINATING-P])` |
|---|
| 232 | |
|---|
| 233 | Define a new macro character in the readtable, per |
|---|
| 234 | SET-MACRO-CHARACTER. If `FUNCTION` is the keyword :DISPATCH, |
|---|
| 235 | `MACRO-CHAR` is made a dispatching macro character, per |
|---|
| 236 | MAKE-DISPATCH-MACRO-CHARACTER. |
|---|
| 237 | |
|---|
| 238 | - `(:SYNTAX-FROM FROM-READTABLE-DESIGNATOR FROM-CHAR TO-CHAR)` |
|---|
| 239 | |
|---|
| 240 | Set the character syntax of TO-CHAR in the readtable being |
|---|
| 241 | defined to the same syntax as FROM-CHAR as per |
|---|
| 242 | SET-SYNTAX-FROM-CHAR. |
|---|
| 243 | |
|---|
| 244 | - `(:CASE CASE-MODE)` |
|---|
| 245 | |
|---|
| 246 | Defines the *case sensitivity mode* of the resulting readtable. |
|---|
| 247 | |
|---|
| 248 | Any number of option clauses may appear. The options are grouped by |
|---|
| 249 | their type, but in each group the order the options appeared |
|---|
| 250 | textually is preserved. The following groups exist and are executed |
|---|
| 251 | in the following order: :MERGE and :FUSE (one |
|---|
| 252 | group), :CASE, :MACRO-CHAR and :DISPATCH-MACRO-CHAR (one group), |
|---|
| 253 | finally :SYNTAX-FROM. |
|---|
| 254 | |
|---|
| 255 | Notes: |
|---|
| 256 | |
|---|
| 257 | The readtable is defined at load-time. If you want to have it |
|---|
| 258 | available at compilation time -- say to use its reader-macros in the |
|---|
| 259 | same file as its definition -- you have to wrap the DEFREADTABLE |
|---|
| 260 | form in an explicit EVAL-WHEN. |
|---|
| 261 | |
|---|
| 262 | On redefinition, the target readtable is made empty first before |
|---|
| 263 | it's refilled according to the clauses. |
|---|
| 264 | |
|---|
| 265 | NIL, :STANDARD, :COMMON-LISP, :MODERN, and :CURRENT are |
|---|
| 266 | preregistered readtable names. |
|---|
| 267 | |
|---|
| 268 | - [macro] IN-READTABLE NAME |
|---|
| 269 | |
|---|
| 270 | Set *READTABLE* to the readtable referred to by the symbol NAME. |
|---|
| 271 | |
|---|
| 272 | - [function] MAKE-READTABLE &OPTIONAL (NAME NIL NAME-SUPPLIED-P) &KEY MERGE |
|---|
| 273 | |
|---|
| 274 | Creates and returns a new readtable under the specified |
|---|
| 275 | NAME. |
|---|
| 276 | |
|---|
| 277 | MERGE takes a list of NAMED-READTABLE-DESIGNATORS and specifies the |
|---|
| 278 | readtables the new readtable is created from. (See the :MERGE clause |
|---|
| 279 | of DEFREADTABLE for details.) |
|---|
| 280 | |
|---|
| 281 | If MERGE is NIL, an empty readtable is used instead. |
|---|
| 282 | |
|---|
| 283 | If NAME is not given, an anonymous empty readtable is returned. |
|---|
| 284 | |
|---|
| 285 | Notes: |
|---|
| 286 | |
|---|
| 287 | An empty readtable is a readtable where each character's syntax is |
|---|
| 288 | the same as in the *standard readtable* except that each macro |
|---|
| 289 | character has been made a constituent. Basically: whitespace stays |
|---|
| 290 | whitespace, everything else is constituent. |
|---|
| 291 | |
|---|
| 292 | - [function] MERGE-READTABLES-INTO RESULT-READTABLE &REST NAMED-READTABLES |
|---|
| 293 | |
|---|
| 294 | Copy the contents of each readtable in NAMED-READTABLES into |
|---|
| 295 | RESULT-READTABLE. |
|---|
| 296 | |
|---|
| 297 | If a macro character appears in more than one of the readtables, |
|---|
| 298 | i.e. if a conflict is discovered during the merge, an error of type |
|---|
| 299 | READER-MACRO-CONFLICT is signaled. |
|---|
| 300 | |
|---|
| 301 | - [function] FIND-READTABLE NAME |
|---|
| 302 | |
|---|
| 303 | Looks for the readtable specified by NAME and returns it if it is |
|---|
| 304 | found. Returns NIL otherwise. |
|---|
| 305 | |
|---|
| 306 | - [function] ENSURE-READTABLE NAME &OPTIONAL (DEFAULT NIL DEFAULT-P) |
|---|
| 307 | |
|---|
| 308 | Looks up the readtable specified by NAME and returns it if it's found. |
|---|
| 309 | If it is not found, it registers the readtable designated by DEFAULT |
|---|
| 310 | under the name represented by NAME; or if no default argument is |
|---|
| 311 | given, it signals an error of type READTABLE-DOES-NOT-EXIST |
|---|
| 312 | instead. |
|---|
| 313 | |
|---|
| 314 | - [function] RENAME-READTABLE OLD-NAME NEW-NAME |
|---|
| 315 | |
|---|
| 316 | Replaces the associated name of the readtable designated by |
|---|
| 317 | OLD-NAME with NEW-NAME. If a readtable is already registered under |
|---|
| 318 | NEW-NAME, an error of type READTABLE-DOES-ALREADY-EXIST is |
|---|
| 319 | signaled. |
|---|
| 320 | |
|---|
| 321 | - [function] READTABLE-NAME NAMED-READTABLE |
|---|
| 322 | |
|---|
| 323 | Returns the name of the readtable designated by NAMED-READTABLE, |
|---|
| 324 | or NIL. |
|---|
| 325 | |
|---|
| 326 | - [function] REGISTER-READTABLE NAME READTABLE |
|---|
| 327 | |
|---|
| 328 | Associate READTABLE with NAME. Returns the readtable. |
|---|
| 329 | |
|---|
| 330 | - [function] UNREGISTER-READTABLE NAMED-READTABLE |
|---|
| 331 | |
|---|
| 332 | Remove the association of NAMED-READTABLE. Returns T if successfull, |
|---|
| 333 | NIL otherwise. |
|---|
| 334 | |
|---|
| 335 | - [function] COPY-NAMED-READTABLE NAMED-READTABLE |
|---|
| 336 | |
|---|
| 337 | Like COPY-READTABLE but takes a NAMED-READTABLE-DESIGNATOR as argument. |
|---|
| 338 | |
|---|
| 339 | - [function] LIST-ALL-NAMED-READTABLES |
|---|
| 340 | |
|---|
| 341 | Returns a list of all registered readtables. The returned list is |
|---|
| 342 | guaranteed to be fresh, but may contain duplicates. |
|---|
| 343 | |
|---|
| 344 | - [type] NAMED-READTABLE-DESIGNATOR |
|---|
| 345 | |
|---|
| 346 | Either a symbol or a readtable itself. |
|---|
| 347 | |
|---|
| 348 | - [condition] READER-MACRO-CONFLICT READTABLE-ERROR |
|---|
| 349 | |
|---|
| 350 | Continuable. |
|---|
| 351 | |
|---|
| 352 | This condition is signaled during the merge process if a reader |
|---|
| 353 | macro (be it a macro character or the sub character of a dispatch |
|---|
| 354 | macro character) is present in the both source and the target |
|---|
| 355 | readtable and the two respective reader macro functions differ. |
|---|
| 356 | |
|---|
| 357 | - [condition] READTABLE-DOES-ALREADY-EXIST READTABLE-ERROR |
|---|
| 358 | |
|---|
| 359 | Continuable. |
|---|
| 360 | |
|---|
| 361 | - [condition] READTABLE-DOES-NOT-EXIST READTABLE-ERROR |
|---|
| 362 | |
|---|
| 363 | * * * |
|---|
| 364 | ###### \[generated by [MGL-PAX](https://github.com/melisgl/mgl-pax)\] |
|---|