0

I've been reading through the sexp.h file reference in an attempt to generate helpful errors when parsing a lisp-like sequence fails because an operator or operand was not formatted as expected.

I am currently using the cparse_sexp function to parse a lisp-style expression into a series of s-expressions elt objects:

pc = cparse_sexp(
  (char*)expression.c_str(),
  expression.length,
  pc);

where pc is a pointer of type pcont_t* and expression is an std::string. I then use this pc pointer to look at the elt objects using pc->last_sexp.

However, if I run into a value that I don't know how to deal with, I'd like to be able to write something like this:

Error was encountered at byte `123` in the vicinity of:
(Operator Operand1 Operand2)
          ^

Where the (Operator Operand1 Operand2) line is taken from the original expression string.

To achieve this I need to know the location from which the respective token was taken. I know I can see the sbuffer value within the pcont_t type, but that just gives me a pointer to the original string. lastPos is supposed to give me the current position, but it just returns the end of the string. How can I parse the string as I use it so that I can see where I'm at?

quant
  • 21,507
  • 32
  • 115
  • 211
  • Unless you modify the parser, you probably can't. If you modify the parser, you can add a couple of fields to the [`elt`](http://sexpr.sourceforge.net/refman/structelt.html) structure that is a pointer to the source where the s-expression starts, and the length of the text of the s-expression. – Some programmer dude Aug 26 '14 at 01:45
  • @JoachimPileborg ok. Could I somehow modify it so that it parses token-by-token, rather than everything at once? Then when I encounter the error I'd have a best-guess using `pc->lastPos`? – quant Aug 26 '14 at 01:48

0 Answers0