Skip to content

Commit 330c84b

Browse files
committed
started name; but parse is failing
1 parent 444f963 commit 330c84b

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

src/parser.rs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::collections::HashMap;
33
use std::marker::PhantomData;
44

55
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};
88

99
pub type Name = String;
1010
pub type SelectionSet = Vec<Selection>;
@@ -140,13 +140,13 @@ macro_rules! make_parser {
140140
($name:ident ($input_var:ident : $input_item_type:ty) -> $output_type:ty { $($tmpl:tt)* } $($rest:tt)*) => {
141141

142142
pub struct $name<T> {
143-
phantom: PhantomData<T>,
143+
_phantom: PhantomData<T>,
144144
}
145145

146146
impl<T> $name<T> {
147147
pub fn new() -> Self {
148148
$name {
149-
phantom: PhantomData
149+
_phantom: PhantomData
150150
}
151151
}
152152
}
@@ -167,14 +167,14 @@ macro_rules! make_parser {
167167
-> $output_type:ty { $($tmpl:tt)* } $($rest:tt)*) => {
168168

169169
pub struct $name<'a, T> {
170-
phantom: PhantomData<T>,
170+
_phantom: PhantomData<T>,
171171
$( $field: &'a $typ),*
172172
}
173173

174174
impl<'a, T> $name<'a, T> {
175175
pub fn new($($field: &'a $typ),*) -> Self {
176176
$name {
177-
phantom: PhantomData,
177+
_phantom: PhantomData,
178178
$( $field: $field),*
179179
}
180180
}
@@ -185,7 +185,7 @@ macro_rules! make_parser {
185185
type Output = $output_type;
186186

187187
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;
189189

190190
$($tmpl)*
191191
}
@@ -195,6 +195,11 @@ macro_rules! make_parser {
195195
};
196196
}
197197

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+
198203
make_parser!(
199204
WhiteSpace(input: char) -> char {
200205
char(' ').or(tab())
@@ -236,6 +241,14 @@ make_parser!(
236241
}
237242
);
238243

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+
239252
// pub fn operation_definition<I: U8Input>(i: I) -> SimpleResult<I,Definition>
240253
// {
241254
// parse!{i;
@@ -250,30 +263,6 @@ make_parser!(
250263
// }
251264
// }
252265

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-
277266
// pub fn alias<I: U8Input>(i: I) -> SimpleResult<I, Option<Name>>
278267
// {
279268
// let parser = |i: I| {
@@ -295,14 +284,25 @@ mod tests {
295284
use super::*;
296285
use combine::{Parser,State};
297286

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+
298293
#[test]
299294
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", ());
301296
}
302297

303298
#[test]
304299
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"));
307307
}
308308
}

0 commit comments

Comments
 (0)