1#ifndef INC_LexerSharedInputState_hpp__
2#define INC_LexerSharedInputState_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/LexerSharedInputState.hpp#2 $
9 */
10
11#include <antlr/config.hpp>
12#include <antlr/InputBuffer.hpp>
13#include <antlr/RefCount.hpp>
14#include <antlr/CharBuffer.hpp>
15#include <string>
16
17#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
18namespace antlr {
19#endif
20
21/** This object contains the data associated with an
22 *  input stream of characters.  Multiple lexers
23 *  share a single LexerSharedInputState to lex
24 *  the same input stream.
25 */
26class ANTLR_API LexerInputState {
27public:
28	/** Construct a new LexerInputState
29	 * @param inbuf the InputBuffer to read from. The object is deleted together
30	 * with the LexerInputState object.
31	 */
32	LexerInputState(InputBuffer* inbuf)
33	: column(1)
34	, line(1)
35	, tokenStartColumn(1)
36	, tokenStartLine(1)
37	, guessing(0)
38	, filename("")
39	, input(inbuf)
40	, inputResponsible(true)
41	{
42	}
43
44	/** Construct a new LexerInputState
45	 * @param inbuf the InputBuffer to read from.
46	 */
47	LexerInputState(InputBuffer& inbuf)
48	: column(1)
49	, line(1)
50	, tokenStartColumn(1)
51	, tokenStartLine(1)
52	, guessing(0)
53	, filename("")
54	, input(&inbuf)
55	, inputResponsible(false)
56	{
57	}
58
59	/** Construct a new LexerInputState
60	 * @param in an istream to read from.
61	 * @see antlr.CharBuffer
62	 */
63	LexerInputState(ANTLR_USE_NAMESPACE(std)istream& in)
64	: column(1)
65	, line(1)
66	, tokenStartColumn(1)
67	, tokenStartLine(1)
68	, guessing(0)
69	, filename("")
70	, input(new CharBuffer(in))
71	, inputResponsible(true)
72	{
73	}
74
75	/** Reset the LexerInputState with a specified stream and filename.
76	 * This method is a hack, dunno what I was thinking when I added it.
77	 * This should actually be done in a subclass.
78	 * @deprecated
79	 */
80	virtual void initialize( ANTLR_USE_NAMESPACE(std)istream& in, const char* file = "" )
81	{
82		column = 1;
83		line = 1;
84		tokenStartColumn = 1;
85		tokenStartLine = 1;
86		guessing = 0;
87		filename = file;
88
89		if( input && inputResponsible )
90			delete input;
91
92		input = new CharBuffer(in);
93		inputResponsible = true;
94	}
95
96	/** Reset the LexerInputState to initial state.
97	 * The underlying InputBuffer is also reset.
98	 */
99	virtual void reset( void )
100	{
101		column = 1;
102		line = 1;
103		tokenStartColumn = 1;
104		tokenStartLine = 1;
105		guessing = 0;
106		input->reset();
107	}
108
109	/** Set the file position of the SharedLexerInputState.
110	 * @param line_ line number to be set
111	 * @param column_ column number to be set
112	 */
113	void setPosition( int line_, int column_ )
114	{
115		line = line_;
116		column = column_;
117	}
118
119	virtual ~LexerInputState()
120	{
121		if (inputResponsible)
122			delete input;
123	}
124
125	int column;
126	int line;
127	int tokenStartColumn;
128	int tokenStartLine;
129	int guessing;
130	/** What file (if known) caused the problem? */
131	ANTLR_USE_NAMESPACE(std)string filename;
132	InputBuffer& getInput();
133private:
134	/// Input buffer we use
135	InputBuffer* input;
136	/// Who is responsible for cleaning up the InputBuffer?
137	bool inputResponsible;
138
139	// we don't want these:
140	LexerInputState(const LexerInputState&);
141	LexerInputState& operator=(const LexerInputState&);
142};
143
144inline InputBuffer& LexerInputState::getInput()
145{
146	return *input;
147}
148
149/// A reference counted LexerInputState object
150typedef RefCount<LexerInputState> LexerSharedInputState;
151
152#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
153}
154#endif
155
156#endif //INC_LexerSharedInputState_hpp__
157