Basic working version of parser (no serialization yet)
Getty Ritter
11 years ago
| 1 | {-# LANGUAGE OverloadedStrings #-} | |
| 2 | ||
| 3 | module Data.TeLML.Parser (Fragment(..), parseDocument) where | |
| 4 | ||
| 5 | import Control.Applicative | |
| 6 | import Data.Attoparsec.Text | |
| 7 | import Data.Char (isAlphaNum) | |
| 8 | import Data.Text (Text) | |
| 9 | import qualified Data.Text as T | |
| 10 | import Prelude hiding (takeWhile) | |
| 11 | ||
| 12 | data Fragment | |
| 13 | = Chunk Text | |
| 14 | | Tag Text [Fragment] | |
| 15 | deriving (Eq, Show) | |
| 16 | ||
| 17 | isSpecial :: Char -> Bool | |
| 18 | isSpecial '\\' = True | |
| 19 | isSpecial '{' = True | |
| 20 | isSpecial '}' = True | |
| 21 | isSpecial '|' = True | |
| 22 | isSpecial _ = False | |
| 23 | ||
| 24 | isTagChar :: Char -> Bool | |
| 25 | isTagChar c = isAlphaNum c | |
| 26 | ||
| 27 | parseFg :: Parser Fragment | |
| 28 | parseFg = (char '\\' *> parseBk) | |
| 29 | <|> (char '{' *> parseFg) | |
| 30 | <|> (Chunk <$> takeWhile1 (not . isSpecial)) | |
| 31 | ||
| 32 | parseBk :: Parser Fragment | |
| 33 | parseBk = (char '\\' *> pure (Chunk "\\")) | |
| 34 | <|> (char '|' *> pure (Chunk "|")) | |
| 35 | <|> parseTag | |
| 36 | ||
| 37 | parseTag :: Parser Fragment | |
| 38 | parseTag = | |
| 39 | Tag <$> (takeWhile isTagChar <* many space) | |
| 40 | <*> (char '{' *> sepBy parseFg (char '|') <* char '}') | |
| 41 | ||
| 42 | parseDocument :: Text -> Either String [Fragment] | |
| 43 | parseDocument = parseOnly (many parseFg <* endOfInput) |
| 1 | module Data.TeLML(parse, Document, Fragment(..)) where | |
| 2 | ||
| 3 | import Data.TeLML.Parser | |
| 4 | import Data.Text (Text) | |
| 5 | ||
| 6 | type Document = [Fragment] | |
| 7 | ||
| 8 | parse :: Text -> Maybe Document | |
| 9 | parse t = | |
| 10 | case parseDocument t of | |
| 11 | Left _ -> Nothing | |
| 12 | Right d -> Just d |
| 1 | Copyright (c) 2014, Getty Ritter | |
| 2 | ||
| 3 | All rights reserved. | |
| 4 | ||
| 5 | Redistribution and use in source and binary forms, with or without | |
| 6 | modification, are permitted provided that the following conditions are met: | |
| 7 | ||
| 8 | * Redistributions of source code must retain the above copyright | |
| 9 | notice, this list of conditions and the following disclaimer. | |
| 10 | ||
| 11 | * Redistributions in binary form must reproduce the above | |
| 12 | copyright notice, this list of conditions and the following | |
| 13 | disclaimer in the documentation and/or other materials provided | |
| 14 | with the distribution. | |
| 15 | ||
| 16 | * Neither the name of Getty Ritter nor the names of other | |
| 17 | contributors may be used to endorse or promote products derived | |
| 18 | from this software without specific prior written permission. | |
| 19 | ||
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 1 | -- Initial telml.cabal generated by cabal init. For further documentation, | |
| 2 | -- see http://haskell.org/cabal/users-guide/ | |
| 3 | ||
| 4 | name: telml | |
| 5 | version: 0.1.0.0 | |
| 6 | -- synopsis: | |
| 7 | -- description: | |
| 8 | license: BSD3 | |
| 9 | license-file: LICENSE | |
| 10 | author: Getty Ritter | |
| 11 | maintainer: gdritter@galois.com | |
| 12 | -- copyright: | |
| 13 | category: Data | |
| 14 | build-type: Simple | |
| 15 | -- extra-source-files: | |
| 16 | cabal-version: >=1.10 | |
| 17 | ||
| 18 | library | |
| 19 | exposed-modules: Data.TeLML | |
| 20 | other-modules: Data.TeLML.Parser | |
| 21 | -- other-extensions: | |
| 22 | build-depends: base >=4.7 && <4.8, text, attoparsec | |
| 23 | -- hs-source-dirs: | |
| 24 | default-language: Haskell2010 |