preprocessor: Handle newlines better
This commit is contained in:
parent
ea6c48b2bd
commit
eb8675ab76
|
@ -35,6 +35,21 @@ Preprocessor::Preprocessor()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char readChar(std::istream &istr)
|
||||||
|
{
|
||||||
|
char ch = (char)istr.get();
|
||||||
|
|
||||||
|
// Handling of newlines..
|
||||||
|
if (ch == '\r')
|
||||||
|
{
|
||||||
|
ch = '\n';
|
||||||
|
if ((char)istr.peek() == '\n')
|
||||||
|
(void)istr.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ch;
|
||||||
|
}
|
||||||
|
|
||||||
/** Just read the code into a string. Perform simple cleanup of the code */
|
/** Just read the code into a string. Perform simple cleanup of the code */
|
||||||
std::string Preprocessor::read(std::istream &istr)
|
std::string Preprocessor::read(std::istream &istr)
|
||||||
{
|
{
|
||||||
|
@ -50,7 +65,7 @@ std::string Preprocessor::read(std::istream &istr)
|
||||||
unsigned int newlines = 0;
|
unsigned int newlines = 0;
|
||||||
|
|
||||||
std::ostringstream code;
|
std::ostringstream code;
|
||||||
for (char ch = (char)istr.get(); istr.good(); ch = (char)istr.get())
|
for (char ch = readChar(istr); istr.good(); ch = readChar(istr))
|
||||||
{
|
{
|
||||||
if (ch < 0)
|
if (ch < 0)
|
||||||
continue;
|
continue;
|
||||||
|
@ -70,12 +85,12 @@ std::string Preprocessor::read(std::istream &istr)
|
||||||
// Remove comments..
|
// Remove comments..
|
||||||
if (ch == '/')
|
if (ch == '/')
|
||||||
{
|
{
|
||||||
char chNext = (char)istr.get();
|
char chNext = readChar(istr);
|
||||||
|
|
||||||
if (chNext == '/')
|
if (chNext == '/')
|
||||||
{
|
{
|
||||||
while (istr.good() && ch != '\n')
|
while (istr.good() && ch != '\n')
|
||||||
ch = (char)istr.get();
|
ch = readChar(istr);
|
||||||
code << "\n";
|
code << "\n";
|
||||||
++lineno;
|
++lineno;
|
||||||
}
|
}
|
||||||
|
@ -86,7 +101,7 @@ std::string Preprocessor::read(std::istream &istr)
|
||||||
while (istr.good() && (chPrev != '*' || ch != '/'))
|
while (istr.good() && (chPrev != '*' || ch != '/'))
|
||||||
{
|
{
|
||||||
chPrev = ch;
|
chPrev = ch;
|
||||||
ch = (char)istr.get();
|
ch = readChar(istr);
|
||||||
if (ch == '\n')
|
if (ch == '\n')
|
||||||
{
|
{
|
||||||
code << "\n";
|
code << "\n";
|
||||||
|
@ -112,7 +127,7 @@ std::string Preprocessor::read(std::istream &istr)
|
||||||
ch = (char)istr.get();
|
ch = (char)istr.get();
|
||||||
if (ch == '\\')
|
if (ch == '\\')
|
||||||
{
|
{
|
||||||
char chNext = (char)istr.get();
|
char chNext = readChar(istr);
|
||||||
if (chNext == '\n')
|
if (chNext == '\n')
|
||||||
++newlines;
|
++newlines;
|
||||||
else
|
else
|
||||||
|
@ -131,14 +146,14 @@ std::string Preprocessor::read(std::istream &istr)
|
||||||
else if (ch == '\'')
|
else if (ch == '\'')
|
||||||
{
|
{
|
||||||
code << "\'";
|
code << "\'";
|
||||||
ch = (char)istr.get();
|
ch = readChar(istr);
|
||||||
code << std::string(1, ch);
|
code << std::string(1, ch);
|
||||||
if (ch == '\\')
|
if (ch == '\\')
|
||||||
{
|
{
|
||||||
ch = (char)istr.get();
|
ch = readChar(istr);
|
||||||
code << std::string(1, ch);
|
code << std::string(1, ch);
|
||||||
}
|
}
|
||||||
ch = (char)istr.get();
|
ch = readChar(istr);
|
||||||
code << "\'";
|
code << "\'";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,10 +161,10 @@ std::string Preprocessor::read(std::istream &istr)
|
||||||
else if (ch == '\\')
|
else if (ch == '\\')
|
||||||
{
|
{
|
||||||
char chNext = (char)istr.peek();
|
char chNext = (char)istr.peek();
|
||||||
if (chNext == '\n')
|
if (chNext == '\n' || chNext == '\r')
|
||||||
{
|
{
|
||||||
++newlines;
|
++newlines;
|
||||||
(void)istr.get(); // Skip the "<backspace><newline>"
|
(void)readChar(istr); // Skip the "<backspace><newline>"
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
code << "\\";
|
code << "\\";
|
||||||
|
|
|
@ -50,6 +50,8 @@ private:
|
||||||
TEST_CASE(test4);
|
TEST_CASE(test4);
|
||||||
TEST_CASE(test5);
|
TEST_CASE(test5);
|
||||||
|
|
||||||
|
TEST_CASE( newlines );
|
||||||
|
|
||||||
TEST_CASE(comments1);
|
TEST_CASE(comments1);
|
||||||
|
|
||||||
TEST_CASE(if0);
|
TEST_CASE(if0);
|
||||||
|
@ -261,6 +263,18 @@ private:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void newlines()
|
||||||
|
{
|
||||||
|
const char filedata[] = "\r\r\n\n";
|
||||||
|
|
||||||
|
// Preprocess
|
||||||
|
std::istringstream istr(filedata);
|
||||||
|
Preprocessor preprocessor;
|
||||||
|
ASSERT_EQUALS("\n\n\n", preprocessor.read(istr));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void comments1()
|
void comments1()
|
||||||
{
|
{
|
||||||
const char filedata[] = "/*\n"
|
const char filedata[] = "/*\n"
|
||||||
|
|
Loading…
Reference in New Issue