git @ Cat's Eye Technologies Falderal / a8275e4
Add beginnings of a Haskell compiler for Falderal files. --HG-- rename : Test/Falderal/Formatter/Identity.hs => Test/Falderal/Formatter/Haskell.hs catseye 10 years ago
5 changed file(s) with 68 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
0 module Test.Falderal.Formatter.Haskell (format) where
1
2 --
3 -- Test.Falderal.Formatter.Haskell -- Haskell compiler for Falderal
4 -- Copyright (c)2011 Cat's Eye Technologies. All rights reserved.
5 --
6 -- Redistribution and use in source and binary forms, with or without
7 -- modification, are permitted provided that the following conditions
8 -- are met:
9 --
10 -- 1. Redistributions of source code must retain the above copyright
11 -- notices, this list of conditions and the following disclaimer.
12 -- 2. Redistributions in binary form must reproduce the above copyright
13 -- notices, this list of conditions, and the following disclaimer in
14 -- the documentation and/or other materials provided with the
15 -- distribution.
16 -- 3. Neither the names of the copyright holders nor the names of their
17 -- contributors may be used to endorse or promote products derived
18 -- from this software without specific prior written permission.
19 --
20 -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 -- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 -- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 -- FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 -- COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 -- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 -- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 -- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 -- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 -- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 -- POSSIBILITY OF SUCH DAMAGE.
32 --
33
34 import System
35
36 import Test.Falderal.Loader
37
38 --
39 -- Formatting function which compiles a Falderal file to Haskell source.
40 -- XXX this is woefully incomplete right now.
41 --
42
43 format ((TestInput text):(ExpectedResult result):rest) =
44 "test " ++ (show text) ++ " " ++ (show result) ++ "\n" ++ (format rest)
45 format ((TestInput text):(ExpectedError result):rest) =
46 "testErr " ++ (show text) ++ " " ++ (show result) ++ "\n" ++ (format rest)
47 format ((TestInput text):foo:rest) =
48 "-- malformed Falderal: no expectation for " ++ (show text) ++ (format (foo:rest))
49 format (_:rest) =
50 format rest
51 format [] =
52 ""
0 module Test.Falderal.Formatter.Identity where
0 module Test.Falderal.Formatter.Identity (format) where
11
22 --
33 -- Test.Falderal.Formatter.Identity -- Identity formatter for Falderal format
5252 (prefixEachLine "> " text)
5353 formatLine (SectionHeading text) =
5454 text ++ "\n" ++ (take (length text) (repeat '-')) ++ "\n"
55
56 format = formatLines (formatLine)
0 module Test.Falderal.Formatter.Markdown where
0 module Test.Falderal.Formatter.Markdown (format) where
11
22 --
33 -- Test.Falderal.Formatter.Identity -- Markdown formatter for Falderal format
5353 (prefixEachLine " " text)
5454 formatLine (SectionHeading text) =
5555 text ++ "\n" ++ (take (length text) (repeat '-')) ++ "\n"
56
57 format = formatLines (formatLine)
0 module Test.Falderal.Formatter where
1
2 -- TODO: export a more restricted interface
0 module Test.Falderal.Formatter (formatFile) where
31
42 --
53 -- Test.Falderal.Formatter -- The Falderal Test Suite Formatter
3836 import Test.Falderal.Loader
3937 import qualified Test.Falderal.Formatter.Identity as Identity
4038 import qualified Test.Falderal.Formatter.Markdown as Markdown
39 import qualified Test.Falderal.Formatter.Haskell as Haskell
4140
4241 --
4342 -- Driver for Falderal file formatting.
4746 -- Map from names of formats to formatter functions.
4847 --
4948
50 getFormatter "identity" = Identity.formatLine
51 getFormatter "markdown" = Markdown.formatLine
52 getFormatter "dump" = \x -> (show x) ++ "\n"
49 getFormatter "identity" = Identity.format
50 getFormatter "markdown" = Markdown.format
51 getFormatter "haskell" = Haskell.format
52 getFormatter "dump" = formatLines (\x -> (show x) ++ "\n")
5353
5454 --
5555 -- We format by (coalesced) lines, instead of by blocks, because by the time
5959 formatFile format fileName = do
6060 testText <- readFile fileName
6161 lines <- return $ transformLines $ lines testText
62 outputText <- return $ formatLines (getFormatter format) lines
62 outputText <- return $ (getFormatter format) lines
6363 putStr outputText
64
65 formatLines formatter lines = foldl (++) "" (map (formatter) lines)
214214
215215 prefixEachLine prefix text =
216216 foldl (++) "" (map (\x -> prefix ++ x ++ "\n") (allLines text))
217
218 formatLines formatter lines = foldl (++) "" (map (formatter) lines)