How to use lineno method in Slash

Best Python code snippet using slash

ast.py

Source:ast.py Github

copy

Full Screen

1"""Python abstract syntax node definitions2This file is automatically generated by Tools/compiler/astgen.py3"""4from compiler.consts import CO_VARARGS, CO_VARKEYWORDS5def 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 l15def flatten_nodes(seq):16 return [n for n in flatten(seq) if isinstance(n, Node)]17nodes = {}18class Node:19 """Abstract base class for ast nodes."""20 def getChildren(self):21 pass # implemented by subclasses22 def __iter__(self):23 for n in self.getChildren():24 yield n25 def asList(self): # for backwards compatibility26 return self.getChildren()27 def getChildNodes(self):28 pass # implemented by subclasses29class EmptyNode(Node):30 pass31class Expression(Node):32 # Expression is an artificial node class to support "eval"33 nodes["expression"] = "Expression"34 def __init__(self, node):35 self.node = node36 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))42class Add(Node):43 def __init__(self, (left, right), lineno=None):44 self.left = left45 self.right = right46 self.lineno = lineno47 def getChildren(self):48 return self.left, self.right49 def getChildNodes(self):50 return self.left, self.right51 def __repr__(self):52 return "Add((%s, %s))" % (repr(self.left), repr(self.right))53class And(Node):54 def __init__(self, nodes, lineno=None):55 self.nodes = nodes56 self.lineno = lineno57 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),)65class AssAttr(Node):66 def __init__(self, expr, attrname, flags, lineno=None):67 self.expr = expr68 self.attrname = attrname69 self.flags = flags70 self.lineno = lineno71 def getChildren(self):72 return self.expr, self.attrname, self.flags73 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))77class AssList(Node):78 def __init__(self, nodes, lineno=None):79 self.nodes = nodes80 self.lineno = lineno81 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),)89class AssName(Node):90 def __init__(self, name, flags, lineno=None):91 self.name = name92 self.flags = flags93 self.lineno = lineno94 def getChildren(self):95 return self.name, self.flags96 def getChildNodes(self):97 return ()98 def __repr__(self):99 return "AssName(%s, %s)" % (repr(self.name), repr(self.flags))100class AssTuple(Node):101 def __init__(self, nodes, lineno=None):102 self.nodes = nodes103 self.lineno = lineno104 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),)112class Assert(Node):113 def __init__(self, test, fail, lineno=None):114 self.test = test115 self.fail = fail116 self.lineno = lineno117 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))130class Assign(Node):131 def __init__(self, nodes, expr, lineno=None):132 self.nodes = nodes133 self.expr = expr134 self.lineno = lineno135 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))147class AugAssign(Node):148 def __init__(self, node, op, expr, lineno=None):149 self.node = node150 self.op = op151 self.expr = expr152 self.lineno = lineno153 def getChildren(self):154 return self.node, self.op, self.expr155 def getChildNodes(self):156 return self.node, self.expr157 def __repr__(self):158 return "AugAssign(%s, %s, %s)" % (repr(self.node), repr(self.op), repr(self.expr))159class Backquote(Node):160 def __init__(self, expr, lineno=None):161 self.expr = expr162 self.lineno = lineno163 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),)169class Bitand(Node):170 def __init__(self, nodes, lineno=None):171 self.nodes = nodes172 self.lineno = lineno173 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),)181class Bitor(Node):182 def __init__(self, nodes, lineno=None):183 self.nodes = nodes184 self.lineno = lineno185 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),)193class Bitxor(Node):194 def __init__(self, nodes, lineno=None):195 self.nodes = nodes196 self.lineno = lineno197 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),)205class Break(Node):206 def __init__(self, lineno=None):207 self.lineno = lineno208 def getChildren(self):209 return ()210 def getChildNodes(self):211 return ()212 def __repr__(self):213 return "Break()"214class CallFunc(Node):215 def __init__(self, node, args, star_args = None, dstar_args = None, lineno=None):216 self.node = node217 self.args = args218 self.star_args = star_args219 self.dstar_args = dstar_args220 self.lineno = lineno221 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))239class Class(Node):240 def __init__(self, name, bases, doc, code, lineno=None):241 self.name = name242 self.bases = bases243 self.doc = doc244 self.code = code245 self.lineno = lineno246 def getChildren(self):247 children = []248 children.append(self.name)249 children.extend(flatten(self.bases))250 children.append(self.doc)251 children.append(self.code)252 return tuple(children)253 def getChildNodes(self):254 nodelist = []255 nodelist.extend(flatten_nodes(self.bases))256 nodelist.append(self.code)257 return tuple(nodelist)258 def __repr__(self):259 return "Class(%s, %s, %s, %s)" % (repr(self.name), repr(self.bases), repr(self.doc), repr(self.code))260class Compare(Node):261 def __init__(self, expr, ops, lineno=None):262 self.expr = expr263 self.ops = ops264 self.lineno = lineno265 def getChildren(self):266 children = []267 children.append(self.expr)268 children.extend(flatten(self.ops))269 return tuple(children)270 def getChildNodes(self):271 nodelist = []272 nodelist.append(self.expr)273 nodelist.extend(flatten_nodes(self.ops))274 return tuple(nodelist)275 def __repr__(self):276 return "Compare(%s, %s)" % (repr(self.expr), repr(self.ops))277class Const(Node):278 def __init__(self, value, lineno=None):279 self.value = value280 self.lineno = lineno281 def getChildren(self):282 return self.value,283 def getChildNodes(self):284 return ()285 def __repr__(self):286 return "Const(%s)" % (repr(self.value),)287class Continue(Node):288 def __init__(self, lineno=None):289 self.lineno = lineno290 def getChildren(self):291 return ()292 def getChildNodes(self):293 return ()294 def __repr__(self):295 return "Continue()"296class Decorators(Node):297 def __init__(self, nodes, lineno=None):298 self.nodes = nodes299 self.lineno = lineno300 def getChildren(self):301 return tuple(flatten(self.nodes))302 def getChildNodes(self):303 nodelist = []304 nodelist.extend(flatten_nodes(self.nodes))305 return tuple(nodelist)306 def __repr__(self):307 return "Decorators(%s)" % (repr(self.nodes),)308class Dict(Node):309 def __init__(self, items, lineno=None):310 self.items = items311 self.lineno = lineno312 def getChildren(self):313 return tuple(flatten(self.items))314 def getChildNodes(self):315 nodelist = []316 nodelist.extend(flatten_nodes(self.items))317 return tuple(nodelist)318 def __repr__(self):319 return "Dict(%s)" % (repr(self.items),)320class Discard(Node):321 def __init__(self, expr, lineno=None):322 self.expr = expr323 self.lineno = lineno324 def getChildren(self):325 return self.expr,326 def getChildNodes(self):327 return self.expr,328 def __repr__(self):329 return "Discard(%s)" % (repr(self.expr),)330class Div(Node):331 def __init__(self, (left, right), lineno=None):332 self.left = left333 self.right = right334 self.lineno = lineno335 def getChildren(self):336 return self.left, self.right337 def getChildNodes(self):338 return self.left, self.right339 def __repr__(self):340 return "Div((%s, %s))" % (repr(self.left), repr(self.right))341class Ellipsis(Node):342 def __init__(self, lineno=None):343 self.lineno = lineno344 def getChildren(self):345 return ()346 def getChildNodes(self):347 return ()348 def __repr__(self):349 return "Ellipsis()"350class Exec(Node):351 def __init__(self, expr, locals, globals, lineno=None):352 self.expr = expr353 self.locals = locals354 self.globals = globals355 self.lineno = lineno356 def getChildren(self):357 children = []358 children.append(self.expr)359 children.append(self.locals)360 children.append(self.globals)361 return tuple(children)362 def getChildNodes(self):363 nodelist = []364 nodelist.append(self.expr)365 if self.locals is not None:366 nodelist.append(self.locals)367 if self.globals is not None:368 nodelist.append(self.globals)369 return tuple(nodelist)370 def __repr__(self):371 return "Exec(%s, %s, %s)" % (repr(self.expr), repr(self.locals), repr(self.globals))372class FloorDiv(Node):373 def __init__(self, (left, right), lineno=None):374 self.left = left375 self.right = right376 self.lineno = lineno377 def getChildren(self):378 return self.left, self.right379 def getChildNodes(self):380 return self.left, self.right381 def __repr__(self):382 return "FloorDiv((%s, %s))" % (repr(self.left), repr(self.right))383class For(Node):384 def __init__(self, assign, list, body, else_, lineno=None):385 self.assign = assign386 self.list = list387 self.body = body388 self.else_ = else_389 self.lineno = lineno390 def getChildren(self):391 children = []392 children.append(self.assign)393 children.append(self.list)394 children.append(self.body)395 children.append(self.else_)396 return tuple(children)397 def getChildNodes(self):398 nodelist = []399 nodelist.append(self.assign)400 nodelist.append(self.list)401 nodelist.append(self.body)402 if self.else_ is not None:403 nodelist.append(self.else_)404 return tuple(nodelist)405 def __repr__(self):406 return "For(%s, %s, %s, %s)" % (repr(self.assign), repr(self.list), repr(self.body), repr(self.else_))407class From(Node):408 def __init__(self, modname, names, level, lineno=None):409 self.modname = modname410 self.names = names411 self.level = level412 self.lineno = lineno413 def getChildren(self):414 return self.modname, self.names, self.level415 def getChildNodes(self):416 return ()417 def __repr__(self):418 return "From(%s, %s, %s)" % (repr(self.modname), repr(self.names), repr(self.level))419class Function(Node):420 def __init__(self, decorators, name, argnames, defaults, flags, doc, code, lineno=None):421 self.decorators = decorators422 self.name = name423 self.argnames = argnames424 self.defaults = defaults425 self.flags = flags426 self.doc = doc427 self.code = code428 self.lineno = lineno429 self.varargs = self.kwargs = None430 if flags & CO_VARARGS:431 self.varargs = 1432 if flags & CO_VARKEYWORDS:433 self.kwargs = 1434 def getChildren(self):435 children = []436 children.append(self.decorators)437 children.append(self.name)438 children.append(self.argnames)439 children.extend(flatten(self.defaults))440 children.append(self.flags)441 children.append(self.doc)442 children.append(self.code)443 return tuple(children)444 def getChildNodes(self):445 nodelist = []446 if self.decorators is not None:447 nodelist.append(self.decorators)448 nodelist.extend(flatten_nodes(self.defaults))449 nodelist.append(self.code)450 return tuple(nodelist)451 def __repr__(self):452 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))453class GenExpr(Node):454 def __init__(self, code, lineno=None):455 self.code = code456 self.lineno = lineno457 self.argnames = ['.0']458 self.varargs = self.kwargs = None459 def getChildren(self):460 return self.code,461 def getChildNodes(self):462 return self.code,463 def __repr__(self):464 return "GenExpr(%s)" % (repr(self.code),)465class GenExprFor(Node):466 def __init__(self, assign, iter, ifs, lineno=None):467 self.assign = assign468 self.iter = iter469 self.ifs = ifs470 self.lineno = lineno471 self.is_outmost = False472 def getChildren(self):473 children = []474 children.append(self.assign)475 children.append(self.iter)476 children.extend(flatten(self.ifs))477 return tuple(children)478 def getChildNodes(self):479 nodelist = []480 nodelist.append(self.assign)481 nodelist.append(self.iter)482 nodelist.extend(flatten_nodes(self.ifs))483 return tuple(nodelist)484 def __repr__(self):485 return "GenExprFor(%s, %s, %s)" % (repr(self.assign), repr(self.iter), repr(self.ifs))486class GenExprIf(Node):487 def __init__(self, test, lineno=None):488 self.test = test489 self.lineno = lineno490 def getChildren(self):491 return self.test,492 def getChildNodes(self):493 return self.test,494 def __repr__(self):495 return "GenExprIf(%s)" % (repr(self.test),)496class GenExprInner(Node):497 def __init__(self, expr, quals, lineno=None):498 self.expr = expr499 self.quals = quals500 self.lineno = lineno501 def getChildren(self):502 children = []503 children.append(self.expr)504 children.extend(flatten(self.quals))505 return tuple(children)506 def getChildNodes(self):507 nodelist = []508 nodelist.append(self.expr)509 nodelist.extend(flatten_nodes(self.quals))510 return tuple(nodelist)511 def __repr__(self):512 return "GenExprInner(%s, %s)" % (repr(self.expr), repr(self.quals))513class Getattr(Node):514 def __init__(self, expr, attrname, lineno=None):515 self.expr = expr516 self.attrname = attrname517 self.lineno = lineno518 def getChildren(self):519 return self.expr, self.attrname520 def getChildNodes(self):521 return self.expr,522 def __repr__(self):523 return "Getattr(%s, %s)" % (repr(self.expr), repr(self.attrname))524class Global(Node):525 def __init__(self, names, lineno=None):526 self.names = names527 self.lineno = lineno528 def getChildren(self):529 return self.names,530 def getChildNodes(self):531 return ()532 def __repr__(self):533 return "Global(%s)" % (repr(self.names),)534class If(Node):535 def __init__(self, tests, else_, lineno=None):536 self.tests = tests537 self.else_ = else_538 self.lineno = lineno539 def getChildren(self):540 children = []541 children.extend(flatten(self.tests))542 children.append(self.else_)543 return tuple(children)544 def getChildNodes(self):545 nodelist = []546 nodelist.extend(flatten_nodes(self.tests))547 if self.else_ is not None:548 nodelist.append(self.else_)549 return tuple(nodelist)550 def __repr__(self):551 return "If(%s, %s)" % (repr(self.tests), repr(self.else_))552class IfExp(Node):553 def __init__(self, test, then, else_, lineno=None):554 self.test = test555 self.then = then556 self.else_ = else_557 self.lineno = lineno558 def getChildren(self):559 return self.test, self.then, self.else_560 def getChildNodes(self):561 return self.test, self.then, self.else_562 def __repr__(self):563 return "IfExp(%s, %s, %s)" % (repr(self.test), repr(self.then), repr(self.else_))564class Import(Node):565 def __init__(self, names, lineno=None):566 self.names = names567 self.lineno = lineno568 def getChildren(self):569 return self.names,570 def getChildNodes(self):571 return ()572 def __repr__(self):573 return "Import(%s)" % (repr(self.names),)574class Invert(Node):575 def __init__(self, expr, lineno=None):576 self.expr = expr577 self.lineno = lineno578 def getChildren(self):579 return self.expr,580 def getChildNodes(self):581 return self.expr,582 def __repr__(self):583 return "Invert(%s)" % (repr(self.expr),)584class Keyword(Node):585 def __init__(self, name, expr, lineno=None):586 self.name = name587 self.expr = expr588 self.lineno = lineno589 def getChildren(self):590 return self.name, self.expr591 def getChildNodes(self):592 return self.expr,593 def __repr__(self):594 return "Keyword(%s, %s)" % (repr(self.name), repr(self.expr))595class Lambda(Node):596 def __init__(self, argnames, defaults, flags, code, lineno=None):597 self.argnames = argnames598 self.defaults = defaults599 self.flags = flags600 self.code = code601 self.lineno = lineno602 self.varargs = self.kwargs = None603 if flags & CO_VARARGS:604 self.varargs = 1605 if flags & CO_VARKEYWORDS:606 self.kwargs = 1607 def getChildren(self):608 children = []609 children.append(self.argnames)610 children.extend(flatten(self.defaults))611 children.append(self.flags)612 children.append(self.code)613 return tuple(children)614 def getChildNodes(self):615 nodelist = []616 nodelist.extend(flatten_nodes(self.defaults))617 nodelist.append(self.code)618 return tuple(nodelist)619 def __repr__(self):620 return "Lambda(%s, %s, %s, %s)" % (repr(self.argnames), repr(self.defaults), repr(self.flags), repr(self.code))621class LeftShift(Node):622 def __init__(self, (left, right), lineno=None):623 self.left = left624 self.right = right625 self.lineno = lineno626 def getChildren(self):627 return self.left, self.right628 def getChildNodes(self):629 return self.left, self.right630 def __repr__(self):631 return "LeftShift((%s, %s))" % (repr(self.left), repr(self.right))632class List(Node):633 def __init__(self, nodes, lineno=None):634 self.nodes = nodes635 self.lineno = lineno636 def getChildren(self):637 return tuple(flatten(self.nodes))638 def getChildNodes(self):639 nodelist = []640 nodelist.extend(flatten_nodes(self.nodes))641 return tuple(nodelist)642 def __repr__(self):643 return "List(%s)" % (repr(self.nodes),)644class ListComp(Node):645 def __init__(self, expr, quals, lineno=None):646 self.expr = expr647 self.quals = quals648 self.lineno = lineno649 def getChildren(self):650 children = []651 children.append(self.expr)652 children.extend(flatten(self.quals))653 return tuple(children)654 def getChildNodes(self):655 nodelist = []656 nodelist.append(self.expr)657 nodelist.extend(flatten_nodes(self.quals))658 return tuple(nodelist)659 def __repr__(self):660 return "ListComp(%s, %s)" % (repr(self.expr), repr(self.quals))661class ListCompFor(Node):662 def __init__(self, assign, list, ifs, lineno=None):663 self.assign = assign664 self.list = list665 self.ifs = ifs666 self.lineno = lineno667 def getChildren(self):668 children = []669 children.append(self.assign)670 children.append(self.list)671 children.extend(flatten(self.ifs))672 return tuple(children)673 def getChildNodes(self):674 nodelist = []675 nodelist.append(self.assign)676 nodelist.append(self.list)677 nodelist.extend(flatten_nodes(self.ifs))678 return tuple(nodelist)679 def __repr__(self):680 return "ListCompFor(%s, %s, %s)" % (repr(self.assign), repr(self.list), repr(self.ifs))681class ListCompIf(Node):682 def __init__(self, test, lineno=None):683 self.test = test684 self.lineno = lineno685 def getChildren(self):686 return self.test,687 def getChildNodes(self):688 return self.test,689 def __repr__(self):690 return "ListCompIf(%s)" % (repr(self.test),)691class Mod(Node):692 def __init__(self, (left, right), lineno=None):693 self.left = left694 self.right = right695 self.lineno = lineno696 def getChildren(self):697 return self.left, self.right698 def getChildNodes(self):699 return self.left, self.right700 def __repr__(self):701 return "Mod((%s, %s))" % (repr(self.left), repr(self.right))702class Module(Node):703 def __init__(self, doc, node, lineno=None):704 self.doc = doc705 self.node = node706 self.lineno = lineno707 def getChildren(self):708 return self.doc, self.node709 def getChildNodes(self):710 return self.node,711 def __repr__(self):712 return "Module(%s, %s)" % (repr(self.doc), repr(self.node))713class Mul(Node):714 def __init__(self, (left, right), lineno=None):715 self.left = left716 self.right = right717 self.lineno = lineno718 def getChildren(self):719 return self.left, self.right720 def getChildNodes(self):721 return self.left, self.right722 def __repr__(self):723 return "Mul((%s, %s))" % (repr(self.left), repr(self.right))724class Name(Node):725 def __init__(self, name, lineno=None):726 self.name = name727 self.lineno = lineno728 def getChildren(self):729 return self.name,730 def getChildNodes(self):731 return ()732 def __repr__(self):733 return "Name(%s)" % (repr(self.name),)734class Not(Node):735 def __init__(self, expr, lineno=None):736 self.expr = expr737 self.lineno = lineno738 def getChildren(self):739 return self.expr,740 def getChildNodes(self):741 return self.expr,742 def __repr__(self):743 return "Not(%s)" % (repr(self.expr),)744class Or(Node):745 def __init__(self, nodes, lineno=None):746 self.nodes = nodes747 self.lineno = lineno748 def getChildren(self):749 return tuple(flatten(self.nodes))750 def getChildNodes(self):751 nodelist = []752 nodelist.extend(flatten_nodes(self.nodes))753 return tuple(nodelist)754 def __repr__(self):755 return "Or(%s)" % (repr(self.nodes),)756class Pass(Node):757 def __init__(self, lineno=None):758 self.lineno = lineno759 def getChildren(self):760 return ()761 def getChildNodes(self):762 return ()763 def __repr__(self):764 return "Pass()"765class Power(Node):766 def __init__(self, (left, right), lineno=None):767 self.left = left768 self.right = right769 self.lineno = lineno770 def getChildren(self):771 return self.left, self.right772 def getChildNodes(self):773 return self.left, self.right774 def __repr__(self):775 return "Power((%s, %s))" % (repr(self.left), repr(self.right))776class Print(Node):777 def __init__(self, nodes, dest, lineno=None):778 self.nodes = nodes779 self.dest = dest780 self.lineno = lineno781 def getChildren(self):782 children = []783 children.extend(flatten(self.nodes))784 children.append(self.dest)785 return tuple(children)786 def getChildNodes(self):787 nodelist = []788 nodelist.extend(flatten_nodes(self.nodes))789 if self.dest is not None:790 nodelist.append(self.dest)791 return tuple(nodelist)792 def __repr__(self):793 return "Print(%s, %s)" % (repr(self.nodes), repr(self.dest))794class Printnl(Node):795 def __init__(self, nodes, dest, lineno=None):796 self.nodes = nodes797 self.dest = dest798 self.lineno = lineno799 def getChildren(self):800 children = []801 children.extend(flatten(self.nodes))802 children.append(self.dest)803 return tuple(children)804 def getChildNodes(self):805 nodelist = []806 nodelist.extend(flatten_nodes(self.nodes))807 if self.dest is not None:808 nodelist.append(self.dest)809 return tuple(nodelist)810 def __repr__(self):811 return "Printnl(%s, %s)" % (repr(self.nodes), repr(self.dest))812class Raise(Node):813 def __init__(self, expr1, expr2, expr3, lineno=None):814 self.expr1 = expr1815 self.expr2 = expr2816 self.expr3 = expr3817 self.lineno = lineno818 def getChildren(self):819 children = []820 children.append(self.expr1)821 children.append(self.expr2)822 children.append(self.expr3)823 return tuple(children)824 def getChildNodes(self):825 nodelist = []826 if self.expr1 is not None:827 nodelist.append(self.expr1)828 if self.expr2 is not None:829 nodelist.append(self.expr2)830 if self.expr3 is not None:831 nodelist.append(self.expr3)832 return tuple(nodelist)833 def __repr__(self):834 return "Raise(%s, %s, %s)" % (repr(self.expr1), repr(self.expr2), repr(self.expr3))835class Return(Node):836 def __init__(self, value, lineno=None):837 self.value = value838 self.lineno = lineno839 def getChildren(self):840 return self.value,841 def getChildNodes(self):842 return self.value,843 def __repr__(self):844 return "Return(%s)" % (repr(self.value),)845class RightShift(Node):846 def __init__(self, (left, right), lineno=None):847 self.left = left848 self.right = right849 self.lineno = lineno850 def getChildren(self):851 return self.left, self.right852 def getChildNodes(self):853 return self.left, self.right854 def __repr__(self):855 return "RightShift((%s, %s))" % (repr(self.left), repr(self.right))856class Slice(Node):857 def __init__(self, expr, flags, lower, upper, lineno=None):858 self.expr = expr859 self.flags = flags860 self.lower = lower861 self.upper = upper862 self.lineno = lineno863 def getChildren(self):864 children = []865 children.append(self.expr)866 children.append(self.flags)867 children.append(self.lower)868 children.append(self.upper)869 return tuple(children)870 def getChildNodes(self):871 nodelist = []872 nodelist.append(self.expr)873 if self.lower is not None:874 nodelist.append(self.lower)875 if self.upper is not None:876 nodelist.append(self.upper)877 return tuple(nodelist)878 def __repr__(self):879 return "Slice(%s, %s, %s, %s)" % (repr(self.expr), repr(self.flags), repr(self.lower), repr(self.upper))880class Sliceobj(Node):881 def __init__(self, nodes, lineno=None):882 self.nodes = nodes883 self.lineno = lineno884 def getChildren(self):885 return tuple(flatten(self.nodes))886 def getChildNodes(self):887 nodelist = []888 nodelist.extend(flatten_nodes(self.nodes))889 return tuple(nodelist)890 def __repr__(self):891 return "Sliceobj(%s)" % (repr(self.nodes),)892class Stmt(Node):893 def __init__(self, nodes, lineno=None):894 self.nodes = nodes895 self.lineno = lineno896 def getChildren(self):897 return tuple(flatten(self.nodes))898 def getChildNodes(self):899 nodelist = []900 nodelist.extend(flatten_nodes(self.nodes))901 return tuple(nodelist)902 def __repr__(self):903 return "Stmt(%s)" % (repr(self.nodes),)904class Sub(Node):905 def __init__(self, (left, right), lineno=None):906 self.left = left907 self.right = right908 self.lineno = lineno909 def getChildren(self):910 return self.left, self.right911 def getChildNodes(self):912 return self.left, self.right913 def __repr__(self):914 return "Sub((%s, %s))" % (repr(self.left), repr(self.right))915class Subscript(Node):916 def __init__(self, expr, flags, subs, lineno=None):917 self.expr = expr918 self.flags = flags919 self.subs = subs920 self.lineno = lineno921 def getChildren(self):922 children = []923 children.append(self.expr)924 children.append(self.flags)925 children.extend(flatten(self.subs))926 return tuple(children)927 def getChildNodes(self):928 nodelist = []929 nodelist.append(self.expr)930 nodelist.extend(flatten_nodes(self.subs))931 return tuple(nodelist)932 def __repr__(self):933 return "Subscript(%s, %s, %s)" % (repr(self.expr), repr(self.flags), repr(self.subs))934class TryExcept(Node):935 def __init__(self, body, handlers, else_, lineno=None):936 self.body = body937 self.handlers = handlers938 self.else_ = else_939 self.lineno = lineno940 def getChildren(self):941 children = []942 children.append(self.body)943 children.extend(flatten(self.handlers))944 children.append(self.else_)945 return tuple(children)946 def getChildNodes(self):947 nodelist = []948 nodelist.append(self.body)949 nodelist.extend(flatten_nodes(self.handlers))950 if self.else_ is not None:951 nodelist.append(self.else_)952 return tuple(nodelist)953 def __repr__(self):954 return "TryExcept(%s, %s, %s)" % (repr(self.body), repr(self.handlers), repr(self.else_))955class TryFinally(Node):956 def __init__(self, body, final, lineno=None):957 self.body = body958 self.final = final959 self.lineno = lineno960 def getChildren(self):961 return self.body, self.final962 def getChildNodes(self):963 return self.body, self.final964 def __repr__(self):965 return "TryFinally(%s, %s)" % (repr(self.body), repr(self.final))966class Tuple(Node):967 def __init__(self, nodes, lineno=None):968 self.nodes = nodes969 self.lineno = lineno970 def getChildren(self):971 return tuple(flatten(self.nodes))972 def getChildNodes(self):973 nodelist = []974 nodelist.extend(flatten_nodes(self.nodes))975 return tuple(nodelist)976 def __repr__(self):977 return "Tuple(%s)" % (repr(self.nodes),)978class UnaryAdd(Node):979 def __init__(self, expr, lineno=None):980 self.expr = expr981 self.lineno = lineno982 def getChildren(self):983 return self.expr,984 def getChildNodes(self):985 return self.expr,986 def __repr__(self):987 return "UnaryAdd(%s)" % (repr(self.expr),)988class UnarySub(Node):989 def __init__(self, expr, lineno=None):990 self.expr = expr991 self.lineno = lineno992 def getChildren(self):993 return self.expr,994 def getChildNodes(self):995 return self.expr,996 def __repr__(self):997 return "UnarySub(%s)" % (repr(self.expr),)998class While(Node):999 def __init__(self, test, body, else_, lineno=None):1000 self.test = test1001 self.body = body1002 self.else_ = else_1003 self.lineno = lineno1004 def getChildren(self):1005 children = []1006 children.append(self.test)1007 children.append(self.body)1008 children.append(self.else_)1009 return tuple(children)1010 def getChildNodes(self):1011 nodelist = []1012 nodelist.append(self.test)1013 nodelist.append(self.body)1014 if self.else_ is not None:1015 nodelist.append(self.else_)1016 return tuple(nodelist)1017 def __repr__(self):1018 return "While(%s, %s, %s)" % (repr(self.test), repr(self.body), repr(self.else_))1019class With(Node):1020 def __init__(self, expr, vars, body, lineno=None):1021 self.expr = expr1022 self.vars = vars1023 self.body = body1024 self.lineno = lineno1025 def getChildren(self):1026 children = []1027 children.append(self.expr)1028 children.append(self.vars)1029 children.append(self.body)1030 return tuple(children)1031 def getChildNodes(self):1032 nodelist = []1033 nodelist.append(self.expr)1034 if self.vars is not None:1035 nodelist.append(self.vars)1036 nodelist.append(self.body)1037 return tuple(nodelist)1038 def __repr__(self):1039 return "With(%s, %s, %s)" % (repr(self.expr), repr(self.vars), repr(self.body))1040class Yield(Node):1041 def __init__(self, value, lineno=None):1042 self.value = value1043 self.lineno = lineno1044 def getChildren(self):1045 return self.value,1046 def getChildNodes(self):1047 return self.value,1048 def __repr__(self):1049 return "Yield(%s)" % (repr(self.value),)1050for name, obj in globals().items():1051 if isinstance(obj, type) and issubclass(obj, Node):...

