1 -- A very simple, but useful, lexical analyser.
2 module SimpleLex(simpleLex) where
4 oper = "!#$%&*+./<=>?@\\^|:~-"
5 -- self-delim ()[]{},;`'"_
6 isalunum c = isAlphanum c || c == '_'
8 simpleLex :: String -> [String]
10 simpleLex (' ' :cs) = simpleLex cs -- ignore white space
11 simpleLex ('\t':cs) = simpleLex cs
12 simpleLex ('\n':cs) = simpleLex cs
13 simpleLex ('-':cs@(c:_)) | isDigit c = -- negative numbers
14 let (t:ts) = simpleLex cs
16 simpleLex (c:cs) | isDigit c = -- numbers (with optional .)
17 let (nn, cs') = span isDigit cs
19 '.':cs'' -> let (d,r) = span isDigit cs''
20 in (c:nn++'.':d) : simpleLex r
21 _ -> (c:nn) : simpleLex cs'
22 simpleLex (c:cs) | isAlpha c = -- identifiers
23 let (nn, cs') = span isalunum cs in (c:nn) : simpleLex cs'
24 simpleLex (c:cs) | c `elem` oper = -- operator
25 let (nn, cs') = span (`elem` oper) cs in (c:nn) : simpleLex cs'
26 simpleLex (c:cs) = [c] : simpleLex cs -- self delimiting chars