![]() |
Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
|
General lexical analysis. This is decoupled from CFGrammars and Parsing.

Classes | |
| class | elementa::parsing::LexerError |
| Base error class. Lexer errors should derive from this. More... | |
| class | elementa::parsing::UnexpSymbol |
| class | elementa::parsing::ExpSymbol |
| class | elementa::parsing::UnexpEnd |
| class | elementa::parsing::InvLexElem |
| class | elementa::parsing::Token |
| Base class for any token (i.e., lexical element) produced by a lexer. More... | |
| class | elementa::parsing::TextToken |
| A token that only consists in its textual value. More... | |
| class | elementa::parsing::Lexer |
| A lexer that produces terminals. More... | |
| class | elementa::parsing::FSMLexer |
| A lexical analyzer that uses a number of FSMs to recognize tokens. More... | |
| class | elementa::parsing::LexerCollector |
| A class to use a lexer for collecting all tokens sequentially. More... | |
Typedefs | |
| using | elementa::parsing::TokenId = int |
| A unique identifier for a token. More... | |
| class elementa::parsing::LexerError |


Public Types | |
Types, consts., etc. | |
| using | StdBase = std::runtime_error |
Public Member Functions | |
| LexerError (const std::string &expl) | |
Launching methods | |
| virtual Exc & | asERR (const std::string &context) noexcept |
| Transform it to be thrown as ERR with the given context. More... | |
| virtual Exc & | asEXC (const std::string &place, const RTTextWithEnum::Combination &flags={RTTextWith::kAll_}) noexcept |
| Transform it to be thrown as EXC with the given place and flags. More... | |
Catching methods | |
| const char * | what (void) const noexcept override |
| Return the complete message. It will live as long as this exception. More... | |
| const char * | explanation (void) const noexcept |
| Return the explanation only. It will live as long as this exception. More... | |
| const char * | context (void) const noexcept |
| Return the place (EXC) or context (ERR) only. Will live as long as this. More... | |
|
inherited |
Definition at line 119 of file exceptions.h.
|
inline |
|
virtualnoexceptinherited |
Transform it to be thrown as ERR with the given context.
Reimplemented in elementa::base::unimplemented, elementa::base::todo, elementa::base::notreach, elementa::base::invstate, elementa::base::internal_error, elementa::base::notfound, elementa::base::invarg, and elementa::base::outofrange.
|
virtualnoexceptinherited |
Transform it to be thrown as EXC with the given place and flags.
The flags are explained in debugging.h, runtime_src_place() function.
Referenced by elementa::base::Szer_Char::deser(), and elementa::base::Szer_NatBin< NatType >::deser().
|
inlineoverridenoexceptinherited |
Return the complete message. It will live as long as this exception.
The message will be one-line except if the explanation or place/context contain some CR.
Definition at line 161 of file exceptions.h.
|
inlinenoexceptinherited |
Return the explanation only. It will live as long as this exception.
Definition at line 164 of file exceptions.h.
|
inlinenoexceptinherited |
Return the place (EXC) or context (ERR) only. Will live as long as this.
Definition at line 167 of file exceptions.h.
| class elementa::parsing::UnexpSymbol |


