1 ! Copyright (C) 2005 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel lists.lazy tools.test strings math
4 sequences parser-combinators arrays math.parser unicode.categories ;
5 IN: parser-combinators.tests
8 { { T{ parse-result f { "a" "b" } T{ slice f 2 4 "abcd" } } } } [
9 "abcd" "a" token "b" token <&> parse list>array
12 { { T{ parse-result f { { "a" "b" } "c" } T{ slice f 3 4 "abcd" } } } } [
13 "abcd" "a" token "b" token <&> "c" token <&> parse list>array
16 { { T{ parse-result f { "a" { "b" "c" } } T{ slice f 3 4 "abcd" } } } } [
17 "abcd" "a" token "b" token "c" token <&> <&> parse list>array
21 "decd" "a" token "b" token <&> parse list>array
25 "dbcd" "a" token "b" token <&> parse list>array
29 "adcd" "a" token "b" token <&> parse list>array
33 { { T{ parse-result f "a" T{ slice f 1 4 "abcd" } } } } [
34 "abcd" "a" token "b" token <|> parse list>array
37 { { T{ parse-result f "b" T{ slice f 1 4 "bbcd" } } } } [
38 "bbcd" "a" token "b" token <|> parse list>array
42 "cbcd" "a" token "b" token <|> parse list>array
47 " abcd" "a" token parse list>array
50 { { T{ parse-result f "a" T{ slice f 3 6 " abcd" } } } } [
51 " abcd" "a" token sp parse list>array
55 { { T{ parse-result f "abcd" T{ slice f 4 4 "abcd" } } T{ parse-result f "abc" T{ slice f 3 4 "abcd" } } } } [
56 "abcd" "abcd" token "abc" token <|> parse list>array
59 { { T{ parse-result f "abcd" T{ slice f 4 4 "abcd" } } } } [
60 "abcd" "abcd" token "abc" token <|> just parse list>array
64 { { T{ parse-result f 48 T{ slice f 1 5 "01234" } } } } [
65 "01234" [ digit? ] satisfy parse list>array
68 { { T{ parse-result f 0 T{ slice f 1 5 "01234" } } } } [
69 "01234" [ digit? ] satisfy [ digit> ] <@ parse list>array
73 { { T{ parse-result f "begin" T{ slice f 5 6 "begin1" } } } } [
74 "begin1" "begin" token parse list>array
78 "begin1" "begin" token some parse
82 "begin" "begin" token some parse
85 ! <& parser and &> parser
86 { { T{ parse-result f { "a" "b" } T{ slice f 2 4 "abcd" } } } } [
87 "abcd" "a" token "b" token <&> parse list>array
90 { { T{ parse-result f "a" T{ slice f 2 4 "abcd" } } } } [
91 "abcd" "a" token "b" token <& parse list>array
94 { { T{ parse-result f "b" T{ slice f 2 4 "abcd" } } } } [
95 "abcd" "a" token "b" token &> parse list>array
98 ! Testing <*> and <:&>
99 { { T{ parse-result f { "1" } T{ slice f 1 4 "1234" } } T{ parse-result f { } "1234" } } } [
100 "1234" "1" token <*> parse list>array
105 T{ parse-result f { "1" "1" "1" "1" } T{ slice f 4 7 "1111234" } }
106 T{ parse-result f { "1" "1" "1" } T{ slice f 3 7 "1111234" } }
107 T{ parse-result f { "1" "1" } T{ slice f 2 7 "1111234" } }
108 T{ parse-result f { "1" } T{ slice f 1 7 "1111234" } }
109 T{ parse-result f { } "1111234" }
113 "1111234" "1" token <*> parse list>array
118 T{ parse-result f { "1111" } T{ slice f 4 7 "1111234" } }
119 T{ parse-result f { "111" } T{ slice f 3 7 "1111234" } }
120 T{ parse-result f { "11" } T{ slice f 2 7 "1111234" } }
121 T{ parse-result f { "1" } T{ slice f 1 7 "1111234" } }
122 T{ parse-result f { { } } "1111234" }
125 "1111234" "1" token <*> [ concat 1array ] <@ parse list>array
128 { { T{ parse-result f { } "234" } } } [
129 "234" "1" token <*> parse list>array
133 { { T{ parse-result f { "1" } T{ slice f 1 4 "1234" } } } } [
134 "1234" "1" token <+> parse list>array
139 T{ parse-result f { "1" "1" "1" "1" } T{ slice f 4 7 "1111234" } }
140 T{ parse-result f { "1" "1" "1" } T{ slice f 3 7 "1111234" } }
141 T{ parse-result f { "1" "1" } T{ slice f 2 7 "1111234" } }
142 T{ parse-result f { "1" } T{ slice f 1 7 "1111234" } }
145 "1111234" "1" token <+> parse list>array
149 "234" "1" token <+> parse list>array