reading s-expressions from a file

Steve Haflich shaflich at gmail.com
Wed Jun 26 16:49:34 UTC 2019


The first thing to realize is that "you" cannot do this, but you might be
able to write CL-conformant code that does it for you.  But the READ
function won't do it by itself.  It's not part of READ's contract, which
silently eats Newlines without counting them for you.

You might consider some readtable magic, changing Newline to a
non-non-breaking macro char that returns nothing but maintains a count
somewhere, except such hackery would miss line breaks inside strings,
#|comments|#, probably also ;regular comments, and escaped newlines inside
symbol names (UGH).

Rather than writing your own entire READ function, if the source is a
regular static file that can be reopened multiple times, wrtie a simple
alternative MY-READ function that calls FILE-POSITION before calling READ
and returns both a multiple values.  Converting a character FILE-POSITION
into a line number, when and if necessary, can be accomplished by reopening
the file and reading characters until the desired FILE-POSITION counting
line breaks along the way.  Taking care to use the same external-format
avoids the difficulty of multi-byte characters and the possibility that
FILE-POSITION does not necessarily increment by 1 for each char.  It is
only guaranteed to increase monotonically.


On Wed, Jun 26, 2019 at 9:19 AM Alexandre Rademaker <arademaker at gmail.com>
wrote:

>
> The “read” function makes really easy to read a bunch of s-expressions
> from a file, but how can I keep track of the line number where the
> expressions were in the file?
>
> Any ideia?
>
> Alexandre
> Sent from my iPhone
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/pro/attachments/20190626/295185d4/attachment.html>


More information about the pro mailing list