-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parsec combinators? #15
Comments
I guess that's what PRs are for and they could get accepted or not or could lead to some discussion. |
Its a work in progress, currently in my private project. Will generate a PR On Wed, Oct 22, 2014 at 9:38 AM, Kirill Zaborsky notifications@github.com
|
But probably some small gist with their API? |
parseSheparseSheet :: Worksheet -> [DetailLine2]
parseSheet sh = catMaybes $ map (parseRow sh) [3 .. 3]
parseRow :: Worksheet -> RowNum -> Maybe DetailLine2
parseRow sh row = r
`debug` ("parseRow:cells= " ++ show cells)
where
cells = map (\col -> cellsh sh (row,col)) [1..11]
r = case parseDl cells of
Left err -> Nothing `debug` ( "parseRow " ++ show row ++ ":" ++ show
err)
Right dl -> Just dl
-- parse :: Stream s Identity t => Parsec s () a -> SourceName -> s ->
Either ParseError a
parseDl :: [Maybe CellValue] -> Either ParseError DetailLine2
parseDl ss = parse p "source" ss
type P a = Parsec [Maybe CellValue] () a
p :: P DetailLine2
p = pDLHeading
pDLHeading :: P DetailLine2
pDLHeading = do
many1 pEmpty
name <- pText
many1 pEmpty
pLabel "Date: Statement For: "
pEmpty
date <- pNumber
return (DLHeading name date)
-- | Return the text from a cell
pText :: P T.Text
pText = tokenPrim show nextPos getMaybeText
where
nextPos pos _ _ = incSourceColumn pos 1
getMaybeText mt = case mt of
Just (CellText str) -> Just (T.fromStrict str)
_ -> Nothing
-- | Return the value of a cell
pNumber :: P Rational
pNumber = tokenPrim show nextPos getMaybeNumber
where
nextPos pos _ _ = incSourceColumn pos 1
getMaybeNumber mt = case mt of
Just (CellDouble d) -> Just (double2Rational d)
_ -> Nothing
-- | Parse an empty cell
pEmpty :: P ()
pEmpty = tokenPrim show nextPos getMaybeCell
where
nextPos pos _ _ = incSourceColumn pos 1
getMaybeCell mt = case mt of
Just _ -> Nothing
_ -> Just ()
-- | Match a cell with a specific label
pLabel :: T.Text -> P ()
pLabel label = tokenPrim show nextPos matchText
where
nextPos pos _ _ = incSourceColumn pos 1
matchText mt = case mt of
Just (CellText str) -> if label == T.fromStrict str
then Just ()
else Nothing
_ -> Nothing
et :: Worksheet -> [DetailLine2]
parseSheet sh = catMaybes $ map (parseRow sh) [3 .. 3]
parseRow :: Worksheet -> RowNum -> Maybe DetailLine2
parseRow sh row = r
`debug` ("parseRow:cells= " ++ show cells)
where
cells = map (\col -> cellsh sh (row,col)) [1..11]
r = case parseDl cells of
Left err -> Nothing `debug` ( "parseRow " ++ show row ++ ":" ++ show
err)
Right dl -> Just dl
-- parse :: Stream s Identity t => Parsec s () a -> SourceName -> s ->
Either ParseError a
parseDl :: [Maybe CellValue] -> Either ParseError DetailLine2
parseDl ss = parse p "source" ss
type P a = Parsec [Maybe CellValue] () a
p :: P DetailLine2
p = pDLHeading
pDLHeading :: P DetailLine2
pDLHeading = do
many1 pEmpty
name <- pText
many1 pEmpty
pLabel "Date: Statement For: "
pEmpty
date <- pNumber
return (DLHeading name date)
-- | Return the text from a cell
pText :: P T.Text
pText = tokenPrim show nextPos getMaybeText
where
nextPos pos _ _ = incSourceColumn pos 1
getMaybeText mt = case mt of
Just (CellText str) -> Just (T.fromStrict str)
_ -> Nothing
-- | Return the value of a cell
pNumber :: P Rational
pNumber = tokenPrim show nextPos getMaybeNumber
where
nextPos pos _ _ = incSourceColumn pos 1
getMaybeNumber mt = case mt of
Just (CellDouble d) -> Just (double2Rational d)
_ -> Nothing
-- | Parse an empty cell
pEmpty :: P ()
pEmpty = tokenPrim show nextPos getMaybeCell
where
nextPos pos _ _ = incSourceColumn pos 1
getMaybeCell mt = case mt of
Just _ -> Nothing
_ -> Just ()
-- | Match a cell with a specific label
pLabel :: T.Text -> P ()
pLabel label = tokenPrim show nextPos matchText
where
nextPos pos _ _ = incSourceColumn pos 1
matchText mt = case mt of
Just (CellText str) -> if label == T.fromStrict str
then Just ()
else Nothing
_ -> Nothing
|
Github is a bit strange it did not use markdown for you email I guess, your message should look like this I think: parseSheparseSheet :: Worksheet -> [DetailLine2]
parseSheet sh = catMaybes $ map (parseRow sh) [3 .. 3]
parseRow :: Worksheet -> RowNum -> Maybe DetailLine2
parseRow sh row = r
`debug` ("parseRow:cells= " ++ show cells)
where
cells = map (\col -> cellsh sh (row,col)) [1..11]
r = case parseDl cells of
Left err -> Nothing `debug` ( "parseRow " ++ show row ++ ":" ++ show
err)
Right dl -> Just dl
-- parse :: Stream s Identity t => Parsec s () a -> SourceName -> s ->
Either ParseError a
parseDl :: [Maybe CellValue] -> Either ParseError DetailLine2
parseDl ss = parse p "source" ss
type P a = Parsec [Maybe CellValue] () a
p :: P DetailLine2
p = pDLHeading
pDLHeading :: P DetailLine2
pDLHeading = do
many1 pEmpty
name <- pText
many1 pEmpty
pLabel "Date: Statement For: "
pEmpty
date <- pNumber
return (DLHeading name date)
-- | Return the text from a cell
pText :: P T.Text
pText = tokenPrim show nextPos getMaybeText
where
nextPos pos _ _ = incSourceColumn pos 1
getMaybeText mt = case mt of
Just (CellText str) -> Just (T.fromStrict str)
_ -> Nothing
-- | Return the value of a cell
pNumber :: P Rational
pNumber = tokenPrim show nextPos getMaybeNumber
where
nextPos pos _ _ = incSourceColumn pos 1
getMaybeNumber mt = case mt of
Just (CellDouble d) -> Just (double2Rational d)
_ -> Nothing
-- | Parse an empty cell
pEmpty :: P ()
pEmpty = tokenPrim show nextPos getMaybeCell
where
nextPos pos _ _ = incSourceColumn pos 1
getMaybeCell mt = case mt of
Just _ -> Nothing
_ -> Just ()
-- | Match a cell with a specific label
pLabel :: T.Text -> P ()
pLabel label = tokenPrim show nextPos matchText
where
nextPos pos _ _ = incSourceColumn pos 1
matchText mt = case mt of
Just (CellText str) -> if label == T.fromStrict str
then Just ()
else Nothing
_ -> Nothing
et :: Worksheet -> [DetailLine2]
parseSheet sh = catMaybes $ map (parseRow sh) [3 .. 3]
parseRow :: Worksheet -> RowNum -> Maybe DetailLine2
parseRow sh row = r
`debug` ("parseRow:cells= " ++ show cells)
where
cells = map (\col -> cellsh sh (row,col)) [1..11]
r = case parseDl cells of
Left err -> Nothing `debug` ( "parseRow " ++ show row ++ ":" ++ show
err)
Right dl -> Just dl
-- parse :: Stream s Identity t => Parsec s () a -> SourceName -> s ->
Either ParseError a
parseDl :: [Maybe CellValue] -> Either ParseError DetailLine2
parseDl ss = parse p "source" ss
type P a = Parsec [Maybe CellValue] () a
p :: P DetailLine2
p = pDLHeading
pDLHeading :: P DetailLine2
pDLHeading = do
many1 pEmpty
name <- pText
many1 pEmpty
pLabel "Date: Statement For: "
pEmpty
date <- pNumber
return (DLHeading name date)
-- | Return the text from a cell
pText :: P T.Text
pText = tokenPrim show nextPos getMaybeText
where
nextPos pos _ _ = incSourceColumn pos 1
getMaybeText mt = case mt of
Just (CellText str) -> Just (T.fromStrict str)
_ -> Nothing
-- | Return the value of a cell
pNumber :: P Rational
pNumber = tokenPrim show nextPos getMaybeNumber
where
nextPos pos _ _ = incSourceColumn pos 1
getMaybeNumber mt = case mt of
Just (CellDouble d) -> Just (double2Rational d)
_ -> Nothing
-- | Parse an empty cell
pEmpty :: P ()
pEmpty = tokenPrim show nextPos getMaybeCell
where
nextPos pos _ _ = incSourceColumn pos 1
getMaybeCell mt = case mt of
Just _ -> Nothing
_ -> Just ()
-- | Match a cell with a specific label
pLabel :: T.Text -> P ()
pLabel label = tokenPrim show nextPos matchText
where
nextPos pos _ _ = incSourceColumn pos 1
matchText mt = case mt of
Just (CellText str) -> if label == T.fromStrict str
then Just ()
else Nothing
_ -> Nothing
|
yep On Wed, Oct 22, 2014 at 11:22 AM, Kirill Zaborsky notifications@github.com
|
Sorry to revive this already quite old issue, but what is the preferred way of parsing spreadsheets? Obviously, the traditional stream-based parsers are a bit limited, since
(see also this comment) If stream-based spreadsheet parsing turns out to be of general interest, I could release a xlsx-megaparsec library. I think this has no place in the xlsx library itself. |
I am writing some Parsec combinators for my own use on top of this.
Do you want a pull request for them when I am done? I am not sure if they belong in this library.
The text was updated successfully, but these errors were encountered: