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

306 行
10 KiB

  1. /*
  2. Tokenizer Header File for MWParserFromHell
  3. Copyright (C) 2012-2013 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. #if PY_MAJOR_VERSION >= 3
  27. #define IS_PY3K
  28. #endif
  29. #define malloc PyObject_Malloc
  30. #define free PyObject_Free
  31. #define DIGITS "0123456789"
  32. #define HEXDIGITS "0123456789abcdefABCDEF"
  33. #define ALPHANUM "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  34. static const char* MARKERS[] = {
  35. "{", "}", "[", "]", "<", ">", "|", "=", "&", "'", "#", "*", ";", ":", "/",
  36. "-", "\n", ""};
  37. #define NUM_MARKERS 18
  38. #define TEXTBUFFER_BLOCKSIZE 1024
  39. #define MAX_DEPTH 40
  40. #define MAX_CYCLES 100000
  41. #define MAX_BRACES 255
  42. #define MAX_ENTITY_SIZE 8
  43. static int route_state = 0, route_context = 0;
  44. #define BAD_ROUTE route_state
  45. #define BAD_ROUTE_CONTEXT route_context
  46. #define FAIL_ROUTE(context) route_state = 1; route_context = context
  47. #define RESET_ROUTE() route_state = 0
  48. static char** entitydefs;
  49. static PyObject* EMPTY;
  50. static PyObject* NOARGS;
  51. static PyObject* tag_defs;
  52. /* Tokens: */
  53. static PyObject* Text;
  54. static PyObject* TemplateOpen;
  55. static PyObject* TemplateParamSeparator;
  56. static PyObject* TemplateParamEquals;
  57. static PyObject* TemplateClose;
  58. static PyObject* ArgumentOpen;
  59. static PyObject* ArgumentSeparator;
  60. static PyObject* ArgumentClose;
  61. static PyObject* WikilinkOpen;
  62. static PyObject* WikilinkSeparator;
  63. static PyObject* WikilinkClose;
  64. static PyObject* HTMLEntityStart;
  65. static PyObject* HTMLEntityNumeric;
  66. static PyObject* HTMLEntityHex;
  67. static PyObject* HTMLEntityEnd;
  68. static PyObject* HeadingStart;
  69. static PyObject* HeadingEnd;
  70. static PyObject* CommentStart;
  71. static PyObject* CommentEnd;
  72. static PyObject* TagOpenOpen;
  73. static PyObject* TagAttrStart;
  74. static PyObject* TagAttrEquals;
  75. static PyObject* TagAttrQuote;
  76. static PyObject* TagCloseOpen;
  77. static PyObject* TagCloseSelfclose;
  78. static PyObject* TagOpenClose;
  79. static PyObject* TagCloseClose;
  80. /* Local contexts: */
  81. #define LC_TEMPLATE 0x00000007
  82. #define LC_TEMPLATE_NAME 0x00000001
  83. #define LC_TEMPLATE_PARAM_KEY 0x00000002
  84. #define LC_TEMPLATE_PARAM_VALUE 0x00000004
  85. #define LC_ARGUMENT 0x00000018
  86. #define LC_ARGUMENT_NAME 0x00000008
  87. #define LC_ARGUMENT_DEFAULT 0x00000010
  88. #define LC_WIKILINK 0x00000060
  89. #define LC_WIKILINK_TITLE 0x00000020
  90. #define LC_WIKILINK_TEXT 0x00000040
  91. #define LC_HEADING 0x00001F80
  92. #define LC_HEADING_LEVEL_1 0x00000080
  93. #define LC_HEADING_LEVEL_2 0x00000100
  94. #define LC_HEADING_LEVEL_3 0x00000200
  95. #define LC_HEADING_LEVEL_4 0x00000400
  96. #define LC_HEADING_LEVEL_5 0x00000800
  97. #define LC_HEADING_LEVEL_6 0x00001000
  98. #define LC_COMMENT 0x00002000
  99. #define LC_TAG 0x0003C000
  100. #define LC_TAG_OPEN 0x00004000
  101. #define LC_TAG_ATTR 0x00008000
  102. #define LC_TAG_BODY 0x00010000
  103. #define LC_TAG_CLOSE 0x00020000
  104. #define LC_STYLE 0x003C0000
  105. #define LC_STYLE_ITALICS 0x00040000
  106. #define LC_STYLE_BOLD 0x00080000
  107. #define LC_STYLE_PASS_AGAIN 0x00100000
  108. #define LC_STYLE_SECOND_PASS 0x00200000
  109. #define LC_DLTERM 0x00400000
  110. #define LC_SAFETY_CHECK 0x1F800000
  111. #define LC_HAS_TEXT 0x00800000
  112. #define LC_FAIL_ON_TEXT 0x01000000
  113. #define LC_FAIL_NEXT 0x02000000
  114. #define LC_FAIL_ON_LBRACE 0x04000000
  115. #define LC_FAIL_ON_RBRACE 0x08000000
  116. #define LC_FAIL_ON_EQUALS 0x10000000
  117. /* Global contexts: */
  118. #define GL_HEADING 0x1
  119. /* Tag contexts: */
  120. #define TAG_NAME 0x01
  121. #define TAG_ATTR_READY 0x02
  122. #define TAG_ATTR_NAME 0x04
  123. #define TAG_ATTR_VALUE 0x08
  124. #define TAG_QUOTED 0x10
  125. #define TAG_NOTE_SPACE 0x20
  126. #define TAG_NOTE_EQUALS 0x40
  127. #define TAG_NOTE_QUOTE 0x80
  128. /* Miscellaneous structs: */
  129. struct Textbuffer {
  130. Py_ssize_t size;
  131. Py_UNICODE* data;
  132. struct Textbuffer* next;
  133. };
  134. struct Stack {
  135. PyObject* stack;
  136. int context;
  137. struct Textbuffer* textbuffer;
  138. struct Stack* next;
  139. };
  140. typedef struct {
  141. PyObject* title;
  142. int level;
  143. } HeadingData;
  144. typedef struct {
  145. int context;
  146. struct Textbuffer* pad_first;
  147. struct Textbuffer* pad_before_eq;
  148. struct Textbuffer* pad_after_eq;
  149. Py_ssize_t reset;
  150. } TagData;
  151. typedef struct Textbuffer Textbuffer;
  152. typedef struct Stack Stack;
  153. /* Tokenizer object definition: */
  154. typedef struct {
  155. PyObject_HEAD
  156. PyObject* text; /* text to tokenize */
  157. Stack* topstack; /* topmost stack */
  158. Py_ssize_t head; /* current position in text */
  159. Py_ssize_t length; /* length of text */
  160. int global; /* global context */
  161. int depth; /* stack recursion depth */
  162. int cycles; /* total number of stack recursions */
  163. } Tokenizer;
  164. /* Macros for accessing Tokenizer data: */
  165. #define Tokenizer_READ(self, delta) (*PyUnicode_AS_UNICODE(Tokenizer_read(self, delta)))
  166. #define Tokenizer_READ_BACKWARDS(self, delta) \
  167. (*PyUnicode_AS_UNICODE(Tokenizer_read_backwards(self, delta)))
  168. #define Tokenizer_CAN_RECURSE(self) (self->depth < MAX_DEPTH && self->cycles < MAX_CYCLES)
  169. /* Macros for accessing HTML tag definitions: */
  170. #define GET_HTML_TAG(markup) (markup == *":" ? "dd" : markup == *";" ? "dt" : "li")
  171. #define IS_PARSABLE(tag) (call_tag_def_func("is_parsable", tag))
  172. #define IS_SINGLE(tag) (call_tag_def_func("is_single", tag))
  173. #define IS_SINGLE_ONLY(tag) (call_tag_def_func("is_single_only", tag))
  174. /* Function prototypes: */
  175. static Textbuffer* Textbuffer_new(void);
  176. static void Textbuffer_dealloc(Textbuffer*);
  177. static TagData* TagData_new(void);
  178. static void TagData_dealloc(TagData*);
  179. static PyObject* Tokenizer_new(PyTypeObject*, PyObject*, PyObject*);
  180. static void Tokenizer_dealloc(Tokenizer*);
  181. static int Tokenizer_init(Tokenizer*, PyObject*, PyObject*);
  182. static int Tokenizer_parse_tag(Tokenizer*);
  183. static PyObject* Tokenizer_parse(Tokenizer*, int, int);
  184. static PyObject* Tokenizer_tokenize(Tokenizer*, PyObject*);
  185. /* More structs for creating the Tokenizer type: */
  186. static PyMethodDef
  187. Tokenizer_methods[] = {
  188. {"tokenize", (PyCFunction) Tokenizer_tokenize, METH_VARARGS,
  189. "Build a list of tokens from a string of wikicode and return it."},
  190. {NULL}
  191. };
  192. static PyMemberDef
  193. Tokenizer_members[] = {
  194. {NULL}
  195. };
  196. static PyMethodDef
  197. module_methods[] = {
  198. {NULL}
  199. };
  200. static PyTypeObject
  201. TokenizerType = {
  202. PyObject_HEAD_INIT(NULL)
  203. 0, /* ob_size */
  204. "_tokenizer.CTokenizer", /* tp_name */
  205. sizeof(Tokenizer), /* tp_basicsize */
  206. 0, /* tp_itemsize */
  207. (destructor) Tokenizer_dealloc, /* tp_dealloc */
  208. 0, /* tp_print */
  209. 0, /* tp_getattr */
  210. 0, /* tp_setattr */
  211. 0, /* tp_compare */
  212. 0, /* tp_repr */
  213. 0, /* tp_as_number */
  214. 0, /* tp_as_sequence */
  215. 0, /* tp_as_mapping */
  216. 0, /* tp_hash */
  217. 0, /* tp_call */
  218. 0, /* tp_str */
  219. 0, /* tp_getattro */
  220. 0, /* tp_setattro */
  221. 0, /* tp_as_buffer */
  222. Py_TPFLAGS_DEFAULT, /* tp_flags */
  223. "Creates a list of tokens from a string of wikicode.", /* tp_doc */
  224. 0, /* tp_traverse */
  225. 0, /* tp_clear */
  226. 0, /* tp_richcompare */
  227. 0, /* tp_weaklistoffset */
  228. 0, /* tp_iter */
  229. 0, /* tp_iternext */
  230. Tokenizer_methods, /* tp_methods */
  231. Tokenizer_members, /* tp_members */
  232. 0, /* tp_getset */
  233. 0, /* tp_base */
  234. 0, /* tp_dict */
  235. 0, /* tp_descr_get */
  236. 0, /* tp_descr_set */
  237. 0, /* tp_dictoffset */
  238. (initproc) Tokenizer_init, /* tp_init */
  239. 0, /* tp_alloc */
  240. Tokenizer_new, /* tp_new */
  241. };