summaryrefslogtreecommitdiffstats
path: root/lib/antlr/antlr/LexerSharedInputState.h
blob: 3f5c26e585b24e19057bd6efbe474cd50f4e9189 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef INC_LexerSharedInputState_h__
#define INC_LexerSharedInputState_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/InputBuffer.h>
#include <antlr/RefCount.h>
#include <antlr/CharBuffer.h>
#include <string>

#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
namespace antlr {
#endif

/** This object contains the data associated with an
 *  input stream of characters.  Multiple lexers
 *  share a single LexerSharedInputState to lex
 *  the same input stream.
 */
class ANTLR_API LexerInputState {
public:
	/** Construct a new LexerInputState
	 * @param inbuf the InputBuffer to read from. The object is deleted together
	 * with the LexerInputState object.
	 */
	LexerInputState(InputBuffer* inbuf)
	: column(1)
	, line(1)
	, tokenStartColumn(1)
	, tokenStartLine(1)
	, guessing(0)
	, filename("")
	, input(inbuf)
	, inputResponsible(true)
	{
	}

	/** Construct a new LexerInputState
	 * @param inbuf the InputBuffer to read from.
	 */
	LexerInputState(InputBuffer& inbuf)
	: column(1)
	, line(1)
	, tokenStartColumn(1)
	, tokenStartLine(1)
	, guessing(0)
	, filename("")
	, input(&inbuf)
	, inputResponsible(false)
	{
	}

	/** Construct a new LexerInputState
	 * @param in an istream to read from.
	 * @see antlr.CharBuffer
	 */
	LexerInputState(ANTLR_USE_NAMESPACE(std)istream& in)
	: column(1)
	, line(1)
	, tokenStartColumn(1)
	, tokenStartLine(1)
	, guessing(0)
	, filename("")
	, input(new CharBuffer(in))
	, inputResponsible(true)
	{
	}

	/** Reset the LexerInputState with a specified stream and filename.
	 * This method is a hack, dunno what I was thinking when I added it.
	 * This should actually be done in a subclass.
	 * @deprecated
	 */
	virtual void initialize( ANTLR_USE_NAMESPACE(std)istream& in, const char* file = "" )
	{
		column = 1;
		line = 1;
		tokenStartColumn = 1;
		tokenStartLine = 1;
		guessing = 0;
		filename = file;

		if( input && inputResponsible )
			delete input;

		input = new CharBuffer(in);
		inputResponsible = true;
	}

	/** Reset the LexerInputState to initial state.
	 * The underlying InputBuffer is also reset.
	 */
	virtual void reset( void )
	{
		column = 1;
		line = 1;
		tokenStartColumn = 1;
		tokenStartLine = 1;
		guessing = 0;
		input->reset();
	}

	/** Set the file position of the SharedLexerInputState.
	 * @param line_ line number to be set
	 * @param column_ column number to be set
	 */
	void setPosition( int line_, int column_ )
	{
		line = line_;
		column = column_;
	}

	virtual ~LexerInputState()
	{
		if (inputResponsible)
			delete input;
	}

	int column;
	int line;
	int tokenStartColumn;
	int tokenStartLine;
	int guessing;
	/** What file (if known) caused the problem? */
	ANTLR_USE_NAMESPACE(std)string filename;
	InputBuffer& getInput();
private:
	/// Input buffer we use
	InputBuffer* input;
	/// Who is responsible for cleaning up the InputBuffer?
	bool inputResponsible;

	// we don't want these:
	LexerInputState(const LexerInputState&);
	LexerInputState& operator=(const LexerInputState&);
};

inline InputBuffer& LexerInputState::getInput()
{
	return *input;
}

/// A reference counted LexerInputState object
typedef RefCount<LexerInputState> LexerSharedInputState;

#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
}
#endif

#endif //INC_LexerSharedInputState_h__