Skip to content

Commit 754250c

Browse files
authored
Add methods to token class in addons (danmar#4320)
1 parent 1934386 commit 754250c

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

addons/cppcheckdata.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,45 @@ def isUnaryOp(self, op):
437437
def isBinaryOp(self):
438438
return self.astOperand1 and self.astOperand2
439439

440+
def forward(self, end=None):
441+
token = self
442+
while token and token != end:
443+
yield token
444+
token = token.next
445+
446+
def backward(self, start=None):
447+
token = self
448+
while token and token != start:
449+
yield token
450+
token = token.previous
451+
452+
def astParents(self):
453+
token = self
454+
while token and token.astParent:
455+
token = token.astParent
456+
yield token
457+
458+
def astTop(self):
459+
top = None
460+
for parent in self.astParents():
461+
top = parent
462+
return top
463+
464+
def tokAt(self, n):
465+
tl = self.forward()
466+
if n < 0:
467+
tl = self.backward()
468+
n = -n
469+
for i, t in enumerate(tl):
470+
if i == n:
471+
return t
472+
473+
def linkAt(self, n):
474+
token = self.tokAt(n)
475+
if token:
476+
return token.link
477+
return None
478+
440479
class Scope:
441480
"""
442481
Scope. Information about global scope, function scopes, class scopes, inner scopes, etc.

0 commit comments

Comments
 (0)