@@ -3,8 +3,8 @@ use std::collections::HashMap;
3
3
use std:: marker:: PhantomData ;
4
4
5
5
use combine:: { Parser , ParseResult , Stream , value} ;
6
- use combine:: char:: { tab, char, crlf, string} ;
7
- use combine:: combinator:: { many, none_of} ;
6
+ use combine:: char:: { tab, char, crlf, string, letter , alpha_num } ;
7
+ use combine:: combinator:: { many, none_of, or } ;
8
8
9
9
pub type Name = String ;
10
10
pub type SelectionSet = Vec < Selection > ;
@@ -140,13 +140,13 @@ macro_rules! make_parser {
140
140
( $name: ident ( $input_var: ident : $input_item_type: ty) -> $output_type: ty { $( $tmpl: tt) * } $( $rest: tt) * ) => {
141
141
142
142
pub struct $name<T > {
143
- phantom : PhantomData <T >,
143
+ _phantom : PhantomData <T >,
144
144
}
145
145
146
146
impl <T > $name<T > {
147
147
pub fn new( ) -> Self {
148
148
$name {
149
- phantom : PhantomData
149
+ _phantom : PhantomData
150
150
}
151
151
}
152
152
}
@@ -167,14 +167,14 @@ macro_rules! make_parser {
167
167
-> $output_type: ty { $( $tmpl: tt) * } $( $rest: tt) * ) => {
168
168
169
169
pub struct $name<' a, T > {
170
- phantom : PhantomData <T >,
170
+ _phantom : PhantomData <T >,
171
171
$( $field: & ' a $typ) ,*
172
172
}
173
173
174
174
impl <' a, T > $name<' a, T > {
175
175
pub fn new( $( $field: & ' a $typ) ,* ) -> Self {
176
176
$name {
177
- phantom : PhantomData ,
177
+ _phantom : PhantomData ,
178
178
$( $field: $field) ,*
179
179
}
180
180
}
@@ -185,7 +185,7 @@ macro_rules! make_parser {
185
185
type Output = $output_type;
186
186
187
187
fn parse_stream( & mut self , $input_var: I ) -> ParseResult <Self :: Output , Self :: Input > {
188
- let & mut $name { phantom , $( $field) ,* } = self ;
188
+ let & mut $name { _phantom , $( $field) ,* } = self ;
189
189
190
190
$( $tmpl) *
191
191
}
@@ -195,6 +195,11 @@ macro_rules! make_parser {
195
195
} ;
196
196
}
197
197
198
+ // TODO graphql and char have a very differing set of code points
199
+ // graphql :: [0009,000A,000D, [0020,FFFF] ]
200
+ // char :: [0,D7FF] u [E000,10FFFF]
201
+ // somehow we need to wrangle char to match the graphql types :)
202
+
198
203
make_parser ! (
199
204
WhiteSpace ( input: char ) -> char {
200
205
char ( ' ' ) . or( tab( ) )
@@ -236,6 +241,14 @@ make_parser!(
236
241
}
237
242
) ;
238
243
244
+ make_parser ! (
245
+ NameP ( input: char ) -> Name {
246
+ or( letter( ) , char ( '_' ) )
247
+ . with( many( alpha_num( ) . or( char ( '_' ) ) ) )
248
+ . parse_stream( input)
249
+ }
250
+ ) ;
251
+
239
252
// pub fn operation_definition<I: U8Input>(i: I) -> SimpleResult<I,Definition>
240
253
// {
241
254
// parse!{i;
@@ -250,30 +263,6 @@ make_parser!(
250
263
// }
251
264
// }
252
265
253
- // pub fn operation_type<I: U8Input>(i: I) -> SimpleResult<I,OperationType>
254
- // {
255
- // let op_type = |i,b,r| string(i,b).map(|_| r);
256
- // parse!{i;
257
- // op_type(b"query", OperationType::Query) <|>
258
- // op_type(b"mutation", OperationType::Mutation)
259
- // }
260
- // }
261
-
262
- // pub fn name<I: U8Input>(i: I) -> SimpleResult<I,Name>
263
- // {
264
- // parse!{i;
265
- // let start = satisfy(|c| is_alpha(c) || c == b'_');
266
- // let rest = take_while(|c| is_alphanumeric(c) || c == b'_');
267
-
268
- // ret {
269
- // let mut start = String::from_utf8(vec![start]).unwrap();
270
- // let rest = String::from_utf8(rest.to_vec()).unwrap();
271
- // start.push_str(&rest);
272
- // start
273
- // }
274
- // }
275
- // }
276
-
277
266
// pub fn alias<I: U8Input>(i: I) -> SimpleResult<I, Option<Name>>
278
267
// {
279
268
// let parser = |i: I| {
@@ -295,14 +284,25 @@ mod tests {
295
284
use super :: * ;
296
285
use combine:: { Parser , State } ;
297
286
287
+ macro_rules! assert_sucessful_parse {
288
+ ( $parser: ident, $input: expr, $result: expr) => {
289
+ assert_eq!( $parser:: new( ) . parse( State :: new( $input) ) . map( |x| x. 0 ) , Ok ( $result) ) ;
290
+ }
291
+ }
292
+
298
293
#[ test]
299
294
fn test_parse_comment ( ) {
300
- assert_eq ! ( LineComment :: new ( ) . parse ( State :: new ( "#hello world\r \n " ) ) . map ( |x| x . 0 ) , Ok ( ( ) ) ) ;
295
+ assert_sucessful_parse ! ( LineComment , "#hello world\r \n " , ( ) ) ;
301
296
}
302
297
303
298
#[ test]
304
299
fn test_parse_operationtype ( ) {
305
- assert_eq ! ( OperationTypeP :: new( ) . parse( State :: new( "query" ) ) . map( |x| x. 0 ) , Ok ( OperationType :: Query ) ) ;
306
- assert_eq ! ( OperationTypeP :: new( ) . parse( State :: new( "mutation" ) ) . map( |x| x. 0 ) , Ok ( OperationType :: Mutation ) ) ;
300
+ assert_sucessful_parse ! ( OperationTypeP , "query" , OperationType :: Query ) ;
301
+ assert_sucessful_parse ! ( OperationTypeP , "mutation" , OperationType :: Mutation ) ;
302
+ }
303
+
304
+ #[ test]
305
+ fn test_parse_name ( ) {
306
+ assert_sucessful_parse ! ( NameP , "_asd" , String :: from( "_asd" ) ) ;
307
307
}
308
308
}
0 commit comments