public class IndentDedentInputBuffer extends java.lang.Object implements InputBuffer
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).
Blank lines (lines containing nothing but whitespace) are removed from the input and the buffer can, optionally, remove line comments (i.e. comments that start with a predefined character sequence and go to the end of the line).
This means that the highest index of this InputBuffer is probably smaller than that of the original input text
buffer, since all line indentations and blank lines 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.
If the input contains illegal indentation the buffer throws an IllegalIndentationException
during construction
Modifier and Type | Class and Description |
---|---|
private class |
IndentDedentInputBuffer.BufferConverter |
Modifier and Type | Field and Description |
---|---|
private DefaultInputBuffer |
convBuffer |
private int[] |
indexMap |
private DefaultInputBuffer |
origBuffer |
private boolean |
skipEmptyLines |
private boolean |
strict |
Constructor and Description |
---|
IndentDedentInputBuffer(char[] input,
int tabStop,
java.lang.String lineCommentStart,
boolean strict)
Creates a new IndentDedentInputBuffer around the given char array.
|
IndentDedentInputBuffer(char[] input,
int tabStop,
java.lang.String lineCommentStart,
boolean strict,
boolean skipEmptyLines)
Creates a new IndentDedentInputBuffer around the given char array.
|
Modifier and Type | Method and Description |
---|---|
char |
charAt(int index)
Returns the character at the given index.
|
java.lang.String |
extract(IndexRange range)
Constructs a new
String from all character covered by the given IndexRange. |
java.lang.String |
extract(int start,
int end)
Constructs a new
String from all character between the given indices. |
java.lang.String |
extractLine(int lineNumber)
Constructs a new
String containing all characters with the given line number except for the trailing
newline. |
int |
getLineCount()
Returns the number of lines in the input buffer.
|
int |
getOriginalIndex(int index)
Translates the given index from the scope of this InputBuffer to the scope of the original, underlying char
array.
|
Position |
getPosition(int index)
Returns the line and column number of the character with the given index encapsulated in a
Position
object. |
private int |
map(int convIndex) |
boolean |
test(int index,
char[] characters)
Determines whether the characters starting at the given index match the ones from the given array (in order).
|
private final DefaultInputBuffer origBuffer
private final DefaultInputBuffer convBuffer
private int[] indexMap
private final boolean strict
private final boolean skipEmptyLines
public IndentDedentInputBuffer(char[] input, int tabStop, java.lang.String lineCommentStart, boolean strict)
input
- the input text.tabStop
- the number of characters in a tab stop.lineCommentStart
- the string starting a line comment or null, if line comments are not definedstrict
- signals whether the buffer should throw an IllegalIndentationException
on
"semi-dedents", if false the buffer silently accepts theseIllegalIndentationException
- if the input contains illegal indentations and the strict flag is setpublic IndentDedentInputBuffer(char[] input, int tabStop, java.lang.String lineCommentStart, boolean strict, boolean skipEmptyLines)
input
- the input text.tabStop
- the number of characters in a tab stop.lineCommentStart
- the string starting a line comment or null, if line comments are not definedstrict
- signals whether the buffer should throw an IllegalIndentationException
on
"semi-dedents", if false the buffer silently accepts theseskipEmptyLines
- signals whether the buffer should swallow empty linesIllegalIndentationException
- if the input contains illegal indentations and the strict flag is setpublic char charAt(int index)
InputBuffer
Chars.EOI
.charAt
in interface InputBuffer
index
- the indexpublic boolean test(int index, char[] characters)
InputBuffer
test
in interface InputBuffer
index
- the index into the input buffer where to start the comparisoncharacters
- the characters to test against the input bufferpublic java.lang.String extract(int start, int end)
InputBuffer
String
from all character between the given indices.
Invalid indices are automatically adjusted to their respective boundary.extract
in interface InputBuffer
start
- the start index (inclusively)end
- the end index (exclusively)public java.lang.String extract(IndexRange range)
InputBuffer
String
from all character covered by the given IndexRange.extract
in interface InputBuffer
range
- the IndexRangepublic Position getPosition(int index)
InputBuffer
Position
object. The very first character has the line number 1 and the column number 1.getPosition
in interface InputBuffer
index
- the index of the character to get the line number ofpublic int getOriginalIndex(int index)
InputBuffer
DefaultInputBuffer
implementation simply returns the given index, but other implementations
like the IndentDedentInputBuffer
or the MutableInputBuffer
need to "undo" all compressions and
index shiftings performed internally in order to return the underlying index.getOriginalIndex
in interface InputBuffer
index
- the index relative to this InputBufferpublic java.lang.String extractLine(int lineNumber)
InputBuffer
String
containing all characters with the given line number except for the trailing
newline.extractLine
in interface InputBuffer
lineNumber
- the line number to getpublic int getLineCount()
InputBuffer
getLineCount
in interface InputBuffer
private int map(int convIndex)