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.
 
 
 
 

1734 lines
45 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 Textbuffer*
  53. Textbuffer_new(void)
  54. {
  55. Textbuffer* buffer = malloc(sizeof(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. 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(Textbuffer* this)
  86. {
  87. 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. Stack* top = malloc(sizeof(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(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. 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. 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_emit(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_emit_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_emit_text(Tokenizer* self, Py_UNICODE text)
  270. {
  271. Textbuffer* buf = self->topstack->textbuffer;
  272. if (buf->size == TEXTBUFFER_BLOCKSIZE) {
  273. 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_emit_all(Tokenizer* self, PyObject* tokenlist)
  289. {
  290. int pushed = 0;
  291. PyObject *stack, *token, *left, *right, *text;
  292. 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_emit_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_emit_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_emit_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_emit_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_emit_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_emit_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_emit_all(self, tokenlist)) {
  455. Py_DECREF(tokenlist);
  456. return -1;
  457. }
  458. Py_DECREF(tokenlist);
  459. if (self->topstack->context & LC_FAIL_NEXT)
  460. self->topstack->context ^= LC_FAIL_NEXT;
  461. return 0;
  462. }
  463. /*
  464. Parse a template at the head of the wikicode string.
  465. */
  466. static int
  467. Tokenizer_parse_template(Tokenizer* self)
  468. {
  469. PyObject *template, *token;
  470. Py_ssize_t reset = self->head;
  471. template = Tokenizer_parse(self, LC_TEMPLATE_NAME, 1);
  472. if (BAD_ROUTE) {
  473. self->head = reset;
  474. return 0;
  475. }
  476. if (!template)
  477. return -1;
  478. token = PyObject_CallObject(TemplateOpen, NULL);
  479. if (!token) {
  480. Py_DECREF(template);
  481. return -1;
  482. }
  483. if (Tokenizer_emit_first(self, token)) {
  484. Py_DECREF(token);
  485. Py_DECREF(template);
  486. return -1;
  487. }
  488. Py_DECREF(token);
  489. if (Tokenizer_emit_all(self, template)) {
  490. Py_DECREF(template);
  491. return -1;
  492. }
  493. Py_DECREF(template);
  494. token = PyObject_CallObject(TemplateClose, NULL);
  495. if (!token)
  496. return -1;
  497. if (Tokenizer_emit(self, token)) {
  498. Py_DECREF(token);
  499. return -1;
  500. }
  501. Py_DECREF(token);
  502. return 0;
  503. }
  504. /*
  505. Parse an argument at the head of the wikicode string.
  506. */
  507. static int
  508. Tokenizer_parse_argument(Tokenizer* self)
  509. {
  510. PyObject *argument, *token;
  511. Py_ssize_t reset = self->head;
  512. argument = Tokenizer_parse(self, LC_ARGUMENT_NAME, 1);
  513. if (BAD_ROUTE) {
  514. self->head = reset;
  515. return 0;
  516. }
  517. if (!argument)
  518. return -1;
  519. token = PyObject_CallObject(ArgumentOpen, NULL);
  520. if (!token) {
  521. Py_DECREF(argument);
  522. return -1;
  523. }
  524. if (Tokenizer_emit_first(self, token)) {
  525. Py_DECREF(token);
  526. Py_DECREF(argument);
  527. return -1;
  528. }
  529. Py_DECREF(token);
  530. if (Tokenizer_emit_all(self, argument)) {
  531. Py_DECREF(argument);
  532. return -1;
  533. }
  534. Py_DECREF(argument);
  535. token = PyObject_CallObject(ArgumentClose, NULL);
  536. if (!token)
  537. return -1;
  538. if (Tokenizer_emit(self, token)) {
  539. Py_DECREF(token);
  540. return -1;
  541. }
  542. Py_DECREF(token);
  543. return 0;
  544. }
  545. /*
  546. Handle a template parameter at the head of the string.
  547. */
  548. static int
  549. Tokenizer_handle_template_param(Tokenizer* self)
  550. {
  551. PyObject *stack, *token;
  552. if (self->topstack->context & LC_TEMPLATE_NAME)
  553. self->topstack->context ^= LC_TEMPLATE_NAME;
  554. else if (self->topstack->context & LC_TEMPLATE_PARAM_VALUE)
  555. self->topstack->context ^= LC_TEMPLATE_PARAM_VALUE;
  556. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  557. stack = Tokenizer_pop_keeping_context(self);
  558. if (!stack)
  559. return -1;
  560. if (Tokenizer_emit_all(self, stack)) {
  561. Py_DECREF(stack);
  562. return -1;
  563. }
  564. Py_DECREF(stack);
  565. }
  566. else
  567. self->topstack->context |= LC_TEMPLATE_PARAM_KEY;
  568. token = PyObject_CallObject(TemplateParamSeparator, NULL);
  569. if (!token)
  570. return -1;
  571. if (Tokenizer_emit(self, token)) {
  572. Py_DECREF(token);
  573. return -1;
  574. }
  575. Py_DECREF(token);
  576. if (Tokenizer_push(self, self->topstack->context))
  577. return -1;
  578. return 0;
  579. }
  580. /*
  581. Handle a template parameter's value at the head of the string.
  582. */
  583. static int
  584. Tokenizer_handle_template_param_value(Tokenizer* self)
  585. {
  586. PyObject *stack, *token;
  587. stack = Tokenizer_pop_keeping_context(self);
  588. if (!stack)
  589. return -1;
  590. if (Tokenizer_emit_all(self, stack)) {
  591. Py_DECREF(stack);
  592. return -1;
  593. }
  594. Py_DECREF(stack);
  595. self->topstack->context ^= LC_TEMPLATE_PARAM_KEY;
  596. self->topstack->context |= LC_TEMPLATE_PARAM_VALUE;
  597. token = PyObject_CallObject(TemplateParamEquals, NULL);
  598. if (!token)
  599. return -1;
  600. if (Tokenizer_emit(self, token)) {
  601. Py_DECREF(token);
  602. return -1;
  603. }
  604. Py_DECREF(token);
  605. return 0;
  606. }
  607. /*
  608. Handle the end of a template at the head of the string.
  609. */
  610. static PyObject*
  611. Tokenizer_handle_template_end(Tokenizer* self)
  612. {
  613. PyObject* stack;
  614. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  615. stack = Tokenizer_pop_keeping_context(self);
  616. if (!stack)
  617. return NULL;
  618. if (Tokenizer_emit_all(self, stack)) {
  619. Py_DECREF(stack);
  620. return NULL;
  621. }
  622. Py_DECREF(stack);
  623. }
  624. self->head++;
  625. stack = Tokenizer_pop(self);
  626. return stack;
  627. }
  628. /*
  629. Handle the separator between an argument's name and default.
  630. */
  631. static int
  632. Tokenizer_handle_argument_separator(Tokenizer* self)
  633. {
  634. PyObject* token;
  635. self->topstack->context ^= LC_ARGUMENT_NAME;
  636. self->topstack->context |= LC_ARGUMENT_DEFAULT;
  637. token = PyObject_CallObject(ArgumentSeparator, NULL);
  638. if (!token)
  639. return -1;
  640. if (Tokenizer_emit(self, token)) {
  641. Py_DECREF(token);
  642. return -1;
  643. }
  644. Py_DECREF(token);
  645. return 0;
  646. }
  647. /*
  648. Handle the end of an argument at the head of the string.
  649. */
  650. static PyObject*
  651. Tokenizer_handle_argument_end(Tokenizer* self)
  652. {
  653. PyObject* stack = Tokenizer_pop(self);
  654. self->head += 2;
  655. return stack;
  656. }
  657. /*
  658. Parse an internal wikilink at the head of the wikicode string.
  659. */
  660. static int
  661. Tokenizer_parse_wikilink(Tokenizer* self)
  662. {
  663. Py_ssize_t reset;
  664. PyObject *wikilink, *token;
  665. int i;
  666. self->head += 2;
  667. reset = self->head - 1;
  668. wikilink = Tokenizer_parse(self, LC_WIKILINK_TITLE, 1);
  669. if (BAD_ROUTE) {
  670. RESET_ROUTE();
  671. self->head = reset;
  672. for (i = 0; i < 2; i++) {
  673. if (Tokenizer_emit_text(self, *"["))
  674. return -1;
  675. }
  676. return 0;
  677. }
  678. if (!wikilink)
  679. return -1;
  680. token = PyObject_CallObject(WikilinkOpen, NULL);
  681. if (!token) {
  682. Py_DECREF(wikilink);
  683. return -1;
  684. }
  685. if (Tokenizer_emit(self, token)) {
  686. Py_DECREF(token);
  687. Py_DECREF(wikilink);
  688. return -1;
  689. }
  690. Py_DECREF(token);
  691. if (Tokenizer_emit_all(self, wikilink)) {
  692. Py_DECREF(wikilink);
  693. return -1;
  694. }
  695. Py_DECREF(wikilink);
  696. token = PyObject_CallObject(WikilinkClose, NULL);
  697. if (!token)
  698. return -1;
  699. if (Tokenizer_emit(self, token)) {
  700. Py_DECREF(token);
  701. return -1;
  702. }
  703. Py_DECREF(token);
  704. if (self->topstack->context & LC_FAIL_NEXT)
  705. self->topstack->context ^= LC_FAIL_NEXT;
  706. return 0;
  707. }
  708. /*
  709. Handle the separator between a wikilink's title and its text.
  710. */
  711. static int
  712. Tokenizer_handle_wikilink_separator(Tokenizer* self)
  713. {
  714. PyObject* token;
  715. self->topstack->context ^= LC_WIKILINK_TITLE;
  716. self->topstack->context |= LC_WIKILINK_TEXT;
  717. token = PyObject_CallObject(WikilinkSeparator, NULL);
  718. if (!token)
  719. return -1;
  720. if (Tokenizer_emit(self, token)) {
  721. Py_DECREF(token);
  722. return -1;
  723. }
  724. Py_DECREF(token);
  725. return 0;
  726. }
  727. /*
  728. Handle the end of a wikilink at the head of the string.
  729. */
  730. static PyObject*
  731. Tokenizer_handle_wikilink_end(Tokenizer* self)
  732. {
  733. PyObject* stack = Tokenizer_pop(self);
  734. self->head += 1;
  735. return stack;
  736. }
  737. /*
  738. Parse a section heading at the head of the wikicode string.
  739. */
  740. static int
  741. Tokenizer_parse_heading(Tokenizer* self)
  742. {
  743. Py_ssize_t reset = self->head;
  744. int best = 1, i, context, diff;
  745. HeadingData *heading;
  746. PyObject *level, *kwargs, *token;
  747. self->global |= GL_HEADING;
  748. self->head += 1;
  749. while (Tokenizer_READ(self, 0) == *"=") {
  750. best++;
  751. self->head++;
  752. }
  753. context = LC_HEADING_LEVEL_1 << (best > 5 ? 5 : best - 1);
  754. heading = (HeadingData*) Tokenizer_parse(self, context, 1);
  755. if (BAD_ROUTE) {
  756. RESET_ROUTE();
  757. self->head = reset + best - 1;
  758. for (i = 0; i < best; i++) {
  759. if (Tokenizer_emit_text(self, *"="))
  760. return -1;
  761. }
  762. self->global ^= GL_HEADING;
  763. return 0;
  764. }
  765. level = PyInt_FromSsize_t(heading->level);
  766. if (!level) {
  767. Py_DECREF(heading->title);
  768. free(heading);
  769. return -1;
  770. }
  771. kwargs = PyDict_New();
  772. if (!kwargs) {
  773. Py_DECREF(level);
  774. Py_DECREF(heading->title);
  775. free(heading);
  776. return -1;
  777. }
  778. PyDict_SetItemString(kwargs, "level", level);
  779. Py_DECREF(level);
  780. token = PyObject_Call(HeadingStart, NOARGS, kwargs);
  781. Py_DECREF(kwargs);
  782. if (!token) {
  783. Py_DECREF(heading->title);
  784. free(heading);
  785. return -1;
  786. }
  787. if (Tokenizer_emit(self, token)) {
  788. Py_DECREF(token);
  789. Py_DECREF(heading->title);
  790. free(heading);
  791. return -1;
  792. }
  793. Py_DECREF(token);
  794. if (heading->level < best) {
  795. diff = best - heading->level;
  796. for (i = 0; i < diff; i++) {
  797. if (Tokenizer_emit_text(self, *"=")) {
  798. Py_DECREF(heading->title);
  799. free(heading);
  800. return -1;
  801. }
  802. }
  803. }
  804. if (Tokenizer_emit_all(self, heading->title)) {
  805. Py_DECREF(heading->title);
  806. free(heading);
  807. return -1;
  808. }
  809. Py_DECREF(heading->title);
  810. free(heading);
  811. token = PyObject_CallObject(HeadingEnd, NULL);
  812. if (!token)
  813. return -1;
  814. if (Tokenizer_emit(self, token)) {
  815. Py_DECREF(token);
  816. return -1;
  817. }
  818. Py_DECREF(token);
  819. self->global ^= GL_HEADING;
  820. return 0;
  821. }
  822. /*
  823. Handle the end of a section heading at the head of the string.
  824. */
  825. static HeadingData*
  826. Tokenizer_handle_heading_end(Tokenizer* self)
  827. {
  828. Py_ssize_t reset = self->head, best;
  829. int i, current, level, diff;
  830. HeadingData *after, *heading;
  831. PyObject *stack;
  832. self->head += 1;
  833. best = 1;
  834. while (Tokenizer_READ(self, 0) == *"=") {
  835. best++;
  836. self->head++;
  837. }
  838. current = heading_level_from_context(self->topstack->context);
  839. level = current > best ? (best > 6 ? 6 : best) :
  840. (current > 6 ? 6 : current);
  841. after = (HeadingData*) Tokenizer_parse(self, self->topstack->context, 1);
  842. if (BAD_ROUTE) {
  843. RESET_ROUTE();
  844. if (level < best) {
  845. diff = best - level;
  846. for (i = 0; i < diff; i++) {
  847. if (Tokenizer_emit_text(self, *"="))
  848. return NULL;
  849. }
  850. }
  851. self->head = reset + best - 1;
  852. }
  853. else {
  854. for (i = 0; i < best; i++) {
  855. if (Tokenizer_emit_text(self, *"=")) {
  856. Py_DECREF(after->title);
  857. free(after);
  858. return NULL;
  859. }
  860. }
  861. if (Tokenizer_emit_all(self, after->title)) {
  862. Py_DECREF(after->title);
  863. free(after);
  864. return NULL;
  865. }
  866. Py_DECREF(after->title);
  867. level = after->level;
  868. free(after);
  869. }
  870. stack = Tokenizer_pop(self);
  871. if (!stack)
  872. return NULL;
  873. heading = malloc(sizeof(HeadingData));
  874. if (!heading) {
  875. PyErr_NoMemory();
  876. return NULL;
  877. }
  878. heading->title = stack;
  879. heading->level = level;
  880. return heading;
  881. }
  882. /*
  883. Actually parse an HTML entity and ensure that it is valid.
  884. */
  885. static int
  886. Tokenizer_really_parse_entity(Tokenizer* self)
  887. {
  888. PyObject *token, *kwargs, *textobj;
  889. Py_UNICODE this;
  890. int numeric, hexadecimal, i, j, zeroes, test;
  891. char *valid, *text, *buffer, *def;
  892. #define FAIL_ROUTE_AND_EXIT() { \
  893. Tokenizer_fail_route(self); \
  894. free(text); \
  895. return 0; \
  896. }
  897. token = PyObject_CallObject(HTMLEntityStart, NULL);
  898. if (!token)
  899. return -1;
  900. if (Tokenizer_emit(self, token)) {
  901. Py_DECREF(token);
  902. return -1;
  903. }
  904. Py_DECREF(token);
  905. self->head++;
  906. this = Tokenizer_READ(self, 0);
  907. if (this == *"") {
  908. Tokenizer_fail_route(self);
  909. return 0;
  910. }
  911. if (this == *"#") {
  912. numeric = 1;
  913. token = PyObject_CallObject(HTMLEntityNumeric, NULL);
  914. if (!token)
  915. return -1;
  916. if (Tokenizer_emit(self, token)) {
  917. Py_DECREF(token);
  918. return -1;
  919. }
  920. Py_DECREF(token);
  921. self->head++;
  922. this = Tokenizer_READ(self, 0);
  923. if (this == *"") {
  924. Tokenizer_fail_route(self);
  925. return 0;
  926. }
  927. if (this == *"x" || this == *"X") {
  928. hexadecimal = 1;
  929. kwargs = PyDict_New();
  930. if (!kwargs)
  931. return -1;
  932. PyDict_SetItemString(kwargs, "char", Tokenizer_read(self, 0));
  933. token = PyObject_Call(HTMLEntityHex, NOARGS, kwargs);
  934. Py_DECREF(kwargs);
  935. if (!token)
  936. return -1;
  937. if (Tokenizer_emit(self, token)) {
  938. Py_DECREF(token);
  939. return -1;
  940. }
  941. Py_DECREF(token);
  942. self->head++;
  943. }
  944. else
  945. hexadecimal = 0;
  946. }
  947. else
  948. numeric = hexadecimal = 0;
  949. if (hexadecimal)
  950. valid = HEXDIGITS;
  951. else if (numeric)
  952. valid = DIGITS;
  953. else
  954. valid = ALPHANUM;
  955. text = calloc(MAX_ENTITY_SIZE, sizeof(char));
  956. if (!text) {
  957. PyErr_NoMemory();
  958. return -1;
  959. }
  960. i = 0;
  961. zeroes = 0;
  962. while (1) {
  963. this = Tokenizer_READ(self, 0);
  964. if (this == *";") {
  965. if (i == 0)
  966. FAIL_ROUTE_AND_EXIT()
  967. break;
  968. }
  969. if (i == 0 && this == *"0") {
  970. zeroes++;
  971. self->head++;
  972. continue;
  973. }
  974. if (i >= 8)
  975. FAIL_ROUTE_AND_EXIT()
  976. for (j = 0; j < NUM_MARKERS; j++) {
  977. if (this == *MARKERS[j])
  978. FAIL_ROUTE_AND_EXIT()
  979. }
  980. j = 0;
  981. while (1) {
  982. if (!valid[j])
  983. FAIL_ROUTE_AND_EXIT()
  984. if (this == valid[j])
  985. break;
  986. j++;
  987. }
  988. text[i] = this;
  989. self->head++;
  990. i++;
  991. }
  992. if (numeric) {
  993. sscanf(text, (hexadecimal ? "%x" : "%d"), &test);
  994. if (test < 1 || test > 0x10FFFF)
  995. FAIL_ROUTE_AND_EXIT()
  996. }
  997. else {
  998. i = 0;
  999. while (1) {
  1000. def = entitydefs[i];
  1001. if (!def) // We've reached the end of the defs without finding it
  1002. FAIL_ROUTE_AND_EXIT()
  1003. if (strcmp(text, def) == 0)
  1004. break;
  1005. i++;
  1006. }
  1007. }
  1008. if (zeroes) {
  1009. buffer = calloc(strlen(text) + zeroes + 1, sizeof(char));
  1010. if (!buffer) {
  1011. free(text);
  1012. PyErr_NoMemory();
  1013. return -1;
  1014. }
  1015. for (i = 0; i < zeroes; i++)
  1016. strcat(buffer, "0");
  1017. strcat(buffer, text);
  1018. free(text);
  1019. text = buffer;
  1020. }
  1021. textobj = PyUnicode_FromString(text);
  1022. if (!textobj) {
  1023. free(text);
  1024. return -1;
  1025. }
  1026. free(text);
  1027. kwargs = PyDict_New();
  1028. if (!kwargs) {
  1029. Py_DECREF(textobj);
  1030. return -1;
  1031. }
  1032. PyDict_SetItemString(kwargs, "text", textobj);
  1033. Py_DECREF(textobj);
  1034. token = PyObject_Call(Text, NOARGS, kwargs);
  1035. Py_DECREF(kwargs);
  1036. if (!token)
  1037. return -1;
  1038. if (Tokenizer_emit(self, token)) {
  1039. Py_DECREF(token);
  1040. return -1;
  1041. }
  1042. Py_DECREF(token);
  1043. token = PyObject_CallObject(HTMLEntityEnd, NULL);
  1044. if (!token)
  1045. return -1;
  1046. if (Tokenizer_emit(self, token)) {
  1047. Py_DECREF(token);
  1048. return -1;
  1049. }
  1050. Py_DECREF(token);
  1051. return 0;
  1052. }
  1053. /*
  1054. Parse an HTML entity at the head of the wikicode string.
  1055. */
  1056. static int
  1057. Tokenizer_parse_entity(Tokenizer* self)
  1058. {
  1059. Py_ssize_t reset = self->head;
  1060. PyObject *tokenlist;
  1061. if (Tokenizer_push(self, 0))
  1062. return -1;
  1063. if (Tokenizer_really_parse_entity(self))
  1064. return -1;
  1065. if (BAD_ROUTE) {
  1066. RESET_ROUTE();
  1067. self->head = reset;
  1068. if (Tokenizer_emit_text(self, *"&"))
  1069. return -1;
  1070. return 0;
  1071. }
  1072. tokenlist = Tokenizer_pop(self);
  1073. if (!tokenlist)
  1074. return -1;
  1075. if (Tokenizer_emit_all(self, tokenlist)) {
  1076. Py_DECREF(tokenlist);
  1077. return -1;
  1078. }
  1079. Py_DECREF(tokenlist);
  1080. return 0;
  1081. }
  1082. /*
  1083. Parse an HTML comment at the head of the wikicode string.
  1084. */
  1085. static int
  1086. Tokenizer_parse_comment(Tokenizer* self)
  1087. {
  1088. Py_ssize_t reset = self->head + 3;
  1089. PyObject *token, *comment;
  1090. int i;
  1091. self->head += 4;
  1092. comment = Tokenizer_parse(self, LC_COMMENT, 1);
  1093. if (BAD_ROUTE) {
  1094. const char* text = "<!--";
  1095. RESET_ROUTE();
  1096. self->head = reset;
  1097. i = 0;
  1098. while (1) {
  1099. if (!text[i])
  1100. return 0;
  1101. if (Tokenizer_emit_text(self, (Py_UNICODE) text[i])) {
  1102. Py_XDECREF(text);
  1103. return -1;
  1104. }
  1105. i++;
  1106. }
  1107. return 0;
  1108. }
  1109. if (!comment)
  1110. return -1;
  1111. token = PyObject_CallObject(CommentStart, NULL);
  1112. if (!token) {
  1113. Py_DECREF(comment);
  1114. return -1;
  1115. }
  1116. if (Tokenizer_emit(self, token)) {
  1117. Py_DECREF(token);
  1118. Py_DECREF(comment);
  1119. return -1;
  1120. }
  1121. Py_DECREF(token);
  1122. if (Tokenizer_emit_all(self, comment)) {
  1123. Py_DECREF(comment);
  1124. return -1;
  1125. }
  1126. Py_DECREF(comment);
  1127. token = PyObject_CallObject(CommentEnd, NULL);
  1128. if (!token)
  1129. return -1;
  1130. if (Tokenizer_emit(self, token)) {
  1131. Py_DECREF(token);
  1132. return -1;
  1133. }
  1134. Py_DECREF(token);
  1135. self->head += 2;
  1136. return 0;
  1137. }
  1138. /*
  1139. Parse an HTML tag at the head of the wikicode string.
  1140. */
  1141. static int
  1142. Tokenizer_parse_tag(Tokenizer* self)
  1143. {
  1144. return 0;
  1145. }
  1146. /*
  1147. Actually parse an HTML tag, starting with the open (<foo>).
  1148. */
  1149. static PyObject*
  1150. Tokenizer_really_parse_tag(Tokenizer* self)
  1151. {
  1152. return NULL;
  1153. }
  1154. /*
  1155. Write a pending tag attribute from data to the stack.
  1156. */
  1157. static int
  1158. Tokenizer_push_tag_buffer(Tokenizer* self, TagOpenData* data)
  1159. {
  1160. return 0;
  1161. }
  1162. /*
  1163. Handle all sorts of text data inside of an HTML open tag.
  1164. */
  1165. static int
  1166. Tokenizer_handle_tag_data(Tokenizer* self, TagOpenData* data, Py_UNICODE text)
  1167. {
  1168. return 0;
  1169. }
  1170. /*
  1171. Handle whitespace inside of an HTML open tag.
  1172. */
  1173. static int
  1174. Tokenizer_handle_tag_space(Tokenizer* self, TagOpenData* data, Py_UNICODE text)
  1175. {
  1176. return 0;
  1177. }
  1178. /*
  1179. Handle regular text inside of an HTML open tag.
  1180. */
  1181. static int
  1182. Tokenizer_handle_tag_text(Tokenizer* self, Py_UNICODE text)
  1183. {
  1184. return 0;
  1185. }
  1186. /*
  1187. Handle the body of an HTML tag that is parser-blacklisted.
  1188. */
  1189. static PyObject*
  1190. Tokenizer_handle_blacklisted_tag(Tokenizer* self)
  1191. {
  1192. return NULL;
  1193. }
  1194. /*
  1195. Handle the closing of a open tag (<foo>).
  1196. */
  1197. static int
  1198. Tokenizer_handle_tag_close_open(Tokenizer* self, TagOpenData* data,
  1199. PyObject* token)
  1200. {
  1201. return 0;
  1202. }
  1203. /*
  1204. Handle the opening of a closing tag (</foo>).
  1205. */
  1206. static int
  1207. Tokenizer_handle_tag_open_close(Tokenizer* self)
  1208. {
  1209. return 0;
  1210. }
  1211. /*
  1212. Handle the ending of a closing tag (</foo>).
  1213. */
  1214. static PyObject*
  1215. Tokenizer_handle_tag_close_close(Tokenizer* self)
  1216. {
  1217. return NULL;
  1218. }
  1219. /*
  1220. Handle the (possible) start of an implicitly closing single tag.
  1221. */
  1222. static int
  1223. Tokenizer_handle_invalid_tag_start(Tokenizer* self)
  1224. {
  1225. return 0;
  1226. }
  1227. /*
  1228. Handle the end of an implicitly closing single-only HTML tag.
  1229. */
  1230. static PyObject*
  1231. Tokenizer_handle_single_only_tag_end(Tokenizer* self)
  1232. {
  1233. return NULL;
  1234. }
  1235. /*
  1236. Handle the stream end when inside a single-supporting HTML tag.
  1237. */
  1238. static PyObject*
  1239. Tokenizer_handle_single_tag_end(Tokenizer* self)
  1240. {
  1241. return NULL;
  1242. }
  1243. /*
  1244. Handle the end of the stream of wikitext.
  1245. */
  1246. static PyObject*
  1247. Tokenizer_handle_end(Tokenizer* self, int context)
  1248. {
  1249. static int fail_contexts = (LC_TEMPLATE | LC_ARGUMENT | LC_WIKILINK |
  1250. LC_HEADING | LC_COMMENT);
  1251. static int double_fail = (LC_TEMPLATE_PARAM_KEY | LC_TAG_CLOSE);
  1252. PyObject *token, *text, *trash;
  1253. int single;
  1254. if (context & fail_contexts) {
  1255. if (context & LC_TAG_BODY) {
  1256. token = PyList_GET_ITEM(self->topstack->stack, 1);
  1257. text = PyObject_GetAttrString(token, "text");
  1258. if (!text)
  1259. return NULL;
  1260. single = IS_SINGLE(text);
  1261. Py_DECREF(text);
  1262. if (single)
  1263. return Tokenizer_handle_single_tag_end(self);
  1264. }
  1265. else if (context & double_fail) {
  1266. trash = Tokenizer_pop(self);
  1267. Py_XDECREF(trash);
  1268. }
  1269. return Tokenizer_fail_route(self);
  1270. }
  1271. return Tokenizer_pop(self);
  1272. }
  1273. /*
  1274. Make sure we are not trying to write an invalid character. Return 0 if
  1275. everything is safe, or -1 if the route must be failed.
  1276. */
  1277. static int
  1278. Tokenizer_verify_safe(Tokenizer* self, int context, Py_UNICODE data)
  1279. {
  1280. if (context & LC_FAIL_NEXT) {
  1281. return -1;
  1282. }
  1283. if (context & LC_WIKILINK_TITLE) {
  1284. if (data == *"]" || data == *"{")
  1285. self->topstack->context |= LC_FAIL_NEXT;
  1286. else if (data == *"\n" || data == *"[" || data == *"}")
  1287. return -1;
  1288. return 0;
  1289. }
  1290. if (context & LC_TAG_CLOSE) {
  1291. if (data == *"<")
  1292. return -1;
  1293. return 0;
  1294. }
  1295. if (context & LC_TEMPLATE_NAME) {
  1296. if (data == *"{" || data == *"}" || data == *"[") {
  1297. self->topstack->context |= LC_FAIL_NEXT;
  1298. return 0;
  1299. }
  1300. if (data == *"]") {
  1301. return -1;
  1302. }
  1303. if (data == *"|")
  1304. return 0;
  1305. if (context & LC_HAS_TEXT) {
  1306. if (context & LC_FAIL_ON_TEXT) {
  1307. if (!Py_UNICODE_ISSPACE(data))
  1308. return -1;
  1309. }
  1310. else {
  1311. if (data == *"\n")
  1312. self->topstack->context |= LC_FAIL_ON_TEXT;
  1313. }
  1314. }
  1315. else if (!Py_UNICODE_ISSPACE(data))
  1316. self->topstack->context |= LC_HAS_TEXT;
  1317. }
  1318. else {
  1319. if (context & LC_FAIL_ON_EQUALS) {
  1320. if (data == *"=") {
  1321. return -1;
  1322. }
  1323. }
  1324. else if (context & LC_FAIL_ON_LBRACE) {
  1325. if (data == *"{" || (Tokenizer_READ(self, -1) == *"{" &&
  1326. Tokenizer_READ(self, -2) == *"{")) {
  1327. if (context & LC_TEMPLATE)
  1328. self->topstack->context |= LC_FAIL_ON_EQUALS;
  1329. else
  1330. self->topstack->context |= LC_FAIL_NEXT;
  1331. return 0;
  1332. }
  1333. self->topstack->context ^= LC_FAIL_ON_LBRACE;
  1334. }
  1335. else if (context & LC_FAIL_ON_RBRACE) {
  1336. if (data == *"}") {
  1337. if (context & LC_TEMPLATE)
  1338. self->topstack->context |= LC_FAIL_ON_EQUALS;
  1339. else
  1340. self->topstack->context |= LC_FAIL_NEXT;
  1341. return 0;
  1342. }
  1343. self->topstack->context ^= LC_FAIL_ON_RBRACE;
  1344. }
  1345. else if (data == *"{")
  1346. self->topstack->context |= LC_FAIL_ON_LBRACE;
  1347. else if (data == *"}")
  1348. self->topstack->context |= LC_FAIL_ON_RBRACE;
  1349. }
  1350. return 0;
  1351. }
  1352. /*
  1353. Parse the wikicode string, using context for when to stop. If push is true,
  1354. we will push a new context, otherwise we won't and context will be ignored.
  1355. */
  1356. static PyObject*
  1357. Tokenizer_parse(Tokenizer* self, int context, int push)
  1358. {
  1359. static int unsafe_contexts = (LC_TEMPLATE_NAME | LC_WIKILINK_TITLE |
  1360. LC_TEMPLATE_PARAM_KEY | LC_ARGUMENT_NAME);
  1361. static int double_unsafe = (LC_TEMPLATE_PARAM_KEY | LC_TAG_CLOSE);
  1362. int this_context, is_marker, i;
  1363. Py_UNICODE this, next, next_next, last;
  1364. PyObject* trash;
  1365. if (push) {
  1366. if (Tokenizer_push(self, context))
  1367. return NULL;
  1368. }
  1369. while (1) {
  1370. this = Tokenizer_READ(self, 0);
  1371. this_context = self->topstack->context;
  1372. if (this_context & unsafe_contexts) {
  1373. if (Tokenizer_verify_safe(self, this_context, this) < 0) {
  1374. if (this_context & double_unsafe) {
  1375. trash = Tokenizer_pop(self);
  1376. Py_XDECREF(trash);
  1377. }
  1378. return Tokenizer_fail_route(self);
  1379. }
  1380. }
  1381. is_marker = 0;
  1382. for (i = 0; i < NUM_MARKERS; i++) {
  1383. if (*MARKERS[i] == this) {
  1384. is_marker = 1;
  1385. break;
  1386. }
  1387. }
  1388. if (!is_marker) {
  1389. Tokenizer_emit_text(self, this);
  1390. self->head++;
  1391. continue;
  1392. }
  1393. if (this == *"")
  1394. return Tokenizer_handle_end(self, this_context);
  1395. next = Tokenizer_READ(self, 1);
  1396. if (this_context & LC_COMMENT) {
  1397. if (this == next && next == *"-") {
  1398. if (Tokenizer_READ(self, 2) == *">")
  1399. return Tokenizer_pop(self);
  1400. }
  1401. Tokenizer_emit_text(self, this);
  1402. }
  1403. else if (this == next && next == *"{") {
  1404. if (Tokenizer_CAN_RECURSE(self)) {
  1405. if (Tokenizer_parse_template_or_argument(self))
  1406. return NULL;
  1407. }
  1408. else
  1409. Tokenizer_emit_text(self, this);
  1410. }
  1411. else if (this == *"|" && this_context & LC_TEMPLATE) {
  1412. if (Tokenizer_handle_template_param(self))
  1413. return NULL;
  1414. }
  1415. else if (this == *"=" && this_context & LC_TEMPLATE_PARAM_KEY) {
  1416. if (Tokenizer_handle_template_param_value(self))
  1417. return NULL;
  1418. }
  1419. else if (this == next && next == *"}" && this_context & LC_TEMPLATE)
  1420. return Tokenizer_handle_template_end(self);
  1421. else if (this == *"|" && this_context & LC_ARGUMENT_NAME) {
  1422. if (Tokenizer_handle_argument_separator(self))
  1423. return NULL;
  1424. }
  1425. else if (this == next && next == *"}" && this_context & LC_ARGUMENT) {
  1426. if (Tokenizer_READ(self, 2) == *"}") {
  1427. return Tokenizer_handle_argument_end(self);
  1428. }
  1429. Tokenizer_emit_text(self, this);
  1430. }
  1431. else if (this == next && next == *"[") {
  1432. if (!(this_context & LC_WIKILINK_TITLE) &&
  1433. Tokenizer_CAN_RECURSE(self)) {
  1434. if (Tokenizer_parse_wikilink(self))
  1435. return NULL;
  1436. }
  1437. else
  1438. Tokenizer_emit_text(self, this);
  1439. }
  1440. else if (this == *"|" && this_context & LC_WIKILINK_TITLE) {
  1441. if (Tokenizer_handle_wikilink_separator(self))
  1442. return NULL;
  1443. }
  1444. else if (this == next && next == *"]" && this_context & LC_WIKILINK)
  1445. return Tokenizer_handle_wikilink_end(self);
  1446. else if (this == *"=" && !(self->global & GL_HEADING)) {
  1447. last = *PyUnicode_AS_UNICODE(Tokenizer_read_backwards(self, 1));
  1448. if (last == *"\n" || last == *"") {
  1449. if (Tokenizer_parse_heading(self))
  1450. return NULL;
  1451. }
  1452. else
  1453. Tokenizer_emit_text(self, this);
  1454. }
  1455. else if (this == *"=" && this_context & LC_HEADING)
  1456. return (PyObject*) Tokenizer_handle_heading_end(self);
  1457. else if (this == *"\n" && this_context & LC_HEADING)
  1458. return Tokenizer_fail_route(self);
  1459. else if (this == *"&") {
  1460. if (Tokenizer_parse_entity(self))
  1461. return NULL;
  1462. }
  1463. else if (this == *"<" && next == *"!") {
  1464. next_next = Tokenizer_READ(self, 2);
  1465. if (next_next == Tokenizer_READ(self, 3) && next_next == *"-") {
  1466. if (Tokenizer_parse_comment(self))
  1467. return NULL;
  1468. }
  1469. else
  1470. Tokenizer_emit_text(self, this);
  1471. }
  1472. else if (this == *"<" && next == *"/" &&
  1473. Tokenizer_READ(self, 2) != *"") {
  1474. if (this_context & LC_TAG_BODY) {
  1475. if (Tokenizer_handle_tag_open_close(self))
  1476. return NULL;
  1477. }
  1478. else {
  1479. if (Tokenizer_handle_invalid_tag_start(self))
  1480. return NULL;
  1481. }
  1482. }
  1483. else if (this == *"<") {
  1484. if (!(this_context & LC_TAG_CLOSE) &&
  1485. Tokenizer_CAN_RECURSE(self)) {
  1486. if (Tokenizer_parse_tag(self))
  1487. return NULL;
  1488. }
  1489. else
  1490. Tokenizer_emit_text(self, this);
  1491. }
  1492. else if (this == *">" && this_context & LC_TAG_CLOSE)
  1493. return Tokenizer_handle_tag_close_close(self);
  1494. else
  1495. Tokenizer_emit_text(self, this);
  1496. self->head++;
  1497. }
  1498. }
  1499. /*
  1500. Build a list of tokens from a string of wikicode and return it.
  1501. */
  1502. static PyObject*
  1503. Tokenizer_tokenize(Tokenizer* self, PyObject* args)
  1504. {
  1505. PyObject *text, *temp;
  1506. if (!PyArg_ParseTuple(args, "U", &text)) {
  1507. const char* encoded;
  1508. Py_ssize_t size;
  1509. /* Failed to parse a Unicode object; try a string instead. */
  1510. PyErr_Clear();
  1511. if (!PyArg_ParseTuple(args, "s#", &encoded, &size))
  1512. return NULL;
  1513. temp = PyUnicode_FromStringAndSize(encoded, size);
  1514. if (!text)
  1515. return NULL;
  1516. Py_XDECREF(self->text);
  1517. text = PySequence_Fast(temp, "expected a sequence");
  1518. Py_XDECREF(temp);
  1519. self->text = text;
  1520. }
  1521. else {
  1522. Py_XDECREF(self->text);
  1523. self->text = PySequence_Fast(text, "expected a sequence");
  1524. }
  1525. self->length = PyList_GET_SIZE(self->text);
  1526. return Tokenizer_parse(self, 0, 1);
  1527. }
  1528. static void
  1529. load_entitydefs(void)
  1530. {
  1531. PyObject *tempmod, *defmap, *deflist;
  1532. unsigned numdefs, i;
  1533. tempmod = PyImport_ImportModule("htmlentitydefs");
  1534. if (!tempmod)
  1535. return;
  1536. defmap = PyObject_GetAttrString(tempmod, "entitydefs");
  1537. if (!defmap)
  1538. return;
  1539. Py_DECREF(tempmod);
  1540. deflist = PyDict_Keys(defmap);
  1541. if (!deflist)
  1542. return;
  1543. Py_DECREF(defmap);
  1544. numdefs = (unsigned) PyList_GET_SIZE(defmap);
  1545. entitydefs = calloc(numdefs + 1, sizeof(char*));
  1546. for (i = 0; i < numdefs; i++)
  1547. entitydefs[i] = PyBytes_AsString(PyList_GET_ITEM(deflist, i));
  1548. Py_DECREF(deflist);
  1549. }
  1550. static void
  1551. load_tokens(void)
  1552. {
  1553. PyObject *tempmod, *tokens,
  1554. *globals = PyEval_GetGlobals(),
  1555. *locals = PyEval_GetLocals(),
  1556. *fromlist = PyList_New(1),
  1557. *modname = PyBytes_FromString("tokens");
  1558. char *name = "mwparserfromhell.parser";
  1559. if (!fromlist || !modname)
  1560. return;
  1561. PyList_SET_ITEM(fromlist, 0, modname);
  1562. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  1563. Py_DECREF(fromlist);
  1564. if (!tempmod)
  1565. return;
  1566. tokens = PyObject_GetAttrString(tempmod, "tokens");
  1567. Py_DECREF(tempmod);
  1568. Text = PyObject_GetAttrString(tokens, "Text");
  1569. TemplateOpen = PyObject_GetAttrString(tokens, "TemplateOpen");
  1570. TemplateParamSeparator = PyObject_GetAttrString(tokens,
  1571. "TemplateParamSeparator");
  1572. TemplateParamEquals = PyObject_GetAttrString(tokens,
  1573. "TemplateParamEquals");
  1574. TemplateClose = PyObject_GetAttrString(tokens, "TemplateClose");
  1575. ArgumentOpen = PyObject_GetAttrString(tokens, "ArgumentOpen");
  1576. ArgumentSeparator = PyObject_GetAttrString(tokens, "ArgumentSeparator");
  1577. ArgumentClose = PyObject_GetAttrString(tokens, "ArgumentClose");
  1578. WikilinkOpen = PyObject_GetAttrString(tokens, "WikilinkOpen");
  1579. WikilinkSeparator = PyObject_GetAttrString(tokens, "WikilinkSeparator");
  1580. WikilinkClose = PyObject_GetAttrString(tokens, "WikilinkClose");
  1581. HTMLEntityStart = PyObject_GetAttrString(tokens, "HTMLEntityStart");
  1582. HTMLEntityNumeric = PyObject_GetAttrString(tokens, "HTMLEntityNumeric");
  1583. HTMLEntityHex = PyObject_GetAttrString(tokens, "HTMLEntityHex");
  1584. HTMLEntityEnd = PyObject_GetAttrString(tokens, "HTMLEntityEnd");
  1585. HeadingStart = PyObject_GetAttrString(tokens, "HeadingStart");
  1586. HeadingEnd = PyObject_GetAttrString(tokens, "HeadingEnd");
  1587. CommentStart = PyObject_GetAttrString(tokens, "CommentStart");
  1588. CommentEnd = PyObject_GetAttrString(tokens, "CommentEnd");
  1589. TagOpenOpen = PyObject_GetAttrString(tokens, "TagOpenOpen");
  1590. TagAttrStart = PyObject_GetAttrString(tokens, "TagAttrStart");
  1591. TagAttrEquals = PyObject_GetAttrString(tokens, "TagAttrEquals");
  1592. TagAttrQuote = PyObject_GetAttrString(tokens, "TagAttrQuote");
  1593. TagCloseOpen = PyObject_GetAttrString(tokens, "TagCloseOpen");
  1594. TagCloseSelfclose = PyObject_GetAttrString(tokens, "TagCloseSelfclose");
  1595. TagOpenClose = PyObject_GetAttrString(tokens, "TagOpenClose");
  1596. TagCloseClose = PyObject_GetAttrString(tokens, "TagCloseClose");
  1597. Py_DECREF(tokens);
  1598. }
  1599. static void
  1600. load_tag_defs(void)
  1601. {
  1602. PyObject *tempmod,
  1603. *globals = PyEval_GetGlobals(),
  1604. *locals = PyEval_GetLocals(),
  1605. *fromlist = PyList_New(1),
  1606. *modname = PyBytes_FromString("tag_defs");
  1607. char *name = "mwparserfromhell";
  1608. if (!fromlist || !modname)
  1609. return;
  1610. PyList_SET_ITEM(fromlist, 0, modname);
  1611. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  1612. Py_DECREF(fromlist);
  1613. if (!tempmod)
  1614. return;
  1615. tag_defs = PyObject_GetAttrString(tempmod, "tag_defs");
  1616. Py_DECREF(tempmod);
  1617. }
  1618. PyMODINIT_FUNC
  1619. init_tokenizer(void)
  1620. {
  1621. PyObject *module;
  1622. TokenizerType.tp_new = PyType_GenericNew;
  1623. if (PyType_Ready(&TokenizerType) < 0)
  1624. return;
  1625. module = Py_InitModule("_tokenizer", module_methods);
  1626. Py_INCREF(&TokenizerType);
  1627. PyModule_AddObject(module, "CTokenizer", (PyObject*) &TokenizerType);
  1628. Py_INCREF(Py_True);
  1629. PyDict_SetItemString(TokenizerType.tp_dict, "USES_C", Py_True);
  1630. EMPTY = PyUnicode_FromString("");
  1631. NOARGS = PyTuple_New(0);
  1632. load_entitydefs();
  1633. load_tokens();
  1634. load_tag_defs();
  1635. }