Add methods to token class in addons (#4320)

This commit is contained in:
Paul Fultz II 2022-07-30 04:09:22 -05:00 committed by GitHub
parent 1934386738
commit 754250cd57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 0 deletions

View File

@ -437,6 +437,45 @@ class Token:
def isBinaryOp(self):
return self.astOperand1 and self.astOperand2
def forward(self, end=None):
token = self
while token and token != end:
yield token
token = token.next
def backward(self, start=None):
token = self
while token and token != start:
yield token
token = token.previous
def astParents(self):
token = self
while token and token.astParent:
token = token.astParent
yield token
def astTop(self):
top = None
for parent in self.astParents():
top = parent
return top
def tokAt(self, n):
tl = self.forward()
if n < 0:
tl = self.backward()
n = -n
for i, t in enumerate(tl):
if i == n:
return t
def linkAt(self, n):
token = self.tokAt(n)
if token:
return token.link
return None
class Scope:
"""
Scope. Information about global scope, function scopes, class scopes, inner scopes, etc.