Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. import os
  4.  
  5. # Remove these tokens because they SCREW EVERYTHING UP
  6. BAD_TOKENS = ["LOV Elixir #"]
  7.  
  8. WARNING_MSG = 'Brackets not balanced: see line {0} in file "{1}"\n\t{2}'
  9.  
  10. paren_pairs = ['()', '[]', '{}']
  11. opens = [a[0] for a in paren_pairs]
  12. backmap = {a[1]:a[0] for a in paren_pairs}
  13.  
  14. def file_is_balanced(filename):
  15. with open(filename, 'rb') as inputfile:
  16. bracket_stack = []
  17. line_stack = []
  18. in_string = False
  19. for i, line in enumerate(inputfile):
  20. # I don't want to have to parse regular expressions
  21. if "create_matcher" in line:
  22. continue
  23. for token in BAD_TOKENS:
  24. line = line.replace(token, "")
  25. line = line.replace("\\\\", "")
  26. line = line.replace("\\\"", "")
  27. line = line.replace("//", "#")
  28. for c in line:
  29. if c == "#" and not in_string:
  30. break
  31. if c == '"':
  32. in_string = not in_string
  33. elif c in opens and not in_string:
  34. bracket_stack.append(c)
  35. line_stack.append(i)
  36. elif c in backmap and not in_string:
  37. if len(bracket_stack) and bracket_stack[-1] == backmap[c]:
  38. bracket_stack.pop()
  39. line_stack.pop()
  40. else:
  41. print(WARNING_MSG.format(
  42. i+1, filename, line
  43. ))
  44. print(bracket_stack)
  45. print(line_stack)
  46. return False
  47. if len(bracket_stack):
  48. print("File is missing trailing brackets: {0}".format(filename))
  49. print(bracket_stack)
  50. print(line_stack)
  51. return 0 == len(bracket_stack)
  52.  
  53. def main(argv=None): # type: (Optional[Sequence[str]]) -> int
  54. retcode = 0
  55. for root, dirnames, filenames in os.walk(os.environ['GIT_DIR']+'/..'):
  56. for filename in filenames:
  57. if filename[-4:] == '.ash':
  58. if not file_is_balanced(os.path.join(root, filename)):
  59. retcode = 1
  60.  
  61. return retcode
  62.  
  63.  
  64. if __name__ == '__main__':
  65. exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement