PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/ham/src/luagsub.c

https://bitbucket.org/MartinRJ/emscripten_make
C | 415 lines | 347 code | 49 blank | 19 comment | 139 complexity | 6ec821cb3091ab042e862a34eddf4950 MD5 | raw file
  1. #include "jam.h"
  2. #include "buffer.h"
  3. #include <stdarg.h>
  4. #include <assert.h>
  5. #include <stddef.h>
  6. /* macro to `unsign' a character */
  7. #define uchar(c) ((unsigned char)(c))
  8. /*
  9. @@ LUA_MAXCAPTURES is the maximum number of captures that a pattern
  10. @* can do during pattern-matching.
  11. ** CHANGE it if you need more captures. This limit is arbitrary.
  12. */
  13. #define LUA_MAXCAPTURES 32
  14. /*
  15. @@ LUA_QL describes how error messages quote program elements.
  16. ** CHANGE it if you want a different appearance.
  17. */
  18. #define LUA_QL(x) "'" x "'"
  19. /*
  20. ** {======================================================
  21. ** PATTERN MATCHING
  22. ** =======================================================
  23. */
  24. #define CAP_UNFINISHED (-1)
  25. #define CAP_POSITION (-2)
  26. typedef struct MatchState {
  27. const char *src_init; /* init of source string */
  28. const char *src_end; /* end (`\0') of source string */
  29. int level; /* total number of captures (finished or unfinished) */
  30. struct {
  31. const char *init;
  32. ptrdiff_t len;
  33. } capture[LUA_MAXCAPTURES];
  34. BUFFER* buff;
  35. } MatchState;
  36. #define L_ESC '%'
  37. #define SPECIALS "^$*+?.([%-"
  38. static int gsub_error (const char *fmt) {
  39. printf("%s\n", fmt);
  40. exit(1);
  41. return 0;
  42. }
  43. static int check_capture (MatchState *ms, int l) {
  44. l -= '1';
  45. if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
  46. return gsub_error("invalid capture index");
  47. return l;
  48. }
  49. static int capture_to_close (MatchState *ms) {
  50. int level = ms->level;
  51. for (level--; level>=0; level--)
  52. if (ms->capture[level].len == CAP_UNFINISHED) return level;
  53. return gsub_error("invalid pattern capture");
  54. }
  55. static const char *classend (MatchState *ms, const char *p) {
  56. switch (*p++) {
  57. case L_ESC: {
  58. if (*p == '\0')
  59. gsub_error("malformed pattern (ends with " LUA_QL("%%") ")");
  60. return p+1;
  61. }
  62. case '[': {
  63. if (*p == '^') p++;
  64. do { /* look for a `]' */
  65. if (*p == '\0')
  66. gsub_error("malformed pattern (missing " LUA_QL("]") ")");
  67. if (*(p++) == L_ESC && *p != '\0')
  68. p++; /* skip escapes (e.g. `%]') */
  69. } while (*p != ']');
  70. return p+1;
  71. }
  72. default: {
  73. return p;
  74. }
  75. }
  76. }
  77. static int match_class (int c, int cl) {
  78. int res;
  79. switch (tolower(cl)) {
  80. case 'a' : res = isalpha(c); break;
  81. case 'c' : res = iscntrl(c); break;
  82. case 'd' : res = isdigit(c); break;
  83. case 'l' : res = islower(c); break;
  84. case 'p' : res = ispunct(c); break;
  85. case 's' : res = isspace(c); break;
  86. case 'u' : res = isupper(c); break;
  87. case 'w' : res = isalnum(c); break;
  88. case 'x' : res = isxdigit(c); break;
  89. case 'z' : res = (c == 0); break;
  90. default: return (cl == c);
  91. }
  92. return (islower(cl) ? res : !res);
  93. }
  94. static int matchbracketclass (int c, const char *p, const char *ec) {
  95. int sig = 1;
  96. if (*(p+1) == '^') {
  97. sig = 0;
  98. p++; /* skip the `^' */
  99. }
  100. while (++p < ec) {
  101. if (*p == L_ESC) {
  102. p++;
  103. if (match_class(c, uchar(*p)))
  104. return sig;
  105. }
  106. else if ((*(p+1) == '-') && (p+2 < ec)) {
  107. p+=2;
  108. if (uchar(*(p-2)) <= c && c <= uchar(*p))
  109. return sig;
  110. }
  111. else if (uchar(*p) == c) return sig;
  112. }
  113. return !sig;
  114. }
  115. static int singlematch (int c, const char *p, const char *ep) {
  116. switch (*p) {
  117. case '.': return 1; /* matches any char */
  118. case L_ESC: return match_class(c, uchar(*(p+1)));
  119. case '[': return matchbracketclass(c, p, ep-1);
  120. default: return (uchar(*p) == c);
  121. }
  122. }
  123. static const char *match (MatchState *ms, const char *s, const char *p);
  124. static const char *matchbalance (MatchState *ms, const char *s,
  125. const char *p) {
  126. if (*p == 0 || *(p+1) == 0)
  127. gsub_error("unbalanced pattern");
  128. if (*s != *p) return NULL;
  129. else {
  130. int b = *p;
  131. int e = *(p+1);
  132. int cont = 1;
  133. while (++s < ms->src_end) {
  134. if (*s == e) {
  135. if (--cont == 0) return s+1;
  136. }
  137. else if (*s == b) cont++;
  138. }
  139. }
  140. return NULL; /* string ends out of balance */
  141. }
  142. static const char *max_expand (MatchState *ms, const char *s,
  143. const char *p, const char *ep) {
  144. ptrdiff_t i = 0; /* counts maximum expand for item */
  145. while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
  146. i++;
  147. /* keeps trying to match with the maximum repetitions */
  148. while (i>=0) {
  149. const char *res = match(ms, (s+i), ep+1);
  150. if (res) return res;
  151. i--; /* else didn't match; reduce 1 repetition to try again */
  152. }
  153. return NULL;
  154. }
  155. static const char *min_expand (MatchState *ms, const char *s,
  156. const char *p, const char *ep) {
  157. for (;;) {
  158. const char *res = match(ms, s, ep+1);
  159. if (res != NULL)
  160. return res;
  161. else if (s<ms->src_end && singlematch(uchar(*s), p, ep))
  162. s++; /* try with one more repetition */
  163. else return NULL;
  164. }
  165. }
  166. static const char *start_capture (MatchState *ms, const char *s,
  167. const char *p, int what) {
  168. const char *res;
  169. int level = ms->level;
  170. if (level >= LUA_MAXCAPTURES) gsub_error("too many captures");
  171. ms->capture[level].init = s;
  172. ms->capture[level].len = what;
  173. ms->level = level+1;
  174. if ((res=match(ms, s, p)) == NULL) /* match failed? */
  175. ms->level--; /* undo capture */
  176. return res;
  177. }
  178. static const char *end_capture (MatchState *ms, const char *s,
  179. const char *p) {
  180. int l = capture_to_close(ms);
  181. const char *res;
  182. ms->capture[l].len = s - ms->capture[l].init; /* close capture */
  183. if ((res = match(ms, s, p)) == NULL) /* match failed? */
  184. ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
  185. return res;
  186. }
  187. static const char *match_capture (MatchState *ms, const char *s, int l) {
  188. size_t len;
  189. l = check_capture(ms, l);
  190. len = ms->capture[l].len;
  191. if ((size_t)(ms->src_end-s) >= len &&
  192. memcmp(ms->capture[l].init, s, len) == 0)
  193. return s+len;
  194. else return NULL;
  195. }
  196. static const char *match (MatchState *ms, const char *s, const char *p) {
  197. init: /* using goto's to optimize tail recursion */
  198. switch (*p) {
  199. case '(': { /* start capture */
  200. if (*(p+1) == ')') /* position capture? */
  201. return start_capture(ms, s, p+2, CAP_POSITION);
  202. else
  203. return start_capture(ms, s, p+1, CAP_UNFINISHED);
  204. }
  205. case ')': { /* end capture */
  206. return end_capture(ms, s, p+1);
  207. }
  208. case L_ESC: {
  209. switch (*(p+1)) {
  210. case 'b': { /* balanced string? */
  211. s = matchbalance(ms, s, p+2);
  212. if (s == NULL) return NULL;
  213. p+=4; goto init; /* else return match(ms, s, p+4); */
  214. }
  215. case 'f': { /* frontier? */
  216. const char *ep; char previous;
  217. p += 2;
  218. if (*p != '[')
  219. gsub_error("missing " LUA_QL("[") " after "
  220. LUA_QL("%%f") " in pattern");
  221. ep = classend(ms, p); /* points to what is next */
  222. previous = (s == ms->src_init) ? '\0' : *(s-1);
  223. if (matchbracketclass(uchar(previous), p, ep-1) ||
  224. !matchbracketclass(uchar(*s), p, ep-1)) return NULL;
  225. p=ep; goto init; /* else return match(ms, s, ep); */
  226. }
  227. default: {
  228. if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */
  229. s = match_capture(ms, s, uchar(*(p+1)));
  230. if (s == NULL) return NULL;
  231. p+=2; goto init; /* else return match(ms, s, p+2) */
  232. }
  233. goto dflt; /* case default */
  234. }
  235. }
  236. }
  237. case '\0': { /* end of pattern */
  238. return s; /* match succeeded */
  239. }
  240. case '$': {
  241. if (*(p+1) == '\0') /* is the `$' the last char in pattern? */
  242. return (s == ms->src_end) ? s : NULL; /* check end of string */
  243. else goto dflt;
  244. }
  245. default: dflt: { /* it is a pattern item */
  246. const char *ep = classend(ms, p); /* points to what is next */
  247. int m = s<ms->src_end && singlematch(uchar(*s), p, ep);
  248. switch (*ep) {
  249. case '?': { /* optional */
  250. const char *res;
  251. if (m && ((res=match(ms, s+1, ep+1)) != NULL))
  252. return res;
  253. p=ep+1; goto init; /* else return match(ms, s, ep+1); */
  254. }
  255. case '*': { /* 0 or more repetitions */
  256. return max_expand(ms, s, p, ep);
  257. }
  258. case '+': { /* 1 or more repetitions */
  259. return (m ? max_expand(ms, s+1, p, ep) : NULL);
  260. }
  261. case '-': { /* 0 or more repetitions (minimum) */
  262. return min_expand(ms, s, p, ep);
  263. }
  264. default: {
  265. if (!m) return NULL;
  266. s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
  267. }
  268. }
  269. }
  270. }
  271. }
  272. static const char *lmemfind (const char *s1, size_t l1,
  273. const char *s2, size_t l2) {
  274. if (l2 == 0) return s1; /* empty strings are everywhere */
  275. else if (l2 > l1) return NULL; /* avoids a negative `l1' */
  276. else {
  277. const char *init; /* to search for a `*s2' inside `s1' */
  278. l2--; /* 1st char will be checked by `memchr' */
  279. l1 = l1-l2; /* `s2' cannot be found after that */
  280. while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
  281. init++; /* 1st char is already checked */
  282. if (memcmp(init, s2+1, l2) == 0)
  283. return init-1;
  284. else { /* correct `l1' and `s1' to try again */
  285. l1 -= init-s1;
  286. s1 = init;
  287. }
  288. }
  289. return NULL; /* not found */
  290. }
  291. }
  292. static void push_onecapture (MatchState *ms, int i, const char *s,
  293. const char *e) {
  294. if (i >= ms->level) {
  295. if (i == 0) { /* ms->level == 0, too */
  296. buffer_addstring(ms->buff, s, e - s); /* add whole match */
  297. } else
  298. gsub_error("invalid capture index");
  299. }
  300. else {
  301. ptrdiff_t l = ms->capture[i].len;
  302. if (l == CAP_UNFINISHED) gsub_error("unfinished capture");
  303. if (l == CAP_POSITION) {
  304. // lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
  305. assert(0);
  306. } else
  307. buffer_addstring(ms->buff, ms->capture[i].init, l);
  308. }
  309. }
  310. static int push_captures (MatchState *ms, const char *s, const char *e) {
  311. int i;
  312. int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
  313. // luaL_checkstack(ms->L, nlevels, "too many captures");
  314. for (i = 0; i < nlevels; i++)
  315. push_onecapture(ms, i, s, e);
  316. return nlevels; /* number of strings pushed */
  317. }
  318. static void add_s (MatchState *ms, const char *s, const char *e, const char *news, size_t l) {
  319. size_t i;
  320. for (i = 0; i < l; i++) {
  321. if (news[i] != L_ESC) {
  322. buffer_addchar(ms->buff, news[i]);
  323. } else {
  324. i++; /* skip ESC */
  325. if (!isdigit(uchar(news[i]))) {
  326. buffer_addchar(ms->buff, news[i]);
  327. } else if (news[i] == '0') {
  328. buffer_addstring(ms->buff, s, e - s);
  329. } else {
  330. push_onecapture(ms, news[i] - '1', s, e);
  331. // luaL_addvalue(b); /* add capture to accumulated result */
  332. }
  333. }
  334. }
  335. }
  336. int str_gsub (BUFFER *buff, const char *src, const char *p, const char *repl, int max_s) {
  337. int anchor;
  338. int n = 0;
  339. MatchState ms;
  340. size_t srcl = strlen(src);
  341. if (max_s == -1)
  342. max_s = (int)(srcl + 1);
  343. anchor = (*p == '^') ? (p++, 1) : 0;
  344. n = 0;
  345. ms.buff = buff;
  346. ms.src_init = src;
  347. ms.src_end = src+srcl;
  348. while (n < max_s) {
  349. const char *e;
  350. ms.level = 0;
  351. e = match(&ms, src, p);
  352. if (e) {
  353. n++;
  354. add_s(&ms, src, e, repl, strlen(repl));
  355. }
  356. if (e && e>src) /* non empty match? */
  357. src = e; /* skip it */
  358. else if (src < ms.src_end) {
  359. buffer_addchar(ms.buff, *src++);
  360. } else break;
  361. if (anchor) break;
  362. }
  363. buffer_addstring(ms.buff, src, ms.src_end-src);
  364. buffer_addchar(ms.buff, 0);
  365. return n;
  366. }