diff --git a/.clang-format b/.clang-format index 8d2eba22..633dfd06 100644 --- a/.clang-format +++ b/.clang-format @@ -9,7 +9,7 @@ AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: true AllowShortBlocksOnASingleLine: false AllowShortCaseLabelsOnASingleLine: false -AllowShortFunctionsOnASingleLine: All +AllowShortFunctionsOnASingleLine: Empty AllowShortIfStatementsOnASingleLine: false AllowShortLoopsOnASingleLine: false AlwaysBreakAfterDefinitionReturnType: None diff --git a/src/compiler/build_tables/get_metadata.cc b/src/compiler/build_tables/get_metadata.cc index da49d103..5c051e0e 100644 --- a/src/compiler/build_tables/get_metadata.cc +++ b/src/compiler/build_tables/get_metadata.cc @@ -21,7 +21,9 @@ int get_metadata(const rules::rule_ptr &rule, rules::MetadataKey key) { // TODO - // Remove this. It is currently needed to make the rule generated // by `LexTableBuilder::after_separators` have the right precedence. - int apply_to(const rules::Seq *rule) { return apply(rule->left); } + int apply_to(const rules::Seq *rule) { + return apply(rule->left); + } }; return GetMetadata(key).apply(rule); diff --git a/src/compiler/build_tables/item.cc b/src/compiler/build_tables/item.cc index 85d2f2eb..155d19b7 100644 --- a/src/compiler/build_tables/item.cc +++ b/src/compiler/build_tables/item.cc @@ -10,9 +10,13 @@ namespace build_tables { Item::Item(const rules::Symbol &lhs, const rules::rule_ptr rule) : lhs(lhs), rule(rule) {} -bool Item::is_done() const { return rule_can_be_blank(rule); } +bool Item::is_done() const { + return rule_can_be_blank(rule); +} -int Item::precedence() const { return get_metadata(rule, rules::PRECEDENCE); } +int Item::precedence() const { + return get_metadata(rule, rules::PRECEDENCE); +} } // namespace build_tables } // namespace tree_sitter diff --git a/src/compiler/build_tables/rule_can_be_blank.cc b/src/compiler/build_tables/rule_can_be_blank.cc index 687bbeb6..88a3460d 100644 --- a/src/compiler/build_tables/rule_can_be_blank.cc +++ b/src/compiler/build_tables/rule_can_be_blank.cc @@ -16,9 +16,13 @@ using std::set; class CanBeBlank : public rules::RuleFn { protected: - bool apply_to(const rules::Blank *) { return true; } + bool apply_to(const rules::Blank *) { + return true; + } - bool apply_to(const rules::Repeat *rule) { return true; } + bool apply_to(const rules::Repeat *rule) { + return true; + } bool apply_to(const rules::Choice *rule) { for (const auto &element : rule->elements) @@ -31,7 +35,9 @@ class CanBeBlank : public rules::RuleFn { return apply(rule->left) && apply(rule->right); } - bool apply_to(const rules::Metadata *rule) { return apply(rule->rule); } + bool apply_to(const rules::Metadata *rule) { + return apply(rule->rule); + } }; class CanBeBlankRecursive : public CanBeBlank { diff --git a/src/compiler/generate_code/c_code.cc b/src/compiler/generate_code/c_code.cc index 639c9304..50204c25 100644 --- a/src/compiler/generate_code/c_code.cc +++ b/src/compiler/generate_code/c_code.cc @@ -315,7 +315,9 @@ class CCodeGenerator { // Helper functions - string lex_state_index(size_t i) { return to_string(i + 1); } + string lex_state_index(size_t i) { + return to_string(i + 1); + } string symbol_id(const rules::Symbol &symbol) { if (symbol.is_built_in()) { @@ -429,7 +431,9 @@ class CCodeGenerator { // General code generation functions - void line() { line(""); } + void line() { + line(""); + } void line(string input) { add("\n"); @@ -450,7 +454,9 @@ class CCodeGenerator { indent_level--; } - void add(string input) { buffer += input; } + void add(string input) { + buffer += input; + } }; string c_code(string name, const ParseTable &parse_table, diff --git a/src/compiler/grammar.cc b/src/compiler/grammar.cc index 0376d77d..9cd1a555 100644 --- a/src/compiler/grammar.cc +++ b/src/compiler/grammar.cc @@ -29,7 +29,9 @@ bool Grammar::operator==(const Grammar &other) const { return true; } -string Grammar::start_rule_name() const { return rules_.front().first; } +string Grammar::start_rule_name() const { + return rules_.front().first; +} ostream &operator<<(ostream &stream, const Grammar &grammar) { stream << string("#> &expected_conflicts) return *this; } -const vector> &Grammar::rules() const { return rules_; } +const vector> &Grammar::rules() const { + return rules_; +} } // namespace tree_sitter diff --git a/src/compiler/parse_table.cc b/src/compiler/parse_table.cc index 9e3372e9..9970be46 100644 --- a/src/compiler/parse_table.cc +++ b/src/compiler/parse_table.cc @@ -29,7 +29,9 @@ ParseAction::ParseAction() consumed_symbol_count(0), associativity(rules::AssociativityUnspecified) {} -ParseAction ParseAction::Error() { return ParseAction(); } +ParseAction ParseAction::Error() { + return ParseAction(); +} ParseAction ParseAction::Accept() { ParseAction action; diff --git a/src/compiler/prepare_grammar/extract_tokens.cc b/src/compiler/prepare_grammar/extract_tokens.cc index 28a91cc8..ef94ea96 100644 --- a/src/compiler/prepare_grammar/extract_tokens.cc +++ b/src/compiler/prepare_grammar/extract_tokens.cc @@ -43,7 +43,9 @@ class SymbolReplacer : public rules::IdentityRuleFn { return result; } - rule_ptr apply_to(const Symbol *rule) { return replace_symbol(*rule).copy(); } + rule_ptr apply_to(const Symbol *rule) { + return replace_symbol(*rule).copy(); + } public: map replacements; @@ -98,7 +100,6 @@ static const GrammarError *ubiq_token_err(const string &msg) { "Not a token: " + msg); } - tuple extract_tokens( const InternedGrammar &grammar) { SyntaxGrammar syntax_grammar; @@ -108,14 +109,16 @@ tuple extract_tokens( vector> extracted_rules; for (auto &pair : grammar.rules) - extracted_rules.push_back({pair.first, extractor.apply(pair.second)}); + extracted_rules.push_back({ pair.first, extractor.apply(pair.second) }); size_t i = 0; for (auto &pair : extracted_rules) { auto &rule = pair.second; auto symbol = dynamic_pointer_cast(rule); - if (symbol.get() && symbol->is_auxiliary() && extractor.token_usage_counts[symbol->index] == 1) { - lexical_grammar.rules.push_back({pair.first, extractor.tokens[symbol->index].second}); + if (symbol.get() && symbol->is_auxiliary() && + extractor.token_usage_counts[symbol->index] == 1) { + lexical_grammar.rules.push_back( + { pair.first, extractor.tokens[symbol->index].second }); extractor.token_usage_counts[symbol->index] = 0; symbol_replacer.replacements.insert( { Symbol(i), diff --git a/src/compiler/prepare_grammar/is_token.cc b/src/compiler/prepare_grammar/is_token.cc index b7d14a7e..488d49d0 100644 --- a/src/compiler/prepare_grammar/is_token.cc +++ b/src/compiler/prepare_grammar/is_token.cc @@ -9,14 +9,20 @@ namespace tree_sitter { namespace prepare_grammar { class IsToken : public rules::RuleFn { - bool apply_to(const rules::String *rule) { return true; } - bool apply_to(const rules::Pattern *rule) { return true; } + bool apply_to(const rules::String *rule) { + return true; + } + bool apply_to(const rules::Pattern *rule) { + return true; + } bool apply_to(const rules::Metadata *rule) { return rule->value_for(rules::IS_TOKEN); } }; -bool is_token(const rules::rule_ptr &rule) { return IsToken().apply(rule); } +bool is_token(const rules::rule_ptr &rule) { + return IsToken().apply(rule); +} } // namespace prepare_grammar } // namespace tree_sitter diff --git a/src/compiler/prepare_grammar/parse_regex.cc b/src/compiler/prepare_grammar/parse_regex.cc index 4ff0d99b..823f7bb5 100644 --- a/src/compiler/prepare_grammar/parse_regex.cc +++ b/src/compiler/prepare_grammar/parse_regex.cc @@ -211,9 +211,13 @@ class PatternParser { iter += lookahead_size; } - uint32_t peek() { return lookahead; } + uint32_t peek() { + return lookahead; + } - bool has_more_input() { return lookahead && iter <= end; } + bool has_more_input() { + return lookahead && iter <= end; + } pair error(string msg) { return { blank(), new GrammarError(GrammarErrorTypeRegex, msg) }; diff --git a/src/compiler/prepare_grammar/token_description.cc b/src/compiler/prepare_grammar/token_description.cc index 54c00562..19877708 100644 --- a/src/compiler/prepare_grammar/token_description.cc +++ b/src/compiler/prepare_grammar/token_description.cc @@ -22,7 +22,9 @@ class TokenDescription : public rules::RuleFn { return "STR_" + util::escape_string(rule->value); } - string apply_to(const rules::Metadata *rule) { return apply(rule->rule); } + string apply_to(const rules::Metadata *rule) { + return apply(rule->rule); + } string apply_to(const rules::Seq *rule) { return "(seq " + apply(rule->left) + " " + apply(rule->right) + ")"; diff --git a/src/compiler/rules/blank.cc b/src/compiler/rules/blank.cc index 64a5d891..4281eba6 100644 --- a/src/compiler/rules/blank.cc +++ b/src/compiler/rules/blank.cc @@ -11,13 +11,21 @@ bool Blank::operator==(const Rule &rule) const { return dynamic_cast(&rule) != nullptr; } -size_t Blank::hash_code() const { return 0; } +size_t Blank::hash_code() const { + return 0; +} -rule_ptr Blank::copy() const { return std::make_shared(); } +rule_ptr Blank::copy() const { + return std::make_shared(); +} -std::string Blank::to_string() const { return "(blank)"; } +std::string Blank::to_string() const { + return "(blank)"; +} -void Blank::accept(Visitor *visitor) const { visitor->visit(this); } +void Blank::accept(Visitor *visitor) const { + visitor->visit(this); +} } // namespace rules } // namespace tree_sitter diff --git a/src/compiler/rules/built_in_symbols.cc b/src/compiler/rules/built_in_symbols.cc index 7a648a3d..92c14866 100644 --- a/src/compiler/rules/built_in_symbols.cc +++ b/src/compiler/rules/built_in_symbols.cc @@ -3,10 +3,21 @@ namespace tree_sitter { namespace rules { -Symbol END_OF_INPUT() { return Symbol(-1, SymbolOptionToken); } -Symbol ERROR() { return Symbol(-2, SymbolOptionToken); } -Symbol START() { return Symbol(-3); } -Symbol DOCUMENT() { return Symbol(-4); } +Symbol END_OF_INPUT() { + return Symbol(-1, SymbolOptionToken); +} + +Symbol ERROR() { + return Symbol(-2, SymbolOptionToken); +} + +Symbol START() { + return Symbol(-3); +} + +Symbol DOCUMENT() { + return Symbol(-4); +} } // namespace rules } // namespace tree_sitter diff --git a/src/compiler/rules/character_set.cc b/src/compiler/rules/character_set.cc index 3e50ed87..d18808ee 100644 --- a/src/compiler/rules/character_set.cc +++ b/src/compiler/rules/character_set.cc @@ -140,9 +140,13 @@ CharacterSet &CharacterSet::exclude(uint32_t min, uint32_t max) { return *this; } -CharacterSet &CharacterSet::include(uint32_t c) { return include(c, c); } +CharacterSet &CharacterSet::include(uint32_t c) { + return include(c, c); +} -CharacterSet &CharacterSet::exclude(uint32_t c) { return exclude(c, c); } +CharacterSet &CharacterSet::exclude(uint32_t c) { + return exclude(c, c); +} bool CharacterSet::is_empty() const { return !includes_all && included_chars.empty(); @@ -186,7 +190,9 @@ vector CharacterSet::excluded_ranges() const { return consolidate_ranges(excluded_chars); } -void CharacterSet::accept(Visitor *visitor) const { visitor->visit(this); } +void CharacterSet::accept(Visitor *visitor) const { + visitor->visit(this); +} } // namespace rules } // namespace tree_sitter diff --git a/src/compiler/rules/choice.cc b/src/compiler/rules/choice.cc index 15b3e76c..b4b08e9e 100644 --- a/src/compiler/rules/choice.cc +++ b/src/compiler/rules/choice.cc @@ -57,7 +57,9 @@ size_t Choice::hash_code() const { return result; } -rule_ptr Choice::copy() const { return std::make_shared(*this); } +rule_ptr Choice::copy() const { + return std::make_shared(*this); +} string Choice::to_string() const { string result = "(choice"; @@ -66,7 +68,9 @@ string Choice::to_string() const { return result + ")"; } -void Choice::accept(Visitor *visitor) const { visitor->visit(this); } +void Choice::accept(Visitor *visitor) const { + visitor->visit(this); +} } // namespace rules } // namespace tree_sitter diff --git a/src/compiler/rules/metadata.cc b/src/compiler/rules/metadata.cc index 17f61a7b..d3d56748 100644 --- a/src/compiler/rules/metadata.cc +++ b/src/compiler/rules/metadata.cc @@ -40,7 +40,9 @@ std::string Metadata::to_string() const { return "(metadata " + rule->to_string() + ")"; } -void Metadata::accept(Visitor *visitor) const { visitor->visit(this); } +void Metadata::accept(Visitor *visitor) const { + visitor->visit(this); +} } // namespace rules } // namespace tree_sitter diff --git a/src/compiler/rules/named_symbol.cc b/src/compiler/rules/named_symbol.cc index 8423fc9a..b5afe89f 100644 --- a/src/compiler/rules/named_symbol.cc +++ b/src/compiler/rules/named_symbol.cc @@ -15,15 +15,21 @@ bool NamedSymbol::operator==(const Rule &rule) const { return other && other->name == name; } -size_t NamedSymbol::hash_code() const { return hash()(name); } +size_t NamedSymbol::hash_code() const { + return hash()(name); +} rule_ptr NamedSymbol::copy() const { return std::make_shared(*this); } -string NamedSymbol::to_string() const { return string("(sym '") + name + "')"; } +string NamedSymbol::to_string() const { + return string("(sym '") + name + "')"; +} -void NamedSymbol::accept(Visitor *visitor) const { visitor->visit(this); } +void NamedSymbol::accept(Visitor *visitor) const { + visitor->visit(this); +} } // namespace rules } // namespace tree_sitter diff --git a/src/compiler/rules/pattern.cc b/src/compiler/rules/pattern.cc index 536391c9..1d6fff39 100644 --- a/src/compiler/rules/pattern.cc +++ b/src/compiler/rules/pattern.cc @@ -16,15 +16,21 @@ bool Pattern::operator==(tree_sitter::rules::Rule const &other) const { return pattern && (pattern->value == value); } -size_t Pattern::hash_code() const { return hash()(value); } +size_t Pattern::hash_code() const { + return hash()(value); +} -rule_ptr Pattern::copy() const { return std::make_shared(*this); } +rule_ptr Pattern::copy() const { + return std::make_shared(*this); +} string Pattern::to_string() const { return string("(pattern '") + util::escape_string(value) + "')"; } -void Pattern::accept(Visitor *visitor) const { visitor->visit(this); } +void Pattern::accept(Visitor *visitor) const { + visitor->visit(this); +} } // namespace rules } // namespace tree_sitter diff --git a/src/compiler/rules/repeat.cc b/src/compiler/rules/repeat.cc index 71fa32df..40f41ed3 100644 --- a/src/compiler/rules/repeat.cc +++ b/src/compiler/rules/repeat.cc @@ -25,15 +25,21 @@ bool Repeat::operator==(const Rule &rule) const { return other && (*other->content == *content); } -size_t Repeat::hash_code() const { return content->hash_code(); } +size_t Repeat::hash_code() const { + return content->hash_code(); +} -rule_ptr Repeat::copy() const { return make_shared(*this); } +rule_ptr Repeat::copy() const { + return make_shared(*this); +} string Repeat::to_string() const { return string("(repeat ") + content->to_string() + ")"; } -void Repeat::accept(Visitor *visitor) const { visitor->visit(this); } +void Repeat::accept(Visitor *visitor) const { + visitor->visit(this); +} } // namespace rules } // namespace tree_sitter diff --git a/src/compiler/rules/rules.cc b/src/compiler/rules/rules.cc index 8770e98c..89f9bf64 100644 --- a/src/compiler/rules/rules.cc +++ b/src/compiler/rules/rules.cc @@ -28,23 +28,37 @@ static rule_ptr metadata(rule_ptr rule, map values) { return std::make_shared(rule, values); } -rule_ptr blank() { return make_shared(); } +rule_ptr blank() { + return make_shared(); +} -rule_ptr choice(const vector &rules) { return Choice::build(rules); } +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); } +rule_ptr seq(const vector &rules) { + return Seq::build(rules); +} -rule_ptr sym(const string &name) { return make_shared(name); } +rule_ptr sym(const string &name) { + return make_shared(name); +} -rule_ptr pattern(const string &value) { return make_shared(value); } +rule_ptr pattern(const string &value) { + return make_shared(value); +} 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( diff --git a/src/compiler/rules/seq.cc b/src/compiler/rules/seq.cc index bd5aa06b..54dc5821 100644 --- a/src/compiler/rules/seq.cc +++ b/src/compiler/rules/seq.cc @@ -25,15 +25,21 @@ bool Seq::operator==(const Rule &rule) const { return other && (*other->left == *left) && (*other->right == *right); } -size_t Seq::hash_code() const { return left->hash_code() ^ right->hash_code(); } +size_t Seq::hash_code() const { + return left->hash_code() ^ right->hash_code(); +} -rule_ptr Seq::copy() const { return std::make_shared(*this); } +rule_ptr Seq::copy() const { + return std::make_shared(*this); +} string Seq::to_string() const { return string("(seq ") + left->to_string() + " " + right->to_string() + ")"; } -void Seq::accept(Visitor *visitor) const { visitor->visit(this); } +void Seq::accept(Visitor *visitor) const { + visitor->visit(this); +} } // namespace rules } // namespace tree_sitter diff --git a/src/compiler/rules/string.cc b/src/compiler/rules/string.cc index 250b70cb..075a4267 100644 --- a/src/compiler/rules/string.cc +++ b/src/compiler/rules/string.cc @@ -15,13 +15,21 @@ bool String::operator==(const Rule &rule) const { return other && (other->value == value); } -size_t String::hash_code() const { return hash()(value); } +size_t String::hash_code() const { + return hash()(value); +} -rule_ptr String::copy() const { return std::make_shared(*this); } +rule_ptr String::copy() const { + return std::make_shared(*this); +} -string String::to_string() const { return string("(string '") + value + "')"; } +string String::to_string() const { + return string("(string '") + value + "')"; +} -void String::accept(Visitor *visitor) const { visitor->visit(this); } +void String::accept(Visitor *visitor) const { + visitor->visit(this); +} } // namespace rules } // namespace tree_sitter diff --git a/src/compiler/rules/symbol.cc b/src/compiler/rules/symbol.cc index ac7b5986..b9f213f2 100644 --- a/src/compiler/rules/symbol.cc +++ b/src/compiler/rules/symbol.cc @@ -31,7 +31,9 @@ size_t Symbol::hash_code() const { return hash()(index) ^ hash()(options); } -rule_ptr Symbol::copy() const { return std::make_shared(*this); } +rule_ptr Symbol::copy() const { + return std::make_shared(*this); +} string Symbol::to_string() const { string name = (options & SymbolOptionAuxiliary) ? "aux_" : ""; @@ -47,13 +49,21 @@ bool Symbol::operator<(const Symbol &other) const { return (index < other.index); } -bool Symbol::is_token() const { return options & SymbolOptionToken; } +bool Symbol::is_token() const { + return options & SymbolOptionToken; +} -bool Symbol::is_built_in() const { return index < 0; } +bool Symbol::is_built_in() const { + return index < 0; +} -bool Symbol::is_auxiliary() const { return options & SymbolOptionAuxiliary; } +bool Symbol::is_auxiliary() const { + return options & SymbolOptionAuxiliary; +} -void Symbol::accept(Visitor *visitor) const { visitor->visit(this); } +void Symbol::accept(Visitor *visitor) const { + visitor->visit(this); +} } // namespace rules } // namespace tree_sitter diff --git a/src/compiler/rules/visitor.h b/src/compiler/rules/visitor.h index 66dfd4b2..b1331b48 100644 --- a/src/compiler/rules/visitor.h +++ b/src/compiler/rules/visitor.h @@ -42,49 +42,89 @@ class RuleFn : private Visitor { } protected: - virtual T default_apply(const Rule *rule) { return T(); } + virtual T default_apply(const Rule *rule) { + return T(); + } virtual T apply_to(const Blank *rule) { return default_apply((const Rule *)rule); } + virtual T apply_to(const CharacterSet *rule) { return default_apply((const Rule *)rule); } + virtual T apply_to(const Choice *rule) { return default_apply((const Rule *)rule); } + virtual T apply_to(const Metadata *rule) { return default_apply((const Rule *)rule); } + virtual T apply_to(const Pattern *rule) { return default_apply((const Rule *)rule); } + virtual T apply_to(const Repeat *rule) { return default_apply((const Rule *)rule); } + virtual T apply_to(const Seq *rule) { return default_apply((const Rule *)rule); } + virtual T apply_to(const String *rule) { return default_apply((const Rule *)rule); } + virtual T apply_to(const NamedSymbol *rule) { return default_apply((const Rule *)rule); } + virtual T apply_to(const Symbol *rule) { return default_apply((const Rule *)rule); } - void visit(const Blank *rule) { value_ = apply_to(rule); } - void visit(const CharacterSet *rule) { value_ = apply_to(rule); } - void visit(const Choice *rule) { value_ = apply_to(rule); } - void visit(const Metadata *rule) { value_ = apply_to(rule); } - void visit(const Pattern *rule) { value_ = apply_to(rule); } - void visit(const Repeat *rule) { value_ = apply_to(rule); } - void visit(const Seq *rule) { value_ = apply_to(rule); } - void visit(const String *rule) { value_ = apply_to(rule); } - void visit(const NamedSymbol *rule) { value_ = apply_to(rule); } - void visit(const Symbol *rule) { value_ = apply_to(rule); } + void visit(const Blank *rule) { + value_ = apply_to(rule); + } + + void visit(const CharacterSet *rule) { + value_ = apply_to(rule); + } + + void visit(const Choice *rule) { + value_ = apply_to(rule); + } + + void visit(const Metadata *rule) { + value_ = apply_to(rule); + } + + void visit(const Pattern *rule) { + value_ = apply_to(rule); + } + + void visit(const Repeat *rule) { + value_ = apply_to(rule); + } + + void visit(const Seq *rule) { + value_ = apply_to(rule); + } + + void visit(const String *rule) { + value_ = apply_to(rule); + } + + void visit(const NamedSymbol *rule) { + value_ = apply_to(rule); + } + + void visit(const Symbol *rule) { + value_ = apply_to(rule); + } private: T value_;