corelib/include/ebcl/SRDDefinitions.hh

322 lines
9.1 KiB
C++
Raw Normal View History

/******************************************************************************/
/* SRD - PARSER DEFINITIONS ***************************************************/
/******************************************************************************/
#ifndef _H_EBCL_SRDDEFINITIONS
#define _H_EBCL_SRDDEFINITIONS
#include <ebcl/SRDData.hh>
#include <ebcl/HashTables.hh>
namespace ebcl {
/*= DEFINITION DATA ==========================================================*/
// Full definition -> {contexts}, default context, {enums}
// Context -> name, {rules}
// Rule -> Accepted input, [context, [init], [exit]], [executor]
// Input def -> Word
// | Enum
// | Token type
// | Alternatives
// | Repetition
// Alternatives -> {input def}
// Repetition -> Input sequence, min count, max count
// Forward declarations
struct T_SRDParserData;
/*----------------------------------------------------------------------------*/
// F_SRDHandler - Parser callbacks
using F_SRDHandler = std::function< bool( T_SRDParserData const& ) >;
/*----------------------------------------------------------------------------*/
// T_SRDEnum - an enumeration
// i.e. set of words with associated sequence of integers, that can be
// recognized as a single input token.
class T_SRDEnum
{
private:
T_String name_;
T_HashIndex index_;
T_Array< T_String > words_;
public:
T_SRDEnum( ) = delete;
T_SRDEnum( T_String name );
T_SRDEnum( T_SRDEnum const& ) = default;
T_SRDEnum( T_SRDEnum && ) = default;
T_SRDEnum & operator <<( T_String && word );
T_SRDEnum & operator <<( char const* word );
T_String const& name( ) const;
auto size( ) const;
T_String const& operator[]( int index ) const;
T_String const& operator[]( uint32_t index ) const;
uint32_t operator[]( T_String const& word ) const;
uint32_t operator[]( char const* word ) const;
bool contains( T_String const& word ) const;
bool contains( char const* word ) const;
};
/*----------------------------------------------------------------------------*/
// E_SRDInputItem - Possible types for input definition elements
enum class E_SRDInputItem
{
WORD , // A specific word
ENUM , // A word from an enumeration
TOKEN , // An arbitrary token
ALTERNATIVE , // A set of alternative rules
REPETITION , // Repetition of a sequence
};
/*----------------------------------------------------------------------------*/
// T_SRDInputItem - An element from an input definition
class T_SRDInputItem
{
public:
struct T_ListHelper {
T_Array< T_SRDInputItem > items;
T_ListHelper( );
T_ListHelper& operator <<( T_SRDInputItem item );
};
private:
E_SRDInputItem type_;
T_String word_;
E_SRDTokenType token_;
T_Array< T_SRDInputItem > items_;
uint32_t min_ , max_;
public:
// Construct an ALTERNATIVE input element
T_SRDInputItem( );
// Construct either a WORD or an ENUM input element
T_SRDInputItem( T_String wordOrName , bool isWord );
// Construct a WORD from a C string
T_SRDInputItem( char const* cString );
// Construct a TOKEN input element
T_SRDInputItem( E_SRDTokenType type );
// Construct a REPETITION input element
T_SRDInputItem( uint32_t min , uint32_t max );
friend void swap( T_SRDInputItem & lhs , T_SRDInputItem & rhs ) noexcept;
T_SRDInputItem( T_SRDInputItem const& ) = default;
T_SRDInputItem( T_SRDInputItem && ) noexcept = default;
T_SRDInputItem & operator=( T_SRDInputItem const& ) = default;
T_SRDInputItem & operator=( T_SRDInputItem && ) = default;
E_SRDInputItem type( ) const;
T_String const& word( ) const;
E_SRDTokenType token( ) const;
T_Array< T_SRDInputItem > items( ) const;
uint32_t min( ) const;
uint32_t max( ) const;
T_SRDInputItem & operator <<( T_SRDInputItem item );
T_SRDInputItem& operator <<( T_ListHelper list );
// This operator is not actually used. It's only here to make the template
// instantiation happy. It actually returns false.
bool operator ==( T_SRDInputItem const& other ) const;
};
void swap( T_SRDInputItem & lhs , T_SRDInputItem & rhs ) noexcept;
T_StringBuilder & operator <<( T_StringBuilder & sb , T_SRDInputItem const& item );
T_Array< T_SRDInputItem >& operator <<( T_Array< T_SRDInputItem > & items ,
T_SRDInputItem::T_ListHelper& list );
extern template class T_Array< T_SRDInputItem >;
/*----------------------------------------------------------------------------*/
// T_SRDInputRule - A full rule from a context
class T_SRDInputRule
{
public:
struct T_SetContextExecutor {
const bool exit;
const F_SRDHandler executor;
T_SetContextExecutor( bool exit , F_SRDHandler executor );
};
struct T_SetContext {
const T_String name;
T_SetContext( T_String string );
};
private:
F_SRDHandler executor_;
T_Array< T_SRDInputItem > items_;
T_String context_;
F_SRDHandler contextEnter_;
F_SRDHandler contextExit_;
public:
T_SRDInputRule & operator <<( T_SRDInputItem item );
T_SRDInputRule & operator <<( F_SRDHandler executor );
T_SRDInputRule & operator <<( T_SetContextExecutor sce );
T_SRDInputRule & operator <<( T_SetContext sc );
T_SRDInputRule & operator <<( T_SRDInputItem::T_ListHelper sc );
F_SRDHandler const& handler( ) const;
T_Array< T_SRDInputItem > const& rule( ) const;
T_String const& context( ) const;
F_SRDHandler const& onEnter( ) const;
F_SRDHandler const& onExit( ) const;
};
T_StringBuilder & operator <<( T_StringBuilder & sb , T_SRDInputRule const& item );
/*----------------------------------------------------------------------------*/
// T_SRDContext - A set of rules that will share common data
class T_SRDContext
{
private:
T_String name_;
T_String parent_;
T_Array< T_SRDInputRule > rules_;
public:
T_SRDContext( ) = delete;
explicit T_SRDContext( T_String name , T_String parent = T_String( ) );
T_SRDContext & operator <<( T_SRDInputRule rule );
T_String const& name( ) const;
T_String const& parent( ) const;
T_Array< T_SRDInputRule > const& rules( ) const;
T_StringBuilder & dump( T_StringBuilder & sb , T_String const& separator );
};
/*----------------------------------------------------------------------------*/
// T_SRDParserDefs - All definitions for a parser
class T_SRDParserDefs
{
public:
// SetHandler - Used to set start/end handlers
struct SetHandler
{
const F_SRDHandler handler;
const bool start;
SetHandler( F_SRDHandler const& handler , bool start );
};
private:
T_String dfCtx_;
T_ObjectTable< T_String , T_SRDContext > ctx_;
T_ObjectTable< T_String , T_SRDEnum > enums_;
F_SRDHandler onStart_;
F_SRDHandler onFinish_;
public:
T_SRDParserDefs( ) = delete;
explicit T_SRDParserDefs( T_String defaultContext );
explicit T_SRDParserDefs( char const* defaultContext );
T_String const& defaultContext( ) const;
void defaultContext( T_String const& name );
void defaultContext( char const* name );
T_SRDParserDefs & operator <<( SetHandler sh );
F_SRDHandler const& onStart( ) const;
F_SRDHandler const& onFinish( ) const;
uint32_t contexts( ) const;
T_SRDContext & operator[]( uint32_t idx );
T_SRDContext const& operator[]( uint32_t idx ) const;
uint32_t contextId( T_String const& name ) const;
T_SRDContext & context( T_String const& name );
T_SRDContext & context( char const* name );
T_SRDContext & context( T_String const& name , T_String const& parent );
T_SRDContext & context( char const* name , char const* parent );
uint32_t enums( ) const;
bool hasEnum( T_String const& name ) const;
T_SRDEnum & enumeration( char const* name );
T_SRDEnum & enumeration( T_String const& name );
T_SRDEnum const& enumeration( T_String const& name ) const;
};
M_CLASS_POINTERS( SRDParserDefs );
/*= PARSER DEFINITIONS HELPERS ===============================================*/
namespace SRD {
T_SRDParserDefs::SetHandler OnStart( F_SRDHandler const& handler );
T_SRDParserDefs::SetHandler OnFinish( F_SRDHandler const& handler );
T_SRDContext Context( T_String name );
T_SRDContext Context( char const* name );
T_SRDContext Context( char const* name , char const* parent );
T_SRDContext Context( T_String name , T_String parent );
T_SRDInputRule Rule( );
T_SRDEnum CreateEnum( T_String name );
T_SRDEnum CreateEnum( char const* name );
T_SRDInputItem Word( T_String const& word );
T_SRDInputItem Word( char const* word );
T_SRDInputItem Word( );
T_SRDInputItem String( );
T_SRDInputItem Binary( );
T_SRDInputItem LStart( );
T_SRDInputItem LEnd( );
T_SRDInputItem Int32( );
T_SRDInputItem Int64( );
T_SRDInputItem Float( );
T_SRDInputItem Enum( T_String name );
T_SRDInputItem Enum( char const* name );
T_SRDInputItem::T_ListHelper List( );
T_SRDInputItem Alt( );
T_SRDInputItem Sub( );
T_SRDInputItem Opt( );
T_SRDInputItem Opt( T_SRDInputItem item );
T_SRDInputItem AtLeast( uint32_t n );
T_SRDInputItem AtMost( uint32_t n );
T_SRDInputItem Times( uint32_t n );
T_SRDInputItem Between( uint32_t a , uint32_t b );
T_SRDInputItem Integer( );
T_SRDInputItem Numeric( );
T_SRDInputItem Text( );
T_SRDInputRule::T_SetContext EnterContext( T_String name );
T_SRDInputRule::T_SetContext EnterContext( char const* name );
T_SRDInputRule::T_SetContextExecutor OnEnter( F_SRDHandler f );
T_SRDInputRule::T_SetContextExecutor OnExit( F_SRDHandler f );
} // namespace SRD
} // namespace
#include <ebcl/inline/SRDDefinitions.hh>
#endif // _H_EBCL_SRDDEFINITIONS