Public Types | |
Types, consts., etc. | |
| using | StdBase = std::runtime_error |
Public Member Functions | |
| UnexpSymbol (const std::string &details) | |
Launching methods | |
| virtual Exc & | asERR (const std::string &context) noexcept |
| Transform it to be thrown as ERR with the given context. More... | |
| virtual Exc & | asEXC (const std::string &place, const RTTextWithEnum::Combination &flags={RTTextWith::kAll_}) noexcept |
| Transform it to be thrown as EXC with the given place and flags. More... | |
Catching methods | |
| const char * | what (void) const noexcept override |
| Return the complete message. It will live as long as this exception. More... | |
| const char * | explanation (void) const noexcept |
| Return the explanation only. It will live as long as this exception. More... | |
| const char * | context (void) const noexcept |
| Return the place (EXC) or context (ERR) only. Will live as long as this. More... | |
|
inherited |
Definition at line 119 of file exceptions.h.
|
inline |
|
virtualnoexceptinherited |
Transform it to be thrown as ERR with the given context.
Reimplemented in elementa::base::unimplemented, elementa::base::todo, elementa::base::notreach, elementa::base::invstate, elementa::base::internal_error, elementa::base::notfound, elementa::base::invarg, and elementa::base::outofrange.
|
virtualnoexceptinherited |
Transform it to be thrown as EXC with the given place and flags.
The flags are explained in debugging.h, runtime_src_place() function.
Referenced by elementa::base::Szer_Char::deser(), and elementa::base::Szer_NatBin< NatType >::deser().
|
inlineoverridenoexceptinherited |
Return the complete message. It will live as long as this exception.
The message will be one-line except if the explanation or place/context contain some CR.
Definition at line 161 of file exceptions.h.
|
inlinenoexceptinherited |
Return the explanation only. It will live as long as this exception.
Definition at line 164 of file exceptions.h.
|
inlinenoexceptinherited |
Return the place (EXC) or context (ERR) only. Will live as long as this.
Definition at line 167 of file exceptions.h.
| class elementa::parsing::ExpSymbol |


Public Types | |
Types, consts., etc. | |
| using | StdBase = std::runtime_error |
Public Member Functions | |
| ExpSymbol (const std::string &details) | |
Launching methods | |
| virtual Exc & | asERR (const std::string &context) noexcept |
| Transform it to be thrown as ERR with the given context. More... | |
| virtual Exc & | asEXC (const std::string &place, const RTTextWithEnum::Combination &flags={RTTextWith::kAll_}) noexcept |
| Transform it to be thrown as EXC with the given place and flags. More... | |
Catching methods | |
| const char * | what (void) const noexcept override |
| Return the complete message. It will live as long as this exception. More... | |
| const char * | explanation (void) const noexcept |
| Return the explanation only. It will live as long as this exception. More... | |
| const char * | context (void) const noexcept |
| Return the place (EXC) or context (ERR) only. Will live as long as this. More... | |
|
inherited |
Definition at line 119 of file exceptions.h.
|
inline |
|
virtualnoexceptinherited |
Transform it to be thrown as ERR with the given context.
Reimplemented in elementa::base::unimplemented, elementa::base::todo, elementa::base::notreach, elementa::base::invstate, elementa::base::internal_error, elementa::base::notfound, elementa::base::invarg, and elementa::base::outofrange.
|
virtualnoexceptinherited |
Transform it to be thrown as EXC with the given place and flags.
The flags are explained in debugging.h, runtime_src_place() function.
Referenced by elementa::base::Szer_Char::deser(), and elementa::base::Szer_NatBin< NatType >::deser().
|
inlineoverridenoexceptinherited |
Return the complete message. It will live as long as this exception.
The message will be one-line except if the explanation or place/context contain some CR.
Definition at line 161 of file exceptions.h.
|
inlinenoexceptinherited |
Return the explanation only. It will live as long as this exception.
Definition at line 164 of file exceptions.h.
|
inlinenoexceptinherited |
Return the place (EXC) or context (ERR) only. Will live as long as this.
Definition at line 167 of file exceptions.h.
| class elementa::parsing::UnexpEnd |


