[Haskell-beginners] Parsing Custom TeX
Jeffrey Drake
jeffd at techsociety.ca
Fri Nov 7 22:02:30 EST 2008
I am getting frustrated when trying to implement a parser for a minimal
TeX subset. I am essentially trying to implement something that
recognizes \commands (no parameters yet), paragraphs (two newlines), and
text (almost anything else).
The code I have so far is:
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
| Text String
| Paragraph
deriving Show
command :: Parser UnTeX
command = do char '\\'
cmd <- many1 (letter <|> digit) <?> "command"
return $ Command cmd
paragraph :: Parser UnTeX
paragraph = do newline
newline
return $ Paragraph
text :: Parser UnTeX
text = do txt <- many1 (alphaNum <|> space)
return $ Text txt
I have no illusions that this is probably far from what I actually need
to be doing. But I am finding documentation on all this to be spotty.
The 'parsec.pdf' is 7 years old and code doesn't always work.
Can anyone point me in the right direction? I just want something to
able to do this simply. After an initial version works, I would like to
have commands with parameters - something like
\command[param][param]{body} but need to have something working first.
Thank you for any help you can provide.
Jeffrey.
More information about the Beginners
mailing list