cpplint
This commit is contained in:
parent
25eda9d889
commit
0d763d229d
28 changed files with 77 additions and 78 deletions
|
|
@ -1,9 +1,8 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
cpplint=externals/cpplint.py
|
||||
|
||||
find src/compiler -type f | xargs $cpplint \
|
||||
externals/cpplint.py \
|
||||
--root=src \
|
||||
--linelength=110 \
|
||||
--filter=-readability/namespace,-legal/copyright \
|
||||
--filter=-legal/copyright,-readability/namespace,-whitespace/indent,-whitespace/line_length \
|
||||
$(find src/compiler -type f) \
|
||||
2>&1
|
||||
|
|
|
|||
|
|
@ -41,12 +41,12 @@ describe("computing FIRST sets", []() {
|
|||
i_token(1) }),
|
||||
i_sym(0) });
|
||||
|
||||
Grammar grammar({
|
||||
PreparedGrammar grammar({
|
||||
{ "rule0", seq({
|
||||
i_token(2),
|
||||
i_token(3),
|
||||
i_token(4) }) }
|
||||
});
|
||||
}, {});
|
||||
|
||||
AssertThat(first_set(rule, grammar), Equals(set<Symbol>({
|
||||
Symbol(0, SymbolOptionToken),
|
||||
|
|
@ -59,11 +59,11 @@ describe("computing FIRST sets", []() {
|
|||
i_sym(0),
|
||||
i_token(1) });
|
||||
|
||||
Grammar grammar({
|
||||
PreparedGrammar grammar({
|
||||
{ "rule0", choice({
|
||||
i_token(0),
|
||||
blank() }) }
|
||||
});
|
||||
}, {});
|
||||
|
||||
AssertThat(first_set(rule, grammar), Equals(set<Symbol>({
|
||||
Symbol(0, SymbolOptionToken),
|
||||
|
|
@ -74,12 +74,12 @@ describe("computing FIRST sets", []() {
|
|||
|
||||
describe("when there are left-recursive rules", [&]() {
|
||||
it("terminates", [&]() {
|
||||
Grammar grammar({
|
||||
PreparedGrammar grammar({
|
||||
{ "rule0", choice({
|
||||
seq({ i_sym(0), i_token(10) }),
|
||||
i_token(11),
|
||||
}) },
|
||||
});
|
||||
}, {});
|
||||
|
||||
auto rule = i_sym(0);
|
||||
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ describe("merging character set transitions", []() {
|
|||
typedef map<CharacterSet, int> int_map;
|
||||
|
||||
auto merge_result = [&](int_map left, int_map right) -> int_map {
|
||||
merge_char_transitions<int>(left, right, [](int l, int r) -> int {
|
||||
return l | r;
|
||||
merge_char_transitions<int>(&left, right, [](const int *l, const int *r) -> int {
|
||||
return *l | *r;
|
||||
});
|
||||
return left;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -37,9 +37,9 @@ describe("extracting tokens from a grammar", []() {
|
|||
});
|
||||
|
||||
it("does not extract blanks into tokens", [&]() {
|
||||
pair<PreparedGrammar, PreparedGrammar> result = extract_tokens(Grammar({
|
||||
pair<PreparedGrammar, PreparedGrammar> result = extract_tokens(PreparedGrammar({
|
||||
{ "rule1", choice({ i_sym(0), blank() }) },
|
||||
}));
|
||||
}, {}));
|
||||
|
||||
AssertThat(result.first, Equals(PreparedGrammar({
|
||||
{ "rule1", choice({ i_sym(0), blank() }) },
|
||||
|
|
|
|||
|
|
@ -192,7 +192,7 @@ namespace tree_sitter {
|
|||
|
||||
const vector<Conflict> conflicts() {
|
||||
return conflict_manager.conflicts();
|
||||
};
|
||||
}
|
||||
|
||||
SymTransitions sym_transitions;
|
||||
ParseTable parse_table;
|
||||
|
|
|
|||
|
|
@ -130,4 +130,4 @@ namespace tree_sitter {
|
|||
message_for_action(right, parse_grammar)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ namespace tree_sitter {
|
|||
class FirstSet : public rules::RuleFn<set<Symbol>> {
|
||||
const PreparedGrammar *grammar;
|
||||
set<Symbol> visited_symbols;
|
||||
|
||||
public:
|
||||
explicit FirstSet(const PreparedGrammar *grammar) : grammar(grammar) {}
|
||||
|
||||
|
|
@ -68,4 +69,4 @@ namespace tree_sitter {
|
|||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,10 +13,10 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
public:
|
||||
GetMetadata(rules::MetadataKey key) : metadata_key(key) {}
|
||||
explicit GetMetadata(rules::MetadataKey key) : metadata_key(key) {}
|
||||
};
|
||||
|
||||
return GetMetadata(key).apply(rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include "compiler/build_tables/item_set_closure.h"
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
#include "tree_sitter/compiler.h"
|
||||
#include "compiler/build_tables/follow_sets.h"
|
||||
#include "compiler/build_tables/item.h"
|
||||
|
|
@ -34,4 +34,4 @@ namespace tree_sitter {
|
|||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,9 +15,9 @@ namespace tree_sitter {
|
|||
|
||||
namespace build_tables {
|
||||
template<typename T>
|
||||
static unordered_set<T> merge_sets(unordered_set<T> &left, const unordered_set<T> &right) {
|
||||
left.insert(right.begin(), right.end());
|
||||
return left;
|
||||
static unordered_set<T> merge_sets(unordered_set<T> *left, const unordered_set<T> *right) {
|
||||
left->insert(right->begin(), right->end());
|
||||
return *left;
|
||||
}
|
||||
|
||||
const Symbol placeholder_lookahead = Symbol(-100);
|
||||
|
|
@ -74,9 +74,9 @@ namespace tree_sitter {
|
|||
SymTransitions::operator()(const ParseItemSet &item_set, const PreparedGrammar &grammar) {
|
||||
map<Symbol, ParseItemSet> result;
|
||||
for (const ParseItem &item : item_set)
|
||||
merge_sym_transitions<ParseItemSet>(result,
|
||||
merge_sym_transitions<ParseItemSet>(&result,
|
||||
sym_transitions_for_item(this, item, grammar),
|
||||
[&](ParseItemSet &l, const ParseItemSet &r) {
|
||||
[&](ParseItemSet *l, const ParseItemSet *r) {
|
||||
return merge_sets(l, r);
|
||||
});
|
||||
return result;
|
||||
|
|
@ -94,11 +94,11 @@ namespace tree_sitter {
|
|||
LexItemSet({ next_item })
|
||||
});
|
||||
}
|
||||
merge_char_transitions<LexItemSet>(result, item_transitions, [](LexItemSet &l, const LexItemSet &r) {
|
||||
merge_char_transitions<LexItemSet>(&result, item_transitions, [](LexItemSet *l, const LexItemSet *r) -> LexItemSet {
|
||||
return merge_sets(l, r);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,16 +15,16 @@ namespace tree_sitter {
|
|||
* using the given function.
|
||||
*/
|
||||
template<typename T>
|
||||
void merge_sym_transitions(std::map<rules::Symbol, T> &left,
|
||||
void merge_sym_transitions(std::map<rules::Symbol, T> *left,
|
||||
const std::map<rules::Symbol, T> &right,
|
||||
std::function<T(T &, const T &)> merge_fn) {
|
||||
std::function<T(T *, const T *)> merge_fn) {
|
||||
for (auto &pair : right) {
|
||||
auto rule = pair.first;
|
||||
bool merged = false;
|
||||
for (auto &existing_pair : left) {
|
||||
for (auto &existing_pair : *left) {
|
||||
auto existing_rule = existing_pair.first;
|
||||
if (existing_rule == rule) {
|
||||
existing_pair.second = merge_fn(existing_pair.second, pair.second);
|
||||
existing_pair.second = merge_fn(&existing_pair.second, &pair.second);
|
||||
merged = true;
|
||||
break;
|
||||
} else if (rule < existing_rule) {
|
||||
|
|
@ -32,7 +32,7 @@ namespace tree_sitter {
|
|||
}
|
||||
}
|
||||
if (!merged)
|
||||
left.insert({ pair.first, pair.second });
|
||||
left->insert({ pair.first, pair.second });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,17 +43,17 @@ namespace tree_sitter {
|
|||
* merging the two previous values using the given function.
|
||||
*/
|
||||
template<typename T>
|
||||
void merge_char_transitions(std::map<rules::CharacterSet, T> &left,
|
||||
void merge_char_transitions(std::map<rules::CharacterSet, T> *left,
|
||||
const std::map<rules::CharacterSet, T> &right,
|
||||
std::function<T(T &, const T &)> merge_fn) {
|
||||
std::function<T(T *, const T *)> merge_fn) {
|
||||
for (auto &new_pair : right) {
|
||||
rules::CharacterSet new_char_set = new_pair.first;
|
||||
T new_value = new_pair.second;
|
||||
|
||||
std::map<rules::CharacterSet, T> pairs_to_insert;
|
||||
|
||||
auto iter = left.begin();
|
||||
while (iter != left.end()) {
|
||||
auto iter = left->begin();
|
||||
while (iter != left->end()) {
|
||||
rules::CharacterSet char_set = iter->first;
|
||||
T value = iter->second;
|
||||
|
||||
|
|
@ -62,17 +62,17 @@ namespace tree_sitter {
|
|||
new_char_set.remove_set(intersection);
|
||||
if (!char_set.is_empty())
|
||||
pairs_to_insert.insert({ char_set, value });
|
||||
pairs_to_insert.insert({ intersection, merge_fn(value, new_value) });
|
||||
left.erase(iter++);
|
||||
pairs_to_insert.insert({ intersection, merge_fn(&value, &new_value) });
|
||||
left->erase(iter++);
|
||||
} else {
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
|
||||
left.insert(pairs_to_insert.begin(), pairs_to_insert.end());
|
||||
left->insert(pairs_to_insert.begin(), pairs_to_insert.end());
|
||||
|
||||
if (!new_char_set.is_empty())
|
||||
left.insert({ new_char_set, new_pair.second });
|
||||
left->insert({ new_char_set, new_pair.second });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,25 +21,25 @@ namespace tree_sitter {
|
|||
|
||||
namespace build_tables {
|
||||
template<typename T>
|
||||
void merge_transitions(map<T, rule_ptr> &left, const map<T, rule_ptr> &right);
|
||||
void merge_transitions(map<T, rule_ptr> *left, const map<T, rule_ptr> &right);
|
||||
|
||||
template<>
|
||||
void merge_transitions(map<CharacterSet, rule_ptr> &left, const map<CharacterSet, rule_ptr> &right) {
|
||||
merge_char_transitions<rule_ptr>(left, right, [](rule_ptr left, rule_ptr right) {
|
||||
return rules::Choice::Build({ left, right });
|
||||
void merge_transitions(map<CharacterSet, rule_ptr> *left, const map<CharacterSet, rule_ptr> &right) {
|
||||
merge_char_transitions<rule_ptr>(left, right, [](rule_ptr *left, const rule_ptr *right) {
|
||||
return rules::Choice::Build({ *left, *right });
|
||||
});
|
||||
}
|
||||
|
||||
template<>
|
||||
void merge_transitions(map<Symbol, rule_ptr> &left, const map<Symbol, rule_ptr> &right) {
|
||||
merge_sym_transitions<rule_ptr>(left, right, [](rule_ptr left, rule_ptr right) {
|
||||
return rules::Choice::Build({ left, right });
|
||||
void merge_transitions(map<Symbol, rule_ptr> *left, const map<Symbol, rule_ptr> &right) {
|
||||
merge_sym_transitions<rule_ptr>(left, right, [](rule_ptr *left, const rule_ptr *right) {
|
||||
return rules::Choice::Build({ *left, *right });
|
||||
});
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void transform_transitions(map<T, rule_ptr> &transitions, std::function<const rule_ptr(rule_ptr)> fn) {
|
||||
for (auto &pair : transitions)
|
||||
void transform_transitions(map<T, rule_ptr> *transitions, std::function<const rule_ptr(rule_ptr)> fn) {
|
||||
for (auto &pair : *transitions)
|
||||
pair.second = fn(pair.second);
|
||||
}
|
||||
|
||||
|
|
@ -64,24 +64,24 @@ namespace tree_sitter {
|
|||
map<T, rule_ptr> apply_to(const rules::Choice *rule) {
|
||||
map<T, rule_ptr> result;
|
||||
for (const auto &el : rule->elements)
|
||||
merge_transitions<T>(result, this->apply(el));
|
||||
merge_transitions<T>(&result, this->apply(el));
|
||||
return result;
|
||||
}
|
||||
|
||||
map<T, rule_ptr> apply_to(const rules::Seq *rule) {
|
||||
auto result = this->apply(rule->left);
|
||||
transform_transitions(result, [&](const rule_ptr &left_rule) {
|
||||
transform_transitions(&result, [&](const rule_ptr &left_rule) {
|
||||
return rules::Seq::Build({ left_rule, rule->right });
|
||||
});
|
||||
if (rule_can_be_blank(rule->left)) {
|
||||
merge_transitions<T>(result, this->apply(rule->right));
|
||||
merge_transitions<T>(&result, this->apply(rule->right));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
map<T, rule_ptr> apply_to(const rules::Repeat *rule) {
|
||||
auto result = this->apply(rule->content);
|
||||
transform_transitions(result, [&](const rule_ptr &value) {
|
||||
transform_transitions(&result, [&](const rule_ptr &value) {
|
||||
return rules::Seq::Build({ value, rule->copy() });
|
||||
});
|
||||
return result;
|
||||
|
|
@ -89,7 +89,7 @@ namespace tree_sitter {
|
|||
|
||||
map<T, rule_ptr> apply_to(const rules::Metadata *rule) {
|
||||
auto result = this->apply(rule->rule);
|
||||
transform_transitions(result, [&](const rule_ptr &to_rule) {
|
||||
transform_transitions(&result, [&](const rule_ptr &to_rule) {
|
||||
return make_shared<rules::Metadata>(to_rule, rule->value);
|
||||
});
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -15,4 +15,4 @@ namespace tree_sitter {
|
|||
}
|
||||
}
|
||||
|
||||
#endif // COMPILER_BUILD_TABLES_RULE_TRANSITIONS_H_
|
||||
#endif // COMPILER_BUILD_TABLES_RULE_TRANSITIONS_H_
|
||||
|
|
|
|||
|
|
@ -17,4 +17,4 @@ namespace tree_sitter {
|
|||
std::ostream& operator<<(std::ostream &stream, const Conflict &conflict) {
|
||||
return stream << "#<conflict " + conflict.description + ">";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ namespace tree_sitter {
|
|||
const LexTable lex_table;
|
||||
const PreparedGrammar syntax_grammar;
|
||||
const PreparedGrammar lexical_grammar;
|
||||
|
||||
public:
|
||||
CCodeGenerator(string name,
|
||||
const ParseTable &parse_table,
|
||||
|
|
@ -83,7 +84,6 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
private:
|
||||
|
||||
const PreparedGrammar & grammar_for_symbol(const rules::Symbol &symbol) {
|
||||
return symbol.is_token() ? lexical_grammar : syntax_grammar;
|
||||
}
|
||||
|
|
@ -293,4 +293,4 @@ namespace tree_sitter {
|
|||
return CCodeGenerator(name, parse_table, lex_table, syntax_grammar, lexical_grammar).code();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,4 +10,4 @@ namespace tree_sitter {
|
|||
}
|
||||
}
|
||||
|
||||
#endif // COMPILER_GENERATE_CODE_TOKEN_DESCRIPTION_H_
|
||||
#endif // COMPILER_GENERATE_CODE_TOKEN_DESCRIPTION_H_
|
||||
|
|
|
|||
|
|
@ -79,4 +79,4 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
const LexStateId LexTable::ERROR_STATE_ID = -1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,4 +62,4 @@ namespace tree_sitter {
|
|||
return PreparedGrammar(rules, aux_rules);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#include "compiler/prepare_grammar/intern_symbols.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "tree_sitter/compiler.h"
|
||||
#include "compiler/prepared_grammar.h"
|
||||
#include "compiler/rules/visitor.h"
|
||||
|
|
@ -24,7 +25,7 @@ namespace tree_sitter {
|
|||
const Grammar grammar;
|
||||
using rules::IdentityRuleFn::apply_to;
|
||||
|
||||
long index_of(string rule_name) {
|
||||
int index_of(string rule_name) {
|
||||
for (size_t i = 0; i < grammar.rules.size(); i++)
|
||||
if (grammar.rules[i].first == rule_name)
|
||||
return i;
|
||||
|
|
@ -32,14 +33,14 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
rule_ptr apply_to(const rules::NamedSymbol *rule) {
|
||||
long index = index_of(rule->name);
|
||||
int index = index_of(rule->name);
|
||||
if (index == -1)
|
||||
missing_rule_name = rule->name;
|
||||
return make_shared<rules::Symbol>(index);
|
||||
}
|
||||
|
||||
public:
|
||||
InternSymbols(const Grammar &grammar) : grammar(grammar) {}
|
||||
explicit InternSymbols(const Grammar &grammar) : grammar(grammar) {}
|
||||
|
||||
string missing_rule_name;
|
||||
};
|
||||
|
|
@ -52,13 +53,13 @@ namespace tree_sitter {
|
|||
auto new_rule = interner.apply(pair.second);
|
||||
if (!interner.missing_rule_name.empty())
|
||||
return {
|
||||
PreparedGrammar(rules),
|
||||
PreparedGrammar({}, {}),
|
||||
new GrammarError(interner.missing_rule_name)
|
||||
};
|
||||
rules.push_back({ pair.first, new_rule });
|
||||
}
|
||||
|
||||
return { PreparedGrammar(rules), nullptr };
|
||||
return { PreparedGrammar(rules, {}), nullptr };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ namespace tree_sitter {
|
|||
class GrammarError {
|
||||
std::string rule_name;
|
||||
public:
|
||||
GrammarError(std::string rule_name);
|
||||
explicit GrammarError(std::string rule_name);
|
||||
std::string message() const;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -16,10 +16,6 @@ namespace tree_sitter {
|
|||
Grammar(rules),
|
||||
aux_rules(aux_rules) {}
|
||||
|
||||
PreparedGrammar::PreparedGrammar(const Grammar &grammar) :
|
||||
Grammar(grammar),
|
||||
aux_rules({}) {}
|
||||
|
||||
const rule_ptr & PreparedGrammar::rule(const Symbol &symbol) const {
|
||||
return symbol.is_auxiliary() ?
|
||||
aux_rules[symbol.index].second :
|
||||
|
|
@ -71,4 +67,4 @@ namespace tree_sitter {
|
|||
|
||||
return stream << string(">");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ namespace tree_sitter {
|
|||
public:
|
||||
PreparedGrammar(const std::vector<std::pair<std::string, rules::rule_ptr>> &rules,
|
||||
const std::vector<std::pair<std::string, rules::rule_ptr>> &aux_rules);
|
||||
PreparedGrammar(const Grammar &grammar);
|
||||
|
||||
bool operator==(const PreparedGrammar &other) const;
|
||||
const std::string & rule_name(const rules::Symbol &symbol) const;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ namespace tree_sitter {
|
|||
namespace rules {
|
||||
class Choice : public Rule {
|
||||
public:
|
||||
Choice(const std::vector<rule_ptr> &elements);
|
||||
explicit Choice(const std::vector<rule_ptr> &elements);
|
||||
static rule_ptr Build(const std::vector<rule_ptr> &rules);
|
||||
|
||||
bool operator==(const Rule& other) const;
|
||||
|
|
@ -23,4 +23,4 @@ namespace tree_sitter {
|
|||
}
|
||||
}
|
||||
|
||||
#endif // COMPILER_RULES_CHOICE_H_
|
||||
#endif // COMPILER_RULES_CHOICE_H_
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "compiler/rules/pattern.h"
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "compiler/rules/visitor.h"
|
||||
#include "compiler/rules/choice.h"
|
||||
#include "compiler/rules/seq.h"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include "compiler/rules/symbol.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include "compiler/rules/visitor.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef COMPILER_RULES_SYMBOL_H_
|
||||
#define COMPILER_RULES_SYMBOL_H_
|
||||
|
||||
#include <string>
|
||||
#include "compiler/rules/rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "compiler/rules/visitor.h"
|
||||
#include <vector>
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rules/blank.h"
|
||||
#include "compiler/rules/character_set.h"
|
||||
|
|
|
|||
|
|
@ -64,4 +64,4 @@ namespace tree_sitter {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue