Git Product home page Git Product logo

Comments (5)

s5bug avatar s5bug commented on June 21, 2024 1

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!

from earley.

ollef avatar ollef commented on June 21, 2024

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!

from earley.

s5bug avatar s5bug commented on June 21, 2024

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 ".")

from earley.

ollef avatar ollef commented on June 21, 2024

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

from earley.

s5bug avatar s5bug commented on June 21, 2024

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?

from earley.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.