Search in sources :

Example 6 with ParserFailure

use of com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure in project riposte by Nike-Inc.

the class ParserTest method test_test6_works.

@Test
public void test_test6_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).filter(test((one, two, three, four, five, six) -> false)).map(match((one, two, three, four, five, six) -> 0));
    oResult = parser.tryParse("123456");
    assertThat(oResult.isPresent()).isFalse();
    parser = number.thenParse(number).thenParse(number).thenParse(number).thenParse(number).thenParse(number).filter(test((one, two, three, four, five, six) -> true)).map(match((one, two, three, four, five, six) -> one + two + three + four + five + six));
    oResult = parser.tryParse("123456");
    assertThat(oResult.isPresent()).isTrue();
    assertThat(oResult.get()).isNotNull();
    assertThat(oResult.get()).isEqualTo(21);
    oResult = parser.tryParse("12345");
    assertThat(oResult.isPresent()).isFalse();
}
Also used : Parsers.string(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.string) Parsers.then(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.then) Parsers.skip(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.skip) ParserFailure(com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure) Parsers.begin(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.begin) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MatchResult(java.util.regex.MatchResult) Parsers.regex(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.regex) Test(org.junit.Test) Supplier(java.util.function.Supplier) Parsers.zeroOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.zeroOrMore) List(java.util.List) ParserInput(com.nike.riposte.util.text.parsercombinator.Parser.ParserInput) Apply.test(com.nike.riposte.util.text.parsercombinator.Parser.Apply.test) Optional(java.util.Optional) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Pattern(java.util.regex.Pattern) Pair(com.nike.internal.util.Pair) Apply.match(com.nike.riposte.util.text.parsercombinator.Parser.Apply.match) Parsers.oneOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.oneOrMore) Pattern(java.util.regex.Pattern) Test(org.junit.Test)

Example 7 with ParserFailure

use of com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure in project riposte by Nike-Inc.

the class ParserTest method test_test3_works.

@Test
public void test_test3_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).filter(test((one, two, three) -> false)).map(match((one, two, three) -> 0));
    oResult = parser.tryParse("123");
    assertThat(oResult.isPresent()).isFalse();
    parser = number.thenParse(number).thenParse(number).filter(test((one, two, three) -> true)).map(match((one, two, three) -> one + two + three));
    oResult = parser.tryParse("123");
    assertThat(oResult.isPresent()).isTrue();
    assertThat(oResult.get()).isNotNull();
    assertThat(oResult.get()).isEqualTo(6);
    oResult = parser.tryParse("12");
    assertThat(oResult.isPresent()).isFalse();
}
Also used : Parsers.string(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.string) Parsers.then(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.then) Parsers.skip(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.skip) ParserFailure(com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure) Parsers.begin(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.begin) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MatchResult(java.util.regex.MatchResult) Parsers.regex(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.regex) Test(org.junit.Test) Supplier(java.util.function.Supplier) Parsers.zeroOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.zeroOrMore) List(java.util.List) ParserInput(com.nike.riposte.util.text.parsercombinator.Parser.ParserInput) Apply.test(com.nike.riposte.util.text.parsercombinator.Parser.Apply.test) Optional(java.util.Optional) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Pattern(java.util.regex.Pattern) Pair(com.nike.internal.util.Pair) Apply.match(com.nike.riposte.util.text.parsercombinator.Parser.Apply.match) Parsers.oneOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.oneOrMore) Pattern(java.util.regex.Pattern) Test(org.junit.Test)

Example 8 with ParserFailure

use of com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure in project riposte by Nike-Inc.

the class ParserTest method test_test7_works.

@Test
public void test_test7_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).filter(test((one, two, three, four, five, six, seven) -> false)).map(match((one, two, three, four, five, six, seven) -> 0));
    oResult = parser.tryParse("1234567");
    assertThat(oResult.isPresent()).isFalse();
    parser = number.thenParse(number).thenParse(number).thenParse(number).thenParse(number).thenParse(number).thenParse(number).filter(test((one, two, three, four, five, six, seven) -> true)).map(match((one, two, three, four, five, six, seven) -> one + two + three + four + five + six + seven));
    oResult = parser.tryParse("1234567");
    assertThat(oResult.isPresent()).isTrue();
    assertThat(oResult.get()).isNotNull();
    assertThat(oResult.get()).isEqualTo(28);
    oResult = parser.tryParse("123456");
    assertThat(oResult.isPresent()).isFalse();
}
Also used : Parsers.string(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.string) Parsers.then(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.then) Parsers.skip(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.skip) ParserFailure(com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure) Parsers.begin(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.begin) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MatchResult(java.util.regex.MatchResult) Parsers.regex(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.regex) Test(org.junit.Test) Supplier(java.util.function.Supplier) Parsers.zeroOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.zeroOrMore) List(java.util.List) ParserInput(com.nike.riposte.util.text.parsercombinator.Parser.ParserInput) Apply.test(com.nike.riposte.util.text.parsercombinator.Parser.Apply.test) Optional(java.util.Optional) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Pattern(java.util.regex.Pattern) Pair(com.nike.internal.util.Pair) Apply.match(com.nike.riposte.util.text.parsercombinator.Parser.Apply.match) Parsers.oneOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.oneOrMore) Pattern(java.util.regex.Pattern) Test(org.junit.Test)

