From ac1033ecd554b5685f89167ce240ab6fee60ca4c Mon Sep 17 00:00:00 2001 From: Sean Luchen Date: Mon, 31 Mar 2025 10:46:18 -0700 Subject: [PATCH] Factor parse_vector_range out into its own function. This also fixes the parsing a bit. It was consuming 1 fewer token than required. --- passes/techmap/libparse.cc | 80 ++++++++++++++++++++------------------ passes/techmap/libparse.h | 1 + 2 files changed, 44 insertions(+), 37 deletions(-) diff --git a/passes/techmap/libparse.cc b/passes/techmap/libparse.cc index afc49b454..09afcf9f3 100644 --- a/passes/techmap/libparse.cc +++ b/passes/techmap/libparse.cc @@ -358,6 +358,48 @@ void LibertyParser::report_unexpected_token(int tok) } } +// FIXME: the AST needs to be extended to store +// these vector ranges. +int LibertyParser::parse_vector_range(int tok) +{ + // parse vector range [A] or [A:B] + std::string arg; + tok = lexer(arg); + if (tok != 'v') + { + // expected a vector array index + error("Expected a number."); + } + else + { + // fixme: check for number A + } + tok = lexer(arg); + // optionally check for : in case of [A:B] + // if it isn't we just expect ']' + // as we have [A] + if (tok == ':') + { + tok = lexer(arg); + if (tok != 'v') + { + // expected a vector array index + error("Expected a number."); + } + else + { + // fixme: check for number B + tok = lexer(arg); + } + } + // expect a closing bracket of array range + if (tok != ']') + { + error("Expected ']' on array range."); + } + return lexer(arg); +} + LibertyAst *LibertyParser::parse() { std::string str; @@ -425,45 +467,9 @@ LibertyAst *LibertyParser::parse() if (tok == ')') break; - // FIXME: the AST needs to be extended to store - // these vector ranges. if (tok == '[') { - // parse vector range [A] or [A:B] - std::string arg; - tok = lexer(arg); - if (tok != 'v') - { - // expected a vector array index - error("Expected a number."); - } - else - { - // fixme: check for number A - } - tok = lexer(arg); - // optionally check for : in case of [A:B] - // if it isn't we just expect ']' - // as we have [A] - if (tok == ':') - { - tok = lexer(arg); - if (tok != 'v') - { - // expected a vector array index - error("Expected a number."); - } - else - { - // fixme: check for number B - tok = lexer(arg); - } - } - // expect a closing bracket of array range - if (tok != ']') - { - error("Expected ']' on array range."); - } + tok = parse_vector_range(tok); continue; } if (tok != 'v') { diff --git a/passes/techmap/libparse.h b/passes/techmap/libparse.h index ea81abe2f..686a2b49f 100644 --- a/passes/techmap/libparse.h +++ b/passes/techmap/libparse.h @@ -106,6 +106,7 @@ namespace Yosys int lexer(std::string &str); void report_unexpected_token(int tok); + int parse_vector_range(int tok); LibertyAst *parse(); void error() const; void error(const std::string &str) const;