-- | Tokens produced by 'Verada.Vibe.Lexer' (vibe-syntax section 7.2). Vibe is Python 3.14's -- syntax: its keywords are Python's (@match@, @case@, @type@ and @_@ are soft, so they are -- names to the lexer and the parser recognises them where they stand), and indentation is -- the layout, which the lexer turns into 'Indent' and 'Dedent' tokens around logical lines. module Verada.Vibe.Token ( Keyword (..) , Token (..) , TokenKind (..) , describeToken , keywordFromText , keywordText , operators ) where import Verada.Source (Span) import Data.Map.Strict qualified as Map import Data.Text (Text) import Data.Text qualified as Text -- | Python's reserved words. data Keyword = KwFalse | KwNone | KwTrue | KwAnd | KwAs | KwAssert | KwAsync | KwAwait | KwBreak | KwClass | KwContinue | KwDef | KwDel | KwElif | KwElse | KwExcept | KwFinally | KwFor | KwFrom | KwGlobal | KwIf | KwImport | KwIn | KwIs | KwLambda | KwNonlocal | KwNot | KwOr | KwPass | KwRaise | KwReturn | KwTry | KwWhile | KwWith | KwYield deriving stock (Bounded, Enum, Eq, Ord, Show) -- | The reserved word, e.g. @KwFalse@ is @False@ and @KwDef@ is @def@. keywordText :: Keyword -> Text keywordText = \case KwFalse -> "False" KwNone -> "None" KwTrue -> "True" k -> Text.toLower (Text.drop 2 (Text.pack (show k))) keywordFromText :: Text -> Maybe Keyword keywordFromText t = Map.lookup t table where table = Map.fromList [(keywordText k, k) | k <- [minBound .. maxBound]] data TokenKind = Name Text | Reserved Keyword | -- | an integer literal's value IntLiteral Integer | -- | a floating-point literal, exactly, and as written FloatLiteral Rational Text | StringLiteral Text | -- | an f-string's opening (@f"@), its literal text between replacement fields, and its -- closing quote, as Python 3.12 tokenizes it; each field is @{@, an expression's -- tokens, then @}@ FStringStart | FStringMiddle Text | FStringEnd | -- | a field's @=@ (@{x=}@): the field's text up to and with it, which the f-string writes FStringDebug Text | -- | a field's conversion, @!r@ or @!s@ FStringConversion Char | -- | a field's format spec, the text after its @:@ FStringSpec Text | -- | an operator or delimiter, as written (@+@, @//=@, @->@, @(@) Op Text | -- | the end of a logical line Newline | Indent | Dedent | EndOfInput deriving stock (Eq, Ord, Show) data Token = Token { span :: Span , kind :: TokenKind } deriving stock (Eq, Ord, Show) -- | Python's operators and delimiters, longest first (so that the lexer takes @**=@ before -- @**@ before @*@). operators :: [Text] operators = [ "**=", "//=", ">>=", "<<=", "..." , "->", ":=", "**", "//", "<<", ">>", "<=", ">=", "==", "!=", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "@=" , "+", "-", "*", "/", "%", "@", "&", "|", "^", "~", "<", ">", "(", ")", "[", "]", "{", "}", ",", ":", ".", ";", "=" ] -- | A token as diagnostics name it. describeToken :: TokenKind -> Text describeToken = \case Name n -> "`" <> n <> "`" Reserved k -> "`" <> keywordText k <> "`" IntLiteral _ -> "a number" FloatLiteral _ t -> "`" <> t <> "`" StringLiteral _ -> "a string" FStringStart -> "an f-string" FStringMiddle _ -> "an f-string's text" FStringEnd -> "the end of an f-string" FStringDebug _ -> "an f-string field's `=`" FStringConversion _ -> "an f-string field's conversion" FStringSpec _ -> "an f-string field's format spec" Op o -> "`" <> o <> "`" Newline -> "the end of the line" Indent -> "an indented block" Dedent -> "the end of an indented block" EndOfInput -> "the end of the file"