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.
 
 
 
 

919 lines
22 KiB

  1. /*
  2. Tokenizer for MWParserFromHell
  3. Copyright (C) 2012 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #include "tokenizer.h"
  21. static PyObject*
  22. Tokenizer_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
  23. {
  24. Tokenizer *self;
  25. self = (Tokenizer*) type->tp_alloc(type, 0);
  26. if (self != NULL) {
  27. self->text = Py_None;
  28. Py_INCREF(Py_None);
  29. self->stacks = PyList_New(0);
  30. if (!self->stacks) {
  31. Py_DECREF(self);
  32. return NULL;
  33. }
  34. self->head = 0;
  35. self->length = 0;
  36. self->global = 0;
  37. }
  38. return (PyObject*) self;
  39. }
  40. static void
  41. Tokenizer_dealloc(Tokenizer* self)
  42. {
  43. Py_XDECREF(self->text);
  44. Py_XDECREF(self->stacks);
  45. Py_XDECREF(self->topstack);
  46. self->ob_type->tp_free((PyObject*) self);
  47. }
  48. static int
  49. Tokenizer_init(Tokenizer* self, PyObject* args, PyObject* kwds)
  50. {
  51. static char* kwlist[] = {NULL};
  52. if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
  53. return -1;
  54. return 0;
  55. }
  56. static int
  57. Tokenizer_set_context(Tokenizer* self, Py_ssize_t value)
  58. {
  59. if (PyList_SetItem(self->topstack, 1, PyInt_FromSsize_t(value)))
  60. return -1;
  61. return 0;
  62. }
  63. static int
  64. Tokenizer_set_textbuffer(Tokenizer* self, PyObject* value)
  65. {
  66. if (PyList_SetItem(self->topstack, 2, value))
  67. return -1;
  68. return 0;
  69. }
  70. /*
  71. Add a new token stack, context, and textbuffer to the list.
  72. */
  73. static int
  74. Tokenizer_push(Tokenizer* self, Py_ssize_t context)
  75. {
  76. PyObject* top = PyList_New(3);
  77. PyList_SET_ITEM(top, 0, PyList_New(0));
  78. PyList_SET_ITEM(top, 1, PyInt_FromSsize_t(context));
  79. PyList_SET_ITEM(top, 2, PyList_New(0));
  80. Py_XDECREF(self->topstack);
  81. self->topstack = top;
  82. if (PyList_Append(self->stacks, top))
  83. return -1;
  84. return 0;
  85. }
  86. /*
  87. Push the textbuffer onto the stack as a Text node and clear it.
  88. */
  89. static int
  90. Tokenizer_push_textbuffer(Tokenizer* self)
  91. {
  92. if (PySequence_Fast_GET_SIZE(Tokenizer_TEXTBUFFER(self)) > 0) {
  93. PyObject* text = PyUnicode_Join(EMPTY, Tokenizer_TEXTBUFFER(self));
  94. if (!text) return -1;
  95. PyObject* class = PyObject_GetAttrString(tokens, "Text");
  96. if (!class) return -1;
  97. PyObject* kwargs = PyDict_New();
  98. if (!kwargs) return -1;
  99. PyDict_SetItemString(kwargs, "text", text);
  100. Py_DECREF(text);
  101. PyObject* token = PyInstance_New(class, NOARGS, kwargs);
  102. Py_DECREF(class);
  103. Py_DECREF(kwargs);
  104. if (!token) return -1;
  105. if (PyList_Append(Tokenizer_STACK(self), token)) {
  106. Py_DECREF(token);
  107. return -1;
  108. }
  109. Py_DECREF(token);
  110. if (Tokenizer_set_textbuffer(self, PyList_New(0)))
  111. return -1;
  112. }
  113. return 0;
  114. }
  115. static int
  116. Tokenizer_delete_top_of_stack(Tokenizer* self)
  117. {
  118. if (PySequence_DelItem(self->stacks, -1))
  119. return -1;
  120. Py_DECREF(self->topstack);
  121. Py_ssize_t size = PySequence_Fast_GET_SIZE(self->stacks);
  122. if (size > 0) {
  123. PyObject* top = PySequence_Fast_GET_ITEM(self->stacks, size - 1);
  124. self->topstack = top;
  125. Py_INCREF(top);
  126. }
  127. else {
  128. self->topstack = NULL;
  129. }
  130. return 0;
  131. }
  132. /*
  133. Pop the current stack/context/textbuffer, returing the stack.
  134. */
  135. static PyObject*
  136. Tokenizer_pop(Tokenizer* self)
  137. {
  138. if (Tokenizer_push_textbuffer(self))
  139. return NULL;
  140. PyObject* stack = Tokenizer_STACK(self);
  141. Py_INCREF(stack);
  142. if (Tokenizer_delete_top_of_stack(self))
  143. return NULL;
  144. return stack;
  145. }
  146. /*
  147. Pop the current stack/context/textbuffer, returing the stack. We will also
  148. replace the underlying stack's context with the current stack's.
  149. */
  150. static PyObject*
  151. Tokenizer_pop_keeping_context(Tokenizer* self)
  152. {
  153. if (Tokenizer_push_textbuffer(self))
  154. return NULL;
  155. PyObject* stack = Tokenizer_STACK(self);
  156. PyObject* context = Tokenizer_CONTEXT(self);
  157. Py_INCREF(stack);
  158. Py_INCREF(context);
  159. if (Tokenizer_delete_top_of_stack(self))
  160. return NULL;
  161. if (PyList_SetItem(self->topstack, 1, context))
  162. return NULL;
  163. return stack;
  164. }
  165. /*
  166. Fail the current tokenization route. Discards the current
  167. stack/context/textbuffer and "raises a BAD_ROUTE exception", which is
  168. implemented using longjmp().
  169. */
  170. static void
  171. Tokenizer_fail_route(Tokenizer* self)
  172. {
  173. Tokenizer_pop(self);
  174. longjmp(exception_env, BAD_ROUTE);
  175. }
  176. /*
  177. Write a token to the end of the current token stack.
  178. */
  179. static int
  180. Tokenizer_write(Tokenizer* self, PyObject* token)
  181. {
  182. if (Tokenizer_push_textbuffer(self))
  183. return -1;
  184. if (PyList_Append(Tokenizer_STACK(self), token))
  185. return -1;
  186. return 0;
  187. }
  188. /*
  189. Write a token to the beginning of the current token stack.
  190. */
  191. static int
  192. Tokenizer_write_first(Tokenizer* self, PyObject* token)
  193. {
  194. if (Tokenizer_push_textbuffer(self))
  195. return -1;
  196. if (PyList_Insert(Tokenizer_STACK(self), 0, token))
  197. return -1;
  198. return 0;
  199. }
  200. /*
  201. Write text to the current textbuffer.
  202. */
  203. static int
  204. Tokenizer_write_text(Tokenizer* self, PyObject* text)
  205. {
  206. if (PyList_Append(Tokenizer_TEXTBUFFER(self), text))
  207. return -1;
  208. return 0;
  209. }
  210. /*
  211. Write a series of tokens to the current stack at once.
  212. */
  213. static int
  214. Tokenizer_write_all(Tokenizer* self, PyObject* tokenlist)
  215. {
  216. if (Tokenizer_push_textbuffer(self))
  217. return -1;
  218. PyObject* stack = Tokenizer_STACK(self);
  219. Py_ssize_t size = PySequence_Fast_GET_SIZE(stack);
  220. if (PyList_SetSlice(stack, size, size, tokenlist))
  221. return -1;
  222. return 0;
  223. }
  224. /*
  225. Pop the current stack, write text, and then write the stack.
  226. */
  227. static int
  228. Tokenizer_write_text_then_stack(Tokenizer* self, PyObject* text)
  229. {
  230. PyObject* stack = Tokenizer_pop(self);
  231. if (Tokenizer_write_text(self, text)) {
  232. Py_XDECREF(stack);
  233. return -1;
  234. }
  235. if (stack) {
  236. if (PySequence_Fast_GET_SIZE(stack) > 0) {
  237. if (Tokenizer_write_all(self, stack)) {
  238. Py_DECREF(stack);
  239. return -1;
  240. }
  241. }
  242. Py_DECREF(stack);
  243. }
  244. self->head--;
  245. return 0;
  246. }
  247. /*
  248. Read the value at a relative point in the wikicode, forwards.
  249. */
  250. static PyObject*
  251. Tokenizer_read(Tokenizer* self, Py_ssize_t delta)
  252. {
  253. Py_ssize_t index = self->head + delta;
  254. if (index >= self->length)
  255. return EMPTY;
  256. return PySequence_Fast_GET_ITEM(self->text, index);
  257. }
  258. /*
  259. Read the value at a relative point in the wikicode, backwards.
  260. */
  261. static PyObject*
  262. Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta)
  263. {
  264. if (delta > self->head)
  265. return EMPTY;
  266. Py_ssize_t index = self->head - delta;
  267. return PySequence_Fast_GET_ITEM(self->text, index);
  268. }
  269. /*
  270. Parse a template or argument at the head of the wikicode string.
  271. */
  272. static int
  273. Tokenizer_parse_template_or_argument(Tokenizer* self)
  274. {
  275. self->head += 2;
  276. unsigned int braces = 2, i;
  277. while (Tokenizer_READ(self, 0) == PU "{") {
  278. self->head++;
  279. braces++;
  280. }
  281. Tokenizer_push(self, 0);
  282. while (braces) {
  283. if (braces == 1) {
  284. PyObject* text = PyUnicode_FromString("{");
  285. if (Tokenizer_write_text_then_stack(self, text)) {
  286. Py_XDECREF(text);
  287. return -1;
  288. }
  289. Py_XDECREF(text);
  290. return 0;
  291. }
  292. if (braces == 2) {
  293. if (setjmp(exception_env) == BAD_ROUTE) {
  294. PyObject* text = PyUnicode_FromString("{{");
  295. if (Tokenizer_write_text_then_stack(self, text)) {
  296. Py_XDECREF(text);
  297. return -1;
  298. }
  299. Py_XDECREF(text);
  300. return 0;
  301. } else {
  302. Tokenizer_parse_template(self);
  303. }
  304. break;
  305. }
  306. if (setjmp(exception_env) == BAD_ROUTE) {
  307. if (setjmp(exception_env) == BAD_ROUTE) {
  308. char bracestr[braces];
  309. for (i = 0; i < braces; i++) {
  310. bracestr[i] = *"{";
  311. }
  312. PyObject* text = PyUnicode_FromString(bracestr);
  313. if (Tokenizer_write_text_then_stack(self, text)) {
  314. Py_XDECREF(text);
  315. return -1;
  316. }
  317. Py_XDECREF(text);
  318. return 0;
  319. }
  320. else {
  321. Tokenizer_parse_template(self);
  322. braces -= 2;
  323. }
  324. }
  325. else {
  326. Tokenizer_parse_argument(self);
  327. braces -= 3;
  328. }
  329. if (braces) {
  330. self->head++;
  331. }
  332. }
  333. PyObject* tokenlist = Tokenizer_pop(self);
  334. if (Tokenizer_write_all(self, tokenlist)) {
  335. Py_DECREF(tokenlist);
  336. return -1;
  337. }
  338. Py_DECREF(tokenlist);
  339. return 0;
  340. }
  341. /*
  342. Parse a template at the head of the wikicode string.
  343. */
  344. static int
  345. Tokenizer_parse_template(Tokenizer* self)
  346. {
  347. PyObject *template, *class, *token;
  348. Py_ssize_t reset = self->head;
  349. if (setjmp(exception_env) == BAD_ROUTE) {
  350. self->head = reset;
  351. longjmp(exception_env, BAD_ROUTE);
  352. }
  353. else {
  354. template = Tokenizer_parse(self, LC_TEMPLATE_NAME);
  355. if (!template) return -1;
  356. class = PyObject_GetAttrString(tokens, "TemplateOpen");
  357. if (!class) return -1;
  358. token = PyInstance_New(class, NOARGS, NOKWARGS);
  359. Py_DECREF(class);
  360. if (!token) return -1;
  361. if (Tokenizer_write_first(self, token)) {
  362. Py_DECREF(token);
  363. return -1;
  364. }
  365. Py_DECREF(token);
  366. if (Tokenizer_write_all(self, template)) {
  367. Py_DECREF(template);
  368. return -1;
  369. }
  370. Py_DECREF(template);
  371. class = PyObject_GetAttrString(tokens, "TemplateClose");
  372. if (!class) return -1;
  373. token = PyInstance_New(class, NOARGS, NOKWARGS);
  374. Py_DECREF(class);
  375. if (!token) return -1;
  376. if (Tokenizer_write(self, token)) {
  377. Py_DECREF(token);
  378. return -1;
  379. }
  380. Py_DECREF(token);
  381. }
  382. return 0;
  383. }
  384. /*
  385. Parse an argument at the head of the wikicode string.
  386. */
  387. static int
  388. Tokenizer_parse_argument(Tokenizer* self)
  389. {
  390. PyObject *argument, *class, *token;
  391. Py_ssize_t reset = self->head;
  392. if (setjmp(exception_env) == BAD_ROUTE) {
  393. self->head = reset;
  394. longjmp(exception_env, BAD_ROUTE);
  395. }
  396. else {
  397. argument = Tokenizer_parse(self, LC_ARGUMENT_NAME);
  398. if (!argument) return -1;
  399. class = PyObject_GetAttrString(tokens, "ArgumentOpen");
  400. if (!class) return -1;
  401. token = PyInstance_New(class, NOARGS, NOKWARGS);
  402. Py_DECREF(class);
  403. if (!token) return -1;
  404. if (Tokenizer_write_first(self, token)) {
  405. Py_DECREF(token);
  406. return -1;
  407. }
  408. Py_DECREF(token);
  409. if (Tokenizer_write_all(self, argument)) {
  410. Py_DECREF(argument);
  411. return -1;
  412. }
  413. Py_DECREF(argument);
  414. class = PyObject_GetAttrString(tokens, "ArgumentClose");
  415. if (!class) return -1;
  416. token = PyInstance_New(class, NOARGS, NOKWARGS);
  417. Py_DECREF(class);
  418. if (!token) return -1;
  419. if (Tokenizer_write(self, token)) {
  420. Py_DECREF(token);
  421. return -1;
  422. }
  423. Py_DECREF(token);
  424. }
  425. return 0;
  426. }
  427. /*
  428. Verify that there are no unsafe characters in the current stack. The route
  429. will be failed if the name contains any element of unsafes in it (not
  430. merely at the beginning or end). This is used when parsing a template name
  431. or parameter key, which cannot contain newlines.
  432. */
  433. static int
  434. Tokenizer_verify_safe(Tokenizer* self, const char* unsafes[])
  435. {
  436. if (Tokenizer_push_textbuffer(self))
  437. return -1;
  438. PyObject* stack = Tokenizer_STACK(self);
  439. if (stack) {
  440. PyObject* textlist = PyList_New(0);
  441. if (!textlist) return -1;
  442. PyObject* class = PyObject_GetAttrString(tokens, "Text");
  443. if (!class) {
  444. Py_DECREF(textlist);
  445. return -1;
  446. }
  447. int i;
  448. Py_ssize_t length = PySequence_Fast_GET_SIZE(stack);
  449. PyObject *token, *textdata;
  450. for (i = 0; i < length; i++) {
  451. token = PySequence_Fast_GET_ITEM(stack, i);
  452. switch (PyObject_IsInstance(token, class)) {
  453. case -1:
  454. Py_DECREF(textlist);
  455. Py_DECREF(class);
  456. return -1;
  457. case 0:
  458. break;
  459. case 1:
  460. textdata = PyObject_GetAttrString(token, "text");
  461. if (!textdata) {
  462. Py_DECREF(textlist);
  463. Py_DECREF(class);
  464. return -1;
  465. }
  466. if (PyList_Append(textlist, textdata)) {
  467. Py_DECREF(textlist);
  468. Py_DECREF(class);
  469. Py_DECREF(textdata);
  470. return -1;
  471. }
  472. Py_DECREF(textdata);
  473. }
  474. }
  475. Py_DECREF(class);
  476. PyObject* text = PyUnicode_Join(EMPTY, textlist);
  477. if (!text) {
  478. Py_DECREF(textlist);
  479. return -1;
  480. }
  481. Py_DECREF(textlist);
  482. PyObject* stripped = PyObject_CallMethod(text, "strip", NULL);
  483. if (!stripped) {
  484. Py_DECREF(text);
  485. return -1;
  486. }
  487. Py_DECREF(text);
  488. const char* unsafe_char;
  489. PyObject* unsafe;
  490. i = 0;
  491. while (1) {
  492. unsafe_char = unsafes[i];
  493. if (!unsafe_char) break;
  494. unsafe = PyUnicode_FromString(unsafe_char);
  495. if (!unsafe) {
  496. Py_DECREF(stripped);
  497. return -1;
  498. }
  499. switch (PyUnicode_Contains(stripped, unsafe)) {
  500. case -1:
  501. Py_DECREF(stripped);
  502. Py_DECREF(unsafe);
  503. return -1;
  504. case 0:
  505. break;
  506. case 1:
  507. Py_DECREF(stripped);
  508. Py_DECREF(unsafe);
  509. Tokenizer_fail_route(self);
  510. }
  511. i++;
  512. }
  513. }
  514. return 0;
  515. }
  516. /*
  517. Handle a template parameter at the head of the string.
  518. */
  519. static int
  520. Tokenizer_handle_template_param(Tokenizer* self)
  521. {
  522. }
  523. /*
  524. Handle a template parameter's value at the head of the string.
  525. */
  526. static int
  527. Tokenizer_handle_template_param_value(Tokenizer* self)
  528. {
  529. }
  530. /*
  531. Handle the end of a template at the head of the string.
  532. */
  533. static PyObject*
  534. Tokenizer_handle_template_end(Tokenizer* self)
  535. {
  536. }
  537. /*
  538. Handle the separator between an argument's name and default.
  539. */
  540. static int
  541. Tokenizer_handle_argument_separator(Tokenizer* self)
  542. {
  543. }
  544. /*
  545. Handle the end of an argument at the head of the string.
  546. */
  547. static PyObject*
  548. Tokenizer_handle_argument_end(Tokenizer* self)
  549. {
  550. }
  551. /*
  552. Parse an internal wikilink at the head of the wikicode string.
  553. */
  554. static int
  555. Tokenizer_parse_wikilink(Tokenizer* self)
  556. {
  557. }
  558. /*
  559. Handle the separator between a wikilink's title and its text.
  560. */
  561. static int
  562. Tokenizer_handle_wikilink_separator(Tokenizer* self)
  563. {
  564. }
  565. /*
  566. Handle the end of a wikilink at the head of the string.
  567. */
  568. static PyObject*
  569. Tokenizer_handle_wikilink_end(Tokenizer* self)
  570. {
  571. }
  572. /*
  573. Parse a section heading at the head of the wikicode string.
  574. */
  575. static int
  576. Tokenizer_parse_heading(Tokenizer* self)
  577. {
  578. }
  579. /*
  580. Handle the end of a section heading at the head of the string.
  581. */
  582. static PyObject*
  583. Tokenizer_handle_heading_end(Tokenizer* self)
  584. {
  585. }
  586. /*
  587. Actually parse an HTML entity and ensure that it is valid.
  588. */
  589. static int
  590. Tokenizer_really_parse_entity(Tokenizer* self)
  591. {
  592. }
  593. /*
  594. Parse an HTML entity at the head of the wikicode string.
  595. */
  596. static int
  597. Tokenizer_parse_entity(Tokenizer* self)
  598. {
  599. }
  600. /*
  601. Parse an HTML comment at the head of the wikicode string.
  602. */
  603. static int
  604. Tokenizer_parse_comment(Tokenizer* self)
  605. {
  606. }
  607. /*
  608. Parse the wikicode string, using context for when to stop.
  609. */
  610. static PyObject*
  611. Tokenizer_parse(Tokenizer* self, Py_ssize_t context)
  612. {
  613. Py_ssize_t fail_contexts = LC_TEMPLATE | LC_ARGUMENT | LC_HEADING | LC_COMMENT;
  614. PyObject *this;
  615. Py_UNICODE *this_data, *next, *next_next, *last;
  616. Py_ssize_t this_context;
  617. int is_marker, i;
  618. Tokenizer_push(self, context);
  619. while (1) {
  620. this = Tokenizer_read(self, 0);
  621. this_data = PyUnicode_AS_UNICODE(this);
  622. is_marker = 0;
  623. for (i = 0; i < NUM_MARKERS; i++) {
  624. if (MARKERS[i] == this_data) {
  625. is_marker = 1;
  626. break;
  627. }
  628. }
  629. if (!is_marker) {
  630. Tokenizer_write_text(self, this);
  631. self->head++;
  632. continue;
  633. }
  634. this_context = Tokenizer_CONTEXT_VAL(self);
  635. if (this == EMPTY) {
  636. if (this_context & fail_contexts) {
  637. Tokenizer_fail_route(self);
  638. }
  639. return Tokenizer_pop(self);
  640. }
  641. next = Tokenizer_READ(self, 1);
  642. if (this_context & LC_COMMENT) {
  643. if (this_data == next && next == PU "-") {
  644. if (Tokenizer_READ(self, 2) == PU ">") {
  645. return Tokenizer_pop(self);
  646. }
  647. }
  648. Tokenizer_write_text(self, this);
  649. }
  650. else if (this_data == next && next == PU "{") {
  651. Tokenizer_parse_template_or_argument(self);
  652. }
  653. else if (this_data == PU "|" && this_context & LC_TEMPLATE) {
  654. Tokenizer_handle_template_param(self);
  655. }
  656. else if (this_data == PU "=" && this_context & LC_TEMPLATE_PARAM_KEY) {
  657. Tokenizer_handle_template_param_value(self);
  658. }
  659. else if (this_data == next && next == PU "}" && this_context & LC_TEMPLATE) {
  660. Tokenizer_handle_template_end(self);
  661. }
  662. else if (this_data == PU "|" && this_context & LC_ARGUMENT_NAME) {
  663. Tokenizer_handle_argument_separator(self);
  664. }
  665. else if (this_data == next && next == PU "}" && this_context & LC_ARGUMENT) {
  666. if (Tokenizer_READ(self, 2) == PU "}") {
  667. return Tokenizer_handle_argument_end(self);
  668. }
  669. Tokenizer_write_text(self, this);
  670. }
  671. else if (this_data == next && next == PU "[") {
  672. if (!(this_context & LC_WIKILINK_TITLE)) {
  673. Tokenizer_parse_wikilink(self);
  674. }
  675. else {
  676. Tokenizer_write_text(self, this);
  677. }
  678. }
  679. else if (this_data == PU "|" && this_context & LC_WIKILINK_TITLE) {
  680. Tokenizer_handle_wikilink_separator(self);
  681. }
  682. else if (this_data == next && next == PU "]" && this_context & LC_WIKILINK) {
  683. return Tokenizer_handle_wikilink_end(self);
  684. }
  685. else if (this_data == PU "=" && !(self->global & GL_HEADING)) {
  686. last = PyUnicode_AS_UNICODE(Tokenizer_read_backwards(self, 1));
  687. if (last == PU "\n" || last == PU "") {
  688. Tokenizer_parse_heading(self);
  689. }
  690. else {
  691. Tokenizer_write_text(self, this);
  692. }
  693. }
  694. else if (this_data == PU "=" && this_context & LC_HEADING) {
  695. return Tokenizer_handle_heading_end(self);
  696. }
  697. else if (this_data == PU "\n" && this_context & LC_HEADING) {
  698. Tokenizer_fail_route(self);
  699. }
  700. else if (this_data == PU "&") {
  701. Tokenizer_parse_entity(self);
  702. }
  703. else if (this_data == PU "<" && next == PU "!") {
  704. next_next = Tokenizer_READ(self, 2);
  705. if (next_next == Tokenizer_READ(self, 3) && next_next == PU "-") {
  706. Tokenizer_parse_comment(self);
  707. }
  708. else {
  709. Tokenizer_write_text(self, this);
  710. }
  711. }
  712. else {
  713. Tokenizer_write_text(self, this);
  714. }
  715. self->head++;
  716. }
  717. }
  718. /*
  719. Build a list of tokens from a string of wikicode and return it.
  720. */
  721. static PyObject*
  722. Tokenizer_tokenize(Tokenizer* self, PyObject *args)
  723. {
  724. PyObject* text;
  725. if (!PyArg_ParseTuple(args, "U", &text)) {
  726. /* Failed to parse a Unicode object; try a string instead. */
  727. PyErr_Clear();
  728. const char* encoded;
  729. Py_ssize_t size;
  730. if (!PyArg_ParseTuple(args, "s#", &encoded, &size)) {
  731. return NULL;
  732. }
  733. PyObject* temp;
  734. temp = PyUnicode_FromStringAndSize(encoded, size);
  735. if (!text)
  736. return NULL;
  737. Py_XDECREF(self->text);
  738. text = PySequence_Fast(temp, "expected a sequence");
  739. Py_XDECREF(temp);
  740. self->text = text;
  741. }
  742. else {
  743. Py_XDECREF(self->text);
  744. self->text = PySequence_Fast(text, "expected a sequence");
  745. }
  746. self->length = PySequence_Length(self->text);
  747. return Tokenizer_parse(self, 0);
  748. }
  749. PyMODINIT_FUNC
  750. init_tokenizer(void)
  751. {
  752. PyObject* module;
  753. TokenizerType.tp_new = PyType_GenericNew;
  754. if (PyType_Ready(&TokenizerType) < 0)
  755. return;
  756. module = Py_InitModule("_tokenizer", module_methods);
  757. Py_INCREF(&TokenizerType);
  758. PyModule_AddObject(module, "CTokenizer", (PyObject*) &TokenizerType);
  759. EMPTY = PyUnicode_FromString("");
  760. NOARGS = PyTuple_New(0);
  761. NOKWARGS = PyDict_New();
  762. PyObject* globals = PyEval_GetGlobals();
  763. PyObject* locals = PyEval_GetLocals();
  764. PyObject* fromlist = PyList_New(0);
  765. tokens = PyImport_ImportModuleLevel("tokens", globals, locals, fromlist, 1);
  766. Py_DECREF(fromlist);
  767. }