From: <cod...@go...> - 2009-08-23 08:57:54
|
Revision: 411 Author: wol...@gm... Date: Sun Aug 23 01:56:37 2009 Log: improve handling of backslashes and comments in the preprocessor multi-line preprocessor directives no longer mess up line numbers. multi-line comments in multi-line preprocessor directives no longer cause trouble. http://code.google.com/p/hoc/source/detail?r=411 Modified: /trunk/hoc/InterfaceGenerator2/Preprocessor.hs /trunk/hoc/Tests/TestPreprocessor.hs ======================================= --- /trunk/hoc/InterfaceGenerator2/Preprocessor.hs Sat Nov 1 04:27:26 2008 +++ /trunk/hoc/InterfaceGenerator2/Preprocessor.hs Sun Aug 23 01:56:37 2009 @@ -144,6 +144,7 @@ unblockComments ('/' : '*' : xs) = "/*" ++ handleComment xs where handleComment ('*' : '/' : xs) = "*/" ++ unblockComments xs + handleComment ('\\': '\n' : xs) = "*/\\\n/*" ++ handleComment xs handleComment ('\n' : xs) = "*/\n/*" ++ handleComment xs handleComment (c : xs) = c : handleComment xs handleComment [] = [] @@ -153,14 +154,26 @@ parseDirectives = map (\l -> case parse line "" l of Left e -> Text $ l ++ "// " ++ show (show e) Right x -> x) . handleBackslashes . lines . unblockComments - -handleBackslashes [] = [] -handleBackslashes (l : ls) - | null l = [] : handleBackslashes ls - | last l == '\\' = case handleBackslashes ls of - (l2 : ls') -> (l ++ '\n' : l2) : ls' - ls' -> ls' - | otherwise = l : handleBackslashes ls + +handleBackslashes = f . map reverse where + f :: [String] -> [String] + f [] = [] + f ls@(firstLine:otherLines) + | null backslashed = reverse firstLine : f otherLines + | otherwise = [reverse (concat (line : reverse backslashed))] + ++ replicate (length backslashed) "" + ++ f rest + where + backslashed :: [String] + backslashed = map (drop 1) $ takeWhile bs ls + unbackslashed = dropWhile bs ls + line :: String + rest:: [String] + (line,rest) = case unbackslashed of + (l:r) -> (l,r) + [] -> ("",[]) + bs ('\\':_) = True + bs _ = False preprocess :: String -> String -> String preprocess fn f = execute fn $ parseDirectives f ======================================= --- /trunk/hoc/Tests/TestPreprocessor.hs Sat Nov 1 04:27:26 2008 +++ /trunk/hoc/Tests/TestPreprocessor.hs Sun Aug 23 01:56:37 2009 @@ -134,8 +134,24 @@ \#endif\n\ \#endif", - "defineBackslash" ~: success + "defineBackslash" ~: "#define FOO bar\\\n\ \ baz\n\ - \success" + \A" ==> + "//#define FOO bar\ + \ baz\n\ + \\n\ + \A", + + "defineArgBackslash" ~: + "#define FOO(x,y) bar\\\n\ + \ baz\n\ + \A" ==> + "//#define FOO(x,y) bar\ + \ baz\n\ + \\n\ + \A", + + "commentBackslash" ~: + "/* abc\\\ndef */\nghi\n" ==> "/* abc*//*def */\n\nghi\n" ] |