Skip to documentation
SLOP

tiny.python.source.token

Reference tiny.python source token

Defined in source.

A token is one name, number, string, keyword or symbol of a program's text.

API (4)

Actions

Public operations.

Types and contracts

Public types and contracts.

No direct callersNo direct callssourcetoken
Static calls · unresolved targets: unknown · external targets: unknown.

Source

Source: lib/python/src/source/root.zig:10

zig
pub const token = @import("token.zig");

Source: lib/python/src/source/token.zig

zig
//! A token is one name, number, string, keyword or symbol of a program's text. A token records its//! kind and the byte range it covers in the text.//!//! The parser decides what to build from a token's kind alone. The parser reads a token's//! characters only for names, numbers and strings.//!//! The kinds follow the categories of lexical analysis in the [Python 3.14 language//! reference](https://docs.python.org/3.14/reference/): names, keywords, literals, operators and//! delimiters. The kinds also include the reference's newline, indent and dedent tokens, which//! carry the line and block structure.//!//! The kinds cover the package's subset: two literal kinds (integers and strings), 19 keywords, 19//! operators and delimiters, and a kind that marks the end of the input. A token stores its byte//! range, `Span`, as two offsets into the text. A token keeps no copy of the text, so it has a//! fixed size and owns no memory. A caller reads a token's characters by passing the same text back//! to `Span.text`.const std = @import("std");/// The kind of a token. The parser switches and matches on it to decide what each token starts./// Keyword kinds are named for their keyword with `_kw` added, as `if_kw` is `if`. `true_kw`,/// `false_kw` and `none_kw` are `True`, `False` and `None`. The lexer gives a keyword kind to these/// 19 words alone. Every other word gets the kind `identifier`. Operator and delimiter kinds are/// named for their characters, as `plus` is `+`, `less_equal` is `<=` and `lparen` is `(`./// `newline`, `indent`, `dedent` and `eof` carry the line and block structure and the end of the/// input.pub const Tag = enum {    /// A name that is none of the 19 keywords. A name starts with an ASCII letter or underscore and    /// continues with ASCII letters, digits and underscores. Python's other reserved words, such as    /// `class` and `import`, get this kind too.    identifier,    /// A run of decimal digits. The token holds no sign, underscore or base prefix: `-5` is a    /// `minus` token and an `integer` token. The parser reads the digits as a signed 128-bit    /// integer.    integer,    /// A string literal between single or double quotes, on one line. Its byte range includes both    /// quotes. A backslash has no special meaning, so the first quote that matches the opening one    /// ends the literal.    string,    and_kw,    break_kw,    continue_kw,    del_kw,    def_kw,    elif_kw,    else_kw,    for_kw,    if_kw,    in_kw,    is_kw,    not_kw,    or_kw,    pass_kw,    return_kw,    while_kw,    true_kw,    false_kw,    none_kw,    plus,    minus,    star,    /// A single `=`, the assignment sign. `==` has its own kind, `equal_equal`.    equal,    equal_equal,    bang_equal,    less,    less_equal,    greater,    greater_equal,    comma,    colon,    dot,    lparen,    rparen,    lbracket,    rbracket,    lbrace,    rbrace,    /// One line break: `\n`, `\r\n` or a lone `\r`. Its byte range covers the bytes of the break.    /// The lexer emits one for every line break, including the break that ends a blank or    /// comment-only line. A line break inside parentheses, brackets or braces yields one as well.    newline,    /// The start of a block. The lexer emits one before the first token of a line indented deeper    /// than the enclosing block. Its byte range covers that line's leading spaces.    indent,    /// The end of a block. The lexer emits one for each indentation level that a less indented line    /// closes. Its byte range covers the leading spaces of that line. At the end of the input, the    /// lexer emits one for each level still open, and each has an empty range at the end of the    /// text.    dedent,    /// The last token of every list that `tokenize` returns, after any `dedent` tokens that close    /// the open blocks. Its byte range is empty and sits at the end of the text.    eof,};/// A byte range of the source text: the offset of its first byte and the offset one past its last/// byte. The parser reads a token's characters back out of the source text through it. A span holds/// two offsets and no pointer, so it borrows nothing.pub const Span = struct {    /// The offset of the range's first byte in the source text.    start: usize,    /// The offset one past the range's last byte. `end - start` is the range's length. An empty    /// range has `start == end`.    end: usize,    /// Returns the bytes of the range, as a slice of the given source text. The parser reads names,    /// integer digits and string literals through it. The caller passes the text the range was    /// taken from, and the range has to lie inside it. The slice borrows the text and allocates    /// nothing.    pub fn text(self: Span, source: []const u8) []const u8 {        return source[self.start..self.end];    }};/// One token: its kind and its byte range in the source text. `tokenize` returns a list of them,/// and the parser walks that list. A token owns no memory. A caller reads its characters back with/// `span.text(source)`.pub const Token = struct {    /// The token's kind.    tag: Tag,    /// The token's byte range in the source text.    span: Span,};test "span returns source text" {    const source = "alpha";    try std.testing.expectEqualStrings("alp", (Span{ .start = 0, .end = 3 }).text(source));}

Audit

Definitions5
Public names9
Members49
Version26.7.0
Revisiondaab053ee433