TorchLean API

NN.Tensor.Internal.Syntax.Lexer

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.

  • isWordChar : CharBool

    Whether a character may continue the current axis word.

  • isIdentifier : StringBool

    Whether a complete word is a valid named axis.

  • isDecimal : StringBool

    Whether a complete word denotes a nonnegative decimal integer.

  • toNat? : StringOption Nat

    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 pack and unpack.

        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
              @[reducible, inline]

              A token with its source location.

              Instances For

                Which source characters count as inter-token whitespace.

                • isWhitespace : CharBool

                  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
                        theorem TorchLean.Tensor.Internal.Syntax.word_valid_of_lex_eq_ok (source : String) (policy : IdentifierPolicy) (whitespace : LexicalWhitespace) {tokens : List Token} (hLex : lex source policy whitespace = Except.ok tokens) {token : Token} (hToken : token tokens) {text : String} (hWord : token.value = TokenKind.word text) :
                        text.toList [] ∀ (character : Char), character text.toListpolicy.isWordChar character = true

                        Every word emitted by a successful lexer run is nonempty and consists entirely of characters accepted by that run's identifier policy.

                        theorem TorchLean.Tensor.Internal.Syntax.lex_word (policy : IdentifierPolicy) (whitespace : LexicalWhitespace) (text : String) (hNonempty : text.toList []) (hCharacters : ∀ (character : Char), character text.toListpolicy.isWordChar character = true) :
                        lex text policy whitespace = Except.ok [{ value := TokenKind.word text, span := { offset := 0, length := text.toList.length } }]

                        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.

                        theorem TorchLean.Tensor.Internal.Syntax.TokenKind.lex_renderSequence_transformation (tokens : List TokenKind) (hWords : ∀ (text : String), word text tokenstext.toList [] ∀ (character : Char), character text.toListIdentifierPolicy.pythonUnicode.isWordChar character = true) :

                        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.