Skip to content

Commit

Permalink
Improve the Parser's API and Storage of Metadata (#3003)
Browse files Browse the repository at this point in the history
  • Loading branch information
InsertCreativityHere authored Oct 30, 2024
1 parent 5407a60 commit 465dc1b
Show file tree
Hide file tree
Showing 25 changed files with 1,189 additions and 1,327 deletions.
254 changes: 130 additions & 124 deletions cpp/src/Slice/Grammar.cpp

Large diffs are not rendered by default.

90 changes: 48 additions & 42 deletions cpp/src/Slice/Grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ opt_semicolon
// ----------------------------------------------------------------------
file_metadata
// ----------------------------------------------------------------------
: ICE_FILE_METADATA_OPEN string_list ICE_FILE_METADATA_CLOSE
: ICE_FILE_METADATA_OPEN metadata_list ICE_FILE_METADATA_CLOSE
{
$$ = $2;
}
Expand All @@ -207,13 +207,13 @@ file_metadata
// ----------------------------------------------------------------------
metadata
// ----------------------------------------------------------------------
: ICE_METADATA_OPEN string_list ICE_METADATA_CLOSE
: ICE_METADATA_OPEN metadata_list ICE_METADATA_CLOSE
{
$$ = $2;
}
| %empty
{
$$ = make_shared<StringListTok>();
$$ = make_shared<MetadataListTok>();
}
;

