3

I'd like to support something like C++'s #include mechanism in a boost spirit parser. Essentially, I have a script command that asks my parser to load a sub script from a file. I'd like to be able to report error messages as described in the tracking input position while parsing post, but they don't cover parsing for multiple inputs.

Can this be reasonably accomplished using boost::spirit::qi?

I've worked around getting the differing inputs in using a smarter iterator type. I'd still like to see accurate positioning though.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • Could you expand a bit on what kind of behavior you are looking for? For example, you can track input position in multiple files by recursively applying a grammar, but I guess this isn't what you are looking for. – academicRobot Jul 01 '10 at 07:22
  • @academic: I want to be able to get things like the current line for when an error occurs. – Billy ONeal Jul 01 '10 at 11:49
  • Gather that much ;), apparently I just over-read the question. – academicRobot Jul 01 '10 at 20:25

1 Answers1

1

IMHO, using a smart iterator is the way to go. What needs to be done is to have a stack of input contexts maintained by the iterator. Each input context stores the information related to a specific file.

Whenever a new file needs to be read (i.e. after seeing an #include statement) a new input context is created. The current input context gets pushed onto the stack, while the new context gets to be the active one. On EOF you pop the next input context from the stack, returning to the point right after the #include. If the stack is empty you reached the EOF of the main file.

In any case, the iterator only gets its input from the active input context.

hkaiser
  • 11,403
  • 1
  • 30
  • 35
  • That's what I'm currently doing. But if you do that, spirit does not know, and as a result there's no way to track the current location. – Billy ONeal Jul 02 '10 at 01:42
  • I thought you could store the current filename and position in the corresponding input context, making it available to Qi. – hkaiser Jul 02 '10 at 13:15
  • I'd like to use Spirit's own position tracking goodies if at all possible. – Billy ONeal Jul 05 '10 at 03:03