packages feed

phino-0.0.90 / src / Parser.hs

{-# LANGUAGE DeriveAnyClass #-}{-# LANGUAGE RecordWildCards #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MIT-- The goal of the module is to parse given phi program to ASTmodule Parser  ( parseProgram  , parseProgramThrows  , parseExpression  , parseExpressionThrows  , parseAttribute  , parseAttributeThrows  , parseAlpha  , parseIndex  , parseNumber  , parseNumberThrows  , parseBinding  , parseBytes  , PhiParser (..)  , phiParser  )whereimport ASTimport Control.Exception (Exception, throwIO)import Control.Monad (guard)import Data.Char (isAsciiLower, isDigit)import Data.Scientific (toRealFloat)import qualified Data.Text as Timport Data.Voidimport GHC.Charimport Miscimport Numericimport Text.Megaparsecimport Text.Megaparsec.Charimport qualified Text.Megaparsec.Char.Lexer as Limport Text.Printf (printf)type Parser = Parsec Void Stringdata ParserException  = CouldNotParseProgram {message :: String}  | CouldNotParseExpression {message :: String}  | CouldNotParseAttribute {message :: String}  | CouldNotParseNumber {message :: String}  deriving (Exception)data PhiParser = PhiParser  { _attribute :: Parser Attribute  , _alpha :: Parser Alpha  , _index :: Parser T.Text  , _binding :: Parser Binding  , _expression :: Parser Expression  , _string :: Parser String  }phiParser :: PhiParserphiParser = PhiParser attribute alpha index' binding expression quotedStrinstance Show ParserException where  show CouldNotParseProgram{..} = printf "Couldn't parse given phi program, cause: %s" message  show CouldNotParseExpression{..} = printf "Couldn't parse given phi expression, cause: %s" message  show CouldNotParseAttribute{..} = printf "Couldn't parse given attribute, cause: %s" message  show CouldNotParseNumber{..} = printf "Couldn't parse given number to 'Φ.number', cause: %s" message-- White space consumerwhiteSpace :: Parser ()whiteSpace = L.space space1 empty empty-- Lexeme that ignores white spaces afterlexeme :: Parser a -> Parser alexeme = L.lexeme whiteSpace-- Strict symbol (or sequence of symbols) with ignored white spaces aftersymbol :: String -> Parser Stringsymbol = L.symbol whiteSpace-- Parsed as String then packed to Text once; BiLambda keeps String so function stays Stringlabel' :: Parser T.Textlabel' = lexeme $ do  first <- oneOf ['a' .. 'z']  rest <- many (satisfy (`notElem` " \r\n\t,.|':;!?][}{)(⟧⟦") <?> "allowed character")  return (T.pack (first : rest))function :: Parser Stringfunction = lexeme $ do  first <- oneOf ['A' .. 'Z']  rest <-    many      ( satisfy          (\ch -> isDigit ch || isAsciiLower ch || ch == '_' || ch == 'φ')          <?> "allowed character in function name"      )  return (first : rest)delta :: Parser Stringdelta =  choice    [ symbol "D>"    , symbol "Δ" >> dashedArrow    ]lambda :: Parser Stringlambda =  choice    [ symbol "L>"    , symbol "λ" >> dashedArrow    ]dashedArrow :: Parser StringdashedArrow = symbol "⤍"arrow :: Parser Stringarrow = choice [symbol "->", symbol "↦"]global :: Parser Stringglobal = choice [symbol "Q", symbol "Φ"]metaSuffix :: Parser StringmetaSuffix = lexeme (many (oneOf ('_' : '-' : ['0' .. '9'] ++ ['a' .. 'z'] ++ ['A' .. 'Z']) <?> "meta suffix"))-- Meta variable names are packed to Text once here; all AST meta fields are Textmeta :: Char -> Parser T.Textmeta ch = do  _ <- char '!'  c <- char ch  suf <- metaSuffix  return (T.pack (c : suf))meta' :: Char -> String -> Parser T.Textmeta' ch uni =  choice    [ meta ch    , do        _ <- string uni        suf <- metaSuffix        return (T.pack (ch : suf))    ]byte :: Parser Stringbyte = do  f <- hexDigitChar >>= upperHex  s <- hexDigitChar >>= upperHex  return [f, s]  where    upperHex ch      | isDigit ch || ('A' <= ch && ch <= 'F') = return ch      | otherwise = fail ("expected 0-9 or A-F, got " ++ show ch)-- bytes-- 0. meta: !b-- 1. empty: ---- 2. one byte: 01--- 3. many bytes: 01-02-...-FFbytes :: Parser Bytesbytes =  lexeme    ( choice        [ BtMeta <$> meta' 'd' "δ"        , symbol "--" >> return BtEmpty        , try $ do            first <- byte            rest <- some $ do              _ <- char '-'              byte            return (BtMany (first : rest))        , do            bte <- byte            _ <- char '-'            return (BtOne bte)        ]        <?> "bytes"    )number :: Parser Expressionnumber = do  sign <- optional (choice [char '-', char '+'])  unsigned <- lexeme L.scientific  return    ( DataNumber        ( numToBts            ( case sign of                -- Negate the Double rather than the Scientific so that a zero                -- literal preserves its sign: Scientific has no negative zero,                -- but negate on Double yields -0.0, a distinct IEEE-754 value.                Just '-' -> negate (toRealFloat unsigned)                _ -> toRealFloat unsigned            )        )    )quotedStr :: Parser StringquotedStr = char '"' >> manyTill (choice [escapedChar, noneOf ['\\', '"']]) (char '"')  where    escapedChar :: Parser Char    escapedChar = do      _ <- char '\\'      c <- oneOf ['\\', '"', 'n', 'r', 't', 'b', 'f', 'u', 'x']      case c of        '\\' -> return '\\'        '"' -> return '"'        'n' -> return '\n'        'r' -> return '\r'        't' -> return '\t'        'b' -> return '\b'        'f' -> return '\f'        'u' -> unicodeEscape        'x' -> hexEscape        _ -> fail ("Unknown escape: \\" ++ [c])    unicodeEscape :: Parser Char    unicodeEscape = do      hexDigits <- count 4 hexDigitChar      case readHex hexDigits of        [(n, "")] ->          if n >= 0xD800 && n <= 0xDBFF            then -- High surrogate, look for low surrogate              do                _ <- string "\\u"                lowHexDigits <- count 4 hexDigitChar                case readHex lowHexDigits of                  [(low, "")] ->                    if low >= 0xDC00 && low <= 0xDFFF                      then do                        -- Valid surrogate pair, combine them                        let codePoint = 0x10000 + ((n - 0xD800) * 0x400) + (low - 0xDC00)                        return (chr codePoint)                      else fail ("Invalid low surrogate: \\u" ++ lowHexDigits)                  _ -> fail ("Invalid low surrogate hex: \\u" ++ lowHexDigits)            else              if n >= 0xDC00 && n <= 0xDFFF                then fail ("Unexpected low surrogate: \\u" ++ hexDigits)                else                  if n >= 0 && n <= 0x10FFFF                    then return (chr n)                    else fail ("Invalid Unicode code point: \\u" ++ hexDigits)        _ -> fail ("Invalid Unicode escape: \\u" ++ hexDigits)    hexEscape :: Parser Char    hexEscape = do      digits <- count 2 hexDigitChar      case readHex digits of        [(n, "")] -> return (chr n)        _ -> fail ("Invalid hex escape: \\x" ++ digits)tauValue :: Parser ExpressiontauValue =  choice    [ try $ do        _ <- arrow        expression    , do        _ <- symbol "("        voids <-          choice            [ rb >> return []            , do                voids' <- map BiVoid <$> void' `sepBy1` symbol ","                rb >> return voids'            ]        _ <- arrow        bs <- formationBindings        bds <- validatedBindings (voids ++ bs)        return (ExFormation (withVoidRho bds))    ]  where    rb :: Parser String    rb = symbol ")"tauBinding :: Parser Attribute -> Parser BindingtauBinding attr = BiTau <$> attr <*> tauValuemetaBinding :: Parser BindingmetaBinding = BiMeta <$> meta' 'B' "𝐵"-- binding-- 1. tau-- 2. void-- 3. delta-- 4. meta delta-- 5. meta-- 6. lambda-- 7. meta lambdabinding :: Parser Bindingbinding =  choice    [ try (tauBinding attribute)    , try $ do        attr <- attribute        _ <- arrow        _ <- choice [symbol "?", symbol "∅"]        return (BiVoid attr)    , try $ do        _ <- delta        BiDelta <$> bytes    , try metaBinding    , try $ do        _ <- lambda        BiLambda . Function . T.pack <$> function    , do        _ <- lambda        BiLambda . FnMeta <$> meta' 'F' "𝑓"    ]    <?> "binding"-- inlined void attribute-- 1. label-- 2. rho-- 3. phivoid' :: Parser Attributevoid' =  choice    [ AtLabel <$> label'    , do        _ <- choice [symbol "^", symbol "ρ"]        return AtRho    , do        _ <- choice [symbol "@", symbol "φ"]        return AtPhi    ]-- attribute-- 1. label-- 2. meta-- 3. rho-- 4. phiattribute :: Parser Attributeattribute =  choice    [ void'    , AtMeta <$> meta' 't' "𝜏"    ]    <?> "attribute"-- index meta: !i, 𝑖index' :: Parser T.Textindex' = meta' 'i' "𝑖"-- alpha-- 1. index: ~0, α0-- 2. meta: α𝑖, ~!ialpha :: Parser Alphaalpha = do  _ <- choice [symbol "~", symbol "α"]  choice    [ Alpha <$> lexeme L.decimal    , AlMeta <$> index'    ]    <?> "alpha"-- application argument-- 1. tau: <attribute> ↦ <expression>-- 2. alpha: <alpha> ↦ <expression>argument :: Parser Argumentargument =  choice    [ try (ArAlpha <$> alpha <*> tauValue)    , ArTau <$> attribute <*> tauValue    ]    <?> "argument"validatedBindings :: [Binding] -> Parser [Binding]validatedBindings bds = case uniqueBindings bds of  Left msg -> fail msg  Right bds' -> return bds'-- formationformationBindings :: Parser [Binding]formationBindings = do  _ <- choice [symbol "[[", symbol "⟦"]  choice    [ rsb >> return []    , do        bs <- binding `sepBy1` symbol ","        rsb >> return bs    ]  where    rsb :: Parser String    rsb = choice [symbol "]]", symbol "⟧"]-- head part of expression-- 1. formation-- 2. this-- 3. global-- 4. termination-- 5. meta expression-- 6. full attribute -> sugar for $.attrexHead :: Parser ExpressionexHead =  choice    [ do        bs <- formationBindings >>= validatedBindings        return (ExFormation (withVoidRho bs))    , do        _ <- choice [symbol "$", symbol "ξ"]        return ExXi    , do        _ <- global        return ExRoot    , do        _ <- choice [symbol "T", symbol "⊥"]        return ExTermination    , number    , lexeme (DataString . strToBts <$> quotedStr)    , try (ExMeta <$> meta' 'e' "𝑒")    , try (ExMeta <$> meta' 'n' "𝑛")    , try (ExMeta <$> meta' 'k' "𝑘")    , ExDispatch ExXi <$> attribute    ]    <?> "expression head"application :: Expression -> [Argument] -> Expressionapplication = foldl ExApplication-- tail optional part of application-- 1. any head + dispatch-- 2. any head except $ and Q + applicationexTail :: Expression -> Parser ExpressionexTail expr =  choice    [ do        next <-          choice            [ do                _ <- symbol "."                ExDispatch expr <$> attribute            , do                guard                  ( case expr of                      ExXi -> False                      ExRoot -> False                      _ -> True                  )                _ <- symbol "("                bds <-                  choice                    [ try $ argument `sepBy1` symbol ","                    , do                        exprs <- expression `sepBy1` symbol ","                        return (zipWith (ArAlpha . Alpha) [0 ..] exprs)                    ]                _ <- symbol ")"                return (application expr bds)            ]            <?> "dispatch or application"        exTail next    , return expr    ]expression :: Parser Expressionexpression = do  expr <- exHead  exTail exprprogram :: Parser Programprogram =  choice    [ do        _ <- symbol "{"        prog <- Program <$> expression        _ <- symbol "}"        return prog    , do        _ <- global        _ <- arrow        Program <$> expression    ]    <?> "program"-- Entry pointparse' :: String -> Parser a -> String -> Either String aparse' name parser input = do  let parsed =        runParser          ( do              _ <- whiteSpace              p <- parser              _ <- eof              return p          )          name          input  case parsed of    Right parsed' -> Right parsed'    Left err -> Left (errorBundlePretty err)parseBytes :: String -> Either String BytesparseBytes = parse' "bytes" bytesparseBinding :: String -> Either String BindingparseBinding = parse' "binding" bindingparseNumber :: String -> Either String ExpressionparseNumber = parse' "number" numberparseNumberThrows :: String -> IO ExpressionparseNumberThrows num = case parseNumber num of  Right num' -> pure num'  Left err -> throwIO (CouldNotParseNumber err)parseAttribute :: String -> Either String AttributeparseAttribute = parse' "attribute" attributeparseAlpha :: String -> Either String AlphaparseAlpha = parse' "alpha" alphaparseIndex :: String -> Either String T.TextparseIndex = parse' "index meta" index'parseAttributeThrows :: String -> IO AttributeparseAttributeThrows attr = case parseAttribute attr of  Right attr' -> pure attr'  Left err -> throwIO (CouldNotParseAttribute err)parseExpression :: String -> Either String ExpressionparseExpression = parse' "expression" expressionparseExpressionThrows :: String -> IO ExpressionparseExpressionThrows ex = case parseExpression ex of  Right expr -> pure expr  Left err -> throwIO (CouldNotParseExpression err)parseProgram :: String -> Either String ProgramparseProgram = parse' "program" programparseProgramThrows :: String -> IO ProgramparseProgramThrows prg = case parseProgram prg of  Right prog -> pure prog  Left err -> throwIO (CouldNotParseProgram err)