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
/*
* 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 LispLexer.cpp
* Implements class LispLexer
*
* @since 14/07/2004 Turku
*/
#include <cstring>
#include "LispLexer.hpp"
using namespace std;
using namespace Shell;
/**
* Initialise a lexer.
* @since 25/08/2009 Redmond
*/
LispLexer::LispLexer (istream& in)
: Lexer (in)
{
} // LispLexer::LispLexer
/**
* Skip all whitespaces and comments. After this operation either
* _lastCharacter will be not whitespace or _eof will be set to true.
* @since 25/08/2009 Redmond
*/
void LispLexer::skipWhiteSpacesAndComments ()
{
bool comment = false;
while (! _eof) {
switch (_lastCharacter) {
case ' ':
case '\t':
case '\r':
case '\f':
readNextChar();
break;
case '\n': // end-of-line comment (if any) finished
comment = false;
readNextChar();
break;
case ';': // Lisp comment sign
comment = true;
readNextChar();
break;
default:
if (comment) {
readNextChar();
break;
}
return;
}
}
} // LispLexer::skipWhiteSpacesAndComments
/**
* Read the next token.
* @since 26/08/2009 Redmond
*/
void LispLexer::readToken (Token& token)
{
skipWhiteSpacesAndComments();
_charCursor = 0;
if (_eof) {
token.tag = TT_EOF;
token.text = "";
return;
}
token.line = _lineNumber;
token.col = _colNumber;
switch (_lastCharacter) {
case '(':
token.tag = TT_LPAR;
saveLastChar();
saveTokenText(token);
readNextChar();
break;
case ')':
token.tag = TT_RPAR;
saveLastChar();
saveTokenText(token);
readNextChar();
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
readNumber(token);
return;
case '"':
readQuotedString(token, '"', '"','"');
return;
case '|':
readQuotedString(token, '|', '|','\\');
return;
case '{':
readQuotedString(token, '{', '}','\\');
return;
default:
readName(token);
token.tag = TT_NAME;
return;
}
} // LispLexer::readToken
/**
* Read a name. No check is made about the current character
* @since 26/08/2009 Redmond
*/
void LispLexer::readName (Token& token)
{
saveLastChar();
while (readNextChar()) {
switch(_lastCharacter) {
case ' ':
case '\t':
case '\r':
case '\f':
case '\n':
case ';':
case ')':
case '(':
case '{':
case '}':
// token parsed
saveTokenText(token);
return;
default:
saveLastChar();
break;
}
}
// no more characters, token parsed
saveTokenText(token);
} // LispLexer::readName
/**
* Read a quoted string.
* @since 26/08/2009 Redmond
*
* Added escapeChar to support smtlib string literals that use " to escape "
* @since May 2020 @author Giles
*/
void LispLexer::readQuotedString(Token& token, char opening, char closing, char escapeChar)
{
bool escape=false;
// Don't save this char so that the final string doesn't contain the quote symbol
//saveLastChar();
while (readNextChar()) {
if (_lastCharacter == escapeChar && !escape && closing != escapeChar) {
escape=true;
}
else if (_lastCharacter == closing && !escape) {
if(closing == escapeChar && lookAhead() == closing){
readNextChar();
}
else{
// Don't save this char so that the final string doesn't contain the quote symbol
//saveLastChar();
saveTokenText(token);
readNextChar();
token.tag = TT_NAME;
return;
}
}
else {
if (escape && _lastCharacter!=closing && _lastCharacter!=escapeChar) {
throw LexerException((std::string)"invalid escape sequence in quoted string ", *this);
}
escape=false;
saveLastChar();
}
}
throw LexerException((std::string)"file ended while reading quoted string ", *this);
} // LispLexer::readQuotedString