A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

3379 linhas
98 KiB

  1. /*
  2. Tokenizer for MWParserFromHell
  3. Copyright (C) 2012-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #include "tokenizer.h"
  21. /*
  22. Determine whether the given Py_UNICODE is a marker.
  23. */
  24. static int is_marker(Py_UNICODE this)
  25. {
  26. int i;
  27. for (i = 0; i < NUM_MARKERS; i++) {
  28. if (MARKERS[i] == this)
  29. return 1;
  30. }
  31. return 0;
  32. }
  33. /*
  34. Given a context, return the heading level encoded within it.
  35. */
  36. static int heading_level_from_context(int n)
  37. {
  38. int level;
  39. n /= LC_HEADING_LEVEL_1;
  40. for (level = 1; n > 1; n >>= 1)
  41. level++;
  42. return level;
  43. }
  44. /*
  45. Call the given function in definitions.py, using 'in1', 'in2', and 'in3' as
  46. parameters, and return its output as a bool.
  47. */
  48. static int call_def_func(const char* funcname, PyObject* in1, PyObject* in2,
  49. PyObject* in3)
  50. {
  51. PyObject* func = PyObject_GetAttrString(definitions, funcname);
  52. PyObject* result = PyObject_CallFunctionObjArgs(func, in1, in2, in3, NULL);
  53. int ans = (result == Py_True) ? 1 : 0;
  54. Py_DECREF(func);
  55. Py_DECREF(result);
  56. return ans;
  57. }
  58. /*
  59. Sanitize the name of a tag so it can be compared with others for equality.
  60. */
  61. static PyObject* strip_tag_name(PyObject* token)
  62. {
  63. PyObject *text, *rstripped, *lowered;
  64. text = PyObject_GetAttrString(token, "text");
  65. if (!text)
  66. return NULL;
  67. rstripped = PyObject_CallMethod(text, "rstrip", NULL);
  68. Py_DECREF(text);
  69. if (!rstripped)
  70. return NULL;
  71. lowered = PyObject_CallMethod(rstripped, "lower", NULL);
  72. Py_DECREF(rstripped);
  73. return lowered;
  74. }
  75. static Textbuffer* Textbuffer_new(void)
  76. {
  77. Textbuffer* buffer = malloc(sizeof(Textbuffer));
  78. if (!buffer) {
  79. PyErr_NoMemory();
  80. return NULL;
  81. }
  82. buffer->size = 0;
  83. buffer->data = malloc(sizeof(Py_UNICODE) * TEXTBUFFER_BLOCKSIZE);
  84. if (!buffer->data) {
  85. free(buffer);
  86. PyErr_NoMemory();
  87. return NULL;
  88. }
  89. buffer->prev = buffer->next = NULL;
  90. return buffer;
  91. }
  92. static void Textbuffer_dealloc(Textbuffer* self)
  93. {
  94. Textbuffer* next;
  95. while (self) {
  96. free(self->data);
  97. next = self->next;
  98. free(self);
  99. self = next;
  100. }
  101. }
  102. /*
  103. Write a Unicode codepoint to the given textbuffer.
  104. */
  105. static int Textbuffer_write(Textbuffer** this, Py_UNICODE code)
  106. {
  107. Textbuffer* self = *this;
  108. if (self->size == TEXTBUFFER_BLOCKSIZE) {
  109. Textbuffer* new = Textbuffer_new();
  110. if (!new)
  111. return -1;
  112. new->next = self;
  113. self->prev = new;
  114. *this = self = new;
  115. }
  116. self->data[self->size++] = code;
  117. return 0;
  118. }
  119. /*
  120. Return the contents of the textbuffer as a Python Unicode object.
  121. */
  122. static PyObject* Textbuffer_render(Textbuffer* self)
  123. {
  124. PyObject *result = PyUnicode_FromUnicode(self->data, self->size);
  125. PyObject *left, *concat;
  126. while (self->next) {
  127. self = self->next;
  128. left = PyUnicode_FromUnicode(self->data, self->size);
  129. concat = PyUnicode_Concat(left, result);
  130. Py_DECREF(left);
  131. Py_DECREF(result);
  132. result = concat;
  133. }
  134. return result;
  135. }
  136. static TagData* TagData_new(void)
  137. {
  138. TagData *self = malloc(sizeof(TagData));
  139. #define ALLOC_BUFFER(name) \
  140. name = Textbuffer_new(); \
  141. if (!name) { \
  142. TagData_dealloc(self); \
  143. return NULL; \
  144. }
  145. if (!self) {
  146. PyErr_NoMemory();
  147. return NULL;
  148. }
  149. self->context = TAG_NAME;
  150. ALLOC_BUFFER(self->pad_first)
  151. ALLOC_BUFFER(self->pad_before_eq)
  152. ALLOC_BUFFER(self->pad_after_eq)
  153. self->quoter = self->reset = 0;
  154. return self;
  155. }
  156. static void TagData_dealloc(TagData* self)
  157. {
  158. #define DEALLOC_BUFFER(name) \
  159. if (name) \
  160. Textbuffer_dealloc(name);
  161. DEALLOC_BUFFER(self->pad_first);
  162. DEALLOC_BUFFER(self->pad_before_eq);
  163. DEALLOC_BUFFER(self->pad_after_eq);
  164. free(self);
  165. }
  166. static int TagData_reset_buffers(TagData* self)
  167. {
  168. #define RESET_BUFFER(name) \
  169. Textbuffer_dealloc(name); \
  170. name = Textbuffer_new(); \
  171. if (!name) \
  172. return -1;
  173. RESET_BUFFER(self->pad_first)
  174. RESET_BUFFER(self->pad_before_eq)
  175. RESET_BUFFER(self->pad_after_eq)
  176. return 0;
  177. }
  178. static PyObject*
  179. Tokenizer_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
  180. {
  181. Tokenizer* self = (Tokenizer*) type->tp_alloc(type, 0);
  182. return (PyObject*) self;
  183. }
  184. static void Tokenizer_dealloc(Tokenizer* self)
  185. {
  186. Stack *this = self->topstack, *next;
  187. Py_XDECREF(self->text);
  188. while (this) {
  189. Py_DECREF(this->stack);
  190. Textbuffer_dealloc(this->textbuffer);
  191. next = this->next;
  192. free(this);
  193. this = next;
  194. }
  195. Py_TYPE(self)->tp_free((PyObject*) self);
  196. }
  197. static int Tokenizer_init(Tokenizer* self, PyObject* args, PyObject* kwds)
  198. {
  199. static char* kwlist[] = {NULL};
  200. if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
  201. return -1;
  202. self->text = Py_None;
  203. Py_INCREF(Py_None);
  204. self->topstack = NULL;
  205. self->head = self->length = self->global = self->depth = self->cycles = 0;
  206. return 0;
  207. }
  208. /*
  209. Add a new token stack, context, and textbuffer to the list.
  210. */
  211. static int Tokenizer_push(Tokenizer* self, uint64_t context)
  212. {
  213. Stack* top = malloc(sizeof(Stack));
  214. if (!top) {
  215. PyErr_NoMemory();
  216. return -1;
  217. }
  218. top->stack = PyList_New(0);
  219. top->context = context;
  220. top->textbuffer = Textbuffer_new();
  221. if (!top->textbuffer)
  222. return -1;
  223. top->next = self->topstack;
  224. self->topstack = top;
  225. self->depth++;
  226. self->cycles++;
  227. return 0;
  228. }
  229. /*
  230. Push the textbuffer onto the stack as a Text node and clear it.
  231. */
  232. static int Tokenizer_push_textbuffer(Tokenizer* self)
  233. {
  234. PyObject *text, *kwargs, *token;
  235. Textbuffer* buffer = self->topstack->textbuffer;
  236. if (buffer->size == 0 && !buffer->next)
  237. return 0;
  238. text = Textbuffer_render(buffer);
  239. if (!text)
  240. return -1;
  241. kwargs = PyDict_New();
  242. if (!kwargs) {
  243. Py_DECREF(text);
  244. return -1;
  245. }
  246. PyDict_SetItemString(kwargs, "text", text);
  247. Py_DECREF(text);
  248. token = PyObject_Call(Text, NOARGS, kwargs);
  249. Py_DECREF(kwargs);
  250. if (!token)
  251. return -1;
  252. if (PyList_Append(self->topstack->stack, token)) {
  253. Py_DECREF(token);
  254. return -1;
  255. }
  256. Py_DECREF(token);
  257. Textbuffer_dealloc(buffer);
  258. self->topstack->textbuffer = Textbuffer_new();
  259. if (!self->topstack->textbuffer)
  260. return -1;
  261. return 0;
  262. }
  263. /*
  264. Pop and deallocate the top token stack/context/textbuffer.
  265. */
  266. static void Tokenizer_delete_top_of_stack(Tokenizer* self)
  267. {
  268. Stack* top = self->topstack;
  269. Py_DECREF(top->stack);
  270. Textbuffer_dealloc(top->textbuffer);
  271. self->topstack = top->next;
  272. free(top);
  273. self->depth--;
  274. }
  275. /*
  276. Pop the current stack/context/textbuffer, returing the stack.
  277. */
  278. static PyObject* Tokenizer_pop(Tokenizer* self)
  279. {
  280. PyObject* stack;
  281. if (Tokenizer_push_textbuffer(self))
  282. return NULL;
  283. stack = self->topstack->stack;
  284. Py_INCREF(stack);
  285. Tokenizer_delete_top_of_stack(self);
  286. return stack;
  287. }
  288. /*
  289. Pop the current stack/context/textbuffer, returing the stack. We will also
  290. replace the underlying stack's context with the current stack's.
  291. */
  292. static PyObject* Tokenizer_pop_keeping_context(Tokenizer* self)
  293. {
  294. PyObject* stack;
  295. uint64_t context;
  296. if (Tokenizer_push_textbuffer(self))
  297. return NULL;
  298. stack = self->topstack->stack;
  299. Py_INCREF(stack);
  300. context = self->topstack->context;
  301. Tokenizer_delete_top_of_stack(self);
  302. self->topstack->context = context;
  303. return stack;
  304. }
  305. /*
  306. Fail the current tokenization route. Discards the current
  307. stack/context/textbuffer and sets the BAD_ROUTE flag.
  308. */
  309. static void* Tokenizer_fail_route(Tokenizer* self)
  310. {
  311. uint64_t context = self->topstack->context;
  312. PyObject* stack = Tokenizer_pop(self);
  313. Py_XDECREF(stack);
  314. FAIL_ROUTE(context);
  315. return NULL;
  316. }
  317. /*
  318. Write a token to the current token stack.
  319. */
  320. static int Tokenizer_emit_token(Tokenizer* self, PyObject* token, int first)
  321. {
  322. PyObject* instance;
  323. if (Tokenizer_push_textbuffer(self))
  324. return -1;
  325. instance = PyObject_CallObject(token, NULL);
  326. if (!instance)
  327. return -1;
  328. if (first ? PyList_Insert(self->topstack->stack, 0, instance) :
  329. PyList_Append(self->topstack->stack, instance)) {
  330. Py_DECREF(instance);
  331. return -1;
  332. }
  333. Py_DECREF(instance);
  334. return 0;
  335. }
  336. /*
  337. Write a token to the current token stack, with kwargs. Steals a reference
  338. to kwargs.
  339. */
  340. static int Tokenizer_emit_token_kwargs(Tokenizer* self, PyObject* token,
  341. PyObject* kwargs, int first)
  342. {
  343. PyObject* instance;
  344. if (Tokenizer_push_textbuffer(self)) {
  345. Py_DECREF(kwargs);
  346. return -1;
  347. }
  348. instance = PyObject_Call(token, NOARGS, kwargs);
  349. if (!instance) {
  350. Py_DECREF(kwargs);
  351. return -1;
  352. }
  353. if (first ? PyList_Insert(self->topstack->stack, 0, instance):
  354. PyList_Append(self->topstack->stack, instance)) {
  355. Py_DECREF(instance);
  356. Py_DECREF(kwargs);
  357. return -1;
  358. }
  359. Py_DECREF(instance);
  360. Py_DECREF(kwargs);
  361. return 0;
  362. }
  363. /*
  364. Write a Unicode codepoint to the current textbuffer.
  365. */
  366. static int Tokenizer_emit_char(Tokenizer* self, Py_UNICODE code)
  367. {
  368. return Textbuffer_write(&(self->topstack->textbuffer), code);
  369. }
  370. /*
  371. Write a string of text to the current textbuffer.
  372. */
  373. static int Tokenizer_emit_text(Tokenizer* self, const char* text)
  374. {
  375. int i = 0;
  376. while (text[i]) {
  377. if (Tokenizer_emit_char(self, text[i]))
  378. return -1;
  379. i++;
  380. }
  381. return 0;
  382. }
  383. /*
  384. Write the contents of another textbuffer to the current textbuffer,
  385. deallocating it in the process.
  386. */
  387. static int
  388. Tokenizer_emit_textbuffer(Tokenizer* self, Textbuffer* buffer, int reverse)
  389. {
  390. Textbuffer *original = buffer;
  391. long i;
  392. if (reverse) {
  393. do {
  394. for (i = buffer->size - 1; i >= 0; i--) {
  395. if (Tokenizer_emit_char(self, buffer->data[i])) {
  396. Textbuffer_dealloc(original);
  397. return -1;
  398. }
  399. }
  400. } while ((buffer = buffer->next));
  401. }
  402. else {
  403. while (buffer->next)
  404. buffer = buffer->next;
  405. do {
  406. for (i = 0; i < buffer->size; i++) {
  407. if (Tokenizer_emit_char(self, buffer->data[i])) {
  408. Textbuffer_dealloc(original);
  409. return -1;
  410. }
  411. }
  412. } while ((buffer = buffer->prev));
  413. }
  414. Textbuffer_dealloc(original);
  415. return 0;
  416. }
  417. /*
  418. Write a series of tokens to the current stack at once.
  419. */
  420. static int Tokenizer_emit_all(Tokenizer* self, PyObject* tokenlist)
  421. {
  422. int pushed = 0;
  423. PyObject *stack, *token, *left, *right, *text;
  424. Textbuffer* buffer;
  425. Py_ssize_t size;
  426. if (PyList_GET_SIZE(tokenlist) > 0) {
  427. token = PyList_GET_ITEM(tokenlist, 0);
  428. switch (PyObject_IsInstance(token, Text)) {
  429. case 0:
  430. break;
  431. case 1: {
  432. pushed = 1;
  433. buffer = self->topstack->textbuffer;
  434. if (buffer->size == 0 && !buffer->next)
  435. break;
  436. left = Textbuffer_render(buffer);
  437. if (!left)
  438. return -1;
  439. right = PyObject_GetAttrString(token, "text");
  440. if (!right)
  441. return -1;
  442. text = PyUnicode_Concat(left, right);
  443. Py_DECREF(left);
  444. Py_DECREF(right);
  445. if (!text)
  446. return -1;
  447. if (PyObject_SetAttrString(token, "text", text)) {
  448. Py_DECREF(text);
  449. return -1;
  450. }
  451. Py_DECREF(text);
  452. Textbuffer_dealloc(buffer);
  453. self->topstack->textbuffer = Textbuffer_new();
  454. if (!self->topstack->textbuffer)
  455. return -1;
  456. break;
  457. }
  458. case -1:
  459. return -1;
  460. }
  461. }
  462. if (!pushed) {
  463. if (Tokenizer_push_textbuffer(self))
  464. return -1;
  465. }
  466. stack = self->topstack->stack;
  467. size = PyList_GET_SIZE(stack);
  468. if (PyList_SetSlice(stack, size, size, tokenlist))
  469. return -1;
  470. return 0;
  471. }
  472. /*
  473. Pop the current stack, write text, and then write the stack. 'text' is a
  474. NULL-terminated array of chars.
  475. */
  476. static int Tokenizer_emit_text_then_stack(Tokenizer* self, const char* text)
  477. {
  478. PyObject* stack = Tokenizer_pop(self);
  479. if (Tokenizer_emit_text(self, text)) {
  480. Py_DECREF(stack);
  481. return -1;
  482. }
  483. if (stack) {
  484. if (PyList_GET_SIZE(stack) > 0) {
  485. if (Tokenizer_emit_all(self, stack)) {
  486. Py_DECREF(stack);
  487. return -1;
  488. }
  489. }
  490. Py_DECREF(stack);
  491. }
  492. self->head--;
  493. return 0;
  494. }
  495. /*
  496. Read the value at a relative point in the wikicode, forwards.
  497. */
  498. static PyObject* Tokenizer_read(Tokenizer* self, Py_ssize_t delta)
  499. {
  500. Py_ssize_t index = self->head + delta;
  501. if (index >= self->length)
  502. return EMPTY;
  503. return PyList_GET_ITEM(self->text, index);
  504. }
  505. /*
  506. Read the value at a relative point in the wikicode, backwards.
  507. */
  508. static PyObject* Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta)
  509. {
  510. Py_ssize_t index;
  511. if (delta > self->head)
  512. return EMPTY;
  513. index = self->head - delta;
  514. return PyList_GET_ITEM(self->text, index);
  515. }
  516. /*
  517. Parse a template at the head of the wikicode string.
  518. */
  519. static int Tokenizer_parse_template(Tokenizer* self)
  520. {
  521. PyObject *template;
  522. Py_ssize_t reset = self->head;
  523. template = Tokenizer_parse(self, LC_TEMPLATE_NAME, 1);
  524. if (BAD_ROUTE) {
  525. self->head = reset;
  526. return 0;
  527. }
  528. if (!template)
  529. return -1;
  530. if (Tokenizer_emit_first(self, TemplateOpen)) {
  531. Py_DECREF(template);
  532. return -1;
  533. }
  534. if (Tokenizer_emit_all(self, template)) {
  535. Py_DECREF(template);
  536. return -1;
  537. }
  538. Py_DECREF(template);
  539. if (Tokenizer_emit(self, TemplateClose))
  540. return -1;
  541. return 0;
  542. }
  543. /*
  544. Parse an argument at the head of the wikicode string.
  545. */
  546. static int Tokenizer_parse_argument(Tokenizer* self)
  547. {
  548. PyObject *argument;
  549. Py_ssize_t reset = self->head;
  550. argument = Tokenizer_parse(self, LC_ARGUMENT_NAME, 1);
  551. if (BAD_ROUTE) {
  552. self->head = reset;
  553. return 0;
  554. }
  555. if (!argument)
  556. return -1;
  557. if (Tokenizer_emit_first(self, ArgumentOpen)) {
  558. Py_DECREF(argument);
  559. return -1;
  560. }
  561. if (Tokenizer_emit_all(self, argument)) {
  562. Py_DECREF(argument);
  563. return -1;
  564. }
  565. Py_DECREF(argument);
  566. if (Tokenizer_emit(self, ArgumentClose))
  567. return -1;
  568. return 0;
  569. }
  570. /*
  571. Parse a template or argument at the head of the wikicode string.
  572. */
  573. static int Tokenizer_parse_template_or_argument(Tokenizer* self)
  574. {
  575. unsigned int braces = 2, i;
  576. PyObject *tokenlist;
  577. self->head += 2;
  578. while (Tokenizer_READ(self, 0) == '{' && braces < MAX_BRACES) {
  579. self->head++;
  580. braces++;
  581. }
  582. if (Tokenizer_push(self, 0))
  583. return -1;
  584. while (braces) {
  585. if (braces == 1) {
  586. if (Tokenizer_emit_text_then_stack(self, "{"))
  587. return -1;
  588. return 0;
  589. }
  590. if (braces == 2) {
  591. if (Tokenizer_parse_template(self))
  592. return -1;
  593. if (BAD_ROUTE) {
  594. RESET_ROUTE();
  595. if (Tokenizer_emit_text_then_stack(self, "{{"))
  596. return -1;
  597. return 0;
  598. }
  599. break;
  600. }
  601. if (Tokenizer_parse_argument(self))
  602. return -1;
  603. if (BAD_ROUTE) {
  604. RESET_ROUTE();
  605. if (Tokenizer_parse_template(self))
  606. return -1;
  607. if (BAD_ROUTE) {
  608. char text[MAX_BRACES + 1];
  609. RESET_ROUTE();
  610. for (i = 0; i < braces; i++) text[i] = '{';
  611. text[braces] = '\0';
  612. if (Tokenizer_emit_text_then_stack(self, text)) {
  613. Py_XDECREF(text);
  614. return -1;
  615. }
  616. Py_XDECREF(text);
  617. return 0;
  618. }
  619. else
  620. braces -= 2;
  621. }
  622. else
  623. braces -= 3;
  624. if (braces)
  625. self->head++;
  626. }
  627. tokenlist = Tokenizer_pop(self);
  628. if (!tokenlist)
  629. return -1;
  630. if (Tokenizer_emit_all(self, tokenlist)) {
  631. Py_DECREF(tokenlist);
  632. return -1;
  633. }
  634. Py_DECREF(tokenlist);
  635. if (self->topstack->context & LC_FAIL_NEXT)
  636. self->topstack->context ^= LC_FAIL_NEXT;
  637. return 0;
  638. }
  639. /*
  640. Handle a template parameter at the head of the string.
  641. */
  642. static int Tokenizer_handle_template_param(Tokenizer* self)
  643. {
  644. PyObject *stack;
  645. if (self->topstack->context & LC_TEMPLATE_NAME)
  646. self->topstack->context ^= LC_TEMPLATE_NAME;
  647. else if (self->topstack->context & LC_TEMPLATE_PARAM_VALUE)
  648. self->topstack->context ^= LC_TEMPLATE_PARAM_VALUE;
  649. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  650. stack = Tokenizer_pop_keeping_context(self);
  651. if (!stack)
  652. return -1;
  653. if (Tokenizer_emit_all(self, stack)) {
  654. Py_DECREF(stack);
  655. return -1;
  656. }
  657. Py_DECREF(stack);
  658. }
  659. else
  660. self->topstack->context |= LC_TEMPLATE_PARAM_KEY;
  661. if (Tokenizer_emit(self, TemplateParamSeparator))
  662. return -1;
  663. if (Tokenizer_push(self, self->topstack->context))
  664. return -1;
  665. return 0;
  666. }
  667. /*
  668. Handle a template parameter's value at the head of the string.
  669. */
  670. static int Tokenizer_handle_template_param_value(Tokenizer* self)
  671. {
  672. PyObject *stack;
  673. stack = Tokenizer_pop_keeping_context(self);
  674. if (!stack)
  675. return -1;
  676. if (Tokenizer_emit_all(self, stack)) {
  677. Py_DECREF(stack);
  678. return -1;
  679. }
  680. Py_DECREF(stack);
  681. self->topstack->context ^= LC_TEMPLATE_PARAM_KEY;
  682. self->topstack->context |= LC_TEMPLATE_PARAM_VALUE;
  683. if (Tokenizer_emit(self, TemplateParamEquals))
  684. return -1;
  685. return 0;
  686. }
  687. /*
  688. Handle the end of a template at the head of the string.
  689. */
  690. static PyObject* Tokenizer_handle_template_end(Tokenizer* self)
  691. {
  692. PyObject* stack;
  693. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  694. stack = Tokenizer_pop_keeping_context(self);
  695. if (!stack)
  696. return NULL;
  697. if (Tokenizer_emit_all(self, stack)) {
  698. Py_DECREF(stack);
  699. return NULL;
  700. }
  701. Py_DECREF(stack);
  702. }
  703. self->head++;
  704. stack = Tokenizer_pop(self);
  705. return stack;
  706. }
  707. /*
  708. Handle the separator between an argument's name and default.
  709. */
  710. static int Tokenizer_handle_argument_separator(Tokenizer* self)
  711. {
  712. self->topstack->context ^= LC_ARGUMENT_NAME;
  713. self->topstack->context |= LC_ARGUMENT_DEFAULT;
  714. if (Tokenizer_emit(self, ArgumentSeparator))
  715. return -1;
  716. return 0;
  717. }
  718. /*
  719. Handle the end of an argument at the head of the string.
  720. */
  721. static PyObject* Tokenizer_handle_argument_end(Tokenizer* self)
  722. {
  723. PyObject* stack = Tokenizer_pop(self);
  724. self->head += 2;
  725. return stack;
  726. }
  727. /*
  728. Parse an internal wikilink at the head of the wikicode string.
  729. */
  730. static int Tokenizer_parse_wikilink(Tokenizer* self)
  731. {
  732. Py_ssize_t reset;
  733. PyObject *wikilink;
  734. self->head += 2;
  735. reset = self->head - 1;
  736. wikilink = Tokenizer_parse(self, LC_WIKILINK_TITLE, 1);
  737. if (BAD_ROUTE) {
  738. RESET_ROUTE();
  739. self->head = reset;
  740. if (Tokenizer_emit_text(self, "[["))
  741. return -1;
  742. return 0;
  743. }
  744. if (!wikilink)
  745. return -1;
  746. if (Tokenizer_emit(self, WikilinkOpen)) {
  747. Py_DECREF(wikilink);
  748. return -1;
  749. }
  750. if (Tokenizer_emit_all(self, wikilink)) {
  751. Py_DECREF(wikilink);
  752. return -1;
  753. }
  754. Py_DECREF(wikilink);
  755. if (Tokenizer_emit(self, WikilinkClose))
  756. return -1;
  757. return 0;
  758. }
  759. /*
  760. Handle the separator between a wikilink's title and its text.
  761. */
  762. static int Tokenizer_handle_wikilink_separator(Tokenizer* self)
  763. {
  764. self->topstack->context ^= LC_WIKILINK_TITLE;
  765. self->topstack->context |= LC_WIKILINK_TEXT;
  766. if (Tokenizer_emit(self, WikilinkSeparator))
  767. return -1;
  768. return 0;
  769. }
  770. /*
  771. Handle the end of a wikilink at the head of the string.
  772. */
  773. static PyObject* Tokenizer_handle_wikilink_end(Tokenizer* self)
  774. {
  775. PyObject* stack = Tokenizer_pop(self);
  776. self->head += 1;
  777. return stack;
  778. }
  779. /*
  780. Parse the URI scheme of a bracket-enclosed external link.
  781. */
  782. static int Tokenizer_parse_bracketed_uri_scheme(Tokenizer* self)
  783. {
  784. static const char* valid = "abcdefghijklmnopqrstuvwxyz0123456789+.-";
  785. Textbuffer* buffer;
  786. PyObject* scheme;
  787. Py_UNICODE this;
  788. int slashes, i;
  789. if (Tokenizer_push(self, LC_EXT_LINK_URI))
  790. return -1;
  791. if (Tokenizer_READ(self, 0) == '/' && Tokenizer_READ(self, 1) == '/') {
  792. if (Tokenizer_emit_text(self, "//"))
  793. return -1;
  794. self->head += 2;
  795. }
  796. else {
  797. buffer = Textbuffer_new();
  798. if (!buffer)
  799. return -1;
  800. while ((this = Tokenizer_READ(self, 0))) {
  801. i = 0;
  802. while (1) {
  803. if (!valid[i])
  804. goto end_of_loop;
  805. if (this == valid[i])
  806. break;
  807. i++;
  808. }
  809. Textbuffer_write(&buffer, this);
  810. if (Tokenizer_emit_char(self, this)) {
  811. Textbuffer_dealloc(buffer);
  812. return -1;
  813. }
  814. self->head++;
  815. }
  816. end_of_loop:
  817. if (this != ':') {
  818. Textbuffer_dealloc(buffer);
  819. Tokenizer_fail_route(self);
  820. return 0;
  821. }
  822. if (Tokenizer_emit_char(self, ':')) {
  823. Textbuffer_dealloc(buffer);
  824. return -1;
  825. }
  826. self->head++;
  827. slashes = (Tokenizer_READ(self, 0) == '/' &&
  828. Tokenizer_READ(self, 1) == '/');
  829. if (slashes) {
  830. if (Tokenizer_emit_text(self, "//")) {
  831. Textbuffer_dealloc(buffer);
  832. return -1;
  833. }
  834. self->head += 2;
  835. }
  836. scheme = Textbuffer_render(buffer);
  837. Textbuffer_dealloc(buffer);
  838. if (!scheme)
  839. return -1;
  840. if (!IS_SCHEME(scheme, slashes, 0)) {
  841. Py_DECREF(scheme);
  842. Tokenizer_fail_route(self);
  843. return 0;
  844. }
  845. Py_DECREF(scheme);
  846. }
  847. return 0;
  848. }
  849. /*
  850. Parse the URI scheme of a free (no brackets) external link.
  851. */
  852. static int Tokenizer_parse_free_uri_scheme(Tokenizer* self)
  853. {
  854. static const char* valid = "abcdefghijklmnopqrstuvwxyz0123456789+.-";
  855. Textbuffer *scheme_buffer = Textbuffer_new(), *temp_buffer;
  856. PyObject *scheme;
  857. Py_UNICODE chunk;
  858. long i;
  859. int slashes, j;
  860. if (!scheme_buffer)
  861. return -1;
  862. // We have to backtrack through the textbuffer looking for our scheme since
  863. // it was just parsed as text:
  864. temp_buffer = self->topstack->textbuffer;
  865. while (temp_buffer) {
  866. for (i = temp_buffer->size - 1; i >= 0; i--) {
  867. chunk = temp_buffer->data[i];
  868. if (Py_UNICODE_ISSPACE(chunk) || is_marker(chunk))
  869. goto end_of_loop;
  870. j = 0;
  871. while (1) {
  872. if (!valid[j]) {
  873. Textbuffer_dealloc(scheme_buffer);
  874. FAIL_ROUTE(0);
  875. return 0;
  876. }
  877. if (chunk == valid[j])
  878. break;
  879. j++;
  880. }
  881. Textbuffer_write(&scheme_buffer, chunk);
  882. }
  883. temp_buffer = temp_buffer->next;
  884. }
  885. end_of_loop:
  886. scheme = Textbuffer_render(scheme_buffer);
  887. if (!scheme) {
  888. Textbuffer_dealloc(scheme_buffer);
  889. return -1;
  890. }
  891. slashes = (Tokenizer_READ(self, 0) == '/' &&
  892. Tokenizer_READ(self, 1) == '/');
  893. if (!IS_SCHEME(scheme, slashes, 1)) {
  894. Py_DECREF(scheme);
  895. Textbuffer_dealloc(scheme_buffer);
  896. FAIL_ROUTE(0);
  897. return 0;
  898. }
  899. Py_DECREF(scheme);
  900. if (Tokenizer_push(self, self->topstack->context | LC_EXT_LINK_URI)) {
  901. Textbuffer_dealloc(scheme_buffer);
  902. return -1;
  903. }
  904. if (Tokenizer_emit_textbuffer(self, scheme_buffer, 1))
  905. return -1;
  906. if (Tokenizer_emit_char(self, ':'))
  907. return -1;
  908. if (slashes) {
  909. if (Tokenizer_emit_text(self, "//"))
  910. return -1;
  911. self->head += 2;
  912. }
  913. return 0;
  914. }
  915. /*
  916. Handle text in a free external link, including trailing punctuation.
  917. */
  918. static int
  919. Tokenizer_handle_free_link_text(Tokenizer* self, int* parens,
  920. Textbuffer** tail, Py_UNICODE this)
  921. {
  922. #define PUSH_TAIL_BUFFER(tail, error) \
  923. if ((tail)->size || (tail)->next) { \
  924. if (Tokenizer_emit_textbuffer(self, tail, 0)) \
  925. return error; \
  926. tail = Textbuffer_new(); \
  927. if (!(tail)) \
  928. return error; \
  929. }
  930. if (this == '(' && !(*parens)) {
  931. *parens = 1;
  932. PUSH_TAIL_BUFFER(*tail, -1)
  933. }
  934. else if (this == ',' || this == ';' || this == '\\' || this == '.' ||
  935. this == ':' || this == '!' || this == '?' ||
  936. (!(*parens) && this == ')'))
  937. return Textbuffer_write(tail, this);
  938. else
  939. PUSH_TAIL_BUFFER(*tail, -1)
  940. return Tokenizer_emit_char(self, this);
  941. }
  942. /*
  943. Return whether the current head is the end of a free link.
  944. */
  945. static int
  946. Tokenizer_is_free_link(Tokenizer* self, Py_UNICODE this, Py_UNICODE next)
  947. {
  948. // Built from Tokenizer_parse()'s end sentinels:
  949. Py_UNICODE after = Tokenizer_READ(self, 2);
  950. uint64_t ctx = self->topstack->context;
  951. return (!this || this == '\n' || this == '[' || this == ']' ||
  952. this == '<' || this == '>' || (this == '\'' && next == '\'') ||
  953. (this == '|' && ctx & LC_TEMPLATE) ||
  954. (this == '=' && ctx & (LC_TEMPLATE_PARAM_KEY | LC_HEADING)) ||
  955. (this == '}' && next == '}' &&
  956. (ctx & LC_TEMPLATE || (after == '}' && ctx & LC_ARGUMENT))));
  957. }
  958. /*
  959. Really parse an external link.
  960. */
  961. static PyObject*
  962. Tokenizer_really_parse_external_link(Tokenizer* self, int brackets,
  963. Textbuffer** extra)
  964. {
  965. Py_UNICODE this, next;
  966. int parens = 0;
  967. if (brackets ? Tokenizer_parse_bracketed_uri_scheme(self) :
  968. Tokenizer_parse_free_uri_scheme(self))
  969. return NULL;
  970. if (BAD_ROUTE)
  971. return NULL;
  972. this = Tokenizer_READ(self, 0);
  973. if (!this || this == '\n' || this == ' ' || this == ']')
  974. return Tokenizer_fail_route(self);
  975. if (!brackets && this == '[')
  976. return Tokenizer_fail_route(self);
  977. while (1) {
  978. this = Tokenizer_READ(self, 0);
  979. next = Tokenizer_READ(self, 1);
  980. if (this == '&') {
  981. PUSH_TAIL_BUFFER(*extra, NULL)
  982. if (Tokenizer_parse_entity(self))
  983. return NULL;
  984. }
  985. else if (this == '<' && next == '!'
  986. && Tokenizer_READ(self, 2) == '-'
  987. && Tokenizer_READ(self, 3) == '-') {
  988. PUSH_TAIL_BUFFER(*extra, NULL)
  989. if (Tokenizer_parse_comment(self))
  990. return NULL;
  991. }
  992. else if (!brackets && Tokenizer_is_free_link(self, this, next)) {
  993. self->head--;
  994. return Tokenizer_pop(self);
  995. }
  996. else if (!this || this == '\n')
  997. return Tokenizer_fail_route(self);
  998. else if (this == '{' && next == '{' && Tokenizer_CAN_RECURSE(self)) {
  999. PUSH_TAIL_BUFFER(*extra, NULL)
  1000. if (Tokenizer_parse_template_or_argument(self))
  1001. return NULL;
  1002. }
  1003. else if (this == ']')
  1004. return Tokenizer_pop(self);
  1005. else if (this == ' ') {
  1006. if (brackets) {
  1007. if (Tokenizer_emit(self, ExternalLinkSeparator))
  1008. return NULL;
  1009. self->topstack->context ^= LC_EXT_LINK_URI;
  1010. self->topstack->context |= LC_EXT_LINK_TITLE;
  1011. self->head++;
  1012. return Tokenizer_parse(self, 0, 0);
  1013. }
  1014. if (Textbuffer_write(extra, ' '))
  1015. return NULL;
  1016. return Tokenizer_pop(self);
  1017. }
  1018. else if (!brackets) {
  1019. if (Tokenizer_handle_free_link_text(self, &parens, extra, this))
  1020. return NULL;
  1021. }
  1022. else {
  1023. if (Tokenizer_emit_char(self, this))
  1024. return NULL;
  1025. }
  1026. self->head++;
  1027. }
  1028. }
  1029. /*
  1030. Remove the URI scheme of a new external link from the textbuffer.
  1031. */
  1032. static int
  1033. Tokenizer_remove_uri_scheme_from_textbuffer(Tokenizer* self, PyObject* link)
  1034. {
  1035. PyObject *text = PyObject_GetAttrString(PyList_GET_ITEM(link, 0), "text"),
  1036. *split, *scheme;
  1037. Py_ssize_t length;
  1038. Textbuffer* temp;
  1039. if (!text)
  1040. return -1;
  1041. split = PyObject_CallMethod(text, "split", "si", ":", 1);
  1042. Py_DECREF(text);
  1043. if (!split)
  1044. return -1;
  1045. scheme = PyList_GET_ITEM(split, 0);
  1046. length = PyUnicode_GET_SIZE(scheme);
  1047. while (length) {
  1048. temp = self->topstack->textbuffer;
  1049. if (length <= temp->size) {
  1050. temp->size -= length;
  1051. break;
  1052. }
  1053. length -= temp->size;
  1054. self->topstack->textbuffer = temp->next;
  1055. free(temp->data);
  1056. free(temp);
  1057. }
  1058. Py_DECREF(split);
  1059. return 0;
  1060. }
  1061. /*
  1062. Parse an external link at the head of the wikicode string.
  1063. */
  1064. static int Tokenizer_parse_external_link(Tokenizer* self, int brackets)
  1065. {
  1066. #define INVALID_CONTEXT self->topstack->context & AGG_NO_EXT_LINKS
  1067. #define NOT_A_LINK \
  1068. if (!brackets && self->topstack->context & LC_DLTERM) \
  1069. return Tokenizer_handle_dl_term(self); \
  1070. return Tokenizer_emit_char(self, Tokenizer_READ(self, 0))
  1071. Py_ssize_t reset = self->head;
  1072. PyObject *link, *kwargs;
  1073. Textbuffer *extra = 0;
  1074. if (INVALID_CONTEXT || !(Tokenizer_CAN_RECURSE(self))) {
  1075. NOT_A_LINK;
  1076. }
  1077. extra = Textbuffer_new();
  1078. if (!extra)
  1079. return -1;
  1080. self->head++;
  1081. link = Tokenizer_really_parse_external_link(self, brackets, &extra);
  1082. if (BAD_ROUTE) {
  1083. RESET_ROUTE();
  1084. self->head = reset;
  1085. Textbuffer_dealloc(extra);
  1086. NOT_A_LINK;
  1087. }
  1088. if (!link) {
  1089. Textbuffer_dealloc(extra);
  1090. return -1;
  1091. }
  1092. if (!brackets) {
  1093. if (Tokenizer_remove_uri_scheme_from_textbuffer(self, link)) {
  1094. Textbuffer_dealloc(extra);
  1095. Py_DECREF(link);
  1096. return -1;
  1097. }
  1098. }
  1099. kwargs = PyDict_New();
  1100. if (!kwargs) {
  1101. Textbuffer_dealloc(extra);
  1102. Py_DECREF(link);
  1103. return -1;
  1104. }
  1105. PyDict_SetItemString(kwargs, "brackets", brackets ? Py_True : Py_False);
  1106. if (Tokenizer_emit_kwargs(self, ExternalLinkOpen, kwargs)) {
  1107. Textbuffer_dealloc(extra);
  1108. Py_DECREF(link);
  1109. return -1;
  1110. }
  1111. if (Tokenizer_emit_all(self, link)) {
  1112. Textbuffer_dealloc(extra);
  1113. Py_DECREF(link);
  1114. return -1;
  1115. }
  1116. Py_DECREF(link);
  1117. if (Tokenizer_emit(self, ExternalLinkClose)) {
  1118. Textbuffer_dealloc(extra);
  1119. return -1;
  1120. }
  1121. if (extra->size || extra->next)
  1122. return Tokenizer_emit_textbuffer(self, extra, 0);
  1123. Textbuffer_dealloc(extra);
  1124. return 0;
  1125. }
  1126. /*
  1127. Parse a section heading at the head of the wikicode string.
  1128. */
  1129. static int Tokenizer_parse_heading(Tokenizer* self)
  1130. {
  1131. Py_ssize_t reset = self->head;
  1132. int best = 1, i, context, diff;
  1133. HeadingData *heading;
  1134. PyObject *level, *kwargs;
  1135. self->global |= GL_HEADING;
  1136. self->head += 1;
  1137. while (Tokenizer_READ(self, 0) == '=') {
  1138. best++;
  1139. self->head++;
  1140. }
  1141. context = LC_HEADING_LEVEL_1 << (best > 5 ? 5 : best - 1);
  1142. heading = (HeadingData*) Tokenizer_parse(self, context, 1);
  1143. if (BAD_ROUTE) {
  1144. RESET_ROUTE();
  1145. self->head = reset + best - 1;
  1146. for (i = 0; i < best; i++) {
  1147. if (Tokenizer_emit_char(self, '='))
  1148. return -1;
  1149. }
  1150. self->global ^= GL_HEADING;
  1151. return 0;
  1152. }
  1153. level = NEW_INT_FUNC(heading->level);
  1154. if (!level) {
  1155. Py_DECREF(heading->title);
  1156. free(heading);
  1157. return -1;
  1158. }
  1159. kwargs = PyDict_New();
  1160. if (!kwargs) {
  1161. Py_DECREF(level);
  1162. Py_DECREF(heading->title);
  1163. free(heading);
  1164. return -1;
  1165. }
  1166. PyDict_SetItemString(kwargs, "level", level);
  1167. Py_DECREF(level);
  1168. if (Tokenizer_emit_kwargs(self, HeadingStart, kwargs)) {
  1169. Py_DECREF(heading->title);
  1170. free(heading);
  1171. return -1;
  1172. }
  1173. if (heading->level < best) {
  1174. diff = best - heading->level;
  1175. for (i = 0; i < diff; i++) {
  1176. if (Tokenizer_emit_char(self, '=')) {
  1177. Py_DECREF(heading->title);
  1178. free(heading);
  1179. return -1;
  1180. }
  1181. }
  1182. }
  1183. if (Tokenizer_emit_all(self, heading->title)) {
  1184. Py_DECREF(heading->title);
  1185. free(heading);
  1186. return -1;
  1187. }
  1188. Py_DECREF(heading->title);
  1189. free(heading);
  1190. if (Tokenizer_emit(self, HeadingEnd))
  1191. return -1;
  1192. self->global ^= GL_HEADING;
  1193. return 0;
  1194. }
  1195. /*
  1196. Handle the end of a section heading at the head of the string.
  1197. */
  1198. static HeadingData* Tokenizer_handle_heading_end(Tokenizer* self)
  1199. {
  1200. Py_ssize_t reset = self->head;
  1201. int best, i, current, level, diff;
  1202. HeadingData *after, *heading;
  1203. PyObject *stack;
  1204. self->head += 1;
  1205. best = 1;
  1206. while (Tokenizer_READ(self, 0) == '=') {
  1207. best++;
  1208. self->head++;
  1209. }
  1210. current = heading_level_from_context(self->topstack->context);
  1211. level = current > best ? (best > 6 ? 6 : best) :
  1212. (current > 6 ? 6 : current);
  1213. after = (HeadingData*) Tokenizer_parse(self, self->topstack->context, 1);
  1214. if (BAD_ROUTE) {
  1215. RESET_ROUTE();
  1216. if (level < best) {
  1217. diff = best - level;
  1218. for (i = 0; i < diff; i++) {
  1219. if (Tokenizer_emit_char(self, '='))
  1220. return NULL;
  1221. }
  1222. }
  1223. self->head = reset + best - 1;
  1224. }
  1225. else {
  1226. for (i = 0; i < best; i++) {
  1227. if (Tokenizer_emit_char(self, '=')) {
  1228. Py_DECREF(after->title);
  1229. free(after);
  1230. return NULL;
  1231. }
  1232. }
  1233. if (Tokenizer_emit_all(self, after->title)) {
  1234. Py_DECREF(after->title);
  1235. free(after);
  1236. return NULL;
  1237. }
  1238. Py_DECREF(after->title);
  1239. level = after->level;
  1240. free(after);
  1241. }
  1242. stack = Tokenizer_pop(self);
  1243. if (!stack)
  1244. return NULL;
  1245. heading = malloc(sizeof(HeadingData));
  1246. if (!heading) {
  1247. PyErr_NoMemory();
  1248. return NULL;
  1249. }
  1250. heading->title = stack;
  1251. heading->level = level;
  1252. return heading;
  1253. }
  1254. /*
  1255. Actually parse an HTML entity and ensure that it is valid.
  1256. */
  1257. static int Tokenizer_really_parse_entity(Tokenizer* self)
  1258. {
  1259. PyObject *kwargs, *textobj;
  1260. Py_UNICODE this;
  1261. int numeric, hexadecimal, i, j, zeroes, test;
  1262. char *valid, *text, *buffer, *def;
  1263. #define FAIL_ROUTE_AND_EXIT() { \
  1264. Tokenizer_fail_route(self); \
  1265. free(text); \
  1266. return 0; \
  1267. }
  1268. if (Tokenizer_emit(self, HTMLEntityStart))
  1269. return -1;
  1270. self->head++;
  1271. this = Tokenizer_READ(self, 0);
  1272. if (!this) {
  1273. Tokenizer_fail_route(self);
  1274. return 0;
  1275. }
  1276. if (this == '#') {
  1277. numeric = 1;
  1278. if (Tokenizer_emit(self, HTMLEntityNumeric))
  1279. return -1;
  1280. self->head++;
  1281. this = Tokenizer_READ(self, 0);
  1282. if (!this) {
  1283. Tokenizer_fail_route(self);
  1284. return 0;
  1285. }
  1286. if (this == 'x' || this == 'X') {
  1287. hexadecimal = 1;
  1288. kwargs = PyDict_New();
  1289. if (!kwargs)
  1290. return -1;
  1291. PyDict_SetItemString(kwargs, "char", Tokenizer_read(self, 0));
  1292. if (Tokenizer_emit_kwargs(self, HTMLEntityHex, kwargs))
  1293. return -1;
  1294. self->head++;
  1295. }
  1296. else
  1297. hexadecimal = 0;
  1298. }
  1299. else
  1300. numeric = hexadecimal = 0;
  1301. if (hexadecimal)
  1302. valid = HEXDIGITS;
  1303. else if (numeric)
  1304. valid = DIGITS;
  1305. else
  1306. valid = ALPHANUM;
  1307. text = calloc(MAX_ENTITY_SIZE, sizeof(char));
  1308. if (!text) {
  1309. PyErr_NoMemory();
  1310. return -1;
  1311. }
  1312. i = 0;
  1313. zeroes = 0;
  1314. while (1) {
  1315. this = Tokenizer_READ(self, 0);
  1316. if (this == ';') {
  1317. if (i == 0)
  1318. FAIL_ROUTE_AND_EXIT()
  1319. break;
  1320. }
  1321. if (i == 0 && this == '0') {
  1322. zeroes++;
  1323. self->head++;
  1324. continue;
  1325. }
  1326. if (i >= MAX_ENTITY_SIZE)
  1327. FAIL_ROUTE_AND_EXIT()
  1328. if (is_marker(this))
  1329. FAIL_ROUTE_AND_EXIT()
  1330. j = 0;
  1331. while (1) {
  1332. if (!valid[j])
  1333. FAIL_ROUTE_AND_EXIT()
  1334. if (this == valid[j])
  1335. break;
  1336. j++;
  1337. }
  1338. text[i] = (char) this;
  1339. self->head++;
  1340. i++;
  1341. }
  1342. if (numeric) {
  1343. sscanf(text, (hexadecimal ? "%x" : "%d"), &test);
  1344. if (test < 1 || test > 0x10FFFF)
  1345. FAIL_ROUTE_AND_EXIT()
  1346. }
  1347. else {
  1348. i = 0;
  1349. while (1) {
  1350. def = entitydefs[i];
  1351. if (!def) // We've reached the end of the defs without finding it
  1352. FAIL_ROUTE_AND_EXIT()
  1353. if (strcmp(text, def) == 0)
  1354. break;
  1355. i++;
  1356. }
  1357. }
  1358. if (zeroes) {
  1359. buffer = calloc(strlen(text) + zeroes + 1, sizeof(char));
  1360. if (!buffer) {
  1361. free(text);
  1362. PyErr_NoMemory();
  1363. return -1;
  1364. }
  1365. for (i = 0; i < zeroes; i++)
  1366. strcat(buffer, "0");
  1367. strcat(buffer, text);
  1368. free(text);
  1369. text = buffer;
  1370. }
  1371. textobj = PyUnicode_FromString(text);
  1372. if (!textobj) {
  1373. free(text);
  1374. return -1;
  1375. }
  1376. free(text);
  1377. kwargs = PyDict_New();
  1378. if (!kwargs) {
  1379. Py_DECREF(textobj);
  1380. return -1;
  1381. }
  1382. PyDict_SetItemString(kwargs, "text", textobj);
  1383. Py_DECREF(textobj);
  1384. if (Tokenizer_emit_kwargs(self, Text, kwargs))
  1385. return -1;
  1386. if (Tokenizer_emit(self, HTMLEntityEnd))
  1387. return -1;
  1388. return 0;
  1389. }
  1390. /*
  1391. Parse an HTML entity at the head of the wikicode string.
  1392. */
  1393. static int Tokenizer_parse_entity(Tokenizer* self)
  1394. {
  1395. Py_ssize_t reset = self->head;
  1396. PyObject *tokenlist;
  1397. if (Tokenizer_push(self, 0))
  1398. return -1;
  1399. if (Tokenizer_really_parse_entity(self))
  1400. return -1;
  1401. if (BAD_ROUTE) {
  1402. RESET_ROUTE();
  1403. self->head = reset;
  1404. if (Tokenizer_emit_char(self, '&'))
  1405. return -1;
  1406. return 0;
  1407. }
  1408. tokenlist = Tokenizer_pop(self);
  1409. if (!tokenlist)
  1410. return -1;
  1411. if (Tokenizer_emit_all(self, tokenlist)) {
  1412. Py_DECREF(tokenlist);
  1413. return -1;
  1414. }
  1415. Py_DECREF(tokenlist);
  1416. return 0;
  1417. }
  1418. /*
  1419. Parse an HTML comment at the head of the wikicode string.
  1420. */
  1421. static int Tokenizer_parse_comment(Tokenizer* self)
  1422. {
  1423. Py_ssize_t reset = self->head + 3;
  1424. PyObject *comment;
  1425. Py_UNICODE this;
  1426. self->head += 4;
  1427. if (Tokenizer_push(self, 0))
  1428. return -1;
  1429. while (1) {
  1430. this = Tokenizer_READ(self, 0);
  1431. if (!this) {
  1432. comment = Tokenizer_pop(self);
  1433. Py_XDECREF(comment);
  1434. self->head = reset;
  1435. return Tokenizer_emit_text(self, "<!--");
  1436. }
  1437. if (this == '-' && Tokenizer_READ(self, 1) == this &&
  1438. Tokenizer_READ(self, 2) == '>') {
  1439. if (Tokenizer_emit_first(self, CommentStart))
  1440. return -1;
  1441. if (Tokenizer_emit(self, CommentEnd))
  1442. return -1;
  1443. comment = Tokenizer_pop(self);
  1444. if (!comment)
  1445. return -1;
  1446. if (Tokenizer_emit_all(self, comment))
  1447. return -1;
  1448. Py_DECREF(comment);
  1449. self->head += 2;
  1450. if (self->topstack->context & LC_FAIL_NEXT) {
  1451. /* _verify_safe() sets this flag while parsing a template name
  1452. when it encounters what might be a comment -- we must unset
  1453. it to let _verify_safe() know it was correct: */
  1454. self->topstack->context ^= LC_FAIL_NEXT;
  1455. }
  1456. return 0;
  1457. }
  1458. if (Tokenizer_emit_char(self, this))
  1459. return -1;
  1460. self->head++;
  1461. }
  1462. }
  1463. /*
  1464. Write a pending tag attribute from data to the stack.
  1465. */
  1466. static int Tokenizer_push_tag_buffer(Tokenizer* self, TagData* data)
  1467. {
  1468. PyObject *tokens, *kwargs, *tmp, *pad_first, *pad_before_eq, *pad_after_eq;
  1469. if (data->context & TAG_QUOTED) {
  1470. kwargs = PyDict_New();
  1471. if (!kwargs)
  1472. return -1;
  1473. tmp = PyUnicode_FromUnicode(&data->quoter, 1);
  1474. if (!tmp)
  1475. return -1;
  1476. PyDict_SetItemString(kwargs, "char", tmp);
  1477. Py_DECREF(tmp);
  1478. if (Tokenizer_emit_first_kwargs(self, TagAttrQuote, kwargs))
  1479. return -1;
  1480. tokens = Tokenizer_pop(self);
  1481. if (!tokens)
  1482. return -1;
  1483. if (Tokenizer_emit_all(self, tokens)) {
  1484. Py_DECREF(tokens);
  1485. return -1;
  1486. }
  1487. Py_DECREF(tokens);
  1488. }
  1489. pad_first = Textbuffer_render(data->pad_first);
  1490. pad_before_eq = Textbuffer_render(data->pad_before_eq);
  1491. pad_after_eq = Textbuffer_render(data->pad_after_eq);
  1492. if (!pad_first || !pad_before_eq || !pad_after_eq)
  1493. return -1;
  1494. kwargs = PyDict_New();
  1495. if (!kwargs)
  1496. return -1;
  1497. PyDict_SetItemString(kwargs, "pad_first", pad_first);
  1498. PyDict_SetItemString(kwargs, "pad_before_eq", pad_before_eq);
  1499. PyDict_SetItemString(kwargs, "pad_after_eq", pad_after_eq);
  1500. Py_DECREF(pad_first);
  1501. Py_DECREF(pad_before_eq);
  1502. Py_DECREF(pad_after_eq);
  1503. if (Tokenizer_emit_first_kwargs(self, TagAttrStart, kwargs))
  1504. return -1;
  1505. tokens = Tokenizer_pop(self);
  1506. if (!tokens)
  1507. return -1;
  1508. if (Tokenizer_emit_all(self, tokens)) {
  1509. Py_DECREF(tokens);
  1510. return -1;
  1511. }
  1512. Py_DECREF(tokens);
  1513. if (TagData_reset_buffers(data))
  1514. return -1;
  1515. return 0;
  1516. }
  1517. /*
  1518. Handle whitespace inside of an HTML open tag.
  1519. */
  1520. static int
  1521. Tokenizer_handle_tag_space(Tokenizer* self, TagData* data, Py_UNICODE text)
  1522. {
  1523. uint64_t ctx = data->context;
  1524. uint64_t end_of_value = (ctx & TAG_ATTR_VALUE &&
  1525. !(ctx & (TAG_QUOTED | TAG_NOTE_QUOTE)));
  1526. if (end_of_value || (ctx & TAG_QUOTED && ctx & TAG_NOTE_SPACE)) {
  1527. if (Tokenizer_push_tag_buffer(self, data))
  1528. return -1;
  1529. data->context = TAG_ATTR_READY;
  1530. }
  1531. else if (ctx & TAG_NOTE_SPACE)
  1532. data->context = TAG_ATTR_READY;
  1533. else if (ctx & TAG_ATTR_NAME) {
  1534. data->context |= TAG_NOTE_EQUALS;
  1535. if (Textbuffer_write(&(data->pad_before_eq), text))
  1536. return -1;
  1537. }
  1538. if (ctx & TAG_QUOTED && !(ctx & TAG_NOTE_SPACE)) {
  1539. if (Tokenizer_emit_char(self, text))
  1540. return -1;
  1541. }
  1542. else if (data->context & TAG_ATTR_READY)
  1543. return Textbuffer_write(&(data->pad_first), text);
  1544. else if (data->context & TAG_ATTR_VALUE)
  1545. return Textbuffer_write(&(data->pad_after_eq), text);
  1546. return 0;
  1547. }
  1548. /*
  1549. Handle regular text inside of an HTML open tag.
  1550. */
  1551. static int Tokenizer_handle_tag_text(Tokenizer* self, Py_UNICODE text)
  1552. {
  1553. Py_UNICODE next = Tokenizer_READ(self, 1);
  1554. if (!is_marker(text) || !Tokenizer_CAN_RECURSE(self))
  1555. return Tokenizer_emit_char(self, text);
  1556. else if (text == next && next == '{')
  1557. return Tokenizer_parse_template_or_argument(self);
  1558. else if (text == next && next == '[')
  1559. return Tokenizer_parse_wikilink(self);
  1560. else if (text == '<')
  1561. return Tokenizer_parse_tag(self);
  1562. return Tokenizer_emit_char(self, text);
  1563. }
  1564. /*
  1565. Handle all sorts of text data inside of an HTML open tag.
  1566. */
  1567. static int
  1568. Tokenizer_handle_tag_data(Tokenizer* self, TagData* data, Py_UNICODE chunk)
  1569. {
  1570. PyObject *trash;
  1571. int first_time, escaped;
  1572. if (data->context & TAG_NAME) {
  1573. first_time = !(data->context & TAG_NOTE_SPACE);
  1574. if (is_marker(chunk) || (Py_UNICODE_ISSPACE(chunk) && first_time)) {
  1575. // Tags must start with text, not spaces
  1576. Tokenizer_fail_route(self);
  1577. return 0;
  1578. }
  1579. else if (first_time)
  1580. data->context |= TAG_NOTE_SPACE;
  1581. else if (Py_UNICODE_ISSPACE(chunk)) {
  1582. data->context = TAG_ATTR_READY;
  1583. return Tokenizer_handle_tag_space(self, data, chunk);
  1584. }
  1585. }
  1586. else if (Py_UNICODE_ISSPACE(chunk))
  1587. return Tokenizer_handle_tag_space(self, data, chunk);
  1588. else if (data->context & TAG_NOTE_SPACE) {
  1589. if (data->context & TAG_QUOTED) {
  1590. data->context = TAG_ATTR_VALUE;
  1591. trash = Tokenizer_pop(self);
  1592. Py_XDECREF(trash);
  1593. self->head = data->reset - 1; // Will be auto-incremented
  1594. }
  1595. else
  1596. Tokenizer_fail_route(self);
  1597. return 0;
  1598. }
  1599. else if (data->context & TAG_ATTR_READY) {
  1600. data->context = TAG_ATTR_NAME;
  1601. if (Tokenizer_push(self, LC_TAG_ATTR))
  1602. return -1;
  1603. }
  1604. else if (data->context & TAG_ATTR_NAME) {
  1605. if (chunk == '=') {
  1606. data->context = TAG_ATTR_VALUE | TAG_NOTE_QUOTE;
  1607. if (Tokenizer_emit(self, TagAttrEquals))
  1608. return -1;
  1609. return 0;
  1610. }
  1611. if (data->context & TAG_NOTE_EQUALS) {
  1612. if (Tokenizer_push_tag_buffer(self, data))
  1613. return -1;
  1614. data->context = TAG_ATTR_NAME;
  1615. if (Tokenizer_push(self, LC_TAG_ATTR))
  1616. return -1;
  1617. }
  1618. }
  1619. else { // data->context & TAG_ATTR_VALUE assured
  1620. escaped = (Tokenizer_READ_BACKWARDS(self, 1) == '\\' &&
  1621. Tokenizer_READ_BACKWARDS(self, 2) != '\\');
  1622. if (data->context & TAG_NOTE_QUOTE) {
  1623. data->context ^= TAG_NOTE_QUOTE;
  1624. if ((chunk == '"' || chunk == '\'') && !escaped) {
  1625. data->context |= TAG_QUOTED;
  1626. data->quoter = chunk;
  1627. data->reset = self->head;
  1628. if (Tokenizer_push(self, self->topstack->context))
  1629. return -1;
  1630. return 0;
  1631. }
  1632. }
  1633. else if (data->context & TAG_QUOTED) {
  1634. if (chunk == data->quoter && !escaped) {
  1635. data->context |= TAG_NOTE_SPACE;
  1636. return 0;
  1637. }
  1638. }
  1639. }
  1640. return Tokenizer_handle_tag_text(self, chunk);
  1641. }
  1642. /*
  1643. Handle the closing of a open tag (<foo>).
  1644. */
  1645. static int
  1646. Tokenizer_handle_tag_close_open(Tokenizer* self, TagData* data, PyObject* cls)
  1647. {
  1648. PyObject *padding, *kwargs;
  1649. if (data->context & (TAG_ATTR_NAME | TAG_ATTR_VALUE)) {
  1650. if (Tokenizer_push_tag_buffer(self, data))
  1651. return -1;
  1652. }
  1653. padding = Textbuffer_render(data->pad_first);
  1654. if (!padding)
  1655. return -1;
  1656. kwargs = PyDict_New();
  1657. if (!kwargs) {
  1658. Py_DECREF(padding);
  1659. return -1;
  1660. }
  1661. PyDict_SetItemString(kwargs, "padding", padding);
  1662. Py_DECREF(padding);
  1663. if (Tokenizer_emit_kwargs(self, cls, kwargs))
  1664. return -1;
  1665. self->head++;
  1666. return 0;
  1667. }
  1668. /*
  1669. Handle the opening of a closing tag (</foo>).
  1670. */
  1671. static int Tokenizer_handle_tag_open_close(Tokenizer* self)
  1672. {
  1673. if (Tokenizer_emit(self, TagOpenClose))
  1674. return -1;
  1675. if (Tokenizer_push(self, LC_TAG_CLOSE))
  1676. return -1;
  1677. self->head++;
  1678. return 0;
  1679. }
  1680. /*
  1681. Handle the ending of a closing tag (</foo>).
  1682. */
  1683. static PyObject* Tokenizer_handle_tag_close_close(Tokenizer* self)
  1684. {
  1685. PyObject *closing, *first, *so, *sc;
  1686. int valid = 1;
  1687. closing = Tokenizer_pop(self);
  1688. if (!closing)
  1689. return NULL;
  1690. if (PyList_GET_SIZE(closing) != 1)
  1691. valid = 0;
  1692. else {
  1693. first = PyList_GET_ITEM(closing, 0);
  1694. switch (PyObject_IsInstance(first, Text)) {
  1695. case 0:
  1696. valid = 0;
  1697. break;
  1698. case 1: {
  1699. so = strip_tag_name(first);
  1700. sc = strip_tag_name(PyList_GET_ITEM(self->topstack->stack, 1));
  1701. if (so && sc) {
  1702. if (PyUnicode_Compare(so, sc))
  1703. valid = 0;
  1704. Py_DECREF(so);
  1705. Py_DECREF(sc);
  1706. break;
  1707. }
  1708. Py_XDECREF(so);
  1709. Py_XDECREF(sc);
  1710. }
  1711. case -1:
  1712. Py_DECREF(closing);
  1713. return NULL;
  1714. }
  1715. }
  1716. if (!valid) {
  1717. Py_DECREF(closing);
  1718. return Tokenizer_fail_route(self);
  1719. }
  1720. if (Tokenizer_emit_all(self, closing)) {
  1721. Py_DECREF(closing);
  1722. return NULL;
  1723. }
  1724. Py_DECREF(closing);
  1725. if (Tokenizer_emit(self, TagCloseClose))
  1726. return NULL;
  1727. return Tokenizer_pop(self);
  1728. }
  1729. /*
  1730. Handle the body of an HTML tag that is parser-blacklisted.
  1731. */
  1732. static PyObject* Tokenizer_handle_blacklisted_tag(Tokenizer* self)
  1733. {
  1734. Py_UNICODE this, next;
  1735. while (1) {
  1736. this = Tokenizer_READ(self, 0);
  1737. next = Tokenizer_READ(self, 1);
  1738. if (!this)
  1739. return Tokenizer_fail_route(self);
  1740. else if (this == '<' && next == '/') {
  1741. if (Tokenizer_handle_tag_open_close(self))
  1742. return NULL;
  1743. self->head++;
  1744. return Tokenizer_parse(self, 0, 0);
  1745. }
  1746. else if (this == '&') {
  1747. if (Tokenizer_parse_entity(self))
  1748. return NULL;
  1749. }
  1750. else if (Tokenizer_emit_char(self, this))
  1751. return NULL;
  1752. self->head++;
  1753. }
  1754. }
  1755. /*
  1756. Handle the end of an implicitly closing single-only HTML tag.
  1757. */
  1758. static PyObject* Tokenizer_handle_single_only_tag_end(Tokenizer* self)
  1759. {
  1760. PyObject *top, *padding, *kwargs;
  1761. top = PyObject_CallMethod(self->topstack->stack, "pop", NULL);
  1762. if (!top)
  1763. return NULL;
  1764. padding = PyObject_GetAttrString(top, "padding");
  1765. Py_DECREF(top);
  1766. if (!padding)
  1767. return NULL;
  1768. kwargs = PyDict_New();
  1769. if (!kwargs) {
  1770. Py_DECREF(padding);
  1771. return NULL;
  1772. }
  1773. PyDict_SetItemString(kwargs, "padding", padding);
  1774. PyDict_SetItemString(kwargs, "implicit", Py_True);
  1775. Py_DECREF(padding);
  1776. if (Tokenizer_emit_kwargs(self, TagCloseSelfclose, kwargs))
  1777. return NULL;
  1778. self->head--; // Offset displacement done by handle_tag_close_open
  1779. return Tokenizer_pop(self);
  1780. }
  1781. /*
  1782. Handle the stream end when inside a single-supporting HTML tag.
  1783. */
  1784. static PyObject* Tokenizer_handle_single_tag_end(Tokenizer* self)
  1785. {
  1786. PyObject *token = 0, *padding, *kwargs;
  1787. Py_ssize_t len, index;
  1788. int depth = 1, is_instance;
  1789. len = PyList_GET_SIZE(self->topstack->stack);
  1790. for (index = 2; index < len; index++) {
  1791. token = PyList_GET_ITEM(self->topstack->stack, index);
  1792. is_instance = PyObject_IsInstance(token, TagOpenOpen);
  1793. if (is_instance == -1)
  1794. return NULL;
  1795. else if (is_instance == 1)
  1796. depth++;
  1797. is_instance = PyObject_IsInstance(token, TagCloseOpen);
  1798. if (is_instance == -1)
  1799. return NULL;
  1800. else if (is_instance == 1) {
  1801. depth--;
  1802. if (depth == 0)
  1803. break;
  1804. }
  1805. }
  1806. if (!token || depth > 0)
  1807. return NULL;
  1808. padding = PyObject_GetAttrString(token, "padding");
  1809. if (!padding)
  1810. return NULL;
  1811. kwargs = PyDict_New();
  1812. if (!kwargs) {
  1813. Py_DECREF(padding);
  1814. return NULL;
  1815. }
  1816. PyDict_SetItemString(kwargs, "padding", padding);
  1817. PyDict_SetItemString(kwargs, "implicit", Py_True);
  1818. Py_DECREF(padding);
  1819. token = PyObject_Call(TagCloseSelfclose, NOARGS, kwargs);
  1820. Py_DECREF(kwargs);
  1821. if (!token)
  1822. return NULL;
  1823. if (PyList_SetItem(self->topstack->stack, index, token)) {
  1824. Py_DECREF(token);
  1825. return NULL;
  1826. }
  1827. return Tokenizer_pop(self);
  1828. }
  1829. /*
  1830. Actually parse an HTML tag, starting with the open (<foo>).
  1831. */
  1832. static PyObject* Tokenizer_really_parse_tag(Tokenizer* self)
  1833. {
  1834. TagData *data = TagData_new();
  1835. PyObject *token, *text, *trash;
  1836. Py_UNICODE this, next;
  1837. int can_exit;
  1838. if (!data)
  1839. return NULL;
  1840. if (Tokenizer_push(self, LC_TAG_OPEN)) {
  1841. TagData_dealloc(data);
  1842. return NULL;
  1843. }
  1844. if (Tokenizer_emit(self, TagOpenOpen)) {
  1845. TagData_dealloc(data);
  1846. return NULL;
  1847. }
  1848. while (1) {
  1849. this = Tokenizer_READ(self, 0);
  1850. next = Tokenizer_READ(self, 1);
  1851. can_exit = (!(data->context & (TAG_QUOTED | TAG_NAME)) ||
  1852. data->context & TAG_NOTE_SPACE);
  1853. if (!this) {
  1854. if (self->topstack->context & LC_TAG_ATTR) {
  1855. if (data->context & TAG_QUOTED) {
  1856. // Unclosed attribute quote: reset, don't die
  1857. data->context = TAG_ATTR_VALUE;
  1858. trash = Tokenizer_pop(self);
  1859. Py_XDECREF(trash);
  1860. self->head = data->reset;
  1861. continue;
  1862. }
  1863. trash = Tokenizer_pop(self);
  1864. Py_XDECREF(trash);
  1865. }
  1866. TagData_dealloc(data);
  1867. return Tokenizer_fail_route(self);
  1868. }
  1869. else if (this == '>' && can_exit) {
  1870. if (Tokenizer_handle_tag_close_open(self, data, TagCloseOpen)) {
  1871. TagData_dealloc(data);
  1872. return NULL;
  1873. }
  1874. TagData_dealloc(data);
  1875. self->topstack->context = LC_TAG_BODY;
  1876. token = PyList_GET_ITEM(self->topstack->stack, 1);
  1877. text = PyObject_GetAttrString(token, "text");
  1878. if (!text)
  1879. return NULL;
  1880. if (IS_SINGLE_ONLY(text)) {
  1881. Py_DECREF(text);
  1882. return Tokenizer_handle_single_only_tag_end(self);
  1883. }
  1884. if (IS_PARSABLE(text)) {
  1885. Py_DECREF(text);
  1886. return Tokenizer_parse(self, 0, 0);
  1887. }
  1888. Py_DECREF(text);
  1889. return Tokenizer_handle_blacklisted_tag(self);
  1890. }
  1891. else if (this == '/' && next == '>' && can_exit) {
  1892. if (Tokenizer_handle_tag_close_open(self, data,
  1893. TagCloseSelfclose)) {
  1894. TagData_dealloc(data);
  1895. return NULL;
  1896. }
  1897. TagData_dealloc(data);
  1898. return Tokenizer_pop(self);
  1899. }
  1900. else {
  1901. if (Tokenizer_handle_tag_data(self, data, this) || BAD_ROUTE) {
  1902. TagData_dealloc(data);
  1903. return NULL;
  1904. }
  1905. }
  1906. self->head++;
  1907. }
  1908. }
  1909. /*
  1910. Handle the (possible) start of an implicitly closing single tag.
  1911. */
  1912. static int Tokenizer_handle_invalid_tag_start(Tokenizer* self)
  1913. {
  1914. Py_ssize_t reset = self->head + 1, pos = 0;
  1915. Textbuffer* buf;
  1916. PyObject *name, *tag;
  1917. Py_UNICODE this;
  1918. self->head += 2;
  1919. buf = Textbuffer_new();
  1920. if (!buf)
  1921. return -1;
  1922. while (1) {
  1923. this = Tokenizer_READ(self, pos);
  1924. if (Py_UNICODE_ISSPACE(this) || is_marker(this)) {
  1925. name = Textbuffer_render(buf);
  1926. if (!name) {
  1927. Textbuffer_dealloc(buf);
  1928. return -1;
  1929. }
  1930. if (!IS_SINGLE_ONLY(name))
  1931. FAIL_ROUTE(0);
  1932. Py_DECREF(name);
  1933. break;
  1934. }
  1935. Textbuffer_write(&buf, this);
  1936. pos++;
  1937. }
  1938. Textbuffer_dealloc(buf);
  1939. if (!BAD_ROUTE)
  1940. tag = Tokenizer_really_parse_tag(self);
  1941. if (BAD_ROUTE) {
  1942. RESET_ROUTE();
  1943. self->head = reset;
  1944. return Tokenizer_emit_text(self, "</");
  1945. }
  1946. if (!tag)
  1947. return -1;
  1948. // Set invalid=True flag of TagOpenOpen
  1949. if (PyObject_SetAttrString(PyList_GET_ITEM(tag, 0), "invalid", Py_True))
  1950. return -1;
  1951. if (Tokenizer_emit_all(self, tag)) {
  1952. Py_DECREF(tag);
  1953. return -1;
  1954. }
  1955. Py_DECREF(tag);
  1956. return 0;
  1957. }
  1958. /*
  1959. Parse an HTML tag at the head of the wikicode string.
  1960. */
  1961. static int Tokenizer_parse_tag(Tokenizer* self)
  1962. {
  1963. Py_ssize_t reset = self->head;
  1964. PyObject* tag;
  1965. self->head++;
  1966. tag = Tokenizer_really_parse_tag(self);
  1967. if (BAD_ROUTE) {
  1968. RESET_ROUTE();
  1969. self->head = reset;
  1970. return Tokenizer_emit_char(self, '<');
  1971. }
  1972. if (!tag) {
  1973. return -1;
  1974. }
  1975. if (Tokenizer_emit_all(self, tag)) {
  1976. Py_DECREF(tag);
  1977. return -1;
  1978. }
  1979. Py_DECREF(tag);
  1980. return 0;
  1981. }
  1982. /*
  1983. Write the body of a tag and the tokens that should surround it.
  1984. */
  1985. static int Tokenizer_emit_style_tag(Tokenizer* self, const char* tag,
  1986. const char* ticks, PyObject* body)
  1987. {
  1988. PyObject *markup, *kwargs;
  1989. markup = PyUnicode_FromString(ticks);
  1990. if (!markup)
  1991. return -1;
  1992. kwargs = PyDict_New();
  1993. if (!kwargs) {
  1994. Py_DECREF(markup);
  1995. return -1;
  1996. }
  1997. PyDict_SetItemString(kwargs, "wiki_markup", markup);
  1998. Py_DECREF(markup);
  1999. if (Tokenizer_emit_kwargs(self, TagOpenOpen, kwargs))
  2000. return -1;
  2001. if (Tokenizer_emit_text(self, tag))
  2002. return -1;
  2003. if (Tokenizer_emit(self, TagCloseOpen))
  2004. return -1;
  2005. if (Tokenizer_emit_all(self, body))
  2006. return -1;
  2007. Py_DECREF(body);
  2008. if (Tokenizer_emit(self, TagOpenClose))
  2009. return -1;
  2010. if (Tokenizer_emit_text(self, tag))
  2011. return -1;
  2012. if (Tokenizer_emit(self, TagCloseClose))
  2013. return -1;
  2014. return 0;
  2015. }
  2016. /*
  2017. Parse wiki-style italics.
  2018. */
  2019. static int Tokenizer_parse_italics(Tokenizer* self)
  2020. {
  2021. Py_ssize_t reset = self->head;
  2022. uint64_t context;
  2023. PyObject *stack;
  2024. stack = Tokenizer_parse(self, LC_STYLE_ITALICS, 1);
  2025. if (BAD_ROUTE) {
  2026. RESET_ROUTE();
  2027. self->head = reset;
  2028. if (BAD_ROUTE_CONTEXT & LC_STYLE_PASS_AGAIN) {
  2029. context = LC_STYLE_ITALICS | LC_STYLE_SECOND_PASS;
  2030. stack = Tokenizer_parse(self, context, 1);
  2031. }
  2032. else
  2033. return Tokenizer_emit_text(self, "''");
  2034. }
  2035. if (!stack)
  2036. return -1;
  2037. return Tokenizer_emit_style_tag(self, "i", "''", stack);
  2038. }
  2039. /*
  2040. Parse wiki-style bold.
  2041. */
  2042. static int Tokenizer_parse_bold(Tokenizer* self)
  2043. {
  2044. Py_ssize_t reset = self->head;
  2045. PyObject *stack;
  2046. stack = Tokenizer_parse(self, LC_STYLE_BOLD, 1);
  2047. if (BAD_ROUTE) {
  2048. RESET_ROUTE();
  2049. self->head = reset;
  2050. if (self->topstack->context & LC_STYLE_SECOND_PASS)
  2051. return Tokenizer_emit_char(self, '\'') ? -1 : 1;
  2052. if (self->topstack->context & LC_STYLE_ITALICS) {
  2053. self->topstack->context |= LC_STYLE_PASS_AGAIN;
  2054. return Tokenizer_emit_text(self, "'''");
  2055. }
  2056. if (Tokenizer_emit_char(self, '\''))
  2057. return -1;
  2058. return Tokenizer_parse_italics(self);
  2059. }
  2060. if (!stack)
  2061. return -1;
  2062. return Tokenizer_emit_style_tag(self, "b", "'''", stack);
  2063. }
  2064. /*
  2065. Parse wiki-style italics and bold together (i.e., five ticks).
  2066. */
  2067. static int Tokenizer_parse_italics_and_bold(Tokenizer* self)
  2068. {
  2069. Py_ssize_t reset = self->head;
  2070. PyObject *stack, *stack2;
  2071. stack = Tokenizer_parse(self, LC_STYLE_BOLD, 1);
  2072. if (BAD_ROUTE) {
  2073. RESET_ROUTE();
  2074. self->head = reset;
  2075. stack = Tokenizer_parse(self, LC_STYLE_ITALICS, 1);
  2076. if (BAD_ROUTE) {
  2077. RESET_ROUTE();
  2078. self->head = reset;
  2079. return Tokenizer_emit_text(self, "'''''");
  2080. }
  2081. if (!stack)
  2082. return -1;
  2083. reset = self->head;
  2084. stack2 = Tokenizer_parse(self, LC_STYLE_BOLD, 1);
  2085. if (BAD_ROUTE) {
  2086. RESET_ROUTE();
  2087. self->head = reset;
  2088. if (Tokenizer_emit_text(self, "'''"))
  2089. return -1;
  2090. return Tokenizer_emit_style_tag(self, "i", "''", stack);
  2091. }
  2092. if (!stack2)
  2093. return -1;
  2094. if (Tokenizer_push(self, 0))
  2095. return -1;
  2096. if (Tokenizer_emit_style_tag(self, "i", "''", stack))
  2097. return -1;
  2098. if (Tokenizer_emit_all(self, stack2))
  2099. return -1;
  2100. Py_DECREF(stack2);
  2101. stack2 = Tokenizer_pop(self);
  2102. if (!stack2)
  2103. return -1;
  2104. return Tokenizer_emit_style_tag(self, "b", "'''", stack2);
  2105. }
  2106. if (!stack)
  2107. return -1;
  2108. reset = self->head;
  2109. stack2 = Tokenizer_parse(self, LC_STYLE_ITALICS, 1);
  2110. if (BAD_ROUTE) {
  2111. RESET_ROUTE();
  2112. self->head = reset;
  2113. if (Tokenizer_emit_text(self, "''"))
  2114. return -1;
  2115. return Tokenizer_emit_style_tag(self, "b", "'''", stack);
  2116. }
  2117. if (!stack2)
  2118. return -1;
  2119. if (Tokenizer_push(self, 0))
  2120. return -1;
  2121. if (Tokenizer_emit_style_tag(self, "b", "'''", stack))
  2122. return -1;
  2123. if (Tokenizer_emit_all(self, stack2))
  2124. return -1;
  2125. Py_DECREF(stack2);
  2126. stack2 = Tokenizer_pop(self);
  2127. if (!stack2)
  2128. return -1;
  2129. return Tokenizer_emit_style_tag(self, "i", "''", stack2);
  2130. }
  2131. /*
  2132. Parse wiki-style formatting (''/''' for italics/bold).
  2133. */
  2134. static PyObject* Tokenizer_parse_style(Tokenizer* self)
  2135. {
  2136. uint64_t context = self->topstack->context, ticks = 2, i;
  2137. self->head += 2;
  2138. while (Tokenizer_READ(self, 0) == '\'') {
  2139. self->head++;
  2140. ticks++;
  2141. }
  2142. if (ticks > 5) {
  2143. for (i = 0; i < ticks - 5; i++) {
  2144. if (Tokenizer_emit_char(self, '\''))
  2145. return NULL;
  2146. }
  2147. ticks = 5;
  2148. }
  2149. else if (ticks == 4) {
  2150. if (Tokenizer_emit_char(self, '\''))
  2151. return NULL;
  2152. ticks = 3;
  2153. }
  2154. if ((context & LC_STYLE_ITALICS && (ticks == 2 || ticks == 5)) ||
  2155. (context & LC_STYLE_BOLD && (ticks == 3 || ticks == 5))) {
  2156. if (ticks == 5)
  2157. self->head -= context & LC_STYLE_ITALICS ? 3 : 2;
  2158. return Tokenizer_pop(self);
  2159. }
  2160. if (!Tokenizer_CAN_RECURSE(self)) {
  2161. if (ticks == 3) {
  2162. if (context & LC_STYLE_SECOND_PASS) {
  2163. if (Tokenizer_emit_char(self, '\''))
  2164. return NULL;
  2165. return Tokenizer_pop(self);
  2166. }
  2167. if (context & LC_STYLE_ITALICS)
  2168. self->topstack->context |= LC_STYLE_PASS_AGAIN;
  2169. }
  2170. for (i = 0; i < ticks; i++) {
  2171. if (Tokenizer_emit_char(self, '\''))
  2172. return NULL;
  2173. }
  2174. }
  2175. else if (ticks == 2) {
  2176. if (Tokenizer_parse_italics(self))
  2177. return NULL;
  2178. }
  2179. else if (ticks == 3) {
  2180. switch (Tokenizer_parse_bold(self)) {
  2181. case 1:
  2182. return Tokenizer_pop(self);
  2183. case -1:
  2184. return NULL;
  2185. }
  2186. }
  2187. else {
  2188. if (Tokenizer_parse_italics_and_bold(self))
  2189. return NULL;
  2190. }
  2191. self->head--;
  2192. return Py_None;
  2193. }
  2194. /*
  2195. Handle a list marker at the head (#, *, ;, :).
  2196. */
  2197. static int Tokenizer_handle_list_marker(Tokenizer* self)
  2198. {
  2199. PyObject *markup = Tokenizer_read(self, 0), *kwargs;
  2200. Py_UNICODE code = *PyUnicode_AS_UNICODE(markup);
  2201. if (code == ';')
  2202. self->topstack->context |= LC_DLTERM;
  2203. kwargs = PyDict_New();
  2204. if (!kwargs)
  2205. return -1;
  2206. PyDict_SetItemString(kwargs, "wiki_markup", markup);
  2207. if (Tokenizer_emit_kwargs(self, TagOpenOpen, kwargs))
  2208. return -1;
  2209. if (Tokenizer_emit_text(self, GET_HTML_TAG(code)))
  2210. return -1;
  2211. if (Tokenizer_emit(self, TagCloseSelfclose))
  2212. return -1;
  2213. return 0;
  2214. }
  2215. /*
  2216. Handle a wiki-style list (#, *, ;, :).
  2217. */
  2218. static int Tokenizer_handle_list(Tokenizer* self)
  2219. {
  2220. Py_UNICODE marker = Tokenizer_READ(self, 1);
  2221. if (Tokenizer_handle_list_marker(self))
  2222. return -1;
  2223. while (marker == '#' || marker == '*' || marker == ';' ||
  2224. marker == ':') {
  2225. self->head++;
  2226. if (Tokenizer_handle_list_marker(self))
  2227. return -1;
  2228. marker = Tokenizer_READ(self, 1);
  2229. }
  2230. return 0;
  2231. }
  2232. /*
  2233. Handle a wiki-style horizontal rule (----) in the string.
  2234. */
  2235. static int Tokenizer_handle_hr(Tokenizer* self)
  2236. {
  2237. PyObject *markup, *kwargs;
  2238. Textbuffer *buffer = Textbuffer_new();
  2239. int i;
  2240. if (!buffer)
  2241. return -1;
  2242. self->head += 3;
  2243. for (i = 0; i < 4; i++) {
  2244. if (Textbuffer_write(&buffer, '-'))
  2245. return -1;
  2246. }
  2247. while (Tokenizer_READ(self, 1) == '-') {
  2248. if (Textbuffer_write(&buffer, '-'))
  2249. return -1;
  2250. self->head++;
  2251. }
  2252. markup = Textbuffer_render(buffer);
  2253. Textbuffer_dealloc(buffer);
  2254. if (!markup)
  2255. return -1;
  2256. kwargs = PyDict_New();
  2257. if (!kwargs)
  2258. return -1;
  2259. PyDict_SetItemString(kwargs, "wiki_markup", markup);
  2260. Py_DECREF(markup);
  2261. if (Tokenizer_emit_kwargs(self, TagOpenOpen, kwargs))
  2262. return -1;
  2263. if (Tokenizer_emit_text(self, "hr"))
  2264. return -1;
  2265. if (Tokenizer_emit(self, TagCloseSelfclose))
  2266. return -1;
  2267. return 0;
  2268. }
  2269. /*
  2270. Handle the term in a description list ('foo' in ';foo:bar').
  2271. */
  2272. static int Tokenizer_handle_dl_term(Tokenizer* self)
  2273. {
  2274. self->topstack->context ^= LC_DLTERM;
  2275. if (Tokenizer_READ(self, 0) == ':')
  2276. return Tokenizer_handle_list_marker(self);
  2277. return Tokenizer_emit_char(self, '\n');
  2278. }
  2279. /*
  2280. Handle the end of the stream of wikitext.
  2281. */
  2282. static PyObject* Tokenizer_handle_end(Tokenizer* self, uint64_t context)
  2283. {
  2284. PyObject *token, *text, *trash;
  2285. int single;
  2286. if (context & AGG_FAIL) {
  2287. if (context & LC_TAG_BODY) {
  2288. token = PyList_GET_ITEM(self->topstack->stack, 1);
  2289. text = PyObject_GetAttrString(token, "text");
  2290. if (!text)
  2291. return NULL;
  2292. single = IS_SINGLE(text);
  2293. Py_DECREF(text);
  2294. if (single)
  2295. return Tokenizer_handle_single_tag_end(self);
  2296. }
  2297. else if (context & AGG_DOUBLE) {
  2298. trash = Tokenizer_pop(self);
  2299. Py_XDECREF(trash);
  2300. }
  2301. return Tokenizer_fail_route(self);
  2302. }
  2303. return Tokenizer_pop(self);
  2304. }
  2305. /*
  2306. Emit a table tag.
  2307. */
  2308. static int Tokenizer_emit_table_tag(Tokenizer* self, const char* open_open_markup,
  2309. const char* tag, PyObject* style, PyObject* padding,
  2310. const char* close_open_markup, PyObject* contents,
  2311. const char* open_close_markup)
  2312. {
  2313. PyObject *open_open_kwargs, *open_open_markup_unicode, *close_open_kwargs, *close_open_markup_unicode,
  2314. *open_close_kwargs, *open_close_markup_unicode;
  2315. open_open_kwargs = PyDict_New();
  2316. if (!open_open_kwargs)
  2317. goto fail_decref_all;
  2318. open_open_markup_unicode = PyUnicode_FromString(open_open_markup);
  2319. if (!open_open_markup_unicode) {
  2320. Py_DECREF(open_open_kwargs);
  2321. goto fail_decref_all;
  2322. }
  2323. PyDict_SetItemString(open_open_kwargs, "wiki_markup", open_open_markup_unicode);
  2324. Py_DECREF(open_open_markup_unicode);
  2325. if (Tokenizer_emit_kwargs(self, TagOpenOpen, open_open_kwargs))
  2326. goto fail_decref_all;
  2327. if (Tokenizer_emit_text(self, tag))
  2328. goto fail_decref_all;
  2329. if (style) {
  2330. if (Tokenizer_emit_all(self, style))
  2331. goto fail_decref_all;
  2332. Py_DECREF(style);
  2333. }
  2334. close_open_kwargs = PyDict_New();
  2335. if (!close_open_kwargs)
  2336. goto fail_decref_padding_contents;
  2337. if (close_open_markup && strlen(close_open_markup) != 0) {
  2338. close_open_markup_unicode = PyUnicode_FromString(close_open_markup);
  2339. if (!close_open_markup_unicode) {
  2340. Py_DECREF(close_open_kwargs);
  2341. goto fail_decref_padding_contents;
  2342. }
  2343. PyDict_SetItemString(close_open_kwargs, "wiki_markup", close_open_markup_unicode);
  2344. Py_DECREF(close_open_markup_unicode);
  2345. }
  2346. PyDict_SetItemString(close_open_kwargs, "padding", padding);
  2347. Py_DECREF(padding);
  2348. if (Tokenizer_emit_kwargs(self, TagCloseOpen, close_open_kwargs))
  2349. goto fail_decref_contents;
  2350. if (contents) {
  2351. if (Tokenizer_emit_all(self, contents))
  2352. goto fail_decref_contents;
  2353. Py_DECREF(contents);
  2354. }
  2355. open_close_kwargs = PyDict_New();
  2356. if (!open_close_kwargs)
  2357. return -1;
  2358. open_close_markup_unicode = PyUnicode_FromString(open_close_markup);
  2359. if (!open_close_markup_unicode) {
  2360. Py_DECREF(open_close_kwargs);
  2361. return -1;
  2362. }
  2363. PyDict_SetItemString(open_close_kwargs, "wiki_markup", open_close_markup_unicode);
  2364. Py_DECREF(open_close_markup_unicode);
  2365. if (Tokenizer_emit_kwargs(self, TagOpenClose, open_close_kwargs))
  2366. return -1;
  2367. if (Tokenizer_emit_text(self, tag))
  2368. return -1;
  2369. if (Tokenizer_emit(self, TagCloseClose))
  2370. return -1;
  2371. return 0;
  2372. fail_decref_all:
  2373. Py_XDECREF(style);
  2374. fail_decref_padding_contents:
  2375. Py_DECREF(padding);
  2376. fail_decref_contents:
  2377. Py_DECREF(contents);
  2378. return -1;
  2379. }
  2380. /*
  2381. Parse until ``end_token`` as style attributes for a table.
  2382. */
  2383. static PyObject* Tokenizer_parse_as_table_style(Tokenizer* self, char end_token,
  2384. int break_on_table_end)
  2385. {
  2386. TagData *data = TagData_new();
  2387. PyObject *padding, *trash;
  2388. Py_UNICODE this, next;
  2389. int can_exit, table_end;
  2390. if (!data)
  2391. return NULL;
  2392. data->context = TAG_ATTR_READY;
  2393. while (1) {
  2394. this = Tokenizer_READ(self, 0);
  2395. next = Tokenizer_READ(self, 1);
  2396. can_exit = (!(data->context & TAG_QUOTED) || data->context & TAG_NOTE_SPACE);
  2397. table_end = (break_on_table_end && this == '|' && next == '}');
  2398. if ((this == end_token && can_exit) || table_end) {
  2399. if (data->context & (TAG_ATTR_NAME | TAG_ATTR_VALUE)) {
  2400. if (Tokenizer_push_tag_buffer(self, data)) {
  2401. TagData_dealloc(data);
  2402. return NULL;
  2403. }
  2404. }
  2405. if (Py_UNICODE_ISSPACE(this))
  2406. Textbuffer_write(&(data->pad_first), this);
  2407. padding = Textbuffer_render(data->pad_first);
  2408. TagData_dealloc(data);
  2409. if (!padding)
  2410. return NULL;
  2411. return padding;
  2412. }
  2413. else if (!this || table_end || this == end_token) {
  2414. if (self->topstack->context & LC_TAG_ATTR) {
  2415. if (data->context & TAG_QUOTED) {
  2416. // Unclosed attribute quote: reset, don't die
  2417. data->context = TAG_ATTR_VALUE;
  2418. trash = Tokenizer_pop(self);
  2419. Py_XDECREF(trash);
  2420. self->head = data->reset;
  2421. continue;
  2422. }
  2423. trash = Tokenizer_pop(self);
  2424. Py_XDECREF(trash);
  2425. }
  2426. TagData_dealloc(data);
  2427. return Tokenizer_fail_route(self);
  2428. }
  2429. else {
  2430. if (Tokenizer_handle_tag_data(self, data, this) || BAD_ROUTE) {
  2431. TagData_dealloc(data);
  2432. return NULL;
  2433. }
  2434. }
  2435. self->head++;
  2436. }
  2437. }
  2438. /*
  2439. Handle the start of a table.
  2440. */
  2441. static int Tokenizer_handle_table_start(Tokenizer* self)
  2442. {
  2443. self->head += 2;
  2444. Py_ssize_t reset = self->head;
  2445. PyObject *style, *padding, *newline_character;
  2446. PyObject *table = NULL;
  2447. if(Tokenizer_push(self, LC_TABLE_OPEN))
  2448. return -1;
  2449. padding = Tokenizer_parse_as_table_style(self, '\n', 1);
  2450. if (BAD_ROUTE) {
  2451. RESET_ROUTE();
  2452. self->head = reset - 1;
  2453. if (Tokenizer_emit_text(self, "{|"))
  2454. return -1;
  2455. return 0;
  2456. }
  2457. if (!padding)
  2458. return -1;
  2459. style = Tokenizer_pop(self);
  2460. if (!style) {
  2461. Py_DECREF(padding);
  2462. return -1;
  2463. }
  2464. newline_character = PyUnicode_FromString("\n");
  2465. if (!newline_character) {
  2466. Py_DECREF(padding);
  2467. Py_DECREF(style);
  2468. return -1;
  2469. }
  2470. // continue to parse if it is NOT an inline table
  2471. if (PyUnicode_Contains(padding, newline_character)) {
  2472. Py_DECREF(newline_character);
  2473. self->head++;
  2474. table = Tokenizer_parse(self, LC_TABLE_OPEN, 1);
  2475. if (BAD_ROUTE) {
  2476. RESET_ROUTE();
  2477. // offset displacement done by parse()
  2478. self->head = reset - 1;
  2479. if (Tokenizer_emit_text(self, "{|"))
  2480. return -1;
  2481. return 0;
  2482. }
  2483. if (!table) {
  2484. Py_DECREF(padding);
  2485. Py_DECREF(style);
  2486. return -1;
  2487. }
  2488. } else {
  2489. Py_DECREF(newline_character);
  2490. // close tag
  2491. self->head += 2;
  2492. }
  2493. if (Tokenizer_emit_table_tag(self, "{|", "table", style, padding, NULL, table, "|}"))
  2494. return -1;
  2495. // offset displacement done by _parse()
  2496. self->head--;
  2497. return 0;
  2498. }
  2499. /*
  2500. Return the stack in order to handle the table end.
  2501. */
  2502. static PyObject * Tokenizer_handle_table_end(Tokenizer* self)
  2503. {
  2504. self->head += 2;
  2505. return Tokenizer_pop(self);
  2506. }
  2507. /*
  2508. Parse as style until end of the line, then continue.
  2509. */
  2510. static int Tokenizer_handle_table_row(Tokenizer* self)
  2511. {
  2512. if (!Tokenizer_CAN_RECURSE(self)) {
  2513. if (Tokenizer_emit_text(self, "|-"))
  2514. return -1;
  2515. self->head += 1;
  2516. return 0;
  2517. }
  2518. Py_ssize_t reset = self->head;
  2519. self->head += 2;
  2520. PyObject *padding, *style, *row;
  2521. if(Tokenizer_push(self, LC_TABLE_OPEN | LC_TABLE_ROW_OPEN))
  2522. return -1;
  2523. padding = Tokenizer_parse_as_table_style(self, '\n', 0);
  2524. if (BAD_ROUTE) {
  2525. self->head = reset;
  2526. return 0;
  2527. }
  2528. if (!padding)
  2529. return -1;
  2530. style = Tokenizer_pop(self);
  2531. if (!style) {
  2532. Py_DECREF(padding);
  2533. return -1;
  2534. }
  2535. // don't parse the style separator
  2536. self->head++;
  2537. row = Tokenizer_parse(self, LC_TABLE_OPEN | LC_TABLE_ROW_OPEN, 1);
  2538. if (BAD_ROUTE) {
  2539. Py_DECREF(padding);
  2540. Py_DECREF(style);
  2541. self->head = reset;
  2542. return 0;
  2543. }
  2544. if (!row) {
  2545. Py_DECREF(padding);
  2546. Py_DECREF(style);
  2547. Py_DECREF(row);
  2548. return -1;
  2549. }
  2550. if (Tokenizer_emit_table_tag(self, "|-", "tr", style, padding, NULL, row, ""))
  2551. return -1;
  2552. // offset displacement done by _parse()
  2553. self->head--;
  2554. return 0;
  2555. }
  2556. /*
  2557. Return the stack in order to handle the table row end.
  2558. */
  2559. static PyObject* Tokenizer_handle_table_row_end(Tokenizer* self)
  2560. {
  2561. return Tokenizer_pop(self);
  2562. }
  2563. /*
  2564. Parse as normal syntax unless we hit a style marker, then parse style
  2565. as HTML attributes and the remainder as normal syntax.
  2566. */
  2567. static int Tokenizer_handle_table_cell(Tokenizer* self, const char *markup,
  2568. const char *tag, uint64_t line_context)
  2569. {
  2570. if (!Tokenizer_CAN_RECURSE(self)) {
  2571. if (Tokenizer_emit_text(self, markup))
  2572. return -1;
  2573. self->head += strlen(markup) - 1;
  2574. return 0;
  2575. }
  2576. uint64_t old_context = self->topstack->context;
  2577. uint64_t cell_context;
  2578. Py_ssize_t reset = self->head;
  2579. self->head += strlen(markup);
  2580. PyObject *padding, *cell;
  2581. PyObject *style = NULL;
  2582. const char *close_open_markup = NULL;
  2583. cell = Tokenizer_parse(self, LC_TABLE_OPEN | LC_TABLE_CELL_OPEN | LC_TABLE_CELL_STYLE | line_context, 1);
  2584. if (BAD_ROUTE) {
  2585. self->head = reset;
  2586. return 0;
  2587. }
  2588. if (!cell)
  2589. return -1;
  2590. cell_context = self->topstack->context;
  2591. self->topstack->context = old_context;
  2592. if (cell_context & LC_TABLE_CELL_STYLE) {
  2593. Py_DECREF(cell);
  2594. self->head = reset + strlen(markup);
  2595. if(Tokenizer_push(self, LC_TABLE_OPEN | LC_TABLE_CELL_OPEN | line_context))
  2596. return -1;
  2597. padding = Tokenizer_parse_as_table_style(self, '|', 0);
  2598. if (BAD_ROUTE) {
  2599. self->head = reset;
  2600. return 0;
  2601. }
  2602. if (!padding)
  2603. return -1;
  2604. style = Tokenizer_pop(self);
  2605. if (!style) {
  2606. Py_DECREF(padding);
  2607. return -1;
  2608. }
  2609. // Don't parse the style separator
  2610. self->head++;
  2611. cell = Tokenizer_parse(self, LC_TABLE_OPEN | LC_TABLE_CELL_OPEN | line_context, 1);
  2612. if (BAD_ROUTE) {
  2613. self->head = reset;
  2614. return 0;
  2615. }
  2616. if (!cell)
  2617. return -1;
  2618. cell_context = self->topstack->context;
  2619. self->topstack->context = old_context;
  2620. }
  2621. else {
  2622. padding = PyUnicode_FromString("");
  2623. if (!padding) {
  2624. Py_DECREF(cell);
  2625. return -1;
  2626. }
  2627. }
  2628. if (style) {
  2629. close_open_markup = "|";
  2630. }
  2631. if (Tokenizer_emit_table_tag(self, markup, tag, style, padding, close_open_markup, cell, ""))
  2632. return -1;
  2633. // keep header/cell line contexts
  2634. self->topstack->context |= cell_context & (LC_TABLE_TH_LINE | LC_TABLE_TD_LINE);
  2635. // offset displacement done by parse()
  2636. self->head--;
  2637. return 0;
  2638. }
  2639. /*
  2640. Returns the context, stack, and whether to reset the cell for style
  2641. in a tuple.
  2642. */
  2643. static PyObject* Tokenizer_handle_table_cell_end(Tokenizer* self, int reset_for_style)
  2644. {
  2645. if (reset_for_style)
  2646. self->topstack->context |= LC_TABLE_CELL_STYLE;
  2647. else
  2648. self->topstack->context &= ~LC_TABLE_CELL_STYLE;
  2649. return Tokenizer_pop_keeping_context(self);
  2650. }
  2651. /*
  2652. Make sure we are not trying to write an invalid character. Return 0 if
  2653. everything is safe, or -1 if the route must be failed.
  2654. */
  2655. static int Tokenizer_verify_safe(Tokenizer* self, uint64_t context, Py_UNICODE data)
  2656. {
  2657. if (context & LC_FAIL_NEXT)
  2658. return -1;
  2659. if (context & LC_WIKILINK_TITLE) {
  2660. if (data == ']' || data == '{')
  2661. self->topstack->context |= LC_FAIL_NEXT;
  2662. else if (data == '\n' || data == '[' || data == '}')
  2663. return -1;
  2664. return 0;
  2665. }
  2666. if (context & LC_EXT_LINK_TITLE)
  2667. return (data == '\n') ? -1 : 0;
  2668. if (context & LC_TAG_CLOSE)
  2669. return (data == '<') ? -1 : 0;
  2670. if (context & LC_TEMPLATE_NAME) {
  2671. if (data == '{' || data == '}' || data == '[') {
  2672. self->topstack->context |= LC_FAIL_NEXT;
  2673. return 0;
  2674. }
  2675. if (data == ']') {
  2676. return -1;
  2677. }
  2678. if (data == '|')
  2679. return 0;
  2680. if (context & LC_HAS_TEXT) {
  2681. if (context & LC_FAIL_ON_TEXT) {
  2682. if (!Py_UNICODE_ISSPACE(data)) {
  2683. if (data == '<' && Tokenizer_READ(self, 1) == '!') {
  2684. self->topstack->context |= LC_FAIL_NEXT;
  2685. return 0;
  2686. }
  2687. return -1;
  2688. }
  2689. }
  2690. else {
  2691. if (data == '\n')
  2692. self->topstack->context |= LC_FAIL_ON_TEXT;
  2693. }
  2694. }
  2695. else if (!Py_UNICODE_ISSPACE(data))
  2696. self->topstack->context |= LC_HAS_TEXT;
  2697. }
  2698. else {
  2699. if (context & LC_FAIL_ON_EQUALS) {
  2700. if (data == '=') {
  2701. return -1;
  2702. }
  2703. }
  2704. else if (context & LC_FAIL_ON_LBRACE) {
  2705. if (data == '{' || (Tokenizer_READ_BACKWARDS(self, 1) == '{' &&
  2706. Tokenizer_READ_BACKWARDS(self, 2) == '{')) {
  2707. if (context & LC_TEMPLATE)
  2708. self->topstack->context |= LC_FAIL_ON_EQUALS;
  2709. else
  2710. self->topstack->context |= LC_FAIL_NEXT;
  2711. return 0;
  2712. }
  2713. self->topstack->context ^= LC_FAIL_ON_LBRACE;
  2714. }
  2715. else if (context & LC_FAIL_ON_RBRACE) {
  2716. if (data == '}') {
  2717. self->topstack->context |= LC_FAIL_NEXT;
  2718. return 0;
  2719. }
  2720. self->topstack->context ^= LC_FAIL_ON_RBRACE;
  2721. }
  2722. else if (data == '{')
  2723. self->topstack->context |= LC_FAIL_ON_LBRACE;
  2724. else if (data == '}')
  2725. self->topstack->context |= LC_FAIL_ON_RBRACE;
  2726. }
  2727. return 0;
  2728. }
  2729. /*
  2730. Returns whether the current head has leading whitespace.
  2731. TODO: treat comments and templates as whitespace, allow fail on non-newline spaces.
  2732. */
  2733. static int Tokenizer_has_leading_whitespace(Tokenizer* self)
  2734. {
  2735. int offset = 1;
  2736. Py_UNICODE current_character;
  2737. while (1) {
  2738. current_character = Tokenizer_READ_BACKWARDS(self, offset);
  2739. if (!current_character || current_character == '\n')
  2740. return 1;
  2741. else if (!Py_UNICODE_ISSPACE(current_character))
  2742. return 0;
  2743. offset++;
  2744. }
  2745. }
  2746. /*
  2747. Parse the wikicode string, using context for when to stop. If push is true,
  2748. we will push a new context, otherwise we won't and context will be ignored.
  2749. */
  2750. static PyObject* Tokenizer_parse(Tokenizer* self, uint64_t context, int push)
  2751. {
  2752. uint64_t this_context;
  2753. Py_UNICODE this, next, next_next, last;
  2754. PyObject* temp;
  2755. if (push) {
  2756. if (Tokenizer_push(self, context))
  2757. return NULL;
  2758. }
  2759. while (1) {
  2760. this = Tokenizer_READ(self, 0);
  2761. this_context = self->topstack->context;
  2762. if (this_context & AGG_UNSAFE) {
  2763. if (Tokenizer_verify_safe(self, this_context, this) < 0) {
  2764. if (this_context & AGG_DOUBLE) {
  2765. temp = Tokenizer_pop(self);
  2766. Py_XDECREF(temp);
  2767. }
  2768. return Tokenizer_fail_route(self);
  2769. }
  2770. }
  2771. if (!is_marker(this)) {
  2772. if (Tokenizer_emit_char(self, this))
  2773. return NULL;
  2774. self->head++;
  2775. continue;
  2776. }
  2777. if (!this)
  2778. return Tokenizer_handle_end(self, this_context);
  2779. next = Tokenizer_READ(self, 1);
  2780. last = Tokenizer_READ_BACKWARDS(self, 1);
  2781. if (this == next && next == '{') {
  2782. if (Tokenizer_CAN_RECURSE(self)) {
  2783. if (Tokenizer_parse_template_or_argument(self))
  2784. return NULL;
  2785. }
  2786. else if (Tokenizer_emit_char(self, this))
  2787. return NULL;
  2788. }
  2789. else if (this == '|' && this_context & LC_TEMPLATE) {
  2790. if (Tokenizer_handle_template_param(self))
  2791. return NULL;
  2792. }
  2793. else if (this == '=' && this_context & LC_TEMPLATE_PARAM_KEY) {
  2794. if (Tokenizer_handle_template_param_value(self))
  2795. return NULL;
  2796. }
  2797. else if (this == next && next == '}' && this_context & LC_TEMPLATE)
  2798. return Tokenizer_handle_template_end(self);
  2799. else if (this == '|' && this_context & LC_ARGUMENT_NAME) {
  2800. if (Tokenizer_handle_argument_separator(self))
  2801. return NULL;
  2802. }
  2803. else if (this == next && next == '}' && this_context & LC_ARGUMENT) {
  2804. if (Tokenizer_READ(self, 2) == '}') {
  2805. return Tokenizer_handle_argument_end(self);
  2806. }
  2807. if (Tokenizer_emit_char(self, this))
  2808. return NULL;
  2809. }
  2810. else if (this == next && next == '[' && Tokenizer_CAN_RECURSE(self)) {
  2811. if (!(this_context & AGG_NO_WIKILINKS)) {
  2812. if (Tokenizer_parse_wikilink(self))
  2813. return NULL;
  2814. }
  2815. else if (Tokenizer_emit_char(self, this))
  2816. return NULL;
  2817. }
  2818. else if (this == '|' && this_context & LC_WIKILINK_TITLE) {
  2819. if (Tokenizer_handle_wikilink_separator(self))
  2820. return NULL;
  2821. }
  2822. else if (this == next && next == ']' && this_context & LC_WIKILINK)
  2823. return Tokenizer_handle_wikilink_end(self);
  2824. else if (this == '[') {
  2825. if (Tokenizer_parse_external_link(self, 1))
  2826. return NULL;
  2827. }
  2828. else if (this == ':' && !is_marker(last)) {
  2829. if (Tokenizer_parse_external_link(self, 0))
  2830. return NULL;
  2831. }
  2832. else if (this == ']' && this_context & LC_EXT_LINK_TITLE)
  2833. return Tokenizer_pop(self);
  2834. else if (this == '=' && !(self->global & GL_HEADING)) {
  2835. if (!last || last == '\n') {
  2836. if (Tokenizer_parse_heading(self))
  2837. return NULL;
  2838. }
  2839. else if (Tokenizer_emit_char(self, this))
  2840. return NULL;
  2841. }
  2842. else if (this == '=' && this_context & LC_HEADING)
  2843. return (PyObject*) Tokenizer_handle_heading_end(self);
  2844. else if (this == '\n' && this_context & LC_HEADING)
  2845. return Tokenizer_fail_route(self);
  2846. else if (this == '&') {
  2847. if (Tokenizer_parse_entity(self))
  2848. return NULL;
  2849. }
  2850. else if (this == '<' && next == '!') {
  2851. next_next = Tokenizer_READ(self, 2);
  2852. if (next_next == Tokenizer_READ(self, 3) && next_next == '-') {
  2853. if (Tokenizer_parse_comment(self))
  2854. return NULL;
  2855. }
  2856. else if (Tokenizer_emit_char(self, this))
  2857. return NULL;
  2858. }
  2859. else if (this == '<' && next == '/' && Tokenizer_READ(self, 2)) {
  2860. if (this_context & LC_TAG_BODY ?
  2861. Tokenizer_handle_tag_open_close(self) :
  2862. Tokenizer_handle_invalid_tag_start(self))
  2863. return NULL;
  2864. }
  2865. else if (this == '<' && !(this_context & LC_TAG_CLOSE)) {
  2866. if (Tokenizer_CAN_RECURSE(self)) {
  2867. if (Tokenizer_parse_tag(self))
  2868. return NULL;
  2869. }
  2870. else if (Tokenizer_emit_char(self, this))
  2871. return NULL;
  2872. }
  2873. else if (this == '>' && this_context & LC_TAG_CLOSE)
  2874. return Tokenizer_handle_tag_close_close(self);
  2875. else if (this == next && next == '\'' && !self->skip_style_tags) {
  2876. temp = Tokenizer_parse_style(self);
  2877. if (temp != Py_None)
  2878. return temp;
  2879. }
  2880. else if ((!last || last == '\n') && (this == '#' || this == '*' || this == ';' || this == ':')) {
  2881. if (Tokenizer_handle_list(self))
  2882. return NULL;
  2883. }
  2884. else if ((!last || last == '\n') && (this == '-' && this == next &&
  2885. this == Tokenizer_READ(self, 2) &&
  2886. this == Tokenizer_READ(self, 3))) {
  2887. if (Tokenizer_handle_hr(self))
  2888. return NULL;
  2889. }
  2890. else if ((this == '\n' || this == ':') && this_context & LC_DLTERM) {
  2891. if (Tokenizer_handle_dl_term(self))
  2892. return NULL;
  2893. // kill potential table contexts
  2894. if (this == '\n')
  2895. self->topstack->context &= ~LC_TABLE_CELL_LINE_CONTEXTS;
  2896. }
  2897. // Start of table parsing
  2898. else if (this == '{' && next == '|' && Tokenizer_has_leading_whitespace(self)) {
  2899. if (Tokenizer_CAN_RECURSE(self)) {
  2900. if (Tokenizer_handle_table_start(self))
  2901. return NULL;
  2902. }
  2903. else if (Tokenizer_emit_char(self, this) || Tokenizer_emit_char(self, next))
  2904. return NULL;
  2905. else
  2906. self->head++;
  2907. }
  2908. else if (this_context & LC_TABLE_OPEN) {
  2909. if (this == '|' && next == '|' && this_context & LC_TABLE_TD_LINE) {
  2910. if (this_context & LC_TABLE_CELL_OPEN)
  2911. return Tokenizer_handle_table_cell_end(self, 0);
  2912. else if (Tokenizer_handle_table_cell(self, "||", "td", LC_TABLE_TD_LINE))
  2913. return NULL;
  2914. }
  2915. else if (this == '|' && next == '|' && this_context & LC_TABLE_TH_LINE) {
  2916. if (this_context & LC_TABLE_CELL_OPEN)
  2917. return Tokenizer_handle_table_cell_end(self, 0);
  2918. else if (Tokenizer_handle_table_cell(self, "||", "th", LC_TABLE_TH_LINE))
  2919. return NULL;
  2920. }
  2921. else if (this == '!' && next == '!' && this_context & LC_TABLE_TH_LINE) {
  2922. if (this_context & LC_TABLE_CELL_OPEN)
  2923. return Tokenizer_handle_table_cell_end(self, 0);
  2924. else if (Tokenizer_handle_table_cell(self, "!!", "th", LC_TABLE_TH_LINE))
  2925. return NULL;
  2926. }
  2927. else if (this == '|' && this_context & LC_TABLE_CELL_STYLE) {
  2928. return Tokenizer_handle_table_cell_end(self, 1);
  2929. }
  2930. // on newline, clear out cell line contexts
  2931. else if (this == '\n' && this_context & LC_TABLE_CELL_LINE_CONTEXTS) {
  2932. self->topstack->context &= ~LC_TABLE_CELL_LINE_CONTEXTS;
  2933. if (Tokenizer_emit_char(self, this))
  2934. return NULL;
  2935. }
  2936. else if (Tokenizer_has_leading_whitespace(self)) {
  2937. if (this == '|' && next == '}') {
  2938. if (this_context & LC_TABLE_CELL_OPEN)
  2939. return Tokenizer_handle_table_cell_end(self, 0);
  2940. if (this_context & LC_TABLE_ROW_OPEN)
  2941. return Tokenizer_handle_table_row_end(self);
  2942. else
  2943. return Tokenizer_handle_table_end(self);
  2944. }
  2945. else if (this == '|' && next == '-') {
  2946. if (this_context & LC_TABLE_CELL_OPEN)
  2947. return Tokenizer_handle_table_cell_end(self, 0);
  2948. if (this_context & LC_TABLE_ROW_OPEN)
  2949. return Tokenizer_handle_table_row_end(self);
  2950. else if (Tokenizer_handle_table_row(self))
  2951. return NULL;
  2952. }
  2953. else if (this == '|') {
  2954. if (this_context & LC_TABLE_CELL_OPEN)
  2955. return Tokenizer_handle_table_cell_end(self, 0);
  2956. else if (Tokenizer_handle_table_cell(self, "|", "td", LC_TABLE_TD_LINE))
  2957. return NULL;
  2958. }
  2959. else if (this == '!') {
  2960. if (this_context & LC_TABLE_CELL_OPEN)
  2961. return Tokenizer_handle_table_cell_end(self, 0);
  2962. else if (Tokenizer_handle_table_cell(self, "!", "th", LC_TABLE_TH_LINE))
  2963. return NULL;
  2964. }
  2965. else if (Tokenizer_emit_char(self, this))
  2966. return NULL;
  2967. }
  2968. else if (Tokenizer_emit_char(self, this))
  2969. return NULL;
  2970. }
  2971. else if (Tokenizer_emit_char(self, this))
  2972. return NULL;
  2973. self->head++;
  2974. }
  2975. }
  2976. /*
  2977. Build a list of tokens from a string of wikicode and return it.
  2978. */
  2979. static PyObject* Tokenizer_tokenize(Tokenizer* self, PyObject* args)
  2980. {
  2981. PyObject *text, *temp, *tokens;
  2982. uint64_t context = 0;
  2983. int skip_style_tags = 0;
  2984. if (PyArg_ParseTuple(args, "U|ii", &text, &context, &skip_style_tags)) {
  2985. Py_XDECREF(self->text);
  2986. self->text = PySequence_Fast(text, "expected a sequence");
  2987. }
  2988. else {
  2989. const char* encoded;
  2990. Py_ssize_t size;
  2991. /* Failed to parse a Unicode object; try a string instead. */
  2992. PyErr_Clear();
  2993. if (!PyArg_ParseTuple(args, "s#|ii", &encoded, &size, &context,
  2994. &skip_style_tags))
  2995. return NULL;
  2996. temp = PyUnicode_FromStringAndSize(encoded, size);
  2997. if (!text)
  2998. return NULL;
  2999. Py_XDECREF(self->text);
  3000. text = PySequence_Fast(temp, "expected a sequence");
  3001. Py_XDECREF(temp);
  3002. self->text = text;
  3003. }
  3004. self->head = self->global = self->depth = self->cycles = 0;
  3005. self->length = PyList_GET_SIZE(self->text);
  3006. self->skip_style_tags = skip_style_tags;
  3007. tokens = Tokenizer_parse(self, context, 1);
  3008. if (!tokens && !PyErr_Occurred()) {
  3009. if (!ParserError) {
  3010. if (load_exceptions())
  3011. return NULL;
  3012. }
  3013. if (BAD_ROUTE) {
  3014. RESET_ROUTE();
  3015. PyErr_SetString(ParserError, "C tokenizer exited with BAD_ROUTE");
  3016. }
  3017. else
  3018. PyErr_SetString(ParserError, "C tokenizer exited unexpectedly");
  3019. return NULL;
  3020. }
  3021. return tokens;
  3022. }
  3023. static int load_entities(void)
  3024. {
  3025. PyObject *tempmod, *defmap, *deflist;
  3026. unsigned numdefs, i;
  3027. #ifdef IS_PY3K
  3028. PyObject *string;
  3029. #endif
  3030. tempmod = PyImport_ImportModule(ENTITYDEFS_MODULE);
  3031. if (!tempmod)
  3032. return -1;
  3033. defmap = PyObject_GetAttrString(tempmod, "entitydefs");
  3034. if (!defmap)
  3035. return -1;
  3036. Py_DECREF(tempmod);
  3037. deflist = PyDict_Keys(defmap);
  3038. if (!deflist)
  3039. return -1;
  3040. Py_DECREF(defmap);
  3041. numdefs = (unsigned) PyList_GET_SIZE(defmap);
  3042. entitydefs = calloc(numdefs + 1, sizeof(char*));
  3043. if (!entitydefs)
  3044. return -1;
  3045. for (i = 0; i < numdefs; i++) {
  3046. #ifdef IS_PY3K
  3047. string = PyUnicode_AsASCIIString(PyList_GET_ITEM(deflist, i));
  3048. if (!string)
  3049. return -1;
  3050. entitydefs[i] = PyBytes_AsString(string);
  3051. #else
  3052. entitydefs[i] = PyBytes_AsString(PyList_GET_ITEM(deflist, i));
  3053. #endif
  3054. if (!entitydefs[i])
  3055. return -1;
  3056. }
  3057. Py_DECREF(deflist);
  3058. return 0;
  3059. }
  3060. static int load_tokens(void)
  3061. {
  3062. PyObject *tempmod, *tokens,
  3063. *globals = PyEval_GetGlobals(),
  3064. *locals = PyEval_GetLocals(),
  3065. *fromlist = PyList_New(1),
  3066. *modname = IMPORT_NAME_FUNC("tokens");
  3067. char *name = "mwparserfromhell.parser";
  3068. if (!fromlist || !modname)
  3069. return -1;
  3070. PyList_SET_ITEM(fromlist, 0, modname);
  3071. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  3072. Py_DECREF(fromlist);
  3073. if (!tempmod)
  3074. return -1;
  3075. tokens = PyObject_GetAttrString(tempmod, "tokens");
  3076. Py_DECREF(tempmod);
  3077. Text = PyObject_GetAttrString(tokens, "Text");
  3078. TemplateOpen = PyObject_GetAttrString(tokens, "TemplateOpen");
  3079. TemplateParamSeparator = PyObject_GetAttrString(tokens,
  3080. "TemplateParamSeparator");
  3081. TemplateParamEquals = PyObject_GetAttrString(tokens,
  3082. "TemplateParamEquals");
  3083. TemplateClose = PyObject_GetAttrString(tokens, "TemplateClose");
  3084. ArgumentOpen = PyObject_GetAttrString(tokens, "ArgumentOpen");
  3085. ArgumentSeparator = PyObject_GetAttrString(tokens, "ArgumentSeparator");
  3086. ArgumentClose = PyObject_GetAttrString(tokens, "ArgumentClose");
  3087. WikilinkOpen = PyObject_GetAttrString(tokens, "WikilinkOpen");
  3088. WikilinkSeparator = PyObject_GetAttrString(tokens, "WikilinkSeparator");
  3089. WikilinkClose = PyObject_GetAttrString(tokens, "WikilinkClose");
  3090. ExternalLinkOpen = PyObject_GetAttrString(tokens, "ExternalLinkOpen");
  3091. ExternalLinkSeparator = PyObject_GetAttrString(tokens,
  3092. "ExternalLinkSeparator");
  3093. ExternalLinkClose = PyObject_GetAttrString(tokens, "ExternalLinkClose");
  3094. HTMLEntityStart = PyObject_GetAttrString(tokens, "HTMLEntityStart");
  3095. HTMLEntityNumeric = PyObject_GetAttrString(tokens, "HTMLEntityNumeric");
  3096. HTMLEntityHex = PyObject_GetAttrString(tokens, "HTMLEntityHex");
  3097. HTMLEntityEnd = PyObject_GetAttrString(tokens, "HTMLEntityEnd");
  3098. HeadingStart = PyObject_GetAttrString(tokens, "HeadingStart");
  3099. HeadingEnd = PyObject_GetAttrString(tokens, "HeadingEnd");
  3100. CommentStart = PyObject_GetAttrString(tokens, "CommentStart");
  3101. CommentEnd = PyObject_GetAttrString(tokens, "CommentEnd");
  3102. TagOpenOpen = PyObject_GetAttrString(tokens, "TagOpenOpen");
  3103. TagAttrStart = PyObject_GetAttrString(tokens, "TagAttrStart");
  3104. TagAttrEquals = PyObject_GetAttrString(tokens, "TagAttrEquals");
  3105. TagAttrQuote = PyObject_GetAttrString(tokens, "TagAttrQuote");
  3106. TagCloseOpen = PyObject_GetAttrString(tokens, "TagCloseOpen");
  3107. TagCloseSelfclose = PyObject_GetAttrString(tokens, "TagCloseSelfclose");
  3108. TagOpenClose = PyObject_GetAttrString(tokens, "TagOpenClose");
  3109. TagCloseClose = PyObject_GetAttrString(tokens, "TagCloseClose");
  3110. Py_DECREF(tokens);
  3111. return 0;
  3112. }
  3113. static int load_defs(void)
  3114. {
  3115. PyObject *tempmod,
  3116. *globals = PyEval_GetGlobals(),
  3117. *locals = PyEval_GetLocals(),
  3118. *fromlist = PyList_New(1),
  3119. *modname = IMPORT_NAME_FUNC("definitions");
  3120. char *name = "mwparserfromhell";
  3121. if (!fromlist || !modname)
  3122. return -1;
  3123. PyList_SET_ITEM(fromlist, 0, modname);
  3124. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  3125. Py_DECREF(fromlist);
  3126. if (!tempmod)
  3127. return -1;
  3128. definitions = PyObject_GetAttrString(tempmod, "definitions");
  3129. Py_DECREF(tempmod);
  3130. return 0;
  3131. }
  3132. static int load_exceptions(void)
  3133. {
  3134. PyObject *tempmod, *parsermod,
  3135. *globals = PyEval_GetGlobals(),
  3136. *locals = PyEval_GetLocals(),
  3137. *fromlist = PyList_New(1),
  3138. *modname = IMPORT_NAME_FUNC("parser");
  3139. char *name = "mwparserfromhell";
  3140. if (!fromlist || !modname)
  3141. return -1;
  3142. PyList_SET_ITEM(fromlist, 0, modname);
  3143. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  3144. Py_DECREF(fromlist);
  3145. if (!tempmod)
  3146. return -1;
  3147. parsermod = PyObject_GetAttrString(tempmod, "parser");
  3148. Py_DECREF(tempmod);
  3149. ParserError = PyObject_GetAttrString(parsermod, "ParserError");
  3150. Py_DECREF(parsermod);
  3151. return 0;
  3152. }
  3153. PyMODINIT_FUNC INIT_FUNC_NAME(void)
  3154. {
  3155. PyObject *module;
  3156. TokenizerType.tp_new = PyType_GenericNew;
  3157. if (PyType_Ready(&TokenizerType) < 0)
  3158. INIT_ERROR;
  3159. module = CREATE_MODULE;
  3160. if (!module)
  3161. INIT_ERROR;
  3162. Py_INCREF(&TokenizerType);
  3163. PyModule_AddObject(module, "CTokenizer", (PyObject*) &TokenizerType);
  3164. Py_INCREF(Py_True);
  3165. PyDict_SetItemString(TokenizerType.tp_dict, "USES_C", Py_True);
  3166. EMPTY = PyUnicode_FromString("");
  3167. NOARGS = PyTuple_New(0);
  3168. if (!EMPTY || !NOARGS || load_entities() || load_tokens() || load_defs())
  3169. INIT_ERROR;
  3170. #ifdef IS_PY3K
  3171. return module;
  3172. #endif
  3173. }