A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

125 行
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. """
  23. This module contains various "context" definitions, which are essentially flags
  24. set during the tokenization process, either on the current parse stack (local
  25. contexts) or affecting all stacks (global contexts). They represent the context
  26. the tokenizer is in, such as inside a template's name definition, or inside a
  27. level-two heading. This is used to determine what tokens are valid at the
  28. current point and also if the current parsing route is invalid.
  29. The tokenizer stores context as an integer, with these definitions bitwise OR'd
  30. to set them, AND'd to check if they're set, and XOR'd to unset them. The
  31. advantage of this is that contexts can have sub-contexts (as ``FOO == 0b11``
  32. will cover ``BAR == 0b10`` and ``BAZ == 0b01``).
  33. Local (stack-specific) contexts:
  34. * :py:const:`TEMPLATE`
  35. * :py:const:`TEMPLATE_NAME`
  36. * :py:const:`TEMPLATE_PARAM_KEY`
  37. * :py:const:`TEMPLATE_PARAM_VALUE`
  38. * :py:const:`ARGUMENT`
  39. * :py:const:`ARGUMENT_NAME`
  40. * :py:const:`ARGUMENT_DEFAULT`
  41. * :py:const:`WIKILINK`
  42. * :py:const:`WIKILINK_TITLE`
  43. * :py:const:`WIKILINK_TEXT`
  44. * :py:const:`HEADING`
  45. * :py:const:`HEADING_LEVEL_1`
  46. * :py:const:`HEADING_LEVEL_2`
  47. * :py:const:`HEADING_LEVEL_3`
  48. * :py:const:`HEADING_LEVEL_4`
  49. * :py:const:`HEADING_LEVEL_5`
  50. * :py:const:`HEADING_LEVEL_6`
  51. * :py:const:`COMMENT`
  52. * :py:const:`TAG`
  53. * :py:const:`TAG_OPEN`
  54. * :py:const:`TAG_OPEN_NAME`
  55. * :py:const:`TAG_OPEN_ATTR`
  56. * :py:const:`TAG_OPEN_ATTR_NAME`
  57. * :py:const:`TAG_OPEN_ATTR_BODY`
  58. * :py:const:`TAG_OPEN_ATTR_QUOTED`
  59. * :py:const:`TAG_OPEN_ATTR_IGNORE`
  60. * :py:const:`TAG_BODY`
  61. * :py:const:`TAG_CLOSE`
  62. Global contexts:
  63. * :py:const:`GL_HEADING`
  64. """
  65. # Local contexts:
  66. TEMPLATE = 0b000000000000000000111
  67. TEMPLATE_NAME = 0b000000000000000000001
  68. TEMPLATE_PARAM_KEY = 0b000000000000000000010
  69. TEMPLATE_PARAM_VALUE = 0b000000000000000000100
  70. ARGUMENT = 0b000000000000000011000
  71. ARGUMENT_NAME = 0b000000000000000001000
  72. ARGUMENT_DEFAULT = 0b000000000000000010000
  73. WIKILINK = 0b000000000000001100000
  74. WIKILINK_TITLE = 0b000000000000000100000
  75. WIKILINK_TEXT = 0b000000000000001000000
  76. HEADING = 0b000000001111110000000
  77. HEADING_LEVEL_1 = 0b000000000000010000000
  78. HEADING_LEVEL_2 = 0b000000000000100000000
  79. HEADING_LEVEL_3 = 0b000000000001000000000
  80. HEADING_LEVEL_4 = 0b000000000010000000000
  81. HEADING_LEVEL_5 = 0b000000000100000000000
  82. HEADING_LEVEL_6 = 0b000000001000000000000
  83. COMMENT = 0b000000010000000000000
  84. TAG = 0b111111100000000000000
  85. TAG_OPEN = 0b001111100000000000000
  86. TAG_OPEN_NAME = 0b000000100000000000000
  87. TAG_OPEN_ATTR = 0b001111000000000000000
  88. TAG_OPEN_ATTR_NAME = 0b000001000000000000000
  89. TAG_OPEN_ATTR_BODY = 0b000010000000000000000
  90. TAG_OPEN_ATTR_QUOTED = 0b000100000000000000000
  91. TAG_OPEN_ATTR_IGNORE = 0b001000000000000000000
  92. TAG_BODY = 0b010000000000000000000
  93. TAG_CLOSE = 0b100000000000000000000
  94. # Global contexts:
  95. GL_HEADING = 0b1