Debian Codegraph @

debian/rust-vendor/winnow/examples/c_expression/test.rs

raw
1 #[allow(clippy::useless_attribute)]
2 #[allow(unused_imports)] // its dead for benches
3 use winnow::error::ParseError;
4 use winnow::Parser;
5
6 use crate::parser::pratt_parser;
7 use crate::parser::Expr;
8
9 #[allow(dead_code)]
10 // to invoke fmt_delimited()
11 struct PrefixNotation(Expr);
12
13 impl core::fmt::Display for PrefixNotation {
14 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
15 self.0.fmt_delimited(f)
16 }
17 }
18
19 #[allow(dead_code)]
20 fn parse(i: &str) -> Result<String, ParseError<&str, winnow::error::ContextError>> {
21 pratt_parser
22 .parse(i)
23 .map(|r| format!("{}", PrefixNotation(r)))
24 }
25
26 #[allow(dead_code)]
27 fn parse_ok(i: &str, expect: &str) {
28 assert_eq!(parse(i).unwrap(), expect);
29 }
30
31 #[test]
32 fn op() {
33 parse_ok(" 1 ", "1");
34 }
35
36 #[test]
37 fn neither() {
38 assert!(parse("1 == 2 == 3").is_err());
39 assert!(parse("1 -le 2 -gt 3").is_err());
40 assert!(parse("1 < 2 < 3").is_err());
41 assert!(parse("1 != 2 == 3").is_err());
42 }
43
44 #[test]
45 fn equal() {
46 parse_ok("x=3", "(= x 3)");
47 parse_ok("x = 2*3", "(= x (* 2 3))");
48 parse_ok("x = y", "(= x y)");
49 parse_ok("a = b = 10", "(= a (= b 10))");
50 parse_ok("x = ((y*4)-2)", "(= x (- (* y 4) 2))");
51 }
52
53 #[test]
54 fn unary() {
55 parse_ok("- - a", "(-(-a))");
56 parse_ok("+ - a", "(-a)");
57 parse_ok("++ -- a", "(pre++(pre--a))");
58 parse_ok("a ++ --", "(post--(post++a))");
59 parse_ok("!x", "(!x)");
60 parse_ok("x--", "(post--x)");
61 parse_ok("x[1]--", "(post--([] x 1))");
62 parse_ok("--x", "(pre--x)");
63 parse_ok("++x[1]", "(pre++([] x 1))");
64 parse_ok("!x--", "(!(post--x))");
65 parse_ok("~x++", "(~(post++x))");
66 parse_ok("x++ - y++", "(- (post++x) (post++y))");
67 parse_ok("++x - ++y", "(- (pre++x) (pre++y))");
68 parse_ok("--1 * 2", "(* (pre--1) 2)");
69 parse_ok("--f . g", "(pre--(. f g))");
70 }
71
72 #[test]
73 fn same_precedence() {
74 // left associative
75 parse_ok("1 + 2 + 3", "(+ (+ 1 2) 3)");
76 parse_ok("1 - 2 - 3", "(- (- 1 2) 3)");
77 parse_ok("1 * 2 * 3", "(* (* 1 2) 3)");
78 parse_ok("1 / 2 / 3", "(/ (/ 1 2) 3)");
79 parse_ok("1 % 2 % 3", "(% (% 1 2) 3)");
80 parse_ok("1 ^ 2 ^ 3", "(^ (^ 1 2) 3)");
81 parse_ok("+-+1", "(-1)");
82 parse_ok("f . g . h", "(. (. f g) h)");
83 parse_ok("++--++1", "(pre++(pre--(pre++1)))");
84 // right associative
85 parse_ok("2 ** 3 ** 2", "(** 2 (** 3 2))");
86 }
87
88 #[test]
89 fn different_precedence() {
90 parse_ok("1 + 2 * 3", "(+ 1 (* 2 3))");
91 parse_ok("1 + 2 * 3 - 4 / 5", "(- (+ 1 (* 2 3)) (/ 4 5))");
92 parse_ok("a + b * c * d + e", "(+ (+ a (* (* b c) d)) e)");
93 parse_ok("1 + ++2 * 3 * 5 + 6", "(+ (+ 1 (* (* (pre++2) 3) 5)) 6)");
94 parse_ok("**3 + &1", "(+ (*(*3)) (&1))");
95 parse_ok("x*y - y*z", "(- (* x y) (* y z))");
96 parse_ok("x/y - y%z", "(- (/ x y) (% y z))");
97 parse_ok("1<2 * 3", "(< 1 (* 2 3))");
98 parse_ok(
99 " 1 + 2 + f . g . h * 3 * 4",
100 "(+ (+ 1 2) (* (* (. (. f g) h) 3) 4))",
101 );
102 }
103
104 #[test]
105 fn prefix_postfix_power() {
106 // https://en.cppreference.com/w/c/language/operator_precedence
107 // `post++` has `1`, `pre--` and `*` have 2
108 parse_ok("--**3++", "(pre--(*(*(post++3))))");
109 parse_ok("**--3++", "(*(*(pre--(post++3))))");
110 parse_ok("&foo()[0]", "(&([] (call foo) 0))");
111 parse_ok("-9!", "(-(!9))");
112 parse_ok("f . g !", "(!(. f g))");
113 }
114
115 #[test]
116 fn prefix_infix() {
117 parse_ok("x - -y", "(- x (-y))");
118 parse_ok("-1 * -2", "(* (-1) (-2))");
119 parse_ok("-x * -y", "(* (-x) (-y))");
120 parse_ok("x - -234", "(- x (-234))");
121 }
122
123 #[test]
124 fn ternary() {
125 parse_ok("a ? 2 + c : -2 * 2", "(? a (+ 2 c) (* (-2) 2))");
126 parse_ok("a ? b : c ? d : e", "(? a b (? c d e))");
127 parse_ok("2! > 1 ? 3 : 1", "(? (> (!2) 1) 3 1)");
128 parse_ok(
129 "2 > 1 ? 1 -ne 3 ? 4 : 5 : 1",
130 "(? (> 2 1) (? (!= 1 3) 4 5) 1)",
131 );
132 parse_ok("a > b ? 0 : 1", "(? (> a b) 0 1)");
133 parse_ok("a > b ? x+1 : y+1", "(? (> a b) (+ x 1) (+ y 1))");
134 parse_ok(
135 "1 ? true1 : 2 ? true2 : false",
136 "(? 1 true1 (? 2 true2 false))",
137 );
138 parse_ok(
139 "1 ? true1 : (2 ? true2 : false)",
140 "(? 1 true1 (? 2 true2 false))",
141 );
142
143 parse_ok(
144 "1 ? (2 ? true : false1) : false2",
145 "(? 1 (? 2 true false1) false2)",
146 );
147 parse_ok(
148 "1 ? 2 ? true : false1 : false2",
149 "(? 1 (? 2 true false1) false2)",
150 );
151 }
152
153 #[test]
154 fn comma() {
155 parse_ok("x=1,y=2,z=3", "(, (, (= x 1) (= y 2)) (= z 3))");
156 parse_ok("a, b, c", "(, (, a b) c)");
157 parse_ok("(a, b, c)", "(, (, a b) c)");
158 parse_ok("f(a, b, c), d", "(, (call f (, (, a b) c)) d)");
159 parse_ok("(a, b, c), d", "(, (, (, a b) c) d)");
160 }
161
162 #[test]
163 fn comma_ternary() {
164 parse_ok("x ? 1 : 2, y ? 3 : 4", "(, (? x 1 2) (? y 3 4))");
165 // Comma expressions can be inside
166 parse_ok("a , b ? c, d : e, f", "(, (, a (? b (, c d) e)) f)");
167 parse_ok("a = 0 ? b : c = d", "(= a (= (? 0 b c) d))");
168 }
169
170 #[test]
171 fn braces() {
172 parse_ok("4*(2+3)", "(* 4 (+ 2 3))");
173 parse_ok("(2+3)*4", "(* (+ 2 3) 4)");
174 parse_ok("(((0)))", "0");
175 }
176
177 #[test]
178 fn logical() {
179 parse_ok("a && b || c && d", "(|| (&& a b) (&& c d))");
180 parse_ok("!a && !b", "(&& (!a) (!b))");
181 parse_ok("a != b && c == d", "(&& (!= a b) (== c d))");
182 }
183
184 #[test]
185 fn array() {
186 parse_ok("x[1,2]", "([] x (, 1 2))");
187 parse_ok("x[1]", "([] x 1)");
188 parse_ok("x[a+b]", "([] x (+ a b))");
189 parse_ok("c = pal[i*8]", "(= c ([] pal (* i 8)))");
190 parse_ok("f[x] = 1", "(= ([] f x) 1)");
191 parse_ok("x[0][1]", "([] ([] x 0) 1)");
192 }
193
194 #[test]
195 fn function_call() {
196 parse_ok("a()", "(call a)");
197 parse_ok("a(+1)", "(call a 1)");
198 parse_ok("a()+1", "(+ (call a) 1)");
199 parse_ok("f(a, b, c)", "(call f (, (, a b) c))");
200 parse_ok("print(x)", "(call print x)");
201 parse_ok(
202 "x = y(2)*3 + y(4)*5",
203 "(= x (+ (* (call y 2) 3) (* (call y 4) 5)))",
204 );
205 parse_ok("x(1,2)+y(3,4)", "(+ (call x (, 1 2)) (call y (, 3 4)))");
206 parse_ok("x(a,b,c[d])", "(call x (, (, a b) ([] c d)))");
207 parse_ok(
208 "x(1,2)*j+y(3,4)*k+z(5,6)*l",
209 "(+ (+ (* (call x (, 1 2)) j) (* (call y (, 3 4)) k)) (* (call z (, 5 6)) l))",
210 );
211 parse_ok("print(test(2,3))", "(call print (call test (, 2 3)))");
212 parse_ok("min(255,n*2)", "(call min (, 255 (* n 2)))");
213 }
214
215 #[test]
216 fn member_access() {
217 parse_ok("a.b", "(. a b)");
218 parse_ok("a.b.c", "(. (. a b) c)");
219 parse_ok("a->b", "(-> a b)");
220 parse_ok("++a->b", "(pre++(-> a b))");
221 parse_ok("a++ ->b", "(-> (post++a) b)");
222 parse_ok("a.(x)", "(. a x)");
223 parse_ok("a.(x+3)", "(. a (+ x 3))");
224 }
225
226 #[test]
227 fn errors() {
228 assert!(parse("x + a b").is_err());
229 assert!(parse("x[a b]").is_err());
230 assert!(parse("x[a)]").is_err());
231 assert!(parse("x(a])").is_err());
232 assert!(parse("[a + b]").is_err());
233 assert!(parse("[a b]").is_err());
234 assert!(parse("+").is_err());
235 assert!(parse("a +").is_err());
236 assert!(parse("<=").is_err());
237 assert!(parse("<= - a + b").is_err());
238 assert!(parse("a b").is_err());
239 assert!(parse("a + b @").is_err());
240 assert!(parse("a + b )").is_err());
241 assert!(parse("( a + b").is_err());
242 assert!(parse("( a + b) c").is_err());
243 assert!(parse("f ( a + b ) c").is_err());
244 assert!(parse("@ a + b").is_err());
245 assert!(parse("a @ b").is_err());
246 assert!(parse("(a @ b)").is_err());
247 assert!(parse(")").is_err());
248 }