[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / lib / hbc / SimpleLex.hs
1 -- A very simple, but useful, lexical analyser.
2 module SimpleLex(simpleLex) where
3
4 oper = "!#$%&*+./<=>?@\\^|:~-"
5 -- self-delim ()[]{},;`'"_
6 isalunum c = isAlphanum c || c == '_'
7
8 simpleLex :: String -> [String]
9 simpleLex "" = []
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 
15         in  ('-':t) : ts
16 simpleLex (c:cs) | isDigit c =                          -- numbers (with optional .)
17         let (nn, cs') = span isDigit cs 
18         in  case cs' of
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