summaryrefslogtreecommitdiffstats
path: root/lib/antlr/antlr/TokenStreamSelector.h
blob: 2ed91d728a830b6f90eafd9aff4613c25f84bc0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef INC_TokenStreamSelector_h__
#define INC_TokenStreamSelector_h__

/* ANTLR Translator Generator
 * Project led by Terence Parr at http://www.jGuru.com
 * Software rights: http://www.antlr.org/license.html
 *
 * $Id$
 */

#include <antlr/config.h>
#include <antlr/TokenStream.h>
#include <map>
#include <stack>

#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
namespace antlr {
#endif

/** A token stream MUX (multiplexor) knows about n token streams
 *  and can multiplex them onto the same channel for use by token
 *  stream consumer like a parser.  This is a way to have multiple
 *  lexers break up the same input stream for a single parser.
 * Or, you can have multiple instances of the same lexer handle
 *  multiple input streams; this works great for includes.
 */
class ANTLR_API TokenStreamSelector : public TokenStream {
protected:
	/** The set of inputs to the MUX */
#ifdef OS_NO_ALLOCATOR
	typedef ANTLR_USE_NAMESPACE(std)less<ANTLR_USE_NAMESPACE(std)string> lessp;
	typedef ANTLR_USE_NAMESPACE(std)map<ANTLR_USE_NAMESPACE(std)string,TokenStream*,lessp> inputStreamNames_coll;
#else
	typedef ANTLR_USE_NAMESPACE(std)map<ANTLR_USE_NAMESPACE(std)string,TokenStream*> inputStreamNames_coll;
#endif
	inputStreamNames_coll inputStreamNames;

	/** The currently-selected token stream input */
	TokenStream* input;

	/** Used to track stack of input streams */
#ifdef OS_NO_ALLOCATOR
	typedef ANTLR_USE_NAMESPACE(std)stack<TokenStream*, ANTLR_USE_NAMESPACE(std)deque<TokenStream*> > streamStack_coll;
#else
	typedef ANTLR_USE_NAMESPACE(std)stack<TokenStream*> streamStack_coll;
#endif
	streamStack_coll streamStack;

public:
	TokenStreamSelector();
	~TokenStreamSelector();

	void addInputStream(TokenStream* stream, const ANTLR_USE_NAMESPACE(std)string& key);

	/// Return the stream from which tokens are being pulled at the moment.
	TokenStream* getCurrentStream() const;

	TokenStream* getStream(const ANTLR_USE_NAMESPACE(std)string& sname) const;

	RefToken nextToken();

	TokenStream* pop();

	void push(TokenStream* stream);

	void push(const ANTLR_USE_NAMESPACE(std)string& sname);

	/** Abort recognition of current Token and try again.
	 *  A stream can push a new stream (for include files
	 *  for example, and then retry(), which will cause
	 *  the current stream to abort back to this.nextToken().
	 *  this.nextToken() then asks for a token from the
	 *  current stream, which is the new "substream."
	 */
	void retry();

	/** Set the stream without pushing old stream */
	void select(TokenStream* stream);

	void select(const ANTLR_USE_NAMESPACE(std)string& sname);
};

#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
}
#endif

#endif //INC_TokenStreamSelector_h__