2017-09-17 10:50:53 +02:00
|
|
|
#include "regex_impl.hh"
|
|
|
|
#include "vector.hh"
|
|
|
|
#include "unit_tests.hh"
|
|
|
|
#include "string.hh"
|
2017-09-18 04:56:14 +02:00
|
|
|
#include "unicode.hh"
|
2017-09-17 10:50:53 +02:00
|
|
|
#include "exception.hh"
|
2017-09-17 12:15:43 +02:00
|
|
|
#include "array_view.hh"
|
2017-09-17 10:50:53 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
namespace RegexProgram
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 12:15:43 +02:00
|
|
|
enum Op : char
|
|
|
|
{
|
|
|
|
Match,
|
|
|
|
Literal,
|
|
|
|
AnyChar,
|
|
|
|
Jump,
|
|
|
|
Split,
|
2017-09-19 09:56:21 +02:00
|
|
|
Save,
|
2017-09-17 12:15:43 +02:00
|
|
|
LineStart,
|
|
|
|
LineEnd,
|
2017-09-18 04:56:14 +02:00
|
|
|
WordBoundary,
|
|
|
|
NotWordBoundary,
|
2017-09-18 05:47:10 +02:00
|
|
|
SubjectBegin,
|
|
|
|
SubjectEnd,
|
2017-09-17 10:50:53 +02:00
|
|
|
};
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
using Offset = size_t;
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
|
|
|
namespace RegexCompiler
|
|
|
|
{
|
2017-09-18 11:22:11 +02:00
|
|
|
struct Quantifier
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-18 11:22:11 +02:00
|
|
|
enum Type
|
|
|
|
{
|
|
|
|
One,
|
|
|
|
Optional,
|
|
|
|
RepeatZeroOrMore,
|
|
|
|
RepeatOneOrMore,
|
|
|
|
RepeatMinMax,
|
|
|
|
};
|
|
|
|
Type type = One;
|
|
|
|
int min = -1, max = -1;
|
|
|
|
|
|
|
|
bool allows_none() const
|
|
|
|
{
|
|
|
|
return type == Quantifier::Optional or
|
|
|
|
type == Quantifier::RepeatZeroOrMore or
|
|
|
|
(type == Quantifier::RepeatMinMax and min <= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool allows_infinite_repeat() const
|
|
|
|
{
|
|
|
|
return type == Quantifier::RepeatZeroOrMore or
|
|
|
|
type == Quantifier::RepeatOneOrMore or
|
|
|
|
(type == Quantifier::RepeatMinMax and max == -1);
|
|
|
|
};
|
2017-09-17 12:15:43 +02:00
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
enum class Op
|
|
|
|
{
|
|
|
|
Literal,
|
|
|
|
AnyChar,
|
|
|
|
Sequence,
|
|
|
|
Alternation,
|
|
|
|
LineStart,
|
|
|
|
LineEnd,
|
2017-09-18 04:56:14 +02:00
|
|
|
WordBoundary,
|
|
|
|
NotWordBoundary,
|
2017-09-18 05:47:10 +02:00
|
|
|
SubjectBegin,
|
|
|
|
SubjectEnd,
|
2017-09-17 12:15:43 +02:00
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
struct AstNode
|
|
|
|
{
|
|
|
|
Op op;
|
|
|
|
char value;
|
|
|
|
Quantifier quantifier;
|
|
|
|
Vector<std::unique_ptr<AstNode>> children;
|
|
|
|
};
|
|
|
|
|
|
|
|
using AstNodePtr = std::unique_ptr<AstNode>;
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-19 09:56:21 +02:00
|
|
|
AstNodePtr make_ast_node(Op op, char value = -1,
|
2017-09-18 11:22:11 +02:00
|
|
|
Quantifier quantifier = {Quantifier::One})
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
|
|
|
return AstNodePtr{new AstNode{op, value, quantifier, {}}};
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-19 04:57:02 +02:00
|
|
|
// Recursive descent parser based on naming using in the ECMAScript
|
|
|
|
// standard, although the syntax is not fully compatible.
|
2017-09-17 12:15:43 +02:00
|
|
|
template<typename Iterator>
|
|
|
|
struct Parser
|
|
|
|
{
|
2017-09-19 09:56:21 +02:00
|
|
|
AstNodePtr parse(Iterator pos, Iterator end)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-19 09:56:21 +02:00
|
|
|
return disjunction(pos, end, 0);
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
private:
|
2017-09-19 09:56:21 +02:00
|
|
|
AstNodePtr disjunction(Iterator& pos, Iterator end, char capture = -1)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 12:15:43 +02:00
|
|
|
AstNodePtr node = alternative(pos, end);
|
|
|
|
if (pos == end or *pos != '|')
|
2017-09-19 09:56:21 +02:00
|
|
|
{
|
|
|
|
node->value = capture;
|
2017-09-17 12:15:43 +02:00
|
|
|
return node;
|
2017-09-19 09:56:21 +02:00
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
|
|
|
|
AstNodePtr res = make_ast_node(Op::Alternation);
|
|
|
|
res->children.push_back(std::move(node));
|
2017-09-17 13:08:10 +02:00
|
|
|
res->children.push_back(disjunction(++pos, end));
|
2017-09-19 09:56:21 +02:00
|
|
|
res->value = capture;
|
2017-09-17 12:15:43 +02:00
|
|
|
return res;
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-19 09:56:21 +02:00
|
|
|
AstNodePtr alternative(Iterator& pos, Iterator end)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
|
|
|
AstNodePtr res = make_ast_node(Op::Sequence);
|
|
|
|
while (auto node = term(pos, end))
|
2017-09-17 10:50:53 +02:00
|
|
|
res->children.push_back(std::move(node));
|
2017-09-17 12:15:43 +02:00
|
|
|
return res;
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-19 09:56:21 +02:00
|
|
|
AstNodePtr term(Iterator& pos, Iterator end)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
|
|
|
if (auto node = assertion(pos, end))
|
|
|
|
return node;
|
|
|
|
if (auto node = atom(pos, end))
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 12:15:43 +02:00
|
|
|
node->quantifier = quantifier(pos, end);
|
|
|
|
return node;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-19 09:56:21 +02:00
|
|
|
AstNodePtr assertion(Iterator& pos, Iterator end)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
|
|
|
switch (*pos)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 12:15:43 +02:00
|
|
|
case '^': ++pos; return make_ast_node(Op::LineStart);
|
|
|
|
case '$': ++pos; return make_ast_node(Op::LineEnd);
|
2017-09-18 04:56:14 +02:00
|
|
|
case '\\':
|
|
|
|
if (pos+1 == end)
|
|
|
|
return nullptr;
|
|
|
|
switch (*(pos+1))
|
|
|
|
{
|
|
|
|
case 'b': pos += 2; return make_ast_node(Op::WordBoundary);
|
|
|
|
case 'B': pos += 2; return make_ast_node(Op::NotWordBoundary);
|
2017-09-18 05:47:10 +02:00
|
|
|
case '`': pos += 2; return make_ast_node(Op::SubjectBegin);
|
|
|
|
case '\'': pos += 2; return make_ast_node(Op::SubjectEnd);
|
2017-09-18 04:56:14 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-09-19 04:57:02 +02:00
|
|
|
/* TODO: look ahead, look behind */
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-19 09:56:21 +02:00
|
|
|
AstNodePtr atom(Iterator& pos, Iterator end)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
|
|
|
const auto c = *pos;
|
|
|
|
switch (c)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 12:15:43 +02:00
|
|
|
case '.': ++pos; return make_ast_node(Op::AnyChar);
|
|
|
|
case '(':
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 12:15:43 +02:00
|
|
|
++pos;
|
2017-09-19 09:56:21 +02:00
|
|
|
auto content = disjunction(pos, end, m_next_capture++);
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
if (pos == end or *pos != ')')
|
|
|
|
throw runtime_error{"Unclosed parenthesis"};
|
|
|
|
++pos;
|
|
|
|
return content;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
default:
|
|
|
|
if (contains("^$.*+?()[]{}|", c))
|
|
|
|
return nullptr;
|
|
|
|
++pos;
|
|
|
|
return make_ast_node(Op::Literal, c);
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-19 09:56:21 +02:00
|
|
|
Quantifier quantifier(Iterator& pos, Iterator end)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-18 11:22:11 +02:00
|
|
|
auto read_int = [](Iterator& pos, Iterator begin, Iterator end) {
|
|
|
|
int res = 0;
|
|
|
|
for (; pos != end; ++pos)
|
|
|
|
{
|
|
|
|
const auto c = *pos;
|
|
|
|
if (c < '0' or c > '9')
|
|
|
|
return pos == begin ? -1 : res;
|
|
|
|
res = res * 10 + c - '0';
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
switch (*pos)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-18 11:22:11 +02:00
|
|
|
case '*': ++pos; return {Quantifier::RepeatZeroOrMore};
|
|
|
|
case '+': ++pos; return {Quantifier::RepeatOneOrMore};
|
|
|
|
case '?': ++pos; return {Quantifier::Optional};
|
|
|
|
case '{':
|
|
|
|
{
|
|
|
|
auto it = pos+1;
|
|
|
|
int min = read_int(it, it, end);
|
|
|
|
int max = -1;
|
|
|
|
if (*it == ',')
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
max = read_int(it, it, end);
|
|
|
|
}
|
|
|
|
if (*it++ != '}')
|
|
|
|
throw runtime_error{"expected closing bracket"};
|
|
|
|
pos = it;
|
|
|
|
return {Quantifier::RepeatMinMax, min, max};
|
|
|
|
}
|
|
|
|
default: return {Quantifier::One};
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-19 09:56:21 +02:00
|
|
|
|
|
|
|
char m_next_capture = 1;
|
2017-09-17 12:15:43 +02:00
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-18 11:22:11 +02:00
|
|
|
RegexProgram::Offset compile_node(Vector<char>& program, const AstNodePtr& node);
|
2017-09-17 12:15:43 +02:00
|
|
|
|
2017-09-18 11:22:11 +02:00
|
|
|
RegexProgram::Offset alloc_offset(Vector<char>& instructions)
|
|
|
|
{
|
|
|
|
auto pos = instructions.size();
|
|
|
|
instructions.resize(instructions.size() + sizeof(RegexProgram::Offset));
|
|
|
|
return pos;
|
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
|
2017-09-18 11:22:11 +02:00
|
|
|
RegexProgram::Offset& get_offset(Vector<char>& instructions, RegexProgram::Offset base)
|
|
|
|
{
|
|
|
|
return *reinterpret_cast<RegexProgram::Offset*>(&instructions[base]);
|
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
|
2017-09-18 11:22:11 +02:00
|
|
|
RegexProgram::Offset compile_node_inner(Vector<char>& program, const AstNodePtr& node)
|
|
|
|
{
|
|
|
|
const auto start_pos = program.size();
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-19 09:56:21 +02:00
|
|
|
const char capture = (node->op == Op::Alternation or node->op == Op::Sequence) ? node->value : -1;
|
|
|
|
if (capture >= 0)
|
|
|
|
{
|
|
|
|
program.push_back(RegexProgram::Save);
|
|
|
|
program.push_back(capture * 2);
|
|
|
|
}
|
|
|
|
|
2017-09-18 11:22:11 +02:00
|
|
|
Vector<RegexProgram::Offset> goto_inner_end_offsets;
|
2017-09-17 12:15:43 +02:00
|
|
|
switch (node->op)
|
|
|
|
{
|
|
|
|
case Op::Literal:
|
|
|
|
program.push_back(RegexProgram::Literal);
|
|
|
|
program.push_back(node->value);
|
|
|
|
break;
|
|
|
|
case Op::AnyChar:
|
|
|
|
program.push_back(RegexProgram::AnyChar);
|
|
|
|
break;
|
|
|
|
case Op::Sequence:
|
|
|
|
for (auto& child : node->children)
|
|
|
|
compile_node(program, child);
|
|
|
|
break;
|
|
|
|
case Op::Alternation:
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 13:08:10 +02:00
|
|
|
auto& children = node->children;
|
|
|
|
kak_assert(children.size() == 2);
|
2017-09-17 12:15:43 +02:00
|
|
|
|
|
|
|
program.push_back(RegexProgram::Split);
|
2017-09-17 13:08:10 +02:00
|
|
|
auto offset = alloc_offset(program);
|
|
|
|
|
|
|
|
compile_node(program, children[0]);
|
|
|
|
program.push_back(RegexProgram::Jump);
|
2017-09-18 11:22:11 +02:00
|
|
|
goto_inner_end_offsets.push_back(alloc_offset(program));
|
2017-09-17 13:08:10 +02:00
|
|
|
|
|
|
|
auto right_pos = compile_node(program, children[1]);
|
|
|
|
get_offset(program, offset) = right_pos;
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
break;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
case Op::LineStart:
|
|
|
|
program.push_back(RegexProgram::LineStart);
|
|
|
|
break;
|
|
|
|
case Op::LineEnd:
|
|
|
|
program.push_back(RegexProgram::LineEnd);
|
|
|
|
break;
|
2017-09-18 04:56:14 +02:00
|
|
|
case Op::WordBoundary:
|
|
|
|
program.push_back(RegexProgram::WordBoundary);
|
|
|
|
break;
|
|
|
|
case Op::NotWordBoundary:
|
|
|
|
program.push_back(RegexProgram::NotWordBoundary);
|
|
|
|
break;
|
2017-09-18 05:47:10 +02:00
|
|
|
case Op::SubjectBegin:
|
|
|
|
program.push_back(RegexProgram::SubjectBegin);
|
|
|
|
break;
|
|
|
|
case Op::SubjectEnd:
|
|
|
|
program.push_back(RegexProgram::SubjectEnd);
|
|
|
|
break;
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-18 11:22:11 +02:00
|
|
|
for (auto& offset : goto_inner_end_offsets)
|
2017-09-17 13:08:10 +02:00
|
|
|
get_offset(program, offset) = program.size();
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-19 09:56:21 +02:00
|
|
|
if (capture >= 0)
|
|
|
|
{
|
|
|
|
program.push_back(RegexProgram::Save);
|
|
|
|
program.push_back(capture * 2 + 1);
|
|
|
|
}
|
|
|
|
|
2017-09-18 11:22:11 +02:00
|
|
|
return start_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegexProgram::Offset compile_node(Vector<char>& program, const AstNodePtr& node)
|
|
|
|
{
|
|
|
|
RegexProgram::Offset pos = program.size();
|
|
|
|
Vector<RegexProgram::Offset> goto_end_offsets;
|
|
|
|
|
|
|
|
if (node->quantifier.allows_none())
|
|
|
|
{
|
|
|
|
program.push_back(RegexProgram::Split);
|
|
|
|
goto_end_offsets.push_back(alloc_offset(program));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto inner_pos = compile_node_inner(program, node);
|
|
|
|
// Write the node multiple times when we have a min count quantifier
|
|
|
|
for (int i = 1; i < node->quantifier.min; ++i)
|
|
|
|
inner_pos = compile_node_inner(program, node);
|
|
|
|
|
|
|
|
if (node->quantifier.allows_infinite_repeat())
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
|
|
|
program.push_back(RegexProgram::Split);
|
2017-09-18 11:22:11 +02:00
|
|
|
get_offset(program, alloc_offset(program)) = inner_pos;
|
|
|
|
}
|
|
|
|
// Write the node as an optional match for the min -> max counts
|
|
|
|
else for (int i = std::max(1, node->quantifier.min); // STILL UGLY !
|
|
|
|
i < node->quantifier.max; ++i)
|
|
|
|
{
|
|
|
|
program.push_back(RegexProgram::Split);
|
|
|
|
goto_end_offsets.push_back(alloc_offset(program));
|
|
|
|
compile_node_inner(program, node);
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-18 11:22:11 +02:00
|
|
|
for (auto offset : goto_end_offsets)
|
|
|
|
get_offset(program, offset) = program.size();
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
return pos;
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
Vector<char> compile(const AstNodePtr& node)
|
|
|
|
{
|
|
|
|
Vector<char> res;
|
|
|
|
compile_node(res, node);
|
|
|
|
res.push_back(RegexProgram::Match);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Iterator>
|
|
|
|
Vector<char> compile(Iterator begin, Iterator end)
|
|
|
|
{
|
2017-09-19 09:56:21 +02:00
|
|
|
return compile(Parser<Iterator>{}.parse(begin, end));
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
namespace RegexProgram
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 12:15:43 +02:00
|
|
|
void dump(ConstArrayView<char> program)
|
|
|
|
{
|
|
|
|
for (size_t pos = 0; pos < program.size(); )
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
|
|
|
printf("%4zd ", pos);
|
2017-09-17 12:15:43 +02:00
|
|
|
switch ((RegexProgram::Op)program[pos++])
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
|
|
|
case RegexProgram::Literal:
|
2017-09-17 12:15:43 +02:00
|
|
|
printf("literal %c\n", program[pos++]);
|
2017-09-17 10:50:53 +02:00
|
|
|
break;
|
|
|
|
case RegexProgram::AnyChar:
|
|
|
|
printf("any char\n");
|
|
|
|
break;
|
|
|
|
case RegexProgram::Jump:
|
2017-09-17 12:15:43 +02:00
|
|
|
printf("jump %zd\n", *reinterpret_cast<const RegexProgram::Offset*>(&program[pos]));
|
2017-09-17 10:50:53 +02:00
|
|
|
pos += sizeof(RegexProgram::Offset);
|
|
|
|
break;
|
|
|
|
case RegexProgram::Split:
|
|
|
|
{
|
2017-09-17 13:08:10 +02:00
|
|
|
printf("split %zd\n", *reinterpret_cast<const RegexProgram::Offset*>(&program[pos]));
|
|
|
|
pos += sizeof(RegexProgram::Offset);
|
2017-09-17 10:50:53 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-09-19 09:56:21 +02:00
|
|
|
case RegexProgram::Save:
|
|
|
|
printf("save %d\n", program[pos++]);
|
|
|
|
break;
|
2017-09-17 10:50:53 +02:00
|
|
|
case RegexProgram::LineStart:
|
|
|
|
printf("line start\n");
|
|
|
|
break;
|
|
|
|
case RegexProgram::LineEnd:
|
|
|
|
printf("line end\n");
|
|
|
|
break;
|
2017-09-18 04:56:14 +02:00
|
|
|
case RegexProgram::WordBoundary:
|
|
|
|
printf("word boundary\n");
|
|
|
|
break;
|
|
|
|
case RegexProgram::NotWordBoundary:
|
|
|
|
printf("not word boundary\n");
|
|
|
|
break;
|
2017-09-18 05:47:10 +02:00
|
|
|
case RegexProgram::SubjectBegin:
|
|
|
|
printf("subject begin\n");
|
|
|
|
break;
|
|
|
|
case RegexProgram::SubjectEnd:
|
|
|
|
printf("subject end\n");
|
|
|
|
break;
|
2017-09-17 10:50:53 +02:00
|
|
|
case RegexProgram::Match:
|
|
|
|
printf("match\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 12:48:23 +02:00
|
|
|
struct ThreadedExecutor
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 12:48:23 +02:00
|
|
|
ThreadedExecutor(ConstArrayView<char> program) : m_program{program} {}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-19 09:56:21 +02:00
|
|
|
struct Thread
|
2017-09-17 12:48:23 +02:00
|
|
|
{
|
2017-09-19 09:56:21 +02:00
|
|
|
const char* inst;
|
|
|
|
Vector<const char*> saves = {};
|
2017-09-17 12:48:23 +02:00
|
|
|
};
|
|
|
|
|
2017-09-19 09:56:21 +02:00
|
|
|
enum class StepResult { Consumed, Matched, Failed };
|
|
|
|
StepResult step(size_t thread_index)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 12:48:23 +02:00
|
|
|
while (true)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-19 09:56:21 +02:00
|
|
|
auto& thread = m_threads[thread_index];
|
2017-09-18 04:56:14 +02:00
|
|
|
char c = m_pos == m_subject.end() ? 0 : *m_pos;
|
2017-09-19 09:56:21 +02:00
|
|
|
const RegexProgram::Op op = (RegexProgram::Op)*thread.inst++;
|
2017-09-17 12:48:23 +02:00
|
|
|
switch (op)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 12:48:23 +02:00
|
|
|
case RegexProgram::Literal:
|
2017-09-19 09:56:21 +02:00
|
|
|
if (*thread.inst++ == c)
|
|
|
|
return StepResult::Consumed;
|
|
|
|
return StepResult::Failed;
|
2017-09-17 12:48:23 +02:00
|
|
|
case RegexProgram::AnyChar:
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Consumed;
|
2017-09-17 12:48:23 +02:00
|
|
|
case RegexProgram::Jump:
|
2017-09-19 09:56:21 +02:00
|
|
|
{
|
|
|
|
auto inst = m_program.begin() + *reinterpret_cast<const RegexProgram::Offset*>(thread.inst);
|
2017-09-18 04:30:01 +02:00
|
|
|
// if instruction is already going to be executed, drop this thread
|
2017-09-19 09:56:21 +02:00
|
|
|
if (std::find_if(m_threads.begin(), m_threads.end(),
|
|
|
|
[inst](const Thread& t) { return t.inst == inst; }) != m_threads.end())
|
|
|
|
return StepResult::Failed;
|
|
|
|
thread.inst = inst;
|
2017-09-17 12:48:23 +02:00
|
|
|
break;
|
2017-09-19 09:56:21 +02:00
|
|
|
}
|
2017-09-17 12:48:23 +02:00
|
|
|
case RegexProgram::Split:
|
|
|
|
{
|
2017-09-19 09:56:21 +02:00
|
|
|
add_thread(*reinterpret_cast<const RegexProgram::Offset*>(thread.inst), thread.saves);
|
|
|
|
// thread is invalidated now, as we mutated the m_thread vector
|
|
|
|
m_threads[thread_index].inst += sizeof(RegexProgram::Offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RegexProgram::Save:
|
|
|
|
{
|
|
|
|
const char index = *thread.inst++;
|
|
|
|
thread.saves[index] = m_pos;
|
2017-09-17 12:48:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RegexProgram::LineStart:
|
|
|
|
if (not is_line_start())
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Failed;
|
2017-09-17 12:48:23 +02:00
|
|
|
break;
|
|
|
|
case RegexProgram::LineEnd:
|
|
|
|
if (not is_line_end())
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Failed;
|
2017-09-17 12:48:23 +02:00
|
|
|
break;
|
2017-09-18 04:56:14 +02:00
|
|
|
case RegexProgram::WordBoundary:
|
|
|
|
if (not is_word_boundary())
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Failed;
|
2017-09-18 04:56:14 +02:00
|
|
|
break;
|
|
|
|
case RegexProgram::NotWordBoundary:
|
|
|
|
if (is_word_boundary())
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Failed;
|
2017-09-18 04:56:14 +02:00
|
|
|
break;
|
2017-09-18 05:47:10 +02:00
|
|
|
case RegexProgram::SubjectBegin:
|
|
|
|
if (m_pos != m_subject.begin())
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Failed;
|
2017-09-18 05:47:10 +02:00
|
|
|
break;
|
|
|
|
case RegexProgram::SubjectEnd:
|
|
|
|
if (m_pos != m_subject.end())
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Failed;
|
2017-09-18 05:47:10 +02:00
|
|
|
break;
|
2017-09-17 12:48:23 +02:00
|
|
|
case RegexProgram::Match:
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Matched;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Failed;
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-17 12:48:23 +02:00
|
|
|
bool match(ConstArrayView<char> program, StringView data)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-19 09:56:21 +02:00
|
|
|
m_threads.clear();
|
|
|
|
add_thread(0, Vector<const char*>(10, nullptr));
|
|
|
|
|
2017-09-17 12:48:23 +02:00
|
|
|
m_subject = data;
|
|
|
|
m_pos = data.begin();
|
|
|
|
|
|
|
|
for (m_pos = m_subject.begin(); m_pos != m_subject.end(); ++m_pos)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 12:48:23 +02:00
|
|
|
for (int i = 0; i < m_threads.size(); ++i)
|
|
|
|
{
|
2017-09-19 09:56:21 +02:00
|
|
|
const auto res = step(i);
|
|
|
|
if (res == StepResult::Matched)
|
|
|
|
{
|
|
|
|
m_captures = std::move(m_threads[i].saves);
|
2017-09-17 12:48:23 +02:00
|
|
|
return true;
|
2017-09-19 09:56:21 +02:00
|
|
|
}
|
|
|
|
else if (res == StepResult::Failed)
|
|
|
|
m_threads[i].inst = nullptr;
|
2017-09-17 12:48:23 +02:00
|
|
|
}
|
2017-09-19 09:56:21 +02:00
|
|
|
m_threads.erase(std::remove_if(m_threads.begin(), m_threads.end(),
|
|
|
|
[](const Thread& t) { return t.inst == nullptr; }), m_threads.end());
|
2017-09-17 12:48:23 +02:00
|
|
|
if (m_threads.empty())
|
2017-09-19 09:56:21 +02:00
|
|
|
return false;
|
2017-09-17 12:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step remaining threads to see if they match without consuming anything else
|
|
|
|
for (int i = 0; i < m_threads.size(); ++i)
|
|
|
|
{
|
2017-09-19 09:56:21 +02:00
|
|
|
if (step(i) == StepResult::Matched)
|
|
|
|
{
|
|
|
|
m_captures = std::move(m_threads[i].saves);
|
2017-09-17 12:15:43 +02:00
|
|
|
return true;
|
2017-09-19 09:56:21 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-17 12:48:23 +02:00
|
|
|
return false;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 09:56:21 +02:00
|
|
|
void add_thread(RegexProgram::Offset pos, Vector<const char*> saves)
|
2017-09-18 04:30:01 +02:00
|
|
|
{
|
|
|
|
const char* inst = m_program.begin() + pos;
|
2017-09-19 09:56:21 +02:00
|
|
|
if (std::find_if(m_threads.begin(), m_threads.end(),
|
|
|
|
[inst](const Thread& t) { return t.inst == inst; }) == m_threads.end())
|
|
|
|
m_threads.push_back({inst, std::move(saves)});
|
2017-09-18 04:30:01 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 12:48:23 +02:00
|
|
|
bool is_line_start() const
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-17 12:48:23 +02:00
|
|
|
return m_pos == m_subject.begin() or *(m_pos-1) == '\n';
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-17 12:48:23 +02:00
|
|
|
|
|
|
|
bool is_line_end() const
|
|
|
|
{
|
|
|
|
return m_pos == m_subject.end() or *m_pos == '\n';
|
|
|
|
}
|
|
|
|
|
2017-09-18 04:56:14 +02:00
|
|
|
bool is_word_boundary() const
|
|
|
|
{
|
|
|
|
return m_pos == m_subject.begin() or
|
|
|
|
m_pos == m_subject.end() or
|
|
|
|
is_word(*(m_pos-1)) != is_word(*m_pos);
|
|
|
|
}
|
|
|
|
|
2017-09-17 12:48:23 +02:00
|
|
|
ConstArrayView<char> m_program;
|
2017-09-19 09:56:21 +02:00
|
|
|
Vector<Thread> m_threads;
|
|
|
|
Vector<const char*> m_captures;
|
2017-09-17 12:48:23 +02:00
|
|
|
StringView m_subject;
|
|
|
|
const char* m_pos;
|
|
|
|
};
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
|
|
|
auto test_regex = UnitTest{[]{
|
2017-09-17 12:48:23 +02:00
|
|
|
using Exec = RegexProgram::ThreadedExecutor;
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-18 04:56:14 +02:00
|
|
|
StringView re = R"(a*b)";
|
2017-09-17 12:15:43 +02:00
|
|
|
auto program = RegexCompiler::compile(re.begin(), re.end());
|
|
|
|
RegexProgram::dump(program);
|
2017-09-17 12:48:23 +02:00
|
|
|
Exec exec{program};
|
|
|
|
kak_assert(exec.match(program, "b"));
|
|
|
|
kak_assert(exec.match(program, "ab"));
|
|
|
|
kak_assert(exec.match(program, "aaab"));
|
|
|
|
kak_assert(not exec.match(program, "acb"));
|
|
|
|
kak_assert(not exec.match(program, ""));
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 13:12:46 +02:00
|
|
|
|
|
|
|
{
|
2017-09-18 04:56:14 +02:00
|
|
|
StringView re = R"(^a.*b$)";
|
2017-09-17 13:12:46 +02:00
|
|
|
auto program = RegexCompiler::compile(re.begin(), re.end());
|
|
|
|
RegexProgram::dump(program);
|
|
|
|
Exec exec{program};
|
|
|
|
kak_assert(exec.match(program, "afoob"));
|
|
|
|
kak_assert(exec.match(program, "ab"));
|
|
|
|
kak_assert(not exec.match(program, "bab"));
|
|
|
|
kak_assert(not exec.match(program, ""));
|
|
|
|
}
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-18 04:56:14 +02:00
|
|
|
StringView re = R"(^(foo|qux|baz)+(bar)?baz$)";
|
2017-09-17 12:15:43 +02:00
|
|
|
auto program = RegexCompiler::compile(re.begin(), re.end());
|
|
|
|
RegexProgram::dump(program);
|
2017-09-17 12:48:23 +02:00
|
|
|
Exec exec{program};
|
|
|
|
kak_assert(exec.match(program, "fooquxbarbaz"));
|
2017-09-19 09:56:21 +02:00
|
|
|
kak_assert(StringView{exec.m_captures[2], exec.m_captures[3]} == "qux");
|
2017-09-17 13:08:10 +02:00
|
|
|
kak_assert(not exec.match(program, "fooquxbarbaze"));
|
2017-09-17 12:48:23 +02:00
|
|
|
kak_assert(not exec.match(program, "quxbar"));
|
|
|
|
kak_assert(not exec.match(program, "blahblah"));
|
2017-09-17 13:08:10 +02:00
|
|
|
kak_assert(exec.match(program, "bazbaz"));
|
2017-09-17 12:48:23 +02:00
|
|
|
kak_assert(exec.match(program, "quxbaz"));
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-18 04:56:14 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
StringView re = R"(.*\b(foo|bar)\b.*)";
|
|
|
|
auto program = RegexCompiler::compile(re.begin(), re.end());
|
|
|
|
RegexProgram::dump(program);
|
|
|
|
Exec exec{program};
|
2017-09-19 04:57:02 +02:00
|
|
|
kak_assert(exec.match(program, "qux foo baz"));
|
2017-09-19 09:56:21 +02:00
|
|
|
kak_assert(StringView{exec.m_captures[2], exec.m_captures[3]} == "foo");
|
2017-09-19 04:57:02 +02:00
|
|
|
kak_assert(not exec.match(program, "quxfoobaz"));
|
2017-09-18 04:56:14 +02:00
|
|
|
kak_assert(exec.match(program, "bar"));
|
|
|
|
kak_assert(not exec.match(program, "foobar"));
|
|
|
|
}
|
2017-09-18 05:47:10 +02:00
|
|
|
{
|
|
|
|
StringView re = R"(\`(foo|bar)\')";
|
|
|
|
auto program = RegexCompiler::compile(re.begin(), re.end());
|
|
|
|
RegexProgram::dump(program);
|
|
|
|
Exec exec{program};
|
|
|
|
kak_assert(exec.match(program, "foo"));
|
|
|
|
kak_assert(exec.match(program, "bar"));
|
|
|
|
kak_assert(not exec.match(program, "foobar"));
|
|
|
|
}
|
2017-09-18 11:22:11 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
StringView re = R"(\`a{3,5}b\')";
|
|
|
|
auto program = RegexCompiler::compile(re.begin(), re.end());
|
|
|
|
RegexProgram::dump(program);
|
|
|
|
Exec exec{program};
|
|
|
|
kak_assert(not exec.match(program, "aab"));
|
|
|
|
kak_assert(exec.match(program, "aaab"));
|
|
|
|
kak_assert(not exec.match(program, "aaaaaab"));
|
|
|
|
kak_assert(exec.match(program, "aaaaab"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
StringView re = R"(\`a{3,}b\')";
|
|
|
|
auto program = RegexCompiler::compile(re.begin(), re.end());
|
|
|
|
RegexProgram::dump(program);
|
|
|
|
Exec exec{program};
|
|
|
|
kak_assert(not exec.match(program, "aab"));
|
|
|
|
kak_assert(exec.match(program, "aaab"));
|
|
|
|
kak_assert(exec.match(program, "aaaaab"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
StringView re = R"(\`a{,3}b\')";
|
|
|
|
auto program = RegexCompiler::compile(re.begin(), re.end());
|
|
|
|
RegexProgram::dump(program);
|
|
|
|
Exec exec{program};
|
|
|
|
kak_assert(exec.match(program, "b"));
|
|
|
|
kak_assert(exec.match(program, "ab"));
|
|
|
|
kak_assert(exec.match(program, "aaab"));
|
|
|
|
kak_assert(not exec.match(program, "aaaab"));
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
}
|