summaryrefslogtreecommitdiffstatshomepage
path: root/extmod/re1.5/recursiveloop.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-10-11 18:55:44 +0100
committerDamien George <damien.p.george@gmail.com>2014-10-11 18:55:44 +0100
commit37671c9a973d20248048343cb168c50f6bbca959 (patch)
tree1bac386c924a0a8bd289bd4a36f3af9974283a33 /extmod/re1.5/recursiveloop.c
parent1ce916aefdaee7bca2bcf2bc4ee7e1eb75002f67 (diff)
parentdd5ee9ff9c67575897ccfa8b4e8b10abecbe9800 (diff)
downloadmicropython-37671c9a973d20248048343cb168c50f6bbca959.tar.gz
micropython-37671c9a973d20248048343cb168c50f6bbca959.zip
Merge branch 'pfalcon-modure'
Diffstat (limited to 'extmod/re1.5/recursiveloop.c')
-rw-r--r--extmod/re1.5/recursiveloop.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/extmod/re1.5/recursiveloop.c b/extmod/re1.5/recursiveloop.c
new file mode 100644
index 0000000000..7b95eb4c95
--- /dev/null
+++ b/extmod/re1.5/recursiveloop.c
@@ -0,0 +1,71 @@
+// Copyright 2007-2009 Russ Cox. All Rights Reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include "regexp.h"
+
+static int
+recursiveloop(char *pc, const char *sp, Subject *input, const char **subp, int nsubp)
+{
+ const char *old;
+ int off;
+
+ for(;;) {
+ if(inst_is_consumer(*pc)) {
+ // If we need to match a character, but there's none left, it's fail
+ if(sp >= input->end)
+ return 0;
+ }
+ switch(*pc++) {
+ case Char:
+ if(*sp != *pc++)
+ return 0;
+ case Any:
+ sp++;
+ continue;
+ case Match:
+ return 1;
+ case Jmp:
+ off = (signed char)*pc++;
+ pc = pc + off;
+ continue;
+ case Split:
+ off = (signed char)*pc++;
+ if(recursiveloop(pc, sp, input, subp, nsubp))
+ return 1;
+ pc = pc + off;
+ continue;
+ case RSplit:
+ off = (signed char)*pc++;
+ if(recursiveloop(pc + off, sp, input, subp, nsubp))
+ return 1;
+ continue;
+ case Save:
+ off = (unsigned char)*pc++;
+ if(off >= nsubp) {
+ continue;
+ }
+ old = subp[off];
+ subp[off] = sp;
+ if(recursiveloop(pc, sp, input, subp, nsubp))
+ return 1;
+ subp[off] = old;
+ return 0;
+ case Bol:
+ if(sp != input->begin)
+ return 0;
+ continue;
+ case Eol:
+ if(sp != input->end)
+ return 0;
+ continue;
+ }
+ re1_5_fatal("recursiveloop");
+ }
+}
+
+int
+re1_5_recursiveloopprog(ByteProg *prog, Subject *input, const char **subp, int nsubp, int is_anchored)
+{
+ return recursiveloop(HANDLE_ANCHORED(prog->insts, is_anchored), input->begin, input, subp, nsubp);
+}