[pypy-svn] r16498 - pypy/dist/pypy/module/_sre

nik at codespeak.net nik at codespeak.net
Thu Aug 25 16:21:38 CEST 2005


Author: nik
Date: Thu Aug 25 16:21:37 2005
New Revision: 16498

Modified:
   pypy/dist/pypy/module/_sre/app_sre.py
   pypy/dist/pypy/module/_sre/interp_sre.py
Log:
some refactorings to clear up confusion about which objects are
wrapped and which are not.


Modified: pypy/dist/pypy/module/_sre/app_sre.py
==============================================================================
--- pypy/dist/pypy/module/_sre/app_sre.py	(original)
+++ pypy/dist/pypy/module/_sre/app_sre.py	Thu Aug 25 16:21:37 2005
@@ -369,6 +369,10 @@
         string_position += 1
     return False
 
+# XXX temporary constants for MatchContext.has_matched
+UNDECIDED = 0
+MATCHED = 1
+NOT_MATCHED = 2
 
 def match(state, pattern_codes):
     # Optimization: Check string length. pattern_codes[3] contains the
@@ -381,13 +385,13 @@
     
     dispatcher = _OpcodeDispatcher()
     state.context_stack.append(_sre._MatchContext(state, pattern_codes))
-    has_matched = None
+    has_matched = UNDECIDED
     while len(state.context_stack) > 0:
         context = state.context_stack[-1]
         has_matched = dispatcher.match(context)
-        if has_matched is not None: # don't pop if context isn't done
+        if has_matched != UNDECIDED: # don't pop if context isn't done
             state.context_stack.pop()
-    return has_matched
+    return has_matched == MATCHED
 
 
 class _Dispatcher(object):
@@ -422,12 +426,12 @@
         """Returns True if the current context matches, False if it doesn't and
         None if matching is not finished, ie must be resumed after child
         contexts have been matched."""
-        while context.remaining_codes() > 0 and context.has_matched is None:
+        while context.remaining_codes() > 0 and context.has_matched == UNDECIDED:
             opcode = context.peek_code()
             if not self.dispatch(opcode, context):
-                return None
-        if context.has_matched is None:
-            context.has_matched = False
+                return UNDECIDED
+        if context.has_matched == UNDECIDED:
+            context.has_matched = NOT_MATCHED
         return context.has_matched
 
     def dispatch(self, opcode, context):
@@ -453,7 +457,7 @@
     def general_op_literal(self, ctx, compare, decorate=lambda x: x):
         if ctx.at_end() or not compare(decorate(ord(ctx.peek_char())),
                                             decorate(ctx.peek_code(1))):
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
         ctx.skip_code(2)
         ctx.skip_char(1)
 
@@ -491,7 +495,7 @@
         #self._log(ctx, "AT", ctx.peek_code(1))
         if not _sre._at_dispatch(ctx.peek_code(1), ctx.state.string,
                                             ctx.string_position, ctx.state.end):
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
             return True
         ctx.skip_code(2)
         return True
@@ -502,7 +506,7 @@
         #self._log(ctx, "CATEGORY", ctx.peek_code(1))
         if ctx.at_end() or \
                  not _sre._category_dispatch(ctx.peek_code(1), ctx.peek_char()):
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
             return True
         ctx.skip_code(2)
         ctx.skip_char(1)
@@ -513,7 +517,7 @@
         # <ANY>
         #self._log(ctx, "ANY")
         if ctx.at_end() or ctx.at_linebreak():
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
             return True
         ctx.skip_code(1)
         ctx.skip_char(1)
@@ -524,7 +528,7 @@
         # <ANY_ALL>
         #self._log(ctx, "ANY_ALL")
         if ctx.at_end():
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
             return True
         ctx.skip_code(1)
         ctx.skip_char(1)
