sqstdrex.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /* see copyright notice in squirrel.h */
  2. #include <squirrel.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <setjmp.h>
  6. #include <sqstdstring.h>
  7. #ifdef _DEBUG
  8. #include <stdio.h>
  9. static const SQChar *g_nnames[] =
  10. {
  11. _SC("NONE"),_SC("OP_GREEDY"), _SC("OP_OR"),
  12. _SC("OP_EXPR"),_SC("OP_NOCAPEXPR"),_SC("OP_DOT"), _SC("OP_CLASS"),
  13. _SC("OP_CCLASS"),_SC("OP_NCLASS"),_SC("OP_RANGE"),_SC("OP_CHAR"),
  14. _SC("OP_EOL"),_SC("OP_BOL"),_SC("OP_WB"),_SC("OP_MB")
  15. };
  16. #endif
  17. #define OP_GREEDY (MAX_CHAR+1) // * + ? {n}
  18. #define OP_OR (MAX_CHAR+2)
  19. #define OP_EXPR (MAX_CHAR+3) //parentesis ()
  20. #define OP_NOCAPEXPR (MAX_CHAR+4) //parentesis (?:)
  21. #define OP_DOT (MAX_CHAR+5)
  22. #define OP_CLASS (MAX_CHAR+6)
  23. #define OP_CCLASS (MAX_CHAR+7)
  24. #define OP_NCLASS (MAX_CHAR+8) //negates class the [^
  25. #define OP_RANGE (MAX_CHAR+9)
  26. #define OP_CHAR (MAX_CHAR+10)
  27. #define OP_EOL (MAX_CHAR+11)
  28. #define OP_BOL (MAX_CHAR+12)
  29. #define OP_WB (MAX_CHAR+13)
  30. #define OP_MB (MAX_CHAR+14) //match balanced
  31. #define SQREX_SYMBOL_ANY_CHAR ('.')
  32. #define SQREX_SYMBOL_GREEDY_ONE_OR_MORE ('+')
  33. #define SQREX_SYMBOL_GREEDY_ZERO_OR_MORE ('*')
  34. #define SQREX_SYMBOL_GREEDY_ZERO_OR_ONE ('?')
  35. #define SQREX_SYMBOL_BRANCH ('|')
  36. #define SQREX_SYMBOL_END_OF_STRING ('$')
  37. #define SQREX_SYMBOL_BEGINNING_OF_STRING ('^')
  38. #define SQREX_SYMBOL_ESCAPE_CHAR ('\\')
  39. typedef int SQRexNodeType;
  40. typedef struct tagSQRexNode{
  41. SQRexNodeType type;
  42. SQInteger left;
  43. SQInteger right;
  44. SQInteger next;
  45. }SQRexNode;
  46. struct SQRex{
  47. const SQChar *_eol;
  48. const SQChar *_bol;
  49. const SQChar *_p;
  50. SQInteger _first;
  51. SQInteger _op;
  52. SQRexNode *_nodes;
  53. SQInteger _nallocated;
  54. SQInteger _nsize;
  55. SQInteger _nsubexpr;
  56. SQRexMatch *_matches;
  57. SQInteger _currsubexp;
  58. void *_jmpbuf;
  59. const SQChar **_error;
  60. };
  61. static SQInteger sqstd_rex_list(SQRex *exp);
  62. static SQInteger sqstd_rex_newnode(SQRex *exp, SQRexNodeType type)
  63. {
  64. SQRexNode n;
  65. n.type = type;
  66. n.next = n.right = n.left = -1;
  67. if(type == OP_EXPR)
  68. n.right = exp->_nsubexpr++;
  69. if(exp->_nallocated < (exp->_nsize + 1)) {
  70. SQInteger oldsize = exp->_nallocated;
  71. exp->_nallocated *= 2;
  72. exp->_nodes = (SQRexNode *)sq_realloc(exp->_nodes, oldsize * sizeof(SQRexNode) ,exp->_nallocated * sizeof(SQRexNode));
  73. }
  74. exp->_nodes[exp->_nsize++] = n;
  75. SQInteger newid = exp->_nsize - 1;
  76. return (SQInteger)newid;
  77. }
  78. static void sqstd_rex_error(SQRex *exp,const SQChar *error)
  79. {
  80. if(exp->_error) *exp->_error = error;
  81. longjmp(*((jmp_buf*)exp->_jmpbuf),-1);
  82. }
  83. static void sqstd_rex_expect(SQRex *exp, SQInteger n){
  84. if((*exp->_p) != n)
  85. sqstd_rex_error(exp, _SC("expected paren"));
  86. exp->_p++;
  87. }
  88. static SQChar sqstd_rex_escapechar(SQRex *exp)
  89. {
  90. if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR){
  91. exp->_p++;
  92. switch(*exp->_p) {
  93. case 'v': exp->_p++; return '\v';
  94. case 'n': exp->_p++; return '\n';
  95. case 't': exp->_p++; return '\t';
  96. case 'r': exp->_p++; return '\r';
  97. case 'f': exp->_p++; return '\f';
  98. default: return (*exp->_p++);
  99. }
  100. } else if(!scisprint(*exp->_p)) sqstd_rex_error(exp,_SC("letter expected"));
  101. return (*exp->_p++);
  102. }
  103. static SQInteger sqstd_rex_charclass(SQRex *exp,SQInteger classid)
  104. {
  105. SQInteger n = sqstd_rex_newnode(exp,OP_CCLASS);
  106. exp->_nodes[n].left = classid;
  107. return n;
  108. }
  109. static SQInteger sqstd_rex_charnode(SQRex *exp,SQBool isclass)
  110. {
  111. SQChar t;
  112. if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR) {
  113. exp->_p++;
  114. switch(*exp->_p) {
  115. case 'n': exp->_p++; return sqstd_rex_newnode(exp,'\n');
  116. case 't': exp->_p++; return sqstd_rex_newnode(exp,'\t');
  117. case 'r': exp->_p++; return sqstd_rex_newnode(exp,'\r');
  118. case 'f': exp->_p++; return sqstd_rex_newnode(exp,'\f');
  119. case 'v': exp->_p++; return sqstd_rex_newnode(exp,'\v');
  120. case 'a': case 'A': case 'w': case 'W': case 's': case 'S':
  121. case 'd': case 'D': case 'x': case 'X': case 'c': case 'C':
  122. case 'p': case 'P': case 'l': case 'u':
  123. {
  124. t = *exp->_p; exp->_p++;
  125. return sqstd_rex_charclass(exp,t);
  126. }
  127. case 'm':
  128. {
  129. SQChar cb, ce; //cb = character begin match ce = character end match
  130. cb = *++exp->_p; //skip 'm'
  131. ce = *++exp->_p;
  132. exp->_p++; //points to the next char to be parsed
  133. if ((!cb) || (!ce)) sqstd_rex_error(exp,_SC("balanced chars expected"));
  134. if ( cb == ce ) sqstd_rex_error(exp,_SC("open/close char can't be the same"));
  135. SQInteger node = sqstd_rex_newnode(exp,OP_MB);
  136. exp->_nodes[node].left = cb;
  137. exp->_nodes[node].right = ce;
  138. return node;
  139. }
  140. case 0:
  141. sqstd_rex_error(exp,_SC("letter expected for argument of escape sequence"));
  142. break;
  143. case 'b':
  144. case 'B':
  145. if(!isclass) {
  146. SQInteger node = sqstd_rex_newnode(exp,OP_WB);
  147. exp->_nodes[node].left = *exp->_p;
  148. exp->_p++;
  149. return node;
  150. } //else default
  151. default:
  152. t = *exp->_p; exp->_p++;
  153. return sqstd_rex_newnode(exp,t);
  154. }
  155. }
  156. else if(!scisprint(*exp->_p)) {
  157. sqstd_rex_error(exp,_SC("letter expected"));
  158. }
  159. t = *exp->_p; exp->_p++;
  160. return sqstd_rex_newnode(exp,t);
  161. }
  162. static SQInteger sqstd_rex_class(SQRex *exp)
  163. {
  164. SQInteger ret = -1;
  165. SQInteger first = -1,chain;
  166. if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING){
  167. ret = sqstd_rex_newnode(exp,OP_NCLASS);
  168. exp->_p++;
  169. }else ret = sqstd_rex_newnode(exp,OP_CLASS);
  170. if(*exp->_p == ']') sqstd_rex_error(exp,_SC("empty class"));
  171. chain = ret;
  172. while(*exp->_p != ']' && exp->_p != exp->_eol) {
  173. if(*exp->_p == '-' && first != -1){
  174. SQInteger r;
  175. if(*exp->_p++ == ']') sqstd_rex_error(exp,_SC("unfinished range"));
  176. r = sqstd_rex_newnode(exp,OP_RANGE);
  177. if(exp->_nodes[first].type>*exp->_p) sqstd_rex_error(exp,_SC("invalid range"));
  178. if(exp->_nodes[first].type == OP_CCLASS) sqstd_rex_error(exp,_SC("cannot use character classes in ranges"));
  179. exp->_nodes[r].left = exp->_nodes[first].type;
  180. SQInteger t = sqstd_rex_escapechar(exp);
  181. exp->_nodes[r].right = t;
  182. exp->_nodes[chain].next = r;
  183. chain = r;
  184. first = -1;
  185. }
  186. else{
  187. if(first!=-1){
  188. SQInteger c = first;
  189. exp->_nodes[chain].next = c;
  190. chain = c;
  191. first = sqstd_rex_charnode(exp,SQTrue);
  192. }
  193. else{
  194. first = sqstd_rex_charnode(exp,SQTrue);
  195. }
  196. }
  197. }
  198. if(first!=-1){
  199. SQInteger c = first;
  200. exp->_nodes[chain].next = c;
  201. }
  202. /* hack? */
  203. exp->_nodes[ret].left = exp->_nodes[ret].next;
  204. exp->_nodes[ret].next = -1;
  205. return ret;
  206. }
  207. static SQInteger sqstd_rex_parsenumber(SQRex *exp)
  208. {
  209. SQInteger ret = *exp->_p-'0';
  210. SQInteger positions = 10;
  211. exp->_p++;
  212. while(isdigit(*exp->_p)) {
  213. ret = ret*10+(*exp->_p++-'0');
  214. if(positions==1000000000) sqstd_rex_error(exp,_SC("overflow in numeric constant"));
  215. positions *= 10;
  216. };
  217. return ret;
  218. }
  219. static SQInteger sqstd_rex_element(SQRex *exp)
  220. {
  221. SQInteger ret = -1;
  222. switch(*exp->_p)
  223. {
  224. case '(': {
  225. SQInteger expr;
  226. exp->_p++;
  227. if(*exp->_p =='?') {
  228. exp->_p++;
  229. sqstd_rex_expect(exp,':');
  230. expr = sqstd_rex_newnode(exp,OP_NOCAPEXPR);
  231. }
  232. else
  233. expr = sqstd_rex_newnode(exp,OP_EXPR);
  234. SQInteger newn = sqstd_rex_list(exp);
  235. exp->_nodes[expr].left = newn;
  236. ret = expr;
  237. sqstd_rex_expect(exp,')');
  238. }
  239. break;
  240. case '[':
  241. exp->_p++;
  242. ret = sqstd_rex_class(exp);
  243. sqstd_rex_expect(exp,']');
  244. break;
  245. case SQREX_SYMBOL_END_OF_STRING: exp->_p++; ret = sqstd_rex_newnode(exp,OP_EOL);break;
  246. case SQREX_SYMBOL_ANY_CHAR: exp->_p++; ret = sqstd_rex_newnode(exp,OP_DOT);break;
  247. default:
  248. ret = sqstd_rex_charnode(exp,SQFalse);
  249. break;
  250. }
  251. SQBool isgreedy = SQFalse;
  252. unsigned short p0 = 0, p1 = 0;
  253. switch(*exp->_p){
  254. case SQREX_SYMBOL_GREEDY_ZERO_OR_MORE: p0 = 0; p1 = 0xFFFF; exp->_p++; isgreedy = SQTrue; break;
  255. case SQREX_SYMBOL_GREEDY_ONE_OR_MORE: p0 = 1; p1 = 0xFFFF; exp->_p++; isgreedy = SQTrue; break;
  256. case SQREX_SYMBOL_GREEDY_ZERO_OR_ONE: p0 = 0; p1 = 1; exp->_p++; isgreedy = SQTrue; break;
  257. case '{':
  258. exp->_p++;
  259. if(!isdigit(*exp->_p)) sqstd_rex_error(exp,_SC("number expected"));
  260. p0 = (unsigned short)sqstd_rex_parsenumber(exp);
  261. /*******************************/
  262. switch(*exp->_p) {
  263. case '}':
  264. p1 = p0; exp->_p++;
  265. break;
  266. case ',':
  267. exp->_p++;
  268. p1 = 0xFFFF;
  269. if(isdigit(*exp->_p)){
  270. p1 = (unsigned short)sqstd_rex_parsenumber(exp);
  271. }
  272. sqstd_rex_expect(exp,'}');
  273. break;
  274. default:
  275. sqstd_rex_error(exp,_SC(", or } expected"));
  276. }
  277. /*******************************/
  278. isgreedy = SQTrue;
  279. break;
  280. }
  281. if(isgreedy) {
  282. SQInteger nnode = sqstd_rex_newnode(exp,OP_GREEDY);
  283. exp->_nodes[nnode].left = ret;
  284. exp->_nodes[nnode].right = ((p0)<<16)|p1;
  285. ret = nnode;
  286. }
  287. 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')) {
  288. SQInteger nnode = sqstd_rex_element(exp);
  289. exp->_nodes[ret].next = nnode;
  290. }
  291. return ret;
  292. }
  293. static SQInteger sqstd_rex_list(SQRex *exp)
  294. {
  295. SQInteger ret=-1,e;
  296. if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING) {
  297. exp->_p++;
  298. ret = sqstd_rex_newnode(exp,OP_BOL);
  299. }
  300. e = sqstd_rex_element(exp);
  301. if(ret != -1) {
  302. exp->_nodes[ret].next = e;
  303. }
  304. else ret = e;
  305. if(*exp->_p == SQREX_SYMBOL_BRANCH) {
  306. SQInteger temp,tright;
  307. exp->_p++;
  308. temp = sqstd_rex_newnode(exp,OP_OR);
  309. exp->_nodes[temp].left = ret;
  310. tright = sqstd_rex_list(exp);
  311. exp->_nodes[temp].right = tright;
  312. ret = temp;
  313. }
  314. return ret;
  315. }
  316. static SQBool sqstd_rex_matchcclass(SQInteger cclass,SQChar c)
  317. {
  318. switch(cclass) {
  319. case 'a': return isalpha(c)?SQTrue:SQFalse;
  320. case 'A': return !isalpha(c)?SQTrue:SQFalse;
  321. case 'w': return (isalnum(c) || c == '_')?SQTrue:SQFalse;
  322. case 'W': return (!isalnum(c) && c != '_')?SQTrue:SQFalse;
  323. case 's': return isspace(c)?SQTrue:SQFalse;
  324. case 'S': return !isspace(c)?SQTrue:SQFalse;
  325. case 'd': return isdigit(c)?SQTrue:SQFalse;
  326. case 'D': return !isdigit(c)?SQTrue:SQFalse;
  327. case 'x': return isxdigit(c)?SQTrue:SQFalse;
  328. case 'X': return !isxdigit(c)?SQTrue:SQFalse;
  329. case 'c': return iscntrl(c)?SQTrue:SQFalse;
  330. case 'C': return !iscntrl(c)?SQTrue:SQFalse;
  331. case 'p': return ispunct(c)?SQTrue:SQFalse;
  332. case 'P': return !ispunct(c)?SQTrue:SQFalse;
  333. case 'l': return islower(c)?SQTrue:SQFalse;
  334. case 'u': return isupper(c)?SQTrue:SQFalse;
  335. }
  336. return SQFalse; /*cannot happen*/
  337. }
  338. static SQBool sqstd_rex_matchclass(SQRex* exp,SQRexNode *node,SQChar c)
  339. {
  340. do {
  341. switch(node->type) {
  342. case OP_RANGE:
  343. if(c >= node->left && c <= node->right) return SQTrue;
  344. break;
  345. case OP_CCLASS:
  346. if(sqstd_rex_matchcclass(node->left,c)) return SQTrue;
  347. break;
  348. default:
  349. if(c == node->type)return SQTrue;
  350. }
  351. } while((node->next != -1) && (node = &exp->_nodes[node->next]));
  352. return SQFalse;
  353. }
  354. static const SQChar *sqstd_rex_matchnode(SQRex* exp,SQRexNode *node,const SQChar *str,SQRexNode *next)
  355. {
  356. SQRexNodeType type = node->type;
  357. switch(type) {
  358. case OP_GREEDY: {
  359. //SQRexNode *greedystop = (node->next != -1) ? &exp->_nodes[node->next] : NULL;
  360. SQRexNode *greedystop = NULL;
  361. SQInteger p0 = (node->right >> 16)&0x0000FFFF, p1 = node->right&0x0000FFFF, nmaches = 0;
  362. const SQChar *s=str, *good = str;
  363. if(node->next != -1) {
  364. greedystop = &exp->_nodes[node->next];
  365. }
  366. else {
  367. greedystop = next;
  368. }
  369. while((nmaches == 0xFFFF || nmaches < p1)) {
  370. const SQChar *stop;
  371. if(!(s = sqstd_rex_matchnode(exp,&exp->_nodes[node->left],s,greedystop)))
  372. break;
  373. nmaches++;
  374. good=s;
  375. if(greedystop) {
  376. //checks that 0 matches satisfy the expression(if so skips)
  377. //if not would always stop(for instance if is a '?')
  378. if(greedystop->type != OP_GREEDY ||
  379. (greedystop->type == OP_GREEDY && ((greedystop->right >> 16)&0x0000FFFF) != 0))
  380. {
  381. SQRexNode *gnext = NULL;
  382. if(greedystop->next != -1) {
  383. gnext = &exp->_nodes[greedystop->next];
  384. }else if(next && next->next != -1){
  385. gnext = &exp->_nodes[next->next];
  386. }
  387. stop = sqstd_rex_matchnode(exp,greedystop,s,gnext);
  388. if(stop) {
  389. //if satisfied stop it
  390. if(p0 == p1 && p0 == nmaches) break;
  391. else if(nmaches >= p0 && p1 == 0xFFFF) break;
  392. else if(nmaches >= p0 && nmaches <= p1) break;
  393. }
  394. }
  395. }
  396. if(s >= exp->_eol)
  397. break;
  398. }
  399. if(p0 == p1 && p0 == nmaches) return good;
  400. else if(nmaches >= p0 && p1 == 0xFFFF) return good;
  401. else if(nmaches >= p0 && nmaches <= p1) return good;
  402. return NULL;
  403. }
  404. case OP_OR: {
  405. const SQChar *asd = str;
  406. SQRexNode *temp=&exp->_nodes[node->left];
  407. while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) {
  408. if(temp->next != -1)
  409. temp = &exp->_nodes[temp->next];
  410. else
  411. return asd;
  412. }
  413. asd = str;
  414. temp = &exp->_nodes[node->right];
  415. while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) {
  416. if(temp->next != -1)
  417. temp = &exp->_nodes[temp->next];
  418. else
  419. return asd;
  420. }
  421. return NULL;
  422. break;
  423. }
  424. case OP_EXPR:
  425. case OP_NOCAPEXPR:{
  426. SQRexNode *n = &exp->_nodes[node->left];
  427. const SQChar *cur = str;
  428. SQInteger capture = -1;
  429. if(node->type != OP_NOCAPEXPR && node->right == exp->_currsubexp) {
  430. capture = exp->_currsubexp;
  431. exp->_matches[capture].begin = cur;
  432. exp->_currsubexp++;
  433. }
  434. SQInteger tempcap = exp->_currsubexp;
  435. do {
  436. SQRexNode *subnext = NULL;
  437. if(n->next != -1) {
  438. subnext = &exp->_nodes[n->next];
  439. }else {
  440. subnext = next;
  441. }
  442. if(!(cur = sqstd_rex_matchnode(exp,n,cur,subnext))) {
  443. if(capture != -1){
  444. exp->_matches[capture].begin = 0;
  445. exp->_matches[capture].len = 0;
  446. }
  447. return NULL;
  448. }
  449. } while((n->next != -1) && (n = &exp->_nodes[n->next]));
  450. exp->_currsubexp = tempcap;
  451. if(capture != -1)
  452. exp->_matches[capture].len = cur - exp->_matches[capture].begin;
  453. return cur;
  454. }
  455. case OP_WB:
  456. if((str == exp->_bol && !isspace(*str))
  457. || (str == exp->_eol && !isspace(*(str-1)))
  458. || (!isspace(*str) && isspace(*(str+1)))
  459. || (isspace(*str) && !isspace(*(str+1))) ) {
  460. return (node->left == 'b')?str:NULL;
  461. }
  462. return (node->left == 'b')?NULL:str;
  463. case OP_BOL:
  464. if(str == exp->_bol) return str;
  465. return NULL;
  466. case OP_EOL:
  467. if(str == exp->_eol) return str;
  468. return NULL;
  469. case OP_DOT:{
  470. if (str == exp->_eol) return NULL;
  471. str++;
  472. }
  473. return str;
  474. case OP_NCLASS:
  475. case OP_CLASS:
  476. if (str == exp->_eol) return NULL;
  477. if(sqstd_rex_matchclass(exp,&exp->_nodes[node->left],*str)?(type == OP_CLASS?SQTrue:SQFalse):(type == OP_NCLASS?SQTrue:SQFalse)) {
  478. str++;
  479. return str;
  480. }
  481. return NULL;
  482. case OP_CCLASS:
  483. if (str == exp->_eol) return NULL;
  484. if(sqstd_rex_matchcclass(node->left,*str)) {
  485. str++;
  486. return str;
  487. }
  488. return NULL;
  489. case OP_MB:
  490. {
  491. SQInteger cb = node->left; //char that opens a balanced expression
  492. if(*str != cb) return NULL; // string doesnt start with open char
  493. SQInteger ce = node->right; //char that closes a balanced expression
  494. SQInteger cont = 1;
  495. const SQChar *streol = exp->_eol;
  496. while (++str < streol) {
  497. if (*str == ce) {
  498. if (--cont == 0) {
  499. return ++str;
  500. }
  501. }
  502. else if (*str == cb) cont++;
  503. }
  504. }
  505. return NULL; // string ends out of balance
  506. default: /* char */
  507. if (str == exp->_eol) return NULL;
  508. if(*str != node->type) return NULL;
  509. str++;
  510. return str;
  511. }
  512. return NULL;
  513. }
  514. /* public api */
  515. SQRex *sqstd_rex_compile(const SQChar *pattern,const SQChar **error)
  516. {
  517. SQRex * volatile exp = (SQRex *)sq_malloc(sizeof(SQRex)); // "volatile" is needed for setjmp()
  518. exp->_eol = exp->_bol = NULL;
  519. exp->_p = pattern;
  520. exp->_nallocated = (SQInteger)scstrlen(pattern) * sizeof(SQChar);
  521. exp->_nodes = (SQRexNode *)sq_malloc(exp->_nallocated * sizeof(SQRexNode));
  522. exp->_nsize = 0;
  523. exp->_matches = 0;
  524. exp->_nsubexpr = 0;
  525. exp->_first = sqstd_rex_newnode(exp,OP_EXPR);
  526. exp->_error = error;
  527. exp->_jmpbuf = sq_malloc(sizeof(jmp_buf));
  528. if(setjmp(*((jmp_buf*)exp->_jmpbuf)) == 0) {
  529. SQInteger res = sqstd_rex_list(exp);
  530. exp->_nodes[exp->_first].left = res;
  531. if(*exp->_p!='\0')
  532. sqstd_rex_error(exp,_SC("unexpected character"));
  533. #ifdef _DEBUG
  534. {
  535. SQInteger nsize,i;
  536. SQRexNode *t;
  537. nsize = exp->_nsize;
  538. t = &exp->_nodes[0];
  539. scprintf(_SC("\n"));
  540. for(i = 0;i < nsize; i++) {
  541. if(exp->_nodes[i].type>MAX_CHAR)
  542. scprintf(_SC("[%02d] %10s "), (SQInt32)i,g_nnames[exp->_nodes[i].type-MAX_CHAR]);
  543. else
  544. scprintf(_SC("[%02d] %10c "), (SQInt32)i,exp->_nodes[i].type);
  545. scprintf(_SC("left %02d right %02d next %02d\n"), (SQInt32)exp->_nodes[i].left, (SQInt32)exp->_nodes[i].right, (SQInt32)exp->_nodes[i].next);
  546. }
  547. scprintf(_SC("\n"));
  548. }
  549. #endif
  550. exp->_matches = (SQRexMatch *) sq_malloc(exp->_nsubexpr * sizeof(SQRexMatch));
  551. memset(exp->_matches,0,exp->_nsubexpr * sizeof(SQRexMatch));
  552. }
  553. else{
  554. sqstd_rex_free(exp);
  555. return NULL;
  556. }
  557. return exp;
  558. }
  559. void sqstd_rex_free(SQRex *exp)
  560. {
  561. if(exp) {
  562. if(exp->_nodes) sq_free(exp->_nodes,exp->_nallocated * sizeof(SQRexNode));
  563. if(exp->_jmpbuf) sq_free(exp->_jmpbuf,sizeof(jmp_buf));
  564. if(exp->_matches) sq_free(exp->_matches,exp->_nsubexpr * sizeof(SQRexMatch));
  565. sq_free(exp,sizeof(SQRex));
  566. }
  567. }
  568. SQBool sqstd_rex_match(SQRex* exp,const SQChar* text)
  569. {
  570. const SQChar* res = NULL;
  571. exp->_bol = text;
  572. exp->_eol = text + scstrlen(text);
  573. exp->_currsubexp = 0;
  574. res = sqstd_rex_matchnode(exp,exp->_nodes,text,NULL);
  575. if(res == NULL || res != exp->_eol)
  576. return SQFalse;
  577. return SQTrue;
  578. }
  579. SQBool sqstd_rex_searchrange(SQRex* exp,const SQChar* text_begin,const SQChar* text_end,const SQChar** out_begin, const SQChar** out_end)
  580. {
  581. const SQChar *cur = NULL;
  582. SQInteger node = exp->_first;
  583. if(text_begin >= text_end) return SQFalse;
  584. exp->_bol = text_begin;
  585. exp->_eol = text_end;
  586. do {
  587. cur = text_begin;
  588. while(node != -1) {
  589. exp->_currsubexp = 0;
  590. cur = sqstd_rex_matchnode(exp,&exp->_nodes[node],cur,NULL);
  591. if(!cur)
  592. break;
  593. node = exp->_nodes[node].next;
  594. }
  595. text_begin++;
  596. } while(cur == NULL && text_begin != text_end);
  597. if(cur == NULL)
  598. return SQFalse;
  599. --text_begin;
  600. if(out_begin) *out_begin = text_begin;
  601. if(out_end) *out_end = cur;
  602. return SQTrue;
  603. }
  604. SQBool sqstd_rex_search(SQRex* exp,const SQChar* text, const SQChar** out_begin, const SQChar** out_end)
  605. {
  606. return sqstd_rex_searchrange(exp,text,text + scstrlen(text),out_begin,out_end);
  607. }
  608. SQInteger sqstd_rex_getsubexpcount(SQRex* exp)
  609. {
  610. return exp->_nsubexpr;
  611. }
  612. SQBool sqstd_rex_getsubexp(SQRex* exp, SQInteger n, SQRexMatch *subexp)
  613. {
  614. if( n<0 || n >= exp->_nsubexpr) return SQFalse;
  615. *subexp = exp->_matches[n];
  616. return SQTrue;
  617. }