TokenStreamSelector.hpp
1 #ifndef INC_TokenStreamSelector_hpp__ 2 #define INC_TokenStreamSelector_hpp__ 3 4 /* ANTLR Translator Generator 5 * Project led by Terence Parr at http://www.jGuru.com 6 * Software rights: http://www.antlr.org/license.html 7 * 8 * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/TokenStreamSelector.hpp#2 $ 9 */ 10 11 #include <antlr/config.hpp> 12 #include <antlr/TokenStream.hpp> 13 #include <map> 14 #include <stack> 15 16 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE 17 namespace antlr { 18 #endif 19 20 /** A token stream MUX (multiplexor) knows about n token streams 21 * and can multiplex them onto the same channel for use by token 22 * stream consumer like a parser. This is a way to have multiple 23 * lexers break up the same input stream for a single parser. 24 * Or, you can have multiple instances of the same lexer handle 25 * multiple input streams; this works great for includes. 26 */ 27 class ANTLR_API TokenStreamSelector : public TokenStream { 28 protected: 29 /** The set of inputs to the MUX */ 30 #ifdef OS_NO_ALLOCATOR 31 typedef ANTLR_USE_NAMESPACE(std)less<ANTLR_USE_NAMESPACE(std)string> lessp; 32 typedef ANTLR_USE_NAMESPACE(std)map<ANTLR_USE_NAMESPACE(std)string,TokenStream*,lessp> inputStreamNames_coll; 33 #else 34 typedef ANTLR_USE_NAMESPACE(std)map<ANTLR_USE_NAMESPACE(std)string,TokenStream*> inputStreamNames_coll; 35 #endif 36 inputStreamNames_coll inputStreamNames; 37 38 /** The currently-selected token stream input */ 39 TokenStream* input; 40 41 /** Used to track stack of input streams */ 42 #ifdef OS_NO_ALLOCATOR 43 typedef ANTLR_USE_NAMESPACE(std)stack<TokenStream*, ANTLR_USE_NAMESPACE(std)deque<TokenStream*> > streamStack_coll; 44 #else 45 typedef ANTLR_USE_NAMESPACE(std)stack<TokenStream*> streamStack_coll; 46 #endif 47 streamStack_coll streamStack; 48 49 public: 50 TokenStreamSelector(); 51 ~TokenStreamSelector(); 52 53 void addInputStream(TokenStream* stream, const ANTLR_USE_NAMESPACE(std)string& key); 54 55 /// Return the stream from which tokens are being pulled at the moment. 56 TokenStream* getCurrentStream() const; 57 58 TokenStream* getStream(const ANTLR_USE_NAMESPACE(std)string& sname) const; 59 60 RefToken nextToken(); 61 62 TokenStream* pop(); 63 64 void push(TokenStream* stream); 65 66 void push(const ANTLR_USE_NAMESPACE(std)string& sname); 67 68 /** Abort recognition of current Token and try again. 69 * A stream can push a new stream (for include files 70 * for example, and then retry(), which will cause 71 * the current stream to abort back to this.nextToken(). 72 * this.nextToken() then asks for a token from the 73 * current stream, which is the new "substream." 74 */ 75 void retry(); 76 77 /** Set the stream without pushing old stream */ 78 void select(TokenStream* stream); 79 80 void select(const ANTLR_USE_NAMESPACE(std)string& sname); 81 }; 82 83 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE 84 } 85 #endif 86 87 #endif //INC_TokenStreamSelector_hpp__