1#!/usr/bin/env python 2# A tool to parse the FormatStyle struct from Format.h and update the 3# documentation in ../ClangFormatStyleOptions.rst automatically. 4# Run from the directory in which this file is located to update the docs. 5 6import collections 7import os 8import re 9import urllib2 10 11CLANG_DIR = os.path.join(os.path.dirname(__file__), '../..') 12FORMAT_STYLE_FILE = os.path.join(CLANG_DIR, 'include/clang/Format/Format.h') 13DOC_FILE = os.path.join(CLANG_DIR, 'docs/ClangFormatStyleOptions.rst') 14 15 16def substitute(text, tag, contents): 17 replacement = '\n.. START_%s\n\n%s\n\n.. END_%s\n' % (tag, contents, tag) 18 pattern = r'\n\.\. START_%s\n.*\n\.\. END_%s\n' % (tag, tag) 19 return re.sub(pattern, '%s', text, flags=re.S) % replacement 20 21def doxygen2rst(text): 22 text = re.sub(r'<tt>\s*(.*?)\s*<\/tt>', r'``\1``', text) 23 text = re.sub(r'\\c ([^ ,;\.]+)', r'``\1``', text) 24 text = re.sub(r'\\\w+ ', '', text) 25 return text 26 27def indent(text, columns): 28 indent = ' ' * columns 29 s = re.sub(r'\n([^\n])', '\n' + indent + '\\1', text, flags=re.S) 30 if s.startswith('\n'): 31 return s 32 return indent + s 33 34class Option: 35 def __init__(self, name, type, comment): 36 self.name = name 37 self.type = type 38 self.comment = comment.strip() 39 self.enum = None 40 self.nested_struct = None 41 42 def __str__(self): 43 s = '**%s** (``%s``)\n%s' % (self.name, self.type, 44 doxygen2rst(indent(self.comment, 2))) 45 if self.enum: 46 s += indent('\n\nPossible values:\n\n%s\n' % self.enum, 2) 47 if self.nested_struct: 48 s += indent('\n\nNested configuration flags:\n\n%s\n' %self.nested_struct, 49 2) 50 return s 51 52class NestedStruct: 53 def __init__(self, name, comment): 54 self.name = name 55 self.comment = comment.strip() 56 self.values = [] 57 58 def __str__(self): 59 return '\n'.join(map(str, self.values)) 60 61class NestedField: 62 def __init__(self, name, comment): 63 self.name = name 64 self.comment = comment.strip() 65 66 def __str__(self): 67 return '\n* ``%s`` %s' % (self.name, doxygen2rst(self.comment)) 68 69class Enum: 70 def __init__(self, name, comment): 71 self.name = name 72 self.comment = comment.strip() 73 self.values = [] 74 75 def __str__(self): 76 return '\n'.join(map(str, self.values)) 77 78class EnumValue: 79 def __init__(self, name, comment): 80 self.name = name 81 self.comment = comment 82 83 def __str__(self): 84 return '* ``%s`` (in configuration: ``%s``)\n%s' % ( 85 self.name, 86 re.sub('.*_', '', self.name), 87 doxygen2rst(indent(self.comment, 2))) 88 89def clean_comment_line(line): 90 match = re.match(r'^/// \\code(\{.(\w+)\})?$', line) 91 if match: 92 lang = match.groups()[1] 93 if not lang: 94 lang = 'c++' 95 return '\n.. code-block:: %s\n\n' % lang 96 if line == '/// \\endcode': 97 return '' 98 return line[4:] + '\n' 99 100def read_options(header): 101 class State: 102 BeforeStruct, Finished, InStruct, InNestedStruct, InNestedFieldComent, \ 103 InFieldComment, InEnum, InEnumMemberComment = range(8) 104 state = State.BeforeStruct 105 106 options = [] 107 enums = {} 108 nested_structs = {} 109 comment = '' 110 enum = None 111 nested_struct = None 112 113 for line in header: 114 line = line.strip() 115 if state == State.BeforeStruct: 116 if line == 'struct FormatStyle {': 117 state = State.InStruct 118 elif state == State.InStruct: 119 if line.startswith('///'): 120 state = State.InFieldComment 121 comment = clean_comment_line(line) 122 elif line == '};': 123 state = State.Finished 124 break 125 elif state == State.InFieldComment: 126 if line.startswith('///'): 127 comment += clean_comment_line(line) 128 elif line.startswith('enum'): 129 state = State.InEnum 130 name = re.sub(r'enum\s+(\w+)\s*\{', '\\1', line) 131 enum = Enum(name, comment) 132 elif line.startswith('struct'): 133 state = State.InNestedStruct 134 name = re.sub(r'struct\s+(\w+)\s*\{', '\\1', line) 135 nested_struct = NestedStruct(name, comment) 136 elif line.endswith(';'): 137 state = State.InStruct 138 field_type, field_name = re.match(r'([<>:\w(,\s)]+)\s+(\w+);', 139 line).groups() 140 option = Option(str(field_name), str(field_type), comment) 141 options.append(option) 142 else: 143 raise Exception('Invalid format, expected comment, field or enum') 144 elif state == State.InNestedStruct: 145 if line.startswith('///'): 146 state = State.InNestedFieldComent 147 comment = clean_comment_line(line) 148 elif line == '};': 149 state = State.InStruct 150 nested_structs[nested_struct.name] = nested_struct 151 elif state == State.InNestedFieldComent: 152 if line.startswith('///'): 153 comment += clean_comment_line(line) 154 else: 155 state = State.InNestedStruct 156 nested_struct.values.append(NestedField(line.replace(';', ''), comment)) 157 elif state == State.InEnum: 158 if line.startswith('///'): 159 state = State.InEnumMemberComment 160 comment = clean_comment_line(line) 161 elif line == '};': 162 state = State.InStruct 163 enums[enum.name] = enum 164 else: 165 raise Exception('Invalid format, expected enum field comment or };') 166 elif state == State.InEnumMemberComment: 167 if line.startswith('///'): 168 comment += clean_comment_line(line) 169 else: 170 state = State.InEnum 171 enum.values.append(EnumValue(line.replace(',', ''), comment)) 172 if state != State.Finished: 173 raise Exception('Not finished by the end of file') 174 175 for option in options: 176 if not option.type in ['bool', 'unsigned', 'int', 'std::string', 177 'std::vector<std::string>', 178 'std::vector<IncludeCategory>']: 179 if enums.has_key(option.type): 180 option.enum = enums[option.type] 181 elif nested_structs.has_key(option.type): 182 option.nested_struct = nested_structs[option.type]; 183 else: 184 raise Exception('Unknown type: %s' % option.type) 185 return options 186 187options = read_options(open(FORMAT_STYLE_FILE)) 188 189options = sorted(options, key=lambda x: x.name) 190options_text = '\n\n'.join(map(str, options)) 191 192contents = open(DOC_FILE).read() 193 194contents = substitute(contents, 'FORMAT_STYLE_OPTIONS', options_text) 195 196with open(DOC_FILE, 'wb') as output: 197 output.write(contents) 198 199