I'm writing a lexer in haskell. Here's the code:
lexer :: String -> [Token]
lexer s
| s =~ whitespace :: Bool =
let token = s =~ whitespace :: String in
lex (drop (length token) s)
| s =~ number :: Bool =
let token = s =~ number :: String in
Val (read token) : lex (drop (length token) s)
| s =~ operator :: Bool =
let token = s =~ operator :: String in
Oper token : lex (drop (length token) s)
| otherwise = error "unrecognized character"
where
whitespace = "^[ \t\n]"
number = "^[0-9]*(\.[0-9]+)?"
operator = "^[+-*/()]"
data Token = Val Int | Oper String
There are two problems I'm having. One, the number regex "^[0-9]*(\.[0-9]+)?"
throws this error:
lexical error in string/character literal at character '['
And when I comment out the line containing it and the part of the function that uses it, I get this error:
Couldn't match expected type `Token' against inferred type `(String, String)' Expected type: [Token] Inferred type: [(String, String)] In the expression: lex (drop (length token) s) In the expression: let token = s =~ whitespace :: String in lex (drop (length token) s)
I have no idea why I'm getting either of these errors. Can someone help me?