return running state from h_lrengine_step

This commit is contained in:
Sven M. Hallberg 2013-06-19 14:16:34 +02:00
parent 409d33c916
commit 168760b10a
2 changed files with 11 additions and 17 deletions

View file

@ -183,7 +183,6 @@ HLREngine *h_lrengine_new(HArena *arena, HArena *tarena, const HLRTable *table)
engine->left = h_slist_new(tarena); engine->left = h_slist_new(tarena);
engine->right = h_slist_new(tarena); engine->right = h_slist_new(tarena);
engine->state = 0; engine->state = 0;
engine->running = 1;
engine->arena = arena; engine->arena = arena;
engine->tarena = tarena; engine->tarena = tarena;
@ -228,7 +227,8 @@ const HLRAction *h_lrengine_action(HLREngine *engine, HInputStream *stream)
return action; return action;
} }
void h_lrengine_step(HLREngine *engine, const HLRAction *action) // run LR parser for one round; returns false when finished
bool h_lrengine_step(HLREngine *engine, const HLRAction *action)
{ {
// short-hand names // short-hand names
HSlist *left = engine->left; HSlist *left = engine->left;
@ -236,11 +236,8 @@ void h_lrengine_step(HLREngine *engine, const HLRAction *action)
HArena *arena = engine->arena; HArena *arena = engine->arena;
HArena *tarena = engine->tarena; HArena *tarena = engine->tarena;
if(action == NULL) { if(action == NULL)
// no handle recognizable in input, terminate return false; // no handle recognizable in input, terminate
engine->running = false;
return;
}
if(action->type == HLR_SHIFT) { if(action->type == HLR_SHIFT) {
h_slist_push(left, (void *)(uintptr_t)engine->state); h_slist_push(left, (void *)(uintptr_t)engine->state);
@ -280,11 +277,8 @@ void h_lrengine_step(HLREngine *engine, const HLRAction *action)
value = (HParsedToken *)symbol->reshape(make_result(arena, value)); value = (HParsedToken *)symbol->reshape(make_result(arena, value));
// call validation and semantic action, if present // call validation and semantic action, if present
if(symbol->pred && !symbol->pred(make_result(tarena, value))) { if(symbol->pred && !symbol->pred(make_result(tarena, value)))
// validation failed -> no parse; terminate return false; // validation failed -> no parse; terminate
engine->running = false;
return;
}
if(symbol->action) if(symbol->action)
value = (HParsedToken *)symbol->action(make_result(arena, value)); value = (HParsedToken *)symbol->action(make_result(arena, value));
@ -292,6 +286,8 @@ void h_lrengine_step(HLREngine *engine, const HLRAction *action)
h_slist_push(right, value); h_slist_push(right, value);
h_slist_push(right, symbol); h_slist_push(right, symbol);
} }
return true;
} }
HParseResult *h_lrengine_result(HLREngine *engine) HParseResult *h_lrengine_result(HLREngine *engine)
@ -317,9 +313,8 @@ HParseResult *h_lr_parse(HAllocator* mm__, const HParser* parser, HInputStream*
HArena *tarena = h_new_arena(mm__, 0); // tmp, deleted after parse HArena *tarena = h_new_arena(mm__, 0); // tmp, deleted after parse
HLREngine *engine = h_lrengine_new(arena, tarena, table); HLREngine *engine = h_lrengine_new(arena, tarena, table);
// run while the recognizer finds handles in the input // iterate engine to completion
while(engine->running) while(h_lrengine_step(engine, h_lrengine_action(engine, stream)));
h_lrengine_step(engine, h_lrengine_action(engine, stream));
HParseResult *result = h_lrengine_result(engine); HParseResult *result = h_lrengine_result(engine);
if(!result) if(!result)

View file

@ -69,7 +69,6 @@ typedef struct HLREngine_ {
HSlist *right; // right stack; input appears here HSlist *right; // right stack; input appears here
size_t state; size_t state;
bool running;
HArena *arena; // will hold the results HArena *arena; // will hold the results
HArena *tarena; // tmp, deleted after parse HArena *tarena; // tmp, deleted after parse
} HLREngine; } HLREngine;
@ -122,7 +121,7 @@ void h_lalr_free(HParser *parser);
const HLRAction *h_lr_lookup(const HLRTable *table, size_t state, const HCFChoice *symbol); const HLRAction *h_lr_lookup(const HLRTable *table, size_t state, const HCFChoice *symbol);
const HLRAction *h_lrengine_action(HLREngine *engine, HInputStream *stream); const HLRAction *h_lrengine_action(HLREngine *engine, HInputStream *stream);
void h_lrengine_step(HLREngine *engine, const HLRAction *action); bool h_lrengine_step(HLREngine *engine, const HLRAction *action);
HParseResult *h_lrengine_result(HLREngine *engine); HParseResult *h_lrengine_result(HLREngine *engine);
HParseResult *h_lr_parse(HAllocator* mm__, const HParser* parser, HInputStream* stream); HParseResult *h_lr_parse(HAllocator* mm__, const HParser* parser, HInputStream* stream);