Alan Zimmerman pushed to branch wip/az/ghc-cpp at Glasgow Haskell Compiler / GHC
Commits:
-
4acbeff0
by Alan Zimmerman at 2025-05-06T22:42:27+01:00
9 changed files:
- compiler/GHC/Parser/PreProcess/Lexer.x
- compiler/GHC/Parser/PreProcess/ParsePP.hs
- compiler/GHC/Parser/PreProcess/ParserM.hs
- testsuite/tests/ghc-cpp/GhcCpp01.hs
- testsuite/tests/ghc-cpp/GhcCpp01.stderr
- utils/check-cpp/Lexer.x
- utils/check-cpp/Main.hs
- utils/check-cpp/ParsePP.hs
- utils/check-cpp/ParserM.hs
Changes:
... | ... | @@ -22,6 +22,8 @@ words :- |
22 | 22 | <0> $white+ ;
|
23 | 23 | ---------------------------------------
|
24 | 24 | |
25 | + <0> "//" .* { mkTv TComment }
|
|
26 | + <0> "/*" .* "*/" { mkTv TComment }
|
|
25 | 27 | <0> "{" { mkTv TOpenBrace }
|
26 | 28 | <0> "}" { mkTv TCloseBrace }
|
27 | 29 | <0> "[" { mkTv TOpenBracket }
|
... | ... | @@ -27,7 +27,7 @@ parseDirective s = |
27 | 27 | case cppLex True s of
|
28 | 28 | Left e -> Left e
|
29 | 29 | Right toks ->
|
30 | - case toks of
|
|
30 | + case map deComment toks of
|
|
31 | 31 | (THash "#" : TIdentifier "define" : ts) -> cppDefine ts
|
32 | 32 | (THash "#" : TIdentifier "undef" : ts) -> Right $ cppUndef (map t_str ts)
|
33 | 33 | (THash "#" : TIdentifier "include" : ts) -> Right $ cppInclude (map t_str ts)
|
... | ... | @@ -112,6 +112,13 @@ cppLex sd s = case lexCppTokenStream s (init_state {scanning_directive = sd}) of |
112 | 112 | Left err -> Left err
|
113 | 113 | Right (_inp, _st, toks) -> Right toks
|
114 | 114 | |
115 | +-- Each comment is replaced with a space
|
|
116 | +-- https://timsong-cpp.github.io/cppwp/n4140/lex#phases-1.3
|
|
117 | +deComment :: Token -> Token
|
|
118 | +deComment (TComment _) = TComment " "
|
|
119 | +deComment t = t
|
|
120 | + |
|
121 | + |
|
115 | 122 | -- ---------------------------------------------------------------------
|
116 | 123 | |
117 | 124 | doATest :: String -> Either String CppDirective
|
... | ... | @@ -91,6 +91,8 @@ init_state = |
91 | 91 | |
92 | 92 | data Token
|
93 | 93 | = TEOF {t_str :: String}
|
94 | + | -- https://timsong-cpp.github.io/cppwp/n4140/lex.comment
|
|
95 | + TComment {t_str :: String}
|
|
94 | 96 | | TIdentifier {t_str :: String}
|
95 | 97 | | TIdentifierLParen {t_str :: String}
|
96 | 98 | | TInteger {t_str :: String}
|
... | ... | @@ -18,13 +18,13 @@ y = 1 |
18 | 18 | #endif
|
19 | 19 | |
20 | 20 | #undef FOO
|
21 | -#ifdef FOO
|
|
21 | +#ifdef FOO /* Check for FOO */
|
|
22 | 22 | complete junk!
|
23 | 23 | #endif
|
24 | 24 | |
25 | 25 | -- nested undef
|
26 | 26 | #define AA
|
27 | -#if 0
|
|
27 | +#if /* hard code for now */ 0
|
|
28 | 28 | #undef AA
|
29 | 29 | #endif
|
30 | 30 |
... | ... | @@ -217,13 +217,13 @@ |
217 | 217 | - |#endif
|
218 | 218 | |
219 | 219 | - |#undef FOO
|
220 | -- |#ifdef FOO
|
|
220 | +- |#ifdef FOO /* Check for FOO */
|
|
221 | 221 | - |complete junk!
|
222 | 222 | - |#endif
|
223 | 223 | |
224 | 224 | - |-- nested undef
|
225 | 225 | - |#define AA
|
226 | -- |#if 0
|
|
226 | +- |#if /* hard code for now */ 0
|
|
227 | 227 | - |#undef AA
|
228 | 228 | - |#endif
|
229 | 229 |
... | ... | @@ -21,6 +21,8 @@ words :- |
21 | 21 | <0> $white+ ;
|
22 | 22 | ---------------------------------------
|
23 | 23 | |
24 | + <0> "//" .* { mkTv TComment }
|
|
25 | + <0> "/*" .* "*/" { mkTv TComment }
|
|
24 | 26 | <0> "{" { mkTv TOpenBrace }
|
25 | 27 | <0> "}" { mkTv TCloseBrace }
|
26 | 28 | <0> "[" { mkTv TOpenBracket }
|
... | ... | @@ -838,3 +838,18 @@ t36 = do |
838 | 838 | , "#endif"
|
839 | 839 | , ""
|
840 | 840 | ]
|
841 | + |
|
842 | +t37 :: IO ()
|
|
843 | +t37 = do
|
|
844 | + dump
|
|
845 | + [ "{-# LANGUAGE GHC_CPP #-}"
|
|
846 | + , "module Example14 where"
|
|
847 | + , ""
|
|
848 | + , "foo ="
|
|
849 | + , "#if 1 /* and a comment */"
|
|
850 | + , " 'a'"
|
|
851 | + , "#else"
|
|
852 | + , " 'b'"
|
|
853 | + , "#endif"
|
|
854 | + , ""
|
|
855 | + ] |
... | ... | @@ -27,7 +27,7 @@ parseDirective s = |
27 | 27 | case cppLex True s of
|
28 | 28 | Left e -> Left e
|
29 | 29 | Right toks ->
|
30 | - case toks of
|
|
30 | + case map deComment toks of
|
|
31 | 31 | (THash "#" : TIdentifier "define" : ts) -> cppDefine ts
|
32 | 32 | (THash "#" : TIdentifier "undef" : ts) -> Right $ cppUndef (map t_str ts)
|
33 | 33 | (THash "#" : TIdentifier "include" : ts) -> Right $ cppInclude (map t_str ts)
|
... | ... | @@ -112,6 +112,13 @@ cppLex sd s = case lexCppTokenStream s (init_state {scanning_directive = sd}) of |
112 | 112 | Left err -> Left err
|
113 | 113 | Right (_inp, _st, toks) -> Right toks
|
114 | 114 | |
115 | +-- Each comment is replaced with a space
|
|
116 | +-- https://timsong-cpp.github.io/cppwp/n4140/lex#phases-1.3
|
|
117 | +deComment :: Token -> Token
|
|
118 | +deComment (TComment _) = TComment " "
|
|
119 | +deComment t = t
|
|
120 | + |
|
121 | + |
|
115 | 122 | -- ---------------------------------------------------------------------
|
116 | 123 | |
117 | 124 | doATest :: String -> Either String CppDirective
|
... | ... | @@ -91,6 +91,8 @@ init_state = |
91 | 91 | |
92 | 92 | data Token
|
93 | 93 | = TEOF {t_str :: String}
|
94 | + | -- https://timsong-cpp.github.io/cppwp/n4140/lex.comment
|
|
95 | + TComment {t_str :: String}
|
|
94 | 96 | | TIdentifier {t_str :: String}
|
95 | 97 | | TIdentifierLParen {t_str :: String}
|
96 | 98 | | TInteger {t_str :: String}
|