Guest User

Untitled

a guest
Jan 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Maybe ( fromJust )
  2. import Text.ParserCombinators.Parsec
  3.  
  4. -- plain text. Let's say '/' is our token, this will parse
  5.     + "fsfasd/dadc" -> "fsfasd/dadc"
  6.     + "fsfasd / dadc" -> "fsfasd / dadc"
  7.     + "fsfasd /dadc" -> "fsfasd"
  8.     + "fsfasd/ dadc" -> "fsfasd"
  9. -- i.e. tokens "surrounded" by spaces or letters
  10. -- are innoquous.
  11. -- oh, tokenList is something like ['/','-',':']
  12. text :: Parser String
  13. text = many1 (noneOf tokenList) >>= \parText -> -- many1: we're sure it's text
  14.                                                 -- and not straight an inline
  15.          
  16.        -- we've met a token, should we continue ( " / " "cvdf/vdf" or not?)        
  17.        let lastChar = last parText in -- safe, since we've used many1
  18.        
  19.        -- so let's try a "continue text" stuff
  20.        try ( oneOf tokenList >>= \token   ->
  21.  
  22.              ( if lastChar == ' ' -- if last was a whitespace, expect ws
  23.                  then space       -- if it was not, any char does
  24.                  else noneOf " "
  25.              ) >>= \charAdd ->
  26.                
  27.              newTextParser   >>= \newText ->
  28.              return ( parText ++ [token, charAdd] ++ newText )
  29.            )
  30.        
  31.        <|> return (parText)  
  32.     where newTextParser =  try text
  33.                        <|> return ""
Add Comment
Please, Sign In to add comment