| 1 | // pgen2.cc
 | 
| 2 | 
 | 
| 3 | #include "pgen2.h"
 | 
| 4 | 
 | 
| 5 | #include <vector>
 | 
| 6 | 
 | 
| 7 | namespace pnode {
 | 
| 8 | 
 | 
| 9 | PNode::PNode(int typ, syntax_asdl::Token* tok, List<PNode*>*)
 | 
| 10 |     : typ(typ), tok(tok), children() {
 | 
| 11 | }
 | 
| 12 | 
 | 
| 13 | void PNode::AddChild(PNode* node) {
 | 
| 14 |   children.push_back(node);
 | 
| 15 | }
 | 
| 16 | 
 | 
| 17 | PNode* PNode::GetChild(int i) {
 | 
| 18 |   int j = i;
 | 
| 19 |   if (j < 0) {
 | 
| 20 |     j += NumChildren();
 | 
| 21 |   }
 | 
| 22 |   return children[j];
 | 
| 23 | }
 | 
| 24 | 
 | 
| 25 | int PNode::NumChildren() {
 | 
| 26 |   return children.size();
 | 
| 27 | }
 | 
| 28 | 
 | 
| 29 | PNodeAllocator::PNodeAllocator() : arena_(new std::vector<PNode>()) {
 | 
| 30 |   arena_->reserve(512);
 | 
| 31 | }
 | 
| 32 | 
 | 
| 33 | PNode* PNodeAllocator::NewPNode(int typ, syntax_asdl::Token* tok) {
 | 
| 34 |   CHECK(arena_->size() < arena_->capacity());
 | 
| 35 |   arena_->emplace_back(typ, tok, nullptr);
 | 
| 36 |   return arena_->data() + (arena_->size() - 1);
 | 
| 37 | }
 | 
| 38 | 
 | 
| 39 | void PNodeAllocator::Clear() {
 | 
| 40 |   delete arena_;
 | 
| 41 |   arena_ = nullptr;
 | 
| 42 | }
 | 
| 43 | 
 | 
| 44 | }  // namespace pnode
 |