[Haskell-cafe] Parsing problem
Eric
eeoam at ukfsn.org
Thu Jun 26 12:13:50 EDT 2008
Daniel Fischer wrote:
> Am Donnerstag, 26. Juni 2008 12:40 schrieb Eric:
>
> What does the tokeniser return?
> I would have to see more of the code to diagnose it.
>
Here is the code for the tokenizer:
type Scanner a = GenParser Char () a
data Token
= INum Integer | FNum Double
| Varid String | Reserved String
deriving (Show,Eq)
scan :: Scanner a -> Scanner(a, SourcePos)
scan p = do pos <- getPosition
x <- p
return(x,pos)
scan_integer, scan_varid, hreserved, htoken :: Scanner
(Token,SourcePos)
scan_integer = do (i,pos) <- scan(integer (makeTokenParser haskellDef))
return (INum i, pos)
scan_varid = do (c,pos) <- scan(hlower)
cs <- identifier(makeTokenParser haskellDef)
return (Varid (c:cs), pos)
hlower :: Scanner Char
hlower = lower <|> char '_'
hreserved = do (cs, pos) <- scan(string "(" <|> string ")")
return (Reserved cs,pos)
htoken = scan_integer <|> scan_varid <|> hreserved
scanall :: String -> [(Token,SourcePos)]
scanall cs = let result = parse (many htoken) "" cs
in case result of
Right tkns -> tkns
Left err -> error (show err)
E.
More information about the Haskell-Cafe
mailing list