From 3aaa08b948065899210b6d64eeb10592032ff4bb Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sun, 9 Mar 2014 23:51:33 -0700 Subject: [PATCH] Fix some egregiously long lines --- script/lint.sh | 7 +++-- src/compiler/build_tables/build_tables.cc | 23 +++++++++++----- src/compiler/build_tables/build_tables.h | 3 ++- src/compiler/build_tables/first_set.h | 6 +++-- src/compiler/build_tables/follow_sets.cc | 3 ++- src/compiler/build_tables/follow_sets.h | 3 ++- src/compiler/build_tables/item.cc | 5 +++- src/compiler/build_tables/item.h | 5 +++- src/compiler/build_tables/item_set_closure.cc | 10 ++++--- src/compiler/build_tables/item_set_closure.h | 3 ++- .../build_tables/item_set_transitions.cc | 20 +++++++++----- .../build_tables/item_set_transitions.h | 7 +++-- src/compiler/build_tables/rule_transitions.cc | 19 +++++++++---- src/compiler/build_tables/rule_transitions.h | 7 +++-- src/compiler/generate_code/c_code.cc | 27 +++++++++++++------ src/compiler/parse_table.cc | 5 +++- src/compiler/parse_table.h | 5 +++- src/compiler/rules/repeat.cc | 2 +- src/compiler/rules/rule.cc | 2 +- src/compiler/rules/string.cc | 2 +- src/compiler/rules/symbol.cc | 2 +- 21 files changed, 117 insertions(+), 49 deletions(-) diff --git a/script/lint.sh b/script/lint.sh index aa727e62..5945153c 100755 --- a/script/lint.sh +++ b/script/lint.sh @@ -1,6 +1,9 @@ #!/usr/bin/env bash -filters=-whitespace,-readability/namespace,-legal/copyright cpplint=externals/cpplint.py -find src/compiler -type f | xargs $cpplint --root=src --filter=$filters 2>&1 +find src/compiler -type f | xargs $cpplint \ + --root=src \ + --linelength=110 \ + --filter=-readability/namespace,-legal/copyright \ + 2>&1 diff --git a/src/compiler/build_tables/build_tables.cc b/src/compiler/build_tables/build_tables.cc index aef2d74a..d60b5941 100644 --- a/src/compiler/build_tables/build_tables.cc +++ b/src/compiler/build_tables/build_tables.cc @@ -46,7 +46,10 @@ namespace tree_sitter { parse_table.add_action(state_id, symbol, ParseAction::Shift(new_state_id)); if (symbol == rules::ERROR) { - parse_table.error_table.insert({ state_id, { new_state_id, first_set(transition.second, grammar) } }); + parse_table.error_table.insert({ + state_id, + { new_state_id, first_set(transition.second, grammar) } + }); } } } @@ -116,10 +119,14 @@ namespace tree_sitter { void add_error_lex_state() { LexItemSet error_item_set; - for (auto &pair : lex_grammar.rules) - error_item_set.insert(LexItem(Symbol(pair.first), pair.second)); - for (auto &pair : lex_grammar.aux_rules) - error_item_set.insert(LexItem(Symbol(pair.first, rules::SymbolTypeAuxiliary), pair.second)); + for (auto &pair : lex_grammar.rules) { + LexItem item(Symbol(pair.first, rules::SymbolTypeNormal), pair.second); + error_item_set.insert(item); + } + for (auto &pair : lex_grammar.aux_rules) { + LexItem item(Symbol(pair.first, rules::SymbolTypeAuxiliary), pair.second); + error_item_set.insert(item); + } add_advance_actions(error_item_set, LexTable::ERROR_STATE_ID); add_accept_token_actions(error_item_set, LexTable::ERROR_STATE_ID); } @@ -146,7 +153,8 @@ namespace tree_sitter { lex_grammar(lex_grammar) {} pair build() { - ParseItem item(rules::START, make_shared(grammar.start_rule_name), {}, rules::END_OF_INPUT); + auto start_symbol = make_shared(grammar.start_rule_name); + ParseItem item(rules::START, start_symbol, {}, rules::END_OF_INPUT); ParseItemSet item_set = item_set_closure(ParseItemSet({ item }), grammar); add_parse_state(item_set); add_error_lex_state(); @@ -154,7 +162,8 @@ namespace tree_sitter { } }; - pair build_tables(const PreparedGrammar &grammar, const PreparedGrammar &lex_grammar) { + pair build_tables(const PreparedGrammar &grammar, + const PreparedGrammar &lex_grammar) { return TableBuilder(grammar, lex_grammar).build(); } } diff --git a/src/compiler/build_tables/build_tables.h b/src/compiler/build_tables/build_tables.h index 2932d21e..500a285d 100644 --- a/src/compiler/build_tables/build_tables.h +++ b/src/compiler/build_tables/build_tables.h @@ -9,7 +9,8 @@ namespace tree_sitter { class PreparedGrammar; namespace build_tables { - std::pair build_tables(const PreparedGrammar &grammar, const PreparedGrammar &lex_grammar); + std::pair build_tables(const PreparedGrammar &grammar, + const PreparedGrammar &lex_grammar); } } diff --git a/src/compiler/build_tables/first_set.h b/src/compiler/build_tables/first_set.h index 697688ec..346ef821 100644 --- a/src/compiler/build_tables/first_set.h +++ b/src/compiler/build_tables/first_set.h @@ -15,13 +15,15 @@ namespace tree_sitter { * the beginning of a string derivable from a given rule, * in a given gramamr. */ - std::set first_set(const rules::rule_ptr &rule, const PreparedGrammar &grammar); + std::set + first_set(const rules::rule_ptr &rule, const PreparedGrammar &grammar); /* * Returns the set of terminal symbols that can appear at * the beginning of any item in the given set. */ - std::set first_set(const ParseItemSet &item_set, const PreparedGrammar &grammar); + std::set + first_set(const ParseItemSet &item_set, const PreparedGrammar &grammar); } } diff --git a/src/compiler/build_tables/follow_sets.cc b/src/compiler/build_tables/follow_sets.cc index 91e2359b..77aa03c2 100644 --- a/src/compiler/build_tables/follow_sets.cc +++ b/src/compiler/build_tables/follow_sets.cc @@ -11,7 +11,8 @@ namespace tree_sitter { using rules::rule_ptr; namespace build_tables { - map> follow_sets(const ParseItem &item, const PreparedGrammar &grammar) { + map> follow_sets(const ParseItem &item, + const PreparedGrammar &grammar) { map> result; for (auto &pair : sym_transitions(item.rule)) { diff --git a/src/compiler/build_tables/follow_sets.h b/src/compiler/build_tables/follow_sets.h index 721cbb4f..53dfb516 100644 --- a/src/compiler/build_tables/follow_sets.h +++ b/src/compiler/build_tables/follow_sets.h @@ -17,7 +17,8 @@ namespace tree_sitter { * item. The values are the sets of terminals which can appear immediately * after the corresponding non-terminals. */ - std::map> follow_sets(const ParseItem &item, const PreparedGrammar &grammar); + std::map> + follow_sets(const ParseItem &item, const PreparedGrammar &grammar); } } diff --git a/src/compiler/build_tables/item.cc b/src/compiler/build_tables/item.cc index a8743803..bff744a7 100644 --- a/src/compiler/build_tables/item.cc +++ b/src/compiler/build_tables/item.cc @@ -67,7 +67,10 @@ namespace tree_sitter { return lhs_eq && rules_eq; } - ParseItem::ParseItem(const Symbol &lhs, const rule_ptr rule, const vector &consumed_symbols, const Symbol &lookahead_sym) : + ParseItem::ParseItem(const Symbol &lhs, + const rule_ptr rule, + const vector &consumed_symbols, + const Symbol &lookahead_sym) : Item(lhs, rule), consumed_symbols(consumed_symbols), lookahead_sym(lookahead_sym) {} diff --git a/src/compiler/build_tables/item.h b/src/compiler/build_tables/item.h index bb54f4ee..f84f53c6 100644 --- a/src/compiler/build_tables/item.h +++ b/src/compiler/build_tables/item.h @@ -28,7 +28,10 @@ namespace tree_sitter { class ParseItem : public Item { public: - ParseItem(const rules::Symbol &lhs, const rules::rule_ptr rule, const std::vector &consumed_symbols, const rules::Symbol &lookahead_sym); + ParseItem(const rules::Symbol &lhs, + const rules::rule_ptr rule, + const std::vector &consumed_symbols, + const rules::Symbol &lookahead_sym); bool operator<(const ParseItem &other) const; bool operator==(const ParseItem &other) const; diff --git a/src/compiler/build_tables/item_set_closure.cc b/src/compiler/build_tables/item_set_closure.cc index 9710f384..ff43fde5 100644 --- a/src/compiler/build_tables/item_set_closure.cc +++ b/src/compiler/build_tables/item_set_closure.cc @@ -12,10 +12,13 @@ namespace tree_sitter { namespace build_tables { static bool contains(const ParseItemSet *items, const ParseItem &item) { - return items->size() > 0 && (std::find(items->begin(), items->end(), item) != items->end()); + if (items->empty()) return false; + return (std::find(items->begin(), items->end(), item) != items->end()); } - static void add_item(ParseItemSet *item_set, const ParseItem &item, const PreparedGrammar &grammar) { + static void add_item(ParseItemSet *item_set, + const ParseItem &item, + const PreparedGrammar &grammar) { if (!contains(item_set, item)) { item_set->insert(item); for (auto &pair : follow_sets(item, grammar)) { @@ -29,7 +32,8 @@ namespace tree_sitter { } } - const ParseItemSet item_set_closure(const ParseItemSet &item_set, const PreparedGrammar &grammar) { + const ParseItemSet item_set_closure(const ParseItemSet &item_set, + const PreparedGrammar &grammar) { ParseItemSet result; for (ParseItem item : item_set) add_item(&result, item, grammar); diff --git a/src/compiler/build_tables/item_set_closure.h b/src/compiler/build_tables/item_set_closure.h index ef03cb31..d55e4727 100644 --- a/src/compiler/build_tables/item_set_closure.h +++ b/src/compiler/build_tables/item_set_closure.h @@ -7,7 +7,8 @@ namespace tree_sitter { class PreparedGrammar; namespace build_tables { - const ParseItemSet item_set_closure(const ParseItemSet &item_set, const PreparedGrammar &grammar); + const ParseItemSet item_set_closure(const ParseItemSet &item_set, + const PreparedGrammar &grammar); } } diff --git a/src/compiler/build_tables/item_set_transitions.cc b/src/compiler/build_tables/item_set_transitions.cc index af37ba26..c443bd6b 100644 --- a/src/compiler/build_tables/item_set_transitions.cc +++ b/src/compiler/build_tables/item_set_transitions.cc @@ -11,7 +11,8 @@ namespace tree_sitter { using rules::Symbol; namespace build_tables { - map char_transitions(const LexItem &item) { + map + char_transitions(const LexItem &item) { map result; for (auto &transition : char_transitions(item.rule)) { LexItem next_item(item.lhs, transition.second); @@ -20,7 +21,8 @@ namespace tree_sitter { return result; } - map sym_transitions(const ParseItem &item, const PreparedGrammar &grammar) { + map + sym_transitions(const ParseItem &item, const PreparedGrammar &grammar) { map result; for (auto transition : sym_transitions(item.rule)) { Symbol rule = transition.first; @@ -39,22 +41,28 @@ namespace tree_sitter { return result; } - map char_transitions(const LexItemSet &item_set, const PreparedGrammar &grammar) { + map + char_transitions(const LexItemSet &item_set, const PreparedGrammar &grammar) { map result; for (const LexItem &item : item_set) { map item_transitions = char_transitions(item); - result = merge_char_transitions(result, item_transitions, [](LexItemSet left, LexItemSet right) { + result = merge_char_transitions(result, + item_transitions, + [](LexItemSet left, LexItemSet right) { return merge_sets(left, right); }); } return result; } - map sym_transitions(const ParseItemSet &item_set, const PreparedGrammar &grammar) { + map + sym_transitions(const ParseItemSet &item_set, const PreparedGrammar &grammar) { map result; for (const ParseItem &item : item_set) { map item_transitions = sym_transitions(item, grammar); - result = merge_sym_transitions(result, item_transitions, [&](ParseItemSet left, ParseItemSet right) { + result = merge_sym_transitions(result, + item_transitions, + [&](ParseItemSet left, ParseItemSet right) { return merge_sets(left, right); }); } diff --git a/src/compiler/build_tables/item_set_transitions.h b/src/compiler/build_tables/item_set_transitions.h index 976fba07..40e23009 100644 --- a/src/compiler/build_tables/item_set_transitions.h +++ b/src/compiler/build_tables/item_set_transitions.h @@ -12,8 +12,11 @@ namespace tree_sitter { } namespace build_tables { - std::map char_transitions(const LexItemSet &item_set, const PreparedGrammar &grammar); - std::map sym_transitions(const ParseItemSet &item_set, const PreparedGrammar &grammar); + std::map + char_transitions(const LexItemSet &item_set, const PreparedGrammar &grammar); + + std::map + sym_transitions(const ParseItemSet &item_set, const PreparedGrammar &grammar); } } diff --git a/src/compiler/build_tables/rule_transitions.cc b/src/compiler/build_tables/rule_transitions.cc index 8a296af7..1e28b85f 100644 --- a/src/compiler/build_tables/rule_transitions.cc +++ b/src/compiler/build_tables/rule_transitions.cc @@ -24,7 +24,8 @@ namespace tree_sitter { map merge_transitions(const map &left, const map &right); template<> - map merge_transitions(const map &left, const map &right) { + map + merge_transitions(const map &left, const map &right) { auto transitions = merge_char_transitions(left, right, [](rule_ptr left, rule_ptr right) { return make_shared(left, right); }); @@ -32,7 +33,8 @@ namespace tree_sitter { } template<> - map merge_transitions(const map &left, const map &right) { + map + merge_transitions(const map &left, const map &right) { auto transitions = merge_sym_transitions(left, right, [](rule_ptr left, rule_ptr right) { return make_shared(left, right); }); @@ -40,7 +42,8 @@ namespace tree_sitter { } template - map map_transitions(const map &initial, std::function map_fn) { + map + map_transitions(const map &initial, std::function map_fn) { map result; for (auto &pair : initial) result.insert({ pair.first, map_fn(pair.second) }); @@ -79,14 +82,20 @@ namespace tree_sitter { void visit(const rules::Repeat *rule) { this->value = map_transitions(this->apply(rule->content), [&](const rule_ptr &value) { - return rules::Seq::Build({ value, make_shared(rule->copy(), make_shared()) }); + return rules::Seq::Build({ + value, + make_shared(rule->copy(), make_shared()) + }); }); } void visit(const rules::String *rule) { rule_ptr result = make_shared(); for (char val : rule->value) - result = rules::Seq::Build({ result, make_shared(set({ val })) }); + result = rules::Seq::Build({ + result, + make_shared(set({ val })) + }); this->value = this->apply(result); } diff --git a/src/compiler/build_tables/rule_transitions.h b/src/compiler/build_tables/rule_transitions.h index a461a2eb..29e43fdc 100644 --- a/src/compiler/build_tables/rule_transitions.h +++ b/src/compiler/build_tables/rule_transitions.h @@ -7,8 +7,11 @@ namespace tree_sitter { namespace build_tables { - std::map char_transitions(const rules::rule_ptr &rule); - std::map sym_transitions(const rules::rule_ptr &rule); + std::map + char_transitions(const rules::rule_ptr &rule); + + std::map + sym_transitions(const rules::rule_ptr &rule); } } diff --git a/src/compiler/generate_code/c_code.cc b/src/compiler/generate_code/c_code.cc index b200e2a3..9230629a 100644 --- a/src/compiler/generate_code/c_code.cc +++ b/src/compiler/generate_code/c_code.cc @@ -123,7 +123,8 @@ namespace tree_sitter { return result; } - string code_for_parse_actions(const set &actions, const set &expected_inputs) { + string code_for_parse_actions(const set &actions, + const set &expected_inputs) { auto action = actions.begin(); switch (action->type) { case ParseActionTypeAccept: @@ -131,7 +132,10 @@ namespace tree_sitter { case ParseActionTypeShift: return "SHIFT(" + to_string(action->state_index) + ");"; case ParseActionTypeReduce: - return "REDUCE(" + symbol_id(action->symbol) + ", " + to_string(action->child_flags.size()) + ", COLLAPSE({" + collapse_flags(action->child_flags) + "}));"; + return "REDUCE(" + + symbol_id(action->symbol) + ", " + + to_string(action->child_flags.size()) + ", " + + "COLLAPSE({" + collapse_flags(action->child_flags) + "}));"; default: return ""; } @@ -149,7 +153,8 @@ namespace tree_sitter { return result; } - string code_for_lex_actions(const set &actions, const set &expected_inputs) { + string code_for_lex_actions(const set &actions, + const set &expected_inputs) { auto action = actions.begin(); if (action == actions.end()) { return "LEX_ERROR();"; @@ -169,7 +174,8 @@ namespace tree_sitter { string body = ""; auto expected_inputs = parse_state.expected_inputs(); for (auto pair : parse_state.actions) - body += _case(symbol_id(pair.first), code_for_parse_actions(pair.second, expected_inputs)); + body += _case(symbol_id(pair.first), + code_for_parse_actions(pair.second, expected_inputs)); body += _default(parse_error_call(expected_inputs)); return string("SET_LEX_STATE(") + to_string(parse_state.lex_state_id) + ");\n" + @@ -180,7 +186,8 @@ namespace tree_sitter { string result = ""; auto expected_inputs = parse_state.expected_inputs(); for (auto pair : parse_state.actions) - result += _if(condition_for_character_rule(pair.first), code_for_lex_actions(pair.second, expected_inputs)); + result += _if(condition_for_character_rule(pair.first), + code_for_lex_actions(pair.second, expected_inputs)); result += code_for_lex_actions(parse_state.default_actions, expected_inputs); return result; } @@ -223,7 +230,9 @@ namespace tree_sitter { } string recover_case(ParseStateId state, set symbols) { - string result = "RECOVER(" + to_string(state) + ", " + to_string(symbols.size()) + ", EXPECT({"; + string result = "RECOVER(" + + to_string(state) + ", " + + to_string(symbols.size()) + ", EXPECT({"; bool started = false; for (auto &symbol : symbols) { if (started) { @@ -239,13 +248,15 @@ namespace tree_sitter { string cases; for (auto &pair : parse_table.error_table) { auto pair_for_state = pair.second; - cases += _case(to_string(pair.first), recover_case(pair_for_state.first, pair_for_state.second)); + cases += _case(to_string(pair.first), + recover_case(pair_for_state.first, pair_for_state.second)); } cases += _default(recover_case(0, set())); string body = _switch("state", cases); return join({ - "static const ts_symbol * ts_recover(ts_state state, ts_state *to_state, size_t *count) {", + "static const ts_symbol * " + "ts_recover(ts_state state, ts_state *to_state, size_t *count) {", indent(body), "}" }); diff --git a/src/compiler/parse_table.cc b/src/compiler/parse_table.cc index fe3821d8..7f1b9f2d 100644 --- a/src/compiler/parse_table.cc +++ b/src/compiler/parse_table.cc @@ -9,7 +9,10 @@ namespace tree_sitter { using std::vector; using rules::Symbol; - ParseAction::ParseAction(ParseActionType type, size_t state_index, Symbol symbol, const vector &child_flags) : + ParseAction::ParseAction(ParseActionType type, + size_t state_index, + Symbol symbol, + const vector &child_flags) : type(type), symbol(symbol), state_index(state_index), diff --git a/src/compiler/parse_table.h b/src/compiler/parse_table.h index 2c5f6911..ec98aada 100644 --- a/src/compiler/parse_table.h +++ b/src/compiler/parse_table.h @@ -17,7 +17,10 @@ namespace tree_sitter { } ParseActionType; class ParseAction { - ParseAction(ParseActionType type, size_t state_index, rules::Symbol symbol, const std::vector &child_flags); + ParseAction(ParseActionType type, + size_t state_index, + rules::Symbol symbol, + const std::vector &child_flags); public: static ParseAction Accept(); static ParseAction Error(); diff --git a/src/compiler/rules/repeat.cc b/src/compiler/rules/repeat.cc index c2284ed0..dd4e3b6e 100644 --- a/src/compiler/rules/repeat.cc +++ b/src/compiler/rules/repeat.cc @@ -4,7 +4,7 @@ namespace tree_sitter { using std::string; - + namespace rules { Repeat::Repeat(const rule_ptr content) : content(content) {} diff --git a/src/compiler/rules/rule.cc b/src/compiler/rules/rule.cc index a5c727d5..14daa6b5 100644 --- a/src/compiler/rules/rule.cc +++ b/src/compiler/rules/rule.cc @@ -4,7 +4,7 @@ namespace tree_sitter { using std::ostream; using std::string; - + namespace rules { bool Rule::operator!=(const Rule &other) const { return !this->operator==(other); diff --git a/src/compiler/rules/string.cc b/src/compiler/rules/string.cc index 69da1e5a..6b8326dc 100644 --- a/src/compiler/rules/string.cc +++ b/src/compiler/rules/string.cc @@ -5,7 +5,7 @@ namespace tree_sitter { using std::string; using std::hash; - + namespace rules { String::String(string value) : value(value) {} diff --git a/src/compiler/rules/symbol.cc b/src/compiler/rules/symbol.cc index d3373dd0..922e3c34 100644 --- a/src/compiler/rules/symbol.cc +++ b/src/compiler/rules/symbol.cc @@ -6,7 +6,7 @@ namespace tree_sitter { using std::string; using std::hash; - + namespace rules { Symbol::Symbol(const std::string &name) : name(name), type(SymbolTypeNormal) {} Symbol::Symbol(const std::string &name, SymbolType type) : name(name), type(type) {}