2013-09-04 23:09:13 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# A tool to parse the FormatStyle struct from Format.h and update the
|
|
|
|
# documentation in ../ClangFormatStyleOptions.rst automatically.
|
|
|
|
# Run from the directory in which this file is located to update the docs.
|
|
|
|
|
|
|
|
import collections
|
2016-02-24 00:11:43 +08:00
|
|
|
import os
|
2013-09-04 23:09:13 +08:00
|
|
|
import re
|
|
|
|
import urllib2
|
|
|
|
|
2016-02-24 00:11:43 +08:00
|
|
|
CLANG_DIR = os.path.join(os.path.dirname(__file__), '../..')
|
|
|
|
FORMAT_STYLE_FILE = os.path.join(CLANG_DIR, 'include/clang/Format/Format.h')
|
|
|
|
DOC_FILE = os.path.join(CLANG_DIR, 'docs/ClangFormatStyleOptions.rst')
|
2013-09-04 23:09:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
def substitute(text, tag, contents):
|
|
|
|
replacement = '\n.. START_%s\n\n%s\n\n.. END_%s\n' % (tag, contents, tag)
|
|
|
|
pattern = r'\n\.\. START_%s\n.*\n\.\. END_%s\n' % (tag, tag)
|
|
|
|
return re.sub(pattern, '%s', text, flags=re.S) % replacement
|
|
|
|
|
|
|
|
def doxygen2rst(text):
|
|
|
|
text = re.sub(r'<tt>\s*(.*?)\s*<\/tt>', r'``\1``', text)
|
|
|
|
text = re.sub(r'\\c ([^ ,;\.]+)', r'``\1``', text)
|
|
|
|
text = re.sub(r'\\\w+ ', '', text)
|
|
|
|
return text
|
|
|
|
|
2017-06-23 19:29:40 +08:00
|
|
|
def indent(text, columns, indent_first_line=True):
|
2013-09-04 23:09:13 +08:00
|
|
|
indent = ' ' * columns
|
|
|
|
s = re.sub(r'\n([^\n])', '\n' + indent + '\\1', text, flags=re.S)
|
2017-06-23 19:29:40 +08:00
|
|
|
if not indent_first_line or s.startswith('\n'):
|
2013-09-04 23:09:13 +08:00
|
|
|
return s
|
|
|
|
return indent + s
|
|
|
|
|
|
|
|
class Option:
|
|
|
|
def __init__(self, name, type, comment):
|
|
|
|
self.name = name
|
|
|
|
self.type = type
|
|
|
|
self.comment = comment.strip()
|
|
|
|
self.enum = None
|
2015-09-29 22:57:55 +08:00
|
|
|
self.nested_struct = None
|
2013-09-04 23:09:13 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
s = '**%s** (``%s``)\n%s' % (self.name, self.type,
|
|
|
|
doxygen2rst(indent(self.comment, 2)))
|
|
|
|
if self.enum:
|
|
|
|
s += indent('\n\nPossible values:\n\n%s\n' % self.enum, 2)
|
2015-09-29 22:57:55 +08:00
|
|
|
if self.nested_struct:
|
|
|
|
s += indent('\n\nNested configuration flags:\n\n%s\n' %self.nested_struct,
|
|
|
|
2)
|
2013-09-04 23:09:13 +08:00
|
|
|
return s
|
|
|
|
|
2015-09-29 22:57:55 +08:00
|
|
|
class NestedStruct:
|
|
|
|
def __init__(self, name, comment):
|
|
|
|
self.name = name
|
|
|
|
self.comment = comment.strip()
|
|
|
|
self.values = []
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '\n'.join(map(str, self.values))
|
|
|
|
|
|
|
|
class NestedField:
|
|
|
|
def __init__(self, name, comment):
|
|
|
|
self.name = name
|
|
|
|
self.comment = comment.strip()
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-06-23 19:29:40 +08:00
|
|
|
return '\n* ``%s`` %s' % (
|
|
|
|
self.name,
|
|
|
|
doxygen2rst(indent(self.comment, 2, indent_first_line=False)))
|
2015-09-29 22:57:55 +08:00
|
|
|
|
2013-09-04 23:09:13 +08:00
|
|
|
class Enum:
|
|
|
|
def __init__(self, name, comment):
|
|
|
|
self.name = name
|
|
|
|
self.comment = comment.strip()
|
|
|
|
self.values = []
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '\n'.join(map(str, self.values))
|
|
|
|
|
|
|
|
class EnumValue:
|
|
|
|
def __init__(self, name, comment):
|
|
|
|
self.name = name
|
2016-02-24 00:11:55 +08:00
|
|
|
self.comment = comment
|
2013-09-04 23:09:13 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '* ``%s`` (in configuration: ``%s``)\n%s' % (
|
|
|
|
self.name,
|
|
|
|
re.sub('.*_', '', self.name),
|
|
|
|
doxygen2rst(indent(self.comment, 2)))
|
|
|
|
|
|
|
|
def clean_comment_line(line):
|
2016-02-24 00:11:55 +08:00
|
|
|
match = re.match(r'^/// \\code(\{.(\w+)\})?$', line)
|
|
|
|
if match:
|
|
|
|
lang = match.groups()[1]
|
|
|
|
if not lang:
|
|
|
|
lang = 'c++'
|
|
|
|
return '\n.. code-block:: %s\n\n' % lang
|
2015-10-06 19:54:18 +08:00
|
|
|
if line == '/// \\endcode':
|
|
|
|
return ''
|
|
|
|
return line[4:] + '\n'
|
2013-09-04 23:09:13 +08:00
|
|
|
|
|
|
|
def read_options(header):
|
|
|
|
class State:
|
2015-09-29 22:57:55 +08:00
|
|
|
BeforeStruct, Finished, InStruct, InNestedStruct, InNestedFieldComent, \
|
|
|
|
InFieldComment, InEnum, InEnumMemberComment = range(8)
|
2013-09-04 23:09:13 +08:00
|
|
|
state = State.BeforeStruct
|
|
|
|
|
|
|
|
options = []
|
|
|
|
enums = {}
|
2015-09-29 22:57:55 +08:00
|
|
|
nested_structs = {}
|
2013-09-04 23:09:13 +08:00
|
|
|
comment = ''
|
|
|
|
enum = None
|
2015-09-29 22:57:55 +08:00
|
|
|
nested_struct = None
|
2013-09-04 23:09:13 +08:00
|
|
|
|
|
|
|
for line in header:
|
|
|
|
line = line.strip()
|
|
|
|
if state == State.BeforeStruct:
|
|
|
|
if line == 'struct FormatStyle {':
|
|
|
|
state = State.InStruct
|
|
|
|
elif state == State.InStruct:
|
|
|
|
if line.startswith('///'):
|
|
|
|
state = State.InFieldComment
|
|
|
|
comment = clean_comment_line(line)
|
|
|
|
elif line == '};':
|
|
|
|
state = State.Finished
|
|
|
|
break
|
|
|
|
elif state == State.InFieldComment:
|
|
|
|
if line.startswith('///'):
|
|
|
|
comment += clean_comment_line(line)
|
|
|
|
elif line.startswith('enum'):
|
|
|
|
state = State.InEnum
|
|
|
|
name = re.sub(r'enum\s+(\w+)\s*\{', '\\1', line)
|
|
|
|
enum = Enum(name, comment)
|
2015-09-29 22:57:55 +08:00
|
|
|
elif line.startswith('struct'):
|
|
|
|
state = State.InNestedStruct
|
|
|
|
name = re.sub(r'struct\s+(\w+)\s*\{', '\\1', line)
|
|
|
|
nested_struct = NestedStruct(name, comment)
|
2013-09-04 23:09:13 +08:00
|
|
|
elif line.endswith(';'):
|
|
|
|
state = State.InStruct
|
2015-09-29 22:57:55 +08:00
|
|
|
field_type, field_name = re.match(r'([<>:\w(,\s)]+)\s+(\w+);',
|
|
|
|
line).groups()
|
2013-09-04 23:09:13 +08:00
|
|
|
option = Option(str(field_name), str(field_type), comment)
|
|
|
|
options.append(option)
|
|
|
|
else:
|
|
|
|
raise Exception('Invalid format, expected comment, field or enum')
|
2015-09-29 22:57:55 +08:00
|
|
|
elif state == State.InNestedStruct:
|
|
|
|
if line.startswith('///'):
|
|
|
|
state = State.InNestedFieldComent
|
|
|
|
comment = clean_comment_line(line)
|
|
|
|
elif line == '};':
|
|
|
|
state = State.InStruct
|
|
|
|
nested_structs[nested_struct.name] = nested_struct
|
|
|
|
elif state == State.InNestedFieldComent:
|
|
|
|
if line.startswith('///'):
|
|
|
|
comment += clean_comment_line(line)
|
|
|
|
else:
|
|
|
|
state = State.InNestedStruct
|
|
|
|
nested_struct.values.append(NestedField(line.replace(';', ''), comment))
|
2013-09-04 23:09:13 +08:00
|
|
|
elif state == State.InEnum:
|
|
|
|
if line.startswith('///'):
|
|
|
|
state = State.InEnumMemberComment
|
|
|
|
comment = clean_comment_line(line)
|
|
|
|
elif line == '};':
|
|
|
|
state = State.InStruct
|
|
|
|
enums[enum.name] = enum
|
|
|
|
else:
|
|
|
|
raise Exception('Invalid format, expected enum field comment or };')
|
|
|
|
elif state == State.InEnumMemberComment:
|
|
|
|
if line.startswith('///'):
|
|
|
|
comment += clean_comment_line(line)
|
|
|
|
else:
|
|
|
|
state = State.InEnum
|
|
|
|
enum.values.append(EnumValue(line.replace(',', ''), comment))
|
|
|
|
if state != State.Finished:
|
|
|
|
raise Exception('Not finished by the end of file')
|
|
|
|
|
|
|
|
for option in options:
|
2014-04-09 22:05:49 +08:00
|
|
|
if not option.type in ['bool', 'unsigned', 'int', 'std::string',
|
2015-09-29 22:57:55 +08:00
|
|
|
'std::vector<std::string>',
|
2017-11-09 23:41:23 +08:00
|
|
|
'std::vector<IncludeCategory>',
|
|
|
|
'std::vector<RawStringFormat>']:
|
2013-09-04 23:09:13 +08:00
|
|
|
if enums.has_key(option.type):
|
|
|
|
option.enum = enums[option.type]
|
2015-09-29 22:57:55 +08:00
|
|
|
elif nested_structs.has_key(option.type):
|
2017-06-23 19:29:40 +08:00
|
|
|
option.nested_struct = nested_structs[option.type]
|
2013-09-04 23:09:13 +08:00
|
|
|
else:
|
|
|
|
raise Exception('Unknown type: %s' % option.type)
|
|
|
|
return options
|
|
|
|
|
|
|
|
options = read_options(open(FORMAT_STYLE_FILE))
|
|
|
|
|
|
|
|
options = sorted(options, key=lambda x: x.name)
|
|
|
|
options_text = '\n\n'.join(map(str, options))
|
|
|
|
|
|
|
|
contents = open(DOC_FILE).read()
|
|
|
|
|
|
|
|
contents = substitute(contents, 'FORMAT_STYLE_OPTIONS', options_text)
|
|
|
|
|
2015-11-20 15:46:19 +08:00
|
|
|
with open(DOC_FILE, 'wb') as output:
|
2013-09-04 23:09:13 +08:00
|
|
|
output.write(contents)
|