tagua_parser/rules/
comments.rs

1// Tagua VM
2//
3//
4// New BSD License
5//
6// Copyright © 2016-2016, Ivan Enderlin.
7// All rights reserved.
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are met:
11//     * Redistributions of source code must retain the above copyright
12//       notice, this list of conditions and the following disclaimer.
13//     * Redistributions in binary form must reproduce the above copyright
14//       notice, this list of conditions and the following disclaimer in the
15//       documentation and/or other materials provided with the distribution.
16//     * Neither the name of the Hoa nor the names of its contributors may be
17//       used to endorse or promote products derived from this software without
18//       specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
24// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30// POSSIBILITY OF SUCH DAMAGE.
31
32//! Group of comment rules.
33//!
34//! The list of all comments is provided by the PHP Language Specification in
35//! the [Grammar chapter, Comments
36//! section](https://github.com/php/php-langspec/blob/master/spec/19-grammar.md#comments).
37
38named!(
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}