Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import os
- # Remove these tokens because they SCREW EVERYTHING UP
- BAD_TOKENS = ["LOV Elixir #"]
- WARNING_MSG = 'Brackets not balanced: see line {0} in file "{1}"\n\t{2}'
- paren_pairs = ['()', '[]', '{}']
- opens = [a[0] for a in paren_pairs]
- backmap = {a[1]:a[0] for a in paren_pairs}
- def file_is_balanced(filename):
- with open(filename, 'rb') as inputfile:
- bracket_stack = []
- line_stack = []
- in_string = False
- for i, line in enumerate(inputfile):
- # I don't want to have to parse regular expressions
- if "create_matcher" in line:
- continue
- for token in BAD_TOKENS:
- line = line.replace(token, "")
- line = line.replace("\\\\", "")
- line = line.replace("\\\"", "")
- line = line.replace("//", "#")
- for c in line:
- if c == "#" and not in_string:
- break
- if c == '"':
- in_string = not in_string
- elif c in opens and not in_string:
- bracket_stack.append(c)
- line_stack.append(i)
- elif c in backmap and not in_string:
- if len(bracket_stack) and bracket_stack[-1] == backmap[c]:
- bracket_stack.pop()
- line_stack.pop()
- else:
- print(WARNING_MSG.format(
- i+1, filename, line
- ))
- print(bracket_stack)
- print(line_stack)
- return False
- if len(bracket_stack):
- print("File is missing trailing brackets: {0}".format(filename))
- print(bracket_stack)
- print(line_stack)
- return 0 == len(bracket_stack)
- def main(argv=None): # type: (Optional[Sequence[str]]) -> int
- retcode = 0
- for root, dirnames, filenames in os.walk(os.environ['GIT_DIR']+'/..'):
- for filename in filenames:
- if filename[-4:] == '.ash':
- if not file_is_balanced(os.path.join(root, filename)):
- retcode = 1
- return retcode
- if __name__ == '__main__':
- exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement