3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-06-07 06:33:24 +00:00

liberty: Error on unclosed curly braces

This is an indication that the liberty file was truncated, which
shouldn't be silently ignored.
This commit is contained in:
Jannis Harder 2025-04-16 19:12:01 +02:00
parent 418e795235
commit ce74404890
2 changed files with 22 additions and 7 deletions

View file

@ -436,6 +436,9 @@ void LibertyParser::report_unexpected_token(int tok)
eReport += "'."; eReport += "'.";
error(eReport); error(eReport);
break; break;
case EOF:
error("Unexpected end of file");
break;
default: default:
eReport = "Unexpected token: "; eReport = "Unexpected token: ";
eReport += static_cast<char>(tok); eReport += static_cast<char>(tok);
@ -484,7 +487,7 @@ void LibertyParser::parse_vector_range(int tok)
} }
} }
LibertyAst *LibertyParser::parse() LibertyAst *LibertyParser::parse(bool top_level)
{ {
std::string str; std::string str;
@ -498,7 +501,13 @@ LibertyAst *LibertyParser::parse()
while ((tok == 'n') || (tok == ';')) while ((tok == 'n') || (tok == ';'))
tok = lexer(str); tok = lexer(str);
if (tok == '}' || tok < 0) if (tok == EOF) {
if (top_level)
return NULL;
report_unexpected_token(tok);
}
if (tok == '}')
return NULL; return NULL;
if (tok != 'v') { if (tok != 'v') {
@ -571,12 +580,18 @@ LibertyAst *LibertyParser::parse()
} }
if (tok == '{') { if (tok == '{') {
bool terminated = false;
while (1) { while (1) {
LibertyAst *child = parse(); LibertyAst *child = parse(false);
if (child == NULL) if (child == NULL) {
terminated = true;
break; break;
}
ast->children.push_back(child); ast->children.push_back(child);
} }
if (!terminated) {
report_unexpected_token(EOF);
}
break; break;
} }

View file

@ -165,7 +165,7 @@ namespace Yosys
void report_unexpected_token(int tok); void report_unexpected_token(int tok);
void parse_vector_range(int tok); void parse_vector_range(int tok);
LibertyAst *parse(); LibertyAst *parse(bool top_level);
void error() const; void error() const;
void error(const std::string &str) const; void error(const std::string &str) const;
@ -174,7 +174,7 @@ namespace Yosys
const LibertyAst *ast = nullptr; const LibertyAst *ast = nullptr;
LibertyParser(std::istream &f) : f(f), line(1) { LibertyParser(std::istream &f) : f(f), line(1) {
shared_ast.reset(parse()); shared_ast.reset(parse(true));
ast = shared_ast.get(); ast = shared_ast.get();
if (!ast) { if (!ast) {
#ifdef FILTERLIB #ifdef FILTERLIB
@ -190,7 +190,7 @@ namespace Yosys
LibertyParser(std::istream &f, const std::string &fname) : f(f), line(1) { LibertyParser(std::istream &f, const std::string &fname) : f(f), line(1) {
shared_ast = LibertyAstCache::instance.cached_ast(fname); shared_ast = LibertyAstCache::instance.cached_ast(fname);
if (!shared_ast) { if (!shared_ast) {
shared_ast.reset(parse()); shared_ast.reset(parse(true));
LibertyAstCache::instance.parsed_ast(fname, shared_ast); LibertyAstCache::instance.parsed_ast(fname, shared_ast);
} }
ast = shared_ast.get(); ast = shared_ast.get();