From 7a27c2ca5bf890b27c8b9215167457b3bb622f96 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Mon, 22 Apr 2024 17:54:17 -0700 Subject: [PATCH] Housekeeping --- src/modal.c | 73 +++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/src/modal.c b/src/modal.c index 4661f8f..21f8f6e 100644 --- a/src/modal.c +++ b/src/modal.c @@ -31,7 +31,7 @@ walk(char *s) } static void -put_reg(char r, char *reg) +write_reg(char r, char *reg) { char c, *cap = walk(reg); if(r == '*') { @@ -78,7 +78,7 @@ put_reg(char r, char *reg) } static int -commit_rule(Rule *r, char *s, int create) +write_rule(Rule *r, char *s, int create) { while((*dst_++ = *s++)) ; @@ -96,40 +96,6 @@ commit_rule(Rule *r, char *s, int create) 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 run_rule(Rule *r, char *s) { @@ -162,7 +128,38 @@ run_rule(Rule *r, char *s) a++, 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 @@ -178,7 +175,7 @@ rewrite(void) r->a = dict_, s = parse_frag(s + 2); r->b = dict_, s = parse_frag(s); while(*s == ' ') s++; - return commit_rule(r, s, 1); + return write_rule(r, s, 1); } if(c == '?' && s[1] == '(') { /* lambda */ cap = walk(s + 1);