/Tools/bgen/bgen/scantools.py

http://unladen-swallow.googlecode.com/ · Python · 849 lines · 763 code · 2 blank · 84 comment · 24 complexity · 61c78aaf681593c41aedbd8dcd7dcfe7 MD5 · raw file

  1. """\
  2. Tools for scanning header files in search of function prototypes.
  3. Often, the function prototypes in header files contain enough information
  4. to automatically generate (or reverse-engineer) interface specifications
  5. from them. The conventions used are very vendor specific, but once you've
  6. figured out what they are they are often a great help, and it sure beats
  7. manually entering the interface specifications. (These are needed to generate
  8. the glue used to access the functions from Python.)
  9. In order to make this class useful, almost every component can be overridden.
  10. The defaults are (currently) tuned to scanning Apple Macintosh header files,
  11. although most Mac specific details are contained in header-specific subclasses.
  12. """
  13. import re
  14. import sys
  15. import os
  16. import fnmatch
  17. from types import *
  18. try:
  19. import MacOS
  20. except ImportError:
  21. MacOS = None
  22. try:
  23. from bgenlocations import CREATOR, INCLUDEDIR
  24. except ImportError:
  25. CREATOR = None
  26. INCLUDEDIR = os.curdir
  27. Error = "scantools.Error"
  28. BEGINHTMLREPORT="""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  29. <html>
  30. <head>
  31. <style type="text/css">
  32. .unmatched { }
  33. .commentstripping { color: grey; text-decoration: line-through }
  34. .comment { text-decoration: line-through }
  35. .notcomment { color: black }
  36. .incomplete { color: maroon }
  37. .constant { color: green }
  38. .pyconstant { background-color: yellow }
  39. .blconstant { background-color: yellow; color: red }
  40. .declaration { color: blue }
  41. .pydeclaration { background-color: yellow }
  42. .type { font-style: italic }
  43. .name { font-weight: bold }
  44. .value { font-style: italic }
  45. .arglist { text-decoration: underline }
  46. .blacklisted { background-color: yellow; color: red }
  47. </style>
  48. <title>Bgen scan report</title>
  49. </head>
  50. <body>
  51. <h1>Bgen scan report</h1>
  52. <h2>Legend</h2>
  53. <p>This scan report is intended to help you debug the regular expressions
  54. used by the bgen scanner. It consists of the original ".h" header file(s)
  55. marked up to show you what the regular expressions in the bgen parser matched
  56. for each line. NOTE: comments in the original source files may or may not be
  57. shown.</p>
  58. <p>The typographic conventions of this file are as follows:</p>
  59. <dl>
  60. <dt>comment stripping</dt>
  61. <dd><pre><span class="commentstripping"><span class="notcomment">comment stripping is </span><span class="comment">/* marked up */</span><span class="notcomment"> and the line is repeated if needed</span></span></pre>
  62. <p>If anything here does not appear to happen correctly look at
  63. <tt>comment1_pat</tt> and <tt>comment2_pat</tt>.</p>
  64. </dd>
  65. <dt>constant definitions</dt>
  66. <dd><pre><span class="constant">#define <span class="name">name</span> <span class="value">value</span></pre>
  67. <p>Highlights name and value of the constant. Governed by <tt>sym_pat</tt>.</p>
  68. </dd>
  69. <dt>function declaration</dt>
  70. <dd><pre><span class="declaration"><span class="type">char *</span><span class="name">rindex</span><span class="arglist">(<span class="type">const char *</span><span class="name">s</span>, <span class="type">int </span><span class="name">c</span>)</span>;</span></pre>
  71. <p>Highlights type, name and argument list. <tt>type_pat</tt>,
  72. <tt>name_pat</tt> and <tt>args_pat</tt> are combined into <tt>whole_pat</tt>, which
  73. is what is used here.</p></dd>
  74. </dd>
  75. <dt>incomplete match for function declaration</dt>
  76. <dd><pre><span class="incomplete"><span class="type">char *</span>foo;</span></pre>
  77. <p>The beginning of this looked promising, but it did not match a function declaration.
  78. In other words, it matched <tt>head_pat</tt> but not <tt>whole_pat</tt>. If the next
  79. declaration has also been gobbled up you need to look at <tt>end_pat</tt>.</p>
  80. </dd>
  81. <dt>unrecognized input</dt>
  82. <dd><pre><span class="unmatched">#include "type.h"</span></pre>
  83. <p>If there are function declarations the scanner has missed (i.e. things
  84. are in this class but you want them to be declarations) you need to adapt
  85. <tt>head_pat</tt>.
  86. </dd>
  87. </dl>
  88. <h2>Output</h2>
  89. <pre>
  90. <span class="unmatched">
  91. """
  92. ENDHTMLREPORT="""</span>
  93. </pre>
  94. </body>
  95. </html>
  96. """
  97. class Scanner:
  98. # Set to 1 in subclass to debug your scanner patterns.
  99. debug = 0
  100. def __init__(self, input = None, output = None, defsoutput = None):
  101. self.initsilent()
  102. self.initblacklists()
  103. self.initrepairinstructions()
  104. self.initpaths()
  105. self.initfiles()
  106. self.initpatterns()
  107. self.compilepatterns()
  108. self.initosspecifics()
  109. self.initusedtypes()
  110. if output:
  111. self.setoutput(output, defsoutput)
  112. if input:
  113. self.setinput(input)
  114. def initusedtypes(self):
  115. self.usedtypes = {}
  116. def typeused(self, type, mode):
  117. if not self.usedtypes.has_key(type):
  118. self.usedtypes[type] = {}
  119. self.usedtypes[type][mode] = None
  120. def reportusedtypes(self):
  121. types = self.usedtypes.keys()
  122. types.sort()
  123. for type in types:
  124. modes = self.usedtypes[type].keys()
  125. modes.sort()
  126. self.report("%s %s", type, " ".join(modes))
  127. def gentypetest(self, file):
  128. fp = open(file, "w")
  129. fp.write("types=[\n")
  130. types = self.usedtypes.keys()
  131. types.sort()
  132. for type in types:
  133. fp.write("\t'%s',\n"%type)
  134. fp.write("]\n")
  135. fp.write("""missing=0
  136. for t in types:
  137. try:
  138. tt = eval(t)
  139. except NameError:
  140. print "** Missing type:", t
  141. missing = 1
  142. if missing: raise "Missing Types"
  143. """)
  144. fp.close()
  145. def initsilent(self):
  146. self.silent = 1
  147. def error(self, format, *args):
  148. if self.silent >= 0:
  149. print format%args
  150. def report(self, format, *args):
  151. if not self.silent:
  152. print format%args
  153. def writeinitialdefs(self):
  154. pass
  155. def initblacklists(self):
  156. self.blacklistnames = self.makeblacklistnames()
  157. self.blacklisttypes = ["unknown", "-"] + self.makeblacklisttypes()
  158. self.greydictnames = self.greylist2dict(self.makegreylist())
  159. def greylist2dict(self, list):
  160. rv = {}
  161. for define, namelist in list:
  162. for name in namelist:
  163. rv[name] = define
  164. return rv
  165. def makeblacklistnames(self):
  166. return []
  167. def makeblacklisttypes(self):
  168. return []
  169. def makegreylist(self):
  170. return []
  171. def initrepairinstructions(self):
  172. self.repairinstructions = self.makerepairinstructions()
  173. self.inherentpointertypes = self.makeinherentpointertypes()
  174. def makerepairinstructions(self):
  175. """Parse the repair file into repair instructions.
  176. The file format is simple:
  177. 1) use \ to split a long logical line in multiple physical lines
  178. 2) everything after the first # on a line is ignored (as comment)
  179. 3) empty lines are ignored
  180. 4) remaining lines must have exactly 3 colon-separated fields:
  181. functionpattern : argumentspattern : argumentsreplacement
  182. 5) all patterns use shell style pattern matching
  183. 6) an empty functionpattern means the same as *
  184. 7) the other two fields are each comma-separated lists of triples
  185. 8) a triple is a space-separated list of 1-3 words
  186. 9) a triple with less than 3 words is padded at the end with "*" words
  187. 10) when used as a pattern, a triple matches the type, name, and mode
  188. of an argument, respectively
  189. 11) when used as a replacement, the words of a triple specify
  190. replacements for the corresponding words of the argument,
  191. with "*" as a word by itself meaning leave the original word
  192. (no other uses of "*" is allowed)
  193. 12) the replacement need not have the same number of triples
  194. as the pattern
  195. """
  196. f = self.openrepairfile()
  197. if not f: return []
  198. print "Reading repair file", repr(f.name), "..."
  199. list = []
  200. lineno = 0
  201. while 1:
  202. line = f.readline()
  203. if not line: break
  204. lineno = lineno + 1
  205. startlineno = lineno
  206. while line[-2:] == '\\\n':
  207. line = line[:-2] + ' ' + f.readline()
  208. lineno = lineno + 1
  209. i = line.find('#')
  210. if i >= 0: line = line[:i]
  211. words = [s.strip() for s in line.split(':')]
  212. if words == ['']: continue
  213. if len(words) <> 3:
  214. print "Line", startlineno,
  215. print ": bad line (not 3 colon-separated fields)"
  216. print repr(line)
  217. continue
  218. [fpat, pat, rep] = words
  219. if not fpat: fpat = "*"
  220. if not pat:
  221. print "Line", startlineno,
  222. print "Empty pattern"
  223. print repr(line)
  224. continue
  225. patparts = [s.strip() for s in pat.split(',')]
  226. repparts = [s.strip() for s in rep.split(',')]
  227. patterns = []
  228. for p in patparts:
  229. if not p:
  230. print "Line", startlineno,
  231. print "Empty pattern part"
  232. print repr(line)
  233. continue
  234. pattern = p.split()
  235. if len(pattern) > 3:
  236. print "Line", startlineno,
  237. print "Pattern part has > 3 words"
  238. print repr(line)
  239. pattern = pattern[:3]
  240. else:
  241. while len(pattern) < 3:
  242. pattern.append("*")
  243. patterns.append(pattern)
  244. replacements = []
  245. for p in repparts:
  246. if not p:
  247. print "Line", startlineno,
  248. print "Empty replacement part"
  249. print repr(line)
  250. continue
  251. replacement = p.split()
  252. if len(replacement) > 3:
  253. print "Line", startlineno,
  254. print "Pattern part has > 3 words"
  255. print repr(line)
  256. replacement = replacement[:3]
  257. else:
  258. while len(replacement) < 3:
  259. replacement.append("*")
  260. replacements.append(replacement)
  261. list.append((fpat, patterns, replacements))
  262. return list
  263. def makeinherentpointertypes(self):
  264. return []
  265. def openrepairfile(self, filename = "REPAIR"):
  266. try:
  267. return open(filename, "rU")
  268. except IOError, msg:
  269. print repr(filename), ":", msg
  270. print "Cannot open repair file -- assume no repair needed"
  271. return None
  272. def initfiles(self):
  273. self.specmine = 0
  274. self.defsmine = 0
  275. self.scanmine = 0
  276. self.htmlmine = 0
  277. self.specfile = sys.stdout
  278. self.defsfile = None
  279. self.scanfile = sys.stdin
  280. self.htmlfile = None
  281. self.lineno = 0
  282. self.line = ""
  283. def initpaths(self):
  284. self.includepath = [os.curdir, INCLUDEDIR]
  285. def initpatterns(self):
  286. self.head_pat = r"^EXTERN_API[^_]"
  287. self.tail_pat = r"[;={}]"
  288. self.type_pat = r"EXTERN_API" + \
  289. r"[ \t\n]*\([ \t\n]*" + \
  290. r"(?P<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*])" + \
  291. r"[ \t\n]*\)[ \t\n]*"
  292. self.name_pat = r"(?P<name>[a-zA-Z0-9_]+)[ \t\n]*"
  293. self.args_pat = r"\((?P<args>([^\(;=\)]+|\([^\(;=\)]*\))*)\)"
  294. self.whole_pat = self.type_pat + self.name_pat + self.args_pat
  295. self.sym_pat = r"^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
  296. r"[ \t]*(?P<defn>[-0-9_a-zA-Z'\"\(][^\t\n,;}]*),?"
  297. self.asplit_pat = r"^(?P<type>.*[^a-zA-Z0-9_])(?P<name>[a-zA-Z0-9_]+)(?P<array>\[\])?$"
  298. self.comment1_pat = r"(?P<rest>.*)//.*"
  299. # note that the next pattern only removes comments that are wholly within one line
  300. self.comment2_pat = r"(?P<rest1>.*)/\*.*\*/(?P<rest2>.*)"
  301. def compilepatterns(self):
  302. for name in dir(self):
  303. if name[-4:] == "_pat":
  304. pat = getattr(self, name)
  305. prog = re.compile(pat)
  306. setattr(self, name[:-4], prog)
  307. def initosspecifics(self):
  308. if MacOS and CREATOR:
  309. self.filetype = 'TEXT'
  310. self.filecreator = CREATOR
  311. else:
  312. self.filetype = self.filecreator = None
  313. def setfiletype(self, filename):
  314. if MacOS and (self.filecreator or self.filetype):
  315. creator, type = MacOS.GetCreatorAndType(filename)
  316. if self.filecreator: creator = self.filecreator
  317. if self.filetype: type = self.filetype
  318. MacOS.SetCreatorAndType(filename, creator, type)
  319. def close(self):
  320. self.closefiles()
  321. def closefiles(self):
  322. self.closespec()
  323. self.closedefs()
  324. self.closescan()
  325. self.closehtml()
  326. def closespec(self):
  327. tmp = self.specmine and self.specfile
  328. self.specfile = None
  329. if tmp: tmp.close()
  330. def closedefs(self):
  331. tmp = self.defsmine and self.defsfile
  332. self.defsfile = None
  333. if tmp: tmp.close()
  334. def closescan(self):
  335. tmp = self.scanmine and self.scanfile
  336. self.scanfile = None
  337. if tmp: tmp.close()
  338. def closehtml(self):
  339. if self.htmlfile: self.htmlfile.write(ENDHTMLREPORT)
  340. tmp = self.htmlmine and self.htmlfile
  341. self.htmlfile = None
  342. if tmp: tmp.close()
  343. def setoutput(self, spec, defs = None):
  344. self.closespec()
  345. self.closedefs()
  346. if spec:
  347. if type(spec) == StringType:
  348. file = self.openoutput(spec)
  349. mine = 1
  350. else:
  351. file = spec
  352. mine = 0
  353. self.specfile = file
  354. self.specmine = mine
  355. if defs:
  356. if type(defs) == StringType:
  357. file = self.openoutput(defs)
  358. mine = 1
  359. else:
  360. file = defs
  361. mine = 0
  362. self.defsfile = file
  363. self.defsmine = mine
  364. def sethtmloutput(self, htmlfile):
  365. self.closehtml()
  366. if htmlfile:
  367. if type(htmlfile) == StringType:
  368. file = self.openoutput(htmlfile)
  369. mine = 1
  370. else:
  371. file = htmlfile
  372. mine = 0
  373. self.htmlfile = file
  374. self.htmlmine = mine
  375. self.htmlfile.write(BEGINHTMLREPORT)
  376. def openoutput(self, filename):
  377. try:
  378. file = open(filename, 'w')
  379. except IOError, arg:
  380. raise IOError, (filename, arg)
  381. self.setfiletype(filename)
  382. return file
  383. def setinput(self, scan = sys.stdin):
  384. if not type(scan) in (TupleType, ListType):
  385. scan = [scan]
  386. self.allscaninputs = scan
  387. self._nextinput()
  388. def _nextinput(self):
  389. if not self.allscaninputs:
  390. return 0
  391. scan = self.allscaninputs[0]
  392. self.allscaninputs = self.allscaninputs[1:]
  393. self.closescan()
  394. if scan:
  395. if type(scan) == StringType:
  396. file = self.openinput(scan)
  397. mine = 1
  398. else:
  399. file = scan
  400. mine = 0
  401. self.scanfile = file
  402. self.scanmine = mine
  403. self.lineno = 0
  404. return 1
  405. def openinput(self, filename):
  406. if not os.path.isabs(filename):
  407. for dir in self.includepath:
  408. fullname = os.path.join(dir, filename)
  409. #self.report("trying full name %r", fullname)
  410. try:
  411. return open(fullname, 'rU')
  412. except IOError:
  413. pass
  414. # If not on the path, or absolute, try default open()
  415. try:
  416. return open(filename, 'rU')
  417. except IOError, arg:
  418. raise IOError, (arg, filename)
  419. def getline(self):
  420. if not self.scanfile:
  421. raise Error, "input file not set"
  422. self.line = self.scanfile.readline()
  423. if not self.line:
  424. if self._nextinput():
  425. return self.getline()
  426. raise EOFError
  427. self.lineno = self.lineno + 1
  428. return self.line
  429. def scan(self):
  430. if not self.scanfile:
  431. self.error("No input file has been specified")
  432. return
  433. inputname = self.scanfile.name
  434. self.report("scanfile = %r", inputname)
  435. if not self.specfile:
  436. self.report("(No interface specifications will be written)")
  437. else:
  438. self.report("specfile = %r", self.specfile.name)
  439. self.specfile.write("# Generated from %r\n\n" % (inputname,))
  440. if not self.defsfile:
  441. self.report("(No symbol definitions will be written)")
  442. else:
  443. self.report("defsfile = %r", (self.defsfile.name,))
  444. self.defsfile.write("# Generated from %r\n\n" % (os.path.split(inputname)[1],))
  445. self.writeinitialdefs()
  446. self.alreadydone = []
  447. try:
  448. while 1:
  449. try: line = self.getline()
  450. except EOFError: break
  451. if self.debug:
  452. self.report("LINE: %r" % (line,))
  453. match = self.comment1.match(line)
  454. if match:
  455. self.htmlreport(line, klass='commentstripping', ranges=[(
  456. match.start('rest'), match.end('rest'), 'notcomment')])
  457. line = match.group('rest')
  458. if self.debug:
  459. self.report("\tafter comment1: %r" % (line,))
  460. match = self.comment2.match(line)
  461. while match:
  462. if match:
  463. self.htmlreport(line, klass='commentstripping', ranges=[
  464. (match.start('rest1'), match.end('rest1'), 'notcomment'),
  465. (match.start('rest2'), match.end('rest2'), 'notcomment')])
  466. line = match.group('rest1')+match.group('rest2')
  467. if self.debug:
  468. self.report("\tafter comment2: %r" % (line,))
  469. match = self.comment2.match(line)
  470. if self.defsfile:
  471. match = self.sym.match(line)
  472. if match:
  473. if self.debug:
  474. self.report("\tmatches sym.")
  475. self.dosymdef(match, line)
  476. continue
  477. match = self.head.match(line)
  478. if match:
  479. if self.debug:
  480. self.report("\tmatches head.")
  481. self.dofuncspec()
  482. continue
  483. self.htmlreport(line, klass='unmatched')
  484. except EOFError:
  485. self.error("Uncaught EOF error")
  486. self.reportusedtypes()
  487. def dosymdef(self, match, line):
  488. name, defn = match.group('name', 'defn')
  489. self.htmlreport(line, klass='constant', ranges=[
  490. (match.start('name'), match.end('name'), 'name'),
  491. (match.start('defn'), match.end('defn'), 'value')])
  492. defn = escape8bit(defn)
  493. if self.debug:
  494. self.report("\tsym: name=%r, defn=%r" % (name, defn))
  495. if not name in self.blacklistnames:
  496. oline = "%s = %s\n" % (name, defn)
  497. self.defsfile.write(oline)
  498. self.htmlreport(oline, klass="pyconstant")
  499. else:
  500. self.defsfile.write("# %s = %s\n" % (name, defn))
  501. self.htmlreport("** no output: name is blacklisted", klass="blconstant")
  502. # XXXX No way to handle greylisted names
  503. def dofuncspec(self):
  504. raw = self.line
  505. while not self.tail.search(raw):
  506. line = self.getline()
  507. if self.debug:
  508. self.report("* CONTINUATION LINE: %r" % (line,))
  509. match = self.comment1.match(line)
  510. if match:
  511. line = match.group('rest')
  512. if self.debug:
  513. self.report("\tafter comment1: %r" % (line,))
  514. match = self.comment2.match(line)
  515. while match:
  516. line = match.group('rest1')+match.group('rest2')
  517. if self.debug:
  518. self.report("\tafter comment1: %r" % (line,))
  519. match = self.comment2.match(line)
  520. raw = raw + line
  521. if self.debug:
  522. self.report("* WHOLE LINE: %r" % (raw,))
  523. self.processrawspec(raw)
  524. return raw
  525. def processrawspec(self, raw):
  526. match = self.whole.search(raw)
  527. if not match:
  528. self.report("Bad raw spec: %r", raw)
  529. if self.debug:
  530. match = self.type.search(raw)
  531. if not match:
  532. self.report("(Type already doesn't match)")
  533. self.htmlreport(raw, klass='incomplete', ranges=[(
  534. match.start('type'), match.end('type'), 'type')])
  535. else:
  536. self.report("(but type matched)")
  537. self.htmlreport(raw, klass='incomplete')
  538. return
  539. type, name, args = match.group('type', 'name', 'args')
  540. ranges=[
  541. (match.start('type'), match.end('type'), 'type'),
  542. (match.start('name'), match.end('name'), 'name'),
  543. (match.start('args'), match.end('args'), 'arglist')]
  544. self.htmlreport(raw, klass='declaration', ranges=ranges)
  545. modifiers = self.getmodifiers(match)
  546. type = self.pythonizename(type)
  547. name = self.pythonizename(name)
  548. if self.checkduplicate(name):
  549. self.htmlreport("*** no output generated: duplicate name", klass="blacklisted")
  550. return
  551. self.report("==> %s %s <==", type, name)
  552. if self.blacklisted(type, name):
  553. self.htmlreport("*** no output generated: function name or return type blacklisted", klass="blacklisted")
  554. self.report("*** %s %s blacklisted", type, name)
  555. return
  556. returnlist = [(type, name, 'ReturnMode')]
  557. returnlist = self.repairarglist(name, returnlist)
  558. [(type, name, returnmode)] = returnlist
  559. arglist = self.extractarglist(args)
  560. arglist = self.repairarglist(name, arglist)
  561. if self.unmanageable(type, name, arglist):
  562. self.htmlreport("*** no output generated: some argument blacklisted", klass="blacklisted")
  563. ##for arg in arglist:
  564. ## self.report(" %r", arg)
  565. self.report("*** %s %s unmanageable", type, name)
  566. return
  567. if modifiers:
  568. self.generate(type, name, arglist, modifiers)
  569. else:
  570. self.generate(type, name, arglist)
  571. def getmodifiers(self, match):
  572. return []
  573. def checkduplicate(self, name):
  574. if name in self.alreadydone:
  575. self.report("Name has already been defined: %r", name)
  576. return True
  577. self.alreadydone.append(name)
  578. return False
  579. def pythonizename(self, name):
  580. name = re.sub("\*", " ptr", name)
  581. name = name.strip()
  582. name = re.sub("[ \t]+", "_", name)
  583. return name
  584. def extractarglist(self, args):
  585. args = args.strip()
  586. if not args or args == "void":
  587. return []
  588. parts = [s.strip() for s in args.split(",")]
  589. arglist = []
  590. for part in parts:
  591. arg = self.extractarg(part)
  592. arglist.append(arg)
  593. return arglist
  594. def extractarg(self, part):
  595. mode = "InMode"
  596. part = part.strip()
  597. match = self.asplit.match(part)
  598. if not match:
  599. self.error("Indecipherable argument: %r", part)
  600. return ("unknown", part, mode)
  601. type, name, array = match.group('type', 'name', 'array')
  602. if array:
  603. # array matches an optional [] after the argument name
  604. type = type + " ptr "
  605. type = self.pythonizename(type)
  606. return self.modifyarg(type, name, mode)
  607. def modifyarg(self, type, name, mode):
  608. if type[:6] == "const_":
  609. type = type[6:]
  610. elif type[-4:] == "_ptr":
  611. type = type[:-4]
  612. mode = "OutMode"
  613. elif type in self.inherentpointertypes:
  614. mode = "OutMode"
  615. if type[-4:] == "_far":
  616. type = type[:-4]
  617. return type, name, mode
  618. def repairarglist(self, functionname, arglist):
  619. arglist = arglist[:]
  620. i = 0
  621. while i < len(arglist):
  622. for item in self.repairinstructions:
  623. if len(item) == 2:
  624. pattern, replacement = item
  625. functionpat = "*"
  626. else:
  627. functionpat, pattern, replacement = item
  628. if not fnmatch.fnmatchcase(functionname, functionpat):
  629. continue
  630. n = len(pattern)
  631. if i+n > len(arglist): continue
  632. current = arglist[i:i+n]
  633. for j in range(n):
  634. if not self.matcharg(pattern[j], current[j]):
  635. break
  636. else: # All items of the pattern match
  637. new = self.substituteargs(
  638. pattern, replacement, current)
  639. if new is not None:
  640. arglist[i:i+n] = new
  641. i = i+len(new) # No recursive substitutions
  642. break
  643. else: # No patterns match
  644. i = i+1
  645. return arglist
  646. def matcharg(self, patarg, arg):
  647. return len(filter(None, map(fnmatch.fnmatchcase, arg, patarg))) == 3
  648. def substituteargs(self, pattern, replacement, old):
  649. new = []
  650. for k in range(len(replacement)):
  651. item = replacement[k]
  652. newitem = [item[0], item[1], item[2]]
  653. for i in range(3):
  654. if item[i] == '*':
  655. newitem[i] = old[k][i]
  656. elif item[i][:1] == '$':
  657. index = int(item[i][1:]) - 1
  658. newitem[i] = old[index][i]
  659. new.append(tuple(newitem))
  660. ##self.report("old: %r", old)
  661. ##self.report("new: %r", new)
  662. return new
  663. def generate(self, tp, name, arglist, modifiers=[]):
  664. self.typeused(tp, 'return')
  665. if modifiers:
  666. classname, listname = self.destination(tp, name, arglist, modifiers)
  667. else:
  668. classname, listname = self.destination(tp, name, arglist)
  669. if not classname or not listname:
  670. self.htmlreport("*** no output generated: self.destination() returned None", klass="blacklisted")
  671. return
  672. if not self.specfile:
  673. self.htmlreport("*** no output generated: no output file specified", klass="blacklisted")
  674. return
  675. self.specfile.write("f = %s(%s, %r,\n" % (classname, tp, name))
  676. for atype, aname, amode in arglist:
  677. self.typeused(atype, amode)
  678. self.specfile.write(" (%s, %r, %s),\n" %
  679. (atype, aname, amode))
  680. if self.greydictnames.has_key(name):
  681. self.specfile.write(" condition=%r,\n"%(self.greydictnames[name],))
  682. self.generatemodifiers(classname, name, modifiers)
  683. self.specfile.write(")\n")
  684. self.specfile.write("%s.append(f)\n\n" % listname)
  685. if self.htmlfile:
  686. oline = "Adding to %s:\n%s(returntype=%s, name=%r" % (listname, classname, tp, name)
  687. for atype, aname, amode in arglist:
  688. oline += ",\n (%s, %r, %s)" % (atype, aname, amode)
  689. oline += ")\n"
  690. self.htmlreport(oline, klass="pydeclaration")
  691. def destination(self, type, name, arglist):
  692. return "FunctionGenerator", "functions"
  693. def generatemodifiers(self, classname, name, modifiers):
  694. pass
  695. def blacklisted(self, type, name):
  696. if type in self.blacklisttypes:
  697. ##self.report("return type %s is blacklisted", type)
  698. return 1
  699. if name in self.blacklistnames:
  700. ##self.report("function name %s is blacklisted", name)
  701. return 1
  702. return 0
  703. def unmanageable(self, type, name, arglist):
  704. for atype, aname, amode in arglist:
  705. if atype in self.blacklisttypes:
  706. self.report("argument type %s is blacklisted", atype)
  707. return 1
  708. return 0
  709. def htmlreport(self, line, klass=None, ranges=None):
  710. if not self.htmlfile: return
  711. if ranges is None:
  712. ranges = []
  713. if klass:
  714. ranges.insert(0, (0, len(line), klass))
  715. oline = ''
  716. i = 0
  717. for c in line:
  718. for b, e, name in ranges:
  719. if b == i:
  720. oline += '<span class="%s">' % name
  721. if e == i:
  722. oline += '</span>'
  723. i += 1
  724. if c == '<': oline += '&lt;'
  725. elif c == '>': oline += '&gt;'
  726. else: oline += c
  727. for b, e, name in ranges:
  728. if b >= i:
  729. oline += '<span class="%s">' % name
  730. if e >= i:
  731. oline += '</span>'
  732. if not line or line[-1] != '\n':
  733. oline += '\n'
  734. self.htmlfile.write(oline)
  735. class Scanner_PreUH3(Scanner):
  736. """Scanner for Universal Headers before release 3"""
  737. def initpatterns(self):
  738. Scanner.initpatterns(self)
  739. self.head_pat = "^extern pascal[ \t]+" # XXX Mac specific!
  740. self.type_pat = "pascal[ \t\n]+(?P<type>[a-zA-Z0-9_ \t]*[a-zA-Z0-9_])[ \t\n]+"
  741. self.whole_pat = self.type_pat + self.name_pat + self.args_pat
  742. self.sym_pat = "^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
  743. "[ \t]*(?P<defn>[-0-9'\"][^\t\n,;}]*),?"
  744. class Scanner_OSX(Scanner):
  745. """Scanner for modern (post UH3.3) Universal Headers """
  746. def initpatterns(self):
  747. Scanner.initpatterns(self)
  748. self.head_pat = "^EXTERN_API(_C)?"
  749. self.type_pat = "EXTERN_API(_C)?" + \
  750. "[ \t\n]*\([ \t\n]*" + \
  751. "(?P<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*])" + \
  752. "[ \t\n]*\)[ \t\n]*"
  753. self.whole_pat = self.type_pat + self.name_pat + self.args_pat
  754. self.sym_pat = "^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
  755. "[ \t]*(?P<defn>[-0-9_a-zA-Z'\"\(][^\t\n,;}]*),?"
  756. _8bit = re.compile(r"[\200-\377]")
  757. def escape8bit(s):
  758. if _8bit.search(s) is not None:
  759. out = []
  760. for c in s:
  761. o = ord(c)
  762. if o >= 128:
  763. out.append("\\" + hex(o)[1:])
  764. else:
  765. out.append(c)
  766. s = "".join(out)
  767. return s
  768. def test():
  769. input = "D:Development:THINK C:Mac #includes:Apple #includes:AppleEvents.h"
  770. output = "@aespecs.py"
  771. defsoutput = "@aedefs.py"
  772. s = Scanner(input, output, defsoutput)
  773. s.scan()
  774. if __name__ == '__main__':
  775. test()