Public Types | |
Types, consts., etc. | |
| using | StdBase = std::runtime_error |
Public Member Functions | |
| UnexpEnd (const std::string &details) | |
Launching methods | |
| virtual Exc & | asERR (const std::string &context) noexcept |
| Transform it to be thrown as ERR with the given context. More... | |
| virtual Exc & | asEXC (const std::string &place, const RTTextWithEnum::Combination &flags={RTTextWith::kAll_}) noexcept |
| Transform it to be thrown as EXC with the given place and flags. More... | |
Catching methods | |
| const char * | what (void) const noexcept override |
| Return the complete message. It will live as long as this exception. More... | |
| const char * | explanation (void) const noexcept |
| Return the explanation only. It will live as long as this exception. More... | |
| const char * | context (void) const noexcept |
| Return the place (EXC) or context (ERR) only. Will live as long as this. More... | |
|
inherited |
Definition at line 119 of file exceptions.h.
|
inline |
|
virtualnoexceptinherited |
Transform it to be thrown as ERR with the given context.
Reimplemented in elementa::base::unimplemented, elementa::base::todo, elementa::base::notreach, elementa::base::invstate, elementa::base::internal_error, elementa::base::notfound, elementa::base::invarg, and elementa::base::outofrange.
|
virtualnoexceptinherited |
Transform it to be thrown as EXC with the given place and flags.
The flags are explained in debugging.h, runtime_src_place() function.
Referenced by elementa::base::Szer_Char::deser(), and elementa::base::Szer_NatBin< NatType >::deser().
|
inlineoverridenoexceptinherited |
Return the complete message. It will live as long as this exception.
The message will be one-line except if the explanation or place/context contain some CR.
Definition at line 161 of file exceptions.h.
|
inlinenoexceptinherited |
Return the explanation only. It will live as long as this exception.
Definition at line 164 of file exceptions.h.
|
inlinenoexceptinherited |
Return the place (EXC) or context (ERR) only. Will live as long as this.
Definition at line 167 of file exceptions.h.
| class elementa::parsing::InvLexElem |


Public Types | |
Types, consts., etc. | |
| using | StdBase = std::runtime_error |
Public Member Functions | |
| InvLexElem (const std::string &details) | |
Launching methods | |
| virtual Exc & | asERR (const std::string &context) noexcept |
| Transform it to be thrown as ERR with the given context. More... | |
| virtual Exc & | asEXC (const std::string &place, const RTTextWithEnum::Combination &flags={RTTextWith::kAll_}) noexcept |
| Transform it to be thrown as EXC with the given place and flags. More... | |
Catching methods | |
| const char * | what (void) const noexcept override |
| Return the complete message. It will live as long as this exception. More... | |
| const char * | explanation (void) const noexcept |
| Return the explanation only. It will live as long as this exception. More... | |
| const char * | context (void) const noexcept |
| Return the place (EXC) or context (ERR) only. Will live as long as this. More... | |
|
inherited |
Definition at line 119 of file exceptions.h.
|
inline |
|
virtualnoexceptinherited |
Transform it to be thrown as ERR with the given context.
Reimplemented in elementa::base::unimplemented, elementa::base::todo, elementa::base::notreach, elementa::base::invstate, elementa::base::internal_error, elementa::base::notfound, elementa::base::invarg, and elementa::base::outofrange.
|
virtualnoexceptinherited |
Transform it to be thrown as EXC with the given place and flags.
The flags are explained in debugging.h, runtime_src_place() function.
Referenced by elementa::base::Szer_Char::deser(), and elementa::base::Szer_NatBin< NatType >::deser().
|
inlineoverridenoexceptinherited |
Return the complete message. It will live as long as this exception.
The message will be one-line except if the explanation or place/context contain some CR.
Definition at line 161 of file exceptions.h.
|
inlinenoexceptinherited |
Return the explanation only. It will live as long as this exception.
Definition at line 164 of file exceptions.h.
|
inlinenoexceptinherited |
Return the place (EXC) or context (ERR) only. Will live as long as this.
Definition at line 167 of file exceptions.h.
| class elementa::parsing::Token |
Base class for any token (i.e., lexical element) produced by a lexer.
Any token class must derive from this and, at least, implement the value() method (it may be calculated from other info).

