File: rule.cc

package info (click to toggle)
tree-sitter-cpp 0.23.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,304 kB
  • sloc: ansic: 530,934; javascript: 2,483; cpp: 414; lisp: 137; makefile: 101; python: 88; sh: 9
file content (287 lines) | stat: -rw-r--r-- 8,448 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "compiler/rule.h"
#include "compiler/util/hash_combine.h"

namespace tree_sitter {
namespace rules {

using std::move;
using std::vector;
using util::hash_combine;

Rule::Rule(const Rule &other) : blank_(Blank{}), type(BlankType) {
  *this = other;
}

Rule::Rule(Rule &&other) noexcept : blank_(Blank{}), type(BlankType) {
  *this = move(other);
}

static void destroy_value(Rule *rule) {
  switch (rule->type) {
    case Rule::BlankType: return rule->blank_.~Blank();
    case Rule::CharacterSetType: return rule->character_set_.~CharacterSet();
    case Rule::StringType: return rule->string_ .~String();
    case Rule::PatternType: return rule->pattern_ .~Pattern();
    case Rule::NamedSymbolType: return rule->named_symbol_.~NamedSymbol();
    case Rule::SymbolType: return rule->symbol_ .~Symbol();
    case Rule::ChoiceType: return rule->choice_ .~Choice();
    case Rule::MetadataType: return rule->metadata_ .~Metadata();
    case Rule::RepeatType: return rule->repeat_ .~Repeat();
    case Rule::SeqType: return rule->seq_ .~Seq();
  }
}

Rule &Rule::operator=(const Rule &other) {
  destroy_value(this);
  type = other.type;
  switch (type) {
    case BlankType:
      new (&blank_) Blank(other.blank_);
      break;
    case CharacterSetType:
      new (&character_set_) CharacterSet(other.character_set_);
      break;
    case StringType:
      new (&string_) String(other.string_);
      break;
    case PatternType:
      new (&pattern_) Pattern(other.pattern_);
      break;
    case NamedSymbolType:
      new (&named_symbol_) NamedSymbol(other.named_symbol_);
      break;
    case SymbolType:
      new (&symbol_) Symbol(other.symbol_);
      break;
    case ChoiceType:
      new (&choice_) Choice(other.choice_);
      break;
    case MetadataType:
      new (&metadata_) Metadata(other.metadata_);
      break;
    case RepeatType:
      new (&repeat_) Repeat(other.repeat_);
      break;
    case SeqType:
      new (&seq_) Seq(other.seq_);
      break;
  }
  return *this;
}

Rule &Rule::operator=(Rule &&other) noexcept {
  destroy_value(this);
  type = other.type;
  switch (type) {
    case BlankType:
      new (&blank_) Blank(move(other.blank_));
      break;
    case CharacterSetType:
      new (&character_set_) CharacterSet(move(other.character_set_));
      break;
    case StringType:
      new (&string_) String(move(other.string_));
      break;
    case PatternType:
      new (&pattern_) Pattern(move(other.pattern_));
      break;
    case NamedSymbolType:
      new (&named_symbol_) NamedSymbol(move(other.named_symbol_));
      break;
    case SymbolType:
      new (&symbol_) Symbol(move(other.symbol_));
      break;
    case ChoiceType:
      new (&choice_) Choice(move(other.choice_));
      break;
    case MetadataType:
      new (&metadata_) Metadata(move(other.metadata_));
      break;
    case RepeatType:
      new (&repeat_) Repeat(move(other.repeat_));
      break;
    case SeqType:
      new (&seq_) Seq(move(other.seq_));
      break;
  }
  other.type = BlankType;
  other.blank_ = Blank{};
  return *this;
}

Rule::~Rule() noexcept {
  destroy_value(this);
}

bool Rule::operator==(const Rule &other) const {
  if (type != other.type) return false;
  switch (type) {
    case Rule::CharacterSetType: return character_set_ == other.character_set_;
    case Rule::StringType: return string_ == other.string_;
    case Rule::PatternType: return pattern_ == other.pattern_;
    case Rule::NamedSymbolType: return named_symbol_ == other.named_symbol_;
    case Rule::SymbolType: return symbol_ == other.symbol_;
    case Rule::ChoiceType: return choice_ == other.choice_;
    case Rule::MetadataType: return metadata_ == other.metadata_;
    case Rule::RepeatType: return repeat_ == other.repeat_;
    case Rule::SeqType: return seq_ == other.seq_;
    default: return blank_ == other.blank_;
  }
}

template <>
bool Rule::is<Blank>() const { return type == BlankType; }

template <>
bool Rule::is<Symbol>() const { return type == SymbolType; }

template <>
bool Rule::is<Repeat>() const { return type == RepeatType; }

template <>
const Symbol & Rule::get_unchecked<Symbol>() const { return symbol_; }

static inline void add_choice_element(std::vector<Rule> *elements, const Rule &new_rule) {
  new_rule.match(
    [elements](Choice choice) {
      for (auto &element : choice.elements) {
        add_choice_element(elements, element);
      }
    },

    [elements](auto rule) {
      for (auto &element : *elements) {
        if (element == rule) return;
      }
      elements->push_back(rule);
    }
  );
}

Rule Rule::choice(const vector<Rule> &rules) {
  vector<Rule> elements;
  for (auto &element : rules) {
    add_choice_element(&elements, element);
  }
  return (elements.size() == 1) ? elements.front() : Choice{elements};
}

Rule Rule::repeat(const Rule &rule) {
  return rule.is<Repeat>() ? rule : Repeat{rule};
}

Rule Rule::seq(const vector<Rule> &rules) {
  Rule result;
  for (const auto &rule : rules) {
    rule.match(
      [](Blank) {},
      [&](Metadata metadata) {
        if (!metadata.rule->is<Blank>()) {
          result = Seq{result, rule};
        }
      },
      [&](auto) {
        if (result.is<Blank>()) {
          result = rule;
        } else {
          result = Seq{result, rule};
        }
      }
    );
  }
  return result;
}

}  // namespace rules
}  // namespace tree_sitter

