2015-01-12 23:01:52 -08:00
|
|
|
#include "compiler/syntax_grammar.h"
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include "compiler/rules/symbol.h"
|
2015-01-11 23:21:58 -08:00
|
|
|
#include "compiler/rules/built_in_symbols.h"
|
2015-01-12 23:01:52 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
|
|
|
|
|
|
|
|
|
using std::string;
|
2015-01-11 23:21:58 -08:00
|
|
|
using std::to_string;
|
2015-01-12 23:01:52 -08:00
|
|
|
using std::pair;
|
|
|
|
|
using std::vector;
|
|
|
|
|
using std::set;
|
|
|
|
|
|
2015-10-01 17:10:39 -07:00
|
|
|
static const vector<Production> START_PRODUCTIONS_TOKEN_ONLY({
|
|
|
|
|
Production({ ProductionStep(rules::Symbol(0, true), 0, AssociativityNone) }),
|
|
|
|
|
});
|
|
|
|
|
|
2015-01-11 23:21:58 -08:00
|
|
|
static const vector<Production> START_PRODUCTIONS({
|
2015-10-01 17:10:39 -07:00
|
|
|
Production({ ProductionStep(rules::Symbol(0), 0, AssociativityNone) }),
|
2015-01-11 23:21:58 -08:00
|
|
|
});
|
2015-01-12 23:01:52 -08:00
|
|
|
|
2015-01-11 23:21:58 -08:00
|
|
|
static const vector<Production> NO_PRODUCTIONS({});
|
2015-01-12 23:01:52 -08:00
|
|
|
|
2015-10-01 17:10:39 -07:00
|
|
|
SyntaxVariable::SyntaxVariable(const string &name, VariableType type,
|
|
|
|
|
const vector<Production> &productions)
|
|
|
|
|
: name(name), productions(productions), type(type) {}
|
|
|
|
|
|
|
|
|
|
ProductionStep::ProductionStep(const rules::Symbol &symbol, int precedence,
|
|
|
|
|
Associativity associativity)
|
|
|
|
|
: symbol(symbol),
|
|
|
|
|
precedence(precedence),
|
|
|
|
|
associativity(associativity),
|
|
|
|
|
rule_id(0) {}
|
|
|
|
|
|
|
|
|
|
ProductionStep::ProductionStep(const rules::Symbol &symbol, int precedence,
|
|
|
|
|
Associativity associativity, int rule_id)
|
|
|
|
|
: symbol(symbol),
|
|
|
|
|
precedence(precedence),
|
|
|
|
|
associativity(associativity),
|
|
|
|
|
rule_id(rule_id) {}
|
|
|
|
|
|
|
|
|
|
bool ProductionStep::operator==(const ProductionStep &other) const {
|
2015-01-11 23:21:58 -08:00
|
|
|
return symbol == other.symbol && precedence == other.precedence &&
|
2015-10-01 17:10:39 -07:00
|
|
|
rule_id == other.rule_id && associativity == other.associativity;
|
2015-01-12 23:01:52 -08:00
|
|
|
}
|
|
|
|
|
|
2015-10-01 17:10:39 -07:00
|
|
|
const vector<Production> &SyntaxGrammar::productions(
|
|
|
|
|
const rules::Symbol &symbol) const {
|
|
|
|
|
if (symbol == rules::START()) {
|
|
|
|
|
if (variables.empty())
|
|
|
|
|
return START_PRODUCTIONS_TOKEN_ONLY;
|
|
|
|
|
else
|
|
|
|
|
return START_PRODUCTIONS;
|
|
|
|
|
} else if (symbol.is_built_in() || symbol.is_token) {
|
2015-01-11 23:21:58 -08:00
|
|
|
return NO_PRODUCTIONS;
|
2015-10-01 17:10:39 -07:00
|
|
|
} else {
|
|
|
|
|
return variables[symbol.index].productions;
|
2015-01-11 23:21:58 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-12 23:01:52 -08:00
|
|
|
} // namespace tree_sitter
|