Abstracting rule parser

This commit is contained in:
Devine Lu Linvega 2024-04-10 11:13:18 -07:00
parent 417ac2f2ad
commit 9f7faa41b5
1 changed files with 13 additions and 13 deletions

View File

@ -131,7 +131,7 @@ commit_rule(Rule *r, char *s, int create)
} }
static char * static char *
parse_rule(char *s) parse_rule_frag(char *s)
{ {
char *ss = walk(s), *d = dict_; char *ss = walk(s), *d = dict_;
if(*s == '(') s++, ss--; if(*s == '(') s++, ss--;
@ -140,17 +140,14 @@ parse_rule(char *s)
return d; return d;
} }
static int static char *
add_rule(char *p) parse_rule(Rule *r, char *s){
{
char c; char c;
Rule *r = rules_++; while((c = *s) && c <= ' ') s++;
r->id = rules_ - rules - 1, p += 2; r->a = parse_rule_frag(s), s = walk(s);
while((c = *p) && c <= ' ') p++; while((c = *s) && c <= ' ') s++;
r->a = parse_rule(p), p = walk(p); r->b = parse_rule_frag(s), s = walk(s);
while((c = *p) && c <= ' ') p++; return s;
r->b = parse_rule(p), p = walk(p);
return commit_rule(r, p, 1);
} }
static int static int
@ -159,8 +156,11 @@ rewrite(void)
char c, last = 0, *p = direction ? bank_b : bank_a; char c, last = 0, *p = direction ? bank_b : bank_a;
while((c = *p) && c <= ' ') p++; while((c = *p) && c <= ' ') p++;
while((c = *p)) { while((c = *p)) {
if(p[0] == '<' && p[1] == '>') if(p[0] == '<' && p[1] == '>'){
return add_rule(p); Rule *r = rules_++;
r->id = rules_ - rules - 1, p += 2;
return commit_rule(r, parse_rule(r, p), 1);
}
if(spacer(last)) { if(spacer(last)) {
Rule *r; Rule *r;
for(r = rules; r < rules_; r++) { for(r = rules; r < rules_; r++) {