summaryrefslogtreecommitdiffhomepage
path: root/grammer.h
blob: e179a9eef0af1aa1884d5e807af9577f0b6675dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once

#include "bnf.h"
#include "minicc.h"

#include <cstdint>
#include <deque>
#include <tuple>
#include <unordered_set>
#include <utility>

class GrammerTest;

namespace Gram {

struct NodePosition {
 index_t node_id{}; // 0-based
 index_t child_pos{}; // 0-based
};

// TreeNodes are only intermediate. Terminal symbols don't get TreeNodes
// token_id: index into token list
// node_id: index into tree node list
struct TreeNode {
 NodePosition pos; // position of this node in tree (i.e. parent node_id + child_pos in parent)
 index_t node_id{}; // this node's id

 std::string type;
 index_t variant; // bnf[type][variant]
 std::vector<int32_t> child_ids; // < 0: terminal: token_id; > 0: non-terminal: node_id; = 0: unset
};

class Compiler
{
private:
 // The result
 std::vector<TreeNode> nodes;
 
 // Input
 std::vector<Token> tokens;

 BNF m_bnf; // not const for access via operator[]
 const std::string m_top;

 std::unordered_map<std::string, std::unordered_set<std::pair<std::string, index_t>, PairHash>> reversedPosFirst; // possible parent types of first childs of a given type

 std::deque<index_t> symbol_variants;
 decltype(symbol_variants)::iterator symbol_variants_it;

 // Tree specific
 void clear();
 
 // Node specific
 std::string GetTypeOfNode(index_t node_id) const;
 index_t AddNode(const std::string& type, index_t variant, NodePosition pos = {});
 // Adds actually used Non-Terminal Symbol Removes it on scope exit (RAII)
 class AddNodeGuard {
  Compiler& m_compiler;
 public:
  AddNodeGuard(Compiler& compiler, index_t variant);
  ~AddNodeGuard();
 };
 void IncNodePosition(NodePosition& pos);

 // top-down algorithm
 std::unordered_map<std::string, size_t> m_min; // cache for MinimumSymbolsNeeded
 size_t minimumSymbolsNeeded(const std::string& symbol);
 size_t minimumSymbolsNeeded(const std::vector<std::string>& symbol_list);
 bool match(std::string symbol, size_t begin, size_t end);
 bool match(std::vector<std::string> symbol_list, size_t begin, size_t end);

 // start / end cache
 struct TupleHash {
  size_t operator()(const std::tuple<std::string,size_t,std::string>& t) const noexcept
  {
   size_t h0 {std::hash<std::string>{}(std::get<0>(t))};
   size_t h1 {std::hash<size_t     >{}(std::get<1>(t))};
   size_t h2 {std::hash<std::string>{}(std::get<2>(t))};
   return h0 ^ (h1 << 1) ^ (h2 << 2);
  }
 };
 // map combination of non-terminal+terminal symbol combination to list of
 // possible BNF positions for the specified non-terminal symbol
 std::unordered_map<std::pair<std::string, std::string>, std::vector<size_t>, PairHashSS> m_match_lut;
 std::unordered_map<std::string, size_t> m_empty_lut; // list of non-terminal symbols that can lead to empty token list (maps to variant)

 void fillStartCache();

 void constructTree();

public:
 Compiler(BNF& bnf, const std::string& top);
 std::vector<TreeNode> compile(std::vector<Token> p_tokens);
 void DumpTree();

 friend class ::GrammerTest;
};

bool ChildIdIsEmpty(int32_t child_id);
bool ChildIdIsToken(int32_t child_id);
bool ChildIdIsNode(int32_t child_id);
index_t TokenIdFromChildId(int32_t child_id);
int32_t ChildIdFromTokenId(index_t token_id);

} // namespace Gram