File size: 9,707 Bytes
e4806d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from ast_parser.errors import LinterException
from ast_parser.token import (
    Token,
    TokenKind,
    is_binary_operator,
    is_relational_operator,
)


class Linter:
    """Linter enables real-time validation of the token chain.

    Args:
        source (str): The source string being tokenized.
    """

    _source: str

    _variable_provided: bool
    _relation_provided: bool

    def __init__(self, source: str) -> None:
        self._source = source

        self._variable_provided = False
        self._relation_provided = False

    def lint(self, token: Token) -> None:
        """The lint method checks the token for integrity, validity and
        relevance. It raises a LinterException if the token is invalid
        or unexpected.

        Args:
            token (Token): The starting token.
            source (str): The source string being tokenized. Used for
                exception messages.

        Raises:
            LinterException: Unexpected binary operator, term missed.
            LinterException: Equation must contain only one relational
                operator.
            LinterException: Term must contain no more than one
                variable.
            LinterException: Unexpected comma at the beginning of the
                equation.
            LinterException: Unexpected comma, equation missed.
            LinterException: Unexpected comma at the end of the
                equation.
            LinterException: Equation must contain a relational
                operator.
        """
        if token.kind != TokenKind.SOF and (
            token.prev_token is None or token.prev_token.next_token is not token
        ):
            raise LinterException(
                self._source,
                token.location,
                "Crude modification of the token chain is detected",
            )

        if token.kind == TokenKind.EOF:
            self._lint_eof(token)

        if is_binary_operator(token):
            self._lint_binary_operator(token)

            self._variable_provided = False

        if token.kind == TokenKind.MUL:
            self._lint_multiplication_operator(token)

        if is_relational_operator(token):
            self._lint_relational_operator(token)

            self._variable_provided = False
            self._relation_provided = True

        if token.kind == TokenKind.VARIABLE:
            self._lint_variable(token)

            self._variable_provided = True

        if token.kind == TokenKind.COMMA:
            self._lint_comma(token)

            self._variable_provided = False
            self._relation_provided = False

    def _lint_eof(self, token: Token) -> None:
        """Lint the EOF Token.

        Args:
            token (Token): The EOF Token.

        Raises:
            LinterException: Equation must contain a relational
                operator.
            LinterException: Unexpected binary operator at the end of
                the equation.
            LinterException: Unexpected EOF, right side of the equation
                is missed.
            LinterException: Unexpected comma at the end of the
                equation.
        """
        if token.prev_token.kind != TokenKind.SOF and not self._relation_provided:
            raise LinterException(
                self._source,
                token.location,
                "Equation must contain a relational operator",
            )
        if is_binary_operator(token.prev_token):
            raise LinterException(
                self._source,
                token.prev_token.location,
                "Unexpected binary operator at the end of the equation",
            )
        if token.prev_token.kind == TokenKind.MUL:
            raise LinterException(
                self._source,
                token.location,
                "Unexpected EOF, multiplier missed",
            )
        if is_relational_operator(token.prev_token):
            raise LinterException(
                self._source,
                token.location,
                "Unexpected EOF, right side of the equation is missed",
            )
        if token.prev_token.kind == TokenKind.COMMA:
            raise LinterException(
                self._source,
                token.location,
                "Unexpected comma at the end of the equation",
            )

    def _lint_binary_operator(self, token: Token) -> None:
        """Lint the binary operator Token.

        Args:
            token (Token): The binary operator Token.

        Raises:
            LinterException: Unexpected binary operator, term missed.
        """
        if is_binary_operator(token.prev_token):
            raise LinterException(
                self._source,
                token.location,
                "Unexpected binary operator, term missed",
            )
        if token.prev_token.kind == TokenKind.MUL:
            raise LinterException(
                self._source,
                token.location,
                "Unexpected binary operator, multiplier missed",
            )

    def _lint_multiplication_operator(self, token: Token) -> None:
        """Lint the multiplication operator Token.

        Args:
            token (Token): The multiplication operator Token.

        Raises:
            LinterException: Unexpected multiplication operator, term
                missed.
        """
        if (
            is_binary_operator(token.prev_token)
            or is_relational_operator(token.prev_token)
            or token.prev_token.kind == TokenKind.COMMA
        ):
            raise LinterException(
                self._source,
                token.location,
                "Unexpected multiplication operator, term missed",
            )
        if token.prev_token.kind == TokenKind.MUL:
            raise LinterException(
                self._source,
                token.location,
                "Unexpected multiplication operator, multiplier missed",
            )

    def _lint_relational_operator(self, token: Token) -> None:
        """Lint the relational operator Token.

        Args:
            token (Token): The relational operator Token.

        Raises:
            LinterException: Equation must contain only one relational
                operator.
            LinterException: Unexpected binary operator, term missed.
            LinterException: Unexpected relational operator, left side
                of the equation is missed.
        """
        if token.prev_token.kind in (TokenKind.SOF, TokenKind.COMMA):
            raise LinterException(
                self._source,
                token.location,
                "Unexpected relational operator, left side of the equation is missed",
            )
        if self._relation_provided:
            raise LinterException(
                self._source,
                token.location,
                "Equation must contain only one relational operator",
            )
        if is_binary_operator(token.prev_token):
            raise LinterException(
                self._source,
                token.location,
                "Unexpected binary operator, term missed",
            )
        if token.prev_token.kind == TokenKind.MUL:
            raise LinterException(
                self._source,
                token.location,
                "Unexpected relational operator, multiplier missed",
            )

    def _lint_variable(self, token: Token) -> None:
        """Lint the variable Token.

        Args:
            token (Token): The variable Token.

        Raises:
            LinterException: Term must contain no more than one
                variable.
        """
        if self._variable_provided:
            raise LinterException(
                self._source,
                token.location,
                "Term must contain no more than one variable",
            )

    def _lint_comma(self, token: Token) -> None:
        """Lint the comma Token.

        Args:
            token (Token): The comma Token.

        Raises:
            LinterException: Unexpected comma at the beginning of the
                equation.
            LinterException: Unexpected comma, equation missed.
            LinterException: Unexpected comma at the end of the
                equation.
        """
        if token.prev_token.kind == TokenKind.SOF:
            raise LinterException(
                self._source,
                token.location,
                "Unexpected comma at the beginning of the equation",
            )
        if not self._relation_provided:
            raise LinterException(
                self._source,
                token.location,
                "Equation must contain a relational operator",
            )
        if is_binary_operator(token.prev_token):
            raise LinterException(
                self._source,
                token.prev_token,
                "Unexpected binary operator at the end of the equation",
            )
        if token.prev_token.kind == TokenKind.MUL:
            raise LinterException(
                self._source,
                token.location,
                "Unexpected comma, multiplier missed",
            )
        if is_relational_operator(token.prev_token):
            raise LinterException(
                self._source,
                token.location,
                "Unexpected comma, right side of the equation is missed",
            )
        if token.prev_token.kind == TokenKind.COMMA:
            raise LinterException(
                self._source,
                token.location,
                "Unexpected comma, equation missed",
            )


__all__ = ("Linter",)