1
1
use pest:: { error:: Error , iterators:: Pairs , Parser } ;
2
2
3
3
#[ derive( Parser ) ]
4
- #[ grammar = "data /redcode.pest" ]
4
+ #[ grammar = "parser/grammar /redcode.pest" ]
5
5
struct Grammar ;
6
6
7
7
pub fn parse ( rule : Rule , input : & str ) -> Result < Pairs < Rule > , Error < Rule > > {
@@ -15,6 +15,28 @@ mod tests {
15
15
16
16
use super :: * ;
17
17
18
+ // NOTE: these "doctests" are not actually compiled or run
19
+ // since they are in a #[cfg(test)] module
20
+
21
+ /// A macro to assert on the way a certain input string parses
22
+ /// Two forms are allowed. One has no identifier:
23
+ /// ```
24
+ /// match_parse!(Field {
25
+ /// "123" | "4567" => [
26
+ /// // This should look like the `tokens` field of `parses_to!`
27
+ /// ],
28
+ /// });
29
+ /// ```
30
+ ///
31
+ /// The other allows you to bind the input string so you can use it in your
32
+ /// ```
33
+ /// match_parse!(input, Field {
34
+ /// "123" | "4567" => [
35
+ /// // You can do something with e.g. `input.len()` here, which
36
+ /// // will be either 3 or 4 depending on the test case
37
+ /// ],
38
+ /// });
39
+ /// ```
18
40
macro_rules! match_parse {
19
41
( $rule: ident $args: tt) => {
20
42
match_parse!( _input, $rule $args)
@@ -78,14 +100,24 @@ mod tests {
78
100
#[ test]
79
101
fn parse_label_expr ( ) {
80
102
match_parse ! ( Expr {
81
- "foo" | "fo2" | "f_2" => [
103
+ "foo" | "fo2" | "f_2" => [
82
104
Expr ( 0 , 3 , [
83
105
Label ( 0 , 3 ) ,
84
106
] ) ,
85
107
]
86
108
} ) ;
87
109
}
88
110
111
+ #[ test]
112
+ fn parse_label ( ) {
113
+ match_parse ! ( label_input, LabelDeclaration {
114
+ "some_label" | "some_label2" => [ Label ( 0 , label_input. len( ) ) ] ,
115
+ "a: " => [ Label ( 0 , 1 ) ] ,
116
+ " a " => [ Label ( 1 , 2 ) ] ,
117
+ "a :" => [ Label ( 0 , 1 ) ] ,
118
+ } ) ;
119
+ }
120
+
89
121
#[ test]
90
122
fn parse_opcode_modifier ( ) {
91
123
match_parse ! ( input, Operation {
@@ -228,24 +260,4 @@ mod tests {
228
260
] ,
229
261
} ) ;
230
262
}
231
-
232
- #[ test]
233
- fn parse_label ( ) {
234
- for & ( label_input, start, end) in [
235
- ( "some_label" , 0 , 10 ) ,
236
- ( "some_label2" , 0 , 11 ) ,
237
- ( "a: " , 0 , 1 ) ,
238
- ( " a " , 1 , 2 ) ,
239
- ( "a :" , 0 , 1 ) ,
240
- ]
241
- . iter ( )
242
- {
243
- parses_to ! {
244
- parser: Grammar ,
245
- input: label_input,
246
- rule: Rule :: LabelDeclaration ,
247
- tokens: [ Label ( start, end) ]
248
- }
249
- }
250
- }
251
263
}
0 commit comments