8000 sljit/regex_src/regexJIT.c at master · devel-hm/sljit · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2591 lines (2241 loc) · 76.1 KB

regexJIT.c

File metadata and controls

2591 lines (2241 loc) · 76.1 KB
/*
* Stack-less Just-In-Time compiler
*
* Copyright 2009-2010 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef REGEX_MATCH_VERBOSE
#include <stdio.h>
#endif
#include <stdbool.h>
#include "sljitLir.h"
#include "regexJIT.h"
/* Extra, hidden flags:
{id!} where id > 0 found in the code. */
#define REGEX_ID_CHECK 0x100
/* When REGEX_NEWLINE && REGEX_MATCH_BEGIN defined, the pattern turn to a normal search,
which starts with [\r\n] character range. */
#define REGEX_FAKE_MATCH_BEGIN 0x200
/* When REGEX_NEWLINE && REGEX_MATCH_END defined, the pattern turn to a normal search,
which ends with [\r\n] character range. */
#define REGEX_FAKE_MATCH_END 0x400
/* Check match completition after every (FINISH_TEST + 1) steps. */
#define FINISH_TEST 0x7
/* --------------------------------------------------------------------- */
/* Structures for JIT-ed pattern matching */
/* --------------------------------------------------------------------- */
struct regex_machine
{
/* flags. */
int flags;
/* Number of state descriptors for one term. */
long no_states;
/* Total size. */
long size;
union {
void *init_match;
long (SLJIT_CALL *call_init)(void *next, void* match);
} u;
#if (defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
struct sljit_function_context context;
#endif
void *continue_match;
/* Variable sized array to contain the handler addresses. */
unsigned long entry_addrs[1];
};
struct regex_match
{
/* Current and next state array. */
long *current;
long *next;
/* Starting. */
long head;
/* String character index (ever increasing). */
long index;
/* Best match found so far (members in priority order). */
long best_begin;
long best_end;
long best_id;
/* Bool flags (encoded as word). */
long fast_quit;
long fast_forward;
/* Machine. */
struct regex_machine *machine;
union {
void *continue_match;
void (SLJIT_CALL *call_continue)(struct regex_match *match, const regex_char_t *input_string, int length);
} u;
/* Variable sized array to contain the state arrays. */
long states[1];
};
/* State vector
ITEM[0] - pointer to the address inside the machine code
ITEM[1] - next pointer
ITEM[2] - string started from (optional)
ITEM[3] - max ID (optional) */
/* Register allocation. */
/* Current state array (loaded & stored: regex_match->current). */
#define R_CURR_STATE SLJIT_S0
/* Next state array (loaded & stored: regex_match->next). */
#define R_NEXT_STATE SLJIT_S1
/* Head (loaded & stored: regex_match->head). */
#define R_NEXT_HEAD SLJIT_S2
/* String fragment pointer. */
#define R_STRING SLJIT_S3
/* String fragment length. */
#define R_LENGTH SLJIT_S4
/* 'struct regex_match*' */
#define R_REGEX_MATCH SLJIT_R0
/* Current character. */
#define R_CURR_CHAR SLJIT_R1
/* Temporary reg. */
#define R_TEMP SLJIT_R2
/* Caches the regex_match->best_begin. */
#define R_BEST_BEGIN SLJIT_R3
/* Current character index. */
#define R_CURR_INDEX SLJIT_R4
/* --------------------------------------------------------------------- */
/* Stack management */
/* --------------------------------------------------------------------- */
/* Try to allocate 2^n blocks. */
#define STACK_FRAGMENT_SIZE (((64 * sizeof(struct stack_item)) - (sizeof(struct stack_fragment_data))) / (sizeof(struct stack_item)))
struct stack_item {
int type;
int value;
};
struct stack_fragment_data {
struct stack_fragment *next;
struct stack_fragment *prev;
};
struct stack_fragment {
struct stack_fragment_data data;
struct stack_item items[STACK_FRAGMENT_SIZE];
};
struct stack {
struct stack_fragment *first;
struct stack_fragment *last;
int index;
int count;
};
#if (defined SLJIT_DEBUG && SLJIT_DEBUG)
static void stack_check(struct stack *stack)
{
struct stack_fragment *curr;
int found;
if (!stack)
return;
SLJIT_ASSERT(stack->index >= 0 && stack->index < STACK_FRAGMENT_SIZE);
if (stack->first == NULL) {
SLJIT_ASSERT(stack->first == NULL && stack->last == NULL);
SLJIT_ASSERT(stack->index == STACK_FRAGMENT_SIZE - 1 && stack->count == 0);
return;
}
found = 0;
if (stack->last == NULL) {
SLJIT_ASSERT(stack->index == STACK_FRAGMENT_SIZE - 1 && stack->count == 0);
found = 1;
}
else
SLJIT_ASSERT(stack->index >= 0 && stack->count >= 0);
SLJIT_ASSERT(stack->first->data.prev == NULL);
curr = stack->first;
while (curr) {
if (curr == stack->last)
found = 1;
if (curr->data.next)
SLJIT_ASSERT(curr->data.next->data.prev == curr);
curr = curr->data.next;
}
SLJIT_ASSERT(found);
}
#endif
static void stack_init(struct stack *stack)
{
stack->first = NULL;
stack->last = NULL;
stack->index = STACK_FRAGMENT_SIZE - 1;
stack->count = 0;
}
static void stack_destroy(struct stack *stack)
{
struct stack_fragment *curr = stack->first;
struct stack_fragment *prev;
#if (defined SLJIT_DEBUG && SLJIT_DEBUG)
stack_check(stack);
#endif
while (curr) {
prev = curr;
curr = curr->data.next;
free(prev);
}
}
static __inline struct stack_item* stack_top(struct stack *stack)
{
SLJIT_ASSERT(stack->last);
return stack->last->items + stack->index;
}
static int stack_push(struct stack *stack, int type, int value)
{
if (stack->last) {
stack->index++;
if (stack->index >= STACK_FRAGMENT_SIZE) {
stack->index = 0;
if (!stack->last->data.next) {
stack->last->data.next = malloc(sizeof(struct stack_fragment));
if (!stack->last->data.next)
return 1;
stack->last->data.next->data.next = NULL;
stack->last->data.next->data.prev = stack->last;
}
stack->last = stack->last->data.next;
}
}
else if (!stack->first) {
stack->last = malloc(sizeof(struct stack_fragment));
if (!stack->last)
return 1;
stack->last->data.prev = NULL;
stack->last->data.next = NULL;
stack->first = stack->last;
stack->index = 0;
}
else {
stack->last = stack->first;
stack->index = 0;
}
stack->last->items[stack->index].type = type;
stack->last->items[stack->index].value = value;
stack->count++;
#if (defined SLJIT_DEBUG && SLJIT_DEBUG)
stack_check(stack);
#endif
return 0;
}
static struct stack_item* stack_pop(struct stack *stack)
{
struct stack_item *ret = stack_top(stack);
if (stack->index > 0)
stack->index--;
else {
stack->last = stack->last->data.prev;
stack->index = STACK_FRAGMENT_SIZE - 1;
}
stack->count--;
#if (defined SLJIT_DEBUG && SLJIT_DEBUG)
stack_check(stack);
#endif
return ret;
}
static __inline void stack_clone(struct stack *src, struct stack *dst)
{
*dst = *src;
}
static int stack_push_copy(struct stack *stack, int items, int length)
{
struct stack_fragment *frag1;
int ind1;
struct stack_fragment *frag2;
int ind2;
int counter;
SLJIT_ASSERT(stack->count >= length && items <= length && items > 0);
/* Allocate the necessary elements. */
counter = items;
frag1 = stack->last;
ind1 = stack->index;
while (counter > 0) {
if (stack->index + counter >= STACK_FRAGMENT_SIZE) {
counter -= STACK_FRAGMENT_SIZE - stack->index - 1 + 1;
stack->index = 0;
if (!stack->last->data.next) {
stack->last->data.next = malloc(sizeof(struct stack_fragment));
if (!stack->last->data.next)
return 1;
stack->last->data.next->data.next = NULL;
stack->last->data.next->data.prev = stack->last;
}
stack->last = stack->last->data.next;
}
else {
stack->index += counter;
counter = 0;
}
}
frag2 = stack->last;
ind2 = stack->index;
while (length > 0) {
frag2->items[ind2--] = frag1->items[ind1--];
if (ind1 < 0) {
ind1 = STACK_FRAGMENT_SIZE - 1;
frag1 = frag1->data.prev;
}
if (ind2 < 0) {
ind2 = STACK_FRAGMENT_SIZE - 1;
frag2 = frag2->data.prev;
}
length--;
}
#if (defined SLJIT_DEBUG && SLJIT_DEBUG)
stack_check(stack);
#endif
stack->count += items;
return 0;
}
/* --------------------------------------------------------------------- */
/* Parser */
/* --------------------------------------------------------------------- */
enum {
/* Common. */
type_begin,
type_end,
type_char,
type_newline,
type_id,
type_rng_start,
type_rng_end,
type_rng_char,
type_rng_left,
type_rng_right,
/* generator only. */
type_branch,
type_jump,
/* Parser only. */
type_open_br,
type_close_br,
type_select,
type_asterisk,
type_plus_sign,
type_qestion_mark
};
struct compiler_common {
/* Temporary stacks. */
struct stack stack;
struct stack depth;
/* REGEX_ flags. */
int flags;
/* Encoded size of the dfa representation. */
long dfa_size;
/* Number of terms. */
long terms_size;
/* Number of state descriptors for one term (same as machine->no_states). */
long no_states;
/* Number of type_rng_(char|left)-s in the longest character range. */
long longest_range_size;
/* DFA linear representation (size: dfa_size). */
struct stack_item *dfa_transitions;
/* Term id and search state pairs (size: dfa_size). */
struct stack_item *search_states;
/* sljit compiler */
struct sljit_compiler *compiler;
/* Machine data, which must be kept for later use. */
struct regex_machine *machine;
/* Temporary space for jumps (size: longest_range_size). */
struct sljit_jump **range_jump_list;
};
static const regex_char_t* decode_number(const regex_char_t *regex_string, int length, int *result)
{
int value = 0;
SLJIT_ASSERT(length > 0);
if (*regex_string < '0' || *regex_string > '9') {
*result = -1;
return regex_string;
}
while (length > 0 && *regex_string >= '0' && *regex_string <= '9') {
value = value * 10 + (*regex_string - '0');
length--;
regex_string++;
}
*result = value;
return regex_string;
}
static int iterate(struct stack *stack, int min, int max)
{
struct stack it;
struct stack_item *item;
int count = -1;
int len = 0;
int depth = 0;
stack_clone(stack, &it);
/* Calculate size. */
while (count < 0) {
item = stack_pop(&it);
switch (item->type) {
case type_id:
case type_rng_end:
case type_rng_char:
case type_rng_left:
case type_rng_right:
case type_plus_sign:
case type_qestion_mark:
len++;
break;
case type_asterisk:
len += 2;
break;
case type_close_br:
depth++;
break;
case type_open_br:
SLJIT_ASSERT(depth > 0);
depth--;
if (depth == 0)
count = it.count;
break;
case type_select:
SLJIT_ASSERT(depth > 0);
len += 2;
break;
default:
SLJIT_ASSERT(item->type != type_begin && item->type != type_end);
if (depth == 0)
count = it.count;
len++;
break;
}
}
if (min == 0 && max == 0) {
/* {0,0} case, not {0,} case: delete subtree. */
stack_clone(&it, stack);
/* and put an empty bracket expression instead of it. */
if (stack_push(stack, type_open_br, 0))
return REGEX_MEMORY_ERROR;
if (stack_push(stack, type_close_br, 0))
return REGEX_MEMORY_ERROR;
return len;
}
count = stack->count - count;
/* Put an open bracket before the sequence. */
if (stack_push_copy(stack, 1, count))
return -1;
#if (defined SLJIT_DEBUG && SLJIT_DEBUG)
SLJIT_ASSERT(stack_push(&it, type_open_br, 0) == 0);
#else
stack_push(&it, type_open_br, 0);
#endif
/* Copy the data. */
if (max > 0) {
len = len * (max - 1);
max -= min;
/* Insert ? operators. */
len += max;
if (min > 0) {
min--;
while (min > 0) {
if (stack_push_copy(stack, count, count))
return -1;
min--;
}
if (max > 0) {
if (stack_push_copy(stack, count, count))
return -1;
if (stack_push(stack, type_qestion_mark, 0))
return REGEX_MEMORY_ERROR;
count++;
max--;
}
}
else {
SLJIT_ASSERT(max > 0);
max--;
count++;
if (stack_push(stack, type_qestion_mark, 0))
return REGEX_MEMORY_ERROR;
}
while (max > 0) {
if (stack_push_copy(stack, count, count))
return -1;
max--;
}
}
else {
SLJIT_ASSERT(min > 0);
min--;
/* Insert + operator. */
len = len * min + 1;
while (min > 0) {
if (stack_push_copy(stack, count, count))
return -1;
min--;
}
if (stack_push(stack, type_plus_sign, 0))
return REGEX_MEMORY_ERROR;
}
/* Close the opened bracket. */
if (stack_push(stack, type_close_br, 0))
return REGEX_MEMORY_ERROR;
return len;
}
static int parse_iterator(const regex_char_t *regex_string, int length, struct stack *stack, long *dfa_size, int begin)
{
/* We only know that *regex_string == { . */
int val1, val2;
const regex_char_t *base_from = regex_string;
const regex_char_t *from;
length--;
regex_string++;
/* Decode left value. */
val2 = -1;
if (length == 0)
return -2;
if (*regex_string == ',') {
val1 = 0;
length--;
regex_string++;
}
else {
from = regex_string;
regex_string = decode_number(regex_string, length, &val1);
if (val1 < 0)
return -2;
length -= regex_string - from;
if (length == 0)
return -2;
if (*regex_string == '}') {
val2 = val1;
if (val1 == 0)
val1 = -1;
}
else if (length >= 2 && *regex_string == '!' && regex_string[1] == '}') {
/* Non posix extension. */
if (stack_push(stack, type_id, val1))
return -1;
(*dfa_size)++;
return (regex_string - base_from) + 1;
}
else {
if (*regex_string != ',')
return -2;
length--;
regex_string++;
}
}
if (begin)
return -2;
/* Decode right value. */
if (val2 == -1) {
if (length == 0)
return -2;
if (*regex_string == '}')
val2 = 0;
else {
from = regex_string;
regex_string = decode_number(regex_string, length, &val2);
length -= regex_string - from;
if (val2 < 0 || length == 0 || *regex_string != '}' || val2 < val1)
return -2;
if (val2 == 0) {
SLJIT_ASSERT(val1 == 0);
val1 = -1;
}
}
}
/* Fast cases. */
if (val1 > 1 || val2 > 1) {
val1 = iterate(stack, val1, val2);
if (val1 < 0)
return -1;
*dfa_size += val1;
}
else if (val1 == 0 && val2 == 0) {
if (stack_push(stack, type_asterisk, 0))
return -1;
*dfa_size += 2;
}
else if (val1 == 1 && val2 == 0) {
if (stack_push(stack, type_plus_sign, 0))
return -1;
(*dfa_size)++;
}
else if (val1 == 0 && val2 == 1) {
if (stack_push(stack, type_qestion_mark, 0))
return -1;
(*dfa_size)++;
}
else if (val1 == -1) {
val1 = iterate(stack, 0, 0);
if (val1 < 0)
return -1;
*dfa_size -= val1;
SLJIT_ASSERT(*dfa_size >= 2);
}
else {
/* Ignore. */
SLJIT_ASSERT(val1 == 1 && val2 == 1);
}
return regex_string - base_from;
}
static int parse_char_range(const regex_char_t *regex_string, int length, struct compiler_common *compiler_common)
{
struct stack* stack = &compiler_common->stack;
const regex_char_t *base_from = regex_string;
regex_char_t left_char, right_char, tmp_char;
length--;
regex_string++;
if (length == 0)
return -2;
if (*regex_string != '^') {
if (stack_push(stack, type_rng_start, 0))
return -1;
}
else {
length--;
regex_string++;
if (length == 0)
return -2;
if (stack_push(stack, type_rng_start, 1))
return -1;
}
/* For both the type_rng_start & type_rng_end. */
compiler_common->dfa_size += 2;
/* Range must be at least 1 character. */
if (*regex_string == ']') {
length--;
regex_string++;
if (stack_push(stack, type_rng_char, ']'))
return -1;
compiler_common->dfa_size++;
}
while (1) {
if (length == 0)
return -2;
if (*regex_string == ']')
break;
if (*regex_string != '\\')
left_char = *regex_string;
else {
regex_string++;
length--;
if (length == 0)
return -2;
left_char = *regex_string;
}
regex_string++;
length--;
/* Is a range here? */
if (length >= 3 && *regex_string == '-' && *(regex_string + 1) != ']') {
regex_string++;
length--;
if (*regex_string != '\\')
right_char = *regex_string;
else {
regex_string++;
length--;
if (length == 0)
return -2;
right_char = *regex_string;
}
regex_string++;
length--;
if (left_char > right_char) {
/* Swap if necessary. */
tmp_char = left_char;
left_char = right_char;
right_char = tmp_char;
}
if (stack_push(stack, type_rng_left, left_char))
return -1;
if (stack_push(stack, type_rng_right, right_char))
return -1;
compiler_common->dfa_size += 2;
}
else {
if (stack_push(stack, type_rng_char, left_char))
return -1;
compiler_common->dfa_size++;
}
}
if (stack_push(stack, type_rng_end, 0))
return -1;
return regex_string - base_from;
}
static int parse(const regex_char_t *regex_string, int length, struct compiler_common *compiler_common)
{
/* Depth of bracketed expressions. */
int depth = 0;
/* Have we already found a term? '1' if not yet. */
int begin = 1;
/* Cache stack pointer. */
struct stack* stack = &compiler_common->stack;
int tmp;
/* Type_begin and type_end. */
compiler_common->dfa_size = 2;
stack_init(stack);
if (stack_push(stack, type_begin, 0))
return REGEX_MEMORY_ERROR;
if (length > 0 && *regex_string == '^') {
compiler_common->flags |= REGEX_MATCH_BEGIN;
length--;
regex_string++;
}
if ((compiler_common->flags & (REGEX_MATCH_BEGIN | REGEX_NEWLINE)) == (REGEX_MATCH_BEGIN | REGEX_NEWLINE)) {
/* Replace REGEX_MATCH_BEGIN flag to REGEX_FAKE_MATCH_BEGIN */
compiler_common->flags &= ~REGEX_MATCH_BEGIN;
compiler_common->flags |= REGEX_FAKE_MATCH_BEGIN;
/* and append a new-line search. */
if (stack_push(stack, type_newline, 0))
return REGEX_MEMORY_ERROR;
compiler_common->dfa_size++;
/* Begin intentionally kept as 1. */
}
while (length > 0) {
switch (*regex_string) {
case '\\' :
length--;
regex_string++;
if (length == 0)
return REGEX_INVALID_REGEX;
if (stack_push(stack, type_char, *regex_string))
return REGEX_MEMORY_ERROR;
begin = 0;
compiler_common->dfa_size++;
break;
case '.' :
if (stack_push(stack, type_rng_start, 1))
return REGEX_MEMORY_ERROR;
if (compiler_common->flags & REGEX_NEWLINE) {
if (stack_push(stack, type_rng_char, '\n'))
return REGEX_MEMORY_ERROR;
if (stack_push(stack, type_rng_char, '\r'))
return REGEX_MEMORY_ERROR;
compiler_common->dfa_size += 2;
}
if (stack_push(stack, type_rng_end, 1))
return REGEX_MEMORY_ERROR;
begin = 0;
compiler_common->dfa_size += 2;
break;
case '(' :
depth++;
if (stack_push(stack, type_open_br, 0))
return REGEX_MEMORY_ERROR;
begin = 1;
break;
case ')' :
if (depth == 0)
return REGEX_INVALID_REGEX;
depth--;
if (stack_push(stack, type_close_br, 0))
return REGEX_MEMORY_ERROR;
begin = 0;
break;
case '|' :
if (stack_push(stack, type_select, 0))
return REGEX_MEMORY_ERROR;
begin = 1;
compiler_common->dfa_size += 2;
break;
case '*' :
if (begin)
return REGEX_INVALID_REGEX;
if (stack_push(stack, type_asterisk, 0))
return REGEX_MEMORY_ERROR;
compiler_common->dfa_size += 2;
break;
case '?' :
case '+' :
if (begin)
return REGEX_INVALID_REGEX;
if (stack_push(stack, (*regex_string == '+') ? type_plus_sign : type_qestion_mark, 0))
return REGEX_MEMORY_ERROR;
compiler_common->dfa_size++;
break;
case '{' :
tmp = parse_iterator(regex_string, length, stack, &compiler_common->dfa_size, begin);
if (tmp >= 0) {
length -= tmp;
regex_string += tmp;
}
else if (tmp == -1)
return REGEX_MEMORY_ERROR;
else {
/* Not a valid range expression. */
SLJIT_ASSERT(tmp == -2);
if (stack_push(stack, type_char, '{'))
return REGEX_MEMORY_ERROR;
compiler_common->dfa_size++;
}
break;
case '[' :
tmp = parse_char_range(regex_string, length, compiler_common);
if (tmp >= 0) {
length -= tmp;
regex_string += tmp;
}
else if (tmp == -1)
return REGEX_MEMORY_ERROR;
else {
SLJIT_ASSERT(tmp == -2);
return REGEX_INVALID_REGEX;
}
begin = 0;
break;
default:
if (length == 1 && *regex_string == '$') {
compiler_common->flags |= REGEX_MATCH_END;
break;
}
if (stack_push(stack, type_char, *regex_string))
return REGEX_MEMORY_ERROR;
begin = 0;
compiler_common->dfa_size++;
break;
}
length--;
regex_string++;
}
if (depth != 0)
return REGEX_INVALID_REGEX;
if ((compiler_common->flags & (REGEX_MATCH_END | REGEX_NEWLINE)) == (REGEX_MATCH_END | REGEX_NEWLINE)) {
/* Replace REGEX_MATCH_END flag to REGEX_FAKE_MATCH_END */
compiler_common->flags &= ~REGEX_MATCH_END;
compiler_common->flags |= REGEX_FAKE_MATCH_END;
/* and append a new-line search. */
if (stack_push(stack, type_newline, 1))
return REGEX_MEMORY_ERROR;
compiler_common->dfa_size++;
/* Begin intentionally kept as 1. */
}
if (stack_push(stack, type_end, 0))
return REGEX_MEMORY_ERROR;
return REGEX_NO_ERROR;
}
/* --------------------------------------------------------------------- */
/* Generating machine state transitions */
/* --------------------------------------------------------------------- */
#define PUT_TRANSITION(typ, val) \
do { \
--transitions_ptr; \
transitions_ptr->type = typ; \
transitions_ptr->value = val; \
} while (0)
static struct stack_item* handle_iteratives(struct stack_item *transitions_ptr, struct stack_item *transitions, struct stack *depth)
{
struct stack_item *item;
while (1) {
item = stack_top(depth);
switch (item->type) {
case type_asterisk:
SLJIT_ASSERT(transitions[item->value].type == type_branch);
transitions[item->value].value = transitions_ptr - transitions;
PUT_TRANSITION(type_branch, item->value + 1);
break;
case type_plus_sign:
SLJIT_ASSERT(transitions[item->value].type == type_branch);
transitions[item->value].value = transitions_ptr - transitions;
break;
case type_qestion_mark:
PUT_TRANSITION(type_branch, item->value);
break;
default:
return transitions_ptr;
}
stack_pop(depth);
}
}
static int generate_transitions(struct compiler_common *compiler_common)
{
struct stack *stack = &compiler_common->stack;
struct stack *depth = &compiler_common->depth;
struct stack_item *transitions_ptr;
struct stack_item *item;
stack_init(depth);
compiler_common->dfa_transitions = malloc(sizeof(struct stack_item) * compiler_common->dfa_size);
if (!compiler_common->dfa_transitions)
return REGEX_MEMORY_ERROR;
/* Go through the items of the stack and generate the necessary branches and jumps (edges of DFA). */
transitions_ptr = compiler_common->dfa_transitions + compiler_common->dfa_size;
while (stack->count > 0) {
item = stack_pop(stack);
switch (item->type) {
case type_begin:
case type_open_br:
item = stack_pop(depth);
if (item->type == type_select)
PUT_TRANSITION(type_branch, item->value + 1);
else
SLJIT_ASSERT(item->type == type_close_br);
if (stack->count == 0)
0