2
# Copyright (c) 2009 The Chromium Authors. All rights reserved.
3
# Use of this source code is governed by a BSD-style license that can be
4
# found in the LICENSE file.
6
# This file pretty-prints the contents of a GYP file.
13
input_file = open(sys.argv[1])
14
input = input_file.read().splitlines()
17
input = sys.stdin.read().splitlines()
19
# This is used to remove comments when we're counting braces.
20
comment_re = re.compile(r'\s*#.*')
22
# This is used to remove quoted strings when we're counting braces.
23
# It takes into account quoted quotes, and makes sure that the quotes
25
# NOTE: It does not handle quotes that span more than one line, or
26
# cases where an escaped quote is preceeded by an escaped backslash.
27
quote_re_str = r'(?P<q>[\'"])(.*?)(?<![^\\][\\])(?P=q)'
28
quote_re = re.compile(quote_re_str)
30
def comment_replace(matchobj):
31
return matchobj.group(1) + matchobj.group(2) + '#' * len(matchobj.group(3))
33
def mask_comments(input):
34
# This is used to mask the quoted strings so we skip braces inside
36
search_re = re.compile(r'(.*?)(#)(.*)')
37
return [search_re.sub(comment_replace, line) for line in input]
39
def quote_replace(matchobj):
40
return "%s%s%s%s" % (matchobj.group(1),
42
'x'*len(matchobj.group(3)),
45
def mask_quotes(input):
46
# This is used to mask the quoted strings so we skip braces inside
48
search_re = re.compile(r'(.*?)' + quote_re_str)
49
return [search_re.sub(quote_replace, line) for line in input]
51
def do_split(input, masked_input, search_re):
54
for (line, masked_line) in zip(input, masked_input):
55
m = search_re.match(masked_line)
57
split = len(m.group(1))
58
line = line[:split] + r'\n' + line[split:]
59
masked_line = masked_line[:split] + r'\n' + masked_line[split:]
60
m = search_re.match(masked_line)
61
output.extend(line.split(r'\n'))
62
mask_output.extend(masked_line.split(r'\n'))
63
return (output, mask_output)
65
# This masks out the quotes and comments, and then splits appropriate
66
# lines (lines that matche the double_*_brace re's above) before
67
# indenting them below.
68
def split_double_braces(input):
69
# These are used to split lines which have multiple braces on them, so
70
# that the indentation looks prettier when all laid out (e.g. closing
71
# braces make a nice diagonal line).
72
double_open_brace_re = re.compile(r'(.*?[\[\{\(,])(\s*)([\[\{\(])')
73
double_close_brace_re = re.compile(r'(.*?[\]\}\)],?)(\s*)([\]\}\)])')
75
masked_input = mask_quotes(input)
76
masked_input = mask_comments(masked_input)
78
(output, mask_output) = do_split(input, masked_input, double_open_brace_re)
79
(output, mask_output) = do_split(output, mask_output, double_close_brace_re)
83
# This keeps track of the number of braces on a given line and returns
84
# the result. It starts at zero and subtracts for closed braces, and
85
# adds for open braces.
86
def count_braces(line):
87
open_braces = ['[', '(', '{']
88
close_braces = [']', ')', '}']
89
closing_prefix_re = re.compile(r'(.*?[^\s\]\}\)]+.*?)([\]\}\)],?)\s*$')
91
stripline = comment_re.sub(r'', line)
92
stripline = quote_re.sub(r"''", stripline)
93
for char in stripline:
94
for brace in open_braces:
97
for brace in close_braces:
105
# This catches the special case of a closing brace having something
106
# other than just whitespace ahead of it -- we don't want to
107
# unindent that until after this line is printed so it stays with
108
# the previous indentation level.
109
if cnt < 0 and closing_prefix_re.match(stripline):
113
# This does the main work of indenting the input based on the brace counts.
114
def prettyprint_input(lines):
119
if comment_re.match(line):
122
line = line.strip('\r\n\t ') # Otherwise doesn't strip \r on Unix.
124
(brace_diff, after) = count_braces(line)
127
print " " * (basic_offset * indent) + line
131
print " " * (basic_offset * indent) + line
133
print " " * (basic_offset * indent) + line
138
# Split up the double braces.
139
lines = split_double_braces(input)
141
# Indent and print the output.
142
prettyprint_input(lines)