Einops lexer #
The lexer separates punctuation from axis words while preserving Unicode scalar offsets. Its default identifier policy reproduces the CPython 3.12 Unicode predicates used by einops v0.8.2. An explicit ASCII policy remains available for applications that deliberately restrict their pattern format.
Identifier operations required by the lexer and parser.
Whether a character may continue the current axis word.
Whether a complete word is a valid named axis.
Whether a complete word denotes a nonnegative decimal integer.
Convert a validated decimal word to its natural-number value.
Instances For
Restrict axis names and anonymous dimensions to ASCII characters.
Instances For
Match the word, identifier, and decimal predicates used by einops v0.8.2 on CPython 3.12.13 with Unicode 15.0.0.
Word characters follow str.isalnum() plus underscore. Complete words then
follow str.isidentifier(), while anonymous dimensions follow
str.isdecimal() and Python's arbitrary-precision decimal conversion.
Instances For
Every ASCII decimal digit is a word character under the Python policy.
Canonical decimal notation is recognized by the Python policy.
Canonical natural-number notation is a nonempty lexer word under the Python identifier policy.
Canonical decimal notation decodes to the natural number it renders.
Tokens shared by the three pattern grammars.
- word
(text : String)
: TokenKind
An identifier or decimal axis literal.
- leftParen : TokenKind
The opening delimiter of a composite axis.
- rightParen : TokenKind
The closing delimiter of a composite axis.
- ellipsis : TokenKind
The
...token. - arrow : TokenKind
The
->token separating input and output expressions. - comma : TokenKind
The comma separating einsum operands.
- star : TokenKind
The packed-axis marker used by
packandunpack.
Instances For
Instances For
Instances For
Canonical source text for one lexer token.
Instances For
Render a token sequence with conventional einops spacing.
Parentheses remain tight, commas have no preceding space, and every other token boundary receives one space. Empty operands are represented by consecutive commas or by a comma adjacent to the arrow.
Instances For
A token with its source location.
Instances For
Which source characters count as inter-token whitespace.
Decide whether a source character separates adjacent tokens.
Instances For
Transformation parsing in einops v0.8.2 treats only spaces as whitespace.
Instances For
Packing uses Python's whitespace splitting behavior.
Instances For
Tokenize an einops pattern using the selected identifier and whitespace policies.
Instances For
Every word emitted by a successful lexer run is nonempty and consists entirely of characters accepted by that run's identifier policy.
A nonempty string consisting entirely of word characters lexes as one word.
This is the compositional lexer fact used by canonical pattern rendering; identifier and decimal validation remain the parser's responsibility.
Canonical token rendering is a right inverse of the transformation lexer once source spans are erased.
The word hypothesis is necessary because TokenKind.word is also available to
clients constructing token lists directly, whereas words produced by lex
always satisfy it.
Canonical token rendering is also a right inverse of the general-whitespace
lexer used by pack and unpack.