reconvert.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #! /usr/bin/env python
  2. r"""Convert old ("regex") regular expressions to new syntax ("re").
  3. When imported as a module, there are two functions, with their own
  4. strings:
  5. convert(s, syntax=None) -- convert a regex regular expression to re syntax
  6. quote(s) -- return a quoted string literal
  7. When used as a script, read a Python string literal (or any other
  8. expression evaluating to a string) from stdin, and write the
  9. translated expression to stdout as a string literal. Unless stdout is
  10. a tty, no trailing \n is written to stdout. This is done so that it
  11. can be used with Emacs C-U M-| (shell-command-on-region with argument
  12. which filters the region through the shell command).
  13. No attempt has been made at coding for performance.
  14. Translation table...
  15. \( ( (unless RE_NO_BK_PARENS set)
  16. \) ) (unless RE_NO_BK_PARENS set)
  17. \| | (unless RE_NO_BK_VBAR set)
  18. \< \b (not quite the same, but alla...)
  19. \> \b (not quite the same, but alla...)
  20. \` \A
  21. \' \Z
  22. Not translated...
  23. .
  24. ^
  25. $
  26. *
  27. + (unless RE_BK_PLUS_QM set, then to \+)
  28. ? (unless RE_BK_PLUS_QM set, then to \?)
  29. \
  30. \b
  31. \B
  32. \w
  33. \W
  34. \1 ... \9
  35. Special cases...
  36. Non-printable characters are always replaced by their 3-digit
  37. escape code (except \t, \n, \r, which use mnemonic escapes)
  38. Newline is turned into | when RE_NEWLINE_OR is set
  39. XXX To be done...
  40. [...] (different treatment of backslashed items?)
  41. [^...] (different treatment of backslashed items?)
  42. ^ $ * + ? (in some error contexts these are probably treated differently)
  43. \vDD \DD (in the regex docs but only works when RE_ANSI_HEX set)
  44. """
  45. import warnings
  46. warnings.filterwarnings("ignore", ".* regex .*", DeprecationWarning, __name__,
  47. append=1)
  48. import regex
  49. from regex_syntax import * # RE_*
  50. __all__ = ["convert","quote"]
  51. # Default translation table
  52. mastertable = {
  53. r'\<': r'\b',
  54. r'\>': r'\b',
  55. r'\`': r'\A',
  56. r'\'': r'\Z',
  57. r'\(': '(',
  58. r'\)': ')',
  59. r'\|': '|',
  60. '(': r'\(',
  61. ')': r'\)',
  62. '|': r'\|',
  63. '\t': r'\t',
  64. '\n': r'\n',
  65. '\r': r'\r',
  66. }
  67. def convert(s, syntax=None):
  68. """Convert a regex regular expression to re syntax.
  69. The first argument is the regular expression, as a string object,
  70. just like it would be passed to regex.compile(). (I.e., pass the
  71. actual string object -- string quotes must already have been
  72. removed and the standard escape processing has already been done,
  73. e.g. by eval().)
  74. The optional second argument is the regex syntax variant to be
  75. used. This is an integer mask as passed to regex.set_syntax();
  76. the flag bits are defined in regex_syntax. When not specified, or
  77. when None is given, the current regex syntax mask (as retrieved by
  78. regex.get_syntax()) is used -- which is 0 by default.
  79. The return value is a regular expression, as a string object that
  80. could be passed to re.compile(). (I.e., no string quotes have
  81. been added -- use quote() below, or repr().)
  82. The conversion is not always guaranteed to be correct. More
  83. syntactical analysis should be performed to detect borderline
  84. cases and decide what to do with them. For example, 'x*?' is not
  85. translated correctly.
  86. """
  87. table = mastertable.copy()
  88. if syntax is None:
  89. syntax = regex.get_syntax()
  90. if syntax & RE_NO_BK_PARENS:
  91. del table[r'\('], table[r'\)']
  92. del table['('], table[')']
  93. if syntax & RE_NO_BK_VBAR:
  94. del table[r'\|']
  95. del table['|']
  96. if syntax & RE_BK_PLUS_QM:
  97. table['+'] = r'\+'
  98. table['?'] = r'\?'
  99. table[r'\+'] = '+'
  100. table[r'\?'] = '?'
  101. if syntax & RE_NEWLINE_OR:
  102. table['\n'] = '|'
  103. res = ""
  104. i = 0
  105. end = len(s)
  106. while i < end:
  107. c = s[i]
  108. i = i+1
  109. if c == '\\':
  110. c = s[i]
  111. i = i+1
  112. key = '\\' + c
  113. key = table.get(key, key)
  114. res = res + key
  115. else:
  116. c = table.get(c, c)
  117. res = res + c
  118. return res
  119. def quote(s, quote=None):
  120. """Convert a string object to a quoted string literal.
  121. This is similar to repr() but will return a "raw" string (r'...'
  122. or r"...") when the string contains backslashes, instead of
  123. doubling all backslashes. The resulting string does *not* always
  124. evaluate to the same string as the original; however it will do
  125. just the right thing when passed into re.compile().
  126. The optional second argument forces the string quote; it must be
  127. a single character which is a valid Python string quote.
  128. """
  129. if quote is None:
  130. q = "'"
  131. altq = "'"
  132. if q in s and altq not in s:
  133. q = altq
  134. else:
  135. assert quote in ('"', "'")
  136. q = quote
  137. res = q
  138. for c in s:
  139. if c == q: c = '\\' + c
  140. elif c < ' ' or c > '~': c = "\\%03o" % ord(c)
  141. res = res + c
  142. res = res + q
  143. if '\\' in res:
  144. res = 'r' + res
  145. return res
  146. def main():
  147. """Main program -- called when run as a script."""
  148. import sys
  149. s = eval(sys.stdin.read())
  150. sys.stdout.write(quote(convert(s)))
  151. if sys.stdout.isatty():
  152. sys.stdout.write("\n")
  153. if __name__ == '__main__':
  154. main()