@@ -533,14 +537,14 @@
     def general_op_in(self, ctx, decorate=lambda x: x):
         #self._log(ctx, "OP_IN")
         if ctx.at_end():
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
             return
         skip = ctx.peek_code(1)
         ctx.skip_code(2) # set op pointer to the set code
         char = decorate(ord(ctx.peek_char()))
         if not _sre._check_charset(ctx.pattern_codes[ctx.code_position:], char,
                                          ctx.state.string, ctx.string_position):
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
             return
         ctx.skip_code(skip - 1)
         ctx.skip_char(1)
@@ -593,14 +597,14 @@
                 ctx.state.string_position = ctx.string_position
                 child_context = ctx.push_new_context(1)
                 yield False
-                if child_context.has_matched:
-                    ctx.has_matched = True
+                if child_context.has_matched == MATCHED:
+                    ctx.has_matched = MATCHED
                     yield True
                 ctx.state.marks_pop_keep()
             ctx.skip_code(current_branch_length)
             current_branch_length = ctx.peek_code(0)
         ctx.state.marks_pop_discard()
-        ctx.has_matched = False
+        ctx.has_matched = NOT_MATCHED
         yield True
 
     def op_repeat_one(self, ctx):
@@ -613,18 +617,18 @@
         #self._log(ctx, "REPEAT_ONE", mincount, maxcount)
 
         if ctx.remaining_chars() < mincount:
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
             yield True
         ctx.state.string_position = ctx.string_position
         count = self.count_repetitions(ctx, maxcount)
         ctx.skip_char(count)
         if count < mincount:
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
             yield True
         if ctx.peek_code(ctx.peek_code(1) + 1) == OPCODES["success"]:
             # tail is empty.  we're finished
             ctx.state.string_position = ctx.string_position
-            ctx.has_matched = True
+            ctx.has_matched = MATCHED
             yield True
 
         ctx.state.marks_push()
@@ -642,8 +646,8 @@
                 ctx.state.string_position = ctx.string_position
                 child_context = ctx.push_new_context(ctx.peek_code(1) + 1)
                 yield False
-                if child_context.has_matched:
-                    ctx.has_matched = True
+                if child_context.has_matched == MATCHED:
+                    ctx.has_matched = MATCHED
                     yield True
                 ctx.skip_char(-1)
                 count -= 1
@@ -655,15 +659,15 @@
                 ctx.state.string_position = ctx.string_position
                 child_context = ctx.push_new_context(ctx.peek_code(1) + 1)
                 yield False
-                if child_context.has_matched:
-                    ctx.has_matched = True
+                if child_context.has_matched == MATCHED:
+                    ctx.has_matched = MATCHED
                     yield True
                 ctx.skip_char(-1)
                 count -= 1
                 ctx.state.marks_pop_keep()
 
         ctx.state.marks_pop_discard()
-        ctx.has_matched = False
+        ctx.has_matched = NOT_MATCHED
         yield True
 
     def op_min_repeat_one(self, ctx):
@@ -674,7 +678,7 @@
         #self._log(ctx, "MIN_REPEAT_ONE", mincount, maxcount)
 
         if ctx.remaining_chars() < mincount:
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
             yield True
         ctx.state.string_position = ctx.string_position
         if mincount == 0:
@@ -682,13 +686,13 @@
         else:
             count = self.count_repetitions(ctx, mincount)
             if count < mincount:
-                ctx.has_matched = False
+                ctx.has_matched = NOT_MATCHED
                 yield True
             ctx.skip_char(count)
         if ctx.peek_code(ctx.peek_code(1) + 1) == OPCODES["success"]:
             # tail is empty.  we're finished
             ctx.state.string_position = ctx.string_position
-            ctx.has_matched = True
+            ctx.has_matched = MATCHED
             yield True
 
         ctx.state.marks_push()
@@ -696,8 +700,8 @@
             ctx.state.string_position = ctx.string_position
             child_context = ctx.push_new_context(ctx.peek_code(1) + 1)
             yield False
