11#include "parts.h"
22
3- #include "../../Parser/tokenizer/cursor .h"
3+ #include "../../Parser/tokenizer/source .h"
44
55static int
66check (int condition , const char * message )
@@ -23,97 +23,31 @@ check_system_error(int failed, const char *message)
2323 return 0 ;
2424}
2525
26- static int
27- same_cursor (const _PyTok_Cursor * left , const _PyTok_Cursor * right )
28- {
29- return left -> source == right -> source &&
30- left -> pos == right -> pos &&
31- left -> line_start == right -> line_start &&
32- left -> line_end == right -> line_end &&
33- left -> lineno == right -> lineno ;
34- }
35-
3626static PyObject *
3727test_tokenizer_source (PyObject * Py_UNUSED (module ),
3828 PyObject * Py_UNUSED (args ))
3929{
4030 _PyTok_SourceText source ;
4131 _PyTok_SourceInit (& source );
4232
43- _PyTok_Loc loc ;
44- _PyTok_Line line ;
45- if (check (_PyTok_SourceLocation (
46- & source , 0 , _PYTOK_AFFINITY_RIGHT , & loc ) == 0 ,
47- "cannot locate empty source" ) < 0 ||
48- check (loc .lineno == 1 && loc .byte_col == 0 ,
49- "wrong empty source location" ) < 0 ||
50- check (_PyTok_SourceLine (& source , 1 , & line ) == 0 ,
51- "cannot find empty source line" ) < 0 ||
52- check (line .start == 0 && line .end == 0 ,
53- "wrong empty source line" ) < 0 ||
54- check_system_error (
55- _PyTok_SourceAppendLine (& source , "" , 0 , 0 ) < 0 ,
56- "accepted empty source line" ) < 0 ||
33+ if (check_system_error (
34+ _PyTok_SourceAppendLine (& source , "" , 0 , 0 ) < 0 ,
35+ "accepted empty source line" ) < 0 ||
5736 check_system_error (
5837 _PyTok_SourceAppendLine (& source , "a\nb\n" , 4 , 0 ) < 0 ,
5938 "accepted multiple source lines" ) < 0 ||
6039 check_system_error (
6140 _PyTok_SourceAppendLine (& source , "a" , 1 , 1 ) < 0 ,
62- "accepted missing implicit newline" ) < 0 ) {
63- goto error ;
64- }
65-
66- if (check (_PyTok_SourceAppendLine (& source , "alpha\n" , 6 , 0 ) == 0 ,
67- "wrong first source offset" ) < 0 ||
41+ "accepted missing implicit newline" ) < 0 ||
42+ check (_PyTok_SourceAppendLine (
43+ & source , "alpha\n" , 6 , 0 ) == 0 ,
44+ "wrong first source offset" ) < 0 ||
6845 check (_PyTok_SourceAppendLine (
6946 & source , "\xce\xb2\n" , 3 , 1 ) == 6 ,
7047 "wrong second source offset" ) < 0 ||
71- check (_PyTok_SourceAppendLine (
72- & source , "nul\0x\n" , 6 , 0 ) == 9 ,
73- "wrong third source offset" ) < 0 ) {
74- goto error ;
75- }
76-
77- int marker_line = 257 ;
78- int final_line = 300 ;
79- _PyTok_Off marker_start = -1 ;
80- for (int lineno = 4 ; lineno <= final_line ; lineno ++ ) {
81- const char * text = lineno == marker_line ? "marker\n" : "x\n" ;
82- Py_ssize_t len = (Py_ssize_t )strlen (text );
83- _PyTok_Off start = _PyTok_SourceAppendLine (
84- & source , text , len , lineno == final_line );
85- if (start < 0 ) {
86- goto error ;
87- }
88- if (lineno == marker_line ) {
89- marker_start = start ;
90- }
91- }
92-
93- if (check (source .nlines == final_line , "wrong source line count" ) < 0 ||
94- check (_PyTok_SourceLine (& source , marker_line , & line ) == 0 ,
95- "cannot find late source line" ) < 0 ||
96- check (line .start == marker_start &&
97- line .end == marker_start + 7 ,
98- "wrong late source line" ) < 0 ||
99- check (!line .implicit_newline && !line .contains_nul ,
100- "wrong late source flags" ) < 0 ||
101- check (_PyTok_SourceLine (& source , 2 , & line ) == 0 ,
102- "cannot find second source line" ) < 0 ||
103- check (line .start == 6 && line .end == 9 &&
104- line .implicit_newline && !line .contains_nul ,
105- "wrong second source line" ) < 0 ||
10648 check (!_PyTok_SourceLineIsImplicit (& source , 1 ) &&
10749 _PyTok_SourceLineIsImplicit (& source , 2 ),
108- "wrong early implicit newline flags" ) < 0 ||
109- check (_PyTok_SourceLine (& source , 3 , & line ) == 0 ,
110- "cannot find third source line" ) < 0 ||
111- check (line .contains_nul , "missing null byte flag" ) < 0 ||
112- check (_PyTok_SourceLine (& source , final_line , & line ) == 0 ,
113- "cannot find final source line" ) < 0 ||
114- check (line .implicit_newline &&
115- _PyTok_SourceLineIsImplicit (& source , final_line ),
116- "missing late implicit newline flag" ) < 0 ) {
50+ "wrong implicit newline flags" ) < 0 ) {
11751 goto error ;
11852 }
11953
@@ -123,217 +57,19 @@ test_tokenizer_source(PyObject *Py_UNUSED(module),
12357 if (check (view != NULL && view_len == 2 &&
12458 memcmp (view , "\xce\xb2" , 2 ) == 0 ,
12559 "wrong source span view" ) < 0 ||
126- check (_PyTok_SourceLocation (
127- & source , marker_start ,
128- _PYTOK_AFFINITY_LEFT , & loc ) == 0 ,
129- "cannot locate left line boundary" ) < 0 ||
130- check (loc .lineno == marker_line - 1 && loc .byte_col == 2 ,
131- "wrong left boundary location" ) < 0 ||
132- check (_PyTok_SourceLocation (
133- & source , marker_start ,
134- _PYTOK_AFFINITY_RIGHT , & loc ) == 0 ,
135- "cannot locate right line boundary" ) < 0 ||
136- check (loc .lineno == marker_line && loc .byte_col == 0 ,
137- "wrong right boundary location" ) < 0 ||
138- check (_PyTok_SourceLocation (
139- & source , marker_start + 1 ,
140- _PYTOK_AFFINITY_RIGHT , & loc ) == 0 ,
141- "cannot locate late source byte" ) < 0 ||
142- check (loc .lineno == marker_line && loc .byte_col == 1 ,
143- "wrong late source location" ) < 0 ) {
144- goto error ;
145- }
146-
147- if (check (_PyTok_SourceLocation (
148- & source , source .len , _PYTOK_AFFINITY_LEFT , & loc ) == 0 ,
149- "cannot locate left EOF" ) < 0 ||
150- check (loc .lineno == final_line && loc .byte_col == 2 ,
151- "wrong left EOF location" ) < 0 ||
152- check (_PyTok_SourceLocation (
153- & source , source .len ,
154- _PYTOK_AFFINITY_RIGHT , & loc ) == 0 ,
155- "cannot locate right EOF" ) < 0 ||
156- check (loc .lineno == final_line + 1 && loc .byte_col == 0 ,
157- "wrong right EOF location" ) < 0 ||
158- check (_PyTok_SourceLine (& source , final_line + 1 , & line ) == 0 ,
159- "cannot find virtual EOF line" ) < 0 ||
160- check (line .start == source .len && line .end == source .len ,
161- "wrong virtual EOF line" ) < 0 ||
162- check (!_PyTok_SourceLineIsImplicit (& source , 0 ) &&
163- !_PyTok_SourceLineIsImplicit (
164- & source , final_line + 1 ),
165- "virtual or invalid line is implicit" ) < 0 ) {
166- goto error ;
167- }
168-
169- view = _PyTok_SourceSpanView (
170- & source , _PyTok_SpanFromBounds (0 , source .len + 1 ), & view_len );
171- if (check_system_error (view == NULL , "accepted invalid source span" ) < 0 ||
17260 check_system_error (
173- _PyTok_SourceLocation (
174- & source , source .len + 1 ,
175- _PYTOK_AFFINITY_RIGHT , & loc ) < 0 ,
176- "accepted invalid source offset" ) < 0 ||
177- check_system_error (
178- _PyTok_SourceLine (& source , final_line + 2 , & line ) < 0 ,
179- "accepted invalid source line" ) < 0 ) {
61+ _PyTok_SourceSpanView (
62+ & source , _PyTok_SpanFromBounds (0 , source .len + 1 ),
63+ & view_len ) == NULL ,
64+ "accepted invalid source span" ) < 0 ) {
18065 goto error ;
18166 }
18267
18368 _PyTok_SourceClear (& source );
184- _PyTok_SourceInit (& source );
18569 if (_PyTok_SourceAppendLine (& source , "tail" , 4 , 0 ) < 0 ||
18670 check_system_error (
18771 _PyTok_SourceAppendLine (& source , "x\n" , 2 , 0 ) < 0 ,
188- "appended after unterminated source line" ) < 0 ||
189- check (_PyTok_SourceLocation (
190- & source , source .len ,
191- _PYTOK_AFFINITY_RIGHT , & loc ) == 0 ,
192- "cannot locate unterminated EOF" ) < 0 ||
193- check (loc .lineno == 1 && loc .byte_col == 4 ,
194- "wrong unterminated EOF location" ) < 0 ) {
195- goto error ;
196- }
197-
198- _PyTok_SourceDiscard (& source );
199- if (check (_PyTok_SourceAppendLine (& source , "a\n" , 2 , 0 ) == 4 ,
200- "wrong retained source offset" ) < 0 ||
201- _PyTok_SourceLine (& source , 1 , & line ) < 0 ||
202- check (line .start == 4 && line .end == 6 ,
203- "wrong retained source line" ) < 0 ||
204- _PyTok_SourceLocation (
205- & source , 4 , _PYTOK_AFFINITY_LEFT , & loc ) < 0 ||
206- check (loc .lineno == 1 && loc .byte_col == 0 ,
207- "wrong retained source location" ) < 0 ) {
208- goto error ;
209- }
210- view = _PyTok_SourceSpanView (
211- & source , _PyTok_SpanFromBounds (4 , 5 ), & view_len );
212- if (check (view != NULL && view_len == 1 && view [0 ] == 'a' ,
213- "wrong retained source span" ) < 0 ||
214- check_system_error (_PyTok_SourceSpanView (
215- & source , _PyTok_SpanFromBounds (0 , 1 ), & view_len ) == NULL ,
216- "accepted discarded source span" ) < 0 ) {
217- goto error ;
218- }
219-
220- _PyTok_SourceClear (& source );
221- Py_RETURN_NONE ;
222-
223- error :
224- _PyTok_SourceClear (& source );
225- return NULL ;
226- }
227-
228- static PyObject *
229- test_tokenizer_cursor (PyObject * Py_UNUSED (module ),
230- PyObject * Py_UNUSED (args ))
231- {
232- _PyTok_SourceText source ;
233- _PyTok_SourceInit (& source );
234- if (_PyTok_SourceAppendLine (& source , "ab\n" , 3 , 0 ) < 0 ||
235- _PyTok_SourceAppendLine (& source , "cd\n" , 3 , 0 ) < 0 ) {
236- goto error ;
237- }
238-
239- _PyTok_Cursor cursor ;
240- _PyTok_CursorInit (& cursor , & source );
241- if (_PyTok_CursorSetOffset (& cursor , source .len ) < 0 ||
242- check (cursor .lineno == 3 && cursor .pos == source .len ,
243- "wrong cursor at virtual EOF" ) < 0 ||
244- _PyTok_CursorSetLine (& cursor , 1 ) < 0 ) {
245- goto error ;
246- }
247-
248- char large [BUFSIZ + 1 ];
249- memset (large , 'z' , sizeof (large ));
250- large [sizeof (large ) - 1 ] = '\n' ;
251- if (_PyTok_SourceAppendLine (& source , large , sizeof (large ), 0 ) < 0 ) {
252- goto error ;
253- }
254-
255- if (check (_PyTok_CursorPeek (& cursor , 0 ) == 'a' ,
256- "wrong cursor peek after relocation" ) < 0 ||
257- check (_PyTok_CursorPeek (& cursor , 1 ) == 'b' ,
258- "wrong distant cursor peek" ) < 0 ||
259- check (_PyTok_CursorAdvance (& cursor ) == 'a' ,
260- "wrong first cursor byte" ) < 0 ||
261- check (_PyTok_CursorAdvance (& cursor ) == 'b' ,
262- "wrong second cursor byte" ) < 0 ||
263- check (_PyTok_CursorAdvance (& cursor ) == '\n' ,
264- "wrong final cursor byte" ) < 0 ||
265- check (_PyTok_CursorAdvance (& cursor ) == EOF ,
266- "cursor advanced past line" ) < 0 ||
267- check (_PyTok_CursorSetOffset (& cursor , 2 ) == 0 ,
268- "cannot seek cursor offset" ) < 0 ||
269- check (_PyTok_CursorAdvance (& cursor ) == '\n' ,
270- "wrong cursor byte after seek" ) < 0 ||
271- check (_PyTok_CursorSetOffset (& cursor , 3 ) == 0 ,
272- "cannot seek line boundary" ) < 0 ||
273- check (cursor .lineno == 2 && cursor .line_start == 3 &&
274- _PyTok_CursorAdvance (& cursor ) == 'c' ,
275- "wrong cursor at line boundary" ) < 0 ||
276- check (_PyTok_CursorSetLine (& cursor , 3 ) == 0 ,
277- "cannot advance cursor to final line" ) < 0 ||
278- check (cursor .line_start == 6 &&
279- _PyTok_CursorAdvance (& cursor ) == 'z' ,
280- "wrong cursor byte on final line" ) < 0 ) {
281- goto error ;
282- }
283-
284- _PyTok_Cursor saved = cursor ;
285- if (check_system_error (
286- _PyTok_CursorSetOffset (& cursor , source .len + 1 ) < 0 ,
287- "accepted invalid cursor offset" ) < 0 ||
288- check (same_cursor (& cursor , & saved ),
289- "invalid offset changed cursor" ) < 0 ||
290- check_system_error (
291- _PyTok_CursorSetLine (& cursor , source .nlines + 2 ) < 0 ,
292- "accepted invalid cursor line" ) < 0 ||
293- check (same_cursor (& cursor , & saved ),
294- "invalid line changed cursor" ) < 0 ||
295- check (_PyTok_CursorSetOffset (& cursor , source .len ) == 0 ,
296- "cannot set cursor to EOF" ) < 0 ||
297- check (cursor .lineno == 4 && cursor .pos == source .len ,
298- "wrong cursor at EOF" ) < 0 ) {
299- goto error ;
300- }
301-
302- #if SIZEOF_VOID_P > 4
303- char byte = 0 ;
304- _PyTok_SourceText huge_source = {
305- .bytes = & byte ,
306- .len = (_PyTok_Off )INT_MAX + 1 ,
307- };
308- _PyTok_Cursor huge_cursor = {
309- .source = & huge_source ,
310- .pos = INT_MAX ,
311- .line_end = (_PyTok_Off )INT_MAX + 1 ,
312- .lineno = 1 ,
313- };
314- if (check (_PyTok_CursorAdvance (& huge_cursor ) == EOF &&
315- huge_cursor .pos == INT_MAX ,
316- "cursor advanced past maximum column" ) < 0 ) {
317- goto error ;
318- }
319- #endif
320-
321- _PyTok_Off base = source .len ;
322- _PyTok_SourceDiscard (& source );
323- if (_PyTok_SourceAppendLine (& source , "ab\n" , 3 , 0 ) < 0 ||
324- _PyTok_SourceAppendLine (& source , "cd" , 2 , 0 ) < 0 ) {
325- goto error ;
326- }
327- _PyTok_CursorInit (& cursor , & source );
328- if (_PyTok_CursorSetLine (& cursor , 1 ) < 0 ||
329- check (cursor .pos == base && _PyTok_CursorPeek (& cursor , 1 ) == 'b' ,
330- "wrong retained cursor line" ) < 0 ||
331- _PyTok_CursorSetLine (& cursor , 2 ) < 0 ||
332- check (_PyTok_CursorAdvance (& cursor ) == 'c' ,
333- "wrong retained cursor byte" ) < 0 ||
334- _PyTok_CursorSetOffset (& cursor , base + 5 ) < 0 ||
335- check (cursor .lineno == 2 && _PyTok_CursorAdvance (& cursor ) == EOF ,
336- "wrong retained cursor EOF" ) < 0 ) {
72+ "appended after unterminated source line" ) < 0 ) {
33773 goto error ;
33874 }
33975
@@ -410,7 +146,6 @@ test_tokenizer_source_discard(PyObject *Py_UNUSED(module),
410146
411147static PyMethodDef test_methods [] = {
412148 {"test_tokenizer_source" , test_tokenizer_source , METH_NOARGS },
413- {"test_tokenizer_cursor" , test_tokenizer_cursor , METH_NOARGS },
414149 {"test_tokenizer_source_discard" , test_tokenizer_source_discard , METH_NOARGS },
415150 {NULL },
416151};
0 commit comments