OILS / cpp / pgen2.h View on Github | oilshell.org

86 lines, 58 significant
1// pgen2.h
2
3#ifndef CPP_PGEN2_H
4#define CPP_PGEN2_H
5
6#include <deque>
7#include <vector>
8
9#include "_gen/frontend/syntax.asdl.h"
10#include "mycpp/runtime.h"
11
12namespace grammar {
13
14typedef Tuple2<int, int> arc_t;
15typedef Dict<int, int> first_t;
16typedef List<List<arc_t*>*> states_t;
17typedef Tuple2<states_t*, first_t*> dfa_t;
18
19class Grammar {
20 public:
21 Grammar();
22
23 Dict<BigStr*, int>* symbol2number;
24 Dict<int, BigStr*>* number2symbol;
25 List<List<Tuple2<int, int>*>*>* states;
26 Dict<int, Tuple2<List<List<Tuple2<int, int>*>*>*, Dict<int, int>*>*>* dfas;
27 List<int>* labels;
28 Dict<BigStr*, int>* keywords;
29 Dict<int, int>* tokens;
30 Dict<BigStr*, int>* symbol2label;
31 int start;
32
33 static constexpr ObjHeader obj_header() {
34 return ObjHeader::ClassFixed(field_mask(), sizeof(Grammar));
35 }
36
37 static constexpr uint32_t field_mask() {
38 return maskbit(offsetof(Grammar, symbol2number)) |
39 maskbit(offsetof(Grammar, number2symbol)) |
40 maskbit(offsetof(Grammar, states)) |
41 maskbit(offsetof(Grammar, dfas)) |
42 maskbit(offsetof(Grammar, labels)) |
43 maskbit(offsetof(Grammar, keywords)) |
44 maskbit(offsetof(Grammar, tokens)) |
45 maskbit(offsetof(Grammar, symbol2label));
46 }
47
48 DISALLOW_COPY_AND_ASSIGN(Grammar)
49};
50
51} // namespace grammar
52
53namespace pnode {
54
55class PNode {
56 public:
57 PNode(int typ, syntax_asdl::Token* tok, List<PNode*>*);
58
59 void AddChild(PNode* node);
60 PNode* GetChild(int i);
61 int NumChildren();
62
63 int typ;
64 syntax_asdl::Token* tok;
65 std::vector<PNode*> children;
66};
67
68class PNodeAllocator {
69 public:
70 PNodeAllocator();
71
72 PNode* NewPNode(int typ, syntax_asdl::Token* tok);
73 void Clear();
74
75 static constexpr ObjHeader obj_header() {
76 return ObjHeader::Class(HeapTag::Opaque, kZeroMask, sizeof(PNodeAllocator));
77 }
78
79 private:
80 // We put this on the heap so we can call its destructor from `Clear()`...
81 std::deque<PNode>* arena_;
82};
83
84} // namespace pnode
85
86#endif // CPP_PGEN2_H