sre_constants.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 sre.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 = 20171005
  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. names = names.strip().split()
  55. items = [_NamedIntConstant(i, name) for i, name in enumerate(names)]
  56. globals().update({item.name: item for item in items})
  57. return items
  58. # operators
  59. # failure=0 success=1 (just because it looks better that way :-)
  60. OPCODES = _makecodes("""
  61. FAILURE SUCCESS
  62. ANY ANY_ALL
  63. ASSERT ASSERT_NOT
  64. AT
  65. BRANCH
  66. CALL
  67. CATEGORY
  68. CHARSET BIGCHARSET
  69. GROUPREF GROUPREF_EXISTS
  70. IN
  71. INFO
  72. JUMP
  73. LITERAL
  74. MARK
  75. MAX_UNTIL
  76. MIN_UNTIL
  77. NOT_LITERAL
  78. NEGATE
  79. RANGE
  80. REPEAT
  81. REPEAT_ONE
  82. SUBPATTERN
  83. MIN_REPEAT_ONE
  84. GROUPREF_IGNORE
  85. IN_IGNORE
  86. LITERAL_IGNORE
  87. NOT_LITERAL_IGNORE
  88. GROUPREF_LOC_IGNORE
  89. IN_LOC_IGNORE
  90. LITERAL_LOC_IGNORE
  91. NOT_LITERAL_LOC_IGNORE
  92. GROUPREF_UNI_IGNORE
  93. IN_UNI_IGNORE
  94. LITERAL_UNI_IGNORE
  95. NOT_LITERAL_UNI_IGNORE
  96. RANGE_UNI_IGNORE
  97. MIN_REPEAT MAX_REPEAT
  98. """)
  99. del OPCODES[-2:] # remove MIN_REPEAT and MAX_REPEAT
  100. # positions
  101. ATCODES = _makecodes("""
  102. AT_BEGINNING AT_BEGINNING_LINE AT_BEGINNING_STRING
  103. AT_BOUNDARY AT_NON_BOUNDARY
  104. AT_END AT_END_LINE AT_END_STRING
  105. AT_LOC_BOUNDARY AT_LOC_NON_BOUNDARY
  106. AT_UNI_BOUNDARY AT_UNI_NON_BOUNDARY
  107. """)
  108. # categories
  109. CHCODES = _makecodes("""
  110. CATEGORY_DIGIT CATEGORY_NOT_DIGIT
  111. CATEGORY_SPACE CATEGORY_NOT_SPACE
  112. CATEGORY_WORD CATEGORY_NOT_WORD
  113. CATEGORY_LINEBREAK CATEGORY_NOT_LINEBREAK
  114. CATEGORY_LOC_WORD CATEGORY_LOC_NOT_WORD
  115. CATEGORY_UNI_DIGIT CATEGORY_UNI_NOT_DIGIT
  116. CATEGORY_UNI_SPACE CATEGORY_UNI_NOT_SPACE
  117. CATEGORY_UNI_WORD CATEGORY_UNI_NOT_WORD
  118. CATEGORY_UNI_LINEBREAK CATEGORY_UNI_NOT_LINEBREAK
  119. """)
  120. # replacement operations for "ignore case" mode
  121. OP_IGNORE = {
  122. LITERAL: LITERAL_IGNORE,
  123. NOT_LITERAL: NOT_LITERAL_IGNORE,
  124. }
  125. OP_LOCALE_IGNORE = {
  126. LITERAL: LITERAL_LOC_IGNORE,
  127. NOT_LITERAL: NOT_LITERAL_LOC_IGNORE,
  128. }
  129. OP_UNICODE_IGNORE = {
  130. LITERAL: LITERAL_UNI_IGNORE,
  131. NOT_LITERAL: NOT_LITERAL_UNI_IGNORE,
  132. }
  133. AT_MULTILINE = {
  134. AT_BEGINNING: AT_BEGINNING_LINE,
  135. AT_END: AT_END_LINE
  136. }
  137. AT_LOCALE = {
  138. AT_BOUNDARY: AT_LOC_BOUNDARY,
  139. AT_NON_BOUNDARY: AT_LOC_NON_BOUNDARY
  140. }
  141. AT_UNICODE = {
  142. AT_BOUNDARY: AT_UNI_BOUNDARY,
  143. AT_NON_BOUNDARY: AT_UNI_NON_BOUNDARY
  144. }
  145. CH_LOCALE = {
  146. CATEGORY_DIGIT: CATEGORY_DIGIT,
  147. CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT,
  148. CATEGORY_SPACE: CATEGORY_SPACE,
  149. CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE,
  150. CATEGORY_WORD: CATEGORY_LOC_WORD,
  151. CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD,
  152. CATEGORY_LINEBREAK: CATEGORY_LINEBREAK,
  153. CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK
  154. }
  155. CH_UNICODE = {
  156. CATEGORY_DIGIT: CATEGORY_UNI_DIGIT,
  157. CATEGORY_NOT_DIGIT: CATEGORY_UNI_NOT_DIGIT,
  158. CATEGORY_SPACE: CATEGORY_UNI_SPACE,
  159. CATEGORY_NOT_SPACE: CATEGORY_UNI_NOT_SPACE,
  160. CATEGORY_WORD: CATEGORY_UNI_WORD,
  161. CATEGORY_NOT_WORD: CATEGORY_UNI_NOT_WORD,
  162. CATEGORY_LINEBREAK: CATEGORY_UNI_LINEBREAK,
  163. CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK
  164. }
  165. # flags
  166. SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking)
  167. SRE_FLAG_IGNORECASE = 2 # case insensitive
  168. SRE_FLAG_LOCALE = 4 # honour system locale
  169. SRE_FLAG_MULTILINE = 8 # treat target as multiline string
  170. SRE_FLAG_DOTALL = 16 # treat target as a single string
  171. SRE_FLAG_UNICODE = 32 # use unicode "locale"
  172. SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments
  173. SRE_FLAG_DEBUG = 128 # debugging
  174. SRE_FLAG_ASCII = 256 # use ascii "locale"
  175. # flags for INFO primitive
  176. SRE_INFO_PREFIX = 1 # has prefix
  177. SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
  178. SRE_INFO_CHARSET = 4 # pattern starts with character from given set
  179. if __name__ == "__main__":
  180. def dump(f, d, prefix):
  181. items = sorted(d)
  182. for item in items:
  183. f.write("#define %s_%s %d\n" % (prefix, item, item))
  184. with open("sre_constants.h", "w") as f:
  185. f.write("""\
  186. /*
  187. * Secret Labs' Regular Expression Engine
  188. *
  189. * regular expression matching engine
  190. *
  191. * NOTE: This file is generated by sre_constants.py. If you need
  192. * to change anything in here, edit sre_constants.py and run it.
  193. *
  194. * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
  195. *
  196. * See the _sre.c file for information on usage and redistribution.
  197. */
  198. """)
  199. f.write("#define SRE_MAGIC %d\n" % MAGIC)
  200. dump(f, OPCODES, "SRE_OP")
  201. dump(f, ATCODES, "SRE")
  202. dump(f, CHCODES, "SRE")
  203. f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
  204. f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
  205. f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
  206. f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
  207. f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
  208. f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
  209. f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
  210. f.write("#define SRE_FLAG_DEBUG %d\n" % SRE_FLAG_DEBUG)
  211. f.write("#define SRE_FLAG_ASCII %d\n" % SRE_FLAG_ASCII)
  212. f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
  213. f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
  214. f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
  215. print("done")