File: indented_block_example.py

package info (click to toggle)
pyparsing 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 12,200 kB
  • sloc: python: 30,867; ansic: 422; sh: 112; makefile: 24
file content (54 lines) | stat: -rw-r--r-- 715 bytes parent folder | download
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#
# indented_block_example.py
#

import pyparsing as pp

ppc = pp.pyparsing_common

data = """\

    A
        100
        101

        102
    B
        200
        201
    
    C
        300

"""

integer = ppc.integer
group = pp.Group(pp.Char(pp.alphas) + pp.Group(pp.IndentedBlock(integer)))

print(group[...].parse_string(data).dump())

# example of a recursive IndentedBlock

data = """\

    A
        100
        101

        102
    B
        200
        b
            210
            211
        202
    C
        300

"""

group = pp.Forward()
group <<= pp.Group(pp.Char(pp.alphas) + pp.Group(pp.IndentedBlock(integer | group)))

print("using search_string")
print(sum(group.search_string(data)).dump())