Expand All @@ -222,19 +222,19 @@ definitions
// ----------------------------------------------------------------------
: definitions file_metadata
{
auto metadata = dynamic_pointer_cast<StringListTok>($2);
auto metadata = dynamic_pointer_cast<MetadataListTok>($2);
if (!metadata->v.empty())
{
currentUnit->addFileMetadata(metadata->v);
currentUnit->addFileMetadata(std::move(metadata->v));
}
}
| definitions metadata definition
{
auto metadata = dynamic_pointer_cast<StringListTok>($2);
auto metadata = dynamic_pointer_cast<MetadataListTok>($2);
auto contained = dynamic_pointer_cast<Contained>($3);
if (contained && !metadata->v.empty())
{
contained->setMetadata(metadata->v);
contained->setMetadata(std::move(metadata->v));
}
}
| %empty
Expand Down Expand Up @@ -987,11 +987,11 @@ data_members
// ----------------------------------------------------------------------
: metadata data_member ';' data_members
{
auto metadata = dynamic_pointer_cast<StringListTok>($1);
auto metadata = dynamic_pointer_cast<MetadataListTok>($1);
auto contained = dynamic_pointer_cast<Contained>($2);
if (contained && !metadata->v.empty())
{
contained->setMetadata(metadata->v);
contained->setMetadata(std::move(metadata->v));
}
}
| error ';' data_members
Expand Down Expand Up @@ -1468,11 +1468,11 @@ operations
// ----------------------------------------------------------------------
: metadata operation ';' operations
{
auto metadata = dynamic_pointer_cast<StringListTok>($1);
auto metadata = dynamic_pointer_cast<MetadataListTok>($1);
auto contained = dynamic_pointer_cast<Contained>($2);
if (contained && !metadata->v.empty())
{
contained->setMetadata(metadata->v);
contained->setMetadata(std::move(metadata->v));
}
}
| error ';' operations
Expand Down Expand Up @@ -1535,18 +1535,18 @@ sequence_def
: ICE_SEQUENCE '<' metadata type '>' ICE_IDENTIFIER
{
auto ident = dynamic_pointer_cast<StringTok>($6);
auto metadata = dynamic_pointer_cast<StringListTok>($3);
auto metadata = dynamic_pointer_cast<MetadataListTok>($3);
auto type = dynamic_pointer_cast<Type>($4);
ContainerPtr cont = currentUnit->currentContainer();
$$ = cont->createSequence(ident->v, type, metadata->v);
$$ = cont->createSequence(ident->v, type, std::move(metadata->v));
}
| ICE_SEQUENCE '<' metadata type '>' keyword
{
auto ident = dynamic_pointer_cast<StringTok>($6);
auto metadata = dynamic_pointer_cast<StringListTok>($3);
auto metadata = dynamic_pointer_cast<MetadataListTok>($3);
auto type = dynamic_pointer_cast<Type>($4);
ContainerPtr cont = currentUnit->currentContainer();
$$ = cont->createSequence(ident->v, type, metadata->v); // Dummy
$$ = cont->createSequence(ident->v, type, std::move(metadata->v)); // Dummy
currentUnit->error("keyword `" + ident->v + "' cannot be used as sequence name");
}
;
Expand All @@ -1557,22 +1557,22 @@ dictionary_def
: ICE_DICTIONARY '<' metadata type ',' metadata type '>' ICE_IDENTIFIER
{
auto ident = dynamic_pointer_cast<StringTok>($9);
auto keyMetadata = dynamic_pointer_cast<StringListTok>($3);
auto keyMetadata = dynamic_pointer_cast<MetadataListTok>($3);
auto keyType = dynamic_pointer_cast<Type>($4);
auto valueMetadata = dynamic_pointer_cast<StringListTok>($6);
auto valueMetadata = dynamic_pointer_cast<MetadataListTok>($6);
auto valueType = dynamic_pointer_cast<Type>($7);
ContainerPtr cont = currentUnit->currentContainer();
$$ = cont->createDictionary(ident->v, keyType, keyMetadata->v, valueType, valueMetadata->v);
$$ = cont->createDictionary(ident->v, keyType, std::move(keyMetadata->v), valueType, std::move(valueMetadata->v));
}
| ICE_DICTIONARY '<' metadata type ',' metadata type '>' keyword
{
auto ident = dynamic_pointer_cast<StringTok>($9);
auto keyMetadata = dynamic_pointer_cast<StringListTok>($3);
auto keyMetadata = dynamic_pointer_cast<MetadataListTok>($3);
auto keyType = dynamic_pointer_cast<Type>($4);
auto valueMetadata = dynamic_pointer_cast<StringListTok>($6);
auto valueMetadata = dynamic_pointer_cast<MetadataListTok>($6);
auto valueType = dynamic_pointer_cast<Type>($7);
ContainerPtr cont = currentUnit->currentContainer();
$$ = cont->createDictionary(ident->v, keyType, keyMetadata->v, valueType, valueMetadata->v); // Dummy
$$ = cont->createDictionary(ident->v, keyType, std::move(keyMetadata->v), valueType, std::move(valueMetadata->v)); // Dummy
currentUnit->error("keyword `" + ident->v + "' cannot be used as dictionary name");
}
;
Expand Down Expand Up @@ -1646,23 +1646,23 @@ enumerator_list
// ----------------------------------------------------------------------
: metadata enumerator ',' enumerator_list
{
auto metadata = dynamic_pointer_cast<StringListTok>($1);
auto metadata = dynamic_pointer_cast<MetadataListTok>($1);
auto enumerator = dynamic_pointer_cast<Enumerator>($2);
if (enumerator && !metadata->v.empty())
{
enumerator->setMetadata(metadata->v);
enumerator->setMetadata(std::move(metadata->v));
}
auto enumeratorList = dynamic_pointer_cast<EnumeratorListTok>($4);
enumeratorList->v.push_front(enumerator);
$$ = enumeratorList;
}
| metadata enumerator
{
auto metadata = dynamic_pointer_cast<StringListTok>($1);
auto metadata = dynamic_pointer_cast<MetadataListTok>($1);
auto enumerator = dynamic_pointer_cast<Enumerator>($2);
if (enumerator && !metadata->v.empty())
{
enumerator->setMetadata(metadata->v);
enumerator->setMetadata(std::move(metadata->v));
}
auto enumeratorList = make_shared<EnumeratorListTok>();
enumeratorList->v.push_front(enumerator);
Expand Down Expand Up @@ -1787,10 +1787,10 @@ parameters
{
ParamDeclPtr pd = op->createParamDecl(tsp->name, tsp->type, isOutParam->v, tsp->isOptional, tsp->tag);
currentUnit->currentContainer()->checkIntroduced(tsp->name, pd);
auto metadata = dynamic_pointer_cast<StringListTok>($2);
auto metadata = dynamic_pointer_cast<MetadataListTok>($2);
if (!metadata->v.empty())
{
pd->setMetadata(metadata->v);
pd->setMetadata(std::move(metadata->v));
}
}
}
Expand All @@ -1803,10 +1803,10 @@ parameters
{
ParamDeclPtr pd = op->createParamDecl(tsp->name, tsp->type, isOutParam->v, tsp->isOptional, tsp->tag);
currentUnit->currentContainer()->checkIntroduced(tsp->name, pd);
auto metadata = dynamic_pointer_cast<StringListTok>($4);
auto metadata = dynamic_pointer_cast<MetadataListTok>($4);
if (!metadata->v.empty())
{
pd->setMetadata(metadata->v);
pd->setMetadata(std::move(metadata->v));
}
}
}
Expand Down Expand Up @@ -1987,21 +1987,27 @@ string_literal
;

