Skip to content

Commit 8d20143

Browse files
committed
added wrapper class "Stream" to TokenList
1 parent e317815 commit 8d20143

2 files changed

Lines changed: 37 additions & 10 deletions

File tree

simplecpp.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,36 @@ void simplecpp::Token::printOut() const
227227
std::cout << std::endl;
228228
}
229229

230+
class simplecpp::TokenList::Stream {
231+
public:
232+
Stream(std::istream &istr)
233+
: istr(istr)
234+
{}
235+
236+
int get() {
237+
return istr.get();
238+
}
239+
int peek() {
240+
return istr.peek();
241+
}
242+
void unget() {
243+
istr.unget();
244+
}
245+
bool good() {
246+
return istr.good();
247+
}
248+
249+
private:
250+
std::istream &istr;
251+
};
252+
230253
simplecpp::TokenList::TokenList(std::vector<std::string> &filenames) : frontToken(nullptr), backToken(nullptr), files(filenames) {}
231254

232255
simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
233256
: frontToken(nullptr), backToken(nullptr), files(filenames)
234257
{
235-
readfile(istr,filename,outputList);
258+
simplecpp::TokenList::Stream stream(istr);
259+
readfile(stream,filename,outputList);
236260
}
237261

238262
simplecpp::TokenList::TokenList(const TokenList &other) : frontToken(nullptr), backToken(nullptr), files(other.files)
@@ -332,7 +356,7 @@ std::string simplecpp::TokenList::stringify() const
332356
return ret.str();
333357
}
334358

335-
static unsigned char readChar(std::istream &istr, unsigned int bom)
359+
static unsigned char readChar(simplecpp::TokenList::Stream &istr, unsigned int bom)
336360
{
337361
unsigned char ch = static_cast<unsigned char>(istr.get());
338362

@@ -363,7 +387,7 @@ static unsigned char readChar(std::istream &istr, unsigned int bom)
363387
return ch;
364388
}
365389

366-
static unsigned char peekChar(std::istream &istr, unsigned int bom)
390+
static unsigned char peekChar(simplecpp::TokenList::Stream &istr, unsigned int bom)
367391
{
368392
unsigned char ch = static_cast<unsigned char>(istr.peek());
369393

@@ -384,14 +408,14 @@ static unsigned char peekChar(std::istream &istr, unsigned int bom)
384408
return ch;
385409
}
386410

387-
static void ungetChar(std::istream &istr, unsigned int bom)
411+
static void ungetChar(simplecpp::TokenList::Stream &istr, unsigned int bom)
388412
{
389413
istr.unget();
390414
if (bom == 0xfeff || bom == 0xfffe)
391415
istr.unget();
392416
}
393417

394-
static unsigned short getAndSkipBOM(std::istream &istr)
418+
static unsigned short getAndSkipBOM(simplecpp::TokenList::Stream &istr)
395419
{
396420
const int ch1 = istr.peek();
397421

@@ -473,7 +497,7 @@ void simplecpp::TokenList::lineDirective(unsigned int fileIndex, unsigned int li
473497

474498
static const std::string COMMENT_END("*/");
475499

476-
void simplecpp::TokenList::readfile(std::istream &istr, const std::string &filename, OutputList *outputList)
500+
void simplecpp::TokenList::readfile(Stream &istr, const std::string &filename, OutputList *outputList)
477501
{
478502
std::stack<simplecpp::Location> loc;
479503

@@ -1166,7 +1190,7 @@ void simplecpp::TokenList::removeComments()
11661190
}
11671191
}
11681192

1169-
std::string simplecpp::TokenList::readUntil(std::istream &istr, const Location &location, const char start, const char end, OutputList *outputList, unsigned int bom)
1193+
std::string simplecpp::TokenList::readUntil(Stream &istr, const Location &location, const char start, const char end, OutputList *outputList, unsigned int bom)
11701194
{
11711195
std::string ret;
11721196
ret += start;
@@ -1297,7 +1321,8 @@ namespace simplecpp {
12971321
Macro(const std::string &name, const std::string &value, std::vector<std::string> &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(false) {
12981322
const std::string def(name + ' ' + value);
12991323
std::istringstream istr(def);
1300-
tokenListDefine.readfile(istr);
1324+
simplecpp::TokenList::Stream stream(istr);
1325+
tokenListDefine.readfile(stream);
13011326
if (!parseDefine(tokenListDefine.cfront()))
13021327
throw std::runtime_error("bad macro syntax. macroname=" + name + " value=" + value);
13031328
}

simplecpp.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ namespace simplecpp {
194194
/** List of tokens. */
195195
class SIMPLECPP_LIB TokenList {
196196
public:
197+
class Stream;
198+
197199
explicit TokenList(std::vector<std::string> &filenames);
198200
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
199201
TokenList(const TokenList &other);
@@ -215,7 +217,7 @@ namespace simplecpp {
215217
void dump() const;
216218
std::string stringify() const;
217219

218-
void readfile(std::istream &istr, const std::string &filename=std::string(), OutputList *outputList = nullptr);
220+
void readfile(Stream &istr, const std::string &filename=std::string(), OutputList *outputList = nullptr);
219221
void constFold();
220222

221223
void removeComments();
@@ -280,7 +282,7 @@ namespace simplecpp {
280282
void constFoldLogicalOp(Token *tok);
281283
void constFoldQuestionOp(Token **tok1);
282284

283-
std::string readUntil(std::istream &istr, const Location &location, char start, char end, OutputList *outputList, unsigned int bom);
285+
std::string readUntil(Stream &istr, const Location &location, char start, char end, OutputList *outputList, unsigned int bom);
284286
void lineDirective(unsigned int fileIndex, unsigned int line, Location *location);
285287

286288
std::string lastLine(int maxsize=100000) const;

0 commit comments

Comments
 (0)