2016-10-23 02:22:48 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* regex.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-10-23 02:22:48 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "regex.h"
|
|
|
|
#include <wchar.h>
|
2017-03-05 16:44:50 +01:00
|
|
|
#include <wctype.h>
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static int RegEx_hex2int(const CharType c) {
|
2016-10-23 02:22:48 +02:00
|
|
|
if ('0' <= c && c <= '9')
|
|
|
|
return int(c - '0');
|
|
|
|
else if ('a' <= c && c <= 'f')
|
|
|
|
return int(c - 'a') + 10;
|
|
|
|
else if ('A' <= c && c <= 'F')
|
|
|
|
return int(c - 'A') + 10;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RegExSearch {
|
|
|
|
|
|
|
|
Ref<RegExMatch> match;
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *str;
|
2016-10-23 02:22:48 +02:00
|
|
|
int end;
|
|
|
|
int eof;
|
|
|
|
|
|
|
|
// For standard quantifier behaviour, test_parent is used to check the
|
|
|
|
// rest of the pattern. If the pattern matches, to prevent the parent
|
|
|
|
// from testing again, the complete flag is used as a shortcut out.
|
|
|
|
bool complete;
|
|
|
|
|
|
|
|
// With lookahead, the position needs to rewind to its starting position
|
|
|
|
// when test_parent is used. Due to functional programming, this state
|
|
|
|
// has to be kept as a parameter.
|
|
|
|
Vector<int> lookahead_pos;
|
|
|
|
|
|
|
|
CharType at(int p_pos) {
|
|
|
|
return str[p_pos];
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExSearch(Ref<RegExMatch> &p_match, int p_end, int p_lookahead)
|
|
|
|
: match(p_match) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
str = p_match->string.c_str();
|
|
|
|
end = p_end;
|
|
|
|
eof = p_match->string.length();
|
|
|
|
complete = false;
|
|
|
|
lookahead_pos.resize(p_lookahead);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNode {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExNode *next;
|
|
|
|
RegExNode *previous;
|
|
|
|
RegExNode *parent;
|
2016-10-23 02:22:48 +02:00
|
|
|
bool quantifiable;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
RegExNode() {
|
|
|
|
|
|
|
|
next = NULL;
|
|
|
|
previous = NULL;
|
|
|
|
parent = NULL;
|
|
|
|
quantifiable = false;
|
|
|
|
length = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~RegExNode() {
|
|
|
|
|
|
|
|
if (next)
|
|
|
|
memdelete(next);
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:16:46 +01:00
|
|
|
// For avoiding RTTI
|
|
|
|
virtual bool is_look_behind() { return false; }
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
return next ? next->test(s, pos) : -1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test_parent(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
if (next)
|
|
|
|
pos = next->test(s, pos);
|
|
|
|
|
|
|
|
if (pos >= 0) {
|
|
|
|
s.complete = true;
|
|
|
|
if (parent)
|
|
|
|
pos = parent->test_parent(s, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos < 0)
|
|
|
|
s.complete = false;
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void increment_length(int amount, bool subtract = false) {
|
|
|
|
|
|
|
|
if (amount >= 0 && length >= 0) {
|
|
|
|
if (!subtract)
|
|
|
|
length += amount;
|
|
|
|
else
|
|
|
|
length -= amount;
|
|
|
|
} else {
|
|
|
|
length = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
parent->increment_length(amount, subtract);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeChar : public RegExNode {
|
|
|
|
|
|
|
|
CharType ch;
|
|
|
|
|
|
|
|
RegExNodeChar(CharType p_char) {
|
|
|
|
|
|
|
|
length = 1;
|
|
|
|
quantifiable = true;
|
|
|
|
ch = p_char;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
if (s.end <= pos || 0 > pos || s.at(pos) != ch)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return next ? next->test(s, pos + 1) : pos + 1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static CharType parse_escape(const CharType *&c) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
int point = 0;
|
|
|
|
switch (c[1]) {
|
|
|
|
case 'x':
|
|
|
|
for (int i = 2; i <= 3; ++i) {
|
|
|
|
int res = RegEx_hex2int(c[i]);
|
|
|
|
if (res == -1)
|
|
|
|
return '\0';
|
|
|
|
point = (point << 4) + res;
|
|
|
|
}
|
|
|
|
c = &c[3];
|
|
|
|
return CharType(point);
|
|
|
|
case 'u':
|
|
|
|
for (int i = 2; i <= 5; ++i) {
|
|
|
|
int res = RegEx_hex2int(c[i]);
|
|
|
|
if (res == -1)
|
|
|
|
return '\0';
|
|
|
|
point = (point << 4) + res;
|
|
|
|
}
|
|
|
|
c = &c[5];
|
|
|
|
return CharType(point);
|
|
|
|
case '0': ++c; return '\0';
|
|
|
|
case 'a': ++c; return '\a';
|
|
|
|
case 'e': ++c; return '\e';
|
|
|
|
case 'f': ++c; return '\f';
|
|
|
|
case 'n': ++c; return '\n';
|
|
|
|
case 'r': ++c; return '\r';
|
|
|
|
case 't': ++c; return '\t';
|
|
|
|
case 'v': ++c; return '\v';
|
|
|
|
case 'b': ++c; return '\b';
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return (++c)[0];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeRange : public RegExNode {
|
|
|
|
|
|
|
|
CharType start;
|
|
|
|
CharType end;
|
|
|
|
|
|
|
|
RegExNodeRange(CharType p_start, CharType p_end) {
|
|
|
|
|
|
|
|
length = 1;
|
|
|
|
quantifiable = true;
|
|
|
|
start = p_start;
|
|
|
|
end = p_end;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
if (s.end <= pos || 0 > pos)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
CharType c = s.at(pos);
|
|
|
|
if (c < start || end < c)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return next ? next->test(s, pos + 1) : pos + 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeShorthand : public RegExNode {
|
|
|
|
|
|
|
|
CharType repr;
|
|
|
|
|
|
|
|
RegExNodeShorthand(CharType p_repr) {
|
|
|
|
|
|
|
|
length = 1;
|
|
|
|
quantifiable = true;
|
|
|
|
repr = p_repr;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
if (s.end <= pos || 0 > pos)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
bool invert = false;
|
|
|
|
CharType c = s.at(pos);
|
|
|
|
switch (repr) {
|
|
|
|
case '.':
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
invert = true;
|
|
|
|
case 'w':
|
|
|
|
found = (c == '_' || iswalnum(c) != 0);
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
invert = true;
|
|
|
|
case 'd':
|
|
|
|
found = ('0' <= c && c <= '9');
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
invert = true;
|
|
|
|
case 's':
|
|
|
|
found = (iswspace(c) != 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found == invert)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return next ? next->test(s, pos + 1) : pos + 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeClass : public RegExNode {
|
|
|
|
|
|
|
|
enum Type {
|
|
|
|
Type_none,
|
|
|
|
Type_alnum,
|
|
|
|
Type_alpha,
|
|
|
|
Type_ascii,
|
|
|
|
Type_blank,
|
|
|
|
Type_cntrl,
|
|
|
|
Type_digit,
|
|
|
|
Type_graph,
|
|
|
|
Type_lower,
|
|
|
|
Type_print,
|
|
|
|
Type_punct,
|
|
|
|
Type_space,
|
|
|
|
Type_upper,
|
|
|
|
Type_xdigit,
|
|
|
|
Type_word
|
|
|
|
};
|
|
|
|
|
|
|
|
Type type;
|
|
|
|
|
|
|
|
bool test_class(CharType c) const {
|
|
|
|
|
|
|
|
static Vector<CharType> REGEX_NODE_SPACE = String(" \t\r\n\f");
|
|
|
|
static Vector<CharType> REGEX_NODE_PUNCT = String("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case Type_alnum:
|
|
|
|
if ('0' <= c && c <= '9') return true;
|
|
|
|
if ('a' <= c && c <= 'z') return true;
|
|
|
|
if ('A' <= c && c <= 'Z') return true;
|
|
|
|
return false;
|
|
|
|
case Type_alpha:
|
|
|
|
if ('a' <= c && c <= 'z') return true;
|
|
|
|
if ('A' <= c && c <= 'Z') return true;
|
|
|
|
return false;
|
|
|
|
case Type_ascii:
|
|
|
|
return (0x00 <= c && c <= 0x7F);
|
|
|
|
case Type_blank:
|
|
|
|
return (c == ' ' || c == '\t');
|
|
|
|
case Type_cntrl:
|
|
|
|
return ((0x00 <= c && c <= 0x1F) || c == 0x7F);
|
|
|
|
case Type_digit:
|
|
|
|
return ('0' <= c && c <= '9');
|
|
|
|
case Type_graph:
|
|
|
|
return (0x20 < c && c < 0x7F);
|
|
|
|
case Type_lower:
|
|
|
|
return ('a' <= c && c <= 'z');
|
|
|
|
case Type_print:
|
2017-02-23 09:28:09 +01:00
|
|
|
return (0x20 < c && c < 0x7f);
|
2016-10-23 02:22:48 +02:00
|
|
|
case Type_punct:
|
|
|
|
return (REGEX_NODE_PUNCT.find(c) >= 0);
|
|
|
|
case Type_space:
|
|
|
|
return (REGEX_NODE_SPACE.find(c) >= 0);
|
|
|
|
case Type_upper:
|
|
|
|
return ('A' <= c && c <= 'Z');
|
|
|
|
case Type_xdigit:
|
|
|
|
if ('0' <= c && c <= '9') return true;
|
|
|
|
if ('a' <= c && c <= 'f') return true;
|
|
|
|
if ('A' <= c && c <= 'F') return true;
|
|
|
|
return false;
|
|
|
|
case Type_word:
|
|
|
|
if ('0' <= c && c <= '9') return true;
|
|
|
|
if ('a' <= c && c <= 'z') return true;
|
|
|
|
if ('A' <= c && c <= 'Z') return true;
|
|
|
|
return (c == '_');
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegExNodeClass(Type p_type) {
|
|
|
|
|
|
|
|
length = 1;
|
|
|
|
quantifiable = true;
|
|
|
|
type = p_type;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
if (s.end <= pos || 0 > pos)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!test_class(s.at(pos)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return next ? next->test(s, pos + 1) : pos + 1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define REGEX_CMP_CLASS(POS, NAME) \
|
|
|
|
if (cmp_class(POS, #NAME)) return Type_##NAME
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static Type parse_type(const CharType *&p_pos) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
REGEX_CMP_CLASS(p_pos, alnum);
|
|
|
|
REGEX_CMP_CLASS(p_pos, alpha);
|
|
|
|
REGEX_CMP_CLASS(p_pos, ascii);
|
|
|
|
REGEX_CMP_CLASS(p_pos, blank);
|
|
|
|
REGEX_CMP_CLASS(p_pos, cntrl);
|
|
|
|
REGEX_CMP_CLASS(p_pos, digit);
|
|
|
|
REGEX_CMP_CLASS(p_pos, graph);
|
|
|
|
REGEX_CMP_CLASS(p_pos, lower);
|
|
|
|
REGEX_CMP_CLASS(p_pos, print);
|
|
|
|
REGEX_CMP_CLASS(p_pos, punct);
|
|
|
|
REGEX_CMP_CLASS(p_pos, space);
|
|
|
|
REGEX_CMP_CLASS(p_pos, upper);
|
|
|
|
REGEX_CMP_CLASS(p_pos, xdigit);
|
|
|
|
REGEX_CMP_CLASS(p_pos, word);
|
|
|
|
return Type_none;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static bool cmp_class(const CharType *&p_pos, const char *p_text) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
unsigned int i = 0;
|
|
|
|
for (i = 0; p_text[i] != '\0'; ++i)
|
|
|
|
if (p_pos[i] != p_text[i])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (p_pos[i++] != ':' || p_pos[i] != ']')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
p_pos = &p_pos[i];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeAnchorStart : public RegExNode {
|
|
|
|
|
|
|
|
RegExNodeAnchorStart() {
|
|
|
|
|
|
|
|
length = 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
if (pos != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return next ? next->test(s, pos) : pos;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeAnchorEnd : public RegExNode {
|
|
|
|
|
|
|
|
RegExNodeAnchorEnd() {
|
|
|
|
|
|
|
|
length = 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
if (pos != s.eof)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return next ? next->test(s, pos) : pos;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeWordBoundary : public RegExNode {
|
|
|
|
|
|
|
|
bool inverse;
|
|
|
|
|
|
|
|
RegExNodeWordBoundary(bool p_inverse) {
|
|
|
|
|
|
|
|
length = 0;
|
|
|
|
inverse = p_inverse;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
bool left = false;
|
|
|
|
bool right = false;
|
|
|
|
|
|
|
|
if (pos != 0) {
|
|
|
|
CharType c = s.at(pos - 1);
|
|
|
|
if (c == '_' || iswalnum(c))
|
|
|
|
left = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos != s.eof) {
|
|
|
|
CharType c = s.at(pos);
|
|
|
|
if (c == '_' || iswalnum(c))
|
|
|
|
right = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((left == right) != inverse)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return next ? next->test(s, pos) : pos;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeQuantifier : public RegExNode {
|
|
|
|
|
|
|
|
int min;
|
|
|
|
int max;
|
|
|
|
bool greedy;
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExNode *child;
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
RegExNodeQuantifier(int p_min, int p_max) {
|
|
|
|
|
|
|
|
min = p_min;
|
|
|
|
max = p_max;
|
|
|
|
greedy = true;
|
|
|
|
child = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
~RegExNodeQuantifier() {
|
|
|
|
|
|
|
|
if (child)
|
|
|
|
memdelete(child);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
return test_step(s, pos, 0, pos);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test_parent(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
s.complete = false;
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int test_step(RegExSearch &s, int pos, int level, int start) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
if (pos > s.end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!greedy && level > min) {
|
|
|
|
int res = next ? next->test(s, pos) : pos;
|
|
|
|
if (s.complete)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
if (res >= 0 && parent->test_parent(s, res) >= 0)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max >= 0 && level > max)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int res = pos;
|
|
|
|
if (level >= 1) {
|
|
|
|
if (level > min + 1 && pos == start)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
res = child->test(s, pos);
|
|
|
|
if (s.complete)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res >= 0) {
|
|
|
|
|
|
|
|
int res_step = test_step(s, res, level + 1, start);
|
|
|
|
if (res_step >= 0)
|
|
|
|
return res_step;
|
|
|
|
|
|
|
|
if (greedy && level >= min) {
|
|
|
|
if (next)
|
|
|
|
res = next->test(s, res);
|
|
|
|
if (s.complete)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
if (res >= 0 && parent->test_parent(s, res) >= 0)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeBackReference : public RegExNode {
|
|
|
|
|
|
|
|
int id;
|
|
|
|
|
|
|
|
RegExNodeBackReference(int p_id) {
|
|
|
|
|
|
|
|
length = -1;
|
|
|
|
quantifiable = true;
|
|
|
|
id = p_id;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExMatch::Group &ref = s.match->captures[id];
|
2016-10-23 02:22:48 +02:00
|
|
|
for (int i = 0; i < ref.length; ++i) {
|
|
|
|
|
|
|
|
if (pos + i >= s.end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (s.at(ref.start + i) != s.at(pos + i))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return next ? next->test(s, pos + ref.length) : pos + ref.length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeGroup : public RegExNode {
|
|
|
|
|
|
|
|
bool inverse;
|
|
|
|
bool reset_pos;
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<RegExNode *> childset;
|
|
|
|
RegExNode *back;
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
RegExNodeGroup() {
|
|
|
|
|
|
|
|
length = 0;
|
|
|
|
quantifiable = true;
|
|
|
|
inverse = false;
|
|
|
|
reset_pos = false;
|
|
|
|
back = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~RegExNodeGroup() {
|
|
|
|
|
|
|
|
for (int i = 0; i < childset.size(); ++i)
|
|
|
|
memdelete(childset[i]);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < childset.size(); ++i) {
|
|
|
|
|
|
|
|
s.complete = false;
|
|
|
|
|
|
|
|
int res = childset[i]->test(s, pos);
|
|
|
|
|
|
|
|
if (s.complete)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
if (inverse) {
|
|
|
|
if (res < 0)
|
|
|
|
res = pos + 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (i + 1 < childset.size())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res >= 0) {
|
|
|
|
if (reset_pos)
|
|
|
|
res = pos;
|
|
|
|
return next ? next->test(s, res) : res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void add_child(RegExNode *node) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
node->parent = this;
|
|
|
|
node->previous = back;
|
|
|
|
|
|
|
|
if (back)
|
|
|
|
back->next = node;
|
|
|
|
else
|
|
|
|
childset.push_back(node);
|
|
|
|
|
|
|
|
increment_length(node->length);
|
|
|
|
|
|
|
|
back = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_childset() {
|
|
|
|
|
|
|
|
if (childset.size() > 0)
|
|
|
|
length = -1;
|
|
|
|
back = NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExNode *swap_back(RegExNode *node) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExNode *old = back;
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
if (old) {
|
|
|
|
if (!old->previous)
|
|
|
|
childset.remove(childset.size() - 1);
|
|
|
|
back = old->previous;
|
|
|
|
increment_length(old->length, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
add_child(node);
|
|
|
|
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeCapturing : public RegExNodeGroup {
|
|
|
|
|
|
|
|
int id;
|
|
|
|
|
|
|
|
RegExNodeCapturing(int p_id = 0) {
|
|
|
|
|
|
|
|
id = p_id;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExMatch::Group &ref = s.match->captures[id];
|
2016-10-23 02:22:48 +02:00
|
|
|
int old_start = ref.start;
|
|
|
|
ref.start = pos;
|
|
|
|
|
|
|
|
int res = RegExNodeGroup::test(s, pos);
|
|
|
|
|
|
|
|
if (res >= 0) {
|
|
|
|
if (!s.complete)
|
|
|
|
ref.length = res - pos;
|
|
|
|
} else {
|
|
|
|
ref.start = old_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test_parent(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExMatch::Group &ref = s.match->captures[id];
|
2016-10-23 02:22:48 +02:00
|
|
|
ref.length = pos - ref.start;
|
|
|
|
return RegExNode::test_parent(s, pos);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static Variant parse_name(const CharType *&c, bool p_allow_numeric) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
if (c[1] == '0') {
|
|
|
|
return -1;
|
|
|
|
} else if ('1' <= c[1] && c[1] <= '9') {
|
|
|
|
if (!p_allow_numeric)
|
|
|
|
return -1;
|
|
|
|
int res = (++c)[0] - '0';
|
|
|
|
while ('0' <= c[1] && c[1] <= '9')
|
|
|
|
res = res * 10 + int((++c)[0] - '0');
|
|
|
|
if ((++c)[0] != '>')
|
|
|
|
return -1;
|
|
|
|
return res;
|
|
|
|
} else if (iswalnum(c[1])) {
|
|
|
|
String res(++c, 1);
|
|
|
|
while (iswalnum(c[1]))
|
|
|
|
res += String(++c, 1);
|
|
|
|
if ((++c)[0] != '>')
|
|
|
|
return -1;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeLookAhead : public RegExNodeGroup {
|
|
|
|
|
|
|
|
int id;
|
|
|
|
|
|
|
|
RegExNodeLookAhead(bool p_inverse, int p_id = 0) {
|
|
|
|
|
|
|
|
quantifiable = false;
|
|
|
|
inverse = p_inverse;
|
|
|
|
reset_pos = true;
|
|
|
|
id = p_id;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
s.lookahead_pos[id] = pos;
|
|
|
|
return RegExNodeGroup::test(s, pos);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test_parent(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
return RegExNode::test_parent(s, s.lookahead_pos[id]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeLookBehind : public RegExNodeGroup {
|
|
|
|
|
|
|
|
RegExNodeLookBehind(bool p_inverse, int p_id = 0) {
|
|
|
|
|
|
|
|
quantifiable = false;
|
|
|
|
inverse = p_inverse;
|
|
|
|
reset_pos = true;
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:16:46 +01:00
|
|
|
virtual bool is_look_behind() { return true; }
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
if (pos < length)
|
|
|
|
return -1;
|
|
|
|
return RegExNodeGroup::test(s, pos - length);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegExNodeBracket : public RegExNode {
|
|
|
|
|
|
|
|
bool inverse;
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<RegExNode *> children;
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
RegExNodeBracket() {
|
|
|
|
|
|
|
|
length = 1;
|
|
|
|
quantifiable = true;
|
|
|
|
inverse = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~RegExNodeBracket() {
|
|
|
|
|
|
|
|
for (int i = 0; i < children.size(); ++i)
|
|
|
|
memdelete(children[i]);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int test(RegExSearch &s, int pos) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < children.size(); ++i) {
|
|
|
|
|
|
|
|
int res = children[i]->test(s, pos);
|
|
|
|
|
|
|
|
if (inverse) {
|
|
|
|
if (res < 0)
|
|
|
|
res = pos + 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (i + 1 < children.size())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res >= 0)
|
|
|
|
return next ? next->test(s, res) : res;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void add_child(RegExNode *node) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
node->parent = this;
|
|
|
|
children.push_back(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop_back() {
|
|
|
|
|
|
|
|
memdelete(children[children.size() - 1]);
|
|
|
|
children.remove(children.size() - 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define REGEX_EXPAND_FAIL(MSG) \
|
|
|
|
{ \
|
|
|
|
ERR_PRINT(MSG); \
|
|
|
|
return String(); \
|
|
|
|
}
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String RegExMatch::expand(const String &p_template) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
String res;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (const CharType *c = p_template.c_str(); *c != '\0'; ++c) {
|
2016-10-23 02:22:48 +02:00
|
|
|
if (c[0] == '\\') {
|
|
|
|
if (('1' <= c[1] && c[1] <= '9') || (c[1] == 'g' && c[2] == '{')) {
|
|
|
|
|
|
|
|
int ref = 0;
|
|
|
|
bool unclosed = false;
|
|
|
|
|
|
|
|
if (c[1] == 'g') {
|
|
|
|
unclosed = true;
|
|
|
|
c = &c[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
while ('0' <= c[1] && c[1] <= '9') {
|
|
|
|
ref = ref * 10 + int(c[1] - '0');
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unclosed) {
|
|
|
|
if (c[1] != '}')
|
|
|
|
REGEX_EXPAND_FAIL("unclosed backreference '{'");
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
|
|
|
|
res += get_string(ref);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (c[1] == 'g' && c[2] == '<') {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *d = &c[2];
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
Variant name = RegExNodeCapturing::parse_name(d, true);
|
|
|
|
if (name == Variant(-1))
|
|
|
|
REGEX_EXPAND_FAIL("unrecognised character for group name");
|
|
|
|
|
|
|
|
c = d;
|
|
|
|
|
|
|
|
res += get_string(name);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *d = c;
|
2016-10-23 02:22:48 +02:00
|
|
|
CharType ch = RegExNodeChar::parse_escape(d);
|
|
|
|
if (c == d)
|
|
|
|
REGEX_EXPAND_FAIL("invalid escape token");
|
|
|
|
res += String(&ch, 1);
|
|
|
|
c = d;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res += String(c, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RegExMatch::get_group_count() const {
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 1; i < captures.size(); ++i)
|
|
|
|
if (captures[i].name.get_type() == Variant::INT)
|
|
|
|
++count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
Array RegExMatch::get_group_array() const {
|
|
|
|
|
|
|
|
Array res;
|
|
|
|
for (int i = 1; i < captures.size(); ++i) {
|
2017-03-05 16:44:50 +01:00
|
|
|
const RegExMatch::Group &capture = captures[i];
|
2016-10-23 02:22:48 +02:00
|
|
|
if (capture.name.get_type() != Variant::INT)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (capture.start >= 0)
|
|
|
|
res.push_back(string.substr(capture.start, capture.length));
|
|
|
|
else
|
|
|
|
res.push_back(String());
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Array RegExMatch::get_names() const {
|
|
|
|
|
|
|
|
Array res;
|
|
|
|
for (int i = 1; i < captures.size(); ++i)
|
|
|
|
if (captures[i].name.get_type() == Variant::STRING)
|
|
|
|
res.push_back(captures[i].name);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary RegExMatch::get_name_dict() const {
|
|
|
|
|
|
|
|
Dictionary res;
|
|
|
|
for (int i = 1; i < captures.size(); ++i) {
|
2017-03-05 16:44:50 +01:00
|
|
|
const RegExMatch::Group &capture = captures[i];
|
2016-10-23 02:22:48 +02:00
|
|
|
if (capture.name.get_type() != Variant::STRING)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (capture.start >= 0)
|
|
|
|
res[capture.name] = string.substr(capture.start, capture.length);
|
|
|
|
else
|
|
|
|
res[capture.name] = String();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String RegExMatch::get_string(const Variant &p_name) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < captures.size(); ++i) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const RegExMatch::Group &capture = captures[i];
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
if (capture.name != p_name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (capture.start == -1)
|
|
|
|
return String();
|
|
|
|
|
|
|
|
return string.substr(capture.start, capture.length);
|
|
|
|
}
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int RegExMatch::get_start(const Variant &p_name) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < captures.size(); ++i)
|
|
|
|
if (captures[i].name == p_name)
|
|
|
|
return captures[i].start;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int RegExMatch::get_end(const Variant &p_name) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < captures.size(); ++i)
|
|
|
|
if (captures[i].name == p_name)
|
|
|
|
return captures[i].start + captures[i].length;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegExMatch::RegExMatch() {
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool RegEx_is_shorthand(CharType ch) {
|
|
|
|
|
|
|
|
switch (ch) {
|
|
|
|
case 'w':
|
|
|
|
case 'W':
|
|
|
|
case 'd':
|
|
|
|
case 'D':
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define REGEX_COMPILE_FAIL(MSG) \
|
|
|
|
{ \
|
|
|
|
ERR_PRINT(MSG); \
|
|
|
|
clear(); \
|
|
|
|
return FAILED; \
|
|
|
|
}
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error RegEx::compile(const String &p_pattern) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2016-10-26 14:05:00 +02:00
|
|
|
ERR_FAIL_COND_V(p_pattern.length() == 0, FAILED);
|
|
|
|
|
|
|
|
if (pattern == p_pattern && root)
|
2016-10-23 02:22:48 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
clear();
|
|
|
|
pattern = p_pattern;
|
|
|
|
group_names.push_back(0);
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExNodeGroup *root_group = memnew(RegExNodeCapturing(0));
|
2016-10-23 02:22:48 +02:00
|
|
|
root = root_group;
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<RegExNodeGroup *> stack;
|
2016-10-23 02:22:48 +02:00
|
|
|
stack.push_back(root_group);
|
|
|
|
int lookahead_level = 0;
|
|
|
|
int numeric_groups = 0;
|
|
|
|
const int numeric_max = 9;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (const CharType *c = p_pattern.c_str(); *c != '\0'; ++c) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
switch (c[0]) {
|
|
|
|
case '(':
|
|
|
|
if (c[1] == '?') {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExNodeGroup *group = NULL;
|
2016-10-23 02:22:48 +02:00
|
|
|
switch (c[2]) {
|
|
|
|
case ':':
|
|
|
|
c = &c[2];
|
|
|
|
group = memnew(RegExNodeGroup());
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
case '=':
|
|
|
|
group = memnew(RegExNodeLookAhead((c[2] == '!'), lookahead_level++));
|
|
|
|
if (lookahead_depth < lookahead_level)
|
|
|
|
lookahead_depth = lookahead_level;
|
|
|
|
c = &c[2];
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
if (c[3] == '!' || c[3] == '=') {
|
|
|
|
group = memnew(RegExNodeLookBehind((c[3] == '!'), lookahead_level++));
|
|
|
|
c = &c[3];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
if (c[3] == '<') {
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *d = &c[3];
|
2016-10-23 02:22:48 +02:00
|
|
|
Variant name = RegExNodeCapturing::parse_name(d, false);
|
|
|
|
if (name == Variant(-1))
|
|
|
|
REGEX_COMPILE_FAIL("unrecognised character for group name");
|
|
|
|
group = memnew(RegExNodeCapturing(group_names.size()));
|
|
|
|
group_names.push_back(name);
|
|
|
|
c = d;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!group)
|
|
|
|
REGEX_COMPILE_FAIL("unrecognised qualifier for group");
|
|
|
|
stack[0]->add_child(group);
|
|
|
|
stack.insert(0, group);
|
|
|
|
|
|
|
|
} else if (numeric_groups < numeric_max) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExNodeCapturing *group = memnew(RegExNodeCapturing(group_names.size()));
|
2016-10-23 02:22:48 +02:00
|
|
|
group_names.push_back(++numeric_groups);
|
|
|
|
stack[0]->add_child(group);
|
|
|
|
stack.insert(0, group);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExNodeGroup *group = memnew(RegExNodeGroup());
|
2016-10-23 02:22:48 +02:00
|
|
|
stack[0]->add_child(group);
|
|
|
|
stack.insert(0, group);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ')':
|
|
|
|
if (stack.size() == 1)
|
|
|
|
REGEX_COMPILE_FAIL("unexpected ')'");
|
|
|
|
stack.remove(0);
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
if (('1' <= c[1] && c[1] <= '9') || (c[1] == 'g' && c[2] == '{')) {
|
|
|
|
|
|
|
|
int ref = 0;
|
|
|
|
bool unclosed = false;
|
|
|
|
|
|
|
|
if (c[1] == 'g') {
|
|
|
|
unclosed = true;
|
|
|
|
c = &c[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
while ('0' <= c[1] && c[1] <= '9') {
|
|
|
|
ref = ref * 10 + int(c[1] - '0');
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unclosed) {
|
|
|
|
if (c[1] != '}')
|
|
|
|
REGEX_COMPILE_FAIL("unclosed backreference '{'");
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ref > numeric_groups || ref <= 0)
|
|
|
|
REGEX_COMPILE_FAIL("backreference not found");
|
|
|
|
|
|
|
|
for (int i = 0; i < stack.size(); ++i)
|
2016-11-01 19:16:46 +01:00
|
|
|
if (stack[i]->is_look_behind())
|
2016-10-23 02:22:48 +02:00
|
|
|
REGEX_COMPILE_FAIL("backreferences inside lookbehind not supported");
|
|
|
|
|
|
|
|
for (int i = 0; i < group_names.size(); ++i) {
|
|
|
|
if (group_names[i].get_type() == Variant::INT && int(group_names[i]) == ref) {
|
|
|
|
ref = group_names[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stack[0]->add_child(memnew(RegExNodeBackReference(ref)));
|
2017-03-05 16:44:50 +01:00
|
|
|
}
|
|
|
|
if (c[1] == 'g' && c[2] == '<') {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *d = &c[2];
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
Variant name = RegExNodeCapturing::parse_name(d, true);
|
|
|
|
if (name == Variant(-1))
|
|
|
|
REGEX_COMPILE_FAIL("unrecognised character for group name");
|
|
|
|
|
|
|
|
c = d;
|
|
|
|
|
|
|
|
for (int i = 0; i < stack.size(); ++i)
|
2016-11-01 19:16:46 +01:00
|
|
|
if (stack[i]->is_look_behind())
|
2016-10-23 02:22:48 +02:00
|
|
|
REGEX_COMPILE_FAIL("backreferences inside lookbehind not supported");
|
|
|
|
|
|
|
|
int ref = -1;
|
|
|
|
|
|
|
|
for (int i = 0; i < group_names.size(); ++i) {
|
|
|
|
if (group_names[i].get_type() == Variant::INT && int(group_names[i]) == ref) {
|
|
|
|
ref = group_names[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ref == -1)
|
|
|
|
REGEX_COMPILE_FAIL("backreference not found");
|
|
|
|
|
|
|
|
stack[0]->add_child(memnew(RegExNodeBackReference(ref)));
|
|
|
|
|
|
|
|
} else if (c[1] == 'b' || c[1] == 'B') {
|
|
|
|
|
|
|
|
stack[0]->add_child(memnew(RegExNodeWordBoundary(*(++c) == 'B')));
|
|
|
|
|
|
|
|
} else if (RegEx_is_shorthand(c[1])) {
|
|
|
|
|
|
|
|
stack[0]->add_child(memnew(RegExNodeShorthand(*(++c))));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *d = c;
|
2016-10-23 02:22:48 +02:00
|
|
|
CharType ch = RegExNodeChar::parse_escape(d);
|
|
|
|
if (c == d)
|
|
|
|
REGEX_COMPILE_FAIL("invalid escape token");
|
|
|
|
stack[0]->add_child(memnew(RegExNodeChar(ch)));
|
|
|
|
c = d;
|
|
|
|
}
|
|
|
|
break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case '[': {
|
|
|
|
RegExNodeBracket *bracket = memnew(RegExNodeBracket());
|
|
|
|
stack[0]->add_child(bracket);
|
|
|
|
if (c[1] == '^') {
|
|
|
|
bracket->inverse = true;
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
bool first_child = true;
|
|
|
|
CharType previous_child;
|
|
|
|
bool previous_child_single = false;
|
|
|
|
while (true) {
|
|
|
|
++c;
|
|
|
|
if (!first_child && c[0] == ']') {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
break;
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (c[0] == '\0') {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
REGEX_COMPILE_FAIL("unclosed bracket expression '['");
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (c[0] == '\\') {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (RegEx_is_shorthand(c[1])) {
|
|
|
|
bracket->add_child(memnew(RegExNodeShorthand(*(++c))));
|
|
|
|
} else {
|
|
|
|
const CharType *d = c;
|
|
|
|
CharType ch = RegExNodeChar::parse_escape(d);
|
|
|
|
if (c == d)
|
|
|
|
REGEX_COMPILE_FAIL("invalid escape token");
|
|
|
|
bracket->add_child(memnew(RegExNodeChar(ch)));
|
|
|
|
c = d;
|
|
|
|
previous_child = ch;
|
|
|
|
previous_child_single = true;
|
|
|
|
}
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (c[0] == ']' && c[1] == ':') {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const CharType *d = &c[2];
|
|
|
|
RegExNodeClass::Type type = RegExNodeClass::parse_type(d);
|
|
|
|
if (type != RegExNodeClass::Type_none) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
c = d;
|
|
|
|
previous_child_single = false;
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bracket->add_child(memnew(RegExNodeChar('[')));
|
|
|
|
previous_child = '[';
|
|
|
|
previous_child_single = true;
|
|
|
|
}
|
|
|
|
} else if (previous_child_single && c[0] == '-') {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c[1] != '\0' && c[1] != ']') {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharType next;
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c[1] == '\\') {
|
|
|
|
const CharType *d = ++c;
|
|
|
|
next = RegExNodeChar::parse_escape(d);
|
|
|
|
if (c == d)
|
|
|
|
REGEX_COMPILE_FAIL("invalid escape token");
|
2016-10-23 02:22:48 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
next = *(++c);
|
2016-10-23 02:22:48 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
|
|
|
|
if (next < previous_child)
|
|
|
|
REGEX_COMPILE_FAIL("text range out of order");
|
|
|
|
|
|
|
|
bracket->pop_back();
|
|
|
|
bracket->add_child(memnew(RegExNodeRange(previous_child, next)));
|
|
|
|
previous_child_single = false;
|
2016-10-23 02:22:48 +02:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bracket->add_child(memnew(RegExNodeChar('-')));
|
|
|
|
previous_child = '-';
|
2016-10-23 02:22:48 +02:00
|
|
|
previous_child_single = true;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
bracket->add_child(memnew(RegExNodeChar(c[0])));
|
|
|
|
previous_child = c[0];
|
|
|
|
previous_child_single = true;
|
2016-10-23 02:22:48 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
first_child = false;
|
2016-10-23 02:22:48 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} break;
|
2016-10-23 02:22:48 +02:00
|
|
|
case '|':
|
|
|
|
for (int i = 0; i < stack.size(); ++i)
|
2016-11-01 19:16:46 +01:00
|
|
|
if (stack[i]->is_look_behind())
|
2016-10-23 02:22:48 +02:00
|
|
|
REGEX_COMPILE_FAIL("alternations inside lookbehind not supported");
|
|
|
|
stack[0]->add_childset();
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
stack[0]->add_child(memnew(RegExNodeAnchorStart()));
|
|
|
|
break;
|
|
|
|
case '$':
|
|
|
|
stack[0]->add_child(memnew(RegExNodeAnchorEnd()));
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
stack[0]->add_child(memnew(RegExNodeShorthand('.')));
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
case '*':
|
|
|
|
case '+':
|
2017-03-05 16:44:50 +01:00
|
|
|
case '{': {
|
|
|
|
int min_val = 0;
|
|
|
|
int max_val = -1;
|
|
|
|
bool valid = true;
|
|
|
|
const CharType *d = c;
|
|
|
|
bool max_set = true;
|
|
|
|
switch (c[0]) {
|
|
|
|
case '?':
|
|
|
|
min_val = 0;
|
|
|
|
max_val = 1;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
min_val = 0;
|
|
|
|
max_val = -1;
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
min_val = 1;
|
|
|
|
max_val = -1;
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
max_set = false;
|
|
|
|
while (valid) {
|
|
|
|
++d;
|
|
|
|
if (d[0] == '}') {
|
|
|
|
break;
|
|
|
|
} else if (d[0] == ',') {
|
|
|
|
max_set = true;
|
|
|
|
} else if ('0' <= d[0] && d[0] <= '9') {
|
|
|
|
if (max_set) {
|
|
|
|
if (max_val < 0)
|
|
|
|
max_val = int(d[0] - '0');
|
|
|
|
else
|
|
|
|
max_val = max_val * 10 + int(d[0] - '0');
|
2016-10-23 02:22:48 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
min_val = min_val * 10 + int(d[0] - '0');
|
2016-10-23 02:22:48 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
|
|
|
valid = false;
|
2016-10-23 02:22:48 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!max_set)
|
|
|
|
max_val = min_val;
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (valid) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
c = d;
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (stack[0]->back == NULL || !stack[0]->back->quantifiable)
|
|
|
|
REGEX_COMPILE_FAIL("element not quantifiable");
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (min_val != max_val)
|
|
|
|
for (int i = 0; i < stack.size(); ++i)
|
|
|
|
if (stack[i]->is_look_behind())
|
|
|
|
REGEX_COMPILE_FAIL("variable length quantifiers inside lookbehind not supported");
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExNodeQuantifier *quant = memnew(RegExNodeQuantifier(min_val, max_val));
|
|
|
|
quant->child = stack[0]->swap_back(quant);
|
|
|
|
quant->child->previous = NULL;
|
|
|
|
quant->child->parent = quant;
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (min_val == max_val && quant->child->length >= 0)
|
|
|
|
quant->length = max_val * quant->child->length;
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c[1] == '?') {
|
|
|
|
quant->greedy = false;
|
|
|
|
++c;
|
2016-10-23 02:22:48 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
break;
|
2016-10-23 02:22:48 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
}
|
2016-10-23 02:22:48 +02:00
|
|
|
default:
|
|
|
|
stack[0]->add_child(memnew(RegExNodeChar(c[0])));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stack.size() > 1)
|
|
|
|
REGEX_COMPILE_FAIL("unclosed group '('");
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<RegExMatch> RegEx::search(const String &p_text, int p_start, int p_end) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2016-10-24 23:13:26 +02:00
|
|
|
ERR_FAIL_COND_V(!is_valid(), NULL);
|
|
|
|
ERR_FAIL_COND_V(p_start < 0, NULL);
|
|
|
|
ERR_FAIL_COND_V(p_start >= p_text.length(), NULL);
|
|
|
|
ERR_FAIL_COND_V(p_end > p_text.length(), NULL);
|
|
|
|
ERR_FAIL_COND_V(p_end != -1 && p_end < p_start, NULL);
|
|
|
|
|
2016-10-23 02:22:48 +02:00
|
|
|
Ref<RegExMatch> res = memnew(RegExMatch());
|
|
|
|
|
|
|
|
for (int i = 0; i < group_names.size(); ++i) {
|
|
|
|
RegExMatch::Group group;
|
|
|
|
group.name = group_names[i];
|
|
|
|
res->captures.push_back(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
res->string = p_text;
|
|
|
|
|
2016-10-24 23:13:26 +02:00
|
|
|
if (p_end == -1)
|
2016-10-23 02:22:48 +02:00
|
|
|
p_end = p_text.length();
|
|
|
|
|
|
|
|
RegExSearch s(res, p_end, lookahead_depth);
|
|
|
|
|
|
|
|
for (int i = p_start; i <= s.end; ++i) {
|
|
|
|
for (int c = 0; c < group_names.size(); ++c) {
|
|
|
|
res->captures[c].start = -1;
|
|
|
|
res->captures[c].length = 0;
|
|
|
|
}
|
|
|
|
if (root->test(s, i) >= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res->captures[0].start >= 0)
|
|
|
|
return res;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String RegEx::sub(const String &p_text, const String &p_replacement, bool p_all, int p_start, int p_end) const {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
2016-10-24 23:13:26 +02:00
|
|
|
ERR_FAIL_COND_V(!is_valid(), p_text);
|
|
|
|
ERR_FAIL_COND_V(p_start < 0, p_text);
|
|
|
|
ERR_FAIL_COND_V(p_start >= p_text.length(), p_text);
|
|
|
|
ERR_FAIL_COND_V(p_end > p_text.length(), p_text);
|
|
|
|
ERR_FAIL_COND_V(p_end != -1 && p_end < p_start, p_text);
|
|
|
|
|
|
|
|
String text = p_text;
|
|
|
|
int start = p_start;
|
|
|
|
|
|
|
|
if (p_end == -1)
|
|
|
|
p_end = p_text.length();
|
|
|
|
|
|
|
|
while (start < text.length() && (p_all || start == p_start)) {
|
|
|
|
|
|
|
|
Ref<RegExMatch> m = search(text, start, p_end);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegExMatch::Group &s = m->captures[0];
|
2016-10-24 23:13:26 +02:00
|
|
|
|
|
|
|
if (s.start < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
String res = text.substr(0, s.start) + m->expand(p_replacement);
|
|
|
|
|
|
|
|
start = res.length();
|
|
|
|
|
|
|
|
if (s.length == 0)
|
|
|
|
++start;
|
|
|
|
|
|
|
|
int sub_end = s.start + s.length;
|
|
|
|
if (sub_end < text.length())
|
|
|
|
res += text.substr(sub_end, text.length() - sub_end);
|
|
|
|
|
|
|
|
p_end += res.length() - text.length();
|
|
|
|
|
|
|
|
text = res;
|
2016-10-23 02:22:48 +02:00
|
|
|
}
|
2016-10-24 23:13:26 +02:00
|
|
|
return text;
|
2016-10-23 02:22:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegEx::clear() {
|
|
|
|
|
|
|
|
if (root)
|
|
|
|
memdelete(root);
|
|
|
|
|
2016-10-26 14:05:00 +02:00
|
|
|
root = NULL;
|
2016-10-23 02:22:48 +02:00
|
|
|
group_names.clear();
|
|
|
|
lookahead_depth = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegEx::is_valid() const {
|
|
|
|
|
|
|
|
return (root != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
String RegEx::get_pattern() const {
|
|
|
|
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RegEx::get_group_count() const {
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 1; i < group_names.size(); ++i)
|
|
|
|
if (group_names[i].get_type() == Variant::INT)
|
|
|
|
++count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
Array RegEx::get_names() const {
|
|
|
|
|
|
|
|
Array res;
|
|
|
|
for (int i = 1; i < group_names.size(); ++i)
|
|
|
|
if (group_names[i].get_type() == Variant::STRING)
|
|
|
|
res.push_back(group_names[i]);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegEx::RegEx() {
|
|
|
|
|
|
|
|
root = NULL;
|
|
|
|
lookahead_depth = 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RegEx::RegEx(const String &p_pattern) {
|
2016-10-23 02:22:48 +02:00
|
|
|
|
|
|
|
root = NULL;
|
|
|
|
compile(p_pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
RegEx::~RegEx() {
|
|
|
|
|
|
|
|
if (root)
|
|
|
|
memdelete(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegExMatch::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("expand", "template"), &RegExMatch::expand);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_group_count"), &RegExMatch::get_group_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_group_array"), &RegExMatch::get_group_array);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_names"), &RegExMatch::get_names);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_name_dict"), &RegExMatch::get_name_dict);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_string", "name"), &RegExMatch::get_string, DEFVAL(0));
|
|
|
|
ClassDB::bind_method(D_METHOD("get_start", "name"), &RegExMatch::get_start, DEFVAL(0));
|
|
|
|
ClassDB::bind_method(D_METHOD("get_end", "name"), &RegExMatch::get_end, DEFVAL(0));
|
2016-10-23 02:22:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegEx::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("clear"), &RegEx::clear);
|
|
|
|
ClassDB::bind_method(D_METHOD("compile", "pattern"), &RegEx::compile);
|
|
|
|
ClassDB::bind_method(D_METHOD("search", "text", "start", "end"), &RegEx::search, DEFVAL(0), DEFVAL(-1));
|
|
|
|
ClassDB::bind_method(D_METHOD("sub", "text", "replacement", "all", "start", "end"), &RegEx::sub, DEFVAL(false), DEFVAL(0), DEFVAL(-1));
|
|
|
|
ClassDB::bind_method(D_METHOD("is_valid"), &RegEx::is_valid);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_pattern"), &RegEx::get_pattern);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_group_count"), &RegEx::get_group_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_names"), &RegEx::get_names);
|
2016-10-26 14:05:00 +02:00
|
|
|
|
2017-02-12 01:11:37 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "pattern"), "compile", "get_pattern");
|
2016-10-23 02:22:48 +02:00
|
|
|
}
|