|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | + |
| 3 | +{- | The application spine of an expression: a head applied to arguments, with |
| 4 | +visible type applications kept aside. Backs arity repair, where a weak model |
| 5 | +over-applies a function (@D.col \@Double "revenue" df@ against @col :: Text -> |
| 6 | +Expr a@) and the fix is to drop the excess argument. |
| 7 | +
|
| 8 | +Lexical, not an AST: nothing in the tree parses an expression to a term or prints |
| 9 | +one back. So this reads only a plain application and declines everything else — |
| 10 | +a decline costs a missed repair, a misparse would corrupt the cell. |
| 11 | +-} |
| 12 | +module Sabela.AI.Spine ( |
| 13 | + Spine (..), |
| 14 | + splitSpine, |
| 15 | + renderSpine, |
| 16 | + trimTo, |
| 17 | + spineArity, |
| 18 | +) where |
| 19 | + |
| 20 | +import Data.Char (isAlpha, isAlphaNum) |
| 21 | +import Data.Text (Text) |
| 22 | +import qualified Data.Text as T |
| 23 | + |
| 24 | +{- | A head applied to arguments. A parenthesised argument is kept whole (parens |
| 25 | +included) so it can be re-read on its own; type applications ride separately so a |
| 26 | +trim cannot drop them. |
| 27 | +-} |
| 28 | +data Spine = Spine |
| 29 | + { spHead :: Text |
| 30 | + , spTypeApps :: [Text] |
| 31 | + , spArgs :: [Text] |
| 32 | + } |
| 33 | + deriving (Eq, Show) |
| 34 | + |
| 35 | +-- | The number of value arguments the spine applies. |
| 36 | +spineArity :: Spine -> Int |
| 37 | +spineArity = length . spArgs |
| 38 | + |
| 39 | +{- | Keep at most @n@ value arguments. The fix for an over-applied head; never |
| 40 | +grows a spine, so a wrong arity can only ever shorten it. |
| 41 | +-} |
| 42 | +trimTo :: Int -> Spine -> Spine |
| 43 | +trimTo n sp = sp{spArgs = take (max 0 n) (spArgs sp)} |
| 44 | + |
| 45 | +-- | Re-render a spine: @head \@T arg1 arg2@. |
| 46 | +renderSpine :: Spine -> Text |
| 47 | +renderSpine sp = T.unwords (spHead sp : spTypeApps sp ++ spArgs sp) |
| 48 | + |
| 49 | +{- | Read a plain application. 'Nothing' unless the whole expression is an |
| 50 | +identifier head applied to atoms, literals, or balanced parenthesised groups — |
| 51 | +an operator, lambda, literal head, or unbalanced text declines. |
| 52 | +-} |
| 53 | +splitSpine :: Text -> Maybe Spine |
| 54 | +splitSpine src = do |
| 55 | + toks <- spineTokens (T.strip src) |
| 56 | + case toks of |
| 57 | + [] -> Nothing |
| 58 | + (h : rest) |
| 59 | + | isIdentHead h -> |
| 60 | + Just |
| 61 | + Spine |
| 62 | + { spHead = h |
| 63 | + , spTypeApps = filter isTypeApp rest |
| 64 | + , spArgs = filter (not . isTypeApp) rest |
| 65 | + } |
| 66 | + _ -> Nothing |
| 67 | + |
| 68 | +-- | An application head: a (possibly qualified) identifier, never a literal. |
| 69 | +isIdentHead :: Text -> Bool |
| 70 | +isIdentHead t = case T.uncons t of |
| 71 | + Just (c, _) -> (isAlpha c || c == '_') && T.all isIdentChar t |
| 72 | + Nothing -> False |
| 73 | + where |
| 74 | + isIdentChar c = isAlphaNum c || c `elem` ("_.'" :: String) |
| 75 | + |
| 76 | +isTypeApp :: Text -> Bool |
| 77 | +isTypeApp = T.isPrefixOf "@" |
| 78 | + |
| 79 | +{- | Split into top-level tokens, keeping string literals and parenthesised |
| 80 | +groups whole. 'Nothing' on unbalanced parens or a token that is not an |
| 81 | +identifier, literal, type application, or group. |
| 82 | +-} |
| 83 | +spineTokens :: Text -> Maybe [Text] |
| 84 | +spineTokens = go [] . T.strip |
| 85 | + where |
| 86 | + go acc t |
| 87 | + | T.null t = Just (reverse acc) |
| 88 | + | otherwise = do |
| 89 | + (tok, rest) <- lexOne t |
| 90 | + go (tok : acc) (T.stripStart rest) |
| 91 | + |
| 92 | +{- | One top-level token. Fails on an unbalanced group or a bare operator, which |
| 93 | +is how a non-application expression declines. |
| 94 | +-} |
| 95 | +lexOne :: Text -> Maybe (Text, Text) |
| 96 | +lexOne t = case T.uncons t of |
| 97 | + Nothing -> Nothing |
| 98 | + Just ('(', _) -> lexGroup t |
| 99 | + Just ('"', _) -> lexString t |
| 100 | + Just (c, _) |
| 101 | + | isAlpha c || c == '_' || c == '@' || isAlphaNum c -> |
| 102 | + let (tok, rest) = T.span plain t |
| 103 | + in if T.null tok then Nothing else Just (tok, rest) |
| 104 | + _ -> Nothing |
| 105 | + where |
| 106 | + plain c = isAlphaNum c || c `elem` ("_.'@[]" :: String) |
| 107 | + |
| 108 | +-- | A balanced parenthesised group, parens kept, nested groups and strings honoured. |
| 109 | +lexGroup :: Text -> Maybe (Text, Text) |
| 110 | +lexGroup t = walk 0 0 False t |
| 111 | + where |
| 112 | + walk :: Int -> Int -> Bool -> Text -> Maybe (Text, Text) |
| 113 | + walk depth i inStr s = case T.uncons s of |
| 114 | + Nothing -> Nothing |
| 115 | + Just (c, cs) |
| 116 | + | inStr, c == '\\' -> skipEscape depth i cs |
| 117 | + | inStr, c == '"' -> walk depth (i + 1) False cs |
| 118 | + | inStr -> walk depth (i + 1) True cs |
| 119 | + | c == '"' -> walk depth (i + 1) True cs |
| 120 | + | c == '(' -> walk (depth + 1) (i + 1) False cs |
| 121 | + | c == ')' -> |
| 122 | + if depth == 1 |
| 123 | + then Just (T.take (i + 1) t, T.drop (i + 1) t) |
| 124 | + else walk (depth - 1) (i + 1) False cs |
| 125 | + | otherwise -> walk depth (i + 1) inStr cs |
| 126 | + skipEscape depth i cs = case T.uncons cs of |
| 127 | + Nothing -> Nothing |
| 128 | + Just (_, cs') -> walk depth (i + 2) True cs' |
| 129 | + |
| 130 | +-- | A string literal, quotes kept, escapes honoured. |
| 131 | +lexString :: Text -> Maybe (Text, Text) |
| 132 | +lexString t = walk 1 (T.drop 1 t) |
| 133 | + where |
| 134 | + walk i s = case T.uncons s of |
| 135 | + Nothing -> Nothing |
| 136 | + Just ('\\', cs) -> case T.uncons cs of |
| 137 | + Nothing -> Nothing |
| 138 | + Just (_, cs') -> walk (i + 2) cs' |
| 139 | + Just ('"', _) -> Just (T.take (i + 1) t, T.drop (i + 1) t) |
| 140 | + Just (_, cs) -> walk (i + 1) cs |
0 commit comments