diff options
Diffstat (limited to 'examples/linenoExample.py')
-rw-r--r-- | examples/linenoExample.py | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/examples/linenoExample.py b/examples/linenoExample.py index f343869..d21e502 100644 --- a/examples/linenoExample.py +++ b/examples/linenoExample.py @@ -15,16 +15,20 @@ to come to the aid of their country."""
# demonstrate use of lineno, line, and col in a parse action
-def reportLongWords(st,locn,toks):
+def reportLongWords(st, locn, toks):
word = toks[0]
if len(word) > 3:
- print("Found '%s' on line %d at column %d" % (word, lineno(locn,st), col(locn,st)))
+ print(
+ "Found '%s' on line %d at column %d"
+ % (word, lineno(locn, st), col(locn, st))
+ )
print("The full line of text was:")
- print("'%s'" % line(locn,st))
- print((" "*col(locn,st))+" ^")
+ print("'%s'" % line(locn, st))
+ print((" " * col(locn, st)) + " ^")
print()
-wd = Word(alphas).setParseAction( reportLongWords )
+
+wd = Word(alphas).setParseAction(reportLongWords)
OneOrMore(wd).parseString(data)
@@ -34,16 +38,19 @@ class Token: def __init__(self, st, locn, tokString):
self.tokenString = tokString
self.locn = locn
- self.sourceLine = line(locn,st)
- self.lineNo = lineno(locn,st)
- self.col = col(locn,st)
+ self.sourceLine = line(locn, st)
+ self.lineNo = lineno(locn, st)
+ self.col = col(locn, st)
+
def __str__(self):
return "%(tokenString)s (line: %(lineNo)d, col: %(col)d)" % self.__dict__
-def createTokenObject(st,locn,toks):
- return Token(st,locn, toks[0])
-wd = Word(alphas).setParseAction( createTokenObject )
+def createTokenObject(st, locn, toks):
+ return Token(st, locn, toks[0])
+
+
+wd = Word(alphas).setParseAction(createTokenObject)
for tokenObj in OneOrMore(wd).parseString(data):
print(tokenObj)
|