Lexer
lexer
¶
ContentT = TypeVar('ContentT', str, bytes, default=str)
module-attribute
¶
Position = int
module-attribute
¶
Zero-based position from the start of the input, measured in Unicode code points
for str or bytes for bytes.
Offset = int
module-attribute
¶
Signed displacement between positions, measured in Unicode code points for str
or bytes for bytes.
Column = int
module-attribute
¶
One-based position within a line, measured in Unicode code points for str
or bytes for bytes.
TokenKindT = TypeVar('TokenKindT', bound=Enum)
module-attribute
¶
Location
¶
Bases: NamedTuple
A file location using the originating input's coordinate units.
Source code in xdsl/utils/lexer.py
Input
dataclass
¶
Bases: Generic[ContentT]
Source name and contents, preserved without encoding or decoding.
Source code in xdsl/utils/lexer.py
content: ContentT = field(repr=False)
class-attribute
instance-attribute
¶
name: str
instance-attribute
¶
len: Offset = field(init=False, repr=False)
class-attribute
instance-attribute
¶
newline: ContentT = field(init=False, repr=False, compare=False)
class-attribute
instance-attribute
¶
LF in the source representation.
__init__(content: ContentT, name: str) -> None
¶
__post_init__()
¶
__len__() -> Offset
¶
get_start_of_line(pos: Position) -> Position
¶
Returns the location of the last newline before pos, or 0 if there are no
previous newlines.
Source code in xdsl/utils/lexer.py
get_end_of_line(pos: Position) -> Position
¶
Returns the position of the first newline after pos, or the length of the
file if there are no more newlines.
Source code in xdsl/utils/lexer.py
at(i: Position) -> str | int | None
¶
Span
dataclass
¶
Bases: Generic[ContentT]
Parts of the input are always passed around as spans, so we know where they originated.
Positions and lengths use the input's native units: Unicode code points for strings and bytes for byte inputs. Converting the contents does not change these coordinates.
Source code in xdsl/utils/lexer.py
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
start: Position
instance-attribute
¶
Inclusive, zero-based start position within the input.
end: Position
instance-attribute
¶
Exclusive, zero-based end position within the input.
input: Input[ContentT]
instance-attribute
¶
The input being operated on
line_offset: int = 0
class-attribute
instance-attribute
¶
A line offset to add to the line number when reporting a location.
len: Offset
property
¶
content: ContentT
property
¶
The source slice, without encoding or decoding.
text: str
property
¶
The source slice as text, decoding byte inputs with strict UTF-8.
bytes: bytes
property
¶
The source slice as bytes, encoding string inputs with strict UTF-8.
__init__(start: Position, end: Position, input: Input[ContentT], line_offset: int = 0) -> None
¶
__len__() -> Offset
¶
get_location() -> Location
¶
Source code in xdsl/utils/lexer.py
print_with_context(msg: str | None = None) -> str
¶
Returns a string containing lines relevant to the span. The Span's contents
are highlighted by up-carets beneath them (^). The message msg is printed
along these.
Byte inputs are decoded as UTF-8 with replacement for display only; the reported location still uses byte columns.
Source code in xdsl/utils/lexer.py
Token
dataclass
¶
Bases: Generic[TokenKindT, ContentT]
Source code in xdsl/utils/lexer.py
kind: TokenKindT
instance-attribute
¶
span: Span[ContentT]
instance-attribute
¶
content: ContentT
property
¶
The source slice, without encoding or decoding.
text: str
property
¶
The token as text, decoding byte inputs with strict UTF-8.
bytes: bytes
property
¶
The token as bytes, encoding string inputs with strict UTF-8.
__init__(kind: TokenKindT, span: Span[ContentT]) -> None
¶
Lexer
dataclass
¶
Bases: ABC, Generic[TokenKindT, ContentT]
Source code in xdsl/utils/lexer.py
input: Input[ContentT]
instance-attribute
¶
Input that is currently being lexed.
pos: Position = field(init=False, default=0)
class-attribute
instance-attribute
¶
Current zero-based position within the input. The position can be out of bounds, in which case the lexer is in EOF state.