-            if child_context.has_matched:
-                ctx.has_matched = True
+            if child_context.has_matched == MATCHED:
+                ctx.has_matched = MATCHED
                 yield True
             ctx.state.string_position = ctx.string_position
             if self.count_repetitions(ctx, 1) == 0:
@@ -707,7 +711,7 @@
             ctx.state.marks_pop_keep()
 
         ctx.state.marks_pop_discard()
-        ctx.has_matched = False
+        ctx.has_matched = NOT_MATCHED
         yield True
 
     def op_repeat(self, ctx):
@@ -742,7 +746,7 @@
             child_context = repeat.push_new_context(4)
             yield False
             ctx.has_matched = child_context.has_matched
-            if not ctx.has_matched:
+            if ctx.has_matched == NOT_MATCHED:
                 repeat.count = count - 1
                 ctx.state.string_position = ctx.string_position
             yield True
@@ -757,9 +761,9 @@
             child_context = repeat.push_new_context(4)
             yield False
             repeat.last_position = save_last_position
-            if child_context.has_matched:
+            if child_context.has_matched == MATCHED:
                 ctx.state.marks_pop_discard()
-                ctx.has_matched = True
+                ctx.has_matched = MATCHED
                 yield True
             ctx.state.marks_pop()
             repeat.count = count - 1
@@ -770,7 +774,7 @@
         child_context = ctx.push_new_context(1)
         yield False
         ctx.has_matched = child_context.has_matched
-        if not ctx.has_matched:
+        if ctx.has_matched == NOT_MATCHED:
             ctx.state.repeat = repeat
             ctx.state.string_position = ctx.string_position
         yield True
@@ -793,7 +797,7 @@
             child_context = repeat.push_new_context(4)
             yield False
             ctx.has_matched = child_context.has_matched
-            if not ctx.has_matched:
+            if ctx.has_matched == NOT_MATCHED:
                 repeat.count = count - 1
                 ctx.state.string_position = ctx.string_position
             yield True
@@ -803,8 +807,8 @@
         ctx.state.repeat = repeat.previous
         child_context = ctx.push_new_context(1)
         yield False
-        if child_context.has_matched:
-            ctx.has_matched = True
+        if child_context.has_matched == MATCHED:
+            ctx.has_matched = MATCHED
             yield True
         ctx.state.repeat = repeat
         ctx.state.string_position = ctx.string_position
@@ -812,13 +816,13 @@
 
         # match more until tail matches
         if count >= maxcount and maxcount != MAXREPEAT:
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
             yield True
         repeat.count = count
         child_context = repeat.push_new_context(4)
         yield False
         ctx.has_matched = child_context.has_matched
-        if not ctx.has_matched:
+        if ctx.has_matched == NOT_MATCHED:
             repeat.count = count - 1
             ctx.state.string_position = ctx.string_position
         yield True
@@ -826,12 +830,12 @@
     def general_op_groupref(self, ctx, decorate=lambda x: x):
         group_start, group_end = ctx.state.get_marks(ctx.peek_code(1))
         if group_start is None or group_end is None or group_end < group_start:
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
             return True
         while group_start < group_end:
             if ctx.at_end() or decorate(ord(ctx.peek_char())) \
                                 != decorate(ord(ctx.state.string[group_start])):
-                ctx.has_matched = False
+                ctx.has_matched = NOT_MATCHED
                 return True
             group_start += 1
             ctx.skip_char(1)
@@ -866,14 +870,14 @@
         #self._log(ctx, "ASSERT", ctx.peek_code(2))
         ctx.state.string_position = ctx.string_position - ctx.peek_code(2)
         if ctx.state.string_position < 0:
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
             yield True
         child_context = ctx.push_new_context(3)
         yield False
-        if child_context.has_matched:
+        if child_context.has_matched == MATCHED:
             ctx.skip_code(ctx.peek_code(1) + 1)
         else:
