aboutsummaryrefslogtreecommitdiffstats
path: root/src/wlp_parser.py
blob: e2e0d19cb5a446c320c86b584b9b58af48f60c8e (plain) (blame)
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
from __future__ import absolute_import
import rply

__lg = rply.LexerGenerator()
# Add takes a rule name, and a regular expression that defines the rule.
__lg.add("OWNER", r'<[a-zA-Z0-9_.+-]+@[a-zA-Z0-9._-]+>')
__lg.add("VAL", r'[\'`"][a-zA-Z0-9@_+.<>() -]+[\'`"]')
__lg.add("VAR", r'[a-zA-Z0-9_<>-]+[:]?')

__lg.ignore(r"\s+")
__lg.ignore(r'[{}=]+')

lexer = __lg.build()

__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
"""


@__pg.production('main : block')
def main(p):
    return p[0]


@__pg.production('block : blockstatement')
def block(p):
    return p


@__pg.production('block : block blockstatement')
def block_blockstatement(p):
    p[0].append(p[1])
    return p[0]


@__pg.production('blockstatement : OWNER commandline')
def blockstatement(p):
    return [p[0], p[1]]


@__pg.production('commandline : command')
def commandline(p):
    return p


@__pg.production('commandline : commandline command')
def commandline_command(p):
    p[0].append(p[1])
    return p[0]


@__pg.production('command : VAR VAL')
def command(p):
    return p


parser = __pg.build()