20 typedef struct Inst Inst;
23 uint type; /* < 0x10000 ==> literal, otherwise action */
40 Inst *startinst; /* First inst. of program; might not be program[0] */
41 Inst *bstartinst; /* same for backwards machine */
42 Channel *rechan; /* chan(Inst*) */
44 typedef struct Ilist Ilist;
47 Inst *inst; /* Instruction of the thread */
49 uint startp; /* first char of match */
54 Ilist *tl, *nl; /* This list, next list */
56 static Rangeset sempty;
61 * 0x100xx are operators, value == precedence
62 * 0x200xx are tokens, i.e. operands for operators
64 #define OPERATOR 0x10000 /* Bitmask of all operators */
65 #define START 0x10000 /* Start, used for marker on stack */
66 #define RBRA 0x10001 /* Right bracket, ) */
67 #define LBRA 0x10002 /* Left bracket, ( */
68 #define OR 0x10003 /* Alternation, | */
69 #define CAT 0x10004 /* Concatentation, implicit operator */
70 #define STAR 0x10005 /* Closure, * */
71 #define PLUS 0x10006 /* a+ == aa* */
72 #define QUEST 0x10007 /* a? == a|nothing, i.e. 0 or 1 a's */
73 #define ANY 0x20000 /* Any character but newline, . */
74 #define NOP 0x20001 /* No operation, internal use only */
75 #define BOL 0x20002 /* Beginning of line, ^ */
76 #define EOL 0x20003 /* End of line, $ */
77 #define CCLASS 0x20004 /* Character class, [] */
78 #define NCCLASS 0x20005 /* Negated character class, [^] */
79 #define END 0x20077 /* Terminate: match found */
81 #define ISATOR 0x10000
87 typedef struct Node Node;
95 Node andstack[NSTACK];
97 int atorstack[NSTACK];
99 int lastwasand; /* Last token was operand */
101 int subidstack[NSTACK];
105 Rune *exprp; /* pointer to next character in source expression */
106 #define DCLASS 10 /* allocation increment */
107 int nclass; /* number active */
108 int Nclass; /* high water mark */
112 void addinst(Ilist *l, Inst *inst, Rangeset *sep);
113 void newmatch(Rangeset*);
114 void bnewmatch(Rangeset*);
115 void pushand(Inst*, Inst*);
119 void startlex(Rune*);
124 void optimize(Inst*);
125 void bldcclass(void);
130 rechan = chancreate(sizeof(Inst*), 0);
131 lastregexp = runemalloc(1);
138 warning(nil, "regexp: %s\n", e);
146 if(progp >= &program[NPROG])
147 regerror("expression too long");
149 progp->u1.left = nil;
150 progp->u.right = nil;
155 realcompile(void *arg)
160 threadsetname("regcomp");
168 /* Start with a low priority operator to prime parser */
170 while((token=lex()) != END){
171 if((token&ISATOR) == OPERATOR)
176 /* Close with a low priority operator */
182 regerror("unmatched `('");
183 --andp; /* points to first and only operand */
184 sendp(rechan, andp->first);
188 /* r is null terminated */
195 nr = runestrlen(r)+1;
196 if(runeeq(lastregexp, runestrlen(lastregexp)+1, r, nr)==TRUE)
199 for(i=0; i<nclass; i++)
205 threadcreate(realcompile, r, STACK);
206 startinst = recvp(rechan);
212 threadcreate(realcompile, r, STACK);
213 bstartinst = recvp(rechan);
214 if(bstartinst == nil)
217 lastregexp = runerealloc(lastregexp, nr);
218 runemove(lastregexp, r, nr);
227 operator(CAT); /* catenate is implicit */
231 i->type = NCCLASS; /* UGH */
232 i->u.class = nclass-1; /* UGH */
241 if(t==RBRA && --nbra<0)
242 regerror("unmatched `)'");
244 cursubid++; /* silently ignored */
253 if(t==STAR || t==QUEST || t==PLUS || t==RBRA)
254 lastwasand = TRUE; /* these look like operands */
258 pushand(Inst *f, Inst *l)
260 if(andp >= &andstack[NSTACK])
261 error("operand stack overflow");
270 if(atorp >= &atorstack[NSTACK])
271 error("operator stack overflow");
273 if(cursubid >= NRange)
284 if(andp <= &andstack[0])
286 sprint(buf, "missing operand for %c", op);
289 regerror("malformed regexp");
296 if(atorp <= &atorstack[0])
297 error("operator stack underflow");
308 while(pri==RBRA || atorp[-1]>=pri){
312 inst2 = newinst(RBRA);
313 inst2->u.subid = *subidp;
314 op1->last->u1.next = inst2;
315 inst1 = newinst(LBRA);
316 inst1->u.subid = *subidp;
317 inst1->u1.next = op1->first;
318 pushand(inst1, inst2);
319 return; /* must have been RBRA */
321 error("unknown regexp operator");
326 inst2 = newinst(NOP);
327 op2->last->u1.next = inst2;
328 op1->last->u1.next = inst2;
330 inst1->u.right = op1->first;
331 inst1->u1.left = op2->first;
332 pushand(inst1, inst2);
337 if(backwards && op2->first->type!=END){
342 op1->last->u1.next = op2->first;
343 pushand(op1->first, op2->last);
348 op2->last->u1.next = inst1;
349 inst1->u.right = op2->first;
350 pushand(inst1, inst1);
355 op2->last->u1.next = inst1;
356 inst1->u.right = op2->first;
357 pushand(op2->first, inst1);
362 inst2 = newinst(NOP);
363 inst1->u1.left = inst2;
364 inst1->u.right = op2->first;
365 op2->last->u1.next = inst2;
366 pushand(inst1, inst2);
374 optimize(Inst *start)
378 for(inst=start; inst->type!=END; inst++){
379 target = inst->u1.next;
380 while(target->type == NOP)
381 target = target->u1.next;
382 inst->u1.next = target;
402 if((c= *exprp++)=='n')
407 --exprp; /* In case we come here again */
447 if(exprp[0]==0 || (exprp[0]=='\\' && exprp[1]==0))
448 regerror("malformed `[]'");
449 if(exprp[0] == '\\'){
455 return *exprp++|0x10000;
466 classp = runemalloc(DCLASS);
469 /* we have already seen the '[' */
471 classp[n++] = '\n'; /* don't match newline in negate case */
476 while((c1 = nextrec()) != ']'){
480 regerror("malformed `[]'");
482 if(n+4 >= na){ /* 3 runes plus NUL */
484 classp = runerealloc(classp, na);
487 exprp++; /* eat '-' */
488 if((c2 = nextrec()) == ']')
490 classp[n+0] = 0xFFFF;
498 if(nclass == Nclass){
500 class = realloc(class, Nclass*sizeof(Rune*));
502 class[nclass++] = classp;
506 classmatch(int classno, int c, int negate)
513 if(p[1]<=c && c<=p[2])
523 * Note optimization in addinst:
524 * *l must be pending when addinst called; if *l has been looked
525 * at already, the optimization is a bug.
528 addinst(Ilist *l, Inst *inst, Rangeset *sep)
532 for(p = l; p->inst; p++){
534 if((sep)->r[0].q0 < p->se.r[0].q0)
535 p->se= *sep; /* this would be bug */
536 return; /* It's already there */
547 return startinst==nil || bstartinst==nil;
550 /* either t!=nil or r!=nil, and we match the string in the appropriate place */
552 rxexecute(Text *t, Rune *r, uint startp, uint eof, Rangeset *rp)
568 if(startinst->type<OPERATOR)
569 startchar = startinst->type;
570 list[0][0].inst = list[1][0].inst = nil;
576 /* Execute machine once for each character */
581 case 0: /* let loop run one more click */
584 case 1: /* expired; wrap to beginning */
585 if(sel.r[0].q0>=0 || eof!=Infinity)
587 list[0][0].inst = list[1][0].inst = nil;
595 if(((wrapped && p>=startp) || sel.r[0].q0>0) && nnl==0)
602 /* fast check for first char */
603 if(startchar && nnl==0 && c!=startchar)
610 if(sel.r[0].q0<0 && (!wrapped || p<startp || startp==eof)){
611 /* Add first instruction to this list */
614 warning(nil, "regexp list overflow\n");
619 addinst(tl, startinst, &sempty);
621 /* Execute machine until this list is empty */
622 for(tlp = tl; inst = tlp->inst; tlp++){ /* assignment = */
625 default: /* regular character */
630 addinst(nl, inst->u1.next, &tlp->se);
635 tlp->se.r[inst->u.subid].q0 = p;
636 inst = inst->u1.next;
640 tlp->se.r[inst->u.subid].q1 = p;
641 inst = inst->u1.next;
648 if(p==0 || (t!=nil && textreadc(t, p-1)=='\n') || (r!=nil && r[p-1]=='\n')){
650 inst = inst->u1.next;
659 if(c>=0 && classmatch(inst->u.class, c, 0))
663 if(c>=0 && classmatch(inst->u.class, c, 1))
667 /* evaluate right choice later */
670 addinst(tlp, inst->u.right, &tlp->se);
671 /* efficiency: advance and re-evaluate */
672 inst = inst->u1.left;
674 case END: /* Match! */
683 return sel.r[0].q0 >= 0;
687 newmatch(Rangeset *sp)
689 if(sel.r[0].q0<0 || sp->r[0].q0<sel.r[0].q0 ||
690 (sp->r[0].q0==sel.r[0].q0 && sp->r[0].q1>sel.r[0].q1))
695 rxbexecute(Text *t, uint startp, Rangeset *rp)
711 if(bstartinst->type<OPERATOR)
712 startchar = bstartinst->type;
713 list[0][0].inst = list[1][0].inst = nil;
715 /* Execute machine once for each character, including terminal NUL */
720 case 0: /* let loop run one more click */
723 case 1: /* expired; wrap to end */
726 list[0][0].inst = list[1][0].inst = nil;
735 if(((wrapped && p<=startp) || sel.r[0].q0>0) && nnl==0)
737 c = textreadc(t, p-1);
739 /* fast check for first char */
740 if(startchar && nnl==0 && c!=startchar)
747 if(sel.r[0].q0<0 && (!wrapped || p>startp)){
748 /* Add first instruction to this list */
751 warning(nil, "regexp list overflow\n");
755 /* the minus is so the optimizations in addinst work */
757 addinst(tl, bstartinst, &sempty);
759 /* Execute machine until this list is empty */
760 for(tlp = tl; inst = tlp->inst; tlp++){ /* assignment = */
763 default: /* regular character */
768 addinst(nl, inst->u1.next, &tlp->se);
773 tlp->se.r[inst->u.subid].q0 = p;
774 inst = inst->u1.next;
777 if(inst->u.subid >= 0)
778 tlp->se.r[inst->u.subid].q1 = p;
779 inst = inst->u1.next;
788 inst = inst->u1.next;
793 if(p<t->file->b.nc && textreadc(t, p)=='\n')
797 if(c>0 && classmatch(inst->u.class, c, 0))
801 if(c>0 && classmatch(inst->u.class, c, 1))
805 /* evaluate right choice later */
808 addinst(tlp, inst->u.right, &tlp->se);
809 /* efficiency: advance and re-evaluate */
810 inst = inst->u1.left;
812 case END: /* Match! */
813 tlp->se.r[0].q0 = -tlp->se.r[0].q0; /* minus sign */
822 return sel.r[0].q0 >= 0;
826 bnewmatch(Rangeset *sp)
830 if(sel.r[0].q0<0 || sp->r[0].q0>sel.r[0].q1 || (sp->r[0].q0==sel.r[0].q1 && sp->r[0].q1<sel.r[0].q0))
831 for(i = 0; i<NRange; i++){ /* note the reversal; q0<=q1 */
832 sel.r[i].q0 = sp->r[i].q1;
833 sel.r[i].q1 = sp->r[i].q0;