Diligent Engine
 
Loading...
Searching...
No Matches
ParsingTools.hpp File Reference
#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.
 

Detailed Description

Parsing tools

Enumeration Type Documentation

◆ SKIP_COMMENT_FLAGS

Flags controlling what kind of comments the SkipComment function should skip.

Enumerator
SKIP_COMMENT_FLAG_NONE 

Skip no comments.

SKIP_COMMENT_FLAG_SINGLE_LINE 

Skip single-line comment.

SKIP_COMMENT_FLAG_MULTILINE 

Skip multi-line comment.

SKIP_COMMENT_FLAG_ALL 

Skip all kinds of comments.

Function Documentation

◆ FindMatchingBracket()

template<typename TokenIterType>
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.

Parameters
[in]Start- start of the string.
[in]End- end of the string.
[in]Pos- search starting position.
Returns
position of the matching bracket, or End if none is found.

◆ FindNextPreprocessorDirective()

template<typename InteratorType>
InteratorType Diligent::Parsing::FindNextPreprocessorDirective ( const InteratorType & Start,
const InteratorType & End,
InteratorType & NameStart,
InteratorType & NameEnd )
inline

Finds the next preprocessor directive in the given range.

# define MACRO
^ ^     ^
| |    NameEnd
| NameStart
Return value 

◆ GetArrayIndex()

template<typename InteratorType>
int Diligent::Parsing::GetArrayIndex ( const InteratorType & Start,
const InteratorType & End,
InteratorType & NameEnd )
inline

Returns the array index if the given variable name is an indexed array element.

Parameters
[in]Start- starting position.
[in]End- end of the input string.
[out]NameEnd- ending position of the variable name.
Returns
- Array index if the given variable name is an array element.
  • -1 if the given variable name is a valid identifier and is not an array element.
  • INT_MIN if the array index is invalid.

◆ GetContext()

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.

Parameters
[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.
Returns
string representing the context around the given position.

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

◆ GetTokenContext()

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.

Parameters
[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

◆ ParseInteger()

template<typename IteratorType, typename ValueType>
IteratorType Diligent::Parsing::ParseInteger ( const IteratorType & Start,
const IteratorType & End,
ValueType & Value )
noexcept

Parses an integer starting from the given position.

Parameters
[in]Start- starting position.
[in]End- end of the input string
[out]Value- parsed integer value.
Returns
position immediately following the last character of the number.

◆ SkipComment()

template<typename InteratorType>
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.

Parameters
[in]Start- starting position.
[in]End- end of the input string.
[in]Flags- flags controlling what kind of comments to skip.
Returns
if the comment is found, the position immediately following the end of the comment; starting position otherwise.

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.

◆ SkipDelimiters()

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.

Parameters
[in]Start- starting position.
[in]End- end of the input string.
[in]Delimiters- optional string containing custom delimiters.
Returns
position of the first non-delimiter character.

◆ SkipDelimitersAndComments()

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 )

Skips all comments and all delimiters starting from the given position.

Parameters
[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.
Returns
true position of the first non-comment non-delimiter character.

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.

◆ SkipFloatNumber()

template<typename IteratorType>
IteratorType Diligent::Parsing::SkipFloatNumber ( const IteratorType & Start,
const IteratorType & End )
noexcept

Skips a floating point number starting from the given position.

Parameters
[in]Start- starting position.
[in]End- end of the input string.
Returns
position immediately following the last character of the number.

◆ SkipIdentifier()

template<typename IteratorType>
IteratorType Diligent::Parsing::SkipIdentifier ( const IteratorType & Start,
const IteratorType & End )
noexcept

Skips one identifier starting from the given position.

Parameters
[in]Start- starting position.
[in]End- end of the input string.
Returns
position immediately following the last character of identifier.

◆ SkipLine()

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.

Parameters
[in]Start- starting position.
[in]End- end of the input string.
[in]GoToNextLine- whether to go to the next line.
Returns
If 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.
Remarks
CRLF ending (\r\n) is treated as a single new line separator.

◆ SkipString()

template<typename IteratorType>
bool Diligent::Parsing::SkipString ( const IteratorType & Start,
const IteratorType & End,
const char * Str,
IteratorType & StringEnd )
noexcept

Skips a specified string starting from the given position.

Parameters
[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.
Returns
true if the string has been found, false otherwise.

◆ SplitString()

template<typename IteratorType, typename HandlerType>
void Diligent::Parsing::SplitString ( const IteratorType & Start,
const IteratorType & End,
HandlerType && Handler )

Splits string into chunks separated by comments and delimiters.

Parameters
[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.

◆ Tokenize()

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 )

Tokenizes the given string using the C-language syntax.

Parameters
[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.
Returns
Tokenized representation of the source string
Remarks
In case of a parsing error, the function throws std::runtime_error.