Auto-format: no single-line functions

This commit is contained in:
Max Brunsfeld 2015-07-31 16:32:24 -07:00
parent e89ec7c85e
commit c18351772a
24 changed files with 243 additions and 77 deletions

View file

@ -9,7 +9,7 @@ AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None

View file

@ -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);

View file

@ -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

View file

@ -16,9 +16,13 @@ using std::set;
class CanBeBlank : public rules::RuleFn<bool> {
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<bool> {
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 {

View file

@ -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,

View file

@ -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("#<grammar");
@ -78,6 +80,8 @@ Grammar &Grammar::expected_conflicts(const set<set<string>> &expected_conflicts)
return *this;
}
const vector<pair<string, rule_ptr>> &Grammar::rules() const { return rules_; }
const vector<pair<string, rule_ptr>> &Grammar::rules() const {
return rules_;
}
} // namespace tree_sitter

View file

@ -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;

View file

@ -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<Symbol, Symbol> replacements;
@ -98,7 +100,6 @@ static const GrammarError *ubiq_token_err(const string &msg) {
"Not a token: " + msg);
}
tuple<SyntaxGrammar, LexicalGrammar, const GrammarError *> extract_tokens(
const InternedGrammar &grammar) {
SyntaxGrammar syntax_grammar;
@ -108,14 +109,16 @@ tuple<SyntaxGrammar, LexicalGrammar, const GrammarError *> extract_tokens(
vector<pair<string, rule_ptr>> 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<const Symbol>(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),

View file

@ -9,14 +9,20 @@ namespace tree_sitter {
namespace prepare_grammar {
class IsToken : public rules::RuleFn<bool> {
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

View file

@ -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<rule_ptr, const GrammarError *> error(string msg) {
return { blank(), new GrammarError(GrammarErrorTypeRegex, msg) };

View file

@ -22,7 +22,9 @@ class TokenDescription : public rules::RuleFn<string> {
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) + ")";

View file

@ -11,13 +11,21 @@ bool Blank::operator==(const Rule &rule) const {
return dynamic_cast<const Blank *>(&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<Blank>(); }
rule_ptr Blank::copy() const {
return std::make_shared<Blank>();
}
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

View file

@ -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

View file

@ -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<CharacterRange> 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

View file

@ -57,7 +57,9 @@ size_t Choice::hash_code() const {
return result;
}
rule_ptr Choice::copy() const { return std::make_shared<Choice>(*this); }
rule_ptr Choice::copy() const {
return std::make_shared<Choice>(*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

View file

@ -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

View file

@ -15,15 +15,21 @@ bool NamedSymbol::operator==(const Rule &rule) const {
return other && other->name == name;
}
size_t NamedSymbol::hash_code() const { return hash<string>()(name); }
size_t NamedSymbol::hash_code() const {
return hash<string>()(name);
}
rule_ptr NamedSymbol::copy() const {
return std::make_shared<NamedSymbol>(*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

View file

@ -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<string>()(value); }
size_t Pattern::hash_code() const {
return hash<string>()(value);
}
rule_ptr Pattern::copy() const { return std::make_shared<Pattern>(*this); }
rule_ptr Pattern::copy() const {
return std::make_shared<Pattern>(*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

View file

@ -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<Repeat>(*this); }
rule_ptr Repeat::copy() const {
return make_shared<Repeat>(*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

View file

@ -28,23 +28,37 @@ static rule_ptr metadata(rule_ptr rule, map<MetadataKey, int> values) {
return std::make_shared<Metadata>(rule, values);
}
rule_ptr blank() { return make_shared<Blank>(); }
rule_ptr blank() {
return make_shared<Blank>();
}
rule_ptr choice(const vector<rule_ptr> &rules) { return Choice::build(rules); }
rule_ptr choice(const vector<rule_ptr> &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<rule_ptr> &rules) { return Seq::build(rules); }
rule_ptr seq(const vector<rule_ptr> &rules) {
return Seq::build(rules);
}
rule_ptr sym(const string &name) { return make_shared<NamedSymbol>(name); }
rule_ptr sym(const string &name) {
return make_shared<NamedSymbol>(name);
}
rule_ptr pattern(const string &value) { return make_shared<Pattern>(value); }
rule_ptr pattern(const string &value) {
return make_shared<Pattern>(value);
}
rule_ptr str(const string &value) {
return token(prec(1, make_shared<String>(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(

View file

@ -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<Seq>(*this); }
rule_ptr Seq::copy() const {
return std::make_shared<Seq>(*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

View file

@ -15,13 +15,21 @@ bool String::operator==(const Rule &rule) const {
return other && (other->value == value);
}
size_t String::hash_code() const { return hash<string>()(value); }
size_t String::hash_code() const {
return hash<string>()(value);
}
rule_ptr String::copy() const { return std::make_shared<String>(*this); }
rule_ptr String::copy() const {
return std::make_shared<String>(*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

View file

@ -31,7 +31,9 @@ size_t Symbol::hash_code() const {
return hash<int>()(index) ^ hash<int16_t>()(options);
}
rule_ptr Symbol::copy() const { return std::make_shared<Symbol>(*this); }
rule_ptr Symbol::copy() const {
return std::make_shared<Symbol>(*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

View file

@ -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_;