From 9163e0d155a85cc57883257b8cca6fcbb22fe7b6 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Wed, 7 Aug 2024 13:45:16 -0400 Subject: [NFC][parser] Rename deftype and subtype (#6819) Match the current spec and clarify terminology by renaming the old `deftype` to `rectype` and renaming the old `subtype` to `typedef`. Also split the parser for actual `subtype` out of the parser for the newly named `typedef`. --- src/parser/parsers.h | 53 +++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) (limited to 'src/parser/parsers.h') diff --git a/src/parser/parsers.h b/src/parser/parsers.h index 2c3916fa3..e3434d1e6 100644 --- a/src/parser/parsers.h +++ b/src/parser/parsers.h @@ -338,8 +338,9 @@ MaybeResult inlineImport(Lexer&); Result> inlineExports(Lexer&); template Result<> comptype(Ctx&); template Result<> sharecomptype(Ctx&); -template MaybeResult subtype(Ctx&); -template MaybeResult<> deftype(Ctx&); +template Result<> subtype(Ctx&); +template MaybeResult<> typedef_(Ctx&); +template MaybeResult<> rectype(Ctx&); template MaybeResult locals(Ctx&); template MaybeResult<> import_(Ctx&); template MaybeResult<> func(Ctx&); @@ -2780,20 +2781,8 @@ template Result<> sharecomptype(Ctx& ctx) { return Ok{}; } -// subtype ::= '(' 'type' id? '(' 'sub' typeidx? sharecomptype ')' ')' -// | '(' 'type' id? sharecomptype ')' -template MaybeResult<> subtype(Ctx& ctx) { - auto pos = ctx.in.getPos(); - - if (!ctx.in.takeSExprStart("type"sv)) { - return {}; - } - - Name name; - if (auto id = ctx.in.takeID()) { - name = *id; - } - +// subtype ::= '(' 'sub' typeidx? sharecomptype ')' | sharecomptype +template Result<> subtype(Ctx& ctx) { if (ctx.in.takeSExprStart("sub"sv)) { if (!ctx.in.takeKeyword("final"sv)) { ctx.setOpen(); @@ -2811,24 +2800,42 @@ template MaybeResult<> subtype(Ctx& ctx) { } else { CHECK_ERR(sharecomptype(ctx)); } + return Ok{}; +} + +// typedef ::= '(' 'type' id? subtype ')' +template MaybeResult<> typedef_(Ctx& ctx) { + auto pos = ctx.in.getPos(); + + if (!ctx.in.takeSExprStart("type"sv)) { + return {}; + } + + Name name; + if (auto id = ctx.in.takeID()) { + name = *id; + } + + auto sub = subtype(ctx); + CHECK_ERR(sub); if (!ctx.in.takeRParen()) { return ctx.in.err("expected end of type definition"); } - ctx.finishSubtype(name, pos); + ctx.finishTypeDef(name, pos); return Ok{}; } -// deftype ::= '(' 'rec' subtype* ')' +// rectype ::= '(' 'rec' subtype* ')' // | subtype -template MaybeResult<> deftype(Ctx& ctx) { +template MaybeResult<> rectype(Ctx& ctx) { auto pos = ctx.in.getPos(); if (ctx.in.takeSExprStart("rec"sv)) { size_t startIndex = ctx.getRecGroupStartIndex(); size_t groupLen = 0; - while (auto type = subtype(ctx)) { + while (auto type = typedef_(ctx)) { CHECK_ERR(type); ++groupLen; } @@ -2836,13 +2843,13 @@ template MaybeResult<> deftype(Ctx& ctx) { return ctx.in.err("expected type definition or end of recursion group"); } ctx.addRecGroup(startIndex, groupLen); - } else if (auto type = subtype(ctx)) { + } else if (auto type = typedef_(ctx)) { CHECK_ERR(type); } else { return {}; } - ctx.finishDeftype(pos); + ctx.finishRectype(pos); return Ok{}; } @@ -3464,7 +3471,7 @@ template MaybeResult<> modulefield(Ctx& ctx) { if (ctx.in.empty() || ctx.in.peekRParen()) { return {}; } - if (auto res = deftype(ctx)) { + if (auto res = rectype(ctx)) { CHECK_ERR(res); return Ok{}; } -- cgit v1.2.3