|
TuringSim
C++ framework to simulate abstract computing models
|
3 #include <state/mConfiguration/mConfiguration.h>
38 typedef std::tuple<TokenKind, size_t, size_t>
Token;
58 MConfigurationParser(
char left,
char right,
char separator, std::set<char> ignore,
const std::string& variablePrefix);
70 std::shared_ptr<const MConfiguration<std::string>>
parse(
const std::string& mConfiguration)
const;
77 std::string
sub(
const std::string& mConfiguration,
const Token& token)
const;
84 Token tokenize(
const std::string& mConfiguration,
size_t start)
const;
90 std::vector<Token>
tokenize(
const std::string& mConfiguration)
const;
105 void translateKeywords(
const std::string& mConfiguration, std::vector<Token>& tokens)
const;
113 std::pair<bool, std::string>
isVariableNode(
const std::string& node)
const;
122 std::pair<std::shared_ptr<const MConfiguration<std::string>>,
size_t>
parse_tokens_m_func(
const std::string& mConfiguration,
const std::vector<Token>& tokens,
size_t start)
const;
131 std::pair<std::vector<std::shared_ptr<const MConfiguration<std::string>>>,
size_t>
parse_tokens_m_func_list(
const std::string& mConfiguration,
const std::vector<Token>& tokens,
size_t start)
const;
140 template<
typename CharT,
typename Traits>
141 friend std::basic_ostream<CharT, Traits>&
operator<< (std::basic_ostream<CharT, Traits>& os,
const TokenKind& tokenKind) {
145 os <<
"IDENT";
break;
149 os <<
"RIGHT";
break;
151 os <<
"SEPARATOR";
break;
153 os <<
"BLANK";
break;
167 template<
typename CharT,
typename Traits>
168 friend std::basic_ostream<CharT, Traits>&
operator<< (std::basic_ostream<CharT, Traits>& os,
const Token& token) {
169 using Utils::Debug::operator<<;
176 std::set<char> ignore{
'\n',
' ',
'\t'};
177 std::string variablePrefix{
"_"};
207 using Utils::Debug::operator<<;
208 std::ostringstream ss;
209 ss << this->
msg << std::endl;
213 ss <<
" toParse: " <<
toParse << std::endl;
215 ss <<
" position: " <<
position << std::endl;
217 ss <<
" position at the end" << std::endl;
220 ss <<
" position after the end" << std::endl;
238 std::vector<MConfigurationParser::Token>
tokens;
273 using Utils::Debug::operator<<;
274 std::ostringstream ss;
275 ss << this->
msg << std::endl;
276 ss <<
" left: " <<
left << std::endl;
277 ss <<
" right: " <<
right << std::endl;
278 ss <<
" separator: " <<
separator << std::endl;
TokenKind
Category of tokens.
MessageException()=delete
a message exception must have an explicative message.
void translateKeywords(const std::string &mConfiguration, std::vector< Token > &tokens) const
Translate a full sequence of tokens.
void checkInitialization() const
Check if parameters of the parser are consistent. This function is called by constructors.
MConfigurationParser()
Builds MConfigurationParser with default parameters.
std::string msg
The error message.
@ LEFT
The left parenthesis.
The Base class for all custom exceptions.
@ EOS
The token at the end of the string.
size_t position
Where the error happened.
Exception launched when we try to parse a string with a syntax error as a MConfiguration<std::string>...
MConfigurationParserBadInitializationException(std::string message, char left, char right, char separator, std::set< char > ignore, const std::string &variablePrefix)
Builds a MConfigurationParserBadInitializationException.
std::string additionalMessage
An optional additional message, depending on the context.
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const TokenKind &tokenKind)
Debug printer of TokenKind.
virtual std::string makeFullMessage() const override
Build the full error message. It should be overridden by derived class that adds data to the exceptio...
std::optional< Token > translateKeyword(const std::string &mConfiguration, const Token &token) const
Translate a single token by recognizing keyword.
Parse a m-configuration written as a std::string into a MConfiguration<std::string>.
@ BLANK
The keyword "blank" for blank symbol.
std::pair< std::vector< std::shared_ptr< const MConfiguration< std::string > > >, size_t > parse_tokens_m_func_list(const std::string &mConfiguration, const std::vector< Token > &tokens, size_t start) const
Parse a SEPARATOR-separated list of m-configuration starting at position start.
char right
The closing delimiter.
char separator
The separator between terms in lists.
std::pair< std::shared_ptr< const MConfiguration< std::string > >, size_t > parse_tokens_m_func(const std::string &mConfiguration, const std::vector< Token > &tokens, size_t start) const
Parse a m-configuration starting at position start.
@ RIGHT
The right parenthesis.
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 vari...
std::tuple< TokenKind, size_t, size_t > Token
Type of tokens: the first component is the category, the second is the starting character,...
Token tokenize(const std::string &mConfiguration, size_t start) const
Lex a single token.
char left
The opening delimiter.
std::string variablePrefix
the prefix used to mark variable nodes.
std::vector< MConfigurationParser::Token > tokens
The list of tokens we parse.
std::string toParse
The input string.
@ SEPARATOR
Separator of terms.
virtual std::string makeFullMessage() const override
Build the full error message. It should be overridden by derived class that adds data to the exceptio...
MConfigurationParserSyntaxErrorException()=delete
Deleted default constructor: a MConfigurationParserSyntaxErrorException must contain interesting info...
Exception thrown when the special characters of the parser are not consistent.
MConfigurationParserBadInitializationException()=delete
Deleted default constructor: a MConfigurationParserBadInitializationException must contain interestin...
std::function< std::basic_ostream< CharT, Traits > &(std::basic_ostream< CharT, Traits > &)> debug(const T &s)
Generic debug printing function.
std::string sub(const std::string &mConfiguration, const Token &token) const
Get the text of a token.
MConfigurationParserSyntaxErrorException(std::string message, std::string toParse, const std::vector< MConfigurationParser::Token > &tokens, size_t position, std::string additionalMessage="")
Builds a MConfigurationParserSyntaxErrorException.
The namespace of everything about m-configurations.
std::shared_ptr< const MConfiguration< std::string > > parse(const std::string &mConfiguration) const
Parse a string into a m-configuration. The usual entry point.
std::set< char > ignore
Set of characters to ignore.