sheepy

build system (sheepy) and package manager (spm) for C
git clone https://spartatek.se/git/sheepy.git
Log | Files | Refs | README | LICENSE

LexatomLoader.i (12028B)


      1 /* -*- C++ -*- vim: set syntax=cpp: */
      2 #ifndef  __QUEX_INCLUDE_GUARD__BUFFER__LEXATOMS__LEXATOM_LOADER_I
      3 #define  __QUEX_INCLUDE_GUARD__BUFFER__LEXATOMS__LEXATOM_LOADER_I
      4 
      5 #include "definitions"
      6 #include "bufferBuffer"
      7 #include "MemoryManager"
      8 #include "LexatomLoader"
      9 #include "Buffer_debug"
     10 
     11 QUEX_NAMESPACE_MAIN_OPEN
     12 
     13 QUEX_INLINE bool       QUEX_NAME(LexatomLoader_lexatom_index_seek)(QUEX_NAME(LexatomLoader)*         me, 
     14                                                                     const QUEX_TYPE_STREAM_POSITION  LexatomIndex);
     15 QUEX_INLINE QUEX_TYPE_STREAM_POSITION 
     16                        QUEX_NAME(LexatomLoader_lexatom_index_tell)(QUEX_NAME(LexatomLoader)* me);
     17 QUEX_INLINE bool       QUEX_NAME(LexatomLoader_lexatom_index_step_to)(QUEX_NAME(LexatomLoader)*        me,
     18                                                                        const QUEX_TYPE_STREAM_POSITION TargetCI);
     19 QUEX_INLINE void       QUEX_NAME(LexatomLoader_lexatom_index_reset_backup)(QUEX_NAME(LexatomLoader)* me, 
     20                                                           QUEX_TYPE_STREAM_POSITION Backup_lexatom_index_next_to_fill, 
     21                                                           ptrdiff_t                 BackupStomachByteN, 
     22                                                           QUEX_TYPE_STREAM_POSITION BackupByteLoaderPosition);
     23 QUEX_INLINE void       QUEX_NAME(LexatomLoader_reverse_byte_order)(QUEX_TYPE_LEXATOM*       Begin, 
     24                                                                   const QUEX_TYPE_LEXATOM* End);
     25 
     26 QUEX_INLINE void       QUEX_NAME(LexatomLoader_delete_self)(QUEX_NAME(LexatomLoader)*); 
     27 
     28                        
     29 QUEX_INLINE QUEX_NAME(LexatomLoader)*
     30 QUEX_NAME(LexatomLoader_new)(QUEX_NAME(ByteLoader)*  byte_loader, 
     31                              QUEX_NAME(Converter)*   converter,
     32                              const size_t            TranslationBufferMemorySize)
     33 {
     34     QUEX_NAME(LexatomLoader)* filler;
     35     (void)TranslationBufferMemorySize;
     36 
     37     /* byte_loader = 0; possible if memory is filled manually.               */
     38     if( converter ) {
     39         filler = QUEX_NAME(LexatomLoader_Converter_new)(byte_loader, converter, 
     40                                                         TranslationBufferMemorySize);
     41     }
     42     else {
     43         filler = QUEX_NAME(LexatomLoader_Plain_new)(byte_loader); 
     44     }
     45     
     46     return filler;
     47 }
     48 
     49 QUEX_INLINE QUEX_NAME(LexatomLoader)* 
     50 QUEX_NAME(LexatomLoader_new_DEFAULT)(QUEX_NAME(ByteLoader)*   byte_loader, 
     51                                      const char*              InputCodecName) 
     52 {
     53 #   if   defined(QUEX_OPTION_CONVERTER_ICONV)
     54     QUEX_NAME(Converter)* converter = QUEX_NAME(Converter_IConv_new)(InputCodecName, 0);
     55 #   elif defined(QUEX_OPTION_CONVERTER_ICU)
     56     QUEX_NAME(Converter)* converter = QUEX_NAME(Converter_ICU_new)(InputCodecName, 0);
     57 #   else
     58     QUEX_NAME(Converter)* converter = (QUEX_NAME(Converter)*)0;
     59 #   endif
     60 
     61     if( converter ) {
     62         converter->ownership = E_Ownership_LEXICAL_ANALYZER;
     63         if( ! InputCodecName ) {
     64 #           ifndef QUEX_OPTION_WARNING_ON_PLAIN_FILLER_DISABLED
     65             __QUEX_STD_printf("Warning: No character encoding name specified, while this\n" \
     66                               "Warning: analyzer was generated for use with a converter.\n" \
     67                               "Warning: Please, consult the documentation about the constructor\n" \
     68                               "Warning: or the reset function. If it is desired to do a plain\n" \
     69                               "Warning: buffer filler with this setup, you might want to disable\n" \
     70                               "Warning: this warning with the macro:\n" \
     71                               "Warning:     QUEX_OPTION_WARNING_ON_PLAIN_FILLER_DISABLED\n");
     72 #           endif
     73             return (QUEX_NAME(LexatomLoader)*)0x0;
     74         }
     75     } 
     76 
     77     return QUEX_NAME(LexatomLoader_new)(byte_loader, converter,
     78                                         QUEX_SETTING_TRANSLATION_BUFFER_SIZE);
     79 }
     80 
     81 QUEX_INLINE void       
     82 QUEX_NAME(LexatomLoader_delete_self)(QUEX_NAME(LexatomLoader)* me)
     83 { 
     84     if( ! me ) return;
     85 
     86     if( me->byte_loader && me->byte_loader->ownership == E_Ownership_LEXICAL_ANALYZER ) {
     87         QUEX_NAME(ByteLoader_delete)(&me->byte_loader);
     88     }
     89 
     90     /* destruct_self: Free resources occupied by 'me' BUT NOT 'myself'.
     91      * delete_self:   Free resources occupied by 'me' AND 'myself'.          */
     92     if( me->derived.destruct_self ) {
     93         me->derived.destruct_self(me);
     94     }
     95 
     96     QUEXED(MemoryManager_free)((void*)me, E_MemoryObjectType_BUFFER_FILLER);
     97 }
     98 
     99 QUEX_INLINE void    
    100 QUEX_NAME(LexatomLoader_setup)(QUEX_NAME(LexatomLoader)*   me,
    101                                size_t       (*derived_load_lexatoms)(QUEX_NAME(LexatomLoader)*,
    102                                                                      QUEX_TYPE_LEXATOM*, 
    103                                                                      const size_t, 
    104                                                                      bool*, bool*),
    105                                ptrdiff_t    (*stomach_byte_n)(QUEX_NAME(LexatomLoader)*),
    106                                void         (*stomach_clear)(QUEX_NAME(LexatomLoader)*),
    107                                void         (*derived_destruct_self)(QUEX_NAME(LexatomLoader)*),
    108                                void         (*derived_fill_prepare)(QUEX_NAME(LexatomLoader)*  me,
    109                                                                     QUEX_NAME(Buffer)*         buffer,
    110                                                                     void**                     begin_p,
    111                                                                     const void**               end_p),
    112                                ptrdiff_t    (*derived_fill_finish)(QUEX_NAME(LexatomLoader)* me,
    113                                                                    QUEX_TYPE_LEXATOM*        BeginP,
    114                                                                    const QUEX_TYPE_LEXATOM*  EndP,
    115                                                                    const void*               FilledEndP),
    116                                void         (*derived_get_fill_boundaries)(QUEX_NAME(LexatomLoader)*  alter_ego,
    117                                                                            QUEX_NAME(Buffer)*         buffer,
    118                                                                            void**                     begin_p, 
    119                                                                            const void**               end_p),
    120                                QUEX_NAME(ByteLoader)*  byte_loader,
    121                                ptrdiff_t    ByteNPerCharacter)
    122 {
    123     __quex_assert(me);
    124     __quex_assert(derived_load_lexatoms);
    125     __quex_assert(derived_destruct_self);
    126 
    127     /* Support for buffer filling without user interaction                   */
    128     me->stomach_byte_n        = stomach_byte_n;
    129     me->stomach_clear         = stomach_clear;
    130     me->input_lexatom_tell    = QUEX_NAME(LexatomLoader_lexatom_index_tell);
    131     me->input_lexatom_seek    = QUEX_NAME(LexatomLoader_lexatom_index_seek);
    132     me->derived.load_lexatoms = derived_load_lexatoms;
    133     me->derived.destruct_self = derived_destruct_self;
    134     me->delete_self           = QUEX_NAME(LexatomLoader_delete_self);
    135 
    136     /* Support for manual buffer filling.                                    */
    137     me->derived.fill_prepare        = derived_fill_prepare;
    138     me->derived.fill_finish         = derived_fill_finish;
    139     me->derived.get_fill_boundaries = derived_get_fill_boundaries;
    140 
    141     me->byte_loader                 = byte_loader;
    142 
    143     me->_byte_order_reversion_active_f = false;
    144     me->lexatom_index_next_to_fill   = 0;
    145     me->byte_n_per_lexatom           = ByteNPerCharacter;
    146 
    147     /* Default: External ownership                                           */
    148     me->ownership = E_Ownership_EXTERNAL;
    149 }
    150 
    151 QUEX_INLINE void
    152 QUEX_NAME(LexatomLoader_reset)(QUEX_NAME(LexatomLoader)* me, QUEX_NAME(ByteLoader)* new_byte_loader)
    153 /* Resets the LexatomLoader with a new QUEX_NAME(ByteLoader).                            */
    154 {
    155     if( new_byte_loader != me->byte_loader ) {
    156         if( QUEX_NAME(ByteLoader_is_equivalent)(new_byte_loader, me->byte_loader) ) {
    157             __QUEX_STD_printf("Upon 'reset': current and new QUEX_NAME(ByteLoader )objects contain same input handle.\n"); 
    158         }
    159         if( me->byte_loader && me->byte_loader->ownership == E_Ownership_LEXICAL_ANALYZER ) {
    160             QUEX_NAME(ByteLoader_delete)(&me->byte_loader);
    161             me->byte_loader = new_byte_loader;
    162         }
    163     }
    164     QUEX_NAME(LexatomLoader_lexatom_index_reset)(me);
    165 }
    166 
    167 QUEX_INLINE ptrdiff_t       
    168 QUEX_NAME(LexatomLoader_load)(QUEX_NAME(LexatomLoader)*  me, 
    169                               QUEX_TYPE_LEXATOM*         LoadP, 
    170                               const ptrdiff_t            LoadN,
    171                               QUEX_TYPE_STREAM_POSITION  StartLexatomIndex,
    172                               bool*                      end_of_stream_f,
    173                               bool*                      encoding_error_f)
    174 /* Seeks the input position StartLexatomIndex and loads 'LoadN' 
    175  * lexatoms into the engine's buffer starting from 'LoadP'.
    176  *
    177  * RETURNS: Number of loaded lexatoms.                                     */
    178 {
    179     ptrdiff_t                loaded_n;
    180 
    181     /* (1) Seek to the position where loading shall start.                       
    182      *                                                                       */
    183     if( ! me->input_lexatom_seek(me, StartLexatomIndex) ) {
    184         return 0;
    185     }
    186     __quex_assert(me->lexatom_index_next_to_fill == StartLexatomIndex);
    187 
    188     /* (2) Load content into the given region.                                   
    189      *                                                                       */
    190     loaded_n = (ptrdiff_t)me->derived.load_lexatoms(me, LoadP, (size_t)LoadN,
    191                                                     end_of_stream_f, encoding_error_f);
    192 #   ifdef QUEX_OPTION_DEBUG_LOAD
    193     {
    194         const QUEX_TYPE_LEXATOM* p;
    195         /* The buffer limit code is not to appear inside the loaded content. */
    196         for(p=LoadP; p != &LoadP[loaded_n]; ++p) {
    197             __quex_assert(*p != QUEX_SETTING_BUFFER_LIMIT_CODE);
    198         }
    199     }
    200 #   endif
    201 
    202     __quex_assert(loaded_n <= LoadN);
    203     me->lexatom_index_next_to_fill += loaded_n;
    204 
    205     /* (3) Optionally reverse the byte order.                                    
    206      *                                                                       */
    207     if( me->_byte_order_reversion_active_f ) {
    208         QUEX_NAME(LexatomLoader_reverse_byte_order)(LoadP, &LoadP[loaded_n]);
    209     }
    210 
    211     return loaded_n;
    212 }
    213 
    214 
    215 QUEX_INLINE void
    216 QUEX_NAME(LexatomLoader_reverse_byte_order)(QUEX_TYPE_LEXATOM*       Begin, 
    217                                            const QUEX_TYPE_LEXATOM* End)
    218 {
    219     uint8_t              tmp = 0xFF;
    220     QUEX_TYPE_LEXATOM* iterator = 0x0;
    221 
    222     switch( sizeof(QUEX_TYPE_LEXATOM) ) {
    223     default:
    224         __quex_assert(false);
    225         break;
    226     case 1:
    227         /* Nothing to be done */
    228         break;
    229     case 2:
    230         for(iterator=Begin; iterator != End; ++iterator) {
    231             tmp = *(((uint8_t*)iterator) + 0);
    232             *(((uint8_t*)iterator) + 0) = *(((uint8_t*)iterator) + 1);
    233             *(((uint8_t*)iterator) + 1) = tmp;
    234         }
    235         break;
    236     case 4:
    237         for(iterator=Begin; iterator != End; ++iterator) {
    238             tmp = *(((uint8_t*)iterator) + 0);
    239             *(((uint8_t*)iterator) + 0) = *(((uint8_t*)iterator) + 3);
    240             *(((uint8_t*)iterator) + 3) = tmp;
    241             tmp = *(((uint8_t*)iterator) + 1);
    242             *(((uint8_t*)iterator) + 1) = *(((uint8_t*)iterator) + 2);
    243             *(((uint8_t*)iterator) + 2) = tmp;
    244         }
    245         break;
    246     }
    247 }
    248 
    249 QUEX_NAMESPACE_MAIN_CLOSE
    250 
    251 #include "Buffer.i"
    252 #include "ByteLoader.i"
    253 #include "LexatomLoader_navigation.i"
    254 #include "LexatomLoader_Converter.i"
    255 #include "LexatomLoader_Plain.i"
    256 
    257 #endif /* __QUEX_INCLUDE_GUARD__BUFFER__BUFFERFILLER_I */
    258