![parsec haskell parsec haskell](http://www.mega-nerd.com/erikd/Img/parsec_expr.png)
![parsec haskell parsec haskell](https://image.slidesharecdn.com/haskell-gong-2009-091120211849-phpapp01/95/parsec-3-keepalived-haskell-gong-2009-18-728.jpg)
In many implementations of todo.txt, the idea of sub-projects was introduce by using either a hyphen or dot to break up the project/sub-projects.
Parsec haskell plus#
Projects in todo.txt are defined as a CamelCased, alpha-numeric string starting with a plus sign. What if we wanted to have variations on an token that were much more diverse? Choice of tokens Our token changed but it was always just expecting more or less digits. In our previous example we had variation that was homomorphic. ' ' : The function read trims off white space before converting the string to the return type, so no need to differentiate between the two cases. The last step is returning an list of Char, either '0' : '9' : or '9' : But if we had the date and the y value consumed the character after '9' we would not get a valid parse of date. When we go to parse the hyphen we are in good shape. If we have the following date we expect x to be '0' and y to be '9'. In the failure case the stream data is not consumed, which is important. option tries to apply the parser, and if it fails the optional value is returned. The value for y is given as an optional value, either as a digit or as the space character. OneToTwoDigits = do x <- digit y <- option ' ' $ digit return ( x : y : ) To do this we need to read each set of digits as individual pieces that vary in length. We’d consider, , and 17-9-1 all to be the same date.
![parsec haskell parsec haskell](https://i.imgur.com/kw2kYzf.jpg)
2 to 4 digit years, where 2 digits represent 20XX.We will be allowing dates to be entered with the following rules: But in some cases you could have a varying description of what you would consider a valid token. It either matched the three characters or it didn’t. Our example parser was pretty straight forward. You should extend your tests to do more of a range, or at least upper and lower bounds to make the coverage better but for now we have a working parser and a unit test. In our test we verify that passing in a valid priority "(A)" works while an invalid priority does not. We are using the shouldBe and shouldNotBe functions from. Module ParserSpec where import Parser import Tasks import ( parse ) import Test.Hspec ( Spec, describe, it, shouldBe, shouldNotBe ) - | Required for auto-discovery spec :: Spec spec = describe "Parsers" $ do describe "Priority" $ do it "Matches (A) as Priority A" $ do parse priority "" "(A)" ` shouldBe ` Right ( 'A' :: Priority ) it "Doesn't match A as Priority A" $ do parse priority "" "A" ` shouldNotBe ` Right ( 'A' :: Priority )