Trim whitespace

This commit is contained in:
Max Brunsfeld 2014-04-25 22:17:23 -07:00
parent 801f4bd0a8
commit 93df5579b4
15 changed files with 78 additions and 78 deletions

View file

@ -83,11 +83,11 @@ namespace tree_sitter {
}
private:
const PreparedGrammar & grammar_for_symbol(const rules::ISymbol &symbol) {
return symbol.is_token() ? lexical_grammar : syntax_grammar;
}
string symbol_id(const rules::ISymbol &symbol) {
if (symbol.is_built_in()) {
return (symbol == rules::ERROR()) ?
@ -101,7 +101,7 @@ namespace tree_sitter {
return "ts_sym_" + name;
}
}
string symbol_name(const rules::ISymbol &symbol) {
if (symbol.is_built_in()) {
return (symbol == rules::ERROR()) ? "error" : "end";

View file

@ -8,7 +8,7 @@
namespace tree_sitter {
class PreparedGrammar;
namespace generate_code {
std::string c_code(std::string name,
const ParseTable &parse_table,

View file

@ -6,18 +6,18 @@
namespace tree_sitter {
using std::string;
namespace generate_code {
class TokenDescription : public rules::RuleFn<string> {
string apply_to(const rules::Pattern *rule) {
return "/" + rule->value + "/";
}
string apply_to(const rules::String *rule) {
return "'" + rule->value + "'";
}
};
std::string token_description(const rules::rule_ptr &rule) {
return TokenDescription().apply(rule);
}

View file

@ -29,20 +29,20 @@ namespace tree_sitter {
bool apply_to(const rules::String *rule) { return true; }
bool apply_to(const rules::Pattern *rule) { return true; }
};
class SymbolInliner : public rules::IdentityRuleFn {
map<ISymbol, ISymbol> replacements;
using rules::IdentityRuleFn::apply_to;
int new_index_for_symbol(const ISymbol &symbol) {
int result = symbol.index;
for (const auto &pair : replacements)
if (pair.first.index < symbol.index &&
if (pair.first.index < symbol.index &&
pair.first.is_auxiliary() == symbol.is_auxiliary())
result--;
return result;
}
rule_ptr apply_to(const ISymbol *rule) {
auto replacement_pair = replacements.find(*rule);
if (replacement_pair != replacements.end())
@ -52,7 +52,7 @@ namespace tree_sitter {
else
return make_shared<ISymbol>(new_index_for_symbol(*rule), rule->options);
}
public:
SymbolInliner(const map<ISymbol, ISymbol> &replacements, size_t rule_count, size_t aux_rule_count) :
replacements(replacements)
@ -78,7 +78,7 @@ namespace tree_sitter {
return result;
}
}
public:
vector<pair<string, rule_ptr>> tokens;
};

View file

@ -15,23 +15,23 @@ namespace tree_sitter {
using std::exception;
GrammarError::GrammarError(string rule_name) : rule_name(rule_name) {}
string GrammarError::message() const {
return "Undefined rule '" + rule_name + "'";
}
namespace prepare_grammar {
class InternSymbols : public rules::IdentityRuleFn {
const Grammar grammar;
using rules::IdentityRuleFn::apply_to;
long index_of(string rule_name) {
for (size_t i = 0; i < grammar.rules.size(); i++)
if (grammar.rules[i].first == rule_name)
return i;
return -1;
}
rule_ptr apply_to(const rules::Symbol *rule) {
long index = index_of(rule->name);
if (index == -1)
@ -44,7 +44,7 @@ namespace tree_sitter {
string missing_rule_name;
};
pair<PreparedGrammar, const GrammarError *> intern_symbols(const Grammar &grammar) {
InternSymbols interner(grammar);
vector<pair<string, rule_ptr>> rules;
@ -58,7 +58,7 @@ namespace tree_sitter {
};
rules.push_back({ pair.first, new_rule });
}
return { PreparedGrammar(rules), nullptr };
}
}

View file

@ -7,7 +7,7 @@
namespace tree_sitter {
class Grammar;
class PreparedGrammar;
class GrammarError {
std::string rule_name;
public:

View file

@ -22,13 +22,13 @@ namespace tree_sitter {
const rule_ptr & PreparedGrammar::rule(const ISymbol &symbol) const {
return symbol.is_auxiliary() ?
aux_rules[symbol.index].second :
aux_rules[symbol.index].second :
rules[symbol.index].second;
}
const string & PreparedGrammar::rule_name(const ISymbol &symbol) const {
return symbol.is_auxiliary() ?
aux_rules[symbol.index].first :
aux_rules[symbol.index].first :
rules[symbol.index].first;
}

View file

@ -7,16 +7,16 @@ namespace tree_sitter {
using std::string;
using std::to_string;
using std::hash;
namespace rules {
ISymbol::ISymbol(int index) :
index(index),
options(SymbolOption(0)) {}
ISymbol::ISymbol(int index, SymbolOption options) :
index(index),
options(options) {}
bool ISymbol::operator==(const ISymbol &other) const {
return (other.index == index) && (other.options == options);
}
@ -25,39 +25,39 @@ namespace tree_sitter {
const ISymbol *other = dynamic_cast<const ISymbol *>(&rule);
return other && this->operator==(*other);
}
size_t ISymbol::hash_code() const {
return hash<int>()(index) ^ hash<int16_t>()(options);
}
rule_ptr ISymbol::copy() const {
return std::make_shared<ISymbol>(*this);
}
string ISymbol::to_string() const {
string name = (options & SymbolOptionAuxiliary) ? "aux_" : "";
name += (options & SymbolOptionToken) ? "token" : "sym";
return "#<" + name + std::to_string(index) + ">";
}
bool ISymbol::operator<(const ISymbol &other) const {
if (options < other.options) return true;
if (options > other.options) return false;
return (index < other.index);
}
bool ISymbol::is_token() const {
return options & SymbolOptionToken;
}
bool ISymbol::is_built_in() const {
return index < 0;
}
bool ISymbol::is_auxiliary() const {
return options & SymbolOptionAuxiliary;
}
void ISymbol::accept(Visitor *visitor) const {
visitor->visit(this);
}

View file

@ -9,25 +9,25 @@ namespace tree_sitter {
SymbolOptionToken = 1 << 0,
SymbolOptionAuxiliary = 1 << 1,
} SymbolOption;
class ISymbol : public Rule {
public:
explicit ISymbol(int index);
ISymbol(int index, SymbolOption options);
bool operator==(const ISymbol &other) const;
bool operator==(const Rule &other) const;
size_t hash_code() const;
rule_ptr copy() const;
std::string to_string() const;
void accept(Visitor *visitor) const;
bool operator<(const ISymbol &other) const;
bool is_token() const;
bool is_built_in() const;
bool is_auxiliary() const;
int index;
SymbolOption options;
};