TuringSim
C++ framework to simulate abstract computing models
Public Types | Public Member Functions | Friends | List of all members
TuringSim::Symbol::TuringStyleMixedSymbolPatternParser Class Reference

A parser of mixed symbol pattern where symbols are std::string More...

#include <symbol/turingStyleMixedSymbolPatternParser.h>

Public Types

enum  TokenKind {
  TokenKind::SEPARATOR, TokenKind::LEFT, TokenKind::RIGHT, TokenKind::IDENT,
  TokenKind::NONE, TokenKind::NON, TokenKind::ANY, TokenKind::TRUE,
  TokenKind::EOS
}
 Category of tokens. More...
 
typedef std::tuple< TokenKind, size_t, size_t > Token
 Type of tokens: the first component is the category, the second is the starting character, and the third is the last character (included).
 

Public Member Functions

 TuringStyleMixedSymbolPatternParser ()
 Builds TuringStyleMixedSymbolPatternParser with default parameters. More...
 
 TuringStyleMixedSymbolPatternParser (char left, char right, char separator, std::string blank, std::set< char > ignore, std::string variablePrefix, std::string escapePrefix)
 Builds TuringStyleMixedSymbolPatternParser with custom parameters. More...
 
void checkInitialization () const
 Check if parameters of the parser are consistent. This function is called by constructors. More...
 
SymbolPattern parse (const std::string &pattern) const
 Parse a string into a symbol pattern. The usual entry point. More...
 
std::pair< bool, std::string > isVariableNode (const std::string &node) const
 Test is a raw node name is a variable name. A node name is a variable name if it starts with the variable name prefix. More...
 
std::pair< SymbolPattern, size_t > parse_negative_pattern (const std::string &pattern, const std::vector< Token > &tokens, size_t position) const
 Parse a negative list of symbols. More...
 
std::pair< SymbolPattern, size_t > parse_positive_pattern (const std::string &pattern, const std::vector< Token > &tokens, size_t position) const
 Parse a positive list of symbols. More...
 
std::string sub (const std::string &pattern, const TuringStyleMixedSymbolPatternParser::Token &token) const
 Get the text of a token. More...
 
std::vector< Tokentokenize (const std::string &pattern) const
 Fully lex a string, without recognizing keyword. More...
 
Token tokenize (const std::string &pattern, size_t start) const
 Lex a single token. More...
 
std::optional< TokentranslateKeyword (const std::string &pattern, const TuringStyleMixedSymbolPatternParser::Token &token) const
 Translate a single token by recognizing keyword. More...
 
void translateKeywords (const std::string &pattern, std::vector< Token > &tokens) const
 Translate a full sequence of tokens. More...
 

Friends

template<typename CharT , typename Traits = std::char_traits <CharT>>
std::basic_ostream< CharT, Traits > & operator<< (std::basic_ostream< CharT, Traits > &os, const TokenKind &tokenKind)
 Debug printer of TokenKind. More...
 
template<typename CharT , typename Traits >
std::basic_ostream< CharT, Traits > & operator<< (std::basic_ostream< CharT, Traits > &os, const Token &token)
 Debug printer of Token. More...
 

Detailed Description

A parser of mixed symbol pattern where symbols are std::string

We use a Turing-style syntax:
pattern ::=
| "empty"
| letter{"," letter}*
| "non("letter{"," letter}*")"
| "any" | "true"
For instance (spaces and tab are not significant):

To specify a symbol named as a keyword, it should be escaped:

All symbols can be specified when instantiating the parser, as long as there is no conflict.

Definition at line 35 of file turingStyleMixedSymbolPatternParser.h.

Member Enumeration Documentation

◆ TokenKind

Category of tokens.

Enumerator
SEPARATOR 

Separator of symbols, usually, the comma.

LEFT 

Left parenthesis.

RIGHT 

Right parenthesis.

IDENT 

Symbols.

NONE 

"none" keyword.

NON 

"non" keyword.

ANY 

"any" keyword.

TRUE 

"true" keyword.

EOS 

The token at the end of the string.

