Skip to content

Commit

Permalink
Add comma-parsing logic into parseListUntil() method and admit traili…
Browse files Browse the repository at this point in the history
…ng commas.
  • Loading branch information
mmaloney-sf committed Jun 26, 2024
1 parent 15b0c09 commit d6085df
Showing 1 changed file with 33 additions and 57 deletions.
90 changes: 33 additions & 57 deletions lib/Dialect/FIRRTL/Import/FIRParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ struct FIRParser {
ParseResult parseToken(FIRToken::Kind expectedToken, const Twine &message);

/// Parse a list of elements, terminated with an arbitrary token.
/// Parse a comma-separated list of elements, terminated with an arbitrary
/// token.
ParseResult parseListUntil(FIRToken::Kind rightToken,
const std::function<ParseResult()> &parseElement);

Expand Down Expand Up @@ -337,15 +339,30 @@ ParseResult FIRParser::parseToken(FIRToken::Kind expectedToken,
return emitError(message);
}

/// Parse a list of elements, terminated with an arbitrary token.
/// Parse a comma-separated list of elements, terminated with an arbitrary
/// token.
ParseResult
FIRParser::parseListUntil(FIRToken::Kind rightToken,
const std::function<ParseResult()> &parseElement) {
bool first = false;

while (!consumeIf(rightToken)) {
// on the second iteration and thereafter, parse a comma.
if (first) {
if (parseToken(FIRToken::comma, "expected ','")) {
return failure();
}
if (consumeIf(rightToken)) {
break;
}
}

if (parseElement())
return failure();

first = true;
}

return success();
}

Expand Down Expand Up @@ -791,13 +808,7 @@ ParseResult FIRParser::parseEnumType(FIRRTLType &result) {
"expected leading '{|' in enumeration type"))
return failure();
SmallVector<FEnumType::EnumElement> elements;
bool first = true;
if (parseListUntil(FIRToken::r_brace_bar, [&]() -> ParseResult {
if (!first) {
if (parseToken(FIRToken::comma, "expected ','")) {
return failure();
}
}
auto fieldLoc = getToken().getLoc();

// Parse the name of the tag.
Expand All @@ -819,7 +830,6 @@ ParseResult FIRParser::parseEnumType(FIRRTLType &result) {
type = UIntType::get(getContext(), 0);
}
elements.emplace_back(StringAttr::get(getContext(), name), type);
first = false;
return success();
}))
return failure();
Expand Down Expand Up @@ -1012,14 +1022,7 @@ ParseResult FIRParser::parseType(FIRRTLType &result, const Twine &message) {

SmallVector<OpenBundleType::BundleElement, 4> elements;
bool bundleCompatible = true;
bool first = true;
if (parseListUntil(FIRToken::r_brace, [&]() -> ParseResult {
if (!first) {
if (parseToken(FIRToken::comma, "expected ','")) {
return failure();
}
}

bool isFlipped = consumeIf(FIRToken::kw_flip);

StringRef fieldName;
Expand All @@ -1034,7 +1037,6 @@ ParseResult FIRParser::parseType(FIRRTLType &result, const Twine &message) {
{StringAttr::get(getContext(), fieldName), isFlipped, type});
bundleCompatible &= isa<BundleType::ElementType>(type);

first = false;
return success();
}))
return failure();
Expand Down Expand Up @@ -2251,15 +2253,8 @@ ParseResult FIRStmtParser::parsePrimExp(Value &result) {
// Parse the operands and constant integer arguments.
SmallVector<Value, 3> operands;
SmallVector<int64_t, 3> integers;
bool first = true;

if (parseListUntil(FIRToken::r_paren, [&]() -> ParseResult {
if (!first) {
if (parseToken(FIRToken::comma, "expected ','")) {
return failure();
}
}

// Handle the integer constant case if present.
if (getToken().isAny(FIRToken::integer, FIRToken::signed_integer,
FIRToken::string)) {
Expand All @@ -2280,8 +2275,6 @@ ParseResult FIRStmtParser::parsePrimExp(Value &result) {

operands.push_back(operand);

first = false;

return success();
}))
return failure();
Expand Down Expand Up @@ -2445,14 +2438,7 @@ ParseResult FIRStmtParser::parseListExp(Value &result) {
return failure();

SmallVector<Value, 3> operands;
bool first = true;
if (parseListUntil(FIRToken::r_paren, [&]() -> ParseResult {
if (!first) {
if (parseToken(FIRToken::comma, "expected ','")) {
return failure();
}
}

Value operand;
locationProcessor.setLoc(loc);
if (parseExp(operand, "expected expression in List expression"))
Expand All @@ -2468,7 +2454,6 @@ ParseResult FIRStmtParser::parseListExp(Value &result) {
}

operands.push_back(operand);
first = false;
return success();
}))
return failure();
Expand Down Expand Up @@ -3427,24 +3412,23 @@ ParseResult FIRStmtParser::parseIntrinsic(Value &result, bool isStatement) {
return emitError("expected ':' in intrinsic expression");

SmallVector<Value> operands;
bool first = true;
auto loc = startTok.getLoc();
if (parseListUntil(FIRToken::r_paren, [&]() -> ParseResult {
if (first) {
if (parseToken(FIRToken::comma, "expected ','")) {
return failure();
}
}

Value operand;
if (parseExp(operand, "expected operand in intrinsic"))
return failure();
operands.push_back(operand);
locationProcessor.setLoc(loc);
return success();
first = false;
}))
return failure();
if (consumeIf(FIRToken::comma)) {
if (parseListUntil(FIRToken::r_paren, [&]() -> ParseResult {
Value operand;
if (parseExp(operand, "expected operand in intrinsic"))
return failure();
operands.push_back(operand);
locationProcessor.setLoc(loc);
return success();
})) {
return failure();
}
} else {
if (parseToken(FIRToken::r_paren, "expected ')' in intrinsic"))
return failure();
}

if (isStatement)
if (parseOptionalInfo())
Expand All @@ -3466,14 +3450,7 @@ ParseResult FIRStmtParser::parseOptionalParams(ArrayAttr &resultParameters) {

SmallVector<Attribute, 8> parameters;
SmallPtrSet<StringAttr, 8> seen;
bool first = true;
if (parseListUntil(FIRToken::greater, [&]() -> ParseResult {
if (!first) {
if (parseToken(FIRToken::comma, "expected ','")) {
return failure();
}
}

StringAttr name;
TypedAttr value;
SMLoc loc;
Expand All @@ -3483,7 +3460,6 @@ ParseResult FIRStmtParser::parseOptionalParams(ArrayAttr &resultParameters) {
return emitError(loc, "redefinition of parameter '" +
name.getValue() + "'");
parameters.push_back(ParamDeclAttr::get(name, value));
first = false;
return success();
}))
return failure();
Expand Down

0 comments on commit d6085df

Please sign in to comment.