A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

203 行
9.2 KiB

  1. /*
  2. Tokenizer Header File 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. #ifndef PY_SSIZE_T_CLEAN
  21. #define PY_SSIZE_T_CLEAN
  22. #endif
  23. #include <Python.h>
  24. #include <setjmp.h>
  25. #include <structmember.h>
  26. #define PU (Py_UNICODE*)
  27. static const Py_UNICODE* MARKERS[] = {
  28. PU "{", PU "}", PU "[", PU "]", PU "<", PU ">", PU "|", PU "=", PU "&",
  29. PU "#", PU "*", PU ";", PU ":", PU "/", PU "-", PU "!", PU "\n", PU ""};
  30. static const int NUM_MARKERS = 17;
  31. static jmp_buf exception_env;
  32. static const int BAD_ROUTE = 1;
  33. static PyObject* EMPTY;
  34. static PyObject* NOARGS;
  35. static PyObject* NOKWARGS;
  36. static PyObject* tokens;
  37. /* Local contexts: */
  38. static const Py_ssize_t LC_TEMPLATE = 0x0007;
  39. static const Py_ssize_t LC_TEMPLATE_NAME = 0x0001;
  40. static const Py_ssize_t LC_TEMPLATE_PARAM_KEY = 0x0002;
  41. static const Py_ssize_t LC_TEMPLATE_PARAM_VALUE = 0x0004;
  42. static const Py_ssize_t LC_ARGUMENT = 0x0018;
  43. static const Py_ssize_t LC_ARGUMENT_NAME = 0x0008;
  44. static const Py_ssize_t LC_ARGUMENT_DEFAULT = 0x0010;
  45. static const Py_ssize_t LC_WIKILINK = 0x0060;
  46. static const Py_ssize_t LC_WIKILINK_TITLE = 0x0020;
  47. static const Py_ssize_t LC_WIKILINK_TEXT = 0x0040;
  48. static const Py_ssize_t LC_HEADING = 0x1f80;
  49. static const Py_ssize_t LC_HEADING_LEVEL_1 = 0x0080;
  50. static const Py_ssize_t LC_HEADING_LEVEL_2 = 0x0100;
  51. static const Py_ssize_t LC_HEADING_LEVEL_3 = 0x0200;
  52. static const Py_ssize_t LC_HEADING_LEVEL_4 = 0x0400;
  53. static const Py_ssize_t LC_HEADING_LEVEL_5 = 0x0800;
  54. static const Py_ssize_t LC_HEADING_LEVEL_6 = 0x1000;
  55. static const Py_ssize_t LC_COMMENT = 0x2000;
  56. /* Global contexts: */
  57. static const Py_ssize_t GL_HEADING = 0x1;
  58. /* Tokenizer object definition: */
  59. typedef struct {
  60. PyObject_HEAD
  61. PyObject* text; /* text to tokenize */
  62. PyObject* stacks; /* token stacks */
  63. PyObject* topstack; /* topmost stack */
  64. Py_ssize_t head; /* current position in text */
  65. Py_ssize_t length; /* length of text */
  66. Py_ssize_t global; /* global context */
  67. } Tokenizer;
  68. /* Macros for accessing Tokenizer data: */
  69. #define Tokenizer_STACK(self) PySequence_Fast_GET_ITEM(self->topstack, 0)
  70. #define Tokenizer_CONTEXT(self) PySequence_Fast_GET_ITEM(self->topstack, 1)
  71. #define Tokenizer_CONTEXT_VAL(self) PyInt_AsSsize_t(Tokenizer_CONTEXT(self))
  72. #define Tokenizer_TEXTBUFFER(self) PySequence_Fast_GET_ITEM(self->topstack, 2)
  73. #define Tokenizer_READ(self, num) PyUnicode_AS_UNICODE(Tokenizer_read(self, num))
  74. /* Tokenizer function prototypes: */
  75. static PyObject* Tokenizer_new(PyTypeObject* type, PyObject* args, PyObject* kwds);
  76. static void Tokenizer_dealloc(Tokenizer* self);
  77. static int Tokenizer_init(Tokenizer* self, PyObject* args, PyObject* kwds);
  78. static int Tokenizer_set_context(Tokenizer* self, Py_ssize_t value);
  79. static int Tokenizer_set_textbuffer(Tokenizer* self, PyObject* value);
  80. static int Tokenizer_push(Tokenizer* self, Py_ssize_t context);
  81. static int Tokenizer_push_textbuffer(Tokenizer* self);
  82. static int Tokenizer_delete_top_of_stack(Tokenizer* self);
  83. static PyObject* Tokenizer_pop(Tokenizer* self);
  84. static PyObject* Tokenizer_pop_keeping_context(Tokenizer* self);
  85. static void Tokenizer_fail_route(Tokenizer* self);
  86. static int Tokenizer_write(Tokenizer* self, PyObject* token);
  87. static int Tokenizer_write_first(Tokenizer* self, PyObject* token);
  88. static int Tokenizer_write_text(Tokenizer* self, PyObject* text);
  89. static int Tokenizer_write_all(Tokenizer* self, PyObject* tokenlist);
  90. static int Tokenizer_write_text_then_stack(Tokenizer* self, PyObject* text);
  91. static PyObject* Tokenizer_read(Tokenizer* self, Py_ssize_t delta);
  92. static PyObject* Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta);
  93. static int Tokenizer_parse_template_or_argument(Tokenizer* self);
  94. static int Tokenizer_parse_template(Tokenizer* self);
  95. static int Tokenizer_parse_argument(Tokenizer* self);
  96. static int Tokenizer_verify_safe(Tokenizer* self, const char* unsafes[]);
  97. static int Tokenizer_handle_template_param(Tokenizer* self);
  98. static int Tokenizer_handle_template_param_value(Tokenizer* self);
  99. static PyObject* Tokenizer_handle_template_end(Tokenizer* self);
  100. static int Tokenizer_handle_argument_separator(Tokenizer* self);
  101. static PyObject* Tokenizer_handle_argument_end(Tokenizer* self);
  102. static int Tokenizer_parse_wikilink(Tokenizer* self);
  103. static int Tokenizer_handle_wikilink_separator(Tokenizer* self);
  104. static PyObject* Tokenizer_handle_wikilink_end(Tokenizer* self);
  105. static int Tokenizer_parse_heading(Tokenizer* self);
  106. static PyObject* Tokenizer_handle_heading_end(Tokenizer* self);
  107. static int Tokenizer_really_parse_entity(Tokenizer* self);
  108. static int Tokenizer_parse_entity(Tokenizer* self);
  109. static int Tokenizer_parse_comment(Tokenizer* self);
  110. static PyObject* Tokenizer_parse(Tokenizer* self, Py_ssize_t context);
  111. static PyObject* Tokenizer_tokenize(Tokenizer* self, PyObject *args);
  112. /* More structs for creating the Tokenizer type: */
  113. static PyMethodDef
  114. Tokenizer_methods[] = {
  115. {"tokenize", (PyCFunction) Tokenizer_tokenize, METH_VARARGS,
  116. "Build a list of tokens from a string of wikicode and return it."},
  117. {NULL}
  118. };
  119. static PyMemberDef
  120. Tokenizer_members[] = {
  121. {NULL}
  122. };
  123. static PyMethodDef
  124. module_methods[] = {
  125. {NULL}
  126. };
  127. static PyTypeObject
  128. TokenizerType = {
  129. PyObject_HEAD_INIT(NULL)
  130. 0, /* ob_size */
  131. "_tokenizer.CTokenizer", /* tp_name */
  132. sizeof(Tokenizer), /* tp_basicsize */
  133. 0, /* tp_itemsize */
  134. (destructor) Tokenizer_dealloc, /* tp_dealloc */
  135. 0, /* tp_print */
  136. 0, /* tp_getattr */
  137. 0, /* tp_setattr */
  138. 0, /* tp_compare */
  139. 0, /* tp_repr */
  140. 0, /* tp_as_number */
  141. 0, /* tp_as_sequence */
  142. 0, /* tp_as_mapping */
  143. 0, /* tp_hash */
  144. 0, /* tp_call */
  145. 0, /* tp_str */
  146. 0, /* tp_getattro */
  147. 0, /* tp_setattro */
  148. 0, /* tp_as_buffer */
  149. Py_TPFLAGS_DEFAULT, /* tp_flags */
  150. "Creates a list of tokens from a string of wikicode.", /* tp_doc */
  151. 0, /* tp_traverse */
  152. 0, /* tp_clear */
  153. 0, /* tp_richcompare */
  154. 0, /* tp_weaklistoffset */
  155. 0, /* tp_iter */
  156. 0, /* tp_iternext */
  157. Tokenizer_methods, /* tp_methods */
  158. Tokenizer_members, /* tp_members */
  159. 0, /* tp_getset */
  160. 0, /* tp_base */
  161. 0, /* tp_dict */
  162. 0, /* tp_descr_get */
  163. 0, /* tp_descr_set */
  164. 0, /* tp_dictoffset */
  165. (initproc) Tokenizer_init, /* tp_init */
  166. 0, /* tp_alloc */
  167. Tokenizer_new, /* tp_new */
  168. };