[Haskell-beginners] Parsing Custom TeX

Jason Dusek jason.dusek at gmail.com
Sat Nov 8 04:00:20 EST 2008


  Each one of the little combinators seems to work as
  advertized. Are you having trouble fitting them together?

--
_jsn


module UnTeX where

import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Prim
import Text.ParserCombinators.Parsec.Language
import qualified Text.ParserCombinators.Parsec.Token as T

data UnTeX
  = Command String [String] String
  | Text String
  | Paragraph
 deriving Show


 -- I don't remember TeX very well, so I'm not sure this is right.
command                     ::  Parser UnTeX
command                      =  do
  char '\\'
  cmd                       <-  ident
  p                         <-  orNot params []
  b                         <-  orNot body ""
  return $ Command cmd p b
 where
  params                     =  many1 $ between (char '[') (char ']') ident
  body                       =  do
    char '{'
    text                    <-  many1 $ noneOf "}"
    char '}'
    return text
  ident                      =  many1 $ letter <|> digit
  orNot p n                  =  choice [try p , return n]


paragraph                   ::  Parser UnTeX
paragraph                    =  do
  newline
  newline
  return $ Paragraph

text                        ::  Parser UnTeX
text                         =  do
  txt                       <-  many1 (alphaNum <|> space)
  return $ Text txt


More information about the Beginners mailing list