-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.py
149 lines (113 loc) · 4.37 KB
/
validators.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
"""Additional tools for tokens validation"""
from ..lexer import BaseToken, TokenTypes
class Validators:
"""
This class is just a wrapper for functions that are used for parsing
"""
@staticmethod
def check_token_type(tokens: list[BaseToken], token_index: int, possible_types: list[int | str]) -> bool:
"""
:param tokens: list of tokens for check
:param token_index: particular token index
:param possible_types: possible types of particular token
:return: some type matches with token name
"""
token_type = tokens[token_index].type
if isinstance(possible_types[0], str):
return token_type.name in possible_types
else:
return token_type.value in possible_types
@staticmethod
def check_token_name(tokens: list[BaseToken], token_index: int, possible_names: list[str]) -> bool:
"""
:param tokens: list of tokens for check
:param token_index: particular token index
:param possible_names: possible names of particular token
:return: some name matches with token name
"""
token_name = tokens[token_index].name
return token_name in possible_names
@staticmethod
def check_token_type_presence(
tokens: list[BaseToken],
required_types: list[str | int | TokenTypes]
):
"""
:param tokens: list of tokens for check
:param required_types: types that must presence in the token list
:return: present all required types
"""
types = tuple(map(lambda t: t.type.name, tokens)) \
if isinstance(required_types[0], str) \
else tuple(map(lambda t: t.type.value, tokens))
for required_type in required_types:
if required_type not in types:
return False
return True
@staticmethod
def check_token_name_presence(tokens: list[BaseToken], required_names: list[str]):
"""
:param tokens: list of tokens for check
:param required_names: names that must presence in the token list
:return: present all required names
"""
names = tuple(map(lambda t: t.name, tokens))
for required_name in required_names:
if required_name not in names:
return False
return True
@staticmethod
def check_token_types(tokens: list[BaseToken], types: list[int]) -> bool:
"""
:param tokens: list of tokens for check
:param types: list of supposed token types in the same order as the tokens
:return: match types
"""
for token, supposed_type in zip(tokens, types):
if supposed_type is None:
continue
if token.type != supposed_type:
return False
return True
@staticmethod
def check_token_texts(tokens: list[BaseToken], texts: list[str]) -> bool:
"""
:param tokens: list of tokens for check
:param texts: list of supposed token texts in the same order as the tokens
:return: match texts
"""
for token, supposed_text in zip(tokens, texts):
if supposed_text is None:
continue
if token.text != supposed_text:
return False
return True
@staticmethod
def check_token_names(tokens: list[BaseToken], names: list[str]) -> bool:
"""
:param tokens: list of tokens for check
:param names: list of supposed token names in the same order as the tokens
:return: match names
"""
for token, supposed_name in zip(tokens, names):
if supposed_name is None:
continue
if token.name != supposed_name:
return False
return True
@staticmethod
def check_min_tokens_length(tokens: list[BaseToken], min_length: int):
"""
:param tokens: list of tokens for check
:param min_length: min length of tokens list
:return: len of tokens >= min length
"""
return len(tokens) >= min_length
@staticmethod
def check_fixed_tokens_length(tokens: list[BaseToken], length: int):
"""
:param tokens: list of tokens for check
:param length: supposed length of tokens list
:return: len of tokens == length
"""
return len(tokens) == length