phino 0.0.0.35 → 0.0.0.36
raw patch · 4 files changed
+40/−13 lines, 4 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ Parser: parseNumber :: String -> Either String Expression
+ Parser: parseNumberThrows :: String -> IO Expression
Files
- phino.cabal +1/−1
- src/Functions.hs +9/−1
- src/Misc.hs +4/−0
- src/Parser.hs +26/−11
phino.cabal view
@@ -1,7 +1,7 @@ cabal-version: 3.0 name: phino-version: 0.0.0.35+version: 0.0.0.36 license: MIT synopsis: Command-Line Manipulator of 𝜑-Calculus Expressions description: Please see the README on GitHub at <https://github.com/objectionary/phino#readme>
src/Functions.hs view
@@ -17,7 +17,7 @@ import Matcher import Misc import Numeric (showHex)-import Parser (parseAttributeThrows)+import Parser (parseAttributeThrows, parseNumberThrows) import Pretty import Random (randomString) import Regexp@@ -163,4 +163,12 @@ attr' <- buildAttributeThrows attr subst pure (TeExpression (DataString (strToBts (prettyAttribute attr')))) buildTermFromFunction "string" _ _ _ = throwIO (userError "Function string() requires exactly 1 argument as attribute or data expression (Φ̇.number or Φ̇.string)")+buildTermFromFunction "number" [Y.ArgExpression expr] subst _ = do+ (expr', _) <- buildExpressionThrows expr subst+ case expr' of+ DataString bts -> do+ num <- parseNumberThrows (btsToUnescapedStr bts)+ pure (TeExpression num)+ _ -> throwIO (userError (printf "Function number() expects expression to be 'Φ̇.string', but got:\n%s" (prettyExpression' expr')))+buildTermFromFunction "number" _ _ _ = throwIO (userError "Function number() requires exactly 1 argument as 'Φ̇.string'") buildTermFromFunction func _ _ _ = throwIO (userError (printf "Function %s() is not supported or does not exist" func))
src/Misc.hs view
@@ -239,6 +239,8 @@ -- "hello" -- >>> btsToStr (BtOne "68") -- "h"+-- >>> btsToStr (BtOne "35")+-- "5" -- >>> btsToStr (BtMany ["77", "6F", "72", "6C", "64"]) -- "world" -- >>> btsToStr BtEmpty@@ -268,6 +270,8 @@ -- "world" -- >>> btsToUnescapedStr (BtMany ["68", "22"]) -- "h\""+-- >>> btsToUnescapedStr (BtOne "35")+-- "5" btsToUnescapedStr :: Bytes -> String btsToUnescapedStr bytes = T.unpack (T.decodeUtf8 (B.pack (btsToWord8 bytes)))
src/Parser.hs view
@@ -12,6 +12,8 @@ parseExpressionThrows, parseAttribute, parseAttributeThrows,+ parseNumber,+ parseNumberThrows, parseBinding, parseBytes, )@@ -39,12 +41,14 @@ = CouldNotParseProgram {message :: String} | CouldNotParseExpression {message :: String} | CouldNotParseAttribute {message :: String}+ | CouldNotParseNumber {message :: String} deriving (Exception) instance Show ParserException where show CouldNotParseProgram {..} = printf "Couldn't parse given phi program, cause: %s" message- show CouldNotParseExpression {..} = 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 consumer whiteSpace :: Parser ()@@ -202,6 +206,18 @@ <?> "bytes" ) +number :: Parser Expression+number = do+ sign <- optional (choice [char '-', char '+'])+ unsigned <- lexeme L.scientific+ let num =+ toRealFloat+ ( case sign of+ Just '-' -> negate unsigned+ _ -> unsigned+ )+ return (DataNumber (numToBts num))+ tauBinding :: Parser Attribute -> Parser Binding tauBinding attr = do attr' <- attr@@ -329,16 +345,7 @@ do _ <- choice [symbol "T", symbol "⊥"] return ExTermination,- do- sign <- optional (choice [char '-', char '+'])- unsigned <- lexeme L.scientific- let num =- toRealFloat- ( case sign of- Just '-' -> negate unsigned- _ -> unsigned- )- return (DataNumber (numToBts num)),+ number, lexeme $ do _ <- char '"' str <- manyTill (choice [escapedChar, noneOf ['\\', '"']]) (char '"')@@ -437,6 +444,14 @@ parseBinding :: String -> Either String Binding parseBinding = parse' "binding" binding++parseNumber :: String -> Either String Expression+parseNumber = parse' "number" number++parseNumberThrows :: String -> IO Expression+parseNumberThrows num = case parseNumber num of+ Right num' -> pure num'+ Left err -> throwIO (CouldNotParseNumber err) parseAttribute :: String -> Either String Attribute parseAttribute = parse' "attribute" fullAttribute