diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2018-10-13 19:35:08 -0400 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2018-10-13 19:35:08 -0400 |
| commit | 981217b98220f7dc94c70fd6f4b7078ecca6020b (patch) | |
| tree | 6666e524a43889fa8175ee8d99fab7243249b2e4 | |
| parent | 864448e37769c96ba3db365de2680e2cff4fe023 (diff) | |
| download | libctemplates-981217b98220f7dc94c70fd6f4b7078ecca6020b.tar.gz libctemplates-981217b98220f7dc94c70fd6f4b7078ecca6020b.tar.bz2 libctemplates-981217b98220f7dc94c70fd6f4b7078ecca6020b.zip | |
Allowed spaces to be used in attributes
* Added an is_whitespace() function
* <TMPL_VAR ...> may have whitespace between
* name = "..."
* default = "..."
* <TMPL_IF ...> may have whitespace between
* name = "..."
* value = "..."
* <TMPL_ELSEIF ...> may have whitespace between
* name = "..."
* value = "..."
| -rw-r--r-- | ctemplates.c | 99 | ||||
| -rw-r--r-- | ctemplates.h | 10 |
2 files changed, 103 insertions, 6 deletions
diff --git a/ctemplates.c b/ctemplates.c index 8d4bbd6..aa7c650 100644 --- a/ctemplates.c +++ b/ctemplates.c @@ -62,6 +62,7 @@ void print_ast(struct TMPL_tagnode* root); struct TMPL_token* TMPL_tokenize(char* tmplstr, size_t strlen); struct TMPL_tagnode* alloc_tagnode(void); size_t get_quoted_string(char* start, size_t len); +int is_whitespace(char c); struct TMPL_tagnode* parse_text( struct TMPL_token* head, struct TMPL_buf* errbuf @@ -163,6 +164,7 @@ TMPL_alloc_token(){ ret->next = NULL; return ret; } + /*Frees an allocated token*/ void TMPL_free_token(struct TMPL_token* token){ @@ -492,6 +494,11 @@ get_quoted_string(char* start, size_t len){ return i; } +int +is_whitespace(char c){ + return (c == ' ' || c == '\t' || c == '\n') ? 1 : 0; +} + struct TMPL_tagnode* parse_text(struct TMPL_token* head, struct TMPL_buf* errbuf){ struct TMPL_tagnode* t = alloc_tagnode(); @@ -515,6 +522,18 @@ parse_elseif(struct TMPL_token* head, struct TMPL_buf* errbuf){ char* start_of_attribs = head->start + TAG_ELSEIF_LENGTH; int name_offset = kmp(start_of_attribs,head->length, ATTRIBUTE_VARNAME,ATTRIBUTE_VARNAME_LENGTH); char* start_of_name = start_of_attribs + name_offset + ATTRIBUTE_VARNAME_LENGTH; + while(is_whitespace(*start_of_name)) + start_of_name++; + if(*start_of_name != '='){ + bputs(errbuf,"Parsing error: Expected \"=\" in <TMPL_ELSEIF ...> after name near "); + bputsn(errbuf,head->start, ERR_MSG_LEN); + bputs(errbuf,"\n"); + }else{ + start_of_name++; + } + while(is_whitespace(*start_of_name)) + start_of_name++; + start_of_name++;//consume " size_t name_length = get_quoted_string(start_of_name,head->length); char* name = (char*)malloc(sizeof(char)*name_length); memcpy(name,start_of_name,name_length); @@ -526,6 +545,18 @@ parse_elseif(struct TMPL_token* head, struct TMPL_buf* errbuf){ t->TMPL_tag.ifelse.testval = NULL; }else{ char* start_of_value = start_of_attribs + testval_offset + ATTRIBUTE_VALUE_LENGTH; + while(is_whitespace(*start_of_value)) + start_of_value++; + if(*start_of_value != '='){ + bputs(errbuf,"Parsing error: Expected \"=\" in <TMPL_ELSEIF ...> after value near "); + bputsn(errbuf,head->start,ERR_MSG_LEN); + bputs(errbuf,"\n"); + }else{ + start_of_value++;//consume = + } + while(is_whitespace(*start_of_value)) + start_of_value++; + start_of_value++;//consume " size_t value_length = get_quoted_string(start_of_value,head->length); char* value = (char*)malloc(sizeof(char)*value_length); memcpy(value,start_of_value,value_length); @@ -577,6 +608,18 @@ parse_if(struct TMPL_token* head, struct TMPL_buf* errbuf){ /*Find the name of the varialbe*/ int name_offset = kmp(start_of_attribs,head->length,ATTRIBUTE_VARNAME,ATTRIBUTE_VARNAME_LENGTH); char* start_of_name = start_of_attribs + name_offset + ATTRIBUTE_VARNAME_LENGTH; + while(is_whitespace(*start_of_name)) + start_of_name++; + if(*start_of_name != '='){ + bputs(errbuf,"Parsing error: Expected \"=\" in <TMPL_IF ...> after name near "); + bputsn(errbuf, head->start, ERR_MSG_LEN); + bputs(errbuf,"\n"); + }else{ + start_of_name++; + } + while(is_whitespace(*start_of_name)) + start_of_name++; + start_of_name++;//consume " size_t name_length = get_quoted_string(start_of_name,head->length); char* name = (char*)malloc(sizeof(char)*name_length); memcpy(name,start_of_name,name_length); @@ -588,6 +631,18 @@ parse_if(struct TMPL_token* head, struct TMPL_buf* errbuf){ t->TMPL_tag.ifelse.testval = NULL; }else{ char* start_of_value = start_of_attribs + testval_offset + ATTRIBUTE_VALUE_LENGTH; + while(is_whitespace(*start_of_value)) + start_of_value++; + if(*start_of_value != '='){ + bputs(errbuf,"Parsing error: Expected \"=\" in <TMPL_IF/TMPL_ELSEIF ...> after value near "); + bputsn(errbuf,head->start,ERR_MSG_LEN); + bputs(errbuf,"\n"); + }else{ + start_of_value++; + } + while(is_whitespace(*start_of_value)) + start_of_value++; + start_of_value++;//consume " size_t value_length = get_quoted_string(start_of_value,head->length); char* value = (char*)malloc(sizeof(char)*value_length); memcpy(value,start_of_value,value_length); @@ -647,6 +702,18 @@ parse_loop(struct TMPL_token* head, struct TMPL_buf* errbuf){ } if(name_offset >= 0){ char* start_of_name = loop_start + name_offset + ATTRIBUTE_VARNAME_LENGTH; + while(is_whitespace(*start_of_name)) + start_of_name++; + if(*start_of_name != '='){ + bputs(errbuf,"Parsing error: Expected \"=\" in <TMPL_LOOP ...> after name near "); + bputsn(errbuf, head->start, ERR_MSG_LEN); + bputs(errbuf,"\n"); + }else{ + start_of_name++;//consume '=' + } + while(is_whitespace(*start_of_name)) + start_of_name++; + start_of_name++;//consume " 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); @@ -681,20 +748,38 @@ parse_loop(struct TMPL_token* head, struct TMPL_buf* errbuf){ return t; } +/*Parses the <TMPL_VAR ...> tokens*/ struct TMPL_tagnode* parse_variable(struct TMPL_token* head, struct TMPL_buf* errbuf){ struct TMPL_tagnode* t = alloc_tagnode(); + /*<TMPL_VAR name="..." default="..."> + ^*/ char* start_of_attribs = head->start + TAG_VAR_LENGTH; + /*Find the length of attributes*/ int attribs_length = head->length - TAG_VAR_LENGTH; + /*Find where the "name" attribute is*/ int in_name = kmp(start_of_attribs,attribs_length,ATTRIBUTE_VARNAME,ATTRIBUTE_VARNAME_LENGTH); + /*Find where the "default" attribute is*/ int in_default = kmp(start_of_attribs,attribs_length,ATTRIBUTE_DEFAULT,ATTRIBUTE_DEFAULT_LENGTH); if(in_name == -1){ - bputs(errbuf,"Parsing error: Could not find \"name\" field in variable near"); + bputs(errbuf,"Parsing error: Could not find \"name\" field in <TMPL_VAR ...> near "); bputsn(errbuf,head->start,ERR_MSG_LEN); bputs(errbuf,"\n"); } if(in_name >= 0){ char* start_of_name = start_of_attribs + in_name + ATTRIBUTE_VARNAME_LENGTH; + while(is_whitespace(*start_of_name)) + start_of_name++; + if(*start_of_name != '='){ + bputs(errbuf,"Parsing error: Expected \"=\" in <TMPL_VAR ...> after name near "); + bputsn(errbuf,head->start, ERR_MSG_LEN); + bputs(errbuf,"\n"); + }else{ + start_of_name++;//consume = + } + while(is_whitespace(*start_of_name)) + start_of_name++; + start_of_name++; //consume " size_t name_size = get_quoted_string(start_of_name,head->length); char* name = (char*)malloc(sizeof(char)*name_size); memcpy(name,start_of_name,name_size); @@ -704,6 +789,18 @@ parse_variable(struct TMPL_token* head, struct TMPL_buf* errbuf){ } if(in_default >= 0){ char* start_of_default = start_of_attribs + in_default + ATTRIBUTE_DEFAULT_LENGTH; + while(is_whitespace(*start_of_default)) + start_of_default++; + if(*start_of_default != '='){ + bputs(errbuf,"Parsing error: Expected \"=\" in <TMPL_VAR ...> after default near "); + bputsn(errbuf,head->start, ERR_MSG_LEN); + bputs(errbuf,"\n"); + }else{ + start_of_default++;//consume = + } + while(is_whitespace(*start_of_default)) + start_of_default++; + start_of_default++; //consume " size_t default_size = get_quoted_string(start_of_default,head->length); char* def = (char*) malloc(sizeof(char)*default_size); def[default_size] = '\0'; diff --git a/ctemplates.h b/ctemplates.h index 4744d14..d1b6bba 100644 --- a/ctemplates.h +++ b/ctemplates.h @@ -32,9 +32,9 @@ typedef struct TMPL_fmtlists TMPL_fmtlists; #define TAG_CONTINUE_TEXT "TMPL_CONTINUE" #define TAG_END_TEXT "TMPL_END" -#define ATTRIBUTE_VARNAME "name=\"" -#define ATTRIBUTE_DEFAULT "default=\"" -#define ATTRIBUTE_VALUE "value=\"" +#define ATTRIBUTE_VARNAME "name" +#define ATTRIBUTE_DEFAULT "default" +#define ATTRIBUTE_VALUE "value" #define ERRBUF_HINTLEN 50 @@ -175,8 +175,8 @@ struct TMPL_token{ enum TMPL_tagtype type;//The type of token size_t length;//Length of token, should always be end-start struct TMPL_token* next;//The next token - unsigned int line; - unsigned int character; + unsigned int line;// Not used currently + unsigned int character;// Not used currently }TMPL_token; void TMPL_add_var_to_varlist(struct TMPL_varlist* vl, char* name, char* value); |