Example 9 with ParserFailure

use of com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure in project riposte by Nike-Inc.

the class ParserTest method test_test4_works.

@Test
public void test_test4_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).filter(test((one, two, three, four) -> false)).map(match((one, two, three, four) -> 0));
    oResult = parser.tryParse("1234");
    assertThat(oResult.isPresent()).isFalse();
    parser = number.thenParse(number).thenParse(number).thenParse(number).filter(test((one, two, three, four) -> true)).map(match((one, two, three, four) -> one + two + three + four));
    oResult = parser.tryParse("1234");
    assertThat(oResult.isPresent()).isTrue();
    assertThat(oResult.get()).isNotNull();
    assertThat(oResult.get()).isEqualTo(10);
    oResult = parser.tryParse("123");
    assertThat(oResult.isPresent()).isFalse();
}
Also used : Parsers.string(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.string) Parsers.then(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.then) Parsers.skip(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.skip) ParserFailure(com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure) Parsers.begin(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.begin) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MatchResult(java.util.regex.MatchResult) Parsers.regex(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.regex) Test(org.junit.Test) Supplier(java.util.function.Supplier) Parsers.zeroOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.zeroOrMore) List(java.util.List) ParserInput(com.nike.riposte.util.text.parsercombinator.Parser.ParserInput) Apply.test(com.nike.riposte.util.text.parsercombinator.Parser.Apply.test) Optional(java.util.Optional) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Pattern(java.util.regex.Pattern) Pair(com.nike.internal.util.Pair) Apply.match(com.nike.riposte.util.text.parsercombinator.Parser.Apply.match) Parsers.oneOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.oneOrMore) Pattern(java.util.regex.Pattern) Test(org.junit.Test)

Example 10 with ParserFailure

use of com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure in project riposte by Nike-Inc.

the class ParserTest method test_test8_works.

@Test
public void test_test8_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).filter(test((one, two, three, four, five, six, seven, eight) -> false)).map(match((one, two, three, four, five, six, seven, eight) -> 0));
    oResult = parser.tryParse("12345678");
    assertThat(oResult.isPresent()).isFalse();
    parser = 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) -> true)).map(match((one, two, three, four, five, six, seven, eight) -> one + two + three + four + five + six + seven + eight));
    oResult = parser.tryParse("12345678");
    assertThat(oResult.isPresent()).isTrue();
    assertThat(oResult.get()).isNotNull();
    assertThat(oResult.get()).isEqualTo(36);
    oResult = parser.tryParse("1234567");
    assertThat(oResult.isPresent()).isFalse();
}
Also used : Parsers.string(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.string) Parsers.then(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.then) Parsers.skip(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.skip) ParserFailure(com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure) Parsers.begin(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.begin) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MatchResult(java.util.regex.MatchResult) Parsers.regex(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.regex) Test(org.junit.Test) Supplier(java.util.function.Supplier) Parsers.zeroOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.zeroOrMore) List(java.util.List) ParserInput(com.nike.riposte.util.text.parsercombinator.Parser.ParserInput) Apply.test(com.nike.riposte.util.text.parsercombinator.Parser.Apply.test) Optional(java.util.Optional) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Pattern(java.util.regex.Pattern) Pair(com.nike.internal.util.Pair) Apply.match(com.nike.riposte.util.text.parsercombinator.Parser.Apply.match) Parsers.oneOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.oneOrMore) Pattern(java.util.regex.Pattern) Test(org.junit.Test)

Aggregations

Pair (com.nike.internal.util.Pair)16 Apply.match (com.nike.riposte.util.text.parsercombinator.Parser.Apply.match)16 Apply.test (com.nike.riposte.util.text.parsercombinator.Parser.Apply.test)16 ParserFailure (com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure)16 ParserInput (com.nike.riposte.util.text.parsercombinator.Parser.ParserInput)16 Parsers.begin (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.begin)16 Parsers.oneOrMore (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.oneOrMore)16 Parsers.regex (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.regex)16 Parsers.skip (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.skip)16 Parsers.string (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.string)16 Parsers.then (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.then)16 Parsers.zeroOrMore (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.zeroOrMore)16 List (java.util.List)16 Optional (java.util.Optional)16 Supplier (java.util.function.Supplier)16 MatchResult (java.util.regex.MatchResult)16 Pattern (java.util.regex.Pattern)16 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)16 Test (org.junit.Test)16