Kaydet (Commit) 3adf8d1d authored tarafından Jack Jansen's avatar Jack Jansen

Converted to use re in stead of regex and regsub (finally:-).

üst 5ca53747
...@@ -14,8 +14,7 @@ The defaults are (currently) tuned to scanning Apple Macintosh header files, ...@@ -14,8 +14,7 @@ The defaults are (currently) tuned to scanning Apple Macintosh header files,
although most Mac specific details are contained in header-specific subclasses. although most Mac specific details are contained in header-specific subclasses.
""" """
import regex import re
import regsub
import string import string
import sys import sys
import os import os
...@@ -33,7 +32,7 @@ Error = "scantools.Error" ...@@ -33,7 +32,7 @@ Error = "scantools.Error"
class Scanner: class Scanner:
# Set to 1 in subclass to debug your scanner patterns. # Set to 1 in subclass to debug your scanner patterns.
debug = 0 debug = 0
def __init__(self, input = None, output = None, defsoutput = None): def __init__(self, input = None, output = None, defsoutput = None):
self.initsilent() self.initsilent()
...@@ -240,27 +239,27 @@ if missing: raise "Missing Types" ...@@ -240,27 +239,27 @@ if missing: raise "Missing Types"
self.includepath = [':', INCLUDEDIR] self.includepath = [':', INCLUDEDIR]
def initpatterns(self): def initpatterns(self):
self.head_pat = "^EXTERN_API[^_]" self.head_pat = r"^EXTERN_API[^_]"
self.tail_pat = "[;={}]" self.tail_pat = r"[;={}]"
self.type_pat = "EXTERN_API" + \ self.type_pat = r"EXTERN_API" + \
"[ \t\n]*([ \t\n]*" + \ r"[ \t\n]*\([ \t\n]*" + \
"\(<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*]\)" + \ r"(?P<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*])" + \
"[ \t\n]*)[ \t\n]*" r"[ \t\n]*\)[ \t\n]*"
self.name_pat = "\(<name>[a-zA-Z0-9_]+\)[ \t\n]*" self.name_pat = r"(?P<name>[a-zA-Z0-9_]+)[ \t\n]*"
self.args_pat = "(\(<args>\([^(;=)]+\|([^(;=)]*)\)*\))" self.args_pat = r"\((?P<args>([^\(;=\)]+|\([^\(;=\)]*\))*)\)"
self.whole_pat = self.type_pat + self.name_pat + self.args_pat self.whole_pat = self.type_pat + self.name_pat + self.args_pat
self.sym_pat = "^[ \t]*\(<name>[a-zA-Z0-9_]+\)[ \t]*=" + \ self.sym_pat = r"^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
"[ \t]*\(<defn>[-0-9_a-zA-Z'\"(][^\t\n,;}]*\),?" r"[ \t]*(?P<defn>[-0-9_a-zA-Z'\"\(][^\t\n,;}]*),?"
self.asplit_pat = "^\(<type>.*[^a-zA-Z0-9_]\)\(<name>[a-zA-Z0-9_]+\)\(<array>\[\]\)?$" self.asplit_pat = r"^(?P<type>.*[^a-zA-Z0-9_])(?P<name>[a-zA-Z0-9_]+)(?P<array>\[\])?$"
self.comment1_pat = "\(<rest>.*\)//.*" self.comment1_pat = r"(?P<rest>.*)//.*"
# note that the next pattern only removes comments that are wholly within one line # note that the next pattern only removes comments that are wholly within one line
self.comment2_pat = "\(<rest1>.*\)/\*.*\*/\(<rest2>.*\)" self.comment2_pat = r"(?P<rest1>.*)/\*.*\*/(?P<rest2>.*)"
def compilepatterns(self): def compilepatterns(self):
for name in dir(self): for name in dir(self):
if name[-4:] == "_pat": if name[-4:] == "_pat":
pat = getattr(self, name) pat = getattr(self, name)
prog = regex.symcomp(pat) prog = re.compile(pat)
setattr(self, name[:-4], prog) setattr(self, name[:-4], prog)
def initosspecifics(self): def initosspecifics(self):
...@@ -404,20 +403,26 @@ if missing: raise "Missing Types" ...@@ -404,20 +403,26 @@ if missing: raise "Missing Types"
except EOFError: break except EOFError: break
if self.debug: if self.debug:
self.report("LINE: %s" % `line`) self.report("LINE: %s" % `line`)
if self.comment1.match(line) >= 0: match = self.comment1.match(line)
line = self.comment1.group('rest') if match:
line = match.group('rest')
if self.debug: if self.debug:
self.report("\tafter comment1: %s" % `line`) self.report("\tafter comment1: %s" % `line`)
while self.comment2.match(line) >= 0: match = self.comment2.match(line)
line = self.comment2.group('rest1')+self.comment2.group('rest2') while match:
line = match.group('rest1')+match.group('rest2')
if self.debug: if self.debug:
self.report("\tafter comment2: %s" % `line`) self.report("\tafter comment2: %s" % `line`)
if self.defsfile and self.sym.match(line) >= 0: match = self.comment2.match(line)
if self.debug: if self.defsfile:
self.report("\tmatches sym.") match = self.sym.match(line)
self.dosymdef() if match:
continue if self.debug:
if self.head.match(line) >= 0: self.report("\tmatches sym.")
self.dosymdef(match)
continue
match = self.head.match(line)
if match:
if self.debug: if self.debug:
self.report("\tmatches head.") self.report("\tmatches head.")
self.dofuncspec() self.dofuncspec()
...@@ -426,8 +431,8 @@ if missing: raise "Missing Types" ...@@ -426,8 +431,8 @@ if missing: raise "Missing Types"
self.error("Uncaught EOF error") self.error("Uncaught EOF error")
self.reportusedtypes() self.reportusedtypes()
def dosymdef(self): def dosymdef(self, match):
name, defn = self.sym.group('name', 'defn') name, defn = match.group('name', 'defn')
if self.debug: if self.debug:
self.report("\tsym: name=%s, defn=%s" % (`name`, `defn`)) self.report("\tsym: name=%s, defn=%s" % (`name`, `defn`))
if not name in self.blacklistnames: if not name in self.blacklistnames:
...@@ -438,35 +443,39 @@ if missing: raise "Missing Types" ...@@ -438,35 +443,39 @@ if missing: raise "Missing Types"
def dofuncspec(self): def dofuncspec(self):
raw = self.line raw = self.line
while self.tail.search(raw) < 0: while not self.tail.search(raw):
line = self.getline() line = self.getline()
if self.debug: if self.debug:
self.report("* CONTINUATION LINE: %s" % `line`) self.report("* CONTINUATION LINE: %s" % `line`)
if self.comment1.match(line) >= 0: match = self.comment1.match(line)
line = self.comment1.group('rest') if match:
line = match.group('rest')
if self.debug: if self.debug:
self.report("\tafter comment1: %s" % `line`) self.report("\tafter comment1: %s" % `line`)
while self.comment2.match(line) >= 0: match = self.comment2.match(line)
line = self.comment2.group('rest1')+self.comment2.group('rest2') while match:
line = match.group('rest1')+match.group('rest2')
if self.debug: if self.debug:
self.report("\tafter comment1: %s" % `line`) self.report("\tafter comment1: %s" % `line`)
match = self.comment2.match(line)
raw = raw + line raw = raw + line
if self.debug: if self.debug:
self.report("* WHOLE LINE: %s" % `raw`) self.report("* WHOLE LINE: %s" % `raw`)
self.processrawspec(raw) self.processrawspec(raw)
def processrawspec(self, raw): def processrawspec(self, raw):
if self.whole.search(raw) < 0: match = self.whole.search(raw)
if not match:
self.report("Bad raw spec: %s", `raw`) self.report("Bad raw spec: %s", `raw`)
if self.debug: if self.debug:
if self.type.search(raw) < 0: if not self.type.search(raw):
self.report("(Type already doesn't match)") self.report("(Type already doesn't match)")
else: else:
self.report("(Type matched: %s)" % `self.type.group('type')`) self.report("(but type matched)")
return return
type, name, args = self.whole.group('type', 'name', 'args') type, name, args = match.group('type', 'name', 'args')
type = regsub.gsub("\*", " ptr", type) type = re.sub("\*", " ptr", type)
type = regsub.gsub("[ \t]+", "_", type) type = re.sub("[ \t]+", "_", type)
if name in self.alreadydone: if name in self.alreadydone:
self.report("Name has already been defined: %s", `name`) self.report("Name has already been defined: %s", `name`)
return return
...@@ -500,16 +509,18 @@ if missing: raise "Missing Types" ...@@ -500,16 +509,18 @@ if missing: raise "Missing Types"
def extractarg(self, part): def extractarg(self, part):
mode = "InMode" mode = "InMode"
if self.asplit.match(part) < 0: part = part.strip()
match = self.asplit.match(part)
if not match:
self.error("Indecipherable argument: %s", `part`) self.error("Indecipherable argument: %s", `part`)
return ("unknown", part, mode) return ("unknown", part, mode)
type, name, array = self.asplit.group('type', 'name', 'array') type, name, array = match.group('type', 'name', 'array')
if array: if array:
# array matches an optional [] after the argument name # array matches an optional [] after the argument name
type = type + " ptr " type = type + " ptr "
type = regsub.gsub("\*", " ptr ", type) type = re.sub("\*", " ptr ", type)
type = string.strip(type) type = string.strip(type)
type = regsub.gsub("[ \t]+", "_", type) type = re.sub("[ \t]+", "_", type)
return self.modifyarg(type, name, mode) return self.modifyarg(type, name, mode)
def modifyarg(self, type, name, mode): def modifyarg(self, type, name, mode):
...@@ -610,23 +621,23 @@ class Scanner_PreUH3(Scanner): ...@@ -610,23 +621,23 @@ class Scanner_PreUH3(Scanner):
def initpatterns(self): def initpatterns(self):
Scanner.initpatterns(self) Scanner.initpatterns(self)
self.head_pat = "^extern pascal[ \t]+" # XXX Mac specific! 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.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.whole_pat = self.type_pat + self.name_pat + self.args_pat
self.sym_pat = "^[ \t]*\(<name>[a-zA-Z0-9_]+\)[ \t]*=" + \ self.sym_pat = "^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
"[ \t]*\(<defn>[-0-9'\"][^\t\n,;}]*\),?" "[ \t]*(?P<defn>[-0-9'\"][^\t\n,;}]*),?"
class Scanner_OSX(Scanner): class Scanner_OSX(Scanner):
"""Scanner for modern (post UH3.3) Universal Headers """ """Scanner for modern (post UH3.3) Universal Headers """
def initpatterns(self): def initpatterns(self):
Scanner.initpatterns(self) Scanner.initpatterns(self)
self.head_pat = "^EXTERN_API\(_C\)?" self.head_pat = "^EXTERN_API(_C)?"
self.type_pat = "EXTERN_API\(_C\)?" + \ self.type_pat = "EXTERN_API(_C)?" + \
"[ \t\n]*([ \t\n]*" + \ "[ \t\n]*\([ \t\n]*" + \
"\(<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*]\)" + \ "(?P<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*])" + \
"[ \t\n]*)[ \t\n]*" "[ \t\n]*\)[ \t\n]*"
self.whole_pat = self.type_pat + self.name_pat + self.args_pat self.whole_pat = self.type_pat + self.name_pat + self.args_pat
self.sym_pat = "^[ \t]*\(<name>[a-zA-Z0-9_]+\)[ \t]*=" + \ self.sym_pat = "^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
"[ \t]*\(<defn>[-0-9_a-zA-Z'\"(][^\t\n,;}]*\),?" "[ \t]*(?P<defn>[-0-9_a-zA-Z'\"\(][^\t\n,;}]*),?"
def test(): def test():
input = "D:Development:THINK C:Mac #includes:Apple #includes:AppleEvents.h" input = "D:Development:THINK C:Mac #includes:Apple #includes:AppleEvents.h"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment