-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdim_token.py
More file actions
190 lines (167 loc) · 4.22 KB
/
dim_token.py
File metadata and controls
190 lines (167 loc) · 4.22 KB
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
# dim_token.py — Structured Token & Span for Dim Compiler
# Replaces raw (TokenType, value) tuples with proper dataclasses.
from dataclasses import dataclass
from enum import Enum, auto
from typing import Optional, Any
class TokenType(Enum):
# Literals
INTEGER = auto()
FLOAT = auto()
STRING = auto()
BOOL = auto()
# Identifiers & Keywords
IDENTIFIER = auto()
KEYWORD = auto()
# Punctuation
COLON = auto()
COMMA = auto()
DOT = auto()
SEMICOLON = auto()
AT = auto() # @
# Grouping
LPAREN = auto()
RPAREN = auto()
LBRACKET = auto()
RBRACKET = auto()
LBRACE = auto()
RBRACE = auto()
# Operators
PLUS = auto()
MINUS = auto()
STAR = auto()
SLASH = auto()
PERCENT = auto()
EQ = auto() # =
EQEQ = auto() # ==
NEQ = auto() # !=
LT = auto() # <
GT = auto() # >
LTE = auto() # <=
GTE = auto() # >=
AND = auto() # and / &&
OR = auto() # or / ||
NOT = auto() # not / !
ARROW = auto() # ->
FAT_ARROW = auto() # =>
AMP = auto() # & (borrow)
PIPE = auto() # |
PLUSEQ = auto() # +=
MINUSEQ = auto() # -=
STAREQ = auto() # *=
SLASHEQ = auto() # /=
PERCENTEQ = auto() # %=
QUESTION = auto() # ? (Result unwrap)
DCOLON = auto() # :: (namespace/enum path)
# Indentation (injected by lexer)
INDENT = auto()
DEDENT = auto()
NEWLINE = auto()
# Meta
EOF = auto()
UNKNOWN = auto()
# Comprehensive keyword set for Dim
KEYWORDS = frozenset(
{
"fn",
"let",
"mut",
"const",
"return",
"if",
"else",
"elif",
"while",
"for",
"in",
"break",
"continue",
"match",
"struct",
"enum",
"trait",
"impl",
"type",
"prompt",
"role",
"output",
"model",
"deterministic",
"async",
"await",
"spawn",
"actor",
"receive",
"import",
"from",
"as",
"pub",
"priv",
"unsafe",
"verify",
"and",
"or",
"not",
"true",
"false",
"none",
"self",
"Self",
"extends",
"where",
"try",
"catch",
"throw",
"finally",
"len",
"range",
"assert",
"panic",
"foreign",
"use",
}
)
@dataclass(frozen=True)
class Span:
"""Half-open byte range [start, end) with file context."""
file: str
line_start: int # 1-indexed
col_start: int # 1-indexed
line_end: int
col_end: int
def __repr__(self) -> str:
if self.line_start == self.line_end:
return f"{self.file}:{self.line_start}:{self.col_start}-{self.col_end}"
return f"{self.file}:{self.line_start}:{self.col_start}-{self.line_end}:{self.col_end}"
def merge(self, other: "Span") -> "Span":
"""Return the smallest span that covers both self and other."""
return Span(
file=self.file,
line_start=min(self.line_start, other.line_start),
col_start=min(self.col_start, other.col_start)
if self.line_start == other.line_start
else (
self.col_start
if self.line_start < other.line_start
else other.col_start
),
line_end=max(self.line_end, other.line_end),
col_end=max(self.col_end, other.col_end)
if self.line_end == other.line_end
else (self.col_end if self.line_end > other.line_end else other.col_end),
)
@staticmethod
def dummy() -> "Span":
return Span("<dummy>", 0, 0, 0, 0)
@dataclass
class Token:
"""A lexed token with type, value, and source location."""
kind: TokenType
value: Any # str, int, float, bool, or None
span: Span
def __repr__(self) -> str:
return f"Token({self.kind.name}, {self.value!r}, {self.span})"
@property
def is_keyword(self) -> bool:
return self.kind == TokenType.KEYWORD
def keyword_is(self, *words: str) -> bool:
return self.kind == TokenType.KEYWORD and self.value in words