-
Notifications
You must be signed in to change notification settings - Fork 593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Support for cpp:identifier
Metadata, and the Groundwork for xxx:identifier
Metadata in General
#3292
Add Support for cpp:identifier
Metadata, and the Groundwork for xxx:identifier
Metadata in General
#3292
Changes from 14 commits
6787ddb
87e3dde
11ac464
78529c0
ce37faf
9cda559
b2e8390
5427090
1cb67c5
329aeae
a3444f1
2c3b573
dca41be
3f54c9d
382482e
2e8b08d
3053c04
c4b3647
8a904fe
6eeb7ac
4084958
e013361
3dc8134
951a19b
3f92679
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,9 @@ compareTag(const T& lhs, const T& rhs) | |
return lhs->tag() < rhs->tag(); | ||
} | ||
|
||
// NOTE: It is important that this list is kept in alphabetical order! | ||
constexpr string_view languages[] = {"cpp", "cs", "java", "js", "matlab", "php", "python", "ruby", "swift"}; | ||
|
||
// Forward declare things from Bison and Flex the parser can use. | ||
extern int slice_parse(); | ||
extern int slice_lineno; | ||
|
@@ -104,8 +107,6 @@ Slice::Metadata::Metadata(string rawMetadata, string file, int line) : GrammarBa | |
if (firstColonPos != string::npos) | ||
{ | ||
// Check if the metadata starts with a language prefix. | ||
// NOTE: It is important that this list is kept in alphabetical order! | ||
constexpr string_view languages[] = {"cpp", "cs", "java", "js", "matlab", "php", "python", "rb", "swift"}; | ||
string prefix = rawMetadata.substr(0, firstColonPos); | ||
bool hasLangPrefix = binary_search(&languages[0], &languages[sizeof(languages) / sizeof(*languages)], prefix); | ||
if (hasLangPrefix) | ||
|
@@ -554,35 +555,37 @@ Slice::Contained::container() const | |
} | ||
|
||
string | ||
Slice::Contained::name() const | ||
Slice::Contained::name(bool mappedName) const | ||
{ | ||
return _name; | ||
} | ||
// If the mapped name was requested, we first check if any 'xxx:identifier' has been applied to this element. | ||
// If so, we return that instead of the element's Slice identifier. | ||
if (mappedName) | ||
{ | ||
static const string metadata = string(_unit->languagePrefix()) + ":identifier"; | ||
if (auto customName = getMetadataArgs(metadata)) | ||
{ | ||
return *customName; | ||
} | ||
} | ||
|
||
string | ||
Slice::Contained::scoped() const | ||
{ | ||
return _scoped; | ||
return _name; | ||
} | ||
|
||
string | ||
Slice::Contained::scope() const | ||
Slice::Contained::scoped(bool mappedName) const | ||
{ | ||
string::size_type idx = _scoped.rfind("::"); | ||
assert(idx != string::npos); | ||
return string(_scoped, 0, idx + 2); | ||
return scope(mappedName) + name(mappedName); | ||
} | ||
|
||
string | ||
Slice::Contained::flattenedScope() const | ||
Slice::Contained::scope(bool mappedName) const | ||
{ | ||
string s = scope(); | ||
string::size_type pos = 0; | ||
while ((pos = s.find("::", pos)) != string::npos) | ||
string scoped; | ||
if (auto container = dynamic_pointer_cast<Contained>(_container)) | ||
{ | ||
s.replace(pos, 2, "_"); | ||
scoped = container->scoped(mappedName); | ||
} | ||
return s; | ||
return scoped + "::"; | ||
} | ||
|
||
string | ||
|
@@ -1050,12 +1053,6 @@ Slice::Contained::Contained(const ContainerPtr& container, const string& name) | |
_container(container), | ||
_name(name) | ||
{ | ||
ContainedPtr cont = dynamic_pointer_cast<Contained>(_container); | ||
if (cont) | ||
{ | ||
_scoped = cont->scoped(); | ||
} | ||
_scoped += "::" + _name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There isn't much point to Most of the calls to |
||
assert(_unit); | ||
_file = _unit->currentFile(); | ||
_line = _unit->currentLine(); | ||
|
@@ -4555,19 +4552,25 @@ Slice::DataMember::DataMember( | |
// ---------------------------------------------------------------------- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the code below this is to allow the compilers to inject their |
||
|
||
UnitPtr | ||
Slice::Unit::createUnit(bool all, const StringList& defaultFileMetadata) | ||
Slice::Unit::createUnit(string_view languagePrefix, bool all, const StringList& defaultFileMetadata) | ||
{ | ||
MetadataList defaultMetadata; | ||
for (const auto& metadataString : defaultFileMetadata) | ||
{ | ||
defaultMetadata.push_back(make_shared<Metadata>(metadataString, "<command-line>", 0)); | ||
} | ||
|
||
UnitPtr unit{new Unit{all, std::move(defaultMetadata)}}; | ||
UnitPtr unit{new Unit{languagePrefix, all, std::move(defaultMetadata)}}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code above is a mystery to me. Would be nice to add a comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not my code, but as I read it, we're creating a cyclic There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SyntaxTreeBase has a We can consider replacing this by a virtual There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think another option is just not having
So, it'd actually be nice to remove this useless inheritance, or simplify it somehow. Unfortunately, we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was talking about this code: MetadataList defaultMetadata;
for (const auto& metadataString : defaultFileMetadata)
{
defaultMetadata.push_back(make_shared<Metadata>(metadataString, "<command-line>", 0));
} |
||
unit->_unit = unit; | ||
return unit; | ||
} | ||
|
||
string_view | ||
Slice::Unit::languagePrefix() const | ||
{ | ||
return _languagePrefix; | ||
} | ||
|
||
void | ||
Slice::Unit::setDocComment(const string& comment) | ||
{ | ||
|
@@ -5017,15 +5020,16 @@ Slice::Unit::getTopLevelModules(const string& file) const | |
} | ||
} | ||
|
||
Slice::Unit::Unit(bool all, MetadataList defaultFileMetadata) | ||
Slice::Unit::Unit(std::string_view languagePrefix, bool all, MetadataList defaultFileMetadata) | ||
: SyntaxTreeBase(nullptr), | ||
Container(nullptr), | ||
_languagePrefix(languagePrefix), | ||
_all(all), | ||
_defaultFileMetadata(std::move(defaultFileMetadata)), | ||
_errors(0), | ||
_currentIncludeLevel(0) | ||
|
||
{ | ||
assert(binary_search(&languages[0], &languages[sizeof(languages) / sizeof(*languages)], _languagePrefix)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this work for 'icerpc'? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It didn't, but I fixed it. And opened an issue to run |
||
} | ||
|
||
// ---------------------------------------------------------------------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -363,10 +363,11 @@ namespace Slice | |
{ | ||
public: | ||
[[nodiscard]] ContainerPtr container() const; | ||
[[nodiscard]] std::string name() const; | ||
[[nodiscard]] std::string scoped() const; | ||
[[nodiscard]] std::string scope() const; | ||
[[nodiscard]] std::string flattenedScope() const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
[[nodiscard]] std::string name(bool mappedName = false) const; | ||
[[nodiscard]] std::string scoped(bool mappedName = false) const; | ||
[[nodiscard]] std::string scope(bool mappedName = false) const; | ||
|
||
[[nodiscard]] std::string file() const; | ||
[[nodiscard]] int line() const; | ||
|
||
|
@@ -401,7 +402,6 @@ namespace Slice | |
|
||
ContainerPtr _container; | ||
std::string _name; | ||
std::string _scoped; | ||
std::string _file; | ||
int _line; | ||
std::string _docComment; | ||
|
@@ -453,7 +453,6 @@ namespace Slice | |
EnumeratorList enumerators() const; | ||
EnumeratorList enumerators(const std::string& identifier) const; | ||
ContainedList contents() const; | ||
std::string thisScope() const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is only used internally. |
||
void visit(ParserVisitor* visitor) override; | ||
|
||
bool checkIntroduced(const std::string& scopedName, ContainedPtr namedThing = nullptr); | ||
|
@@ -483,6 +482,8 @@ namespace Slice | |
} | ||
|
||
protected: | ||
std::string thisScope() const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
|
||
bool validateConstant( | ||
const std::string& name, | ||
const TypePtr& type, | ||
|
@@ -988,7 +989,9 @@ namespace Slice | |
class Unit final : public virtual Container | ||
{ | ||
public: | ||
static UnitPtr createUnit(bool all, const StringList& defaultFileMetadata = StringList()); | ||
static UnitPtr createUnit(std::string_view languagePrefix, bool all, const StringList& defaultFileMetadata = StringList()); | ||
|
||
[[nodiscard]] std::string_view languagePrefix() const; | ||
|
||
void setDocComment(const std::string& comment); | ||
void addToDocComment(const std::string& comment); | ||
|
@@ -1040,11 +1043,12 @@ namespace Slice | |
std::set<std::string> getTopLevelModules(const std::string& file) const; | ||
|
||
private: | ||
Unit(bool all, MetadataList defaultFileMetadata); | ||
Unit(std::string_view languagePrefix, bool all, MetadataList defaultFileMetadata); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this use of string_view over the top. Using strings is preferable, especially for something you adopt. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine, I switched it to use |
||
|
||
void pushDefinitionContext(); | ||
void popDefinitionContext(); | ||
|
||
const std::string_view _languagePrefix; | ||
bool _all; | ||
MetadataList _defaultFileMetadata; | ||
int _errors; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,7 +176,7 @@ compile(const vector<string>& argv) | |
} | ||
else | ||
{ | ||
UnitPtr p = Unit::createUnit(false); | ||
UnitPtr p = Unit::createUnit("icerpc", false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this work since icerpc is not a mapped language in Parser.cpp? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't. Good catch that I forgot to add it to the list of known languages. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think adding 'icerpc' to the list of known language mappings is a good fix. You could make this first parameter std::optional or treat "" as a special value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed |
||
int parseStatus = p->parse(*i, cppHandle, debug); | ||
|
||
if (!icecpp->close()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just swapped the order here to be consistent with other lists.