-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.py
88 lines (67 loc) · 2.52 KB
/
rule.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
from enum import Enum
from typing import Callable, List, Union
from compiler.tokenizer import TokenType
class RuleError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class RuleElementType(Enum):
# ex) 'class'
# token type should be keyword, symbol, integerConstant, stringConstant, idenifier
FIXED_TERMINAL = 0
# ex) className
VAR_TERMINAL = 1
NON_TERMINAL = 2
# ex) ()
UNION = 3
OR = 4
ZERO_OR_ONE = 5
MULTIPLE = 6
# ex) type
REF = 7
LIST = 8
STRING_CONSTANT = 9
INTEGER_CONSTANT = 10
class RuleElement:
def __init__(self, rule_element_type: RuleElementType, desc: str):
self.element_type = rule_element_type
self.desc = desc
def compare_with_token(self, token: str) -> bool:
if self.element_type == RuleElementType.FIXED_TERMINAL:
return token == self.desc
elif self.element_type == RuleElementType.VAR_TERMINAL:
return True
raise NotImplementedError("Need to implement")
# ex) classVarDec*
class MultipleRuleElement(RuleElement):
def __init__(self, desc: str, ref: Union[RuleElement, Callable]):
super().__init__(rule_element_type=RuleElementType.MULTIPLE, desc=desc)
self.ref = ref
class ZeroOneRuleElement(RuleElement):
def __init__(self, desc: str, ref: Union[RuleElement, Callable]):
super().__init__(rule_element_type=RuleElementType.ZERO_OR_ONE, desc=desc)
self.ref = ref
# ex) group rule elements as list
class ListRuleElement(RuleElement):
def __init__(self, desc: str, rules: List[RuleElement]):
super().__init__(rule_element_type=RuleElementType.LIST, desc=desc)
self.rules = rules
# ex) type
# type => 'int' | 'char' | 'boolean' | className
class RefRuleElement(RuleElement):
def __init__(self, desc: str, ref: Union[RuleElement, Callable]):
super().__init__(rule_element_type=RuleElementType.REF, desc=desc)
self.ref = ref
def compare_with_token(self, token: str) -> bool:
return self.ref.compare_with_token(token)
# ex) 'static' | 'field'
class OrRuleElement(RuleElement):
def __init__(self, desc: str, or_elements: List[RuleElement]):
super().__init__(rule_element_type=RuleElementType.OR, desc=desc)
self.or_elements = or_elements
def compare_with_token(self, token: str) -> bool:
for element in self.or_elements:
if element.compare_with_token(token) is True:
return True
return False