Skip to content

Commit 7e33f80

Browse files
committed
added int value; updated variable definition parser
1 parent 6041e59 commit 7e33f80

File tree

1 file changed

+82
-6
lines changed

1 file changed

+82
-6
lines changed

src/parser.rs

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::collections::HashMap;
55
use std::marker::PhantomData;
66

77
use combine::{Parser, ParseResult, Stream};
8-
use combine::char::{tab, char, crlf, string, letter, alpha_num};
9-
use combine::combinator::{between, many, none_of, or, optional, value};
8+
use combine::char::{tab, char, crlf, string, letter, alpha_num, digit};
9+
use combine::combinator::{between, many, none_of, one_of, or, optional, value};
1010

1111
pub type Name = String;
1212
pub type SelectionSet = Vec<Selection>;
@@ -309,8 +309,9 @@ make_parser!(
309309
.skip(char(':'))
310310
.skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
311311
.and(TypeParser::new())
312-
.map(|(variable,var_type)| {
313-
VariableDefinition::new(variable, var_type, None)
312+
.and(optional(DefaultValue::new()))
313+
.map(|((variable,var_type),opt_type)| {
314+
VariableDefinition::new(variable, var_type, opt_type)
314315
})
315316
.skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
316317
.parse_stream(input)
@@ -330,7 +331,7 @@ make_parser!(
330331
let named_type = NameParser::new().map(Type::Named);
331332
let list_type = between(char('['),char(']'), TypeParser::new()).map(|t| Type::List(Box::new(t)));
332333

333-
// i can't clone for some reason -.-
334+
// TODO i can't clone for some reason -.-
334335
let non_null_type = char('!')
335336
.with(or(
336337
NameParser::new().map(Type::Named),
@@ -344,6 +345,60 @@ make_parser!(
344345
}
345346
);
346347

348+
make_parser!(
349+
DefaultValue(input: char) -> Value {
350+
char('=')
351+
.skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
352+
.with(ValueParser::new(&false))
353+
.parse_stream(input)
354+
}
355+
);
356+
357+
make_parser!(
358+
ValueParser(input: char, variable: &bool) -> Value {
359+
360+
let mut constants = IntValue::new();
361+
362+
if *variable {
363+
VariableParser::new().with(constants).parse_stream(input)
364+
} else {
365+
constants.parse_stream(input)
366+
}
367+
}
368+
);
369+
370+
make_parser!(
371+
IntValue(input: char) -> Value {
372+
optional(char('-'))
373+
.and(
374+
or(
375+
char('0').map(|c| {
376+
let mut result = String::new();
377+
result.push(c);
378+
result
379+
}),
380+
one_of("123456789".chars())
381+
.and(many::<String,_>(digit()))
382+
.map(|(c,rest)| {
383+
let mut result = String::new();
384+
result.push(c);
385+
result.push_str(&rest);
386+
result
387+
})
388+
)
389+
.map(|number| number.parse::<i32>().unwrap())
390+
)
391+
.map(|(neg,number)| {
392+
match neg {
393+
Some(_) => -number,
394+
None => number
395+
}
396+
})
397+
.map(Value::Int)
398+
.parse_stream(input)
399+
}
400+
);
401+
347402
make_parser!(
348403
Alias(input: char) -> Name {
349404
NameParser::new()
@@ -431,6 +486,27 @@ mod tests {
431486
fn test_parse_variabledefinition_nodefaultvalue() {
432487
assert_successful_parse!(VariableDefinitionParser,
433488
"$devicePicSize: Int",
434-
VariableDefinition::new(String::from("devicePicSize"), Type::Named(String::from("Int")), None));
489+
VariableDefinition::new(String::from("devicePicSize"),
490+
Type::Named(String::from("Int")),
491+
None));
492+
}
493+
494+
#[test]
495+
fn test_parse_variabledefinition_defaultvalue() {
496+
assert_successful_parse!(VariableDefinitionParser,
497+
"$devicePicSize: Int = 10",
498+
VariableDefinition::new(String::from("devicePicSize"),
499+
Type::Named(String::from("Int")),
500+
Some(Value::Int(10))));
501+
}
502+
503+
#[test]
504+
fn test_parse_value() {
505+
assert_successful_parse!(IntValue, "0", Value::Int(0));
506+
assert_successful_parse!(IntValue, "-0", Value::Int(0));
507+
assert_successful_parse!(IntValue, "1", Value::Int(1));
508+
assert_successful_parse!(IntValue, "-1", Value::Int(-1));
509+
assert_successful_parse!(IntValue, "10", Value::Int(10));
510+
assert_successful_parse!(IntValue, "-10", Value::Int(-10));
435511
}
436512
}

0 commit comments

Comments
 (0)