Skip to content

Lexer

The lexer reads a source file one character at a time and emits tokens. Later stages never look at the raw text again, so a comment, a space, or a line ending has already been decided here.

source.py loads the file as UTF-8, strips a leading byte-order mark, and turns Windows and old Mac line endings into \n . The diagnostic name stays the path the user wrote. The directory is kept so a later import can resolve a relative path.

What a Token Carries

Each token has a kind, the spelling taken from the source, and a line and column. The column is what an error caret points at. The final token is always end of file, so the parser has a place to stop.

lupe tokens hello.lupe prints one token per line in the form line:column KIND spelling .

What the Lexer Decides

A # starts a comment that runs to the end of the line. A shebang is therefore a comment, and the lexer does not need a special case for it. Spaces and indentation are skipped. Indentation does not open or close a block.

A line break becomes a newline token only outside () , [] , and {} . Inside those delimiters a line break is layout, which lets a list or a call span lines. Consecutive newlines collapse to one, so blank lines do not become empty statements.

Names that match a reserved word become keyword tokens. Every other name is an identifier. Numbers are a run of digits, or digits, a dot, and more digits. A string uses single or double quotes and accepts \n , \t , \r , \\ , \" , and \' . An unknown character, an unknown escape, or a string that never closes is a lexical error and names the file, line, and column.