Skip to content
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

Parsing into a list delimited by a token #46

Closed
s5bug opened this issue Jan 20, 2020 · 5 comments
Closed

Parsing into a list delimited by a token #46

s5bug opened this issue Jan 20, 2020 · 5 comments

Comments

@s5bug
Copy link

s5bug commented Jan 20, 2020

I'm a very novice Haskell programmer and I wanted to write something with parsers. My current solution for parsing abc.def.ghi uses the split package like so:

dparse :: (String -> Bool) -> Prod r String String [String]
dparse f = splitOn "." <$> satisfy f

I feel like, from my Scala+cats knowledge, that this is a very poor way to do this. How should I properly implement it?

@ollef
Copy link
Owner

ollef commented Jan 20, 2020

Hey!

Several parser combinator libraries define a function called sepBy, which might be close what you're after. Perhaps it would help to have a look at what they do, e.g. https://www.stackage.org/haddock/lts-14.20/parser-combinators-1.1.0/src/Control.Applicative.Combinators.html#sepBy. Note that you have to use rule in Earley to create recursive parsers.

Your approach might work as well. If you e.g. want to parse qualified names, you could first parse a list of allowed characters including '.' using Earley, and then split it outside of Earley (e.g. using splitOn "." <$> ... as in your example).

Hope this helps!

@s5bug
Copy link
Author

s5bug commented Jan 20, 2020

The sepBy you linked isn't recursive, but copying it to Earley doesn't actually split and just outputs "abc.def.ghi" instead of ["abc", "def", "ghi"].

sepBy :: Alternative m => m a -> m sep -> m [a]
sepBy p sep = sepBy1 p sep <|> pure []

sepBy1 :: Alternative m => m a -> m sep -> m [a]
sepBy1 p sep = liftA2 (:) p (many (sep *> p))

dparse :: (String -> Bool) -> Prod r String String [String]
dparse f = sepBy (satisfy f) (token ".")

@ollef
Copy link
Owner

ollef commented Jan 20, 2020

Maybe you meant for your parser to work on Chars as tokens instead of Strings? Otherwise you need to tokenise first.

@s5bug
Copy link
Author

s5bug commented Jan 20, 2020

Probably? Right now I'm trying to get it to parse

arti abc.def.ghi
pack jkl.mno

into

Prog
  (Arti
    (Domain ["abc", "def", "ghi"])
  )
  (Pack
    (Domain ["jkl", "mno"])
  )

So what you're saying is I shouldn't feed words into the parser and make it work on Char?

@s5bug
Copy link
Author

s5bug commented Jan 20, 2020

Yeah, I got it to work with Char:

sepBy :: Alternative m => m a -> m sep -> m [a]
sepBy p sep = sepBy1 p sep <|> pure []

sepBy1 :: Alternative m => m a -> m sep -> m [a]
sepBy1 p sep = liftA2 (:) p (many (sep *> p))

someBy :: Eq a => a -> Prod r e a [[a]]
someBy s = sepBy (some $ satisfy $ (/=) s) (token s)

I just have to properly handle whitespace now. Thank you!

@s5bug s5bug closed this as completed Jan 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants