File size: 2,430 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
from __future__ import annotations

from dataclasses import dataclass, field
from enum import Enum


class TokenKind(str, Enum):
    """Kind of the Token."""

    SOF = "<SOF>"
    """Start of file."""
    EOF = "<EOF>"
    """End of file."""

    ADD = "+"
    """Addition operator."""
    SUB = "-"
    """Subtraction operator."""
    MUL = "*"
    """Multiplication operator."""

    EQ = "="
    """Equality operator."""
    LEQ = "<="
    """Less than or equal to operator."""
    GEQ = ">="
    """Greater than or equal to operator."""

    COEFFICIENT = "Coefficient"
    """Coefficient of a variable."""
    VARIABLE = "Variable"
    """Variable name."""

    COMMA = ","
    """Comma."""


@dataclass
class Location:
    """Location of the Token in the source.

    The Location is more user-friendly than the start and end indices.
    """

    line: int
    """Line number of the token in the source. Starts at 1."""
    column: int
    """Column number of the token in the source. Starts at 1."""


@dataclass
class Token:
    """Token of the source code."""

    kind: TokenKind
    """Kind of the token."""

    start: int = field(repr=False)
    """The index of the first character of the token."""
    end: int = field(repr=False)
    """The index of the first character after the token."""
    location: Location
    """The Location of the token in the source."""

    value: str
    """The value of the token."""

    prev_token: Token | None = field(repr=False, default=None)
    """The previous Token in the source."""
    next_token: Token | None = field(repr=False, default=None)
    """The next Token in the source."""


def is_binary_operator(token: Token) -> bool:
    """Check if the Token is an algebraic binary operator.

    Args:
        token (Token): The Token to check.

    Returns:
        bool: True if the Token is an algebraic binary operator, False
            otherwise.
    """
    return token.kind in (TokenKind.ADD, TokenKind.SUB)


def is_relational_operator(token: Token) -> bool:
    """Check if the Token is a relational operator.

    Args:
        token (Token): The Token to check.

    Returns:
        bool: True if the Token is a relational operator, False
            otherwise.
    """
    return token.kind in (TokenKind.EQ, TokenKind.LEQ, TokenKind.GEQ)


__all__ = (
    "TokenKind",
    "Location",
    "Token",
    "is_binary_operator",
    "is_relational_operator",
)