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.
 
 
 
 

421 lines
11 KiB

  1. /*
  2. Copyright (C) 2012-2018 Ben Kurtovic <ben.kurtovic@gmail.com>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. this software and associated documentation files (the "Software"), to deal in
  5. the Software without restriction, including without limitation the rights to
  6. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  7. of the Software, and to permit persons to whom the Software is furnished to do
  8. so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. #include "tok_support.h"
  20. #include "textbuffer.h"
  21. #include "tokens.h"
  22. /*
  23. Add a new token stack, context, and textbuffer to the list.
  24. */
  25. int Tokenizer_push(Tokenizer* self, uint64_t context)
  26. {
  27. Stack* top = malloc(sizeof(Stack));
  28. if (!top) {
  29. PyErr_NoMemory();
  30. return -1;
  31. }
  32. top->stack = PyList_New(0);
  33. top->context = context;
  34. top->textbuffer = Textbuffer_new(&self->text);
  35. if (!top->textbuffer)
  36. return -1;
  37. top->ident.head = self->head;
  38. top->ident.context = context;
  39. top->next = self->topstack;
  40. self->topstack = top;
  41. self->depth++;
  42. return 0;
  43. }
  44. /*
  45. Push the textbuffer onto the stack as a Text node and clear it.
  46. */
  47. int Tokenizer_push_textbuffer(Tokenizer* self)
  48. {
  49. PyObject *text, *kwargs, *token;
  50. Textbuffer* buffer = self->topstack->textbuffer;
  51. if (buffer->length == 0)
  52. return 0;
  53. text = Textbuffer_render(buffer);
  54. if (!text)
  55. return -1;
  56. kwargs = PyDict_New();
  57. if (!kwargs) {
  58. Py_DECREF(text);
  59. return -1;
  60. }
  61. PyDict_SetItemString(kwargs, "text", text);
  62. Py_DECREF(text);
  63. token = PyObject_Call(Text, NOARGS, kwargs);
  64. Py_DECREF(kwargs);
  65. if (!token)
  66. return -1;
  67. if (PyList_Append(self->topstack->stack, token)) {
  68. Py_DECREF(token);
  69. return -1;
  70. }
  71. Py_DECREF(token);
  72. if (Textbuffer_reset(buffer))
  73. return -1;
  74. return 0;
  75. }
  76. /*
  77. Pop and deallocate the top token stack/context/textbuffer.
  78. */
  79. void Tokenizer_delete_top_of_stack(Tokenizer* self)
  80. {
  81. Stack* top = self->topstack;
  82. Py_DECREF(top->stack);
  83. Textbuffer_dealloc(top->textbuffer);
  84. self->topstack = top->next;
  85. free(top);
  86. self->depth--;
  87. }
  88. /*
  89. Pop the current stack/context/textbuffer, returing the stack.
  90. */
  91. PyObject* Tokenizer_pop(Tokenizer* self)
  92. {
  93. PyObject* stack;
  94. if (Tokenizer_push_textbuffer(self))
  95. return NULL;
  96. stack = self->topstack->stack;
  97. Py_INCREF(stack);
  98. Tokenizer_delete_top_of_stack(self);
  99. return stack;
  100. }
  101. /*
  102. Pop the current stack/context/textbuffer, returing the stack. We will also
  103. replace the underlying stack's context with the current stack's.
  104. */
  105. PyObject* Tokenizer_pop_keeping_context(Tokenizer* self)
  106. {
  107. PyObject* stack;
  108. uint64_t context;
  109. if (Tokenizer_push_textbuffer(self))
  110. return NULL;
  111. stack = self->topstack->stack;
  112. Py_INCREF(stack);
  113. context = self->topstack->context;
  114. Tokenizer_delete_top_of_stack(self);
  115. self->topstack->context = context;
  116. return stack;
  117. }
  118. /*
  119. Compare two route_tree_nodes that are in their avl_tree_node forms.
  120. */
  121. static int compare_nodes(
  122. const struct avl_tree_node* na, const struct avl_tree_node* nb)
  123. {
  124. route_tree_node *a = avl_tree_entry(na, route_tree_node, node);
  125. route_tree_node *b = avl_tree_entry(nb, route_tree_node, node);
  126. if (a->id.head < b->id.head)
  127. return -1;
  128. if (a->id.head > b->id.head)
  129. return 1;
  130. return (a->id.context > b->id.context) - (a->id.context < b->id.context);
  131. }
  132. /*
  133. Remember that the current route (head + context at push) is invalid.
  134. This will be noticed when calling Tokenizer_check_route with the same head
  135. and context, and the route will be failed immediately.
  136. */
  137. void Tokenizer_memoize_bad_route(Tokenizer *self)
  138. {
  139. route_tree_node *node = malloc(sizeof(route_tree_node));
  140. if (node) {
  141. node->id = self->topstack->ident;
  142. if (avl_tree_insert(&self->bad_routes, &node->node, compare_nodes))
  143. free(node);
  144. }
  145. }
  146. /*
  147. Fail the current tokenization route. Discards the current
  148. stack/context/textbuffer and sets the BAD_ROUTE flag. Also records the
  149. ident of the failed stack so future parsing attempts down this route can be
  150. stopped early.
  151. */
  152. void* Tokenizer_fail_route(Tokenizer* self)
  153. {
  154. uint64_t context = self->topstack->context;
  155. PyObject* stack;
  156. Tokenizer_memoize_bad_route(self);
  157. stack = Tokenizer_pop(self);
  158. Py_XDECREF(stack);
  159. FAIL_ROUTE(context);
  160. return NULL;
  161. }
  162. /*
  163. Check if pushing a new route here with the given context would definitely
  164. fail, based on a previous call to Tokenizer_fail_route() with the same
  165. stack. (Or any other call to Tokenizer_memoize_bad_route().)
  166. Return 0 if safe and -1 if unsafe. The BAD_ROUTE flag will be set in the
  167. latter case.
  168. This function is not necessary to call and works as an optimization
  169. implementation detail. (The Python tokenizer checks every route on push,
  170. but this would introduce too much overhead in C tokenizer due to the need
  171. to check for a bad route after every call to Tokenizer_push.)
  172. */
  173. int Tokenizer_check_route(Tokenizer* self, uint64_t context)
  174. {
  175. StackIdent ident = {self->head, context};
  176. struct avl_tree_node *node = (struct avl_tree_node*) (&ident + 1);
  177. if (avl_tree_lookup_node(self->bad_routes, node, compare_nodes)) {
  178. FAIL_ROUTE(context);
  179. return -1;
  180. }
  181. return 0;
  182. }
  183. /*
  184. Free the tokenizer's bad route cache tree. Intended to be called by the
  185. main tokenizer function after parsing is finished.
  186. */
  187. void Tokenizer_free_bad_route_tree(Tokenizer *self)
  188. {
  189. struct avl_tree_node *cur = avl_tree_first_in_postorder(self->bad_routes);
  190. struct avl_tree_node *parent;
  191. while (cur) {
  192. route_tree_node *node = avl_tree_entry(cur, route_tree_node, node);
  193. parent = avl_get_parent(cur);
  194. free(node);
  195. cur = avl_tree_next_in_postorder(cur, parent);
  196. }
  197. self->bad_routes = NULL;
  198. }
  199. /*
  200. Write a token to the current token stack.
  201. */
  202. int Tokenizer_emit_token(Tokenizer* self, PyObject* token, int first)
  203. {
  204. PyObject* instance;
  205. if (Tokenizer_push_textbuffer(self))
  206. return -1;
  207. instance = PyObject_CallObject(token, NULL);
  208. if (!instance)
  209. return -1;
  210. if (first ? PyList_Insert(self->topstack->stack, 0, instance) :
  211. PyList_Append(self->topstack->stack, instance)) {
  212. Py_DECREF(instance);
  213. return -1;
  214. }
  215. Py_DECREF(instance);
  216. return 0;
  217. }
  218. /*
  219. Write a token to the current token stack, with kwargs. Steals a reference
  220. to kwargs.
  221. */
  222. int Tokenizer_emit_token_kwargs(Tokenizer* self, PyObject* token,
  223. PyObject* kwargs, int first)
  224. {
  225. PyObject* instance;
  226. if (Tokenizer_push_textbuffer(self)) {
  227. Py_DECREF(kwargs);
  228. return -1;
  229. }
  230. instance = PyObject_Call(token, NOARGS, kwargs);
  231. if (!instance) {
  232. Py_DECREF(kwargs);
  233. return -1;
  234. }
  235. if (first ? PyList_Insert(self->topstack->stack, 0, instance):
  236. PyList_Append(self->topstack->stack, instance)) {
  237. Py_DECREF(instance);
  238. Py_DECREF(kwargs);
  239. return -1;
  240. }
  241. Py_DECREF(instance);
  242. Py_DECREF(kwargs);
  243. return 0;
  244. }
  245. /*
  246. Write a Unicode codepoint to the current textbuffer.
  247. */
  248. int Tokenizer_emit_char(Tokenizer* self, Py_UCS4 code)
  249. {
  250. return Textbuffer_write(self->topstack->textbuffer, code);
  251. }
  252. /*
  253. Write a string of text to the current textbuffer.
  254. */
  255. int Tokenizer_emit_text(Tokenizer* self, const char* text)
  256. {
  257. int i = 0;
  258. while (text[i]) {
  259. if (Tokenizer_emit_char(self, text[i]))
  260. return -1;
  261. i++;
  262. }
  263. return 0;
  264. }
  265. /*
  266. Write the contents of another textbuffer to the current textbuffer,
  267. deallocating it in the process.
  268. */
  269. int Tokenizer_emit_textbuffer(Tokenizer* self, Textbuffer* buffer)
  270. {
  271. int retval = Textbuffer_concat(self->topstack->textbuffer, buffer);
  272. Textbuffer_dealloc(buffer);
  273. return retval;
  274. }
  275. /*
  276. Write a series of tokens to the current stack at once.
  277. */
  278. int Tokenizer_emit_all(Tokenizer* self, PyObject* tokenlist)
  279. {
  280. int pushed = 0;
  281. PyObject *stack, *token, *left, *right, *text;
  282. Textbuffer* buffer;
  283. Py_ssize_t size;
  284. if (PyList_GET_SIZE(tokenlist) > 0) {
  285. token = PyList_GET_ITEM(tokenlist, 0);
  286. switch (PyObject_IsInstance(token, Text)) {
  287. case 0:
  288. break;
  289. case 1: {
  290. pushed = 1;
  291. buffer = self->topstack->textbuffer;
  292. if (buffer->length == 0)
  293. break;
  294. left = Textbuffer_render(buffer);
  295. if (!left)
  296. return -1;
  297. right = PyObject_GetAttrString(token, "text");
  298. if (!right)
  299. return -1;
  300. text = PyUnicode_Concat(left, right);
  301. Py_DECREF(left);
  302. Py_DECREF(right);
  303. if (!text)
  304. return -1;
  305. if (PyObject_SetAttrString(token, "text", text)) {
  306. Py_DECREF(text);
  307. return -1;
  308. }
  309. Py_DECREF(text);
  310. if (Textbuffer_reset(buffer))
  311. return -1;
  312. break;
  313. }
  314. case -1:
  315. return -1;
  316. }
  317. }
  318. if (!pushed) {
  319. if (Tokenizer_push_textbuffer(self))
  320. return -1;
  321. }
  322. stack = self->topstack->stack;
  323. size = PyList_GET_SIZE(stack);
  324. if (PyList_SetSlice(stack, size, size, tokenlist))
  325. return -1;
  326. return 0;
  327. }
  328. /*
  329. Pop the current stack, write text, and then write the stack. 'text' is a
  330. NULL-terminated array of chars.
  331. */
  332. int Tokenizer_emit_text_then_stack(Tokenizer* self, const char* text)
  333. {
  334. PyObject* stack = Tokenizer_pop(self);
  335. if (Tokenizer_emit_text(self, text)) {
  336. Py_DECREF(stack);
  337. return -1;
  338. }
  339. if (stack) {
  340. if (PyList_GET_SIZE(stack) > 0) {
  341. if (Tokenizer_emit_all(self, stack)) {
  342. Py_DECREF(stack);
  343. return -1;
  344. }
  345. }
  346. Py_DECREF(stack);
  347. }
  348. self->head--;
  349. return 0;
  350. }
  351. /*
  352. Internal function to read the codepoint at the given index from the input.
  353. */
  354. static Py_UCS4 read_codepoint(TokenizerInput* text, Py_ssize_t index)
  355. {
  356. return PyUnicode_READ(text->kind, text->data, index);
  357. }
  358. /*
  359. Read the value at a relative point in the wikicode, forwards.
  360. */
  361. Py_UCS4 Tokenizer_read(Tokenizer* self, Py_ssize_t delta)
  362. {
  363. Py_ssize_t index = self->head + delta;
  364. if (index >= self->text.length)
  365. return '\0';
  366. return read_codepoint(&self->text, index);
  367. }
  368. /*
  369. Read the value at a relative point in the wikicode, backwards.
  370. */
  371. Py_UCS4 Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta)
  372. {
  373. Py_ssize_t index;
  374. if (delta > self->head)
  375. return '\0';
  376. index = self->head - delta;
  377. return read_codepoint(&self->text, index);
  378. }