File: matchPreviousDemo.py

package info (click to toggle)
pyparsing 2.0.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,196 kB
  • ctags: 6,279
  • sloc: python: 11,708; makefile: 35; sh: 33
file content (33 lines) | stat: -rw-r--r-- 587 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#
# matchPreviousDemo.py
#

from pyparsing import *

src = """
class a
...
end a;
 
class b
...
end b;
 
class c
...
end d;"""


identifier = Word(alphas)

classIdent = identifier("classname")  # note that this also makes a copy of identifier 
classHead = "class" + classIdent
classBody = "..."
classEnd = "end" + matchPreviousLiteral(classIdent) + ';'
classDefn = classHead + classBody + classEnd

# use this form to catch syntax error
# classDefn = classHead + classBody - classEnd

for tokens in classDefn.searchString(src):
    print(tokens.classname)