Home | History | Annotate | Download | only in src

Lines Matching defs:ms

213 static const char *match (MatchState *ms, const char *s, const char *p);
226 static int check_capture (MatchState *ms, int l) {
228 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
229 return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
234 static int capture_to_close (MatchState *ms) {
235 int level = ms->level;
237 if (ms->capture[level].len == CAP_UNFINISHED) return level;
238 return luaL_error(ms->L, "invalid pattern capture");
242 static const char *classend (MatchState *ms, const char *p) {
245 if (p == ms->p_end)
246 luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");
252 if (p == ms->p_end)
253 luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");
254 if (*(p++) == L_ESC && p < ms->p_end)
309 static int singlematch (MatchState *ms, const char *s, const char *p,
311 if (s >= ms->src_end)
325 static const char *matchbalance (MatchState *ms, const char *s,
327 if (p >= ms->p_end - 1)
328 luaL_error(ms->L, "malformed pattern "
335 while (++s < ms->src_end) {
346 static const char *max_expand (MatchState *ms, const char *s,
349 while (singlematch(ms, s + i, p, ep))
353 const char *res = match(ms, (s+i), ep+1);
361 static const char *min_expand (MatchState *ms, const char *s,
364 const char *res = match(ms, s, ep+1);
367 else if (singlematch(ms, s, p, ep))
374 static const char *start_capture (MatchState *ms, const char *s,
377 int level = ms->level;
378 if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
379 ms->capture[level].init = s;
380 ms->capture[level].len = what;
381 ms->level = level+1;
382 if ((res=match(ms, s, p)) == NULL) /* match failed? */
383 ms->level--; /* undo capture */
388 static const char *end_capture (MatchState *ms, const char *s,
390 int l = capture_to_close(ms);
392 ms->capture[l].len = s - ms->capture[l].init; /* close capture */
393 if ((res = match(ms, s, p)) == NULL) /* match failed? */
394 ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
399 static const char *match_capture (MatchState *ms, const char *s, int l) {
401 l = check_capture(ms, l);
402 len = ms->capture[l].len;
403 if ((size_t)(ms->src_end-s) >= len &&
404 memcmp(ms->capture[l].init, s, len) == 0)
410 static const char *match (MatchState *ms, const char *s, const char *p) {
411 if (ms->matchdepth-- == 0)
412 luaL_error(ms->L, "pattern too complex");
414 if (p != ms->p_end) { /* end of pattern? */
418 s = start_capture(ms, s, p + 2, CAP_POSITION);
420 s = start_capture(ms, s, p + 1, CAP_UNFINISHED);
424 s = end_capture(ms, s, p + 1);
428 if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */
430 s = (s == ms->src_end) ? s : NULL; /* check end of string */
436 s = matchbalance(ms, s, p + 2);
438 p += 4; goto init; /* return match(ms, s, p + 4); */
446 luaL_error(ms->L, "missing " LUA_QL("[") " after "
448 ep = classend(ms, p); /* points to what is next */
449 previous = (s == ms->src_init) ? '\0' : *(s - 1);
452 p = ep; goto init; /* return match(ms, s, ep); */
460 s = match_capture(ms, s, uchar(*(p + 1)));
462 p += 2; goto init; /* return match(ms, s, p + 2) */
471 const char *ep = classend(ms, p); /* points to optional suffix */
473 if (!singlematch(ms, s, p, ep)) {
475 p = ep + 1; goto init; /* return match(ms, s, ep + 1); */
484 if ((res = match(ms, s + 1, ep + 1)) != NULL)
487 p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */
495 s = max_expand(ms, s, p, ep);
498 s = min_expand(ms, s, p, ep);
501 s++; p = ep; goto init; /* return match(ms, s + 1, ep); */
508 ms->matchdepth++;
536 static void push_onecapture (MatchState *ms, int i, const char *s,
538 if (i >= ms->level) {
539 if (i == 0) /* ms->level == 0, too */
540 lua_pushlstring(ms->L, s, e - s); /* add whole match */
542 luaL_error(ms->L, "invalid capture index");
545 ptrdiff_t l = ms->capture[i].len;
546 if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
548 lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
550 lua_pushlstring(ms->L, ms->capture[i].init, l);
555 static int push_captures (MatchState *ms, const char *s, const char *e) {
557 int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
558 luaL_checkstack(ms->L, nlevels, "too many captures");
560 push_onecapture(ms, i, s, e);
598 MatchState ms;
604 ms.L = L;
605 ms.matchdepth = MAXCCALLS;
606 ms.src_init = s;
607 ms.src_end = s + ls;
608 ms.p_end = p + lp;
611 ms.level = 0;
612 lua_assert(ms.matchdepth == MAXCCALLS);
613 if ((res=match(&ms, s1, p)) != NULL) {
617 return push_captures(&ms, NULL, 0) + 2;
620 return push_captures(&ms, s1, res);
622 } while (s1++ < ms.src_end && !anchor);
640 MatchState ms;
645 ms.L = L;
646 ms.matchdepth = MAXCCALLS;
647 ms.src_init = s;
648 ms.src_end = s+ls;
649 ms.p_end = p + lp;
651 src <= ms.src_end;
654 ms.level = 0;
655 lua_assert(ms.matchdepth == MAXCCALLS);
656 if ((e = match(&ms, src, p)) != NULL) {
661 return push_captures(&ms, src, e);
678 static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
681 const char *news = lua_tolstring(ms->L, 3, &l);
689 luaL_error(ms->L, "invalid use of " LUA_QL("%c")
696 push_onecapture(ms, news[i] - '1', s, e);
704 static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
706 lua_State *L = ms->L;
711 n = push_captures(ms, s, e);
716 push_onecapture(ms, 0, s, e);
721 add_s(ms, b, s, e);
743 MatchState ms;
752 ms.L = L;
753 ms.matchdepth = MAXCCALLS;
754 ms.src_init = src;
755 ms.src_end = src+srcl;
756 ms.p_end = p + lp;
759 ms.level = 0;
760 lua_assert(ms.matchdepth == MAXCCALLS);
761 e = match(&ms, src, p);
764 add_value(&ms, &b, src, e, tr);
768 else if (src < ms.src_end)
773 luaL_addlstring(&b, src, ms.src_end-src);