HParserCacheValue* recall(HParserCacheKey *k, HParseState *state) { HParserCacheValue *cached = h_hashtable_get(state->cache, k); HRecursionHead *head = h_hashtable_get(state->recursion_heads, &k->input_pos); if (!head) { // No heads found return cached; } else { // Some heads found if (!cached && head->head_parser != k->parser && !h_slist_find(head->involved_set, k->parser)) { // Nothing in the cache, and the key parser is not involved cached = cached_result(state, NULL); cached->input_stream = k->input_pos; } if (h_slist_find(head->eval_set, k->parser)) { // Something is in the cache, and the key parser is in the eval set. Remove the key parser from the eval set of the head. head->eval_set = h_slist_remove_all(head->eval_set, k->parser); HParseResult *tmp_res = perform_lowlevel_parse(state, k->parser); // update the cache if (!cached) { cached = cached_result(state, tmp_res); h_hashtable_put(state->cache, k, cached); } else { cached->value_type = PC_RIGHT; cached->right = tmp_res; cached->input_stream = state->input_stream; } } return cached; } }
void h_symbol_put(HParseState *state, const char* key, void *value) { if (!state->symbol_table) { state->symbol_table = h_slist_new(state->arena); h_slist_push(state->symbol_table, h_hashtable_new(state->arena, h_eq_ptr, h_hash_ptr)); } HHashTable *head = h_slist_top(state->symbol_table); assert(!h_hashtable_present(head, key)); h_hashtable_put(head, key, value); }
void h_hashtable_update(HHashTable *dst, const HHashTable *src) { size_t i; HHashTableEntry *hte; for(i=0; i < src->capacity; i++) { for(hte = &src->contents[i]; hte; hte = hte->next) { if(hte->key == NULL) continue; h_hashtable_put(dst, hte->key, hte->value); } } }
/* Warth's recursion. Hi Alessandro! */ HParseResult* h_do_parse(const HParser* parser, HParseState *state) { HParserCacheKey *key = a_new(HParserCacheKey, 1); key->input_pos = state->input_stream; key->parser = parser; HParserCacheValue *m = recall(key, state); // check to see if there is already a result for this object... if (!m) { // It doesn't exist, so create a dummy result to cache HLeftRec *base = a_new(HLeftRec, 1); base->seed = NULL; base->rule = parser; base->head = NULL; h_slist_push(state->lr_stack, base); // cache it h_hashtable_put(state->cache, key, cached_lr(state, base)); // parse the input HParseResult *tmp_res = perform_lowlevel_parse(state, parser); // the base variable has passed equality tests with the cache h_slist_pop(state->lr_stack); // update the cached value to our new position HParserCacheValue *cached = h_hashtable_get(state->cache, key); assert(cached != NULL); cached->input_stream = state->input_stream; // setupLR, used below, mutates the LR to have a head if appropriate, so we check to see if we have one if (NULL == base->head) { h_hashtable_put(state->cache, key, cached_result(state, tmp_res)); return tmp_res; } else { base->seed = tmp_res; HParseResult *res = lr_answer(key, state, base); return res; } } else { // it exists! state->input_stream = m->input_stream; if (PC_LEFT == m->value_type) { setupLR(parser, state, m->left); return m->left->seed; } else { return m->right; } } }
void h_hashtable_merge(void *(*combine)(void *v1, const void *v2), HHashTable *dst, const HHashTable *src) { size_t i; HHashTableEntry *hte; for(i=0; i < src->capacity; i++) { for(hte = &src->contents[i]; hte; hte = hte->next) { if(hte->key == NULL) continue; void *dstvalue = h_hashtable_get(dst, hte->key); void *srcvalue = hte->value; h_hashtable_put(dst, hte->key, combine(dstvalue, srcvalue)); } } }
static HCFChoice *new_enhanced_symbol(HLREnhGrammar *eg, const HCFChoice *sym) { HArena *arena = eg->arena; HCFChoice *esym = h_arena_malloc(arena, sizeof(HCFChoice)); *esym = *sym; HHashSet *cs = h_hashtable_get(eg->corr, sym); if (!cs) { cs = h_hashset_new(arena, h_eq_symbol, h_hash_symbol); h_hashtable_put(eg->corr, sym, cs); } h_hashset_put(cs, esym); return esym; }
static HLREnhGrammar *enhance_grammar(const HCFGrammar *g, const HLRDFA *dfa, const HLRTable *table) { HAllocator *mm__ = g->mm__; HArena *arena = g->arena; HLREnhGrammar *eg = h_arena_malloc(arena, sizeof(HLREnhGrammar)); eg->tmap = h_hashtable_new(arena, h_eq_transition, h_hash_transition); eg->smap = h_hashtable_new(arena, h_eq_ptr, h_hash_ptr); eg->corr = h_hashtable_new(arena, h_eq_symbol, h_hash_symbol); // XXX must use h_eq/hash_ptr for symbols! so enhanced CHARs are different eg->arena = arena; // establish mapping between transitions and symbols for(HSlistNode *x=dfa->transitions->head; x; x=x->next) { HLRTransition *t = x->elem; assert(!h_hashtable_present(eg->tmap, t)); HCFChoice *sym = new_enhanced_symbol(eg, t->symbol); h_hashtable_put(eg->tmap, t, sym); h_hashtable_put(eg->smap, sym, t); } // transform the productions H_FOREACH(eg->tmap, HLRTransition *t, HCFChoice *sym) transform_productions(table, eg, t->from, sym); H_END_FOREACH // add the start symbol HCFChoice *start = new_enhanced_symbol(eg, g->start); transform_productions(table, eg, 0, start); eg->grammar = h_cfgrammar_(mm__, start); return eg; }
HParseResult* grow(HParserCacheKey *k, HParseState *state, HRecursionHead *head) { // Store the head into the recursion_heads h_hashtable_put(state->recursion_heads, &k->input_pos, head); HParserCacheValue *old_cached = h_hashtable_get(state->cache, k); if (!old_cached || PC_LEFT == old_cached->value_type) h_platform_errx(1, "impossible match"); HParseResult *old_res = old_cached->right; // rewind the input state->input_stream = k->input_pos; // reset the eval_set of the head of the recursion at each beginning of growth head->eval_set = h_slist_copy(head->involved_set); HParseResult *tmp_res = perform_lowlevel_parse(state, k->parser); if (tmp_res) { if (pos_lt(old_cached->input_stream, state->input_stream)) { h_hashtable_put(state->cache, k, cached_result(state, tmp_res)); return grow(k, state, head); } else { // we're done with growing, we can remove data from the recursion head h_hashtable_del(state->recursion_heads, &k->input_pos); HParserCacheValue *cached = h_hashtable_get(state->cache, k); if (cached && PC_RIGHT == cached->value_type) { state->input_stream = cached->input_stream; return cached->right; } else { h_platform_errx(1, "impossible match"); } } } else { h_hashtable_del(state->recursion_heads, &k->input_pos); state->input_stream = old_cached->input_stream; return old_res; } }
HParseResult* lr_answer(HParserCacheKey *k, HParseState *state, HLeftRec *growable) { if (growable->head) { if (growable->head->head_parser != k->parser) { // not the head rule, so not growing return growable->seed; } else { // update cache h_hashtable_put(state->cache, k, cached_result(state, growable->seed)); if (!growable->seed) return NULL; else return grow(k, state, growable->head); } } else { h_platform_errx(1, "lrAnswer with no head"); } }
// for each lookahead symbol (fs), put action into tmap // returns 0 on success, -1 on conflict // ignores forall entries static int terminals_put(HStringMap *tmap, const HStringMap *fs, HLRAction *action) { int ret = 0; if (fs->epsilon_branch) { HLRAction *prev = tmap->epsilon_branch; if (prev && prev != action) { // conflict tmap->epsilon_branch = h_lr_conflict(tmap->arena, prev, action); ret = -1; } else { tmap->epsilon_branch = action; } } if (fs->end_branch) { HLRAction *prev = tmap->end_branch; if (prev && prev != action) { // conflict tmap->end_branch = h_lr_conflict(tmap->arena, prev, action); ret = -1; } else { tmap->end_branch = action; } } H_FOREACH(fs->char_branches, void *key, HStringMap *fs_) HStringMap *tmap_ = h_hashtable_get(tmap->char_branches, key); if (!tmap_) { tmap_ = h_stringmap_new(tmap->arena); h_hashtable_put(tmap->char_branches, key, tmap_); } if (terminals_put(tmap_, fs_, action) < 0) { ret = -1; } H_END_FOREACH return ret; }