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.
 
 
 
 

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