1 /* see copyright notice in squirrel.h */
6 #include "sqstdstring.h"
11 static const SQChar *g_nnames[] =
13 _SC("NONE"),_SC("OP_GREEDY"), _SC("OP_OR"),
14 _SC("OP_EXPR"),_SC("OP_NOCAPEXPR"),_SC("OP_DOT"), _SC("OP_CLASS"),
15 _SC("OP_CCLASS"),_SC("OP_NCLASS"),_SC("OP_RANGE"),_SC("OP_CHAR"),
16 _SC("OP_EOL"),_SC("OP_BOL"),_SC("OP_WB")
21 #define OP_GREEDY MAX_CHAR+1 // * + ? {n}
22 #define OP_OR MAX_CHAR+2
23 #define OP_EXPR MAX_CHAR+3 //parentesis ()
24 #define OP_NOCAPEXPR MAX_CHAR+4 //parentesis (?:)
25 #define OP_DOT MAX_CHAR+5
26 #define OP_CLASS MAX_CHAR+6
27 #define OP_CCLASS MAX_CHAR+7
28 #define OP_NCLASS MAX_CHAR+8 //negates class the [^
29 #define OP_RANGE MAX_CHAR+9
30 #define OP_CHAR MAX_CHAR+10
31 #define OP_EOL MAX_CHAR+11
32 #define OP_BOL MAX_CHAR+12
33 #define OP_WB MAX_CHAR+13
35 #define SQREX_SYMBOL_ANY_CHAR '.'
36 #define SQREX_SYMBOL_GREEDY_ONE_OR_MORE '+'
37 #define SQREX_SYMBOL_GREEDY_ZERO_OR_MORE '*'
38 #define SQREX_SYMBOL_GREEDY_ZERO_OR_ONE '?'
39 #define SQREX_SYMBOL_BRANCH '|'
40 #define SQREX_SYMBOL_END_OF_STRING '$'
41 #define SQREX_SYMBOL_BEGINNING_OF_STRING '^'
42 #define SQREX_SYMBOL_ESCAPE_CHAR '\\'
45 typedef int SQRexNodeType;
47 typedef struct tagSQRexNode{
67 const SQChar **_error;
70 static int sqstd_rex_list(SQRex *exp);
72 static int sqstd_rex_newnode(SQRex *exp, SQRexNodeType type)
76 n.next = n.right = n.left = -1;
78 n.right = exp->_nsubexpr++;
79 if(exp->_nallocated < (exp->_nsize + 1)) {
80 int oldsize = exp->_nallocated;
81 exp->_nallocated *= 2;
82 exp->_nodes = (SQRexNode *)sq_realloc(exp->_nodes, oldsize * sizeof(SQRexNode) ,exp->_nallocated * sizeof(SQRexNode));
84 exp->_nodes[exp->_nsize++] = n;
85 return (int)exp->_nsize - 1;
88 static void sqstd_rex_error(SQRex *exp,const SQChar *error)
90 if(exp->_error) *exp->_error = error;
91 longjmp(*((jmp_buf*)exp->_jmpbuf),-1);
94 static void sqstd_rex_expect(SQRex *exp, int n){
96 sqstd_rex_error(exp, _SC("expected paren"));
100 static SQBool sqstd_rex_ischar(SQChar c)
103 case SQREX_SYMBOL_BRANCH:case SQREX_SYMBOL_GREEDY_ZERO_OR_MORE:
104 case SQREX_SYMBOL_GREEDY_ZERO_OR_ONE:case SQREX_SYMBOL_GREEDY_ONE_OR_MORE:
105 case SQREX_SYMBOL_BEGINNING_OF_STRING:case SQREX_SYMBOL_END_OF_STRING:
106 case SQREX_SYMBOL_ANY_CHAR:case SQREX_SYMBOL_ESCAPE_CHAR:case '(':case ')':case '[':case '{': case '}':
112 static SQChar sqstd_rex_escapechar(SQRex *exp)
114 if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR){
117 case 'v': exp->_p++; return '\v';
118 case 'n': exp->_p++; return '\n';
119 case 't': exp->_p++; return '\t';
120 case 'r': exp->_p++; return '\r';
121 case 'f': exp->_p++; return '\f';
122 default: return (*exp->_p++);
124 } else if(!sqstd_rex_ischar(*exp->_p)) sqstd_rex_error(exp,_SC("letter expected"));
128 static int sqstd_rex_charclass(SQRex *exp,int classid)
130 int n = sqstd_rex_newnode(exp,OP_CCLASS);
131 exp->_nodes[n].left = classid;
135 static int sqstd_rex_charnode(SQRex *exp,SQBool isclass)
137 if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR) {
140 case 'n': exp->_p++; return sqstd_rex_newnode(exp,'\n');
141 case 't': exp->_p++; return sqstd_rex_newnode(exp,'\t');
142 case 'r': exp->_p++; return sqstd_rex_newnode(exp,'\r');
143 case 'f': exp->_p++; return sqstd_rex_newnode(exp,'\f');
144 case 'v': exp->_p++; return sqstd_rex_newnode(exp,'\v');
145 case 'a': case 'A': case 'w': case 'W': case 's': case 'S':
146 case 'd': case 'D': case 'x': case 'X': case 'c': case 'C':
147 case 'p': case 'P': case 'l': case 'u':
151 return sqstd_rex_charclass(exp,t);
156 int node = sqstd_rex_newnode(exp,OP_WB);
157 exp->_nodes[node].left = *exp->_p;
161 default: return sqstd_rex_newnode(exp,(*exp->_p++));
164 else if(!sqstd_rex_ischar(*exp->_p)) {
166 sqstd_rex_error(exp,_SC("letter expected"));
168 return sqstd_rex_newnode(exp,*exp->_p++);
170 static int sqstd_rex_class(SQRex *exp)
173 int first = -1,chain;
174 if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING){
175 ret = sqstd_rex_newnode(exp,OP_NCLASS);
177 }else ret = sqstd_rex_newnode(exp,OP_CLASS);
179 if(*exp->_p == ']' || *exp->_p == '-'){
184 while(*exp->_p != ']' && exp->_p != exp->_eol) {
185 if(*exp->_p == '-' && first != -1){
187 if(*exp->_p++ == ']') sqstd_rex_error(exp,_SC("unfinished range"));
188 r = sqstd_rex_newnode(exp,OP_RANGE);
189 if(first>*exp->_p) sqstd_rex_error(exp,_SC("invalid range"));
190 if(exp->_nodes[first].type == OP_CCLASS) sqstd_rex_error(exp,_SC("cannot use character classes in ranges"));
191 exp->_nodes[r].left = exp->_nodes[first].type;
192 exp->_nodes[r].right = sqstd_rex_escapechar(exp);
193 exp->_nodes[chain].next = r;
200 exp->_nodes[chain].next = c;
202 first = sqstd_rex_charnode(exp,SQTrue);
205 first = sqstd_rex_charnode(exp,SQTrue);
211 exp->_nodes[chain].next = c;
216 exp->_nodes[ret].left = exp->_nodes[ret].next;
217 exp->_nodes[ret].next = -1;
221 static int sqstd_rex_parsenumber(SQRex *exp)
223 int ret = *exp->_p-'0';
226 while(isdigit(*exp->_p)) {
227 ret = ret*10+(*exp->_p++-'0');
228 if(positions==1000000000) sqstd_rex_error(exp,_SC("overflow in numeric constant"));
234 static int sqstd_rex_element(SQRex *exp)
246 sqstd_rex_expect(exp,':');
247 expr = sqstd_rex_newnode(exp,OP_NOCAPEXPR);
250 expr = sqstd_rex_newnode(exp,OP_EXPR);
251 exp->_nodes[expr].left = sqstd_rex_list(exp);
253 sqstd_rex_expect(exp,')');
258 ret = sqstd_rex_class(exp);
259 sqstd_rex_expect(exp,']');
261 case SQREX_SYMBOL_END_OF_STRING: exp->_p++; ret = sqstd_rex_newnode(exp,OP_EOL);break;
262 case SQREX_SYMBOL_ANY_CHAR: exp->_p++; ret = sqstd_rex_newnode(exp,OP_DOT);break;
264 ret = sqstd_rex_charnode(exp,SQFalse);
270 unsigned short p0 = 0, p1 = 0;
272 case SQREX_SYMBOL_GREEDY_ZERO_OR_MORE: p0 = 0; p1 = 0xFFFF; exp->_p++; goto __end;
273 case SQREX_SYMBOL_GREEDY_ONE_OR_MORE: p0 = 1; p1 = 0xFFFF; exp->_p++; goto __end;
274 case SQREX_SYMBOL_GREEDY_ZERO_OR_ONE: p0 = 0; p1 = 1; exp->_p++; goto __end;
277 if(!isdigit(*exp->_p)) sqstd_rex_error(exp,_SC("number expected"));
278 p0 = sqstd_rex_parsenumber(exp);
286 if(isdigit(*exp->_p)){
287 p1 = sqstd_rex_parsenumber(exp);
289 sqstd_rex_expect(exp,'}');
292 sqstd_rex_error(exp,_SC(", or } expected"));
296 int nnode = sqstd_rex_newnode(exp,OP_GREEDY);
298 exp->_nodes[nnode].left = ret;
299 exp->_nodes[nnode].right = ((p0)<<16)|p1;
304 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')
305 exp->_nodes[ret].next = sqstd_rex_element(exp);
309 static int sqstd_rex_list(SQRex *exp)
312 if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING) {
314 ret = sqstd_rex_newnode(exp,OP_BOL);
316 e = sqstd_rex_element(exp);
318 exp->_nodes[ret].next = e;
322 if(*exp->_p == SQREX_SYMBOL_BRANCH) {
325 temp = sqstd_rex_newnode(exp,OP_OR);
326 exp->_nodes[temp].left = ret;
327 exp->_nodes[temp].right = sqstd_rex_list(exp);
333 static SQBool sqstd_rex_matchcclass(int cclass,SQChar c)
336 case 'a': return isalpha(c)?SQTrue:SQFalse;
337 case 'A': return !isalpha(c)?SQTrue:SQFalse;
338 case 'w': return (isalnum(c) || c == '_')?SQTrue:SQFalse;
339 case 'W': return (!isalnum(c) && c != '_')?SQTrue:SQFalse;
340 case 's': return isspace(c)?SQTrue:SQFalse;
341 case 'S': return !isspace(c)?SQTrue:SQFalse;
342 case 'd': return isdigit(c)?SQTrue:SQFalse;
343 case 'D': return !isdigit(c)?SQTrue:SQFalse;
344 case 'x': return isxdigit(c)?SQTrue:SQFalse;
345 case 'X': return !isxdigit(c)?SQTrue:SQFalse;
346 case 'c': return iscntrl(c)?SQTrue:SQFalse;
347 case 'C': return !iscntrl(c)?SQTrue:SQFalse;
348 case 'p': return ispunct(c)?SQTrue:SQFalse;
349 case 'P': return !ispunct(c)?SQTrue:SQFalse;
350 case 'l': return islower(c)?SQTrue:SQFalse;
351 case 'u': return isupper(c)?SQTrue:SQFalse;
353 return SQFalse; /*cannot happen*/
356 static SQBool sqstd_rex_matchclass(SQRex* exp,SQRexNode *node,SQChar c)
361 if(c >= node->left && c <= node->right) return SQTrue;
364 if(sqstd_rex_matchcclass(node->left,c)) return SQTrue;
367 if(c == node->type)return SQTrue;
369 } while((node->next != -1) && (node = &exp->_nodes[node->next]));
373 static const SQChar *sqstd_rex_matchnode(SQRex* exp,SQRexNode *node,const SQChar *str)
375 SQRexNodeType type = node->type;
378 int p0 = (node->right >> 16)&0x0000FFFF, p1 = node->right&0x0000FFFF, nmaches = 0;
379 const SQChar *s=str, *good = str;
380 while((nmaches == 0xFFFF || nmaches < p1)
381 && (s = sqstd_rex_matchnode(exp,&exp->_nodes[node->left],s))) {
387 if(p0 == p1 && p0 == nmaches) return good;
388 else if(nmaches >= p0 && p1 == 0xFFFF) return good;
389 else if(nmaches >= p0 && nmaches <= p1) return good;
393 const SQChar *asd = str;
394 SQRexNode *temp=&exp->_nodes[node->left];
395 while(asd = sqstd_rex_matchnode(exp,temp,asd)) {
397 temp = &exp->_nodes[temp->next];
402 temp = &exp->_nodes[node->right];
403 while(asd = sqstd_rex_matchnode(exp,temp,asd)) {
405 temp = &exp->_nodes[temp->next];
414 SQRexNode *n = &exp->_nodes[node->left];
415 const SQChar *cur = str;
417 if(node->type != OP_NOCAPEXPR && node->right == exp->_currsubexp) {
418 capture = exp->_currsubexp;
419 exp->_matches[capture].begin = cur;
424 if(!(cur = sqstd_rex_matchnode(exp,n,cur))) {
426 exp->_matches[capture].begin = 0;
427 exp->_matches[capture].len = 0;
431 } while((n->next != -1) && (n = &exp->_nodes[n->next]));
434 exp->_matches[capture].len = cur - exp->_matches[capture].begin;
438 if(str == exp->_bol && !isspace(*str)
439 || (str == exp->_eol && !isspace(*(str-1)))
440 || (!isspace(*str) && isspace(*(str+1)))
441 || (isspace(*str) && !isspace(*(str+1))) ) {
442 return (node->left == 'b')?str:NULL;
444 return (node->left == 'b')?NULL:str;
446 if(str == exp->_bol) return str;
449 if(str == exp->_eol) return str;
456 if(sqstd_rex_matchclass(exp,&exp->_nodes[node->left],*str)?(type == OP_CLASS?SQTrue:SQFalse):(type == OP_NCLASS?SQTrue:SQFalse)) {
462 if(sqstd_rex_matchcclass(node->left,*str)) {
468 if(*str != node->type) return NULL;
476 SQRex *sqstd_rex_compile(const SQChar *pattern,const SQChar **error)
478 SQRex *exp = (SQRex *)sq_malloc(sizeof(SQRex));
480 exp->_nallocated = (int)scstrlen(pattern) * sizeof(SQChar);
481 exp->_nodes = (SQRexNode *)sq_malloc(exp->_nallocated * sizeof(SQRexNode));
485 exp->_first = sqstd_rex_newnode(exp,OP_EXPR);
487 exp->_jmpbuf = sq_malloc(sizeof(jmp_buf));
488 if(setjmp(*((jmp_buf*)exp->_jmpbuf)) == 0) {
489 exp->_nodes[exp->_first].left=sqstd_rex_list(exp);
491 sqstd_rex_error(exp,_SC("unexpected character"));
499 for(i = 0;i < nsize; i++) {
500 if(exp->_nodes[i].type>MAX_CHAR)
501 scprintf(_SC("[%02d] %10s "),i,g_nnames[exp->_nodes[i].type-MAX_CHAR]);
503 scprintf(_SC("[%02d] %10c "),i,exp->_nodes[i].type);
504 scprintf(_SC("left %02d right %02d next %02d\n"),exp->_nodes[i].left,exp->_nodes[i].right,exp->_nodes[i].next);
509 exp->_matches = (SQRexMatch *) sq_malloc(exp->_nsubexpr * sizeof(SQRexMatch));
510 memset(exp->_matches,0,exp->_nsubexpr * sizeof(SQRexMatch));
519 void sqstd_rex_free(SQRex *exp)
522 if(exp->_nodes) sq_free(exp->_nodes,exp->_nallocated * sizeof(SQRexNode));
523 if(exp->_jmpbuf) sq_free(exp->_jmpbuf,sizeof(jmp_buf));
524 if(exp->_matches) sq_free(exp->_matches,exp->_nsubexpr * sizeof(SQRexMatch));
525 sq_free(exp,sizeof(SQRex));
529 SQBool sqstd_rex_match(SQRex* exp,const SQChar* text)
531 const SQChar* res = NULL;
533 exp->_eol = text + scstrlen(text);
534 exp->_currsubexp = 0;
535 res = sqstd_rex_matchnode(exp,exp->_nodes,text);
536 if(res == NULL || res != exp->_eol)
541 SQBool sqstd_rex_searchrange(SQRex* exp,const SQChar* text_begin,const SQChar* text_end,const SQChar** out_begin, const SQChar** out_end)
543 const SQChar *cur = NULL;
544 int node = exp->_first;
545 if(text_begin >= text_end) return SQFalse;
546 exp->_bol = text_begin;
547 exp->_eol = text_end;
551 exp->_currsubexp = 0;
552 cur = sqstd_rex_matchnode(exp,&exp->_nodes[node],cur);
555 node = exp->_nodes[node].next;
558 } while(cur == NULL && text_begin != text_end);
565 if(out_begin) *out_begin = text_begin;
566 if(out_end) *out_end = cur;
570 SQBool sqstd_rex_search(SQRex* exp,const SQChar* text, const SQChar** out_begin, const SQChar** out_end)
572 return sqstd_rex_searchrange(exp,text,text + scstrlen(text),out_begin,out_end);
575 int sqstd_rex_getsubexpcount(SQRex* exp)
577 return exp->_nsubexpr;
580 SQBool sqstd_rex_getsubexp(SQRex* exp, int n, SQRexMatch *subexp)
582 if( n<0 || n >= exp->_nsubexpr) return SQFalse;
583 *subexp = exp->_matches[n];