Search in sources :

Example 1 with ParserFailure

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();
}
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 2 with ParserFailure

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");
}
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 3 with ParserFailure

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");
}
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 4 with ParserFailure

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();
}
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 5 with ParserFailure

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");
}
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