|
tfel
|
class in charge of reading a C++-style file and converting it in tokens. More...
#include </home/runner/work/tfel/tfel/include/TFEL/Utilities/CxxTokenizer.hxx>
Classes | |
| struct | IsValidIdentifierOptions |
| options used to parse an identifier More... | |
Public Types | |
| using | TokensContainer = std::vector<Token> |
| a simple alias | |
| using | const_iterator = TokensContainer::const_iterator |
| a simple alias | |
| using | size_type = TokensContainer::size_type |
| a simple alias | |
Public Member Functions | |
| CxxTokenizer () | |
| default constructor | |
| CxxTokenizer (const CxxTokenizerOptions &) | |
| constructor with options | |
| CxxTokenizer (const std::string &) | |
| constructor from a file | |
| CxxTokenizer (const std::string &, const CxxTokenizerOptions &) | |
| constructor from a file | |
| template<typename InputIterator> | |
| void | import (const InputIterator, const InputIterator) |
| import the specified tokens | |
| void | parseString (const std::string &) |
| analyse the string given all previous tokens are erased | |
| void | openFile (const std::string &) |
| import the specified file | |
| void | printFileTokens (std::ostream &) const |
| print the stored tokens to the specified stream | |
| void | setCStyleCommentOpened (const bool) |
| assume that a C-style comment was opened at the beginning of the processing. This is usefull when parsing a file line by line | |
| bool | isCStyleCommentOpened () const |
| void | setRawStringOpened (const bool) |
| assume that a raw string was opened at the beginning of the processing. | |
| void | setRawStringDelimiter (std::string_view) |
| set the raw string deliminter. This assume that a raw string was opened at the beginning of the processing, i.e. sets the rawStringOpened boolean to true. | |
| bool | isRawStringOpened () const |
| std::string | getCurrentRawStringDelimiter () const noexcept |
| void | stripComments () |
| remove all comment tokens | |
| void | clear () |
| clear the object | |
| void | treatCharAsString (const bool) |
| set if char tokens shall be treated as string tokens | |
| void | keepCommentBoundaries (const bool) |
| set if the comment boundaries must be left in the resulting tokens. | |
| void | treatDotAsSeparator (const bool) |
| set if '.' shall be treated as as sperator | |
| void | treatPlusAsSeparator (const bool) |
| set if '+' shall be treated as as sperator | |
| void | treatMinusAsSeparator (const bool) |
| set if '-' shall be treated as as sperator | |
| void | mergeStrings (const bool) |
| set if successive strings shall be merged | |
| void | extractNumbers (const bool) |
| set if tokens shall be split to extract numbers | |
| void | addSeparator (const std::string &) |
| add a new separator | |
| void | substitute (const std::map< std::string, std::string > &) |
| apply the given substitutions by calling replace_all on the values hold by the standard and string tokens. | |
| bool | empty () const |
| const_iterator | begin () const |
| const_iterator | end () const |
| const_iterator | cbegin () const |
| const_iterator | cend () const |
| const Token & | operator[] (const size_type) const |
| size_type | size () const |
| virtual | ~CxxTokenizer () |
| destructor | |
Static Public Member Functions | |
| static bool | isValidIdentifier (std::string_view, const IsValidIdentifierOptions &) |
| static bool | isValidIdentifier (std::string_view, const bool=true) |
| static void | checkNotEndOfLine (const std::string &, const_iterator &, const const_iterator) |
| an helper method throwing an exception if the given iterator is egal to the third argument (which shall point after the last token of the line) | |
| static void | checkNotEndOfLine (const std::string &, const std::string &, const_iterator &, const const_iterator) |
| an helper method throwing an exception if the given iterator is egal to the fourth argument (which shall point after the last token of the line) | |
| static void | readSpecifiedToken (const std::string &, const std::string &, const_iterator &, const const_iterator) |
| an helper method throwing an exception if the given iterator is egal to the fourth argument (which shall point after the last token of the line) or if the given token is not equal to the expected value | |
| static unsigned int | readUnsignedInt (const_iterator &, const const_iterator) |
| an helper method to extract an unsigned int from the given token | |
| static std::string | readString (const_iterator &, const const_iterator) |
| an helper method to extract a string from the given token | |
| static std::vector< std::string > | readArray (const std::string &, const_iterator &, const const_iterator) |
| an helper method to extract a set of values from the given as an array | |
| static void | readArray (const std::string &, std::vector< std::string > &, const_iterator &, const const_iterator) |
| an helper method to extract a set of values from the given as an array | |
| static std::vector< Token > | readList (const std::string &, const std::string &, const std::string &, const_iterator &, const const_iterator) |
| extract a list of token | |
| static void | readList (std::vector< Token > &, const std::string &, const std::string &, const std::string &, const_iterator &, const const_iterator) |
| extract a list of token | |
| static std::vector< std::string > | readStringArray (const_iterator &, const const_iterator) |
| an helper method to extract a set of values from the given as an array | |
| static double | readDouble (const_iterator &, const const_iterator) |
| an helper method to extract a double from the given token | |
| static int | readInt (const_iterator &, const const_iterator) |
| an helper method to extract a int from the given token | |
Protected Member Functions | |
| virtual void | parseStream (std::istream &, Token::size_type &, const std::string &) |
| tokenize the lines of a stream | |
| virtual void | splitLine (const std::string &, const Token::size_type) |
| virtual void | parseChar (Token::size_type &, std::string::const_iterator &, const std::string::const_iterator, const Token::size_type) |
| virtual void | parseString (Token::size_type &, std::string::const_iterator &, const std::string::const_iterator, const Token::size_type, const char) |
| virtual void | parseCComment (Token::size_type &, std::string::const_iterator &, const std::string::const_iterator, const Token::size_type) |
| virtual void | parseCxxComment (Token::size_type &, std::string::const_iterator &, const std::string::const_iterator, const Token::size_type) |
| virtual void | try_join (Token::size_type &, std::string::const_iterator &, const std::string::const_iterator, const Token::size_type, const char) |
| virtual void | try_join (Token::size_type &, std::string::const_iterator &, const std::string::const_iterator, const Token::size_type, const char, const char) |
| virtual void | parseNumber (Token::size_type &, std::string::const_iterator &, const std::string::const_iterator, const Token::size_type) |
| read a number | |
| virtual void | parsePreprocessorDirective (Token::size_type &, std::string::const_iterator &, const std::string::const_iterator, const std::string::const_iterator, const Token::size_type) |
| virtual void | parseStandardLine (Token::size_type &, std::string::const_iterator &, const std::string::const_iterator, const std::string::const_iterator, const Token::size_type) |
| virtual void | parseRawString (Token::size_type &, std::string::const_iterator &, const std::string::const_iterator, const Token::size_type, const Token::size_type) |
| read the contents of a raw string literal, which starts at the current iterator, past the opening R"delim( prefix | |
| Protected Member Functions inherited from tfel::utilities::CxxTokenizerOptions | |
| CxxTokenizerOptions () | |
| default constructor | |
| CxxTokenizerOptions (const CxxTokenizerOptions &) | |
| copy constructor | |
| CxxTokenizerOptions (CxxTokenizerOptions &&) noexcept | |
| move constructor | |
| CxxTokenizerOptions & | operator= (const CxxTokenizerOptions &) |
| standard assignement | |
| CxxTokenizerOptions & | operator= (CxxTokenizerOptions &&) noexcept |
| move assignement | |
| ~CxxTokenizerOptions () | |
| destructor | |
Static Protected Member Functions | |
| static bool | isValidFunctionIdentifier (const std::string &, const bool=true) |
Protected Attributes | |
| std::vector< std::string > | additional_separators |
| TokensContainer | tokens |
| list of tokens read | |
| std::map< Token::size_type, std::string > | comments |
| store the comments which have been removed by the stripComments method. The key of the map is the line a comment starts on and the value is the values of the comments of this line, in the order they appear in the line, separated by a space character. | |
| bool | cStyleCommentOpened = false |
| if true, a cStyle comment what opened when the last line treatment was finised | |
| bool | rawStringOpened = false |
| std::string | currentRawStringDelimiter |
| name of the current delimiter for a raw string | |
| Protected Attributes inherited from tfel::utilities::CxxTokenizerOptions | |
| bool | bKeepCommentBoundaries = false |
| if true, keep the comment boundaries | |
| bool | shallMergeStrings = false |
| if true, successive strings are merged | |
| bool | allowStrayHashCharacter = false |
| allow stray hash character | |
| bool | treatHashCharacterAsCommentDelimiter = false |
| the hash character can start a comment, as in python, shell, etc.. | |
| bool | allowStrayBackSlash = false |
| allow stray backslash | |
| bool | treatPreprocessorDirectives = true |
| treat preprocessor directives | |
| bool | treatStrings = true |
| treat strings and characters If false, the ' and '"' are treated as indivdual tokens | |
| bool | treatNumbers = true |
| extract number | |
| bool | treatCComments = true |
| treat comments | |
| bool | treatCxxComments = true |
| treat comments | |
| bool | joinCxxTwoCharactersSeparators = true |
| bool | graveAccentAsSeparator = true |
| strings and characters | |
| bool | charAsString = false |
| bool | dotAsSeparator = true |
| treat . as a separator | |
| bool | plusAsSeparator = true |
| treat + as a separator | |
| bool | minusAsSeparator = true |
| treat - as a separator | |
| bool | addCurlyBraces = false |
| add curly braces at the beginning and at the end of the file | |
class in charge of reading a C++-style file and converting it in tokens.
The value of a number token does not hold the digit separators of the C++14 integer literals: @IterMax 1'000; gives the number token 1000, whose value is therefore not the substring of the line taken at the offset of this token. The offsets of the tokens remain those of the file, whatever the numbers which precede them, so that a source code can be rewritten from the offsets of its tokens, as the MFront interfaces for Abaqus, Aster and CalculiX do.
|
explicit |
constructor with options
| [in] | o | options |
|
explicit |
constructor from a file
| [in] | f | : file name |
| tfel::utilities::CxxTokenizer::CxxTokenizer | ( | const std::string & | , |
| const CxxTokenizerOptions & | ) |
constructor from a file
| [in] | f | : file name |
| [in] | o | options |
| void tfel::utilities::CxxTokenizer::addSeparator | ( | const std::string & | ) |
add a new separator
| [in] | s | separator |
|
nodiscard |
|
nodiscard |
|
nodiscard |
|
static |
an helper method throwing an exception if the given iterator is egal to the fourth argument (which shall point after the last token of the line)
| [in] | method | : calling method name |
| [in] | error | : error message |
| [in] | p | : iterator to the current token |
| [in] | pe | : iterator pointing after the last token of the line |
| std::runtime_error | if the given iterator is egal to the fourth argument |
|
static |
an helper method throwing an exception if the given iterator is egal to the third argument (which shall point after the last token of the line)
| [in] | method | : calling method name |
| [in] | p | : iterator to the current token |
| [in] | pe | : iterator pointing after the last token of the line |
| std::runtime_error | if the given iterator is egal to the third argument |
|
nodiscard |
|
nodiscard |
| void tfel::utilities::CxxTokenizer::extractNumbers | ( | const bool | ) |
set if tokens shall be split to extract numbers
| [in] | b | : boolean telling numbers shall be extracted from tokens |
|
nodiscardnoexcept |
| void tfel::utilities::CxxTokenizer::import | ( | const InputIterator | p, |
| const InputIterator | pe ) |
import the specified tokens
| [in] | p | : iterator to the first token |
| [in] | pe | : iterator past the last token |
|
nodiscard |
A C style comment may span several lines: a comment which is not closed on the line where it opens does not make the tokenizer throw. The state of the tokenizer is left open and the lines which follow are gathered in the value of the comment token until the comment is closed, which is what allows tokenizing a file line by line. A file which ends inside a comment is thus accepted, on the contrary to a quoted string or to a character literal which are rejected when they are not closed in their line: the caller is responsible for checking the state of the tokenizer when the end of the file is reached, as this method does. See issue #1395.
|
nodiscard |
The raw string literals behave as the C style comments: a literal which is not closed when the end of the contents is reached does not make the tokenizer throw and leaves the state of the tokenizer open. A file which ends inside a raw string literal is thus accepted: the caller is responsible for checking the state of the tokenizer when the end of the file is reached, as this method does, getCurrentRawStringDelimiter giving the delimiter of the opened literal. See issue #1395.
|
static |
| [in] | s | : variable name |
| [in] | b | : boolean telling if C++ keywords are allowed. |
|
static |
| [in] | s | : variable name |
| [in] | opts | : options |
| void tfel::utilities::CxxTokenizer::keepCommentBoundaries | ( | const bool | ) |
set if the comment boundaries must be left in the resulting tokens.
| [in] | b | : boolean telling if char tokens shall be treated as string tokens |
| void tfel::utilities::CxxTokenizer::mergeStrings | ( | const bool | ) |
set if successive strings shall be merged
| [in] | b | : boolean |
| void tfel::utilities::CxxTokenizer::openFile | ( | const std::string & | ) |
import the specified file
| [in] | f | : file name |
|
protectedvirtual |
read a number
The digit separators of the C++14 integer literals are removed from the value of the token: 1'000 gives the value 1000, see the description of the class.
| [in,out] | o | offset in the line |
| [in,out] | p | iterator to the current character |
| [in] | pe | iterator past the end of the line |
| [in] | n | line number |
|
protectedvirtual |
read the contents of a raw string literal, which starts at the current iterator, past the opening R"delim( prefix
The value of the token holds the whole literal, from the R delimiter to the closing " included, as for a quoted string, and its offset is that of the R, see issue #1390. The opening prefix is restored from the delimiter of the literal, which the caller has read and kept in currentRawStringDelimiter.
| [in,out] | o | current offset in line |
| [in,out] | p | current iterator |
| [in] | pe | iterator past the last character in the line |
| [in] | n | current line number |
| [in] | ro | offset of the R which opens the literal |
|
protectedvirtual |
tokenize the lines of a stream
The lines are tokenized from the current position of the stream and n is increased by the number of lines which have been tokenized. The last line of a stream may not be terminated by an end of line character: such a line is tokenized as the others. On the contrary, an end of line character at the very end of a stream does not introduce an additional empty line, so that tokenizing a stream gives the same tokens as tokenizing its contents line by line, up to the line numbers, which are those of the stream.
| [in] | in | input stream |
| [in,out] | n | line number |
| [in] | from | origin of the stream |
| void tfel::utilities::CxxTokenizer::parseString | ( | const std::string & | ) |
analyse the string given all previous tokens are erased
The line numbers of the tokens of the string start at the first line: tokenizing a file with several calls to this method, a call per line, gives the same tokens as tokenizing its whole contents with a single call, but the lines of those tokens are those of the line the call is given. A caller who wants to keep the line numbering of a file shall use the parseStream method, which takes the current line number as an argument.
| [in] | s | : string to be parsed |
| void tfel::utilities::CxxTokenizer::printFileTokens | ( | std::ostream & | ) | const |
print the stored tokens to the specified stream
| [out] | out | : output stream |
|
static |
an helper method to extract a set of values from the given as an array
| [in] | method | : calling method name |
| [in] | p | : iterator to the current token |
| [in] | pe | : iterator pointing after the last token of the line |
| std::runtime_error | if the given iterator is egal to the second argument or if the given token is not a string |
|
static |
an helper method to extract a set of values from the given as an array
| [in] | method | : calling method name |
| [out] | v | : the extracted array |
| [in] | p | : iterator to the current token |
| [in] | pe | : iterator pointing after the last token of the line |
| std::runtime_error | if the given iterator is egal to the second argument or if the given token is not a string |
|
static |
an helper method to extract a double from the given token
| [in] | p | : iterator to the current token |
| [in] | pe | : iterator pointing after the last token of the line |
| std::runtime_error | if the given iterator is egal to the second argument |
|
static |
an helper method to extract a int from the given token
| [in] | p | : iterator to the current token |
| [in] | pe | : iterator pointing after the last token of the line |
| std::runtime_error | if the given iterator is egal to the second argument |
|
static |
extract a list of token
| [in] | m | : calling method name (used for error message) |
| [in] | db | : beginning delimiter |
| [in] | de | : end delimiter |
| [in] | p | : iterator to the current token |
| [in] | pe | : iterator pointing after the last token of the line |
|
static |
extract a list of token
| [out] | l | : list read |
| [in] | m | : calling method name (used for error message) |
| [in] | db | : beginning delimiter |
| [in] | de | : end delimiter |
| [in] | p | : iterator to the current token |
| [in] | pe | : iterator pointing after the last token of the line |
|
static |
an helper method throwing an exception if the given iterator is egal to the fourth argument (which shall point after the last token of the line) or if the given token is not equal to the expected value
| [in] | method | : calling method name |
| [in] | value | : expected value |
| [in] | p | : iterator to the current token |
| [in] | pe | : iterator pointing after the last token of the line |
| std::runtime_error | if the given iterator is egal to the fourth argument |
|
static |
an helper method to extract a string from the given token
| [in] | p | : iterator to the current token |
| [in] | pe | : iterator pointing after the last token of the line |
| std::runtime_error | if the given iterator is egal to the second argument or if the given token is not a string |
|
static |
an helper method to extract a set of values from the given as an array
| [out] | v | : the extracted array |
| [in] | p | : iterator to the current token |
| [in] | pe | : iterator pointing after the last token of the line |
| std::runtime_error | if the given iterator is egal to the second argument or if the given token is not a string |
|
static |
an helper method to extract an unsigned int from the given token
| [in] | p | : iterator to the current token |
| [in] | pe | : iterator pointing after the last token of the line |
| std::runtime_error | if the given iterator is egal to the second argument |
| void tfel::utilities::CxxTokenizer::setCStyleCommentOpened | ( | const bool | ) |
assume that a C-style comment was opened at the beginning of the processing. This is usefull when parsing a file line by line
| [in] | b | boolean |
| void tfel::utilities::CxxTokenizer::setRawStringDelimiter | ( | std::string_view | ) |
set the raw string deliminter. This assume that a raw string was opened at the beginning of the processing, i.e. sets the rawStringOpened boolean to true.
| [in] | d | new delimiter |
| void tfel::utilities::CxxTokenizer::setRawStringOpened | ( | const bool | ) |
assume that a raw string was opened at the beginning of the processing.
This is usefull when parsing a file line by line
| [in] | b | boolean |
|
nodiscard |
|
protectedvirtual |
| [in] | l | line |
| [in] | n | line number |
| [in] | from | origin of the stream |
| void tfel::utilities::CxxTokenizer::stripComments | ( | ) |
remove all comment tokens
The values of the removed comments of the kind Token::Comment are gathered in the comments data member: a line may hold several such comments, which are gathered in the order they appear in the line, separated by a space character.
| void tfel::utilities::CxxTokenizer::substitute | ( | const std::map< std::string, std::string > & | ) |
apply the given substitutions by calling replace_all on the values hold by the standard and string tokens.
| [in] | substitutions | list of substitutions |
| void tfel::utilities::CxxTokenizer::treatCharAsString | ( | const bool | ) |
set if char tokens shall be treated as string tokens
| [in] | b | : boolean telling if char tokens shall be treated as string tokens |
| void tfel::utilities::CxxTokenizer::treatDotAsSeparator | ( | const bool | ) |
set if '.' shall be treated as as sperator
| [in] | b | : boolean telling if '.' shall be treated as as sperator |
| void tfel::utilities::CxxTokenizer::treatMinusAsSeparator | ( | const bool | ) |
set if '-' shall be treated as as sperator
| [in] | b | : boolean telling if '-' shall be treated as as separator |
| void tfel::utilities::CxxTokenizer::treatPlusAsSeparator | ( | const bool | ) |
set if '+' shall be treated as as sperator
| [in] | b | : boolean telling if '+' shall be treated as as separator |
|
protected |
store the comments which have been removed by the stripComments method. The key of the map is the line a comment starts on and the value is the values of the comments of this line, in the order they appear in the line, separated by a space character.
The empty comments, that is a // comment or a C style comment whose contents is empty, bring nothing and are then not stored: a line whose only comment is empty gives no entry in this map.
|
protected |
if true, a cStyle comment what opened when the last line treatment was finised