Merged rule parsers

This commit is contained in:
Devine Lu Linvega 2024-04-27 15:06:00 -07:00
parent dd1fddb20c
commit 93326ecf73
1 changed files with 19 additions and 21 deletions

View File

@ -104,7 +104,7 @@ write_tail(char *s)
static int
apply_rule(Rule *r, char *s)
{
unsigned int i, rid;
unsigned char i, rid;
char c, *a = r->a, *b = r->b, *origin = dst_, *reg;
/* phase: clean regs */
if(rmax) {
@ -152,28 +152,23 @@ apply_rule(Rule *r, char *s)
}
static char *
compile_rule(Rule *r, int id, char *src)
parse_frag(Rule *r, int side, char *src)
{
char c, *cap;
int wrapped;
r->id = id, r->a = &empty, r->b = ∅
/* phase: compile left */
char c, *cap;
if(side)
r->b = ∅
else
r->a = ∅
while((c = *src) && c == ' ') src++;
if(c == ')' || (c == '<' && src[1] == '>')) return src;
r->a = dict_, cap = walk(src), wrapped = c == '(';
if(side)
r->b = dict_;
else
r->a = dict_;
cap = walk(src), wrapped = c == '(';
if(wrapped) src++, cap--;
while(src < cap) {
c = *src, *dict_++ = *src++;
}
src += wrapped, *dict_++ = 0;
/* phase: compile right */
while((c = *src) && c == ' ') src++;
if(c == ')' || (c == '<' && src[1] == '>')) return src;
r->b = dict_, cap = walk(src), wrapped = c == '(';
if(wrapped) src++, cap--;
while(src < cap) {
c = *src, *dict_++ = *src++;
}
while(src < cap) c = *src, *dict_++ = *src++;
src += wrapped, *dict_++ = 0;
return src;
}
@ -203,8 +198,8 @@ rewrite(void)
Rule *r = NULL;
/* phase: define */
if(c == '<' && s[1] == '>') {
r = rules_;
s = compile_rule(r, rules_ - rules - 1, s + 2);
r = rules_, r->id = rules_ - rules - 1;
s = parse_frag(r, 1, parse_frag(r, 0, s + 2));
if(*r->a) {
if(!quiet && r->a)
printf("<> (%s) (%s)\n", r->a, r->b);
@ -226,7 +221,10 @@ rewrite(void)
}
/* phase: lambda */
if(c == '?' && s[1] == '(') {
cap = walk(s + 1), compile_rule(&lambda, -1, s + 2), s = cap;
cap = walk(s + 1);
r = &lambda, r->id = -1;
parse_frag(r, 1, parse_frag(r, 0, s + 2));
s = cap;
while(*s == ' ') s++;
if(!apply_rule(&lambda, s)) write_tail(s);
return 1;