Skip to content

Commit

Permalink
More review fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
InsertCreativityHere committed Oct 30, 2024
1 parent 7f989a4 commit 61d2ae9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 44 deletions.
66 changes: 32 additions & 34 deletions cpp/src/Slice/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,43 +96,16 @@ namespace Slice
// ----------------------------------------------------------------------
Slice::Metadata::Metadata(string rawMetadata, string file, int line) : GrammarBase()
{
std::tie(_directive, _arguments) = parseRawMetadata(std::move(rawMetadata));
_file = std::move(file);
_line = line;
}

string_view
Slice::Metadata::directive() const
{
return _directive;
}

string_view
Slice::Metadata::arguments() const
{
return _arguments;
}

string
Slice::Metadata::file() const
{
return _file;
}

int
Slice::Metadata::line() const
{
return _line;
}

pair<string, string>
Slice::Metadata::parseRawMetadata(string rawMetadata)
{
// If the metadata contains no colons, then it must be a directive with no arguments.
size_t firstColonPos = rawMetadata.find(':');
if (firstColonPos == string::npos)
{
return {rawMetadata, ""};
_directive = rawMetadata;
_arguments = "";
return;
}

// Otherwise, we check whether the colon is for a language prefix or for arguments and split the string accordingly.
Expand All @@ -148,7 +121,9 @@ Slice::Metadata::parseRawMetadata(string rawMetadata)
if (isLanguage)
{
// If the piece before the colon was a language prefix, don't split up the string.
return {rawMetadata, ""};
_directive = rawMetadata;
_arguments = "";
return;
}
else
{
Expand All @@ -162,9 +137,32 @@ Slice::Metadata::parseRawMetadata(string rawMetadata)
splitPos = secondColonPos;
}

string directive = rawMetadata.substr(0, splitPos);
string arguments = rawMetadata.substr(splitPos + 1);
return {directive, arguments};
_directive = rawMetadata.substr(0, splitPos);
_arguments = rawMetadata.substr(splitPos + 1);
}

string_view
Slice::Metadata::directive() const
{
return _directive;
}

string_view
Slice::Metadata::arguments() const
{
return _arguments;
}

string
Slice::Metadata::file() const
{
return _file;
}

int
Slice::Metadata::line() const
{
return _line;
}

// ----------------------------------------------------------------------
Expand Down
3 changes: 0 additions & 3 deletions cpp/src/Slice/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@ namespace Slice
friend std::ostream& operator<<(std::ostream& out, const Metadata& metadata);

private:
/// Parses a metadata string into a pair of (directive, arguments) strings.
static std::pair<std::string, std::string> parseRawMetadata(std::string rawMetadata);

std::string _directive;
std::string _arguments;

Expand Down
12 changes: 6 additions & 6 deletions cpp/src/slice2cpp/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,17 +1097,17 @@ Slice::Gen::MetadataVisitor::visitConst(const ConstPtr& p)
MetadataList
Slice::Gen::MetadataVisitor::validate(
const SyntaxTreeBasePtr& cont,
const MetadataList& metadata,
MetadataList metadata,
const string& file,
bool operation)
{
const UnitPtr ut = cont->unit();
const DefinitionContextPtr dc = ut->findDefinitionContext(file);
assert(dc);

MetadataList newMetadata = metadata;
for (const auto& meta : metadata)
for (MetadataList::const_iterator q = metadata.begin(); q != metadata.end();)
{
MetadataPtr meta = *q++;
string_view directive = meta->directive();
string_view arguments = meta->arguments();

Expand All @@ -1118,7 +1118,7 @@ Slice::Gen::MetadataVisitor::validate(
ostringstream ostr;
ostr << "ignoring invalid metadata '" << *meta << "'";
dc->warning(InvalidMetadata, meta->file(), meta->line(), ostr.str());
newMetadata.remove(meta);
metadata.remove(meta);
continue;
}

Expand Down Expand Up @@ -1184,9 +1184,9 @@ Slice::Gen::MetadataVisitor::validate(
ostringstream ostr;
ostr << "ignoring invalid metadata '" << *meta << "'";
dc->warning(InvalidMetadata, meta->file(), meta->line(), ostr.str());
newMetadata.remove(meta);
metadata.remove(meta);
}
return newMetadata;
return metadata;
}

TypeContext
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/slice2cpp/Gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ namespace Slice
void visitConst(const ConstPtr&) final;

private:
MetadataList validate(const SyntaxTreeBasePtr&, const MetadataList&, const std::string&, bool = false);
MetadataList validate(const SyntaxTreeBasePtr&, MetadataList, const std::string&, bool = false);
};

static void validateMetadata(const UnitPtr&);
Expand Down

0 comments on commit 61d2ae9

Please sign in to comment.