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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
import logging
import rply
logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s',
level=logging.DEBUG)
pg = rply.ParserGenerator(['OWNER', 'VAL', 'VAR'],
cache_id='wlp_parser')
"""
$accept ::= block $end
block ::= blockstatement
| block blockstatement
blockstatement ::= owner '{' commandline '}'
commandline ::= command
| commandline command
command ::= varpart '=' valpart
owner ::= OWNERID
varpart ::= VARID
valpart ::= VALID
"""
## Makes from this source
##
## <kame@innocent.com> {
## From: = 'ME' Sender: = "Cosimo" Reply-to = "me"
## }
##
## <alfarano@students.cs.unibo.it> {
## From: = 'Cosimo Alfarano'
## X-Firstname: = 'Cosimo'
## }
##
## <kame@innocent.com> {
## From: = 'kame@inwind.it'
## Reply-to: = "KA"
## Sender: = "Kalfa"
## }
##
## this dictionary
##
## expected_dict = {'alfarano@students.cs.unibo.it': {
## 'From:': 'Cosimo Alfarano',
## 'X-Firstname:': 'Cosimo'
## },
## 'kame@innocent.com': {
## 'From:': 'kame@inwind.it',
## 'Reply-to': 'me',
## 'Reply-to:': 'KA',
## 'Sender:': 'Kalfa'}
## }
@pg.production('main : block')
def main(p):
return p[0]
# block:
# blockstatement
# | block blockstatement
@pg.production('block : blockstatement')
def block(p):
logging.debug('block p = %s', p)
return p[0]
@pg.production('block : block blockstatement')
def block_blockstatement(p):
logging.debug('block p = %s', p)
return [p[0], [p[1]]]
# blockstatement:
# owner '{' commandline '}'
@pg.production('blockstatement : OWNER commandline')
def blockstatement(p):
logging.debug('blockstatement p = %s', p)
return [p[0], [p[1]]]
# commandline:
# command | commandline command
@pg.production('commandline : command')
def commandline(p):
logging.debug('commandline p = %s', p)
return p[0]
@pg.production('commandline : commandline command')
def commandline_command(p):
return p[0] + [p[1]]
# command:
# varpart '=' valpart { found(left,right,owner); }
@pg.production('command : VAR VAL')
def command(p):
logging.debug('command p = %s', p)
return [p[0], p[1]]
parser = pg.build()
|