Class IndentDedentInputBuffer
- All Implemented Interfaces:
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
-
Constructor Summary
ConstructorDescriptionIndentDedentInputBuffer
(char[] input, int tabStop, String lineCommentStart, boolean strict) Creates a new IndentDedentInputBuffer around the given char array.IndentDedentInputBuffer
(char[] input, int tabStop, String lineCommentStart, boolean strict, boolean skipEmptyLines) Creates a new IndentDedentInputBuffer around the given char array. -
Method Summary
Modifier and TypeMethodDescriptionchar
charAt
(int index) Returns the character at the given index.extract
(int start, int end) Constructs a newString
from all character between the given indices.extract
(IndexRange range) Constructs a newString
from all character covered by the given IndexRange.extractLine
(int lineNumber) Constructs a newString
containing all characters with the given line number except for the trailing newline.int
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.getPosition
(int index) Returns the line and column number of the character with the given index encapsulated in aPosition
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).
-
Constructor Details
-
IndentDedentInputBuffer
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.lineCommentStart
- the string starting a line comment or null, if line comments are not definedstrict
- signals whether the buffer should throw anIllegalIndentationException
on "semi-dedents", if false the buffer silently accepts these- Throws:
IllegalIndentationException
- if the input contains illegal indentations and the strict flag is set
-
IndentDedentInputBuffer
public IndentDedentInputBuffer(char[] input, int tabStop, String lineCommentStart, boolean strict, boolean skipEmptyLines) 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.lineCommentStart
- the string starting a line comment or null, if line comments are not definedstrict
- signals whether the buffer should throw anIllegalIndentationException
on "semi-dedents", if false the buffer silently accepts theseskipEmptyLines
- signals whether the buffer should swallow empty lines- Throws:
IllegalIndentationException
- if the input contains illegal indentations and the strict flag is set
-
-
Method Details
-
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 returnsChars.EOI
.- Specified by:
charAt
in interfaceInputBuffer
- 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 interfaceInputBuffer
- 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
Description copied from interface:InputBuffer
Constructs a newString
from all character between the given indices. Invalid indices are automatically adjusted to their respective boundary.- Specified by:
extract
in interfaceInputBuffer
- Parameters:
start
- the start index (inclusively)end
- the end index (exclusively)- Returns:
- a new String (non-interned)
-
extract
Description copied from interface:InputBuffer
Constructs a newString
from all character covered by the given IndexRange.- Specified by:
extract
in interfaceInputBuffer
- Parameters:
range
- the IndexRange- Returns:
- a new String (non-interned)
-
getPosition
Description copied from interface:InputBuffer
Returns the line and column number of the character with the given index encapsulated in aPosition
object. The very first character has the line number 1 and the column number 1.- Specified by:
getPosition
in interfaceInputBuffer
- Parameters:
index
- the index of the character to get the line number of- Returns:
- the line number
-
getOriginalIndex
public int getOriginalIndex(int index) Description copied from interface:InputBuffer
Translates the given index from the scope of this InputBuffer to the scope of the original, underlying char array. TheDefaultInputBuffer
implementation simply returns the given index, but other implementations like theIndentDedentInputBuffer
or theMutableInputBuffer
need to "undo" all compressions and index shiftings performed internally in order to return the underlying index.- Specified by:
getOriginalIndex
in interfaceInputBuffer
- Parameters:
index
- the index relative to this InputBuffer- Returns:
- the index relative to the underlying string or char array
-
extractLine
Description copied from interface:InputBuffer
Constructs a newString
containing all characters with the given line number except for the trailing newline.- Specified by:
extractLine
in interfaceInputBuffer
- Parameters:
lineNumber
- the line number to get- Returns:
- the string
-
getLineCount
public int getLineCount()Description copied from interface:InputBuffer
Returns the number of lines in the input buffer.- Specified by:
getLineCount
in interfaceInputBuffer
- Returns:
- number of lines in the input buffer.
-