/* Backward traverse the macro units and set stopping tokens for each unit. * A stopping token is defined as the content of a macro unit of type DMACRO_TOK. * XXX, also define stopping token by DMACRO_BR units? */ static void DMacroGroup_SetStop( DMacroGroup *self, DArray *stops ) { DMacroGroup *group; daoint i, j; /* printf( "stop : %i\n", stops->size ); */ for(i=self->units->size-1; i>=0; i--){ DMacroUnit *unit = (DMacroUnit*) self->units->items.pVoid[i]; if( unit->type == DMACRO_GRP || unit->type == DMACRO_ALT ){ group = (DMacroGroup*) unit; /* self->stops as temporary array: */ DArray_Assign( self->stops, stops ); /* recursive set stopping tokens for macro groups: */ DMacroGroup_SetStop( group, self->stops ); /* if the group has to be presented at least once, * no propagating the stopping tokens to the previous macro units. */ if( group->repeat > DMACRO_ZERO_OR_MORE ) DArray_Clear( stops ); /* add stopping token, why only one ? XXX */ if( group->stops->size >0) DArray_PushFront( stops, group->stops->items.pString[0] ); }else if( unit->type == DMACRO_TOK ){ /* printf( "%s", unit->marker->mbs ); */ DArray_Clear( stops ); /* define a stopping token */ DArray_Append( stops, unit->marker ); DArray_Append( unit->stops, unit->marker ); }else{ /* printf( "%s", unit->marker->mbs ); */ for(j=0; j<stops->size; j++) DArray_Append( unit->stops, stops->items.pString[j] ); } /* printf( " : %i; ", unit->stops->size ); */ } if( self->repeat == DMACRO_ZERO_OR_MORE || self->repeat == DMACRO_ONE_OR_MORE ){ /* this is fine for DMACRO_GRP unit, what about DMACRO_ALT unit? XXX */ if( self->units->size >1 ){ DMacroUnit *first = (DMacroUnit*) self->units->items.pVoid[0]; DMacroGroup_AddStop( self, first->stops ); } } DArray_Assign( self->stops, stops ); /* printf( "group : %i\n", self->stops->size ); */ }
int DaoParser_ParseMacro( DaoParser *self, int start ) { int rb1, rb2, i = start, N = self->tokens->size; DaoToken **toks = self->tokens->items.pToken; DaoMacro *macro; DString *lang = NULL; DArray *stops; DMap *markers; if( start + 5 >= N ) return -1; if( toks[start+1]->type != DTOK_LCB ){ lang = & toks[start+1]->string; if( toks[start+1]->type != DTOK_IDENTIFIER ){ DaoParser_Error( self, DAO_TOKEN_NEED_NAME, lang ); return -1; } if( lang->size == 3 && strcmp( lang->mbs, "dao" ) == 0 ){ DaoParser_Error( self, DAO_TOKEN_NEED_NAME, lang ); return -1; } start += 1; } if( toks[start+1]->name != DTOK_LCB ) return -1; self->curLine = toks[start]->line; rb1 = DaoParser_FindPairToken( self, DTOK_LCB, DTOK_RCB, start, -1 ); if( rb1 <0 || rb1 +3 >= N ) return -1; if( toks[rb1+1]->name != DKEY_AS || toks[rb1+2]->name != DTOK_LCB ){ DaoParser_Error( self, DAO_CTW_INV_MAC_DEFINE, NULL ); return -1; } rb2 = DaoParser_FindPairToken( self, DTOK_LCB, DTOK_RCB, rb1 + 1, -1 ); if( rb2 <0 ) return -1; /* for( i=start; i<rb2; i++ ) printf( "%s ", toks[i]->string.mbs ); printf("\n"); */ macro = DaoMacro_New(); if( DaoParser_MakeMacroGroup( self, macro->macroMatch, macro->macroMatch, start+2, rb1 ) ==0 ){ DaoMacro_Delete( macro ); return -1; } if( macro->macroMatch->units->size >0 ){ DMacroUnit *unit = (DMacroUnit*) macro->macroMatch->units->items.pVoid[0]; if( unit->type != DMACRO_TOK ) DaoParser_Error( self, DAO_CTW_INV_MAC_FIRSTOK, & toks[i]->string ); } if( toks[rb1+3]->line != toks[rb1+2]->line ) macro->macroApply->cpos = toks[rb1+3]->cpos; if( DaoParser_MakeMacroGroup( self, macro->macroApply, macro->macroApply, rb1+3, rb2 ) ==0 ){ DaoMacro_Delete( macro ); return -1; } markers = DMap_New(D_STRING,0); for(i=start+2; i<rb1; i++){ if( toks[i]->string.mbs[0] == '$' ){ if( MAP_Find( markers, & toks[i]->string ) != NULL ){ self->curLine = toks[i]->line; DaoParser_Error( self, DAO_CTW_REDEF_MAC_MARKER, & toks[i]->string ); return 0; } MAP_Insert( markers, & toks[i]->string, 0 ); } } DMap_Clear( markers ); i = rb1+3; if( DString_EQ( & toks[start+2]->string, & toks[rb1+3]->string ) ) i ++; while( i < rb2 ){ char ch = toks[i]->string.mbs[0]; if( ch != '$' && ch != '\\' && ch != '\'' ){ if( MAP_Find( markers, & toks[i]->string ) == NULL ){ DArray_Append( macro->keyListApply, & toks[i]->string ); MAP_Insert( markers, & toks[i]->string, 0 ); } } i ++; } stops = DArray_New(D_TOKEN); DMacroGroup_SetStop( macro->macroMatch, stops ); DMacroGroup_FindVariables( macro->macroMatch ); DArray_Clear( stops ); DMacroGroup_SetStop( macro->macroApply, stops ); DaoNamespace_AddMacro( self->nameSpace, lang, & toks[start+2]->string, macro ); DArray_Delete( stops ); DMap_Delete( markers ); return rb2; }
int DaoParser_ParseMacro( DaoParser *self, int start ) { int rb1, rb2, i = start, N = self->tokens->size; DaoToken **toks = self->tokens->items.pToken; DMacroUnit *first; DaoMacro *macro; DString *lang = NULL; DArray *stops; DMap *markers; DNode *it; if( start + 5 >= N ) return -1; if( toks[start+1]->type != DTOK_LCB ){ lang = & toks[start+1]->string; if( toks[start+1]->type != DTOK_IDENTIFIER ){ DaoParser_Error( self, DAO_TOKEN_NEED_NAME, lang ); return -1; } if( lang->size == 3 && strcmp( lang->mbs, "dao" ) == 0 ){ DaoParser_Error( self, DAO_TOKEN_NEED_NAME, lang ); return -1; } start += 1; } if( toks[start+1]->name != DTOK_LCB ) return -1; self->curLine = toks[start]->line; rb1 = DaoParser_FindPairToken( self, DTOK_LCB, DTOK_RCB, start, -1 ); if( rb1 <0 || rb1 +3 >= N ) return -1; if( toks[rb1+1]->name != DKEY_AS || toks[rb1+2]->name != DTOK_LCB ){ DaoParser_Error( self, DAO_CTW_INV_MAC_DEFINE, NULL ); return -1; } rb2 = DaoParser_FindPairToken( self, DTOK_LCB, DTOK_RCB, rb1 + 1, -1 ); if( rb2 <0 ) return -1; /* for( i=start; i<rb2; i++ ) printf( "%s ", toks[i]->string.mbs ); printf("\n"); */ macro = DaoMacro_New(); markers = DMap_New(D_STRING,0); if( DaoParser_MakeMacroGroup( self, macro->macroMatch, macro->macroMatch, start+2, rb1, markers, NULL ) ==0 ){ goto Error; } if( macro->macroMatch->units->size == 0 ) goto Error; first = (DMacroUnit*) macro->macroMatch->units->items.pVoid[0]; if( first->type != DMACRO_TOK ){ DaoParser_Error( self, DAO_CTW_INV_MAC_FIRSTOK, & toks[i]->string ); goto Error; } for(it=DMap_First(markers); it; it=DMap_Next(markers,it)){ if( it->value.pInt > 1 ){ DaoParser_Error( self, DAO_CTW_REDEF_MAC_MARKER, it->key.pString ); goto Error; } } DMap_Clear( markers ); if( toks[rb1+3]->line != toks[rb1+2]->line ) macro->macroApply->cpos = toks[rb1+3]->cpos; if( DaoParser_MakeMacroGroup( self, macro->macroApply, macro->macroApply, rb1+3, rb2, NULL, markers ) ==0 ){ goto Error; } for(it=DMap_First(markers); it; it=DMap_Next(markers,it)){ DArray_Append( macro->keyListApply, it->key.pString ); } stops = DArray_New(D_TOKEN); DMacroGroup_SetStop( macro->macroMatch, stops ); DMacroGroup_FindVariables( macro->macroMatch ); DArray_Clear( stops ); DMacroGroup_SetStop( macro->macroApply, stops ); DaoNamespace_AddMacro( self->nameSpace, lang, & first->marker->string, macro ); DArray_Delete( stops ); DMap_Delete( markers ); return rb2; Error: DaoMacro_Delete( macro ); DMap_Delete( markers ); return -1; }