-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
64 lines (53 loc) · 1.76 KB
/
parser.py
File metadata and controls
64 lines (53 loc) · 1.76 KB
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
from lexer import TokenTypes
class Parser(object):
def __init__(self, tokens=None):
if tokens is None:
tokens = []
self.tokens = tokens
def parse(self):
children = []
child = []
for token in self.tokens:
if token.type != TokenTypes.AND:
child.append(token)
else:
children.append(self.prereqs(child))
child = []
children.append(self.prereqs(child))
if len(children) == 1:
return children[0]
return AndNode(children)
def prereqs(self, tokens):
if tokens[0].type == TokenTypes.COURSENAME:
return Course(tokens[0].value)
elif tokens[0].type == TokenTypes.ALLOF:
return AndNode(self.course_list(tokens[1:]))
elif tokens[0].type == TokenTypes.ONEOF:
return OrNode(self.course_list(tokens[1:]))
elif tokens[0].type == TokenTypes.EITHERA:
return self.either_or(tokens)
else:
exit(1) # you fucked up!
def course_list(self, tokens):
return list(map(lambda token: Course(token.value), tokens))
def either_or(self, tokens):
either_a = []
either_b = []
for token in tokens[1:]:
if token.type == TokenTypes.EITHERB:
either_a = either_b.copy()
either_b = []
else:
either_b.append(token)
return OrNode([self.prereqs(either_a), self.prereqs(either_b)])
class AST():
pass
class AndNode(AST):
def __init__(self, params):
self.children = params
class OrNode(AST):
def __init__(self, params):
self.children = params
class Course(AST):
def __init__(self, param):
self.name = param