A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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