-            ctx.has_matched = False
+            ctx.has_matched = NOT_MATCHED
         yield True
 
     def op_assert_not(self, ctx):
@@ -884,8 +888,8 @@
         if ctx.state.string_position >= 0:
             child_context = ctx.push_new_context(3)
             yield False
-            if child_context.has_matched:
-                ctx.has_matched = False
+            if child_context.has_matched == MATCHED:
+                ctx.has_matched = NOT_MATCHED
                 yield True
         ctx.skip_code(ctx.peek_code(1) + 1)
         yield True
@@ -914,10 +918,10 @@
             # a success opcode
             ctx.code_position = reset_position
             self.dispatch(ctx.peek_code(), ctx)
-            if ctx.has_matched is False: # could be None as well
+            if ctx.has_matched == NOT_MATCHED: # could be None as well
                 break
             count += 1
-        ctx.has_matched = None
+        ctx.has_matched = UNDECIDED
         ctx.code_position = code_position
         ctx.string_position = string_position
         return count

Modified: pypy/dist/pypy/module/_sre/interp_sre.py
==============================================================================
--- pypy/dist/pypy/module/_sre/interp_sre.py	(original)
+++ pypy/dist/pypy/module/_sre/interp_sre.py	Thu Aug 25 16:21:37 2005
@@ -153,62 +153,54 @@
 
 class W_MatchContext(Wrappable):
 
+    UNDECIDED = 0
+    MATCHED = 1
+    NOT_MATCHED = 2
+
     def __init__(self, space, w_state, w_pattern_codes):
         self.space = space
-        self.w_state = w_state
+        self.state = w_state
         self.pattern_codes_w = space.unpackiterable(w_pattern_codes)
-        self.w_string_position = space.wrap(w_state.string_position)
-        self.w_code_position = space.wrap(0)
-        self.w_has_matched = space.w_None
-
-        # XXX These attributes are only really used with repeat operations.
-        # Formerly a subclass of MatchContext was used for these, but for
-        # simplictiy we simply add them here for now.
-        #self.count = space.wrap(-1)
-        #self.previous = context.state.repeat
-        #self.last_position = None
+        self.string_position = w_state.string_position
+        self.code_position = 0
+        self.has_matched = self.UNDECIDED
 
     def push_new_context(self, w_pattern_offset):
         """Creates a new child context of this context and pushes it on the
         stack. pattern_offset is the offset off the current code position to
         start interpreting from."""
         pattern_offset = self.space.int_w(w_pattern_offset)
-        pattern_codes_w = self.pattern_codes_w[
-                    self.space.int_w(self.w_code_position) + pattern_offset:]
-        w_child_context = self.space.wrap(W_MatchContext(self.space, self.w_state,
+        pattern_codes_w = self.pattern_codes_w[self.code_position + pattern_offset:]
+        w_child_context = self.space.wrap(W_MatchContext(self.space, self.state,
                                            self.space.newlist(pattern_codes_w)))
-        self.space.call_method(self.w_state.w_context_stack, "append", w_child_context)
-        #context_stack = self.space.unpackiterable(self.w_state.w_context_stack)
-        #context_stack.append(w_child_context)
+        self.space.call_method(self.state.w_context_stack, "append", w_child_context)
         return w_child_context
 
     def peek_char(self, w_peek=0):
-        return self.space.getitem(self.w_state.w_string,
-                                self.space.add(self.w_string_position, w_peek))
+        return self.space.getitem(self.state.w_string,
+                self.space.add(self.space.wrap(self.string_position), w_peek))
 
     def skip_char(self, w_skip_count):
-        self.w_string_position = self.space.add(self.w_string_position, w_skip_count)
+        self.string_position = self.string_position + self.space.int_w(w_skip_count)
 
     def remaining_chars(self):
-        return self.space.sub(self.space.wrap(self.w_state.end), self.w_string_position)
+        return self.space.wrap(self.state.end - self.string_position)
 
     def peek_code(self, w_peek=0):
         space = self.space
-        return self.pattern_codes_w[
-                        space.int_w(space.add(self.w_code_position, w_peek))]
+        return self.pattern_codes_w[self.code_position + self.space.int_w(w_peek)]
 
     def skip_code(self, w_skip_count):
-        self.w_code_position = self.space.add(self.w_code_position, w_skip_count)
+        self.code_position = self.code_position + self.space.int_w(w_skip_count)
 
     def remaining_codes(self):
-        return self.space.sub(self.space.wrap(len(self.pattern_codes_w)),
-                                                           self.w_code_position)
+        return self.space.wrap(len(self.pattern_codes_w) - self.code_position)
 
     def at_beginning(self):
-        return self.space.eq(self.w_string_position, self.space.wrap(0))
+        return self.space.newbool(self.string_position == 0)
 
     def at_end(self):
-        return self.space.eq(self.w_string_position, self.space.wrap(self.w_state.end))
+        return self.space.newbool(self.string_position == self.state.end)
 
     def at_linebreak(self):
         space = self.space
@@ -216,11 +208,11 @@
                     space.eq(self.peek_char(space.wrap(0)), space.wrap("\n")))
 
 W_MatchContext.typedef = TypeDef("W_MatchContext",
-    state = interp_attrproperty_w("w_state", W_MatchContext),
-    string_position = interp_attrproperty_obj_w("w_string_position", W_MatchContext),
+    state = interp_attrproperty_w("state", W_MatchContext),
+    string_position = interp_attrproperty_int("string_position", W_MatchContext),
     pattern_codes = interp_attrproperty_list_w("pattern_codes_w", W_MatchContext),
-    code_position = interp_attrproperty_obj_w("w_code_position", W_MatchContext),
-    has_matched = interp_attrproperty_obj_w("w_has_matched", W_MatchContext),
+    code_position = interp_attrproperty_int("code_position", W_MatchContext),
+    has_matched = interp_attrproperty_int("has_matched", W_MatchContext),
     push_new_context = interp2app(W_MatchContext.push_new_context),
     peek_char = interp2app(W_MatchContext.peek_char),
     skip_char = interp2app(W_MatchContext.skip_char),
@@ -240,10 +232,10 @@
 class W_RepeatContext(W_MatchContext):
     
     def __init__(self, space, w_context):
-        W_MatchContext.__init__(self, space, w_context.w_state,
-            space.newlist(w_context.pattern_codes_w[space.int_w(w_context.w_code_position):]))
+        W_MatchContext.__init__(self, space, w_context.state,
+            space.newlist(w_context.pattern_codes_w[w_context.code_position:]))
         self.w_count = space.wrap(-1)
-        self.w_previous = w_context.w_state.w_repeat
+        self.w_previous = w_context.state.w_repeat
         self.w_last_position = space.w_None
 
 W_RepeatContext.typedef = TypeDef("W_RepeatContext", W_MatchContext.typedef,
@@ -264,15 +256,15 @@
     opcode = space.int_w(w_opcode)
     return space.newbool(opcode_dispatch_table[opcode] is not None)
 
-def op_success(space, w_ctx):
+def op_success(space, ctx):
     # end of pattern
-    w_ctx.w_state.string_position = space.int_w(w_ctx.w_string_position)
-    w_ctx.w_has_matched = space.newbool(True)
+    ctx.state.string_position = ctx.string_position
+    ctx.has_matched = ctx.MATCHED
     return True
 
-def op_failure(space, w_ctx):
+def op_failure(space, ctx):
     # immediate failure
-    w_ctx.w_has_matched = space.newbool(False)
+    ctx.has_matched = ctx.NOT_MATCHED
     return True
 
 opcode_dispatch_table = [



More information about the Pypy-commit mailing list