ast.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. """Python abstract syntax node definitions
  2. This file is automatically generated by Tools/compiler/astgen.py
  3. """
  4. from compiler.consts import CO_VARARGS, CO_VARKEYWORDS
  5. def flatten(seq):
  6. l = []
  7. for elt in seq:
  8. t = type(elt)
  9. if t is tuple or t is list:
  10. for elt2 in flatten(elt):
  11. l.append(elt2)
  12. else:
  13. l.append(elt)
  14. return l
  15. def flatten_nodes(seq):
  16. return [n for n in flatten(seq) if isinstance(n, Node)]
  17. nodes = {}
  18. class Node:
  19. """Abstract base class for ast nodes."""
  20. def getChildren(self):
  21. pass # implemented by subclasses
  22. def __iter__(self):
  23. for n in self.getChildren():
  24. yield n
  25. def asList(self): # for backwards compatibility
  26. return self.getChildren()
  27. def getChildNodes(self):
  28. pass # implemented by subclasses
  29. class EmptyNode(Node):
  30. pass
  31. class Expression(Node):
  32. # Expression is an artificial node class to support "eval"
  33. nodes["expression"] = "Expression"
  34. def __init__(self, node):
  35. self.node = node
  36. def getChildren(self):
  37. return self.node,
  38. def getChildNodes(self):
  39. return self.node,
  40. def __repr__(self):
  41. return "Expression(%s)" % (repr(self.node))
  42. class Add(Node):
  43. def __init__(self, leftright, lineno=None):
  44. self.left = leftright[0]
  45. self.right = leftright[1]
  46. self.lineno = lineno
  47. def getChildren(self):
  48. return self.left, self.right
  49. def getChildNodes(self):
  50. return self.left, self.right
  51. def __repr__(self):
  52. return "Add((%s, %s))" % (repr(self.left), repr(self.right))
  53. class And(Node):
  54. def __init__(self, nodes, lineno=None):
  55. self.nodes = nodes
  56. self.lineno = lineno
  57. def getChildren(self):
  58. return tuple(flatten(self.nodes))
  59. def getChildNodes(self):
  60. nodelist = []
  61. nodelist.extend(flatten_nodes(self.nodes))
  62. return tuple(nodelist)
  63. def __repr__(self):
  64. return "And(%s)" % (repr(self.nodes),)
  65. class AssAttr(Node):
  66. def __init__(self, expr, attrname, flags, lineno=None):
  67. self.expr = expr
  68. self.attrname = attrname
  69. self.flags = flags
  70. self.lineno = lineno
  71. def getChildren(self):
  72. return self.expr, self.attrname, self.flags
  73. def getChildNodes(self):
  74. return self.expr,
  75. def __repr__(self):
  76. return "AssAttr(%s, %s, %s)" % (repr(self.expr), repr(self.attrname), repr(self.flags))
  77. class AssList(Node):
  78. def __init__(self, nodes, lineno=None):
  79. self.nodes = nodes
  80. self.lineno = lineno
  81. def getChildren(self):
  82. return tuple(flatten(self.nodes))
  83. def getChildNodes(self):
  84. nodelist = []
  85. nodelist.extend(flatten_nodes(self.nodes))
  86. return tuple(nodelist)
  87. def __repr__(self):
  88. return "AssList(%s)" % (repr(self.nodes),)
  89. class AssName(Node):
  90. def __init__(self, name, flags, lineno=None):
  91. self.name = name
  92. self.flags = flags
  93. self.lineno = lineno
  94. def getChildren(self):
  95. return self.name, self.flags
  96. def getChildNodes(self):
  97. return ()
  98. def __repr__(self):
  99. return "AssName(%s, %s)" % (repr(self.name), repr(self.flags))
  100. class AssTuple(Node):
  101. def __init__(self, nodes, lineno=None):
  102. self.nodes = nodes
  103. self.lineno = lineno
  104. def getChildren(self):
  105. return tuple(flatten(self.nodes))
  106. def getChildNodes(self):
  107. nodelist = []
  108. nodelist.extend(flatten_nodes(self.nodes))
  109. return tuple(nodelist)
  110. def __repr__(self):
  111. return "AssTuple(%s)" % (repr(self.nodes),)
  112. class Assert(Node):
  113. def __init__(self, test, fail, lineno=None):
  114. self.test = test
  115. self.fail = fail
  116. self.lineno = lineno
  117. def getChildren(self):
  118. children = []
  119. children.append(self.test)
  120. children.append(self.fail)
  121. return tuple(children)
  122. def getChildNodes(self):
  123. nodelist = []
  124. nodelist.append(self.test)
  125. if self.fail is not None:
  126. nodelist.append(self.fail)
  127. return tuple(nodelist)
  128. def __repr__(self):
  129. return "Assert(%s, %s)" % (repr(self.test), repr(self.fail))
  130. class Assign(Node):
  131. def __init__(self, nodes, expr, lineno=None):
  132. self.nodes = nodes
  133. self.expr = expr
  134. self.lineno = lineno
  135. def getChildren(self):
  136. children = []
  137. children.extend(flatten(self.nodes))
  138. children.append(self.expr)
  139. return tuple(children)
  140. def getChildNodes(self):
  141. nodelist = []
  142. nodelist.extend(flatten_nodes(self.nodes))
  143. nodelist.append(self.expr)
  144. return tuple(nodelist)
  145. def __repr__(self):
  146. return "Assign(%s, %s)" % (repr(self.nodes), repr(self.expr))
  147. class AugAssign(Node):
  148. def __init__(self, node, op, expr, lineno=None):
  149. self.node = node
  150. self.op = op
  151. self.expr = expr
  152. self.lineno = lineno
  153. def getChildren(self):
  154. return self.node, self.op, self.expr
  155. def getChildNodes(self):
  156. return self.node, self.expr
  157. def __repr__(self):
  158. return "AugAssign(%s, %s, %s)" % (repr(self.node), repr(self.op), repr(self.expr))
  159. class Backquote(Node):
  160. def __init__(self, expr, lineno=None):
  161. self.expr = expr
  162. self.lineno = lineno
  163. def getChildren(self):
  164. return self.expr,
  165. def getChildNodes(self):
  166. return self.expr,
  167. def __repr__(self):
  168. return "Backquote(%s)" % (repr(self.expr),)
  169. class Bitand(Node):
  170. def __init__(self, nodes, lineno=None):
  171. self.nodes = nodes
  172. self.lineno = lineno
  173. def getChildren(self):
  174. return tuple(flatten(self.nodes))
  175. def getChildNodes(self):
  176. nodelist = []
  177. nodelist.extend(flatten_nodes(self.nodes))
  178. return tuple(nodelist)
  179. def __repr__(self):
  180. return "Bitand(%s)" % (repr(self.nodes),)
  181. class Bitor(Node):
  182. def __init__(self, nodes, lineno=None):
  183. self.nodes = nodes
  184. self.lineno = lineno
  185. def getChildren(self):
  186. return tuple(flatten(self.nodes))
  187. def getChildNodes(self):
  188. nodelist = []
  189. nodelist.extend(flatten_nodes(self.nodes))
  190. return tuple(nodelist)
  191. def __repr__(self):
  192. return "Bitor(%s)" % (repr(self.nodes),)
  193. class Bitxor(Node):
  194. def __init__(self, nodes, lineno=None):
  195. self.nodes = nodes
  196. self.lineno = lineno
  197. def getChildren(self):
  198. return tuple(flatten(self.nodes))
  199. def getChildNodes(self):
  200. nodelist = []
  201. nodelist.extend(flatten_nodes(self.nodes))
  202. return tuple(nodelist)
  203. def __repr__(self):
  204. return "Bitxor(%s)" % (repr(self.nodes),)
  205. class Break(Node):
  206. def __init__(self, lineno=None):
  207. self.lineno = lineno
  208. def getChildren(self):
  209. return ()
  210. def getChildNodes(self):
  211. return ()
  212. def __repr__(self):
  213. return "Break()"
  214. class CallFunc(Node):
  215. def __init__(self, node, args, star_args = None, dstar_args = None, lineno=None):
  216. self.node = node
  217. self.args = args
  218. self.star_args = star_args
  219. self.dstar_args = dstar_args
  220. self.lineno = lineno
  221. def getChildren(self):
  222. children = []
  223. children.append(self.node)
  224. children.extend(flatten(self.args))
  225. children.append(self.star_args)
  226. children.append(self.dstar_args)
  227. return tuple(children)
  228. def getChildNodes(self):
  229. nodelist = []
  230. nodelist.append(self.node)
  231. nodelist.extend(flatten_nodes(self.args))
  232. if self.star_args is not None:
  233. nodelist.append(self.star_args)
  234. if self.dstar_args is not None:
  235. nodelist.append(self.dstar_args)
  236. return tuple(nodelist)
  237. def __repr__(self):
  238. return "CallFunc(%s, %s, %s, %s)" % (repr(self.node), repr(self.args), repr(self.star_args), repr(self.dstar_args))
  239. class Class(Node):
  240. def __init__(self, name, bases, doc, code, decorators = None, lineno=None):
  241. self.name = name
  242. self.bases = bases
  243. self.doc = doc
  244. self.code = code
  245. self.decorators = decorators
  246. self.lineno = lineno
  247. def getChildren(self):
  248. children = []
  249. children.append(self.name)
  250. children.extend(flatten(self.bases))
  251. children.append(self.doc)
  252. children.append(self.code)
  253. children.append(self.decorators)
  254. return tuple(children)
  255. def getChildNodes(self):
  256. nodelist = []
  257. nodelist.extend(flatten_nodes(self.bases))
  258. nodelist.append(self.code)
  259. if self.decorators is not None:
  260. nodelist.append(self.decorators)
  261. return tuple(nodelist)
  262. def __repr__(self):
  263. return "Class(%s, %s, %s, %s, %s)" % (repr(self.name), repr(self.bases), repr(self.doc), repr(self.code), repr(self.decorators))
  264. class Compare(Node):
  265. def __init__(self, expr, ops, lineno=None):
  266. self.expr = expr
  267. self.ops = ops
  268. self.lineno = lineno
  269. def getChildren(self):
  270. children = []
  271. children.append(self.expr)
  272. children.extend(flatten(self.ops))
  273. return tuple(children)
  274. def getChildNodes(self):
  275. nodelist = []
  276. nodelist.append(self.expr)
  277. nodelist.extend(flatten_nodes(self.ops))
  278. return tuple(nodelist)
  279. def __repr__(self):
  280. return "Compare(%s, %s)" % (repr(self.expr), repr(self.ops))
  281. class Const(Node):
  282. def __init__(self, value, lineno=None):
  283. self.value = value
  284. self.lineno = lineno
  285. def getChildren(self):
  286. return self.value,
  287. def getChildNodes(self):
  288. return ()
  289. def __repr__(self):
  290. return "Const(%s)" % (repr(self.value),)
  291. class Continue(Node):
  292. def __init__(self, lineno=None):
  293. self.lineno = lineno
  294. def getChildren(self):
  295. return ()
  296. def getChildNodes(self):
  297. return ()
  298. def __repr__(self):
  299. return "Continue()"
  300. class Decorators(Node):
  301. def __init__(self, nodes, lineno=None):
  302. self.nodes = nodes
  303. self.lineno = lineno
  304. def getChildren(self):
  305. return tuple(flatten(self.nodes))
  306. def getChildNodes(self):
  307. nodelist = []
  308. nodelist.extend(flatten_nodes(self.nodes))
  309. return tuple(nodelist)
  310. def __repr__(self):
  311. return "Decorators(%s)" % (repr(self.nodes),)
  312. class Dict(Node):
  313. def __init__(self, items, lineno=None):
  314. self.items = items
  315. self.lineno = lineno
  316. def getChildren(self):
  317. return tuple(flatten(self.items))
  318. def getChildNodes(self):
  319. nodelist = []
  320. nodelist.extend(flatten_nodes(self.items))
  321. return tuple(nodelist)
  322. def __repr__(self):
  323. return "Dict(%s)" % (repr(self.items),)
  324. class Discard(Node):
  325. def __init__(self, expr, lineno=None):
  326. self.expr = expr
  327. self.lineno = lineno
  328. def getChildren(self):
  329. return self.expr,
  330. def getChildNodes(self):
  331. return self.expr,
  332. def __repr__(self):
  333. return "Discard(%s)" % (repr(self.expr),)
  334. class Div(Node):
  335. def __init__(self, leftright, lineno=None):
  336. self.left = leftright[0]
  337. self.right = leftright[1]
  338. self.lineno = lineno
  339. def getChildren(self):
  340. return self.left, self.right
  341. def getChildNodes(self):
  342. return self.left, self.right
  343. def __repr__(self):
  344. return "Div((%s, %s))" % (repr(self.left), repr(self.right))
  345. class Ellipsis(Node):
  346. def __init__(self, lineno=None):
  347. self.lineno = lineno
  348. def getChildren(self):
  349. return ()
  350. def getChildNodes(self):
  351. return ()
  352. def __repr__(self):
  353. return "Ellipsis()"
  354. class Exec(Node):
  355. def __init__(self, expr, locals, globals, lineno=None):
  356. self.expr = expr
  357. self.locals = locals
  358. self.globals = globals
  359. self.lineno = lineno
  360. def getChildren(self):
  361. children = []
  362. children.append(self.expr)
  363. children.append(self.locals)
  364. children.append(self.globals)
  365. return tuple(children)
  366. def getChildNodes(self):
  367. nodelist = []
  368. nodelist.append(self.expr)
  369. if self.locals is not None:
  370. nodelist.append(self.locals)
  371. if self.globals is not None:
  372. nodelist.append(self.globals)
  373. return tuple(nodelist)
  374. def __repr__(self):
  375. return "Exec(%s, %s, %s)" % (repr(self.expr), repr(self.locals), repr(self.globals))
  376. class FloorDiv(Node):
  377. def __init__(self, leftright, lineno=None):
  378. self.left = leftright[0]
  379. self.right = leftright[1]
  380. self.lineno = lineno
  381. def getChildren(self):
  382. return self.left, self.right
  383. def getChildNodes(self):
  384. return self.left, self.right
  385. def __repr__(self):
  386. return "FloorDiv((%s, %s))" % (repr(self.left), repr(self.right))
  387. class For(Node):
  388. def __init__(self, assign, list, body, else_, lineno=None):
  389. self.assign = assign
  390. self.list = list
  391. self.body = body
  392. self.else_ = else_
  393. self.lineno = lineno
  394. def getChildren(self):
  395. children = []
  396. children.append(self.assign)
  397. children.append(self.list)
  398. children.append(self.body)
  399. children.append(self.else_)
  400. return tuple(children)
  401. def getChildNodes(self):
  402. nodelist = []
  403. nodelist.append(self.assign)
  404. nodelist.append(self.list)
  405. nodelist.append(self.body)
  406. if self.else_ is not None:
  407. nodelist.append(self.else_)
  408. return tuple(nodelist)
  409. def __repr__(self):
  410. return "For(%s, %s, %s, %s)" % (repr(self.assign), repr(self.list), repr(self.body), repr(self.else_))
  411. class From(Node):
  412. def __init__(self, modname, names, level, lineno=None):
  413. self.modname = modname
  414. self.names = names
  415. self.level = level
  416. self.lineno = lineno
  417. def getChildren(self):
  418. return self.modname, self.names, self.level
  419. def getChildNodes(self):
  420. return ()
  421. def __repr__(self):
  422. return "From(%s, %s, %s)" % (repr(self.modname), repr(self.names), repr(self.level))
  423. class Function(Node):
  424. def __init__(self, decorators, name, argnames, defaults, flags, doc, code, lineno=None):
  425. self.decorators = decorators
  426. self.name = name
  427. self.argnames = argnames
  428. self.defaults = defaults
  429. self.flags = flags
  430. self.doc = doc
  431. self.code = code
  432. self.lineno = lineno
  433. self.varargs = self.kwargs = None
  434. if flags & CO_VARARGS:
  435. self.varargs = 1
  436. if flags & CO_VARKEYWORDS:
  437. self.kwargs = 1
  438. def getChildren(self):
  439. children = []
  440. children.append(self.decorators)
  441. children.append(self.name)
  442. children.append(self.argnames)
  443. children.extend(flatten(self.defaults))
  444. children.append(self.flags)
  445. children.append(self.doc)
  446. children.append(self.code)
  447. return tuple(children)
  448. def getChildNodes(self):
  449. nodelist = []
  450. if self.decorators is not None:
  451. nodelist.append(self.decorators)
  452. nodelist.extend(flatten_nodes(self.defaults))
  453. nodelist.append(self.code)
  454. return tuple(nodelist)
  455. def __repr__(self):
  456. return "Function(%s, %s, %s, %s, %s, %s, %s)" % (repr(self.decorators), repr(self.name), repr(self.argnames), repr(self.defaults), repr(self.flags), repr(self.doc), repr(self.code))
  457. class GenExpr(Node):
  458. def __init__(self, code, lineno=None):
  459. self.code = code
  460. self.lineno = lineno
  461. self.argnames = ['.0']
  462. self.varargs = self.kwargs = None
  463. def getChildren(self):
  464. return self.code,
  465. def getChildNodes(self):
  466. return self.code,
  467. def __repr__(self):
  468. return "GenExpr(%s)" % (repr(self.code),)
  469. class GenExprFor(Node):
  470. def __init__(self, assign, iter, ifs, lineno=None):
  471. self.assign = assign
  472. self.iter = iter
  473. self.ifs = ifs
  474. self.lineno = lineno
  475. self.is_outmost = False
  476. def getChildren(self):
  477. children = []
  478. children.append(self.assign)
  479. children.append(self.iter)
  480. children.extend(flatten(self.ifs))
  481. return tuple(children)
  482. def getChildNodes(self):
  483. nodelist = []
  484. nodelist.append(self.assign)
  485. nodelist.append(self.iter)
  486. nodelist.extend(flatten_nodes(self.ifs))
  487. return tuple(nodelist)
  488. def __repr__(self):
  489. return "GenExprFor(%s, %s, %s)" % (repr(self.assign), repr(self.iter), repr(self.ifs))
  490. class GenExprIf(Node):
  491. def __init__(self, test, lineno=None):
  492. self.test = test
  493. self.lineno = lineno
  494. def getChildren(self):
  495. return self.test,
  496. def getChildNodes(self):
  497. return self.test,
  498. def __repr__(self):
  499. return "GenExprIf(%s)" % (repr(self.test),)
  500. class GenExprInner(Node):
  501. def __init__(self, expr, quals, lineno=None):
  502. self.expr = expr
  503. self.quals = quals
  504. self.lineno = lineno
  505. def getChildren(self):
  506. children = []
  507. children.append(self.expr)
  508. children.extend(flatten(self.quals))
  509. return tuple(children)
  510. def getChildNodes(self):
  511. nodelist = []
  512. nodelist.append(self.expr)
  513. nodelist.extend(flatten_nodes(self.quals))
  514. return tuple(nodelist)
  515. def __repr__(self):
  516. return "GenExprInner(%s, %s)" % (repr(self.expr), repr(self.quals))
  517. class Getattr(Node):
  518. def __init__(self, expr, attrname, lineno=None):
  519. self.expr = expr
  520. self.attrname = attrname
  521. self.lineno = lineno
  522. def getChildren(self):
  523. return self.expr, self.attrname
  524. def getChildNodes(self):
  525. return self.expr,
  526. def __repr__(self):
  527. return "Getattr(%s, %s)" % (repr(self.expr), repr(self.attrname))
  528. class Global(Node):
  529. def __init__(self, names, lineno=None):
  530. self.names = names
  531. self.lineno = lineno
  532. def getChildren(self):
  533. return self.names,
  534. def getChildNodes(self):
  535. return ()
  536. def __repr__(self):
  537. return "Global(%s)" % (repr(self.names),)
  538. class If(Node):
  539. def __init__(self, tests, else_, lineno=None):
  540. self.tests = tests
  541. self.else_ = else_
  542. self.lineno = lineno
  543. def getChildren(self):
  544. children = []
  545. children.extend(flatten(self.tests))
  546. children.append(self.else_)
  547. return tuple(children)
  548. def getChildNodes(self):
  549. nodelist = []
  550. nodelist.extend(flatten_nodes(self.tests))
  551. if self.else_ is not None:
  552. nodelist.append(self.else_)
  553. return tuple(nodelist)
  554. def __repr__(self):
  555. return "If(%s, %s)" % (repr(self.tests), repr(self.else_))
  556. class IfExp(Node):
  557. def __init__(self, test, then, else_, lineno=None):
  558. self.test = test
  559. self.then = then
  560. self.else_ = else_
  561. self.lineno = lineno
  562. def getChildren(self):
  563. return self.test, self.then, self.else_
  564. def getChildNodes(self):
  565. return self.test, self.then, self.else_
  566. def __repr__(self):
  567. return "IfExp(%s, %s, %s)" % (repr(self.test), repr(self.then), repr(self.else_))
  568. class Import(Node):
  569. def __init__(self, names, lineno=None):
  570. self.names = names
  571. self.lineno = lineno
  572. def getChildren(self):
  573. return self.names,
  574. def getChildNodes(self):
  575. return ()
  576. def __repr__(self):
  577. return "Import(%s)" % (repr(self.names),)
  578. class Invert(Node):
  579. def __init__(self, expr, lineno=None):
  580. self.expr = expr
  581. self.lineno = lineno
  582. def getChildren(self):
  583. return self.expr,
  584. def getChildNodes(self):
  585. return self.expr,
  586. def __repr__(self):
  587. return "Invert(%s)" % (repr(self.expr),)
  588. class Keyword(Node):
  589. def __init__(self, name, expr, lineno=None):
  590. self.name = name
  591. self.expr = expr
  592. self.lineno = lineno
  593. def getChildren(self):
  594. return self.name, self.expr
  595. def getChildNodes(self):
  596. return self.expr,
  597. def __repr__(self):
  598. return "Keyword(%s, %s)" % (repr(self.name), repr(self.expr))
  599. class Lambda(Node):
  600. def __init__(self, argnames, defaults, flags, code, lineno=None):
  601. self.argnames = argnames
  602. self.defaults = defaults
  603. self.flags = flags
  604. self.code = code
  605. self.lineno = lineno
  606. self.varargs = self.kwargs = None
  607. if flags & CO_VARARGS:
  608. self.varargs = 1
  609. if flags & CO_VARKEYWORDS:
  610. self.kwargs = 1
  611. def getChildren(self):
  612. children = []
  613. children.append(self.argnames)
  614. children.extend(flatten(self.defaults))
  615. children.append(self.flags)
  616. children.append(self.code)
  617. return tuple(children)
  618. def getChildNodes(self):
  619. nodelist = []
  620. nodelist.extend(flatten_nodes(self.defaults))
  621. nodelist.append(self.code)
  622. return tuple(nodelist)
  623. def __repr__(self):
  624. return "Lambda(%s, %s, %s, %s)" % (repr(self.argnames), repr(self.defaults), repr(self.flags), repr(self.code))
  625. class LeftShift(Node):
  626. def __init__(self, leftright, lineno=None):
  627. self.left = leftright[0]
  628. self.right = leftright[1]
  629. self.lineno = lineno
  630. def getChildren(self):
  631. return self.left, self.right
  632. def getChildNodes(self):
  633. return self.left, self.right
  634. def __repr__(self):
  635. return "LeftShift((%s, %s))" % (repr(self.left), repr(self.right))
  636. class List(Node):
  637. def __init__(self, nodes, lineno=None):
  638. self.nodes = nodes
  639. self.lineno = lineno
  640. def getChildren(self):
  641. return tuple(flatten(self.nodes))
  642. def getChildNodes(self):
  643. nodelist = []
  644. nodelist.extend(flatten_nodes(self.nodes))
  645. return tuple(nodelist)
  646. def __repr__(self):
  647. return "List(%s)" % (repr(self.nodes),)
  648. class ListComp(Node):
  649. def __init__(self, expr, quals, lineno=None):
  650. self.expr = expr
  651. self.quals = quals
  652. self.lineno = lineno
  653. def getChildren(self):
  654. children = []
  655. children.append(self.expr)
  656. children.extend(flatten(self.quals))
  657. return tuple(children)
  658. def getChildNodes(self):
  659. nodelist = []
  660. nodelist.append(self.expr)
  661. nodelist.extend(flatten_nodes(self.quals))
  662. return tuple(nodelist)
  663. def __repr__(self):
  664. return "ListComp(%s, %s)" % (repr(self.expr), repr(self.quals))
  665. class ListCompFor(Node):
  666. def __init__(self, assign, list, ifs, lineno=None):
  667. self.assign = assign
  668. self.list = list
  669. self.ifs = ifs
  670. self.lineno = lineno
  671. def getChildren(self):
  672. children = []
  673. children.append(self.assign)
  674. children.append(self.list)
  675. children.extend(flatten(self.ifs))
  676. return tuple(children)
  677. def getChildNodes(self):
  678. nodelist = []
  679. nodelist.append(self.assign)
  680. nodelist.append(self.list)
  681. nodelist.extend(flatten_nodes(self.ifs))
  682. return tuple(nodelist)
  683. def __repr__(self):
  684. return "ListCompFor(%s, %s, %s)" % (repr(self.assign), repr(self.list), repr(self.ifs))
  685. class ListCompIf(Node):
  686. def __init__(self, test, lineno=None):
  687. self.test = test
  688. self.lineno = lineno
  689. def getChildren(self):
  690. return self.test,
  691. def getChildNodes(self):
  692. return self.test,
  693. def __repr__(self):
  694. return "ListCompIf(%s)" % (repr(self.test),)
  695. class SetComp(Node):
  696. def __init__(self, expr, quals, lineno=None):
  697. self.expr = expr
  698. self.quals = quals
  699. self.lineno = lineno
  700. def getChildren(self):
  701. children = []
  702. children.append(self.expr)
  703. children.extend(flatten(self.quals))
  704. return tuple(children)
  705. def getChildNodes(self):
  706. nodelist = []
  707. nodelist.append(self.expr)
  708. nodelist.extend(flatten_nodes(self.quals))
  709. return tuple(nodelist)
  710. def __repr__(self):
  711. return "SetComp(%s, %s)" % (repr(self.expr), repr(self.quals))
  712. class DictComp(Node):
  713. def __init__(self, key, value, quals, lineno=None):
  714. self.key = key
  715. self.value = value
  716. self.quals = quals
  717. self.lineno = lineno
  718. def getChildren(self):
  719. children = []
  720. children.append(self.key)
  721. children.append(self.value)
  722. children.extend(flatten(self.quals))
  723. return tuple(children)
  724. def getChildNodes(self):
  725. nodelist = []
  726. nodelist.append(self.key)
  727. nodelist.append(self.value)
  728. nodelist.extend(flatten_nodes(self.quals))
  729. return tuple(nodelist)
  730. def __repr__(self):
  731. return "DictComp(%s, %s, %s)" % (repr(self.key), repr(self.value), repr(self.quals))
  732. class Mod(Node):
  733. def __init__(self, leftright, lineno=None):
  734. self.left = leftright[0]
  735. self.right = leftright[1]
  736. self.lineno = lineno
  737. def getChildren(self):
  738. return self.left, self.right
  739. def getChildNodes(self):
  740. return self.left, self.right
  741. def __repr__(self):
  742. return "Mod((%s, %s))" % (repr(self.left), repr(self.right))
  743. class Module(Node):
  744. def __init__(self, doc, node, lineno=None):
  745. self.doc = doc
  746. self.node = node
  747. self.lineno = lineno
  748. def getChildren(self):
  749. return self.doc, self.node
  750. def getChildNodes(self):
  751. return self.node,
  752. def __repr__(self):
  753. return "Module(%s, %s)" % (repr(self.doc), repr(self.node))
  754. class Mul(Node):
  755. def __init__(self, leftright, lineno=None):
  756. self.left = leftright[0]
  757. self.right = leftright[1]
  758. self.lineno = lineno
  759. def getChildren(self):
  760. return self.left, self.right
  761. def getChildNodes(self):
  762. return self.left, self.right
  763. def __repr__(self):
  764. return "Mul((%s, %s))" % (repr(self.left), repr(self.right))
  765. class Name(Node):
  766. def __init__(self, name, lineno=None):
  767. self.name = name
  768. self.lineno = lineno
  769. def getChildren(self):
  770. return self.name,
  771. def getChildNodes(self):
  772. return ()
  773. def __repr__(self):
  774. return "Name(%s)" % (repr(self.name),)
  775. class Not(Node):
  776. def __init__(self, expr, lineno=None):
  777. self.expr = expr
  778. self.lineno = lineno
  779. def getChildren(self):
  780. return self.expr,
  781. def getChildNodes(self):
  782. return self.expr,
  783. def __repr__(self):
  784. return "Not(%s)" % (repr(self.expr),)
  785. class Or(Node):
  786. def __init__(self, nodes, lineno=None):
  787. self.nodes = nodes
  788. self.lineno = lineno
  789. def getChildren(self):
  790. return tuple(flatten(self.nodes))
  791. def getChildNodes(self):
  792. nodelist = []
  793. nodelist.extend(flatten_nodes(self.nodes))
  794. return tuple(nodelist)
  795. def __repr__(self):
  796. return "Or(%s)" % (repr(self.nodes),)
  797. class Pass(Node):
  798. def __init__(self, lineno=None):
  799. self.lineno = lineno
  800. def getChildren(self):
  801. return ()
  802. def getChildNodes(self):
  803. return ()
  804. def __repr__(self):
  805. return "Pass()"
  806. class Power(Node):
  807. def __init__(self, leftright, lineno=None):
  808. self.left = leftright[0]
  809. self.right = leftright[1]
  810. self.lineno = lineno
  811. def getChildren(self):
  812. return self.left, self.right
  813. def getChildNodes(self):
  814. return self.left, self.right
  815. def __repr__(self):
  816. return "Power((%s, %s))" % (repr(self.left), repr(self.right))
  817. class Print(Node):
  818. def __init__(self, nodes, dest, lineno=None):
  819. self.nodes = nodes
  820. self.dest = dest
  821. self.lineno = lineno
  822. def getChildren(self):
  823. children = []
  824. children.extend(flatten(self.nodes))
  825. children.append(self.dest)
  826. return tuple(children)
  827. def getChildNodes(self):
  828. nodelist = []
  829. nodelist.extend(flatten_nodes(self.nodes))
  830. if self.dest is not None:
  831. nodelist.append(self.dest)
  832. return tuple(nodelist)
  833. def __repr__(self):
  834. return "Print(%s, %s)" % (repr(self.nodes), repr(self.dest))
  835. class Printnl(Node):
  836. def __init__(self, nodes, dest, lineno=None):
  837. self.nodes = nodes
  838. self.dest = dest
  839. self.lineno = lineno
  840. def getChildren(self):
  841. children = []
  842. children.extend(flatten(self.nodes))
  843. children.append(self.dest)
  844. return tuple(children)
  845. def getChildNodes(self):
  846. nodelist = []
  847. nodelist.extend(flatten_nodes(self.nodes))
  848. if self.dest is not None:
  849. nodelist.append(self.dest)
  850. return tuple(nodelist)
  851. def __repr__(self):
  852. return "Printnl(%s, %s)" % (repr(self.nodes), repr(self.dest))
  853. class Raise(Node):
  854. def __init__(self, expr1, expr2, expr3, lineno=None):
  855. self.expr1 = expr1
  856. self.expr2 = expr2
  857. self.expr3 = expr3
  858. self.lineno = lineno
  859. def getChildren(self):
  860. children = []
  861. children.append(self.expr1)
  862. children.append(self.expr2)
  863. children.append(self.expr3)
  864. return tuple(children)
  865. def getChildNodes(self):
  866. nodelist = []
  867. if self.expr1 is not None:
  868. nodelist.append(self.expr1)
  869. if self.expr2 is not None:
  870. nodelist.append(self.expr2)
  871. if self.expr3 is not None:
  872. nodelist.append(self.expr3)
  873. return tuple(nodelist)
  874. def __repr__(self):
  875. return "Raise(%s, %s, %s)" % (repr(self.expr1), repr(self.expr2), repr(self.expr3))
  876. class Return(Node):
  877. def __init__(self, value, lineno=None):
  878. self.value = value
  879. self.lineno = lineno
  880. def getChildren(self):
  881. return self.value,
  882. def getChildNodes(self):
  883. return self.value,
  884. def __repr__(self):
  885. return "Return(%s)" % (repr(self.value),)
  886. class RightShift(Node):
  887. def __init__(self, leftright, lineno=None):
  888. self.left = leftright[0]
  889. self.right = leftright[1]
  890. self.lineno = lineno
  891. def getChildren(self):
  892. return self.left, self.right
  893. def getChildNodes(self):
  894. return self.left, self.right
  895. def __repr__(self):
  896. return "RightShift((%s, %s))" % (repr(self.left), repr(self.right))
  897. class Set(Node):
  898. def __init__(self, nodes, lineno=None):
  899. self.nodes = nodes
  900. self.lineno = lineno
  901. def getChildren(self):
  902. return tuple(flatten(self.nodes))
  903. def getChildNodes(self):
  904. nodelist = []
  905. nodelist.extend(flatten_nodes(self.nodes))
  906. return tuple(nodelist)
  907. def __repr__(self):
  908. return "Set(%s)" % (repr(self.nodes),)
  909. class Slice(Node):
  910. def __init__(self, expr, flags, lower, upper, lineno=None):
  911. self.expr = expr
  912. self.flags = flags
  913. self.lower = lower
  914. self.upper = upper
  915. self.lineno = lineno
  916. def getChildren(self):
  917. children = []
  918. children.append(self.expr)
  919. children.append(self.flags)
  920. children.append(self.lower)
  921. children.append(self.upper)
  922. return tuple(children)
  923. def getChildNodes(self):
  924. nodelist = []
  925. nodelist.append(self.expr)
  926. if self.lower is not None:
  927. nodelist.append(self.lower)
  928. if self.upper is not None:
  929. nodelist.append(self.upper)
  930. return tuple(nodelist)
  931. def __repr__(self):
  932. return "Slice(%s, %s, %s, %s)" % (repr(self.expr), repr(self.flags), repr(self.lower), repr(self.upper))
  933. class Sliceobj(Node):
  934. def __init__(self, nodes, lineno=None):
  935. self.nodes = nodes
  936. self.lineno = lineno
  937. def getChildren(self):
  938. return tuple(flatten(self.nodes))
  939. def getChildNodes(self):
  940. nodelist = []
  941. nodelist.extend(flatten_nodes(self.nodes))
  942. return tuple(nodelist)
  943. def __repr__(self):
  944. return "Sliceobj(%s)" % (repr(self.nodes),)
  945. class Stmt(Node):
  946. def __init__(self, nodes, lineno=None):
  947. self.nodes = nodes
  948. self.lineno = lineno
  949. def getChildren(self):
  950. return tuple(flatten(self.nodes))
  951. def getChildNodes(self):
  952. nodelist = []
  953. nodelist.extend(flatten_nodes(self.nodes))
  954. return tuple(nodelist)
  955. def __repr__(self):
  956. return "Stmt(%s)" % (repr(self.nodes),)
  957. class Sub(Node):
  958. def __init__(self, leftright, lineno=None):
  959. self.left = leftright[0]
  960. self.right = leftright[1]
  961. self.lineno = lineno
  962. def getChildren(self):
  963. return self.left, self.right
  964. def getChildNodes(self):
  965. return self.left, self.right
  966. def __repr__(self):
  967. return "Sub((%s, %s))" % (repr(self.left), repr(self.right))
  968. class Subscript(Node):
  969. def __init__(self, expr, flags, subs, lineno=None):
  970. self.expr = expr
  971. self.flags = flags
  972. self.subs = subs
  973. self.lineno = lineno
  974. def getChildren(self):
  975. children = []
  976. children.append(self.expr)
  977. children.append(self.flags)
  978. children.extend(flatten(self.subs))
  979. return tuple(children)
  980. def getChildNodes(self):
  981. nodelist = []
  982. nodelist.append(self.expr)
  983. nodelist.extend(flatten_nodes(self.subs))
  984. return tuple(nodelist)
  985. def __repr__(self):
  986. return "Subscript(%s, %s, %s)" % (repr(self.expr), repr(self.flags), repr(self.subs))
  987. class TryExcept(Node):
  988. def __init__(self, body, handlers, else_, lineno=None):
  989. self.body = body
  990. self.handlers = handlers
  991. self.else_ = else_
  992. self.lineno = lineno
  993. def getChildren(self):
  994. children = []
  995. children.append(self.body)
  996. children.extend(flatten(self.handlers))
  997. children.append(self.else_)
  998. return tuple(children)
  999. def getChildNodes(self):
  1000. nodelist = []
  1001. nodelist.append(self.body)
  1002. nodelist.extend(flatten_nodes(self.handlers))
  1003. if self.else_ is not None:
  1004. nodelist.append(self.else_)
  1005. return tuple(nodelist)
  1006. def __repr__(self):
  1007. return "TryExcept(%s, %s, %s)" % (repr(self.body), repr(self.handlers), repr(self.else_))
  1008. class TryFinally(Node):
  1009. def __init__(self, body, final, lineno=None):
  1010. self.body = body
  1011. self.final = final
  1012. self.lineno = lineno
  1013. def getChildren(self):
  1014. return self.body, self.final
  1015. def getChildNodes(self):
  1016. return self.body, self.final
  1017. def __repr__(self):
  1018. return "TryFinally(%s, %s)" % (repr(self.body), repr(self.final))
  1019. class Tuple(Node):
  1020. def __init__(self, nodes, lineno=None):
  1021. self.nodes = nodes
  1022. self.lineno = lineno
  1023. def getChildren(self):
  1024. return tuple(flatten(self.nodes))
  1025. def getChildNodes(self):
  1026. nodelist = []
  1027. nodelist.extend(flatten_nodes(self.nodes))
  1028. return tuple(nodelist)
  1029. def __repr__(self):
  1030. return "Tuple(%s)" % (repr(self.nodes),)
  1031. class UnaryAdd(Node):
  1032. def __init__(self, expr, lineno=None):
  1033. self.expr = expr
  1034. self.lineno = lineno
  1035. def getChildren(self):
  1036. return self.expr,
  1037. def getChildNodes(self):
  1038. return self.expr,
  1039. def __repr__(self):
  1040. return "UnaryAdd(%s)" % (repr(self.expr),)
  1041. class UnarySub(Node):
  1042. def __init__(self, expr, lineno=None):
  1043. self.expr = expr
  1044. self.lineno = lineno
  1045. def getChildren(self):
  1046. return self.expr,
  1047. def getChildNodes(self):
  1048. return self.expr,
  1049. def __repr__(self):
  1050. return "UnarySub(%s)" % (repr(self.expr),)
  1051. class While(Node):
  1052. def __init__(self, test, body, else_, lineno=None):
  1053. self.test = test
  1054. self.body = body
  1055. self.else_ = else_
  1056. self.lineno = lineno
  1057. def getChildren(self):
  1058. children = []
  1059. children.append(self.test)
  1060. children.append(self.body)
  1061. children.append(self.else_)
  1062. return tuple(children)
  1063. def getChildNodes(self):
  1064. nodelist = []
  1065. nodelist.append(self.test)
  1066. nodelist.append(self.body)
  1067. if self.else_ is not None:
  1068. nodelist.append(self.else_)
  1069. return tuple(nodelist)
  1070. def __repr__(self):
  1071. return "While(%s, %s, %s)" % (repr(self.test), repr(self.body), repr(self.else_))
  1072. class With(Node):
  1073. def __init__(self, expr, vars, body, lineno=None):
  1074. self.expr = expr
  1075. self.vars = vars
  1076. self.body = body
  1077. self.lineno = lineno
  1078. def getChildren(self):
  1079. children = []
  1080. children.append(self.expr)
  1081. children.append(self.vars)
  1082. children.append(self.body)
  1083. return tuple(children)
  1084. def getChildNodes(self):
  1085. nodelist = []
  1086. nodelist.append(self.expr)
  1087. if self.vars is not None:
  1088. nodelist.append(self.vars)
  1089. nodelist.append(self.body)
  1090. return tuple(nodelist)
  1091. def __repr__(self):
  1092. return "With(%s, %s, %s)" % (repr(self.expr), repr(self.vars), repr(self.body))
  1093. class Yield(Node):
  1094. def __init__(self, value, lineno=None):
  1095. self.value = value
  1096. self.lineno = lineno
  1097. def getChildren(self):
  1098. return self.value,
  1099. def getChildNodes(self):
  1100. return self.value,
  1101. def __repr__(self):
  1102. return "Yield(%s)" % (repr(self.value),)
  1103. for name, obj in globals().items():
  1104. if isinstance(obj, type) and issubclass(obj, Node):
  1105. nodes[name.lower()] = obj