use of com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure in project riposte by Nike-Inc.
the class ParserTest method test_test5_works.
@Test
public void test_test5_works() throws ParserFailure {
final Pattern numberPattern = Pattern.compile("([0-9])");
final Parser<Integer> number = regex(numberPattern).map(m -> new Integer(m.group(1)));
Parser<Number> parser;
Optional<Number> oResult;
parser = number.thenParse(number).thenParse(number).thenParse(number).thenParse(number).filter(test((one, two, three, four, five) -> false)).map(match((one, two, three, four, five) -> 0));
oResult = parser.tryParse("12345");
assertThat(oResult.isPresent()).isFalse();
parser = number.thenParse(number).thenParse(number).thenParse(number).thenParse(number).filter(test((one, two, three, four, five) -> true)).map(match((one, two, three, four, five) -> one + two + three + four + five));
oResult = parser.tryParse("12345");
assertThat(oResult.isPresent()).isTrue();
assertThat(oResult.get()).isNotNull();
assertThat(oResult.get()).isEqualTo(15);
oResult = parser.tryParse("1234");
assertThat(oResult.isPresent()).isFalse();
}
use of com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure in project riposte by Nike-Inc.
the class ParserTest method test_match9_works.
@Test
public void test_match9_works() throws ParserFailure {
final Pattern numberPattern = Pattern.compile("([0-9])");
final Pattern booleanPattern = Pattern.compile("(true|false)");
final Supplier<Parser<Integer>> number = () -> regex(numberPattern).map(m -> new Integer(m.group(1)));
final Supplier<Parser<Boolean>> bool = () -> regex(booleanPattern).map(m -> new Boolean(m.group(1)));
Parser<String> parser = number.get().thenParse(string("A")).thenParse(bool).thenParse(number).thenParse(string("B")).thenParse(bool).thenParse(number).thenParse(string("C")).thenParse(bool).map(match((first, second, third, fourth, fifth, sixth, seventh, eighth, nineth) -> {
return first.toString() + "+" + second.toString() + "+" + third.toString() + "+" + fourth.toString() + "+" + fifth.toString() + "+" + sixth.toString() + "+" + seventh.toString() + "+" + eighth.toString() + "+" + nineth.toString();
}));
Optional<String> oResult = parser.tryParse("1Atrue2Bfalse3Ctrue");
assertThat(oResult.isPresent()).isTrue();
assertThat(oResult.get()).isNotNull();
assertThat(oResult.get()).isEqualTo("1+A+true+2+B+false+3+C+true");
}
use of com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure in project riposte by Nike-Inc.
the class ParserTest method test_match8_works.
@Test
public void test_match8_works() throws ParserFailure {
final Pattern numberPattern = Pattern.compile("([0-9])");
final Pattern booleanPattern = Pattern.compile("(true|false)");
final Supplier<Parser<Integer>> number = () -> regex(numberPattern).map(m -> new Integer(m.group(1)));
final Supplier<Parser<Boolean>> bool = () -> regex(booleanPattern).map(m -> new Boolean(m.group(1)));
Parser<String> parser = number.get().thenParse(string("A")).thenParse(bool).thenParse(number).thenParse(string("B")).thenParse(bool).thenParse(number).thenParse(string("C")).map(match((first, second, third, fourth, fifth, sixth, seventh, eighth) -> {
return first.toString() + "+" + second.toString() + "+" + third.toString() + "+" + fourth.toString() + "+" + fifth.toString() + "+" + sixth.toString() + "+" + seventh.toString() + "+" + eighth.toString();
}));
Optional<String> oResult = parser.tryParse("1Atrue2Bfalse3C");
assertThat(oResult.isPresent()).isTrue();
assertThat(oResult.get()).isNotNull();
assertThat(oResult.get()).isEqualTo("1+A+true+2+B+false+3+C");
}
use of com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure in project riposte by Nike-Inc.
the class ParserTest method test_test9_works.
@Test
public void test_test9_works() throws ParserFailure {
final Pattern numberPattern = Pattern.compile("([0-9])");
final Parser<Integer> number = regex(numberPattern).map(m -> new Integer(m.group(1)));
Parser<Number> parser;
Optional<Number> oResult;
parser = number.thenParse(number).thenParse(number).thenParse(number).thenParse(number).thenParse(number).thenParse(number).thenParse(number).thenParse(number).filter(test((one, two, three, four, five, six, seven, eight, nine) -> false)).map(match((one, two, three, four, five, six, seven, eight, nine) -> 0));
oResult = parser.tryParse("123456789");
assertThat(oResult.isPresent()).isFalse();
parser = number.thenParse(number).thenParse(number).thenParse(number).thenParse(number).thenParse(number).thenParse(number).thenParse(number).thenParse(number).filter(test((one, two, three, four, five, six, seven, eight, nine) -> true)).map(match((one, two, three, four, five, six, seven, eight, nine) -> one + two + three + four + five + six + seven + eight + nine));
oResult = parser.tryParse("123456789");
assertThat(oResult.isPresent()).isTrue();
assertThat(oResult.get()).isNotNull();
assertThat(oResult.get()).isEqualTo(45);
oResult = parser.tryParse("12345678");
assertThat(oResult.isPresent()).isFalse();
}
use of com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure in project riposte by Nike-Inc.
the class ParserTest method test_match6_works.
@Test
public void test_match6_works() throws ParserFailure {
final Pattern numberPattern = Pattern.compile("([0-9])");
final Pattern booleanPattern = Pattern.compile("(true|false)");
final Supplier<Parser<Integer>> number = () -> regex(numberPattern).map(m -> new Integer(m.group(1)));
final Supplier<Parser<Boolean>> bool = () -> regex(booleanPattern).map(m -> new Boolean(m.group(1)));
Parser<String> parser = number.get().thenParse(string("A")).thenParse(bool).thenParse(number).thenParse(string("B")).thenParse(bool).map(match((first, second, third, fourth, fifth, sixth) -> {
return first.toString() + "+" + second.toString() + "+" + third.toString() + "+" + fourth.toString() + "+" + fifth.toString() + "+" + sixth.toString();
}));
Optional<String> oResult = parser.tryParse("1Atrue2Bfalse");
assertThat(oResult.isPresent()).isTrue();
assertThat(oResult.get()).isNotNull();
assertThat(oResult.get()).isEqualTo("1+A+true+2+B+false");
}
Aggregations