Kiln » babybearparser
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

tip An example of making the parser more robust with alternation.

Changeset bf5f58b35044

Parent c381bb2b7e39

by Profile picture of User 138Hao Lian <hao@fogcreek.com>

Changes to 2 files · Browse files at bf5f58b35044 Showing diff from parent c381bb2b7e39 Diff from another changeset...

 
45
46
47
48
 
49
50
51
 
45
46
47
 
48
49
50
51
@@ -45,7 +45,7 @@
 let filterName = ["author"; "file"; "project"; "repo"] |> Seq.map pstring |> choice  let regularFilter =   (filterName |>> findFilter) - .>> (pchar ':') + .>>? (pchar ':')   .>>. (phrase <|> word <?> "filter argument")   |>> pipeline  let dateWord = many1Chars <| noneOf ". "
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
 
 module babybearparser.Testing    open FParsec  open Microsoft.VisualStudio.TestTools.UnitTesting  open System    open babybearparser.Parser    let equals actual expected = Assert.IsTrue ((actual = expected), sprintf "%+A <> %+A" actual expected)    let throwsParseError (f: unit -> unit) contains =   try   f(); Assert.Fail ("throws: no exception thrown of type ParseError")   with   | :? ParseError as ex -> Assert.IsTrue (ex.Message.Contains(contains), sprintf "%O does not contain %s" ex contains)    [<TestClass>]  type public Tests () =   (* Tests that testing works *)   [<TestMethod>]   member this.TestsWorking() =   equals 1 1     [<TestMethod>]   member this.TestsKeywords() =   equals (parse "foo bar") [KeywordAtom <| Word "foo"; KeywordAtom <| Word "bar"]     [<TestMethod>]   member this.TestsEmpty() =   equals (List.length <| parse "") 0     [<TestMethod>]   member this.TestsPhrase() =   equals (parse "\"foo \\\" bar\"") [KeywordAtom <| Phrase "foo \" bar"]     [<TestMethod>]   member this.TestsPhraseAndKeyword() =   equals (parse "\"foo bar\" boo") [KeywordAtom <| Phrase "foo bar"; KeywordAtom <| Word "boo"]     [<TestMethod>]   member this.TestsFilter() =   equals (parse "\"foo bar\" project:boo") [KeywordAtom <| Phrase "foo bar"; FilterAtom <| Project "boo"]     [<TestMethod>]   member this.TestsFilters() =   equals (parse "\"foo bar\" project:boo repo:radley") [KeywordAtom <| Phrase "foo bar"; FilterAtom <| Project "boo"; FilterAtom <| Repo "radley"]     [<TestMethod>]   member this.TestsFilterPhrase() =   equals (parse "\"foo bar\" project:\"boo radley\"") [KeywordAtom <| Phrase "foo bar"; FilterAtom <| Project "boo radley"]     [<TestMethod>]   member this.TestsEmptyFilter() =   equals (parse "\"foo bar\" project:\"\"") [KeywordAtom <| Phrase "foo bar"; FilterAtom <| Project ""]     [<TestMethod>]   member this.TestsConfusingFilter() =   equals (parse ":taft:") [KeywordAtom <| Word ":taft:"]     [<TestMethod>]   member this.TestsMissingFilter() =   let f () = parse "\"foo bar\" project:" |> ignore   throwsParseError f "filter argument"     [<TestMethod>]   member this.TestsUnknownFilter() =   equals (parse "unknown:careless") [KeywordAtom <| Word "unknown:careless"]     [<TestMethod>]   member this.TestsDateRange() =   equals (parse "date:yesterday..today") [(DateTime.UtcNow.Date.AddDays(-1.), DateTime.UtcNow.Date) |> Date |> FilterAtom]     [<TestMethod>]   member this.TestsDateRange2() =   equals (parse "date:yesterday..2008-05-01") [(DateTime.UtcNow.Date.AddDays(-1.), DateTime.Parse("2008-05-01")) |> Date |> FilterAtom]     [<TestMethod>]   member this.TestsDateRangeMissingEnd() =   equals (parse "date:yesterday.. eggs") [(DateTime.UtcNow.Date.AddDays(-1.), DateTime.UtcNow.Date) |> Date |> FilterAtom; KeywordAtom <| Word "eggs"]     [<TestMethod>]   member this.TestsDateRangeMissingStart() =   equals (parse "date:..today eggs") [(DateTime.MinValue, DateTime.UtcNow.Date) |> Date |> FilterAtom; KeywordAtom <| Word "eggs"]     [<TestMethod>]   member this.TestsDateRangeMissingEverything() =   equals (parse "date:.. eggs") [(DateTime.MinValue, DateTime.UtcNow.Date) |> Date |> FilterAtom; KeywordAtom <| Word "eggs"]     [<TestMethod>]   member this.TestsInvalidDate() =   let f () = parse "date:\"many moons ago\"..today eggs" |> ignore   throwsParseError f "Unrecognized date" + + [<TestMethod>] + member this.TestsFilterAlternation() = + equals (parse "projects") [KeywordAtom <| Word "projects"] + + [<TestMethod>] + member this.TestsFilterAlternation2() = + equals (parse "proj") [KeywordAtom <| Word "proj"] + + [<TestMethod>] + member this.TestsEmptyFilter2() = + let f () = parse "project:" |> ignore + throwsParseError f "filter argument" \ No newline at end of file