// ----------------------------------------------------------------------
string_list
metadata_list
// ----------------------------------------------------------------------
: string_list ',' string_literal
: metadata_list ',' string_literal
{
auto str = dynamic_pointer_cast<StringTok>($3);
auto stringList = dynamic_pointer_cast<StringListTok>($1);
stringList->v.push_back(str->v);
$$ = stringList;
auto metadataList = dynamic_pointer_cast<MetadataListTok>($1);

auto metadata = make_shared<Metadata>(str->v, currentUnit->currentFile(), currentUnit->currentLine());
metadataList->v.push_back(metadata);

$$ = metadataList;
}
| string_literal
{
auto str = dynamic_pointer_cast<StringTok>($1);
auto stringList = make_shared<StringListTok>();
stringList->v.push_back(str->v);
$$ = stringList;
auto metadataList = make_shared<MetadataListTok>();

auto metadata = make_shared<Metadata>(str->v, currentUnit->currentFile(), currentUnit->currentLine());
metadataList->v.push_back(metadata);

$$ = metadataList;
}
;

Expand Down Expand Up @@ -2089,21 +2095,21 @@ const_def
// ----------------------------------------------------------------------
: ICE_CONST metadata type ICE_IDENTIFIER '=' const_initializer
{
auto metadata = dynamic_pointer_cast<StringListTok>($2);
auto metadata = dynamic_pointer_cast<MetadataListTok>($2);
auto const_type = dynamic_pointer_cast<Type>($3);
auto ident = dynamic_pointer_cast<StringTok>($4);
auto value = dynamic_pointer_cast<ConstDefTok>($6);
$$ = currentUnit->currentContainer()->createConst(ident->v, const_type, metadata->v, value->v,
$$ = currentUnit->currentContainer()->createConst(ident->v, const_type, std::move(metadata->v), value->v,
value->valueAsString);
}
| ICE_CONST metadata type '=' const_initializer
{
auto metadata = dynamic_pointer_cast<StringListTok>($2);
auto metadata = dynamic_pointer_cast<MetadataListTok>($2);
auto const_type = dynamic_pointer_cast<Type>($3);
auto value = dynamic_pointer_cast<ConstDefTok>($5);
currentUnit->error("missing constant name");
$$ = currentUnit->currentContainer()->createConst(Ice::generateUUID(), const_type, metadata->v, value->v,
value->valueAsString, Dummy); // Dummy
$$ = currentUnit->currentContainer()->createConst(Ice::generateUUID(), const_type, std::move(metadata->v),
value->v, value->valueAsString, Dummy); // Dummy
}
;

Expand Down
6 changes: 3 additions & 3 deletions cpp/src/Slice/GrammarUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ namespace Slice
std::string literal;
};

struct StringListTok final : public GrammarBase
struct MetadataListTok final : public GrammarBase
{
StringListTok() {}
StringList v;
MetadataListTok() {}
MetadataList v;
};

struct TypeStringTok final : public GrammarBase
Expand Down
Loading

0 comments on commit 465dc1b

Please sign in to comment.