Organize source into compiler and runtime dirs
This commit is contained in:
parent
e480cf538d
commit
9618efd12a
46 changed files with 169 additions and 38 deletions
|
|
@ -1,129 +0,0 @@
|
|||
#include "choice.h"
|
||||
#include "seq.h"
|
||||
#include "Pattern.h"
|
||||
#include "transition_map.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
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() {
|
||||
rule_ptr result = atom();
|
||||
if (has_more_input() && (peek() == '+')) {
|
||||
next();
|
||||
result = repeat(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
rule_ptr atom() {
|
||||
rule_ptr result;
|
||||
switch (peek()) {
|
||||
case '(':
|
||||
next();
|
||||
result = rule();
|
||||
if (peek() != ')')
|
||||
error("mismatched parens");
|
||||
else
|
||||
next();
|
||||
break;
|
||||
case ')':
|
||||
error("mismatched parens");
|
||||
break;
|
||||
case '\\':
|
||||
next();
|
||||
result = escaped_char(peek());
|
||||
next();
|
||||
break;
|
||||
default:
|
||||
result = character(peek());
|
||||
next();
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
rule_ptr escaped_char(char value) {
|
||||
switch (value) {
|
||||
case '(':
|
||||
case ')':
|
||||
return character(value);
|
||||
case 'w':
|
||||
return char_class(CharClassTypeWord);
|
||||
case 'd':
|
||||
return char_class(CharClassTypeDigit);
|
||||
default:
|
||||
error("unrecognized escape sequence");
|
||||
return rule_ptr();
|
||||
}
|
||||
}
|
||||
|
||||
void next() {
|
||||
position++;
|
||||
}
|
||||
|
||||
char peek() {
|
||||
return input[position];
|
||||
}
|
||||
|
||||
bool has_more_input() {
|
||||
return position < length;
|
||||
}
|
||||
|
||||
void error(const char *message) {
|
||||
throw std::string("Invalid regex pattern '") + input + "': " + message;
|
||||
}
|
||||
|
||||
const std::string input;
|
||||
const size_t length;
|
||||
int position;
|
||||
};
|
||||
|
||||
Pattern::Pattern(const std::string &string) : value(string) {};
|
||||
|
||||
pattern_ptr pattern(const std::string &value) {
|
||||
return std::make_shared<Pattern>(value);
|
||||
}
|
||||
|
||||
TransitionMap<Rule> Pattern::transitions() const {
|
||||
return to_rule_tree()->transitions();
|
||||
}
|
||||
|
||||
rule_ptr Pattern::to_rule_tree() const {
|
||||
return PatternParser(value).rule();
|
||||
}
|
||||
|
||||
bool Pattern::operator ==(tree_sitter::rules::Rule const &other) const {
|
||||
auto pattern = dynamic_cast<const Pattern *>(&other);
|
||||
return pattern && (pattern->value == value);
|
||||
}
|
||||
|
||||
std::string Pattern::to_string() const {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue