|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <initializer_list> |
| 4 | +#include <list> |
| 5 | +#include <ostream> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +struct ASTLocation { |
| 9 | + size_t line; |
| 10 | + size_t column; |
| 11 | + |
| 12 | + operator std::string() const; |
| 13 | +}; |
| 14 | + |
| 15 | +std::ostream & operator<<(std::ostream &, const ASTLocation &); |
| 16 | + |
| 17 | +class Parser; |
| 18 | + |
| 19 | +class ASTNode { |
| 20 | + private: |
| 21 | + ASTNode() {} |
| 22 | + |
| 23 | + public: |
| 24 | + Parser *parser; |
| 25 | + int symbol; |
| 26 | + ASTLocation location; |
| 27 | + const std::string *lexerInfo; |
| 28 | + ASTNode *parent = nullptr; |
| 29 | + std::list<ASTNode *> children; |
| 30 | + int debugIndex = -1; |
| 31 | + |
| 32 | + ASTNode(Parser &, int sym, const ASTLocation &loc, const char *info); |
| 33 | + ASTNode(Parser &, int sym, const ASTLocation &loc, const std::string *info); |
| 34 | + ASTNode(Parser &, int sym, const char *info); |
| 35 | + ASTNode(Parser &, int sym, const std::string *info); |
| 36 | + ASTNode(Parser &, int sym, const ASTLocation &loc); |
| 37 | + ASTNode(Parser &, int sym); |
| 38 | + /** Constructs an ASTNode that adopts another node and copies its location. */ |
| 39 | + ASTNode(Parser &, int sym, ASTNode *, const char *info = ""); |
| 40 | + ASTNode(Parser &, int sym, ASTNode *, const std::string *info); |
| 41 | + virtual ~ASTNode(); |
| 42 | + |
| 43 | + ASTNode * operator[](size_t) const; |
| 44 | + ASTNode * at(size_t) const; |
| 45 | + size_t size() const; |
| 46 | + bool empty() const; |
| 47 | + ASTNode * adopt(ASTNode *, bool do_locate = false); |
| 48 | + ASTNode * adopt(std::initializer_list<ASTNode *>); |
| 49 | + ASTNode * absorb(ASTNode *); |
| 50 | + ASTNode * clear(); |
| 51 | + ASTNode * copy() const; |
| 52 | + /** Copies the location from another node. */ |
| 53 | + ASTNode * locate(const ASTNode *); |
| 54 | + /** Copies the location from the first node in the list that isn't null. */ |
| 55 | + ASTNode * locate(std::initializer_list<const ASTNode *>); |
| 56 | + ASTNode * locate(const ASTLocation &); |
| 57 | + long atoi() const; |
| 58 | + long atoi(int offset) const; |
| 59 | + const char * getName() const; |
| 60 | + void debug(int indent = 0, bool is_last = false, bool suppress_line = false) const; |
| 61 | + virtual std::string debugExtra() const; |
| 62 | + virtual std::string style() const; |
| 63 | + |
| 64 | + static void destroy(std::initializer_list<ASTNode *>); |
| 65 | + |
| 66 | + ASTNode * front() const; |
| 67 | + ASTNode * back() const; |
| 68 | + decltype(children)::iterator begin(); |
| 69 | + decltype(children)::iterator end(); |
| 70 | + decltype(children)::const_iterator begin() const; |
| 71 | + decltype(children)::const_iterator end() const; |
| 72 | + decltype(children)::const_iterator cbegin() const noexcept; |
| 73 | + decltype(children)::const_iterator cend() const noexcept; |
| 74 | +}; |
0 commit comments