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.
 
 
 
 

126 lines
3.8 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. #pragma once
  20. #ifndef PY_SSIZE_T_CLEAN
  21. #define PY_SSIZE_T_CLEAN // See: https://docs.python.org/2/c-api/arg.html
  22. #endif
  23. #include <Python.h>
  24. #include <structmember.h>
  25. #include <bytesobject.h>
  26. /* Compatibility macros */
  27. #if PY_MAJOR_VERSION >= 3
  28. #define IS_PY3K
  29. #endif
  30. #ifndef uint64_t
  31. #define uint64_t unsigned PY_LONG_LONG
  32. #endif
  33. #define malloc PyObject_Malloc // XXX: yuck
  34. #define realloc PyObject_Realloc
  35. #define free PyObject_Free
  36. /* Unicode support macros */
  37. #if defined(IS_PY3K) && PY_MINOR_VERSION >= 3
  38. #define PEP_393
  39. #endif
  40. #ifdef PEP_393
  41. #define Unicode Py_UCS4
  42. #define PyUnicode_FROM_SINGLE(chr) \
  43. PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &(chr), 1)
  44. #else
  45. #define Unicode Py_UNICODE
  46. #define PyUnicode_FROM_SINGLE(chr) \
  47. PyUnicode_FromUnicode(&(chr), 1)
  48. #define PyUnicode_GET_LENGTH PyUnicode_GET_SIZE
  49. #endif
  50. /* Error handling macros */
  51. #define BAD_ROUTE self->route_state
  52. #define BAD_ROUTE_CONTEXT self->route_context
  53. #define FAIL_ROUTE(context) { \
  54. self->route_state = 1; \
  55. self->route_context = context; \
  56. }
  57. #define RESET_ROUTE() self->route_state = 0
  58. /* Shared globals */
  59. extern char** entitydefs;
  60. extern PyObject* NOARGS;
  61. extern PyObject* definitions;
  62. /* Structs */
  63. typedef struct {
  64. Py_ssize_t capacity;
  65. Py_ssize_t length;
  66. #ifdef PEP_393
  67. PyObject* object;
  68. int kind;
  69. void* data;
  70. #else
  71. Py_UNICODE* data;
  72. #endif
  73. } Textbuffer;
  74. struct Stack {
  75. PyObject* stack;
  76. uint64_t context;
  77. Textbuffer* textbuffer;
  78. struct Stack* next;
  79. };
  80. typedef struct Stack Stack;
  81. typedef struct {
  82. PyObject* object; /* base PyUnicodeObject object */
  83. Py_ssize_t length; /* length of object, in code points */
  84. #ifdef PEP_393
  85. int kind; /* object's kind value */
  86. void* data; /* object's raw unicode buffer */
  87. #else
  88. Py_UNICODE* buf; /* object's internal buffer */
  89. #endif
  90. } TokenizerInput;
  91. typedef struct {
  92. PyObject_HEAD
  93. TokenizerInput text; /* text to tokenize */
  94. Stack* topstack; /* topmost stack */
  95. Py_ssize_t head; /* current position in text */
  96. int global; /* global context */
  97. int depth; /* stack recursion depth */
  98. int cycles; /* total number of stack recursions */
  99. int route_state; /* whether a BadRoute has been triggered */
  100. uint64_t route_context; /* context when the last BadRoute was triggered */
  101. int skip_style_tags; /* temp fix for the sometimes broken tag parser */
  102. } Tokenizer;