Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Stacked Input Streams - "include" files files

Java Java

Here is sample Java-code for implementation 'include' directive. Thanks Terence Parr for important notices and Tim Clark for improvements.

...

Because this is a fairly common request and doing such things in C is always a little more time consuming,  this this is built this into the standard ANTLR3 C runtime library (though you can always override the functions of course if they do not do quite what you want.

...

POPSTREAM()

The PUSHSTREAM(s) macros macro is generally the only thing you need and is available in the lexer only (for what should be ovbvious reasons, though I did wonder about supporting multiple parsing streams. . hmm). The C parser example in the examples tgz/zip available at the Fisheye repositorythe Fisheye repository shows you exactly how to use this feature. PUSHSTREAM saves teh the current input stream and replaces it with the supplied stream 's'. It does NOT reset the lexer or input stream, thoguh though you can do this in your lexer rule if required.

In brief, the parameter (s) to PUSHSTREAM is of type pANTLR3_INPUT_STREAM, which you create yourself in a lexer rule. You are responisble responsible for closing the stream when everythign everything (including the tokens created from the stream) have finsihed finished with it, so you should track it somewhere. However, when the stream is exhausted, the standard nextToken() implementation will automatically switch back to the previously saved stream (if any), until all streams are exhausted.

...