Package com.blackrook.archetext.struct
Class PreprocessorLexer
- java.lang.Object
-
- com.blackrook.archetext.struct.Lexer
-
- com.blackrook.archetext.struct.PreprocessorLexer
-
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:
Lexer
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PreprocessorLexer.DefaultIncluder
Default includer to use when none specified.static interface
PreprocessorLexer.Includer
An interface that allows the user to resolve a resource by path when the PreprocessorLexer parses it.static class
PreprocessorLexer.PreprocessorException
Thrown on preprocessor error.static interface
PreprocessorLexer.StringProvider
Lambda interface that returns a string.-
Nested classes/interfaces inherited from class com.blackrook.archetext.struct.Lexer
Lexer.Kernel, Lexer.Parser, Lexer.ReaderStack, Lexer.Token
-
-
Field Summary
Fields Modifier and Type Field Description static PreprocessorLexer.DefaultIncluder
DEFAULT_INCLUDER
The singular instance for the default includer.static String
DIRECTIVE_BANG
Preprocessor directive - Bang.static String
DIRECTIVE_DEFINE
Preprocessor directive - Define.static String
DIRECTIVE_ELSE
Preprocessor directive - Else.static String
DIRECTIVE_ENDIF
Preprocessor directive - End If.static String
DIRECTIVE_IFDEF
Preprocessor directive - If Defined.static String
DIRECTIVE_IFNDEF
Preprocessor directive - If Undefined.static String
DIRECTIVE_INCLUDE
Preprocessor directive - Include.static String
DIRECTIVE_UNDEFINE
Preprocessor directive - Undefine.-
Fields inherited from class com.blackrook.archetext.struct.Lexer
DEBUG, END_OF_LEXER, END_OF_STREAM, NEWLINE
-
-
Constructor Summary
Constructors Constructor Description PreprocessorLexer(Lexer.Kernel kernel, Reader in)
Creates a new preprocessor lexer around a reader.PreprocessorLexer(Lexer.Kernel kernel, String in)
Creates a new preprocessor lexer around a String, that will be wrapped into a StringReader.PreprocessorLexer(Lexer.Kernel kernel, String name, Reader in)
Creates a new preprocessor lexer around a reader.PreprocessorLexer(Lexer.Kernel kernel, String name, String in)
Creates a new preprocessor lexer around a String, that will be wrapped into a StringReader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addDefine(String macro, PreprocessorLexer.StringProvider tokenProvider)
Adds a define macro to this lexer.void
addDefine(String macro, String tokenString)
Adds a define macro to this lexer.protected String
getInfoLine(String streamName, int lineNumber, String token, String message)
Lexer.Token
nextToken()
Gets the next token.protected void
preprocess()
protected void
processDirectiveLine(String streamName, int lineNumber, String directiveLine)
Called when a full directive is read and needs to be processed.protected char
readChar()
Reads a character from the stream.void
setIncluder(PreprocessorLexer.Includer includer)
Sets the primary includer to use for resolving included streams.-
Methods inherited from class com.blackrook.archetext.struct.Lexer
clearCurrentLexeme, getCurrentLexeme, getCurrentLineNumber, getCurrentStream, getCurrentStreamName, getRawStringEnd, getState, getStringEnd, isCommentEndDelimiterStart, isDelimiterStart, isDigit, isExponent, isExponentSign, isHexDigit, isLetter, isLexerEnd, isNewline, isPoint, isRawStringStart, isSpace, isStreamEnd, isStringEnd, isStringEscape, isStringStart, isTab, isUnderscore, isWhitespace, modifyType, pushStream, saveChar, setDelimBreak, setMultilineStringStartAndEnd, setState, setStringStartAndEnd
-
-
-
-
Field Detail
-
DIRECTIVE_BANG
public static final String DIRECTIVE_BANG
Preprocessor directive - Bang.- See Also:
- Constant Field Values
-
DIRECTIVE_INCLUDE
public static final String DIRECTIVE_INCLUDE
Preprocessor directive - Include.- See Also:
- Constant Field Values
-
DIRECTIVE_DEFINE
public static final String DIRECTIVE_DEFINE
Preprocessor directive - Define.- See Also:
- Constant Field Values
-
DIRECTIVE_UNDEFINE
public static final String DIRECTIVE_UNDEFINE
Preprocessor directive - Undefine.- See Also:
- Constant Field Values
-
DIRECTIVE_IFDEF
public static final String DIRECTIVE_IFDEF
Preprocessor directive - If Defined.- See Also:
- Constant Field Values
-
DIRECTIVE_IFNDEF
public static final String DIRECTIVE_IFNDEF
Preprocessor directive - If Undefined.- See Also:
- Constant Field Values
-
DIRECTIVE_ENDIF
public static final String DIRECTIVE_ENDIF
Preprocessor directive - End If.- See Also:
- Constant Field Values
-
DIRECTIVE_ELSE
public static final String DIRECTIVE_ELSE
Preprocessor directive - Else.- See Also:
- Constant Field Values
-
DEFAULT_INCLUDER
public static final PreprocessorLexer.DefaultIncluder DEFAULT_INCLUDER
The singular instance for the default includer.
-
-
Constructor Detail
-
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 Detail
-
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 classLexer
- 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 classLexer
- Returns:
- the character read, or
Lexer.END_OF_LEXER
if no more characters, orLexer.END_OF_STREAM
if end of current stream. - Throws:
IOException
- if a character cannot be read.
-
preprocess
protected void preprocess() throws IOException
- Throws:
IOException
-
-