jaskell.parsec.common.Option Maven / Gradle / Ivy
package jaskell.parsec.common;
import jaskell.parsec.ParsecException;
import java.io.EOFException;
import java.util.Optional;
/**
* Created by Mars Liu on 16/9/18.
* Option accept a parsec<E, T, Status, Tran> and
* return new one parsec<Optional<T>, E, Status, Tran>.
* If the parser fail, Option parsec just return option.empty without Exception thrown.
*/
public class Option
implements Parsec> {
private final Parsec parser;
public Option(Parsec parser) {
this.parser = parser;
}
@Override
public Optional parse(State s)
throws EOFException, ParsecException {
Integer status = s.status();
try{
return Optional.of(parser.parse(s));
} catch (Exception e) {
if(status.equals(s.status())) {
return Optional.empty();
} else {
throw e;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy