tibetan-utils (empty) → 0.1.0.0
raw patch · 8 files changed
+177/−0 lines, 8 filesdep +basedep +compositiondep +eithersetup-changed
Dependencies added: base, composition, either, hspec, hspec-megaparsec, megaparsec, text, text-show, tibetan-utils
Files
- LICENSE +30/−0
- README.md +5/−0
- Setup.hs +2/−0
- src/Text/Megaparsec/Char/Tibetan.hs +8/−0
- src/Text/Megaparsec/Lexer/Tibetan.hs +34/−0
- src/TextShow/Data/Integral/Tibetan.hs +21/−0
- test/Spec.hs +27/−0
- tibetan-utils.cabal +50/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Vanessa McHale (c) 2017++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Vanessa McHale nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,5 @@+## Tibetan numeral parser++Haskell library with test suite to parse Tibetan numerals as integers.++See [Text.Megaparsec](https://hackage.haskell.org/package/megaparsec) documentation for how to use.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Text/Megaparsec/Char/Tibetan.hs view
@@ -0,0 +1,8 @@+module Text.Megaparsec.Char.Tibetan where++import Text.Megaparsec+import Text.Megaparsec.Text++-- | Parse a consonant+consonantCharBo :: Parser Char+consonantCharBo = oneOf ("ཨཅཆརཏཡཕཙཚཛའསདབངམགལཞཟཤཀཁཔནཐཇཉཝཧ" :: String)
+ src/Text/Megaparsec/Lexer/Tibetan.hs view
@@ -0,0 +1,34 @@+{-# LANGUAGE FlexibleContexts #-}++-- | Parser to parse Tibetan numerals+module Text.Megaparsec.Lexer.Tibetan+ ( parseNumber+ ) where++import Data.Composition+import qualified Data.Text as T+import Text.Megaparsec+import Text.Megaparsec.Text+import Text.Megaparsec.Prim+import System.Environment+import Data.Either.Combinators++-- | Yields a command line parser in case you want a command-line executable for use with another language+readBo :: String -> Maybe Integer+readBo = rightToMaybe . (runParser parseNumber "") . T.pack++-- | Parse Tibetan numerals, returning a positive integer+parseNumber :: Parser Integer+parseNumber = do+ digits <- reverse <$> some parseNumeral+ (pure . (`div` 10) $ foldr ((*10) .* (+)) 0 digits) <?> "tibetan integer"++-- | Parse a single digit+parseNumeral :: Parser Integer+parseNumeral = foldr (<|>) (parseDigit '༠' 0) $ zipWith parseDigit "༠༡༢༣༤༥༦༧༨༩" (0:[1..9])++-- | Parser a given char as a given integer+parseDigit :: Char -> Integer -> Parser Integer+parseDigit c i = do+ char c+ pure i
+ src/TextShow/Data/Integral/Tibetan.hs view
@@ -0,0 +1,21 @@+module TextShow.Data.Integral.Tibetan ( builderBo+ , showBo) where++import qualified Data.Text.Lazy as TL+import TextShow++-- | Display positive integer as a `String`+showBo :: Integer -> Maybe String+showBo = (fmap toString) . builderBo++-- | show a positive intgeger as text, return `Nothing` if that doesn't work+builderBo :: (Integral a) => a -> Maybe Builder+builderBo int+ | int >= 0 = Just . fromLazyText $ foldr (.) (id) + (zipWith TL.replace + (map showT [0..9])+ ["༠","༡","༢","༣","༤","༥","༦","༧","༨","༩"])+ (showT (fromIntegral int :: Integer))+ | otherwise = Nothing++showT = TL.pack . show
+ test/Spec.hs view
@@ -0,0 +1,27 @@+{-# LANGUAGE OverloadedStrings #-}++import Test.Hspec+import Test.Hspec.Megaparsec+import Text.Megaparsec.Lexer.Tibetan+import Text.Megaparsec+import TextShow.Data.Integral.Tibetan+import qualified Data.Text as T++main :: IO ()+main = hspec $ do+ describe "parseNumber" $ do+ it "parses Tibetan numerals as an Int" $ do+ (runParser parseNumber "") tibStr `shouldParse` 6320+ it "fails when given a character that isn't a tibetan numeral" $ do+ (runParser parseNumber "") `shouldFailOn` otherStr+ describe "showBo" $ do+ it "displays a positive integer using tibetan numerals" $ do+ showBo 15 `shouldBe` Just ("༡༥")+ it "fails on negative inputs" $ do+ showBo (-12) `shouldBe` Nothing++tibStr :: T.Text+tibStr = "༦༣༢༠"++otherStr :: T.Text+otherStr = "ཨ་ནི"
+ tibetan-utils.cabal view
@@ -0,0 +1,50 @@+name: tibetan-utils+version: 0.1.0.0+cabal-version: >=1.10+build-type: Simple+license: BSD3+license-file: LICENSE+copyright: Copyright: (c) 2016 Vanessa McHale+maintainer: tmchale@wisc.edu+homepage: https://github.com/vmchale/tibetan-utils#readme+synopsis: Initial project template from stack+description:+ Please see README.md+category: Web+author: Vanessa McHale+extra-source-files:+ README.md++source-repository head+ type: git+ location: https://github.com/vmchale/tibetan-utils++library+ exposed-modules:+ Text.Megaparsec.Lexer.Tibetan+ Text.Megaparsec.Char.Tibetan+ TextShow.Data.Integral.Tibetan+ build-depends:+ base >=4.7 && <5,+ megaparsec >=5.0.1 && <5.1,+ text >=1.2.2.1 && <1.3,+ composition >=1.0.2.1 && <1.1,+ text-show ==3.4.*,+ either >=4.4.1.1 && <4.5+ default-language: Haskell2010+ default-extensions: OverloadedStrings+ hs-source-dirs: src++test-suite tibetan-utils-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ build-depends:+ base >=4.9.0.0 && <4.10,+ tibetan-utils >=0.1.0.0 && <0.2,+ hspec >=2.2.4 && <2.3,+ hspec-megaparsec >=0.2.1 && <0.3,+ text >=1.2.2.1 && <1.3,+ megaparsec >=5.0.1 && <5.1+ default-language: Haskell2010+ hs-source-dirs: test+ ghc-options: -threaded -rtsopts -with-rtsopts=-N