|
definition: |
lex :: ReadS String
lex xs = case xs of
"" -> [("", "")]
(c:cs) | isSpace c -> lex $ dropWhile isSpace cs
('\'':s) ->
[('\'' : ch ++ "'", t) | (ch, '\'' : t) <- lexCharLiteral s, ch /= "'"]
('"':s) -> [('"' : str, t) | (str, t) <- lexString s]
(c:cs) | isSingle c -> [([c], cs)]
| isSymbol c -> [(c : sym, t) | (sym, t) <- [span isSymbol cs]]
| isAlpha c -> [(c : nam, t) | (nam, t) <- [span isIdChar cs]]
| isDigit c -> [ (c : ds ++ fe, t) | (ds, s) <- [span isDigit cs]
, (fe, t) <- lexFracExp s ]
| otherwise -> []
where
isSingle c = c `elem` ",;()[]{}_`"
isSymbol c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
isIdChar c = isAlphaNum c || c `elem` "_'"
lexFracExp s = case s of
('.':c:cs) | isDigit c ->
[('.' : ds ++ e, u) | (ds, t) <- lexDigits (c : cs), (e, u) <- lexExp t]
_ -> lexExp s
lexExp s = case s of
(e:cs) | e `elem` "eE" ->
[ (e : c : ds, u) | (c:t) <- [cs], c `elem` "+-"
, (ds, u) <- lexDigits t ] ++
[(e : ds, t) | (ds, t) <- lexDigits cs]
_ -> [("", s)]
lexString s = case s of
('"':cs) -> [("\"", cs)]
_ -> [ (ch ++ str, u) | (ch, t) <- lexStringItem s
, (str, u) <- lexString t ]
lexStringItem s = case s of
('\\':'&':cs) -> [("\\&", cs)]
('\\':c:cs) | isSpace c -> [("\\&", t) | '\\':t <- [dropWhile isSpace cs]]
_ -> lexCharLiteral s
|
|
demand: |
argument 1 |
|
deterministic: |
deterministic operation |
|
documentation: |
| Reads a single lexeme from the given string. Initial white space is discarded and the characters of the lexeme are returned. If the input string contains only white space, `lex` returns the empty string as lexeme. If there is no legal lexeme at the beginning of the input string, the operation fails, i.e., `[]` is returned. |
|
failfree: |
_ |
|
indeterministic: |
referentially transparent operation |
|
infix: |
no fixity defined |
|
iotype: |
{({[]}) |-> {:} || ({:}) |-> _}
|
|
name: |
lex |
|
precedence: |
no precedence defined |
|
result-values: |
_ |
|
signature: |
[Char] -> [([Char], [Char])] |
|
solution-complete: |
operation might suspend on free variables |
|
terminating: |
possibly non-terminating |
|
totally-defined: |
possibly non-reducible on same data term |