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

81 lines
2.1 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 {
using std::make_shared;
using std::string;
using std::set;
using std::vector;
using std::map;
2014-03-09 19:49:35 -07:00
namespace rules {
static const int KEYWORD_PRECEDENCE = 100;
2014-06-16 21:33:35 -07:00
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>();
}
2014-03-09 19:49:35 -07:00
rule_ptr choice(const vector<rule_ptr> &rules) {
return Choice::Build(rules);
}
2014-03-09 19:49:35 -07:00
2014-02-21 08:23:20 -08:00
rule_ptr repeat(const rule_ptr &content) {
return std::make_shared<Repeat>(content);
}
2014-03-09 19:49:35 -07:00
rule_ptr seq(const vector<rule_ptr> &rules) {
return Seq::Build(rules);
}
2014-02-11 13:15:44 -08:00
rule_ptr sym(const string &name) {
2014-04-28 20:15:49 -07:00
return make_shared<NamedSymbol>(name);
}
2014-02-21 08:23:20 -08:00
rule_ptr pattern(const string &value) {
return make_shared<Pattern>(value);
}
2014-03-09 19:49:35 -07:00
2014-02-21 08:23:20 -08:00
rule_ptr str(const string &value) {
return make_shared<String>(value);
}
2014-03-09 19:49:35 -07:00
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)));
}
2014-05-01 23:28:40 -07:00
2014-02-26 19:03:43 -08:00
rule_ptr err(const rule_ptr &rule) {
2014-03-28 13:51:32 -07:00
return choice({ rule, ERROR().copy() });
2014-02-26 19:03:43 -08:00
}
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 }});
}
}
}