A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

375 rader
9.7 KiB

  1. /*
  2. Copyright (C) 2012-2015 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();
  35. if (!top->textbuffer)
  36. return -1;
  37. top->next = self->topstack;
  38. self->topstack = top;
  39. self->depth++;
  40. self->cycles++;
  41. return 0;
  42. }
  43. /*
  44. Push the textbuffer onto the stack as a Text node and clear it.
  45. */
  46. int Tokenizer_push_textbuffer(Tokenizer* self)
  47. {
  48. PyObject *text, *kwargs, *token;
  49. Textbuffer* buffer = self->topstack->textbuffer;
  50. if (buffer->size == 0 && !buffer->next)
  51. return 0;
  52. text = Textbuffer_render(buffer);
  53. if (!text)
  54. return -1;
  55. kwargs = PyDict_New();
  56. if (!kwargs) {
  57. Py_DECREF(text);
  58. return -1;
  59. }
  60. PyDict_SetItemString(kwargs, "text", text);
  61. Py_DECREF(text);
  62. token = PyObject_Call(Text, NOARGS, kwargs);
  63. Py_DECREF(kwargs);
  64. if (!token)
  65. return -1;
  66. if (PyList_Append(self->topstack->stack, token)) {
  67. Py_DECREF(token);
  68. return -1;
  69. }
  70. Py_DECREF(token);
  71. Textbuffer_dealloc(buffer);
  72. self->topstack->textbuffer = Textbuffer_new();
  73. if (!self->topstack->textbuffer)
  74. return -1;
  75. return 0;
  76. }
  77. /*
  78. Pop and deallocate the top token stack/context/textbuffer.
  79. */
  80. void Tokenizer_delete_top_of_stack(Tokenizer* self)
  81. {
  82. Stack* top = self->topstack;
  83. Py_DECREF(top->stack);
  84. Textbuffer_dealloc(top->textbuffer);
  85. self->topstack = top->next;
  86. free(top);
  87. self->depth--;
  88. }
  89. /*
  90. Pop the current stack/context/textbuffer, returing the stack.
  91. */
  92. PyObject* Tokenizer_pop(Tokenizer* self)
  93. {
  94. PyObject* stack;
  95. if (Tokenizer_push_textbuffer(self))
  96. return NULL;
  97. stack = self->topstack->stack;
  98. Py_INCREF(stack);
  99. Tokenizer_delete_top_of_stack(self);
  100. return stack;
  101. }
  102. /*
  103. Pop the current stack/context/textbuffer, returing the stack. We will also
  104. replace the underlying stack's context with the current stack's.
  105. */
  106. PyObject* Tokenizer_pop_keeping_context(Tokenizer* self)
  107. {
  108. PyObject* stack;
  109. uint64_t context;
  110. if (Tokenizer_push_textbuffer(self))
  111. return NULL;
  112. stack = self->topstack->stack;
  113. Py_INCREF(stack);
  114. context = self->topstack->context;
  115. Tokenizer_delete_top_of_stack(self);
  116. self->topstack->context = context;
  117. return stack;
  118. }
  119. /*
  120. Fail the current tokenization route. Discards the current
  121. stack/context/textbuffer and sets the BAD_ROUTE flag.
  122. */
  123. void* Tokenizer_fail_route(Tokenizer* self)
  124. {
  125. uint64_t context = self->topstack->context;
  126. PyObject* stack = Tokenizer_pop(self);
  127. Py_XDECREF(stack);
  128. FAIL_ROUTE(context);
  129. return NULL;
  130. }
  131. /*
  132. Write a token to the current token stack.
  133. */
  134. int Tokenizer_emit_token(Tokenizer* self, PyObject* token, int first)
  135. {
  136. PyObject* instance;
  137. if (Tokenizer_push_textbuffer(self))
  138. return -1;
  139. instance = PyObject_CallObject(token, NULL);
  140. if (!instance)
  141. return -1;
  142. if (first ? PyList_Insert(self->topstack->stack, 0, instance) :
  143. PyList_Append(self->topstack->stack, instance)) {
  144. Py_DECREF(instance);
  145. return -1;
  146. }
  147. Py_DECREF(instance);
  148. return 0;
  149. }
  150. /*
  151. Write a token to the current token stack, with kwargs. Steals a reference
  152. to kwargs.
  153. */
  154. int Tokenizer_emit_token_kwargs(Tokenizer* self, PyObject* token,
  155. PyObject* kwargs, int first)
  156. {
  157. PyObject* instance;
  158. if (Tokenizer_push_textbuffer(self)) {
  159. Py_DECREF(kwargs);
  160. return -1;
  161. }
  162. instance = PyObject_Call(token, NOARGS, kwargs);
  163. if (!instance) {
  164. Py_DECREF(kwargs);
  165. return -1;
  166. }
  167. if (first ? PyList_Insert(self->topstack->stack, 0, instance):
  168. PyList_Append(self->topstack->stack, instance)) {
  169. Py_DECREF(instance);
  170. Py_DECREF(kwargs);
  171. return -1;
  172. }
  173. Py_DECREF(instance);
  174. Py_DECREF(kwargs);
  175. return 0;
  176. }
  177. /*
  178. Write a Unicode codepoint to the current textbuffer.
  179. */
  180. int Tokenizer_emit_char(Tokenizer* self, Unicode code)
  181. {
  182. return Textbuffer_write(&(self->topstack->textbuffer), code);
  183. }
  184. /*
  185. Write a string of text to the current textbuffer.
  186. */
  187. int Tokenizer_emit_text(Tokenizer* self, const char* text)
  188. {
  189. int i = 0;
  190. while (text[i]) {
  191. if (Tokenizer_emit_char(self, text[i]))
  192. return -1;
  193. i++;
  194. }
  195. return 0;
  196. }
  197. /*
  198. Write the contents of another textbuffer to the current textbuffer,
  199. deallocating it in the process.
  200. */
  201. int
  202. Tokenizer_emit_textbuffer(Tokenizer* self, Textbuffer* buffer, int reverse)
  203. {
  204. Textbuffer *original = buffer;
  205. Py_ssize_t i;
  206. if (reverse) {
  207. do {
  208. for (i = buffer->size - 1; i >= 0; i--) {
  209. if (Tokenizer_emit_char(self, buffer->data[i])) {
  210. Textbuffer_dealloc(original);
  211. return -1;
  212. }
  213. }
  214. } while ((buffer = buffer->next));
  215. }
  216. else {
  217. while (buffer->next)
  218. buffer = buffer->next;
  219. do {
  220. for (i = 0; i < buffer->size; i++) {
  221. if (Tokenizer_emit_char(self, buffer->data[i])) {
  222. Textbuffer_dealloc(original);
  223. return -1;
  224. }
  225. }
  226. } while ((buffer = buffer->prev));
  227. }
  228. Textbuffer_dealloc(original);
  229. return 0;
  230. }
  231. /*
  232. Write a series of tokens to the current stack at once.
  233. */
  234. int Tokenizer_emit_all(Tokenizer* self, PyObject* tokenlist)
  235. {
  236. int pushed = 0;
  237. PyObject *stack, *token, *left, *right, *text;
  238. Textbuffer* buffer;
  239. Py_ssize_t size;
  240. if (PyList_GET_SIZE(tokenlist) > 0) {
  241. token = PyList_GET_ITEM(tokenlist, 0);
  242. switch (PyObject_IsInstance(token, Text)) {
  243. case 0:
  244. break;
  245. case 1: {
  246. pushed = 1;
  247. buffer = self->topstack->textbuffer;
  248. if (buffer->size == 0 && !buffer->next)
  249. break;
  250. left = Textbuffer_render(buffer);
  251. if (!left)
  252. return -1;
  253. right = PyObject_GetAttrString(token, "text");
  254. if (!right)
  255. return -1;
  256. text = PyUnicode_Concat(left, right);
  257. Py_DECREF(left);
  258. Py_DECREF(right);
  259. if (!text)
  260. return -1;
  261. if (PyObject_SetAttrString(token, "text", text)) {
  262. Py_DECREF(text);
  263. return -1;
  264. }
  265. Py_DECREF(text);
  266. Textbuffer_dealloc(buffer);
  267. self->topstack->textbuffer = Textbuffer_new();
  268. if (!self->topstack->textbuffer)
  269. return -1;
  270. break;
  271. }
  272. case -1:
  273. return -1;
  274. }
  275. }
  276. if (!pushed) {
  277. if (Tokenizer_push_textbuffer(self))
  278. return -1;
  279. }
  280. stack = self->topstack->stack;
  281. size = PyList_GET_SIZE(stack);
  282. if (PyList_SetSlice(stack, size, size, tokenlist))
  283. return -1;
  284. return 0;
  285. }
  286. /*
  287. Pop the current stack, write text, and then write the stack. 'text' is a
  288. NULL-terminated array of chars.
  289. */
  290. int Tokenizer_emit_text_then_stack(Tokenizer* self, const char* text)
  291. {
  292. PyObject* stack = Tokenizer_pop(self);
  293. if (Tokenizer_emit_text(self, text)) {
  294. Py_DECREF(stack);
  295. return -1;
  296. }
  297. if (stack) {
  298. if (PyList_GET_SIZE(stack) > 0) {
  299. if (Tokenizer_emit_all(self, stack)) {
  300. Py_DECREF(stack);
  301. return -1;
  302. }
  303. }
  304. Py_DECREF(stack);
  305. }
  306. self->head--;
  307. return 0;
  308. }
  309. /*
  310. Internal function to read the codepoint at the given index from the input.
  311. */
  312. static Unicode read_codepoint(TokenizerInput* text, Py_ssize_t index)
  313. {
  314. #ifdef PEP_393
  315. return PyUnicode_READ(text->kind, text->data, index);
  316. #else
  317. return text->buf[index];
  318. #endif
  319. }
  320. /*
  321. Read the value at a relative point in the wikicode, forwards.
  322. */
  323. Unicode Tokenizer_read(Tokenizer* self, Py_ssize_t delta)
  324. {
  325. Py_ssize_t index = self->head + delta;
  326. if (index >= self->text.length)
  327. return EMPTY;
  328. return read_codepoint(&self->text, index);
  329. }
  330. /*
  331. Read the value at a relative point in the wikicode, backwards.
  332. */
  333. Unicode Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta)
  334. {
  335. Py_ssize_t index;
  336. if (delta > self->head)
  337. return EMPTY;
  338. index = self->head - delta;
  339. return read_codepoint(&self->text, index);
  340. }