/** * Returns a parser that produces an element from the stream that satisfies the given predicate, * or fails. * * @param missing The error if no element is available. * @param sat The error if the element does not satisfy the predicate. * @param f The predicate that the element should satisfy. * @return A parser that produces an element from the stream that satisfies the given predicate, * or fails. */ public static <I, E> Parser<Stream<I>, I, E> satisfy( final F0<E> missing, final F<I, E> sat, final F<I, Boolean> f) { return StreamParser.<I, E>element(missing) .bind( x -> f.f(x) ? Parser.<Stream<I>, I, E>value(x) : Parser.<Stream<I>, I, E>fail(sat.f(x))); }
/** * Returns a parser that produces a title-case character. * * @param missing The error if there is no character on the stream to produce a title-case * character with. * @param sat The error if the produced character is not a title-case character. * @return A parser that produces a title-case character. * @see Character#isTitleCase(char) */ public static <E> Parser<Stream<Character>, Character, E> titleCase( final F0<E> missing, final F<Character, E> sat) { return StreamParser.satisfy(missing, sat, c -> Character.isTitleCase(c)); }
/** * Returns a parser that produces a space character. * * @param missing The error if there is no character on the stream to produce a space character * with. * @param sat The error if the produced character is not a space character. * @return A parser that produces a space character. * @see Character#isSpace(char) */ public static <E> Parser<Stream<Character>, Character, E> space( final F0<E> missing, final F<Character, E> sat) { return StreamParser.satisfy(missing, sat, c -> Character.isSpaceChar(c)); }
/** * Returns a parser that produces a low-surrogate character. * * @param missing The error if there is no character on the stream to produce a low-surrogate * character with. * @param sat The error if the produced character is not a low-surrogate character. * @return A parser that produces a low-surrogate character. * @see Character#isLowSurrogate(char) */ public static <E> Parser<Stream<Character>, Character, E> lowSurrogate( final F0<E> missing, final F<Character, E> sat) { return StreamParser.satisfy(missing, sat, c -> Character.isLowSurrogate(c)); }
/** * Returns a parser that produces an alpha-numeric character. * * @param missing The error if there is no character on the stream to produce an alpha-numeric * character with. * @param sat The error if the produced character is not an alpha-numeric character. * @return A parser that produces an alpha-numeric character. * @see Character#isLetterOrDigit(char) */ public static <E> Parser<Stream<Character>, Character, E> alphaNum( final F0<E> missing, final F<Character, E> sat) { return StreamParser.satisfy(missing, sat, c -> Character.isLetterOrDigit(c)); }
/** * Returns a parser that produces a Java identifier start character. * * @param missing The error if there is no character on the stream to produce a Java identifier * start character with. * @param sat The error if the produced character is not a Java identifier start character. * @return A parser that produces a Java identifier start character. * @see Character#isJavaIdentifierStart(char) */ public static <E> Parser<Stream<Character>, Character, E> javaIdentifierStart( final F0<E> missing, final F<Character, E> sat) { return StreamParser.satisfy(missing, sat, c -> Character.isJavaIdentifierStart(c)); }
/** * Returns a parser that produces an ISO control character. * * @param missing The error if there is no character on the stream to produce an ISO control * character with. * @param sat The error if the produced character is not an ISO control character. * @return A parser that produces an ISO control character. * @see Character#isISOControl(char) */ public static <E> Parser<Stream<Character>, Character, E> isoControl( final F0<E> missing, final F<Character, E> sat) { return StreamParser.satisfy(missing, sat, c -> Character.isISOControl(c)); }
/** * Returns a parser that produces an identifier-ignorable character. * * @param missing The error if there is no character on the stream to produce an * identifier-ignorable character with. * @param sat The error if the produced character is not an identifier-ignorable character. * @return A parser that produces an identifier-ignorable character. * @see Character#isIdentifierIgnorable(char) */ public static <E> Parser<Stream<Character>, Character, E> identifierIgnorable( final F0<E> missing, final F<Character, E> sat) { return StreamParser.satisfy(missing, sat, c -> Character.isIdentifierIgnorable(c)); }
/** * Returns a parser that produces a digit (0 to 9). * * @param missing The error if there is no character on the stream to produce a digit with. * @param sat The error if the produced character is not a digit. * @return A parser that produces a digit (0 to 9). */ public static <E> Parser<Stream<Character>, Digit, E> digit( final F0<E> missing, final F<Character, E> sat) { return StreamParser.satisfy(missing, sat, c -> Character.isDigit(c)) .map(c1 -> Digit.fromChar(c1).some()); }
/** * Returns a parser that produces the given character or fails otherwise. * * @param missing The error if no character is available. * @param sat The error if the produced character is not the one given. * @param c The character to produce in the parser. * @return A parser that produces the given character or fails otherwise. */ public static <E> Parser<Stream<Character>, Character, E> character( final F0<E> missing, final F<Character, E> sat, final char c) { return StreamParser.satisfy(missing, sat, x -> x == c); }
/** * Returns a parser that produces a character if one is available or fails with the given error. * * @param e The error to fail with if a character is unavailable. * @return A parser that produces a character if one is available or fails with the given error. */ public static <E> Parser<Stream<Character>, Character, E> character(final F0<E> e) { return StreamParser.element(e); }
/** * Returns a parser that produces a unicode identifier part character. * * @param missing The error if there is no character on the stream to produce a unicode * identifier part character with. * @param sat The error if the produced character is not a unicode identifier part character. * @return A parser that produces a unicode identifier part character. * @see Character#isUnicodeIdentifierPart(char) */ public static <E> Parser<Stream<Character>, Character, E> unicodeIdentiferPart( final F0<E> missing, final F<Character, E> sat) { return StreamParser.satisfy(missing, sat, c -> Character.isUnicodeIdentifierPart(c)); }