cmListFileCache: Convert CMake language parser to class

Refactor the parser implementation into a class to make
it easier to extend.
This commit is contained in:
Brad King 2013-02-16 20:14:23 -05:00
parent e945949d14
commit e75b69f55b
1 changed files with 98 additions and 70 deletions

View File

@ -22,43 +22,52 @@
# pragma warn -8060 /* possibly incorrect assignment */ # pragma warn -8060 /* possibly incorrect assignment */
#endif #endif
bool cmListFileCacheParseFunction(cmListFileLexer* lexer, //----------------------------------------------------------------------------
cmListFileFunction& function, struct cmListFileParser
const char* filename); {
cmListFileParser(cmListFile* lf, cmMakefile* mf, const char* filename);
~cmListFileParser();
bool ParseFile();
bool ParseFunction(const char* name, long line);
void AddArgument(cmListFileLexer_Token* token,
cmListFileArgument::Delimiter delim);
cmListFile* ListFile;
cmMakefile* Makefile;
const char* FileName;
cmListFileLexer* Lexer;
cmListFileFunction Function;
};
bool cmListFile::ParseFile(const char* filename, //----------------------------------------------------------------------------
bool topLevel, cmListFileParser::cmListFileParser(cmListFile* lf, cmMakefile* mf,
cmMakefile *mf) const char* filename):
ListFile(lf), Makefile(mf), FileName(filename),
Lexer(cmListFileLexer_New())
{ {
if(!cmSystemTools::FileExists(filename))
{
return false;
} }
// Create the scanner. //----------------------------------------------------------------------------
cmListFileLexer* lexer = cmListFileLexer_New(); cmListFileParser::~cmListFileParser()
if(!lexer)
{ {
cmSystemTools::Error("cmListFileCache: error allocating lexer "); cmListFileLexer_Delete(this->Lexer);
return false;
} }
//----------------------------------------------------------------------------
bool cmListFileParser::ParseFile()
{
// Open the file. // Open the file.
if(!cmListFileLexer_SetFileName(lexer, filename)) if(!cmListFileLexer_SetFileName(this->Lexer, this->FileName))
{ {
cmListFileLexer_Delete(lexer);
cmSystemTools::Error("cmListFileCache: error can not open file ", cmSystemTools::Error("cmListFileCache: error can not open file ",
filename); this->FileName);
return false; return false;
} }
// Use a simple recursive-descent parser to process the token // Use a simple recursive-descent parser to process the token
// stream. // stream.
this->ModifiedTime = cmSystemTools::ModifiedTime(filename);
bool parseError = false;
bool haveNewline = true; bool haveNewline = true;
cmListFileLexer_Token* token; while(cmListFileLexer_Token* token =
while(!parseError && (token = cmListFileLexer_Scan(lexer))) cmListFileLexer_Scan(this->Lexer))
{ {
if(token->type == cmListFileLexer_Token_Newline) if(token->type == cmListFileLexer_Token_Newline)
{ {
@ -69,51 +78,66 @@ bool cmListFile::ParseFile(const char* filename,
if(haveNewline) if(haveNewline)
{ {
haveNewline = false; haveNewline = false;
cmListFileFunction inFunction; if(this->ParseFunction(token->text, token->line))
inFunction.Name = token->text;
inFunction.FilePath = filename;
inFunction.Line = token->line;
if(cmListFileCacheParseFunction(lexer, inFunction, filename))
{ {
this->Functions.push_back(inFunction); this->ListFile->Functions.push_back(this->Function);
} }
else else
{ {
parseError = true; return false;
} }
} }
else else
{ {
cmOStringStream error; cmOStringStream error;
error << "Error in cmake code at\n" error << "Error in cmake code at\n"
<< filename << ":" << token->line << ":\n" << this->FileName << ":" << token->line << ":\n"
<< "Parse error. Expected a newline, got " << "Parse error. Expected a newline, got "
<< cmListFileLexer_GetTypeAsString(lexer, token->type) << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
<< " with text \"" << token->text << "\"."; << " with text \"" << token->text << "\".";
cmSystemTools::Error(error.str().c_str()); cmSystemTools::Error(error.str().c_str());
parseError = true; return false;
} }
} }
else else
{ {
cmOStringStream error; cmOStringStream error;
error << "Error in cmake code at\n" error << "Error in cmake code at\n"
<< filename << ":" << token->line << ":\n" << this->FileName << ":" << token->line << ":\n"
<< "Parse error. Expected a command name, got " << "Parse error. Expected a command name, got "
<< cmListFileLexer_GetTypeAsString(lexer, token->type) << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
<< " with text \"" << " with text \""
<< token->text << "\"."; << token->text << "\".";
cmSystemTools::Error(error.str().c_str()); cmSystemTools::Error(error.str().c_str());
parseError = true; return false;
} }
} }
return true;
}
//----------------------------------------------------------------------------
bool cmListFile::ParseFile(const char* filename,
bool topLevel,
cmMakefile *mf)
{
if(!cmSystemTools::FileExists(filename))
{
return false;
}
bool parseError = false;
this->ModifiedTime = cmSystemTools::ModifiedTime(filename);
{
cmListFileParser parser(this, mf, filename);
parseError = !parser.ParseFile();
}
if(parseError) if(parseError)
{ {
this->ModifiedTime = 0; this->ModifiedTime = 0;
} }
cmListFileLexer_Delete(lexer);
// do we need a cmake_policy(VERSION call? // do we need a cmake_policy(VERSION call?
if(topLevel) if(topLevel)
{ {
@ -209,17 +233,22 @@ bool cmListFile::ParseFile(const char* filename,
return true; return true;
} }
bool cmListFileCacheParseFunction(cmListFileLexer* lexer, //----------------------------------------------------------------------------
cmListFileFunction& function, bool cmListFileParser::ParseFunction(const char* name, long line)
const char* filename)
{ {
// Inintialize a new function call.
this->Function = cmListFileFunction();
this->Function.FilePath = this->FileName;
this->Function.Name = name;
this->Function.Line = line;
// Command name has already been parsed. Read the left paren. // Command name has already been parsed. Read the left paren.
cmListFileLexer_Token* token; cmListFileLexer_Token* token;
if(!(token = cmListFileLexer_Scan(lexer))) if(!(token = cmListFileLexer_Scan(this->Lexer)))
{ {
cmOStringStream error; cmOStringStream error;
error << "Error in cmake code at\n" error << "Error in cmake code at\n" << this->FileName << ":"
<< filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n" << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
<< "Parse error. Function missing opening \"(\"."; << "Parse error. Function missing opening \"(\".";
cmSystemTools::Error(error.str().c_str()); cmSystemTools::Error(error.str().c_str());
return false; return false;
@ -227,26 +256,24 @@ bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
if(token->type != cmListFileLexer_Token_ParenLeft) if(token->type != cmListFileLexer_Token_ParenLeft)
{ {
cmOStringStream error; cmOStringStream error;
error << "Error in cmake code at\n" error << "Error in cmake code at\n" << this->FileName << ":"
<< filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n" << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
<< "Parse error. Expected \"(\", got " << "Parse error. Expected \"(\", got "
<< cmListFileLexer_GetTypeAsString(lexer, token->type) << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
<< " with text \"" << token->text << "\"."; << " with text \"" << token->text << "\".";
cmSystemTools::Error(error.str().c_str()); cmSystemTools::Error(error.str().c_str());
return false; return false;
} }
// Arguments. // Arguments.
unsigned long lastLine = cmListFileLexer_GetCurrentLine(lexer); unsigned long lastLine = cmListFileLexer_GetCurrentLine(this->Lexer);
unsigned long parenDepth = 0; unsigned long parenDepth = 0;
while((token = cmListFileLexer_Scan(lexer))) while((token = cmListFileLexer_Scan(this->Lexer)))
{ {
if(token->type == cmListFileLexer_Token_ParenLeft) if(token->type == cmListFileLexer_Token_ParenLeft)
{ {
parenDepth++; parenDepth++;
cmListFileArgument a("(", cmListFileArgument::Unquoted, this->AddArgument(token, cmListFileArgument::Unquoted);
filename, token->line);
function.Arguments.push_back(a);
} }
else if(token->type == cmListFileLexer_Token_ParenRight) else if(token->type == cmListFileLexer_Token_ParenRight)
{ {
@ -255,43 +282,36 @@ bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
return true; return true;
} }
parenDepth--; parenDepth--;
cmListFileArgument a(")", cmListFileArgument::Unquoted, this->AddArgument(token, cmListFileArgument::Unquoted);
filename, token->line);
function.Arguments.push_back(a);
} }
else if(token->type == cmListFileLexer_Token_Identifier || else if(token->type == cmListFileLexer_Token_Identifier ||
token->type == cmListFileLexer_Token_ArgumentUnquoted) token->type == cmListFileLexer_Token_ArgumentUnquoted)
{ {
cmListFileArgument a(token->text, cmListFileArgument::Unquoted, this->AddArgument(token, cmListFileArgument::Unquoted);
filename, token->line);
function.Arguments.push_back(a);
} }
else if(token->type == cmListFileLexer_Token_ArgumentQuoted) else if(token->type == cmListFileLexer_Token_ArgumentQuoted)
{ {
cmListFileArgument a(token->text, cmListFileArgument::Quoted, this->AddArgument(token, cmListFileArgument::Quoted);
filename, token->line);
function.Arguments.push_back(a);
} }
else if(token->type != cmListFileLexer_Token_Newline) else if(token->type != cmListFileLexer_Token_Newline)
{ {
// Error. // Error.
cmOStringStream error; cmOStringStream error;
error << "Error in cmake code at\n" error << "Error in cmake code at\n" << this->FileName << ":"
<< filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
<< ":\n"
<< "Parse error. Function missing ending \")\". " << "Parse error. Function missing ending \")\". "
<< "Instead found " << "Instead found "
<< cmListFileLexer_GetTypeAsString(lexer, token->type) << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
<< " with text \"" << token->text << "\"."; << " with text \"" << token->text << "\".";
cmSystemTools::Error(error.str().c_str()); cmSystemTools::Error(error.str().c_str());
return false; return false;
} }
lastLine = cmListFileLexer_GetCurrentLine(lexer); lastLine = cmListFileLexer_GetCurrentLine(this->Lexer);
} }
cmOStringStream error; cmOStringStream error;
error << "Error in cmake code at\n" error << "Error in cmake code at\n"
<< filename << ":" << lastLine << ":\n" << this->FileName << ":" << lastLine << ":\n"
<< "Parse error. Function missing ending \")\". " << "Parse error. Function missing ending \")\". "
<< "End of file reached."; << "End of file reached.";
cmSystemTools::Error(error.str().c_str()); cmSystemTools::Error(error.str().c_str());
@ -299,6 +319,14 @@ bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
return false; return false;
} }
//----------------------------------------------------------------------------
void cmListFileParser::AddArgument(cmListFileLexer_Token* token,
cmListFileArgument::Delimiter delim)
{
cmListFileArgument a(token->text, delim, this->FileName, token->line);
this->Function.Arguments.push_back(a);
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc) std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
{ {