#include <cstring>
#include <sstream>
#include <limits>
#include <vector>
#include <algorithm>
#include "../../Primitives/interface/BasicTypes.h"
#include "../../Primitives/interface/FlagEnum.h"
#include "../../Platforms/Basic/interface/DebugUtilities.hpp"
#include "StringTools.h"
Namespaces | |
namespace | Diligent |
Graphics engine namespace. | |
Enumerations | |
enum | Diligent::Parsing::SKIP_COMMENT_FLAGS : Uint32 { Diligent::Parsing::SKIP_COMMENT_FLAG_NONE = 0u , Diligent::Parsing::SKIP_COMMENT_FLAG_SINGLE_LINE = 1u << 0u , Diligent::Parsing::SKIP_COMMENT_FLAG_MULTILINE = 1u << 1u , Diligent::Parsing::SKIP_COMMENT_FLAG_ALL = SKIP_COMMENT_FLAG_SINGLE_LINE | SKIP_COMMENT_FLAG_MULTILINE } |
Flags controlling what kind of comments the SkipComment function should skip. More... | |
Functions | |
bool | Diligent::Parsing::IsWhitespace (Char Symbol) noexcept |
Returns true if the character is a white space or tab. | |
bool | Diligent::Parsing::IsNewLine (Char Symbol) noexcept |
Returns true if the character is a new line character. | |
bool | Diligent::Parsing::IsDelimiter (Char Symbol) noexcept |
Returns true if the character is a delimiter symbol (white space or new line) | |
bool | Diligent::Parsing::IsStatementSeparator (Char Symbol) noexcept |
Returns true if the character is a statement separator symbol. | |
bool | Diligent::Parsing::IsDigit (Char Symbol) noexcept |
Returns true if the character is a digit between 0 and 9. | |
template<typename InteratorType> | |
InteratorType | Diligent::Parsing::SkipLine (const InteratorType &Start, const InteratorType &End, bool GoToNextLine=false) noexcept |
Skips all characters until the end of the line. | |
template<typename InteratorType> | |
InteratorType | Diligent::Parsing::SkipComment (const InteratorType &Start, const InteratorType &End, SKIP_COMMENT_FLAGS Flags=SKIP_COMMENT_FLAG_ALL) noexcept(false) |
Skips single-line and multi-line comments starting from the given position. | |
template<typename InteratorType> | |
InteratorType | Diligent::Parsing::SkipDelimiters (const InteratorType &Start, const InteratorType &End, const char *Delimiters=nullptr) noexcept |
Skips all delimiters starting from the given position. | |
template<typename IteratorType> | |
IteratorType | Diligent::Parsing::SkipDelimitersAndComments (const IteratorType &Start, const IteratorType &End, const char *Delimiters=nullptr, SKIP_COMMENT_FLAGS CommentFlags=SKIP_COMMENT_FLAG_ALL) noexcept(false) |
Skips all comments and all delimiters starting from the given position. | |
template<typename IteratorType> | |
IteratorType | Diligent::Parsing::SkipIdentifier (const IteratorType &Start, const IteratorType &End) noexcept |
Skips one identifier starting from the given position. | |
template<typename IteratorType> | |
IteratorType | Diligent::Parsing::SkipFloatNumber (const IteratorType &Start, const IteratorType &End) noexcept |
Skips a floating point number starting from the given position. | |
template<typename IteratorType> | |
bool | Diligent::Parsing::SkipString (const IteratorType &Start, const IteratorType &End, const char *Str, IteratorType &StringEnd) noexcept |
template<typename IteratorType, typename ValueType> | |
IteratorType | Diligent::Parsing::ParseInteger (const IteratorType &Start, const IteratorType &End, ValueType &Value) noexcept |
template<typename IteratorType, typename HandlerType> | |
void | Diligent::Parsing::SplitString (const IteratorType &Start, const IteratorType &End, HandlerType &&Handler) noexcept(false) |
template<typename IteratorType> | |
std::string | Diligent::Parsing::GetContext (const IteratorType &Start, const IteratorType &End, IteratorType Pos, size_t NumLines) noexcept |
Prints a parsing context around the given position in the string. | |
template<typename TokenClass, typename ContainerType, typename IteratorType, typename CreateTokenFuncType, typename GetTokenTypeFunctType> | |
ContainerType | Diligent::Parsing::Tokenize (const IteratorType &SourceStart, const IteratorType &SourceEnd, CreateTokenFuncType CreateToken, GetTokenTypeFunctType GetTokenType) noexcept(false) |
Tokenizes the given string using the C-language syntax. | |
template<typename ContainerType> | |
std::string | Diligent::Parsing::BuildSource (const ContainerType &Tokens) noexcept |
Builds source string from tokens. | |
template<typename TokenIterType> | |
TokenIterType | Diligent::Parsing::FindFunction (const TokenIterType &Start, const TokenIterType &End, const char *Name) noexcept |
Finds a function with the given name in the token scope. | |
template<typename TokenIterType> | |
TokenIterType | Diligent::Parsing::FindMatchingBracket (const TokenIterType &Start, const TokenIterType &End, TokenIterType Pos) |
template<typename TokenIterType> | |
std::string | Diligent::Parsing::GetTokenContext (const TokenIterType &Start, const TokenIterType &End, TokenIterType Pos, size_t NumAdjacentLines) |
Prints a parsing context around the given token. | |
template<typename InteratorType> | |
std::string | Diligent::Parsing::RefinePreprocessorDirective (const InteratorType &Start, const InteratorType &End) noexcept |
Extracts the preprocessor directive from the given range. | |
template<typename InteratorType> | |
int | Diligent::Parsing::GetArrayIndex (const InteratorType &Start, const InteratorType &End, InteratorType &NameEnd) |
template<typename InteratorType> | |
InteratorType | Diligent::Parsing::FindNextPreprocessorDirective (const InteratorType &Start, const InteratorType &End, InteratorType &NameStart, InteratorType &NameEnd) |
void | Diligent::Parsing::StripPreprocessorDirectives (std::string &Source, const std::vector< std::string > &Directives) |
Strips all preprocessor directives from the source string. | |
Parsing tools
enum Diligent::Parsing::SKIP_COMMENT_FLAGS : Uint32 |
TokenIterType Diligent::Parsing::FindMatchingBracket | ( | const TokenIterType & | Start, |
const TokenIterType & | End, | ||
TokenIterType | Pos ) |
Searches for the matching bracket. For open brackets, searches in the forward direction. For closing brackets, searches backwards.
[in] | Start | - start of the string. |
[in] | End | - end of the string. |
[in] | Pos | - search starting position. |
|
inline |
Finds the next preprocessor directive in the given range.
# define MACRO ^ ^ ^ | | NameEnd | NameStart Return value
|
inline |
Returns the array index if the given variable name is an indexed array element.
[in] | Start | - starting position. |
[in] | End | - end of the input string. |
[out] | NameEnd | - ending position of the variable name. |
|
noexcept |
Prints a parsing context around the given position in the string.
[in] | Start | - start of the string. |
[in] | End | - end of the string. |
[in] | Pos | - position around which to print the context and which will be highlighted by ^. |
[in] | NumLines | - the number of lines above and below. |
The context looks like shown below:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ^ Ut enim ad minim veniam, quis nostrud exercitation ullamco lab
std::string Diligent::Parsing::GetTokenContext | ( | const TokenIterType & | Start, |
const TokenIterType & | End, | ||
TokenIterType | Pos, | ||
size_t | NumAdjacentLines ) |
Prints a parsing context around the given token.
[in] | Start | - start of the token range. |
[in] | End | - end of the token range. |
[in] | Pos | - position around which to print the context and which will be highlighted by ^. |
[in] | NumAdjacentLines | - the number of adjacent lines above and below to print. |
The context looks like shown below:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ^ Ut enim ad minim veniam, quis nostrud exercitation ullamco lab
|
noexcept |
Parses an integer starting from the given position.
[in] | Start | - starting position. |
[in] | End | - end of the input string |
[out] | Value | - parsed integer value. |
InteratorType Diligent::Parsing::SkipComment | ( | const InteratorType & | Start, |
const InteratorType & | End, | ||
SKIP_COMMENT_FLAGS | Flags = SKIP_COMMENT_FLAG_ALL ) |
Skips single-line and multi-line comments starting from the given position.
[in] | Start | - starting position. |
[in] | End | - end of the input string. |
[in] | Flags | - flags controlling what kind of comments to skip. |
In case of an error while parsing the comment (e.g. /*
is not closed), the function throws an exception of type std::pair<InteratorType, const char*>
, where first is the position of the error, and second is the error description.
|
noexcept |
Skips all delimiters starting from the given position.
[in] | Start | - starting position. |
[in] | End | - end of the input string. |
[in] | Delimiters | - optional string containing custom delimiters. |
IteratorType Diligent::Parsing::SkipDelimitersAndComments | ( | const IteratorType & | Start, |
const IteratorType & | End, | ||
const char * | Delimiters = nullptr, | ||
SKIP_COMMENT_FLAGS | CommentFlags = SKIP_COMMENT_FLAG_ALL ) |
Skips all comments and all delimiters starting from the given position.
[in] | Start | - starting position. |
[in] | End | - end of the input string. |
[in] | Delimiters | - optional string containing custom delimiters. |
[in] | CommentFlags | - optional flags controlling what kind of comments to skip. |
In case of a parsing error (which means there is an open multi-line comment /*...
), the function throws an exception of type std::pair<InteratorType, const char*>
, where first is the position of the error, and second is the error description.
|
noexcept |
Skips a floating point number starting from the given position.
[in] | Start | - starting position. |
[in] | End | - end of the input string. |
|
noexcept |
Skips one identifier starting from the given position.
[in] | Start | - starting position. |
[in] | End | - end of the input string. |
|
noexcept |
Skips all characters until the end of the line.
[in] | Start | - starting position. |
[in] | End | - end of the input string. |
[in] | GoToNextLine | - whether to go to the next line. |
GoToNextLine
is true
, the position following the new line character at the end of the string. If GoToNextLine
is false
, the position of the new line character at the end of the string.\r\n
) is treated as a single new line separator.
|
noexcept |
Skips a specified string starting from the given position.
[in] | Start | - starting position. |
[in] | End | - end of the input string. |
[in] | Str | - string to skip. |
[out] | StringEnd | - position immediately following the last character of the string, if found, or Start otherwise. |
void Diligent::Parsing::SplitString | ( | const IteratorType & | Start, |
const IteratorType & | End, | ||
HandlerType && | Handler ) |
Splits string into chunks separated by comments and delimiters.
[in] | Start | - start of the string to split. |
[in] | End | - end of the string to split. |
[in] | Handler | - user-provided handler to call for each chunk. |
The function starts from the beginning of the strings and splits it into chunks separated by comments and delimiters. For each chunk, it calls the user-provided handler and passes the start of the preceding comments/delimiters part. The handler must then process the text at the current position and move the pointer. It should return true to continue processing, and false to stop it.
In case of a parsing error, the function throws an exception of type std::pair<InteratorType, const char*>
, where first is the position of the error, and second is the error description.
ContainerType Diligent::Parsing::Tokenize | ( | const IteratorType & | SourceStart, |
const IteratorType & | SourceEnd, | ||
CreateTokenFuncType | CreateToken, | ||
GetTokenTypeFunctType | GetTokenType ) |
Tokenizes the given string using the C-language syntax.
[in] | SourceStart | - start of the source string. |
[in] | SourceEnd | - end of the source string. |
[in] | CreateToken | - a handler called every time a new token should be created. |
[in] | GetTokenType | - a function that should return the token type for the given literal. |