ROSE  0.11.145.0
Ast.h
1 #ifndef Rosebud_Ast_H
2 #define Rosebud_Ast_H
3 
4 #include <Rosebud/BasicTypes.h>
5 
6 #include <Sawyer/Cached.h>
7 #include <Sawyer/Tree.h>
8 
9 #include <list>
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 namespace Rosebud {
15 
54 namespace Ast {
55 
57 // AST nodes specific to Rosebud
59 
60 //------------------------------------------------------------------------------------------------------------------------------
61 // Base class of all AST nodes for Rosebud
62 class Node: public Sawyer::Tree::Vertex<Node> {
63 public:
64  using Ptr = NodePtr;
65 
66  template<class T>
67  std::shared_ptr<T> findAncestor() {
68  return traverseReverse<T>([](const std::shared_ptr<T> &node, TraversalEvent) {
69  return node;
70  });
71  }
72 };
73 
74 //------------------------------------------------------------------------------------------------------------------------------
76 class TokenList: public Node {
77 public:
79  using Ptr = TokenListPtr;
80 
81 private:
82  Sawyer::Cached<std::string> string_; // cached string representation
83 
84 public:
86  std::vector<Token> tokens;
87 
88 protected:
90  TokenList();
91 
92 public:
94  static Ptr instance();
95 
97  bool empty() const;
98 
100  size_t size() const;
101 
103  void push_back(const Token &token);
104 
111  std::string string();
112  std::string string(const FilePtr&);
113  void string(const std::string&);
119  std::vector<Token>::iterator begin() {
120  return tokens.begin();
121  }
122  std::vector<Token>::iterator end() {
123  return tokens.end();
124  }
126 };
127 
128 //------------------------------------------------------------------------------------------------------------------------------
130 class CppStack: public Node {
131 public:
133  using Ptr = CppStackPtr;
134 
136  enum class Type {
137  IF,
138  ELSE,
139  END,
140  OTHER
141  };
142 
144  struct Directive {
147  std::string lexeme;
148  };
149 
154  using Level = std::list<Directive>;
155 
159  using Stack = std::vector<Level>;
160 
161 public:
164 
165 protected:
167  CppStack();
168 
169 public:
171  static Ptr instance();
172 
174  static Type type(const std::string &directive);
175 
179  static bool process(const FilePtr&, const Token &directive, Stack &runningStack);
180 
184  bool printError(const FilePtr &file) const;
185 
187  void emitOpen(std::ostream&);
188 
190  void emitClose(std::ostream&);
191 };
192 
193 //------------------------------------------------------------------------------------------------------------------------------
195 class ArgumentList: public Node {
196 public:
198  using Ptr = ArgumentListPtr;
199 
200 public:
201  EdgeVector<TokenList> elmts;
202 
203 protected:
204  ArgumentList();
205 
206 public:
208  static Ptr instance();
209 };
210 
211 //------------------------------------------------------------------------------------------------------------------------------
216 class Attribute: public Node {
217 public:
219  using Ptr = AttributePtr;
220 
221 public:
223  std::string fqName;
224 
226  std::vector<Token> nameTokens;
227 
232 
233 protected:
235  Attribute();
236 public:
237 
245  static Ptr instance();
246  static Ptr instance(const std::string &fqName, const std::vector<Token> &nameTokens);
248 };
249 
250 //------------------------------------------------------------------------------------------------------------------------------
252 class Definition: public Node {
253 public:
255  using Ptr = DefinitionPtr;
256 
257 public:
260 
262  std::string name;
263 
266 
270  std::string doc;
271 
276 
282 
288  std::string priorText;
289 
294 
296  EdgeVector<Attribute> attributes;
297 
298 protected:
300  Definition();
301 
302 public:
306  AttributePtr findAttribute(const std::string &fqName);
307 };
308 
309 //------------------------------------------------------------------------------------------------------------------------------
314 class Property: public Definition {
315 public:
317  using Ptr = PropertyPtr;
318 
319 public:
322 
325 
331 
337 
343 
344 protected:
346  Property();
347 
348 public:
350  static Ptr instance();
351 };
352 
353 //------------------------------------------------------------------------------------------------------------------------------
355 class Class: public Definition {
356 public:
358  using Ptr = ClassPtr;
359 
361  using Inheritance = std::vector<std::pair<std::string /*visibility*/, std::string/*name*/>>;
362 
363 public:
365  EdgeVector<Property> properties;
366 
369 
378  std::string endText;
379 
384 
386  std::string tag;
387 
388 protected:
390  Class();
391 
392 public:
394  static Ptr instance();
395 };
396 
397 //------------------------------------------------------------------------------------------------------------------------------
399 class File: public Node {
400 public:
402  using Ptr = FilePtr;
403 
404 private:
405  TokenStream stream_;
406 
407 public:
409  EdgeVector<Class> classes;
410 
415  std::string endText;
416 
421 
422 protected:
423  File() = delete;
424 
426  File(const std::string &name);
427 
428 public:
432  static Ptr instance(const std::string &name);
433 
435  std::string name();
436 
443 
447  const Token token(size_t lookAhead = 0);
448 
457  std::string lexeme(size_t position = 0);
458  std::string lexeme(const Token&);
467  bool matches(const Token&, const char*);
468  bool matches(const Token&, const std::string&);
469  bool matches(size_t lookAhead, const char*);
470  bool matches(size_t lookAhead, const std::string&);
471  bool matches(const char*);
472  bool matches(const std::string&);
478  bool matchesAny(size_t tokenPosition, const std::vector<std::string>&);
479 
483  bool startsWith(const Token&, const char*);
484 
492  Token consume();
493  void consume(size_t n);
501  std::string content(const std::vector<Token>&, Expand);
502 
506  std::string content(size_t begin, size_t end);
507 
516  std::string trimmedContent(size_t begin, size_t end, Token &outputToken);
517  std::string trimmedContent(size_t begin, size_t end, const Token &exclude, Token &outputToken);
524  void emitContext(std::ostream&, const Token &first, const Token &locus, const Token &last);
525 };
526 
527 //------------------------------------------------------------------------------------------------------------------------------
531 class Project: public Node {
532 public:
534  using Ptr = ProjectPtr;
535 
536 public:
538  EdgeVector<File> files;
539 
540 protected:
542  Project();
543 
544 public:
546  static Ptr instance();
547 
552  std::vector<ClassPtr> allClassesFileOrder();
553 };
554 
555 } // namespace
556 } // namespace
557 #endif
Type type
Type of conditional compilation directive.
Definition: Ast.h:145
std::string tag
If non-empty, overrides the tag name for this type.
Definition: Ast.h:386
Sawyer::Optional< std::string > dataMemberName
Optional data member name override.
Definition: Ast.h:330
CppStack()
Default constructor used only by derived classes.
std::string priorText
Input text before the definition.
Definition: Ast.h:288
size_t size() const
Number of tokens.
Information about C preprocessor conditional compilation directives.
Definition: Ast.h:130
static Ptr instance()
Allocating constructor.
std::vector< Token > tokens
The ordered tokens.
Definition: Ast.h:86
std::string content(const std::vector< Token > &, Expand)
Input string for token list.
std::vector< Token >::iterator end()
Iterators.
Definition: Ast.h:122
Conditional compilation directive.
Definition: Ast.h:144
std::string endText
Text after the last member definition.
Definition: Ast.h:378
std::string lexeme
Cached lexeme.
Definition: Ast.h:147
std::shared_ptr< TokenList > TokenListPtr
Shared-ownership pointer to a TokenList.
Definition: ud/BasicTypes.h:39
std::string fqName
Fully qualified name.
Definition: Ast.h:223
std::shared_ptr< Project > ProjectPtr
Shared-ownership pointer to a Project.
Definition: ud/BasicTypes.h:55
std::vector< Token >::iterator begin()
Iterators.
Definition: Ast.h:119
Token docToken
Token associated with the Doxygen comment.
Definition: Ast.h:275
std::string lexeme(size_t position=0)
Text associated with a token.
A node that holds a list of arguments.
Definition: Ast.h:195
Rosebud is a tool to generate code for ROSE.
Definition: Ast.h:14
std::shared_ptr< ArgumentList > ArgumentListPtr
Shared-ownership pointer to a ArgumentList.
Definition: ud/BasicTypes.h:41
static Ptr instance()
Allocating constructor.
Sawyer::Tree::TraversalEvent TraversalEvent
Alias for traversal events.
Definition: Tree.h:124
bool printError(const FilePtr &file) const
Print an error message for the top of the stack if there's something wrong with it.
Not a conditional compilation directive.
std::shared_ptr< Definition > DefinitionPtr
Shared-ownership pointer to a Definition.
Definition: ud/BasicTypes.h:47
EdgeVector< Property > properties
Non-null list of zero or more properties.
Definition: Ast.h:365
std::string name
Unqualified name for the definition.
Definition: Ast.h:262
std::vector< Level > Stack
A stack of nested conditional compilation directives.
Definition: Ast.h:159
void push_back(const Token &token)
Insert a token at the end of the list.
Sawyer::Optional< std::vector< std::string > > mutatorNames
Optional override for mutator names.
Definition: Ast.h:342
Definition()
Default constructor used only by derived classes.
Token startToken
Token at the start of the definition.
Definition: Ast.h:259
bool startsWith(const Token &, const char *)
Test whether a token starts with a certain string.
Edge< ArgumentList > arguments
Attribute arguments.
Definition: Ast.h:231
An input file.
Definition: Ast.h:399
Root of an AST for one or more input files.
Definition: Ast.h:531
const Token token(size_t lookAhead=0)
Current or future token.
Token endTextToken
Token that encloses endText.
Definition: Ast.h:420
AttributePtr findAttribute(const std::string &fqName)
Finds an attribute with the specified fully qualified name.
static Ptr instance()
Allocating constructor.
TokenList()
Default constructor used only by derived classes.
static Type type(const std::string &directive)
Type of directive.
Property()
Default constructor used only by derived classes.
EdgeVector< Attribute > attributes
Non-null pointer to the list of attributes controlling this property.
Definition: Ast.h:296
std::string doc
Doxygen documentation comment.
Definition: Ast.h:270
Represents a class definition.
Definition: Ast.h:355
std::string string()
Return the text for all tokens in this list.
Token endTextToken
Token that encloses endText.
Definition: Ast.h:383
void emitContext(std::ostream &, const Token &first, const Token &locus, const Token &last)
Emit the context for a diagnostic message.
Token priorTextToken
Token describing the location of the prior text.
Definition: Ast.h:293
Base class for class and property definitions.
Definition: Ast.h:252
std::string trimmedContent(size_t begin, size_t end, Token &outputToken)
Input string for file region.
static Ptr instance(const std::string &name)
Allocating constructor.
Base class for tree vertices.
Definition: Tree.h:114
Class()
Default constructor used only by derived classes.
Edge< TokenList > cType
Optional pointer to tokens that define the property type.
Definition: Ast.h:321
std::vector< Token > nameTokens
One or more tokens associated with the name.
Definition: Ast.h:226
Edge< CppStack > cppStack
C preprocessor pending conditional compilation directives.
Definition: Ast.h:281
std::string name()
Name of the input file.
bool empty() const
True if there are no tokens in this node.
static Ptr instance()
Allocating constructor.
Stack stack
The stack of nested conditional compilation directives.
Definition: Ast.h:163
An attribute adjusting the details for a definition.
Definition: Ast.h:216
Attribute()
Default constructor used only by derived classes.
std::string endText
Text after the last class definition until the end of the file.
Definition: Ast.h:415
void emitClose(std::ostream &)
Emit matching #endif for all control flow directives in this stack.
std::shared_ptr< Property > PropertyPtr
Shared-ownership pointer to a Property.
Definition: ud/BasicTypes.h:49
Expand
How to obtain text when converting a sequence of tokens to a string.
Definition: ud/BasicTypes.h:16
static bool process(const FilePtr &, const Token &directive, Stack &runningStack)
Adjust the stack based on the C preprocessor directive.
bool matchesAny(size_t tokenPosition, const std::vector< std::string > &)
Test whether a token matches any text.
Inheritance inheritance
Information about base classes.
Definition: Ast.h:368
Token consume()
Consume tokens.
std::shared_ptr< File > FilePtr
Shared-ownership pointer to a File.
Definition: ud/BasicTypes.h:53
Token nameToken
Token for the definition's unqualified name.
Definition: Ast.h:265
std::shared_ptr< Class > ClassPtr
Shared-ownership pointer to a Class.
Definition: ud/BasicTypes.h:51
Edge< TokenList > cInit
Optional pointer to tokens that define the property's initial value.
Definition: Ast.h:324
static Ptr instance()
Allocating constructor.
std::vector< std::pair< std::string, std::string >> Inheritance
A list of access specifiers and class names used for class inheritance.
Definition: Ast.h:361
TokenStream & tokenStream()
Token stream for the file.
std::shared_ptr< Attribute > AttributePtr
Shared-ownership pointer to a Attribute.
Definition: ud/BasicTypes.h:45
Token token
Token representing the conditional compilation directive.
Definition: Ast.h:146
std::vector< ClassPtr > allClassesFileOrder()
Return all classes defined in the input.
std::list< Directive > Level
The CPP directives that belong to the same level.
Definition: Ast.h:154
Node that holds a sequence of consecutive tokens from an input file.
Definition: Ast.h:76
Represents a class property definition.
Definition: Ast.h:314
Type
Type of CPP directive.
Definition: Ast.h:136
static Ptr instance()
Allocating constructor.
#if, #ifdef, or #ifndef.
std::shared_ptr< CppStack > CppStackPtr
Shared-ownership pointer to a CppStack.
Definition: ud/BasicTypes.h:43
Sawyer::Optional< std::vector< std::string > > accessorNames
Optional override for accessor names.
Definition: Ast.h:336
Project()
Default constructor used only by derived classes.
void emitOpen(std::ostream &)
Emit all control flow directives in this stack.
std::shared_ptr< Node > NodePtr
Shared-ownership pointer to a Node.
Definition: ud/BasicTypes.h:37
EdgeVector< Class > classes
Non-null list of zero or more class definitions.
Definition: Ast.h:409
EdgeVector< File > files
Non-null list of input files.
Definition: Ast.h:538
bool matches(const Token &, const char *)
Test whether a token matches a string.
static Ptr instance()
Allocating constructor.