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

jaskell.parsec.common.ChNone 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.common;

import static java.util.stream.Collectors.toSet;

import jaskell.parsec.ParsecException;

import java.io.EOFException;
import java.util.Set;
import java.util.stream.IntStream;

/**
 * Created by Mars Liu on 2016-01-10.
 * ChNone 即 char none of,是为 Character 特化的 none of
 */
public class ChNone implements Parsec {
    private final Set chars;
    private final Boolean caseSensitive;

    @Override
    public Character parse(State s)
        throws EOFException, ParsecException {
        Character c = s.next();
        if(caseSensitive){
            if(!chars.contains(c)){
                return c;
            }
        }else{
            if(!chars.contains(c.toString().toLowerCase().charAt(0))){
                return c;
            }
        }

        throw s.trap(String.format("expect any char none of %s (case sensitive %b) but get %c",
            chars, caseSensitive, c));
    }

    public ChNone(String data){
        this(data, false);
    }

    public ChNone(String data, boolean caseSensitive){
        this.caseSensitive = caseSensitive;
        if(caseSensitive) {
            this.chars = IntStream.range(0, data.length())
                .mapToObj(data::charAt).collect(toSet());
        } else {
            String content = data.toLowerCase();
            this.chars = IntStream.range(0, content.length())
                .mapToObj(content::charAt).collect(toSet());
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy