A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

1455 lines
39 KiB

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