![JAR search and dependency download from the Maven repository](/logo.png)
org.petitparser.parser.actions.ActionParser Maven / Gradle / Ivy
package org.petitparser.parser.actions;
import org.petitparser.context.Context;
import org.petitparser.context.Result;
import org.petitparser.parser.Parser;
import org.petitparser.parser.combinators.DelegateParser;
import java.util.Objects;
import java.util.function.Function;
/**
* A parser that performs a transformation with a given function on the successful parse result of
* the delegate.
*
* @param The type of the function argument.
* @param The type of the function result.
*/
public class ActionParser extends DelegateParser {
protected final Function function;
public ActionParser(Parser delegate, Function function) {
super(delegate);
this.function = Objects.requireNonNull(function, "Undefined function");
}
@Override
public Result parseOn(Context context) {
Result result = delegate.parseOn(context);
if (result.isSuccess()) {
return result.success(function.apply(result.get()));
} else {
return result;
}
}
@Override
protected boolean hasEqualProperties(Parser other) {
return super.hasEqualProperties(other) &&
Objects.equals(function, ((ActionParser) other).function);
}
@Override
public ActionParser copy() {
return new ActionParser<>(delegate, function);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy