📄 grammar.c
字号:
mem_free ((void **) re);
}
}
static byte regbyte_ctx_extract (regbyte_ctx **re, map_byte *reg)
{
/* first lookup in the register stack */
while (*re != NULL)
{
if ((**re).m_regbyte == reg)
return (**re).m_current_value;
re = &(**re).m_prev;
}
/* if not found - return the default value */
return reg->data;
}
/*
emit type typedef
*/
typedef enum emit_type_
{
et_byte, /* explicit number */
et_stream, /* eaten character */
et_position /* current position */
} emit_type;
/*
emit destination typedef
*/
typedef enum emit_dest_
{
ed_output, /* write to the output buffer */
ed_regbyte /* write a particular regbyte */
} emit_dest;
/*
emit typedef
*/
typedef struct emit_
{
emit_dest m_emit_dest;
emit_type m_emit_type; /* ed_output */
byte m_byte; /* et_byte */
map_byte *m_regbyte; /* ed_regbyte */
byte *m_regname; /* ed_regbyte - temporary */
struct emit_ *m_next;
} emit;
static void emit_create (emit **em)
{
*em = (emit *) mem_alloc (sizeof (emit));
if (*em)
{
(**em).m_emit_dest = ed_output;
(**em).m_emit_type = et_byte;
(**em).m_byte = '\0';
(**em).m_regbyte = NULL;
(**em).m_regname = NULL;
(**em).m_next = NULL;
}
}
static void emit_destroy (emit **em)
{
if (*em)
{
emit_destroy (&(**em).m_next);
mem_free ((void **) &(**em).m_regname);
mem_free ((void **) em);
}
}
static unsigned int emit_size (emit *_E)
{
unsigned int n = 0;
while (_E != NULL)
{
if (_E->m_emit_dest == ed_output)
{
if (_E->m_emit_type == et_position)
n += 4; /* position is a 32-bit unsigned integer */
else
n++;
}
_E = _E->m_next;
}
return n;
}
static int emit_push (emit *_E, byte *_P, byte c, unsigned int _Pos, regbyte_ctx **_Ctx)
{
while (_E != NULL)
{
if (_E->m_emit_dest == ed_output)
{
if (_E->m_emit_type == et_byte)
*_P++ = _E->m_byte;
else if (_E->m_emit_type == et_stream)
*_P++ = c;
else /* _Em->type == et_position */
{
*_P++ = (byte) (_Pos);
*_P++ = (byte) (_Pos >> 8);
*_P++ = (byte) (_Pos >> 16);
*_P++ = (byte) (_Pos >> 24);
}
}
else
{
regbyte_ctx *new_rbc;
regbyte_ctx_create (&new_rbc);
if (new_rbc == NULL)
return 1;
new_rbc->m_prev = *_Ctx;
new_rbc->m_regbyte = _E->m_regbyte;
*_Ctx = new_rbc;
if (_E->m_emit_type == et_byte)
new_rbc->m_current_value = _E->m_byte;
else if (_E->m_emit_type == et_stream)
new_rbc->m_current_value = c;
}
_E = _E->m_next;
}
return 0;
}
/*
error typedef
*/
typedef struct error_
{
byte *m_text;
byte *m_token_name;
struct rule_ *m_token;
} error;
static void error_create (error **er)
{
*er = (error *) mem_alloc (sizeof (error));
if (*er)
{
(**er).m_text = NULL;
(**er).m_token_name = NULL;
(**er).m_token = NULL;
}
}
static void error_destroy (error **er)
{
if (*er)
{
mem_free ((void **) &(**er).m_text);
mem_free ((void **) &(**er).m_token_name);
mem_free ((void **) er);
}
}
struct dict_;
static byte *error_get_token (error *, struct dict_ *, const byte *, unsigned int);
/*
condition operand type typedef
*/
typedef enum cond_oper_type_
{
cot_byte, /* constant 8-bit unsigned integer */
cot_regbyte /* pointer to byte register containing the current value */
} cond_oper_type;
/*
condition operand typedef
*/
typedef struct cond_oper_
{
cond_oper_type m_type;
byte m_byte; /* cot_byte */
map_byte *m_regbyte; /* cot_regbyte */
byte *m_regname; /* cot_regbyte - temporary */
} cond_oper;
/*
condition type typedef
*/
typedef enum cond_type_
{
ct_equal,
ct_not_equal
} cond_type;
/*
condition typedef
*/
typedef struct cond_
{
cond_type m_type;
cond_oper m_operands[2];
} cond;
static void cond_create (cond **co)
{
*co = (cond *) mem_alloc (sizeof (cond));
if (*co)
{
(**co).m_operands[0].m_regname = NULL;
(**co).m_operands[1].m_regname = NULL;
}
}
static void cond_destroy (cond **co)
{
if (*co)
{
mem_free ((void **) &(**co).m_operands[0].m_regname);
mem_free ((void **) &(**co).m_operands[1].m_regname);
mem_free ((void **) co);
}
}
/*
specifier type typedef
*/
typedef enum spec_type_
{
st_false,
st_true,
st_byte,
st_byte_range,
st_string,
st_identifier,
st_identifier_loop,
st_debug
} spec_type;
/*
specifier typedef
*/
typedef struct spec_
{
spec_type m_spec_type;
byte m_byte[2]; /* st_byte, st_byte_range */
byte *m_string; /* st_string */
struct rule_ *m_rule; /* st_identifier, st_identifier_loop */
emit *m_emits;
error *m_errtext;
cond *m_cond;
struct spec_ *next;
} spec;
static void spec_create (spec **sp)
{
*sp = (spec *) mem_alloc (sizeof (spec));
if (*sp)
{
(**sp).m_spec_type = st_false;
(**sp).m_byte[0] = '\0';
(**sp).m_byte[1] = '\0';
(**sp).m_string = NULL;
(**sp).m_rule = NULL;
(**sp).m_emits = NULL;
(**sp).m_errtext = NULL;
(**sp).m_cond = NULL;
(**sp).next = NULL;
}
}
static void spec_destroy (spec **sp)
{
if (*sp)
{
spec_destroy (&(**sp).next);
emit_destroy (&(**sp).m_emits);
error_destroy (&(**sp).m_errtext);
mem_free ((void **) &(**sp).m_string);
cond_destroy (&(**sp).m_cond);
mem_free ((void **) sp);
}
}
GRAMMAR_IMPLEMENT_LIST_APPEND(spec)
/*
operator typedef
*/
typedef enum oper_
{
op_none,
op_and,
op_or
} oper;
/*
rule typedef
*/
typedef struct rule_
{
oper m_oper;
spec *m_specs;
struct rule_ *next;
int m_referenced;
} rule;
static void rule_create (rule **ru)
{
*ru = (rule *) mem_alloc (sizeof (rule));
if (*ru)
{
(**ru).m_oper = op_none;
(**ru).m_specs = NULL;
(**ru).next = NULL;
(**ru).m_referenced = 0;
}
}
static void rule_destroy (rule **ru)
{
if (*ru)
{
rule_destroy (&(**ru).next);
spec_destroy (&(**ru).m_specs);
mem_free ((void **) ru);
}
}
GRAMMAR_IMPLEMENT_LIST_APPEND(rule)
/*
returns unique grammar id
*/
static grammar next_valid_grammar_id (void)
{
static grammar id = 0;
return ++id;
}
/*
dictionary typedef
*/
typedef struct dict_
{
rule *m_rulez;
rule *m_syntax;
rule *m_string;
map_byte *m_regbytes;
grammar m_id;
struct dict_ *next;
} dict;
static void dict_create (dict **di)
{
*di = (dict *) mem_alloc (sizeof (dict));
if (*di)
{
(**di).m_rulez = NULL;
(**di).m_syntax = NULL;
(**di).m_string = NULL;
(**di).m_regbytes = NULL;
(**di).m_id = next_valid_grammar_id ();
(**di).next = NULL;
}
}
static void dict_destroy (dict **di)
{
if (*di)
{
rule_destroy (&(**di).m_rulez);
map_byte_destroy (&(**di).m_regbytes);
mem_free ((void **) di);
}
}
GRAMMAR_IMPLEMENT_LIST_APPEND(dict)
static void dict_find (dict **di, grammar key, dict **data)
{
while (*di)
{
if ((**di).m_id == key)
{
*data = *di;
return;
}
di = &(**di).next;
}
*data = NULL;
}
static dict *g_dicts = NULL;
/*
byte array typedef
*/
typedef struct barray_
{
byte *data;
unsigned int len;
} barray;
static void barray_create (barray **ba)
{
*ba = (barray *) mem_alloc (sizeof (barray));
if (*ba)
{
(**ba).data = NULL;
(**ba).len = 0;
}
}
static void barray_destroy (barray **ba)
{
if (*ba)
{
mem_free ((void **) &(**ba).data);
mem_free ((void **) ba);
}
}
/*
reallocates byte array to requested size,
returns 0 on success,
returns 1 otherwise
*/
static int barray_resize (barray **ba, unsigned int nlen)
{
byte *new_pointer;
if (nlen == 0)
{
mem_free ((void **) &(**ba).data);
(**ba).data = NULL;
(**ba).len = 0;
return 0;
}
else
{
new_pointer = (byte *) mem_realloc ((**ba).data, (**ba).len * sizeof (byte),
nlen * sizeof (byte));
if (new_pointer)
{
(**ba).data = new_pointer;
(**ba).len = nlen;
return 0;
}
}
return 1;
}
/*
adds byte array pointed by *nb to the end of array pointed by *ba,
returns 0 on success,
returns 1 otherwise
*/
static int barray_append (barray **ba, barray **nb)
{
const unsigned int len = (**ba).len;
if (barray_resize (ba, (**ba).len + (**nb).len))
return 1;
mem_copy ((**ba).data + len, (**nb).data, (**nb).len);
return 0;
}
/*
adds emit chain pointed by em to the end of array pointed by *ba,
returns 0 on success,
returns 1 otherwise
*/
static int barray_push (barray **ba, emit *em, byte c, unsigned int pos, regbyte_ctx **rbc)
{
unsigned int count = emit_size (em);
if (barray_resize (ba, (**ba).len + count))
return 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -