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.
 
 
 
 

398 lines
11 KiB

  1. /*
  2. Copyright (C) 2012-2017 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. Fail the current tokenization route. Discards the current
  134. stack/context/textbuffer and sets the BAD_ROUTE flag. Also records the
  135. ident of the failed stack so future parsing attempts down this route can be
  136. stopped early.
  137. */
  138. void* Tokenizer_fail_route(Tokenizer* self)
  139. {
  140. uint64_t context = self->topstack->context;
  141. route_tree_node *node = malloc(sizeof(route_tree_node));
  142. if (node) {
  143. node->id = self->topstack->ident;
  144. if (avl_tree_insert(&self->bad_routes, &node->node, compare_nodes))
  145. free(node);
  146. }
  147. PyObject* stack = Tokenizer_pop(self);
  148. Py_XDECREF(stack);
  149. FAIL_ROUTE(context);
  150. return NULL;
  151. }
  152. /*
  153. Check if pushing a new route here with the given context would definitely
  154. fail, based on a previous call to Tokenizer_fail_route() with the same
  155. stack.
  156. Return 0 if safe and -1 if unsafe. The BAD_ROUTE flag will be set in the
  157. latter case.
  158. This function is not necessary to call and works as an optimization
  159. implementation detail. (The Python tokenizer checks every route on push,
  160. but this would introduce too much overhead in C tokenizer due to the need
  161. to check for a bad route after every call to Tokenizer_push.)
  162. */
  163. int Tokenizer_check_route(Tokenizer* self, uint64_t context)
  164. {
  165. StackIdent ident = {self->head, context};
  166. struct avl_tree_node *node = (struct avl_tree_node*) (&ident + 1);
  167. if (avl_tree_lookup_node(self->bad_routes, node, compare_nodes)) {
  168. FAIL_ROUTE(context);
  169. return -1;
  170. }
  171. return 0;
  172. }
  173. /*
  174. Write a token to the current token stack.
  175. */
  176. int Tokenizer_emit_token(Tokenizer* self, PyObject* token, int first)
  177. {
  178. PyObject* instance;
  179. if (Tokenizer_push_textbuffer(self))
  180. return -1;
  181. instance = PyObject_CallObject(token, NULL);
  182. if (!instance)
  183. return -1;
  184. if (first ? PyList_Insert(self->topstack->stack, 0, instance) :
  185. PyList_Append(self->topstack->stack, instance)) {
  186. Py_DECREF(instance);
  187. return -1;
  188. }
  189. Py_DECREF(instance);
  190. return 0;
  191. }
  192. /*
  193. Write a token to the current token stack, with kwargs. Steals a reference
  194. to kwargs.
  195. */
  196. int Tokenizer_emit_token_kwargs(Tokenizer* self, PyObject* token,
  197. PyObject* kwargs, int first)
  198. {
  199. PyObject* instance;
  200. if (Tokenizer_push_textbuffer(self)) {
  201. Py_DECREF(kwargs);
  202. return -1;
  203. }
  204. instance = PyObject_Call(token, NOARGS, kwargs);
  205. if (!instance) {
  206. Py_DECREF(kwargs);
  207. return -1;
  208. }
  209. if (first ? PyList_Insert(self->topstack->stack, 0, instance):
  210. PyList_Append(self->topstack->stack, instance)) {
  211. Py_DECREF(instance);
  212. Py_DECREF(kwargs);
  213. return -1;
  214. }
  215. Py_DECREF(instance);
  216. Py_DECREF(kwargs);
  217. return 0;
  218. }
  219. /*
  220. Write a Unicode codepoint to the current textbuffer.
  221. */
  222. int Tokenizer_emit_char(Tokenizer* self, Unicode code)
  223. {
  224. return Textbuffer_write(self->topstack->textbuffer, code);
  225. }
  226. /*
  227. Write a string of text to the current textbuffer.
  228. */
  229. int Tokenizer_emit_text(Tokenizer* self, const char* text)
  230. {
  231. int i = 0;
  232. while (text[i]) {
  233. if (Tokenizer_emit_char(self, text[i]))
  234. return -1;
  235. i++;
  236. }
  237. return 0;
  238. }
  239. /*
  240. Write the contents of another textbuffer to the current textbuffer,
  241. deallocating it in the process.
  242. */
  243. int Tokenizer_emit_textbuffer(Tokenizer* self, Textbuffer* buffer)
  244. {
  245. int retval = Textbuffer_concat(self->topstack->textbuffer, buffer);
  246. Textbuffer_dealloc(buffer);
  247. return retval;
  248. }
  249. /*
  250. Write a series of tokens to the current stack at once.
  251. */
  252. int Tokenizer_emit_all(Tokenizer* self, PyObject* tokenlist)
  253. {
  254. int pushed = 0;
  255. PyObject *stack, *token, *left, *right, *text;
  256. Textbuffer* buffer;
  257. Py_ssize_t size;
  258. if (PyList_GET_SIZE(tokenlist) > 0) {
  259. token = PyList_GET_ITEM(tokenlist, 0);
  260. switch (PyObject_IsInstance(token, Text)) {
  261. case 0:
  262. break;
  263. case 1: {
  264. pushed = 1;
  265. buffer = self->topstack->textbuffer;
  266. if (buffer->length == 0)
  267. break;
  268. left = Textbuffer_render(buffer);
  269. if (!left)
  270. return -1;
  271. right = PyObject_GetAttrString(token, "text");
  272. if (!right)
  273. return -1;
  274. text = PyUnicode_Concat(left, right);
  275. Py_DECREF(left);
  276. Py_DECREF(right);
  277. if (!text)
  278. return -1;
  279. if (PyObject_SetAttrString(token, "text", text)) {
  280. Py_DECREF(text);
  281. return -1;
  282. }
  283. Py_DECREF(text);
  284. if (Textbuffer_reset(buffer))
  285. return -1;
  286. break;
  287. }
  288. case -1:
  289. return -1;
  290. }
  291. }
  292. if (!pushed) {
  293. if (Tokenizer_push_textbuffer(self))
  294. return -1;
  295. }
  296. stack = self->topstack->stack;
  297. size = PyList_GET_SIZE(stack);
  298. if (PyList_SetSlice(stack, size, size, tokenlist))
  299. return -1;
  300. return 0;
  301. }
  302. /*
  303. Pop the current stack, write text, and then write the stack. 'text' is a
  304. NULL-terminated array of chars.
  305. */
  306. int Tokenizer_emit_text_then_stack(Tokenizer* self, const char* text)
  307. {
  308. PyObject* stack = Tokenizer_pop(self);
  309. if (Tokenizer_emit_text(self, text)) {
  310. Py_DECREF(stack);
  311. return -1;
  312. }
  313. if (stack) {
  314. if (PyList_GET_SIZE(stack) > 0) {
  315. if (Tokenizer_emit_all(self, stack)) {
  316. Py_DECREF(stack);
  317. return -1;
  318. }
  319. }
  320. Py_DECREF(stack);
  321. }
  322. self->head--;
  323. return 0;
  324. }
  325. /*
  326. Internal function to read the codepoint at the given index from the input.
  327. */
  328. static Unicode read_codepoint(TokenizerInput* text, Py_ssize_t index)
  329. {
  330. #ifdef PEP_393
  331. return PyUnicode_READ(text->kind, text->data, index);
  332. #else
  333. return text->buf[index];
  334. #endif
  335. }
  336. /*
  337. Read the value at a relative point in the wikicode, forwards.
  338. */
  339. Unicode Tokenizer_read(Tokenizer* self, Py_ssize_t delta)
  340. {
  341. Py_ssize_t index = self->head + delta;
  342. if (index >= self->text.length)
  343. return '\0';
  344. return read_codepoint(&self->text, index);
  345. }
  346. /*
  347. Read the value at a relative point in the wikicode, backwards.
  348. */
  349. Unicode Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta)
  350. {
  351. Py_ssize_t index;
  352. if (delta > self->head)
  353. return '\0';
  354. index = self->head - delta;
  355. return read_codepoint(&self->text, index);
  356. }