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-25 15:24:24 +02:00
|
|
|
#include "utf8.hh"
|
|
|
|
#include "utf8_iterator.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-19 10:44:58 +02:00
|
|
|
struct CompiledRegex
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-19 10:44:58 +02:00
|
|
|
enum Op : char
|
|
|
|
{
|
|
|
|
Match,
|
|
|
|
Literal,
|
|
|
|
AnyChar,
|
2017-09-23 09:18:21 +02:00
|
|
|
CharRange,
|
|
|
|
NegativeCharRange,
|
2017-09-19 10:44:58 +02:00
|
|
|
Jump,
|
2017-09-24 16:14:35 +02:00
|
|
|
Split_PrioritizeParent,
|
|
|
|
Split_PrioritizeChild,
|
2017-09-19 10:44:58 +02:00
|
|
|
Save,
|
|
|
|
LineStart,
|
|
|
|
LineEnd,
|
|
|
|
WordBoundary,
|
|
|
|
NotWordBoundary,
|
|
|
|
SubjectBegin,
|
|
|
|
SubjectEnd,
|
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-19 11:26:24 +02:00
|
|
|
using Offset = unsigned;
|
2017-09-19 10:44:58 +02:00
|
|
|
|
|
|
|
Vector<char> bytecode;
|
|
|
|
size_t save_count;
|
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
|
|
|
namespace RegexCompiler
|
|
|
|
{
|
2017-09-19 11:26:24 +02:00
|
|
|
|
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,
|
2017-09-23 09:18:21 +02:00
|
|
|
CharRange,
|
|
|
|
NegativeCharRange,
|
2017-09-17 12:15:43 +02:00
|
|
|
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;
|
2017-09-25 15:24:24 +02:00
|
|
|
Codepoint value;
|
2017-09-17 12:15:43 +02:00
|
|
|
Quantifier quantifier;
|
|
|
|
Vector<std::unique_ptr<AstNode>> children;
|
|
|
|
};
|
|
|
|
|
|
|
|
using AstNodePtr = std::unique_ptr<AstNode>;
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
struct CharRange { Codepoint min, max; };
|
2017-09-23 09:18:21 +02:00
|
|
|
|
|
|
|
struct ParsedRegex
|
|
|
|
{
|
|
|
|
AstNodePtr ast;
|
|
|
|
size_t capture_count;
|
|
|
|
Vector<Vector<CharRange>> ranges;
|
|
|
|
};
|
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
AstNodePtr make_ast_node(Op op, Codepoint 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-23 09:18:21 +02:00
|
|
|
// Recursive descent parser based on naming used in the ECMAScript
|
2017-09-19 04:57:02 +02:00
|
|
|
// standard, although the syntax is not fully compatible.
|
2017-09-17 12:15:43 +02:00
|
|
|
struct Parser
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
struct InvalidPolicy
|
|
|
|
{
|
|
|
|
Codepoint operator()(Codepoint cp) { throw runtime_error{"Invalid utf8 in regex"}; }
|
|
|
|
};
|
|
|
|
|
|
|
|
using Iterator = utf8::iterator<const char*, Codepoint, int, InvalidPolicy>;
|
|
|
|
|
|
|
|
static ParsedRegex parse(StringView re)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-23 09:18:21 +02:00
|
|
|
ParsedRegex res;
|
|
|
|
res.capture_count = 1;
|
2017-09-25 15:24:24 +02:00
|
|
|
Iterator pos{re.begin(), re}, end{re.end(), re};
|
2017-09-23 09:18:21 +02:00
|
|
|
res.ast = disjunction(res, pos, end, 0);
|
|
|
|
return res;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
private:
|
2017-09-25 15:24:24 +02:00
|
|
|
static AstNodePtr disjunction(ParsedRegex& parsed_regex, Iterator& pos, Iterator end, unsigned capture = -1)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-23 09:18:21 +02:00
|
|
|
AstNodePtr node = alternative(parsed_regex, pos, end);
|
2017-09-17 12:15:43 +02:00
|
|
|
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-23 09:18:21 +02:00
|
|
|
res->children.push_back(disjunction(parsed_regex, ++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-23 09:18:21 +02:00
|
|
|
static AstNodePtr alternative(ParsedRegex& parsed_regex, Iterator& pos, Iterator end)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
|
|
|
AstNodePtr res = make_ast_node(Op::Sequence);
|
2017-09-23 09:18:21 +02:00
|
|
|
while (auto node = term(parsed_regex, pos, end))
|
2017-09-17 10:50:53 +02:00
|
|
|
res->children.push_back(std::move(node));
|
2017-09-25 15:24:24 +02:00
|
|
|
if (res->children.empty())
|
|
|
|
throw runtime_error{"Parse error in alternative"};
|
2017-09-17 12:15:43 +02:00
|
|
|
return res;
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-23 09:18:21 +02:00
|
|
|
static AstNodePtr term(ParsedRegex& parsed_regex, Iterator& pos, Iterator end)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-23 09:18:21 +02:00
|
|
|
if (auto node = assertion(parsed_regex, pos, end))
|
2017-09-17 12:15:43 +02:00
|
|
|
return node;
|
2017-09-23 09:18:21 +02:00
|
|
|
if (auto node = atom(parsed_regex, pos, end))
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-23 09:18:21 +02:00
|
|
|
node->quantifier = quantifier(parsed_regex, pos, end);
|
2017-09-17 12:15:43 +02:00
|
|
|
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-23 09:18:21 +02:00
|
|
|
static AstNodePtr assertion(ParsedRegex& parsed_regex, Iterator& pos, Iterator end)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
if (pos == end)
|
|
|
|
return nullptr;
|
|
|
|
|
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-23 09:18:21 +02:00
|
|
|
static AstNodePtr atom(ParsedRegex& parsed_regex, Iterator& pos, Iterator end)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
if (pos == end)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const Codepoint cp = *pos;
|
|
|
|
switch (cp)
|
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-23 09:18:21 +02:00
|
|
|
auto content = disjunction(parsed_regex, pos, end, parsed_regex.capture_count++);
|
2017-09-19 09:56:21 +02:00
|
|
|
|
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-23 09:18:21 +02:00
|
|
|
case '\\':
|
|
|
|
++pos;
|
|
|
|
return atom_escape(parsed_regex, pos, end);
|
|
|
|
case '[':
|
|
|
|
++pos;
|
|
|
|
return character_class(parsed_regex, pos, end);
|
2017-09-17 12:15:43 +02:00
|
|
|
default:
|
2017-09-25 15:24:24 +02:00
|
|
|
if (contains("^$.*+?()[]{}|", cp))
|
2017-09-17 12:15:43 +02:00
|
|
|
return nullptr;
|
|
|
|
++pos;
|
2017-09-25 15:24:24 +02:00
|
|
|
return make_ast_node(Op::Literal, cp);
|
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-23 09:18:21 +02:00
|
|
|
static AstNodePtr atom_escape(ParsedRegex& parsed_regex, Iterator& pos, Iterator end)
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
const Codepoint cp = *pos;
|
2017-09-23 09:18:21 +02:00
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
struct { Codepoint name; Codepoint value; } control_escapes[] = {
|
2017-09-23 09:18:21 +02:00
|
|
|
{ 'f', '\f' }, { 'n', '\n' }, { 'r', '\r' }, { 't', '\t' }, { 'v', '\v' }
|
|
|
|
};
|
|
|
|
for (auto& control : control_escapes)
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
if (control.name == cp)
|
2017-09-23 09:18:21 +02:00
|
|
|
return make_ast_node(Op::Literal, control.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TOOD: \c..., \0..., '\0x...', \u...
|
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
if (contains("^$\\.*+?()[]{}|", cp)) // SyntaxCharacter
|
|
|
|
return make_ast_node(Op::Literal, cp);
|
2017-09-23 09:18:21 +02:00
|
|
|
throw runtime_error{"Unknown atom escape"};
|
|
|
|
}
|
|
|
|
|
|
|
|
static AstNodePtr character_class(ParsedRegex& parsed_regex, Iterator& pos, Iterator end)
|
|
|
|
{
|
|
|
|
const bool negative = pos != end and *pos == '^';
|
|
|
|
if (negative)
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
Vector<CharRange> ranges;
|
|
|
|
while (pos != end and *pos != ']')
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
const auto cp = *pos++;
|
|
|
|
if (cp == '-')
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
|
|
|
ranges.push_back({ '-', 0 });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos == end)
|
|
|
|
break;
|
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
CharRange range = { cp, 0 };
|
2017-09-23 09:18:21 +02:00
|
|
|
if (*pos == '-')
|
|
|
|
{
|
|
|
|
if (++pos == end)
|
|
|
|
break;
|
|
|
|
range.max = *pos++;
|
|
|
|
if (range.min > range.max)
|
|
|
|
throw runtime_error{"Invalid range specified"};
|
|
|
|
}
|
|
|
|
ranges.push_back(range);
|
|
|
|
}
|
|
|
|
if (pos == end)
|
|
|
|
throw runtime_error{"Unclosed character class"};
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
auto ranges_id = parsed_regex.ranges.size();
|
|
|
|
parsed_regex.ranges.push_back(std::move(ranges));
|
|
|
|
|
|
|
|
return make_ast_node(negative ? Op::NegativeCharRange : Op::CharRange, ranges_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Quantifier quantifier(ParsedRegex& parsed_regex, Iterator& pos, Iterator end)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
if (pos == end)
|
|
|
|
return {Quantifier::One};
|
|
|
|
|
2017-09-18 11:22:11 +02:00
|
|
|
auto read_int = [](Iterator& pos, Iterator begin, Iterator end) {
|
|
|
|
int res = 0;
|
|
|
|
for (; pos != end; ++pos)
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
const auto cp = *pos;
|
|
|
|
if (cp < '0' or cp > '9')
|
2017-09-18 11:22:11 +02:00
|
|
|
return pos == begin ? -1 : res;
|
2017-09-25 15:24:24 +02:00
|
|
|
res = res * 10 + cp - '0';
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
|
|
|
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-17 10:50:53 +02:00
|
|
|
|
2017-09-19 10:44:58 +02:00
|
|
|
CompiledRegex::Offset alloc_offset(CompiledRegex& program)
|
2017-09-18 11:22:11 +02:00
|
|
|
{
|
2017-09-19 10:44:58 +02:00
|
|
|
auto pos = program.bytecode.size();
|
|
|
|
program.bytecode.resize(pos + sizeof(CompiledRegex::Offset));
|
2017-09-18 11:22:11 +02:00
|
|
|
return pos;
|
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
|
2017-09-19 10:44:58 +02:00
|
|
|
CompiledRegex::Offset& get_offset(CompiledRegex& program, CompiledRegex::Offset pos)
|
2017-09-18 11:22:11 +02:00
|
|
|
{
|
2017-09-19 10:44:58 +02:00
|
|
|
return *reinterpret_cast<CompiledRegex::Offset*>(&program.bytecode[pos]);
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
void push_codepoint(CompiledRegex& program, Codepoint cp)
|
|
|
|
{
|
|
|
|
utf8::dump(std::back_inserter(program.bytecode), cp);
|
|
|
|
}
|
|
|
|
|
2017-09-23 09:18:21 +02:00
|
|
|
CompiledRegex::Offset compile_node(CompiledRegex& program, const ParsedRegex& parsed_regex, const AstNodePtr& node);
|
|
|
|
|
|
|
|
CompiledRegex::Offset compile_node_inner(CompiledRegex& program, const ParsedRegex& parsed_regex, const AstNodePtr& node)
|
2017-09-18 11:22:11 +02:00
|
|
|
{
|
2017-09-19 10:44:58 +02:00
|
|
|
const auto start_pos = program.bytecode.size();
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
const Codepoint capture = (node->op == Op::Alternation or node->op == Op::Sequence) ? node->value : -1;
|
|
|
|
if (capture != -1)
|
2017-09-19 09:56:21 +02:00
|
|
|
{
|
2017-09-19 10:44:58 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::Save);
|
|
|
|
program.bytecode.push_back(capture * 2);
|
2017-09-19 09:56:21 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 10:44:58 +02:00
|
|
|
Vector<CompiledRegex::Offset> goto_inner_end_offsets;
|
2017-09-17 12:15:43 +02:00
|
|
|
switch (node->op)
|
|
|
|
{
|
|
|
|
case Op::Literal:
|
2017-09-19 10:44:58 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::Literal);
|
2017-09-25 15:24:24 +02:00
|
|
|
push_codepoint(program, node->value);
|
2017-09-17 12:15:43 +02:00
|
|
|
break;
|
|
|
|
case Op::AnyChar:
|
2017-09-19 10:44:58 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::AnyChar);
|
2017-09-17 12:15:43 +02:00
|
|
|
break;
|
2017-09-23 09:18:21 +02:00
|
|
|
case Op::CharRange: case Op::NegativeCharRange:
|
|
|
|
{
|
|
|
|
auto& ranges = parsed_regex.ranges[node->value];
|
|
|
|
size_t single_count = std::count_if(ranges.begin(), ranges.end(),
|
|
|
|
[](auto& r) { return r.max == 0; });
|
|
|
|
program.bytecode.push_back(node->op == Op::CharRange ?
|
|
|
|
CompiledRegex::CharRange
|
|
|
|
: CompiledRegex::NegativeCharRange);
|
|
|
|
|
|
|
|
program.bytecode.push_back((char)single_count);
|
|
|
|
program.bytecode.push_back((char)(ranges.size() - single_count));
|
|
|
|
for (auto& r : ranges)
|
|
|
|
{
|
|
|
|
if (r.max == 0)
|
2017-09-25 15:24:24 +02:00
|
|
|
push_codepoint(program, r.min);
|
2017-09-23 09:18:21 +02:00
|
|
|
}
|
|
|
|
for (auto& r : ranges)
|
|
|
|
{
|
|
|
|
if (r.max != 0)
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
push_codepoint(program, r.min);
|
|
|
|
push_codepoint(program, r.max);
|
2017-09-23 09:18:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
case Op::Sequence:
|
|
|
|
for (auto& child : node->children)
|
2017-09-23 09:18:21 +02:00
|
|
|
compile_node(program, parsed_regex, child);
|
2017-09-17 12:15:43 +02:00
|
|
|
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
|
|
|
|
2017-09-24 16:14:35 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::Split_PrioritizeParent);
|
2017-09-17 13:08:10 +02:00
|
|
|
auto offset = alloc_offset(program);
|
|
|
|
|
2017-09-23 09:18:21 +02:00
|
|
|
compile_node(program, parsed_regex, children[0]);
|
2017-09-19 10:44:58 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::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
|
|
|
|
2017-09-23 09:18:21 +02:00
|
|
|
auto right_pos = compile_node(program, parsed_regex, children[1]);
|
2017-09-17 13:08:10 +02:00
|
|
|
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:
|
2017-09-19 10:44:58 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::LineStart);
|
2017-09-17 12:15:43 +02:00
|
|
|
break;
|
|
|
|
case Op::LineEnd:
|
2017-09-19 10:44:58 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::LineEnd);
|
2017-09-17 12:15:43 +02:00
|
|
|
break;
|
2017-09-18 04:56:14 +02:00
|
|
|
case Op::WordBoundary:
|
2017-09-19 10:44:58 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::WordBoundary);
|
2017-09-18 04:56:14 +02:00
|
|
|
break;
|
|
|
|
case Op::NotWordBoundary:
|
2017-09-19 10:44:58 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::NotWordBoundary);
|
2017-09-18 04:56:14 +02:00
|
|
|
break;
|
2017-09-18 05:47:10 +02:00
|
|
|
case Op::SubjectBegin:
|
2017-09-19 10:44:58 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::SubjectBegin);
|
2017-09-18 05:47:10 +02:00
|
|
|
break;
|
|
|
|
case Op::SubjectEnd:
|
2017-09-19 10:44:58 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::SubjectEnd);
|
2017-09-18 05:47:10 +02:00
|
|
|
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-19 10:44:58 +02:00
|
|
|
get_offset(program, offset) = program.bytecode.size();
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
if (capture != -1)
|
2017-09-19 09:56:21 +02:00
|
|
|
{
|
2017-09-19 10:44:58 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::Save);
|
|
|
|
program.bytecode.push_back(capture * 2 + 1);
|
2017-09-19 09:56:21 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 11:22:11 +02:00
|
|
|
return start_pos;
|
|
|
|
}
|
|
|
|
|
2017-09-23 09:18:21 +02:00
|
|
|
CompiledRegex::Offset compile_node(CompiledRegex& program, const ParsedRegex& parsed_regex, const AstNodePtr& node)
|
2017-09-18 11:22:11 +02:00
|
|
|
{
|
2017-09-19 10:44:58 +02:00
|
|
|
CompiledRegex::Offset pos = program.bytecode.size();
|
|
|
|
Vector<CompiledRegex::Offset> goto_end_offsets;
|
2017-09-18 11:22:11 +02:00
|
|
|
|
|
|
|
if (node->quantifier.allows_none())
|
|
|
|
{
|
2017-09-24 16:14:35 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::Split_PrioritizeParent);
|
2017-09-18 11:22:11 +02:00
|
|
|
goto_end_offsets.push_back(alloc_offset(program));
|
|
|
|
}
|
|
|
|
|
2017-09-23 09:18:21 +02:00
|
|
|
auto inner_pos = compile_node_inner(program, parsed_regex, node);
|
2017-09-18 11:22:11 +02:00
|
|
|
// Write the node multiple times when we have a min count quantifier
|
|
|
|
for (int i = 1; i < node->quantifier.min; ++i)
|
2017-09-23 09:18:21 +02:00
|
|
|
inner_pos = compile_node_inner(program, parsed_regex, node);
|
2017-09-18 11:22:11 +02:00
|
|
|
|
|
|
|
if (node->quantifier.allows_infinite_repeat())
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-24 16:14:35 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::Split_PrioritizeChild);
|
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)
|
|
|
|
{
|
2017-09-24 16:14:35 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::Split_PrioritizeParent);
|
2017-09-18 11:22:11 +02:00
|
|
|
goto_end_offsets.push_back(alloc_offset(program));
|
2017-09-23 09:18:21 +02:00
|
|
|
compile_node_inner(program, parsed_regex, 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)
|
2017-09-19 10:44:58 +02:00
|
|
|
get_offset(program, offset) = program.bytecode.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-19 11:26:24 +02:00
|
|
|
constexpr CompiledRegex::Offset prefix_size = 3 + 2 * sizeof(CompiledRegex::Offset);
|
|
|
|
|
|
|
|
// Add a '.*' as the first instructions for the search use case
|
|
|
|
void write_search_prefix(CompiledRegex& program)
|
|
|
|
{
|
|
|
|
kak_assert(program.bytecode.empty());
|
2017-09-24 16:14:35 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::Split_PrioritizeChild);
|
2017-09-19 11:26:24 +02:00
|
|
|
get_offset(program, alloc_offset(program)) = prefix_size;
|
|
|
|
program.bytecode.push_back(CompiledRegex::AnyChar);
|
2017-09-24 16:14:35 +02:00
|
|
|
program.bytecode.push_back(CompiledRegex::Split_PrioritizeParent);
|
2017-09-19 11:26:24 +02:00
|
|
|
get_offset(program, alloc_offset(program)) = 1 + sizeof(CompiledRegex::Offset);
|
|
|
|
}
|
|
|
|
|
2017-09-23 09:18:21 +02:00
|
|
|
CompiledRegex compile(const ParsedRegex& parsed_regex)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-19 10:44:58 +02:00
|
|
|
CompiledRegex res;
|
2017-09-19 11:26:24 +02:00
|
|
|
write_search_prefix(res);
|
2017-09-23 09:18:21 +02:00
|
|
|
compile_node(res, parsed_regex, parsed_regex.ast);
|
2017-09-19 10:44:58 +02:00
|
|
|
res.bytecode.push_back(CompiledRegex::Match);
|
2017-09-23 09:18:21 +02:00
|
|
|
res.save_count = parsed_regex.capture_count * 2;
|
2017-09-17 12:15:43 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
CompiledRegex compile(StringView re)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
return compile(Parser::parse(re));
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-19 10:44:58 +02:00
|
|
|
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 10:44:58 +02:00
|
|
|
void dump(const CompiledRegex& program)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
for (auto pos = program.bytecode.data(), end = program.bytecode.data() + program.bytecode.size();
|
|
|
|
pos < end; )
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
printf("%4zd ", pos - program.bytecode.data());
|
2017-09-23 09:18:21 +02:00
|
|
|
const auto op = (CompiledRegex::Op)*pos++;
|
|
|
|
switch (op)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Literal:
|
2017-09-25 15:24:24 +02:00
|
|
|
printf("literal %lc\n", utf8::read_codepoint(pos, (const char*)nullptr));
|
2017-09-17 10:50:53 +02:00
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::AnyChar:
|
2017-09-17 10:50:53 +02:00
|
|
|
printf("any char\n");
|
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Jump:
|
2017-09-19 11:26:24 +02:00
|
|
|
printf("jump %u\n", *reinterpret_cast<const CompiledRegex::Offset*>(&*pos));
|
2017-09-19 10:44:58 +02:00
|
|
|
pos += sizeof(CompiledRegex::Offset);
|
2017-09-17 10:50:53 +02:00
|
|
|
break;
|
2017-09-24 16:14:35 +02:00
|
|
|
case CompiledRegex::Split_PrioritizeParent:
|
|
|
|
case CompiledRegex::Split_PrioritizeChild:
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-24 16:14:35 +02:00
|
|
|
printf("split (prioritize %s) %u\n",
|
|
|
|
op == CompiledRegex::Split_PrioritizeParent ? "parent" : "child",
|
|
|
|
*reinterpret_cast<const CompiledRegex::Offset*>(&*pos));
|
2017-09-19 10:44:58 +02:00
|
|
|
pos += sizeof(CompiledRegex::Offset);
|
2017-09-17 10:50:53 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Save:
|
|
|
|
printf("save %d\n", *pos++);
|
2017-09-19 09:56:21 +02:00
|
|
|
break;
|
2017-09-23 09:18:21 +02:00
|
|
|
case CompiledRegex::CharRange: case CompiledRegex::NegativeCharRange:
|
|
|
|
{
|
|
|
|
printf("%schar range, [", op == CompiledRegex::NegativeCharRange ? "negative " : "");
|
|
|
|
auto single_count = *pos++;
|
|
|
|
auto range_count = *pos++;
|
|
|
|
for (int i = 0; i < single_count; ++i)
|
2017-09-25 15:24:24 +02:00
|
|
|
printf("%lc", utf8::read_codepoint(pos, (const char*)nullptr));
|
2017-09-23 09:18:21 +02:00
|
|
|
printf("]");
|
|
|
|
|
|
|
|
for (int i = 0; i < range_count; ++i)
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
Codepoint min = utf8::read_codepoint(pos, (const char*)nullptr);
|
|
|
|
Codepoint max = utf8::read_codepoint(pos, (const char*)nullptr);
|
|
|
|
printf(" [%lc-%lc]", min, max);
|
2017-09-23 09:18:21 +02:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
break;
|
|
|
|
}
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::LineStart:
|
2017-09-17 10:50:53 +02:00
|
|
|
printf("line start\n");
|
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::LineEnd:
|
2017-09-17 10:50:53 +02:00
|
|
|
printf("line end\n");
|
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::WordBoundary:
|
2017-09-18 04:56:14 +02:00
|
|
|
printf("word boundary\n");
|
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::NotWordBoundary:
|
2017-09-18 04:56:14 +02:00
|
|
|
printf("not word boundary\n");
|
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::SubjectBegin:
|
2017-09-18 05:47:10 +02:00
|
|
|
printf("subject begin\n");
|
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::SubjectEnd:
|
2017-09-18 05:47:10 +02:00
|
|
|
printf("subject end\n");
|
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Match:
|
2017-09-17 10:50:53 +02:00
|
|
|
printf("match\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
template<typename Iterator>
|
2017-09-19 10:44:58 +02:00
|
|
|
struct ThreadedRegexVM
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
ThreadedRegexVM(const CompiledRegex& 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-25 15:24:24 +02:00
|
|
|
const auto prog_start = m_program.bytecode.data();
|
|
|
|
const auto prog_end = prog_start + m_program.bytecode.size();
|
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-25 15:24:24 +02:00
|
|
|
const Codepoint cp = m_pos == m_end ? 0 : *m_pos;
|
2017-09-19 10:44:58 +02:00
|
|
|
const CompiledRegex::Op op = (CompiledRegex::Op)*thread.inst++;
|
2017-09-17 12:48:23 +02:00
|
|
|
switch (op)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Literal:
|
2017-09-25 15:24:24 +02:00
|
|
|
if (utf8::read_codepoint(thread.inst, prog_end) == cp)
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Consumed;
|
|
|
|
return StepResult::Failed;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::AnyChar:
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Consumed;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Jump:
|
2017-09-19 09:56:21 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto inst = prog_start + *reinterpret_cast<const CompiledRegex::Offset*>(thread.inst);
|
2017-09-19 11:26:24 +02:00
|
|
|
// if instruction is already going to be executed by another thread, 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-24 16:14:35 +02:00
|
|
|
case CompiledRegex::Split_PrioritizeParent:
|
2017-09-17 12:48:23 +02:00
|
|
|
{
|
2017-09-24 16:14:35 +02:00
|
|
|
add_thread(thread_index+1, *reinterpret_cast<const CompiledRegex::Offset*>(thread.inst), thread.saves);
|
2017-09-19 09:56:21 +02:00
|
|
|
// thread is invalidated now, as we mutated the m_thread vector
|
2017-09-19 10:44:58 +02:00
|
|
|
m_threads[thread_index].inst += sizeof(CompiledRegex::Offset);
|
2017-09-19 09:56:21 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-09-24 16:14:35 +02:00
|
|
|
case CompiledRegex::Split_PrioritizeChild:
|
|
|
|
{
|
|
|
|
add_thread(thread_index+1, thread.inst + sizeof(CompiledRegex::Offset) - prog_start, thread.saves);
|
|
|
|
// thread is invalidated now, as we mutated the m_thread vector
|
|
|
|
m_threads[thread_index].inst = prog_start + *reinterpret_cast<const CompiledRegex::Offset*>(m_threads[thread_index].inst);
|
|
|
|
break;
|
|
|
|
}
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Save:
|
2017-09-19 09:56:21 +02:00
|
|
|
{
|
|
|
|
const char index = *thread.inst++;
|
2017-09-25 15:24:24 +02:00
|
|
|
thread.saves[index] = m_pos.base();
|
2017-09-17 12:48:23 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-09-23 09:18:21 +02:00
|
|
|
case CompiledRegex::CharRange: case CompiledRegex::NegativeCharRange:
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
const int single_count = *thread.inst++;
|
|
|
|
const int range_count = *thread.inst++;
|
2017-09-23 09:18:21 +02:00
|
|
|
for (int i = 0; i < single_count; ++i)
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto candidate = utf8::read_codepoint(thread.inst, prog_end);
|
|
|
|
if (cp == candidate)
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
thread.inst = utf8::advance(thread.inst, prog_end, CharCount{single_count - (i + 1) + range_count * 2});
|
2017-09-23 09:18:21 +02:00
|
|
|
return op == CompiledRegex::CharRange ? StepResult::Consumed : StepResult::Failed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < range_count; ++i)
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto min = utf8::read_codepoint(thread.inst, prog_end);
|
|
|
|
auto max = utf8::read_codepoint(thread.inst, prog_end);
|
|
|
|
if (min <= cp and cp <= max)
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
thread.inst = utf8::advance(thread.inst, prog_end, CharCount{(range_count - (i + 1)) * 2});
|
2017-09-23 09:18:21 +02:00
|
|
|
return op == CompiledRegex::CharRange ? StepResult::Consumed : StepResult::Failed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return op == CompiledRegex::CharRange ? StepResult::Failed : StepResult::Consumed;
|
|
|
|
}
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::LineStart:
|
2017-09-17 12:48:23 +02:00
|
|
|
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;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::LineEnd:
|
2017-09-17 12:48:23 +02:00
|
|
|
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-19 10:44:58 +02:00
|
|
|
case CompiledRegex::WordBoundary:
|
2017-09-18 04:56:14 +02:00
|
|
|
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;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::NotWordBoundary:
|
2017-09-18 04:56:14 +02:00
|
|
|
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-19 10:44:58 +02:00
|
|
|
case CompiledRegex::SubjectBegin:
|
2017-09-25 15:24:24 +02:00
|
|
|
if (m_pos != m_begin)
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Failed;
|
2017-09-18 05:47:10 +02:00
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::SubjectEnd:
|
2017-09-25 15:24:24 +02:00
|
|
|
if (m_pos != m_end)
|
2017-09-19 09:56:21 +02:00
|
|
|
return StepResult::Failed;
|
2017-09-18 05:47:10 +02:00
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Match:
|
2017-09-24 16:14:35 +02:00
|
|
|
thread.inst = nullptr;
|
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-24 16:14:35 +02:00
|
|
|
bool exec(StringView data, bool match = true, bool longest = false)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-24 16:14:35 +02:00
|
|
|
bool found_match = false;
|
2017-09-19 09:56:21 +02:00
|
|
|
m_threads.clear();
|
2017-09-24 16:14:35 +02:00
|
|
|
add_thread(0, match ? RegexCompiler::prefix_size : 0,
|
2017-09-19 11:26:24 +02:00
|
|
|
Vector<const char*>(m_program.save_count, nullptr));
|
2017-09-19 09:56:21 +02:00
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
m_begin = data.begin();
|
|
|
|
m_end = data.end();
|
2017-09-17 12:48:23 +02:00
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
for (m_pos = Utf8It{m_begin, m_begin, m_end}; m_pos != m_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)
|
|
|
|
{
|
2017-09-24 16:23:02 +02:00
|
|
|
if (match)
|
|
|
|
continue; // We are not at end, this is not a full match
|
|
|
|
|
2017-09-19 09:56:21 +02:00
|
|
|
m_captures = std::move(m_threads[i].saves);
|
2017-09-24 16:14:35 +02:00
|
|
|
found_match = true;
|
|
|
|
m_threads.resize(i); // remove this and lower priority threads
|
|
|
|
if (not longest)
|
|
|
|
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-24 16:14:35 +02:00
|
|
|
found_match = true;
|
|
|
|
m_threads.resize(i); // remove this and lower priority threads
|
|
|
|
if (not longest)
|
|
|
|
return true;
|
2017-09-19 09:56:21 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-24 16:14:35 +02:00
|
|
|
return found_match;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-24 16:14:35 +02:00
|
|
|
void add_thread(int index, CompiledRegex::Offset pos, Vector<const char*> saves)
|
2017-09-18 04:30:01 +02:00
|
|
|
{
|
2017-09-19 10:44:58 +02:00
|
|
|
const char* inst = m_program.bytecode.data() + 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())
|
2017-09-24 16:14:35 +02:00
|
|
|
m_threads.insert(m_threads.begin() + index, {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-25 15:24:24 +02:00
|
|
|
return m_pos == m_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
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
return m_pos == m_end or *m_pos == '\n';
|
2017-09-17 12:48:23 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 04:56:14 +02:00
|
|
|
bool is_word_boundary() const
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
return m_pos == m_begin or m_pos == m_end or
|
2017-09-18 04:56:14 +02:00
|
|
|
is_word(*(m_pos-1)) != is_word(*m_pos);
|
|
|
|
}
|
|
|
|
|
2017-09-19 10:44:58 +02:00
|
|
|
const CompiledRegex& m_program;
|
2017-09-19 09:56:21 +02:00
|
|
|
Vector<Thread> m_threads;
|
2017-09-25 15:24:24 +02:00
|
|
|
|
|
|
|
using Utf8It = utf8::iterator<Iterator>;
|
|
|
|
|
|
|
|
Iterator m_begin;
|
|
|
|
Iterator m_end;
|
|
|
|
Utf8It m_pos;
|
2017-09-24 16:14:35 +02:00
|
|
|
|
|
|
|
Vector<const char*> m_captures;
|
2017-09-17 12:48:23 +02:00
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
|
|
|
auto test_regex = UnitTest{[]{
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto program = RegexCompiler::compile(R"(a*b)");
|
2017-09-19 10:44:58 +02:00
|
|
|
dump(program);
|
2017-09-25 15:24:24 +02:00
|
|
|
ThreadedRegexVM<const char*> vm{program};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("b"));
|
|
|
|
kak_assert(vm.exec("ab"));
|
|
|
|
kak_assert(vm.exec("aaab"));
|
|
|
|
kak_assert(not vm.exec("acb"));
|
2017-09-24 16:23:02 +02:00
|
|
|
kak_assert(not vm.exec("abc"));
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(not vm.exec(""));
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 13:12:46 +02:00
|
|
|
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto program = RegexCompiler::compile(R"(^a.*b$)");
|
2017-09-19 10:44:58 +02:00
|
|
|
dump(program);
|
2017-09-25 15:24:24 +02:00
|
|
|
ThreadedRegexVM<const char*> vm{program};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("afoob"));
|
|
|
|
kak_assert(vm.exec("ab"));
|
|
|
|
kak_assert(not vm.exec("bab"));
|
|
|
|
kak_assert(not vm.exec(""));
|
2017-09-17 13:12:46 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto program = RegexCompiler::compile(R"(^(foo|qux|baz)+(bar)?baz$)");
|
2017-09-19 10:44:58 +02:00
|
|
|
dump(program);
|
2017-09-25 15:24:24 +02:00
|
|
|
ThreadedRegexVM<const char*> vm{program};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("fooquxbarbaz"));
|
2017-09-19 10:44:58 +02:00
|
|
|
kak_assert(StringView{vm.m_captures[2], vm.m_captures[3]} == "qux");
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(not vm.exec("fooquxbarbaze"));
|
|
|
|
kak_assert(not vm.exec("quxbar"));
|
|
|
|
kak_assert(not vm.exec("blahblah"));
|
|
|
|
kak_assert(vm.exec("bazbaz"));
|
|
|
|
kak_assert(vm.exec("quxbaz"));
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-18 04:56:14 +02:00
|
|
|
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto program = RegexCompiler::compile(R"(.*\b(foo|bar)\b.*)");
|
2017-09-19 10:44:58 +02:00
|
|
|
dump(program);
|
2017-09-25 15:24:24 +02:00
|
|
|
ThreadedRegexVM<const char*> vm{program};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("qux foo baz"));
|
2017-09-19 10:44:58 +02:00
|
|
|
kak_assert(StringView{vm.m_captures[2], vm.m_captures[3]} == "foo");
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(not vm.exec("quxfoobaz"));
|
|
|
|
kak_assert(vm.exec("bar"));
|
|
|
|
kak_assert(not vm.exec("foobar"));
|
2017-09-18 04:56:14 +02:00
|
|
|
}
|
2017-09-18 05:47:10 +02:00
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto program = RegexCompiler::compile(R"(\`(foo|bar)\')");
|
2017-09-19 10:44:58 +02:00
|
|
|
dump(program);
|
2017-09-25 15:24:24 +02:00
|
|
|
ThreadedRegexVM<const char*> vm{program};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("foo"));
|
|
|
|
kak_assert(vm.exec("bar"));
|
|
|
|
kak_assert(not vm.exec("foobar"));
|
2017-09-18 05:47:10 +02:00
|
|
|
}
|
2017-09-18 11:22:11 +02:00
|
|
|
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto program = RegexCompiler::compile(R"(\`a{3,5}b\')");
|
2017-09-19 10:44:58 +02:00
|
|
|
dump(program);
|
2017-09-25 15:24:24 +02:00
|
|
|
ThreadedRegexVM<const char*> vm{program};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(not vm.exec("aab"));
|
|
|
|
kak_assert(vm.exec("aaab"));
|
|
|
|
kak_assert(not vm.exec("aaaaaab"));
|
|
|
|
kak_assert(vm.exec("aaaaab"));
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto program = RegexCompiler::compile(R"(\`a{3,}b\')");
|
2017-09-19 10:44:58 +02:00
|
|
|
dump(program);
|
2017-09-25 15:24:24 +02:00
|
|
|
ThreadedRegexVM<const char*> vm{program};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(not vm.exec("aab"));
|
|
|
|
kak_assert(vm.exec("aaab"));
|
|
|
|
kak_assert(vm.exec("aaaaab"));
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto program = RegexCompiler::compile(R"(\`a{,3}b\')");
|
2017-09-19 10:44:58 +02:00
|
|
|
dump(program);
|
2017-09-25 15:24:24 +02:00
|
|
|
ThreadedRegexVM<const char*> vm{program};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("b"));
|
|
|
|
kak_assert(vm.exec("ab"));
|
|
|
|
kak_assert(vm.exec("aaab"));
|
|
|
|
kak_assert(not vm.exec("aaaab"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto program = RegexCompiler::compile(R"(f.*a(.*o))");
|
2017-09-19 11:26:24 +02:00
|
|
|
dump(program);
|
2017-09-25 15:24:24 +02:00
|
|
|
ThreadedRegexVM<const char*> vm{program};
|
2017-09-24 16:14:35 +02:00
|
|
|
kak_assert(vm.exec("blahfoobarfoobaz", false, true));
|
|
|
|
kak_assert(StringView{vm.m_captures[0], vm.m_captures[1]} == "foobarfoo");
|
|
|
|
kak_assert(StringView{vm.m_captures[2], vm.m_captures[3]} == "rfoo");
|
|
|
|
kak_assert(vm.exec("mais que fais la police", false, true));
|
|
|
|
kak_assert(StringView{vm.m_captures[0], vm.m_captures[1]} == "fais la po");
|
|
|
|
kak_assert(StringView{vm.m_captures[2], vm.m_captures[3]} == " po");
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
2017-09-23 09:18:21 +02:00
|
|
|
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
auto program = RegexCompiler::compile(R"([àb-dX-Z]{3,5})");
|
2017-09-23 09:18:21 +02:00
|
|
|
dump(program);
|
2017-09-25 15:24:24 +02:00
|
|
|
ThreadedRegexVM<const char*> vm{program};
|
|
|
|
kak_assert(vm.exec("càY"));
|
|
|
|
kak_assert(not vm.exec("àeY"));
|
|
|
|
kak_assert(vm.exec("dcbàX"));
|
2017-09-23 09:18:21 +02:00
|
|
|
kak_assert(not vm.exec("efg"));
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
}
|