#!/usr/bin/env python # Copyright (c) 2009 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # This file pretty-prints the contents of a GYP file. import sys import re input = [] if len(sys.argv) > 1: input_file = open(sys.argv[1]) input = input_file.read().splitlines() input_file.close() else: input = sys.stdin.read().splitlines() # This is used to remove comments when we're counting braces. comment_re = re.compile(r'\s*#.*') # This is used to remove quoted strings when we're counting braces. # It takes into account quoted quotes, and makes sure that the quotes # match. # NOTE: It does not handle quotes that span more than one line, or # cases where an escaped quote is preceeded by an escaped backslash. quote_re_str = r'(?P[\'"])(.*?)(? 0: after = True # This catches the special case of a closing brace having something # other than just whitespace ahead of it -- we don't want to # unindent that until after this line is printed so it stays with # the previous indentation level. if cnt < 0 and closing_prefix_re.match(stripline): after = True return (cnt, after) # This does the main work of indenting the input based on the brace counts. def prettyprint_input(lines): indent = 0 basic_offset = 2 last_line = "" for line in lines: if comment_re.match(line): print line else: line = line.strip('\r\n\t ') # Otherwise doesn't strip \r on Unix. if len(line) > 0: (brace_diff, after) = count_braces(line) if brace_diff != 0: if after: print " " * (basic_offset * indent) + line indent += brace_diff else: indent += brace_diff print " " * (basic_offset * indent) + line else: print " " * (basic_offset * indent) + line else: print "" last_line = line # Split up the double braces. lines = split_double_braces(input) # Indent and print the output. prettyprint_input(lines)