2013-12-18 20:58:05 -08:00
|
|
|
#include "rules.h"
|
2013-11-10 14:24:25 -08:00
|
|
|
#include "transition_map.h"
|
|
|
|
|
|
2013-12-19 23:05:54 -08:00
|
|
|
using std::string;
|
2013-11-14 21:25:58 -08:00
|
|
|
|
2013-11-10 14:24:25 -08:00
|
|
|
namespace tree_sitter {
|
|
|
|
|
namespace rules {
|
2013-11-14 21:25:58 -08:00
|
|
|
class PatternParser {
|
|
|
|
|
public:
|
|
|
|
|
PatternParser(const string &input) :
|
|
|
|
|
input(input),
|
|
|
|
|
position(0),
|
|
|
|
|
length(input.length()) {}
|
|
|
|
|
|
|
|
|
|
rule_ptr rule() {
|
|
|
|
|
auto result = term();
|
|
|
|
|
while (has_more_input() && peek() == '|') {
|
|
|
|
|
next();
|
|
|
|
|
result = choice({ result, term() });
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
rule_ptr term() {
|
|
|
|
|
rule_ptr result = factor();
|
|
|
|
|
while (has_more_input() && (peek() != '|') && (peek() != ')'))
|
|
|
|
|
result = seq({ result, factor() });
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rule_ptr factor() {
|
2013-11-15 13:35:35 -08:00
|
|
|
rule_ptr result = atom();
|
|
|
|
|
if (has_more_input() && (peek() == '+')) {
|
|
|
|
|
next();
|
|
|
|
|
result = repeat(result);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2013-11-14 21:25:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rule_ptr atom() {
|
|
|
|
|
rule_ptr result;
|
|
|
|
|
switch (peek()) {
|
|
|
|
|
case '(':
|
|
|
|
|
next();
|
|
|
|
|
result = rule();
|
2013-11-15 08:46:45 -08:00
|
|
|
if (peek() != ')')
|
|
|
|
|
error("mismatched parens");
|
|
|
|
|
else
|
2013-11-14 21:25:58 -08:00
|
|
|
next();
|
2013-11-15 08:46:45 -08:00
|
|
|
break;
|
|
|
|
|
case ')':
|
|
|
|
|
error("mismatched parens");
|
2013-11-14 21:25:58 -08:00
|
|
|
break;
|
|
|
|
|
case '\\':
|
|
|
|
|
next();
|
2013-11-20 19:00:20 -08:00
|
|
|
result = escaped_char(peek());
|
2013-11-15 08:46:45 -08:00
|
|
|
next();
|
|
|
|
|
break;
|
2013-11-14 21:25:58 -08:00
|
|
|
default:
|
|
|
|
|
result = character(peek());
|
|
|
|
|
next();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-11-15 08:46:45 -08:00
|
|
|
return result;
|
2013-11-14 21:25:58 -08:00
|
|
|
}
|
|
|
|
|
|
2013-11-20 19:00:20 -08:00
|
|
|
rule_ptr escaped_char(char value) {
|
|
|
|
|
switch (value) {
|
|
|
|
|
case '(':
|
|
|
|
|
case ')':
|
|
|
|
|
return character(value);
|
|
|
|
|
case 'w':
|
2013-12-21 23:53:26 -08:00
|
|
|
return character(CharClassWord);
|
2013-11-20 19:00:20 -08:00
|
|
|
case 'd':
|
2013-12-21 23:53:26 -08:00
|
|
|
return character(CharClassDigit);
|
2013-11-20 19:00:20 -08:00
|
|
|
default:
|
|
|
|
|
error("unrecognized escape sequence");
|
|
|
|
|
return rule_ptr();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-14 21:25:58 -08:00
|
|
|
void next() {
|
|
|
|
|
position++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char peek() {
|
|
|
|
|
return input[position];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool has_more_input() {
|
|
|
|
|
return position < length;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-15 08:46:45 -08:00
|
|
|
void error(const char *message) {
|
2013-12-19 23:05:54 -08:00
|
|
|
throw string("Invalid regex pattern '") + input + "': " + message;
|
2013-11-15 08:46:45 -08:00
|
|
|
}
|
|
|
|
|
|
2013-12-19 23:05:54 -08:00
|
|
|
const string input;
|
2013-11-14 21:25:58 -08:00
|
|
|
const size_t length;
|
|
|
|
|
int position;
|
|
|
|
|
};
|
|
|
|
|
|
2013-12-19 23:05:54 -08:00
|
|
|
Pattern::Pattern(const string &string) : value(string) {};
|
2013-11-14 21:25:58 -08:00
|
|
|
|
2013-12-18 20:58:05 -08:00
|
|
|
bool Pattern::operator==(tree_sitter::rules::Rule const &other) const {
|
2013-11-20 19:00:20 -08:00
|
|
|
auto pattern = dynamic_cast<const Pattern *>(&other);
|
|
|
|
|
return pattern && (pattern->value == value);
|
2013-11-10 14:24:25 -08:00
|
|
|
}
|
|
|
|
|
|
2013-12-19 23:05:54 -08:00
|
|
|
string Pattern::to_string() const {
|
2013-12-30 23:12:19 -08:00
|
|
|
return string("#<pattern '") + value + "'>";
|
2013-11-10 14:24:25 -08:00
|
|
|
}
|
2013-12-18 20:58:05 -08:00
|
|
|
|
2013-12-19 23:16:13 -08:00
|
|
|
void Pattern::accept(Visitor &visitor) const {
|
2013-12-18 20:58:05 -08:00
|
|
|
visitor.visit(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rule_ptr Pattern::to_rule_tree() const {
|
|
|
|
|
return PatternParser(value).rule();
|
|
|
|
|
}
|
2013-11-10 14:24:25 -08:00
|
|
|
}
|
|
|
|
|
}
|