X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Flexer%2Flexer.c;h=8d2468ab7bb31aae1e13e1eff902f0a2b88daa9f;hb=685be7e2f49916d06005ba8588dcf9d0be896aac;hp=36680f52bca43f2b85aa493ce77b539e8adf76b6;hpb=35c08c1615757db98c03c3947c151ea3b2179f71;p=pspp diff --git a/src/language/lexer/lexer.c b/src/language/lexer/lexer.c index 36680f52bc..8d2468ab7b 100644 --- a/src/language/lexer/lexer.c +++ b/src/language/lexer/lexer.c @@ -272,7 +272,7 @@ lex_next_error (struct lexer *lexer, int n0, int n1, const char *format, ...) /* Prints a syntax error message saying that OPTION0 or one of the other strings following it, up to the first NULL, is expected. */ void -lex_error_expecting (struct lexer *lexer, const char *option0, ...) +(lex_error_expecting) (struct lexer *lexer, const char *option0, ...) { enum { MAX_OPTIONS = 8 }; const char *options[MAX_OPTIONS + 1]; @@ -428,14 +428,14 @@ lex_end_of_command (struct lexer *lexer) /* Returns true if the current token is a number. */ bool -lex_is_number (struct lexer *lexer) +lex_is_number (const struct lexer *lexer) { return lex_next_is_number (lexer, 0); } /* Returns true if the current token is a string. */ bool -lex_is_string (struct lexer *lexer) +lex_is_string (const struct lexer *lexer) { return lex_next_is_string (lexer, 0); } @@ -443,14 +443,14 @@ lex_is_string (struct lexer *lexer) /* Returns the value of the current token, which must be a floating point number. */ double -lex_number (struct lexer *lexer) +lex_number (const struct lexer *lexer) { return lex_next_number (lexer, 0); } /* Returns true iff the current token is an integer. */ bool -lex_is_integer (struct lexer *lexer) +lex_is_integer (const struct lexer *lexer) { return lex_next_is_integer (lexer, 0); } @@ -458,7 +458,7 @@ lex_is_integer (struct lexer *lexer) /* Returns the value of the current token, which must be an integer. */ long -lex_integer (struct lexer *lexer) +lex_integer (const struct lexer *lexer) { return lex_next_integer (lexer, 0); } @@ -472,7 +472,7 @@ lex_integer (struct lexer *lexer) /* Returns true if the token N ahead of the current token is a number. */ bool -lex_next_is_number (struct lexer *lexer, int n) +lex_next_is_number (const struct lexer *lexer, int n) { enum token_type next_token = lex_next_token (lexer, n); return next_token == T_POS_NUM || next_token == T_NEG_NUM; @@ -480,7 +480,7 @@ lex_next_is_number (struct lexer *lexer, int n) /* Returns true if the token N ahead of the current token is a string. */ bool -lex_next_is_string (struct lexer *lexer, int n) +lex_next_is_string (const struct lexer *lexer, int n) { return lex_next_token (lexer, n) == T_STRING; } @@ -488,7 +488,7 @@ lex_next_is_string (struct lexer *lexer, int n) /* Returns the value of the token N ahead of the current token, which must be a floating point number. */ double -lex_next_number (struct lexer *lexer, int n) +lex_next_number (const struct lexer *lexer, int n) { assert (lex_next_is_number (lexer, n)); return lex_next_tokval (lexer, n); @@ -496,7 +496,7 @@ lex_next_number (struct lexer *lexer, int n) /* Returns true if the token N ahead of the current token is an integer. */ bool -lex_next_is_integer (struct lexer *lexer, int n) +lex_next_is_integer (const struct lexer *lexer, int n) { double value; @@ -510,7 +510,7 @@ lex_next_is_integer (struct lexer *lexer, int n) /* Returns the value of the token N ahead of the current token, which must be an integer. */ long -lex_next_integer (struct lexer *lexer, int n) +lex_next_integer (const struct lexer *lexer, int n) { assert (lex_next_is_integer (lexer, n)); return lex_next_tokval (lexer, n); @@ -588,7 +588,7 @@ lex_force_match_id (struct lexer *lexer, const char *identifier) return true; else { - lex_error_expecting (lexer, identifier, NULL_SENTINEL); + lex_error_expecting (lexer, identifier); return false; } } @@ -609,11 +609,11 @@ lex_force_match (struct lexer *lexer, enum token_type type) if (type_string) { char *s = xasprintf ("`%s'", type_string); - lex_error_expecting (lexer, s, NULL_SENTINEL); + lex_error_expecting (lexer, s); free (s); } else - lex_error_expecting (lexer, token_type_to_name (type), NULL_SENTINEL); + lex_error_expecting (lexer, token_type_to_name (type)); return false; } @@ -1231,7 +1231,10 @@ lex_ellipsize__ (struct substring in, char *out, size_t out_size) int mblen; assert (out_size >= 16); - out_maxlen = out_size - (in.length >= out_size ? 3 : 0) - 1; + out_maxlen = out_size - 1; + if (in.length > out_maxlen - 3) + out_maxlen -= 3; + for (out_len = 0; out_len < in.length; out_len += mblen) { if (in.string[out_len] == '\n' @@ -1243,6 +1246,10 @@ lex_ellipsize__ (struct substring in, char *out, size_t out_size) mblen = u8_mblen (CHAR_CAST (const uint8_t *, in.string + out_len), in.length - out_len); + + if (mblen < 0) + break; + if (out_len + mblen > out_maxlen) break; } @@ -1257,7 +1264,6 @@ lex_source_error_valist (struct lex_source *src, int n0, int n1, { const struct lex_token *token; struct string s; - struct msg m; ds_init_empty (&s); @@ -1285,14 +1291,16 @@ lex_source_error_valist (struct lex_source *src, int n0, int n1, } ds_put_byte (&s, '.'); - m.category = MSG_C_SYNTAX; - m.severity = MSG_S_ERROR; - m.file_name = src->reader->file_name; - m.first_line = lex_source_get_first_line_number (src, n0); - m.last_line = lex_source_get_last_line_number (src, n1); - m.first_column = lex_source_get_first_column (src, n0); - m.last_column = lex_source_get_last_column (src, n1); - m.text = ds_steal_cstr (&s); + struct msg m = { + .category = MSG_C_SYNTAX, + .severity = MSG_S_ERROR, + .file_name = src->reader->file_name, + .first_line = lex_source_get_first_line_number (src, n0), + .last_line = lex_source_get_last_line_number (src, n1), + .first_column = lex_source_get_first_column (src, n0), + .last_column = lex_source_get_last_column (src, n1), + .text = ds_steal_cstr (&s), + }; msg_emit (&m); } @@ -1434,13 +1442,9 @@ lex_source_get__ (const struct lex_source *src_) if (copy_len > 0 && line[copy_len - 1] == '\r') copy_len--; - /* Make a copy of the line with \n end-of-line and null terminator. */ - char *syntax = xmalloc (copy_len + 2); - memcpy (syntax, line, copy_len); - syntax[copy_len] = '\n'; - syntax[copy_len + 1] = '\0'; - - text_item_submit (text_item_create_nocopy (TEXT_ITEM_SYNTAX, syntax)); + /* Submit the line as syntax. */ + text_item_submit (text_item_create_nocopy (TEXT_ITEM_SYNTAX, + xmemdup0 (line, copy_len))); src->journal_pos += line_len; }