001/*
002 * Copyright (C) 2009-2011 Mathias Doenitz
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.parboiled.buffers;
018
019import org.parboiled.support.Chars;
020import org.parboiled.support.IndexRange;
021import org.parboiled.support.Position;
022import org.parboiled.support.Chars;
023import org.parboiled.support.IndexRange;
024import org.parboiled.support.Position;
025
026/**
027 * Abstraction of a simple char[] buffer holding the input text to be parsed.
028 */
029public interface InputBuffer {
030
031    /**
032     * Returns the character at the given index. If the index is invalid the method returns
033     * {@link Chars#EOI}.
034     *
035     * @param index the index
036     * @return the character at the given index or Chars.EOI.
037     */
038    char charAt(int index);
039
040    /**
041     * Determines whether the characters starting at the given index match the ones from the given array (in order).
042     *
043     * @param index      the index into the input buffer where to start the comparison
044     * @param characters the characters to test against the input buffer
045     * @return true if matched
046     */
047    boolean test(int index, char[] characters);
048
049    /**
050     * Constructs a new {@link String} from all character between the given indices.
051     * Invalid indices are automatically adjusted to their respective boundary.
052     *
053     * @param start the start index (inclusively)
054     * @param end   the end index (exclusively)
055     * @return a new String (non-interned)
056     */
057    String extract(int start, int end);
058    
059    /**
060     * Constructs a new {@link String} from all character covered by the given IndexRange.
061     *
062     * @param range the IndexRange
063     * @return a new String (non-interned)
064     */
065    String extract(IndexRange range);
066
067    /**
068     * Returns the line and column number of the character with the given index encapsulated in a
069     * {@link Position}
070     * object. The very first character has the line number 1 and the column number 1.
071     *
072     * @param index the index of the character to get the line number of
073     * @return the line number
074     */
075    Position getPosition(int index);
076
077    /**
078     * Translates the given index from the scope of this InputBuffer to the scope of the original, underlying char
079     * array. The {@link DefaultInputBuffer} implementation simply returns the given index, but other implementations
080     * like the {@link IndentDedentInputBuffer} or the {@link MutableInputBuffer} need to "undo" all compressions and
081     * index shiftings performed internally in order to return the underlying index. 
082     * 
083     * @param index the index relative to this InputBuffer
084     * @return the index relative to the underlying string or char array
085     */
086    int getOriginalIndex(int index);
087
088    /**
089     * Constructs a new {@link String} containing all characters with the given line number except for the trailing
090     * newline.
091     *
092     * @param lineNumber the line number to get
093     * @return the string
094     */
095    String extractLine(int lineNumber);
096
097    /**
098     * Returns the number of lines in the input buffer.
099     *
100     * @return number of lines in the input buffer.
101     */
102    int getLineCount();
103}