1 /* see copyright notice in squirrel.h */
6 #include "sqstdstring.h"
9 #define scisprint iswprint
11 #define scisprint isprint
17 static const SQChar *g_nnames[] =
19 _SC("NONE"),_SC("OP_GREEDY"), _SC("OP_OR"),
20 _SC("OP_EXPR"),_SC("OP_NOCAPEXPR"),_SC("OP_DOT"), _SC("OP_CLASS"),
21 _SC("OP_CCLASS"),_SC("OP_NCLASS"),_SC("OP_RANGE"),_SC("OP_CHAR"),
22 _SC("OP_EOL"),_SC("OP_BOL"),_SC("OP_WB")
27 #define OP_GREEDY MAX_CHAR+1 // * + ? {n}
28 #define OP_OR MAX_CHAR+2
29 #define OP_EXPR MAX_CHAR+3 //parentesis ()
30 #define OP_NOCAPEXPR MAX_CHAR+4 //parentesis (?:)
31 #define OP_DOT MAX_CHAR+5
32 #define OP_CLASS MAX_CHAR+6
33 #define OP_CCLASS MAX_CHAR+7
34 #define OP_NCLASS MAX_CHAR+8 //negates class the [^
35 #define OP_RANGE MAX_CHAR+9
36 #define OP_CHAR MAX_CHAR+10
37 #define OP_EOL MAX_CHAR+11
38 #define OP_BOL MAX_CHAR+12
39 #define OP_WB MAX_CHAR+13
41 #define SQREX_SYMBOL_ANY_CHAR '.'
42 #define SQREX_SYMBOL_GREEDY_ONE_OR_MORE '+'
43 #define SQREX_SYMBOL_GREEDY_ZERO_OR_MORE '*'
44 #define SQREX_SYMBOL_GREEDY_ZERO_OR_ONE '?'
45 #define SQREX_SYMBOL_BRANCH '|'
46 #define SQREX_SYMBOL_END_OF_STRING '$'
47 #define SQREX_SYMBOL_BEGINNING_OF_STRING '^'
48 #define SQREX_SYMBOL_ESCAPE_CHAR '\\'
51 typedef int SQRexNodeType;
53 typedef struct tagSQRexNode{
67 SQInteger _nallocated;
71 SQInteger _currsubexp;
73 const SQChar **_error;
76 static SQInteger sqstd_rex_list(SQRex *exp);
78 static SQInteger sqstd_rex_newnode(SQRex *exp, SQRexNodeType type)
82 n.next = n.right = n.left = -1;
84 n.right = exp->_nsubexpr++;
85 if(exp->_nallocated < (exp->_nsize + 1)) {
86 SQInteger oldsize = exp->_nallocated;
87 exp->_nallocated *= 2;
88 exp->_nodes = (SQRexNode *)sq_realloc(exp->_nodes, oldsize * sizeof(SQRexNode) ,exp->_nallocated * sizeof(SQRexNode));
90 exp->_nodes[exp->_nsize++] = n;
91 return (SQInteger)exp->_nsize - 1;
94 static void sqstd_rex_error(SQRex *exp,const SQChar *error)
96 if(exp->_error) *exp->_error = error;
97 longjmp(*((jmp_buf*)exp->_jmpbuf),-1);
100 static void sqstd_rex_expect(SQRex *exp, SQInteger n){
102 sqstd_rex_error(exp, _SC("expected paren"));
106 /*static SQBool sqstd_rex_ischar(SQChar c)
109 case SQREX_SYMBOL_BRANCH:case SQREX_SYMBOL_GREEDY_ZERO_OR_MORE:
110 case SQREX_SYMBOL_GREEDY_ZERO_OR_ONE:case SQREX_SYMBOL_GREEDY_ONE_OR_MORE:
111 case SQREX_SYMBOL_BEGINNING_OF_STRING:case SQREX_SYMBOL_END_OF_STRING:
112 case SQREX_SYMBOL_ANY_CHAR:case SQREX_SYMBOL_ESCAPE_CHAR:case '(':case ')':case '[':case '{': case '}':
118 static SQChar sqstd_rex_escapechar(SQRex *exp)
120 if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR){
123 case 'v': exp->_p++; return '\v';
124 case 'n': exp->_p++; return '\n';
125 case 't': exp->_p++; return '\t';
126 case 'r': exp->_p++; return '\r';
127 case 'f': exp->_p++; return '\f';
128 default: return (*exp->_p++);
130 } else if(!scisprint(*exp->_p)) sqstd_rex_error(exp,_SC("letter expected"));
134 static SQInteger sqstd_rex_charclass(SQRex *exp,SQInteger classid)
136 SQInteger n = sqstd_rex_newnode(exp,OP_CCLASS);
137 exp->_nodes[n].left = classid;
141 static SQInteger sqstd_rex_charnode(SQRex *exp,SQBool isclass)
143 if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR) {
146 case 'n': exp->_p++; return sqstd_rex_newnode(exp,'\n');
147 case 't': exp->_p++; return sqstd_rex_newnode(exp,'\t');
148 case 'r': exp->_p++; return sqstd_rex_newnode(exp,'\r');
149 case 'f': exp->_p++; return sqstd_rex_newnode(exp,'\f');
150 case 'v': exp->_p++; return sqstd_rex_newnode(exp,'\v');
151 case 'a': case 'A': case 'w': case 'W': case 's': case 'S':
152 case 'd': case 'D': case 'x': case 'X': case 'c': case 'C':
153 case 'p': case 'P': case 'l': case 'u':
157 return sqstd_rex_charclass(exp,t);
162 SQInteger node = sqstd_rex_newnode(exp,OP_WB);
163 exp->_nodes[node].left = *exp->_p;
167 default: return sqstd_rex_newnode(exp,(*exp->_p++));
170 else if(!scisprint(*exp->_p)) {
172 sqstd_rex_error(exp,_SC("letter expected"));
174 return sqstd_rex_newnode(exp,*exp->_p++);
176 static SQInteger sqstd_rex_class(SQRex *exp)
179 SQInteger first = -1,chain;
180 if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING){
181 ret = sqstd_rex_newnode(exp,OP_NCLASS);
183 }else ret = sqstd_rex_newnode(exp,OP_CLASS);
185 if(*exp->_p == ']') sqstd_rex_error(exp,_SC("empty class"));
187 while(*exp->_p != ']' && exp->_p != exp->_eol) {
188 if(*exp->_p == '-' && first != -1){
190 if(*exp->_p++ == ']') sqstd_rex_error(exp,_SC("unfinished range"));
191 r = sqstd_rex_newnode(exp,OP_RANGE);
192 if(first>*exp->_p) sqstd_rex_error(exp,_SC("invalid range"));
193 if(exp->_nodes[first].type == OP_CCLASS) sqstd_rex_error(exp,_SC("cannot use character classes in ranges"));
194 exp->_nodes[r].left = exp->_nodes[first].type;
195 exp->_nodes[r].right = sqstd_rex_escapechar(exp);
196 exp->_nodes[chain].next = r;
203 exp->_nodes[chain].next = c;
205 first = sqstd_rex_charnode(exp,SQTrue);
208 first = sqstd_rex_charnode(exp,SQTrue);
214 exp->_nodes[chain].next = c;
219 exp->_nodes[ret].left = exp->_nodes[ret].next;
220 exp->_nodes[ret].next = -1;
224 static SQInteger sqstd_rex_parsenumber(SQRex *exp)
226 SQInteger ret = *exp->_p-'0';
227 SQInteger positions = 10;
229 while(isdigit(*exp->_p)) {
230 ret = ret*10+(*exp->_p++-'0');
231 if(positions==1000000000) sqstd_rex_error(exp,_SC("overflow in numeric constant"));
237 static SQInteger sqstd_rex_element(SQRex *exp)
249 sqstd_rex_expect(exp,':');
250 expr = sqstd_rex_newnode(exp,OP_NOCAPEXPR);
253 expr = sqstd_rex_newnode(exp,OP_EXPR);
254 exp->_nodes[expr].left = sqstd_rex_list(exp);
256 sqstd_rex_expect(exp,')');
261 ret = sqstd_rex_class(exp);
262 sqstd_rex_expect(exp,']');
264 case SQREX_SYMBOL_END_OF_STRING: exp->_p++; ret = sqstd_rex_newnode(exp,OP_EOL);break;
265 case SQREX_SYMBOL_ANY_CHAR: exp->_p++; ret = sqstd_rex_newnode(exp,OP_DOT);break;
267 ret = sqstd_rex_charnode(exp,SQFalse);
273 unsigned short p0 = 0, p1 = 0;
275 case SQREX_SYMBOL_GREEDY_ZERO_OR_MORE: p0 = 0; p1 = 0xFFFF; exp->_p++; goto __end;
276 case SQREX_SYMBOL_GREEDY_ONE_OR_MORE: p0 = 1; p1 = 0xFFFF; exp->_p++; goto __end;
277 case SQREX_SYMBOL_GREEDY_ZERO_OR_ONE: p0 = 0; p1 = 1; exp->_p++; goto __end;
280 if(!isdigit(*exp->_p)) sqstd_rex_error(exp,_SC("number expected"));
281 p0 = (unsigned short)sqstd_rex_parsenumber(exp);
289 if(isdigit(*exp->_p)){
290 p1 = (unsigned short)sqstd_rex_parsenumber(exp);
292 sqstd_rex_expect(exp,'}');
295 sqstd_rex_error(exp,_SC(", or } expected"));
299 SQInteger nnode = sqstd_rex_newnode(exp,OP_GREEDY);
301 exp->_nodes[nnode].left = ret;
302 exp->_nodes[nnode].right = ((p0)<<16)|p1;
307 if(*exp->_p != SQREX_SYMBOL_BRANCH && *exp->_p != ')' && *exp->_p != SQREX_SYMBOL_GREEDY_ZERO_OR_MORE && *exp->_p != SQREX_SYMBOL_GREEDY_ONE_OR_MORE && *exp->_p != '\0')
308 exp->_nodes[ret].next = sqstd_rex_element(exp);
312 static SQInteger sqstd_rex_list(SQRex *exp)
315 if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING) {
317 ret = sqstd_rex_newnode(exp,OP_BOL);
319 e = sqstd_rex_element(exp);
321 exp->_nodes[ret].next = e;
325 if(*exp->_p == SQREX_SYMBOL_BRANCH) {
328 temp = sqstd_rex_newnode(exp,OP_OR);
329 exp->_nodes[temp].left = ret;
330 exp->_nodes[temp].right = sqstd_rex_list(exp);
336 static SQBool sqstd_rex_matchcclass(SQInteger cclass,SQChar c)
339 case 'a': return isalpha(c)?SQTrue:SQFalse;
340 case 'A': return !isalpha(c)?SQTrue:SQFalse;
341 case 'w': return (isalnum(c) || c == '_')?SQTrue:SQFalse;
342 case 'W': return (!isalnum(c) && c != '_')?SQTrue:SQFalse;
343 case 's': return isspace(c)?SQTrue:SQFalse;
344 case 'S': return !isspace(c)?SQTrue:SQFalse;
345 case 'd': return isdigit(c)?SQTrue:SQFalse;
346 case 'D': return !isdigit(c)?SQTrue:SQFalse;
347 case 'x': return isxdigit(c)?SQTrue:SQFalse;
348 case 'X': return !isxdigit(c)?SQTrue:SQFalse;
349 case 'c': return iscntrl(c)?SQTrue:SQFalse;
350 case 'C': return !iscntrl(c)?SQTrue:SQFalse;
351 case 'p': return ispunct(c)?SQTrue:SQFalse;
352 case 'P': return !ispunct(c)?SQTrue:SQFalse;
353 case 'l': return islower(c)?SQTrue:SQFalse;
354 case 'u': return isupper(c)?SQTrue:SQFalse;
356 return SQFalse; /*cannot happen*/
359 static SQBool sqstd_rex_matchclass(SQRex* exp,SQRexNode *node,SQChar c)
364 if(c >= node->left && c <= node->right) return SQTrue;
367 if(sqstd_rex_matchcclass(node->left,c)) return SQTrue;
370 if(c == node->type)return SQTrue;
372 } while((node->next != -1) && (node = &exp->_nodes[node->next]));
376 static const SQChar *sqstd_rex_matchnode(SQRex* exp,SQRexNode *node,const SQChar *str,SQRexNode *next)
379 SQRexNodeType type = node->type;
382 //SQRexNode *greedystop = (node->next != -1) ? &exp->_nodes[node->next] : NULL;
383 SQRexNode *greedystop = NULL;
384 SQInteger p0 = (node->right >> 16)&0x0000FFFF, p1 = node->right&0x0000FFFF, nmaches = 0;
385 const SQChar *s=str, *good = str;
387 if(node->next != -1) {
388 greedystop = &exp->_nodes[node->next];
394 while((nmaches == 0xFFFF || nmaches < p1)) {
397 if(!(s = sqstd_rex_matchnode(exp,&exp->_nodes[node->left],s,greedystop)))
402 //checks that 0 matches satisfy the expression(if so skips)
403 //if not would always stop(for instance if is a '?')
404 if(greedystop->type != OP_GREEDY ||
405 (greedystop->type == OP_GREEDY && ((greedystop->right >> 16)&0x0000FFFF) != 0))
407 SQRexNode *gnext = NULL;
408 if(greedystop->next != -1) {
409 gnext = &exp->_nodes[greedystop->next];
410 }else if(next && next->next != -1){
411 gnext = &exp->_nodes[next->next];
413 stop = sqstd_rex_matchnode(exp,greedystop,s,gnext);
415 //if satisfied stop it
416 if(p0 == p1 && p0 == nmaches) break;
417 else if(nmaches >= p0 && p1 == 0xFFFF) break;
418 else if(nmaches >= p0 && nmaches <= p1) break;
426 if(p0 == p1 && p0 == nmaches) return good;
427 else if(nmaches >= p0 && p1 == 0xFFFF) return good;
428 else if(nmaches >= p0 && nmaches <= p1) return good;
432 const SQChar *asd = str;
433 SQRexNode *temp=&exp->_nodes[node->left];
434 while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) {
436 temp = &exp->_nodes[temp->next];
441 temp = &exp->_nodes[node->right];
442 while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) {
444 temp = &exp->_nodes[temp->next];
453 SQRexNode *n = &exp->_nodes[node->left];
454 const SQChar *cur = str;
455 SQInteger capture = -1;
456 if(node->type != OP_NOCAPEXPR && node->right == exp->_currsubexp) {
457 capture = exp->_currsubexp;
458 exp->_matches[capture].begin = cur;
463 SQRexNode *subnext = NULL;
465 subnext = &exp->_nodes[n->next];
469 if(!(cur = sqstd_rex_matchnode(exp,n,cur,subnext))) {
471 exp->_matches[capture].begin = 0;
472 exp->_matches[capture].len = 0;
476 } while((n->next != -1) && (n = &exp->_nodes[n->next]));
479 exp->_matches[capture].len = cur - exp->_matches[capture].begin;
483 if(str == exp->_bol && !isspace(*str)
484 || (str == exp->_eol && !isspace(*(str-1)))
485 || (!isspace(*str) && isspace(*(str+1)))
486 || (isspace(*str) && !isspace(*(str+1))) ) {
487 return (node->left == 'b')?str:NULL;
489 return (node->left == 'b')?NULL:str;
491 if(str == exp->_bol) return str;
494 if(str == exp->_eol) return str;
502 if(sqstd_rex_matchclass(exp,&exp->_nodes[node->left],*str)?(type == OP_CLASS?SQTrue:SQFalse):(type == OP_NCLASS?SQTrue:SQFalse)) {
508 if(sqstd_rex_matchcclass(node->left,*str)) {
514 if(*str != node->type) return NULL;
522 SQRex *sqstd_rex_compile(const SQChar *pattern,const SQChar **error)
524 SQRex *exp = (SQRex *)sq_malloc(sizeof(SQRex));
525 exp->_eol = exp->_bol = NULL;
527 exp->_nallocated = (SQInteger)scstrlen(pattern) * sizeof(SQChar);
528 exp->_nodes = (SQRexNode *)sq_malloc(exp->_nallocated * sizeof(SQRexNode));
532 exp->_first = sqstd_rex_newnode(exp,OP_EXPR);
534 exp->_jmpbuf = sq_malloc(sizeof(jmp_buf));
535 if(setjmp(*((jmp_buf*)exp->_jmpbuf)) == 0) {
536 exp->_nodes[exp->_first].left=sqstd_rex_list(exp);
538 sqstd_rex_error(exp,_SC("unexpected character"));
546 for(i = 0;i < nsize; i++) {
547 if(exp->_nodes[i].type>MAX_CHAR)
548 scprintf(_SC("[%02d] %10s "),i,g_nnames[exp->_nodes[i].type-MAX_CHAR]);
550 scprintf(_SC("[%02d] %10c "),i,exp->_nodes[i].type);
551 scprintf(_SC("left %02d right %02d next %02d\n"),exp->_nodes[i].left,exp->_nodes[i].right,exp->_nodes[i].next);
556 exp->_matches = (SQRexMatch *) sq_malloc(exp->_nsubexpr * sizeof(SQRexMatch));
557 memset(exp->_matches,0,exp->_nsubexpr * sizeof(SQRexMatch));
566 void sqstd_rex_free(SQRex *exp)
569 if(exp->_nodes) sq_free(exp->_nodes,exp->_nallocated * sizeof(SQRexNode));
570 if(exp->_jmpbuf) sq_free(exp->_jmpbuf,sizeof(jmp_buf));
571 if(exp->_matches) sq_free(exp->_matches,exp->_nsubexpr * sizeof(SQRexMatch));
572 sq_free(exp,sizeof(SQRex));
576 SQBool sqstd_rex_match(SQRex* exp,const SQChar* text)
578 const SQChar* res = NULL;
580 exp->_eol = text + scstrlen(text);
581 exp->_currsubexp = 0;
582 res = sqstd_rex_matchnode(exp,exp->_nodes,text,NULL);
583 if(res == NULL || res != exp->_eol)
588 SQBool sqstd_rex_searchrange(SQRex* exp,const SQChar* text_begin,const SQChar* text_end,const SQChar** out_begin, const SQChar** out_end)
590 const SQChar *cur = NULL;
591 SQInteger node = exp->_first;
592 if(text_begin >= text_end) return SQFalse;
593 exp->_bol = text_begin;
594 exp->_eol = text_end;
598 exp->_currsubexp = 0;
599 cur = sqstd_rex_matchnode(exp,&exp->_nodes[node],cur,NULL);
602 node = exp->_nodes[node].next;
605 } while(cur == NULL && text_begin != text_end);
612 if(out_begin) *out_begin = text_begin;
613 if(out_end) *out_end = cur;
617 SQBool sqstd_rex_search(SQRex* exp,const SQChar* text, const SQChar** out_begin, const SQChar** out_end)
619 return sqstd_rex_searchrange(exp,text,text + scstrlen(text),out_begin,out_end);
622 SQInteger sqstd_rex_getsubexpcount(SQRex* exp)
624 return exp->_nsubexpr;
627 SQBool sqstd_rex_getsubexp(SQRex* exp, SQInteger n, SQRexMatch *subexp)
629 if( n<0 || n >= exp->_nsubexpr) return SQFalse;
630 *subexp = exp->_matches[n];