Parse repeats in regex patterns

This commit is contained in:
Max Brunsfeld 2013-11-15 13:35:35 -08:00
parent 92e73a9e70
commit 100ab56779
2 changed files with 24 additions and 1 deletions

View file

@ -60,4 +60,22 @@ Describe(pattern_rules) {
character('b')
})->to_string()));
}
It(parses_repeating_rules) {
pattern_ptr rule = pattern("(ab)+(cd)+");
AssertThat(
rule->to_rule_tree()->to_string(),
Equals(
seq({
repeat(seq({
character('a'),
character('b')
})),
repeat(seq({
character('c'),
character('d')
})),
})->to_string()
));
}
};

View file

@ -32,7 +32,12 @@ namespace tree_sitter {
}
rule_ptr factor() {
return atom();
rule_ptr result = atom();
if (has_more_input() && (peek() == '+')) {
next();
result = repeat(result);
}
return result;
}
rule_ptr atom() {