Skip to content

Commit

Permalink
(Rosebud) Adjusted to use new Sawyer::Tree features
Browse files Browse the repository at this point in the history
* In particular, 1:N edges are used, which simplifies some code. For
  instance, you don't need the magic "*()" anymore when iterating over
  a node with a variable number of children.

  Example. Old code:

    for (const auto &attribute: *property->attributes())

  Becomes

    for (const auto &attribute: property->attributes)

RPM-357
  • Loading branch information
matzke1 committed Apr 19, 2023
1 parent 1405052 commit aabb913
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 66 deletions.
25 changes: 18 additions & 7 deletions src/Rosebud/Ast.C
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ CppStack::emitClose(std::ostream &out) {
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ArgumentList
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ArgumentList::ArgumentList()
: elmts(*this) {}

ArgumentList::Ptr
ArgumentList::instance() {
return Ptr(new ArgumentList);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Attribute
Expand All @@ -188,11 +199,11 @@ Attribute::instance(const std::string &fqName, const std::vector<Token> &nameTok
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Definition::Definition()
: cppStack(*this, CppStack::instance()), attributes(*this, AttributeList::instance()) {}
: cppStack(*this, CppStack::instance()), attributes(*this) {}

Attribute::Ptr
Definition::findAttribute(const std::string &fqName) {
for (const auto &attr: *attributes()) {
for (const auto &attr: attributes) {
if (attr->fqName == fqName)
return attr();
}
Expand All @@ -216,7 +227,7 @@ Property::instance() {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Class::Class()
: properties(*this, PropertyList::instance()) {}
: properties(*this) {}


Class::Ptr
Expand All @@ -229,7 +240,7 @@ Class::instance() {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

File::File(const std::string &name)
: stream_(name), classes(*this, ClassList::instance()) {}
: stream_(name), classes(*this) {}

File::Ptr
File::instance(const std::string &name) {
Expand Down Expand Up @@ -392,7 +403,7 @@ File::emitContext(std::ostream &out, const Token &first, const Token &locus, con
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Project::Project()
: files(*this, FileList::instance()) {}
: files(*this) {}

Project::Ptr
Project::instance() {
Expand All @@ -402,8 +413,8 @@ Project::instance() {
std::vector<Class::Ptr>
Project::allClassesFileOrder() {
std::vector<Class::Ptr> retval;
for (const auto &file: *files()) {
for (const auto &c: *file->classes())
for (const auto &file: files) {
for (const auto &c: file->classes)
retval.push_back(c());
}
return retval;
Expand Down
46 changes: 21 additions & 25 deletions src/Rosebud/Ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,22 @@ class CppStack: public Node {
};

//------------------------------------------------------------------------------------------------------------------------------
/** Ordered list of arguments.
*
* An argument list is a sequence of token sequences. */
using ArgumentList = Sawyer::Tree::List<Node, TokenList>;
/** A node that holds a list of arguments. */
class ArgumentList: public Node {
public:
/** Shared-ownership pointer. */
using Ptr = ArgumentListPtr;

public:
EdgeVector<TokenList> elmts;

protected:
ArgumentList();

public:
/** Allocating constructor. */
static Ptr instance();
};

//------------------------------------------------------------------------------------------------------------------------------
/** An attribute adjusting the details for a definition.
Expand All @@ -203,7 +215,7 @@ using ArgumentList = Sawyer::Tree::List<Node, TokenList>;
* definition. */
class Attribute: public Node {
public:
/** Allocating constructor. */
/** Shared-ownership pointer. */
using Ptr = AttributePtr;

public:
Expand Down Expand Up @@ -235,10 +247,6 @@ class Attribute: public Node {
/** @} */
};

//------------------------------------------------------------------------------------------------------------------------------
/** An ordered sequence of attributes with their arguments. */
using AttributeList = Sawyer::Tree::List<Node, Attribute>;

//------------------------------------------------------------------------------------------------------------------------------
/** Base class for class and property definitions. */
class Definition: public Node {
Expand Down Expand Up @@ -285,7 +293,7 @@ class Definition: public Node {
Token priorTextToken;

/** Non-null pointer to the list of attributes controlling this property. */
Edge<AttributeList> attributes;
EdgeVector<Attribute> attributes;

protected:
/** Default constructor used only by derived classes. */
Expand Down Expand Up @@ -342,10 +350,6 @@ class Property: public Definition {
static Ptr instance();
};

//------------------------------------------------------------------------------------------------------------------------------
/** An ordered sequence of properties. */
using PropertyList = Sawyer::Tree::List<Node, Property>;

//------------------------------------------------------------------------------------------------------------------------------
/** Represents a class definition. */
class Class: public Definition {
Expand All @@ -358,7 +362,7 @@ class Class: public Definition {

public:
/** Non-null list of zero or more properties. */
Edge<PropertyList> properties;
EdgeVector<Property> properties;

/** Information about base classes. */
Inheritance inheritance;
Expand Down Expand Up @@ -387,10 +391,6 @@ class Class: public Definition {
static Ptr instance();
};

//------------------------------------------------------------------------------------------------------------------------------
/** An ordered sequence of class definitions. */
using ClassList = Sawyer::Tree::List<Node, Class>;

//------------------------------------------------------------------------------------------------------------------------------
/** An input file. */
class File: public Node {
Expand All @@ -403,7 +403,7 @@ class File: public Node {

public:
/** Non-null list of zero or more class definitions. */
Edge<ClassList> classes;
EdgeVector<Class> classes;

/** Text after the last class definition until the end of the file.
*
Expand Down Expand Up @@ -521,10 +521,6 @@ class File: public Node {
void emitContext(std::ostream&, const Token &first, const Token &locus, const Token &last);
};

//------------------------------------------------------------------------------------------------------------------------------
/** An ordered list of files. */
using FileList = Sawyer::Tree::List<Node, File>;

//------------------------------------------------------------------------------------------------------------------------------
/** Root of an AST for one or more input files.
*
Expand All @@ -536,7 +532,7 @@ class Project: public Node {

public:
/** Non-null list of input files. */
Edge<FileList> files;
EdgeVector<File> files;

protected:
/** Default constructor used only by derived classes. */
Expand Down
2 changes: 2 additions & 0 deletions src/Rosebud/BasicTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Node;
using NodePtr = std::shared_ptr<Node>; /**< Shared-ownership pointer to a @ref Node. */
class TokenList;
using TokenListPtr = std::shared_ptr<TokenList>; /**< Shared-ownership pointer to a @ref TokenList. */
class ArgumentList;
using ArgumentListPtr = std::shared_ptr<ArgumentList>; /**< Shared-ownership pointer to a @ref ArgumentList. */
class CppStack;
using CppStackPtr = std::shared_ptr<CppStack>; /**< Shared-ownership pointer to a @ref CppStack. */
class Attribute;
Expand Down
2 changes: 1 addition & 1 deletion src/Rosebud/BoostSerializer.C
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ BoostSerializer::generate(std::ostream &header, std::ostream &impl, const Ast::C
header <<THIS_LOCATION <<" s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(" <<super.second <<");\n";

// Serialize all properties that request serialization
for (const auto &p: *c->properties()) {
for (const auto &p:c->properties) {
if (!p->findAttribute("Rosebud::no_serialize")) {
const std::string memberName = generator.propertyDataMemberName(p());
header <<locationDirective(p->findAncestor<Ast::File>(), p->startToken)
Expand Down
6 changes: 3 additions & 3 deletions src/Rosebud/CxxGenerator.C
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ CxxGenerator::genDefaultConstructor(std::ostream &header, std::ostream &impl, co
impl <<"\n"
<<THIS_LOCATION <<c->name <<"::" <<c->name <<"()";
size_t nInits = 0;
for (const auto &p: *c->properties()) {
for (const auto &p: c->properties) {
const std::string expr = ctorInitializerExpression(p(), initialValue(p()));
if (!expr.empty()) {
impl <<"\n" <<THIS_LOCATION <<locationDirective(p(), p->startToken)
Expand Down Expand Up @@ -142,7 +142,7 @@ CxxGenerator::genArgsConstructor(std::ostream &header, std::ostream &impl, const
}

// Emit the property initializations, some of which come from arguments
for (const auto &p: *c->properties()) {
for (const auto &p: c->properties) {
auto arg = std::find(args.begin(), args.end(), p());
auto argClass = p->findAncestor<Ast::Class>();
if (arg != args.end()) {
Expand Down Expand Up @@ -233,7 +233,7 @@ CxxGenerator::genInitProperties(std::ostream &header, std::ostream &impl, const
<<c->name <<"::initializeProperties() {\n";

// Initialize the properties of this class
for (const auto &p: *c->properties()) {
for (const auto &p: c->properties) {
const std::string stmt = resetStatement(p());
if (!stmt.empty())
impl <<" " <<stmt <<"\n";
Expand Down
4 changes: 2 additions & 2 deletions src/Rosebud/RoseGenerator.C
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ RoseGenerator::genTraversals(std::ostream &header, std::ostream &impl, const Ast

const std::string base = firstPublicBaseClass(c);
std::vector<Ast::Property::Ptr> children;
for (const auto &p: *c->properties()) {
for (const auto &p: c->properties) {
if (isTreeEdge(p()))
children.push_back(p());
}
Expand Down Expand Up @@ -382,7 +382,7 @@ RoseGenerator::genClass(const Ast::Class::Ptr &c, const Hierarchy &h) {
<<" using Ptr = " <<c->name <<"Ptr;\n";

// Class definition body between the "{" and "};"
for (const auto &p: *c->properties())
for (const auto &p: c->properties)
genProperty(header, impl, p());

// User-defined stuff at the end of the class before the closing "}"
Expand Down
2 changes: 1 addition & 1 deletion src/Rosebud/RosettaGenerator.C
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ RosettaGenerator::genClassDefinition(std::ostream &rosetta, const Ast::Class::Pt

genClassBegin(rosetta, c);

for (const auto &p: *c->properties()) {
for (const auto &p: c->properties) {
// Stuff in the input that's prior to the property definition should go in the "other" section of output
header <<locationDirective(p(), p->priorTextToken) <<p->priorText;

Expand Down
2 changes: 1 addition & 1 deletion src/Rosebud/Utility.C
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ allConstructorArguments(const Ast::Class::Ptr &c, const Hierarchy &h_) {
using Traversal = DepthFirstReverseGraphTraversal<Hierarchy>;
for (Traversal t(h, root, LEAVE_VERTEX); t; ++t) {
Ast::Class::Ptr baseClass = t.vertex()->value();
for (const auto &p: *baseClass->properties()) {
for (const auto &p: baseClass->properties) {
if (p->findAttribute("Rosebud::ctor_arg"))
retval.push_back(p());
}
Expand Down
14 changes: 7 additions & 7 deletions src/Rosebud/YamlGenerator.C
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ YamlGenerator::genAttribute(Sawyer::Yaml::Node &root, const Ast::Attribute::Ptr
root["name"] = attribute->fqName;
if (settings.showingLocations)
genLocation(root["name_location"], attribute, attribute->nameTokens);
if (attribute->arguments && !attribute->arguments->empty()) {
for (const auto &arg: *attribute->arguments()) {
if (attribute->arguments) {
for (const auto &arg: attribute->arguments->elmts) {
auto &argNode = root["arguments"].pushBack();
argNode["argument"] = arg->string();
if (settings.showingLocations)
Expand Down Expand Up @@ -137,7 +137,7 @@ YamlGenerator::genDefinition(Sawyer::Yaml::Node &root, const Ast::Definition::Pt
genLocation(root["prior_text_location"], defn, defn->priorTextToken);
}

for (const auto &attribute: *defn->attributes())
for (const auto &attribute: defn->attributes)
genAttribute(root["attributes"].pushBack(), attribute());
}

Expand Down Expand Up @@ -189,7 +189,7 @@ YamlGenerator::genClass(Sawyer::Yaml::Node &root, const Ast::Class::Ptr &c, cons
node["super_class"] = pair.second;
}

for (const auto &property: *c->properties()) {
for (const auto &property: c->properties) {
auto &node = root["properties"].pushBack();
genProperty(node, property());
}
Expand All @@ -216,7 +216,7 @@ YamlGenerator::generate(const Ast::Project::Ptr &project) {
checkClassHierarchy(h);

// Information about files
for (const auto &file: *project->files()) {
for (const auto &file: project->files) {
auto &fileNode = root["files"].pushBack();
fileNode["name"] = file->tokenStream().fileName();
fileNode["lines"] = file->tokenStream().content().nLines();
Expand All @@ -229,8 +229,8 @@ YamlGenerator::generate(const Ast::Project::Ptr &project) {
}

// Information about classes
for (const auto &file: *project->files()) {
for (const auto &c: *file->classes()) {
for (const auto &file: project->files) {
for (const auto &c: file->classes) {
auto &classNode = root["classes"].pushBack();
genClass(classNode, c(), h);
}
Expand Down
Loading

0 comments on commit aabb913

Please sign in to comment.