[Prev][Next][Index][Thread]

Simple Reader Macros for Dylan



As promised here is a very brief description of a very simple reader macro 
facility that permits Dylan to have friendly literal formats for popular 
data types. This was thought up by Andrew Armstrong, Keith Playford and 
myself while at Harlequin.  We would love to get your feedback on it.  The 
following is Keith's description of it.

Jonathan Bachrach
President, Functional Objects, Inc.

Simple Reader Macros

The syntax:

#<name>:<text>

gets transformed, setter-like, into:

<name>-parser(<text>)

The <text> part can be either delimited or undelimited. Undelimited text
can contain anything but commas, semicolons, brackets of any kind, and
whitespace. There is no \ escape processing. All the following are valid:

#http://www.functionalobjects.com/
#time:12:30am
#date:12/3/2000
#file:D:\dylan\sources\
#mailto:sales@functionalobjects.com

In the delimited form, you have a choice of delimiters: "...", (...), [...],
{...}. Within the delimiters, only the matching close delimiter must be
escaped. The selection allows you to choose the delimiter requiring least
escaping for the enclosed data. The text (less the delimiters) is passed
to the parsing function. Examples:

#file:"C:\Program Files\Functional Objects\."
#html:{<html>
<head><title>Foo</title></head>
<body bgcolor="#FFFFFF">
</body>
</html>}

An example parser:

define method html-parser
     (text :: <byte-string>) => (doc :: <html-document>)
   make(<html-document>, text: text);
end method;

If an appropriate function isn't defined, you get a standard unbound variable
reference message indicating the # literal.

Also in order to allow these sort of literals in literal lists and vectors, 
we further propose to allow arbitrary expressions in "literal" lists
and vectors. The same constancy/sharing rules would still apply to the
lists and vectors themselves. That is:

let times = #[#time:12:30am, #time:4:15pm];
==
let times = #[time-parser("12:30am"), time-parser("4:15pm")];

which is allowed, but still:

times[0] := times[1];

"is an error" in case the compiler can work its magic.






Follow-Ups: