Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3adf8d1

Browse files
committed
Converted to use re in stead of regex and regsub (finally:-).
1 parent 5ca5374 commit 3adf8d1

1 file changed

Lines changed: 65 additions & 54 deletions

File tree

Tools/bgen/bgen/scantools.py

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
although most Mac specific details are contained in header-specific subclasses.
1515
"""
1616

17-
import regex
18-
import regsub
17+
import re
1918
import string
2019
import sys
2120
import os
@@ -33,7 +32,7 @@
3332
class Scanner:
3433

3534
# Set to 1 in subclass to debug your scanner patterns.
36-
debug = 0
35+
debug = 0
3736

3837
def __init__(self, input = None, output = None, defsoutput = None):
3938
self.initsilent()
@@ -240,27 +239,27 @@ def initpaths(self):
240239
self.includepath = [':', INCLUDEDIR]
241240

242241
def initpatterns(self):
243-
self.head_pat = "^EXTERN_API[^_]"
244-
self.tail_pat = "[;={}]"
245-
self.type_pat = "EXTERN_API" + \
246-
"[ \t\n]*([ \t\n]*" + \
247-
"\(<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*]\)" + \
248-
"[ \t\n]*)[ \t\n]*"
249-
self.name_pat = "\(<name>[a-zA-Z0-9_]+\)[ \t\n]*"
250-
self.args_pat = "(\(<args>\([^(;=)]+\|([^(;=)]*)\)*\))"
242+
self.head_pat = r"^EXTERN_API[^_]"
243+
self.tail_pat = r"[;={}]"
244+
self.type_pat = r"EXTERN_API" + \
245+
r"[ \t\n]*\([ \t\n]*" + \
246+
r"(?P<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*])" + \
247+
r"[ \t\n]*\)[ \t\n]*"
248+
self.name_pat = r"(?P<name>[a-zA-Z0-9_]+)[ \t\n]*"
249+
self.args_pat = r"\((?P<args>([^\(;=\)]+|\([^\(;=\)]*\))*)\)"
251250
self.whole_pat = self.type_pat + self.name_pat + self.args_pat
252-
self.sym_pat = "^[ \t]*\(<name>[a-zA-Z0-9_]+\)[ \t]*=" + \
253-
"[ \t]*\(<defn>[-0-9_a-zA-Z'\"(][^\t\n,;}]*\),?"
254-
self.asplit_pat = "^\(<type>.*[^a-zA-Z0-9_]\)\(<name>[a-zA-Z0-9_]+\)\(<array>\[\]\)?$"
255-
self.comment1_pat = "\(<rest>.*\)//.*"
251+
self.sym_pat = r"^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
252+
r"[ \t]*(?P<defn>[-0-9_a-zA-Z'\"\(][^\t\n,;}]*),?"
253+
self.asplit_pat = r"^(?P<type>.*[^a-zA-Z0-9_])(?P<name>[a-zA-Z0-9_]+)(?P<array>\[\])?$"
254+
self.comment1_pat = r"(?P<rest>.*)//.*"
256255
# note that the next pattern only removes comments that are wholly within one line
257-
self.comment2_pat = "\(<rest1>.*\)/\*.*\*/\(<rest2>.*\)"
256+
self.comment2_pat = r"(?P<rest1>.*)/\*.*\*/(?P<rest2>.*)"
258257

259258
def compilepatterns(self):
260259
for name in dir(self):
261260
if name[-4:] == "_pat":
262261
pat = getattr(self, name)
263-
prog = regex.symcomp(pat)
262+
prog = re.compile(pat)
264263
setattr(self, name[:-4], prog)
265264

266265
def initosspecifics(self):
@@ -404,20 +403,26 @@ def scan(self):
404403
except EOFError: break
405404
if self.debug:
406405
self.report("LINE: %s" % `line`)
407-
if self.comment1.match(line) >= 0:
408-
line = self.comment1.group('rest')
406+
match = self.comment1.match(line)
407+
if match:
408+
line = match.group('rest')
409409
if self.debug:
410410
self.report("\tafter comment1: %s" % `line`)
411-
while self.comment2.match(line) >= 0:
412-
line = self.comment2.group('rest1')+self.comment2.group('rest2')
411+
match = self.comment2.match(line)
412+
while match:
413+
line = match.group('rest1')+match.group('rest2')
413414
if self.debug:
414415
self.report("\tafter comment2: %s" % `line`)
415-
if self.defsfile and self.sym.match(line) >= 0:
416-
if self.debug:
417-
self.report("\tmatches sym.")
418-
self.dosymdef()
419-
continue
420-
if self.head.match(line) >= 0:
416+
match = self.comment2.match(line)
417+
if self.defsfile:
418+
match = self.sym.match(line)
419+
if match:
420+
if self.debug:
421+
self.report("\tmatches sym.")
422+
self.dosymdef(match)
423+
continue
424+
match = self.head.match(line)
425+
if match:
421426
if self.debug:
422427
self.report("\tmatches head.")
423428
self.dofuncspec()
@@ -426,8 +431,8 @@ def scan(self):
426431
self.error("Uncaught EOF error")
427432
self.reportusedtypes()
428433

429-
def dosymdef(self):
430-
name, defn = self.sym.group('name', 'defn')
434+
def dosymdef(self, match):
435+
name, defn = match.group('name', 'defn')
431436
if self.debug:
432437
self.report("\tsym: name=%s, defn=%s" % (`name`, `defn`))
433438
if not name in self.blacklistnames:
@@ -438,35 +443,39 @@ def dosymdef(self):
438443

439444
def dofuncspec(self):
440445
raw = self.line
441-
while self.tail.search(raw) < 0:
446+
while not self.tail.search(raw):
442447
line = self.getline()
443448
if self.debug:
444449
self.report("* CONTINUATION LINE: %s" % `line`)
445-
if self.comment1.match(line) >= 0:
446-
line = self.comment1.group('rest')
450+
match = self.comment1.match(line)
451+
if match:
452+
line = match.group('rest')
447453
if self.debug:
448454
self.report("\tafter comment1: %s" % `line`)
449-
while self.comment2.match(line) >= 0:
450-
line = self.comment2.group('rest1')+self.comment2.group('rest2')
455+
match = self.comment2.match(line)
456+
while match:
457+
line = match.group('rest1')+match.group('rest2')
451458
if self.debug:
452459
self.report("\tafter comment1: %s" % `line`)
460+
match = self.comment2.match(line)
453461
raw = raw + line
454462
if self.debug:
455463
self.report("* WHOLE LINE: %s" % `raw`)
456464
self.processrawspec(raw)
457465

458466
def processrawspec(self, raw):
459-
if self.whole.search(raw) < 0:
467+
match = self.whole.search(raw)
468+
if not match:
460469
self.report("Bad raw spec: %s", `raw`)
461470
if self.debug:
462-
if self.type.search(raw) < 0:
471+
if not self.type.search(raw):
463472
self.report("(Type already doesn't match)")
464473
else:
465-
self.report("(Type matched: %s)" % `self.type.group('type')`)
474+
self.report("(but type matched)")
466475
return
467-
type, name, args = self.whole.group('type', 'name', 'args')
468-
type = regsub.gsub("\*", " ptr", type)
469-
type = regsub.gsub("[ \t]+", "_", type)
476+
type, name, args = match.group('type', 'name', 'args')
477+
type = re.sub("\*", " ptr", type)
478+
type = re.sub("[ \t]+", "_", type)
470479
if name in self.alreadydone:
471480
self.report("Name has already been defined: %s", `name`)
472481
return
@@ -500,16 +509,18 @@ def extractarglist(self, args):
500509

501510
def extractarg(self, part):
502511
mode = "InMode"
503-
if self.asplit.match(part) < 0:
512+
part = part.strip()
513+
match = self.asplit.match(part)
514+
if not match:
504515
self.error("Indecipherable argument: %s", `part`)
505516
return ("unknown", part, mode)
506-
type, name, array = self.asplit.group('type', 'name', 'array')
517+
type, name, array = match.group('type', 'name', 'array')
507518
if array:
508519
# array matches an optional [] after the argument name
509520
type = type + " ptr "
510-
type = regsub.gsub("\*", " ptr ", type)
521+
type = re.sub("\*", " ptr ", type)
511522
type = string.strip(type)
512-
type = regsub.gsub("[ \t]+", "_", type)
523+
type = re.sub("[ \t]+", "_", type)
513524
return self.modifyarg(type, name, mode)
514525

515526
def modifyarg(self, type, name, mode):
@@ -610,23 +621,23 @@ class Scanner_PreUH3(Scanner):
610621
def initpatterns(self):
611622
Scanner.initpatterns(self)
612623
self.head_pat = "^extern pascal[ \t]+" # XXX Mac specific!
613-
self.type_pat = "pascal[ \t\n]+\(<type>[a-zA-Z0-9_ \t]*[a-zA-Z0-9_]\)[ \t\n]+"
624+
self.type_pat = "pascal[ \t\n]+(?P<type>[a-zA-Z0-9_ \t]*[a-zA-Z0-9_])[ \t\n]+"
614625
self.whole_pat = self.type_pat + self.name_pat + self.args_pat
615-
self.sym_pat = "^[ \t]*\(<name>[a-zA-Z0-9_]+\)[ \t]*=" + \
616-
"[ \t]*\(<defn>[-0-9'\"][^\t\n,;}]*\),?"
626+
self.sym_pat = "^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
627+
"[ \t]*(?P<defn>[-0-9'\"][^\t\n,;}]*),?"
617628

618629
class Scanner_OSX(Scanner):
619630
"""Scanner for modern (post UH3.3) Universal Headers """
620631
def initpatterns(self):
621632
Scanner.initpatterns(self)
622-
self.head_pat = "^EXTERN_API\(_C\)?"
623-
self.type_pat = "EXTERN_API\(_C\)?" + \
624-
"[ \t\n]*([ \t\n]*" + \
625-
"\(<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*]\)" + \
626-
"[ \t\n]*)[ \t\n]*"
633+
self.head_pat = "^EXTERN_API(_C)?"
634+
self.type_pat = "EXTERN_API(_C)?" + \
635+
"[ \t\n]*\([ \t\n]*" + \
636+
"(?P<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*])" + \
637+
"[ \t\n]*\)[ \t\n]*"
627638
self.whole_pat = self.type_pat + self.name_pat + self.args_pat
628-
self.sym_pat = "^[ \t]*\(<name>[a-zA-Z0-9_]+\)[ \t]*=" + \
629-
"[ \t]*\(<defn>[-0-9_a-zA-Z'\"(][^\t\n,;}]*\),?"
639+
self.sym_pat = "^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
640+
"[ \t]*(?P<defn>[-0-9_a-zA-Z'\"\(][^\t\n,;}]*),?"
630641

631642
def test():
632643
input = "D:Development:THINK C:Mac #includes:Apple #includes:AppleEvents.h"

0 commit comments

Comments
 (0)