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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* This file is part of the source code of the software program
* Vampire. It is protected by applicable
* copyright laws.
*
* This source code is distributed under the licence found here
* https://vprover.github.io/license.html
* and in the source directory
*/
/**
* @file Shell/Lexer.cpp
* Implements class Lexer
*
* @since 27/07/2004 Torrevieja
*/
#include "Debug/Assertion.hpp"
#include "Lib/Int.hpp"
#include "Lexer.hpp"
using namespace std;
using namespace Shell;
using namespace Lib;
/**
* Initialise a lexer.
* @since 27/07/2004 Torrevieja
*/
Lexer::Lexer (istream& in)
: _charBuffer(512), _stream(in)
{
readNextChar();
} // Lexer::Lexer
bool Lexer::handleChar()
{
if (_lastCharacter == '\n') {
_colNumber = 0;
_lineNumber++;
} else {
_colNumber++;
}
if (_lastCharacter == -1) {
_eof = true;
return false;
}
return true;
}
/**
* Reads next character into _lastCharacter.
*
* @return true if such a character exists
* @since 14/07/2004 Turku
*/
bool Lexer::readNextChar ()
{
if (_lookAheadChar) {
_lastCharacter = _lookAheadChar;
_lookAheadChar = 0;
return handleChar();
}
if (_eof) {
return false;
}
_lastCharacter = _stream.get();
return handleChar();
} // Lexer::readNextChar
/**
* Read a number (either integer or floating point).
* @since 15/07/2004 Turku
*/
void Lexer::readNumber (Token& token)
{
if (_lastCharacter == '-') {
saveLastChar();
readNextChar();
}
readUnsignedInteger();
if (_lastCharacter == '.') {
saveLastChar();
if (readNextChar()) {
if (isDigit(_lastCharacter)) {
readUnsignedInteger();
token.tag = TT_REAL;
saveTokenText(token);
return;
}
}
saveTokenText(token);
throw LexerException((std::string)"incorrect number format in " + token.text,
*this);
}
token.tag = TT_INTEGER;
saveTokenText(token);
} // Lexer::readNumber
/**
* Save last character in the character buffer (if capacity permits).
* @since 15/07/2004 Turku
*/
void Lexer::saveLastChar ()
{
_charBuffer[_charCursor++] = (char)_lastCharacter;
} // Lexer::saveLastChar
/**
* Save character in the character buffer (if capacity permits).
* @since 25/08/2004 Torrevieja
*/
void Lexer::saveChar (int character)
{
_charBuffer[_charCursor++] = (char)character;
} // Lexer::saveChar
/**
* Save the content of the current character buffer as the text
* of the given token.
* @since 15/07/2004 Turku
*/
void Lexer::saveTokenText (Token& token)
{
_charBuffer[_charCursor] = 0;
token.text = _charBuffer.content();
} // Lexer::saveTokenText
/**
* Read an unsigned integer.
* @since 15/07/2004 Turku
*/
void Lexer::readUnsignedInteger ()
{
saveLastChar();
while (readNextChar() && isDigit(_lastCharacter)) {
saveLastChar();
}
} // Lexer::readUnsignedInteger
/**
* Create a new lexer exception.
* @since 15/07/2004 Turku
*/
LexerException::LexerException (std::string message,const Lexer& lexer)
: _message (message)
{
if (lexer.isAtEndOfFile()) {
_message += " at end of input";
return;
}
int line = lexer.lineNumber();
if (lexer.lastCharacter() == '\n') {
line--;
}
_message += " in line ";
_message += Int::toString(line);
} // LexerException::LexerException
/**
* Write itself to an ostream.
* @since 15/07/2004 Turku
*/
void LexerException::cry (std::ostream& out) const
{
out << "Lexer exception: " << _message << '\n';
} // LexerException::LexerException
/**
* Read a sequence of characters cs without saving them.
* @since 25/08/2004 Torrevieja
*/
void Lexer::readSequence (const char* cs)
{
while (*cs) {
readNextChar();
if (lastCharacter() != *cs) {
throw LexerException((std::string)cs +
" expected",*this);
}
cs++;
}
readNextChar();
} // Lexer::readSequence
/**
* Look ahead one character and return it.
* @since 27/11/2006 Haifa
*/
int Lexer::lookAhead()
{
ASS(! _lookAheadChar); // cannot look ahead by two characters!
_lookAheadChar = _stream.get();
return _lookAheadChar;
} // Lexer::lookAhead()