diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2017-12-29 01:21:32 -0500 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2017-12-29 01:21:32 -0500 |
| commit | e81f26718e5c95de6140631652f1319857602ff8 (patch) | |
| tree | fee2cc5f7d0c97c3d92309144f8022ad053f7d98 /ctemplates.c | |
| parent | 1d1cb9e3d003f23bddce0a744ffc60a7e82bf23c (diff) | |
| download | libctemplates-e81f26718e5c95de6140631652f1319857602ff8.tar.gz libctemplates-e81f26718e5c95de6140631652f1319857602ff8.tar.bz2 libctemplates-e81f26718e5c95de6140631652f1319857602ff8.zip | |
Fixed bugfixes
Fixed bug with tokenizing,
also fixed a bug with parsing loops
also fixed a bug with adding vars to a varlist
Diffstat (limited to 'ctemplates.c')
| -rw-r--r-- | ctemplates.c | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/ctemplates.c b/ctemplates.c index a470ebc..fb32b67 100644 --- a/ctemplates.c +++ b/ctemplates.c @@ -286,7 +286,8 @@ scan_text(char* start, size_t strlen, size_t* consumed){ enum TMPL_tagtype type = tag_null; char* cursor = start; while(type == tag_null){ - cursor = (char*)memchr(cursor,'<',strlen); + int left = strlen - (cursor - start); + cursor = (char*)memchr(cursor,'<',left); if(cursor == NULL){ cursor = start + (strlen > 0 ? strlen : 1); break; @@ -353,6 +354,7 @@ TMPL_add_var_to_varlist(struct TMPL_varlist* t, char* varname, char* var){ vi->type = vartype_var; size_t slen = strlen(var); vi->item.s = (char*)malloc(sizeof(char)*slen); + vi->item.s[slen] = '\0'; memcpy(vi->item.s,var,slen); int succ = hashmap_put(t->map,varname,vi); if(succ != MAP_OK){ @@ -426,38 +428,38 @@ print_ast(struct TMPL_tagnode* root){ } struct TMPL_token* -TMPL_tokenize(char* tmplstr, size_t strlen){ - if(strlen == 0) return NULL; +TMPL_tokenize(char* tmplstr, size_t m_strlen){ + if(m_strlen == 0) return NULL; struct TMPL_token* first; char* textcursor = tmplstr; size_t newlength; enum TMPL_tagtype ttype; - ttype = starts_with_token(tmplstr+1,strlen); - if(ttype == tag_null){ - first = scan_text(textcursor,strlen,&newlength); - first->type = tag_text; - }else{ - first = scan_tag(textcursor,strlen,&newlength); + ttype = starts_with_token(tmplstr+1,m_strlen); + if(*tmplstr == '<' && ttype != tag_null){ + first = scan_tag(textcursor,m_strlen,&newlength); first->type = ttype; + }else{ + first = scan_text(textcursor,m_strlen,&newlength); + first->type = tag_text; } textcursor += newlength; first->end = textcursor; first->length = newlength; - strlen -= newlength; + m_strlen -= newlength; struct TMPL_token* tokencursor = first; struct TMPL_token* newnode; while(*textcursor != '\0'){ - ttype = starts_with_token(textcursor+1,strlen); + ttype = starts_with_token(textcursor+1,m_strlen); if(ttype == tag_null){ - newnode = scan_text(textcursor,strlen,&newlength); + newnode = scan_text(textcursor,m_strlen,&newlength); newnode->type = tag_text; }else{ - newnode = scan_tag(textcursor,strlen,&newlength); + newnode = scan_tag(textcursor,m_strlen,&newlength); newnode->type = ttype; } tokencursor->next = newnode; textcursor += newlength; - strlen -= newlength; + m_strlen -= newlength; tokencursor = newnode; } return first; @@ -639,6 +641,7 @@ parse_loop(struct TMPL_token* head, struct TMPL_buf* errbuf){ char* start_of_name = loop_start + name_offset + ATTRIBUTE_VARNAME_LENGTH; size_t name_size = get_quoted_string(start_of_name,head->length); char* loopname = (char*)malloc(sizeof(char)*name_size); + memcpy(loopname,start_of_name,name_size); loopname[name_size] = '\0'; t->TMPL_tag.loop.loopname = loopname; @@ -782,6 +785,7 @@ parse(struct TMPL_token* head, struct TMPL_buf* errbuf){ if(head == NULL){ return NULL; } + printf("Parsing token of type: %d\n",head->type); switch(head->type){ case tag_text: root = parse_text(head,errbuf); @@ -805,6 +809,7 @@ parse(struct TMPL_token* head, struct TMPL_buf* errbuf){ root = NULL; break; default: + printf("Hit token of type: %d, exiting\n", head->type); exit(-1); break; } @@ -826,9 +831,15 @@ compile(char* tmplstr){ struct TMPL_templates* ret = alloc_templates(); ret->out = alloc_tmpl_buf(); ret->errout = alloc_tmpl_buf(); + printf("About to do hard things\n"); struct TMPL_token* tokens = TMPL_tokenize(tmplstr,slen); + print_tokens(tokens); + printf("Done tokenizeing\n"); struct TMPL_tagnode* ast = parse(tokens,ret->errout); + printf("Done making ast\n"); + print_ast(ast); if(ast == NULL){ + printf("Failed to compile\n"); }else{ ret->roottag = ast; } |
