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

net.hamnaberg.json.internal.org.javafp.parsecj.State Maven / Gradle / Ivy

There is a newer version: 6.2.0
Show newest version
package org.javafp.parsecj;

import org.javafp.parsecj.utils.CharSequences;

import java.util.*;

/**
 * An interface for parseable, immutable symbol streams.
 * @param  Input stream symbol type.
 */
public interface State {
    static  State of(S[] symbols) {
        return new ArrayState(symbols);
    }

    static State of(Character[] symbols) {
        return new CharArrayState(symbols);
    }

    static State of(String symbols) {
        return new StringState(symbols);
    }

    int position();

    boolean end();

    S current();

    List current(int n);

    default State next() {
        return next(1);
    }

    State next(int n);
}

interface CharState extends State {
    CharSequence getCharSequence();
    CharSequence getCharSequence(int length);
}

class StringState implements CharState {

    protected final String symbols;

    protected final int pos;

    StringState(String symbols, int pos) {
        this.symbols = symbols;
        this.pos = pos;
    }

    StringState(String symbols) {
        this(symbols, 0);
    }

    @Override
    public int position() {
        return pos;
    }

    @Override
    public boolean end() {
        return pos >= symbols.length();
    }

    @Override
    public Character current() {
        return pos < symbols.length() ? symbols.charAt(pos) : null;
    }

    @Override
    public List current(int n) {
        final List chars = new ArrayList<>(n);
        for (int i = pos; i < pos + n; ++i) {
            chars.add(symbols.charAt(i));
        }
        return chars;
    }

    @Override
    public StringState next() {
        return new StringState(symbols, pos + 1);
    }

    @Override
    public StringState next(int n) {
        return new StringState(symbols, pos + n);
    }

    @Override
    public CharSequence getCharSequence() {
        return symbols.substring(pos);
    }

    @Override
    public CharSequence getCharSequence(int length) {
        return symbols.substring(pos, pos + length);
    }
}

class ArrayState implements State {

    protected final S[] symbols;

    protected final int pos;

    ArrayState(S[] symbols, int pos) {
        this.symbols = symbols;
        this.pos = pos;
    }

    ArrayState(S[] symbols) {
        this(symbols, 0);
    }

    @Override
    public int position() {
        return pos;
    }

    @Override
    public boolean end() {
        return pos >= symbols.length;
    }

    @Override
    public S current() {
        return pos < symbols.length ? symbols[pos] : null;
    }

    @Override
    public List current(int n) {
        return Arrays.asList(symbols).subList(pos, pos + n);
    }

    @Override
    public State next() {
        return new ArrayState(symbols, pos + 1);
    }

    @Override
    public State next(int n) {
        return new ArrayState(symbols, pos + n);
    }
}

class CharArrayState extends ArrayState implements CharState {

    CharArrayState(Character[] symbols, int i) {
        super(symbols, i);
    }

    CharArrayState(Character[] symbols) {
        super(symbols);
    }

    @Override
    public CharSequence getCharSequence() {
        return CharSequences.of(symbols, pos, symbols.length - pos);
    }

    @Override
    public CharSequence getCharSequence(final int maxLength) {
        final int length = Math.min(symbols.length - pos, maxLength);
        return CharSequences.of(symbols, pos, length);
    }
}