Skip to content
Snippets Groups Projects
Commit 745aaebe authored by Sven M. Hallberg's avatar Sven M. Hallberg
Browse files

fix desugaring of h_whitespace and add reshape action

parent d081cf42
No related branches found
No related tags found
No related merge requests found
#include <ctype.h>
#include <assert.h>
#include "parser_internal.h"
static HParseResult* parse_whitespace(void* env, HParseState *state) {
......@@ -14,28 +15,47 @@ static HParseResult* parse_whitespace(void* env, HParseState *state) {
return h_do_parse((HParser*)env, state);
}
static const char SPACE_CHRS[6] = {' ', '\f', '\n', '\r', '\t', '\v'};
static const HParsedToken *h_act_last(const HParseResult *p) {
assert(p->ast);
assert(p->ast->token_type == TT_SEQUENCE);
return p->ast->seq->elements[p->ast->seq->used-1];
}
static HCFChoice* desugar_whitespace(HAllocator *mm__, void *env) {
HCFChoice *ret = h_new(HCFChoice, 1);
ret->type = HCF_CHOICE;
ret->seq = h_new(HCFSequence*, 3);
HCFChoice *ws = h_new(HCFChoice, 1);
ws->type = HCF_CHOICE;
ws->seq = h_new(HCFSequence*, 3);
HCFSequence *nonempty = h_new(HCFSequence, 1);
nonempty->items = h_new(HCFChoice*, 3);
nonempty->items[0] = h_new(HCFChoice, 1);
nonempty->items[0]->type = HCF_CHARSET;
nonempty->items[0]->charset = new_charset(mm__);
charset_set(nonempty->items[0]->charset, '\t', 1);
charset_set(nonempty->items[0]->charset, ' ', 1);
charset_set(nonempty->items[0]->charset, '\n', 1);
charset_set(nonempty->items[0]->charset, '\r', 1);
nonempty->items[1] = ret; // yay circular pointer!
for(size_t i=0; i<sizeof(SPACE_CHRS); i++)
charset_set(nonempty->items[0]->charset, SPACE_CHRS[i], 1);
nonempty->items[1] = ws; // yay circular pointer!
nonempty->items[2] = NULL;
ret->seq[0] = nonempty;
ws->seq[0] = nonempty;
HCFSequence *empty = h_new(HCFSequence, 1);
empty->items = h_new(HCFChoice*, 1);
empty->items[0] = NULL;
ret->seq[1] = empty;
ret->seq[2] = NULL;
ret->action = NULL;
ws->seq[1] = empty;
ws->seq[2] = NULL;
HCFChoice *ret = h_new(HCFChoice, 1);
ret->type = HCF_CHOICE;
ret->seq = h_new(HCFSequence*, 2);
ret->seq[0] = h_new(HCFSequence, 1);
ret->seq[0]->items = h_new(HCFChoice*, 3);
ret->seq[0]->items[0] = ws;
ret->seq[0]->items[1] = h_desugar(mm__, (HParser *)env);
ret->seq[0]->items[2] = NULL;
ret->seq[1] = NULL;
ret->reshape = h_act_last;
return ret;
}
......@@ -53,7 +73,6 @@ static bool ws_ctrvm(HRVMProg *prog, void *env) {
HParser *p = (HParser*)env;
uint16_t start = h_rvm_get_ip(prog);
uint16_t next;
const char SPACE_CHRS[6] = {' ', '\f', '\n', '\r', '\t', '\v'};
for (int i = 0; i < 6; i++) {
next = h_rvm_insert_insn(prog, RVM_FORK, 0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment