2014-04-14 08:38:11 -07:00
|
|
|
#include <vector>
|
2014-04-14 23:11:10 -07:00
|
|
|
#include <map>
|
2014-04-14 08:38:11 -07:00
|
|
|
#include <set>
|
|
|
|
|
#include <string>
|
2014-02-17 12:53:57 -08:00
|
|
|
#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"
|
2014-04-14 23:11:10 -07:00
|
|
|
#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"
|
2013-12-19 23:05:54 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
2014-02-16 22:13:08 -08:00
|
|
|
using std::make_shared;
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::set;
|
|
|
|
|
using std::vector;
|
2014-04-14 23:11:10 -07:00
|
|
|
using std::map;
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2013-12-19 23:05:54 -08:00
|
|
|
namespace rules {
|
2014-05-01 12:43:29 -07:00
|
|
|
static rule_ptr metadata(rule_ptr rule, map<MetadataKey, int> values) {
|
|
|
|
|
return std::make_shared<Metadata>(rule, values);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-19 23:05:54 -08:00
|
|
|
rule_ptr blank() {
|
|
|
|
|
return make_shared<Blank>();
|
|
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-07 13:26:51 -08:00
|
|
|
rule_ptr choice(const vector<rule_ptr> &rules) {
|
2014-02-16 22:13:08 -08:00
|
|
|
return Choice::Build(rules);
|
2013-12-19 23:05:54 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-21 08:23:20 -08:00
|
|
|
rule_ptr repeat(const rule_ptr &content) {
|
2013-12-19 23:05:54 -08:00
|
|
|
return std::make_shared<Repeat>(content);
|
|
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-07 13:26:51 -08:00
|
|
|
rule_ptr seq(const vector<rule_ptr> &rules) {
|
2014-02-16 22:13:08 -08:00
|
|
|
return Seq::Build(rules);
|
2013-12-19 23:05:54 -08:00
|
|
|
}
|
|
|
|
|
|
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-01-28 13:27:30 -08:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
2014-04-14 23:11:10 -07:00
|
|
|
|
|
|
|
|
rule_ptr prec(int precedence, rule_ptr rule) {
|
2014-05-01 12:43:29 -07:00
|
|
|
return metadata(rule, { { PRECEDENCE, precedence } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rule_ptr token(rule_ptr rule) {
|
|
|
|
|
return metadata(rule, { { IS_TOKEN, 1 } });
|
2014-04-14 23:11:10 -07:00
|
|
|
}
|
2013-12-19 23:05:54 -08:00
|
|
|
}
|
|
|
|
|
}
|