namespace std {

size_t hash<Symbol>::operator()(const Symbol &symbol) const {
  auto result = hash<int>()(symbol.index);
  hash_combine(&result, hash<int>()(symbol.type));
  return result;
}

size_t hash<NamedSymbol>::operator()(const NamedSymbol &symbol) const {
  return hash<string>()(symbol.value);
}

size_t hash<Pattern>::operator()(const Pattern &symbol) const {
  return hash<string>()(symbol.value);
}

size_t hash<String>::operator()(const String &symbol) const {
  return hash<string>()(symbol.value);
}

size_t hash<CharacterSet>::operator()(const CharacterSet &character_set) const {
  size_t result = 0;
  hash_combine(&result, character_set.includes_all);
  hash_combine(&result, character_set.included_chars.size());
  for (uint32_t c : character_set.included_chars) {
    hash_combine(&result, c);
  }
  hash_combine(&result, character_set.excluded_chars.size());
  for (uint32_t c : character_set.excluded_chars) {
    hash_combine(&result, c);
  }
  return result;
}

size_t hash<Blank>::operator()(const Blank &blank) const {
  return 0;
}

size_t hash<Choice>::operator()(const Choice &choice) const {
  size_t result = 0;
  for (const auto &element : choice.elements) {
    symmetric_hash_combine(&result, element);
  }
  return result;
}

size_t hash<Repeat>::operator()(const Repeat &repeat) const {
  size_t result = 0;
  hash_combine(&result, *repeat.rule);
  return result;
}

size_t hash<Seq>::operator()(const Seq &seq) const {
  size_t result = 0;
  hash_combine(&result, *seq.left);
  hash_combine(&result, *seq.right);
  return result;
}

size_t hash<Metadata>::operator()(const Metadata &metadata) const {
  size_t result = 0;
  hash_combine(&result, *metadata.rule);
  hash_combine(&result, metadata.params.precedence);
  hash_combine<int>(&result, metadata.params.associativity);
  hash_combine(&result, metadata.params.has_precedence);
  hash_combine(&result, metadata.params.has_associativity);
  hash_combine(&result, metadata.params.is_token);
  hash_combine(&result, metadata.params.is_string);
  hash_combine(&result, metadata.params.is_active);
  hash_combine(&result, metadata.params.is_main_token);
  return result;
}

size_t hash<Rule>::operator()(const Rule &rule) const {
  size_t result = hash<int>()(rule.type);
  switch (rule.type) {
    case Rule::CharacterSetType: return result ^ hash<CharacterSet>()(rule.character_set_);
    case Rule::StringType: return result ^ hash<String>()(rule.string_);
    case Rule::PatternType: return result ^ hash<Pattern>()(rule.pattern_);
    case Rule::NamedSymbolType: return result ^ hash<NamedSymbol>()(rule.named_symbol_);
    case Rule::SymbolType: return result ^ hash<Symbol>()(rule.symbol_);
    case Rule::ChoiceType: return result ^ hash<Choice>()(rule.choice_);
    case Rule::MetadataType: return result ^ hash<Metadata>()(rule.metadata_);
    case Rule::RepeatType: return result ^ hash<Repeat>()(rule.repeat_);
    case Rule::SeqType: return result ^ hash<Seq>()(rule.seq_);
    default: return result ^ hash<Blank>()(rule.blank_);
  }
}

}  // namespace std