Housekeeping

This commit is contained in:
Devine Lu Linvega 2024-04-22 17:54:17 -07:00
parent 9b034eac95
commit 7a27c2ca5b
1 changed files with 35 additions and 38 deletions

View File

@ -31,7 +31,7 @@ walk(char *s)
} }
static void static void
put_reg(char r, char *reg) write_reg(char r, char *reg)
{ {
char c, *cap = walk(reg); char c, *cap = walk(reg);
if(r == '*') { if(r == '*') {
@ -78,7 +78,7 @@ put_reg(char r, char *reg)
} }
static int static int
commit_rule(Rule *r, char *s, int create) write_rule(Rule *r, char *s, int create)
{ {
while((*dst_++ = *s++)) while((*dst_++ = *s++))
; ;
@ -96,40 +96,6 @@ commit_rule(Rule *r, char *s, int create)
return 1; return 1;
} }
static int
write_rule(Rule *r, char *s)
{
char c, *b = r->b, *reg, *origin = dst_;
while((c = *b++))
if(c == '?' && (reg = regs[(int)*b]))
put_reg(*b++, reg);
else
*dst_++ = c;
if(dst_ == origin) {
while(*s == ' ') s++;
if(*s == ')' && *(dst_ - 1) == ' ') dst_--;
}
return commit_rule(r, s, 0);
}
static char *
parse_frag(char *s)
{
char c, *cap;
while((c = *s) && c == ' ') s++;
if(c != ')' && !(c == '<' && s[1] == '>')) {
cap = walk(s);
if(c == '(') {
s++;
while(s < cap - 1) *dict_++ = *s++;
s++;
} else
while(s < cap) *dict_++ = *s++;
}
*dict_++ = 0;
return s;
}
static int static int
run_rule(Rule *r, char *s) run_rule(Rule *r, char *s)
{ {
@ -162,7 +128,38 @@ run_rule(Rule *r, char *s)
a++, p++; a++, p++;
} }
c = *p; c = *p;
return spacer(c) ? write_rule(r, p) : 0; if(spacer(c)) {
char *b = r->b, *reg, *origin = dst_;
while((c = *b++))
if(c == '?' && (reg = regs[(int)*b]))
write_reg(*b++, reg);
else
*dst_++ = c;
if(dst_ == origin) {
while(*p == ' ') p++;
if(*p == ')' && *(dst_ - 1) == ' ') dst_--;
}
return write_rule(r, p, 0);
}
return 0;
}
static char *
parse_frag(char *s)
{
char c, *cap;
while((c = *s) && c == ' ') s++;
if(c != ')' && !(c == '<' && s[1] == '>')) {
cap = walk(s);
if(c == '(') {
s++;
while(s < cap - 1) *dict_++ = *s++;
s++;
} else
while(s < cap) *dict_++ = *s++;
}
*dict_++ = 0;
return s;
} }
static int static int
@ -178,7 +175,7 @@ rewrite(void)
r->a = dict_, s = parse_frag(s + 2); r->a = dict_, s = parse_frag(s + 2);
r->b = dict_, s = parse_frag(s); r->b = dict_, s = parse_frag(s);
while(*s == ' ') s++; while(*s == ' ') s++;
return commit_rule(r, s, 1); return write_rule(r, s, 1);
} }
if(c == '?' && s[1] == '(') { /* lambda */ if(c == '?' && s[1] == '(') { /* lambda */
cap = walk(s + 1); cap = walk(s + 1);