A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

1488 řádky
40 KiB

  1. /*
  2. Tokenizer for MWParserFromHell
  3. Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  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. double log2(double n)
  22. {
  23. return log(n) / log(2);
  24. }
  25. static PyObject*
  26. Tokenizer_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
  27. {
  28. Tokenizer* self = (Tokenizer*) type->tp_alloc(type, 0);
  29. return (PyObject*) self;
  30. }
  31. static struct Textbuffer*
  32. Textbuffer_new(void)
  33. {
  34. struct Textbuffer* buffer = malloc(sizeof(struct Textbuffer));
  35. if (!buffer) {
  36. PyErr_NoMemory();
  37. return NULL;
  38. }
  39. buffer->size = 0;
  40. buffer->data = malloc(sizeof(Py_UNICODE) * TEXTBUFFER_BLOCKSIZE);
  41. if (!buffer->data) {
  42. free(buffer);
  43. PyErr_NoMemory();
  44. return NULL;
  45. }
  46. buffer->next = NULL;
  47. return buffer;
  48. }
  49. static void
  50. Tokenizer_dealloc(Tokenizer* self)
  51. {
  52. struct Stack *this = self->topstack, *next;
  53. Py_XDECREF(self->text);
  54. while (this) {
  55. Py_DECREF(this->stack);
  56. Textbuffer_dealloc(this->textbuffer);
  57. next = this->next;
  58. free(this);
  59. this = next;
  60. }
  61. self->ob_type->tp_free((PyObject*) self);
  62. }
  63. static void
  64. Textbuffer_dealloc(struct Textbuffer* this)
  65. {
  66. struct Textbuffer* next;
  67. while (this) {
  68. free(this->data);
  69. next = this->next;
  70. free(this);
  71. this = next;
  72. }
  73. }
  74. static int
  75. Tokenizer_init(Tokenizer* self, PyObject* args, PyObject* kwds)
  76. {
  77. static char* kwlist[] = {NULL};
  78. if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
  79. return -1;
  80. self->text = Py_None;
  81. Py_INCREF(Py_None);
  82. self->topstack = NULL;
  83. self->head = 0;
  84. self->length = 0;
  85. self->global = 0;
  86. return 0;
  87. }
  88. /*
  89. Add a new token stack, context, and textbuffer to the list.
  90. */
  91. static int
  92. Tokenizer_push(Tokenizer* self, int context)
  93. {
  94. struct Stack* top = malloc(sizeof(struct Stack));
  95. if (!top) {
  96. PyErr_NoMemory();
  97. return -1;
  98. }
  99. top->stack = PyList_New(0);
  100. top->context = context;
  101. top->textbuffer = Textbuffer_new();
  102. if (!top->textbuffer)
  103. return -1;
  104. top->next = self->topstack;
  105. self->topstack = top;
  106. self->depth++;
  107. self->cycles++;
  108. return 0;
  109. }
  110. /*
  111. Return the contents of the textbuffer as a Python Unicode object.
  112. */
  113. static PyObject*
  114. Textbuffer_render(struct Textbuffer* self)
  115. {
  116. PyObject *result = PyUnicode_FromUnicode(self->data, self->size);
  117. PyObject *left, *concat;
  118. while (self->next) {
  119. self = self->next;
  120. left = PyUnicode_FromUnicode(self->data, self->size);
  121. concat = PyUnicode_Concat(left, result);
  122. Py_DECREF(left);
  123. Py_DECREF(result);
  124. result = concat;
  125. }
  126. return result;
  127. }
  128. /*
  129. Push the textbuffer onto the stack as a Text node and clear it.
  130. */
  131. static int
  132. Tokenizer_push_textbuffer(Tokenizer* self)
  133. {
  134. PyObject *text, *kwargs, *token;
  135. struct Textbuffer* buffer = self->topstack->textbuffer;
  136. if (buffer->size == 0 && !buffer->next)
  137. return 0;
  138. text = Textbuffer_render(buffer);
  139. if (!text)
  140. return -1;
  141. kwargs = PyDict_New();
  142. if (!kwargs) {
  143. Py_DECREF(text);
  144. return -1;
  145. }
  146. PyDict_SetItemString(kwargs, "text", text);
  147. Py_DECREF(text);
  148. token = PyObject_Call(Text, NOARGS, kwargs);
  149. Py_DECREF(kwargs);
  150. if (!token)
  151. return -1;
  152. if (PyList_Append(self->topstack->stack, token)) {
  153. Py_DECREF(token);
  154. return -1;
  155. }
  156. Py_DECREF(token);
  157. Textbuffer_dealloc(buffer);
  158. self->topstack->textbuffer = Textbuffer_new();
  159. if (!self->topstack->textbuffer)
  160. return -1;
  161. return 0;
  162. }
  163. static void
  164. Tokenizer_delete_top_of_stack(Tokenizer* self)
  165. {
  166. struct Stack* top = self->topstack;
  167. Py_DECREF(top->stack);
  168. Textbuffer_dealloc(top->textbuffer);
  169. self->topstack = top->next;
  170. free(top);
  171. self->depth--;
  172. }
  173. /*
  174. Pop the current stack/context/textbuffer, returing the stack.
  175. */
  176. static PyObject*
  177. Tokenizer_pop(Tokenizer* self)
  178. {
  179. PyObject* stack;
  180. if (Tokenizer_push_textbuffer(self))
  181. return NULL;
  182. stack = self->topstack->stack;
  183. Py_INCREF(stack);
  184. Tokenizer_delete_top_of_stack(self);
  185. return stack;
  186. }
  187. /*
  188. Pop the current stack/context/textbuffer, returing the stack. We will also
  189. replace the underlying stack's context with the current stack's.
  190. */
  191. static PyObject*
  192. Tokenizer_pop_keeping_context(Tokenizer* self)
  193. {
  194. PyObject* stack;
  195. int context;
  196. if (Tokenizer_push_textbuffer(self))
  197. return NULL;
  198. stack = self->topstack->stack;
  199. Py_INCREF(stack);
  200. context = self->topstack->context;
  201. Tokenizer_delete_top_of_stack(self);
  202. self->topstack->context = context;
  203. return stack;
  204. }
  205. /*
  206. Fail the current tokenization route. Discards the current
  207. stack/context/textbuffer and raises a BadRoute exception.
  208. */
  209. static void*
  210. Tokenizer_fail_route(Tokenizer* self)
  211. {
  212. PyObject* stack = Tokenizer_pop(self);
  213. Py_XDECREF(stack);
  214. FAIL_ROUTE();
  215. return NULL;
  216. }
  217. /*
  218. Write a token to the end of the current token stack.
  219. */
  220. static int
  221. Tokenizer_write(Tokenizer* self, PyObject* token)
  222. {
  223. if (Tokenizer_push_textbuffer(self))
  224. return -1;
  225. if (PyList_Append(self->topstack->stack, token))
  226. return -1;
  227. return 0;
  228. }
  229. /*
  230. Write a token to the beginning of the current token stack.
  231. */
  232. static int
  233. Tokenizer_write_first(Tokenizer* self, PyObject* token)
  234. {
  235. if (Tokenizer_push_textbuffer(self))
  236. return -1;
  237. if (PyList_Insert(self->topstack->stack, 0, token))
  238. return -1;
  239. return 0;
  240. }
  241. /*
  242. Write text to the current textbuffer.
  243. */
  244. static int
  245. Tokenizer_write_text(Tokenizer* self, Py_UNICODE text)
  246. {
  247. struct Textbuffer* buf = self->topstack->textbuffer;
  248. if (buf->size == TEXTBUFFER_BLOCKSIZE) {
  249. struct Textbuffer* new = Textbuffer_new();
  250. if (!new)
  251. return -1;
  252. new->next = buf;
  253. self->topstack->textbuffer = new;
  254. buf = new;
  255. }
  256. buf->data[buf->size] = text;
  257. buf->size++;
  258. return 0;
  259. }
  260. /*
  261. Write a series of tokens to the current stack at once.
  262. */
  263. static int
  264. Tokenizer_write_all(Tokenizer* self, PyObject* tokenlist)
  265. {
  266. int pushed = 0;
  267. PyObject *stack, *token, *left, *right, *text;
  268. struct Textbuffer* buffer;
  269. Py_ssize_t size;
  270. if (PyList_GET_SIZE(tokenlist) > 0) {
  271. token = PyList_GET_ITEM(tokenlist, 0);
  272. switch (PyObject_IsInstance(token, Text)) {
  273. case 0:
  274. break;
  275. case 1: {
  276. pushed = 1;
  277. buffer = self->topstack->textbuffer;
  278. if (buffer->size == 0 && !buffer->next)
  279. break;
  280. left = Textbuffer_render(buffer);
  281. if (!left)
  282. return -1;
  283. right = PyObject_GetAttrString(token, "text");
  284. if (!right)
  285. return -1;
  286. text = PyUnicode_Concat(left, right);
  287. Py_DECREF(left);
  288. Py_DECREF(right);
  289. if (!text)
  290. return -1;
  291. if (PyObject_SetAttrString(token, "text", text)) {
  292. Py_DECREF(text);
  293. return -1;
  294. }
  295. Py_DECREF(text);
  296. Textbuffer_dealloc(buffer);
  297. self->topstack->textbuffer = Textbuffer_new();
  298. if (!self->topstack->textbuffer)
  299. return -1;
  300. break;
  301. }
  302. case -1:
  303. return -1;
  304. }
  305. }
  306. if (!pushed) {
  307. if (Tokenizer_push_textbuffer(self))
  308. return -1;
  309. }
  310. stack = self->topstack->stack;
  311. size = PyList_GET_SIZE(stack);
  312. if (PyList_SetSlice(stack, size, size, tokenlist))
  313. return -1;
  314. return 0;
  315. }
  316. /*
  317. Pop the current stack, write text, and then write the stack. 'text' is a
  318. NULL-terminated array of chars.
  319. */
  320. static int
  321. Tokenizer_write_text_then_stack(Tokenizer* self, const char* text)
  322. {
  323. PyObject* stack = Tokenizer_pop(self);
  324. int i = 0;
  325. while (1) {
  326. if (!text[i])
  327. break;
  328. if (Tokenizer_write_text(self, (Py_UNICODE) text[i])) {
  329. Py_XDECREF(stack);
  330. return -1;
  331. }
  332. i++;
  333. }
  334. if (stack) {
  335. if (PyList_GET_SIZE(stack) > 0) {
  336. if (Tokenizer_write_all(self, stack)) {
  337. Py_DECREF(stack);
  338. return -1;
  339. }
  340. }
  341. Py_DECREF(stack);
  342. }
  343. self->head--;
  344. return 0;
  345. }
  346. /*
  347. Read the value at a relative point in the wikicode, forwards.
  348. */
  349. static PyObject*
  350. Tokenizer_read(Tokenizer* self, Py_ssize_t delta)
  351. {
  352. Py_ssize_t index = self->head + delta;
  353. if (index >= self->length)
  354. return EMPTY;
  355. return PyList_GET_ITEM(self->text, index);
  356. }
  357. /*
  358. Read the value at a relative point in the wikicode, backwards.
  359. */
  360. static PyObject*
  361. Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta)
  362. {
  363. Py_ssize_t index;
  364. if (delta > self->head)
  365. return EMPTY;
  366. index = self->head - delta;
  367. return PyList_GET_ITEM(self->text, index);
  368. }
  369. /*
  370. Parse a template or argument at the head of the wikicode string.
  371. */
  372. static int
  373. Tokenizer_parse_template_or_argument(Tokenizer* self)
  374. {
  375. unsigned int braces = 2, i;
  376. PyObject *tokenlist;
  377. self->head += 2;
  378. while (Tokenizer_READ(self, 0) == *"{" && braces < MAX_BRACES) {
  379. self->head++;
  380. braces++;
  381. }
  382. if (Tokenizer_push(self, 0))
  383. return -1;
  384. while (braces) {
  385. if (braces == 1) {
  386. if (Tokenizer_write_text_then_stack(self, "{"))
  387. return -1;
  388. return 0;
  389. }
  390. if (braces == 2) {
  391. if (Tokenizer_parse_template(self))
  392. return -1;
  393. if (BAD_ROUTE) {
  394. RESET_ROUTE();
  395. if (Tokenizer_write_text_then_stack(self, "{{"))
  396. return -1;
  397. return 0;
  398. }
  399. break;
  400. }
  401. if (Tokenizer_parse_argument(self))
  402. return -1;
  403. if (BAD_ROUTE) {
  404. RESET_ROUTE();
  405. if (Tokenizer_parse_template(self))
  406. return -1;
  407. if (BAD_ROUTE) {
  408. char text[MAX_BRACES];
  409. RESET_ROUTE();
  410. for (i = 0; i < braces; i++) text[i] = *"{";
  411. text[braces] = *"";
  412. if (Tokenizer_write_text_then_stack(self, text)) {
  413. Py_XDECREF(text);
  414. return -1;
  415. }
  416. Py_XDECREF(text);
  417. return 0;
  418. }
  419. else
  420. braces -= 2;
  421. }
  422. else
  423. braces -= 3;
  424. if (braces)
  425. self->head++;
  426. }
  427. tokenlist = Tokenizer_pop(self);
  428. if (!tokenlist)
  429. return -1;
  430. if (Tokenizer_write_all(self, tokenlist)) {
  431. Py_DECREF(tokenlist);
  432. return -1;
  433. }
  434. Py_DECREF(tokenlist);
  435. return 0;
  436. }
  437. /*
  438. Parse a template at the head of the wikicode string.
  439. */
  440. static int
  441. Tokenizer_parse_template(Tokenizer* self)
  442. {
  443. PyObject *template, *token;
  444. Py_ssize_t reset = self->head;
  445. template = Tokenizer_parse(self, LC_TEMPLATE_NAME);
  446. if (BAD_ROUTE) {
  447. self->head = reset;
  448. return 0;
  449. }
  450. if (!template)
  451. return -1;
  452. token = PyObject_CallObject(TemplateOpen, NULL);
  453. if (!token) {
  454. Py_DECREF(template);
  455. return -1;
  456. }
  457. if (Tokenizer_write_first(self, token)) {
  458. Py_DECREF(token);
  459. Py_DECREF(template);
  460. return -1;
  461. }
  462. Py_DECREF(token);
  463. if (Tokenizer_write_all(self, template)) {
  464. Py_DECREF(template);
  465. return -1;
  466. }
  467. Py_DECREF(template);
  468. token = PyObject_CallObject(TemplateClose, NULL);
  469. if (!token)
  470. return -1;
  471. if (Tokenizer_write(self, token)) {
  472. Py_DECREF(token);
  473. return -1;
  474. }
  475. Py_DECREF(token);
  476. return 0;
  477. }
  478. /*
  479. Parse an argument at the head of the wikicode string.
  480. */
  481. static int
  482. Tokenizer_parse_argument(Tokenizer* self)
  483. {
  484. PyObject *argument, *token;
  485. Py_ssize_t reset = self->head;
  486. argument = Tokenizer_parse(self, LC_ARGUMENT_NAME);
  487. if (BAD_ROUTE) {
  488. self->head = reset;
  489. return 0;
  490. }
  491. if (!argument)
  492. return -1;
  493. token = PyObject_CallObject(ArgumentOpen, NULL);
  494. if (!token) {
  495. Py_DECREF(argument);
  496. return -1;
  497. }
  498. if (Tokenizer_write_first(self, token)) {
  499. Py_DECREF(token);
  500. Py_DECREF(argument);
  501. return -1;
  502. }
  503. Py_DECREF(token);
  504. if (Tokenizer_write_all(self, argument)) {
  505. Py_DECREF(argument);
  506. return -1;
  507. }
  508. Py_DECREF(argument);
  509. token = PyObject_CallObject(ArgumentClose, NULL);
  510. if (!token)
  511. return -1;
  512. if (Tokenizer_write(self, token)) {
  513. Py_DECREF(token);
  514. return -1;
  515. }
  516. Py_DECREF(token);
  517. return 0;
  518. }
  519. /*
  520. Handle a template parameter at the head of the string.
  521. */
  522. static int
  523. Tokenizer_handle_template_param(Tokenizer* self)
  524. {
  525. PyObject *stack, *token;
  526. if (self->topstack->context & LC_TEMPLATE_NAME)
  527. self->topstack->context ^= LC_TEMPLATE_NAME;
  528. else if (self->topstack->context & LC_TEMPLATE_PARAM_VALUE)
  529. self->topstack->context ^= LC_TEMPLATE_PARAM_VALUE;
  530. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  531. stack = Tokenizer_pop_keeping_context(self);
  532. if (!stack)
  533. return -1;
  534. if (Tokenizer_write_all(self, stack)) {
  535. Py_DECREF(stack);
  536. return -1;
  537. }
  538. Py_DECREF(stack);
  539. }
  540. else
  541. self->topstack->context |= LC_TEMPLATE_PARAM_KEY;
  542. token = PyObject_CallObject(TemplateParamSeparator, NULL);
  543. if (!token)
  544. return -1;
  545. if (Tokenizer_write(self, token)) {
  546. Py_DECREF(token);
  547. return -1;
  548. }
  549. Py_DECREF(token);
  550. if (Tokenizer_push(self, self->topstack->context))
  551. return -1;
  552. return 0;
  553. }
  554. /*
  555. Handle a template parameter's value at the head of the string.
  556. */
  557. static int
  558. Tokenizer_handle_template_param_value(Tokenizer* self)
  559. {
  560. PyObject *stack, *token;
  561. stack = Tokenizer_pop_keeping_context(self);
  562. if (!stack)
  563. return -1;
  564. if (Tokenizer_write_all(self, stack)) {
  565. Py_DECREF(stack);
  566. return -1;
  567. }
  568. Py_DECREF(stack);
  569. self->topstack->context ^= LC_TEMPLATE_PARAM_KEY;
  570. self->topstack->context |= LC_TEMPLATE_PARAM_VALUE;
  571. token = PyObject_CallObject(TemplateParamEquals, NULL);
  572. if (!token)
  573. return -1;
  574. if (Tokenizer_write(self, token)) {
  575. Py_DECREF(token);
  576. return -1;
  577. }
  578. Py_DECREF(token);
  579. return 0;
  580. }
  581. /*
  582. Handle the end of a template at the head of the string.
  583. */
  584. static PyObject*
  585. Tokenizer_handle_template_end(Tokenizer* self)
  586. {
  587. PyObject* stack;
  588. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  589. stack = Tokenizer_pop_keeping_context(self);
  590. if (!stack)
  591. return NULL;
  592. if (Tokenizer_write_all(self, stack)) {
  593. Py_DECREF(stack);
  594. return NULL;
  595. }
  596. Py_DECREF(stack);
  597. }
  598. self->head++;
  599. stack = Tokenizer_pop(self);
  600. return stack;
  601. }
  602. /*
  603. Handle the separator between an argument's name and default.
  604. */
  605. static int
  606. Tokenizer_handle_argument_separator(Tokenizer* self)
  607. {
  608. PyObject* token;
  609. self->topstack->context ^= LC_ARGUMENT_NAME;
  610. self->topstack->context |= LC_ARGUMENT_DEFAULT;
  611. token = PyObject_CallObject(ArgumentSeparator, NULL);
  612. if (!token)
  613. return -1;
  614. if (Tokenizer_write(self, token)) {
  615. Py_DECREF(token);
  616. return -1;
  617. }
  618. Py_DECREF(token);
  619. return 0;
  620. }
  621. /*
  622. Handle the end of an argument at the head of the string.
  623. */
  624. static PyObject*
  625. Tokenizer_handle_argument_end(Tokenizer* self)
  626. {
  627. PyObject* stack = Tokenizer_pop(self);
  628. self->head += 2;
  629. return stack;
  630. }
  631. /*
  632. Parse an internal wikilink at the head of the wikicode string.
  633. */
  634. static int
  635. Tokenizer_parse_wikilink(Tokenizer* self)
  636. {
  637. Py_ssize_t reset;
  638. PyObject *wikilink, *token;
  639. int i;
  640. self->head += 2;
  641. reset = self->head - 1;
  642. wikilink = Tokenizer_parse(self, LC_WIKILINK_TITLE);
  643. if (BAD_ROUTE) {
  644. RESET_ROUTE();
  645. self->head = reset;
  646. for (i = 0; i < 2; i++) {
  647. if (Tokenizer_write_text(self, *"["))
  648. return -1;
  649. }
  650. return 0;
  651. }
  652. if (!wikilink)
  653. return -1;
  654. token = PyObject_CallObject(WikilinkOpen, NULL);
  655. if (!token) {
  656. Py_DECREF(wikilink);
  657. return -1;
  658. }
  659. if (Tokenizer_write(self, token)) {
  660. Py_DECREF(token);
  661. Py_DECREF(wikilink);
  662. return -1;
  663. }
  664. Py_DECREF(token);
  665. if (Tokenizer_write_all(self, wikilink)) {
  666. Py_DECREF(wikilink);
  667. return -1;
  668. }
  669. Py_DECREF(wikilink);
  670. token = PyObject_CallObject(WikilinkClose, NULL);
  671. if (!token)
  672. return -1;
  673. if (Tokenizer_write(self, token)) {
  674. Py_DECREF(token);
  675. return -1;
  676. }
  677. Py_DECREF(token);
  678. return 0;
  679. }
  680. /*
  681. Handle the separator between a wikilink's title and its text.
  682. */
  683. static int
  684. Tokenizer_handle_wikilink_separator(Tokenizer* self)
  685. {
  686. PyObject* token;
  687. self->topstack->context ^= LC_WIKILINK_TITLE;
  688. self->topstack->context |= LC_WIKILINK_TEXT;
  689. token = PyObject_CallObject(WikilinkSeparator, NULL);
  690. if (!token)
  691. return -1;
  692. if (Tokenizer_write(self, token)) {
  693. Py_DECREF(token);
  694. return -1;
  695. }
  696. Py_DECREF(token);
  697. return 0;
  698. }
  699. /*
  700. Handle the end of a wikilink at the head of the string.
  701. */
  702. static PyObject*
  703. Tokenizer_handle_wikilink_end(Tokenizer* self)
  704. {
  705. PyObject* stack = Tokenizer_pop(self);
  706. self->head += 1;
  707. return stack;
  708. }
  709. /*
  710. Parse a section heading at the head of the wikicode string.
  711. */
  712. static int
  713. Tokenizer_parse_heading(Tokenizer* self)
  714. {
  715. Py_ssize_t reset = self->head;
  716. int best = 1, i, context, diff;
  717. HeadingData *heading;
  718. PyObject *level, *kwargs, *token;
  719. self->global |= GL_HEADING;
  720. self->head += 1;
  721. while (Tokenizer_READ(self, 0) == *"=") {
  722. best++;
  723. self->head++;
  724. }
  725. context = LC_HEADING_LEVEL_1 << (best > 5 ? 5 : best - 1);
  726. heading = (HeadingData*) Tokenizer_parse(self, context);
  727. if (BAD_ROUTE) {
  728. RESET_ROUTE();
  729. self->head = reset + best - 1;
  730. for (i = 0; i < best; i++) {
  731. if (Tokenizer_write_text(self, *"="))
  732. return -1;
  733. }
  734. self->global ^= GL_HEADING;
  735. return 0;
  736. }
  737. level = PyInt_FromSsize_t(heading->level);
  738. if (!level) {
  739. Py_DECREF(heading->title);
  740. free(heading);
  741. return -1;
  742. }
  743. kwargs = PyDict_New();
  744. if (!kwargs) {
  745. Py_DECREF(level);
  746. Py_DECREF(heading->title);
  747. free(heading);
  748. return -1;
  749. }
  750. PyDict_SetItemString(kwargs, "level", level);
  751. Py_DECREF(level);
  752. token = PyObject_Call(HeadingStart, NOARGS, kwargs);
  753. Py_DECREF(kwargs);
  754. if (!token) {
  755. Py_DECREF(heading->title);
  756. free(heading);
  757. return -1;
  758. }
  759. if (Tokenizer_write(self, token)) {
  760. Py_DECREF(token);
  761. Py_DECREF(heading->title);
  762. free(heading);
  763. return -1;
  764. }
  765. Py_DECREF(token);
  766. if (heading->level < best) {
  767. diff = best - heading->level;
  768. for (i = 0; i < diff; i++) {
  769. if (Tokenizer_write_text(self, *"=")) {
  770. Py_DECREF(heading->title);
  771. free(heading);
  772. return -1;
  773. }
  774. }
  775. }
  776. if (Tokenizer_write_all(self, heading->title)) {
  777. Py_DECREF(heading->title);
  778. free(heading);
  779. return -1;
  780. }
  781. Py_DECREF(heading->title);
  782. free(heading);
  783. token = PyObject_CallObject(HeadingEnd, NULL);
  784. if (!token)
  785. return -1;
  786. if (Tokenizer_write(self, token)) {
  787. Py_DECREF(token);
  788. return -1;
  789. }
  790. Py_DECREF(token);
  791. self->global ^= GL_HEADING;
  792. return 0;
  793. }
  794. /*
  795. Handle the end of a section heading at the head of the string.
  796. */
  797. static HeadingData*
  798. Tokenizer_handle_heading_end(Tokenizer* self)
  799. {
  800. Py_ssize_t reset = self->head, best;
  801. int i, current, level, diff;
  802. HeadingData *after, *heading;
  803. PyObject *stack;
  804. self->head += 1;
  805. best = 1;
  806. while (Tokenizer_READ(self, 0) == *"=") {
  807. best++;
  808. self->head++;
  809. }
  810. current = log2(self->topstack->context / LC_HEADING_LEVEL_1) + 1;
  811. level = current > best ? (best > 6 ? 6 : best) :
  812. (current > 6 ? 6 : current);
  813. after = (HeadingData*) Tokenizer_parse(self, self->topstack->context);
  814. if (BAD_ROUTE) {
  815. RESET_ROUTE();
  816. if (level < best) {
  817. diff = best - level;
  818. for (i = 0; i < diff; i++) {
  819. if (Tokenizer_write_text(self, *"="))
  820. return NULL;
  821. }
  822. }
  823. self->head = reset + best - 1;
  824. }
  825. else {
  826. for (i = 0; i < best; i++) {
  827. if (Tokenizer_write_text(self, *"=")) {
  828. Py_DECREF(after->title);
  829. free(after);
  830. return NULL;
  831. }
  832. }
  833. if (Tokenizer_write_all(self, after->title)) {
  834. Py_DECREF(after->title);
  835. free(after);
  836. return NULL;
  837. }
  838. Py_DECREF(after->title);
  839. level = after->level;
  840. free(after);
  841. }
  842. stack = Tokenizer_pop(self);
  843. if (!stack)
  844. return NULL;
  845. heading = malloc(sizeof(HeadingData));
  846. if (!heading) {
  847. PyErr_NoMemory();
  848. return NULL;
  849. }
  850. heading->title = stack;
  851. heading->level = level;
  852. return heading;
  853. }
  854. /*
  855. Actually parse an HTML entity and ensure that it is valid.
  856. */
  857. static int
  858. Tokenizer_really_parse_entity(Tokenizer* self)
  859. {
  860. PyObject *token, *kwargs, *textobj;
  861. Py_UNICODE this;
  862. int numeric, hexadecimal, i, j, test;
  863. char *valid, *text, *def;
  864. #define FAIL_ROUTE_AND_EXIT() { \
  865. Tokenizer_fail_route(self); \
  866. free(text); \
  867. return 0; \
  868. }
  869. token = PyObject_CallObject(HTMLEntityStart, NULL);
  870. if (!token)
  871. return -1;
  872. if (Tokenizer_write(self, token)) {
  873. Py_DECREF(token);
  874. return -1;
  875. }
  876. Py_DECREF(token);
  877. self->head++;
  878. this = Tokenizer_READ(self, 0);
  879. if (this == *"") {
  880. Tokenizer_fail_route(self);
  881. return 0;
  882. }
  883. if (this == *"#") {
  884. numeric = 1;
  885. token = PyObject_CallObject(HTMLEntityNumeric, NULL);
  886. if (!token)
  887. return -1;
  888. if (Tokenizer_write(self, token)) {
  889. Py_DECREF(token);
  890. return -1;
  891. }
  892. Py_DECREF(token);
  893. self->head++;
  894. this = Tokenizer_READ(self, 0);
  895. if (this == *"") {
  896. Tokenizer_fail_route(self);
  897. return 0;
  898. }
  899. if (this == *"x" || this == *"X") {
  900. hexadecimal = 1;
  901. kwargs = PyDict_New();
  902. if (!kwargs)
  903. return -1;
  904. PyDict_SetItemString(kwargs, "char", Tokenizer_read(self, 0));
  905. token = PyObject_Call(HTMLEntityHex, NOARGS, kwargs);
  906. Py_DECREF(kwargs);
  907. if (!token)
  908. return -1;
  909. if (Tokenizer_write(self, token)) {
  910. Py_DECREF(token);
  911. return -1;
  912. }
  913. Py_DECREF(token);
  914. self->head++;
  915. }
  916. else
  917. hexadecimal = 0;
  918. }
  919. else
  920. numeric = hexadecimal = 0;
  921. if (hexadecimal)
  922. valid = HEXDIGITS;
  923. else if (numeric)
  924. valid = DIGITS;
  925. else
  926. valid = ALPHANUM;
  927. text = calloc(MAX_ENTITY_SIZE, sizeof(char));
  928. if (!text) {
  929. PyErr_NoMemory();
  930. return -1;
  931. }
  932. i = 0;
  933. while (1) {
  934. this = Tokenizer_READ(self, 0);
  935. if (this == *";") {
  936. if (i == 0)
  937. FAIL_ROUTE_AND_EXIT()
  938. break;
  939. }
  940. if (i == 0 && this == *"0") {
  941. self->head++;
  942. continue;
  943. }
  944. if (i >= 8)
  945. FAIL_ROUTE_AND_EXIT()
  946. for (j = 0; j < NUM_MARKERS; j++) {
  947. if (this == *MARKERS[j])
  948. FAIL_ROUTE_AND_EXIT()
  949. }
  950. j = 0;
  951. while (1) {
  952. if (!valid[j])
  953. FAIL_ROUTE_AND_EXIT()
  954. if (this == valid[j])
  955. break;
  956. j++;
  957. }
  958. text[i] = this;
  959. self->head++;
  960. i++;
  961. }
  962. if (numeric) {
  963. sscanf(text, (hexadecimal ? "%x" : "%d"), &test);
  964. if (test < 1 || test > 0x10FFFF)
  965. FAIL_ROUTE_AND_EXIT()
  966. }
  967. else {
  968. i = 0;
  969. while (1) {
  970. def = entitydefs[i];
  971. if (!def) // We've reached the end of the defs without finding it
  972. FAIL_ROUTE_AND_EXIT()
  973. if (strcmp(text, def) == 0)
  974. break;
  975. i++;
  976. }
  977. }
  978. textobj = PyUnicode_FromString(text);
  979. if (!textobj) {
  980. free(text);
  981. return -1;
  982. }
  983. free(text);
  984. kwargs = PyDict_New();
  985. if (!kwargs) {
  986. Py_DECREF(textobj);
  987. return -1;
  988. }
  989. PyDict_SetItemString(kwargs, "text", textobj);
  990. Py_DECREF(textobj);
  991. token = PyObject_Call(Text, NOARGS, kwargs);
  992. Py_DECREF(kwargs);
  993. if (!token)
  994. return -1;
  995. if (Tokenizer_write(self, token)) {
  996. Py_DECREF(token);
  997. return -1;
  998. }
  999. Py_DECREF(token);
  1000. token = PyObject_CallObject(HTMLEntityEnd, NULL);
  1001. if (!token)
  1002. return -1;
  1003. if (Tokenizer_write(self, token)) {
  1004. Py_DECREF(token);
  1005. return -1;
  1006. }
  1007. Py_DECREF(token);
  1008. return 0;
  1009. }
  1010. /*
  1011. Parse an HTML entity at the head of the wikicode string.
  1012. */
  1013. static int
  1014. Tokenizer_parse_entity(Tokenizer* self)
  1015. {
  1016. Py_ssize_t reset = self->head;
  1017. PyObject *tokenlist;
  1018. if (Tokenizer_push(self, 0))
  1019. return -1;
  1020. if (Tokenizer_really_parse_entity(self))
  1021. return -1;
  1022. if (BAD_ROUTE) {
  1023. RESET_ROUTE();
  1024. self->head = reset;
  1025. if (Tokenizer_write_text(self, *"&"))
  1026. return -1;
  1027. return 0;
  1028. }
  1029. tokenlist = Tokenizer_pop(self);
  1030. if (!tokenlist)
  1031. return -1;
  1032. if (Tokenizer_write_all(self, tokenlist)) {
  1033. Py_DECREF(tokenlist);
  1034. return -1;
  1035. }
  1036. Py_DECREF(tokenlist);
  1037. return 0;
  1038. }
  1039. /*
  1040. Parse an HTML comment at the head of the wikicode string.
  1041. */
  1042. static int
  1043. Tokenizer_parse_comment(Tokenizer* self)
  1044. {
  1045. Py_ssize_t reset = self->head + 3;
  1046. PyObject *token, *comment;
  1047. int i;
  1048. self->head += 4;
  1049. comment = Tokenizer_parse(self, LC_COMMENT);
  1050. if (BAD_ROUTE) {
  1051. const char* text = "<!--";
  1052. RESET_ROUTE();
  1053. self->head = reset;
  1054. i = 0;
  1055. while (1) {
  1056. if (!text[i])
  1057. return 0;
  1058. if (Tokenizer_write_text(self, (Py_UNICODE) text[i])) {
  1059. Py_XDECREF(text);
  1060. return -1;
  1061. }
  1062. i++;
  1063. }
  1064. return 0;
  1065. }
  1066. if (!comment)
  1067. return -1;
  1068. token = PyObject_CallObject(CommentStart, NULL);
  1069. if (!token) {
  1070. Py_DECREF(comment);
  1071. return -1;
  1072. }
  1073. if (Tokenizer_write(self, token)) {
  1074. Py_DECREF(token);
  1075. Py_DECREF(comment);
  1076. return -1;
  1077. }
  1078. Py_DECREF(token);
  1079. if (Tokenizer_write_all(self, comment)) {
  1080. Py_DECREF(comment);
  1081. return -1;
  1082. }
  1083. Py_DECREF(comment);
  1084. token = PyObject_CallObject(CommentEnd, NULL);
  1085. if (!token)
  1086. return -1;
  1087. if (Tokenizer_write(self, token)) {
  1088. Py_DECREF(token);
  1089. return -1;
  1090. }
  1091. Py_DECREF(token);
  1092. self->head += 2;
  1093. return 0;
  1094. }
  1095. /*
  1096. Make sure we are not trying to write an invalid character. Return 0 if
  1097. everything is safe, or -1 if the route must be failed.
  1098. */
  1099. static int
  1100. Tokenizer_verify_safe(Tokenizer* self, int context, Py_UNICODE data)
  1101. {
  1102. if (context & LC_FAIL_NEXT) {
  1103. return -1;
  1104. }
  1105. if (context & LC_WIKILINK_TITLE) {
  1106. if (data == *"]" || data == *"{")
  1107. self->topstack->context |= LC_FAIL_NEXT;
  1108. else if (data == *"\n" || data == *"[" || data == *"}")
  1109. return -1;
  1110. return 0;
  1111. }
  1112. if (context & LC_TEMPLATE_NAME) {
  1113. if (data == *"{" || data == *"}" || data == *"[") {
  1114. self->topstack->context |= LC_FAIL_NEXT;
  1115. return 0;
  1116. }
  1117. if (data == *"]") {
  1118. return -1;
  1119. }
  1120. if (data == *"|")
  1121. return 0;
  1122. if (context & LC_HAS_TEXT) {
  1123. if (context & LC_FAIL_ON_TEXT) {
  1124. if (!Py_UNICODE_ISSPACE(data))
  1125. return -1;
  1126. }
  1127. else {
  1128. if (data == *"\n")
  1129. self->topstack->context |= LC_FAIL_ON_TEXT;
  1130. }
  1131. }
  1132. else if (!Py_UNICODE_ISSPACE(data))
  1133. self->topstack->context |= LC_HAS_TEXT;
  1134. }
  1135. else {
  1136. if (context & LC_FAIL_ON_EQUALS) {
  1137. if (data == *"=") {
  1138. return -1;
  1139. }
  1140. }
  1141. else if (context & LC_FAIL_ON_LBRACE) {
  1142. if (data == *"{" || (Tokenizer_READ(self, -1) == *"{" &&
  1143. Tokenizer_READ(self, -2) == *"{")) {
  1144. if (context & LC_TEMPLATE)
  1145. self->topstack->context |= LC_FAIL_ON_EQUALS;
  1146. else
  1147. self->topstack->context |= LC_FAIL_NEXT;
  1148. return 0;
  1149. }
  1150. self->topstack->context ^= LC_FAIL_ON_LBRACE;
  1151. }
  1152. else if (context & LC_FAIL_ON_RBRACE) {
  1153. if (data == *"}") {
  1154. if (context & LC_TEMPLATE)
  1155. self->topstack->context |= LC_FAIL_ON_EQUALS;
  1156. else
  1157. self->topstack->context |= LC_FAIL_NEXT;
  1158. return 0;
  1159. }
  1160. self->topstack->context ^= LC_FAIL_ON_RBRACE;
  1161. }
  1162. else if (data == *"{")
  1163. self->topstack->context |= LC_FAIL_ON_LBRACE;
  1164. else if (data == *"}")
  1165. self->topstack->context |= LC_FAIL_ON_RBRACE;
  1166. }
  1167. return 0;
  1168. }
  1169. /*
  1170. Parse the wikicode string, using context for when to stop.
  1171. */
  1172. static PyObject*
  1173. Tokenizer_parse(Tokenizer* self, int context)
  1174. {
  1175. static int fail_contexts = (LC_TEMPLATE | LC_ARGUMENT | LC_WIKILINK |
  1176. LC_HEADING | LC_COMMENT);
  1177. static int unsafe_contexts = (LC_TEMPLATE_NAME | LC_WIKILINK_TITLE |
  1178. LC_TEMPLATE_PARAM_KEY | LC_ARGUMENT_NAME);
  1179. int this_context, is_marker, i;
  1180. Py_UNICODE this, next, next_next, last;
  1181. PyObject *trash;
  1182. if (Tokenizer_push(self, context))
  1183. return NULL;
  1184. while (1) {
  1185. this = Tokenizer_READ(self, 0);
  1186. this_context = self->topstack->context;
  1187. if (this_context & unsafe_contexts) {
  1188. if (Tokenizer_verify_safe(self, this_context, this) < 0) {
  1189. if (this_context & LC_TEMPLATE_PARAM_KEY) {
  1190. trash = Tokenizer_pop(self);
  1191. Py_XDECREF(trash);
  1192. }
  1193. Tokenizer_fail_route(self);
  1194. return NULL;
  1195. }
  1196. }
  1197. is_marker = 0;
  1198. for (i = 0; i < NUM_MARKERS; i++) {
  1199. if (*MARKERS[i] == this) {
  1200. is_marker = 1;
  1201. break;
  1202. }
  1203. }
  1204. if (!is_marker) {
  1205. Tokenizer_write_text(self, this);
  1206. self->head++;
  1207. continue;
  1208. }
  1209. if (this == *"") {
  1210. if (this_context & LC_TEMPLATE_PARAM_KEY) {
  1211. trash = Tokenizer_pop(self);
  1212. Py_XDECREF(trash);
  1213. }
  1214. if (this_context & fail_contexts)
  1215. return Tokenizer_fail_route(self);
  1216. return Tokenizer_pop(self);
  1217. }
  1218. next = Tokenizer_READ(self, 1);
  1219. if (this_context & LC_COMMENT) {
  1220. if (this == next && next == *"-") {
  1221. if (Tokenizer_READ(self, 2) == *">")
  1222. return Tokenizer_pop(self);
  1223. }
  1224. Tokenizer_write_text(self, this);
  1225. }
  1226. else if (this == next && next == *"{") {
  1227. if (Tokenizer_CAN_RECURSE(self)) {
  1228. if (Tokenizer_parse_template_or_argument(self))
  1229. return NULL;
  1230. if (self->topstack->context & LC_FAIL_NEXT)
  1231. self->topstack->context ^= LC_FAIL_NEXT;
  1232. }
  1233. else
  1234. Tokenizer_write_text(self, this);
  1235. }
  1236. else if (this == *"|" && this_context & LC_TEMPLATE) {
  1237. if (Tokenizer_handle_template_param(self))
  1238. return NULL;
  1239. }
  1240. else if (this == *"=" && this_context & LC_TEMPLATE_PARAM_KEY) {
  1241. if (Tokenizer_handle_template_param_value(self))
  1242. return NULL;
  1243. }
  1244. else if (this == next && next == *"}" && this_context & LC_TEMPLATE)
  1245. return Tokenizer_handle_template_end(self);
  1246. else if (this == *"|" && this_context & LC_ARGUMENT_NAME) {
  1247. if (Tokenizer_handle_argument_separator(self))
  1248. return NULL;
  1249. }
  1250. else if (this == next && next == *"}" && this_context & LC_ARGUMENT) {
  1251. if (Tokenizer_READ(self, 2) == *"}") {
  1252. return Tokenizer_handle_argument_end(self);
  1253. }
  1254. Tokenizer_write_text(self, this);
  1255. }
  1256. else if (this == next && next == *"[") {
  1257. if (!(this_context & LC_WIKILINK_TITLE) &&
  1258. Tokenizer_CAN_RECURSE(self)) {
  1259. if (Tokenizer_parse_wikilink(self))
  1260. return NULL;
  1261. if (self->topstack->context & LC_FAIL_NEXT)
  1262. self->topstack->context ^= LC_FAIL_NEXT;
  1263. }
  1264. else
  1265. Tokenizer_write_text(self, this);
  1266. }
  1267. else if (this == *"|" && this_context & LC_WIKILINK_TITLE) {
  1268. if (Tokenizer_handle_wikilink_separator(self))
  1269. return NULL;
  1270. }
  1271. else if (this == next && next == *"]" && this_context & LC_WIKILINK)
  1272. return Tokenizer_handle_wikilink_end(self);
  1273. else if (this == *"=" && !(self->global & GL_HEADING)) {
  1274. last = *PyUnicode_AS_UNICODE(Tokenizer_read_backwards(self, 1));
  1275. if (last == *"\n" || last == *"") {
  1276. if (Tokenizer_parse_heading(self))
  1277. return NULL;
  1278. }
  1279. else
  1280. Tokenizer_write_text(self, this);
  1281. }
  1282. else if (this == *"=" && this_context & LC_HEADING)
  1283. return (PyObject*) Tokenizer_handle_heading_end(self);
  1284. else if (this == *"\n" && this_context & LC_HEADING)
  1285. return Tokenizer_fail_route(self);
  1286. else if (this == *"&") {
  1287. if (Tokenizer_parse_entity(self))
  1288. return NULL;
  1289. }
  1290. else if (this == *"<" && next == *"!") {
  1291. next_next = Tokenizer_READ(self, 2);
  1292. if (next_next == Tokenizer_READ(self, 3) && next_next == *"-") {
  1293. if (Tokenizer_parse_comment(self))
  1294. return NULL;
  1295. }
  1296. else
  1297. Tokenizer_write_text(self, this);
  1298. }
  1299. else
  1300. Tokenizer_write_text(self, this);
  1301. self->head++;
  1302. }
  1303. }
  1304. /*
  1305. Build a list of tokens from a string of wikicode and return it.
  1306. */
  1307. static PyObject*
  1308. Tokenizer_tokenize(Tokenizer* self, PyObject* args)
  1309. {
  1310. PyObject *text, *temp;
  1311. if (!PyArg_ParseTuple(args, "U", &text)) {
  1312. const char* encoded;
  1313. Py_ssize_t size;
  1314. /* Failed to parse a Unicode object; try a string instead. */
  1315. PyErr_Clear();
  1316. if (!PyArg_ParseTuple(args, "s#", &encoded, &size))
  1317. return NULL;
  1318. temp = PyUnicode_FromStringAndSize(encoded, size);
  1319. if (!text)
  1320. return NULL;
  1321. Py_XDECREF(self->text);
  1322. text = PySequence_Fast(temp, "expected a sequence");
  1323. Py_XDECREF(temp);
  1324. self->text = text;
  1325. }
  1326. else {
  1327. Py_XDECREF(self->text);
  1328. self->text = PySequence_Fast(text, "expected a sequence");
  1329. }
  1330. self->length = PyList_GET_SIZE(self->text);
  1331. return Tokenizer_parse(self, 0);
  1332. }
  1333. PyMODINIT_FUNC
  1334. init_tokenizer(void)
  1335. {
  1336. PyObject *module, *tempmod, *defmap, *deflist, *globals, *locals,
  1337. *fromlist, *modname;
  1338. unsigned numdefs, i;
  1339. char *name;
  1340. TokenizerType.tp_new = PyType_GenericNew;
  1341. if (PyType_Ready(&TokenizerType) < 0)
  1342. return;
  1343. module = Py_InitModule("_tokenizer", module_methods);
  1344. Py_INCREF(&TokenizerType);
  1345. PyModule_AddObject(module, "CTokenizer", (PyObject*) &TokenizerType);
  1346. Py_INCREF(Py_True);
  1347. PyDict_SetItemString(TokenizerType.tp_dict, "USES_C", Py_True);
  1348. tempmod = PyImport_ImportModule("htmlentitydefs");
  1349. if (!tempmod)
  1350. return;
  1351. defmap = PyObject_GetAttrString(tempmod, "entitydefs");
  1352. if (!defmap)
  1353. return;
  1354. Py_DECREF(tempmod);
  1355. deflist = PyDict_Keys(defmap);
  1356. if (!deflist)
  1357. return;
  1358. Py_DECREF(defmap);
  1359. numdefs = (unsigned) PyList_GET_SIZE(defmap);
  1360. entitydefs = calloc(numdefs + 1, sizeof(char*));
  1361. for (i = 0; i < numdefs; i++)
  1362. entitydefs[i] = PyBytes_AsString(PyList_GET_ITEM(deflist, i));
  1363. Py_DECREF(deflist);
  1364. EMPTY = PyUnicode_FromString("");
  1365. NOARGS = PyTuple_New(0);
  1366. name = "mwparserfromhell.parser";
  1367. globals = PyEval_GetGlobals();
  1368. locals = PyEval_GetLocals();
  1369. fromlist = PyList_New(1);
  1370. if (!fromlist)
  1371. return;
  1372. modname = PyBytes_FromString("tokens");
  1373. if (!modname)
  1374. return;
  1375. PyList_SET_ITEM(fromlist, 0, modname);
  1376. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  1377. Py_DECREF(fromlist);
  1378. if (!tempmod)
  1379. return;
  1380. tokens = PyObject_GetAttrString(tempmod, "tokens");
  1381. Py_DECREF(tempmod);
  1382. Text = PyObject_GetAttrString(tokens, "Text");
  1383. TemplateOpen = PyObject_GetAttrString(tokens, "TemplateOpen");
  1384. TemplateParamSeparator = PyObject_GetAttrString(tokens,
  1385. "TemplateParamSeparator");
  1386. TemplateParamEquals = PyObject_GetAttrString(tokens,
  1387. "TemplateParamEquals");
  1388. TemplateClose = PyObject_GetAttrString(tokens, "TemplateClose");
  1389. ArgumentOpen = PyObject_GetAttrString(tokens, "ArgumentOpen");
  1390. ArgumentSeparator = PyObject_GetAttrString(tokens, "ArgumentSeparator");
  1391. ArgumentClose = PyObject_GetAttrString(tokens, "ArgumentClose");
  1392. WikilinkOpen = PyObject_GetAttrString(tokens, "WikilinkOpen");
  1393. WikilinkSeparator = PyObject_GetAttrString(tokens, "WikilinkSeparator");
  1394. WikilinkClose = PyObject_GetAttrString(tokens, "WikilinkClose");
  1395. HTMLEntityStart = PyObject_GetAttrString(tokens, "HTMLEntityStart");
  1396. HTMLEntityNumeric = PyObject_GetAttrString(tokens, "HTMLEntityNumeric");
  1397. HTMLEntityHex = PyObject_GetAttrString(tokens, "HTMLEntityHex");
  1398. HTMLEntityEnd = PyObject_GetAttrString(tokens, "HTMLEntityEnd");
  1399. HeadingStart = PyObject_GetAttrString(tokens, "HeadingStart");
  1400. HeadingEnd = PyObject_GetAttrString(tokens, "HeadingEnd");
  1401. CommentStart = PyObject_GetAttrString(tokens, "CommentStart");
  1402. CommentEnd = PyObject_GetAttrString(tokens, "CommentEnd");
  1403. TagOpenOpen = PyObject_GetAttrString(tokens, "TagOpenOpen");
  1404. TagAttrStart = PyObject_GetAttrString(tokens, "TagAttrStart");
  1405. TagAttrEquals = PyObject_GetAttrString(tokens, "TagAttrEquals");
  1406. TagAttrQuote = PyObject_GetAttrString(tokens, "TagAttrQuote");
  1407. TagCloseOpen = PyObject_GetAttrString(tokens, "TagCloseOpen");
  1408. TagCloseSelfclose = PyObject_GetAttrString(tokens, "TagCloseSelfclose");
  1409. TagOpenClose = PyObject_GetAttrString(tokens, "TagOpenClose");
  1410. TagCloseClose = PyObject_GetAttrString(tokens, "TagCloseClose");
  1411. }