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.
 
 
 
 

141 lines
4.0 KiB

  1. /*
  2. Copyright (C) 2012-2017 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. #include "avl_tree.h"
  27. /* Compatibility macros */
  28. #if PY_MAJOR_VERSION >= 3
  29. #define IS_PY3K
  30. #endif
  31. #ifndef uint64_t
  32. #define uint64_t unsigned PY_LONG_LONG
  33. #endif
  34. #define malloc PyObject_Malloc // XXX: yuck
  35. #define realloc PyObject_Realloc
  36. #define free PyObject_Free
  37. /* Unicode support macros */
  38. #if defined(IS_PY3K) && PY_MINOR_VERSION >= 3
  39. #define PEP_393
  40. #endif
  41. #ifdef PEP_393
  42. #define Unicode Py_UCS4
  43. #define PyUnicode_FROM_SINGLE(chr) \
  44. PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &(chr), 1)
  45. #else
  46. #define Unicode Py_UNICODE
  47. #define PyUnicode_FROM_SINGLE(chr) \
  48. PyUnicode_FromUnicode(&(chr), 1)
  49. #define PyUnicode_GET_LENGTH PyUnicode_GET_SIZE
  50. #endif
  51. /* Error handling macros */
  52. #define BAD_ROUTE self->route_state
  53. #define BAD_ROUTE_CONTEXT self->route_context
  54. #define FAIL_ROUTE(context) { \
  55. self->route_state = 1; \
  56. self->route_context = context; \
  57. }
  58. #define RESET_ROUTE() self->route_state = 0
  59. /* Shared globals */
  60. extern char** entitydefs;
  61. extern PyObject* NOARGS;
  62. extern PyObject* definitions;
  63. /* Structs */
  64. typedef struct {
  65. Py_ssize_t capacity;
  66. Py_ssize_t length;
  67. #ifdef PEP_393
  68. PyObject* object;
  69. int kind;
  70. void* data;
  71. #else
  72. Py_UNICODE* data;
  73. #endif
  74. } Textbuffer;
  75. typedef struct {
  76. Py_ssize_t head;
  77. uint64_t context;
  78. } StackIdent;
  79. struct Stack {
  80. PyObject* stack;
  81. uint64_t context;
  82. Textbuffer* textbuffer;
  83. StackIdent ident;
  84. struct Stack* next;
  85. };
  86. typedef struct Stack Stack;
  87. typedef struct {
  88. PyObject* object; /* base PyUnicodeObject object */
  89. Py_ssize_t length; /* length of object, in code points */
  90. #ifdef PEP_393
  91. int kind; /* object's kind value */
  92. void* data; /* object's raw unicode buffer */
  93. #else
  94. Py_UNICODE* buf; /* object's internal buffer */
  95. #endif
  96. } TokenizerInput;
  97. typedef struct avl_tree_node avl_tree;
  98. typedef struct {
  99. StackIdent id;
  100. struct avl_tree_node node;
  101. } route_tree_node;
  102. typedef struct {
  103. PyObject_HEAD
  104. TokenizerInput text; /* text to tokenize */
  105. Stack* topstack; /* topmost stack */
  106. Py_ssize_t head; /* current position in text */
  107. int global; /* global context */
  108. int depth; /* stack recursion depth */
  109. int route_state; /* whether a BadRoute has been triggered */
  110. uint64_t route_context; /* context when the last BadRoute was triggered */
  111. avl_tree* bad_routes; /* stack idents for routes known to fail */
  112. int skip_style_tags; /* temp fix for the sometimes broken tag parser */
  113. } Tokenizer;