Full Screen

Full Screen

conv.py

Source:conv.py Github

copy

Full Screen

1# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.2# Licensed to PSF under a Contributor Agreement.3"""Convert graminit.[ch] spit out by pgen to Python code.4Pgen is the Python parser generator. It is useful to quickly create a5parser from a grammar file in Python's grammar notation. But I don't6want my parsers to be written in C (yet), so I'm translating the7parsing tables to Python data structures and writing a Python parse8engine.9Note that the token numbers are constants determined by the standard10Python tokenizer. The standard token module defines these numbers and11their names (the names are not used much). The token numbers are12hardcoded into the Python tokenizer and into pgen. A Python13implementation of the Python tokenizer is also available, in the14standard tokenize module.15On the other hand, symbol numbers (representing the grammar's16non-terminals) are assigned by pgen based on the actual grammar17input.18Note: this module is pretty much obsolete; the pgen module generates19equivalent grammar tables directly from the Grammar.txt input file20without having to invoke the Python pgen C program.21"""22# Python imports23import re24# Local imports25from pgen2 import grammar, token26class Converter(grammar.Grammar):27 """Grammar subclass that reads classic pgen output files.28 The run() method reads the tables as produced by the pgen parser29 generator, typically contained in two C files, graminit.h and30 graminit.c. The other methods are for internal use only.31 See the base class for more documentation.32 """33 def run(self, graminit_h, graminit_c):34 """Load the grammar tables from the text files written by pgen."""35 self.parse_graminit_h(graminit_h)36 self.parse_graminit_c(graminit_c)37 self.finish_off()38 def parse_graminit_h(self, filename):39 """Parse the .h file written by pgen. (Internal)40 This file is a sequence of #define statements defining the41 nonterminals of the grammar as numbers. We build two tables42 mapping the numbers to names and back.43 """44 try:45 f = open(filename)46 except OSError as err:47 print("Can't open %s: %s" % (filename, err))48 return False49 self.symbol2number = {}50 self.number2symbol = {}51 lineno = 052 for line in f:53 lineno += 154 mo = re.match(r"^#define\s+(\w+)\s+(\d+)$", line)55 if not mo and line.strip():56 print("%s(%s): can't parse %s" % (filename, lineno,57 line.strip()))58 else:59 symbol, number = mo.groups()60 number = int(number)61 assert symbol not in self.symbol2number62 assert number not in self.number2symbol63 self.symbol2number[symbol] = number64 self.number2symbol[number] = symbol65 return True66 def parse_graminit_c(self, filename):67 """Parse the .c file written by pgen. (Internal)68 The file looks as follows. The first two lines are always this:69 #include "pgenheaders.h"70 #include "grammar.h"71 After that come four blocks:72 1) one or more state definitions73 2) a table defining dfas74 3) a table defining labels75 4) a struct defining the grammar76 A state definition has the following form:77 - one or more arc arrays, each of the form:78 static arc arcs_<n>_<m>[<k>] = {79 {<i>, <j>},80 ...81 };82 - followed by a state array, of the form:83 static state states_<s>[<t>] = {84 {<k>, arcs_<n>_<m>},85 ...86 };87 """88 try:89 f = open(filename)90 except OSError as err:91 print("Can't open %s: %s" % (filename, err))92 return False93 # The code below essentially uses f's iterator-ness!94 lineno = 095 # Expect the two #include lines96 lineno, line = lineno+1, next(f)97 assert line == '#include "pgenheaders.h"\n', (lineno, line)98 lineno, line = lineno+1, next(f)99 assert line == '#include "grammar.h"\n', (lineno, line)100 # Parse the state definitions101 lineno, line = lineno+1, next(f)102 allarcs = {}103 states = []104 while line.startswith("static arc "):105 while line.startswith("static arc "):106 mo = re.match(r"static arc arcs_(\d+)_(\d+)\[(\d+)\] = {$",107 line)108 assert mo, (lineno, line)109 n, m, k = list(map(int, mo.groups()))110 arcs = []111 for _ in range(k):112 lineno, line = lineno+1, next(f)113 mo = re.match(r"\s+{(\d+), (\d+)},$", line)114 assert mo, (lineno, line)115 i, j = list(map(int, mo.groups()))116 arcs.append((i, j))117 lineno, line = lineno+1, next(f)118 assert line == "};\n", (lineno, line)119 allarcs[(n, m)] = arcs120 lineno, line = lineno+1, next(f)121 mo = re.match(r"static state states_(\d+)\[(\d+)\] = {$", line)122 assert mo, (lineno, line)123 s, t = list(map(int, mo.groups()))124 assert s == len(states), (lineno, line)125 state = []126 for _ in range(t):127 lineno, line = lineno+1, next(f)128 mo = re.match(r"\s+{(\d+), arcs_(\d+)_(\d+)},$", line)129 assert mo, (lineno, line)130 k, n, m = list(map(int, mo.groups()))131 arcs = allarcs[n, m]132 assert k == len(arcs), (lineno, line)133 state.append(arcs)134 states.append(state)135 lineno, line = lineno+1, next(f)136 assert line == "};\n", (lineno, line)137 lineno, line = lineno+1, next(f)138 self.states = states139 # Parse the dfas140 dfas = {}141 mo = re.match(r"static dfa dfas\[(\d+)\] = {$", line)142 assert mo, (lineno, line)143 ndfas = int(mo.group(1))144 for i in range(ndfas):145 lineno, line = lineno+1, next(f)146 mo = re.match(r'\s+{(\d+), "(\w+)", (\d+), (\d+), states_(\d+),$',147 line)148 assert mo, (lineno, line)149 symbol = mo.group(2)150 number, x, y, z = list(map(int, mo.group(1, 3, 4, 5)))151 assert self.symbol2number[symbol] == number, (lineno, line)152 assert self.number2symbol[number] == symbol, (lineno, line)153 assert x == 0, (lineno, line)154 state = states[z]155 assert y == len(state), (lineno, line)156 lineno, line = lineno+1, next(f)157 mo = re.match(r'\s+("(?:\\\d\d\d)*")},$', line)158 assert mo, (lineno, line)159 first = {}160 rawbitset = eval(mo.group(1))161 for i, c in enumerate(rawbitset):162 byte = ord(c)163 for j in range(8):164 if byte & (1<<j):165 first[i*8 + j] = 1166 dfas[number] = (state, first)167 lineno, line = lineno+1, next(f)168 assert line == "};\n", (lineno, line)169 self.dfas = dfas170 # Parse the labels171 labels = []172 lineno, line = lineno+1, next(f)173 mo = re.match(r"static label labels\[(\d+)\] = {$", line)174 assert mo, (lineno, line)175 nlabels = int(mo.group(1))176 for i in range(nlabels):177 lineno, line = lineno+1, next(f)178 mo = re.match(r'\s+{(\d+), (0|"\w+")},$', line)179 assert mo, (lineno, line)180 x, y = mo.groups()181 x = int(x)182 if y == "0":183 y = None184 else:185 y = eval(y)186 labels.append((x, y))187 lineno, line = lineno+1, next(f)188 assert line == "};\n", (lineno, line)189 self.labels = labels190 # Parse the grammar struct191 lineno, line = lineno+1, next(f)192 assert line == "grammar _PyParser_Grammar = {\n", (lineno, line)193 lineno, line = lineno+1, next(f)194 mo = re.match(r"\s+(\d+),$", line)195 assert mo, (lineno, line)196 ndfas = int(mo.group(1))197 assert ndfas == len(self.dfas)198 lineno, line = lineno+1, next(f)199 assert line == "\tdfas,\n", (lineno, line)200 lineno, line = lineno+1, next(f)201 mo = re.match(r"\s+{(\d+), labels},$", line)202 assert mo, (lineno, line)203 nlabels = int(mo.group(1))204 assert nlabels == len(self.labels), (lineno, line)205 lineno, line = lineno+1, next(f)206 mo = re.match(r"\s+(\d+)$", line)207 assert mo, (lineno, line)208 start = int(mo.group(1))209 assert start in self.number2symbol, (lineno, line)210 self.start = start211 lineno, line = lineno+1, next(f)212 assert line == "};\n", (lineno, line)213 try:214 lineno, line = lineno+1, next(f)215 except StopIteration:216 pass217 else:218 assert 0, (lineno, line)219 def finish_off(self):220 """Create additional useful structures. (Internal)."""221 self.keywords = {} # map from keyword strings to arc labels222 self.tokens = {} # map from numeric token values to arc labels223 for ilabel, (type, value) in enumerate(self.labels):224 if type == token.NAME and value is not None:225 self.keywords[value] = ilabel226 elif value is None:...

Full Screen

Full Screen

messages.py

Source:messages.py Github

copy

Full Screen

1# (c) 2005 Divmod, Inc. See LICENSE file for details2class Message(object):3 message = ''4 message_args = ()5 def __init__(self, filename, lineno):6 self.filename = filename7 self.lineno = lineno8 def __str__(self):9 return '%s:%s: %s' % (self.filename, self.lineno, self.message % self.message_args)10class UnusedImport(Message):11 message = '%r imported but unused'12 def __init__(self, filename, lineno, name):13 Message.__init__(self, filename, lineno)14 self.message_args = (name,)15class RedefinedWhileUnused(Message):16 message = 'redefinition of unused %r from line %r'17 def __init__(self, filename, lineno, name, orig_lineno):18 Message.__init__(self, filename, lineno)19 self.message_args = (name, orig_lineno)20class ImportShadowedByLoopVar(Message):21 message = 'import %r from line %r shadowed by loop variable'22 def __init__(self, filename, lineno, name, orig_lineno):23 Message.__init__(self, filename, lineno)24 self.message_args = (name, orig_lineno)25class ImportStarUsed(Message):26 message = "'from %s import *' used; unable to detect undefined names"27 def __init__(self, filename, lineno, modname):28 Message.__init__(self, filename, lineno)29 self.message_args = (modname,)30class UndefinedName(Message):31 message = 'undefined name %r'32 def __init__(self, filename, lineno, name):33 Message.__init__(self, filename, lineno)34 self.message_args = (name,)35class UndefinedExport(Message):36 message = 'undefined name %r in __all__'37 def __init__(self, filename, lineno, name):38 Message.__init__(self, filename, lineno)39 self.message_args = (name,)40class UndefinedLocal(Message):41 message = "local variable %r (defined in enclosing scope on line %r) referenced before assignment"42 def __init__(self, filename, lineno, name, orig_lineno):43 Message.__init__(self, filename, lineno)44 self.message_args = (name, orig_lineno)45class DuplicateArgument(Message):46 message = 'duplicate argument %r in function definition'47 def __init__(self, filename, lineno, name):48 Message.__init__(self, filename, lineno)49 self.message_args = (name,)50class RedefinedFunction(Message):51 message = 'redefinition of function %r from line %r'52 def __init__(self, filename, lineno, name, orig_lineno):53 Message.__init__(self, filename, lineno)54 self.message_args = (name, orig_lineno)55class LateFutureImport(Message):56 message = 'future import(s) %r after other statements'57 def __init__(self, filename, lineno, names):58 Message.__init__(self, filename, lineno)59 self.message_args = (names,)60class UnusedVariable(Message):61 """62 Indicates that a variable has been explicity assigned to but not actually63 used.64 """65 message = 'local variable %r is assigned to but never used'66 def __init__(self, filename, lineno, names):67 Message.__init__(self, filename, lineno)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful