All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jaskell.parsec.Option Maven / Gradle / Ivy

Go to download

This is a utils library for java 8 project. It include parsec combinators and sql generators library.

There is a newer version: 2.9.2
Show newest version
package jaskell.parsec;

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, Status, Tran> {
    private final Parsec parser;

    public Option(Parsec parser) {
        this.parser = parser;
    }

    @Override
    public  Optional parse(State s)
            throws EOFException, ParsecException {
        Status 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