org.jparsercombinator.ParserCombinatorOptional Maven / Gradle / Ivy
package org.jparsercombinator;
import java.util.Optional;
class ParserCombinatorOptional implements ParserCombinator> {
private ParserCombinator combinator;
ParserCombinatorOptional(ParserCombinator combinator) {
this.combinator = combinator;
}
@Override
public Result> apply(String input) {
Result result = combinator.apply(input);
if (result.isAccepted()) {
return new Accept<>(Optional.of(result.result()), result.remainingInput());
}
return new Accept<>(Optional.empty(), input);
}
}