Skip to main content

spec.toml_tokens

spec.toml_tokens(text)

Split TOML text into strings, comments, line breaks and single characters.

This is the one scanner of the package that knows where a string or a comment is, so that a bracket, a # or a [ inside a string never counts as TOML syntax.

Parameters

Name Type Description Default
text str The TOML text. required

Returns

Name Type Description
Iterator[tuple[str, int, int]] Iterator[tuple[str, int, int]]: (kind, start, end) with kind one of "string", "comment", "newline" (a line feed outside a string) and "char" (any other character but blanks).

Examples

from babregen.spec import toml_tokens

text = 'a = ["x]", 1]  # [b]'
print([text[s:e] for kind, s, e in toml_tokens(text) if kind != "char"])
['"x]"', '# [b]']