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.
 
 
 
 

118 lines
3.6 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 free PyObject_Free
  35. /* Unicode support macros */
  36. #if defined(IS_PY3K) && PYTHON_MINOR_VERSION >= 3
  37. #define PEP_393
  38. #endif
  39. #ifdef PEP_393
  40. #define Unicode Py_UCS4
  41. #define PyUnicode_FROM_SINGLE(chr) \
  42. PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &(chr), 1)
  43. #else
  44. #define Unicode Py_UNICODE
  45. #define PyUnicode_FROM_SINGLE(chr) \
  46. PyUnicode_FromUnicode(&(chr), 1)
  47. #endif
  48. /* Error handling macros */
  49. #define BAD_ROUTE self->route_state
  50. #define BAD_ROUTE_CONTEXT self->route_context
  51. #define FAIL_ROUTE(context) { \
  52. self->route_state = 1; \
  53. self->route_context = context; \
  54. }
  55. #define RESET_ROUTE() self->route_state = 0
  56. /* Shared globals */
  57. extern char** entitydefs;
  58. extern PyObject* EMPTY;
  59. extern PyObject* NOARGS;
  60. extern PyObject* definitions;
  61. /* Structs */
  62. typedef struct {
  63. Py_ssize_t size;
  64. Unicode* data;
  65. } Textbuffer;
  66. struct Stack {
  67. PyObject* stack;
  68. uint64_t context;
  69. Textbuffer* textbuffer;
  70. struct Stack* next;
  71. };
  72. typedef struct Stack Stack;
  73. typedef struct {
  74. PyObject* object; /* base PyUnicodeObject object */
  75. Py_ssize_t length; /* length of object, in code points */
  76. #ifdef PEP_393
  77. int kind; /* object's kind value */
  78. void* data; /* object's raw unicode buffer */
  79. #else
  80. Py_UNICODE* buf; /* object's internal buffer */
  81. #endif
  82. } TokenizerInput;
  83. typedef struct {
  84. PyObject_HEAD
  85. TokenizerInput text; /* text to tokenize */
  86. Stack* topstack; /* topmost stack */
  87. Py_ssize_t head; /* current position in text */
  88. int global; /* global context */
  89. int depth; /* stack recursion depth */
  90. int cycles; /* total number of stack recursions */
  91. int route_state; /* whether a BadRoute has been triggered */
  92. uint64_t route_context; /* context when the last BadRoute was triggered */
  93. int skip_style_tags; /* temp fix for the sometimes broken tag parser */
  94. } Tokenizer;