PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Notepad++/plugins/PythonScript/lib/compiler/symbols.py

https://gitlab.com/Kissaki/Omni-Bot-0.8
Python | 462 lines | 462 code | 0 blank | 0 comment | 0 complexity | 0c761320f020c306e702a24045f4996c MD5 | raw file
  1. """Module symbol-table generator"""
  2. from compiler import ast
  3. from compiler.consts import SC_LOCAL, SC_GLOBAL_IMPLICIT, SC_GLOBAL_EXPLICT, \
  4. SC_FREE, SC_CELL, SC_UNKNOWN
  5. from compiler.misc import mangle
  6. import types
  7. import sys
  8. MANGLE_LEN = 256
  9. class Scope:
  10. # XXX how much information do I need about each name?
  11. def __init__(self, name, module, klass=None):
  12. self.name = name
  13. self.module = module
  14. self.defs = {}
  15. self.uses = {}
  16. self.globals = {}
  17. self.params = {}
  18. self.frees = {}
  19. self.cells = {}
  20. self.children = []
  21. # nested is true if the class could contain free variables,
  22. # i.e. if it is nested within another function.
  23. self.nested = None
  24. self.generator = None
  25. self.klass = None
  26. if klass is not None:
  27. for i in range(len(klass)):
  28. if klass[i] != '_':
  29. self.klass = klass[i:]
  30. break
  31. def __repr__(self):
  32. return "<%s: %s>" % (self.__class__.__name__, self.name)
  33. def mangle(self, name):
  34. if self.klass is None:
  35. return name
  36. return mangle(name, self.klass)
  37. def add_def(self, name):
  38. self.defs[self.mangle(name)] = 1
  39. def add_use(self, name):
  40. self.uses[self.mangle(name)] = 1
  41. def add_global(self, name):
  42. name = self.mangle(name)
  43. if name in self.uses or name in self.defs:
  44. pass # XXX warn about global following def/use
  45. if name in self.params:
  46. raise SyntaxError, "%s in %s is global and parameter" % \
  47. (name, self.name)
  48. self.globals[name] = 1
  49. self.module.add_def(name)
  50. def add_param(self, name):
  51. name = self.mangle(name)
  52. self.defs[name] = 1
  53. self.params[name] = 1
  54. def get_names(self):
  55. d = {}
  56. d.update(self.defs)
  57. d.update(self.uses)
  58. d.update(self.globals)
  59. return d.keys()
  60. def add_child(self, child):
  61. self.children.append(child)
  62. def get_children(self):
  63. return self.children
  64. def DEBUG(self):
  65. print >> sys.stderr, self.name, self.nested and "nested" or ""
  66. print >> sys.stderr, "\tglobals: ", self.globals
  67. print >> sys.stderr, "\tcells: ", self.cells
  68. print >> sys.stderr, "\tdefs: ", self.defs
  69. print >> sys.stderr, "\tuses: ", self.uses
  70. print >> sys.stderr, "\tfrees:", self.frees
  71. def check_name(self, name):
  72. """Return scope of name.
  73. The scope of a name could be LOCAL, GLOBAL, FREE, or CELL.
  74. """
  75. if name in self.globals:
  76. return SC_GLOBAL_EXPLICT
  77. if name in self.cells:
  78. return SC_CELL
  79. if name in self.defs:
  80. return SC_LOCAL
  81. if self.nested and (name in self.frees or name in self.uses):
  82. return SC_FREE
  83. if self.nested:
  84. return SC_UNKNOWN
  85. else:
  86. return SC_GLOBAL_IMPLICIT
  87. def get_free_vars(self):
  88. if not self.nested:
  89. return ()
  90. free = {}
  91. free.update(self.frees)
  92. for name in self.uses.keys():
  93. if name not in self.defs and name not in self.globals:
  94. free[name] = 1
  95. return free.keys()
  96. def handle_children(self):
  97. for child in self.children:
  98. frees = child.get_free_vars()
  99. globals = self.add_frees(frees)
  100. for name in globals:
  101. child.force_global(name)
  102. def force_global(self, name):
  103. """Force name to be global in scope.
  104. Some child of the current node had a free reference to name.
  105. When the child was processed, it was labelled a free
  106. variable. Now that all its enclosing scope have been
  107. processed, the name is known to be a global or builtin. So
  108. walk back down the child chain and set the name to be global
  109. rather than free.
  110. Be careful to stop if a child does not think the name is
  111. free.
  112. """
  113. self.globals[name] = 1
  114. if name in self.frees:
  115. del self.frees[name]
  116. for child in self.children:
  117. if child.check_name(name) == SC_FREE:
  118. child.force_global(name)
  119. def add_frees(self, names):
  120. """Process list of free vars from nested scope.
  121. Returns a list of names that are either 1) declared global in the
  122. parent or 2) undefined in a top-level parent. In either case,
  123. the nested scope should treat them as globals.
  124. """
  125. child_globals = []
  126. for name in names:
  127. sc = self.check_name(name)
  128. if self.nested:
  129. if sc == SC_UNKNOWN or sc == SC_FREE \
  130. or isinstance(self, ClassScope):
  131. self.frees[name] = 1
  132. elif sc == SC_GLOBAL_IMPLICIT:
  133. child_globals.append(name)
  134. elif isinstance(self, FunctionScope) and sc == SC_LOCAL:
  135. self.cells[name] = 1
  136. elif sc != SC_CELL:
  137. child_globals.append(name)
  138. else:
  139. if sc == SC_LOCAL:
  140. self.cells[name] = 1
  141. elif sc != SC_CELL:
  142. child_globals.append(name)
  143. return child_globals
  144. def get_cell_vars(self):
  145. return self.cells.keys()
  146. class ModuleScope(Scope):
  147. __super_init = Scope.__init__
  148. def __init__(self):
  149. self.__super_init("global", self)
  150. class FunctionScope(Scope):
  151. pass
  152. class GenExprScope(Scope):
  153. __super_init = Scope.__init__
  154. __counter = 1
  155. def __init__(self, module, klass=None):
  156. i = self.__counter
  157. self.__counter += 1
  158. self.__super_init("generator expression<%d>"%i, module, klass)
  159. self.add_param('.0')
  160. def get_names(self):
  161. keys = Scope.get_names(self)
  162. return keys
  163. class LambdaScope(FunctionScope):
  164. __super_init = Scope.__init__
  165. __counter = 1
  166. def __init__(self, module, klass=None):
  167. i = self.__counter
  168. self.__counter += 1
  169. self.__super_init("lambda.%d" % i, module, klass)
  170. class ClassScope(Scope):
  171. __super_init = Scope.__init__
  172. def __init__(self, name, module):
  173. self.__super_init(name, module, name)
  174. class SymbolVisitor:
  175. def __init__(self):
  176. self.scopes = {}
  177. self.klass = None
  178. # node that define new scopes
  179. def visitModule(self, node):
  180. scope = self.module = self.scopes[node] = ModuleScope()
  181. self.visit(node.node, scope)
  182. visitExpression = visitModule
  183. def visitFunction(self, node, parent):
  184. if node.decorators:
  185. self.visit(node.decorators, parent)
  186. parent.add_def(node.name)
  187. for n in node.defaults:
  188. self.visit(n, parent)
  189. scope = FunctionScope(node.name, self.module, self.klass)
  190. if parent.nested or isinstance(parent, FunctionScope):
  191. scope.nested = 1
  192. self.scopes[node] = scope
  193. self._do_args(scope, node.argnames)
  194. self.visit(node.code, scope)
  195. self.handle_free_vars(scope, parent)
  196. def visitGenExpr(self, node, parent):
  197. scope = GenExprScope(self.module, self.klass);
  198. if parent.nested or isinstance(parent, FunctionScope) \
  199. or isinstance(parent, GenExprScope):
  200. scope.nested = 1
  201. self.scopes[node] = scope
  202. self.visit(node.code, scope)
  203. self.handle_free_vars(scope, parent)
  204. def visitGenExprInner(self, node, scope):
  205. for genfor in node.quals:
  206. self.visit(genfor, scope)
  207. self.visit(node.expr, scope)
  208. def visitGenExprFor(self, node, scope):
  209. self.visit(node.assign, scope, 1)
  210. self.visit(node.iter, scope)
  211. for if_ in node.ifs:
  212. self.visit(if_, scope)
  213. def visitGenExprIf(self, node, scope):
  214. self.visit(node.test, scope)
  215. def visitLambda(self, node, parent, assign=0):
  216. # Lambda is an expression, so it could appear in an expression
  217. # context where assign is passed. The transformer should catch
  218. # any code that has a lambda on the left-hand side.
  219. assert not assign
  220. for n in node.defaults:
  221. self.visit(n, parent)
  222. scope = LambdaScope(self.module, self.klass)
  223. if parent.nested or isinstance(parent, FunctionScope):
  224. scope.nested = 1
  225. self.scopes[node] = scope
  226. self._do_args(scope, node.argnames)
  227. self.visit(node.code, scope)
  228. self.handle_free_vars(scope, parent)
  229. def _do_args(self, scope, args):
  230. for name in args:
  231. if type(name) == types.TupleType:
  232. self._do_args(scope, name)
  233. else:
  234. scope.add_param(name)
  235. def handle_free_vars(self, scope, parent):
  236. parent.add_child(scope)
  237. scope.handle_children()
  238. def visitClass(self, node, parent):
  239. parent.add_def(node.name)
  240. for n in node.bases:
  241. self.visit(n, parent)
  242. scope = ClassScope(node.name, self.module)
  243. if parent.nested or isinstance(parent, FunctionScope):
  244. scope.nested = 1
  245. if node.doc is not None:
  246. scope.add_def('__doc__')
  247. scope.add_def('__module__')
  248. self.scopes[node] = scope
  249. prev = self.klass
  250. self.klass = node.name
  251. self.visit(node.code, scope)
  252. self.klass = prev
  253. self.handle_free_vars(scope, parent)
  254. # name can be a def or a use
  255. # XXX a few calls and nodes expect a third "assign" arg that is
  256. # true if the name is being used as an assignment. only
  257. # expressions contained within statements may have the assign arg.
  258. def visitName(self, node, scope, assign=0):
  259. if assign:
  260. scope.add_def(node.name)
  261. else:
  262. scope.add_use(node.name)
  263. # operations that bind new names
  264. def visitFor(self, node, scope):
  265. self.visit(node.assign, scope, 1)
  266. self.visit(node.list, scope)
  267. self.visit(node.body, scope)
  268. if node.else_:
  269. self.visit(node.else_, scope)
  270. def visitFrom(self, node, scope):
  271. for name, asname in node.names:
  272. if name == "*":
  273. continue
  274. scope.add_def(asname or name)
  275. def visitImport(self, node, scope):
  276. for name, asname in node.names:
  277. i = name.find(".")
  278. if i > -1:
  279. name = name[:i]
  280. scope.add_def(asname or name)
  281. def visitGlobal(self, node, scope):
  282. for name in node.names:
  283. scope.add_global(name)
  284. def visitAssign(self, node, scope):
  285. """Propagate assignment flag down to child nodes.
  286. The Assign node doesn't itself contains the variables being
  287. assigned to. Instead, the children in node.nodes are visited
  288. with the assign flag set to true. When the names occur in
  289. those nodes, they are marked as defs.
  290. Some names that occur in an assignment target are not bound by
  291. the assignment, e.g. a name occurring inside a slice. The
  292. visitor handles these nodes specially; they do not propagate
  293. the assign flag to their children.
  294. """
  295. for n in node.nodes:
  296. self.visit(n, scope, 1)
  297. self.visit(node.expr, scope)
  298. def visitAssName(self, node, scope, assign=1):
  299. scope.add_def(node.name)
  300. def visitAssAttr(self, node, scope, assign=0):
  301. self.visit(node.expr, scope, 0)
  302. def visitSubscript(self, node, scope, assign=0):
  303. self.visit(node.expr, scope, 0)
  304. for n in node.subs:
  305. self.visit(n, scope, 0)
  306. def visitSlice(self, node, scope, assign=0):
  307. self.visit(node.expr, scope, 0)
  308. if node.lower:
  309. self.visit(node.lower, scope, 0)
  310. if node.upper:
  311. self.visit(node.upper, scope, 0)
  312. def visitAugAssign(self, node, scope):
  313. # If the LHS is a name, then this counts as assignment.
  314. # Otherwise, it's just use.
  315. self.visit(node.node, scope)
  316. if isinstance(node.node, ast.Name):
  317. self.visit(node.node, scope, 1) # XXX worry about this
  318. self.visit(node.expr, scope)
  319. # prune if statements if tests are false
  320. _const_types = types.StringType, types.IntType, types.FloatType
  321. def visitIf(self, node, scope):
  322. for test, body in node.tests:
  323. if isinstance(test, ast.Const):
  324. if type(test.value) in self._const_types:
  325. if not test.value:
  326. continue
  327. self.visit(test, scope)
  328. self.visit(body, scope)
  329. if node.else_:
  330. self.visit(node.else_, scope)
  331. # a yield statement signals a generator
  332. def visitYield(self, node, scope):
  333. scope.generator = 1
  334. self.visit(node.value, scope)
  335. def list_eq(l1, l2):
  336. return sorted(l1) == sorted(l2)
  337. if __name__ == "__main__":
  338. import sys
  339. from compiler import parseFile, walk
  340. import symtable
  341. def get_names(syms):
  342. return [s for s in [s.get_name() for s in syms.get_symbols()]
  343. if not (s.startswith('_[') or s.startswith('.'))]
  344. for file in sys.argv[1:]:
  345. print file
  346. f = open(file)
  347. buf = f.read()
  348. f.close()
  349. syms = symtable.symtable(buf, file, "exec")
  350. mod_names = get_names(syms)
  351. tree = parseFile(file)
  352. s = SymbolVisitor()
  353. walk(tree, s)
  354. # compare module-level symbols
  355. names2 = s.scopes[tree].get_names()
  356. if not list_eq(mod_names, names2):
  357. print
  358. print "oops", file
  359. print sorted(mod_names)
  360. print sorted(names2)
  361. sys.exit(-1)
  362. d = {}
  363. d.update(s.scopes)
  364. del d[tree]
  365. scopes = d.values()
  366. del d
  367. for s in syms.get_symbols():
  368. if s.is_namespace():
  369. l = [sc for sc in scopes
  370. if sc.name == s.get_name()]
  371. if len(l) > 1:
  372. print "skipping", s.get_name()
  373. else:
  374. if not list_eq(get_names(s.get_namespace()),
  375. l[0].get_names()):
  376. print s.get_name()
  377. print sorted(get_names(s.get_namespace()))
  378. print sorted(l[0].get_names())
  379. sys.exit(-1)