Fix external core syntax (though not full compilation)
[ghc.git] / compiler / parser / LexCore.hs
1 module LexCore where
2
3 import ParserCoreUtils
4 import Ratio
5 import Char
6 import Numeric
7
8 isNameChar c = isAlpha c || isDigit c || (c == '_') || (c == '\'')
9 || (c == '$') || (c == '-') || (c == '.')
10 isKeywordChar c = isAlpha c || (c == '_')
11
12 lexer :: (Token -> P a) -> P a
13 lexer cont [] = cont TKEOF []
14 lexer cont ('\n':cs) = \line -> lexer cont cs (line+1)
15 lexer cont ('-':'>':cs) = cont TKrarrow cs
16
17 lexer cont (c:cs)
18 | isSpace c = lexer cont cs
19 | isLower c || (c == '_') = lexName cont TKname (c:cs)
20 | isUpper c = lexName cont TKcname (c:cs)
21 | isDigit c || (c == '-') = lexNum cont (c:cs)
22
23 lexer cont ('%':cs) = lexKeyword cont cs
24 lexer cont ('\'':cs) = lexChar cont cs
25 lexer cont ('\"':cs) = lexString [] cont cs
26 lexer cont ('#':cs) = cont TKhash cs
27 lexer cont ('(':cs) = cont TKoparen cs
28 lexer cont (')':cs) = cont TKcparen cs
29 lexer cont ('{':cs) = cont TKobrace cs
30 lexer cont ('}':cs) = cont TKcbrace cs
31 lexer cont ('=':cs) = cont TKeq cs
32 lexer cont (':':'=':':':cs) = cont TKcoloneqcolon cs
33 lexer cont (':':':':cs) = cont TKcoloncolon cs
34 lexer cont ('*':cs) = cont TKstar cs
35 lexer cont ('.':cs) = cont TKdot cs
36 lexer cont ('\\':cs) = cont TKlambda cs
37 lexer cont ('@':cs) = cont TKat cs
38 lexer cont ('?':cs) = cont TKquestion cs
39 lexer cont (';':cs) = cont TKsemicolon cs
40 -- 20060420 GHC spits out constructors with colon in them nowadays. jds
41 -- 20061103 but it's easier to parse if we split on the colon, and treat them
42 -- as several tokens
43 lexer cont (':':cs) = cont TKcolon cs
44 -- 20060420 Likewise does it create identifiers starting with dollar. jds
45 lexer cont ('$':cs) = lexName cont TKname ('$':cs)
46 lexer cont (c:cs) = failP "invalid character" [c]
47
48
49
50 lexChar cont ('\\':'x':h1:h0:'\'':cs)
51 | isHexEscape [h1,h0] = cont (TKchar (hexToChar h1 h0)) cs
52 lexChar cont ('\\':cs) = failP "invalid char character" ('\\':(take 10 cs))
53 lexChar cont ('\'':cs) = failP "invalid char character" ['\'']
54 lexChar cont ('\"':cs) = failP "invalid char character" ['\"']
55 lexChar cont (c:'\'':cs) = cont (TKchar c) cs
56
57
58 lexString s cont ('\\':'x':h1:h0:cs)
59 | isHexEscape [h1,h0] = lexString (s++[hexToChar h1 h0]) cont cs
60 lexString s cont ('\\':cs) = failP "invalid string character" ['\\']
61 lexString s cont ('\'':cs) = failP "invalid string character" ['\'']
62 lexString s cont ('\"':cs) = cont (TKstring s) cs
63 lexString s cont (c:cs) = lexString (s++[c]) cont cs
64
65 isHexEscape = all (\c -> isHexDigit c && (isDigit c || isLower c))
66
67 hexToChar h1 h0 = chr (digitToInt h1 * 16 + digitToInt h0)
68
69
70 lexNum cont cs =
71 case cs of
72 ('-':cs) -> f (-1) cs
73 _ -> f 1 cs
74 where f sgn cs =
75 case span isDigit cs of
76 (digits,'.':c:rest)
77 | isDigit c -> cont (TKrational (fromInteger sgn * r)) rest'
78 where ((r,rest'):_) = readFloat (digits ++ ('.':c:rest))
79 -- When reading a floating-point number, which is
80 -- a bit complicated, use the Haskell 98 library function
81 (digits,rest) -> cont (TKinteger (sgn * (read digits))) rest
82
83 lexName cont cstr cs = cont (cstr name) rest
84 where (name,rest) = span isNameChar cs
85
86 lexKeyword cont cs =
87 case span isKeywordChar cs of
88 ("module",rest) -> cont TKmodule rest
89 ("data",rest) -> cont TKdata rest
90 ("newtype",rest) -> cont TKnewtype rest
91 ("forall",rest) -> cont TKforall rest
92 ("rec",rest) -> cont TKrec rest
93 ("let",rest) -> cont TKlet rest
94 ("in",rest) -> cont TKin rest
95 ("case",rest) -> cont TKcase rest
96 ("of",rest) -> cont TKof rest
97 ("cast",rest) -> cont TKcast rest
98 ("note",rest) -> cont TKnote rest
99 ("external",rest) -> cont TKexternal rest
100 ("local",rest) -> cont TKlocal rest
101 ("_",rest) -> cont TKwild rest
102 _ -> failP "invalid keyword" ('%':cs)
103