_constants.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #
  2. # Secret Labs' Regular Expression Engine
  3. #
  4. # various symbols used by the regular expression engine.
  5. # run this script to update the _sre include files!
  6. #
  7. # Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
  8. #
  9. # See the __init__.py file for information on usage and redistribution.
  10. #
  11. """Internal support module for sre"""
  12. # update when constants are added or removed
  13. MAGIC = 20221023
  14. from _sre import MAXREPEAT, MAXGROUPS
  15. # SRE standard exception (access as sre.error)
  16. # should this really be here?
  17. class error(Exception):
  18. """Exception raised for invalid regular expressions.
  19. Attributes:
  20. msg: The unformatted error message
  21. pattern: The regular expression pattern
  22. pos: The index in the pattern where compilation failed (may be None)
  23. lineno: The line corresponding to pos (may be None)
  24. colno: The column corresponding to pos (may be None)
  25. """
  26. __module__ = 're'
  27. def __init__(self, msg, pattern=None, pos=None):
  28. self.msg = msg
  29. self.pattern = pattern
  30. self.pos = pos
  31. if pattern is not None and pos is not None:
  32. msg = '%s at position %d' % (msg, pos)
  33. if isinstance(pattern, str):
  34. newline = '\n'
  35. else:
  36. newline = b'\n'
  37. self.lineno = pattern.count(newline, 0, pos) + 1
  38. self.colno = pos - pattern.rfind(newline, 0, pos)
  39. if newline in pattern:
  40. msg = '%s (line %d, column %d)' % (msg, self.lineno, self.colno)
  41. else:
  42. self.lineno = self.colno = None
  43. super().__init__(msg)
  44. class _NamedIntConstant(int):
  45. def __new__(cls, value, name):
  46. self = super(_NamedIntConstant, cls).__new__(cls, value)
  47. self.name = name
  48. return self
  49. def __repr__(self):
  50. return self.name
  51. __reduce__ = None
  52. MAXREPEAT = _NamedIntConstant(MAXREPEAT, 'MAXREPEAT')
  53. def _makecodes(*names):
  54. items = [_NamedIntConstant(i, name) for i, name in enumerate(names)]
  55. globals().update({item.name: item for item in items})
  56. return items
  57. # operators
  58. OPCODES = _makecodes(
  59. # failure=0 success=1 (just because it looks better that way :-)
  60. 'FAILURE', 'SUCCESS',
  61. 'ANY', 'ANY_ALL',
  62. 'ASSERT', 'ASSERT_NOT',
  63. 'AT',
  64. 'BRANCH',
  65. 'CATEGORY',
  66. 'CHARSET', 'BIGCHARSET',
  67. 'GROUPREF', 'GROUPREF_EXISTS',
  68. 'IN',
  69. 'INFO',
  70. 'JUMP',
  71. 'LITERAL',
  72. 'MARK',
  73. 'MAX_UNTIL',
  74. 'MIN_UNTIL',
  75. 'NOT_LITERAL',
  76. 'NEGATE',
  77. 'RANGE',
  78. 'REPEAT',
  79. 'REPEAT_ONE',
  80. 'SUBPATTERN',
  81. 'MIN_REPEAT_ONE',
  82. 'ATOMIC_GROUP',
  83. 'POSSESSIVE_REPEAT',
  84. 'POSSESSIVE_REPEAT_ONE',
  85. 'GROUPREF_IGNORE',
  86. 'IN_IGNORE',
  87. 'LITERAL_IGNORE',
  88. 'NOT_LITERAL_IGNORE',
  89. 'GROUPREF_LOC_IGNORE',
  90. 'IN_LOC_IGNORE',
  91. 'LITERAL_LOC_IGNORE',
  92. 'NOT_LITERAL_LOC_IGNORE',
  93. 'GROUPREF_UNI_IGNORE',
  94. 'IN_UNI_IGNORE',
  95. 'LITERAL_UNI_IGNORE',
  96. 'NOT_LITERAL_UNI_IGNORE',
  97. 'RANGE_UNI_IGNORE',
  98. # The following opcodes are only occurred in the parser output,
  99. # but not in the compiled code.
  100. 'MIN_REPEAT', 'MAX_REPEAT',
  101. )
  102. del OPCODES[-2:] # remove MIN_REPEAT and MAX_REPEAT
  103. # positions
  104. ATCODES = _makecodes(
  105. 'AT_BEGINNING', 'AT_BEGINNING_LINE', 'AT_BEGINNING_STRING',
  106. 'AT_BOUNDARY', 'AT_NON_BOUNDARY',
  107. 'AT_END', 'AT_END_LINE', 'AT_END_STRING',
  108. 'AT_LOC_BOUNDARY', 'AT_LOC_NON_BOUNDARY',
  109. 'AT_UNI_BOUNDARY', 'AT_UNI_NON_BOUNDARY',
  110. )
  111. # categories
  112. CHCODES = _makecodes(
  113. 'CATEGORY_DIGIT', 'CATEGORY_NOT_DIGIT',
  114. 'CATEGORY_SPACE', 'CATEGORY_NOT_SPACE',
  115. 'CATEGORY_WORD', 'CATEGORY_NOT_WORD',
  116. 'CATEGORY_LINEBREAK', 'CATEGORY_NOT_LINEBREAK',
  117. 'CATEGORY_LOC_WORD', 'CATEGORY_LOC_NOT_WORD',
  118. 'CATEGORY_UNI_DIGIT', 'CATEGORY_UNI_NOT_DIGIT',
  119. 'CATEGORY_UNI_SPACE', 'CATEGORY_UNI_NOT_SPACE',
  120. 'CATEGORY_UNI_WORD', 'CATEGORY_UNI_NOT_WORD',
  121. 'CATEGORY_UNI_LINEBREAK', 'CATEGORY_UNI_NOT_LINEBREAK',
  122. )
  123. # replacement operations for "ignore case" mode
  124. OP_IGNORE = {
  125. LITERAL: LITERAL_IGNORE,
  126. NOT_LITERAL: NOT_LITERAL_IGNORE,
  127. }
  128. OP_LOCALE_IGNORE = {
  129. LITERAL: LITERAL_LOC_IGNORE,
  130. NOT_LITERAL: NOT_LITERAL_LOC_IGNORE,
  131. }
  132. OP_UNICODE_IGNORE = {
  133. LITERAL: LITERAL_UNI_IGNORE,
  134. NOT_LITERAL: NOT_LITERAL_UNI_IGNORE,
  135. }
  136. AT_MULTILINE = {
  137. AT_BEGINNING: AT_BEGINNING_LINE,
  138. AT_END: AT_END_LINE
  139. }
  140. AT_LOCALE = {
  141. AT_BOUNDARY: AT_LOC_BOUNDARY,
  142. AT_NON_BOUNDARY: AT_LOC_NON_BOUNDARY
  143. }
  144. AT_UNICODE = {
  145. AT_BOUNDARY: AT_UNI_BOUNDARY,
  146. AT_NON_BOUNDARY: AT_UNI_NON_BOUNDARY
  147. }
  148. CH_LOCALE = {
  149. CATEGORY_DIGIT: CATEGORY_DIGIT,
  150. CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT,
  151. CATEGORY_SPACE: CATEGORY_SPACE,
  152. CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE,
  153. CATEGORY_WORD: CATEGORY_LOC_WORD,
  154. CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD,
  155. CATEGORY_LINEBREAK: CATEGORY_LINEBREAK,
  156. CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK
  157. }
  158. CH_UNICODE = {
  159. CATEGORY_DIGIT: CATEGORY_UNI_DIGIT,
  160. CATEGORY_NOT_DIGIT: CATEGORY_UNI_NOT_DIGIT,
  161. CATEGORY_SPACE: CATEGORY_UNI_SPACE,
  162. CATEGORY_NOT_SPACE: CATEGORY_UNI_NOT_SPACE,
  163. CATEGORY_WORD: CATEGORY_UNI_WORD,
  164. CATEGORY_NOT_WORD: CATEGORY_UNI_NOT_WORD,
  165. CATEGORY_LINEBREAK: CATEGORY_UNI_LINEBREAK,
  166. CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK
  167. }
  168. # flags
  169. SRE_FLAG_TEMPLATE = 1 # template mode (unknown purpose, deprecated)
  170. SRE_FLAG_IGNORECASE = 2 # case insensitive
  171. SRE_FLAG_LOCALE = 4 # honour system locale
  172. SRE_FLAG_MULTILINE = 8 # treat target as multiline string
  173. SRE_FLAG_DOTALL = 16 # treat target as a single string
  174. SRE_FLAG_UNICODE = 32 # use unicode "locale"
  175. SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments
  176. SRE_FLAG_DEBUG = 128 # debugging
  177. SRE_FLAG_ASCII = 256 # use ascii "locale"
  178. # flags for INFO primitive
  179. SRE_INFO_PREFIX = 1 # has prefix
  180. SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
  181. SRE_INFO_CHARSET = 4 # pattern starts with character from given set