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