Definition at line 41 of file turingStyleMixedSymbolPatternParser.h.

Constructor & Destructor Documentation

◆ TuringStyleMixedSymbolPatternParser() [1/2]

TuringSim::Symbol::TuringStyleMixedSymbolPatternParser::TuringStyleMixedSymbolPatternParser ( )

Builds TuringStyleMixedSymbolPatternParser with default parameters.

We have:

  • left = '('
  • right = ')'
  • separator = ','
  • blank = ""
  • ignore = {' ', '\n'}
  • variablePrefix = "_"
  • escapePrefix = "\"

Definition at line 61 of file turingStyleMixedSymbolPatternParser.cpp.

Here is the call graph for this function:

◆ TuringStyleMixedSymbolPatternParser() [2/2]

TuringSim::Symbol::TuringStyleMixedSymbolPatternParser::TuringStyleMixedSymbolPatternParser ( char  left,
char  right,
char  separator,
std::string  blank,
std::set< char >  ignore,
std::string  variablePrefix,
std::string  escapePrefix 
)

Builds TuringStyleMixedSymbolPatternParser with custom parameters.

Parameters
[in]leftthe left parenthesis.
[in]rightthe right parenthesis.
[in]separatorthe separators of symbols.
[in]blankthe blank symbol, represented by "blank" keyword.
[in]ignoreset of characters to ignore.
[in]variablePrefixThe prefix of names that denotes keys. The prefix is removed from the name.
[in]escapePrefixThe prefix to add before an identifier to force it to be recognize as an identifier, even if it is also a keyword.

Definition at line 65 of file turingStyleMixedSymbolPatternParser.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ checkInitialization()

void TuringSim::Symbol::TuringStyleMixedSymbolPatternParser::checkInitialization ( ) const

Check if parameters of the parser are consistent. This function is called by constructors.

Exceptions
TuringStyleMixedSymbolPatternParserBadInitializationExceptionif parameters are conflicting.

Definition at line 5 of file turingStyleMixedSymbolPatternParser.cpp.

Here is the caller graph for this function:

◆ isVariableNode()

std::pair< bool, std::string > TuringSim::Symbol::TuringStyleMixedSymbolPatternParser::isVariableNode ( const std::string &  node) const

Test is a raw node name is a variable name. A node name is a variable name if it starts with the variable name prefix.

Parameters
[in]nodethe name of the node.
Returns
a pair. If node is not a variable name, the returned pair is (false, node), otherwise, it is (true, variableName) where variable name is obtained from node by removing the variable name prefix.

Definition at line 112 of file turingStyleMixedSymbolPatternParser.cpp.

Here is the caller graph for this function:

◆ parse()

TuringStyleMixedSymbolPatternParser::SymbolPattern TuringSim::Symbol::TuringStyleMixedSymbolPatternParser::parse ( const std::string &  pattern) const

Parse a string into a symbol pattern. The usual entry point.

Parameters
[in]patternthe string to parse.
Returns
The parsed pattern.
Exceptions
TuringStyleMixedSymbolPatternSyntaxErrorExceptionif there is a syntax error.

Definition at line 70 of file turingStyleMixedSymbolPatternParser.cpp.

Here is the call graph for this function:

◆ parse_negative_pattern()

std::pair< TuringStyleMixedSymbolPatternParser::SymbolPattern, size_t > TuringSim::Symbol::TuringStyleMixedSymbolPatternParser::parse_negative_pattern ( const std::string &  pattern,
const std::vector< Token > &  tokens,
size_t  position 
) const

Parse a negative list of symbols.

Parameters
[in]patternthe full string to parse.
[in]tokensthe sequence of tokens.
[in]positionthe position in tokens to start parsing.
Returns
The parsed pattern and the new position.
Exceptions
TuringStyleMixedSymbolPatternSyntaxErrorExceptionif there is a syntax error.

Definition at line 124 of file turingStyleMixedSymbolPatternParser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_positive_pattern()

