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-07-20 21:43:27 -07:00
|
|
|
namespace rules {
|
|
|
|
|
|
|
|
|
|
using std::make_shared;
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::set;
|
|
|
|
|
using std::vector;
|
|
|
|
|
using std::map;
|
|
|
|
|
|
|
|
|
|
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>(); }
|
|
|
|
|
|
2015-01-14 20:54:48 -08:00
|
|
|
rule_ptr choice(const vector<rule_ptr> &rules) { return Choice::build(rules); }
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
rule_ptr repeat(const rule_ptr &content) {
|
2015-01-14 21:09:39 -08:00
|
|
|
return Repeat::build(content);
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-14 20:54:48 -08:00
|
|
|
rule_ptr seq(const vector<rule_ptr> &rules) { return Seq::build(rules); }
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
rule_ptr sym(const string &name) { return make_shared<NamedSymbol>(name); }
|
|
|
|
|
|
|
|
|
|
rule_ptr pattern(const string &value) { return make_shared<Pattern>(value); }
|
|
|
|
|
|
2015-03-22 14:05:40 -07:00
|
|
|
rule_ptr str(const string &value) {
|
2015-03-23 21:02:40 -07:00
|
|
|
return token(prec(1, make_shared<String>(value)));
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rule_ptr err(const rule_ptr &rule) {
|
|
|
|
|
return choice({ rule, ERROR().copy() });
|
2013-12-19 23:05:54 -08:00
|
|
|
}
|
2014-07-20 21:43:27 -07:00
|
|
|
|
2015-03-23 21:02:40 -07:00
|
|
|
rule_ptr prec(int precedence, const rule_ptr &rule, Associativity associativity) {
|
2015-03-16 23:12:08 -07:00
|
|
|
return metadata(rule, {
|
|
|
|
|
{ PRECEDENCE, precedence },
|
2015-03-23 21:02:40 -07:00
|
|
|
{ ASSOCIATIVITY, associativity }
|
2015-03-16 23:12:08 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-23 21:02:40 -07:00
|
|
|
rule_ptr prec(int precedence, const rule_ptr &rule) {
|
|
|
|
|
return prec(precedence, rule, AssociativityLeft);
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
2015-03-15 20:31:41 -07:00
|
|
|
rule_ptr token(const rule_ptr &rule) {
|
2014-07-20 21:43:27 -07:00
|
|
|
return metadata(rule, { { IS_TOKEN, 1 } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace rules
|
|
|
|
|
} // namespace tree_sitter
|