forked from dhvanipa/error_deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexical_analysis.py
195 lines (157 loc) · 5.65 KB
/
lexical_analysis.py
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Copyright 2017 Eddie Antonio Santos <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Named `token_utils` because `token` is a Python standard library import, and
it breaks things if you shadow standard library stuff...
"""
from typing import Iterator
__all__ = [
'Lexeme',
'Token',
'Location',
'Position'
]
class Lexeme:
"""
A lexeme is an abstract token.
Lexeme pg. 111
From "Compilers Principles, Techniques, & Tools, 2nd Ed." (WorldCat)
by Aho, Lam, Sethi and Ullman:
> A lexeme is a sequence of characters in the source program that matches
> the pattern for a token and is identified by the lexical analyzer as an
> instance of that token.
"""
__slots__ = 'name', 'value'
def __init__(self, *, name, value):
self.name = name
self.value = value
# TODO: avoid storing value if name and value are equal?
def __str__(self):
return self.value
def __repr__(self):
return f"Lexeme(name={self.name!r}, value={self.value!r})"
class Position:
"""
A line/column position in a text file.
"""
__slots__ = 'line', 'column'
def __init__(self, *, line: int, column: int) -> None:
self.line = line
self.column = column
def __eq__(self, other) -> bool:
return (isinstance(other, Position)
and self.line == other.line
and self.column == other.column)
def __repr__(self) -> str:
return f"Position(line={self.line!r}, column={self.column!r})"
class Location:
"""
Represents the exact location of a token in a file.
"""
__slots__ = 'start', 'end'
def __init__(self, *, start: Position, end: Position) -> None:
self.start = start
self.end = end
def __eq__(self, other) -> bool:
return (isinstance(other, Location)
and self.start == other.start
and self.end == other.end)
@property
def spans_single_line(self) -> bool:
"""
True if the token spans multiple lines.
"""
return self.start.line == self.end.line
def __repr__(self) -> str:
return f"Location(start={self.start!r}, end={self.end!r})"
@classmethod
def from_string(self, text: str, *, line: int, column: int) -> 'Location':
r"""
Determine the location from the size of the string.
>>> Location.from_string("from", line=2, column=13)
Location(start=Position(line=2, column=13), end=Position(line=2, column=16))
>>> code = "'''hello,\nworld\n'''"
>>> Location.from_string(code, line=14, column=6)
Location(start=Position(line=14, column=6), end=Position(line=16, column=2))
"""
start = Position(line=line, column=column)
# How many lines are in the token?
# NOTE: this may be different than what the tokenizer thinks is the
# last line, due to handling of CR, LF, and CRLF, but I won't worry
# too much about it.
lines = text.split('\n')
end_line = start.line + len(lines) - 1
end_col = len(lines[-1]) - 1
if len(lines) == 1:
end_col += start.column
end = Position(line=end_line, column=end_col)
return Location(start=start, end=end)
class Token(Lexeme):
"""
A lexeme with a location. Includes location information.
From "Compilers Principles, Techniques, & Tools, 2nd Ed." (WorldCat) by
Aho, Lam, Sethi and Ullman:
> A token is a pair consisting of a token name and an optional attribute
> value. The token name is an abstract symbol representing a kind of
> lexical unit, e.g., a particular keyword, or sequence of input
> characters denoting an identifier. The token names are the input symbols
> that the parser processes.
"""
__slots__ = 'start', 'end'
def __init__(self, *, name: str, value: str, start: Position, end: Position) -> None:
super().__init__(name=name, value=value)
self.start = start
self.end = end
@property
def column(self) -> int:
"""
Column number of the beginning of the token.
"""
return self.start.column
@property
def line(self) -> int:
"""
Line number of the beginning of the token.
"""
return self.start.line
@property
def lines(self) -> Iterator[int]:
"""
An order list of all the lines in this file
"""
yield from range(self.start.line, self.end.line + 1)
@property
def location(self) -> Location:
"""
Location of the token.
"""
return Location(start=self.start, end=self.end)
@property
def loc(self) -> Location:
"""
Deprecated. Location of the token.
"""
return self.location
@property
def spans_single_line(self) -> bool:
"""
True if the token spans multiple lines.
"""
return self.location.spans_single_line
def __repr__(self) -> str:
return (f"Token("
f"name={self.name!r}, value={self.value!r}, "
f"start={self.start!r}, end={self.end!r})")