[Python-checkins] python/dist/src/Tools/bgen/bgen scantools.py,1.26,1.27

jackjansen@sourceforge.net jackjansen@sourceforge.net
Tue, 23 Apr 2002 15:43:39 -0700


Update of /cvsroot/python/python/dist/src/Tools/bgen/bgen
In directory usw-pr-cvs1:/tmp/cvs-serv19111/Python/Tools/bgen/bgen

Modified Files:
	scantools.py 
Log Message:
Converted to use re in stead of regex and regsub (finally:-).

Index: scantools.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/bgen/bgen/scantools.py,v
retrieving revision 1.26
retrieving revision 1.27
diff -C2 -d -r1.26 -r1.27
*** scantools.py	12 Apr 2002 13:21:49 -0000	1.26
--- scantools.py	23 Apr 2002 22:43:37 -0000	1.27
***************
*** 15,20 ****
  """
  
! import regex
! import regsub
  import string
  import sys
--- 15,19 ----
  """
  
! import re
  import string
  import sys
***************
*** 34,38 ****
  
  	# Set to 1 in subclass to debug your scanner patterns.
! 	debug = 0 
  
  	def __init__(self, input = None, output = None, defsoutput = None):
--- 33,37 ----
  
  	# Set to 1 in subclass to debug your scanner patterns.
! 	debug = 0
  
  	def __init__(self, input = None, output = None, defsoutput = None):
***************
*** 241,259 ****
  
  	def initpatterns(self):
! 		self.head_pat = "^EXTERN_API[^_]"
! 		self.tail_pat = "[;={}]"
! 		self.type_pat = "EXTERN_API" + \
! 						"[ \t\n]*([ \t\n]*" + \
! 						"\(<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*]\)" + \
! 						"[ \t\n]*)[ \t\n]*"
! 		self.name_pat = "\(<name>[a-zA-Z0-9_]+\)[ \t\n]*"
! 		self.args_pat = "(\(<args>\([^(;=)]+\|([^(;=)]*)\)*\))"
  		self.whole_pat = self.type_pat + self.name_pat + self.args_pat
! 		self.sym_pat = "^[ \t]*\(<name>[a-zA-Z0-9_]+\)[ \t]*=" + \
! 		               "[ \t]*\(<defn>[-0-9_a-zA-Z'\"(][^\t\n,;}]*\),?"
! 		self.asplit_pat = "^\(<type>.*[^a-zA-Z0-9_]\)\(<name>[a-zA-Z0-9_]+\)\(<array>\[\]\)?$"
! 		self.comment1_pat = "\(<rest>.*\)//.*"
  		# note that the next pattern only removes comments that are wholly within one line
! 		self.comment2_pat = "\(<rest1>.*\)/\*.*\*/\(<rest2>.*\)"
  
  	def compilepatterns(self):
--- 240,258 ----
  
  	def initpatterns(self):
! 		self.head_pat = r"^EXTERN_API[^_]"
! 		self.tail_pat = r"[;={}]"
! 		self.type_pat = r"EXTERN_API" + \
! 						r"[ \t\n]*\([ \t\n]*" + \
! 						r"(?P<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*])" + \
! 						r"[ \t\n]*\)[ \t\n]*"
! 		self.name_pat = r"(?P<name>[a-zA-Z0-9_]+)[ \t\n]*"
! 		self.args_pat = r"\((?P<args>([^\(;=\)]+|\([^\(;=\)]*\))*)\)"
  		self.whole_pat = self.type_pat + self.name_pat + self.args_pat
! 		self.sym_pat = r"^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
! 		               r"[ \t]*(?P<defn>[-0-9_a-zA-Z'\"\(][^\t\n,;}]*),?"
! 		self.asplit_pat = r"^(?P<type>.*[^a-zA-Z0-9_])(?P<name>[a-zA-Z0-9_]+)(?P<array>\[\])?$"
! 		self.comment1_pat = r"(?P<rest>.*)//.*"
  		# note that the next pattern only removes comments that are wholly within one line
! 		self.comment2_pat = r"(?P<rest1>.*)/\*.*\*/(?P<rest2>.*)"
  
  	def compilepatterns(self):
***************
*** 261,265 ****
  			if name[-4:] == "_pat":
  				pat = getattr(self, name)
! 				prog = regex.symcomp(pat)
  				setattr(self, name[:-4], prog)
  
--- 260,264 ----
  			if name[-4:] == "_pat":
  				pat = getattr(self, name)
! 				prog = re.compile(pat)
  				setattr(self, name[:-4], prog)
  
***************
*** 405,422 ****
  				if self.debug:
  					self.report("LINE: %s" % `line`)
! 				if self.comment1.match(line) >= 0:
! 					line = self.comment1.group('rest')
  					if self.debug:
  						self.report("\tafter comment1: %s" % `line`)
! 				while self.comment2.match(line) >= 0:
! 					line = self.comment2.group('rest1')+self.comment2.group('rest2')
  					if self.debug:
  						self.report("\tafter comment2: %s" % `line`)
! 				if self.defsfile and self.sym.match(line) >= 0:
! 					if self.debug:
! 						self.report("\tmatches sym.")
! 					self.dosymdef()
! 					continue
! 				if self.head.match(line) >= 0:
  					if self.debug:
  						self.report("\tmatches head.")
--- 404,427 ----
  				if self.debug:
  					self.report("LINE: %s" % `line`)
! 				match = self.comment1.match(line)
! 				if match:
! 					line = match.group('rest')
  					if self.debug:
  						self.report("\tafter comment1: %s" % `line`)
! 				match = self.comment2.match(line)
! 				while match:
! 					line = match.group('rest1')+match.group('rest2')
  					if self.debug:
  						self.report("\tafter comment2: %s" % `line`)
! 					match = self.comment2.match(line)
! 				if self.defsfile:
! 					match = self.sym.match(line)
! 					if match:
! 						if self.debug:
! 							self.report("\tmatches sym.")
! 						self.dosymdef(match)
! 						continue
! 				match = self.head.match(line)
! 				if match:
  					if self.debug:
  						self.report("\tmatches head.")
***************
*** 427,432 ****
  		self.reportusedtypes()
  
! 	def dosymdef(self):
! 		name, defn = self.sym.group('name', 'defn')
  		if self.debug:
  			self.report("\tsym: name=%s, defn=%s" % (`name`, `defn`))
--- 432,437 ----
  		self.reportusedtypes()
  
! 	def dosymdef(self, match):
! 		name, defn = match.group('name', 'defn')
  		if self.debug:
  			self.report("\tsym: name=%s, defn=%s" % (`name`, `defn`))
***************
*** 439,454 ****
  	def dofuncspec(self):
  		raw = self.line
! 		while self.tail.search(raw) < 0:
  			line = self.getline()
  			if self.debug:
  				self.report("* CONTINUATION LINE: %s" % `line`)
! 			if self.comment1.match(line) >= 0:
! 				line = self.comment1.group('rest')
  				if self.debug:
  					self.report("\tafter comment1: %s" % `line`)
! 			while self.comment2.match(line) >= 0:
! 				line = self.comment2.group('rest1')+self.comment2.group('rest2')
  				if self.debug:
  					self.report("\tafter comment1: %s" % `line`)
  			raw = raw + line
  		if self.debug:
--- 444,462 ----
  	def dofuncspec(self):
  		raw = self.line
! 		while not self.tail.search(raw):
  			line = self.getline()
  			if self.debug:
  				self.report("* CONTINUATION LINE: %s" % `line`)
! 			match = self.comment1.match(line)
! 			if match:
! 				line = match.group('rest')
  				if self.debug:
  					self.report("\tafter comment1: %s" % `line`)
! 			match = self.comment2.match(line)
! 			while match:
! 				line = match.group('rest1')+match.group('rest2')
  				if self.debug:
  					self.report("\tafter comment1: %s" % `line`)
+ 				match = self.comment2.match(line)
  			raw = raw + line
  		if self.debug:
***************
*** 457,471 ****
  
  	def processrawspec(self, raw):
! 		if self.whole.search(raw) < 0:
  			self.report("Bad raw spec: %s", `raw`)
  			if self.debug:
! 				if self.type.search(raw) < 0:
  					self.report("(Type already doesn't match)")
  				else:
! 					self.report("(Type matched: %s)" % `self.type.group('type')`)
  			return
! 		type, name, args = self.whole.group('type', 'name', 'args')
! 		type = regsub.gsub("\*", " ptr", type)
! 		type = regsub.gsub("[ \t]+", "_", type)
  		if name in self.alreadydone:
  			self.report("Name has already been defined: %s", `name`)
--- 465,480 ----
  
  	def processrawspec(self, raw):
! 		match = self.whole.search(raw)
! 		if not match:
  			self.report("Bad raw spec: %s", `raw`)
  			if self.debug:
! 				if not self.type.search(raw):
  					self.report("(Type already doesn't match)")
  				else:
! 					self.report("(but type matched)")
  			return
! 		type, name, args = match.group('type', 'name', 'args')
! 		type = re.sub("\*", " ptr", type)
! 		type = re.sub("[ \t]+", "_", type)
  		if name in self.alreadydone:
  			self.report("Name has already been defined: %s", `name`)
***************
*** 501,514 ****
  	def extractarg(self, part):
  		mode = "InMode"
! 		if self.asplit.match(part) < 0:
  			self.error("Indecipherable argument: %s", `part`)
  			return ("unknown", part, mode)
! 		type, name, array = self.asplit.group('type', 'name', 'array')
  		if array:
  			# array matches an optional [] after the argument name
  			type = type + " ptr "
! 		type = regsub.gsub("\*", " ptr ", type)
  		type = string.strip(type)
! 		type = regsub.gsub("[ \t]+", "_", type)
  		return self.modifyarg(type, name, mode)
  	
--- 510,525 ----
  	def extractarg(self, part):
  		mode = "InMode"
! 		part = part.strip()
! 		match = self.asplit.match(part)
! 		if not match:
  			self.error("Indecipherable argument: %s", `part`)
  			return ("unknown", part, mode)
! 		type, name, array = match.group('type', 'name', 'array')
  		if array:
  			# array matches an optional [] after the argument name
  			type = type + " ptr "
! 		type = re.sub("\*", " ptr ", type)
  		type = string.strip(type)
! 		type = re.sub("[ \t]+", "_", type)
  		return self.modifyarg(type, name, mode)
  	
***************
*** 611,618 ****
  		Scanner.initpatterns(self)
  		self.head_pat = "^extern pascal[ \t]+" # XXX Mac specific!
! 		self.type_pat = "pascal[ \t\n]+\(<type>[a-zA-Z0-9_ \t]*[a-zA-Z0-9_]\)[ \t\n]+"
  		self.whole_pat = self.type_pat + self.name_pat + self.args_pat
! 		self.sym_pat = "^[ \t]*\(<name>[a-zA-Z0-9_]+\)[ \t]*=" + \
! 		               "[ \t]*\(<defn>[-0-9'\"][^\t\n,;}]*\),?"
  
  class Scanner_OSX(Scanner):
--- 622,629 ----
  		Scanner.initpatterns(self)
  		self.head_pat = "^extern pascal[ \t]+" # XXX Mac specific!
! 		self.type_pat = "pascal[ \t\n]+(?P<type>[a-zA-Z0-9_ \t]*[a-zA-Z0-9_])[ \t\n]+"
  		self.whole_pat = self.type_pat + self.name_pat + self.args_pat
! 		self.sym_pat = "^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
! 		               "[ \t]*(?P<defn>[-0-9'\"][^\t\n,;}]*),?"
  
  class Scanner_OSX(Scanner):
***************
*** 620,631 ****
  	def initpatterns(self):
  		Scanner.initpatterns(self)
! 		self.head_pat = "^EXTERN_API\(_C\)?"
! 		self.type_pat = "EXTERN_API\(_C\)?" + \
! 						"[ \t\n]*([ \t\n]*" + \
! 						"\(<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*]\)" + \
! 						"[ \t\n]*)[ \t\n]*"
  		self.whole_pat = self.type_pat + self.name_pat + self.args_pat
! 		self.sym_pat = "^[ \t]*\(<name>[a-zA-Z0-9_]+\)[ \t]*=" + \
! 		               "[ \t]*\(<defn>[-0-9_a-zA-Z'\"(][^\t\n,;}]*\),?"
  	
  def test():
--- 631,642 ----
  	def initpatterns(self):
  		Scanner.initpatterns(self)
! 		self.head_pat = "^EXTERN_API(_C)?"
! 		self.type_pat = "EXTERN_API(_C)?" + \
! 						"[ \t\n]*\([ \t\n]*" + \
! 						"(?P<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*])" + \
! 						"[ \t\n]*\)[ \t\n]*"
  		self.whole_pat = self.type_pat + self.name_pat + self.args_pat
! 		self.sym_pat = "^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
! 		               "[ \t]*(?P<defn>[-0-9_a-zA-Z'\"\(][^\t\n,;}]*),?"
  	
  def test():