java.lang.Object
org.eclipse.lemminx.extensions.relaxng.grammar.rnc.parser.RNCScanner

public class RNCScanner extends Object
A scanner (lexer/tokenizer) for RELAX NG Compact Syntax (RNC) text.

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 element always produces RNCTokenType.KeywordElement. The escaped form \element produces RNCTokenType.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 Details

    • RNCScanner

      public RNCScanner(CharSequence input, int initialOffset)
      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

      public static RNCScanner createScanner(CharSequence input)
      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

      public static RNCScanner createScanner(CharSequence input, int initialOffset)
      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

      public RNCTokenType scan()
      Scans and returns the next token.

      After this call, use getTokenOffset(), getTokenEnd(), getTokenLength(), and getTokenText() to inspect the returned token.

      Returns:
      the next token type, or RNCTokenType.EOS when the input is exhausted.
    • getTokenType

      public RNCTokenType getTokenType()
      Returns the type of the current token (set by the last scan() 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

      public String 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

      public String getTokenError()
      Returns any error message associated with the current token.
      Returns:
      the error message, or null if no error.
    • getScannerState

      public RNCScannerState getScannerState()
      Returns the current scanner state.
      Returns:
      the scanner state.