user797257
user797257

Reputation:

How to get the string that caused parse error?

Suppose I have this code:

(handler-case (read ...)
  (parse-error (condition)
     (format t "What text was I reading last to get this error? ~s~&"
       (how-to-get-this-text? condition))))

I can only see the parse-namestring accessors, but it gives the message of the error, not the text it was parsing.

EDIT

In my case the problem is less generic, so an alternative solution not involving the entire string that failed to parse can be good too.

Imagine this example code I'm trying to parse:

prefix(perhaps (nested (symbolic)) expressions))suffix

In some cases I need to stop on "suffix" and in others, I need to continue, the suffix itself has no other meaning but just being an indicator of the action the parser should take next.

Upvotes: 3

Views: 277

Answers (2)

Rainer Joswig
Rainer Joswig

Reputation: 139321

READ parses from a stream, not a string. The s-expression can be arbitrarily long. Should READ keep a string of what's been read?

What you might need is a special stream. In standard Common Lisp there is no mechanism for user defined streams. But in real life every implementation has such extensible streams. See for example 'gray streams'.

http://www.sbcl.org/1.0/manual/Gray-Streams.html

Upvotes: 4

Xach
Xach

Reputation: 11829

There's no standard function to do it. You might be able to brute-force something with read-from-string, but whatever you do, it will require some extra work.

Upvotes: 3

Related Questions