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
|
|
|
|
2017-09-26 09:44:30 +02:00
|
|
|
#include "buffer_utils.hh"
|
|
|
|
|
2017-09-17 10:50:53 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
struct ParsedRegex
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-26 16:04:47 +02:00
|
|
|
enum Op
|
2017-09-19 10:44:58 +02:00
|
|
|
{
|
|
|
|
Literal,
|
|
|
|
AnyChar,
|
2017-09-26 11:03:12 +02:00
|
|
|
Matcher,
|
2017-09-26 16:04:47 +02:00
|
|
|
Sequence,
|
|
|
|
Alternation,
|
2017-09-19 10:44:58 +02:00
|
|
|
LineStart,
|
|
|
|
LineEnd,
|
|
|
|
WordBoundary,
|
|
|
|
NotWordBoundary,
|
|
|
|
SubjectBegin,
|
|
|
|
SubjectEnd,
|
2017-09-26 17:01:06 +02:00
|
|
|
ResetStart,
|
2017-09-28 14:43:45 +02:00
|
|
|
LookAhead,
|
|
|
|
NegativeLookAhead,
|
2017-10-04 17:00:19 +02:00
|
|
|
LookBehind,
|
2017-09-28 14:43:45 +02:00
|
|
|
NegativeLookBehind,
|
2017-09-19 10:44:58 +02:00
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
struct Quantifier
|
|
|
|
{
|
|
|
|
enum Type
|
|
|
|
{
|
|
|
|
One,
|
|
|
|
Optional,
|
|
|
|
RepeatZeroOrMore,
|
|
|
|
RepeatOneOrMore,
|
|
|
|
RepeatMinMax,
|
|
|
|
};
|
|
|
|
Type type = One;
|
2017-09-28 11:50:04 +02:00
|
|
|
bool greedy = true;
|
2017-09-26 16:04:47 +02:00
|
|
|
int min = -1, max = -1;
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
bool allows_none() const
|
|
|
|
{
|
|
|
|
return type == Quantifier::Optional or
|
|
|
|
type == Quantifier::RepeatZeroOrMore or
|
|
|
|
(type == Quantifier::RepeatMinMax and min <= 0);
|
|
|
|
}
|
2017-09-19 11:26:24 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
bool allows_infinite_repeat() const
|
|
|
|
{
|
|
|
|
return type == Quantifier::RepeatZeroOrMore or
|
|
|
|
type == Quantifier::RepeatOneOrMore or
|
|
|
|
(type == Quantifier::RepeatMinMax and max == -1);
|
|
|
|
};
|
2017-09-18 11:22:11 +02:00
|
|
|
};
|
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
struct AstNode
|
2017-09-18 11:22:11 +02:00
|
|
|
{
|
2017-09-26 16:04:47 +02:00
|
|
|
Op op;
|
|
|
|
Codepoint value;
|
|
|
|
Quantifier quantifier;
|
2017-09-29 05:22:09 +02:00
|
|
|
bool ignore_case;
|
2017-09-26 16:04:47 +02:00
|
|
|
Vector<std::unique_ptr<AstNode>> children;
|
2017-09-18 11:22:11 +02:00
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
using AstNodePtr = std::unique_ptr<AstNode>;
|
2017-09-23 09:18:21 +02:00
|
|
|
|
|
|
|
AstNodePtr ast;
|
|
|
|
size_t capture_count;
|
2017-09-26 11:03:12 +02:00
|
|
|
Vector<std::function<bool (Codepoint)>> matchers;
|
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-26 15:38:56 +02:00
|
|
|
struct RegexParser
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
RegexParser(StringView re)
|
|
|
|
: m_regex{re}, m_pos{re.begin(), re}
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
m_parsed_regex.capture_count = 1;
|
|
|
|
m_parsed_regex.ast = disjunction(0);
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
ParsedRegex get_parsed_regex() { return std::move(m_parsed_regex); }
|
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
static ParsedRegex parse(StringView re) { return RegexParser{re}.get_parsed_regex(); }
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
private:
|
2017-09-26 09:44:30 +02:00
|
|
|
struct InvalidPolicy
|
|
|
|
{
|
|
|
|
Codepoint operator()(Codepoint cp) { throw runtime_error{"Invalid utf8 in regex"}; }
|
|
|
|
};
|
|
|
|
|
|
|
|
using Iterator = utf8::iterator<const char*, Codepoint, int, InvalidPolicy>;
|
2017-09-26 16:04:47 +02:00
|
|
|
using AstNodePtr = ParsedRegex::AstNodePtr;
|
2017-09-26 09:44:30 +02:00
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
AstNodePtr disjunction(unsigned capture = -1)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
AstNodePtr node = alternative();
|
|
|
|
if (at_end() or *m_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
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
++m_pos;
|
2017-09-26 16:04:47 +02:00
|
|
|
AstNodePtr res = new_node(ParsedRegex::Alternation);
|
2017-09-17 12:15:43 +02:00
|
|
|
res->children.push_back(std::move(node));
|
2017-09-26 15:38:56 +02:00
|
|
|
res->children.push_back(disjunction());
|
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-28 14:43:45 +02:00
|
|
|
AstNodePtr alternative(ParsedRegex::Op op = ParsedRegex::Sequence)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-28 14:43:45 +02:00
|
|
|
AstNodePtr res = new_node(op);
|
2017-09-26 15:38:56 +02:00
|
|
|
while (auto node = term())
|
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-26 15:38:56 +02:00
|
|
|
AstNodePtr term()
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
if (auto node = assertion())
|
2017-09-17 12:15:43 +02:00
|
|
|
return node;
|
2017-09-26 15:38:56 +02:00
|
|
|
if (auto node = atom())
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
node->quantifier = quantifier();
|
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-26 15:38:56 +02:00
|
|
|
AstNodePtr assertion()
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
if (at_end())
|
2017-09-25 15:24:24 +02:00
|
|
|
return nullptr;
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
switch (*m_pos)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-26 16:04:47 +02:00
|
|
|
case '^': ++m_pos; return new_node(ParsedRegex::LineStart);
|
|
|
|
case '$': ++m_pos; return new_node(ParsedRegex::LineEnd);
|
2017-09-18 04:56:14 +02:00
|
|
|
case '\\':
|
2017-09-26 15:38:56 +02:00
|
|
|
if (m_pos+1 == m_regex.end())
|
2017-09-18 04:56:14 +02:00
|
|
|
return nullptr;
|
2017-09-26 15:38:56 +02:00
|
|
|
switch (*(m_pos+1))
|
2017-09-18 04:56:14 +02:00
|
|
|
{
|
2017-09-26 16:04:47 +02:00
|
|
|
case 'b': m_pos += 2; return new_node(ParsedRegex::WordBoundary);
|
|
|
|
case 'B': m_pos += 2; return new_node(ParsedRegex::NotWordBoundary);
|
2017-09-29 04:39:59 +02:00
|
|
|
case 'A': m_pos += 2; return new_node(ParsedRegex::SubjectBegin);
|
|
|
|
case 'z': m_pos += 2; return new_node(ParsedRegex::SubjectEnd);
|
2017-09-26 17:01:06 +02:00
|
|
|
case 'K': m_pos += 2; return new_node(ParsedRegex::ResetStart);
|
2017-09-18 04:56:14 +02:00
|
|
|
}
|
|
|
|
break;
|
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-26 15:38:56 +02:00
|
|
|
AstNodePtr atom()
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
if (at_end())
|
2017-09-25 15:24:24 +02:00
|
|
|
return nullptr;
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
const Codepoint cp = *m_pos;
|
2017-09-25 15:24:24 +02:00
|
|
|
switch (cp)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-26 16:04:47 +02:00
|
|
|
case '.': ++m_pos; return new_node(ParsedRegex::AnyChar);
|
2017-09-17 12:15:43 +02:00
|
|
|
case '(':
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-28 14:43:45 +02:00
|
|
|
auto advance = [&]() {
|
|
|
|
if (++m_pos == m_regex.end())
|
|
|
|
parse_error("unclosed parenthesis");
|
|
|
|
return *m_pos;
|
|
|
|
};
|
|
|
|
|
|
|
|
AstNodePtr content;
|
|
|
|
if (advance() == '?')
|
|
|
|
{
|
|
|
|
auto c = advance();
|
|
|
|
if (c == ':')
|
2017-10-04 16:30:22 +02:00
|
|
|
{
|
|
|
|
++m_pos;
|
2017-09-28 14:43:45 +02:00
|
|
|
content = disjunction(-1);
|
2017-10-04 16:30:22 +02:00
|
|
|
}
|
2017-09-28 14:43:45 +02:00
|
|
|
else if (contains("=!<", c))
|
|
|
|
{
|
|
|
|
bool behind = false;
|
|
|
|
if (c == '<')
|
|
|
|
{
|
|
|
|
advance();
|
|
|
|
behind = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto type = *m_pos++;
|
|
|
|
if (type == '=')
|
|
|
|
content = alternative(behind ? ParsedRegex::LookBehind
|
|
|
|
: ParsedRegex::LookAhead);
|
|
|
|
else if (type == '!')
|
|
|
|
content = alternative(behind ? ParsedRegex::NegativeLookBehind
|
|
|
|
: ParsedRegex::NegativeLookAhead);
|
|
|
|
else
|
|
|
|
parse_error("invalid disjunction");
|
|
|
|
|
|
|
|
validate_lookaround(content);
|
|
|
|
}
|
2017-09-29 05:22:09 +02:00
|
|
|
else if (c == 'i' or c == 'I')
|
|
|
|
{
|
|
|
|
m_ignore_case = c == 'i';
|
|
|
|
if (advance() != ')')
|
|
|
|
parse_error("unclosed parenthesis");
|
|
|
|
++m_pos;
|
|
|
|
return atom(); // get next atom
|
|
|
|
}
|
2017-09-28 14:43:45 +02:00
|
|
|
else
|
|
|
|
parse_error("invalid disjunction");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
content = disjunction(m_parsed_regex.capture_count++);
|
2017-09-19 09:56:21 +02:00
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
if (at_end() or *m_pos != ')')
|
|
|
|
parse_error("unclosed parenthesis");
|
|
|
|
++m_pos;
|
2017-09-17 12:15:43 +02:00
|
|
|
return content;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-23 09:18:21 +02:00
|
|
|
case '\\':
|
2017-09-26 15:38:56 +02:00
|
|
|
++m_pos;
|
|
|
|
return atom_escape();
|
2017-09-23 09:18:21 +02:00
|
|
|
case '[':
|
2017-09-26 15:38:56 +02:00
|
|
|
++m_pos;
|
|
|
|
return character_class();
|
2017-09-26 16:42:54 +02:00
|
|
|
case '|': case ')':
|
|
|
|
return nullptr;
|
2017-09-17 12:15:43 +02:00
|
|
|
default:
|
2017-09-26 16:42:54 +02:00
|
|
|
if (contains("^$.*+?[]{}", cp))
|
|
|
|
parse_error(format("unexpected '{}'", cp));
|
2017-09-26 15:38:56 +02:00
|
|
|
++m_pos;
|
2017-09-26 16:04:47 +02:00
|
|
|
return new_node(ParsedRegex::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-26 15:38:56 +02:00
|
|
|
AstNodePtr atom_escape()
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
const Codepoint cp = *m_pos++;
|
2017-09-23 09:18:21 +02:00
|
|
|
|
2017-09-26 16:51:05 +02:00
|
|
|
if (cp == 'Q')
|
|
|
|
{
|
|
|
|
auto escaped_sequence = new_node(ParsedRegex::Sequence);
|
|
|
|
constexpr StringView end_mark{"\\E"};
|
|
|
|
auto quote_end = std::search(m_pos.base(), m_regex.end(), end_mark.begin(), end_mark.end());
|
|
|
|
while (m_pos != quote_end)
|
|
|
|
escaped_sequence->children.push_back(new_node(ParsedRegex::Literal, *m_pos++));
|
|
|
|
if (quote_end != m_regex.end())
|
|
|
|
m_pos += 2;
|
|
|
|
|
|
|
|
return escaped_sequence;
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:04:41 +02:00
|
|
|
// CharacterClassEscape
|
2017-10-06 13:51:41 +02:00
|
|
|
auto class_it = find_if(character_class_escapes,
|
|
|
|
[cp = to_lower(cp)](auto& c) { return c.cp == cp; });
|
|
|
|
if (class_it != std::end(character_class_escapes))
|
2017-09-26 15:04:41 +02:00
|
|
|
{
|
2017-10-06 13:51:41 +02:00
|
|
|
auto matcher_id = m_parsed_regex.matchers.size();
|
|
|
|
m_parsed_regex.matchers.push_back(
|
|
|
|
[ctype = class_it->ctype ? wctype(class_it->ctype) : (wctype_t)0,
|
|
|
|
chars = class_it->additional_chars, neg = is_upper(cp)] (Codepoint cp) {
|
|
|
|
return ((ctype != 0 and iswctype(cp, ctype)) or contains(chars, cp)) != neg;
|
|
|
|
});
|
|
|
|
return new_node(ParsedRegex::Matcher, matcher_id);
|
2017-09-26 15:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CharacterEscape
|
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-26 16:04:47 +02:00
|
|
|
return new_node(ParsedRegex::Literal, control.value);
|
2017-09-23 09:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TOOD: \c..., \0..., '\0x...', \u...
|
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
if (contains("^$\\.*+?()[]{}|", cp)) // SyntaxCharacter
|
2017-09-26 16:04:47 +02:00
|
|
|
return new_node(ParsedRegex::Literal, cp);
|
2017-09-28 14:43:45 +02:00
|
|
|
parse_error(format("unknown atom escape '{}'", cp));
|
2017-09-23 09:18:21 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
AstNodePtr character_class()
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
const bool negative = m_pos != m_regex.end() and *m_pos == '^';
|
2017-09-23 09:18:21 +02:00
|
|
|
if (negative)
|
2017-09-26 15:38:56 +02:00
|
|
|
++m_pos;
|
2017-09-23 09:18:21 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
struct CharRange { Codepoint min, max; };
|
2017-09-23 09:18:21 +02:00
|
|
|
Vector<CharRange> ranges;
|
2017-09-27 08:04:05 +02:00
|
|
|
Vector<Codepoint> excluded;
|
2017-09-26 15:04:41 +02:00
|
|
|
Vector<std::pair<wctype_t, bool>> ctypes;
|
2017-09-26 15:38:56 +02:00
|
|
|
while (m_pos != m_regex.end() and *m_pos != ']')
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
2017-10-02 07:49:08 +02:00
|
|
|
auto cp = *m_pos++;
|
2017-09-25 15:24:24 +02:00
|
|
|
if (cp == '-')
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
2017-09-26 11:03:12 +02:00
|
|
|
ranges.push_back({ '-', '-' });
|
2017-09-23 09:18:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
if (at_end())
|
2017-09-23 09:18:21 +02:00
|
|
|
break;
|
|
|
|
|
2017-09-26 15:04:41 +02:00
|
|
|
if (cp == '\\')
|
|
|
|
{
|
|
|
|
auto it = find_if(character_class_escapes,
|
2017-10-06 13:51:41 +02:00
|
|
|
[cp = to_lower(*m_pos)](auto& t) { return t.cp == cp; });
|
2017-09-26 15:04:41 +02:00
|
|
|
if (it != std::end(character_class_escapes))
|
|
|
|
{
|
2017-10-06 13:51:41 +02:00
|
|
|
auto negative = is_upper(*m_pos);
|
2017-09-27 08:04:05 +02:00
|
|
|
if (it->ctype)
|
2017-10-06 13:51:41 +02:00
|
|
|
ctypes.push_back({wctype(it->ctype), not negative});
|
|
|
|
for (auto& c : it->additional_chars)
|
2017-09-27 08:04:05 +02:00
|
|
|
{
|
2017-10-06 13:51:41 +02:00
|
|
|
if (negative)
|
2017-09-27 08:04:05 +02:00
|
|
|
excluded.push_back((Codepoint)c);
|
|
|
|
else
|
|
|
|
ranges.push_back({(Codepoint)c, (Codepoint)c});
|
|
|
|
}
|
2017-09-26 15:38:56 +02:00
|
|
|
++m_pos;
|
2017-09-26 15:04:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-10-02 07:49:08 +02:00
|
|
|
else // its just an escaped character
|
|
|
|
{
|
|
|
|
|
|
|
|
if (++m_pos == m_regex.end())
|
|
|
|
break;
|
|
|
|
cp = *m_pos;
|
|
|
|
}
|
2017-09-26 15:04:41 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 11:03:12 +02:00
|
|
|
CharRange range = { cp, cp };
|
2017-09-26 15:38:56 +02:00
|
|
|
if (*m_pos == '-')
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
if (++m_pos == m_regex.end())
|
2017-09-23 09:18:21 +02:00
|
|
|
break;
|
2017-09-26 16:37:21 +02:00
|
|
|
if (*m_pos != ']')
|
|
|
|
{
|
|
|
|
range.max = *m_pos++;
|
|
|
|
if (range.min > range.max)
|
|
|
|
parse_error("invalid range specified");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ranges.push_back(range);
|
|
|
|
range = { '-', '-' };
|
|
|
|
}
|
2017-09-23 09:18:21 +02:00
|
|
|
}
|
|
|
|
ranges.push_back(range);
|
|
|
|
}
|
2017-09-26 15:38:56 +02:00
|
|
|
if (at_end())
|
|
|
|
parse_error("unclosed character class");
|
|
|
|
++m_pos;
|
2017-09-23 09:18:21 +02:00
|
|
|
|
2017-09-29 05:22:09 +02:00
|
|
|
if (m_ignore_case)
|
|
|
|
{
|
|
|
|
for (auto& range : ranges)
|
|
|
|
{
|
|
|
|
range.min = to_lower(range.max);
|
|
|
|
range.max = to_lower(range.max);
|
|
|
|
}
|
|
|
|
for (auto& cp : excluded)
|
|
|
|
cp = to_lower(cp);
|
|
|
|
}
|
|
|
|
|
2017-10-05 03:36:40 +02:00
|
|
|
// Optimize the relatively common case of using a character class to
|
|
|
|
// escape a character, such as [*]
|
|
|
|
if (ctypes.empty() and excluded.empty() and not negative and
|
|
|
|
ranges.size() == 1 and ranges.front().min == ranges.front().max)
|
|
|
|
return new_node(ParsedRegex::Literal, ranges.front().min);
|
|
|
|
|
2017-09-26 15:04:41 +02:00
|
|
|
auto matcher = [ranges = std::move(ranges),
|
2017-09-27 08:04:05 +02:00
|
|
|
ctypes = std::move(ctypes),
|
2017-09-29 05:22:09 +02:00
|
|
|
excluded = std::move(excluded),
|
|
|
|
negative, ignore_case = m_ignore_case] (Codepoint cp) {
|
|
|
|
if (ignore_case)
|
|
|
|
cp = to_lower(cp);
|
|
|
|
|
2017-09-26 11:03:12 +02:00
|
|
|
auto found = contains_that(ranges, [cp](auto& r) {
|
|
|
|
return r.min <= cp and cp <= r.max;
|
2017-09-26 15:04:41 +02:00
|
|
|
}) or contains_that(ctypes, [cp](auto& c) {
|
|
|
|
return (bool)iswctype(cp, c.first) == c.second;
|
2017-09-27 08:04:05 +02:00
|
|
|
}) or (not excluded.empty() and not contains(excluded, cp));
|
2017-09-26 11:03:12 +02:00
|
|
|
return negative ? not found : found;
|
|
|
|
};
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
auto matcher_id = m_parsed_regex.matchers.size();
|
|
|
|
m_parsed_regex.matchers.push_back(std::move(matcher));
|
2017-09-23 09:18:21 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
return new_node(ParsedRegex::Matcher, matcher_id);
|
2017-09-23 09:18:21 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
ParsedRegex::Quantifier quantifier()
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
if (at_end())
|
2017-09-26 16:04:47 +02:00
|
|
|
return {ParsedRegex::Quantifier::One};
|
2017-09-25 15:24:24 +02:00
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
auto read_int = [](auto& pos, auto begin, auto end) {
|
2017-09-18 11:22:11 +02:00
|
|
|
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-28 11:50:04 +02:00
|
|
|
auto check_greedy = [&]() {
|
|
|
|
if (at_end() or *m_pos != '?')
|
|
|
|
return true;
|
|
|
|
++m_pos;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
switch (*m_pos)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-28 11:50:04 +02:00
|
|
|
case '*': ++m_pos; return {ParsedRegex::Quantifier::RepeatZeroOrMore, check_greedy()};
|
|
|
|
case '+': ++m_pos; return {ParsedRegex::Quantifier::RepeatOneOrMore, check_greedy()};
|
|
|
|
case '?': ++m_pos; return {ParsedRegex::Quantifier::Optional, check_greedy()};
|
2017-09-18 11:22:11 +02:00
|
|
|
case '{':
|
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
auto it = m_pos+1;
|
|
|
|
const int min = read_int(it, it, m_regex.end());
|
2017-09-26 13:44:01 +02:00
|
|
|
int max = min;
|
2017-09-18 11:22:11 +02:00
|
|
|
if (*it == ',')
|
|
|
|
{
|
|
|
|
++it;
|
2017-09-26 15:38:56 +02:00
|
|
|
max = read_int(it, it, m_regex.end());
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
|
|
|
if (*it++ != '}')
|
2017-09-26 15:38:56 +02:00
|
|
|
parse_error("expected closing bracket");
|
|
|
|
m_pos = it;
|
2017-10-02 09:35:21 +02:00
|
|
|
return {ParsedRegex::Quantifier::RepeatMinMax, check_greedy(), min, max};
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
2017-09-26 16:04:47 +02:00
|
|
|
default: return {ParsedRegex::Quantifier::One};
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-26 15:04:41 +02:00
|
|
|
|
2017-09-29 05:22:09 +02:00
|
|
|
AstNodePtr new_node(ParsedRegex::Op op, Codepoint value = -1,
|
|
|
|
ParsedRegex::Quantifier quantifier = {ParsedRegex::Quantifier::One})
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
2017-09-29 05:22:09 +02:00
|
|
|
return AstNodePtr{new ParsedRegex::AstNode{op, value, quantifier, m_ignore_case, {}}};
|
2017-09-26 16:04:47 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
bool at_end() const { return m_pos == m_regex.end(); }
|
|
|
|
|
|
|
|
[[gnu::noreturn]]
|
|
|
|
void parse_error(StringView error)
|
|
|
|
{
|
|
|
|
throw runtime_error(format("regex parse error: {} at '{}<<<HERE>>>{}'", error,
|
|
|
|
StringView{m_regex.begin(), m_pos.base()},
|
|
|
|
StringView{m_pos.base(), m_regex.end()}));
|
|
|
|
}
|
|
|
|
|
2017-09-28 14:43:45 +02:00
|
|
|
void validate_lookaround(const AstNodePtr& node)
|
|
|
|
{
|
|
|
|
for (auto& child : node->children)
|
|
|
|
if (child->op != ParsedRegex::Literal)
|
|
|
|
parse_error("Lookaround can only contain literals");
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
ParsedRegex m_parsed_regex;
|
|
|
|
StringView m_regex;
|
|
|
|
Iterator m_pos;
|
2017-09-29 05:22:09 +02:00
|
|
|
bool m_ignore_case = false;
|
2017-09-26 15:38:56 +02:00
|
|
|
|
2017-09-26 15:04:41 +02:00
|
|
|
struct CharacterClassEscape {
|
|
|
|
Codepoint cp;
|
|
|
|
const char* ctype;
|
|
|
|
StringView additional_chars;
|
|
|
|
bool neg;
|
|
|
|
};
|
|
|
|
|
2017-09-27 08:04:05 +02:00
|
|
|
static const CharacterClassEscape character_class_escapes[8];
|
2017-09-26 15:04:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// For some reason Gcc fails to link if this is constexpr
|
2017-09-27 08:04:05 +02:00
|
|
|
const RegexParser::CharacterClassEscape RegexParser::character_class_escapes[8] = {
|
2017-09-26 15:04:41 +02:00
|
|
|
{ 'd', "digit", "", false },
|
|
|
|
{ 'w', "alnum", "_", false },
|
|
|
|
{ 's', "space", "", false },
|
2017-09-27 08:04:05 +02:00
|
|
|
{ 'h', nullptr, " \t", false },
|
2017-09-17 12:15:43 +02:00
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
struct RegexCompiler
|
2017-09-25 15:24:24 +02:00
|
|
|
{
|
2017-09-26 16:04:47 +02:00
|
|
|
RegexCompiler(const ParsedRegex& parsed_regex)
|
|
|
|
: m_parsed_regex{parsed_regex}
|
|
|
|
{
|
|
|
|
compile_node(m_parsed_regex.ast);
|
|
|
|
push_op(CompiledRegex::Match);
|
|
|
|
m_program.matchers = m_parsed_regex.matchers;
|
|
|
|
m_program.save_count = m_parsed_regex.capture_count * 2;
|
2017-10-06 07:40:27 +02:00
|
|
|
m_program.start_chars = compute_start_chars();
|
2017-09-26 16:04:47 +02:00
|
|
|
}
|
2017-09-25 15:24:24 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
CompiledRegex get_compiled_regex() { return std::move(m_program); }
|
2017-09-23 09:18:21 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
using Offset = CompiledRegex::Offset;
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
static CompiledRegex compile(StringView re)
|
2017-09-19 09:56:21 +02:00
|
|
|
{
|
2017-09-26 16:04:47 +02:00
|
|
|
return RegexCompiler{RegexParser::parse(re)}.get_compiled_regex();
|
2017-09-19 09:56:21 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
private:
|
|
|
|
Offset compile_node_inner(const ParsedRegex::AstNodePtr& node)
|
|
|
|
{
|
|
|
|
const auto start_pos = m_program.bytecode.size();
|
|
|
|
|
|
|
|
const Codepoint capture = (node->op == ParsedRegex::Alternation or node->op == ParsedRegex::Sequence) ? node->value : -1;
|
|
|
|
if (capture != -1)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-26 16:04:47 +02:00
|
|
|
push_op(CompiledRegex::Save);
|
|
|
|
push_byte(capture * 2);
|
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
Vector<Offset> goto_inner_end_offsets;
|
|
|
|
switch (node->op)
|
|
|
|
{
|
|
|
|
case ParsedRegex::Literal:
|
2017-09-29 05:22:09 +02:00
|
|
|
push_op(node->ignore_case ? CompiledRegex::LiteralIgnoreCase
|
|
|
|
: CompiledRegex::Literal);
|
|
|
|
push_codepoint(node->ignore_case ? to_lower(node->value)
|
|
|
|
: node->value);
|
2017-09-26 16:04:47 +02:00
|
|
|
break;
|
|
|
|
case ParsedRegex::AnyChar:
|
|
|
|
push_op(CompiledRegex::AnyChar);
|
|
|
|
break;
|
|
|
|
case ParsedRegex::Matcher:
|
|
|
|
push_op(CompiledRegex::Matcher);
|
|
|
|
push_byte(node->value);
|
|
|
|
case ParsedRegex::Sequence:
|
|
|
|
for (auto& child : node->children)
|
|
|
|
compile_node(child);
|
|
|
|
break;
|
|
|
|
case ParsedRegex::Alternation:
|
|
|
|
{
|
|
|
|
auto& children = node->children;
|
|
|
|
kak_assert(children.size() == 2);
|
|
|
|
|
|
|
|
push_op(CompiledRegex::Split_PrioritizeParent);
|
|
|
|
auto offset = alloc_offset();
|
2017-09-17 13:08:10 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
compile_node(children[0]);
|
|
|
|
push_op(CompiledRegex::Jump);
|
|
|
|
goto_inner_end_offsets.push_back(alloc_offset());
|
2017-09-17 13:08:10 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
auto right_pos = compile_node(children[1]);
|
|
|
|
get_offset(offset) = right_pos;
|
2017-09-17 13:08:10 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-09-28 14:43:45 +02:00
|
|
|
case ParsedRegex::LookAhead:
|
|
|
|
push_op(CompiledRegex::LookAhead);
|
|
|
|
push_string(node->children);
|
|
|
|
break;
|
|
|
|
case ParsedRegex::NegativeLookAhead:
|
|
|
|
push_op(CompiledRegex::NegativeLookAhead);
|
|
|
|
push_string(node->children);
|
|
|
|
break;
|
2017-10-04 17:00:19 +02:00
|
|
|
case ParsedRegex::LookBehind:
|
|
|
|
push_op(CompiledRegex::LookBehind);
|
|
|
|
push_string(node->children, true);
|
|
|
|
break;
|
2017-09-28 14:43:45 +02:00
|
|
|
case ParsedRegex::NegativeLookBehind:
|
|
|
|
push_op(CompiledRegex::NegativeLookBehind);
|
|
|
|
push_string(node->children, true);
|
|
|
|
break;
|
2017-09-26 16:04:47 +02:00
|
|
|
case ParsedRegex::LineStart:
|
|
|
|
push_op(CompiledRegex::LineStart);
|
|
|
|
break;
|
|
|
|
case ParsedRegex::LineEnd:
|
|
|
|
push_op(CompiledRegex::LineEnd);
|
|
|
|
break;
|
|
|
|
case ParsedRegex::WordBoundary:
|
|
|
|
push_op(CompiledRegex::WordBoundary);
|
|
|
|
break;
|
|
|
|
case ParsedRegex::NotWordBoundary:
|
|
|
|
push_op(CompiledRegex::NotWordBoundary);
|
|
|
|
break;
|
|
|
|
case ParsedRegex::SubjectBegin:
|
|
|
|
push_op(CompiledRegex::SubjectBegin);
|
|
|
|
break;
|
|
|
|
case ParsedRegex::SubjectEnd:
|
|
|
|
push_op(CompiledRegex::SubjectEnd);
|
|
|
|
break;
|
2017-09-26 17:01:06 +02:00
|
|
|
case ParsedRegex::ResetStart:
|
|
|
|
push_op(CompiledRegex::Save);
|
|
|
|
push_byte(0);
|
|
|
|
break;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
for (auto& offset : goto_inner_end_offsets)
|
|
|
|
get_offset(offset) = m_program.bytecode.size();
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
if (capture != -1)
|
|
|
|
{
|
|
|
|
push_op(CompiledRegex::Save);
|
|
|
|
push_byte(capture * 2 + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return start_pos;
|
2017-09-19 09:56:21 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
Offset compile_node(const ParsedRegex::AstNodePtr& node)
|
|
|
|
{
|
|
|
|
Offset pos = m_program.bytecode.size();
|
|
|
|
Vector<Offset> goto_end_offsets;
|
2017-09-18 11:22:11 +02:00
|
|
|
|
2017-09-28 11:50:04 +02:00
|
|
|
auto& quantifier = node->quantifier;
|
|
|
|
|
|
|
|
if (quantifier.allows_none())
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
2017-09-28 11:50:04 +02:00
|
|
|
push_op(quantifier.greedy ? CompiledRegex::Split_PrioritizeParent
|
|
|
|
: CompiledRegex::Split_PrioritizeChild);
|
2017-09-26 16:04:47 +02:00
|
|
|
goto_end_offsets.push_back(alloc_offset());
|
|
|
|
}
|
2017-09-18 11:22:11 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
auto inner_pos = compile_node_inner(node);
|
|
|
|
// Write the node multiple times when we have a min count quantifier
|
2017-09-28 11:50:04 +02:00
|
|
|
for (int i = 1; i < quantifier.min; ++i)
|
2017-09-26 16:04:47 +02:00
|
|
|
inner_pos = compile_node_inner(node);
|
2017-09-18 11:22:11 +02:00
|
|
|
|
2017-09-28 11:50:04 +02:00
|
|
|
if (quantifier.allows_infinite_repeat())
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
2017-09-28 11:50:04 +02:00
|
|
|
push_op(quantifier.greedy ? CompiledRegex::Split_PrioritizeChild
|
|
|
|
: CompiledRegex::Split_PrioritizeParent);
|
2017-09-26 16:04:47 +02:00
|
|
|
get_offset(alloc_offset()) = inner_pos;
|
|
|
|
}
|
|
|
|
// Write the node as an optional match for the min -> max counts
|
2017-09-28 11:50:04 +02:00
|
|
|
else for (int i = std::max(1, quantifier.min); // STILL UGLY !
|
|
|
|
i < quantifier.max; ++i)
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
2017-09-28 11:50:04 +02:00
|
|
|
push_op(quantifier.greedy ? CompiledRegex::Split_PrioritizeParent
|
|
|
|
: CompiledRegex::Split_PrioritizeChild);
|
2017-09-26 16:04:47 +02:00
|
|
|
goto_end_offsets.push_back(alloc_offset());
|
|
|
|
compile_node_inner(node);
|
|
|
|
}
|
2017-09-18 11:22:11 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
for (auto offset : goto_end_offsets)
|
|
|
|
get_offset(offset) = m_program.bytecode.size();
|
|
|
|
|
|
|
|
return pos;
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
2017-09-26 16:04:47 +02:00
|
|
|
|
|
|
|
Offset alloc_offset()
|
|
|
|
{
|
|
|
|
auto pos = m_program.bytecode.size();
|
|
|
|
m_program.bytecode.resize(pos + sizeof(Offset));
|
|
|
|
return pos;
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
Offset& get_offset(Offset pos)
|
|
|
|
{
|
|
|
|
return *reinterpret_cast<Offset*>(&m_program.bytecode[pos]);
|
|
|
|
}
|
2017-09-19 11:26:24 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
void push_op(CompiledRegex::Op op)
|
|
|
|
{
|
|
|
|
m_program.bytecode.push_back(op);
|
|
|
|
}
|
2017-09-19 11:26:24 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
void push_byte(char byte)
|
|
|
|
{
|
|
|
|
m_program.bytecode.push_back(byte);
|
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
void push_codepoint(Codepoint cp)
|
|
|
|
{
|
|
|
|
utf8::dump(std::back_inserter(m_program.bytecode), cp);
|
|
|
|
}
|
2017-09-19 10:44:58 +02:00
|
|
|
|
2017-09-28 14:43:45 +02:00
|
|
|
void push_string(const Vector<ParsedRegex::AstNodePtr>& codepoints, bool reversed = false)
|
|
|
|
{
|
|
|
|
if (codepoints.size() > 127)
|
|
|
|
throw runtime_error{"Too long literal string"};
|
|
|
|
|
|
|
|
push_byte(codepoints.size());
|
|
|
|
if (reversed)
|
|
|
|
for (auto& cp : codepoints | reverse())
|
|
|
|
push_codepoint(cp->value);
|
|
|
|
else
|
|
|
|
for (auto& cp : codepoints)
|
|
|
|
push_codepoint(cp->value);
|
|
|
|
}
|
|
|
|
|
2017-10-06 07:40:27 +02:00
|
|
|
// Fills accepted and rejected according to which chars can start the given node,
|
|
|
|
// returns true if the node did not consume the char, hence a following node in
|
|
|
|
// sequence would be still relevant for the parent node start chars computation.
|
|
|
|
bool compute_start_chars(const ParsedRegex::AstNodePtr& node,
|
|
|
|
bool (&accepted)[256], bool (&rejected)[256]) const
|
|
|
|
{
|
|
|
|
switch (node->op)
|
|
|
|
{
|
|
|
|
case ParsedRegex::Literal:
|
|
|
|
if (node->value < 256)
|
|
|
|
accepted[node->value] = true;
|
|
|
|
return node->quantifier.allows_none();
|
|
|
|
case ParsedRegex::AnyChar:
|
|
|
|
for (auto& b : accepted)
|
|
|
|
b = true;
|
|
|
|
return node->quantifier.allows_none();
|
|
|
|
case ParsedRegex::Matcher:
|
|
|
|
for (auto& b : accepted) // treat matcher as everything can match for now
|
|
|
|
b = true;
|
|
|
|
return node->quantifier.allows_none();
|
|
|
|
case ParsedRegex::Sequence:
|
|
|
|
{
|
|
|
|
bool consumed = false;
|
|
|
|
for (auto& child : node->children)
|
|
|
|
{
|
|
|
|
if (not compute_start_chars(child, accepted, rejected))
|
|
|
|
{
|
|
|
|
consumed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return not consumed or node->quantifier.allows_none();
|
|
|
|
}
|
|
|
|
case ParsedRegex::Alternation:
|
|
|
|
{
|
|
|
|
bool all_consumed = not node->quantifier.allows_none();
|
|
|
|
for (auto& child : node->children)
|
|
|
|
{
|
|
|
|
if (compute_start_chars(child, accepted, rejected))
|
|
|
|
all_consumed = false;
|
|
|
|
}
|
|
|
|
return not all_consumed;
|
|
|
|
}
|
|
|
|
case ParsedRegex::LineStart:
|
|
|
|
case ParsedRegex::LineEnd:
|
|
|
|
case ParsedRegex::WordBoundary:
|
|
|
|
case ParsedRegex::NotWordBoundary:
|
|
|
|
case ParsedRegex::SubjectBegin:
|
|
|
|
case ParsedRegex::SubjectEnd:
|
|
|
|
case ParsedRegex::ResetStart:
|
|
|
|
return true;
|
|
|
|
case ParsedRegex::LookAhead:
|
|
|
|
if (node->children.empty())
|
|
|
|
compute_start_chars(node->children.front(), accepted, rejected);
|
|
|
|
return true;
|
|
|
|
case ParsedRegex::NegativeLookAhead:
|
|
|
|
if (node->children.empty())
|
|
|
|
compute_start_chars(node->children.front(), rejected, accepted);
|
|
|
|
return true;
|
|
|
|
case ParsedRegex::LookBehind:
|
|
|
|
return true;
|
|
|
|
case ParsedRegex::NegativeLookBehind:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CompiledRegex::StartChars> compute_start_chars() const
|
|
|
|
{
|
|
|
|
bool accepted[256] = {};
|
|
|
|
bool rejected[256] = {};
|
|
|
|
if (compute_start_chars(m_parsed_regex.ast, accepted, rejected))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto start_chars = std::make_unique<CompiledRegex::StartChars>();
|
|
|
|
for (int i = 0; i < 256; ++i)
|
|
|
|
start_chars->map[i] = accepted[i] and not rejected[i];
|
|
|
|
|
|
|
|
return start_chars;
|
|
|
|
}
|
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
CompiledRegex m_program;
|
|
|
|
const ParsedRegex& m_parsed_regex;
|
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-26 16:31:18 +02:00
|
|
|
void dump_regex(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-29 05:22:09 +02:00
|
|
|
case CompiledRegex::LiteralIgnoreCase:
|
|
|
|
printf("literal (ignore case) %lc\n", utf8::read_codepoint(pos, (const char*)nullptr));
|
|
|
|
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-26 11:03:12 +02:00
|
|
|
case CompiledRegex::Matcher:
|
|
|
|
printf("matcher %d\n", *pos++);
|
2017-09-23 09:18:21 +02:00
|
|
|
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-28 14:43:45 +02:00
|
|
|
case CompiledRegex::LookAhead:
|
|
|
|
case CompiledRegex::NegativeLookAhead:
|
|
|
|
case CompiledRegex::LookBehind:
|
|
|
|
case CompiledRegex::NegativeLookBehind:
|
|
|
|
{
|
|
|
|
int count = *pos++;
|
|
|
|
StringView str{pos, pos + count};
|
|
|
|
const char* name = nullptr;
|
|
|
|
if (op == CompiledRegex::LookAhead)
|
|
|
|
name = "look ahead";
|
|
|
|
if (op == CompiledRegex::NegativeLookAhead)
|
|
|
|
name = "negative look ahead";
|
|
|
|
if (op == CompiledRegex::LookBehind)
|
|
|
|
name = "look behind";
|
|
|
|
if (op == CompiledRegex::NegativeLookBehind)
|
|
|
|
name = "negative look behind";
|
|
|
|
|
|
|
|
printf("%s (%s)\n", name, (const char*)str.zstr());
|
|
|
|
pos += count;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Match:
|
2017-09-17 10:50:53 +02:00
|
|
|
printf("match\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 08:59:04 +02:00
|
|
|
CompiledRegex compile_regex(StringView re)
|
2017-09-26 09:44:30 +02:00
|
|
|
{
|
2017-10-02 08:59:04 +02:00
|
|
|
CompiledRegex res;
|
2017-09-26 09:44:30 +02:00
|
|
|
try
|
|
|
|
{
|
2017-10-02 08:59:04 +02:00
|
|
|
res = RegexCompiler::compile(re);
|
2017-09-26 09:44:30 +02:00
|
|
|
}
|
|
|
|
catch (runtime_error& err)
|
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
write_to_debug_buffer(err.what());
|
2017-09-26 09:44:30 +02:00
|
|
|
}
|
2017-10-02 08:59:04 +02:00
|
|
|
return std::move(res);
|
2017-09-26 09:44:30 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 10:50:53 +02:00
|
|
|
auto test_regex = UnitTest{[]{
|
2017-10-02 10:50:41 +02:00
|
|
|
struct TestVM : CompiledRegex, ThreadedRegexVM<const char*>
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-26 16:31:18 +02:00
|
|
|
TestVM(StringView re, bool dump = false)
|
2017-10-02 10:50:41 +02:00
|
|
|
: CompiledRegex{RegexCompiler::compile(re)},
|
|
|
|
ThreadedRegexVM{(const CompiledRegex&)*this}
|
|
|
|
{ if (dump) dump_regex(*this); }
|
2017-09-26 16:16:35 +02:00
|
|
|
|
2017-10-02 08:59:04 +02:00
|
|
|
bool exec(StringView re, bool match = true, bool longest = false)
|
|
|
|
{
|
2017-10-02 16:34:57 +02:00
|
|
|
RegexExecFlags flags = RegexExecFlags::None;
|
|
|
|
if (not match)
|
|
|
|
flags |= RegexExecFlags::Search;
|
|
|
|
if (not longest)
|
|
|
|
flags |= RegexExecFlags::AnyMatch;
|
|
|
|
|
|
|
|
return ThreadedRegexVM::exec(re.begin(), re.end(), flags);
|
2017-10-02 08:59:04 +02:00
|
|
|
}
|
2017-09-26 16:16:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"(a*b)"};
|
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-26 16:16:35 +02:00
|
|
|
TestVM vm{R"(^a.*b$)"};
|
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-26 16:16:35 +02:00
|
|
|
TestVM vm{R"(^(foo|qux|baz)+(bar)?baz$)"};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("fooquxbarbaz"));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[2], vm.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-26 16:16:35 +02:00
|
|
|
TestVM vm{R"(.*\b(foo|bar)\b.*)"};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("qux foo baz"));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[2], vm.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-26 16:16:35 +02:00
|
|
|
|
2017-09-18 05:47:10 +02:00
|
|
|
{
|
2017-09-26 16:16:35 +02:00
|
|
|
TestVM vm{R"((foo|bar))"};
|
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-26 16:16:35 +02:00
|
|
|
TestVM vm{R"(a{3,5}b)"};
|
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-26 16:16:35 +02:00
|
|
|
TestVM vm{R"(a{3}b)"};
|
2017-09-26 13:44:01 +02:00
|
|
|
kak_assert(not vm.exec("aab"));
|
|
|
|
kak_assert(vm.exec("aaab"));
|
|
|
|
kak_assert(not vm.exec("aaaab"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-09-26 16:16:35 +02:00
|
|
|
TestVM vm{R"(a{3,}b)"};
|
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-26 16:16:35 +02:00
|
|
|
TestVM vm{R"(a{,3}b)"};
|
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-26 16:16:35 +02:00
|
|
|
TestVM vm{R"(f.*a(.*o))"};
|
2017-09-24 16:14:35 +02:00
|
|
|
kak_assert(vm.exec("blahfoobarfoobaz", false, true));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "foobarfoo");
|
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == "rfoo");
|
2017-09-24 16:14:35 +02:00
|
|
|
kak_assert(vm.exec("mais que fais la police", false, true));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "fais la po");
|
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == " po");
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
2017-09-23 09:18:21 +02:00
|
|
|
|
|
|
|
{
|
2017-09-26 16:37:21 +02:00
|
|
|
TestVM vm{R"([àb-dX-Z-]{3,5})"};
|
|
|
|
kak_assert(vm.exec("cà-Y"));
|
2017-09-25 15:24:24 +02:00
|
|
|
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-26 15:04:41 +02:00
|
|
|
|
2017-10-02 09:35:21 +02:00
|
|
|
{
|
|
|
|
TestVM vm{R"((a{3,5})a+)"};
|
|
|
|
kak_assert(vm.exec("aaaaaa", true, true));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == "aaaaa");
|
2017-10-02 09:35:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"((a{3,5}?)a+)"};
|
|
|
|
kak_assert(vm.exec("aaaaaa", true, true));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == "aaa");
|
2017-10-02 09:35:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"((a{3,5}?)a)"};
|
|
|
|
kak_assert(vm.exec("aaaa"));
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:04:41 +02:00
|
|
|
{
|
2017-09-26 16:16:35 +02:00
|
|
|
TestVM vm{R"(\d{3})"};
|
2017-09-26 15:04:41 +02:00
|
|
|
kak_assert(vm.exec("123"));
|
|
|
|
kak_assert(not vm.exec("1x3"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-09-26 16:16:35 +02:00
|
|
|
TestVM vm{R"([-\d]+)"};
|
2017-09-26 15:04:41 +02:00
|
|
|
kak_assert(vm.exec("123-456"));
|
|
|
|
kak_assert(not vm.exec("123_456"));
|
|
|
|
}
|
2017-09-26 16:51:05 +02:00
|
|
|
|
2017-09-27 08:04:05 +02:00
|
|
|
{
|
|
|
|
TestVM vm{R"([ \H]+)"};
|
|
|
|
kak_assert(vm.exec("abc "));
|
|
|
|
kak_assert(not vm.exec("a \t"));
|
|
|
|
}
|
|
|
|
|
2017-09-26 16:51:05 +02:00
|
|
|
{
|
|
|
|
TestVM vm{R"(\Q{}[]*+?\Ea+)"};
|
|
|
|
kak_assert(vm.exec("{}[]*+?aa"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"(\Q...)"};
|
|
|
|
kak_assert(vm.exec("..."));
|
|
|
|
kak_assert(not vm.exec("bla"));
|
|
|
|
}
|
2017-09-26 17:01:06 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"(foo\Kbar)"};
|
|
|
|
kak_assert(vm.exec("foobar", true, true));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "bar");
|
2017-09-26 17:01:06 +02:00
|
|
|
kak_assert(not vm.exec("bar", true, true));
|
|
|
|
}
|
2017-09-28 11:50:04 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"((fo+?).*)"};
|
|
|
|
kak_assert(vm.exec("foooo", true, true));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == "fo");
|
2017-09-28 11:50:04 +02:00
|
|
|
}
|
2017-09-28 14:43:45 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"((?=foo).)"};
|
|
|
|
kak_assert(vm.exec("barfoo", false, true));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "f");
|
2017-09-28 14:43:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"((?!foo)...)"};
|
|
|
|
kak_assert(not vm.exec("foo"));
|
|
|
|
kak_assert(vm.exec("qux"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"(...(?<=foo))"};
|
|
|
|
kak_assert(vm.exec("foo"));
|
|
|
|
kak_assert(not vm.exec("qux"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"(...(?<!foo))"};
|
|
|
|
kak_assert(not vm.exec("foo"));
|
|
|
|
kak_assert(vm.exec("qux"));
|
|
|
|
}
|
2017-09-29 05:22:09 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"(Foo(?i)f[oB]+)"};
|
|
|
|
kak_assert(vm.exec("FooFOoBb"));
|
|
|
|
}
|
2017-10-02 07:49:08 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"([^\]]+)"};
|
|
|
|
kak_assert(not vm.exec("a]c"));
|
|
|
|
kak_assert(vm.exec("abc"));
|
|
|
|
}
|
2017-10-04 16:30:22 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"((?:foo)+)"};
|
|
|
|
kak_assert(vm.exec("foofoofoo"));
|
|
|
|
kak_assert(not vm.exec("barbarbar"));
|
|
|
|
}
|
2017-10-06 07:40:27 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"((?<!\\)(?:\\\\)*")"};
|
|
|
|
kak_assert(vm.exec("foo\"", false));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TestVM vm{R"($)"};
|
|
|
|
kak_assert(vm.exec("foo\n", false, true));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(*vm.captures()[0] == '\n');
|
2017-10-06 07:40:27 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
}
|