Skip to content

Commit 54fec42

Browse files
committed
TypeSpec: add enumerator support
1 parent d48558f commit 54fec42

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

Units/parser-typespec.r/simple-typespec.d/expected.tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Name.Space input.tsp /^namespace Name.Space;$/;" n
22
Inner input.tsp /^namespace Inner {$/;" n namespace:Name.Space
33
Versions input.tsp /^ enum Versions {$/;" g namespace:Name.Space.Inner
4+
v2024_05_01_preview input.tsp /^ v2024_05_01_preview: "2024-05-01-preview",$/;" e enum:Name.Space.Inner.Versions
45
OpA input.tsp /^ op OpA<T extends TypeSpec.Reflection.Model> is OpB<$/;" o namespace:Name.Space.Inner
56
InterfaceA input.tsp /^ interface InterfaceA extends InterfaceB {$/;" i namespace:Name.Space.Inner
67
opA input.tsp /^ opA is OpA<{$/;" p interface:Name.Space.Inner.InterfaceA

parsers/typespec.c

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef enum {
3232
K_UNION,
3333
K_ALIAS,
3434
K_PROPERTY,
35+
K_ENUMERATOR,
3536
COUNT_KIND
3637
} typeSpecKind;
3738

@@ -43,7 +44,8 @@ static kindDefinition TypeSpecKinds[COUNT_KIND] = {
4344
{ true, 'm', "model", "models" },
4445
{ true, 'u', "union", "unions" },
4546
{ true, 'a', "alias", "aliases" },
46-
{ true, 'p', "property", "properties" }
47+
{ true, 'p', "property", "properties" },
48+
{ true, 'e', "enumerator", "enumerators (values inside an enumeration)", .version = 1 },
4749
};
4850

4951
typedef enum eTokenType {
@@ -388,8 +390,12 @@ static void parseNamespace(tokenInfo *const token)
388390
vStringDelete(name);
389391
}
390392

391-
static void parseEnum(tokenInfo *const token)
393+
static void parseEnum(tokenInfo *const parentToken)
392394
{
395+
tokenInfo *token = newToken ();
396+
copyToken (token, parentToken);
397+
setScope(token, parentToken->scope);
398+
393399
readToken(token);
394400

395401
if (token->type == TOKEN_IDENTIFIER)
@@ -399,7 +405,36 @@ static void parseEnum(tokenInfo *const token)
399405

400406
if (token->type == TOKEN_OPEN_CURLY)
401407
{
402-
enterScope(token, enumIndex);
408+
/* Parse enum body */
409+
while (token->type != TOKEN_CLOSE_CURLY && token->type != TOKEN_EOF)
410+
{
411+
if (token->type == TOKEN_IDENTIFIER)
412+
{
413+
/* Create tag for enumerator */
414+
setScope (token, enumIndex);
415+
makeTypeSpecTag(token, K_ENUMERATOR);
416+
readToken(token);
417+
418+
/* Skip value assignment if any */
419+
if (token->type == TOKEN_EQUAL_SIGN)
420+
{
421+
readToken(token);
422+
/* Skip the value */
423+
while (token->type != TOKEN_COMMA &&
424+
token->type != TOKEN_CLOSE_CURLY &&
425+
token->type != TOKEN_EOF)
426+
{
427+
readToken(token);
428+
}
429+
}
430+
431+
/* Skip comma if present */
432+
if (token->type == TOKEN_COMMA)
433+
readToken(token);
434+
}
435+
else
436+
readToken(token);
437+
}
403438
}
404439
}
405440
}
@@ -678,5 +713,7 @@ extern parserDefinition* TypeSpecParser (void)
678713
def->keywordCount = ARRAY_SIZE (TypeSpecKeywordTable);
679714
def->defaultScopeSeparator = SCOPE_SEPARATOR;
680715
def->useCork = CORK_QUEUE;
716+
def->versionCurrent = 1;
717+
def->versionAge = 1;
681718
return def;
682719
}

0 commit comments

Comments
 (0)