git @ Cat's Eye Technologies Falderal / e3d8bca
Parse shell command functionality specifiers in Falderal files. catseye 13 years ago
3 changed file(s) with 75 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
4040 format _ blocks =
4141 prelude ++ (formatBlocks $ transformBlocks blocks "") ++ postlude
4242
43 formatBlocks ((commandString, test):rest) =
43 formatBlocks ((commandString, test@(Test desc input expectation)):rest) =
4444 let
45 Test desc input expectation = test
46 -- ExpectedResult expected = expectation
47 expected = show expectation
45 Output expected = expectation
4846 inputHereDoc = hereDoc "input.txt" input
4947 expectedHereDoc = hereDoc "expected.txt" expected
50 command = commandString ++ "\n"
48 command = commandString ++ " <input.txt >output.txt\n"
49 diff = "diff -u expected.txt output.txt\n"
50 formattedBlock = inputHereDoc ++ expectedHereDoc ++ command ++ diff
5151 in
52 inputHereDoc ++ expectedHereDoc ++ command ++ formatBlocks rest
52 formattedBlock ++ "\n" ++ formatBlocks rest
5353 formatBlocks (_:rest) =
5454 formatBlocks rest
5555 formatBlocks [] =
6767 -- XXX derive sentinel from text
6868
6969 hereDoc filename text =
70 "cat >" ++ filename ++ "<<EOF\n" ++ text ++ "\nEOF\n"
70 "cat >" ++ filename ++ " <<EOF\n" ++ text ++ "\nEOF\n"
7171
7272 prelude =
7373 "#!/bin/sh\n\
160160 -- Parse a pragma.
161161 --
162162
163 parseHaskellFunctionality text =
164 case stripPrefix "Haskell function " text of
165 Just specifier ->
166 let
167 (moduleName, functionName) = parseSpecifier specifier
168 in
169 Just $ HaskellDirective moduleName functionName
170 Nothing ->
171 Nothing
172
173 parseShellFunctionality text =
174 case stripPrefix "shell command " text of
175 Just specifier ->
176 Just $ ShellDirective $ parseQuotedString specifier
177 Nothing ->
178 Nothing
179
180 functionalities = [
181 parseHaskellFunctionality,
182 parseShellFunctionality
183 ]
184
163185 parsePragma text =
164186 case stripPrefix "Tests for " text of
165187 Just rest ->
166 case stripPrefix "Haskell function " rest of
167 Just specifier ->
168 let
169 (moduleName, functionName) = parseSpecifier specifier
170 in
171 HaskellDirective moduleName functionName
172 Nothing -> error "bad pragma"
188 tryFunctionalities functionalities rest
173189 Nothing ->
174 error "bad pragma"
190 error ("bad pragma: " ++ text)
191
192 tryFunctionalities [] text =
193 error ("bad functionality: " ++ text)
194 tryFunctionalities (func:rest) text =
195 case func text of
196 Just x -> x
197 Nothing -> tryFunctionalities rest text
175198
176199 parseSpecifier specifier =
177200 let
178201 (m, f) = break (\y -> y == ':') specifier
179202 in
180203 (m, stripLeading ':' f)
204
205 parseQuotedString ('"':rest) =
206 parseQuotedString' rest
207 parseQuotedString (_:rest) =
208 parseQuotedString rest
209
210 parseQuotedString' ('"':rest) =
211 ""
212 parseQuotedString' (char:rest) =
213 (char:parseQuotedString' rest)
0 Tests for wc
1 ============
2
3 This Falderal document was written by Chris Pressey. It is hereby placed in
4 the public domain.
5
6 -> Tests for shell command "wc -l"
7
8 | These are eight words
9 | that span
10 | three lines.
11 = 3
12
13 -> Tests for shell command "wc -w"
14
15 | These are eight words
16 | that span
17 | three lines.
18 = 8
19
20 | Here are eight words on a single line.
21 = 8
22
23 An intentionally failing test.
24
25 | Not four words!
26 = 4