ROSE 0.11.145.192
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
14namespace Rosebud {
15
54namespace Ast {
55
57// AST nodes specific to Rosebud
59
60//------------------------------------------------------------------------------------------------------------------------------
62class Node: public Sawyer::Tree::Vertex<Node> {
63public:
64 virtual ~Node() {}
65
66 using Ptr = NodePtr;
67
68 template<class T>
69 std::shared_ptr<T> findAncestor() {
70 return traverseReverse<T>([](const std::shared_ptr<T> &node, TraversalEvent) {
71 return node;
72 });
73 }
74};
75
76//------------------------------------------------------------------------------------------------------------------------------
78class TokenList: public Node {
79public:
82
83private:
84 Sawyer::Cached<std::string> string_; // cached string representation
85
86public:
88 std::vector<Token> tokens;
89
90protected:
93
94public:
96 static Ptr instance();
97
99 bool empty() const;
100
102 size_t size() const;
103
105 void push_back(const Token &token);
106
113 std::string string();
114 std::string string(const FilePtr&);
115 void string(const std::string&);
121 std::vector<Token>::iterator begin() {
122 return tokens.begin();
123 }
124 std::vector<Token>::iterator end() {
125 return tokens.end();
126 }
128};
129
130//------------------------------------------------------------------------------------------------------------------------------
132class CppStack: public Node {
133public:
136
138 enum class Type {
139 IF,
140 ELSE,
141 END,
142 OTHER
143 };
144
146 struct Directive {
149 std::string lexeme;
150 };
151
156 using Level = std::list<Directive>;
157
161 using Stack = std::vector<Level>;
162
163public:
166
167protected:
170
171public:
173 static Ptr instance();
174
176 static Type type(const std::string &directive);
177
181 static bool process(const FilePtr&, const Token &directive, Stack &runningStack);
182
186 bool printError(const FilePtr &file) const;
187
189 void emitOpen(std::ostream&);
190
192 void emitClose(std::ostream&);
193};
194
195//------------------------------------------------------------------------------------------------------------------------------
197class ArgumentList: public Node {
198public:
201
202public:
203 EdgeVector<TokenList> elmts;
204
205protected:
206 ArgumentList();
207
208public:
210 static Ptr instance();
211};
212
213//------------------------------------------------------------------------------------------------------------------------------
218class Attribute: public Node {
219public:
222
223public:
225 std::string fqName;
226
228 std::vector<Token> nameTokens;
229
234
235protected:
238public:
239
247 static Ptr instance();
248 static Ptr instance(const std::string &fqName, const std::vector<Token> &nameTokens);
250};
251
252//------------------------------------------------------------------------------------------------------------------------------
254class Definition: public Node {
255public:
258
259public:
262
264 std::string name;
265
268
272 std::string doc;
273
278
284
290 std::string priorText;
291
296
298 EdgeVector<Attribute> attributes;
299
300protected:
303
304public:
308 AttributePtr findAttribute(const std::string &fqName);
309};
310
311//------------------------------------------------------------------------------------------------------------------------------
360
361//------------------------------------------------------------------------------------------------------------------------------
363class Class: public Definition {
364public:
366 using Ptr = ClassPtr;
367
369 using Inheritance = std::vector<std::pair<std::string /*visibility*/, std::string/*name*/>>;
370
371public:
373 EdgeVector<Property> properties;
374
377
386 std::string endText;
387
392
394 std::string tag;
395
398
402
403protected:
406
407public:
409 static Ptr instance();
410};
411
412//------------------------------------------------------------------------------------------------------------------------------
414class File: public Node {
415public:
417 using Ptr = FilePtr;
418
419private:
420 TokenStream stream_;
421
422public:
424 EdgeVector<Class> classes;
425
430 std::string endText;
431
436
437protected:
438 File() = delete;
439
441 File(const std::string &name);
442
443public:
447 static Ptr instance(const std::string &name);
448
450 std::string name();
451
458
462 const Token token(size_t lookAhead = 0);
463
472 std::string lexeme(size_t position = 0);
473 std::string lexeme(const Token&);
482 bool matches(const Token&, const char*);
483 bool matches(const Token&, const std::string&);
484 bool matches(size_t lookAhead, const char*);
485 bool matches(size_t lookAhead, const std::string&);
486 bool matches(const char*);
487 bool matches(const std::string&);
493 bool matchesAny(size_t tokenPosition, const std::vector<std::string>&);
494
498 bool startsWith(const Token&, const char*);
499
508 void consume(size_t n);
516 std::string content(const std::vector<Token>&, Expand);
517
521 std::string content(size_t begin, size_t end);
522
531 std::string trimmedContent(size_t begin, size_t end, Token &outputToken);
532 std::string trimmedContent(size_t begin, size_t end, const Token &exclude, Token &outputToken);
539 void emitContext(std::ostream&, const Token &first, const Token &locus, const Token &last);
540};
541
542//------------------------------------------------------------------------------------------------------------------------------
546class Project: public Node {
547public:
550
551public:
553 EdgeVector<File> files;
554
555protected:
558
559public:
561 static Ptr instance();
562
567 std::vector<ClassPtr> allClassesFileOrder();
568
572 ClassPtr findClassByName(const std::string&);
573};
574
575} // namespace
576} // namespace
577#endif
A node that holds a list of arguments.
Definition Ast.h:197
ArgumentListPtr Ptr
Shared-ownership pointer.
Definition Ast.h:200
static Ptr instance()
Allocating constructor.
An attribute adjusting the details for a definition.
Definition Ast.h:218
Attribute()
Default constructor used only by derived classes.
std::vector< Token > nameTokens
One or more tokens associated with the name.
Definition Ast.h:228
Edge< ArgumentList > arguments
Attribute arguments.
Definition Ast.h:233
std::string fqName
Fully qualified name.
Definition Ast.h:225
static Ptr instance()
Allocating constructor.
static Ptr instance(const std::string &fqName, const std::vector< Token > &nameTokens)
Allocating constructor.
AttributePtr Ptr
Shared-ownership pointer.
Definition Ast.h:221
Represents a class definition.
Definition Ast.h:363
std::vector< std::pair< std::string, std::string > > Inheritance
A list of access specifiers and class names used for class inheritance.
Definition Ast.h:369
Class()
Default constructor used only by derived classes.
std::string tag
If non-empty, overrides the tag name for this type.
Definition Ast.h:394
EdgeVector< Property > properties
Non-null list of zero or more properties.
Definition Ast.h:373
Inheritance inheritance
Information about base classes.
Definition Ast.h:376
std::string qualifiedNamespace
If non-empty, the qualified namespace in which this class is defined.
Definition Ast.h:397
Token endTextToken
Token that encloses endText.
Definition Ast.h:391
std::string userSerializerFunction
If non-empty, then the generated serialization functions will call a user-defined serialization funct...
Definition Ast.h:401
static Ptr instance()
Allocating constructor.
ClassPtr Ptr
Shared-ownership pointer.
Definition Ast.h:366
std::string endText
Text after the last member definition.
Definition Ast.h:386
Information about C preprocessor conditional compilation directives.
Definition Ast.h:132
std::list< Directive > Level
The CPP directives that belong to the same level.
Definition Ast.h:156
static bool process(const FilePtr &, const Token &directive, Stack &runningStack)
Adjust the stack based on the C preprocessor directive.
Type
Type of CPP directive.
Definition Ast.h:138
@ OTHER
Not a conditional compilation directive.
@ IF
#if, #ifdef, or #ifndef.
void emitOpen(std::ostream &)
Emit all control flow directives in this stack.
std::vector< Level > Stack
A stack of nested conditional compilation directives.
Definition Ast.h:161
static Type type(const std::string &directive)
Type of directive.
bool printError(const FilePtr &file) const
Print an error message for the top of the stack if there's something wrong with it.
void emitClose(std::ostream &)
Emit matching #endif for all control flow directives in this stack.
Stack stack
The stack of nested conditional compilation directives.
Definition Ast.h:165
CppStack()
Default constructor used only by derived classes.
static Ptr instance()
Allocating constructor.
CppStackPtr Ptr
Shared-ownership pointer.
Definition Ast.h:135
Base class for class and property definitions.
Definition Ast.h:254
std::string doc
Doxygen documentation comment.
Definition Ast.h:272
Definition()
Default constructor used only by derived classes.
DefinitionPtr Ptr
Shared-ownership pointer.
Definition Ast.h:257
Edge< CppStack > cppStack
C preprocessor pending conditional compilation directives.
Definition Ast.h:283
EdgeVector< Attribute > attributes
Non-null pointer to the list of attributes controlling this property.
Definition Ast.h:298
std::string priorText
Input text before the definition.
Definition Ast.h:290
AttributePtr findAttribute(const std::string &fqName)
Finds an attribute with the specified fully qualified name.
Token docToken
Token associated with the Doxygen comment.
Definition Ast.h:277
Token startToken
Token at the start of the definition.
Definition Ast.h:261
Token nameToken
Token for the definition's unqualified name.
Definition Ast.h:267
std::string name
Unqualified name for the definition.
Definition Ast.h:264
Token priorTextToken
Token describing the location of the prior text.
Definition Ast.h:295
An input file.
Definition Ast.h:414
void emitContext(std::ostream &, const Token &first, const Token &locus, const Token &last)
Emit the context for a diagnostic message.
std::string name()
Name of the input file.
FilePtr Ptr
Shared-ownership pointer.
Definition Ast.h:417
std::string endText
Text after the last class definition until the end of the file.
Definition Ast.h:430
bool matches(size_t lookAhead, const char *)
Test whether a token matches a string.
std::string content(size_t begin, size_t end)
Part of file.
std::string trimmedContent(size_t begin, size_t end, Token &outputToken)
Input string for file region.
std::string lexeme(size_t position=0)
Text associated with a token.
static Ptr instance(const std::string &name)
Allocating constructor.
bool matches(const Token &, const char *)
Test whether a token matches a string.
bool matchesAny(size_t tokenPosition, const std::vector< std::string > &)
Test whether a token matches any text.
std::string trimmedContent(size_t begin, size_t end, const Token &exclude, Token &outputToken)
Input string for file region.
const Token token(size_t lookAhead=0)
Current or future token.
bool startsWith(const Token &, const char *)
Test whether a token starts with a certain string.
bool matches(const Token &, const std::string &)
Test whether a token matches a string.
EdgeVector< Class > classes
Non-null list of zero or more class definitions.
Definition Ast.h:424
void consume(size_t n)
Consume tokens.
Token consume()
Consume tokens.
bool matches(const char *)
Test whether a token matches a string.
bool matches(const std::string &)
Test whether a token matches a string.
File(const std::string &name)
Constructor used only by derived classes.
std::string lexeme(const Token &)
Text associated with a token.
bool matches(size_t lookAhead, const std::string &)
Test whether a token matches a string.
TokenStream & tokenStream()
Token stream for the file.
std::string content(const std::vector< Token > &, Expand)
Input string for token list.
Token endTextToken
Token that encloses endText.
Definition Ast.h:435
Base class of all AST nodes for Rosebud.
Definition Ast.h:62
Root of an AST for one or more input files.
Definition Ast.h:546
ClassPtr findClassByName(const std::string &)
Find a class by name.
EdgeVector< File > files
Non-null list of input files.
Definition Ast.h:553
ProjectPtr Ptr
Shared-ownership pointer.
Definition Ast.h:549
std::vector< ClassPtr > allClassesFileOrder()
Return all classes defined in the input.
Project()
Default constructor used only by derived classes.
static Ptr instance()
Allocating constructor.
Represents a class property definition.
Definition Ast.h:316
Sawyer::Optional< std::vector< std::string > > accessorNames
Optional override for accessor names.
Definition Ast.h:338
Sawyer::Optional< std::vector< std::string > > mutatorNames
Optional override for mutator names.
Definition Ast.h:344
Edge< TokenList > cType
Optional pointer to tokens that define the property type.
Definition Ast.h:323
Edge< TokenList > cInit
Optional pointer to tokens that define the property's initial value.
Definition Ast.h:326
Sawyer::Optional< std::string > serializerBaseName
Optional override for the serializer name.
Definition Ast.h:350
PropertyPtr Ptr
Shared-ownership pointer.
Definition Ast.h:319
static Ptr instance()
Allocating constructor.
Sawyer::Optional< std::string > dataMemberName
Optional data member name override.
Definition Ast.h:332
Property()
Default constructor used only by derived classes.
Node that holds a sequence of consecutive tokens from an input file.
Definition Ast.h:78
size_t size() const
Number of tokens.
TokenListPtr Ptr
Shared-ownership pointer.
Definition Ast.h:81
void string(const std::string &)
Return the text for all tokens in this list.
std::vector< Token >::iterator begin()
Iterators.
Definition Ast.h:121
std::vector< Token > tokens
The ordered tokens.
Definition Ast.h:88
void push_back(const Token &token)
Insert a token at the end of the list.
std::string string()
Return the text for all tokens in this list.
bool empty() const
True if there are no tokens in this node.
TokenList()
Default constructor used only by derived classes.
std::string string(const FilePtr &)
Return the text for all tokens in this list.
static Ptr instance()
Allocating constructor.
std::vector< Token >::iterator end()
Iterators.
Definition Ast.h:124
Implements cache data members.
Definition Cached.h:40
Holds a value or nothing.
Definition Optional.h:56
Base class for tree vertices.
Definition Tree.h:125
std::shared_ptr< Class > ClassPtr
Shared-ownership pointer to a Class.
std::shared_ptr< Property > PropertyPtr
Shared-ownership pointer to a Property.
std::shared_ptr< Node > NodePtr
Shared-ownership pointer to a Node.
std::shared_ptr< Definition > DefinitionPtr
Shared-ownership pointer to a Definition.
std::shared_ptr< ArgumentList > ArgumentListPtr
Shared-ownership pointer to a ArgumentList.
std::shared_ptr< Project > ProjectPtr
Shared-ownership pointer to a Project.
std::shared_ptr< CppStack > CppStackPtr
Shared-ownership pointer to a CppStack.
std::shared_ptr< File > FilePtr
Shared-ownership pointer to a File.
std::shared_ptr< TokenList > TokenListPtr
Shared-ownership pointer to a TokenList.
std::shared_ptr< Attribute > AttributePtr
Shared-ownership pointer to a Attribute.
Rosebud is a tool to generate abstract syntax trees.
Definition Ast.h:14
Expand
How to obtain text when converting a sequence of tokens to a string.
TraversalEvent
Traversal event.
Definition Tree.h:44
Conditional compilation directive.
Definition Ast.h:146
Token token
Token representing the conditional compilation directive.
Definition Ast.h:148
std::string lexeme
Cached lexeme.
Definition Ast.h:149
Type type
Type of conditional compilation directive.
Definition Ast.h:147