Regex: Code cleanup in the regex impl

This commit is contained in:
Maxime Coste 2017-09-17 19:15:43 +09:00
parent 8c9976ea72
commit a448e1e222

View File

@ -3,11 +3,12 @@
#include "unit_tests.hh" #include "unit_tests.hh"
#include "string.hh" #include "string.hh"
#include "exception.hh" #include "exception.hh"
#include "array_view.hh"
namespace Kakoune namespace Kakoune
{ {
struct RegexProgram namespace RegexProgram
{ {
enum Op : char enum Op : char
{ {
@ -21,11 +22,7 @@ struct RegexProgram
}; };
using Offset = size_t; using Offset = size_t;
using Instructions = Vector<char>; }
Instructions instructions;
};
namespace RegexCompiler namespace RegexCompiler
{ {
@ -151,10 +148,9 @@ namespace RegexCompiler
} }
}; };
RegexProgram::Offset compile_node(RegexProgram& program, const AstNodePtr& node) RegexProgram::Offset compile_node(Vector<char>& program, const AstNodePtr& node)
{ {
auto& insts = program.instructions; RegexProgram::Offset pos = program.size();
RegexProgram::Offset pos = insts.size();
auto allow_none = [](Quantifier quantifier) { auto allow_none = [](Quantifier quantifier) {
return quantifier == Quantifier::Optional or return quantifier == Quantifier::Optional or
@ -166,36 +162,36 @@ namespace RegexCompiler
quantifier == Quantifier::RepeatOneOrMore; quantifier == Quantifier::RepeatOneOrMore;
}; };
auto alloc_offsets = [](RegexProgram::Instructions& instructions, int count) { auto alloc_offsets = [](Vector<char>& instructions, int count) {
auto pos = instructions.size(); auto pos = instructions.size();
instructions.resize(instructions.size() + count * sizeof(RegexProgram::Offset)); instructions.resize(instructions.size() + count * sizeof(RegexProgram::Offset));
return pos; return pos;
}; };
auto get_offset = [](RegexProgram::Instructions& instructions, RegexProgram::Offset base, int index = 0) { auto get_offset = [](Vector<char>& instructions, RegexProgram::Offset base, int index = 0) {
return reinterpret_cast<RegexProgram::Offset*>(&instructions[base]) + index; return reinterpret_cast<RegexProgram::Offset*>(&instructions[base]) + index;
}; };
RegexProgram::Offset optional_offset = -1; RegexProgram::Offset optional_offset = -1;
if (allow_none(node->quantifier)) if (allow_none(node->quantifier))
{ {
insts.push_back(RegexProgram::Split); program.push_back(RegexProgram::Split);
insts.push_back(2); program.push_back(2);
auto offsets = alloc_offsets(insts, 2); auto offsets = alloc_offsets(program, 2);
*get_offset(insts, offsets) = insts.size(); *get_offset(program, offsets) = program.size();
optional_offset = offsets; optional_offset = offsets;
} }
Vector<RegexProgram::Offset> goto_end_offsets; Vector<RegexProgram::Offset> goto_end_offsets;
auto content_pos = insts.size(); auto content_pos = program.size();
switch (node->op) switch (node->op)
{ {
case Op::Literal: case Op::Literal:
insts.push_back(RegexProgram::Literal); program.push_back(RegexProgram::Literal);
insts.push_back(node->value); program.push_back(node->value);
break; break;
case Op::AnyChar: case Op::AnyChar:
insts.push_back(RegexProgram::AnyChar); program.push_back(RegexProgram::AnyChar);
break; break;
case Op::Sequence: case Op::Sequence:
for (auto& child : node->children) for (auto& child : node->children)
@ -207,79 +203,86 @@ namespace RegexCompiler
if (count > 255) if (count > 255)
throw runtime_error{"More than 255 elements in an alternation is not supported"}; throw runtime_error{"More than 255 elements in an alternation is not supported"};
insts.push_back(RegexProgram::Split); program.push_back(RegexProgram::Split);
insts.push_back(count); program.push_back(count);
auto offsets = alloc_offsets(insts, count); auto offsets = alloc_offsets(program, count);
auto& children = node->children; auto& children = node->children;
for (int i = 0; i < children.size(); ++i) for (int i = 0; i < children.size(); ++i)
{ {
auto child_pos = compile_node(program, children[i]); auto child_pos = compile_node(program, children[i]);
*get_offset(insts, offsets, i) = child_pos; *get_offset(program, offsets, i) = child_pos;
// Jump to end after executing that children // Jump to end after executing that children
insts.push_back(RegexProgram::Jump); program.push_back(RegexProgram::Jump);
goto_end_offsets.push_back(alloc_offsets(insts, 1)); goto_end_offsets.push_back(alloc_offsets(program, 1));
} }
break; break;
} }
case Op::LineStart: case Op::LineStart:
insts.push_back(RegexProgram::LineStart); program.push_back(RegexProgram::LineStart);
break; break;
case Op::LineEnd: case Op::LineEnd:
insts.push_back(RegexProgram::LineEnd); program.push_back(RegexProgram::LineEnd);
break; break;
} }
for (auto& offset : goto_end_offsets) for (auto& offset : goto_end_offsets)
*get_offset(insts, offset) = insts.size(); *get_offset(program, offset) = program.size();
if (is_repeat(node->quantifier)) if (is_repeat(node->quantifier))
{ {
insts.push_back(RegexProgram::Split); program.push_back(RegexProgram::Split);
insts.push_back(2); program.push_back(2);
auto offsets = alloc_offsets(insts, 2); auto offsets = alloc_offsets(program, 2);
*get_offset(insts, offsets, 0) = content_pos; *get_offset(program, offsets, 0) = content_pos;
*get_offset(insts, offsets, 1) = insts.size(); *get_offset(program, offsets, 1) = program.size();
} }
if (optional_offset != -1) if (optional_offset != -1)
*get_offset(insts, optional_offset, 1) = insts.size(); *get_offset(program, optional_offset, 1) = program.size();
return pos; return pos;
} }
RegexProgram compile(const AstNodePtr& node) Vector<char> compile(const AstNodePtr& node)
{ {
RegexProgram res; Vector<char> res;
compile_node(res, node); compile_node(res, node);
res.instructions.push_back(RegexProgram::Match); res.push_back(RegexProgram::Match);
return res; return res;
} }
template<typename Iterator>
Vector<char> compile(Iterator begin, Iterator end)
{
return compile(Parser<Iterator>::parse(begin, end));
}
} }
void dump_program(const RegexProgram& program) namespace RegexProgram
{ {
auto& insts = program.instructions; void dump(ConstArrayView<char> program)
for (size_t pos = 0; pos < insts.size(); ) {
for (size_t pos = 0; pos < program.size(); )
{ {
printf("%4zd ", pos); printf("%4zd ", pos);
switch ((RegexProgram::Op)insts[pos++]) switch ((RegexProgram::Op)program[pos++])
{ {
case RegexProgram::Literal: case RegexProgram::Literal:
printf("literal %c\n", insts[pos++]); printf("literal %c\n", program[pos++]);
break; break;
case RegexProgram::AnyChar: case RegexProgram::AnyChar:
printf("any char\n"); printf("any char\n");
break; break;
case RegexProgram::Jump: case RegexProgram::Jump:
printf("jump %zd\n", *reinterpret_cast<const RegexProgram::Offset*>(&insts[pos])); printf("jump %zd\n", *reinterpret_cast<const RegexProgram::Offset*>(&program[pos]));
pos += sizeof(RegexProgram::Offset); pos += sizeof(RegexProgram::Offset);
break; break;
case RegexProgram::Split: case RegexProgram::Split:
{ {
int count = insts[pos++]; int count = program[pos++];
printf("split ["); printf("split [");
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
printf("%zd%s", reinterpret_cast<const RegexProgram::Offset*>(&insts[pos])[i], printf("%zd%s", reinterpret_cast<const RegexProgram::Offset*>(&program[pos])[i],
(i == count - 1) ? "]\n" : ", "); (i == count - 1) ? "]\n" : ", ");
pos += count * sizeof(RegexProgram::Offset); pos += count * sizeof(RegexProgram::Offset);
break; break;
@ -296,17 +299,15 @@ void dump_program(const RegexProgram& program)
} }
} }
bool regex_match(const RegexProgram& program, StringView data)
{
const char* start = program.instructions.data();
Vector<const char*> threads = { start };
struct StepResult struct StepResult
{ {
enum Result { Consumed, Stepped, Matched, Failed } result; enum Result { Consumed, Matched, Failed } result;
const char* next = nullptr; const char* next = nullptr;
}; };
auto step_thread = [&](const char* inst, char c) -> StepResult
StepResult step_thread(const char* inst, char c, const char* start, Vector<const char*>& threads)
{
while (true)
{ {
const RegexProgram::Op op = (RegexProgram::Op)*inst++; const RegexProgram::Op op = (RegexProgram::Op)*inst++;
switch (op) switch (op)
@ -318,43 +319,44 @@ bool regex_match(const RegexProgram& program, StringView data)
case RegexProgram::AnyChar: case RegexProgram::AnyChar:
return { StepResult::Consumed, inst }; return { StepResult::Consumed, inst };
case RegexProgram::Jump: case RegexProgram::Jump:
return { StepResult::Stepped, start + *reinterpret_cast<const RegexProgram::Offset*>(inst) }; inst = start + *reinterpret_cast<const RegexProgram::Offset*>(inst);
break;
case RegexProgram::Split: case RegexProgram::Split:
{ {
const int count = *inst++; const int count = *inst++;
auto* offsets = reinterpret_cast<const RegexProgram::Offset*>(inst); auto* offsets = reinterpret_cast<const RegexProgram::Offset*>(inst);
for (int o = 1; o < count; ++o) for (int o = 1; o < count; ++o)
threads.push_back(start + offsets[o]); threads.push_back(start + offsets[o]);
return { StepResult::Stepped, start + offsets[0] }; inst = start + offsets[0];
break;
} }
case RegexProgram::LineStart: case RegexProgram::LineStart:
// TODO // TODO
return { StepResult::Stepped, inst }; break;
case RegexProgram::LineEnd: case RegexProgram::LineEnd:
// TODO // TODO
return { StepResult::Stepped, inst }; break;
case RegexProgram::Match: case RegexProgram::Match:
return { StepResult::Matched }; return { StepResult::Matched };
} }
}
return { StepResult::Failed }; return { StepResult::Failed };
}; }
bool match(ConstArrayView<char> program, StringView data)
{
const char* start = program.begin();
Vector<const char*> threads = { start };
for (auto c : data) for (auto c : data)
{ {
for (int i = 0; i < threads.size(); ++i) for (int i = 0; i < threads.size(); ++i)
{ {
while (threads[i]) auto res = step_thread(threads[i], c, start, threads);
{
auto res = step_thread(threads[i], c);
threads[i] = res.next; threads[i] = res.next;
if (res.result == StepResult::Matched)
if (res.result == StepResult::Consumed or
res.result == StepResult::Failed)
break;
else if (res.result == StepResult::Matched)
return true; return true;
} }
}
threads.erase(std::remove(threads.begin(), threads.end(), nullptr), threads.end()); threads.erase(std::remove(threads.begin(), threads.end(), nullptr), threads.end());
if (threads.empty()) if (threads.empty())
break; break;
@ -363,29 +365,34 @@ bool regex_match(const RegexProgram& program, StringView data)
// Step remaining threads to see if they match without consuming anything else // Step remaining threads to see if they match without consuming anything else
for (int i = 0; i < threads.size(); ++i) for (int i = 0; i < threads.size(); ++i)
{ {
while (threads[i]) if (step_thread(threads[i], 0, start, threads).result == StepResult::Matched)
{
auto res = step_thread(threads[i], 0);
threads[i] = res.next;
if (res.result == StepResult::Consumed)
break;
else if (res.result == StepResult::Matched)
return true; return true;
} }
}
return false; return false;
} }
}
auto test_regex = UnitTest{[]{ auto test_regex = UnitTest{[]{
{
StringView re = "a*b";
auto program = RegexCompiler::compile(re.begin(), re.end());
RegexProgram::dump(program);
kak_assert(RegexProgram::match(program, "b"));
kak_assert(RegexProgram::match(program, "ab"));
kak_assert(RegexProgram::match(program, "aaab"));
kak_assert(not RegexProgram::match(program, "acb"));
kak_assert(not RegexProgram::match(program, ""));
}
{
StringView re = "^(foo|qux)+(bar)?baz$"; StringView re = "^(foo|qux)+(bar)?baz$";
auto node = RegexCompiler::Parser<const char*>::parse(re.begin(), re.end()); auto program = RegexCompiler::compile(re.begin(), re.end());
kak_assert(node); RegexProgram::dump(program);
auto program = RegexCompiler::compile(node); kak_assert(RegexProgram::match(program, "fooquxbarbaz"));
dump_program(program); kak_assert(not RegexProgram::match(program, "quxbar"));
kak_assert(regex_match(program, "fooquxbarbaz")); kak_assert(not RegexProgram::match(program, "blahblah"));
kak_assert(not regex_match(program, "quxbar")); kak_assert(RegexProgram::match(program, "foobaz"));
kak_assert(not regex_match(program, "blahblah")); kak_assert(RegexProgram::match(program, "quxbaz"));
kak_assert(regex_match(program, "foobaz")); }
}}; }};
} }