teditor  1.8.0@@fee5e94
Terminal based editor written in C++
parse_tree.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include "core/utils.h"
5 #include <ostream>
6 #include <vector>
7 #include "core/pos2d.h"
8 
9 namespace teditor {
10 namespace parser {
11 
13 struct Token {
19  uint32_t type;
24 
25  bool isEof() const { return type == End; }
26 
28  static const uint32_t End;
30  static const uint32_t Unknown;
32  static const uint32_t Root;
33 
34  friend std::ostream& operator<<(std::ostream& os, const Token& tok);
35 }; // struct Token
36 
38 struct Node {
39  Node(const Token& t) : tok(t), children() {}
40  Node(uint32_t type, const Point& start, const Point& end);
41  ~Node() { for (auto* child : children) delete child; }
42  // ownership is taken over by this class
43  void add(Node* n) { children.push_back(n); }
44  void add(const Token& t) { add(new Node(t)); }
45  void add(uint32_t type, const Point& start, const Point& end);
46  size_t size() const { return children.size(); }
47  bool isLeaf() const { return children.empty(); }
48  Node& operator[](size_t idx) { return *children[idx]; }
49  const Node& operator[](size_t idx) const { return *children[idx]; }
50 
52  std::vector<Node*> children;
53 
54  friend std::ostream& operator<<(std::ostream& os, const Node& n);
55 
56  private:
57  void printNode(std::ostream& os, int depth) const;
58 }; // struct Node
59 
60 } // namespace parser
61 } // namespace teditor
parse_tree.h
teditor::parser::Node::Node
Node(const Token &t)
Definition: parse_tree.h:39
teditor::parser::operator<<
std::ostream & operator<<(std::ostream &os, const Token &tok)
Definition: parse_tree.cpp:11
teditor::parser::Token::isEof
bool isEof() const
Definition: parse_tree.h:25
teditor::parser::Node::operator[]
const Node & operator[](size_t idx) const
Definition: parse_tree.h:49
teditor::parser::Token::type
uint32_t type
Definition: parse_tree.h:19
teditor::Pos2d::y
T y
Definition: pos2d.h:16
teditor::parser::Token::operator<<
friend std::ostream & operator<<(std::ostream &os, const Token &tok)
Definition: parse_tree.cpp:11
teditor::parser::Token::end
Point end
Definition: parse_tree.h:23
utils.h
teditor::parser::Node::children
std::vector< Node * > children
Definition: parse_tree.h:52
teditor::parser::Node::isLeaf
bool isLeaf() const
Definition: parse_tree.h:47
teditor::parser::Node::add
void add(Node *n)
Definition: parse_tree.h:43
teditor::parser::Token
Definition: parse_tree.h:13
teditor::parser::Token::Unknown
static const uint32_t Unknown
Definition: parse_tree.h:30
teditor::parser::Token::End
static const uint32_t End
Definition: parse_tree.h:28
teditor::parser::Node::add
void add(const Token &t)
Definition: parse_tree.h:44
teditor::parser::Token::Root
static const uint32_t Root
Definition: parse_tree.h:32
teditor::parser::Node::size
size_t size() const
Definition: parse_tree.h:46
teditor::parser::Node
Definition: parse_tree.h:38
teditor::Pos2d< int >
teditor::parser::Node::operator<<
friend std::ostream & operator<<(std::ostream &os, const Node &n)
Definition: parse_tree.cpp:47
pos2d.h
teditor::parser::Node::tok
Token tok
Definition: parse_tree.h:51
teditor::Pos2d::x
T x
Definition: pos2d.h:16
teditor::parser::Token::start
Point start
Definition: parse_tree.h:21
teditor::parser::Node::~Node
~Node()
Definition: parse_tree.h:41
teditor
Definition: any.hpp:10
teditor::parser::Node::operator[]
Node & operator[](size_t idx)
Definition: parse_tree.h:48