godot/drivers/nrex/nrex.cpp

1497 lines
39 KiB
C++
Raw Normal View History

2015-07-24 00:18:46 +00:00
// NREX: Node RegEx
// Version 0.2
2015-07-24 00:18:46 +00:00
//
// Copyright (c) 2015-2016, Zher Huei Lee
2015-07-24 00:18:46 +00:00
// All rights reserved.
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
#include "nrex.hpp"
#ifdef NREX_UNICODE
#include <wctype.h>
#include <wchar.h>
#define NREX_ISALPHANUM iswalnum
2015-10-20 08:37:49 +00:00
#define NREX_ISSPACE iswspace
2015-07-24 00:18:46 +00:00
#define NREX_STRLEN wcslen
#else
#include <ctype.h>
#include <string.h>
#define NREX_ISALPHANUM isalnum
2015-10-20 08:37:49 +00:00
#define NREX_ISSPACE isspace
2015-07-24 00:18:46 +00:00
#define NREX_STRLEN strlen
#endif
#ifdef NREX_THROW_ERROR
#define NREX_COMPILE_ERROR(M) throw nrex_compile_error(M)
#else
#define NREX_COMPILE_ERROR(M) reset(); return false
#endif
#ifndef NREX_NEW
#define NREX_NEW(X) new X
#define NREX_NEW_ARRAY(X, N) new X[N]
#define NREX_DELETE(X) delete X
#define NREX_DELETE_ARRAY(X) delete[] X
#endif
template<typename T>
class nrex_array
{
private:
T* _data;
unsigned int _reserved;
unsigned int _size;
public:
nrex_array()
: _data(NREX_NEW_ARRAY(T, 2))
, _reserved(2)
, _size(0)
{
}
nrex_array(unsigned int reserved)
: _data(NREX_NEW_ARRAY(T, reserved ? reserved : 1))
, _reserved(reserved ? reserved : 1)
, _size(0)
{
}
2015-07-24 00:18:46 +00:00
~nrex_array()
{
NREX_DELETE_ARRAY(_data);
}
unsigned int size() const
{
return _size;
}
void reserve(unsigned int size)
{
if (size < _size) {
size = _size;
}
if (size == 0) {
size = 1;
}
2015-07-24 00:18:46 +00:00
T* old = _data;
_data = NREX_NEW_ARRAY(T, size);
_reserved = size;
for (unsigned int i = 0; i < _size; ++i)
{
_data[i] = old[i];
}
NREX_DELETE_ARRAY(old);
}
void push(T item)
{
if (_size == _reserved)
{
reserve(_reserved * 2);
}
_data[_size] = item;
_size++;
}
const T& top() const
2015-07-24 00:18:46 +00:00
{
return _data[_size - 1];
}
const T& operator[] (unsigned int i) const
{
return _data[i];
}
void pop()
{
if (_size > 0)
{
--_size;
}
}
};
2015-10-20 08:37:49 +00:00
static int nrex_parse_hex(nrex_char c)
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00: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;
}
static nrex_char nrex_unescape(const nrex_char*& c)
{
switch (c[1])
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
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';
case 'x':
{
int point = 0;
for (int i = 2; i <= 3; ++i)
{
int res = nrex_parse_hex(c[i]);
if (res == -1)
{
return '\0';
}
point = (point << 4) + res;
}
c = &c[3];
return nrex_char(point);
}
case 'u':
{
int point = 0;
for (int i = 2; i <= 5; ++i)
{
int res = nrex_parse_hex(c[i]);
if (res == -1)
{
return '\0';
}
point = (point << 4) + res;
}
c = &c[5];
return nrex_char(point);
}
2015-07-24 00:18:46 +00:00
}
2015-10-20 08:37:49 +00:00
return (++c)[0];
2015-07-24 00:18:46 +00:00
}
struct nrex_search
{
const nrex_char* str;
nrex_result* captures;
int end;
bool complete;
nrex_array<int> lookahead_pos;
2015-07-24 00:18:46 +00:00
nrex_char at(int pos)
{
return str[pos];
}
nrex_search(const nrex_char* str, nrex_result* captures, int lookahead)
2015-07-24 00:18:46 +00:00
: str(str)
, captures(captures)
, end(0)
, lookahead_pos(lookahead)
2015-07-24 00:18:46 +00:00
{
}
};
struct nrex_node
{
nrex_node* next;
nrex_node* previous;
nrex_node* parent;
bool quantifiable;
2015-10-20 08:37:49 +00:00
int length;
2015-07-24 00:18:46 +00:00
nrex_node(bool quantify = false)
: next(NULL)
, previous(NULL)
, parent(NULL)
, quantifiable(quantify)
2015-10-20 08:37:49 +00:00
, length(-1)
2015-07-24 00:18:46 +00:00
{
}
virtual ~nrex_node()
{
if (next)
{
2015-07-24 01:02:52 +00:00
NREX_DELETE(next);
2015-07-24 00:18:46 +00:00
}
}
virtual int test(nrex_search* s, int pos) const
{
return next ? next->test(s, pos) : -1;
}
virtual int test_parent(nrex_search* s, int pos) const
{
if (next)
{
pos = next->test(s, pos);
}
if (pos >= 0)
{
s->complete = true;
}
2015-07-24 00:18:46 +00:00
if (parent && pos >= 0)
{
pos = parent->test_parent(s, pos);
}
if (pos < 0)
2015-07-24 00:18:46 +00:00
{
s->complete = false;
2015-07-24 00:18:46 +00:00
}
return pos;
}
2015-10-20 08:37:49 +00:00
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);
}
}
2015-07-24 00:18:46 +00:00
};
enum nrex_group_type
2015-07-24 00:18:46 +00:00
{
nrex_group_capture,
nrex_group_non_capture,
nrex_group_bracket,
nrex_group_look_ahead,
nrex_group_look_behind,
};
2015-10-19 03:45:45 +00:00
struct nrex_node_group : public nrex_node
{
nrex_group_type type;
int id;
2015-07-24 00:18:46 +00:00
bool negate;
nrex_array<nrex_node*> childset;
nrex_node* back;
nrex_node_group(nrex_group_type type, int id = 0)
2015-07-24 00:18:46 +00:00
: nrex_node(true)
, type(type)
, id(id)
2015-07-24 00:18:46 +00:00
, negate(false)
, back(NULL)
{
if (type != nrex_group_bracket)
2015-10-20 08:37:49 +00:00
{
length = 0;
}
else
{
length = 1;
}
if (type == nrex_group_look_ahead || type == nrex_group_look_behind)
{
quantifiable = false;
}
2015-07-24 00:18:46 +00:00
}
virtual ~nrex_node_group()
{
for (unsigned int i = 0; i < childset.size(); ++i)
{
2015-07-24 01:02:52 +00:00
NREX_DELETE(childset[i]);
2015-07-24 00:18:46 +00:00
}
}
int test(nrex_search* s, int pos) const
{
int old_start;
if (type == nrex_group_capture)
2015-07-24 00:18:46 +00:00
{
old_start = s->captures[id].start;
s->captures[id].start = pos;
2015-07-24 00:18:46 +00:00
}
for (unsigned int i = 0; i < childset.size(); ++i)
{
s->complete = false;
2015-10-20 08:37:49 +00:00
int offset = 0;
if (type == nrex_group_look_behind)
2015-10-20 08:37:49 +00:00
{
if (pos < length)
{
return -1;
}
2015-10-20 08:37:49 +00:00
offset = length;
}
if (type == nrex_group_look_ahead)
{
s->lookahead_pos.push(pos);
}
2015-10-20 08:37:49 +00:00
int res = childset[i]->test(s, pos - offset);
if (type == nrex_group_look_ahead)
{
s->lookahead_pos.pop();
}
2015-07-24 00:18:46 +00:00
if (s->complete)
{
return res;
}
2015-07-24 12:25:04 +00:00
if (negate)
{
if (res < 0)
{
res = pos + 1;
}
else
{
return -1;
}
2015-10-19 03:45:45 +00:00
if (i + 1 < childset.size())
{
continue;
}
2015-07-24 12:25:04 +00:00
}
if (res >= 0)
2015-07-24 00:18:46 +00:00
{
if (type == nrex_group_capture)
2015-10-19 03:45:45 +00:00
{
s->captures[id].length = res - pos;
2015-10-19 03:45:45 +00:00
}
else if (type == nrex_group_look_ahead || type == nrex_group_look_behind)
2015-07-24 00:18:46 +00:00
{
2015-10-19 03:45:45 +00:00
res = pos;
2015-07-24 00:18:46 +00:00
}
return next ? next->test(s, res) : res;
}
}
if (type == nrex_group_capture)
{
s->captures[id].start = old_start;
}
2015-07-24 00:18:46 +00:00
return -1;
}
virtual int test_parent(nrex_search* s, int pos) const
{
if (type == nrex_group_capture)
{
s->captures[id].length = pos - s->captures[id].start;
}
if (type == nrex_group_look_ahead)
2015-07-24 00:18:46 +00:00
{
pos = s->lookahead_pos[id];
2015-07-24 00:18:46 +00:00
}
return nrex_node::test_parent(s, pos);
}
void add_childset()
{
if (childset.size() > 0 && type != nrex_group_bracket)
2015-10-20 08:37:49 +00:00
{
length = -1;
}
2015-07-24 00:18:46 +00:00
back = NULL;
}
void add_child(nrex_node* node)
{
node->parent = this;
node->previous = back;
if (back && type != nrex_group_bracket)
2015-07-24 00:18:46 +00:00
{
back->next = node;
}
else
{
childset.push(node);
}
if (type != nrex_group_bracket)
2015-10-20 08:37:49 +00:00
{
increment_length(node->length);
}
2015-07-24 00:18:46 +00:00
back = node;
}
nrex_node* swap_back(nrex_node* node)
{
if (!back)
{
add_child(node);
return NULL;
}
nrex_node* old = back;
if (!old->previous)
{
childset.pop();
}
if (type != nrex_group_bracket)
2015-10-20 08:37:49 +00:00
{
increment_length(old->length, true);
}
2015-07-24 00:18:46 +00:00
back = old->previous;
add_child(node);
return old;
}
2015-10-20 08:37:49 +00:00
void pop_back()
{
if (back)
{
nrex_node* old = back;
if (!old->previous)
{
childset.pop();
}
if (type != nrex_group_bracket)
2015-10-20 08:37:49 +00:00
{
increment_length(old->length, true);
}
back = old->previous;
NREX_DELETE(old);
}
}
2015-07-24 00:18:46 +00:00
};
struct nrex_node_char : public nrex_node
{
nrex_char ch;
nrex_node_char(nrex_char c)
: nrex_node(true)
, ch(c)
{
2015-10-20 08:37:49 +00:00
length = 1;
2015-07-24 00:18:46 +00:00
}
int test(nrex_search* s, int pos) const
{
if (s->end <= pos || 0 > pos || s->at(pos) != ch)
2015-07-24 00:18:46 +00:00
{
return -1;
}
return next ? next->test(s, pos + 1) : pos + 1;
}
};
struct nrex_node_range : public nrex_node
{
nrex_char start;
nrex_char end;
nrex_node_range(nrex_char s, nrex_char e)
: nrex_node(true)
, start(s)
, end(e)
{
2015-10-20 08:37:49 +00:00
length = 1;
2015-07-24 00:18:46 +00:00
}
int test(nrex_search* s, int pos) const
{
if (s->end <= pos || 0 > pos)
2015-07-24 00:18:46 +00:00
{
return -1;
}
nrex_char c = s->at(pos);
if (c < start || end < c)
{
return -1;
}
return next ? next->test(s, pos + 1) : pos + 1;
}
};
2015-10-20 08:37:49 +00:00
enum nrex_class_type
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
nrex_class_none,
nrex_class_alnum,
nrex_class_alpha,
nrex_class_blank,
nrex_class_cntrl,
nrex_class_digit,
nrex_class_graph,
nrex_class_lower,
nrex_class_print,
nrex_class_punct,
nrex_class_space,
nrex_class_upper,
nrex_class_xdigit,
nrex_class_word
};
static bool nrex_compare_class(const nrex_char** pos, const char* text)
{
unsigned int i = 0;
for (i = 0; text[i] != '\0'; ++i)
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
if ((*pos)[i] != text[i])
{
return false;
}
2015-07-24 00:18:46 +00:00
}
2015-10-20 08:37:49 +00:00
if ((*pos)[i++] != ':' || (*pos)[i] != ']')
{
return false;
}
*pos = &(*pos)[i];
return true;
}
#define NREX_COMPARE_CLASS(POS, NAME) if (nrex_compare_class(POS, #NAME)) return nrex_class_ ## NAME
static nrex_class_type nrex_parse_class(const nrex_char** pos)
{
NREX_COMPARE_CLASS(pos, alnum);
NREX_COMPARE_CLASS(pos, alpha);
NREX_COMPARE_CLASS(pos, blank);
NREX_COMPARE_CLASS(pos, cntrl);
NREX_COMPARE_CLASS(pos, digit);
NREX_COMPARE_CLASS(pos, graph);
NREX_COMPARE_CLASS(pos, lower);
NREX_COMPARE_CLASS(pos, print);
NREX_COMPARE_CLASS(pos, punct);
NREX_COMPARE_CLASS(pos, space);
NREX_COMPARE_CLASS(pos, upper);
NREX_COMPARE_CLASS(pos, xdigit);
NREX_COMPARE_CLASS(pos, word);
return nrex_class_none;
2015-07-24 00:18:46 +00:00
}
2015-10-20 08:37:49 +00:00
struct nrex_node_class : public nrex_node
{
nrex_class_type type;
nrex_node_class(nrex_class_type t)
: nrex_node(true)
, type(t)
{
length = 1;
}
int test(nrex_search* s, int pos) const
{
if (s->end <= pos || 0 > pos)
2015-10-20 08:37:49 +00:00
{
return -1;
}
if (!test_class(s->at(pos)))
{
return -1;
}
return next ? next->test(s, pos + 1) : pos + 1;
}
bool test_class(nrex_char c) const
{
if ((0 <= c && c <= 0x1F) || c == 0x7F)
{
if (type == nrex_class_cntrl)
{
return true;
}
}
else if (c < 0x7F)
{
if (type == nrex_class_print)
{
return true;
}
else if (type == nrex_class_graph && c != ' ')
{
return true;
}
else if ('0' <= c && c <= '9')
{
switch (type)
{
case nrex_class_alnum:
case nrex_class_digit:
case nrex_class_xdigit:
case nrex_class_word:
return true;
default:
break;
}
}
else if ('A' <= c && c <= 'Z')
{
switch (type)
{
case nrex_class_alnum:
case nrex_class_alpha:
case nrex_class_upper:
case nrex_class_word:
return true;
case nrex_class_xdigit:
if (c <= 'F')
{
return true;
}
default:
break;
}
}
else if ('a' <= c && c <= 'z')
{
switch (type)
{
case nrex_class_alnum:
case nrex_class_alpha:
case nrex_class_lower:
case nrex_class_word:
return true;
case nrex_class_xdigit:
if (c <= 'f')
{
return true;
}
default:
break;
}
}
}
switch (c)
{
case ' ':
case '\t':
if (type == nrex_class_blank)
{
return true;
}
case '\r':
case '\n':
case '\f':
if (type == nrex_class_space)
{
return true;
}
break;
case '_':
if (type == nrex_class_word)
{
return true;
}
case ']':
case '[':
case '!':
case '"':
case '#':
case '$':
case '%':
case '&':
case '\'':
case '(':
case ')':
case '*':
case '+':
case ',':
case '.':
case '/':
case ':':
case ';':
case '<':
case '=':
case '>':
case '?':
case '@':
case '\\':
case '^':
case '`':
case '{':
case '|':
case '}':
case '~':
case '-':
if (type == nrex_class_punct)
{
return true;
}
break;
default:
break;
}
return false;
}
};
2015-07-24 00:18:46 +00:00
static bool nrex_is_shorthand(nrex_char repr)
{
switch (repr)
{
case 'W':
case 'w':
case 'D':
case 'd':
case 'S':
case 's':
return true;
}
return false;
}
struct nrex_node_shorthand : public nrex_node
{
nrex_char repr;
nrex_node_shorthand(nrex_char c)
: nrex_node(true)
, repr(c)
{
2015-10-20 08:37:49 +00:00
length = 1;
2015-07-24 00:18:46 +00:00
}
int test(nrex_search* s, int pos) const
{
if (s->end <= pos || 0 > pos)
2015-07-24 00:18:46 +00:00
{
return -1;
}
bool found = false;
bool invert = false;
nrex_char c = s->at(pos);
switch (repr)
{
case '.':
found = true;
break;
case 'W':
invert = true;
case 'w':
if (c == '_' || NREX_ISALPHANUM(c))
{
found = true;
}
break;
case 'D':
invert = true;
case 'd':
if ('0' <= c && c <= '9')
{
found = true;
}
break;
case 'S':
invert = true;
case 's':
2015-10-20 08:37:49 +00:00
if (NREX_ISSPACE(c))
2015-07-24 00:18:46 +00:00
{
found = true;
}
break;
}
if (found == invert)
{
return -1;
}
return next ? next->test(s, pos + 1) : pos + 1;
}
};
static bool nrex_is_quantifier(nrex_char repr)
{
switch (repr)
{
case '?':
case '*':
case '+':
case '{':
return true;
}
return false;
}
struct nrex_node_quantifier : public nrex_node
{
int min;
int max;
bool greedy;
nrex_node* child;
2015-10-20 08:37:49 +00:00
nrex_node_quantifier(int min, int max)
2015-07-24 00:18:46 +00:00
: nrex_node()
2015-10-20 08:37:49 +00:00
, min(min)
, max(max)
2015-07-24 00:18:46 +00:00
, greedy(true)
, child(NULL)
{
}
virtual ~nrex_node_quantifier()
{
if (child)
{
2015-07-24 01:02:52 +00:00
NREX_DELETE(child);
2015-07-24 00:18:46 +00:00
}
}
int test(nrex_search* s, int pos) const
{
return test_step(s, pos, 0, pos);
2015-10-20 08:37:49 +00:00
}
int test_step(nrex_search* s, int pos, int level, int start) const
2015-10-20 08:37:49 +00:00
{
if (pos > s->end)
2015-10-20 08:37:49 +00:00
{
return -1;
}
if (!greedy && level > min)
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
int res = pos;
if (next)
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
res = next->test(s, res);
}
if (s->complete)
{
return res;
}
if (res >= 0 && parent->test_parent(s, res) >= 0)
{
return res;
2015-07-24 00:18:46 +00:00
}
2015-10-20 08:37:49 +00:00
}
if (max >= 0 && level > max)
2015-10-20 08:37:49 +00:00
{
return -1;
}
if (level > 1 && level > min + 1 && pos == start)
{
return -1;
}
int res = pos;
if (level >= 1)
{
res = child->test(s, pos);
if (s->complete)
{
return res;
}
2015-10-20 08:37:49 +00:00
}
if (res >= 0)
{
int res_step = test_step(s, res, level + 1, start);
2015-10-20 08:37:49 +00:00
if (res_step >= 0)
{
return res_step;
}
else if (greedy && level >= min)
2015-07-24 00:18:46 +00:00
{
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;
}
virtual int test_parent(nrex_search* s, int pos) const
{
s->complete = false;
return pos;
}
2015-07-24 00:18:46 +00:00
};
struct nrex_node_anchor : public nrex_node
{
bool end;
nrex_node_anchor(bool end)
: nrex_node()
, end(end)
{
2015-10-20 08:37:49 +00:00
length = 0;
2015-07-24 00:18:46 +00:00
}
int test(nrex_search* s, int pos) const
{
if (!end && pos != 0)
{
return -1;
}
else if (end && pos != s->end)
{
return -1;
}
return next ? next->test(s, pos) : pos;
}
};
2015-10-20 08:37:49 +00:00
struct nrex_node_word_boundary : public nrex_node
{
bool inverse;
nrex_node_word_boundary(bool inverse)
: nrex_node()
, inverse(inverse)
{
length = 0;
}
int test(nrex_search* s, int pos) const
{
bool left = false;
bool right = false;
if (pos != 0)
{
nrex_char c = s->at(pos - 1);
if (c == '_' || NREX_ISALPHANUM(c))
{
left = true;
}
}
if (pos != s->end)
{
nrex_char c = s->at(pos);
if (c == '_' || NREX_ISALPHANUM(c))
{
right = true;
}
}
if ((left != right) == inverse)
{
return -1;
}
return next ? next->test(s, pos) : pos;
}
};
2015-07-24 00:18:46 +00:00
struct nrex_node_backreference : public nrex_node
{
int ref;
nrex_node_backreference(int ref)
: nrex_node(true)
, ref(ref)
{
2015-10-20 08:37:49 +00:00
length = -1;
2015-07-24 00:18:46 +00:00
}
int test(nrex_search* s, int pos) const
{
nrex_result& r = s->captures[ref];
for (int i = 0; i < r.length; ++i)
{
if (pos + i >= s->end)
{
return -1;
}
if (s->at(r.start + i) != s->at(pos + i))
{
return -1;
}
}
return next ? next->test(s, pos + r.length) : pos + r.length;
}
};
2015-10-20 08:37:49 +00:00
bool nrex_has_lookbehind(nrex_array<nrex_node_group*>& stack)
{
for (unsigned int i = 0; i < stack.size(); i++)
{
if (stack[i]->type == nrex_group_look_behind)
2015-10-20 08:37:49 +00:00
{
return true;
}
}
return false;
}
2015-07-24 00:18:46 +00:00
nrex::nrex()
: _capturing(0)
, _lookahead_depth(0)
2015-07-24 00:18:46 +00:00
, _root(NULL)
{
}
nrex::nrex(const nrex_char* pattern, int captures)
: _capturing(0)
, _lookahead_depth(0)
, _root(NULL)
{
compile(pattern, captures);
}
2015-07-24 00:18:46 +00:00
nrex::~nrex()
{
if (_root)
{
2015-07-24 01:02:52 +00:00
NREX_DELETE(_root);
2015-07-24 00:18:46 +00:00
}
}
bool nrex::valid() const
{
return (_root != NULL);
}
void nrex::reset()
{
_capturing = 0;
_lookahead_depth = 0;
2015-07-24 00:18:46 +00:00
if (_root)
{
2015-07-24 01:02:52 +00:00
NREX_DELETE(_root);
2015-07-24 00:18:46 +00:00
}
_root = NULL;
}
int nrex::capture_size() const
{
if (_root)
{
return _capturing + 1;
}
return 0;
2015-07-24 00:18:46 +00:00
}
bool nrex::compile(const nrex_char* pattern, int captures)
2015-07-24 00:18:46 +00:00
{
reset();
nrex_node_group* root = NREX_NEW(nrex_node_group(nrex_group_capture, _capturing));
2015-07-24 00:18:46 +00:00
nrex_array<nrex_node_group*> stack;
stack.push(root);
unsigned int lookahead_level = 0;
2015-07-24 00:18:46 +00:00
_root = root;
for (const nrex_char* c = pattern; c[0] != '\0'; ++c)
{
if (c[0] == '(')
{
if (c[1] == '?')
{
if (c[2] == ':')
{
c = &c[2];
nrex_node_group* group = NREX_NEW(nrex_node_group(nrex_group_non_capture));
2015-10-19 03:45:45 +00:00
stack.top()->add_child(group);
stack.push(group);
}
else if (c[2] == '!' || c[2] == '=')
{
c = &c[2];
nrex_node_group* group = NREX_NEW(nrex_node_group(nrex_group_look_ahead, lookahead_level++));
2015-10-19 03:45:45 +00:00
group->negate = (c[0] == '!');
2015-07-24 00:18:46 +00:00
stack.top()->add_child(group);
stack.push(group);
if (lookahead_level > _lookahead_depth)
{
_lookahead_depth = lookahead_level;
}
2015-07-24 00:18:46 +00:00
}
2015-10-20 08:37:49 +00:00
else if (c[2] == '<' && (c[3] == '!' || c[3] == '='))
{
c = &c[3];
nrex_node_group* group = NREX_NEW(nrex_node_group(nrex_group_look_behind));
2015-10-20 08:37:49 +00:00
group->negate = (c[0] == '!');
stack.top()->add_child(group);
stack.push(group);
}
2015-07-24 00:18:46 +00:00
else
{
2015-10-20 08:37:49 +00:00
NREX_COMPILE_ERROR("unrecognised qualifier for group");
2015-07-24 00:18:46 +00:00
}
}
else if (captures >= 0 && _capturing < captures)
2015-07-24 00:18:46 +00:00
{
nrex_node_group* group = NREX_NEW(nrex_node_group(nrex_group_capture, ++_capturing));
2015-07-24 00:18:46 +00:00
stack.top()->add_child(group);
stack.push(group);
}
else
{
nrex_node_group* group = NREX_NEW(nrex_node_group(nrex_group_non_capture));
2015-07-24 00:18:46 +00:00
stack.top()->add_child(group);
stack.push(group);
}
}
else if (c[0] == ')')
{
if (stack.size() > 1)
{
if (stack.top()->type == nrex_group_look_ahead)
{
--lookahead_level;
}
2015-07-24 00:18:46 +00:00
stack.pop();
}
else
{
NREX_COMPILE_ERROR("unexpected ')'");
}
}
else if (c[0] == '[')
{
nrex_node_group* group = NREX_NEW(nrex_node_group(nrex_group_bracket));
2015-07-24 00:18:46 +00:00
stack.top()->add_child(group);
if (c[1] == '^')
{
group->negate = true;
++c;
}
2015-10-20 08:37:49 +00:00
bool first_child = true;
nrex_char previous_child;
bool previous_child_single = false;
2015-07-24 00:18:46 +00:00
while (true)
{
group->add_childset();
++c;
if (c[0] == '\0')
{
2015-10-20 08:37:49 +00:00
NREX_COMPILE_ERROR("unclosed bracket expression '['");
}
if (c[0] == '[' && c[1] == ':')
{
const nrex_char* d = &c[2];
nrex_class_type cls = nrex_parse_class(&d);
if (cls != nrex_class_none)
{
c = d;
group->add_child(NREX_NEW(nrex_node_class(cls)));
previous_child_single = false;
}
else
{
group->add_child(NREX_NEW(nrex_node_char('[')));
previous_child = '[';
previous_child_single = true;
}
2015-07-24 00:18:46 +00:00
}
2015-10-20 08:37:49 +00:00
else if (c[0] == ']' && !first_child)
2015-07-24 00:18:46 +00:00
{
break;
}
else if (c[0] == '\\')
{
2015-10-20 08:37:49 +00:00
if (nrex_is_shorthand(c[1]))
2015-07-24 00:18:46 +00:00
{
group->add_child(NREX_NEW(nrex_node_shorthand(c[1])));
++c;
2015-10-20 08:37:49 +00:00
previous_child_single = false;
2015-07-24 00:18:46 +00:00
}
else
{
2015-10-20 08:37:49 +00:00
const nrex_char* d = c;
nrex_char unescaped = nrex_unescape(d);
if (c == d)
{
NREX_COMPILE_ERROR("invalid escape token");
}
group->add_child(NREX_NEW(nrex_node_char(unescaped)));
c = d;
previous_child = unescaped;
previous_child_single = true;
2015-07-24 00:18:46 +00:00
}
}
2015-10-20 08:37:49 +00:00
else if (previous_child_single && c[0] == '-')
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
bool is_range = false;
nrex_char next;
if (c[1] != '\0' && c[1] != ']')
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
if (c[1] == '\\')
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
const nrex_char* d = ++c;
next = nrex_unescape(d);
if (c == d)
{
NREX_COMPILE_ERROR("invalid escape token in range");
}
2015-07-24 00:18:46 +00:00
}
2015-10-20 08:37:49 +00:00
else
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
next = c[1];
++c;
2015-07-24 00:18:46 +00:00
}
2015-10-20 08:37:49 +00:00
is_range = true;
}
if (is_range)
{
if (next < previous_child)
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
NREX_COMPILE_ERROR("text range out of order");
2015-07-24 00:18:46 +00:00
}
2015-10-20 08:37:49 +00:00
group->pop_back();
group->add_child(NREX_NEW(nrex_node_range(previous_child, next)));
previous_child_single = false;
}
else
{
group->add_child(NREX_NEW(nrex_node_char(c[0])));
previous_child = c[0];
previous_child_single = true;
2015-07-24 00:18:46 +00:00
}
2015-10-20 08:37:49 +00:00
}
else
{
2015-07-24 00:18:46 +00:00
group->add_child(NREX_NEW(nrex_node_char(c[0])));
2015-10-20 08:37:49 +00:00
previous_child = c[0];
previous_child_single = true;
2015-07-24 00:18:46 +00:00
}
2015-10-20 08:37:49 +00:00
first_child = false;
2015-07-24 00:18:46 +00:00
}
}
else if (nrex_is_quantifier(c[0]))
{
2015-10-20 08:37:49 +00:00
int min = 0;
int max = -1;
bool valid_quantifier = true;
2015-07-24 00:18:46 +00:00
if (c[0] == '?')
{
2015-10-20 08:37:49 +00:00
min = 0;
max = 1;
2015-07-24 00:18:46 +00:00
}
else if (c[0] == '+')
{
2015-10-20 08:37:49 +00:00
min = 1;
max = -1;
2015-07-24 00:18:46 +00:00
}
else if (c[0] == '*')
{
2015-10-20 08:37:49 +00:00
min = 0;
max = -1;
2015-07-24 00:18:46 +00:00
}
else if (c[0] == '{')
{
bool max_set = false;
2015-10-20 08:37:49 +00:00
const nrex_char* d = c;
2015-07-24 00:18:46 +00:00
while (true)
{
2015-10-20 08:37:49 +00:00
++d;
if (d[0] == '\0')
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
valid_quantifier = false;
break;
2015-07-24 00:18:46 +00:00
}
2015-10-20 08:37:49 +00:00
else if (d[0] == '}')
2015-07-24 00:18:46 +00:00
{
break;
}
2015-10-20 08:37:49 +00:00
else if (d[0] == ',')
2015-07-24 00:18:46 +00:00
{
max_set = true;
continue;
}
2015-10-20 08:37:49 +00:00
else if (d[0] < '0' || '9' < d[0])
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
valid_quantifier = false;
break;
2015-07-24 00:18:46 +00:00
}
if (max_set)
{
2015-10-20 08:37:49 +00:00
if (max < 0)
2015-07-24 00:18:46 +00:00
{
2015-10-20 08:37:49 +00:00
max = int(d[0] - '0');
2015-07-24 00:18:46 +00:00
}
else
{
2015-10-20 08:37:49 +00:00
max = max * 10 + int(d[0] - '0');
2015-07-24 00:18:46 +00:00
}
}
else
{
2015-10-20 08:37:49 +00:00
min = min * 10 + int(d[0] - '0');
2015-07-24 00:18:46 +00:00
}
}
if (!max_set)
{
2015-10-20 08:37:49 +00:00
max = min;
}
if (valid_quantifier)
{
c = d;
2015-07-24 00:18:46 +00:00
}
}
2015-10-20 08:37:49 +00:00
if (valid_quantifier)
2015-07-24 00:18:46 +00:00
{
if (stack.top()->back == NULL || !stack.top()->back->quantifiable)
{
NREX_COMPILE_ERROR("element not quantifiable");
}
2015-10-20 08:37:49 +00:00
nrex_node_quantifier* quant = NREX_NEW(nrex_node_quantifier(min, max));
if (min == max)
{
if (stack.top()->back->length >= 0)
{
quant->length = max * stack.top()->back->length;
}
}
else
{
if (nrex_has_lookbehind(stack))
{
NREX_COMPILE_ERROR("variable length quantifiers inside lookbehind not supported");
}
}
quant->child = stack.top()->swap_back(quant);
quant->child->previous = NULL;
quant->child->next = NULL;
quant->child->parent = quant;
if (c[1] == '?')
{
quant->greedy = false;
++c;
}
}
else
{
stack.top()->add_child(NREX_NEW(nrex_node_char(c[0])));
2015-07-24 00:18:46 +00:00
}
}
else if (c[0] == '|')
{
2015-10-20 08:37:49 +00:00
if (nrex_has_lookbehind(stack))
{
NREX_COMPILE_ERROR("alternations inside lookbehind not supported");
}
2015-07-24 00:18:46 +00:00
stack.top()->add_childset();
}
else if (c[0] == '^' || c[0] == '$')
{
stack.top()->add_child(NREX_NEW(nrex_node_anchor((c[0] == '$'))));
}
else if (c[0] == '.')
{
stack.top()->add_child(NREX_NEW(nrex_node_shorthand('.')));
}
else if (c[0] == '\\')
{
2015-10-20 08:37:49 +00:00
if (nrex_is_shorthand(c[1]))
2015-07-24 00:18:46 +00:00
{
stack.top()->add_child(NREX_NEW(nrex_node_shorthand(c[1])));
++c;
}
else if (('1' <= c[1] && c[1] <= '9') || (c[1] == 'g' && c[2] == '{'))
2015-07-24 00:18:46 +00:00
{
int ref = 0;
bool unclosed = false;
if (c[1] == 'g')
2015-07-24 00:18:46 +00:00
{
unclosed = true;
2015-07-24 00:18:46 +00:00
c = &c[2];
}
while ('0' <= c[1] && c[1] <= '9')
2015-07-24 00:18:46 +00:00
{
ref = ref * 10 + int(c[1] - '0');
2015-07-24 00:18:46 +00:00
++c;
}
if (c[1] == '}')
{
unclosed = false;
++c;
}
if (ref > _capturing || ref <= 0 || unclosed)
2015-07-24 00:18:46 +00:00
{
NREX_COMPILE_ERROR("backreference to non-existent capture");
}
2015-10-20 08:37:49 +00:00
if (nrex_has_lookbehind(stack))
{
NREX_COMPILE_ERROR("backreferences inside lookbehind not supported");
}
2015-07-24 00:18:46 +00:00
stack.top()->add_child(NREX_NEW(nrex_node_backreference(ref)));
}
2015-10-20 08:37:49 +00:00
else if (c[1] == 'b' || c[1] == 'B')
{
stack.top()->add_child(NREX_NEW(nrex_node_word_boundary(c[1] == 'B')));
++c;
}
2015-07-24 00:18:46 +00:00
else
{
2015-10-20 08:37:49 +00:00
const nrex_char* d = c;
nrex_char unescaped = nrex_unescape(d);
if (c == d)
{
NREX_COMPILE_ERROR("invalid escape token");
}
stack.top()->add_child(NREX_NEW(nrex_node_char(unescaped)));
c = d;
2015-07-24 00:18:46 +00:00
}
}
else
{
stack.top()->add_child(NREX_NEW(nrex_node_char(c[0])));
}
}
2015-10-20 08:37:49 +00:00
if (stack.size() > 1)
{
NREX_COMPILE_ERROR("unclosed group '('");
}
2015-07-24 00:18:46 +00:00
return true;
}
bool nrex::match(const nrex_char* str, nrex_result* captures, int offset, int end) const
{
if (!_root)
{
return false;
}
nrex_search s(str, captures, _lookahead_depth);
2015-07-24 00:18:46 +00:00
if (end >= offset)
{
s.end = end;
}
else
{
s.end = NREX_STRLEN(str);
}
for (int i = offset; i <= s.end; ++i)
2015-07-24 00:18:46 +00:00
{
for (int c = 0; c <= _capturing; ++c)
{
captures[c].start = 0;
captures[c].length = 0;
}
if (_root->test(&s, i) >= 0)
{
return true;
}
}
return false;
}