Class RNCScanner
Produces a stream of RNCTokenType tokens by scanning the input
character-by-character. The scanner is forward-only: call scan()
repeatedly to advance through the token stream until RNCTokenType.EOS
is returned.
Use case: all RNC language features (formatting, semantic tokens, folding, highlighting, completions, code actions, diagnostics) use this scanner to tokenize RNC content without building a full parse tree. This keeps each feature lightweight and fast.
Example usage:
CharSequence text = "start = element root { text }";
RNCScanner scanner = RNCScanner.createScanner(text);
RNCTokenType token = scanner.scan();
while (token != RNCTokenType.EOS) {
System.out.println(token + ": " + scanner.getTokenText()
+ " [" + scanner.getTokenOffset() + ".." + scanner.getTokenEnd() + ")");
token = scanner.scan();
}
// Output:
// KeywordStart: start [0..5)
// Whitespace: [5..6)
// Assign: = [6..7)
// Whitespace: [7..8)
// KeywordElement: element [8..15)
// Whitespace: [15..16)
// Identifier: root [16..20)
// Whitespace: [20..21)
// BraceOpen: { [21..22)
// Whitespace: [22..23)
// KeywordText: text [23..27)
// Whitespace: [27..28)
// BraceClose: } [28..29)
Design Notes
- Uses char-by-char scanning (no regex or substring) for performance on large files.
- Keywords are recognized context-free: the word
elementalways producesRNCTokenType.KeywordElement. The escaped form\elementproducesRNCTokenType.Identifier, allowing reserved words to be used as names. - Whitespace tokens are emitted (not skipped) so that the formatter can inspect and adjust them.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionRNCScanner(CharSequence input, int initialOffset) Creates a scanner starting at the given offset. -
Method Summary
Modifier and TypeMethodDescriptionstatic RNCScannercreateScanner(CharSequence input) Creates a scanner for the given input, starting from the beginning.static RNCScannercreateScanner(CharSequence input, int initialOffset) Creates a scanner for the given input, starting at a specific offset.Returns the current scanner state.intReturns the end offset of the current token in the source text.Returns any error message associated with the current token.intReturns the length of the current token (end - offset).intReturns the start offset of the current token in the source text.Returns the text content of the current token.Returns the type of the current token (set by the lastscan()call).scan()Scans and returns the next token.
-
Constructor Details
-
RNCScanner
Creates a scanner starting at the given offset.- Parameters:
input- the RNC text to scan.initialOffset- the offset to start scanning from.
-
-
Method Details
-
createScanner
Creates a scanner for the given input, starting from the beginning.Use case: most callers scan the entire document from offset 0.
- Parameters:
input- the RNC text to scan.- Returns:
- a new scanner positioned at offset 0.
-
createScanner
Creates a scanner for the given input, starting at a specific offset.Use case: scanning a sub-region of the document (e.g. for incremental updates or range formatting).
- Parameters:
input- the RNC text to scan.initialOffset- the offset to start scanning from.- Returns:
- a new scanner positioned at the given offset.
-
scan
Scans and returns the next token.After this call, use
getTokenOffset(),getTokenEnd(),getTokenLength(), andgetTokenText()to inspect the returned token.- Returns:
- the next token type, or
RNCTokenType.EOSwhen the input is exhausted.
-
getTokenType
Returns the type of the current token (set by the lastscan()call).- Returns:
- the current token type.
-
getTokenOffset
public int getTokenOffset()Returns the start offset of the current token in the source text.- Returns:
- the token's start offset (0-based, inclusive).
-
getTokenEnd
public int getTokenEnd()Returns the end offset of the current token in the source text.- Returns:
- the token's end offset (0-based, exclusive).
-
getTokenLength
public int getTokenLength()Returns the length of the current token (end - offset).- Returns:
- the token length in characters.
-
getTokenText
Returns the text content of the current token.Use case: extracting the identifier name or literal value after scanning.
- Returns:
- the token's text.
-
getTokenError
Returns any error message associated with the current token.- Returns:
- the error message, or
nullif no error.
-
getScannerState
Returns the current scanner state.- Returns:
- the scanner state.
-