std::pair< TuringStyleMixedSymbolPatternParser::SymbolPattern, size_t > TuringSim::Symbol::TuringStyleMixedSymbolPatternParser::parse_positive_pattern ( const std::string &  pattern,
const std::vector< Token > &  tokens,
size_t  position 
) const

Parse a positive list of symbols.

Parameters
[in]patternthe full string to parse.
[in]tokensthe sequence of tokens.
[in]positionthe position in tokens to start parsing.
Returns
The parsed pattern and the new position.
Exceptions
TuringStyleMixedSymbolPatternSyntaxErrorExceptionif there is a syntax error.

Definition at line 197 of file turingStyleMixedSymbolPatternParser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sub()

std::string TuringSim::Symbol::TuringStyleMixedSymbolPatternParser::sub ( const std::string &  pattern,
const TuringStyleMixedSymbolPatternParser::Token token 
) const

Get the text of a token.

Parameters
[in]patternthe full string.
[in]tokenthe token we want the string.
Returns
the text of the token.

Definition at line 262 of file turingStyleMixedSymbolPatternParser.cpp.

Here is the caller graph for this function:

◆ tokenize() [1/2]

std::vector< TuringStyleMixedSymbolPatternParser::Token > TuringSim::Symbol::TuringStyleMixedSymbolPatternParser::tokenize ( const std::string &  pattern) const

Fully lex a string, without recognizing keyword.

Parameters
[in]patternthe string to parse.
Returns
the vector of raw tokens.

Definition at line 269 of file turingStyleMixedSymbolPatternParser.cpp.

Here is the caller graph for this function:

◆ tokenize() [2/2]

TuringStyleMixedSymbolPatternParser::Token TuringSim::Symbol::TuringStyleMixedSymbolPatternParser::tokenize ( const std::string &  pattern,
size_t  start 
) const

Lex a single token.

Parameters
[in]patternthe full string to parse.
[in]startthe starting position.
Returns
the token parsed from position start.

Definition at line 282 of file turingStyleMixedSymbolPatternParser.cpp.

◆ translateKeyword()

std::optional< TuringStyleMixedSymbolPatternParser::Token > TuringSim::Symbol::TuringStyleMixedSymbolPatternParser::translateKeyword ( const std::string &  pattern,
const TuringStyleMixedSymbolPatternParser::Token token 
) const

Translate a single token by recognizing keyword.

Parameters
[in]patternthe full string to parse.
[in]tokenthe token to translate.
Returns
an empty option if no translation is required, the new token otherwise.

This function translate IDENT token into dedicated keyword tokens.

Definition at line 298 of file turingStyleMixedSymbolPatternParser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ translateKeywords()

void TuringSim::Symbol::TuringStyleMixedSymbolPatternParser::translateKeywords ( const std::string &  pattern,
std::vector< Token > &  tokens 
) const

Translate a full sequence of tokens.

Parameters
[in]patternthe full string to parse.
[in,out]tokensthe list of tokens, modified in-place.

Definition at line 318 of file turingStyleMixedSymbolPatternParser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator<< [1/2]

template<typename CharT , typename Traits >
std::basic_ostream<CharT, Traits>& operator<< ( std::basic_ostream< CharT, Traits > &  os,
const Token token 
)
friend

Debug printer of Token.

Template Parameters
CharTthe char type.
Traitsstd::basic_ostream trait.
Parameters
[in,out]osthe output stream
[in]tokenthe token to print.
Returns
the updated stream

Definition at line 204 of file turingStyleMixedSymbolPatternParser.h.

◆ operator<< [2/2]

template<typename CharT , typename Traits = std::char_traits <CharT>>
std::basic_ostream<CharT, Traits>& operator<< ( std::basic_ostream< CharT, Traits > &  os,
const TokenKind tokenKind 
)
friend

Debug printer of TokenKind.

Template Parameters
CharTthe char type.
Traitsstd::basic_ostream trait.
Parameters
[in,out]osthe output stream
[in]tokenKindthe token kind to print.
Returns
the updated stream

Definition at line 163 of file turingStyleMixedSymbolPatternParser.h.


The documentation for this class was generated from the following files: