A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

2009 Zeilen
54 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 Textbuffer*
  47. Textbuffer_new(void)
  48. {
  49. Textbuffer* buffer = malloc(sizeof(Textbuffer));
  50. if (!buffer) {
  51. PyErr_NoMemory();
  52. return NULL;
  53. }
  54. buffer->size = 0;
  55. buffer->data = malloc(sizeof(Py_UNICODE) * TEXTBUFFER_BLOCKSIZE);
  56. if (!buffer->data) {
  57. free(buffer);
  58. PyErr_NoMemory();
  59. return NULL;
  60. }
  61. buffer->next = NULL;
  62. return buffer;
  63. }
  64. static void
  65. Textbuffer_dealloc(Textbuffer* self)
  66. {
  67. Textbuffer* next;
  68. while (self) {
  69. free(self->data);
  70. next = self->next;
  71. free(self);
  72. self = next;
  73. }
  74. }
  75. /*
  76. Write text to the given textbuffer.
  77. */
  78. static int
  79. Textbuffer_write(Textbuffer** this, Py_UNICODE text)
  80. {
  81. Textbuffer* self = *this;
  82. if (self->size == TEXTBUFFER_BLOCKSIZE) {
  83. Textbuffer* new = Textbuffer_new();
  84. if (!new)
  85. return -1;
  86. new->next = self;
  87. *this = self = new;
  88. }
  89. self->data[self->size] = text;
  90. self->size++;
  91. return 0;
  92. }
  93. /*
  94. Return the contents of the textbuffer as a Python Unicode object.
  95. */
  96. static PyObject*
  97. Textbuffer_render(Textbuffer* self)
  98. {
  99. PyObject *result = PyUnicode_FromUnicode(self->data, self->size);
  100. PyObject *left, *concat;
  101. while (self->next) {
  102. self = self->next;
  103. left = PyUnicode_FromUnicode(self->data, self->size);
  104. concat = PyUnicode_Concat(left, result);
  105. Py_DECREF(left);
  106. Py_DECREF(result);
  107. result = concat;
  108. }
  109. return result;
  110. }
  111. static PyObject*
  112. Tokenizer_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
  113. {
  114. Tokenizer* self = (Tokenizer*) type->tp_alloc(type, 0);
  115. return (PyObject*) self;
  116. }
  117. static void
  118. Tokenizer_dealloc(Tokenizer* self)
  119. {
  120. Stack *this = self->topstack, *next;
  121. Py_XDECREF(self->text);
  122. while (this) {
  123. Py_DECREF(this->stack);
  124. Textbuffer_dealloc(this->textbuffer);
  125. next = this->next;
  126. free(this);
  127. this = next;
  128. }
  129. self->ob_type->tp_free((PyObject*) self);
  130. }
  131. static int
  132. Tokenizer_init(Tokenizer* self, PyObject* args, PyObject* kwds)
  133. {
  134. static char* kwlist[] = {NULL};
  135. if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
  136. return -1;
  137. self->text = Py_None;
  138. Py_INCREF(Py_None);
  139. self->topstack = NULL;
  140. self->head = 0;
  141. self->length = 0;
  142. self->global = 0;
  143. return 0;
  144. }
  145. /*
  146. Add a new token stack, context, and textbuffer to the list.
  147. */
  148. static int
  149. Tokenizer_push(Tokenizer* self, int context)
  150. {
  151. Stack* top = malloc(sizeof(Stack));
  152. if (!top) {
  153. PyErr_NoMemory();
  154. return -1;
  155. }
  156. top->stack = PyList_New(0);
  157. top->context = context;
  158. top->textbuffer = Textbuffer_new();
  159. if (!top->textbuffer)
  160. return -1;
  161. top->next = self->topstack;
  162. self->topstack = top;
  163. self->depth++;
  164. self->cycles++;
  165. return 0;
  166. }
  167. /*
  168. Push the textbuffer onto the stack as a Text node and clear it.
  169. */
  170. static int
  171. Tokenizer_push_textbuffer(Tokenizer* self)
  172. {
  173. PyObject *text, *kwargs, *token;
  174. Textbuffer* buffer = self->topstack->textbuffer;
  175. if (buffer->size == 0 && !buffer->next)
  176. return 0;
  177. text = Textbuffer_render(buffer);
  178. if (!text)
  179. return -1;
  180. kwargs = PyDict_New();
  181. if (!kwargs) {
  182. Py_DECREF(text);
  183. return -1;
  184. }
  185. PyDict_SetItemString(kwargs, "text", text);
  186. Py_DECREF(text);
  187. token = PyObject_Call(Text, NOARGS, kwargs);
  188. Py_DECREF(kwargs);
  189. if (!token)
  190. return -1;
  191. if (PyList_Append(self->topstack->stack, token)) {
  192. Py_DECREF(token);
  193. return -1;
  194. }
  195. Py_DECREF(token);
  196. Textbuffer_dealloc(buffer);
  197. self->topstack->textbuffer = Textbuffer_new();
  198. if (!self->topstack->textbuffer)
  199. return -1;
  200. return 0;
  201. }
  202. /*
  203. Pop and deallocate the top token stack/context/textbuffer.
  204. */
  205. static void
  206. Tokenizer_delete_top_of_stack(Tokenizer* self)
  207. {
  208. Stack* top = self->topstack;
  209. Py_DECREF(top->stack);
  210. Textbuffer_dealloc(top->textbuffer);
  211. self->topstack = top->next;
  212. free(top);
  213. self->depth--;
  214. }
  215. /*
  216. Pop the current stack/context/textbuffer, returing the stack.
  217. */
  218. static PyObject*
  219. Tokenizer_pop(Tokenizer* self)
  220. {
  221. PyObject* stack;
  222. if (Tokenizer_push_textbuffer(self))
  223. return NULL;
  224. stack = self->topstack->stack;
  225. Py_INCREF(stack);
  226. Tokenizer_delete_top_of_stack(self);
  227. return stack;
  228. }
  229. /*
  230. Pop the current stack/context/textbuffer, returing the stack. We will also
  231. replace the underlying stack's context with the current stack's.
  232. */
  233. static PyObject*
  234. Tokenizer_pop_keeping_context(Tokenizer* self)
  235. {
  236. PyObject* stack;
  237. int context;
  238. if (Tokenizer_push_textbuffer(self))
  239. return NULL;
  240. stack = self->topstack->stack;
  241. Py_INCREF(stack);
  242. context = self->topstack->context;
  243. Tokenizer_delete_top_of_stack(self);
  244. self->topstack->context = context;
  245. return stack;
  246. }
  247. /*
  248. Fail the current tokenization route. Discards the current
  249. stack/context/textbuffer and raises a BadRoute exception.
  250. */
  251. static void*
  252. Tokenizer_fail_route(Tokenizer* self)
  253. {
  254. PyObject* stack = Tokenizer_pop(self);
  255. Py_XDECREF(stack);
  256. FAIL_ROUTE();
  257. return NULL;
  258. }
  259. /*
  260. Write a token to the end of the current token stack.
  261. */
  262. static int
  263. Tokenizer_emit(Tokenizer* self, PyObject* token)
  264. {
  265. if (Tokenizer_push_textbuffer(self))
  266. return -1;
  267. if (PyList_Append(self->topstack->stack, token))
  268. return -1;
  269. return 0;
  270. }
  271. /*
  272. Write a token to the beginning of the current token stack.
  273. */
  274. static int
  275. Tokenizer_emit_first(Tokenizer* self, PyObject* token)
  276. {
  277. if (Tokenizer_push_textbuffer(self))
  278. return -1;
  279. if (PyList_Insert(self->topstack->stack, 0, token))
  280. return -1;
  281. return 0;
  282. }
  283. /*
  284. Write text to the current textbuffer.
  285. */
  286. static int
  287. Tokenizer_emit_text(Tokenizer* self, Py_UNICODE text)
  288. {
  289. return Textbuffer_write(&(self->topstack->textbuffer), text);
  290. }
  291. /*
  292. Write a series of tokens to the current stack at once.
  293. */
  294. static int
  295. Tokenizer_emit_all(Tokenizer* self, PyObject* tokenlist)
  296. {
  297. int pushed = 0;
  298. PyObject *stack, *token, *left, *right, *text;
  299. Textbuffer* buffer;
  300. Py_ssize_t size;
  301. if (PyList_GET_SIZE(tokenlist) > 0) {
  302. token = PyList_GET_ITEM(tokenlist, 0);
  303. switch (PyObject_IsInstance(token, Text)) {
  304. case 0:
  305. break;
  306. case 1: {
  307. pushed = 1;
  308. buffer = self->topstack->textbuffer;
  309. if (buffer->size == 0 && !buffer->next)
  310. break;
  311. left = Textbuffer_render(buffer);
  312. if (!left)
  313. return -1;
  314. right = PyObject_GetAttrString(token, "text");
  315. if (!right)
  316. return -1;
  317. text = PyUnicode_Concat(left, right);
  318. Py_DECREF(left);
  319. Py_DECREF(right);
  320. if (!text)
  321. return -1;
  322. if (PyObject_SetAttrString(token, "text", text)) {
  323. Py_DECREF(text);
  324. return -1;
  325. }
  326. Py_DECREF(text);
  327. Textbuffer_dealloc(buffer);
  328. self->topstack->textbuffer = Textbuffer_new();
  329. if (!self->topstack->textbuffer)
  330. return -1;
  331. break;
  332. }
  333. case -1:
  334. return -1;
  335. }
  336. }
  337. if (!pushed) {
  338. if (Tokenizer_push_textbuffer(self))
  339. return -1;
  340. }
  341. stack = self->topstack->stack;
  342. size = PyList_GET_SIZE(stack);
  343. if (PyList_SetSlice(stack, size, size, tokenlist))
  344. return -1;
  345. return 0;
  346. }
  347. /*
  348. Pop the current stack, write text, and then write the stack. 'text' is a
  349. NULL-terminated array of chars.
  350. */
  351. static int
  352. Tokenizer_emit_text_then_stack(Tokenizer* self, const char* text)
  353. {
  354. PyObject* stack = Tokenizer_pop(self);
  355. int i = 0;
  356. while (1) {
  357. if (!text[i])
  358. break;
  359. if (Tokenizer_emit_text(self, (Py_UNICODE) text[i])) {
  360. Py_XDECREF(stack);
  361. return -1;
  362. }
  363. i++;
  364. }
  365. if (stack) {
  366. if (PyList_GET_SIZE(stack) > 0) {
  367. if (Tokenizer_emit_all(self, stack)) {
  368. Py_DECREF(stack);
  369. return -1;
  370. }
  371. }
  372. Py_DECREF(stack);
  373. }
  374. self->head--;
  375. return 0;
  376. }
  377. /*
  378. Read the value at a relative point in the wikicode, forwards.
  379. */
  380. static PyObject*
  381. Tokenizer_read(Tokenizer* self, Py_ssize_t delta)
  382. {
  383. Py_ssize_t index = self->head + delta;
  384. if (index >= self->length)
  385. return EMPTY;
  386. return PyList_GET_ITEM(self->text, index);
  387. }
  388. /*
  389. Read the value at a relative point in the wikicode, backwards.
  390. */
  391. static PyObject*
  392. Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta)
  393. {
  394. Py_ssize_t index;
  395. if (delta > self->head)
  396. return EMPTY;
  397. index = self->head - delta;
  398. return PyList_GET_ITEM(self->text, index);
  399. }
  400. /*
  401. Parse a template or argument at the head of the wikicode string.
  402. */
  403. static int
  404. Tokenizer_parse_template_or_argument(Tokenizer* self)
  405. {
  406. unsigned int braces = 2, i;
  407. PyObject *tokenlist;
  408. self->head += 2;
  409. while (Tokenizer_READ(self, 0) == *"{" && braces < MAX_BRACES) {
  410. self->head++;
  411. braces++;
  412. }
  413. if (Tokenizer_push(self, 0))
  414. return -1;
  415. while (braces) {
  416. if (braces == 1) {
  417. if (Tokenizer_emit_text_then_stack(self, "{"))
  418. return -1;
  419. return 0;
  420. }
  421. if (braces == 2) {
  422. if (Tokenizer_parse_template(self))
  423. return -1;
  424. if (BAD_ROUTE) {
  425. RESET_ROUTE();
  426. if (Tokenizer_emit_text_then_stack(self, "{{"))
  427. return -1;
  428. return 0;
  429. }
  430. break;
  431. }
  432. if (Tokenizer_parse_argument(self))
  433. return -1;
  434. if (BAD_ROUTE) {
  435. RESET_ROUTE();
  436. if (Tokenizer_parse_template(self))
  437. return -1;
  438. if (BAD_ROUTE) {
  439. char text[MAX_BRACES + 1];
  440. RESET_ROUTE();
  441. for (i = 0; i < braces; i++) text[i] = *"{";
  442. text[braces] = *"";
  443. if (Tokenizer_emit_text_then_stack(self, text)) {
  444. Py_XDECREF(text);
  445. return -1;
  446. }
  447. Py_XDECREF(text);
  448. return 0;
  449. }
  450. else
  451. braces -= 2;
  452. }
  453. else
  454. braces -= 3;
  455. if (braces)
  456. self->head++;
  457. }
  458. tokenlist = Tokenizer_pop(self);
  459. if (!tokenlist)
  460. return -1;
  461. if (Tokenizer_emit_all(self, tokenlist)) {
  462. Py_DECREF(tokenlist);
  463. return -1;
  464. }
  465. Py_DECREF(tokenlist);
  466. if (self->topstack->context & LC_FAIL_NEXT)
  467. self->topstack->context ^= LC_FAIL_NEXT;
  468. return 0;
  469. }
  470. /*
  471. Parse a template at the head of the wikicode string.
  472. */
  473. static int
  474. Tokenizer_parse_template(Tokenizer* self)
  475. {
  476. PyObject *template, *token;
  477. Py_ssize_t reset = self->head;
  478. template = Tokenizer_parse(self, LC_TEMPLATE_NAME, 1);
  479. if (BAD_ROUTE) {
  480. self->head = reset;
  481. return 0;
  482. }
  483. if (!template)
  484. return -1;
  485. token = PyObject_CallObject(TemplateOpen, NULL);
  486. if (!token) {
  487. Py_DECREF(template);
  488. return -1;
  489. }
  490. if (Tokenizer_emit_first(self, token)) {
  491. Py_DECREF(token);
  492. Py_DECREF(template);
  493. return -1;
  494. }
  495. Py_DECREF(token);
  496. if (Tokenizer_emit_all(self, template)) {
  497. Py_DECREF(template);
  498. return -1;
  499. }
  500. Py_DECREF(template);
  501. token = PyObject_CallObject(TemplateClose, NULL);
  502. if (!token)
  503. return -1;
  504. if (Tokenizer_emit(self, token)) {
  505. Py_DECREF(token);
  506. return -1;
  507. }
  508. Py_DECREF(token);
  509. return 0;
  510. }
  511. /*
  512. Parse an argument at the head of the wikicode string.
  513. */
  514. static int
  515. Tokenizer_parse_argument(Tokenizer* self)
  516. {
  517. PyObject *argument, *token;
  518. Py_ssize_t reset = self->head;
  519. argument = Tokenizer_parse(self, LC_ARGUMENT_NAME, 1);
  520. if (BAD_ROUTE) {
  521. self->head = reset;
  522. return 0;
  523. }
  524. if (!argument)
  525. return -1;
  526. token = PyObject_CallObject(ArgumentOpen, NULL);
  527. if (!token) {
  528. Py_DECREF(argument);
  529. return -1;
  530. }
  531. if (Tokenizer_emit_first(self, token)) {
  532. Py_DECREF(token);
  533. Py_DECREF(argument);
  534. return -1;
  535. }
  536. Py_DECREF(token);
  537. if (Tokenizer_emit_all(self, argument)) {
  538. Py_DECREF(argument);
  539. return -1;
  540. }
  541. Py_DECREF(argument);
  542. token = PyObject_CallObject(ArgumentClose, NULL);
  543. if (!token)
  544. return -1;
  545. if (Tokenizer_emit(self, token)) {
  546. Py_DECREF(token);
  547. return -1;
  548. }
  549. Py_DECREF(token);
  550. return 0;
  551. }
  552. /*
  553. Handle a template parameter at the head of the string.
  554. */
  555. static int
  556. Tokenizer_handle_template_param(Tokenizer* self)
  557. {
  558. PyObject *stack, *token;
  559. if (self->topstack->context & LC_TEMPLATE_NAME)
  560. self->topstack->context ^= LC_TEMPLATE_NAME;
  561. else if (self->topstack->context & LC_TEMPLATE_PARAM_VALUE)
  562. self->topstack->context ^= LC_TEMPLATE_PARAM_VALUE;
  563. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  564. stack = Tokenizer_pop_keeping_context(self);
  565. if (!stack)
  566. return -1;
  567. if (Tokenizer_emit_all(self, stack)) {
  568. Py_DECREF(stack);
  569. return -1;
  570. }
  571. Py_DECREF(stack);
  572. }
  573. else
  574. self->topstack->context |= LC_TEMPLATE_PARAM_KEY;
  575. token = PyObject_CallObject(TemplateParamSeparator, NULL);
  576. if (!token)
  577. return -1;
  578. if (Tokenizer_emit(self, token)) {
  579. Py_DECREF(token);
  580. return -1;
  581. }
  582. Py_DECREF(token);
  583. if (Tokenizer_push(self, self->topstack->context))
  584. return -1;
  585. return 0;
  586. }
  587. /*
  588. Handle a template parameter's value at the head of the string.
  589. */
  590. static int
  591. Tokenizer_handle_template_param_value(Tokenizer* self)
  592. {
  593. PyObject *stack, *token;
  594. stack = Tokenizer_pop_keeping_context(self);
  595. if (!stack)
  596. return -1;
  597. if (Tokenizer_emit_all(self, stack)) {
  598. Py_DECREF(stack);
  599. return -1;
  600. }
  601. Py_DECREF(stack);
  602. self->topstack->context ^= LC_TEMPLATE_PARAM_KEY;
  603. self->topstack->context |= LC_TEMPLATE_PARAM_VALUE;
  604. token = PyObject_CallObject(TemplateParamEquals, NULL);
  605. if (!token)
  606. return -1;
  607. if (Tokenizer_emit(self, token)) {
  608. Py_DECREF(token);
  609. return -1;
  610. }
  611. Py_DECREF(token);
  612. return 0;
  613. }
  614. /*
  615. Handle the end of a template at the head of the string.
  616. */
  617. static PyObject*
  618. Tokenizer_handle_template_end(Tokenizer* self)
  619. {
  620. PyObject* stack;
  621. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  622. stack = Tokenizer_pop_keeping_context(self);
  623. if (!stack)
  624. return NULL;
  625. if (Tokenizer_emit_all(self, stack)) {
  626. Py_DECREF(stack);
  627. return NULL;
  628. }
  629. Py_DECREF(stack);
  630. }
  631. self->head++;
  632. stack = Tokenizer_pop(self);
  633. return stack;
  634. }
  635. /*
  636. Handle the separator between an argument's name and default.
  637. */
  638. static int
  639. Tokenizer_handle_argument_separator(Tokenizer* self)
  640. {
  641. PyObject* token;
  642. self->topstack->context ^= LC_ARGUMENT_NAME;
  643. self->topstack->context |= LC_ARGUMENT_DEFAULT;
  644. token = PyObject_CallObject(ArgumentSeparator, NULL);
  645. if (!token)
  646. return -1;
  647. if (Tokenizer_emit(self, token)) {
  648. Py_DECREF(token);
  649. return -1;
  650. }
  651. Py_DECREF(token);
  652. return 0;
  653. }
  654. /*
  655. Handle the end of an argument at the head of the string.
  656. */
  657. static PyObject*
  658. Tokenizer_handle_argument_end(Tokenizer* self)
  659. {
  660. PyObject* stack = Tokenizer_pop(self);
  661. self->head += 2;
  662. return stack;
  663. }
  664. /*
  665. Parse an internal wikilink at the head of the wikicode string.
  666. */
  667. static int
  668. Tokenizer_parse_wikilink(Tokenizer* self)
  669. {
  670. Py_ssize_t reset;
  671. PyObject *wikilink, *token;
  672. int i;
  673. self->head += 2;
  674. reset = self->head - 1;
  675. wikilink = Tokenizer_parse(self, LC_WIKILINK_TITLE, 1);
  676. if (BAD_ROUTE) {
  677. RESET_ROUTE();
  678. self->head = reset;
  679. for (i = 0; i < 2; i++) {
  680. if (Tokenizer_emit_text(self, *"["))
  681. return -1;
  682. }
  683. return 0;
  684. }
  685. if (!wikilink)
  686. return -1;
  687. token = PyObject_CallObject(WikilinkOpen, NULL);
  688. if (!token) {
  689. Py_DECREF(wikilink);
  690. return -1;
  691. }
  692. if (Tokenizer_emit(self, token)) {
  693. Py_DECREF(token);
  694. Py_DECREF(wikilink);
  695. return -1;
  696. }
  697. Py_DECREF(token);
  698. if (Tokenizer_emit_all(self, wikilink)) {
  699. Py_DECREF(wikilink);
  700. return -1;
  701. }
  702. Py_DECREF(wikilink);
  703. token = PyObject_CallObject(WikilinkClose, NULL);
  704. if (!token)
  705. return -1;
  706. if (Tokenizer_emit(self, token)) {
  707. Py_DECREF(token);
  708. return -1;
  709. }
  710. Py_DECREF(token);
  711. if (self->topstack->context & LC_FAIL_NEXT)
  712. self->topstack->context ^= LC_FAIL_NEXT;
  713. return 0;
  714. }
  715. /*
  716. Handle the separator between a wikilink's title and its text.
  717. */
  718. static int
  719. Tokenizer_handle_wikilink_separator(Tokenizer* self)
  720. {
  721. PyObject* token;
  722. self->topstack->context ^= LC_WIKILINK_TITLE;
  723. self->topstack->context |= LC_WIKILINK_TEXT;
  724. token = PyObject_CallObject(WikilinkSeparator, NULL);
  725. if (!token)
  726. return -1;
  727. if (Tokenizer_emit(self, token)) {
  728. Py_DECREF(token);
  729. return -1;
  730. }
  731. Py_DECREF(token);
  732. return 0;
  733. }
  734. /*
  735. Handle the end of a wikilink at the head of the string.
  736. */
  737. static PyObject*
  738. Tokenizer_handle_wikilink_end(Tokenizer* self)
  739. {
  740. PyObject* stack = Tokenizer_pop(self);
  741. self->head += 1;
  742. return stack;
  743. }
  744. /*
  745. Parse a section heading at the head of the wikicode string.
  746. */
  747. static int
  748. Tokenizer_parse_heading(Tokenizer* self)
  749. {
  750. Py_ssize_t reset = self->head;
  751. int best = 1, i, context, diff;
  752. HeadingData *heading;
  753. PyObject *level, *kwargs, *token;
  754. self->global |= GL_HEADING;
  755. self->head += 1;
  756. while (Tokenizer_READ(self, 0) == *"=") {
  757. best++;
  758. self->head++;
  759. }
  760. context = LC_HEADING_LEVEL_1 << (best > 5 ? 5 : best - 1);
  761. heading = (HeadingData*) Tokenizer_parse(self, context, 1);
  762. if (BAD_ROUTE) {
  763. RESET_ROUTE();
  764. self->head = reset + best - 1;
  765. for (i = 0; i < best; i++) {
  766. if (Tokenizer_emit_text(self, *"="))
  767. return -1;
  768. }
  769. self->global ^= GL_HEADING;
  770. return 0;
  771. }
  772. level = PyInt_FromSsize_t(heading->level);
  773. if (!level) {
  774. Py_DECREF(heading->title);
  775. free(heading);
  776. return -1;
  777. }
  778. kwargs = PyDict_New();
  779. if (!kwargs) {
  780. Py_DECREF(level);
  781. Py_DECREF(heading->title);
  782. free(heading);
  783. return -1;
  784. }
  785. PyDict_SetItemString(kwargs, "level", level);
  786. Py_DECREF(level);
  787. token = PyObject_Call(HeadingStart, NOARGS, kwargs);
  788. Py_DECREF(kwargs);
  789. if (!token) {
  790. Py_DECREF(heading->title);
  791. free(heading);
  792. return -1;
  793. }
  794. if (Tokenizer_emit(self, token)) {
  795. Py_DECREF(token);
  796. Py_DECREF(heading->title);
  797. free(heading);
  798. return -1;
  799. }
  800. Py_DECREF(token);
  801. if (heading->level < best) {
  802. diff = best - heading->level;
  803. for (i = 0; i < diff; i++) {
  804. if (Tokenizer_emit_text(self, *"=")) {
  805. Py_DECREF(heading->title);
  806. free(heading);
  807. return -1;
  808. }
  809. }
  810. }
  811. if (Tokenizer_emit_all(self, heading->title)) {
  812. Py_DECREF(heading->title);
  813. free(heading);
  814. return -1;
  815. }
  816. Py_DECREF(heading->title);
  817. free(heading);
  818. token = PyObject_CallObject(HeadingEnd, NULL);
  819. if (!token)
  820. return -1;
  821. if (Tokenizer_emit(self, token)) {
  822. Py_DECREF(token);
  823. return -1;
  824. }
  825. Py_DECREF(token);
  826. self->global ^= GL_HEADING;
  827. return 0;
  828. }
  829. /*
  830. Handle the end of a section heading at the head of the string.
  831. */
  832. static HeadingData*
  833. Tokenizer_handle_heading_end(Tokenizer* self)
  834. {
  835. Py_ssize_t reset = self->head, best;
  836. int i, current, level, diff;
  837. HeadingData *after, *heading;
  838. PyObject *stack;
  839. self->head += 1;
  840. best = 1;
  841. while (Tokenizer_READ(self, 0) == *"=") {
  842. best++;
  843. self->head++;
  844. }
  845. current = heading_level_from_context(self->topstack->context);
  846. level = current > best ? (best > 6 ? 6 : best) :
  847. (current > 6 ? 6 : current);
  848. after = (HeadingData*) Tokenizer_parse(self, self->topstack->context, 1);
  849. if (BAD_ROUTE) {
  850. RESET_ROUTE();
  851. if (level < best) {
  852. diff = best - level;
  853. for (i = 0; i < diff; i++) {
  854. if (Tokenizer_emit_text(self, *"="))
  855. return NULL;
  856. }
  857. }
  858. self->head = reset + best - 1;
  859. }
  860. else {
  861. for (i = 0; i < best; i++) {
  862. if (Tokenizer_emit_text(self, *"=")) {
  863. Py_DECREF(after->title);
  864. free(after);
  865. return NULL;
  866. }
  867. }
  868. if (Tokenizer_emit_all(self, after->title)) {
  869. Py_DECREF(after->title);
  870. free(after);
  871. return NULL;
  872. }
  873. Py_DECREF(after->title);
  874. level = after->level;
  875. free(after);
  876. }
  877. stack = Tokenizer_pop(self);
  878. if (!stack)
  879. return NULL;
  880. heading = malloc(sizeof(HeadingData));
  881. if (!heading) {
  882. PyErr_NoMemory();
  883. return NULL;
  884. }
  885. heading->title = stack;
  886. heading->level = level;
  887. return heading;
  888. }
  889. /*
  890. Actually parse an HTML entity and ensure that it is valid.
  891. */
  892. static int
  893. Tokenizer_really_parse_entity(Tokenizer* self)
  894. {
  895. PyObject *token, *kwargs, *textobj;
  896. Py_UNICODE this;
  897. int numeric, hexadecimal, i, j, zeroes, test;
  898. char *valid, *text, *buffer, *def;
  899. #define FAIL_ROUTE_AND_EXIT() { \
  900. Tokenizer_fail_route(self); \
  901. free(text); \
  902. return 0; \
  903. }
  904. token = PyObject_CallObject(HTMLEntityStart, NULL);
  905. if (!token)
  906. return -1;
  907. if (Tokenizer_emit(self, token)) {
  908. Py_DECREF(token);
  909. return -1;
  910. }
  911. Py_DECREF(token);
  912. self->head++;
  913. this = Tokenizer_READ(self, 0);
  914. if (this == *"") {
  915. Tokenizer_fail_route(self);
  916. return 0;
  917. }
  918. if (this == *"#") {
  919. numeric = 1;
  920. token = PyObject_CallObject(HTMLEntityNumeric, NULL);
  921. if (!token)
  922. return -1;
  923. if (Tokenizer_emit(self, token)) {
  924. Py_DECREF(token);
  925. return -1;
  926. }
  927. Py_DECREF(token);
  928. self->head++;
  929. this = Tokenizer_READ(self, 0);
  930. if (this == *"") {
  931. Tokenizer_fail_route(self);
  932. return 0;
  933. }
  934. if (this == *"x" || this == *"X") {
  935. hexadecimal = 1;
  936. kwargs = PyDict_New();
  937. if (!kwargs)
  938. return -1;
  939. PyDict_SetItemString(kwargs, "char", Tokenizer_read(self, 0));
  940. token = PyObject_Call(HTMLEntityHex, NOARGS, kwargs);
  941. Py_DECREF(kwargs);
  942. if (!token)
  943. return -1;
  944. if (Tokenizer_emit(self, token)) {
  945. Py_DECREF(token);
  946. return -1;
  947. }
  948. Py_DECREF(token);
  949. self->head++;
  950. }
  951. else
  952. hexadecimal = 0;
  953. }
  954. else
  955. numeric = hexadecimal = 0;
  956. if (hexadecimal)
  957. valid = HEXDIGITS;
  958. else if (numeric)
  959. valid = DIGITS;
  960. else
  961. valid = ALPHANUM;
  962. text = calloc(MAX_ENTITY_SIZE, sizeof(char));
  963. if (!text) {
  964. PyErr_NoMemory();
  965. return -1;
  966. }
  967. i = 0;
  968. zeroes = 0;
  969. while (1) {
  970. this = Tokenizer_READ(self, 0);
  971. if (this == *";") {
  972. if (i == 0)
  973. FAIL_ROUTE_AND_EXIT()
  974. break;
  975. }
  976. if (i == 0 && this == *"0") {
  977. zeroes++;
  978. self->head++;
  979. continue;
  980. }
  981. if (i >= 8)
  982. FAIL_ROUTE_AND_EXIT()
  983. for (j = 0; j < NUM_MARKERS; j++) {
  984. if (this == *MARKERS[j])
  985. FAIL_ROUTE_AND_EXIT()
  986. }
  987. j = 0;
  988. while (1) {
  989. if (!valid[j])
  990. FAIL_ROUTE_AND_EXIT()
  991. if (this == valid[j])
  992. break;
  993. j++;
  994. }
  995. text[i] = this;
  996. self->head++;
  997. i++;
  998. }
  999. if (numeric) {
  1000. sscanf(text, (hexadecimal ? "%x" : "%d"), &test);
  1001. if (test < 1 || test > 0x10FFFF)
  1002. FAIL_ROUTE_AND_EXIT()
  1003. }
  1004. else {
  1005. i = 0;
  1006. while (1) {
  1007. def = entitydefs[i];
  1008. if (!def) // We've reached the end of the defs without finding it
  1009. FAIL_ROUTE_AND_EXIT()
  1010. if (strcmp(text, def) == 0)
  1011. break;
  1012. i++;
  1013. }
  1014. }
  1015. if (zeroes) {
  1016. buffer = calloc(strlen(text) + zeroes + 1, sizeof(char));
  1017. if (!buffer) {
  1018. free(text);
  1019. PyErr_NoMemory();
  1020. return -1;
  1021. }
  1022. for (i = 0; i < zeroes; i++)
  1023. strcat(buffer, "0");
  1024. strcat(buffer, text);
  1025. free(text);
  1026. text = buffer;
  1027. }
  1028. textobj = PyUnicode_FromString(text);
  1029. if (!textobj) {
  1030. free(text);
  1031. return -1;
  1032. }
  1033. free(text);
  1034. kwargs = PyDict_New();
  1035. if (!kwargs) {
  1036. Py_DECREF(textobj);
  1037. return -1;
  1038. }
  1039. PyDict_SetItemString(kwargs, "text", textobj);
  1040. Py_DECREF(textobj);
  1041. token = PyObject_Call(Text, NOARGS, kwargs);
  1042. Py_DECREF(kwargs);
  1043. if (!token)
  1044. return -1;
  1045. if (Tokenizer_emit(self, token)) {
  1046. Py_DECREF(token);
  1047. return -1;
  1048. }
  1049. Py_DECREF(token);
  1050. token = PyObject_CallObject(HTMLEntityEnd, NULL);
  1051. if (!token)
  1052. return -1;
  1053. if (Tokenizer_emit(self, token)) {
  1054. Py_DECREF(token);
  1055. return -1;
  1056. }
  1057. Py_DECREF(token);
  1058. return 0;
  1059. }
  1060. /*
  1061. Parse an HTML entity at the head of the wikicode string.
  1062. */
  1063. static int
  1064. Tokenizer_parse_entity(Tokenizer* self)
  1065. {
  1066. Py_ssize_t reset = self->head;
  1067. PyObject *tokenlist;
  1068. if (Tokenizer_push(self, 0))
  1069. return -1;
  1070. if (Tokenizer_really_parse_entity(self))
  1071. return -1;
  1072. if (BAD_ROUTE) {
  1073. RESET_ROUTE();
  1074. self->head = reset;
  1075. if (Tokenizer_emit_text(self, *"&"))
  1076. return -1;
  1077. return 0;
  1078. }
  1079. tokenlist = Tokenizer_pop(self);
  1080. if (!tokenlist)
  1081. return -1;
  1082. if (Tokenizer_emit_all(self, tokenlist)) {
  1083. Py_DECREF(tokenlist);
  1084. return -1;
  1085. }
  1086. Py_DECREF(tokenlist);
  1087. return 0;
  1088. }
  1089. /*
  1090. Parse an HTML comment at the head of the wikicode string.
  1091. */
  1092. static int
  1093. Tokenizer_parse_comment(Tokenizer* self)
  1094. {
  1095. Py_ssize_t reset = self->head + 3;
  1096. PyObject *token, *comment;
  1097. int i;
  1098. self->head += 4;
  1099. comment = Tokenizer_parse(self, LC_COMMENT, 1);
  1100. if (BAD_ROUTE) {
  1101. const char* text = "<!--";
  1102. RESET_ROUTE();
  1103. self->head = reset;
  1104. i = 0;
  1105. while (1) {
  1106. if (!text[i])
  1107. return 0;
  1108. if (Tokenizer_emit_text(self, (Py_UNICODE) text[i])) {
  1109. Py_XDECREF(text);
  1110. return -1;
  1111. }
  1112. i++;
  1113. }
  1114. return 0;
  1115. }
  1116. if (!comment)
  1117. return -1;
  1118. token = PyObject_CallObject(CommentStart, NULL);
  1119. if (!token) {
  1120. Py_DECREF(comment);
  1121. return -1;
  1122. }
  1123. if (Tokenizer_emit(self, token)) {
  1124. Py_DECREF(token);
  1125. Py_DECREF(comment);
  1126. return -1;
  1127. }
  1128. Py_DECREF(token);
  1129. if (Tokenizer_emit_all(self, comment)) {
  1130. Py_DECREF(comment);
  1131. return -1;
  1132. }
  1133. Py_DECREF(comment);
  1134. token = PyObject_CallObject(CommentEnd, NULL);
  1135. if (!token)
  1136. return -1;
  1137. if (Tokenizer_emit(self, token)) {
  1138. Py_DECREF(token);
  1139. return -1;
  1140. }
  1141. Py_DECREF(token);
  1142. self->head += 2;
  1143. return 0;
  1144. }
  1145. /*
  1146. Parse an HTML tag at the head of the wikicode string.
  1147. */
  1148. static int
  1149. Tokenizer_parse_tag(Tokenizer* self)
  1150. {
  1151. Py_ssize_t reset = self->head;
  1152. PyObject* tag;
  1153. self->head++;
  1154. tag = Tokenizer_really_parse_tag(self);
  1155. if (BAD_ROUTE) {
  1156. self->head = reset;
  1157. return Tokenizer_emit_text(self, *"<");
  1158. }
  1159. if (!tag) {
  1160. return -1;
  1161. }
  1162. Tokenizer_emit_all(self, tag);
  1163. Py_DECREF(tag);
  1164. return 0;
  1165. }
  1166. /*
  1167. Actually parse an HTML tag, starting with the open (<foo>).
  1168. */
  1169. static PyObject*
  1170. Tokenizer_really_parse_tag(Tokenizer* self)
  1171. {
  1172. TagOpenData *data = malloc(sizeof(TagOpenData));
  1173. PyObject *token, *text, *trash;
  1174. Py_UNICODE this, next;
  1175. int can_exit;
  1176. if (!data)
  1177. return NULL;
  1178. data->pad_first = Textbuffer_new();
  1179. data->pad_before_eq = Textbuffer_new();
  1180. data->pad_after_eq = Textbuffer_new();
  1181. if (!data->pad_first || !data->pad_before_eq || !data->pad_after_eq) {
  1182. free(data);
  1183. return NULL;
  1184. }
  1185. if (Tokenizer_push(self, LC_TAG_OPEN)) {
  1186. free(data);
  1187. return NULL;
  1188. }
  1189. token = PyObject_CallObject(TagOpenOpen, NULL);
  1190. if (!token) {
  1191. free(data);
  1192. return NULL;
  1193. }
  1194. if (Tokenizer_emit(self, token)) {
  1195. Py_DECREF(token);
  1196. free(data);
  1197. return NULL;
  1198. }
  1199. Py_DECREF(token);
  1200. while (1) {
  1201. this = Tokenizer_READ(self, 0);
  1202. next = Tokenizer_READ(self, 1);
  1203. can_exit = (!(data->context & (TAG_QUOTED | TAG_NAME)) ||
  1204. data->context & TAG_NOTE_SPACE);
  1205. if (this == *"") {
  1206. if (self->topstack->context & LC_TAG_ATTR) {
  1207. if (data->context & TAG_QUOTED) {
  1208. // Unclosed attribute quote: reset, don't die
  1209. data->context = TAG_ATTR_VALUE;
  1210. trash = Tokenizer_pop(self);
  1211. Py_XDECREF(trash);
  1212. self->head = data->reset;
  1213. continue;
  1214. }
  1215. trash = Tokenizer_pop(self);
  1216. Py_XDECREF(trash);
  1217. }
  1218. free(data);
  1219. return Tokenizer_fail_route(self);
  1220. }
  1221. else if (this == *">" && can_exit) {
  1222. if (Tokenizer_handle_tag_close_open(self, data, TagCloseOpen)) {
  1223. free(data);
  1224. return NULL;
  1225. }
  1226. free(data);
  1227. self->topstack->context = LC_TAG_BODY;
  1228. token = PyList_GET_ITEM(self->topstack->stack, 1);
  1229. text = PyObject_GetAttrString(token, "text");
  1230. if (!text)
  1231. return NULL;
  1232. if (IS_SINGLE_ONLY(text)) {
  1233. Py_DECREF(text);
  1234. return Tokenizer_handle_single_only_tag_end(self);
  1235. }
  1236. if (IS_PARSABLE(text)) {
  1237. Py_DECREF(text);
  1238. return Tokenizer_parse(self, 0, 0);
  1239. }
  1240. Py_DECREF(text);
  1241. return Tokenizer_handle_blacklisted_tag(self);
  1242. }
  1243. else if (this == *"/" && next == *">" && can_exit) {
  1244. if (Tokenizer_handle_tag_close_open(self, data, TagCloseSelfclose)) {
  1245. free(data);
  1246. return NULL;
  1247. }
  1248. free(data);
  1249. return Tokenizer_pop(self);
  1250. }
  1251. else {
  1252. if (Tokenizer_handle_tag_data(self, data, this) || BAD_ROUTE) {
  1253. free(data);
  1254. return NULL;
  1255. }
  1256. }
  1257. self->head++;
  1258. }
  1259. }
  1260. /*
  1261. Write a pending tag attribute from data to the stack.
  1262. */
  1263. static int
  1264. Tokenizer_push_tag_buffer(Tokenizer* self, TagOpenData* data)
  1265. {
  1266. PyObject *token, *tokens, *kwargs, *pad_first, *pad_before_eq,
  1267. *pad_after_eq;
  1268. if (data->context & TAG_QUOTED) {
  1269. token = PyObject_CallObject(TagAttrQuote, NULL);
  1270. if (!token)
  1271. return -1;
  1272. if (Tokenizer_emit_first(self, token)) {
  1273. Py_DECREF(token);
  1274. return -1;
  1275. }
  1276. Py_DECREF(token);
  1277. tokens = Tokenizer_pop(self);
  1278. if (!tokens)
  1279. return -1;
  1280. if (Tokenizer_emit_all(self, tokens)) {
  1281. Py_DECREF(tokens);
  1282. return -1;
  1283. }
  1284. Py_DECREF(tokens);
  1285. }
  1286. pad_first = Textbuffer_render(data->pad_first);
  1287. pad_before_eq = Textbuffer_render(data->pad_before_eq);
  1288. pad_after_eq = Textbuffer_render(data->pad_after_eq);
  1289. if (!pad_first || !pad_before_eq || !pad_after_eq)
  1290. return -1;
  1291. kwargs = PyDict_New();
  1292. if (!kwargs)
  1293. return -1;
  1294. PyDict_SetItemString(kwargs, "pad_first", pad_first);
  1295. PyDict_SetItemString(kwargs, "pad_before_eq", pad_before_eq);
  1296. PyDict_SetItemString(kwargs, "pad_after_eq", pad_after_eq);
  1297. Py_DECREF(pad_first);
  1298. Py_DECREF(pad_before_eq);
  1299. Py_DECREF(pad_after_eq);
  1300. token = PyObject_Call(TagAttrStart, NOARGS, kwargs);
  1301. Py_DECREF(kwargs);
  1302. if (!token)
  1303. return -1;
  1304. if (Tokenizer_emit_first(self, token)) {
  1305. Py_DECREF(token);
  1306. return -1;
  1307. }
  1308. Py_DECREF(token);
  1309. tokens = Tokenizer_pop(self);
  1310. if (!tokens)
  1311. return -1;
  1312. if (Tokenizer_emit_all(self, tokens)) {
  1313. Py_DECREF(tokens);
  1314. return -1;
  1315. }
  1316. Py_DECREF(tokens);
  1317. Textbuffer_dealloc(data->pad_first);
  1318. Textbuffer_dealloc(data->pad_before_eq);
  1319. Textbuffer_dealloc(data->pad_after_eq);
  1320. data->pad_first = Textbuffer_new();
  1321. data->pad_before_eq = Textbuffer_new();
  1322. data->pad_after_eq = Textbuffer_new();
  1323. if (!data->pad_first || !data->pad_before_eq || !data->pad_after_eq)
  1324. return -1;
  1325. return 0;
  1326. }
  1327. /*
  1328. Handle all sorts of text data inside of an HTML open tag.
  1329. */
  1330. static int
  1331. Tokenizer_handle_tag_data(Tokenizer* self, TagOpenData* data, Py_UNICODE chunk)
  1332. {
  1333. PyObject *trash, *token;
  1334. int first_time, i, is_marker = 0, escaped;
  1335. if (data->context & TAG_NAME) {
  1336. first_time = !(data->context & TAG_NOTE_SPACE);
  1337. for (i = 0; i < NUM_MARKERS; i++) {
  1338. if (*MARKERS[i] == chunk) {
  1339. is_marker = 1;
  1340. break;
  1341. }
  1342. }
  1343. if (is_marker || (Py_UNICODE_ISSPACE(chunk) && first_time)) {
  1344. // Tags must start with text, not spaces
  1345. Tokenizer_fail_route(self);
  1346. return 0;
  1347. }
  1348. else if (first_time)
  1349. data->context |= TAG_NOTE_SPACE;
  1350. else if (Py_UNICODE_ISSPACE(chunk))
  1351. data->context = TAG_ATTR_READY;
  1352. }
  1353. else if (Py_UNICODE_ISSPACE(chunk))
  1354. return Tokenizer_handle_tag_space(self, data, chunk);
  1355. else if (data->context & TAG_NOTE_SPACE) {
  1356. if (data->context & TAG_QUOTED) {
  1357. data->context = TAG_ATTR_VALUE;
  1358. trash = Tokenizer_pop(self);
  1359. Py_XDECREF(trash);
  1360. self->head = data->reset - 1; // Will be auto-incremented
  1361. }
  1362. else
  1363. Tokenizer_fail_route(self);
  1364. return 0;
  1365. }
  1366. else if (data->context & TAG_ATTR_READY) {
  1367. data->context = TAG_ATTR_NAME;
  1368. if (Tokenizer_push(self, LC_TAG_ATTR))
  1369. return -1;
  1370. }
  1371. else if (data->context & TAG_ATTR_NAME) {
  1372. if (chunk == *"=") {
  1373. data->context = TAG_ATTR_VALUE | TAG_NOTE_QUOTE;
  1374. token = PyObject_CallObject(TagAttrEquals, NULL);
  1375. if (!token)
  1376. return -1;
  1377. if (Tokenizer_emit(self, token)) {
  1378. Py_DECREF(token);
  1379. return -1;
  1380. }
  1381. Py_DECREF(token);
  1382. return 0;
  1383. }
  1384. if (data->context & TAG_NOTE_EQUALS) {
  1385. if (Tokenizer_push_tag_buffer(self, data))
  1386. return -1;
  1387. data->context = TAG_ATTR_NAME;
  1388. if (Tokenizer_push(self, LC_TAG_ATTR))
  1389. return -1;
  1390. }
  1391. }
  1392. else if (data->context & TAG_ATTR_VALUE) {
  1393. escaped = (Tokenizer_READ_BACKWARDS(self, 1) == *"\\" &&
  1394. Tokenizer_READ_BACKWARDS(self, 2) != *"\\");
  1395. if (data->context & TAG_NOTE_QUOTE) {
  1396. data->context ^= TAG_NOTE_QUOTE;
  1397. if (chunk == *"\"" && !escaped) {
  1398. data->context |= TAG_QUOTED;
  1399. if (Tokenizer_push(self, self->topstack->context))
  1400. return -1;
  1401. data->reset = self->head;
  1402. return 0;
  1403. }
  1404. }
  1405. else if (data->context & TAG_QUOTED) {
  1406. if (chunk == *"\"" && !escaped) {
  1407. data->context |= TAG_NOTE_SPACE;
  1408. return 0;
  1409. }
  1410. }
  1411. }
  1412. return Tokenizer_handle_tag_text(self, chunk);
  1413. }
  1414. /*
  1415. Handle whitespace inside of an HTML open tag.
  1416. */
  1417. static int
  1418. Tokenizer_handle_tag_space(Tokenizer* self, TagOpenData* data, Py_UNICODE text)
  1419. {
  1420. int ctx = data->context;
  1421. int end_of_value = (ctx & TAG_ATTR_VALUE &&
  1422. !(ctx & (TAG_QUOTED | TAG_NOTE_QUOTE)));
  1423. if (end_of_value || (ctx & TAG_QUOTED && ctx & TAG_NOTE_SPACE)) {
  1424. if (Tokenizer_push_tag_buffer(self, data))
  1425. return -1;
  1426. data->context = TAG_ATTR_READY;
  1427. }
  1428. else if (ctx & TAG_NOTE_SPACE)
  1429. data->context = TAG_ATTR_READY;
  1430. else if (ctx & TAG_ATTR_NAME) {
  1431. data->context |= TAG_NOTE_EQUALS;
  1432. Textbuffer_write(&(data->pad_before_eq), text);
  1433. }
  1434. if (ctx & TAG_QUOTED && !(ctx & TAG_NOTE_SPACE)) {
  1435. if (Tokenizer_emit_text(self, text))
  1436. return -1;
  1437. }
  1438. else if (data->context & TAG_ATTR_READY)
  1439. Textbuffer_write(&(data->pad_first), text);
  1440. else if (data->context & TAG_ATTR_VALUE)
  1441. Textbuffer_write(&(data->pad_after_eq), text);
  1442. return 0;
  1443. }
  1444. /*
  1445. Handle regular text inside of an HTML open tag.
  1446. */
  1447. static int
  1448. Tokenizer_handle_tag_text(Tokenizer* self, Py_UNICODE text)
  1449. {
  1450. return 0;
  1451. }
  1452. /*
  1453. Handle the body of an HTML tag that is parser-blacklisted.
  1454. */
  1455. static PyObject*
  1456. Tokenizer_handle_blacklisted_tag(Tokenizer* self)
  1457. {
  1458. return NULL;
  1459. }
  1460. /*
  1461. Handle the closing of a open tag (<foo>).
  1462. */
  1463. static int
  1464. Tokenizer_handle_tag_close_open(Tokenizer* self, TagOpenData* data,
  1465. PyObject* token)
  1466. {
  1467. return 0;
  1468. }
  1469. /*
  1470. Handle the opening of a closing tag (</foo>).
  1471. */
  1472. static int
  1473. Tokenizer_handle_tag_open_close(Tokenizer* self)
  1474. {
  1475. return 0;
  1476. }
  1477. /*
  1478. Handle the ending of a closing tag (</foo>).
  1479. */
  1480. static PyObject*
  1481. Tokenizer_handle_tag_close_close(Tokenizer* self)
  1482. {
  1483. return NULL;
  1484. }
  1485. /*
  1486. Handle the (possible) start of an implicitly closing single tag.
  1487. */
  1488. static int
  1489. Tokenizer_handle_invalid_tag_start(Tokenizer* self)
  1490. {
  1491. return 0;
  1492. }
  1493. /*
  1494. Handle the end of an implicitly closing single-only HTML tag.
  1495. */
  1496. static PyObject*
  1497. Tokenizer_handle_single_only_tag_end(Tokenizer* self)
  1498. {
  1499. return NULL;
  1500. }
  1501. /*
  1502. Handle the stream end when inside a single-supporting HTML tag.
  1503. */
  1504. static PyObject*
  1505. Tokenizer_handle_single_tag_end(Tokenizer* self)
  1506. {
  1507. return NULL;
  1508. }
  1509. /*
  1510. Handle the end of the stream of wikitext.
  1511. */
  1512. static PyObject*
  1513. Tokenizer_handle_end(Tokenizer* self, int context)
  1514. {
  1515. static int fail_contexts = (LC_TEMPLATE | LC_ARGUMENT | LC_WIKILINK |
  1516. LC_HEADING | LC_COMMENT);
  1517. static int double_fail = (LC_TEMPLATE_PARAM_KEY | LC_TAG_CLOSE);
  1518. PyObject *token, *text, *trash;
  1519. int single;
  1520. if (context & fail_contexts) {
  1521. if (context & LC_TAG_BODY) {
  1522. token = PyList_GET_ITEM(self->topstack->stack, 1);
  1523. text = PyObject_GetAttrString(token, "text");
  1524. if (!text)
  1525. return NULL;
  1526. single = IS_SINGLE(text);
  1527. Py_DECREF(text);
  1528. if (single)
  1529. return Tokenizer_handle_single_tag_end(self);
  1530. }
  1531. else if (context & double_fail) {
  1532. trash = Tokenizer_pop(self);
  1533. Py_XDECREF(trash);
  1534. }
  1535. return Tokenizer_fail_route(self);
  1536. }
  1537. return Tokenizer_pop(self);
  1538. }
  1539. /*
  1540. Make sure we are not trying to write an invalid character. Return 0 if
  1541. everything is safe, or -1 if the route must be failed.
  1542. */
  1543. static int
  1544. Tokenizer_verify_safe(Tokenizer* self, int context, Py_UNICODE data)
  1545. {
  1546. if (context & LC_FAIL_NEXT) {
  1547. return -1;
  1548. }
  1549. if (context & LC_WIKILINK_TITLE) {
  1550. if (data == *"]" || data == *"{")
  1551. self->topstack->context |= LC_FAIL_NEXT;
  1552. else if (data == *"\n" || data == *"[" || data == *"}")
  1553. return -1;
  1554. return 0;
  1555. }
  1556. if (context & LC_TAG_CLOSE) {
  1557. if (data == *"<")
  1558. return -1;
  1559. return 0;
  1560. }
  1561. if (context & LC_TEMPLATE_NAME) {
  1562. if (data == *"{" || data == *"}" || data == *"[") {
  1563. self->topstack->context |= LC_FAIL_NEXT;
  1564. return 0;
  1565. }
  1566. if (data == *"]") {
  1567. return -1;
  1568. }
  1569. if (data == *"|")
  1570. return 0;
  1571. if (context & LC_HAS_TEXT) {
  1572. if (context & LC_FAIL_ON_TEXT) {
  1573. if (!Py_UNICODE_ISSPACE(data))
  1574. return -1;
  1575. }
  1576. else {
  1577. if (data == *"\n")
  1578. self->topstack->context |= LC_FAIL_ON_TEXT;
  1579. }
  1580. }
  1581. else if (!Py_UNICODE_ISSPACE(data))
  1582. self->topstack->context |= LC_HAS_TEXT;
  1583. }
  1584. else {
  1585. if (context & LC_FAIL_ON_EQUALS) {
  1586. if (data == *"=") {
  1587. return -1;
  1588. }
  1589. }
  1590. else if (context & LC_FAIL_ON_LBRACE) {
  1591. if (data == *"{" || (Tokenizer_READ(self, -1) == *"{" &&
  1592. Tokenizer_READ(self, -2) == *"{")) {
  1593. if (context & LC_TEMPLATE)
  1594. self->topstack->context |= LC_FAIL_ON_EQUALS;
  1595. else
  1596. self->topstack->context |= LC_FAIL_NEXT;
  1597. return 0;
  1598. }
  1599. self->topstack->context ^= LC_FAIL_ON_LBRACE;
  1600. }
  1601. else if (context & LC_FAIL_ON_RBRACE) {
  1602. if (data == *"}") {
  1603. if (context & LC_TEMPLATE)
  1604. self->topstack->context |= LC_FAIL_ON_EQUALS;
  1605. else
  1606. self->topstack->context |= LC_FAIL_NEXT;
  1607. return 0;
  1608. }
  1609. self->topstack->context ^= LC_FAIL_ON_RBRACE;
  1610. }
  1611. else if (data == *"{")
  1612. self->topstack->context |= LC_FAIL_ON_LBRACE;
  1613. else if (data == *"}")
  1614. self->topstack->context |= LC_FAIL_ON_RBRACE;
  1615. }
  1616. return 0;
  1617. }
  1618. /*
  1619. Parse the wikicode string, using context for when to stop. If push is true,
  1620. we will push a new context, otherwise we won't and context will be ignored.
  1621. */
  1622. static PyObject*
  1623. Tokenizer_parse(Tokenizer* self, int context, int push)
  1624. {
  1625. static int unsafe_contexts = (LC_TEMPLATE_NAME | LC_WIKILINK_TITLE |
  1626. LC_TEMPLATE_PARAM_KEY | LC_ARGUMENT_NAME);
  1627. static int double_unsafe = (LC_TEMPLATE_PARAM_KEY | LC_TAG_CLOSE);
  1628. int this_context, is_marker, i;
  1629. Py_UNICODE this, next, next_next, last;
  1630. PyObject* trash;
  1631. if (push) {
  1632. if (Tokenizer_push(self, context))
  1633. return NULL;
  1634. }
  1635. while (1) {
  1636. this = Tokenizer_READ(self, 0);
  1637. this_context = self->topstack->context;
  1638. if (this_context & unsafe_contexts) {
  1639. if (Tokenizer_verify_safe(self, this_context, this) < 0) {
  1640. if (this_context & double_unsafe) {
  1641. trash = Tokenizer_pop(self);
  1642. Py_XDECREF(trash);
  1643. }
  1644. return Tokenizer_fail_route(self);
  1645. }
  1646. }
  1647. is_marker = 0;
  1648. for (i = 0; i < NUM_MARKERS; i++) {
  1649. if (*MARKERS[i] == this) {
  1650. is_marker = 1;
  1651. break;
  1652. }
  1653. }
  1654. if (!is_marker) {
  1655. if (Tokenizer_emit_text(self, this))
  1656. return NULL;
  1657. self->head++;
  1658. continue;
  1659. }
  1660. if (this == *"")
  1661. return Tokenizer_handle_end(self, this_context);
  1662. next = Tokenizer_READ(self, 1);
  1663. if (this_context & LC_COMMENT) {
  1664. if (this == next && next == *"-") {
  1665. if (Tokenizer_READ(self, 2) == *">")
  1666. return Tokenizer_pop(self);
  1667. }
  1668. if (Tokenizer_emit_text(self, this))
  1669. return NULL;
  1670. }
  1671. else if (this == next && next == *"{") {
  1672. if (Tokenizer_CAN_RECURSE(self)) {
  1673. if (Tokenizer_parse_template_or_argument(self))
  1674. return NULL;
  1675. }
  1676. else if (Tokenizer_emit_text(self, this))
  1677. return NULL;
  1678. }
  1679. else if (this == *"|" && this_context & LC_TEMPLATE) {
  1680. if (Tokenizer_handle_template_param(self))
  1681. return NULL;
  1682. }
  1683. else if (this == *"=" && this_context & LC_TEMPLATE_PARAM_KEY) {
  1684. if (Tokenizer_handle_template_param_value(self))
  1685. return NULL;
  1686. }
  1687. else if (this == next && next == *"}" && this_context & LC_TEMPLATE)
  1688. return Tokenizer_handle_template_end(self);
  1689. else if (this == *"|" && this_context & LC_ARGUMENT_NAME) {
  1690. if (Tokenizer_handle_argument_separator(self))
  1691. return NULL;
  1692. }
  1693. else if (this == next && next == *"}" && this_context & LC_ARGUMENT) {
  1694. if (Tokenizer_READ(self, 2) == *"}") {
  1695. return Tokenizer_handle_argument_end(self);
  1696. }
  1697. if (Tokenizer_emit_text(self, this))
  1698. return NULL;
  1699. }
  1700. else if (this == next && next == *"[") {
  1701. if (!(this_context & LC_WIKILINK_TITLE) &&
  1702. Tokenizer_CAN_RECURSE(self)) {
  1703. if (Tokenizer_parse_wikilink(self))
  1704. return NULL;
  1705. }
  1706. else if (Tokenizer_emit_text(self, this))
  1707. return NULL;
  1708. }
  1709. else if (this == *"|" && this_context & LC_WIKILINK_TITLE) {
  1710. if (Tokenizer_handle_wikilink_separator(self))
  1711. return NULL;
  1712. }
  1713. else if (this == next && next == *"]" && this_context & LC_WIKILINK)
  1714. return Tokenizer_handle_wikilink_end(self);
  1715. else if (this == *"=" && !(self->global & GL_HEADING)) {
  1716. last = Tokenizer_READ_BACKWARDS(self, 1);
  1717. if (last == *"\n" || last == *"") {
  1718. if (Tokenizer_parse_heading(self))
  1719. return NULL;
  1720. }
  1721. else if (Tokenizer_emit_text(self, this))
  1722. return NULL;
  1723. }
  1724. else if (this == *"=" && this_context & LC_HEADING)
  1725. return (PyObject*) Tokenizer_handle_heading_end(self);
  1726. else if (this == *"\n" && this_context & LC_HEADING)
  1727. return Tokenizer_fail_route(self);
  1728. else if (this == *"&") {
  1729. if (Tokenizer_parse_entity(self))
  1730. return NULL;
  1731. }
  1732. else if (this == *"<" && next == *"!") {
  1733. next_next = Tokenizer_READ(self, 2);
  1734. if (next_next == Tokenizer_READ(self, 3) && next_next == *"-") {
  1735. if (Tokenizer_parse_comment(self))
  1736. return NULL;
  1737. }
  1738. else if (Tokenizer_emit_text(self, this))
  1739. return NULL;
  1740. }
  1741. else if (this == *"<" && next == *"/" &&
  1742. Tokenizer_READ(self, 2) != *"") {
  1743. if (this_context & LC_TAG_BODY) {
  1744. if (Tokenizer_handle_tag_open_close(self))
  1745. return NULL;
  1746. }
  1747. else {
  1748. if (Tokenizer_handle_invalid_tag_start(self))
  1749. return NULL;
  1750. }
  1751. }
  1752. else if (this == *"<") {
  1753. if (!(this_context & LC_TAG_CLOSE) &&
  1754. Tokenizer_CAN_RECURSE(self)) {
  1755. if (Tokenizer_parse_tag(self))
  1756. return NULL;
  1757. }
  1758. else if (Tokenizer_emit_text(self, this))
  1759. return NULL;
  1760. }
  1761. else if (this == *">" && this_context & LC_TAG_CLOSE)
  1762. return Tokenizer_handle_tag_close_close(self);
  1763. else if (Tokenizer_emit_text(self, this))
  1764. return NULL;
  1765. self->head++;
  1766. }
  1767. }
  1768. /*
  1769. Build a list of tokens from a string of wikicode and return it.
  1770. */
  1771. static PyObject*
  1772. Tokenizer_tokenize(Tokenizer* self, PyObject* args)
  1773. {
  1774. PyObject *text, *temp;
  1775. if (!PyArg_ParseTuple(args, "U", &text)) {
  1776. const char* encoded;
  1777. Py_ssize_t size;
  1778. /* Failed to parse a Unicode object; try a string instead. */
  1779. PyErr_Clear();
  1780. if (!PyArg_ParseTuple(args, "s#", &encoded, &size))
  1781. return NULL;
  1782. temp = PyUnicode_FromStringAndSize(encoded, size);
  1783. if (!text)
  1784. return NULL;
  1785. Py_XDECREF(self->text);
  1786. text = PySequence_Fast(temp, "expected a sequence");
  1787. Py_XDECREF(temp);
  1788. self->text = text;
  1789. }
  1790. else {
  1791. Py_XDECREF(self->text);
  1792. self->text = PySequence_Fast(text, "expected a sequence");
  1793. }
  1794. self->length = PyList_GET_SIZE(self->text);
  1795. return Tokenizer_parse(self, 0, 1);
  1796. }
  1797. static void
  1798. load_entitydefs(void)
  1799. {
  1800. PyObject *tempmod, *defmap, *deflist;
  1801. unsigned numdefs, i;
  1802. tempmod = PyImport_ImportModule("htmlentitydefs");
  1803. if (!tempmod)
  1804. return;
  1805. defmap = PyObject_GetAttrString(tempmod, "entitydefs");
  1806. if (!defmap)
  1807. return;
  1808. Py_DECREF(tempmod);
  1809. deflist = PyDict_Keys(defmap);
  1810. if (!deflist)
  1811. return;
  1812. Py_DECREF(defmap);
  1813. numdefs = (unsigned) PyList_GET_SIZE(defmap);
  1814. entitydefs = calloc(numdefs + 1, sizeof(char*));
  1815. for (i = 0; i < numdefs; i++)
  1816. entitydefs[i] = PyBytes_AsString(PyList_GET_ITEM(deflist, i));
  1817. Py_DECREF(deflist);
  1818. }
  1819. static void
  1820. load_tokens(void)
  1821. {
  1822. PyObject *tempmod, *tokens,
  1823. *globals = PyEval_GetGlobals(),
  1824. *locals = PyEval_GetLocals(),
  1825. *fromlist = PyList_New(1),
  1826. *modname = PyBytes_FromString("tokens");
  1827. char *name = "mwparserfromhell.parser";
  1828. if (!fromlist || !modname)
  1829. return;
  1830. PyList_SET_ITEM(fromlist, 0, modname);
  1831. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  1832. Py_DECREF(fromlist);
  1833. if (!tempmod)
  1834. return;
  1835. tokens = PyObject_GetAttrString(tempmod, "tokens");
  1836. Py_DECREF(tempmod);
  1837. Text = PyObject_GetAttrString(tokens, "Text");
  1838. TemplateOpen = PyObject_GetAttrString(tokens, "TemplateOpen");
  1839. TemplateParamSeparator = PyObject_GetAttrString(tokens,
  1840. "TemplateParamSeparator");
  1841. TemplateParamEquals = PyObject_GetAttrString(tokens,
  1842. "TemplateParamEquals");
  1843. TemplateClose = PyObject_GetAttrString(tokens, "TemplateClose");
  1844. ArgumentOpen = PyObject_GetAttrString(tokens, "ArgumentOpen");
  1845. ArgumentSeparator = PyObject_GetAttrString(tokens, "ArgumentSeparator");
  1846. ArgumentClose = PyObject_GetAttrString(tokens, "ArgumentClose");
  1847. WikilinkOpen = PyObject_GetAttrString(tokens, "WikilinkOpen");
  1848. WikilinkSeparator = PyObject_GetAttrString(tokens, "WikilinkSeparator");
  1849. WikilinkClose = PyObject_GetAttrString(tokens, "WikilinkClose");
  1850. HTMLEntityStart = PyObject_GetAttrString(tokens, "HTMLEntityStart");
  1851. HTMLEntityNumeric = PyObject_GetAttrString(tokens, "HTMLEntityNumeric");
  1852. HTMLEntityHex = PyObject_GetAttrString(tokens, "HTMLEntityHex");
  1853. HTMLEntityEnd = PyObject_GetAttrString(tokens, "HTMLEntityEnd");
  1854. HeadingStart = PyObject_GetAttrString(tokens, "HeadingStart");
  1855. HeadingEnd = PyObject_GetAttrString(tokens, "HeadingEnd");
  1856. CommentStart = PyObject_GetAttrString(tokens, "CommentStart");
  1857. CommentEnd = PyObject_GetAttrString(tokens, "CommentEnd");
  1858. TagOpenOpen = PyObject_GetAttrString(tokens, "TagOpenOpen");
  1859. TagAttrStart = PyObject_GetAttrString(tokens, "TagAttrStart");
  1860. TagAttrEquals = PyObject_GetAttrString(tokens, "TagAttrEquals");
  1861. TagAttrQuote = PyObject_GetAttrString(tokens, "TagAttrQuote");
  1862. TagCloseOpen = PyObject_GetAttrString(tokens, "TagCloseOpen");
  1863. TagCloseSelfclose = PyObject_GetAttrString(tokens, "TagCloseSelfclose");
  1864. TagOpenClose = PyObject_GetAttrString(tokens, "TagOpenClose");
  1865. TagCloseClose = PyObject_GetAttrString(tokens, "TagCloseClose");
  1866. Py_DECREF(tokens);
  1867. }
  1868. static void
  1869. load_tag_defs(void)
  1870. {
  1871. PyObject *tempmod,
  1872. *globals = PyEval_GetGlobals(),
  1873. *locals = PyEval_GetLocals(),
  1874. *fromlist = PyList_New(1),
  1875. *modname = PyBytes_FromString("tag_defs");
  1876. char *name = "mwparserfromhell";
  1877. if (!fromlist || !modname)
  1878. return;
  1879. PyList_SET_ITEM(fromlist, 0, modname);
  1880. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  1881. Py_DECREF(fromlist);
  1882. if (!tempmod)
  1883. return;
  1884. tag_defs = PyObject_GetAttrString(tempmod, "tag_defs");
  1885. Py_DECREF(tempmod);
  1886. }
  1887. PyMODINIT_FUNC
  1888. init_tokenizer(void)
  1889. {
  1890. PyObject *module;
  1891. TokenizerType.tp_new = PyType_GenericNew;
  1892. if (PyType_Ready(&TokenizerType) < 0)
  1893. return;
  1894. module = Py_InitModule("_tokenizer", module_methods);
  1895. Py_INCREF(&TokenizerType);
  1896. PyModule_AddObject(module, "CTokenizer", (PyObject*) &TokenizerType);
  1897. Py_INCREF(Py_True);
  1898. PyDict_SetItemString(TokenizerType.tp_dict, "USES_C", Py_True);
  1899. EMPTY = PyUnicode_FromString("");
  1900. NOARGS = PyTuple_New(0);
  1901. load_entitydefs();
  1902. load_tokens();
  1903. load_tag_defs();
  1904. }