-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.py
181 lines (153 loc) · 4.1 KB
/
Parser.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from pyparsing import *
if_condition = (
Suppress(Literal("[")) +
Word(printables).setResultsName("subject") +
Word(alphanums).setResultsName("verb") +
Optional(
Word(alphanums).setResultsName("object")
) +
Suppress(Literal("]"))
)
else_condition = (
Suppress(Literal("[else]"))
)
condition_text = (
SkipTo(Literal("[") | Literal("}")).setParseAction(lambda t: t[0].strip())
)
condition_block = Group(
Suppress(Literal("{")) +
Group(
if_condition.setResultsName("condition") +
condition_text.setResultsName("condition_text")
).setResultsName("if_condition") +
Group(
Optional(
else_condition +
condition_text.setResultsName("condition_text")
)
).setResultsName("else_condition") +
Suppress(Literal("}"))
)
desc_block = Group(
OneOrMore(
condition_block |
Word(printables)
)
)
local_variable = (
Suppress(Literal(".")) +
Word(alphanums).setParseAction(lambda t: "." + t[0])
)
room_variables = Group(
Suppress(
Literal("Variables:") +
LineEnd()
) +
OneOrMore(
Group(
local_variable +
Suppress(Literal("="))+
Word(alphanums) +
Suppress(LineEnd())
)
)
).setResultsName("local_variables")
path = Group(
Word(alphanums).setResultsName("target") +
Suppress(Literal("->")) +
Word(alphanums).setResultsName("destination")
)
command = Group(
OneOrMore(Word(alphanums)).setParseAction(lambda t: " ".join(t)) +
Suppress(
Literal(":") +
LineEnd()
) +
Group(OneOrMore(
Word(alphanums).setResultsName("function") +
Group(OneOrMore(
local_variable |
path |
Word(alphanums) +
Suppress(LineEnd())
)).setResultsName("parameters")
)) +
Suppress(OneOrMore(LineEnd()))
)
room_commands = Group(
Suppress(
Literal("Commands:") +
LineEnd()
) +
OneOrMore(command)
).setResultsName("commands")
room_ID = (
Suppress(Literal("+")) +
Word(alphanums).setResultsName("id") +
Suppress(Literal(":")) +
Suppress(OneOrMore(LineEnd()))
)
# room_name = Literal("Name:") + desc_block + Suppress(OneOrMore(LineEnd()))
room_name = (
Suppress(Literal("Name:")) +
SkipTo(LineEnd()).setParseAction(lambda t: desc_block.parseString(str(t[0])))
)
room_desc = (
Suppress(Literal("Description:")) +
SkipTo(LineEnd()).setParseAction(lambda t: desc_block.parseString(str(t[0]))[0])
).setResultsName("description")
# room_desc = Literal("Description:") + desc_block + Suppress(OneOrMore(LineEnd()))
# room_desc = Literal("Description:") + OneOrMore(Word(printables)) + Suppress(LineEnd())
room_paths = Group(
Suppress(Literal("Paths:") + LineEnd()) +
OneOrMore(
path +
Suppress(LineEnd())
)
).setResultsName("paths")
room = Group(
room_ID +
room_name &
room_desc &
Optional(room_commands) &
Optional(room_variables) &
Optional(room_paths)
)
combined = OneOrMore(
OneOrMore(
room
)
)
parsed = """
+room1:
Name: A Room
Description: sfdlk jklf jdksl fds {[dfsj f] djfd sds [else] slkd sd dsfaf ksd } hi
Commands:
throw switch:
enable .lights
do something
throw switch2:
enable .lights
do something
Variables:
.lights = true
.an = off
Paths:
north -> house
south -> room2
"""
# print(local_variable.parseString(".hi"))
# print(roomCommands.parseString(parsed))
print(combined.parseString(parsed)[0])
# roomDesc
"""
TODO: need parsed condition block to be tuple where first item is key (but not
dictionary because can have duplicates). For the moment, since there currently
arent any other description-based entities beside conditional blocks.
"""
# with open("testInput2") as raw:
# parsed = parseFile(raw)
# parsed = desc_block.parseString("sfdlk jklf jdksl fds {[dfsj f] djfd sds [else] slkd sd dsfaf ksd } hi")
# print(parsed)
# print(desc_block.parseString("sfdlk jklf jdksl fds {[dfsj f] djfd sds [else] slkd sd dsfaf ksd } hi"))
# print(type(parsed.asList()[0]))