tree-sitter/spec/compiler/rules/pattern_spec.cpp

97 lines
2.5 KiB
C++
Raw Normal View History

#include "spec_helper.h"
#include "rules.h"
#include "transition_map.h"
using namespace tree_sitter::rules;
START_TEST
describe("parsing pattern rules", []() {
it("parses simple strings", [&]() {
Pattern rule("abc");
AssertThat(
rule.to_rule_tree()->to_string(),
Equals(seq({
character('a'),
character('b'),
character('c')
})->to_string()));
});
2013-11-20 19:00:20 -08:00
it("parses character classes", []() {
Pattern rule("\\w-\\d");
2013-11-20 19:00:20 -08:00
AssertThat(
rule.to_rule_tree()->to_string(),
2013-11-20 19:00:20 -08:00
Equals(seq({
character(CharClassWord),
2013-11-20 19:00:20 -08:00
character('-'),
character(CharClassDigit)
2013-11-20 19:00:20 -08:00
})->to_string()));
});
2013-11-20 19:00:20 -08:00
it("parses choices", []() {
Pattern rule("ab|cd|ef");
AssertThat(
rule.to_rule_tree()->to_string(),
Equals(choice({
seq({
character('a'),
character('b'),
}),
seq({
character('c'),
character('d')
}),
seq({
character('e'),
character('f')
})
})->to_string()));
});
it("parses choices in sequences", []() {
Pattern rule("(a|b)cd");
AssertThat(
rule.to_rule_tree()->to_string(),
Equals(seq({
choice({
character('a'),
character('b'),
}),
character('c'),
character('d')
})->to_string()));
});
it("parses special characters when they are escaped", []() {
Pattern rule("a\\(b");
AssertThat(
rule.to_rule_tree()->to_string(),
Equals(seq({
character('a'),
character('('),
character('b')
})->to_string()));
});
2013-11-15 13:35:35 -08:00
it("parses repeating rules", []() {
Pattern rule("(ab)+(cd)+");
2013-11-15 13:35:35 -08:00
AssertThat(
rule.to_rule_tree()->to_string(),
2013-11-15 13:35:35 -08:00
Equals(
seq({
repeat(seq({
character('a'),
character('b')
})),
repeat(seq({
character('c'),
character('d')
})),
})->to_string()
));
});
});
END_TEST