|
1 | | -from copy import copy |
2 | | -from enum import Enum |
3 | | -from typing import List, Optional |
| 1 | +from typing import List |
4 | 2 |
|
5 | 3 | from ..error import GraphQLSyntaxError |
6 | | -from .source import Source |
| 4 | +from .ast import Token |
7 | 5 | from .block_string import dedent_block_string_value |
| 6 | +from .source import Source |
| 7 | +from .token_kind import TokenKind |
8 | 8 |
|
9 | | -__all__ = ["Lexer", "TokenKind", "Token", "is_punctuator_token"] |
10 | | - |
11 | | - |
12 | | -class TokenKind(Enum): |
13 | | - """Each kind of token""" |
14 | | - |
15 | | - SOF = "<SOF>" |
16 | | - EOF = "<EOF>" |
17 | | - BANG = "!" |
18 | | - DOLLAR = "$" |
19 | | - AMP = "&" |
20 | | - PAREN_L = "(" |
21 | | - PAREN_R = ")" |
22 | | - SPREAD = "..." |
23 | | - COLON = ":" |
24 | | - EQUALS = "=" |
25 | | - AT = "@" |
26 | | - BRACKET_L = "[" |
27 | | - BRACKET_R = "]" |
28 | | - BRACE_L = "{" |
29 | | - PIPE = "|" |
30 | | - BRACE_R = "}" |
31 | | - NAME = "Name" |
32 | | - INT = "Int" |
33 | | - FLOAT = "Float" |
34 | | - STRING = "String" |
35 | | - BLOCK_STRING = "BlockString" |
36 | | - COMMENT = "Comment" |
37 | | - |
38 | | - |
39 | | -class Token: |
40 | | - __slots__ = ("kind", "start", "end", "line", "column", "prev", "next", "value") |
41 | | - |
42 | | - def __init__( |
43 | | - self, |
44 | | - kind: TokenKind, |
45 | | - start: int, |
46 | | - end: int, |
47 | | - line: int, |
48 | | - column: int, |
49 | | - prev: "Token" = None, |
50 | | - value: str = None, |
51 | | - ) -> None: |
52 | | - self.kind = kind |
53 | | - self.start, self.end = start, end |
54 | | - self.line, self.column = line, column |
55 | | - self.prev: Optional[Token] = prev |
56 | | - self.next: Optional[Token] = None |
57 | | - self.value: Optional[str] = value |
58 | | - |
59 | | - def __str__(self): |
60 | | - return self.desc |
61 | | - |
62 | | - def __repr__(self): |
63 | | - """Print a simplified form when appearing in repr() or inspect().""" |
64 | | - return f"<Token {self.desc} {self.line}/{self.column}>" |
65 | | - |
66 | | - def __inspect__(self): |
67 | | - return repr(self) |
68 | | - |
69 | | - def __eq__(self, other): |
70 | | - if isinstance(other, Token): |
71 | | - return ( |
72 | | - self.kind == other.kind |
73 | | - and self.start == other.start |
74 | | - and self.end == other.end |
75 | | - and self.line == other.line |
76 | | - and self.column == other.column |
77 | | - and self.value == other.value |
78 | | - ) |
79 | | - elif isinstance(other, str): |
80 | | - return other == self.desc |
81 | | - return False |
82 | | - |
83 | | - def __copy__(self): |
84 | | - """Create a shallow copy of the token""" |
85 | | - return self.__class__( |
86 | | - self.kind, |
87 | | - self.start, |
88 | | - self.end, |
89 | | - self.line, |
90 | | - self.column, |
91 | | - self.prev, |
92 | | - self.value, |
93 | | - ) |
94 | | - |
95 | | - def __deepcopy__(self, memo): |
96 | | - """Allow only shallow copies to avoid recursion.""" |
97 | | - return copy(self) |
98 | | - |
99 | | - @property |
100 | | - def desc(self) -> str: |
101 | | - """A helper property to describe a token as a string for debugging""" |
102 | | - kind, value = self.kind.value, self.value |
103 | | - return f"{kind} {value!r}" if value else kind |
| 9 | +__all__ = ["Lexer", "is_punctuator_token"] |
104 | 10 |
|
105 | 11 |
|
106 | 12 | _punctuator_tokens = frozenset( |
|
0 commit comments