-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.py
More file actions
174 lines (148 loc) · 5.1 KB
/
Copy pathlexer.py
File metadata and controls
174 lines (148 loc) · 5.1 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
import re
from dataclasses import dataclass
from typing import List, Optional
@dataclass
class Token:
type: str
lex: str
line: int
col: int
class Lexer:
def __init__(self, source: str):
self.source = source
self.tokens: List[Token] = []
self.start = 0
self.current = 0
self.line = 1
self.line_start = 0
def scan_tokens(self) -> List[Token]:
while not self.is_at_end():
self.start = self.current
self.scan_token()
self.tokens.append(Token("EOF", "", self.line, self.current - self.line_start))
return self.tokens
def is_at_end(self) -> bool:
return self.current >= len(self.source)
def advance(self) -> str:
self.current += 1
return self.source[self.current - 1]
def peek(self) -> str:
if self.is_at_end():
return '\0'
return self.source[self.current]
def peek_next(self) -> str:
if self.current + 1 >= len(self.source):
return '\0'
return self.source[self.current + 1]
def match(self, expected: str) -> bool:
if self.is_at_end():
return False
if self.source[self.current] != expected:
return False
self.current += 1
return True
def add_token(self, type: str, literal: str = None):
text = self.source[self.start:self.current]
# Calculate column
col = self.start - self.line_start + 1
self.tokens.append(Token(type, text, self.line, col))
def scan_token(self):
c = self.advance()
if c == '(': self.add_token('LPAREN')
elif c == ')': self.add_token('RPAREN')
elif c == '{': self.add_token('LBRACE')
elif c == '}': self.add_token('RBRACE')
elif c == '[': self.add_token('LBRACK')
elif c == ']': self.add_token('RBRACK')
elif c == ',': self.add_token('COMMA')
elif c == ';': self.add_token('SEMI')
elif c == '+': self.add_token('PLUS')
elif c == '-': self.add_token('MINUS')
elif c == '*': self.add_token('STAR')
elif c == '/':
if self.match('/'):
# Comment //
while self.peek() != '\n' and not self.is_at_end():
self.advance()
elif self.match('*'):
# Block comment /* ... */
while not self.is_at_end():
if self.peek() == '*' and self.peek_next() == '/':
self.advance()
self.advance()
break
if self.peek() == '\n':
self.line += 1
self.line_start = self.current + 1
self.advance()
else:
self.add_token('SLASH')
elif c == '!':
self.add_token('NE' if self.match('=') else 'NOT')
elif c == '=':
self.add_token('EQ' if self.match('=') else 'EQUAL')
elif c == '<':
self.add_token('LE' if self.match('=') else 'LT')
elif c == '>':
self.add_token('GE' if self.match('=') else 'GT')
elif c == '&':
if self.match('&'): self.add_token('AND')
elif c == '|':
if self.match('|'): self.add_token('OR')
elif c in [' ', '\r', '\t']:
pass # Ignore whitespace
elif c == '\n':
self.line += 1
self.line_start = self.current
elif c == '"':
self.string()
else:
if c.isdigit():
self.number()
elif c.isalpha() or c == '_':
self.identifier()
else:
# Unexpected character
pass
def string(self):
while self.peek() != '"' and not self.is_at_end():
if self.peek() == '\n':
self.line += 1
self.line_start = self.current + 1
self.advance()
if self.is_at_end():
# Unterminated string
return
self.advance() # The closing "
# Trim quotes
value = self.source[self.start+1 : self.current-1]
self.add_token('STRING', value)
def number(self):
is_float = False
while self.peek().isdigit():
self.advance()
if self.peek() == '.' and self.peek_next().isdigit():
is_float = True
self.advance() # Consume .
while self.peek().isdigit():
self.advance()
self.add_token('NUM')
def identifier(self):
while self.peek().isalnum() or self.peek() == '_':
self.advance()
text = self.source[self.start:self.current]
type = self.keywords.get(text, 'ID')
self.add_token(type)
keywords = {
'int': 'INT',
'float': 'FLOAT',
'char': 'CHAR',
'void': 'VOID',
'if': 'IF',
'else': 'ELSE',
'for': 'FOR',
'while': 'WHILE',
'return': 'RETURN',
'true': 'TRUE',
'false': 'FALSE'
}