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

net.intelie.pipes.util.PipesCharStream Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
package net.intelie.pipes.util;

import net.intelie.pipes.ast.SourceLocation;
import net.intelie.pipes.generated.CharStream;

import java.io.IOException;

public class PipesCharStream implements CharStream {
    private final String input;
    private int[] line, column;
    private int index = -1, beginIndex = -1;

    public PipesCharStream(String input) {
        this.input = input;
        this.line = new int[input.length()];
        this.column = new int[input.length()];
        initLineColumn(input, line, column);
    }

    public SourceLocation makeLocation(SourceLocation.Type type, int begin, int end) {
        return new SourceLocation(
                new SourceLocation(input, begin, line[begin], column[begin]).withType(type),
                new SourceLocation(input, end, line[end-1], column[end-1])
        );
    }

    private static void initLineColumn(String input, int[] lines, int[] columns) {
        int column = 0, line = 1, tabSize = 8;
        boolean prevCharIsLF = false, prevCharIsCR = false;
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            column++;

            if (prevCharIsLF) {
                prevCharIsLF = false;
                line += (column = 1);
            } else if (prevCharIsCR) {
                prevCharIsCR = false;
                if (c == '\n') {
                    prevCharIsLF = true;
                } else
                    line += (column = 1);
            }

            switch (c) {
                case '\r':
                    prevCharIsCR = true;
                    break;
                case '\n':
                    prevCharIsLF = true;
                    break;
                case '\t':
                    column--;
                    column += (tabSize - (column % tabSize));
                    break;
                default:
                    break;
            }

            lines[i] = line;
            columns[i] = column;
        }

    }

    public String getInput() {
        return input;
    }

    public int getIndex() {
        return Math.max(0, index);
    }

    public int getBeginIndex() {
        return Math.max(0, beginIndex);
    }

    @Override
    public char readChar() throws IOException {
        if (index + 1 >= input.length())
            throw new IHateExceptionsForControlFlowException();
        return input.charAt(++index);
    }

    @Override
    public int getColumn() {
        return getColumn(index);
    }

    @Override
    public int getLine() {
        return getLine(index);
    }

    @Override
    public int getEndColumn() {
        return getColumn(index);
    }

    @Override
    public int getEndLine() {
        return getLine(index);
    }

    @Override
    public int getBeginColumn() {
        return getColumn(beginIndex);
    }

    @Override
    public int getBeginLine() {
        return getLine(beginIndex);
    }

    public int getColumn(int index) {
        if (index < 0) return 1;
        if (index >= column.length)
            return column.length > 0 ? column[column.length - 1] + 1 : 1;
        return column[index];
    }

    public int getLine(int index) {
        if (index < 0) return 1;
        if (index >= line.length)
            return line.length > 0 ? line[line.length - 1] : 1;

        return line[index];
    }

    @Override
    public void backup(int amount) {
        index -= amount;
    }

    @Override
    public char BeginToken() throws IOException {
        if (index + 1 >= input.length()) {
            beginIndex = input.length();
            index = input.length();
            throw new IHateExceptionsForControlFlowException();
        }
        char c = input.charAt(++index);
        beginIndex = index;
        return c;
    }

    @Override
    public String GetImage() {
        return input.substring(Math.max(0, beginIndex), index + 1);
    }

    @Override
    public char[] GetSuffix(int len) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void Done() {
        throw new UnsupportedOperationException();
    }

    public static class IHateExceptionsForControlFlowException extends IOException {
        public IHateExceptionsForControlFlowException() {
            super("this API is dumb");
        }

        @Override
        public synchronized Throwable fillInStackTrace() {
            return this;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy