Commit d28da906 authored by Brad King's avatar Brad King
Browse files

cmFortranParser: Inject a newline at end-of-file when missing

Our parser grammar expects all statements to end in an `EOSTMT` token
such as a newline.  Ensure that the last statement in a file can be
parsed even if it is missing a newline.
parent 8c65a501
......@@ -118,11 +118,13 @@ struct cmFortranFile
: File(file)
, Buffer(buffer)
, Directory(dir)
, LastCharWasNewline(false)
{
}
FILE* File;
YY_BUFFER_STATE Buffer;
std::string Directory;
bool LastCharWasNewline;
};
struct cmFortranParser_s
......
......@@ -119,8 +119,19 @@ int cmFortranParser_Input(cmFortranParser* parser, char* buffer,
// Read from the file on top of the stack. If the stack is empty,
// the end of the translation unit has been reached.
if (!parser->FileStack.empty()) {
FILE* file = parser->FileStack.top().File;
return (int)fread(buffer, 1, bufferSize, file);
cmFortranFile& ff = parser->FileStack.top();
FILE* file = ff.File;
size_t n = fread(buffer, 1, bufferSize, file);
if (n > 0) {
ff.LastCharWasNewline = buffer[n - 1] == '\n';
} else if (!ff.LastCharWasNewline) {
// The file ended without a newline. Inject one so
// that the file always ends in an end-of-statement.
buffer[0] = '\n';
n = 1;
ff.LastCharWasNewline = true;
}
return (int)n;
}
return 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment