diff --git a/script/lint.sh b/script/lint.sh index 5945153c..a52e6528 100755 --- a/script/lint.sh +++ b/script/lint.sh @@ -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 diff --git a/spec/compiler/build_tables/first_set_spec.cc b/spec/compiler/build_tables/first_set_spec.cc index 5eb9f1bc..ee9ec8d5 100644 --- a/spec/compiler/build_tables/first_set_spec.cc +++ b/spec/compiler/build_tables/first_set_spec.cc @@ -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(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(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); diff --git a/spec/compiler/build_tables/merge_transitions_spec.cc b/spec/compiler/build_tables/merge_transitions_spec.cc index d8686e11..43e1d744 100644 --- a/spec/compiler/build_tables/merge_transitions_spec.cc +++ b/spec/compiler/build_tables/merge_transitions_spec.cc @@ -10,8 +10,8 @@ describe("merging character set transitions", []() { typedef map int_map; auto merge_result = [&](int_map left, int_map right) -> int_map { - merge_char_transitions(left, right, [](int l, int r) -> int { - return l | r; + merge_char_transitions(&left, right, [](const int *l, const int *r) -> int { + return *l | *r; }); return left; }; diff --git a/spec/compiler/prepare_grammar/extract_tokens_spec.cc b/spec/compiler/prepare_grammar/extract_tokens_spec.cc index d39f8a41..58f4442b 100644 --- a/spec/compiler/prepare_grammar/extract_tokens_spec.cc +++ b/spec/compiler/prepare_grammar/extract_tokens_spec.cc @@ -37,9 +37,9 @@ describe("extracting tokens from a grammar", []() { }); it("does not extract blanks into tokens", [&]() { - pair result = extract_tokens(Grammar({ + pair result = extract_tokens(PreparedGrammar({ { "rule1", choice({ i_sym(0), blank() }) }, - })); + }, {})); AssertThat(result.first, Equals(PreparedGrammar({ { "rule1", choice({ i_sym(0), blank() }) }, diff --git a/src/compiler/build_tables/build_tables.cc b/src/compiler/build_tables/build_tables.cc index 0d3ae78d..e8921be0 100644 --- a/src/compiler/build_tables/build_tables.cc +++ b/src/compiler/build_tables/build_tables.cc @@ -192,7 +192,7 @@ namespace tree_sitter { const vector conflicts() { return conflict_manager.conflicts(); - }; + } SymTransitions sym_transitions; ParseTable parse_table; diff --git a/src/compiler/build_tables/conflict_manager.cc b/src/compiler/build_tables/conflict_manager.cc index 0838a0cf..a7d506cb 100644 --- a/src/compiler/build_tables/conflict_manager.cc +++ b/src/compiler/build_tables/conflict_manager.cc @@ -130,4 +130,4 @@ namespace tree_sitter { message_for_action(right, parse_grammar))); } } -} \ No newline at end of file +} diff --git a/src/compiler/build_tables/first_set.cc b/src/compiler/build_tables/first_set.cc index 9036ee9a..b7c56609 100644 --- a/src/compiler/build_tables/first_set.cc +++ b/src/compiler/build_tables/first_set.cc @@ -16,6 +16,7 @@ namespace tree_sitter { class FirstSet : public rules::RuleFn> { const PreparedGrammar *grammar; set visited_symbols; + public: explicit FirstSet(const PreparedGrammar *grammar) : grammar(grammar) {} @@ -68,4 +69,4 @@ namespace tree_sitter { return result; } } -} \ No newline at end of file +} diff --git a/src/compiler/build_tables/get_metadata.cc b/src/compiler/build_tables/get_metadata.cc index ca616458..f1fbfd25 100644 --- a/src/compiler/build_tables/get_metadata.cc +++ b/src/compiler/build_tables/get_metadata.cc @@ -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); } } -} \ No newline at end of file +} diff --git a/src/compiler/build_tables/item_set_closure.cc b/src/compiler/build_tables/item_set_closure.cc index a0124e98..0f0c5f77 100644 --- a/src/compiler/build_tables/item_set_closure.cc +++ b/src/compiler/build_tables/item_set_closure.cc @@ -1,6 +1,6 @@ #include "compiler/build_tables/item_set_closure.h" -#include #include +#include #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; } } -} \ No newline at end of file +} diff --git a/src/compiler/build_tables/item_set_transitions.cc b/src/compiler/build_tables/item_set_transitions.cc index 0f124010..a275c0e2 100644 --- a/src/compiler/build_tables/item_set_transitions.cc +++ b/src/compiler/build_tables/item_set_transitions.cc @@ -15,9 +15,9 @@ namespace tree_sitter { namespace build_tables { template - static unordered_set merge_sets(unordered_set &left, const unordered_set &right) { - left.insert(right.begin(), right.end()); - return left; + static unordered_set merge_sets(unordered_set *left, const unordered_set *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 result; for (const ParseItem &item : item_set) - merge_sym_transitions(result, + merge_sym_transitions(&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(result, item_transitions, [](LexItemSet &l, const LexItemSet &r) { + merge_char_transitions(&result, item_transitions, [](LexItemSet *l, const LexItemSet *r) -> LexItemSet { return merge_sets(l, r); }); } return result; } } -} \ No newline at end of file +} diff --git a/src/compiler/build_tables/merge_transitions.h b/src/compiler/build_tables/merge_transitions.h index b06eecc6..0bbe82bc 100644 --- a/src/compiler/build_tables/merge_transitions.h +++ b/src/compiler/build_tables/merge_transitions.h @@ -15,16 +15,16 @@ namespace tree_sitter { * using the given function. */ template - void merge_sym_transitions(std::map &left, + void merge_sym_transitions(std::map *left, const std::map &right, - std::function merge_fn) { + std::function 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 - void merge_char_transitions(std::map &left, + void merge_char_transitions(std::map *left, const std::map &right, - std::function merge_fn) { + std::function merge_fn) { for (auto &new_pair : right) { rules::CharacterSet new_char_set = new_pair.first; T new_value = new_pair.second; std::map 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 }); } } } diff --git a/src/compiler/build_tables/rule_transitions.cc b/src/compiler/build_tables/rule_transitions.cc index 06bbe6cc..d2dc999c 100644 --- a/src/compiler/build_tables/rule_transitions.cc +++ b/src/compiler/build_tables/rule_transitions.cc @@ -21,25 +21,25 @@ namespace tree_sitter { namespace build_tables { template - void merge_transitions(map &left, const map &right); + void merge_transitions(map *left, const map &right); template<> - void merge_transitions(map &left, const map &right) { - merge_char_transitions(left, right, [](rule_ptr left, rule_ptr right) { - return rules::Choice::Build({ left, right }); + void merge_transitions(map *left, const map &right) { + merge_char_transitions(left, right, [](rule_ptr *left, const rule_ptr *right) { + return rules::Choice::Build({ *left, *right }); }); } template<> - void merge_transitions(map &left, const map &right) { - merge_sym_transitions(left, right, [](rule_ptr left, rule_ptr right) { - return rules::Choice::Build({ left, right }); + void merge_transitions(map *left, const map &right) { + merge_sym_transitions(left, right, [](rule_ptr *left, const rule_ptr *right) { + return rules::Choice::Build({ *left, *right }); }); } template - void transform_transitions(map &transitions, std::function fn) { - for (auto &pair : transitions) + void transform_transitions(map *transitions, std::function fn) { + for (auto &pair : *transitions) pair.second = fn(pair.second); } @@ -64,24 +64,24 @@ namespace tree_sitter { map apply_to(const rules::Choice *rule) { map result; for (const auto &el : rule->elements) - merge_transitions(result, this->apply(el)); + merge_transitions(&result, this->apply(el)); return result; } map 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(result, this->apply(rule->right)); + merge_transitions(&result, this->apply(rule->right)); } return result; } map 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 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(to_rule, rule->value); }); return result; diff --git a/src/compiler/build_tables/rule_transitions.h b/src/compiler/build_tables/rule_transitions.h index 29e43fdc..06b19820 100644 --- a/src/compiler/build_tables/rule_transitions.h +++ b/src/compiler/build_tables/rule_transitions.h @@ -15,4 +15,4 @@ namespace tree_sitter { } } -#endif // COMPILER_BUILD_TABLES_RULE_TRANSITIONS_H_ \ No newline at end of file +#endif // COMPILER_BUILD_TABLES_RULE_TRANSITIONS_H_ diff --git a/src/compiler/conflict.cc b/src/compiler/conflict.cc index ffe9fd0d..7fb942c4 100644 --- a/src/compiler/conflict.cc +++ b/src/compiler/conflict.cc @@ -17,4 +17,4 @@ namespace tree_sitter { std::ostream& operator<<(std::ostream &stream, const Conflict &conflict) { return stream << "#"; } -} \ No newline at end of file +} diff --git a/src/compiler/generate_code/c_code.cc b/src/compiler/generate_code/c_code.cc index e56b657a..690f2ab7 100644 --- a/src/compiler/generate_code/c_code.cc +++ b/src/compiler/generate_code/c_code.cc @@ -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(); } } -} \ No newline at end of file +} diff --git a/src/compiler/generate_code/token_description.h b/src/compiler/generate_code/token_description.h index 38eff77d..4e70ee6e 100644 --- a/src/compiler/generate_code/token_description.h +++ b/src/compiler/generate_code/token_description.h @@ -10,4 +10,4 @@ namespace tree_sitter { } } -#endif // COMPILER_GENERATE_CODE_TOKEN_DESCRIPTION_H_ \ No newline at end of file +#endif // COMPILER_GENERATE_CODE_TOKEN_DESCRIPTION_H_ diff --git a/src/compiler/lex_table.cc b/src/compiler/lex_table.cc index 6b078091..77780467 100644 --- a/src/compiler/lex_table.cc +++ b/src/compiler/lex_table.cc @@ -79,4 +79,4 @@ namespace tree_sitter { } const LexStateId LexTable::ERROR_STATE_ID = -1; -} \ No newline at end of file +} diff --git a/src/compiler/prepare_grammar/expand_repeats.cc b/src/compiler/prepare_grammar/expand_repeats.cc index 49980181..b5ec93dc 100644 --- a/src/compiler/prepare_grammar/expand_repeats.cc +++ b/src/compiler/prepare_grammar/expand_repeats.cc @@ -62,4 +62,4 @@ namespace tree_sitter { return PreparedGrammar(rules, aux_rules); } } -} \ No newline at end of file +} diff --git a/src/compiler/prepare_grammar/intern_symbols.cc b/src/compiler/prepare_grammar/intern_symbols.cc index 0a05ea0b..2d67e21c 100644 --- a/src/compiler/prepare_grammar/intern_symbols.cc +++ b/src/compiler/prepare_grammar/intern_symbols.cc @@ -1,5 +1,6 @@ #include "compiler/prepare_grammar/intern_symbols.h" #include +#include #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(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 }; } } } diff --git a/src/compiler/prepare_grammar/intern_symbols.h b/src/compiler/prepare_grammar/intern_symbols.h index d0a12f4d..0c2cc3ca 100644 --- a/src/compiler/prepare_grammar/intern_symbols.h +++ b/src/compiler/prepare_grammar/intern_symbols.h @@ -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; }; diff --git a/src/compiler/prepared_grammar.cc b/src/compiler/prepared_grammar.cc index aa34cfbd..d5fd9cb2 100644 --- a/src/compiler/prepared_grammar.cc +++ b/src/compiler/prepared_grammar.cc @@ -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(">"); } -} \ No newline at end of file +} diff --git a/src/compiler/prepared_grammar.h b/src/compiler/prepared_grammar.h index 1e63e18c..faa7d3b6 100644 --- a/src/compiler/prepared_grammar.h +++ b/src/compiler/prepared_grammar.h @@ -12,7 +12,6 @@ namespace tree_sitter { public: PreparedGrammar(const std::vector> &rules, const std::vector> &aux_rules); - PreparedGrammar(const Grammar &grammar); bool operator==(const PreparedGrammar &other) const; const std::string & rule_name(const rules::Symbol &symbol) const; diff --git a/src/compiler/rules/choice.h b/src/compiler/rules/choice.h index fc66c174..3d6f62ff 100644 --- a/src/compiler/rules/choice.h +++ b/src/compiler/rules/choice.h @@ -9,7 +9,7 @@ namespace tree_sitter { namespace rules { class Choice : public Rule { public: - Choice(const std::vector &elements); + explicit Choice(const std::vector &elements); static rule_ptr Build(const std::vector &rules); bool operator==(const Rule& other) const; @@ -23,4 +23,4 @@ namespace tree_sitter { } } -#endif // COMPILER_RULES_CHOICE_H_ \ No newline at end of file +#endif // COMPILER_RULES_CHOICE_H_ diff --git a/src/compiler/rules/pattern.cc b/src/compiler/rules/pattern.cc index ecead488..45c71996 100644 --- a/src/compiler/rules/pattern.cc +++ b/src/compiler/rules/pattern.cc @@ -1,6 +1,7 @@ #include "compiler/rules/pattern.h" #include #include +#include #include "compiler/rules/visitor.h" #include "compiler/rules/choice.h" #include "compiler/rules/seq.h" diff --git a/src/compiler/rules/symbol.cc b/src/compiler/rules/symbol.cc index 8652d8f4..aa27a33b 100644 --- a/src/compiler/rules/symbol.cc +++ b/src/compiler/rules/symbol.cc @@ -1,6 +1,6 @@ #include "compiler/rules/symbol.h" -#include #include +#include #include "compiler/rules/visitor.h" namespace tree_sitter { diff --git a/src/compiler/rules/symbol.h b/src/compiler/rules/symbol.h index c73ae8be..5a4e1135 100644 --- a/src/compiler/rules/symbol.h +++ b/src/compiler/rules/symbol.h @@ -1,6 +1,7 @@ #ifndef COMPILER_RULES_SYMBOL_H_ #define COMPILER_RULES_SYMBOL_H_ +#include #include "compiler/rules/rule.h" namespace tree_sitter { diff --git a/src/compiler/rules/visitor.cc b/src/compiler/rules/visitor.cc index e64b1b0a..6e8d12a0 100644 --- a/src/compiler/rules/visitor.cc +++ b/src/compiler/rules/visitor.cc @@ -1,4 +1,5 @@ #include "compiler/rules/visitor.h" +#include #include "compiler/rules/rule.h" #include "compiler/rules/blank.h" #include "compiler/rules/character_set.h" diff --git a/src/compiler/util/string_helpers.cc b/src/compiler/util/string_helpers.cc index ace3da2b..7fe90645 100644 --- a/src/compiler/util/string_helpers.cc +++ b/src/compiler/util/string_helpers.cc @@ -64,4 +64,4 @@ namespace tree_sitter { } } } -} \ No newline at end of file +}