-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(journal): add parsing of journal into ast, improve readability o…
…f test output
- Loading branch information
1 parent
86b2af7
commit 1ebc4aa
Showing
5 changed files
with
178 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module Journal.Internal.Parse where | ||
|
||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString.Char8 as BSChar8 | ||
import Data.ByteString.Internal (c2w, w2c) | ||
import Data.Char (digitToInt) | ||
import Data.List (foldl') | ||
import Data.Word (Word32, Word8) | ||
import Text.Parsec | ||
import Text.Parsec.ByteString (Parser, parseFromFile) | ||
|
||
------------------------------------------------------------------------ | ||
|
||
data JournalAST = JournalAST | ||
{ jaActiveFile :: FileAST | ||
} | ||
|
||
data FileAST = FileAST | ||
{ faContent :: [EntryAST] | ||
, faFooter :: Maybe FooterAST | ||
} | ||
deriving Show | ||
|
||
data EntryAST = EntryAST HeaderAST BodyAST | ||
deriving Show | ||
|
||
data HeaderAST = HeaderAST | ||
{ haTag :: Word8 | ||
, haVersion :: Word8 | ||
, haLength :: Word32 | ||
} | ||
deriving Show | ||
|
||
newtype BodyAST = BodyAST ByteString | ||
deriving Show | ||
|
||
data FooterAST = FooterAST HeaderAST PaddingAST | ||
deriving Show | ||
|
||
newtype PaddingAST = PaddingAST ByteString | ||
deriving Show | ||
|
||
------------------------------------------------------------------------ | ||
|
||
parseFileAST :: FilePath -> IO (Either ParseError FileAST) | ||
parseFileAST fp = parseFromFile fileASTP fp | ||
|
||
fileASTP :: Parser FileAST | ||
fileASTP = FileAST <$> many1 entryP <*> optionMaybe footerP <?> "fileASTP" | ||
|
||
entryP :: Parser EntryAST | ||
entryP = EntryAST <$> headerP <*> bodyP <?> "entryP" | ||
|
||
headerP :: Parser HeaderAST | ||
headerP = HeaderAST <$> tagP <*> versionP <*> lengthP <?> "headerP" | ||
|
||
tagP :: Parser Word8 | ||
tagP = c2w <$> (char (w2c (fromIntegral 0)) | ||
<|> char (w2c (fromIntegral 1)) | ||
<|> char (w2c (fromIntegral 2)) | ||
<|> char (w2c (fromIntegral 4)) | ||
<?> "tagP") | ||
|
||
versionP :: Parser Word8 | ||
versionP = c2w <$> anyToken <?> "versionP" | ||
|
||
lengthP :: Parser Word32 | ||
lengthP = fromIntegral . foldl' (\a i -> a * 10 + digitToInt i) 0 <$> | ||
count 4 anyToken <?> "lengthP" | ||
|
||
bodyP :: Parser BodyAST | ||
bodyP = BodyAST . BSChar8.pack <$> many1 anyToken <?> "bodyP" | ||
|
||
footerP :: Parser FooterAST | ||
footerP = FooterAST <$> headerP <*> paddingP <?> "footerP" | ||
|
||
paddingP :: Parser PaddingAST | ||
paddingP = PaddingAST . BSChar8.pack <$> | ||
(many1 (char '\NUL') <|> eof *> pure "") <?> "paddingP" |
Oops, something went wrong.