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

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