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.
 
 
 
 

316 lines
11 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. #include <bytesobject.h>
  27. #if PY_MAJOR_VERSION >= 3
  28. #define IS_PY3K
  29. #endif
  30. #define malloc PyObject_Malloc
  31. #define free PyObject_Free
  32. #define DIGITS "0123456789"
  33. #define HEXDIGITS "0123456789abcdefABCDEF"
  34. #define ALPHANUM "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  35. static const char* MARKERS[] = {
  36. "{", "}", "[", "]", "<", ">", "|", "=", "&", "'", "#", "*", ";", ":", "/",
  37. "-", "\n", ""};
  38. #define NUM_MARKERS 18
  39. #define TEXTBUFFER_BLOCKSIZE 1024
  40. #define MAX_DEPTH 40
  41. #define MAX_CYCLES 100000
  42. #define MAX_BRACES 255
  43. #define MAX_ENTITY_SIZE 8
  44. static int route_state = 0, route_context = 0;
  45. #define BAD_ROUTE route_state
  46. #define BAD_ROUTE_CONTEXT route_context
  47. #define FAIL_ROUTE(context) route_state = 1; route_context = context
  48. #define RESET_ROUTE() route_state = 0
  49. static char** entitydefs;
  50. static PyObject* EMPTY;
  51. static PyObject* NOARGS;
  52. static PyObject* tag_defs;
  53. /* Tokens: */
  54. static PyObject* Text;
  55. static PyObject* TemplateOpen;
  56. static PyObject* TemplateParamSeparator;
  57. static PyObject* TemplateParamEquals;
  58. static PyObject* TemplateClose;
  59. static PyObject* ArgumentOpen;
  60. static PyObject* ArgumentSeparator;
  61. static PyObject* ArgumentClose;
  62. static PyObject* WikilinkOpen;
  63. static PyObject* WikilinkSeparator;
  64. static PyObject* WikilinkClose;
  65. static PyObject* HTMLEntityStart;
  66. static PyObject* HTMLEntityNumeric;
  67. static PyObject* HTMLEntityHex;
  68. static PyObject* HTMLEntityEnd;
  69. static PyObject* HeadingStart;
  70. static PyObject* HeadingEnd;
  71. static PyObject* CommentStart;
  72. static PyObject* CommentEnd;
  73. static PyObject* TagOpenOpen;
  74. static PyObject* TagAttrStart;
  75. static PyObject* TagAttrEquals;
  76. static PyObject* TagAttrQuote;
  77. static PyObject* TagCloseOpen;
  78. static PyObject* TagCloseSelfclose;
  79. static PyObject* TagOpenClose;
  80. static PyObject* TagCloseClose;
  81. /* Local contexts: */
  82. #define LC_TEMPLATE 0x0000007
  83. #define LC_TEMPLATE_NAME 0x0000001
  84. #define LC_TEMPLATE_PARAM_KEY 0x0000002
  85. #define LC_TEMPLATE_PARAM_VALUE 0x0000004
  86. #define LC_ARGUMENT 0x0000018
  87. #define LC_ARGUMENT_NAME 0x0000008
  88. #define LC_ARGUMENT_DEFAULT 0x0000010
  89. #define LC_WIKILINK 0x0000060
  90. #define LC_WIKILINK_TITLE 0x0000020
  91. #define LC_WIKILINK_TEXT 0x0000040
  92. #define LC_HEADING 0x0001F80
  93. #define LC_HEADING_LEVEL_1 0x0000080
  94. #define LC_HEADING_LEVEL_2 0x0000100
  95. #define LC_HEADING_LEVEL_3 0x0000200
  96. #define LC_HEADING_LEVEL_4 0x0000400
  97. #define LC_HEADING_LEVEL_5 0x0000800
  98. #define LC_HEADING_LEVEL_6 0x0001000
  99. #define LC_TAG 0x001E000
  100. #define LC_TAG_OPEN 0x0002000
  101. #define LC_TAG_ATTR 0x0004000
  102. #define LC_TAG_BODY 0x0008000
  103. #define LC_TAG_CLOSE 0x0010000
  104. #define LC_STYLE 0x01E0000
  105. #define LC_STYLE_ITALICS 0x0020000
  106. #define LC_STYLE_BOLD 0x0040000
  107. #define LC_STYLE_PASS_AGAIN 0x0080000
  108. #define LC_STYLE_SECOND_PASS 0x0100000
  109. #define LC_DLTERM 0x0200000
  110. #define LC_SAFETY_CHECK 0xFC00000
  111. #define LC_HAS_TEXT 0x0400000
  112. #define LC_FAIL_ON_TEXT 0x0800000
  113. #define LC_FAIL_NEXT 0x1000000
  114. #define LC_FAIL_ON_LBRACE 0x2000000
  115. #define LC_FAIL_ON_RBRACE 0x4000000
  116. #define LC_FAIL_ON_EQUALS 0x8000000
  117. /* Global contexts: */
  118. #define GL_HEADING 0x1
  119. /* Aggregate contexts: */
  120. #define AGG_FAIL (LC_TEMPLATE | LC_ARGUMENT | LC_WIKILINK | LC_HEADING | LC_TAG | LC_STYLE)
  121. #define AGG_UNSAFE (LC_TEMPLATE_NAME | LC_WIKILINK_TITLE | LC_TEMPLATE_PARAM_KEY | LC_ARGUMENT_NAME)
  122. #define AGG_DOUBLE (LC_TEMPLATE_PARAM_KEY | LC_TAG_CLOSE)
  123. /* Tag contexts: */
  124. #define TAG_NAME 0x01
  125. #define TAG_ATTR_READY 0x02
  126. #define TAG_ATTR_NAME 0x04
  127. #define TAG_ATTR_VALUE 0x08
  128. #define TAG_QUOTED 0x10
  129. #define TAG_NOTE_SPACE 0x20
  130. #define TAG_NOTE_EQUALS 0x40
  131. #define TAG_NOTE_QUOTE 0x80
  132. /* Miscellaneous structs: */
  133. struct Textbuffer {
  134. Py_ssize_t size;
  135. Py_UNICODE* data;
  136. struct Textbuffer* next;
  137. };
  138. struct Stack {
  139. PyObject* stack;
  140. int context;
  141. struct Textbuffer* textbuffer;
  142. struct Stack* next;
  143. };
  144. typedef struct {
  145. PyObject* title;
  146. int level;
  147. } HeadingData;
  148. typedef struct {
  149. int context;
  150. struct Textbuffer* pad_first;
  151. struct Textbuffer* pad_before_eq;
  152. struct Textbuffer* pad_after_eq;
  153. Py_ssize_t reset;
  154. } TagData;
  155. typedef struct Textbuffer Textbuffer;
  156. typedef struct Stack Stack;
  157. /* Tokenizer object definition: */
  158. typedef struct {
  159. PyObject_HEAD
  160. PyObject* text; /* text to tokenize */
  161. Stack* topstack; /* topmost stack */
  162. Py_ssize_t head; /* current position in text */
  163. Py_ssize_t length; /* length of text */
  164. int global; /* global context */
  165. int depth; /* stack recursion depth */
  166. int cycles; /* total number of stack recursions */
  167. } Tokenizer;
  168. /* Macros related to Tokenizer functions: */
  169. #define Tokenizer_READ(self, delta) (*PyUnicode_AS_UNICODE(Tokenizer_read(self, delta)))
  170. #define Tokenizer_READ_BACKWARDS(self, delta) \
  171. (*PyUnicode_AS_UNICODE(Tokenizer_read_backwards(self, delta)))
  172. #define Tokenizer_CAN_RECURSE(self) (self->depth < MAX_DEPTH && self->cycles < MAX_CYCLES)
  173. #define Tokenizer_emit(self, token) Tokenizer_emit_token(self, token, 0)
  174. #define Tokenizer_emit_first(self, token) Tokenizer_emit_token(self, token, 1)
  175. #define Tokenizer_emit_kwargs(self, token, kwargs) Tokenizer_emit_token_kwargs(self, token, kwargs, 0)
  176. #define Tokenizer_emit_first_kwargs(self, token, kwargs) Tokenizer_emit_token_kwargs(self, token, kwargs, 1)
  177. /* Macros for accessing HTML tag definitions: */
  178. #define GET_HTML_TAG(markup) (markup == *":" ? "dd" : markup == *";" ? "dt" : "li")
  179. #define IS_PARSABLE(tag) (call_tag_def_func("is_parsable", tag))
  180. #define IS_SINGLE(tag) (call_tag_def_func("is_single", tag))
  181. #define IS_SINGLE_ONLY(tag) (call_tag_def_func("is_single_only", tag))
  182. /* Function prototypes: */
  183. static Textbuffer* Textbuffer_new(void);
  184. static void Textbuffer_dealloc(Textbuffer*);
  185. static TagData* TagData_new(void);
  186. static void TagData_dealloc(TagData*);
  187. static PyObject* Tokenizer_new(PyTypeObject*, PyObject*, PyObject*);
  188. static void Tokenizer_dealloc(Tokenizer*);
  189. static int Tokenizer_init(Tokenizer*, PyObject*, PyObject*);
  190. static int Tokenizer_parse_tag(Tokenizer*);
  191. static PyObject* Tokenizer_parse(Tokenizer*, int, int);
  192. static PyObject* Tokenizer_tokenize(Tokenizer*, PyObject*);
  193. /* More structs for creating the Tokenizer type: */
  194. static PyMethodDef Tokenizer_methods[] = {
  195. {"tokenize", (PyCFunction) Tokenizer_tokenize, METH_VARARGS,
  196. "Build a list of tokens from a string of wikicode and return it."},
  197. {NULL}
  198. };
  199. static PyMemberDef Tokenizer_members[] = {
  200. {NULL}
  201. };
  202. static PyTypeObject TokenizerType = {
  203. PyVarObject_HEAD_INIT(NULL, 0)
  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. };
  242. #ifdef IS_PY3K
  243. static PyModuleDef module_def = {
  244. PyModuleDef_HEAD_INIT,
  245. "_tokenizer",
  246. "Creates a list of tokens from a string of wikicode.",
  247. -1, NULL, NULL, NULL, NULL, NULL
  248. };
  249. #endif