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.
 
 
 
 

117 lines
3.5 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/3/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. #ifndef uint64_t
  29. #define uint64_t unsigned PY_LONG_LONG
  30. #endif
  31. #define malloc PyObject_Malloc // XXX: yuck
  32. #define realloc PyObject_Realloc
  33. #define free PyObject_Free
  34. /* Unicode support macros */
  35. #define PyUnicode_FROM_SINGLE(chr) \
  36. PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &(chr), 1)
  37. /* Error handling macros */
  38. #define BAD_ROUTE self->route_state
  39. #define BAD_ROUTE_CONTEXT self->route_context
  40. #define FAIL_ROUTE(context) { \
  41. self->route_state = 1; \
  42. self->route_context = context; \
  43. }
  44. #define RESET_ROUTE() self->route_state = 0
  45. /* Shared globals */
  46. extern char** entitydefs;
  47. extern PyObject* NOARGS;
  48. extern PyObject* definitions;
  49. /* Structs */
  50. typedef struct {
  51. Py_ssize_t capacity;
  52. Py_ssize_t length;
  53. PyObject* object;
  54. int kind;
  55. void* data;
  56. } Textbuffer;
  57. typedef struct {
  58. Py_ssize_t head;
  59. uint64_t context;
  60. } StackIdent;
  61. struct Stack {
  62. PyObject* stack;
  63. uint64_t context;
  64. Textbuffer* textbuffer;
  65. StackIdent ident;
  66. struct Stack* next;
  67. };
  68. typedef struct Stack Stack;
  69. typedef struct {
  70. PyObject* object; /* base PyUnicodeObject object */
  71. Py_ssize_t length; /* length of object, in code points */
  72. int kind; /* object's kind value */
  73. void* data; /* object's raw unicode buffer */
  74. } TokenizerInput;
  75. typedef struct avl_tree_node avl_tree;
  76. typedef struct {
  77. StackIdent id;
  78. struct avl_tree_node node;
  79. } route_tree_node;
  80. typedef struct {
  81. PyObject_HEAD
  82. TokenizerInput text; /* text to tokenize */
  83. Stack* topstack; /* topmost stack */
  84. Py_ssize_t head; /* current position in text */
  85. int global; /* global context */
  86. int depth; /* stack recursion depth */
  87. int route_state; /* whether a BadRoute has been triggered */
  88. uint64_t route_context; /* context when the last BadRoute was triggered */
  89. avl_tree* bad_routes; /* stack idents for routes known to fail */
  90. int skip_style_tags; /* temp fix for the sometimes broken tag parser */
  91. } Tokenizer;