|
1 | 1 | #include "Python.h" |
2 | | -#include "errcode.h" |
3 | | - |
| 2 | +#include "buffer.h" |
4 | 3 | #include "state.h" |
5 | 4 |
|
6 | | -/* Traverse and remember all f-string buffers, in order to be able to restore |
7 | | - them after reallocating tok->buf */ |
8 | 5 | void |
9 | | -_PyLexer_remember_fstring_buffers(struct tok_state *tok) |
| 6 | +_PyLexer_SnapshotBuffer(struct tok_state *tok, const char *base, |
| 7 | + _PyLexer_BufferSnapshot *snapshot) |
10 | 8 | { |
11 | | - int index; |
12 | | - tokenizer_mode *mode; |
13 | | - |
14 | | - for (index = tok->tok_mode_stack_index; index >= 0; --index) { |
15 | | - mode = &(tok->tok_mode_stack[index]); |
| 9 | + snapshot->buf = tok->buf - base; |
| 10 | + snapshot->cur = tok->cur - tok->buf; |
| 11 | + snapshot->inp = tok->inp - tok->buf; |
| 12 | + snapshot->start = tok->start == NULL ? -1 : tok->start - tok->buf; |
| 13 | + snapshot->line_start = tok->line_start == NULL |
| 14 | + ? -1 : tok->line_start - tok->buf; |
| 15 | + snapshot->multi_line_start = tok->multi_line_start == NULL |
| 16 | + ? -1 : tok->multi_line_start - tok->buf; |
| 17 | + for (int index = tok->tok_mode_stack_index; index >= 0; --index) { |
| 18 | + tokenizer_mode *mode = &tok->tok_mode_stack[index]; |
16 | 19 | mode->start_offset = mode->start == NULL ? -1 : mode->start - tok->buf; |
17 | | - mode->multi_line_start_offset = mode->multi_line_start == NULL ? -1 : mode->multi_line_start - tok->buf; |
| 20 | + mode->multi_line_start_offset = mode->multi_line_start == NULL |
| 21 | + ? -1 : mode->multi_line_start - tok->buf; |
18 | 22 | } |
19 | 23 | } |
20 | 24 |
|
21 | | -/* Traverse and restore all f-string buffers after reallocating tok->buf */ |
22 | 25 | void |
23 | | -_PyLexer_restore_fstring_buffers(struct tok_state *tok) |
24 | | -{ |
25 | | - int index; |
26 | | - tokenizer_mode *mode; |
27 | | - |
28 | | - for (index = tok->tok_mode_stack_index; index >= 0; --index) { |
29 | | - mode = &(tok->tok_mode_stack[index]); |
30 | | - mode->start = mode->start_offset < 0 ? NULL : tok->buf + mode->start_offset; |
31 | | - mode->multi_line_start = mode->multi_line_start_offset < 0 ? NULL : tok->buf + mode->multi_line_start_offset; |
32 | | - } |
33 | | -} |
34 | | - |
35 | | -int |
36 | | -_PyLexer_tok_reserve_buf(struct tok_state *tok, Py_ssize_t size) |
| 26 | +_PyLexer_RestoreBuffer(struct tok_state *tok, char *base, |
| 27 | + const _PyLexer_BufferSnapshot *snapshot) |
37 | 28 | { |
38 | | - Py_ssize_t cur = tok->cur - tok->buf; |
39 | | - Py_ssize_t oldsize = tok->inp - tok->buf; |
40 | | - Py_ssize_t newsize = oldsize + Py_MAX(size, oldsize >> 1); |
41 | | - if (newsize > tok->end - tok->buf) { |
42 | | - char *newbuf = tok->buf; |
43 | | - Py_ssize_t start = tok->start == NULL ? -1 : tok->start - tok->buf; |
44 | | - Py_ssize_t line_start = tok->start == NULL ? -1 : tok->line_start - tok->buf; |
45 | | - Py_ssize_t multi_line_start = tok->multi_line_start - tok->buf; |
46 | | - _PyLexer_remember_fstring_buffers(tok); |
47 | | - newbuf = (char *)PyMem_Realloc(newbuf, newsize); |
48 | | - if (newbuf == NULL) { |
49 | | - tok->done = E_NOMEM; |
50 | | - return 0; |
51 | | - } |
52 | | - tok->buf = newbuf; |
53 | | - tok->cur = tok->buf + cur; |
54 | | - tok->inp = tok->buf + oldsize; |
55 | | - tok->end = tok->buf + newsize; |
56 | | - tok->start = start < 0 ? NULL : tok->buf + start; |
57 | | - tok->line_start = line_start < 0 ? NULL : tok->buf + line_start; |
58 | | - tok->multi_line_start = multi_line_start < 0 ? NULL : tok->buf + multi_line_start; |
59 | | - _PyLexer_restore_fstring_buffers(tok); |
| 29 | + tok->buf = base + snapshot->buf; |
| 30 | + tok->cur = tok->buf + snapshot->cur; |
| 31 | + tok->inp = tok->buf + snapshot->inp; |
| 32 | + tok->start = snapshot->start < 0 |
| 33 | + ? NULL : tok->buf + snapshot->start; |
| 34 | + tok->line_start = snapshot->line_start < 0 |
| 35 | + ? NULL : tok->buf + snapshot->line_start; |
| 36 | + tok->multi_line_start = snapshot->multi_line_start < 0 |
| 37 | + ? NULL : tok->buf + snapshot->multi_line_start; |
| 38 | + for (int index = tok->tok_mode_stack_index; index >= 0; --index) { |
| 39 | + tokenizer_mode *mode = &tok->tok_mode_stack[index]; |
| 40 | + mode->start = mode->start_offset < 0 |
| 41 | + ? NULL : tok->buf + mode->start_offset; |
| 42 | + mode->multi_line_start = mode->multi_line_start_offset < 0 |
| 43 | + ? NULL : tok->buf + mode->multi_line_start_offset; |
60 | 44 | } |
61 | | - return 1; |
62 | 45 | } |
0 commit comments