verifier.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright 2015 Google Inc. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Verify that the generated code is valid code.
  15. This takes a line of code and "normalizes" it. I.e., it transforms the snippet
  16. into something that has the potential to compile.
  17. VerifyCode(): the main function exported by this module.
  18. """
  19. import ast
  20. import re
  21. import sys
  22. import textwrap
  23. class InternalError(Exception):
  24. """Internal error in verifying formatted code."""
  25. pass
  26. def VerifyCode(code):
  27. """Verify that the reformatted code is syntactically correct.
  28. Arguments:
  29. code: (unicode) The reformatted code snippet.
  30. Raises:
  31. SyntaxError if the code was reformatted incorrectly.
  32. """
  33. try:
  34. compile(textwrap.dedent(code).encode('UTF-8'), '<string>', 'exec')
  35. except SyntaxError:
  36. try:
  37. ast.parse(textwrap.dedent(code.lstrip('\n')).lstrip(), '<string>', 'exec')
  38. except SyntaxError:
  39. try:
  40. normalized_code = _NormalizeCode(code)
  41. compile(normalized_code.encode('UTF-8'), '<string>', 'exec')
  42. except SyntaxError:
  43. raise InternalError(sys.exc_info()[1])
  44. def _NormalizeCode(code):
  45. """Make sure that the code snippet is compilable."""
  46. code = textwrap.dedent(code.lstrip('\n')).lstrip()
  47. # Split the code to lines and get rid of all leading full-comment lines as
  48. # they can mess up the normalization attempt.
  49. lines = code.split('\n')
  50. i = 0
  51. for i, line in enumerate(lines):
  52. line = line.strip()
  53. if line and not line.startswith('#'):
  54. break
  55. code = '\n'.join(lines[i:]) + '\n'
  56. if re.match(r'(if|while|for|with|def|class|async|await)\b', code):
  57. code += '\n pass'
  58. elif re.match(r'(elif|else)\b', code):
  59. try:
  60. try_code = 'if True:\n pass\n' + code + '\n pass'
  61. ast.parse(
  62. textwrap.dedent(try_code.lstrip('\n')).lstrip(), '<string>', 'exec')
  63. code = try_code
  64. except SyntaxError:
  65. # The assumption here is that the code is on a single line.
  66. code = 'if True: pass\n' + code
  67. elif code.startswith('@'):
  68. code += '\ndef _():\n pass'
  69. elif re.match(r'try\b', code):
  70. code += '\n pass\nexcept:\n pass'
  71. elif re.match(r'(except|finally)\b', code):
  72. code = 'try:\n pass\n' + code + '\n pass'
  73. elif re.match(r'(return|yield)\b', code):
  74. code = 'def _():\n ' + code
  75. elif re.match(r'(continue|break)\b', code):
  76. code = 'while True:\n ' + code
  77. elif re.match(r'print\b', code):
  78. code = 'from __future__ import print_function\n' + code
  79. return code + '\n'