Replace {left,right}_assoc w/ prec, with an associativity argument

This commit is contained in:
Max Brunsfeld 2015-03-23 21:02:40 -07:00
parent a19b0e75ac
commit b1f8ba6202
7 changed files with 37 additions and 38 deletions

View file

@ -3,6 +3,7 @@
#include <string>
#include <map>
#include "tree_sitter/compiler.h"
#include "compiler/rules/rule.h"
namespace tree_sitter {
@ -16,11 +17,7 @@ enum MetadataKey {
ASSOCIATIVITY,
};
enum Associativity {
AssociativityUnspecified,
AssociativityLeft,
AssociativityRight,
};
const Associativity AssociativityUnspecified = (Associativity)0;
class Metadata : public Rule {
public:

View file

@ -43,25 +43,22 @@ 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 token(left_assoc(1, make_shared<String>(value)));
return token(prec(1, make_shared<String>(value)));
}
rule_ptr err(const rule_ptr &rule) {
return choice({ rule, ERROR().copy() });
}
rule_ptr left_assoc(int precedence, const rule_ptr &rule) {
rule_ptr prec(int precedence, const rule_ptr &rule, Associativity associativity) {
return metadata(rule, {
{ PRECEDENCE, precedence },
{ ASSOCIATIVITY, AssociativityLeft }
{ ASSOCIATIVITY, associativity }
});
}
rule_ptr right_assoc(int precedence, const rule_ptr &rule) {
return metadata(rule, {
{ PRECEDENCE, precedence },
{ ASSOCIATIVITY, AssociativityRight }
});
rule_ptr prec(int precedence, const rule_ptr &rule) {
return prec(precedence, rule, AssociativityLeft);
}
rule_ptr token(const rule_ptr &rule) {