regx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <thread.h>
  5. #include <cursor.h>
  6. #include <mouse.h>
  7. #include <keyboard.h>
  8. #include <frame.h>
  9. #include <fcall.h>
  10. #include <plumb.h>
  11. #include "dat.h"
  12. #include "fns.h"
  13. Rangeset sel;
  14. Rune *lastregexp;
  15. /*
  16. * Machine Information
  17. */
  18. typedef struct Inst Inst;
  19. struct Inst
  20. {
  21. uint type; /* < 0x10000 ==> literal, otherwise action */
  22. union {
  23. int sid;
  24. int subid;
  25. int class;
  26. Inst *other;
  27. Inst *right;
  28. };
  29. union{
  30. Inst *left;
  31. Inst *next;
  32. };
  33. };
  34. #define NPROG 1024
  35. Inst program[NPROG];
  36. Inst *progp;
  37. Inst *startinst; /* First inst. of program; might not be program[0] */
  38. Inst *bstartinst; /* same for backwards machine */
  39. Channel *rechan; /* chan(Inst*) */
  40. typedef struct Ilist Ilist;
  41. struct Ilist
  42. {
  43. Inst *inst; /* Instruction of the thread */
  44. Rangeset se;
  45. uint startp; /* first char of match */
  46. };
  47. #define NLIST 128
  48. Ilist *tl, *nl; /* This list, next list */
  49. Ilist list[2][NLIST];
  50. static Rangeset sempty;
  51. /*
  52. * Actions and Tokens
  53. *
  54. * 0x100xx are operators, value == precedence
  55. * 0x200xx are tokens, i.e. operands for operators
  56. */
  57. #define OPERATOR 0x10000 /* Bitmask of all operators */
  58. #define START 0x10000 /* Start, used for marker on stack */
  59. #define RBRA 0x10001 /* Right bracket, ) */
  60. #define LBRA 0x10002 /* Left bracket, ( */
  61. #define OR 0x10003 /* Alternation, | */
  62. #define CAT 0x10004 /* Concatentation, implicit operator */
  63. #define STAR 0x10005 /* Closure, * */
  64. #define PLUS 0x10006 /* a+ == aa* */
  65. #define QUEST 0x10007 /* a? == a|nothing, i.e. 0 or 1 a's */
  66. #define ANY 0x20000 /* Any character but newline, . */
  67. #define NOP 0x20001 /* No operation, internal use only */
  68. #define BOL 0x20002 /* Beginning of line, ^ */
  69. #define EOL 0x20003 /* End of line, $ */
  70. #define CCLASS 0x20004 /* Character class, [] */
  71. #define NCCLASS 0x20005 /* Negated character class, [^] */
  72. #define END 0x20077 /* Terminate: match found */
  73. #define ISATOR 0x10000
  74. #define ISAND 0x20000
  75. /*
  76. * Parser Information
  77. */
  78. typedef struct Node Node;
  79. struct Node
  80. {
  81. Inst *first;
  82. Inst *last;
  83. };
  84. #define NSTACK 20
  85. Node andstack[NSTACK];
  86. Node *andp;
  87. int atorstack[NSTACK];
  88. int *atorp;
  89. int lastwasand; /* Last token was operand */
  90. int cursubid;
  91. int subidstack[NSTACK];
  92. int *subidp;
  93. int backwards;
  94. int nbra;
  95. Rune *exprp; /* pointer to next character in source expression */
  96. #define DCLASS 10 /* allocation increment */
  97. int nclass; /* number active */
  98. int Nclass; /* high water mark */
  99. Rune **class;
  100. int negateclass;
  101. void addinst(Ilist *l, Inst *inst, Rangeset *sep);
  102. void newmatch(Rangeset*);
  103. void bnewmatch(Rangeset*);
  104. void pushand(Inst*, Inst*);
  105. void pushator(int);
  106. Node *popand(int);
  107. int popator(void);
  108. void startlex(Rune*);
  109. int lex(void);
  110. void operator(int);
  111. void operand(int);
  112. void evaluntil(int);
  113. void optimize(Inst*);
  114. void bldcclass(void);
  115. void
  116. rxinit(void)
  117. {
  118. rechan = chancreate(sizeof(Inst*), 0);
  119. lastregexp = runemalloc(1);
  120. }
  121. void
  122. regerror(char *e)
  123. {
  124. lastregexp[0] = 0;
  125. warning(nil, "regexp: %s\n", e);
  126. sendp(rechan, nil);
  127. threadexits(nil);
  128. }
  129. Inst *
  130. newinst(int t)
  131. {
  132. if(progp >= &program[NPROG])
  133. regerror("expression too long");
  134. progp->type = t;
  135. progp->left = nil;
  136. progp->right = nil;
  137. return progp++;
  138. }
  139. void
  140. realcompile(void *arg)
  141. {
  142. int token;
  143. Rune *s;
  144. threadsetname("regcomp");
  145. s = arg;
  146. startlex(s);
  147. atorp = atorstack;
  148. andp = andstack;
  149. subidp = subidstack;
  150. cursubid = 0;
  151. lastwasand = FALSE;
  152. /* Start with a low priority operator to prime parser */
  153. pushator(START-1);
  154. while((token=lex()) != END){
  155. if((token&ISATOR) == OPERATOR)
  156. operator(token);
  157. else
  158. operand(token);
  159. }
  160. /* Close with a low priority operator */
  161. evaluntil(START);
  162. /* Force END */
  163. operand(END);
  164. evaluntil(START);
  165. if(nbra)
  166. regerror("unmatched `('");
  167. --andp; /* points to first and only operand */
  168. sendp(rechan, andp->first);
  169. threadexits(nil);
  170. }
  171. /* r is null terminated */
  172. int
  173. rxcompile(Rune *r)
  174. {
  175. int i, nr;
  176. Inst *oprogp;
  177. nr = runestrlen(r)+1;
  178. if(runeeq(lastregexp, runestrlen(lastregexp)+1, r, nr)==TRUE)
  179. return TRUE;
  180. lastregexp[0] = 0;
  181. for(i=0; i<nclass; i++)
  182. free(class[i]);
  183. nclass = 0;
  184. progp = program;
  185. backwards = FALSE;
  186. bstartinst = nil;
  187. threadcreate(realcompile, r, STACK);
  188. startinst = recvp(rechan);
  189. if(startinst == nil)
  190. return FALSE;
  191. optimize(program);
  192. oprogp = progp;
  193. backwards = TRUE;
  194. threadcreate(realcompile, r, STACK);
  195. bstartinst = recvp(rechan);
  196. if(bstartinst == nil)
  197. return FALSE;
  198. optimize(oprogp);
  199. lastregexp = runerealloc(lastregexp, nr);
  200. runemove(lastregexp, r, nr);
  201. return TRUE;
  202. }
  203. void
  204. operand(int t)
  205. {
  206. Inst *i;
  207. if(lastwasand)
  208. operator(CAT); /* catenate is implicit */
  209. i = newinst(t);
  210. if(t == CCLASS){
  211. if(negateclass)
  212. i->type = NCCLASS; /* UGH */
  213. i->class = nclass-1; /* UGH */
  214. }
  215. pushand(i, i);
  216. lastwasand = TRUE;
  217. }
  218. void
  219. operator(int t)
  220. {
  221. if(t==RBRA && --nbra<0)
  222. regerror("unmatched `)'");
  223. if(t==LBRA){
  224. cursubid++; /* silently ignored */
  225. nbra++;
  226. if(lastwasand)
  227. operator(CAT);
  228. }else
  229. evaluntil(t);
  230. if(t!=RBRA)
  231. pushator(t);
  232. lastwasand = FALSE;
  233. if(t==STAR || t==QUEST || t==PLUS || t==RBRA)
  234. lastwasand = TRUE; /* these look like operands */
  235. }
  236. void
  237. pushand(Inst *f, Inst *l)
  238. {
  239. if(andp >= &andstack[NSTACK])
  240. error("operand stack overflow");
  241. andp->first = f;
  242. andp->last = l;
  243. andp++;
  244. }
  245. void
  246. pushator(int t)
  247. {
  248. if(atorp >= &atorstack[NSTACK])
  249. error("operator stack overflow");
  250. *atorp++=t;
  251. if(cursubid >= NRange)
  252. *subidp++= -1;
  253. else
  254. *subidp++=cursubid;
  255. }
  256. Node *
  257. popand(int op)
  258. {
  259. char buf[64];
  260. if(andp <= &andstack[0])
  261. if(op){
  262. sprint(buf, "missing operand for %c", op);
  263. regerror(buf);
  264. }else
  265. regerror("malformed regexp");
  266. return --andp;
  267. }
  268. int
  269. popator()
  270. {
  271. if(atorp <= &atorstack[0])
  272. error("operator stack underflow");
  273. --subidp;
  274. return *--atorp;
  275. }
  276. void
  277. evaluntil(int pri)
  278. {
  279. Node *op1, *op2, *t;
  280. Inst *inst1, *inst2;
  281. while(pri==RBRA || atorp[-1]>=pri){
  282. switch(popator()){
  283. case LBRA:
  284. op1 = popand('(');
  285. inst2 = newinst(RBRA);
  286. inst2->subid = *subidp;
  287. op1->last->next = inst2;
  288. inst1 = newinst(LBRA);
  289. inst1->subid = *subidp;
  290. inst1->next = op1->first;
  291. pushand(inst1, inst2);
  292. return; /* must have been RBRA */
  293. default:
  294. error("unknown regexp operator");
  295. break;
  296. case OR:
  297. op2 = popand('|');
  298. op1 = popand('|');
  299. inst2 = newinst(NOP);
  300. op2->last->next = inst2;
  301. op1->last->next = inst2;
  302. inst1 = newinst(OR);
  303. inst1->right = op1->first;
  304. inst1->left = op2->first;
  305. pushand(inst1, inst2);
  306. break;
  307. case CAT:
  308. op2 = popand(0);
  309. op1 = popand(0);
  310. if(backwards && op2->first->type!=END){
  311. t = op1;
  312. op1 = op2;
  313. op2 = t;
  314. }
  315. op1->last->next = op2->first;
  316. pushand(op1->first, op2->last);
  317. break;
  318. case STAR:
  319. op2 = popand('*');
  320. inst1 = newinst(OR);
  321. op2->last->next = inst1;
  322. inst1->right = op2->first;
  323. pushand(inst1, inst1);
  324. break;
  325. case PLUS:
  326. op2 = popand('+');
  327. inst1 = newinst(OR);
  328. op2->last->next = inst1;
  329. inst1->right = op2->first;
  330. pushand(op2->first, inst1);
  331. break;
  332. case QUEST:
  333. op2 = popand('?');
  334. inst1 = newinst(OR);
  335. inst2 = newinst(NOP);
  336. inst1->left = inst2;
  337. inst1->right = op2->first;
  338. op2->last->next = inst2;
  339. pushand(inst1, inst2);
  340. break;
  341. }
  342. }
  343. }
  344. void
  345. optimize(Inst *start)
  346. {
  347. Inst *inst, *target;
  348. for(inst=start; inst->type!=END; inst++){
  349. target = inst->next;
  350. while(target->type == NOP)
  351. target = target->next;
  352. inst->next = target;
  353. }
  354. }
  355. void
  356. startlex(Rune *s)
  357. {
  358. exprp = s;
  359. nbra = 0;
  360. }
  361. int
  362. lex(void){
  363. int c;
  364. c = *exprp++;
  365. switch(c){
  366. case '\\':
  367. if(*exprp)
  368. if((c= *exprp++)=='n')
  369. c='\n';
  370. break;
  371. case 0:
  372. c = END;
  373. --exprp; /* In case we come here again */
  374. break;
  375. case '*':
  376. c = STAR;
  377. break;
  378. case '?':
  379. c = QUEST;
  380. break;
  381. case '+':
  382. c = PLUS;
  383. break;
  384. case '|':
  385. c = OR;
  386. break;
  387. case '.':
  388. c = ANY;
  389. break;
  390. case '(':
  391. c = LBRA;
  392. break;
  393. case ')':
  394. c = RBRA;
  395. break;
  396. case '^':
  397. c = BOL;
  398. break;
  399. case '$':
  400. c = EOL;
  401. break;
  402. case '[':
  403. c = CCLASS;
  404. bldcclass();
  405. break;
  406. }
  407. return c;
  408. }
  409. int
  410. nextrec(void)
  411. {
  412. if(exprp[0]==0 || (exprp[0]=='\\' && exprp[1]==0))
  413. regerror("malformed `[]'");
  414. if(exprp[0] == '\\'){
  415. exprp++;
  416. if(*exprp=='n'){
  417. exprp++;
  418. return '\n';
  419. }
  420. return *exprp++|0x10000;
  421. }
  422. return *exprp++;
  423. }
  424. void
  425. bldcclass(void)
  426. {
  427. int c1, c2, n, na;
  428. Rune *classp;
  429. classp = runemalloc(DCLASS);
  430. n = 0;
  431. na = DCLASS;
  432. /* we have already seen the '[' */
  433. if(*exprp == '^'){
  434. classp[n++] = '\n'; /* don't match newline in negate case */
  435. negateclass = TRUE;
  436. exprp++;
  437. }else
  438. negateclass = FALSE;
  439. while((c1 = nextrec()) != ']'){
  440. if(c1 == '-'){
  441. Error:
  442. free(classp);
  443. regerror("malformed `[]'");
  444. }
  445. if(n+4 >= na){ /* 3 runes plus NUL */
  446. na += DCLASS;
  447. classp = runerealloc(classp, na);
  448. }
  449. if(*exprp == '-'){
  450. exprp++; /* eat '-' */
  451. if((c2 = nextrec()) == ']')
  452. goto Error;
  453. classp[n+0] = 0xFFFF;
  454. classp[n+1] = c1;
  455. classp[n+2] = c2;
  456. n += 3;
  457. }else
  458. classp[n++] = c1;
  459. }
  460. classp[n] = 0;
  461. if(nclass == Nclass){
  462. Nclass += DCLASS;
  463. class = realloc(class, Nclass*sizeof(Rune*));
  464. }
  465. class[nclass++] = classp;
  466. }
  467. int
  468. classmatch(int classno, int c, int negate)
  469. {
  470. Rune *p;
  471. p = class[classno];
  472. while(*p){
  473. if(*p == 0xFFFF){
  474. if(p[1]<=c && c<=p[2])
  475. return !negate;
  476. p += 3;
  477. }else if(*p++ == c)
  478. return !negate;
  479. }
  480. return negate;
  481. }
  482. /*
  483. * Note optimization in addinst:
  484. * *l must be pending when addinst called; if *l has been looked
  485. * at already, the optimization is a bug.
  486. */
  487. void
  488. addinst(Ilist *l, Inst *inst, Rangeset *sep)
  489. {
  490. Ilist *p;
  491. for(p = l; p->inst; p++){
  492. if(p->inst==inst){
  493. if((sep)->r[0].q0 < p->se.r[0].q0)
  494. p->se= *sep; /* this would be bug */
  495. return; /* It's already there */
  496. }
  497. }
  498. p->inst = inst;
  499. p->se= *sep;
  500. (p+1)->inst = nil;
  501. }
  502. int
  503. rxnull(void)
  504. {
  505. return startinst==nil || bstartinst==nil;
  506. }
  507. /* either t!=nil or r!=nil, and we match the string in the appropriate place */
  508. int
  509. rxexecute(Text *t, Rune *r, uint startp, uint eof, Rangeset *rp)
  510. {
  511. int flag;
  512. Inst *inst;
  513. Ilist *tlp;
  514. uint p;
  515. int nnl, ntl;
  516. int nc, c;
  517. int wrapped;
  518. int startchar;
  519. flag = 0;
  520. p = startp;
  521. startchar = 0;
  522. wrapped = 0;
  523. nnl = 0;
  524. if(startinst->type<OPERATOR)
  525. startchar = startinst->type;
  526. list[0][0].inst = list[1][0].inst = nil;
  527. sel.r[0].q0 = -1;
  528. if(t != nil)
  529. nc = t->file->nc;
  530. else
  531. nc = runestrlen(r);
  532. /* Execute machine once for each character */
  533. for(;;p++){
  534. doloop:
  535. if(p>=eof || p>=nc){
  536. switch(wrapped++){
  537. case 0: /* let loop run one more click */
  538. case 2:
  539. break;
  540. case 1: /* expired; wrap to beginning */
  541. if(sel.r[0].q0>=0 || eof!=Infinity)
  542. goto Return;
  543. list[0][0].inst = list[1][0].inst = nil;
  544. p = 0;
  545. goto doloop;
  546. default:
  547. goto Return;
  548. }
  549. c = 0;
  550. }else{
  551. if(((wrapped && p>=startp) || sel.r[0].q0>0) && nnl==0)
  552. break;
  553. if(t != nil)
  554. c = textreadc(t, p);
  555. else
  556. c = r[p];
  557. }
  558. /* fast check for first char */
  559. if(startchar && nnl==0 && c!=startchar)
  560. continue;
  561. tl = list[flag];
  562. nl = list[flag^=1];
  563. nl->inst = nil;
  564. ntl = nnl;
  565. nnl = 0;
  566. if(sel.r[0].q0<0 && (!wrapped || p<startp || startp==eof)){
  567. /* Add first instruction to this list */
  568. if(++ntl >= NLIST){
  569. Overflow:
  570. warning(nil, "regexp list overflow\n");
  571. sel.r[0].q0 = -1;
  572. goto Return;
  573. }
  574. sempty.r[0].q0 = p;
  575. addinst(tl, startinst, &sempty);
  576. }
  577. /* Execute machine until this list is empty */
  578. for(tlp = tl; inst = tlp->inst; tlp++){ /* assignment = */
  579. Switchstmt:
  580. switch(inst->type){
  581. default: /* regular character */
  582. if(inst->type==c){
  583. Addinst:
  584. if(++nnl >= NLIST)
  585. goto Overflow;
  586. addinst(nl, inst->next, &tlp->se);
  587. }
  588. break;
  589. case LBRA:
  590. if(inst->subid>=0)
  591. tlp->se.r[inst->subid].q0 = p;
  592. inst = inst->next;
  593. goto Switchstmt;
  594. case RBRA:
  595. if(inst->subid>=0)
  596. tlp->se.r[inst->subid].q1 = p;
  597. inst = inst->next;
  598. goto Switchstmt;
  599. case ANY:
  600. if(c!='\n')
  601. goto Addinst;
  602. break;
  603. case BOL:
  604. if(p==0 || (t!=nil && textreadc(t, p-1)=='\n') || (r!=nil && r[p-1]=='\n')){
  605. Step:
  606. inst = inst->next;
  607. goto Switchstmt;
  608. }
  609. break;
  610. case EOL:
  611. if(c == '\n')
  612. goto Step;
  613. break;
  614. case CCLASS:
  615. if(c>=0 && classmatch(inst->class, c, 0))
  616. goto Addinst;
  617. break;
  618. case NCCLASS:
  619. if(c>=0 && classmatch(inst->class, c, 1))
  620. goto Addinst;
  621. break;
  622. case OR:
  623. /* evaluate right choice later */
  624. if(++ntl >= NLIST)
  625. goto Overflow;
  626. addinst(tlp, inst->right, &tlp->se);
  627. /* efficiency: advance and re-evaluate */
  628. inst = inst->left;
  629. goto Switchstmt;
  630. case END: /* Match! */
  631. tlp->se.r[0].q1 = p;
  632. newmatch(&tlp->se);
  633. break;
  634. }
  635. }
  636. }
  637. Return:
  638. *rp = sel;
  639. return sel.r[0].q0 >= 0;
  640. }
  641. void
  642. newmatch(Rangeset *sp)
  643. {
  644. if(sel.r[0].q0<0 || sp->r[0].q0<sel.r[0].q0 ||
  645. (sp->r[0].q0==sel.r[0].q0 && sp->r[0].q1>sel.r[0].q1))
  646. sel = *sp;
  647. }
  648. int
  649. rxbexecute(Text *t, uint startp, Rangeset *rp)
  650. {
  651. int flag;
  652. Inst *inst;
  653. Ilist *tlp;
  654. int p;
  655. int nnl, ntl;
  656. int c;
  657. int wrapped;
  658. int startchar;
  659. flag = 0;
  660. nnl = 0;
  661. wrapped = 0;
  662. p = startp;
  663. startchar = 0;
  664. if(bstartinst->type<OPERATOR)
  665. startchar = bstartinst->type;
  666. list[0][0].inst = list[1][0].inst = nil;
  667. sel.r[0].q0= -1;
  668. /* Execute machine once for each character, including terminal NUL */
  669. for(;;--p){
  670. doloop:
  671. if(p <= 0){
  672. switch(wrapped++){
  673. case 0: /* let loop run one more click */
  674. case 2:
  675. break;
  676. case 1: /* expired; wrap to end */
  677. if(sel.r[0].q0>=0)
  678. goto Return;
  679. list[0][0].inst = list[1][0].inst = nil;
  680. p = t->file->nc;
  681. goto doloop;
  682. case 3:
  683. default:
  684. goto Return;
  685. }
  686. c = 0;
  687. }else{
  688. if(((wrapped && p<=startp) || sel.r[0].q0>0) && nnl==0)
  689. break;
  690. c = textreadc(t, p-1);
  691. }
  692. /* fast check for first char */
  693. if(startchar && nnl==0 && c!=startchar)
  694. continue;
  695. tl = list[flag];
  696. nl = list[flag^=1];
  697. nl->inst = nil;
  698. ntl = nnl;
  699. nnl = 0;
  700. if(sel.r[0].q0<0 && (!wrapped || p>startp)){
  701. /* Add first instruction to this list */
  702. if(++ntl >= NLIST){
  703. Overflow:
  704. warning(nil, "regexp list overflow\n");
  705. sel.r[0].q0 = -1;
  706. goto Return;
  707. }
  708. /* the minus is so the optimizations in addinst work */
  709. sempty.r[0].q0 = -p;
  710. addinst(tl, bstartinst, &sempty);
  711. }
  712. /* Execute machine until this list is empty */
  713. for(tlp = tl; inst = tlp->inst; tlp++){ /* assignment = */
  714. Switchstmt:
  715. switch(inst->type){
  716. default: /* regular character */
  717. if(inst->type == c){
  718. Addinst:
  719. if(++nnl >= NLIST)
  720. goto Overflow;
  721. addinst(nl, inst->next, &tlp->se);
  722. }
  723. break;
  724. case LBRA:
  725. if(inst->subid>=0)
  726. tlp->se.r[inst->subid].q0 = p;
  727. inst = inst->next;
  728. goto Switchstmt;
  729. case RBRA:
  730. if(inst->subid >= 0)
  731. tlp->se.r[inst->subid].q1 = p;
  732. inst = inst->next;
  733. goto Switchstmt;
  734. case ANY:
  735. if(c != '\n')
  736. goto Addinst;
  737. break;
  738. case BOL:
  739. if(c=='\n' || p==0){
  740. Step:
  741. inst = inst->next;
  742. goto Switchstmt;
  743. }
  744. break;
  745. case EOL:
  746. if(p<t->file->nc && textreadc(t, p)=='\n')
  747. goto Step;
  748. break;
  749. case CCLASS:
  750. if(c>0 && classmatch(inst->class, c, 0))
  751. goto Addinst;
  752. break;
  753. case NCCLASS:
  754. if(c>0 && classmatch(inst->class, c, 1))
  755. goto Addinst;
  756. break;
  757. case OR:
  758. /* evaluate right choice later */
  759. if(++ntl >= NLIST)
  760. goto Overflow;
  761. addinst(tlp, inst->right, &tlp->se);
  762. /* efficiency: advance and re-evaluate */
  763. inst = inst->left;
  764. goto Switchstmt;
  765. case END: /* Match! */
  766. tlp->se.r[0].q0 = -tlp->se.r[0].q0; /* minus sign */
  767. tlp->se.r[0].q1 = p;
  768. bnewmatch(&tlp->se);
  769. break;
  770. }
  771. }
  772. }
  773. Return:
  774. *rp = sel;
  775. return sel.r[0].q0 >= 0;
  776. }
  777. void
  778. bnewmatch(Rangeset *sp)
  779. {
  780. int i;
  781. 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))
  782. for(i = 0; i<NRange; i++){ /* note the reversal; q0<=q1 */
  783. sel.r[i].q0 = sp->r[i].q1;
  784. sel.r[i].q1 = sp->r[i].q0;
  785. }
  786. }