/
tokenizer.c
3418 lines (3211 loc) · 98.9 KB
/
tokenizer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Tokenizer for MWParserFromHell
Copyright (C) 2012-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "tokenizer.h"
/*
Determine whether the given Py_UNICODE is a marker.
*/
static int is_marker(Py_UNICODE this)
{
int i;
for (i = 0; i < NUM_MARKERS; i++) {
if (MARKERS[i] == this)
return 1;
}
return 0;
}
/*
Given a context, return the heading level encoded within it.
*/
static int heading_level_from_context(int n)
{
int level;
n /= LC_HEADING_LEVEL_1;
for (level = 1; n > 1; n >>= 1)
level++;
return level;
}
/*
Call the given function in definitions.py, using 'in1', 'in2', and 'in3' as
parameters, and return its output as a bool.
*/
static int call_def_func(const char* funcname, PyObject* in1, PyObject* in2,
PyObject* in3)
{
PyObject* func = PyObject_GetAttrString(definitions, funcname);
PyObject* result = PyObject_CallFunctionObjArgs(func, in1, in2, in3, NULL);
int ans = (result == Py_True) ? 1 : 0;
Py_DECREF(func);
Py_DECREF(result);
return ans;
}
/*
Sanitize the name of a tag so it can be compared with others for equality.
*/
static PyObject* strip_tag_name(PyObject* token, int take_attr)
{
PyObject *text, *rstripped, *lowered;
if (take_attr) {
text = PyObject_GetAttrString(token, "text");
if (!text)
return NULL;
rstripped = PyObject_CallMethod(text, "rstrip", NULL);
Py_DECREF(text);
}
else
rstripped = PyObject_CallMethod(token, "rstrip", NULL);
if (!rstripped)
return NULL;
lowered = PyObject_CallMethod(rstripped, "lower", NULL);
Py_DECREF(rstripped);
return lowered;
}
static Textbuffer* Textbuffer_new(void)
{
Textbuffer* buffer = malloc(sizeof(Textbuffer));
if (!buffer) {
PyErr_NoMemory();
return NULL;
}
buffer->size = 0;
buffer->data = malloc(sizeof(Py_UNICODE) * TEXTBUFFER_BLOCKSIZE);
if (!buffer->data) {
free(buffer);
PyErr_NoMemory();
return NULL;
}
buffer->prev = buffer->next = NULL;
return buffer;
}
static void Textbuffer_dealloc(Textbuffer* self)
{
Textbuffer* next;
while (self) {
free(self->data);
next = self->next;
free(self);
self = next;
}
}
/*
Write a Unicode codepoint to the given textbuffer.
*/
static int Textbuffer_write(Textbuffer** this, Py_UNICODE code)
{
Textbuffer* self = *this;
if (self->size == TEXTBUFFER_BLOCKSIZE) {
Textbuffer* new = Textbuffer_new();
if (!new)
return -1;
new->next = self;
self->prev = new;
*this = self = new;
}
self->data[self->size++] = code;
return 0;
}
/*
Return the contents of the textbuffer as a Python Unicode object.
*/
static PyObject* Textbuffer_render(Textbuffer* self)
{
PyObject *result = PyUnicode_FromUnicode(self->data, self->size);
PyObject *left, *concat;
while (self->next) {
self = self->next;
left = PyUnicode_FromUnicode(self->data, self->size);
concat = PyUnicode_Concat(left, result);
Py_DECREF(left);
Py_DECREF(result);
result = concat;
}
return result;
}
static TagData* TagData_new(void)
{
TagData *self = malloc(sizeof(TagData));
#define ALLOC_BUFFER(name) \
name = Textbuffer_new(); \
if (!name) { \
TagData_dealloc(self); \
return NULL; \
}
if (!self) {
PyErr_NoMemory();
return NULL;
}
self->context = TAG_NAME;
ALLOC_BUFFER(self->pad_first)
ALLOC_BUFFER(self->pad_before_eq)
ALLOC_BUFFER(self->pad_after_eq)
self->quoter = self->reset = 0;
return self;
}
static void TagData_dealloc(TagData* self)
{
#define DEALLOC_BUFFER(name) \
if (name) \
Textbuffer_dealloc(name);
DEALLOC_BUFFER(self->pad_first);
DEALLOC_BUFFER(self->pad_before_eq);
DEALLOC_BUFFER(self->pad_after_eq);
free(self);
}
static int TagData_reset_buffers(TagData* self)
{
#define RESET_BUFFER(name) \
Textbuffer_dealloc(name); \
name = Textbuffer_new(); \
if (!name) \
return -1;
RESET_BUFFER(self->pad_first)
RESET_BUFFER(self->pad_before_eq)
RESET_BUFFER(self->pad_after_eq)
return 0;
}
static PyObject*
Tokenizer_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
{
Tokenizer* self = (Tokenizer*) type->tp_alloc(type, 0);
return (PyObject*) self;
}
static void Tokenizer_dealloc(Tokenizer* self)
{
Stack *this = self->topstack, *next;
Py_XDECREF(self->text);
while (this) {
Py_DECREF(this->stack);
Textbuffer_dealloc(this->textbuffer);
next = this->next;
free(this);
this = next;
}
Py_TYPE(self)->tp_free((PyObject*) self);
}
static int Tokenizer_init(Tokenizer* self, PyObject* args, PyObject* kwds)
{
static char* kwlist[] = {NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
return -1;
self->text = Py_None;
Py_INCREF(Py_None);
self->topstack = NULL;
self->head = self->length = self->global = self->depth = self->cycles = 0;
return 0;
}
/*
Add a new token stack, context, and textbuffer to the list.
*/
static int Tokenizer_push(Tokenizer* self, uint64_t context)
{
Stack* top = malloc(sizeof(Stack));
if (!top) {
PyErr_NoMemory();
return -1;
}
top->stack = PyList_New(0);
top->context = context;
top->textbuffer = Textbuffer_new();
if (!top->textbuffer)
return -1;
top->next = self->topstack;
self->topstack = top;
self->depth++;
self->cycles++;
return 0;
}
/*
Push the textbuffer onto the stack as a Text node and clear it.
*/
static int Tokenizer_push_textbuffer(Tokenizer* self)
{
PyObject *text, *kwargs, *token;
Textbuffer* buffer = self->topstack->textbuffer;
if (buffer->size == 0 && !buffer->next)
return 0;
text = Textbuffer_render(buffer);
if (!text)
return -1;
kwargs = PyDict_New();
if (!kwargs) {
Py_DECREF(text);
return -1;
}
PyDict_SetItemString(kwargs, "text", text);
Py_DECREF(text);
token = PyObject_Call(Text, NOARGS, kwargs);
Py_DECREF(kwargs);
if (!token)
return -1;
if (PyList_Append(self->topstack->stack, token)) {
Py_DECREF(token);
return -1;
}
Py_DECREF(token);
Textbuffer_dealloc(buffer);
self->topstack->textbuffer = Textbuffer_new();
if (!self->topstack->textbuffer)
return -1;
return 0;
}
/*
Pop and deallocate the top token stack/context/textbuffer.
*/
static void Tokenizer_delete_top_of_stack(Tokenizer* self)
{
Stack* top = self->topstack;
Py_DECREF(top->stack);
Textbuffer_dealloc(top->textbuffer);
self->topstack = top->next;
free(top);
self->depth--;
}
/*
Pop the current stack/context/textbuffer, returing the stack.
*/
static PyObject* Tokenizer_pop(Tokenizer* self)
{
PyObject* stack;
if (Tokenizer_push_textbuffer(self))
return NULL;
stack = self->topstack->stack;
Py_INCREF(stack);
Tokenizer_delete_top_of_stack(self);
return stack;
}
/*
Pop the current stack/context/textbuffer, returing the stack. We will also
replace the underlying stack's context with the current stack's.
*/
static PyObject* Tokenizer_pop_keeping_context(Tokenizer* self)
{
PyObject* stack;
uint64_t context;
if (Tokenizer_push_textbuffer(self))
return NULL;
stack = self->topstack->stack;
Py_INCREF(stack);
context = self->topstack->context;
Tokenizer_delete_top_of_stack(self);
self->topstack->context = context;
return stack;
}
/*
Fail the current tokenization route. Discards the current
stack/context/textbuffer and sets the BAD_ROUTE flag.
*/
static void* Tokenizer_fail_route(Tokenizer* self)
{
uint64_t context = self->topstack->context;
PyObject* stack = Tokenizer_pop(self);
Py_XDECREF(stack);
FAIL_ROUTE(context);
return NULL;
}
/*
Write a token to the current token stack.
*/
static int Tokenizer_emit_token(Tokenizer* self, PyObject* token, int first)
{
PyObject* instance;
if (Tokenizer_push_textbuffer(self))
return -1;
instance = PyObject_CallObject(token, NULL);
if (!instance)
return -1;
if (first ? PyList_Insert(self->topstack->stack, 0, instance) :
PyList_Append(self->topstack->stack, instance)) {
Py_DECREF(instance);
return -1;
}
Py_DECREF(instance);
return 0;
}
/*
Write a token to the current token stack, with kwargs. Steals a reference
to kwargs.
*/
static int Tokenizer_emit_token_kwargs(Tokenizer* self, PyObject* token,
PyObject* kwargs, int first)
{
PyObject* instance;
if (Tokenizer_push_textbuffer(self)) {
Py_DECREF(kwargs);
return -1;
}
instance = PyObject_Call(token, NOARGS, kwargs);
if (!instance) {
Py_DECREF(kwargs);
return -1;
}
if (first ? PyList_Insert(self->topstack->stack, 0, instance):
PyList_Append(self->topstack->stack, instance)) {
Py_DECREF(instance);
Py_DECREF(kwargs);
return -1;
}
Py_DECREF(instance);
Py_DECREF(kwargs);
return 0;
}
/*
Write a Unicode codepoint to the current textbuffer.
*/
static int Tokenizer_emit_char(Tokenizer* self, Py_UNICODE code)
{
return Textbuffer_write(&(self->topstack->textbuffer), code);
}
/*
Write a string of text to the current textbuffer.
*/
static int Tokenizer_emit_text(Tokenizer* self, const char* text)
{
int i = 0;
while (text[i]) {
if (Tokenizer_emit_char(self, text[i]))
return -1;
i++;
}
return 0;
}
/*
Write the contents of another textbuffer to the current textbuffer,
deallocating it in the process.
*/
static int
Tokenizer_emit_textbuffer(Tokenizer* self, Textbuffer* buffer, int reverse)
{
Textbuffer *original = buffer;
long i;
if (reverse) {
do {
for (i = buffer->size - 1; i >= 0; i--) {
if (Tokenizer_emit_char(self, buffer->data[i])) {
Textbuffer_dealloc(original);
return -1;
}
}
} while ((buffer = buffer->next));
}
else {
while (buffer->next)
buffer = buffer->next;
do {
for (i = 0; i < buffer->size; i++) {
if (Tokenizer_emit_char(self, buffer->data[i])) {
Textbuffer_dealloc(original);
return -1;
}
}
} while ((buffer = buffer->prev));
}
Textbuffer_dealloc(original);
return 0;
}
/*
Write a series of tokens to the current stack at once.
*/
static int Tokenizer_emit_all(Tokenizer* self, PyObject* tokenlist)
{
int pushed = 0;
PyObject *stack, *token, *left, *right, *text;
Textbuffer* buffer;
Py_ssize_t size;
if (PyList_GET_SIZE(tokenlist) > 0) {
token = PyList_GET_ITEM(tokenlist, 0);
switch (PyObject_IsInstance(token, Text)) {
case 0:
break;
case 1: {
pushed = 1;
buffer = self->topstack->textbuffer;
if (buffer->size == 0 && !buffer->next)
break;
left = Textbuffer_render(buffer);
if (!left)
return -1;
right = PyObject_GetAttrString(token, "text");
if (!right)
return -1;
text = PyUnicode_Concat(left, right);
Py_DECREF(left);
Py_DECREF(right);
if (!text)
return -1;
if (PyObject_SetAttrString(token, "text", text)) {
Py_DECREF(text);
return -1;
}
Py_DECREF(text);
Textbuffer_dealloc(buffer);
self->topstack->textbuffer = Textbuffer_new();
if (!self->topstack->textbuffer)
return -1;
break;
}
case -1:
return -1;
}
}
if (!pushed) {
if (Tokenizer_push_textbuffer(self))
return -1;
}
stack = self->topstack->stack;
size = PyList_GET_SIZE(stack);
if (PyList_SetSlice(stack, size, size, tokenlist))
return -1;
return 0;
}
/*
Pop the current stack, write text, and then write the stack. 'text' is a
NULL-terminated array of chars.
*/
static int Tokenizer_emit_text_then_stack(Tokenizer* self, const char* text)
{
PyObject* stack = Tokenizer_pop(self);
if (Tokenizer_emit_text(self, text)) {
Py_DECREF(stack);
return -1;
}
if (stack) {
if (PyList_GET_SIZE(stack) > 0) {
if (Tokenizer_emit_all(self, stack)) {
Py_DECREF(stack);
return -1;
}
}
Py_DECREF(stack);
}
self->head--;
return 0;
}
/*
Read the value at a relative point in the wikicode, forwards.
*/
static PyObject* Tokenizer_read(Tokenizer* self, Py_ssize_t delta)
{
Py_ssize_t index = self->head + delta;
if (index >= self->length)
return EMPTY;
return PyList_GET_ITEM(self->text, index);
}
/*
Read the value at a relative point in the wikicode, backwards.
*/
static PyObject* Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta)
{
Py_ssize_t index;
if (delta > self->head)
return EMPTY;
index = self->head - delta;
return PyList_GET_ITEM(self->text, index);
}
/*
Parse a template at the head of the wikicode string.
*/
static int Tokenizer_parse_template(Tokenizer* self)
{
PyObject *template;
Py_ssize_t reset = self->head;
template = Tokenizer_parse(self, LC_TEMPLATE_NAME, 1);
if (BAD_ROUTE) {
self->head = reset;
return 0;
}
if (!template)
return -1;
if (Tokenizer_emit_first(self, TemplateOpen)) {
Py_DECREF(template);
return -1;
}
if (Tokenizer_emit_all(self, template)) {
Py_DECREF(template);
return -1;
}
Py_DECREF(template);
if (Tokenizer_emit(self, TemplateClose))
return -1;
return 0;
}
/*
Parse an argument at the head of the wikicode string.
*/
static int Tokenizer_parse_argument(Tokenizer* self)
{
PyObject *argument;
Py_ssize_t reset = self->head;
argument = Tokenizer_parse(self, LC_ARGUMENT_NAME, 1);
if (BAD_ROUTE) {
self->head = reset;
return 0;
}
if (!argument)
return -1;
if (Tokenizer_emit_first(self, ArgumentOpen)) {
Py_DECREF(argument);
return -1;
}
if (Tokenizer_emit_all(self, argument)) {
Py_DECREF(argument);
return -1;
}
Py_DECREF(argument);
if (Tokenizer_emit(self, ArgumentClose))
return -1;
return 0;
}
/*
Parse a template or argument at the head of the wikicode string.
*/
static int Tokenizer_parse_template_or_argument(Tokenizer* self)
{
unsigned int braces = 2, i;
PyObject *tokenlist;
self->head += 2;
while (Tokenizer_READ(self, 0) == '{' && braces < MAX_BRACES) {
self->head++;
braces++;
}
if (Tokenizer_push(self, 0))
return -1;
while (braces) {
if (braces == 1) {
if (Tokenizer_emit_text_then_stack(self, "{"))
return -1;
return 0;
}
if (braces == 2) {
if (Tokenizer_parse_template(self))
return -1;
if (BAD_ROUTE) {
RESET_ROUTE();
if (Tokenizer_emit_text_then_stack(self, "{{"))
return -1;
return 0;
}
break;
}
if (Tokenizer_parse_argument(self))
return -1;
if (BAD_ROUTE) {
RESET_ROUTE();
if (Tokenizer_parse_template(self))
return -1;
if (BAD_ROUTE) {
char text[MAX_BRACES + 1];
RESET_ROUTE();
for (i = 0; i < braces; i++) text[i] = '{';
text[braces] = '\0';
if (Tokenizer_emit_text_then_stack(self, text))
return -1;
return 0;
}
else
braces -= 2;
}
else
braces -= 3;
if (braces)
self->head++;
}
tokenlist = Tokenizer_pop(self);
if (!tokenlist)
return -1;
if (Tokenizer_emit_all(self, tokenlist)) {
Py_DECREF(tokenlist);
return -1;
}
Py_DECREF(tokenlist);
if (self->topstack->context & LC_FAIL_NEXT)
self->topstack->context ^= LC_FAIL_NEXT;
return 0;
}
/*
Handle a template parameter at the head of the string.
*/
static int Tokenizer_handle_template_param(Tokenizer* self)
{
PyObject *stack;
if (self->topstack->context & LC_TEMPLATE_NAME)
self->topstack->context ^= LC_TEMPLATE_NAME;
else if (self->topstack->context & LC_TEMPLATE_PARAM_VALUE)
self->topstack->context ^= LC_TEMPLATE_PARAM_VALUE;
if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
stack = Tokenizer_pop_keeping_context(self);
if (!stack)
return -1;
if (Tokenizer_emit_all(self, stack)) {
Py_DECREF(stack);
return -1;
}
Py_DECREF(stack);
}
else
self->topstack->context |= LC_TEMPLATE_PARAM_KEY;
if (Tokenizer_emit(self, TemplateParamSeparator))
return -1;
if (Tokenizer_push(self, self->topstack->context))
return -1;
return 0;
}
/*
Handle a template parameter's value at the head of the string.
*/
static int Tokenizer_handle_template_param_value(Tokenizer* self)
{
PyObject *stack;
stack = Tokenizer_pop_keeping_context(self);
if (!stack)
return -1;
if (Tokenizer_emit_all(self, stack)) {
Py_DECREF(stack);
return -1;
}
Py_DECREF(stack);
self->topstack->context ^= LC_TEMPLATE_PARAM_KEY;
self->topstack->context |= LC_TEMPLATE_PARAM_VALUE;
if (Tokenizer_emit(self, TemplateParamEquals))
return -1;
return 0;
}
/*
Handle the end of a template at the head of the string.
*/
static PyObject* Tokenizer_handle_template_end(Tokenizer* self)
{
PyObject* stack;
if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
stack = Tokenizer_pop_keeping_context(self);
if (!stack)
return NULL;
if (Tokenizer_emit_all(self, stack)) {
Py_DECREF(stack);
return NULL;
}
Py_DECREF(stack);
}
self->head++;
stack = Tokenizer_pop(self);
return stack;
}
/*
Handle the separator between an argument's name and default.
*/
static int Tokenizer_handle_argument_separator(Tokenizer* self)
{
self->topstack->context ^= LC_ARGUMENT_NAME;
self->topstack->context |= LC_ARGUMENT_DEFAULT;
if (Tokenizer_emit(self, ArgumentSeparator))
return -1;
return 0;
}
/*
Handle the end of an argument at the head of the string.
*/
static PyObject* Tokenizer_handle_argument_end(Tokenizer* self)
{
PyObject* stack = Tokenizer_pop(self);
self->head += 2;
return stack;
}
/*
Parse an internal wikilink at the head of the wikicode string.
*/
static int Tokenizer_parse_wikilink(Tokenizer* self)
{
Py_ssize_t reset;
PyObject *wikilink;
self->head += 2;
reset = self->head - 1;
wikilink = Tokenizer_parse(self, LC_WIKILINK_TITLE, 1);
if (BAD_ROUTE) {
RESET_ROUTE();
self->head = reset;
if (Tokenizer_emit_text(self, "[["))
return -1;
return 0;
}
if (!wikilink)
return -1;
if (Tokenizer_emit(self, WikilinkOpen)) {
Py_DECREF(wikilink);
return -1;
}
if (Tokenizer_emit_all(self, wikilink)) {
Py_DECREF(wikilink);
return -1;
}
Py_DECREF(wikilink);
if (Tokenizer_emit(self, WikilinkClose))
return -1;
return 0;
}
/*
Handle the separator between a wikilink's title and its text.
*/
static int Tokenizer_handle_wikilink_separator(Tokenizer* self)
{
self->topstack->context ^= LC_WIKILINK_TITLE;
self->topstack->context |= LC_WIKILINK_TEXT;
if (Tokenizer_emit(self, WikilinkSeparator))
return -1;
return 0;
}
/*
Handle the end of a wikilink at the head of the string.
*/
static PyObject* Tokenizer_handle_wikilink_end(Tokenizer* self)
{
PyObject* stack = Tokenizer_pop(self);
self->head += 1;
return stack;
}
/*
Parse the URI scheme of a bracket-enclosed external link.
*/
static int Tokenizer_parse_bracketed_uri_scheme(Tokenizer* self)
{
static const char* valid = "abcdefghijklmnopqrstuvwxyz0123456789+.-";
Textbuffer* buffer;
PyObject* scheme;
Py_UNICODE this;
int slashes, i;
if (Tokenizer_push(self, LC_EXT_LINK_URI))
return -1;
if (Tokenizer_READ(self, 0) == '/' && Tokenizer_READ(self, 1) == '/') {
if (Tokenizer_emit_text(self, "//"))
return -1;
self->head += 2;
}
else {
buffer = Textbuffer_new();
if (!buffer)
return -1;
while ((this = Tokenizer_READ(self, 0))) {
i = 0;
while (1) {
if (!valid[i])
goto end_of_loop;
if (this == valid[i])
break;
i++;
}
Textbuffer_write(&buffer, this);
if (Tokenizer_emit_char(self, this)) {
Textbuffer_dealloc(buffer);
return -1;
}
self->head++;
}
end_of_loop:
if (this != ':') {
Textbuffer_dealloc(buffer);
Tokenizer_fail_route(self);
return 0;
}
if (Tokenizer_emit_char(self, ':')) {
Textbuffer_dealloc(buffer);
return -1;
}
self->head++;
slashes = (Tokenizer_READ(self, 0) == '/' &&
Tokenizer_READ(self, 1) == '/');
if (slashes) {
if (Tokenizer_emit_text(self, "//")) {
Textbuffer_dealloc(buffer);
return -1;
}
self->head += 2;
}
scheme = Textbuffer_render(buffer);
Textbuffer_dealloc(buffer);
if (!scheme)
return -1;
if (!IS_SCHEME(scheme, slashes, 0)) {
Py_DECREF(scheme);
Tokenizer_fail_route(self);
return 0;
}
Py_DECREF(scheme);
}
return 0;
}
/*
Parse the URI scheme of a free (no brackets) external link.
*/
static int Tokenizer_parse_free_uri_scheme(Tokenizer* self)
{
static const char* valid = "abcdefghijklmnopqrstuvwxyz0123456789+.-";
Textbuffer *scheme_buffer = Textbuffer_new(), *temp_buffer;
PyObject *scheme;
Py_UNICODE chunk;
long i;
int slashes, j;
if (!scheme_buffer)
return -1;
// We have to backtrack through the textbuffer looking for our scheme since
// it was just parsed as text:
temp_buffer = self->topstack->textbuffer;
while (temp_buffer) {
for (i = temp_buffer->size - 1; i >= 0; i--) {
chunk = temp_buffer->data[i];
if (Py_UNICODE_ISSPACE(chunk) || is_marker(chunk))
goto end_of_loop;
j = 0;
while (1) {
if (!valid[j]) {
Textbuffer_dealloc(scheme_buffer);
FAIL_ROUTE(0);
return 0;
}
if (chunk == valid[j])
break;
j++;
}
Textbuffer_write(&scheme_buffer, chunk);
}
temp_buffer = temp_buffer->next;
}
end_of_loop:
scheme = Textbuffer_render(scheme_buffer);
if (!scheme) {
Textbuffer_dealloc(scheme_buffer);
return -1;
}
slashes = (Tokenizer_READ(self, 0) == '/' &&
Tokenizer_READ(self, 1) == '/');
if (!IS_SCHEME(scheme, slashes, 1)) {
Py_DECREF(scheme);
Textbuffer_dealloc(scheme_buffer);
FAIL_ROUTE(0);
return 0;
}
Py_DECREF(scheme);
if (Tokenizer_push(self, self->topstack->context | LC_EXT_LINK_URI)) {
Textbuffer_dealloc(scheme_buffer);
return -1;
}
if (Tokenizer_emit_textbuffer(self, scheme_buffer, 1))
return -1;
if (Tokenizer_emit_char(self, ':'))
return -1;
if (slashes) {
if (Tokenizer_emit_text(self, "//"))
return -1;
self->head += 2;
}
return 0;
}