tagua_parser/rules/
comments.rs1named!(
39 pub comment,
40 alt!(
41 comment_single_line
42 | comment_delimited
43 )
44);
45
46named!(
47 comment_single_line,
48 preceded!(
49 alt!(tag!("//") | tag!("#")),
50 re_bytes_find_static!(r"^.*?(\r\n|\r|\n|$)")
51 )
52);
53
54named!(
55 comment_delimited,
56 preceded!(
57 tag!("/*"),
58 take_until_and_consume!("*/")
59 )
60);
61
62
63#[cfg(test)]
64mod tests {
65 use super::{
66 comment,
67 comment_delimited,
68 comment_single_line
69 };
70 use super::super::super::internal::{
71 Error,
72 ErrorKind,
73 Result
74 };
75
76 #[test]
77 fn case_comment_single_line_double_slash_empty() {
78 let input = b"//";
79 let output = Result::Done(&b""[..], &b""[..]);
80
81 assert_eq!(comment_single_line(input), output);
82 assert_eq!(comment(input), output);
83 }
84
85 #[test]
86 fn case_comment_single_line_double_slash_with_feed() {
87 let input = b"// foobar\nbazqux";
88 let output = Result::Done(&b"bazqux"[..], &b" foobar\n"[..]);
89
90 assert_eq!(comment_single_line(input), output);
91 assert_eq!(comment(input), output);
92 }
93
94 #[test]
95 fn case_comment_single_line_double_slash_with_carriage_return() {
96 let input = b"// foobar\rbazqux";
97 let output = Result::Done(&b"bazqux"[..], &b" foobar\r"[..]);
98
99 assert_eq!(comment_single_line(input), output);
100 assert_eq!(comment(input), output);
101 }
102
103 #[test]
104 fn case_comment_single_line_double_slash_with_carriage_return_feed() {
105 let input = b"// foobar\r\nbazqux";
106 let output = Result::Done(&b"bazqux"[..], &b" foobar\r\n"[..]);
107
108 assert_eq!(comment_single_line(input), output);
109 assert_eq!(comment(input), output);
110 }
111
112 #[test]
113 fn case_comment_single_line_double_slash_without_ending() {
114 let input = b"// foobar";
115 let output = Result::Done(&b""[..], &b" foobar"[..]);
116
117 assert_eq!(comment_single_line(input), output);
118 assert_eq!(comment(input), output);
119 }
120
121 #[test]
122 fn case_comment_single_line_double_slash_embedded() {
123 let input = b"//foo//bar";
124 let output = Result::Done(&b""[..], &b"foo//bar"[..]);
125
126 assert_eq!(comment_single_line(input), output);
127 assert_eq!(comment(input), output);
128 }
129
130 #[test]
131 fn case_comment_single_line_hash_empty() {
132 let input = b"#";
133 let output = Result::Done(&b""[..], &b""[..]);
134
135 assert_eq!(comment_single_line(input), output);
136 assert_eq!(comment(input), output);
137 }
138
139 #[test]
140 fn case_comment_single_line_hash_with_line_feed() {
141 let input = b"# foobar\nbazqux";
142 let output = Result::Done(&b"bazqux"[..], &b" foobar\n"[..]);
143
144 assert_eq!(comment_single_line(input), output);
145 assert_eq!(comment(input), output);
146 }
147
148 #[test]
149 fn case_comment_single_line_hash_with_carriage_return() {
150 let input = b"# foobar\rbazqux";
151 let output = Result::Done(&b"bazqux"[..], &b" foobar\r"[..]);
152
153 assert_eq!(comment_single_line(input), output);
154 assert_eq!(comment(input), output);
155 }
156
157 #[test]
158 fn case_comment_single_line_hash_with_carriage_return_line_feed() {
159 let input = b"# foobar\r\nbazqux";
160 let output = Result::Done(&b"bazqux"[..], &b" foobar\r\n"[..]);
161
162 assert_eq!(comment_single_line(input), output);
163 assert_eq!(comment(input), output);
164 }
165
166 #[test]
167 fn case_comment_single_line_hash_without_line_ending() {
168 let input = b"# foobar";
169 let output = Result::Done(&b""[..], &b" foobar"[..]);
170
171 assert_eq!(comment_single_line(input), output);
172 assert_eq!(comment(input), output);
173 }
174
175 #[test]
176 fn case_comment_single_line_hash_embedded() {
177 let input = b"#foo#bar";
178 let output = Result::Done(&b""[..], &b"foo#bar"[..]);
179
180 assert_eq!(comment_single_line(input), output);
181 assert_eq!(comment(input), output);
182 }
183
184 #[test]
185 fn case_comment_delimited_empty() {
186 let input = b"/**/xyz";
187 let output = Result::Done(&b"xyz"[..], &b""[..]);
188
189 assert_eq!(comment_delimited(input), output);
190 assert_eq!(comment(input), output);
191 }
192
193 #[test]
194 fn case_comment_delimited() {
195 let input = b"/* foo bar\nbaz\r\nqux // hello,\n /*world!*/xyz */";
196 let output = Result::Done(&b"xyz */"[..], &b" foo bar\nbaz\r\nqux // hello,\n /*world!"[..]);
197
198 assert_eq!(comment_delimited(input), output);
199 assert_eq!(comment(input), output);
200 }
201
202 #[test]
203 fn case_invalid_comment_delimited_not_closed() {
204 let input = b"/*foobar";
205
206 assert_eq!(comment_delimited(input), Result::Error(Error::Position(ErrorKind::TakeUntilAndConsume, &b"foobar"[..])));
207 assert_eq!(comment(input), Result::Error(Error::Position(ErrorKind::Alt, &input[..])));
208 }
209}