X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=lexer.c;h=b00b8a22481625ec455015dba152c10ca47fb853;hb=8c2442b5efb0f534ac76b62f2b0cee24481991f9;hp=be337acce4e19e779e53bb3892eace1a2b6f5508;hpb=db53eee277e14326fb656c9c389be4b50504c7ee;p=cparser diff --git a/lexer.c b/lexer.c index be337ac..b00b8a2 100644 --- a/lexer.c +++ b/lexer.c @@ -62,8 +62,8 @@ static void parse_error(const char *msg) static inline void next_real_char(void) { - bufpos++; - if(bufpos >= bufend) { + assert(bufpos <= bufend); + if (bufpos >= bufend) { size_t s = fread(buf + MAX_PUTBACK, 1, sizeof(buf) - MAX_PUTBACK, input); if(s == 0) { @@ -73,20 +73,13 @@ static inline void next_real_char(void) bufpos = buf + MAX_PUTBACK; bufend = buf + MAX_PUTBACK + s; } - c = *(bufpos); + c = *bufpos++; } static inline void put_back(int pc) { - assert(bufpos >= buf); - //assert(bufpos < buf+MAX_PUTBACK || *bufpos == pc); - - char *p = buf + (bufpos - buf); - *p = (char) pc; - - /* going backwards in the buffer is legal as long as it's not more often - * than MAX_PUTBACK */ - bufpos--; + assert(bufpos > buf); + *(--bufpos - buf + buf) = (char) pc; #ifdef DEBUG_CHARS printf("putback '%c'\n", pc); @@ -677,28 +670,30 @@ static int parse_escape_sequence(void) } } -const char *concat_strings(const char *s1, const char *s2) +string_t concat_strings(const string_t *const s1, const string_t *const s2) { - size_t len1 = strlen(s1); - size_t len2 = strlen(s2); + const size_t len1 = s1->size - 1; + const size_t len2 = s2->size - 1; - char *concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1); - memcpy(concat, s1, len1); - memcpy(concat + len1, s2, len2 + 1); + char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1); + memcpy(concat, s1->begin, len1); + memcpy(concat + len1, s2->begin, len2 + 1); +#if 0 /* TODO hash */ const char *result = strset_insert(&stringset, concat); if(result != concat) { obstack_free(&symbol_obstack, concat); } return result; +#else + return (string_t){ concat, len1 + len2 + 1 }; +#endif } static void parse_string_literal(void) { - unsigned start_linenr = lexer_token.source_position.linenr; - char *string; - const char *result; + const unsigned start_linenr = lexer_token.source_position.linenr; assert(c == '"'); next_char(); @@ -735,16 +730,22 @@ end_of_string: /* add finishing 0 to the string */ obstack_1grow(&symbol_obstack, '\0'); - string = obstack_finish(&symbol_obstack); + const size_t size = (size_t)obstack_object_size(&symbol_obstack); + const char *const string = obstack_finish(&symbol_obstack); +#if 0 /* TODO hash */ /* check if there is already a copy of the string */ result = strset_insert(&stringset, string); if(result != string) { obstack_free(&symbol_obstack, string); } +#else + const char *const result = string; +#endif - lexer_token.type = T_STRING_LITERAL; - lexer_token.v.string = result; + lexer_token.type = T_STRING_LITERAL; + lexer_token.v.string.begin = result; + lexer_token.v.string.size = size; } static void parse_wide_character_constant(void) @@ -996,7 +997,7 @@ static void parse_line_directive(void) next_pp_token(); } if(pp_token.type == T_STRING_LITERAL) { - lexer_token.source_position.input_name = pp_token.v.string; + lexer_token.source_position.input_name = pp_token.v.string.begin; next_pp_token(); } @@ -1303,6 +1304,8 @@ void lexer_open_stream(FILE *stream, const char *input_name) lexer_token.source_position.input_name = input_name; symbol_L = symbol_table_insert("L"); + bufpos = NULL; + bufend = NULL; /* place a virtual \n at the beginning so the lexer knows that we're * at the beginning of a line */