[Python-checkins] python/dist/src/Lib sre_compile.py, 1.49, 1.50 sre_constants.py, 1.33, 1.34 sre_parse.py, 1.57, 1.58

niemeyer at users.sourceforge.net niemeyer at users.sourceforge.net
Fri Oct 17 18:13:18 EDT 2003


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv19832/Lib

Modified Files:
	sre_compile.py sre_constants.py sre_parse.py 
Log Message:
Implemented non-recursive SRE matching.


Index: sre_compile.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/sre_compile.py,v
retrieving revision 1.49
retrieving revision 1.50
diff -C2 -d -r1.49 -r1.50
*** sre_compile.py	2 Jul 2003 21:37:16 -0000	1.49
--- sre_compile.py	17 Oct 2003 22:13:16 -0000	1.50
***************
*** 146,149 ****
--- 146,162 ----
                  emit(OPCODES[op])
              emit(av-1)
+         elif op is GROUPREF_EXISTS:
+             emit(OPCODES[op])
+             emit((av[0]-1)*2)
+             skipyes = len(code); emit(0)
+             _compile(code, av[1], flags)
+             if av[2]:
+                 emit(OPCODES[JUMP])
+                 skipno = len(code); emit(0)
+                 code[skipyes] = len(code) - skipyes + 1
+                 _compile(code, av[2], flags)
+                 code[skipno] = len(code) - skipno
+             else:
+                 code[skipyes] = len(code) - skipyes + 1
          else:
              raise ValueError, ("unsupported operand type", op)

Index: sre_constants.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/sre_constants.py,v
retrieving revision 1.33
retrieving revision 1.34
diff -C2 -d -r1.33 -r1.34
*** sre_constants.py	16 Oct 2003 05:53:16 -0000	1.33
--- sre_constants.py	17 Oct 2003 22:13:16 -0000	1.34
***************
*** 14,18 ****
  # update when constants are added or removed
  
! MAGIC = 20030419
  
  # max code word in this release
--- 14,18 ----
  # update when constants are added or removed
  
! MAGIC = 20031017
  
  # max code word in this release
***************
*** 43,46 ****
--- 43,47 ----
  GROUPREF = "groupref"
  GROUPREF_IGNORE = "groupref_ignore"
+ GROUPREF_EXISTS = "groupref_exists"
  IN = "in"
  IN_IGNORE = "in_ignore"
***************
*** 109,113 ****
      CATEGORY,
      CHARSET, BIGCHARSET,
!     GROUPREF, GROUPREF_IGNORE,
      IN, IN_IGNORE,
      INFO,
--- 110,114 ----
      CATEGORY,
      CHARSET, BIGCHARSET,
!     GROUPREF, GROUPREF_EXISTS, GROUPREF_IGNORE,
      IN, IN_IGNORE,
      INFO,

Index: sre_parse.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/sre_parse.py,v
retrieving revision 1.57
retrieving revision 1.58
diff -C2 -d -r1.57 -r1.58
*** sre_parse.py	19 Apr 2003 08:37:23 -0000	1.57
--- sre_parse.py	17 Oct 2003 22:13:16 -0000	1.58
***************
*** 365,368 ****
--- 365,382 ----
      return subpattern
  
+ def _parse_sub_cond(source, state, condgroup):
+     item_yes = _parse(source, state) 
+     if source.match("|"):
+         item_no = _parse(source, state) 
+         if source.match("|"):
+             raise error, "conditional backref with more than two branches"
+     else:
+         item_no = None
+     if source.next and not source.match(")", 0):
+         raise error, "pattern not properly closed"
+     subpattern = SubPattern(state)
+     subpattern.append((GROUPREF_EXISTS, (condgroup, item_yes, item_no)))
+     return subpattern
+ 
  def _parse(source, state):
      # parse a simple pattern
***************
*** 500,503 ****
--- 514,518 ----
              group = 1
              name = None
+             condgroup = None
              if source.match("?"):
                  group = 0
***************
*** 569,572 ****
--- 584,607 ----
                          subpattern.append((ASSERT_NOT, (dir, p)))
                      continue
+                 elif source.match("("):
+                     # conditional backreference group
+                     condname = ""
+                     while 1:
+                         char = source.get()
+                         if char is None:
+                             raise error, "unterminated name"
+                         if char == ")":
+                             break
+                         condname = condname + char
+                     group = 2
+                     if isname(condname):
+                         condgroup = state.groupdict.get(condname)
+                         if condgroup is None:
+                             raise error, "unknown group name"
+                     else:
+                         try:
+                             condgroup = atoi(condname)
+                         except ValueError:
+                             raise error, "bad character in group name"
                  else:
                      # flags
***************
*** 582,586 ****
                  else:
                      group = state.opengroup(name)
!                 p = _parse_sub(source, state)
                  if not source.match(")"):
                      raise error, "unbalanced parenthesis"
--- 617,624 ----
                  else:
                      group = state.opengroup(name)
!                 if condgroup:
!                     p = _parse_sub_cond(source, state, condgroup)
!                 else:
!                     p = _parse_sub(source, state)
                  if not source.match(")"):
                      raise error, "unbalanced parenthesis"





More information about the Python-checkins mailing list