Lines Matching refs:exp

71 static SQInteger sqstd_rex_list(SQRex *exp);
73 static SQInteger sqstd_rex_newnode(SQRex *exp, SQRexNodeType type) in sqstd_rex_newnode() argument
79 n.right = exp->_nsubexpr++; in sqstd_rex_newnode()
80 if(exp->_nallocated < (exp->_nsize + 1)) { in sqstd_rex_newnode()
81 SQInteger oldsize = exp->_nallocated; in sqstd_rex_newnode()
82 exp->_nallocated *= 2; in sqstd_rex_newnode()
83exp->_nodes = (SQRexNode *)sq_realloc(exp->_nodes, oldsize * sizeof(SQRexNode) ,exp->_nallocated *… in sqstd_rex_newnode()
85 exp->_nodes[exp->_nsize++] = n; in sqstd_rex_newnode()
86 SQInteger newid = exp->_nsize - 1; in sqstd_rex_newnode()
90 static void sqstd_rex_error(SQRex *exp,const SQChar *error) in sqstd_rex_error() argument
92 if(exp->_error) *exp->_error = error; in sqstd_rex_error()
93 longjmp(*((jmp_buf*)exp->_jmpbuf),-1); in sqstd_rex_error()
96 static void sqstd_rex_expect(SQRex *exp, SQInteger n){ in sqstd_rex_expect() argument
97 if((*exp->_p) != n) in sqstd_rex_expect()
98 sqstd_rex_error(exp, _SC("expected paren")); in sqstd_rex_expect()
99 exp->_p++; in sqstd_rex_expect()
102 static SQChar sqstd_rex_escapechar(SQRex *exp) in sqstd_rex_escapechar() argument
104 if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR){ in sqstd_rex_escapechar()
105 exp->_p++; in sqstd_rex_escapechar()
106 switch(*exp->_p) { in sqstd_rex_escapechar()
107 case 'v': exp->_p++; return '\v'; in sqstd_rex_escapechar()
108 case 'n': exp->_p++; return '\n'; in sqstd_rex_escapechar()
109 case 't': exp->_p++; return '\t'; in sqstd_rex_escapechar()
110 case 'r': exp->_p++; return '\r'; in sqstd_rex_escapechar()
111 case 'f': exp->_p++; return '\f'; in sqstd_rex_escapechar()
112 default: return (*exp->_p++); in sqstd_rex_escapechar()
114 } else if(!scisprint(*exp->_p)) sqstd_rex_error(exp,_SC("letter expected")); in sqstd_rex_escapechar()
115 return (*exp->_p++); in sqstd_rex_escapechar()
118 static SQInteger sqstd_rex_charclass(SQRex *exp,SQInteger classid) in sqstd_rex_charclass() argument
120 SQInteger n = sqstd_rex_newnode(exp,OP_CCLASS); in sqstd_rex_charclass()
121 exp->_nodes[n].left = classid; in sqstd_rex_charclass()
125 static SQInteger sqstd_rex_charnode(SQRex *exp,SQBool isclass) in sqstd_rex_charnode() argument
128 if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR) { in sqstd_rex_charnode()
129 exp->_p++; in sqstd_rex_charnode()
130 switch(*exp->_p) { in sqstd_rex_charnode()
131 case 'n': exp->_p++; return sqstd_rex_newnode(exp,'\n'); in sqstd_rex_charnode()
132 case 't': exp->_p++; return sqstd_rex_newnode(exp,'\t'); in sqstd_rex_charnode()
133 case 'r': exp->_p++; return sqstd_rex_newnode(exp,'\r'); in sqstd_rex_charnode()
134 case 'f': exp->_p++; return sqstd_rex_newnode(exp,'\f'); in sqstd_rex_charnode()
135 case 'v': exp->_p++; return sqstd_rex_newnode(exp,'\v'); in sqstd_rex_charnode()
140 t = *exp->_p; exp->_p++; in sqstd_rex_charnode()
141 return sqstd_rex_charclass(exp,t); in sqstd_rex_charnode()
146 cb = *++exp->_p; //skip 'm' in sqstd_rex_charnode()
147 ce = *++exp->_p; in sqstd_rex_charnode()
148 exp->_p++; //points to the next char to be parsed in sqstd_rex_charnode()
149 if ((!cb) || (!ce)) sqstd_rex_error(exp,_SC("balanced chars expected")); in sqstd_rex_charnode()
150 if ( cb == ce ) sqstd_rex_error(exp,_SC("open/close char can't be the same")); in sqstd_rex_charnode()
151 SQInteger node = sqstd_rex_newnode(exp,OP_MB); in sqstd_rex_charnode()
152 exp->_nodes[node].left = cb; in sqstd_rex_charnode()
153 exp->_nodes[node].right = ce; in sqstd_rex_charnode()
157 sqstd_rex_error(exp,_SC("letter expected for argument of escape sequence")); in sqstd_rex_charnode()
162 SQInteger node = sqstd_rex_newnode(exp,OP_WB); in sqstd_rex_charnode()
163 exp->_nodes[node].left = *exp->_p; in sqstd_rex_charnode()
164 exp->_p++; in sqstd_rex_charnode()
168 t = *exp->_p; exp->_p++; in sqstd_rex_charnode()
169 return sqstd_rex_newnode(exp,t); in sqstd_rex_charnode()
172 else if(!scisprint(*exp->_p)) { in sqstd_rex_charnode()
174 sqstd_rex_error(exp,_SC("letter expected")); in sqstd_rex_charnode()
176 t = *exp->_p; exp->_p++; in sqstd_rex_charnode()
177 return sqstd_rex_newnode(exp,t); in sqstd_rex_charnode()
179 static SQInteger sqstd_rex_class(SQRex *exp) in sqstd_rex_class() argument
183 if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING){ in sqstd_rex_class()
184 ret = sqstd_rex_newnode(exp,OP_NCLASS); in sqstd_rex_class()
185 exp->_p++; in sqstd_rex_class()
186 }else ret = sqstd_rex_newnode(exp,OP_CLASS); in sqstd_rex_class()
188 if(*exp->_p == ']') sqstd_rex_error(exp,_SC("empty class")); in sqstd_rex_class()
190 while(*exp->_p != ']' && exp->_p != exp->_eol) { in sqstd_rex_class()
191 if(*exp->_p == '-' && first != -1){ in sqstd_rex_class()
193 if(*exp->_p++ == ']') sqstd_rex_error(exp,_SC("unfinished range")); in sqstd_rex_class()
194 r = sqstd_rex_newnode(exp,OP_RANGE); in sqstd_rex_class()
195 if(exp->_nodes[first].type>*exp->_p) sqstd_rex_error(exp,_SC("invalid range")); in sqstd_rex_class()
196 …if(exp->_nodes[first].type == OP_CCLASS) sqstd_rex_error(exp,_SC("cannot use character classes in … in sqstd_rex_class()
197 exp->_nodes[r].left = exp->_nodes[first].type; in sqstd_rex_class()
198 SQInteger t = sqstd_rex_escapechar(exp); in sqstd_rex_class()
199 exp->_nodes[r].right = t; in sqstd_rex_class()
200 exp->_nodes[chain].next = r; in sqstd_rex_class()
207 exp->_nodes[chain].next = c; in sqstd_rex_class()
209 first = sqstd_rex_charnode(exp,SQTrue); in sqstd_rex_class()
212 first = sqstd_rex_charnode(exp,SQTrue); in sqstd_rex_class()
218 exp->_nodes[chain].next = c; in sqstd_rex_class()
221 exp->_nodes[ret].left = exp->_nodes[ret].next; in sqstd_rex_class()
222 exp->_nodes[ret].next = -1; in sqstd_rex_class()
226 static SQInteger sqstd_rex_parsenumber(SQRex *exp) in sqstd_rex_parsenumber() argument
228 SQInteger ret = *exp->_p-'0'; in sqstd_rex_parsenumber()
230 exp->_p++; in sqstd_rex_parsenumber()
231 while(isdigit(*exp->_p)) { in sqstd_rex_parsenumber()
232 ret = ret*10+(*exp->_p++-'0'); in sqstd_rex_parsenumber()
233 if(positions==1000000000) sqstd_rex_error(exp,_SC("overflow in numeric constant")); in sqstd_rex_parsenumber()
239 static SQInteger sqstd_rex_element(SQRex *exp) in sqstd_rex_element() argument
242 switch(*exp->_p) in sqstd_rex_element()
246 exp->_p++; in sqstd_rex_element()
249 if(*exp->_p =='?') { in sqstd_rex_element()
250 exp->_p++; in sqstd_rex_element()
251 sqstd_rex_expect(exp,':'); in sqstd_rex_element()
252 expr = sqstd_rex_newnode(exp,OP_NOCAPEXPR); in sqstd_rex_element()
255 expr = sqstd_rex_newnode(exp,OP_EXPR); in sqstd_rex_element()
256 SQInteger newn = sqstd_rex_list(exp); in sqstd_rex_element()
257 exp->_nodes[expr].left = newn; in sqstd_rex_element()
259 sqstd_rex_expect(exp,')'); in sqstd_rex_element()
263 exp->_p++; in sqstd_rex_element()
264 ret = sqstd_rex_class(exp); in sqstd_rex_element()
265 sqstd_rex_expect(exp,']'); in sqstd_rex_element()
267 case SQREX_SYMBOL_END_OF_STRING: exp->_p++; ret = sqstd_rex_newnode(exp,OP_EOL);break; in sqstd_rex_element()
268 case SQREX_SYMBOL_ANY_CHAR: exp->_p++; ret = sqstd_rex_newnode(exp,OP_DOT);break; in sqstd_rex_element()
270 ret = sqstd_rex_charnode(exp,SQFalse); in sqstd_rex_element()
277 switch(*exp->_p){ in sqstd_rex_element()
278 … case SQREX_SYMBOL_GREEDY_ZERO_OR_MORE: p0 = 0; p1 = 0xFFFF; exp->_p++; isgreedy = SQTrue; break; in sqstd_rex_element()
279 … case SQREX_SYMBOL_GREEDY_ONE_OR_MORE: p0 = 1; p1 = 0xFFFF; exp->_p++; isgreedy = SQTrue; break; in sqstd_rex_element()
280 case SQREX_SYMBOL_GREEDY_ZERO_OR_ONE: p0 = 0; p1 = 1; exp->_p++; isgreedy = SQTrue; break; in sqstd_rex_element()
282 exp->_p++; in sqstd_rex_element()
283 if(!isdigit(*exp->_p)) sqstd_rex_error(exp,_SC("number expected")); in sqstd_rex_element()
284 p0 = (unsigned short)sqstd_rex_parsenumber(exp); in sqstd_rex_element()
286 switch(*exp->_p) { in sqstd_rex_element()
288 p1 = p0; exp->_p++; in sqstd_rex_element()
291 exp->_p++; in sqstd_rex_element()
293 if(isdigit(*exp->_p)){ in sqstd_rex_element()
294 p1 = (unsigned short)sqstd_rex_parsenumber(exp); in sqstd_rex_element()
296 sqstd_rex_expect(exp,'}'); in sqstd_rex_element()
299 sqstd_rex_error(exp,_SC(", or } expected")); in sqstd_rex_element()
307 SQInteger nnode = sqstd_rex_newnode(exp,OP_GREEDY); in sqstd_rex_element()
308 exp->_nodes[nnode].left = ret; in sqstd_rex_element()
309 exp->_nodes[nnode].right = ((p0)<<16)|p1; in sqstd_rex_element()
313exp->_p != SQREX_SYMBOL_BRANCH) && (*exp->_p != ')') && (*exp->_p != SQREX_SYMBOL_GREEDY_ZERO_OR_M… in sqstd_rex_element()
314 SQInteger nnode = sqstd_rex_element(exp); in sqstd_rex_element()
315 exp->_nodes[ret].next = nnode; in sqstd_rex_element()
321 static SQInteger sqstd_rex_list(SQRex *exp) in sqstd_rex_list() argument
324 if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING) { in sqstd_rex_list()
325 exp->_p++; in sqstd_rex_list()
326 ret = sqstd_rex_newnode(exp,OP_BOL); in sqstd_rex_list()
328 e = sqstd_rex_element(exp); in sqstd_rex_list()
330 exp->_nodes[ret].next = e; in sqstd_rex_list()
334 if(*exp->_p == SQREX_SYMBOL_BRANCH) { in sqstd_rex_list()
336 exp->_p++; in sqstd_rex_list()
337 temp = sqstd_rex_newnode(exp,OP_OR); in sqstd_rex_list()
338 exp->_nodes[temp].left = ret; in sqstd_rex_list()
339 tright = sqstd_rex_list(exp); in sqstd_rex_list()
340 exp->_nodes[temp].right = tright; in sqstd_rex_list()
369 static SQBool sqstd_rex_matchclass(SQRex* exp,SQRexNode *node,SQChar c) in sqstd_rex_matchclass() argument
382 } while((node->next != -1) && (node = &exp->_nodes[node->next])); in sqstd_rex_matchclass()
386 static const SQChar *sqstd_rex_matchnode(SQRex* exp,SQRexNode *node,const SQChar *str,SQRexNode *ne… in sqstd_rex_matchnode() argument
398 greedystop = &exp->_nodes[node->next]; in sqstd_rex_matchnode()
407 if(!(s = sqstd_rex_matchnode(exp,&exp->_nodes[node->left],s,greedystop))) in sqstd_rex_matchnode()
419 gnext = &exp->_nodes[greedystop->next]; in sqstd_rex_matchnode()
421 gnext = &exp->_nodes[next->next]; in sqstd_rex_matchnode()
423 stop = sqstd_rex_matchnode(exp,greedystop,s,gnext); in sqstd_rex_matchnode()
433 if(s >= exp->_eol) in sqstd_rex_matchnode()
443 SQRexNode *temp=&exp->_nodes[node->left]; in sqstd_rex_matchnode()
444 while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) { in sqstd_rex_matchnode()
446 temp = &exp->_nodes[temp->next]; in sqstd_rex_matchnode()
451 temp = &exp->_nodes[node->right]; in sqstd_rex_matchnode()
452 while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) { in sqstd_rex_matchnode()
454 temp = &exp->_nodes[temp->next]; in sqstd_rex_matchnode()
463 SQRexNode *n = &exp->_nodes[node->left]; in sqstd_rex_matchnode()
466 if(node->type != OP_NOCAPEXPR && node->right == exp->_currsubexp) { in sqstd_rex_matchnode()
467 capture = exp->_currsubexp; in sqstd_rex_matchnode()
468 exp->_matches[capture].begin = cur; in sqstd_rex_matchnode()
469 exp->_currsubexp++; in sqstd_rex_matchnode()
471 SQInteger tempcap = exp->_currsubexp; in sqstd_rex_matchnode()
475 subnext = &exp->_nodes[n->next]; in sqstd_rex_matchnode()
479 if(!(cur = sqstd_rex_matchnode(exp,n,cur,subnext))) { in sqstd_rex_matchnode()
481 exp->_matches[capture].begin = 0; in sqstd_rex_matchnode()
482 exp->_matches[capture].len = 0; in sqstd_rex_matchnode()
486 } while((n->next != -1) && (n = &exp->_nodes[n->next])); in sqstd_rex_matchnode()
488 exp->_currsubexp = tempcap; in sqstd_rex_matchnode()
490 exp->_matches[capture].len = cur - exp->_matches[capture].begin; in sqstd_rex_matchnode()
494 if((str == exp->_bol && !isspace(*str)) in sqstd_rex_matchnode()
495 || (str == exp->_eol && !isspace(*(str-1))) in sqstd_rex_matchnode()
502 if(str == exp->_bol) return str; in sqstd_rex_matchnode()
505 if(str == exp->_eol) return str; in sqstd_rex_matchnode()
508 if (str == exp->_eol) return NULL; in sqstd_rex_matchnode()
514 if (str == exp->_eol) return NULL; in sqstd_rex_matchnode()
515 …if(sqstd_rex_matchclass(exp,&exp->_nodes[node->left],*str)?(type == OP_CLASS?SQTrue:SQFalse):(type… in sqstd_rex_matchnode()
521 if (str == exp->_eol) return NULL; in sqstd_rex_matchnode()
533 const SQChar *streol = exp->_eol; in sqstd_rex_matchnode()
545 if (str == exp->_eol) return NULL; in sqstd_rex_matchnode()
556 SQRex * volatile exp = (SQRex *)sq_malloc(sizeof(SQRex)); // "volatile" is needed for setjmp() in sqstd_rex_compile() local
557 exp->_eol = exp->_bol = NULL; in sqstd_rex_compile()
558 exp->_p = pattern; in sqstd_rex_compile()
559 exp->_nallocated = (SQInteger)scstrlen(pattern) * sizeof(SQChar); in sqstd_rex_compile()
560 exp->_nodes = (SQRexNode *)sq_malloc(exp->_nallocated * sizeof(SQRexNode)); in sqstd_rex_compile()
561 exp->_nsize = 0; in sqstd_rex_compile()
562 exp->_matches = 0; in sqstd_rex_compile()
563 exp->_nsubexpr = 0; in sqstd_rex_compile()
564 exp->_first = sqstd_rex_newnode(exp,OP_EXPR); in sqstd_rex_compile()
565 exp->_error = error; in sqstd_rex_compile()
566 exp->_jmpbuf = sq_malloc(sizeof(jmp_buf)); in sqstd_rex_compile()
567 if(setjmp(*((jmp_buf*)exp->_jmpbuf)) == 0) { in sqstd_rex_compile()
568 SQInteger res = sqstd_rex_list(exp); in sqstd_rex_compile()
569 exp->_nodes[exp->_first].left = res; in sqstd_rex_compile()
570 if(*exp->_p!='\0') in sqstd_rex_compile()
571 sqstd_rex_error(exp,_SC("unexpected character")); in sqstd_rex_compile()
576 nsize = exp->_nsize; in sqstd_rex_compile()
577 t = &exp->_nodes[0]; in sqstd_rex_compile()
580 if(exp->_nodes[i].type>MAX_CHAR) in sqstd_rex_compile()
581 … scprintf(_SC("[%02d] %10s "), (SQInt32)i,g_nnames[exp->_nodes[i].type-MAX_CHAR]); in sqstd_rex_compile()
583 scprintf(_SC("[%02d] %10c "), (SQInt32)i,exp->_nodes[i].type); in sqstd_rex_compile()
584 …eft %02d right %02d next %02d\n"), (SQInt32)exp->_nodes[i].left, (SQInt32)exp->_nodes[i].right, (S… in sqstd_rex_compile()
589 exp->_matches = (SQRexMatch *) sq_malloc(exp->_nsubexpr * sizeof(SQRexMatch)); in sqstd_rex_compile()
590 memset(exp->_matches,0,exp->_nsubexpr * sizeof(SQRexMatch)); in sqstd_rex_compile()
593 sqstd_rex_free(exp); in sqstd_rex_compile()
596 return exp; in sqstd_rex_compile()
599 void sqstd_rex_free(SQRex *exp) in sqstd_rex_free() argument
601 if(exp) { in sqstd_rex_free()
602 if(exp->_nodes) sq_free(exp->_nodes,exp->_nallocated * sizeof(SQRexNode)); in sqstd_rex_free()
603 if(exp->_jmpbuf) sq_free(exp->_jmpbuf,sizeof(jmp_buf)); in sqstd_rex_free()
604 if(exp->_matches) sq_free(exp->_matches,exp->_nsubexpr * sizeof(SQRexMatch)); in sqstd_rex_free()
605 sq_free(exp,sizeof(SQRex)); in sqstd_rex_free()
609 SQBool sqstd_rex_match(SQRex* exp,const SQChar* text) in sqstd_rex_match() argument
612 exp->_bol = text; in sqstd_rex_match()
613 exp->_eol = text + scstrlen(text); in sqstd_rex_match()
614 exp->_currsubexp = 0; in sqstd_rex_match()
615 res = sqstd_rex_matchnode(exp,exp->_nodes,text,NULL); in sqstd_rex_match()
616 if(res == NULL || res != exp->_eol) in sqstd_rex_match()
621 SQBool sqstd_rex_searchrange(SQRex* exp,const SQChar* text_begin,const SQChar* text_end,const SQCha… in sqstd_rex_searchrange() argument
624 SQInteger node = exp->_first; in sqstd_rex_searchrange()
626 exp->_bol = text_begin; in sqstd_rex_searchrange()
627 exp->_eol = text_end; in sqstd_rex_searchrange()
631 exp->_currsubexp = 0; in sqstd_rex_searchrange()
632 cur = sqstd_rex_matchnode(exp,&exp->_nodes[node],cur,NULL); in sqstd_rex_searchrange()
635 node = exp->_nodes[node].next; in sqstd_rex_searchrange()
650 SQBool sqstd_rex_search(SQRex* exp,const SQChar* text, const SQChar** out_begin, const SQChar** out… in sqstd_rex_search() argument
652 return sqstd_rex_searchrange(exp,text,text + scstrlen(text),out_begin,out_end); in sqstd_rex_search()
655 SQInteger sqstd_rex_getsubexpcount(SQRex* exp) in sqstd_rex_getsubexpcount() argument
657 return exp->_nsubexpr; in sqstd_rex_getsubexpcount()
660 SQBool sqstd_rex_getsubexp(SQRex* exp, SQInteger n, SQRexMatch *subexp) in sqstd_rex_getsubexp() argument
662 if( n<0 || n >= exp->_nsubexpr) return SQFalse; in sqstd_rex_getsubexp()
663 *subexp = exp->_matches[n]; in sqstd_rex_getsubexp()