Public Types | |
| using | Ptr = std::shared_ptr< Token > |
| Safe pointer to a token. More... | |
| using | PtrVector = std::vector< Ptr > |
| A vector of token pointers. More... | |
Public Attributes | |
| TokenId | c |
| Id of the element, e.g., in a grammar. More... | |
Public Member Functions | |
| Token (TokenId tid) | |
| Token (const Token &)=default | |
| Token (Token &&)=default | |
| Token & | operator= (const Token &)=default |
| Token & | operator= (Token &&)=default |
| virtual bool | operator== (const Token &) const =0 |
| virtual bool | operator!= (const Token &o) const |
| virtual const std::string & | value (void) const =0 |
| Must return a reference to the textual value for the token. More... | |
| virtual void | setValue (const std::string &v)=0 |
| Must change the textual value of the token by V. More... | |
| virtual std::string | to_string (void) const |
| Return a string representation of the token (can be overriden). | |
| using elementa::parsing::Token::Ptr = std::shared_ptr<Token> |
| using elementa::parsing::Token::PtrVector = std::vector<Ptr> |
|
inlinevirtual |
|
pure virtual |
Must return a reference to the textual value for the token.
Implemented in elementa::parsing::TextToken.
|
pure virtual |
Must change the textual value of the token by V.
Implemented in elementa::parsing::TextToken.
| class elementa::parsing::TextToken |


Public Types | |
| using | Ptr = std::shared_ptr< Token > |
| Safe pointer to a token. More... | |
| using | PtrVector = std::vector< Ptr > |
| A vector of token pointers. More... | |
Public Attributes | |
| TokenId | c |
| Id of the element, e.g., in a grammar. More... | |
Public Member Functions | |
| TextToken (TokenId tid, const std::string &val) | |
| TextToken (const TextToken &oth) | |
| TextToken (TextToken &&oth) | |
| TextToken & | operator= (const TextToken &oth) |
| TextToken & | operator= (TextToken &&oth) |
| void | setValue (const std::string &v) |
| Must change the textual value of the token by V. More... | |
| bool | operator== (const Token &o) const |
| const std::string & | value (void) const |
| Must return a reference to the textual value for the token. More... | |
| virtual bool | operator!= (const Token &o) const |
| virtual std::string | to_string (void) const |
| Return a string representation of the token (can be overriden). | |
|
inherited |
|
inherited |
|
inline |
|
inline |
|
inline |
|
inlinevirtual |
Must change the textual value of the token by V.
Implements elementa::parsing::Token.
|
inlinevirtual |
Implements elementa::parsing::Token.
|
inlinevirtual |
Must return a reference to the textual value for the token.
Implements elementa::parsing::Token.
|
inlinevirtualinherited |
| class elementa::parsing::Lexer |
A lexer that produces terminals.
Use this class as base for your lexer if you do not want any finite state machine based lexer. Otherwise, use FSMLexer.

Public Member Functions | |
| Lexer (elementa::base::SerChIFilter< elementa::base::SerChFilt_Loc > &inputch) | |
| Constructor. More... | |
| virtual | ~Lexer (void)=default |
| Virtual destructor for polymorphic deletions. | |
| Lexer (const Lexer &)=delete | |
| Lexer (Lexer &&)=delete | |
| Lexer & | operator= (const Lexer &)=delete |
| Lexer & | operator= (Lexer &&)=delete |
| elementa::base::InSerCh & | inputChannel (void) const noexcept |
| Return a reference to the associated, non-location input channel. More... | |
| elementa::base::SerChIFilter< elementa::base::SerChFilt_Loc > & | locInputChannel (void) const noexcept |
| Return a reference to the associated location input channel. More... | |
| elementa::base::SerChLoc & | location (void) const noexcept |
| Return a ref. to the location associated to the channel used by the lexr. More... | |
| virtual Token::Ptr | getToken (void)=0 |
| Must create and return a pointer to the token built from the input. More... | |
|
inline |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
pure virtual |
Must create and return a pointer to the token built from the input.
The token cannot be null except for indicating the channel has been exhausted without recognizing anything.
Implemented in elementa::parsing::FSMLexer.
| class elementa::parsing::FSMLexer |
A lexical analyzer that uses a number of FSMs to recognize tokens.
This lexer works in the POSIX "leftmost longest" way: the longest FSM that recognizes the input is the one selected, and, if several, the first one provided in the definition of FSMs is selected. A FSM recognizes the input when it is atEnd() and new observations cannot be consumed by it (return false if any is fed into it). This lexer has no state: each time getToken() is called, all the internal FSMs are reset and start to recognize again.


Public Types | |
Types, consts, etc. | |
| using | FSMClass = elementa::adts::BaseTextFSM |
| Shortcut for the base FSM class. More... | |
| using | TermRecog = std::pair< TokenId, FSMClass::Ptr > |
| A FSM associated to the id of a terminal that it recognizes. More... | |
Public Member Functions | |
| elementa::base::InSerCh & | inputChannel (void) const noexcept |
| Return a reference to the associated, non-location input channel. More... | |
| elementa::base::SerChIFilter< elementa::base::SerChFilt_Loc > & | locInputChannel (void) const noexcept |
| Return a reference to the associated location input channel. More... | |
| elementa::base::SerChLoc & | location (void) const noexcept |
| Return a ref. to the location associated to the channel used by the lexr. More... | |
Constructors | |
| FSMLexer (elementa::base::SerChIFilter< elementa::base::SerChFilt_Loc > &inputch, Recognizers &definition) | |
| Default constructor. More... | |
| FSMLexer (const FSMLexer &)=delete | |
| FSMLexer (FSMLexer &&)=delete | |
| FSMLexer & | operator= (const FSMLexer &)=delete |
| FSMLexer & | operator= (FSMLexer &&)=delete |
Methods | |
| Token::Ptr | getToken (void) final |
| Derived classes cannot implement this. More... | |
| using elementa::parsing::FSMLexer::TermRecog = std::pair< TokenId , FSMClass::Ptr > |
|
inline |
Default constructor.
| definition | contains the FSMs, one FSM per possible token to be recognized (excluding comments and separators, that are dealt with differently). This parameter must outlive the FSMLexer, since the latter just takes a reference to it (the recognizers will be modified through it). |
|
finalvirtual |
Derived classes cannot implement this.
Reset all recognizers, then feed them with chars coming from the input channel until either the channel is exhausted or no recognizer remains active. Comments are skipped (no token will be generated). The same occurs with separators. The result contains a pointer to a TextToken.
Implements elementa::parsing::Lexer.
|
inlinenoexceptinherited |
|
inlinenoexceptinherited |
|
inlinenoexceptinherited |
| class elementa::parsing::LexerCollector |
A class to use a lexer for collecting all tokens sequentially.
Public Types | |
Types, consts, etc. | |
| using | Observer = std::function< bool(Lexer &, const LexerError *, Token::Ptr &)> |
| An observer for being called after each token is collected. More... | |
Public Member Functions | |
Constructors | |
| LexerCollector (Lexer &lexer, Observer &obs) | |
| Constructor. More... | |
| LexerCollector (const LexerCollector &)=delete | |
| LexerCollector (LexerCollector &&)=delete | |
| LexerCollector & | operator= (const LexerCollector &)=delete |
| LexerCollector & | operator= (LexerCollector &&)=delete |
| virtual | ~LexerCollector (void)=default |
Methods | |
| void | collect (bool log=false) |
| Collect all tokens from the channel of the lexer, calling the observer. More... | |
| const Token::PtrVector & | log (void) const noexcept |
| Get a reference to the internal log of tokens. More... | |
| using elementa::parsing::LexerCollector::Observer = std::function<bool(Lexer &, const LexerError *, Token::Ptr &)> |
An observer for being called after each token is collected.
The LexerError * will be nullptr if no lxr error occurred in that token. Note that it will be nullptr (no error) but the token may be null if it has not been recognized and the channel has been exhausted. The LexerError * will point to a non-null LexerError if such error has occurred (it is not thrown). The TokenPtr will be null if error or no token is recognized in the channel. The observer must return TRUE to go on collecting tokens or FALSE to abort. In the last case, the last token will be logged (see collect()) only if it was non-null. If the observer throws, the collector throws to its caller.
| void elementa::parsing::LexerCollector::collect | ( | bool | log = false | ) |
Collect all tokens from the channel of the lexer, calling the observer.
If the channel gets exhausted, stop collecting.
| log | indicates whether to keep copy of all tokens internally (TRUE) or not (FALSE). |
|
inlinenoexcept |
| using elementa::parsing::TokenId = typedef int |
#include <elementa/parsing/lexer.h>
A unique identifier for a token.