tree-sitter/src/compiler/rules/rules.cc

71 lines
1.8 KiB
C++
Raw Normal View History

2014-04-14 08:38:11 -07:00
#include <vector>
#include <map>
2014-04-14 08:38:11 -07:00
#include <set>
#include <string>
#include "tree_sitter/compiler.h"
2014-03-09 21:37:21 -07:00
#include "compiler/rules/rule.h"
#include "compiler/rules/blank.h"
2014-04-28 20:15:49 -07:00
#include "compiler/rules/named_symbol.h"
2014-03-09 21:37:21 -07:00
#include "compiler/rules/choice.h"
#include "compiler/rules/seq.h"
#include "compiler/rules/string.h"
#include "compiler/rules/metadata.h"
2014-03-09 21:37:21 -07:00
#include "compiler/rules/pattern.h"
#include "compiler/rules/character_set.h"
#include "compiler/rules/repeat.h"
#include "compiler/rules/built_in_symbols.h"
namespace tree_sitter {
namespace rules {
using std::make_shared;
using std::string;
using std::set;
using std::vector;
using std::map;
static const int KEYWORD_PRECEDENCE = 100;
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 choice(const vector<rule_ptr> &rules) { return Choice::build(rules); }
rule_ptr repeat(const rule_ptr &content) {
return std::make_shared<Repeat>(content);
}
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 pattern(const string &value) { return make_shared<Pattern>(value); }
rule_ptr str(const string &value) { return make_shared<String>(value); }
rule_ptr keyword(const string &value) {
return token(prec(KEYWORD_PRECEDENCE, str(value)));
}
rule_ptr keypattern(const string &value) {
return token(prec(KEYWORD_PRECEDENCE, pattern(value)));
}
rule_ptr err(const rule_ptr &rule) {
return choice({ rule, ERROR().copy() });
}
rule_ptr prec(int precedence, rule_ptr rule) {
return metadata(rule, { { PRECEDENCE, precedence } });
}
rule_ptr token(rule_ptr rule) {
return metadata(rule, { { IS_TOKEN, 1 } });
}
} // namespace rules
} // namespace tree_sitter