org.parboiled.buffers
Class IndentDedentInputBuffer
java.lang.Object
org.parboiled.buffers.DefaultInputBuffer
org.parboiled.buffers.IndentDedentInputBuffer
- All Implemented Interfaces:
- InputBuffer
public class IndentDedentInputBuffer
- extends DefaultInputBuffer
Special, immutable InputBuffer implementation for indentation based grammars.
This InputBuffer collapses all space and tab characters at the beginning of a text line into either nothing (if
the line has the same indentation level as the previous line), a special Chars.INDENT
character (if the line
has a greater indentation level than the previous line) or one or more Chars.DEDENT
characters (if the line
has a lower indentation level than the previous line).
This means that the highest index of this InputBuffer is probably smaller than that of the original input text
buffer, since all line indentation have been collapsed. However, the implementation will make sure that
getPosition(int)
, extract(int, int)
, etc. will work as expected and always return the "correct"
result from the underlying, original input buffer.
Constructor Summary |
IndentDedentInputBuffer(char[] input,
int tabStop)
Creates a new IndentDedentInputBuffer around the given char array. |
Method Summary |
protected char[] |
buildBuffer2()
|
char |
charAt(int index)
Returns the character at the given index. |
java.lang.String |
extract(int start,
int end)
Constructs a new String from all character between the given indices. |
InputBuffer.Position |
getPosition(int index)
Returns the line and column number of the character with the given index encapsulated in a
InputBuffer.Position
object. |
boolean |
test(int index,
char[] characters)
Determines whether the characters starting at the given index match the ones from the given array (in order). |
protected int |
translate(int ix2)
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
length2
protected final int length2
buffer2
protected final char[] buffer2
newlines2
protected int[] newlines2
tabStop
protected final int tabStop
IndentDedentInputBuffer
public IndentDedentInputBuffer(char[] input,
int tabStop)
- Creates a new IndentDedentInputBuffer around the given char array. Note that for performance reasons the given
char array is not defensively copied.
- Parameters:
input
- the input text.tabStop
- the number of characters in a tab stop.
charAt
public char charAt(int index)
- Description copied from interface:
InputBuffer
- Returns the character at the given index. If the index is invalid the method returns
Chars.EOI
.
- Specified by:
charAt
in interface InputBuffer
- Overrides:
charAt
in class DefaultInputBuffer
- Parameters:
index
- the index
- Returns:
- the character at the given index or Chars.EOI.
test
public boolean test(int index,
char[] characters)
- Description copied from interface:
InputBuffer
- Determines whether the characters starting at the given index match the ones from the given array (in order).
- Specified by:
test
in interface InputBuffer
- Overrides:
test
in class DefaultInputBuffer
- Parameters:
index
- the index into the input buffer where to start the comparisoncharacters
- the characters to test against the input buffer
- Returns:
- true if matched
extract
public java.lang.String extract(int start,
int end)
- Description copied from interface:
InputBuffer
- Constructs a new
String
from all character between the given indices.
Invalid indices are automatically adjusted to their respective boundary.
- Specified by:
extract
in interface InputBuffer
- Overrides:
extract
in class DefaultInputBuffer
- Parameters:
start
- the start index (inclusively)end
- the end index (exclusively)
- Returns:
- a new String (non-interned)
getPosition
public InputBuffer.Position getPosition(int index)
- Description copied from interface:
InputBuffer
- Returns the line and column number of the character with the given index encapsulated in a
InputBuffer.Position
object. The very first character has the line number 1 and the column number 1.
- Specified by:
getPosition
in interface InputBuffer
- Overrides:
getPosition
in class DefaultInputBuffer
- Parameters:
index
- the index of the character to get the line number of
- Returns:
- the line number
translate
protected int translate(int ix2)
buildBuffer2
protected char[] buildBuffer2()