Class PreprocessorLexer

java.lang.Object
com.blackrook.rookscript.struct.Lexer
com.blackrook.rookscript.struct.PreprocessorLexer
Direct Known Subclasses:
ScriptLexer

public class PreprocessorLexer extends Lexer
A lexer that scans for specific directives and affects the stream.
  • #include "file path" - Pushes a file onto the stream stack.
  • #define MACRO ...tokens... - Defines a macro and the string to replace them with. Defines are case-insensitive.
  • #undefine MACRO - Undefines a macro.
  • #ifdef MACRO - Includes the next set of lines (until #endif) if the provided macro is defined.
  • #ifndef MACRO - Includes the next set of lines (until #endif) if the provided macro is NOT defined.
  • #endif - Ends an "if" directive block.
  • #else - Block that is used if an "if" block does not succeed.
Author:
Matthew Tropiano
See Also:
  • Field Details

  • Constructor Details

    • PreprocessorLexer

      public PreprocessorLexer(Lexer.Kernel kernel, String in)
      Creates a new preprocessor lexer around a String, that will be wrapped into a StringReader. This will also assign this lexer a default name.
      Parameters:
      kernel - the lexer kernel to use for defining how to parse the input text.
      in - the string to read from.
    • PreprocessorLexer

      public PreprocessorLexer(Lexer.Kernel kernel, String name, String in)
      Creates a new preprocessor lexer around a String, that will be wrapped into a StringReader.
      Parameters:
      kernel - the lexer kernel to use for defining how to parse the input text.
      name - the name of this lexer.
      in - the reader to read from.
    • PreprocessorLexer

      public PreprocessorLexer(Lexer.Kernel kernel, Reader in)
      Creates a new preprocessor lexer around a reader. This will also assign this lexer a default name.
      Parameters:
      kernel - the kernel to use for this lexer.
      in - the reader to read from.
    • PreprocessorLexer

      public PreprocessorLexer(Lexer.Kernel kernel, String name, Reader in)
      Creates a new preprocessor lexer around a reader.
      Parameters:
      kernel - the kernel to use for this lexer.
      name - the name of this lexer.
      in - the reader to read from.
  • Method Details

    • setIncluder

      public void setIncluder(PreprocessorLexer.Includer includer)
      Sets the primary includer to use for resolving included streams.
      Parameters:
      includer - the includer to use.
    • addDefine

      public void addDefine(String macro, String tokenString)
      Adds a define macro to this lexer.
      Parameters:
      macro - the macro identifier.
      tokenString - the string to push onto the lexer.
    • addDefine

      public void addDefine(String macro, PreprocessorLexer.StringProvider tokenProvider)
      Adds a define macro to this lexer.
      Parameters:
      macro - the macro identifier.
      tokenProvider - a string-producing lambda function.
    • nextToken

      public Lexer.Token nextToken() throws IOException
      Description copied from class: Lexer
      Gets the next token. If there are no tokens left to read, this will return null. This method is NOT thread-safe!
      Overrides:
      nextToken in class Lexer
      Returns:
      the next token, or null if no more tokens to read.
      Throws:
      IOException - if a token cannot be read by the underlying Reader.
    • getInfoLine

      protected String getInfoLine(String streamName, int lineNumber, String token, String message)
    • readChar

      protected char readChar() throws IOException
      Description copied from class: Lexer
      Reads a character from the stream.
      Overrides:
      readChar in class Lexer
      Returns:
      the character read, or Lexer.END_OF_LEXER if no more characters, or Lexer.END_OF_STREAM if end of current stream.
      Throws:
      IOException - if a character cannot be read.
    • preprocess

      protected void preprocess() throws IOException
      Throws:
      IOException
    • processDirectiveLine

      protected void processDirectiveLine(String streamName, int lineNumber, String directiveLine)
      Called when a full directive is read and needs to be processed.
      Parameters:
      streamName - the stream name.
      lineNumber - the line number.
      directiveLine - the directive line to process.