From f9b057f3a9889f36680c42a0e089356a50eb66aa Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 27 Jul 2015 18:29:48 -0700 Subject: [PATCH] clang-format everything --- .clang-format | 57 ++++++++---- include/tree_sitter/compiler.h | 5 +- include/tree_sitter/parser.h | 85 ++++++++--------- include/tree_sitter/runtime.h | 2 +- src/compiler/build_tables/build_lex_table.cc | 28 +++--- .../build_tables/build_parse_table.cc | 81 ++++++++++------- src/compiler/build_tables/build_parse_table.h | 4 +- src/compiler/build_tables/build_tables.cc | 4 +- src/compiler/build_tables/build_tables.h | 4 +- .../build_tables/item_set_transitions.cc | 22 ++--- .../build_tables/item_set_transitions.h | 4 +- .../build_tables/lex_conflict_manager.cc | 7 +- .../build_tables/parse_conflict_manager.cc | 33 ++++--- .../build_tables/parse_conflict_manager.h | 8 +- src/compiler/build_tables/parse_item.cc | 12 ++- src/compiler/build_tables/rule_transitions.h | 4 +- src/compiler/compile.cc | 12 +-- src/compiler/generate_code/c_code.cc | 79 ++++++++-------- src/compiler/lex_table.cc | 4 +- src/compiler/parse_table.cc | 36 +++++--- src/compiler/parse_table.h | 7 +- .../prepare_grammar/expand_repeats.cc | 12 ++- src/compiler/prepare_grammar/expand_tokens.cc | 8 +- src/compiler/prepare_grammar/expand_tokens.h | 2 +- .../prepare_grammar/extract_tokens.cc | 26 +++--- src/compiler/prepare_grammar/extract_tokens.h | 2 +- .../prepare_grammar/intern_symbols.cc | 6 +- .../prepare_grammar/interned_grammar.h | 1 - src/compiler/prepare_grammar/parse_regex.cc | 20 ++-- .../prepare_grammar/prepare_grammar.cc | 2 +- .../prepare_grammar/prepare_grammar.h | 2 +- src/compiler/rules/character_set.cc | 4 +- src/compiler/rules/rules.cc | 14 +-- src/compiler/rules/symbol.cc | 2 +- src/runtime/document.c | 4 +- src/runtime/lexer.c | 42 ++++----- src/runtime/node.c | 14 +-- src/runtime/parse_stack.c | 87 +++++++++--------- src/runtime/parse_stack.h | 5 +- src/runtime/parser.c | 91 +++++++++++-------- src/runtime/stack.c | 8 +- src/runtime/string_input.c | 8 +- src/runtime/tree.c | 39 ++++---- src/runtime/tree_vector.h | 15 +-- 44 files changed, 496 insertions(+), 416 deletions(-) diff --git a/.clang-format b/.clang-format index 908f7495..8d2eba22 100644 --- a/.clang-format +++ b/.clang-format @@ -1,46 +1,65 @@ --- -# BasedOnStyle: Google +Language: Cpp AccessModifierOffset: -1 -ConstructorInitializerIndentWidth: 4 +AlignAfterOpenBracket: true +AlignConsecutiveAssignments: false AlignEscapedNewlinesLeft: true +AlignOperands: true AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All AllowShortIfStatementsOnASingleLine: false AllowShortLoopsOnASingleLine: false -AlwaysBreakTemplateDeclarations: true +AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakBeforeMultilineStrings: true -BreakBeforeBinaryOperators: false +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: false -BinPackParameters: true ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' ConstructorInitializerAllOnOneLineOrOnePerLine: true -DerivePointerBinding: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 2 +Cpp11BracedListStyle: false +DerivePointerAlignment: true +DisableFormat: false ExperimentalAutoDetectBinPacking: false +ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] IndentCaseLabels: true +IndentWidth: 2 +IndentWrappedFunctionNames: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' MaxEmptyLinesToKeep: 1 NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false ObjCSpaceBeforeProtocolList: false PenaltyBreakBeforeFirstCallParameter: 1 PenaltyBreakComment: 60 -PenaltyBreakString: 1000 PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 PenaltyExcessCharacter: 20 PenaltyReturnTypeOnItsOwnLine: 200 -PointerBindsToType: true +PointerAlignment: Left +SpaceAfterCStyleCast: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 2 -Cpp11BracedListStyle: false +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false Standard: Auto -IndentWidth: 2 TabWidth: 8 UseTab: Never -BreakBeforeBraces: Attach -IndentFunctionDeclarationAfterType: true -SpacesInParentheses: false -SpacesInAngles: false -SpaceInEmptyParentheses: false -SpacesInCStyleCastParentheses: false -SpaceAfterControlStatementKeyword: true -SpaceBeforeAssignmentOperators: true -ContinuationIndentWidth: 2 ... diff --git a/include/tree_sitter/compiler.h b/include/tree_sitter/compiler.h index 0a9b2245..f2aacfbe 100644 --- a/include/tree_sitter/compiler.h +++ b/include/tree_sitter/compiler.h @@ -16,7 +16,7 @@ typedef std::shared_ptr rule_ptr; enum Associativity { AssociativityLeft = 1, - AssociativityRight + AssociativityRight, }; rule_ptr blank(); @@ -68,7 +68,8 @@ class GrammarError { std::string message; }; -std::pair compile(const Grammar &, std::string); +std::pair compile(const Grammar &, + std::string); std::ostream &operator<<(std::ostream &stream, const Grammar &grammar); std::ostream &operator<<(std::ostream &stream, const GrammarError *error); diff --git a/include/tree_sitter/parser.h b/include/tree_sitter/parser.h index 3cae89af..7e4d5141 100644 --- a/include/tree_sitter/parser.h +++ b/include/tree_sitter/parser.h @@ -73,19 +73,19 @@ struct TSLanguage { * Lexer Macros */ -#define START_LEXER() \ +#define START_LEXER() \ const bool error_mode = (lex_state == ts_lex_state_error); \ - lexer->start_fn(lexer, lex_state); \ - int32_t lookahead; \ - next_state: \ + lexer->start_fn(lexer, lex_state); \ + int32_t lookahead; \ + next_state: \ lookahead = lexer->lookahead; #define START_TOKEN() lexer->start_token_fn(lexer); -#define GO_TO_STATE(state_index) \ - { \ - lex_state = state_index; \ - goto next_state; \ +#define GO_TO_STATE(state_index) \ + { \ + lex_state = state_index; \ + goto next_state; \ } #define ADVANCE(state_index) \ @@ -98,60 +98,63 @@ struct TSLanguage { return lexer->accept_fn(lexer, symbol, ts_hidden_symbol_flags[symbol], \ ts_symbol_names[symbol]); -#define LEX_ERROR() \ - if (error_mode) { \ - if (lex_state == ts_lex_state_error) \ - ADVANCE(ts_lex_state_error) \ - else \ - GO_TO_STATE(ts_lex_state_error) \ - } else { \ - ACCEPT_TOKEN(ts_builtin_sym_error) \ +#define LEX_ERROR() \ + if (error_mode) { \ + if (lex_state == ts_lex_state_error) \ + ADVANCE(ts_lex_state_error) \ + else \ + GO_TO_STATE(ts_lex_state_error) \ + } else { \ + ACCEPT_TOKEN(ts_builtin_sym_error) \ } /* * Parse Table Macros */ -#define ACTIONS(...) \ - (TSParseAction[]) {__VA_ARGS__, {.type = 0}} +#define ACTIONS(...) \ + (TSParseAction[]) { \ + __VA_ARGS__, { .type = 0 } \ + } -#define SHIFT(to_state_value) \ - { \ - .type = TSParseActionTypeShift, .data = { .to_state = to_state_value } \ +#define SHIFT(to_state_value) \ + { \ + .type = TSParseActionTypeShift, .data = {.to_state = to_state_value } \ } #define SHIFT_EXTRA() \ { .type = TSParseActionTypeShiftExtra } -#define REDUCE_EXTRA(symbol_val) \ - { \ - .type = TSParseActionTypeReduceExtra, .data = { .symbol = symbol_val } \ +#define REDUCE_EXTRA(symbol_val) \ + { \ + .type = TSParseActionTypeReduceExtra, .data = {.symbol = symbol_val } \ } -#define REDUCE(symbol_val, child_count_val) \ - { \ - .type = TSParseActionTypeReduce, \ - .data = { .symbol = symbol_val, .child_count = child_count_val } \ +#define REDUCE(symbol_val, child_count_val) \ + { \ + .type = TSParseActionTypeReduce, \ + .data = {.symbol = symbol_val, .child_count = child_count_val } \ } -#define REDUCE_FRAGILE(symbol_val, child_count_val) \ - { \ - .type = TSParseActionTypeReduceFragile, \ - .data = { .symbol = symbol_val, .child_count = child_count_val } \ +#define REDUCE_FRAGILE(symbol_val, child_count_val) \ + { \ + .type = TSParseActionTypeReduceFragile, \ + .data = {.symbol = symbol_val, .child_count = child_count_val } \ } #define ACCEPT_INPUT() \ { .type = TSParseActionTypeAccept } -#define EXPORT_LANGUAGE(language_name) \ - static TSLanguage language = { .symbol_count = SYMBOL_COUNT, \ - .hidden_symbol_flags = ts_hidden_symbol_flags, \ - .parse_table = \ - (const TSParseAction **)ts_parse_actions, \ - .lex_states = ts_lex_states, \ - .symbol_names = ts_symbol_names, \ - .lex_fn = ts_lex, }; \ - \ +#define EXPORT_LANGUAGE(language_name) \ + static TSLanguage language = { \ + .symbol_count = SYMBOL_COUNT, \ + .hidden_symbol_flags = ts_hidden_symbol_flags, \ + .parse_table = (const TSParseAction **)ts_parse_actions, \ + .lex_states = ts_lex_states, \ + .symbol_names = ts_symbol_names, \ + .lex_fn = ts_lex, \ + }; \ + \ const TSLanguage *language_name() { return &language; } #ifdef __cplusplus diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 7028d955..bef414d2 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -22,7 +22,7 @@ typedef struct { typedef enum { TSDebugTypeParse, - TSDebugTypeLex + TSDebugTypeLex, } TSDebugType; typedef struct { diff --git a/src/compiler/build_tables/build_lex_table.cc b/src/compiler/build_tables/build_lex_table.cc index 64b280b6..7b20b627 100644 --- a/src/compiler/build_tables/build_lex_table.cc +++ b/src/compiler/build_tables/build_lex_table.cc @@ -37,7 +37,9 @@ class LexTableBuilder { public: LexTableBuilder(ParseTable *parse_table, const LexicalGrammar &lex_grammar) - : lex_grammar(lex_grammar), conflict_manager(lex_grammar), parse_table(parse_table) {} + : lex_grammar(lex_grammar), + conflict_manager(lex_grammar), + parse_table(parse_table) {} LexTable build() { for (auto &parse_state : parse_table->states) { @@ -56,12 +58,12 @@ class LexTableBuilder { continue; if (symbol == rules::END_OF_INPUT()) - result.insert(LexItem( - symbol, after_separators(CharacterSet().include(0).copy()))); + result.insert( + LexItem(symbol, after_separators(CharacterSet().include(0).copy()))); else if (symbol.is_token()) result.insert( - LexItem(symbol, after_separators(lex_grammar.rule(symbol)))); + LexItem(symbol, after_separators(lex_grammar.rule(symbol)))); } return result; } @@ -96,8 +98,9 @@ class LexTableBuilder { LexItemSet new_item_set = transition.second; LexStateId new_state_id = add_lex_state(new_item_set); auto action = LexAction::Advance( - new_state_id, precedence_values_for_item_set(new_item_set)); - if (conflict_manager.resolve(action, lex_table.state(state_id).default_action)) + new_state_id, precedence_values_for_item_set(new_item_set)); + if (conflict_manager.resolve(action, + lex_table.state(state_id).default_action)) lex_table.state(state_id).actions[rule] = action; } } @@ -119,12 +122,13 @@ class LexTableBuilder { } rules::rule_ptr after_separators(rules::rule_ptr rule) { - return rules::Seq::build( - { make_shared( - separator_rule(), - map( - { { rules::START_TOKEN, 1 }, { rules::PRECEDENCE, -1 }, })), - rule, }); + return rules::Seq::build({ + make_shared( + separator_rule(), map({ + { rules::START_TOKEN, 1 }, { rules::PRECEDENCE, -1 }, + })), + rule, + }); } rules::rule_ptr separator_rule() const { diff --git a/src/compiler/build_tables/build_parse_table.cc b/src/compiler/build_tables/build_parse_table.cc index 935021bf..21e8f332 100644 --- a/src/compiler/build_tables/build_parse_table.cc +++ b/src/compiler/build_tables/build_parse_table.cc @@ -41,15 +41,17 @@ class ParseTableBuilder { public: ParseTableBuilder(const SyntaxGrammar &grammar, const LexicalGrammar &lex_grammar) - : grammar(grammar), lexical_grammar(lex_grammar), conflict_manager(grammar) {} + : grammar(grammar), + lexical_grammar(lex_grammar), + conflict_manager(grammar) {} pair build() { auto start_symbol = grammar.rules.empty() - ? make_shared(0, rules::SymbolOptionToken) - : make_shared(0); + ? make_shared(0, rules::SymbolOptionToken) + : make_shared(0); ParseItem start_item(rules::START(), start_symbol, {}); add_parse_state( - item_set_closure(start_item, { rules::END_OF_INPUT() }, grammar)); + item_set_closure(start_item, { rules::END_OF_INPUT() }, grammar)); while (!item_sets_to_process.empty()) { auto pair = item_sets_to_process.back(); @@ -62,10 +64,9 @@ class ParseTableBuilder { add_shift_extra_actions(item_set, state_id); if (!conflicts.empty()) - return { - parse_table, - new GrammarError(GrammarErrorTypeParseConflict, "Unresolved conflict.\n\n" + *conflicts.begin()) - }; + return { parse_table, new GrammarError(GrammarErrorTypeParseConflict, + "Unresolved conflict.\n\n" + + *conflicts.begin()) }; } for (ParseStateId state = 0; state < parse_table.states.size(); state++) @@ -95,9 +96,10 @@ class ParseTableBuilder { const Symbol &symbol = transition.first; const ParseItemSet &next_item_set = transition.second; - ParseAction *new_action = add_action(state_id, symbol, - ParseAction::Shift(0, precedence_values_for_item_set(next_item_set)), - item_set); + ParseAction *new_action = add_action( + state_id, symbol, + ParseAction::Shift(0, precedence_values_for_item_set(next_item_set)), + item_set); if (new_action) new_action->state_index = add_parse_state(next_item_set); } @@ -110,11 +112,11 @@ class ParseTableBuilder { if (item.is_done()) { ParseAction action = - (item.lhs == rules::START()) - ? ParseAction::Accept() - : ParseAction::Reduce(item.lhs, item.consumed_symbols.size(), - item.precedence(), item.associativity(), - get_production_id(item.consumed_symbols)); + (item.lhs == rules::START()) + ? ParseAction::Accept() + : ParseAction::Reduce(item.lhs, item.consumed_symbols.size(), + item.precedence(), item.associativity(), + get_production_id(item.consumed_symbols)); for (const auto &lookahead_sym : lookahead_symbols) add_action(state_id, lookahead_sym, action, item_set); @@ -122,15 +124,17 @@ class ParseTableBuilder { } } - void add_shift_extra_actions(const ParseItemSet &item_set, ParseStateId state_id) { + void add_shift_extra_actions(const ParseItemSet &item_set, + ParseStateId state_id) { for (const Symbol &ubiquitous_symbol : grammar.ubiquitous_tokens) - add_action(state_id, ubiquitous_symbol, ParseAction::ShiftExtra(), item_set); + add_action(state_id, ubiquitous_symbol, ParseAction::ShiftExtra(), + item_set); } void add_reduce_extra_actions(ParseStateId state_id) { const ParseItemSet item_set; const map> &actions = - parse_table.states[state_id].actions; + parse_table.states[state_id].actions; for (const Symbol &ubiquitous_symbol : grammar.ubiquitous_tokens) { const auto &entry = actions.find(ubiquitous_symbol); @@ -142,7 +146,8 @@ class ParseTableBuilder { size_t shift_state_id = action.state_index; for (const auto &pair : actions) { const Symbol &lookahead_sym = pair.first; - ParseAction reduce_extra = ParseAction::ReduceExtra(ubiquitous_symbol); + ParseAction reduce_extra = + ParseAction::ReduceExtra(ubiquitous_symbol); add_action(shift_state_id, lookahead_sym, reduce_extra, item_set); } } @@ -151,14 +156,16 @@ class ParseTableBuilder { } ParseAction *add_action(ParseStateId state_id, Symbol lookahead_sym, - const ParseAction &action, const ParseItemSet &item_set) { + const ParseAction &action, + const ParseItemSet &item_set) { auto ¤t_actions = parse_table.states[state_id].actions; auto current_entry = current_actions.find(lookahead_sym); if (current_entry == current_actions.end()) return &parse_table.set_action(state_id, lookahead_sym, action); const ParseAction current_action = current_entry->second[0]; - auto resolution = conflict_manager.resolve(action, current_action, lookahead_sym); + auto resolution = + conflict_manager.resolve(action, current_action, lookahead_sym); switch (resolution.second) { case ConflictTypeNone: @@ -180,7 +187,8 @@ class ParseTableBuilder { if (has_expected_conflict(goal_symbols)) return &parse_table.add_action(state_id, lookahead_sym, action); else - conflicts.insert(conflict_description(action, current_action, lookahead_sym, goal_symbols)); + conflicts.insert(conflict_description(action, current_action, + lookahead_sym, goal_symbols)); break; } } @@ -216,9 +224,9 @@ class ParseTableBuilder { } string conflict_description(const ParseAction &new_action, - const ParseAction &old_action, - const rules::Symbol &symbol, - const set &goal_symbols) const { + const ParseAction &old_action, + const rules::Symbol &symbol, + const set &goal_symbols) const { string symbols_string; bool started = false; for (const auto &symbol : goal_symbols) { @@ -228,12 +236,14 @@ class ParseTableBuilder { started = true; } - return - "Within: " + symbols_string + "\n" - "Lookahead: " + symbol_name(symbol) + "\n" + - "Possible Actions:\n" - "* " + action_description(old_action) + "\n" + - "* " + action_description(new_action); + return "Within: " + symbols_string + + "\n" + "Lookahead: " + + symbol_name(symbol) + "\n" + + "Possible Actions:\n" + "* " + + action_description(old_action) + "\n" + "* " + + action_description(new_action); } string symbol_name(const rules::Symbol &symbol) const { @@ -273,9 +283,10 @@ class ParseTableBuilder { if (action.precedence_values.size() > 1) { result += " (Precedences " + to_string(*action.precedence_values.begin()) + - ", " + to_string(*action.precedence_values.rbegin()) + ")"; + ", " + to_string(*action.precedence_values.rbegin()) + ")"; } else { - result += " (Precedence " + to_string(*action.precedence_values.begin()) + ")"; + result += + " (Precedence " + to_string(*action.precedence_values.begin()) + ")"; } return result; @@ -294,7 +305,7 @@ class ParseTableBuilder { }; pair build_parse_table( - const SyntaxGrammar &grammar, const LexicalGrammar &lex_grammar) { + const SyntaxGrammar &grammar, const LexicalGrammar &lex_grammar) { return ParseTableBuilder(grammar, lex_grammar).build(); } diff --git a/src/compiler/build_tables/build_parse_table.h b/src/compiler/build_tables/build_parse_table.h index 919e4db9..a9e439af 100644 --- a/src/compiler/build_tables/build_parse_table.h +++ b/src/compiler/build_tables/build_parse_table.h @@ -13,8 +13,8 @@ class LexicalGrammar; namespace build_tables { -std::pair -build_parse_table(const SyntaxGrammar &, const LexicalGrammar &); +std::pair build_parse_table( + const SyntaxGrammar &, const LexicalGrammar &); } // namespace build_tables } // namespace tree_sitter diff --git a/src/compiler/build_tables/build_tables.cc b/src/compiler/build_tables/build_tables.cc index 3ec47be7..c8fba813 100644 --- a/src/compiler/build_tables/build_tables.cc +++ b/src/compiler/build_tables/build_tables.cc @@ -11,8 +11,8 @@ using std::tuple; using std::vector; using std::make_tuple; -tuple -build_tables(const SyntaxGrammar &grammar, const LexicalGrammar &lex_grammar) { +tuple build_tables( + const SyntaxGrammar &grammar, const LexicalGrammar &lex_grammar) { auto parse_table_result = build_parse_table(grammar, lex_grammar); ParseTable parse_table = parse_table_result.first; const GrammarError *error = parse_table_result.second; diff --git a/src/compiler/build_tables/build_tables.h b/src/compiler/build_tables/build_tables.h index c1e7ef3e..af796d71 100644 --- a/src/compiler/build_tables/build_tables.h +++ b/src/compiler/build_tables/build_tables.h @@ -15,8 +15,8 @@ class LexicalGrammar; namespace build_tables { -std::tuple -build_tables(const SyntaxGrammar &, const LexicalGrammar &); +std::tuple build_tables( + const SyntaxGrammar &, const LexicalGrammar &); } // namespace build_tables } // namespace tree_sitter diff --git a/src/compiler/build_tables/item_set_transitions.cc b/src/compiler/build_tables/item_set_transitions.cc index d4c5eefc..3cf4c83a 100644 --- a/src/compiler/build_tables/item_set_transitions.cc +++ b/src/compiler/build_tables/item_set_transitions.cc @@ -27,13 +27,13 @@ map sym_transitions(const ParseItemSet &item_set, consumed_symbols.push_back(transition.first); ParseItem new_item(item.lhs, transition.second, consumed_symbols); merge_sym_transition( - &result, { transition.first, - item_set_closure(new_item, lookahead_symbols, grammar) }, - [](ParseItemSet *left, const ParseItemSet *right) { - for (auto &pair : *right) - left->operator[](pair.first) - .insert(pair.second.begin(), pair.second.end()); - }); + &result, { transition.first, + item_set_closure(new_item, lookahead_symbols, grammar) }, + [](ParseItemSet *left, const ParseItemSet *right) { + for (auto &pair : *right) + left->operator[](pair.first) + .insert(pair.second.begin(), pair.second.end()); + }); } } return result; @@ -45,10 +45,10 @@ map char_transitions(const LexItemSet &item_set) { for (auto &transition : char_transitions(item.rule)) { LexItem next_item(item.lhs, transition.second); merge_char_transition( - &result, { transition.first, LexItemSet({ next_item }) }, - [](LexItemSet *left, const LexItemSet *right) { - left->insert(right->begin(), right->end()); - }); + &result, { transition.first, LexItemSet({ next_item }) }, + [](LexItemSet *left, const LexItemSet *right) { + left->insert(right->begin(), right->end()); + }); } } return result; diff --git a/src/compiler/build_tables/item_set_transitions.h b/src/compiler/build_tables/item_set_transitions.h index 221377a0..93ad04c9 100644 --- a/src/compiler/build_tables/item_set_transitions.h +++ b/src/compiler/build_tables/item_set_transitions.h @@ -17,10 +17,10 @@ class Symbol; namespace build_tables { std::map sym_transitions( - const ParseItemSet &item_set, const SyntaxGrammar &grammar); + const ParseItemSet &item_set, const SyntaxGrammar &grammar); std::map char_transitions( - const LexItemSet &item_set); + const LexItemSet &item_set); } // namespace build_tables } // namespace tree_sitter diff --git a/src/compiler/build_tables/lex_conflict_manager.cc b/src/compiler/build_tables/lex_conflict_manager.cc index b82951e8..30201d37 100644 --- a/src/compiler/build_tables/lex_conflict_manager.cc +++ b/src/compiler/build_tables/lex_conflict_manager.cc @@ -6,10 +6,11 @@ namespace tree_sitter { namespace build_tables { -LexConflictManager::LexConflictManager(const LexicalGrammar &grammar) : - grammar(grammar) {} +LexConflictManager::LexConflictManager(const LexicalGrammar &grammar) + : grammar(grammar) {} -bool LexConflictManager::resolve(const LexAction &new_action, const LexAction &old_action) const { +bool LexConflictManager::resolve(const LexAction &new_action, + const LexAction &old_action) const { if (new_action.type < old_action.type) return !resolve(old_action, new_action); diff --git a/src/compiler/build_tables/parse_conflict_manager.cc b/src/compiler/build_tables/parse_conflict_manager.cc index a24e9bf6..4380ae35 100644 --- a/src/compiler/build_tables/parse_conflict_manager.cc +++ b/src/compiler/build_tables/parse_conflict_manager.cc @@ -10,22 +10,21 @@ using std::pair; using std::vector; ParseConflictManager::ParseConflictManager(const SyntaxGrammar &syntax_grammar) - : syntax_grammar(syntax_grammar) {} + : syntax_grammar(syntax_grammar) {} -pair -ParseConflictManager::resolve(const ParseAction &new_action, - const ParseAction &old_action, - const rules::Symbol &symbol) const { +pair ParseConflictManager::resolve( + const ParseAction &new_action, const ParseAction &old_action, + const rules::Symbol &symbol) const { if (new_action.type < old_action.type) { auto opposite = resolve(old_action, new_action, symbol); - return {!opposite.first, opposite.second}; + return { !opposite.first, opposite.second }; } switch (old_action.type) { case ParseActionTypeError: case ParseActionTypeShiftExtra: case ParseActionTypeReduceExtra: - return {true, ConflictTypeNone}; + return { true, ConflictTypeNone }; case ParseActionTypeShift: if (new_action.type == ParseActionTypeReduce) { @@ -33,20 +32,20 @@ ParseConflictManager::resolve(const ParseAction &new_action, int max_precedence = *old_action.precedence_values.rbegin(); int new_precedence = *new_action.precedence_values.rbegin(); if (new_precedence < min_precedence) - return {false, ConflictTypeResolved}; + return { false, ConflictTypeResolved }; else if (new_precedence > max_precedence) - return {true, ConflictTypeResolved}; + return { true, ConflictTypeResolved }; else if (min_precedence == max_precedence) { switch (new_action.associativity) { case rules::AssociativityLeft: - return {true, ConflictTypeResolved}; + return { true, ConflictTypeResolved }; case rules::AssociativityRight: - return {false, ConflictTypeResolved}; + return { false, ConflictTypeResolved }; default: - return {false, ConflictTypeUnresolved}; + return { false, ConflictTypeUnresolved }; } } else { - return {false, ConflictTypeUnresolved}; + return { false, ConflictTypeUnresolved }; } } @@ -55,11 +54,11 @@ ParseConflictManager::resolve(const ParseAction &new_action, int old_precedence = *old_action.precedence_values.begin(); int new_precedence = *new_action.precedence_values.begin(); if (new_precedence > old_precedence) { - return {true, ConflictTypeResolved}; + return { true, ConflictTypeResolved }; } else if (new_precedence < old_precedence) { - return {false, ConflictTypeResolved}; + return { false, ConflictTypeResolved }; } else { - return {false, ConflictTypeUnresolved}; + return { false, ConflictTypeUnresolved }; } } @@ -67,7 +66,7 @@ ParseConflictManager::resolve(const ParseAction &new_action, break; } - return {false, ConflictTypeNone}; + return { false, ConflictTypeNone }; } } // namespace build_tables diff --git a/src/compiler/build_tables/parse_conflict_manager.h b/src/compiler/build_tables/parse_conflict_manager.h index 24b79d55..40557020 100644 --- a/src/compiler/build_tables/parse_conflict_manager.h +++ b/src/compiler/build_tables/parse_conflict_manager.h @@ -10,7 +10,9 @@ namespace tree_sitter { class ParseAction; -namespace rules { class Symbol; } +namespace rules { +class Symbol; +} namespace build_tables { @@ -25,8 +27,8 @@ class ParseConflictManager { public: ParseConflictManager(const SyntaxGrammar &); - std::pair resolve( - const ParseAction &, const ParseAction &, const rules::Symbol &) const; + std::pair resolve(const ParseAction &, const ParseAction &, + const rules::Symbol &) const; }; } // namespace build_tables diff --git a/src/compiler/build_tables/parse_item.cc b/src/compiler/build_tables/parse_item.cc index c575f100..37c7eb83 100644 --- a/src/compiler/build_tables/parse_item.cc +++ b/src/compiler/build_tables/parse_item.cc @@ -21,10 +21,14 @@ bool ParseItem::operator==(const ParseItem &other) const { } bool ParseItem::operator<(const ParseItem &other) const { - if (lhs < other.lhs) return true; - if (other.lhs < lhs) return false; - if (consumed_symbols.size() < other.consumed_symbols.size()) return true; - if (other.consumed_symbols.size() < consumed_symbols.size()) return false; + if (lhs < other.lhs) + return true; + if (other.lhs < lhs) + return false; + if (consumed_symbols.size() < other.consumed_symbols.size()) + return true; + if (other.consumed_symbols.size() < consumed_symbols.size()) + return false; return rule < other.rule; } diff --git a/src/compiler/build_tables/rule_transitions.h b/src/compiler/build_tables/rule_transitions.h index 3fdb4b07..38cae0ea 100644 --- a/src/compiler/build_tables/rule_transitions.h +++ b/src/compiler/build_tables/rule_transitions.h @@ -9,10 +9,10 @@ namespace tree_sitter { namespace build_tables { std::map char_transitions( - const rules::rule_ptr &rule); + const rules::rule_ptr &rule); std::map sym_transitions( - const rules::rule_ptr &rule); + const rules::rule_ptr &rule); } // namespace build_tables } // namespace tree_sitter diff --git a/src/compiler/compile.cc b/src/compiler/compile.cc index 8e53ebf1..9f194e78 100644 --- a/src/compiler/compile.cc +++ b/src/compiler/compile.cc @@ -13,29 +13,29 @@ using std::vector; using std::get; using std::make_tuple; -pair -compile(const Grammar &grammar, std::string name) { +pair compile(const Grammar &grammar, + std::string name) { auto prepare_grammar_result = prepare_grammar::prepare_grammar(grammar); const SyntaxGrammar &syntax_grammar = get<0>(prepare_grammar_result); const LexicalGrammar &lexical_grammar = get<1>(prepare_grammar_result); const GrammarError *error = get<2>(prepare_grammar_result); if (error) - return {"", error}; + return { "", error }; auto table_build_result = - build_tables::build_tables(syntax_grammar, lexical_grammar); + build_tables::build_tables(syntax_grammar, lexical_grammar); const ParseTable &parse_table = get<0>(table_build_result); const LexTable &lex_table = get<1>(table_build_result); error = get<2>(table_build_result); if (error) - return {"", error}; + return { "", error }; string code = generate_code::c_code(name, parse_table, lex_table, syntax_grammar, lexical_grammar); - return {code, nullptr}; + return { code, nullptr }; } } // namespace tree_sitter diff --git a/src/compiler/generate_code/c_code.cc b/src/compiler/generate_code/c_code.cc index 3da035cc..639c9304 100644 --- a/src/compiler/generate_code/c_code.cc +++ b/src/compiler/generate_code/c_code.cc @@ -23,40 +23,42 @@ using std::to_string; using std::vector; using util::escape_char; -static const map REPLACEMENTS({ { '~', "TILDE" }, - { '`', "BQUOTE" }, - { '!', "BANG" }, - { '@', "AT" }, - { '#', "POUND" }, - { '$', "DOLLAR" }, - { '%', "PERCENT" }, - { '^', "CARET" }, - { '&', "AMP" }, - { '*', "STAR" }, - { '(', "LPAREN" }, - { ')', "RPAREN" }, - { '-', "DASH" }, - { '+', "PLUS" }, - { '=', "EQ" }, - { '{', "LBRACE" }, - { '}', "RBRACE" }, - { '[', "LBRACK" }, - { ']', "RBRACK" }, - { '\\', "BSLASH" }, - { '|', "PIPE" }, - { ':', "COLON" }, - { ';', "SEMI" }, - { '"', "DQUOTE" }, - { '\'', "SQUOTE" }, - { '<', "LT" }, - { '>', "GT" }, - { ',', "COMMA" }, - { '.', "DOT" }, - { '?', "QMARK" }, - { '/', "SLASH" }, - { '\n', "LB" }, - { '\r', "CR" }, - { '\t', "TAB" }, }); +static const map REPLACEMENTS({ + { '~', "TILDE" }, + { '`', "BQUOTE" }, + { '!', "BANG" }, + { '@', "AT" }, + { '#', "POUND" }, + { '$', "DOLLAR" }, + { '%', "PERCENT" }, + { '^', "CARET" }, + { '&', "AMP" }, + { '*', "STAR" }, + { '(', "LPAREN" }, + { ')', "RPAREN" }, + { '-', "DASH" }, + { '+', "PLUS" }, + { '=', "EQ" }, + { '{', "LBRACE" }, + { '}', "RBRACE" }, + { '[', "LBRACK" }, + { ']', "RBRACK" }, + { '\\', "BSLASH" }, + { '|', "PIPE" }, + { ':', "COLON" }, + { ';', "SEMI" }, + { '"', "DQUOTE" }, + { '\'', "SQUOTE" }, + { '<', "LT" }, + { '>', "GT" }, + { ',', "COMMA" }, + { '.', "DOT" }, + { '?', "QMARK" }, + { '/', "SLASH" }, + { '\n', "LB" }, + { '\r', "CR" }, + { '\t', "TAB" }, +}); class CCodeGenerator { string buffer; @@ -182,8 +184,8 @@ class CCodeGenerator { line("#pragma GCC diagnostic ignored \"-Wmissing-field-initializers\""); line(); line( - "static const TSParseAction *" - "ts_parse_actions[STATE_COUNT][SYMBOL_COUNT] = {"); + "static const TSParseAction *" + "ts_parse_actions[STATE_COUNT][SYMBOL_COUNT] = {"); indent([&]() { for (const auto &state : parse_table.states) { @@ -356,7 +358,7 @@ class CCodeGenerator { bool reduce_action_is_fragile(const ParseAction &action) const { return parse_table.fragile_production_ids.find(action.production_id) != - parse_table.fragile_production_ids.end(); + parse_table.fragile_production_ids.end(); } // C-code generation functions @@ -455,7 +457,8 @@ string c_code(string name, const ParseTable &parse_table, const LexTable &lex_table, const SyntaxGrammar &syntax_grammar, const LexicalGrammar &lexical_grammar) { return CCodeGenerator(name, parse_table, lex_table, syntax_grammar, - lexical_grammar).code(); + lexical_grammar) + .code(); } } // namespace generate_code diff --git a/src/compiler/lex_table.cc b/src/compiler/lex_table.cc index 16e272c4..9fecc716 100644 --- a/src/compiler/lex_table.cc +++ b/src/compiler/lex_table.cc @@ -47,10 +47,10 @@ std::ostream &operator<<(std::ostream &stream, const LexAction &action) { return stream << string("#"); case LexActionTypeAccept: return stream << string("#"; + ">"; case LexActionTypeAdvance: return stream << string("#"; + ">"; default: return stream; } diff --git a/src/compiler/parse_table.cc b/src/compiler/parse_table.cc index 1e7685dc..9e3372e9 100644 --- a/src/compiler/parse_table.cc +++ b/src/compiler/parse_table.cc @@ -13,8 +13,7 @@ using rules::Symbol; ParseAction::ParseAction(ParseActionType type, ParseStateId state_index, Symbol symbol, size_t consumed_symbol_count, set precedence_values, - rules::Associativity associativity, - int production_id) + rules::Associativity associativity, int production_id) : type(type), symbol(symbol), state_index(state_index), @@ -30,9 +29,7 @@ ParseAction::ParseAction() consumed_symbol_count(0), associativity(rules::AssociativityUnspecified) {} -ParseAction ParseAction::Error() { - return ParseAction(); -} +ParseAction ParseAction::Error() { return ParseAction(); } ParseAction ParseAction::Accept() { ParseAction action; @@ -60,7 +57,8 @@ ParseAction ParseAction::ReduceExtra(Symbol symbol) { } ParseAction ParseAction::Reduce(Symbol symbol, size_t consumed_symbol_count, - int precedence, rules::Associativity associativity, + int precedence, + rules::Associativity associativity, int production_id) { return ParseAction(ParseActionTypeReduce, 0, symbol, consumed_symbol_count, { precedence }, associativity, production_id); @@ -71,17 +69,23 @@ bool ParseAction::operator==(const ParseAction &other) const { bool symbols_eq = symbol == other.symbol; bool state_indices_eq = state_index == other.state_index; bool consumed_symbol_counts_eq = - consumed_symbol_count == other.consumed_symbol_count; + consumed_symbol_count == other.consumed_symbol_count; return types_eq && symbols_eq && state_indices_eq && consumed_symbol_counts_eq; } bool ParseAction::operator<(const ParseAction &other) const { - if (type < other.type) return true; - if (other.type < type) return false; - if (symbol < other.symbol) return true; - if (other.symbol < symbol) return false; - if (state_index < other.state_index) return true; - if (other.state_index < state_index) return false; + if (type < other.type) + return true; + if (other.type < type) + return false; + if (symbol < other.symbol) + return true; + if (other.symbol < symbol) + return false; + if (state_index < other.state_index) + return true; + if (other.state_index < state_index) + return false; return consumed_symbol_count < other.consumed_symbol_count; } @@ -138,13 +142,15 @@ ParseStateId ParseTable::add_state() { return states.size() - 1; } -ParseAction &ParseTable::set_action(ParseStateId id, Symbol symbol, ParseAction action) { +ParseAction &ParseTable::set_action(ParseStateId id, Symbol symbol, + ParseAction action) { symbols.insert(symbol); states[id].actions[symbol] = vector({ action }); return *states[id].actions[symbol].begin(); } -ParseAction &ParseTable::add_action(ParseStateId id, Symbol symbol, ParseAction action) { +ParseAction &ParseTable::add_action(ParseStateId id, Symbol symbol, + ParseAction action) { symbols.insert(symbol); states[id].actions[symbol].push_back(action); return *states[id].actions[symbol].rbegin(); diff --git a/src/compiler/parse_table.h b/src/compiler/parse_table.h index 7db3fb40..ae8822a8 100644 --- a/src/compiler/parse_table.h +++ b/src/compiler/parse_table.h @@ -36,7 +36,8 @@ class ParseAction { static ParseAction Shift(ParseStateId state_index, std::set precedence_values); static ParseAction Reduce(rules::Symbol symbol, size_t consumed_symbol_count, - int precedence, rules::Associativity, int production_id); + int precedence, rules::Associativity, + int production_id); static ParseAction ShiftExtra(); static ParseAction ReduceExtra(rules::Symbol symbol); bool operator==(const ParseAction &) const; @@ -86,9 +87,9 @@ class ParseTable { public: ParseStateId add_state(); ParseAction &set_action(ParseStateId state_id, rules::Symbol symbol, - ParseAction action); + ParseAction action); ParseAction &add_action(ParseStateId state_id, rules::Symbol symbol, - ParseAction action); + ParseAction action); std::vector states; std::set symbols; diff --git a/src/compiler/prepare_grammar/expand_repeats.cc b/src/compiler/prepare_grammar/expand_repeats.cc index 1750e3a2..ca578a7e 100644 --- a/src/compiler/prepare_grammar/expand_repeats.cc +++ b/src/compiler/prepare_grammar/expand_repeats.cc @@ -40,13 +40,14 @@ class ExpandRepeats : public rules::IdentityRuleFn { rule_ptr inner_rule = apply(rule->content); size_t index = aux_rules.size(); - string helper_rule_name = rule_name + string("_repeat") + to_string(++repeat_count); + string helper_rule_name = + rule_name + string("_repeat") + to_string(++repeat_count); Symbol repeat_symbol(offset + index, rules::SymbolOptionAuxiliary); existing_repeats.push_back({ rule->copy(), repeat_symbol }); aux_rules.push_back( - { helper_rule_name, - Seq::build({ inner_rule, Choice::build({ repeat_symbol.copy(), - make_shared() }) }) }); + { helper_rule_name, + Seq::build({ inner_rule, Choice::build({ repeat_symbol.copy(), + make_shared() }) }) }); return repeat_symbol.copy(); } @@ -74,7 +75,8 @@ SyntaxGrammar expand_repeats(const SyntaxGrammar &grammar) { ExpandRepeats expander(result.aux_rules.size()); for (auto &pair : grammar.rules) - result.rules.push_back({ pair.first, expander.expand(pair.second, pair.first) }); + result.rules.push_back( + { pair.first, expander.expand(pair.second, pair.first) }); result.aux_rules.insert(result.aux_rules.end(), expander.aux_rules.begin(), expander.aux_rules.end()); diff --git a/src/compiler/prepare_grammar/expand_tokens.cc b/src/compiler/prepare_grammar/expand_tokens.cc index d0e5055f..b71f1d18 100644 --- a/src/compiler/prepare_grammar/expand_tokens.cc +++ b/src/compiler/prepare_grammar/expand_tokens.cc @@ -56,8 +56,8 @@ class ExpandTokens : public rules::IdentityRuleFn { ExpandTokens() : error(nullptr) {} }; -pair -expand_tokens(const LexicalGrammar &grammar) { +pair expand_tokens( + const LexicalGrammar &grammar) { LexicalGrammar result; ExpandTokens expander; @@ -82,7 +82,9 @@ expand_tokens(const LexicalGrammar &grammar) { result.separators.push_back(rule); } - return { result, nullptr, }; + return { + result, nullptr, + }; } } // namespace prepare_grammar diff --git a/src/compiler/prepare_grammar/expand_tokens.h b/src/compiler/prepare_grammar/expand_tokens.h index 5f666e43..83387794 100644 --- a/src/compiler/prepare_grammar/expand_tokens.h +++ b/src/compiler/prepare_grammar/expand_tokens.h @@ -11,7 +11,7 @@ class LexicalGrammar; namespace prepare_grammar { std::pair expand_tokens( - const LexicalGrammar &); + const LexicalGrammar &); } // namespace prepare_grammar } // namespace tree_sitter diff --git a/src/compiler/prepare_grammar/extract_tokens.cc b/src/compiler/prepare_grammar/extract_tokens.cc index 27ceaa42..d557ea91 100644 --- a/src/compiler/prepare_grammar/extract_tokens.cc +++ b/src/compiler/prepare_grammar/extract_tokens.cc @@ -89,12 +89,13 @@ class TokenExtractor : public rules::IdentityRuleFn { vector> tokens; }; -static const GrammarError * ubiq_token_err(const string &msg) { - return new GrammarError(GrammarErrorTypeInvalidUbiquitousToken, "Not a token: " + msg); +static const GrammarError *ubiq_token_err(const string &msg) { + return new GrammarError(GrammarErrorTypeInvalidUbiquitousToken, + "Not a token: " + msg); } -tuple -extract_tokens(const InternedGrammar &grammar) { +tuple extract_tokens( + const InternedGrammar &grammar) { SyntaxGrammar syntax_grammar; LexicalGrammar lexical_grammar; SymbolReplacer symbol_replacer; @@ -104,12 +105,12 @@ extract_tokens(const InternedGrammar &grammar) { for (auto &pair : grammar.rules) { if (is_token(pair.second)) { lexical_grammar.rules.push_back(pair); - symbol_replacer.replacements.insert({ - Symbol(i), - Symbol(lexical_grammar.rules.size() - 1, SymbolOptionToken) - }); + symbol_replacer.replacements.insert( + { Symbol(i), + Symbol(lexical_grammar.rules.size() - 1, SymbolOptionToken) }); } else { - syntax_grammar.rules.push_back({ pair.first, extractor.apply(pair.second) }); + syntax_grammar.rules.push_back( + { pair.first, extractor.apply(pair.second) }); } i++; } @@ -123,11 +124,14 @@ extract_tokens(const InternedGrammar &grammar) { } else { auto sym = dynamic_pointer_cast(extractor.apply(rule)); if (!sym.get()) - return make_tuple(syntax_grammar, lexical_grammar, ubiq_token_err(rule->to_string())); + return make_tuple(syntax_grammar, lexical_grammar, + ubiq_token_err(rule->to_string())); Symbol symbol = symbol_replacer.replace_symbol(*sym); if (!symbol.is_token()) - return make_tuple(syntax_grammar, lexical_grammar, ubiq_token_err(syntax_grammar.rules[symbol.index].first)); + return make_tuple( + syntax_grammar, lexical_grammar, + ubiq_token_err(syntax_grammar.rules[symbol.index].first)); syntax_grammar.ubiquitous_tokens.insert(symbol); } diff --git a/src/compiler/prepare_grammar/extract_tokens.h b/src/compiler/prepare_grammar/extract_tokens.h index 8dfa0c6a..1aee7e88 100644 --- a/src/compiler/prepare_grammar/extract_tokens.h +++ b/src/compiler/prepare_grammar/extract_tokens.h @@ -14,7 +14,7 @@ class LexicalGrammar; namespace prepare_grammar { std::tuple extract_tokens( - const InternedGrammar &); + const InternedGrammar &); } // namespace prepare_grammar } // namespace tree_sitter diff --git a/src/compiler/prepare_grammar/intern_symbols.cc b/src/compiler/prepare_grammar/intern_symbols.cc index 657f84c0..f611a63a 100644 --- a/src/compiler/prepare_grammar/intern_symbols.cc +++ b/src/compiler/prepare_grammar/intern_symbols.cc @@ -42,7 +42,7 @@ class InternSymbols : public rules::IdentityRuleFn { string missing_rule_name; }; -const GrammarError * missing_rule_error(string rule_name) { +const GrammarError *missing_rule_error(string rule_name) { return new GrammarError(GrammarErrorTypeUndefinedSymbol, "Undefined rule '" + rule_name + "'"); } @@ -54,14 +54,14 @@ pair intern_symbols(const Grammar &gramma for (auto &pair : grammar.rules()) { auto new_rule = interner.apply(pair.second); if (!interner.missing_rule_name.empty()) - return {result, missing_rule_error(interner.missing_rule_name)}; + return { result, missing_rule_error(interner.missing_rule_name) }; result.rules.push_back({ pair.first, new_rule }); } for (auto &rule : grammar.ubiquitous_tokens()) { auto new_rule = interner.apply(rule); if (!interner.missing_rule_name.empty()) - return {result, missing_rule_error(interner.missing_rule_name)}; + return { result, missing_rule_error(interner.missing_rule_name) }; result.ubiquitous_tokens.insert(new_rule); } diff --git a/src/compiler/prepare_grammar/interned_grammar.h b/src/compiler/prepare_grammar/interned_grammar.h index c29cd1b5..a9ed9919 100644 --- a/src/compiler/prepare_grammar/interned_grammar.h +++ b/src/compiler/prepare_grammar/interned_grammar.h @@ -20,5 +20,4 @@ struct InternedGrammar { } // namespace prepare_grammar } // namespace tree_sitter - #endif // COMPILER_PREPARE_GRAMMAR_INTERNED_GRAMMAR_H_ diff --git a/src/compiler/prepare_grammar/parse_regex.cc b/src/compiler/prepare_grammar/parse_regex.cc index 49e23ad7..4ff0d99b 100644 --- a/src/compiler/prepare_grammar/parse_regex.cc +++ b/src/compiler/prepare_grammar/parse_regex.cc @@ -49,7 +49,7 @@ class PatternParser { choices.push_back(pair.first); } while (has_more_input()); auto rule = - (choices.size() > 1) ? make_shared(choices) : choices.front(); + (choices.size() > 1) ? make_shared(choices) : choices.front(); return { rule, nullptr }; } @@ -115,8 +115,12 @@ class PatternParser { next(); return { pair.first.copy(), nullptr }; } - case ')': { return error("unmatched close paren"); } - case ']': { return error("unmatched close square bracket"); } + case ')': { + return error("unmatched close paren"); + } + case ']': { + return error("unmatched close square bracket"); + } case '.': { next(); return { CharacterSet().include_all().exclude('\n').copy(), nullptr }; @@ -180,15 +184,15 @@ class PatternParser { return CharacterSet().include('a', 'z').include('A', 'Z'); case 'w': return CharacterSet() - .include('a', 'z') - .include('A', 'Z') - .include('0', '9') - .include('_'); + .include('a', 'z') + .include('A', 'Z') + .include('0', '9') + .include('_'); case 'd': return CharacterSet().include('0', '9'); case 's': return CharacterSet().include(' ').include('\t').include('\n').include( - '\r'); + '\r'); case 't': return CharacterSet().include('\t'); case 'n': diff --git a/src/compiler/prepare_grammar/prepare_grammar.cc b/src/compiler/prepare_grammar/prepare_grammar.cc index d5fa73e9..7c9c93ed 100644 --- a/src/compiler/prepare_grammar/prepare_grammar.cc +++ b/src/compiler/prepare_grammar/prepare_grammar.cc @@ -14,7 +14,7 @@ using std::get; using std::make_tuple; tuple prepare_grammar( - const Grammar &input_grammar) { + const Grammar &input_grammar) { // Convert all string-based `NamedSymbols` into numerical `Symbols` auto intern_result = intern_symbols(input_grammar); diff --git a/src/compiler/prepare_grammar/prepare_grammar.h b/src/compiler/prepare_grammar/prepare_grammar.h index 5800ea80..80196c8b 100644 --- a/src/compiler/prepare_grammar/prepare_grammar.h +++ b/src/compiler/prepare_grammar/prepare_grammar.h @@ -13,7 +13,7 @@ class GrammarError; namespace prepare_grammar { std::tuple prepare_grammar( - const Grammar &); + const Grammar &); } // namespace prepare_grammar } // namespace tree_sitter diff --git a/src/compiler/rules/character_set.cc b/src/compiler/rules/character_set.cc index c3c044a2..3e50ed87 100644 --- a/src/compiler/rules/character_set.cc +++ b/src/compiler/rules/character_set.cc @@ -169,10 +169,10 @@ CharacterSet CharacterSet::remove_set(const CharacterSet &other) { if (other.includes_all) { result.included_chars = included_chars; included_chars = - remove_chars(&result.included_chars, other.excluded_chars); + remove_chars(&result.included_chars, other.excluded_chars); } else { result.included_chars = - remove_chars(&included_chars, other.included_chars); + remove_chars(&included_chars, other.included_chars); } } return result; diff --git a/src/compiler/rules/rules.cc b/src/compiler/rules/rules.cc index f133672c..8770e98c 100644 --- a/src/compiler/rules/rules.cc +++ b/src/compiler/rules/rules.cc @@ -32,9 +32,7 @@ rule_ptr blank() { return make_shared(); } rule_ptr choice(const vector &rules) { return Choice::build(rules); } -rule_ptr repeat(const rule_ptr &content) { - return Repeat::build(content); -} +rule_ptr repeat(const rule_ptr &content) { return Repeat::build(content); } rule_ptr seq(const vector &rules) { return Seq::build(rules); } @@ -46,15 +44,11 @@ rule_ptr str(const string &value) { return token(prec(1, make_shared(value))); } -rule_ptr err(const rule_ptr &rule) { - return choice({ rule, ERROR().copy() }); -} +rule_ptr err(const rule_ptr &rule) { return choice({ rule, ERROR().copy() }); } rule_ptr prec(int precedence, const rule_ptr &rule, Associativity associativity) { - return metadata(rule, { - { PRECEDENCE, precedence }, - { ASSOCIATIVITY, associativity } - }); + return metadata( + rule, { { PRECEDENCE, precedence }, { ASSOCIATIVITY, associativity } }); } rule_ptr prec(int precedence, const rule_ptr &rule) { diff --git a/src/compiler/rules/symbol.cc b/src/compiler/rules/symbol.cc index 086f62aa..ac7b5986 100644 --- a/src/compiler/rules/symbol.cc +++ b/src/compiler/rules/symbol.cc @@ -11,7 +11,7 @@ using std::to_string; using std::hash; SymbolOption SymbolOptionAuxToken = - SymbolOption(SymbolOptionToken | SymbolOptionAuxiliary); + SymbolOption(SymbolOptionToken | SymbolOptionAuxiliary); Symbol::Symbol(int index) : index(index), options(SymbolOption(0)) {} diff --git a/src/runtime/document.c b/src/runtime/document.c index 8877398e..aa50d9a0 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -27,11 +27,11 @@ void ts_document_free(TSDocument *document) { static void reparse(TSDocument *document, TSInputEdit *edit) { if (document->input.read_fn && document->parser.language) { const TSTree *tree = - ts_parser_parse(&document->parser, document->input, edit); + ts_parser_parse(&document->parser, document->input, edit); if (document->node) ts_node_release(document->node); document->node = - ts_node_make_root(tree, document->parser.language->symbol_names); + ts_node_make_root(tree, document->parser.language->symbol_names); } } diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 3ad9f31c..42fe94d7 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -13,10 +13,10 @@ lexer->debug_buffer); \ } -#define DEBUG_LOOKAHEAD() \ - DEBUG((0 < lexer->lookahead &&lexer->lookahead < 256) \ - ? "lookahead char:'%c'" \ - : "lookahead char:%d", \ +#define DEBUG_LOOKAHEAD() \ + DEBUG((0 < lexer->lookahead && lexer->lookahead < 256) \ + ? "lookahead char:'%c'" \ + : "lookahead char:%d", \ lexer->lookahead); static const char *empty_chunk = ""; @@ -35,8 +35,8 @@ static void read_next_chunk(TSLexer *lexer) { static void read_lookahead(TSLexer *lexer) { size_t position_in_chunk = lexer->current_position.bytes - lexer->chunk_start; lexer->lookahead_size = utf8proc_iterate( - (const uint8_t *)lexer->chunk + position_in_chunk, - lexer->chunk_size - position_in_chunk + 1, &lexer->lookahead); + (const uint8_t *)lexer->chunk + position_in_chunk, + lexer->chunk_size - position_in_chunk + 1, &lexer->lookahead); DEBUG_LOOKAHEAD(); } @@ -71,9 +71,9 @@ static bool advance(TSLexer *lexer, TSStateId state) { static TSTree *accept(TSLexer *lexer, TSSymbol symbol, int is_hidden, const char *symbol_name) { TSLength size = - ts_length_sub(lexer->current_position, lexer->token_start_position); + ts_length_sub(lexer->current_position, lexer->token_start_position); TSLength padding = - ts_length_sub(lexer->token_start_position, lexer->token_end_position); + ts_length_sub(lexer->token_start_position, lexer->token_end_position); lexer->token_end_position = lexer->current_position; if (symbol == ts_builtin_sym_error) { @@ -91,19 +91,19 @@ static TSTree *accept(TSLexer *lexer, TSSymbol symbol, int is_hidden, */ TSLexer ts_lexer_make() { - TSLexer result = (TSLexer) { .start_fn = start, - .start_token_fn = start_token, - .advance_fn = advance, - .accept_fn = accept, - .chunk = NULL, - .chunk_start = 0, - .chunk_size = 0, - .current_position = ts_length_zero(), - .token_start_position = ts_length_zero(), - .token_end_position = ts_length_zero(), - .lookahead = 0, - .lookahead_size = 0, - .debugger = ts_debugger_null() }; + TSLexer result = (TSLexer){.start_fn = start, + .start_token_fn = start_token, + .advance_fn = advance, + .accept_fn = accept, + .chunk = NULL, + .chunk_start = 0, + .chunk_size = 0, + .current_position = ts_length_zero(), + .token_start_position = ts_length_zero(), + .token_end_position = ts_length_zero(), + .lookahead = 0, + .lookahead_size = 0, + .debugger = ts_debugger_null() }; return result; } diff --git a/src/runtime/node.c b/src/runtime/node.c index d45d6b1b..785d566f 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -8,12 +8,14 @@ TSNode *ts_node_make(const TSTree *tree, TSNode *parent, size_t index, if (parent) ts_node_retain(parent); TSNode *result = malloc(sizeof(TSNode)); - *result = (TSNode) { .ref_count = 1, - .parent = parent, - .index = index, - .content = tree, - .position = position, - .names = names, }; + *result = (TSNode){ + .ref_count = 1, + .parent = parent, + .index = index, + .content = tree, + .position = position, + .names = names, + }; return result; } diff --git a/src/runtime/parse_stack.c b/src/runtime/parse_stack.c index 0ef85040..d08690d9 100644 --- a/src/runtime/parse_stack.c +++ b/src/runtime/parse_stack.c @@ -30,7 +30,7 @@ struct ParseStack { ParseStack *ts_parse_stack_new(TreeSelectionCallback tree_selection_callback) { ParseStack *this = malloc(sizeof(ParseStack)); - *this = (ParseStack) { + *this = (ParseStack){ .heads = calloc(INITIAL_HEAD_CAPACITY, sizeof(ParseStackNode *)), .head_count = 1, .head_capacity = INITIAL_HEAD_CAPACITY, @@ -81,13 +81,15 @@ ParseStackEntry *ts_parse_stack_entry_next(const ParseStackEntry *entry, int i) */ static void stack_node_retain(ParseStackNode *this) { - if (!this) return; + if (!this) + return; assert(this->ref_count != 0); this->ref_count++; } static bool stack_node_release(ParseStackNode *this) { - if (!this) return false; + if (!this) + return false; assert(this->ref_count != 0); this->ref_count--; if (this->ref_count == 0) { @@ -101,23 +103,26 @@ static bool stack_node_release(ParseStackNode *this) { } } -static ParseStackNode *stack_node_new(ParseStackNode *next, TSStateId state, TSTree *tree) { +static ParseStackNode *stack_node_new(ParseStackNode *next, TSStateId state, + TSTree *tree) { ParseStackNode *this = malloc(sizeof(ParseStackNode)); ts_tree_retain(tree); stack_node_retain(next); - *this = (ParseStackNode) { + *this = (ParseStackNode){ .ref_count = 1, .successor_count = 1, - .successors = {next, NULL, NULL}, - .entry = { - .state = state, - .tree = tree, - }, + .successors = { next, NULL, NULL }, + .entry = + { + .state = state, .tree = tree, + }, }; return this; } -static void ts_parse_stack_add_node_successor(ParseStack *this, ParseStackNode *node, ParseStackNode *new_successor) { +static void ts_parse_stack_add_node_successor(ParseStack *this, + ParseStackNode *node, + ParseStackNode *new_successor) { for (int i = 0; i < node->successor_count; i++) { ParseStackNode *successor = node->successors[i]; if (successor == new_successor) @@ -125,12 +130,11 @@ static void ts_parse_stack_add_node_successor(ParseStack *this, ParseStackNode * if (successor->entry.state == new_successor->entry.state) { if (successor->entry.tree != new_successor->entry.tree) successor->entry.tree = this->tree_selection_callback.callback( - this->tree_selection_callback.data, - successor->entry.tree, - new_successor->entry.tree - ); + this->tree_selection_callback.data, successor->entry.tree, + new_successor->entry.tree); for (int j = 0; j < new_successor->successor_count; j++) - ts_parse_stack_add_node_successor(this, successor, new_successor->successors[j]); + ts_parse_stack_add_node_successor(this, successor, + new_successor->successors[j]); return; } } @@ -147,7 +151,8 @@ static void ts_parse_stack_add_node_successor(ParseStack *this, ParseStackNode * static int ts_parse_stack_add_head(ParseStack *this, ParseStackNode *node) { if (this->head_count == this->head_capacity) { this->head_capacity += 3; - this->heads = realloc(this->heads, this->head_capacity * sizeof(ParseStackNode *)); + this->heads = + realloc(this->heads, this->head_capacity * sizeof(ParseStackNode *)); } int new_index = this->head_count++; this->heads[new_index] = node; @@ -155,7 +160,8 @@ static int ts_parse_stack_add_head(ParseStack *this, ParseStackNode *node) { return new_index; } -static int ts_parse_stack_find_or_add_head(ParseStack *this, ParseStackNode *node) { +static int ts_parse_stack_find_or_add_head(ParseStack *this, + ParseStackNode *node) { for (int i = 0; i < this->head_count; i++) if (this->heads[i] == node) { return i; @@ -171,16 +177,14 @@ void ts_parse_stack_remove_head(ParseStack *this, int head_index) { this->head_count--; } -static bool ts_parse_stack_merge_head(ParseStack *this, int head_index, TSStateId state, TSTree *tree) { - for (int i = 0; i < head_index; i++) { +static bool ts_parse_stack_merge_head(ParseStack *this, int head_index, + TSStateId state, TSTree *tree) { + for (int i = 0; i < head_index; i++) { ParseStackNode *head = this->heads[i]; if (head->entry.state == state) { if (head->entry.tree != tree) { head->entry.tree = this->tree_selection_callback.callback( - this->tree_selection_callback.data, - head->entry.tree, - tree - ); + this->tree_selection_callback.data, head->entry.tree, tree); } ts_parse_stack_add_node_successor(this, head, this->heads[head_index]); ts_parse_stack_remove_head(this, head_index); @@ -194,7 +198,8 @@ static bool ts_parse_stack_merge_head(ParseStack *this, int head_index, TSStateI * Section: Mutating the stack (Public) */ -bool ts_parse_stack_push(ParseStack *this, int head_index, TSStateId state, TSTree *tree) { +bool ts_parse_stack_push(ParseStack *this, int head_index, TSStateId state, + TSTree *tree) { assert(head_index < this->head_count); if (ts_parse_stack_merge_head(this, head_index, state, tree)) return true; @@ -202,14 +207,12 @@ bool ts_parse_stack_push(ParseStack *this, int head_index, TSStateId state, TSTr return false; } -void ts_parse_stack_add_alternative(ParseStack *this, int head_index, TSTree *tree) { +void ts_parse_stack_add_alternative(ParseStack *this, int head_index, + TSTree *tree) { assert(head_index < this->head_count); ParseStackEntry *entry = &this->heads[head_index]->entry; entry->tree = this->tree_selection_callback.callback( - this->tree_selection_callback.data, - entry->tree, - tree - ); + this->tree_selection_callback.data, entry->tree, tree); } int ts_parse_stack_split(ParseStack *this, int head_index) { @@ -217,15 +220,16 @@ int ts_parse_stack_split(ParseStack *this, int head_index) { return ts_parse_stack_add_head(this, this->heads[head_index]); } -ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int child_count, bool count_extra) { +ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, + int child_count, bool count_extra) { ParseStackNode *previous_head = this->heads[head_index]; int path_count = 1; int capacity = (child_count == -1) ? STARTING_TREE_CAPACITY : child_count; - size_t tree_counts_by_path[MAX_POP_PATH_COUNT] = {child_count}; - ParseStackNode *nodes_by_path[MAX_POP_PATH_COUNT] = {previous_head}; - TreeVector trees_by_path[MAX_POP_PATH_COUNT] = {tree_vector_new(capacity)}; - bool is_shared_by_path[MAX_POP_PATH_COUNT] = {false}; + size_t tree_counts_by_path[MAX_POP_PATH_COUNT] = { child_count }; + ParseStackNode *nodes_by_path[MAX_POP_PATH_COUNT] = { previous_head }; + TreeVector trees_by_path[MAX_POP_PATH_COUNT] = { tree_vector_new(capacity) }; + bool is_shared_by_path[MAX_POP_PATH_COUNT] = { false }; /* * Reduce along every possible path in parallel. Stop when the given number @@ -260,7 +264,8 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int for (int i = 0; i < node->successor_count; i++) { int next_path; if (i > 0) { - if (path_count == MAX_POP_PATH_COUNT) break; + if (path_count == MAX_POP_PATH_COUNT) + break; next_path = path_count; tree_counts_by_path[next_path] = tree_counts_by_path[path]; trees_by_path[next_path] = trees_by_path[path]; @@ -286,7 +291,7 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int index = ts_parse_stack_find_or_add_head(this, nodes_by_path[path]); } - this->last_pop_results[path] = (ParseStackPopResult) { + this->last_pop_results[path] = (ParseStackPopResult){ .index = index, .tree_count = trees_by_path[path].size, .trees = trees_by_path[path].contents, @@ -294,9 +299,8 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int } stack_node_release(previous_head); - return (ParseStackPopResultList) { - .size = path_count, - .contents = this->last_pop_results, + return (ParseStackPopResultList){ + .size = path_count, .contents = this->last_pop_results, }; } @@ -304,7 +308,8 @@ void ts_parse_stack_shrink(ParseStack *this, int head_index, int count) { ParseStackNode *head = this->heads[head_index]; ParseStackNode *new_head = head; for (int i = 0; i < count; i++) { - if (new_head->successor_count == 0) break; + if (new_head->successor_count == 0) + break; new_head = new_head->successors[0]; } stack_node_retain(new_head); diff --git a/src/runtime/parse_stack.h b/src/runtime/parse_stack.h index ddbe10d3..7cc623f3 100644 --- a/src/runtime/parse_stack.h +++ b/src/runtime/parse_stack.h @@ -27,7 +27,7 @@ typedef struct { typedef struct { void *data; - TSTree * (*callback)(void *data, TSTree *, TSTree *); + TSTree *(*callback)(void *data, TSTree *, TSTree *); } TreeSelectionCallback; /* @@ -90,7 +90,8 @@ void ts_parse_stack_add_alternative(ParseStack *, int head, TSTree *); * which had previously been merged. It returns a struct that indicates the * index of each revealed head and the trees removed from that head. */ -ParseStackPopResultList ts_parse_stack_pop(ParseStack *, int head, int count, bool count_extra); +ParseStackPopResultList ts_parse_stack_pop(ParseStack *, int head, int count, + bool count_extra); /* * Remove the given number of entries from the given head of the stack. diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 0ff5539b..167f7c87 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -29,13 +29,13 @@ */ static const TSParseAction ERROR_ACTIONS[2] = { - {.type = TSParseActionTypeError}, - {.type = 0} + {.type = TSParseActionTypeError }, {.type = 0 } }; -static const TSParseAction *get_actions(const TSLanguage *language, TSStateId state, - TSSymbol sym) { - const TSParseAction *actions = (language->parse_table + (state * language->symbol_count))[sym]; +static const TSParseAction *get_actions(const TSLanguage *language, + TSStateId state, TSSymbol sym) { + const TSParseAction *actions = + (language->parse_table + (state * language->symbol_count))[sym]; return actions ? actions : ERROR_ACTIONS; } @@ -47,9 +47,10 @@ static TSParseAction get_action(const TSLanguage *language, TSStateId state, static TSLength break_down_left_stack(TSParser *parser, TSInputEdit edit) { ts_stack_shrink(&parser->right_stack, 0); - TSLength prev_size = ts_tree_total_size(ts_parse_stack_top_tree(parser->stack, 0)); + TSLength prev_size = + ts_tree_total_size(ts_parse_stack_top_tree(parser->stack, 0)); parser->total_chars = - prev_size.chars + edit.chars_inserted - edit.chars_removed; + prev_size.chars + edit.chars_inserted - edit.chars_removed; TSLength left_subtree_end = prev_size; size_t right_subtree_start = parser->total_chars; @@ -61,7 +62,8 @@ static TSLength break_down_left_stack(TSParser *parser, TSInputEdit edit) { size_t child_count; TSTree **children = ts_tree_children(node, &child_count); - if (left_subtree_end.chars < edit.position && !children && node->symbol != ts_builtin_sym_error) + if (left_subtree_end.chars < edit.position && !children && + node->symbol != ts_builtin_sym_error) break; DEBUG("pop_left sym:%s, state:%u", SYM_NAME(node->symbol), @@ -75,12 +77,12 @@ static TSLength break_down_left_stack(TSParser *parser, TSInputEdit edit) { TSStateId state = ts_parse_stack_top_state(parser->stack, 0); TSParseAction action = get_action(parser->language, state, child->symbol); TSStateId next_state = - ts_tree_is_extra(child) ? state : action.data.to_state; + ts_tree_is_extra(child) ? state : action.data.to_state; DEBUG("push_left sym:%s, state:%u", SYM_NAME(child->symbol), next_state); ts_parse_stack_push(parser->stack, 0, next_state, child); left_subtree_end = - ts_length_add(left_subtree_end, ts_tree_total_size(child)); + ts_length_add(left_subtree_end, ts_tree_total_size(child)); } for (size_t j = child_count - 1; j + 1 > i; j--) { @@ -107,7 +109,7 @@ static TSTree *break_down_right_stack(TSParser *parser) { TSStateId state = ts_parse_stack_top_state(parser->stack, 0); size_t right_subtree_start = - parser->total_chars - ts_stack_total_tree_size(stack).chars; + parser->total_chars - ts_stack_total_tree_size(stack).chars; for (;;) { TSTree *node = ts_stack_top_node(stack); @@ -119,8 +121,7 @@ static TSTree *break_down_right_stack(TSParser *parser) { TSParseAction action = get_action(parser->language, state, node->symbol); bool is_usable = (action.type != TSParseActionTypeError) && - !ts_tree_is_extra(node) && - !ts_tree_is_empty(node) && + !ts_tree_is_extra(node) && !ts_tree_is_empty(node) && !ts_tree_is_fragile_left(node) && !ts_tree_is_fragile_right(node); if (is_usable && right_subtree_start == current_position.chars) { @@ -157,9 +158,9 @@ static TSTree *get_next_node(TSParser *parser, TSStateId lex_state) { ts_tree_is_extra(node), ts_tree_total_size(node).chars); parser->lexer.token_start_position = - ts_length_add(parser->lexer.current_position, node->padding); + ts_length_add(parser->lexer.current_position, node->padding); parser->lexer.token_end_position = parser->lexer.current_position = - ts_length_add(parser->lexer.token_start_position, node->size); + ts_length_add(parser->lexer.token_start_position, node->size); parser->lexer.lookahead = 0; parser->lexer.lookahead_size = 0; @@ -184,9 +185,11 @@ static void shift_extra(TSParser *parser, int head, TSStateId state) { shift(parser, head, state); } -static TSTree * reduce_helper(TSParser *parser, int head, TSSymbol symbol, size_t child_count, bool extra, bool count_extra) { +static TSTree *reduce_helper(TSParser *parser, int head, TSSymbol symbol, + size_t child_count, bool extra, bool count_extra) { bool hidden = parser->language->hidden_symbol_flags[symbol]; - ParseStackPopResultList pop_results = ts_parse_stack_pop(parser->stack, head, child_count, count_extra); + ParseStackPopResultList pop_results = + ts_parse_stack_pop(parser->stack, head, child_count, count_extra); TSTree *parent = NULL; TSTree **last_children = NULL; @@ -196,13 +199,15 @@ static TSTree * reduce_helper(TSParser *parser, int head, TSSymbol symbol, size_ ParseStackPopResult pop_result = pop_results.contents[i]; if (pop_result.trees != last_children) { - parent = ts_tree_make_node(symbol, pop_result.tree_count, pop_result.trees, hidden); + parent = ts_tree_make_node(symbol, pop_result.tree_count, + pop_result.trees, hidden); } if (pop_result.index == last_index) { ts_parse_stack_add_alternative(parser->stack, pop_result.index, parent); } else { - TSStateId top_state = ts_parse_stack_top_state(parser->stack, pop_result.index); + TSStateId top_state = + ts_parse_stack_top_state(parser->stack, pop_result.index); TSStateId state; if (extra) { @@ -222,7 +227,8 @@ static TSTree * reduce_helper(TSParser *parser, int head, TSSymbol symbol, size_ return parent; } -static void reduce(TSParser *parser, int head, TSSymbol symbol, size_t child_count) { +static void reduce(TSParser *parser, int head, TSSymbol symbol, + size_t child_count) { reduce_helper(parser, head, symbol, child_count, false, false); } @@ -230,14 +236,17 @@ static void reduce_extra(TSParser *parser, int head, TSSymbol symbol) { reduce_helper(parser, head, symbol, 1, true, false); } -static void reduce_fragile(TSParser *parser, int head, TSSymbol symbol, size_t child_count) { - TSTree *reduced = reduce_helper(parser, head, symbol, child_count, false, false); +static void reduce_fragile(TSParser *parser, int head, TSSymbol symbol, + size_t child_count) { + TSTree *reduced = + reduce_helper(parser, head, symbol, child_count, false, false); ts_tree_set_fragile_left(reduced); ts_tree_set_fragile_right(reduced); } static void reduce_error(TSParser *parser, int head, size_t child_count) { - TSTree *reduced = reduce_helper(parser, head, ts_builtin_sym_error, child_count, false, true); + TSTree *reduced = + reduce_helper(parser, head, ts_builtin_sym_error, child_count, false, true); reduced->size = ts_length_add(reduced->size, parser->lookahead->padding); parser->lookahead->padding = ts_length_zero(); ts_tree_set_fragile_left(reduced); @@ -255,20 +264,20 @@ static bool handle_error(TSParser *parser, int head) { * expected and the current lookahead token is expected afterwards. */ int i = -1; - for (ParseStackEntry *entry = entry_before_error; - entry != NULL; + for (ParseStackEntry *entry = entry_before_error; entry != NULL; entry = ts_parse_stack_entry_next(entry, head), i++) { TSStateId stack_state = entry->state; - TSParseAction action_on_error = get_action( - parser->language, stack_state, ts_builtin_sym_error); + TSParseAction action_on_error = + get_action(parser->language, stack_state, ts_builtin_sym_error); if (action_on_error.type == TSParseActionTypeShift) { TSStateId state_after_error = action_on_error.data.to_state; TSParseAction action_after_error = get_action( - parser->language, state_after_error, parser->lookahead->symbol); + parser->language, state_after_error, parser->lookahead->symbol); if (action_after_error.type != TSParseActionTypeError) { - DEBUG("recover state:%u, count:%lu", state_after_error, error_token_count + i); + DEBUG("recover state:%u, count:%lu", state_after_error, + error_token_count + i); reduce_error(parser, head, error_token_count + i); return true; } @@ -303,14 +312,15 @@ static TSTree *finish(TSParser *parser) { * Public */ -TSTree * ts_parser_select_tree(void *data, TSTree *left, TSTree *right) { +TSTree *ts_parser_select_tree(void *data, TSTree *left, TSTree *right) { return left; } TSParser ts_parser_make() { - return (TSParser) { .lexer = ts_lexer_make(), - .stack = ts_parse_stack_new((TreeSelectionCallback) {NULL, ts_parser_select_tree}), - .right_stack = ts_stack_make() }; + return (TSParser){.lexer = ts_lexer_make(), + .stack = ts_parse_stack_new( + (TreeSelectionCallback){ NULL, ts_parser_select_tree }), + .right_stack = ts_stack_make() }; } void ts_parser_destroy(TSParser *parser) { @@ -343,7 +353,8 @@ typedef enum { ParserNextResult ts_parser_next(TSParser *parser, int head_to_advance) { TSStateId state = ts_parse_stack_top_state(parser->stack, head_to_advance); - const TSParseAction *next_action = get_actions(parser->language, state, parser->lookahead->symbol); + const TSParseAction *next_action = + get_actions(parser->language, state, parser->lookahead->symbol); int head, next_head = head_to_advance; ParserNextResult result = ParserNextResultNone; @@ -394,7 +405,8 @@ ParserNextResult ts_parser_next(TSParser *parser, int head_to_advance) { break; case TSParseActionTypeReduce: - DEBUG("reduce sym:%s, count:%u", SYM_NAME(action.data.symbol), action.data.child_count); + DEBUG("reduce sym:%s, count:%u", SYM_NAME(action.data.symbol), + action.data.child_count); reduce(parser, head, action.data.symbol, action.data.child_count); break; @@ -404,8 +416,10 @@ ParserNextResult ts_parser_next(TSParser *parser, int head_to_advance) { break; case TSParseActionTypeReduceFragile: - DEBUG("reduce_fragile sym:%s, count:%u", SYM_NAME(action.data.symbol), action.data.child_count); - reduce_fragile(parser, head, action.data.symbol, action.data.child_count); + DEBUG("reduce_fragile sym:%s, count:%u", SYM_NAME(action.data.symbol), + action.data.child_count); + reduce_fragile(parser, head, action.data.symbol, + action.data.child_count); break; case TSParseActionTypeAccept: @@ -435,7 +449,8 @@ const TSTree *ts_parser_parse(TSParser *parser, TSInput input, for (;;) { TSStateId state = ts_parse_stack_top_state(parser->stack, 0); - parser->lookahead = get_next_node(parser, parser->language->lex_states[state]); + parser->lookahead = + get_next_node(parser, parser->language->lex_states[state]); DEBUG("lookahead sym:%s", SYM_NAME(parser->lookahead->symbol)); DEBUG("head_count: %d", ts_parse_stack_head_count(parser->stack)); diff --git a/src/runtime/stack.c b/src/runtime/stack.c index 72e90e71..dce2b222 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -7,9 +7,9 @@ static size_t INITIAL_SIZE = 100; static TSStateId INITIAL_STATE = 0; TSStack ts_stack_make() { - return (TSStack) { .size = 0, - .capacity = INITIAL_SIZE, - .entries = malloc(INITIAL_SIZE * sizeof(TSStackEntry)) }; + return (TSStack){.size = 0, + .capacity = INITIAL_SIZE, + .entries = malloc(INITIAL_SIZE * sizeof(TSStackEntry)) }; } void ts_stack_delete(TSStack *stack) { @@ -33,7 +33,7 @@ void ts_stack_push(TSStack *stack, TSStateId state, TSTree *node) { if (stack->size == stack->capacity) { stack->capacity *= 2; stack->entries = - realloc(stack->entries, stack->capacity * sizeof(*stack->entries)); + realloc(stack->entries, stack->capacity * sizeof(*stack->entries)); } stack->entries[stack->size].state = state; stack->entries[stack->size].node = node; diff --git a/src/runtime/string_input.c b/src/runtime/string_input.c index ac8d0ba9..80ce85ab 100644 --- a/src/runtime/string_input.c +++ b/src/runtime/string_input.c @@ -30,8 +30,8 @@ TSInput ts_string_input_make(const char *string) { data->string = string; data->position = 0; data->length = strlen(string); - return (TSInput) { .data = (void *)data, - .read_fn = ts_string_input_read, - .seek_fn = ts_string_input_seek, - .release_fn = free }; + return (TSInput){.data = (void *)data, + .read_fn = ts_string_input_read, + .seek_fn = ts_string_input_seek, + .release_fn = free }; } diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 9ca837a9..4cdd2fd6 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -9,13 +9,15 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength size, TSLength padding, bool is_hidden) { TSTree *result = malloc(sizeof(TSTree)); - *result = (TSTree) { .ref_count = 1, - .symbol = sym, - .size = size, - .child_count = 0, - .children = NULL, - .padding = padding, - .options = is_hidden ? TSTreeOptionsHidden : 0, }; + *result = (TSTree){ + .ref_count = 1, + .symbol = sym, + .size = size, + .child_count = 0, + .children = NULL, + .padding = padding, + .options = is_hidden ? TSTreeOptionsHidden : 0, + }; return result; } @@ -78,15 +80,15 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count, * performing a second allocation and storing an additional pointer. */ TSTree *result = - malloc(sizeof(TSTree) + (visible_child_count * sizeof(TSTreeChild))); - *result = (TSTree) { .ref_count = 1, - .symbol = symbol, - .children = children, - .child_count = child_count, - .visible_child_count = visible_child_count, - .size = size, - .padding = padding, - .options = options }; + malloc(sizeof(TSTree) + (visible_child_count * sizeof(TSTreeChild))); + *result = (TSTree){.ref_count = 1, + .symbol = symbol, + .children = children, + .child_count = child_count, + .visible_child_count = visible_child_count, + .size = size, + .padding = padding, + .options = options }; /* * Associate a relative offset with each of the visible child nodes, so that @@ -110,7 +112,7 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count, for (size_t j = 0; j < n; j++) { visible_children[vis_i].tree = grandchildren[j].tree; visible_children[vis_i].offset = - ts_length_add(offset, grandchildren[j].offset); + ts_length_add(offset, grandchildren[j].offset); vis_i++; } } @@ -147,7 +149,8 @@ TSLength ts_tree_total_size(const TSTree *tree) { bool ts_tree_eq(const TSTree *node1, const TSTree *node2) { if (node1) { - if (!node2) return false; + if (!node2) + return false; } else { return !node2; } diff --git a/src/runtime/tree_vector.h b/src/runtime/tree_vector.h index 84255191..af31c770 100644 --- a/src/runtime/tree_vector.h +++ b/src/runtime/tree_vector.h @@ -15,10 +15,8 @@ typedef struct { } TreeVector; static inline TreeVector tree_vector_new(size_t size) { - return (TreeVector) { - .contents = malloc(size * sizeof(TSTree *)), - .capacity = size, - .size = 0, + return (TreeVector){ + .contents = malloc(size * sizeof(TSTree *)), .capacity = size, .size = 0, }; } @@ -42,12 +40,9 @@ static inline void tree_vector_reverse(TreeVector *this) { } static inline TreeVector tree_vector_copy(TreeVector *this) { - return (TreeVector) { - .contents = memcpy( - malloc(this->capacity * sizeof(TSTree *)), - this->contents, - this->size * sizeof(TSTree *) - ), + return (TreeVector){ + .contents = memcpy(malloc(this->capacity * sizeof(TSTree *)), + this->contents, this->size * sizeof(TSTree *)), .capacity = this->capacity, .size = this->size, };