| ... | @@ -126,6 +126,19 @@ static AstNode *ast_parse_container_decl_auto(ParseContext *pc); | ... | @@ -126,6 +126,19 @@ static AstNode *ast_parse_container_decl_auto(ParseContext *pc); |
| 126 | static AstNode *ast_parse_container_decl_type(ParseContext *pc); | 126 | static AstNode *ast_parse_container_decl_type(ParseContext *pc); |
| 127 | static AstNode *ast_parse_byte_align(ParseContext *pc); | 127 | static AstNode *ast_parse_byte_align(ParseContext *pc); |
| 128 | | 128 | |
| | 129 | ATTRIBUTE_NORETURN |
| | 130 | static void ast_error_offset(RootStruct *root_struct, ErrColor err_color, |
| | 131 | TokenIndex token, size_t bad_index, Buf *msg) |
| | 132 | { |
| | 133 | assert(token < root_struct->token_count); |
| | 134 | uint32_t byte_offset = root_struct->token_locs[token].offset; |
| | 135 | ErrorMsg *err = err_msg_create_with_offset(root_struct->path, |
| | 136 | byte_offset + bad_index, buf_ptr(root_struct->source_code), msg); |
| | 137 | |
| | 138 | print_err_msg(err, err_color); |
| | 139 | exit(EXIT_FAILURE); |
| | 140 | } |
| | 141 | |
| 129 | ATTRIBUTE_PRINTF(3, 4) | 142 | ATTRIBUTE_PRINTF(3, 4) |
| 130 | ATTRIBUTE_NORETURN | 143 | ATTRIBUTE_NORETURN |
| 131 | static void ast_error(ParseContext *pc, TokenIndex token, const char *format, ...) { | 144 | static void ast_error(ParseContext *pc, TokenIndex token, const char *format, ...) { |
| ... | @@ -135,13 +148,7 @@ static void ast_error(ParseContext *pc, TokenIndex token, const char *format, .. | ... | @@ -135,13 +148,7 @@ static void ast_error(ParseContext *pc, TokenIndex token, const char *format, .. |
| 135 | va_end(ap); | 148 | va_end(ap); |
| 136 | | 149 | |
| 137 | RootStruct *root_struct = pc->owner->data.structure.root_struct; | 150 | RootStruct *root_struct = pc->owner->data.structure.root_struct; |
| 138 | assert(token < root_struct->token_count); | 151 | ast_error_offset(root_struct, pc->err_color, token, 0, msg); |
| 139 | uint32_t byte_offset = root_struct->token_locs[token].offset; | | |
| 140 | ErrorMsg *err = err_msg_create_with_offset(root_struct->path, | | |
| 141 | byte_offset, buf_ptr(root_struct->source_code), msg); | | |
| 142 | | | |
| 143 | print_err_msg(err, pc->err_color); | | |
| 144 | exit(EXIT_FAILURE); | | |
| 145 | } | 152 | } |
| 146 | | 153 | |
| 147 | ATTRIBUTE_NORETURN | 154 | ATTRIBUTE_NORETURN |
| ... | @@ -202,20 +209,9 @@ static void put_back_token(ParseContext *pc) { | ... | @@ -202,20 +209,9 @@ static void put_back_token(ParseContext *pc) { |
| 202 | pc->current_token -= 1; | 209 | pc->current_token -= 1; |
| 203 | } | 210 | } |
| 204 | | 211 | |
| 205 | static Buf *token_string_literal_buf(RootStruct *root_struct, TokenIndex token) { | 212 | static Buf *token_buf(ParseContext *pc, TokenIndex token) { |
| 206 | Error err; | 213 | Error err; |
| 207 | assert(root_struct->token_ids[token] == TokenIdStringLiteral); | | |
| 208 | const char *source = buf_ptr(root_struct->source_code); | | |
| 209 | size_t byte_offset = root_struct->token_locs[token].offset; | | |
| 210 | size_t bad_index; | | |
| 211 | Buf *str = buf_alloc(); | | |
| 212 | if ((err = source_string_literal_buf(source + byte_offset, str, &bad_index))) { | | |
| 213 | zig_panic("TODO handle string literal parse error"); | | |
| 214 | } | | |
| 215 | return str; | | |
| 216 | } | | |
| 217 | | 214 | |
| 218 | static Buf *token_buf(ParseContext *pc, TokenIndex token) { | | |
| 219 | if (token == 0) | 215 | if (token == 0) |
| 220 | return nullptr; | 216 | return nullptr; |
| 221 | | 217 | |
| ... | @@ -223,7 +219,16 @@ static Buf *token_buf(ParseContext *pc, TokenIndex token) { | ... | @@ -223,7 +219,16 @@ static Buf *token_buf(ParseContext *pc, TokenIndex token) { |
| 223 | if (root_struct->token_ids[token] == TokenIdIdentifier) { | 219 | if (root_struct->token_ids[token] == TokenIdIdentifier) { |
| 224 | return token_identifier_buf(root_struct, token); | 220 | return token_identifier_buf(root_struct, token); |
| 225 | } else if (root_struct->token_ids[token] == TokenIdStringLiteral) { | 221 | } else if (root_struct->token_ids[token] == TokenIdStringLiteral) { |
| 226 | return token_string_literal_buf(root_struct, token); | 222 | assert(root_struct->token_ids[token] == TokenIdStringLiteral); |
| | 223 | const char *source = buf_ptr(root_struct->source_code); |
| | 224 | size_t byte_offset = root_struct->token_locs[token].offset; |
| | 225 | size_t bad_index; |
| | 226 | Buf *str = buf_alloc(); |
| | 227 | if ((err = source_string_literal_buf(source + byte_offset, str, &bad_index))) { |
| | 228 | ast_error_offset(root_struct, pc->err_color, token, bad_index, |
| | 229 | buf_create_from_str("invalid string literal character")); |
| | 230 | } |
| | 231 | return str; |
| 227 | } else { | 232 | } else { |
| 228 | zig_unreachable(); | 233 | zig_unreachable(); |
| 229 | } | 234 | } |
| ... | @@ -3493,7 +3498,8 @@ Buf *token_identifier_buf(RootStruct *root_struct, TokenIndex token) { | ... | @@ -3493,7 +3498,8 @@ Buf *token_identifier_buf(RootStruct *root_struct, TokenIndex token) { |
| 3493 | size_t bad_index; | 3498 | size_t bad_index; |
| 3494 | Buf *str = buf_alloc(); | 3499 | Buf *str = buf_alloc(); |
| 3495 | if ((err = source_string_literal_buf(source + byte_offset + 1, str, &bad_index))) { | 3500 | if ((err = source_string_literal_buf(source + byte_offset + 1, str, &bad_index))) { |
| 3496 | zig_panic("TODO handle string literal parse error"); | 3501 | ast_error_offset(root_struct, ErrColorAuto, token, bad_index + 1, |
| | 3502 | buf_create_from_str("invalid string literal character")); |
| 3497 | } | 3503 | } |
| 3498 | return str; | 3504 | return str; |
| 3499 | } else { | 3505 | } else { |