| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Copyright © 2019 Michael Catanzaro <[email protected]> |
| 4 |
# |
| 5 |
# This program is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
# Based on original C lineup-parameters by Sébastien Wilmet <[email protected]> |
| 19 |
# Rewritten in Python to allow simple execution from source directory. |
| 20 |
# |
| 21 |
# Usage: lineup-parameters [file] |
| 22 |
# If the file is not given, stdin is read. |
| 23 |
# The result is printed to stdout. |
| 24 |
# |
| 25 |
# The restrictions: |
| 26 |
# - The function name must be at column 0, followed by a space and an opening |
| 27 |
# parenthesis; |
| 28 |
# - One parameter per line; |
| 29 |
# - A parameter must follow certain rules (see the regex in the code), but it |
| 30 |
# doesn't accept all possibilities of the C language. |
| 31 |
# - The opening curly brace ("{") of the function must also be at column 0. |
| 32 |
# |
| 33 |
# If one restriction is missing, the function declaration is not modified. |
| 34 |
# |
| 35 |
# Example: |
| 36 |
# |
| 37 |
# gboolean |
| 38 |
# frobnitz (Frobnitz *frobnitz, |
| 39 |
# gint magic_number, |
| 40 |
# GError **error) |
| 41 |
# { |
| 42 |
# ... |
| 43 |
# } |
| 44 |
# |
| 45 |
# Becomes: |
| 46 |
# |
| 47 |
# gboolean |
| 48 |
# frobnitz (Frobnitz *frobnitz, |
| 49 |
# gint magic_number, |
| 50 |
# GError **error) |
| 51 |
# { |
| 52 |
# ... |
| 53 |
# } |
| 54 |
# |
| 55 |
# TODO: support "..." vararg parameter |
| 56 |
|
| 57 |
import argparse |
| 58 |
import re |
| 59 |
import sys |
| 60 |
|
| 61 |
from typing import NamedTuple |
| 62 |
|
| 63 |
# https://regexr.com/ is your friend. |
| 64 |
functionNameRegex = re.compile(r'^(\w+) ?\(') |
| 65 |
parameterRegex = re.compile( |
| 66 |
r'^\s*(?P<type>(const\s+)?\w+)' |
| 67 |
r'\s+(?P<stars>\**)' |
| 68 |
r'\s*(?P<name>\w+)' |
| 69 |
r'\s*(?P<end>,|\))' |
| 70 |
r'\s*$') |
| 71 |
openingCurlyBraceRegex = re.compile(r'^{\s*$') |
| 72 |
|
| 73 |
|
| 74 |
def matchFunctionName(line): |
| 75 |
match = functionNameRegex.match(line) |
| 76 |
if match: |
| 77 |
functionName = match.group(1) |
| 78 |
firstParamPosition = match.end(0) |
| 79 |
return (functionName, firstParamPosition) |
| 80 |
return (None, 0) |
| 81 |
|
| 82 |
|
| 83 |
class ParameterInfo(NamedTuple): |
| 84 |
paramType: str |
| 85 |
name: str |
| 86 |
numStars: int |
| 87 |
isLastParameter: bool |
| 88 |
|
| 89 |
|
| 90 |
def matchParameter(line): |
| 91 |
_, firstParamPosition = matchFunctionName(line) |
| 92 |
match = parameterRegex.match(line[firstParamPosition:]) |
| 93 |
if match is None: |
| 94 |
return None |
| 95 |
paramType = match.group('type') |
| 96 |
assert(paramType is not None) |
| 97 |
name = match.group('name') |
| 98 |
assert(name is not None) |
| 99 |
stars = match.group('stars') |
| 100 |
numStars = len(stars) if stars is not None else 0 |
| 101 |
end = match.group('end') |
| 102 |
isLastParameter = True if end is not None and end == ')' else False |
| 103 |
return ParameterInfo(paramType, name, numStars, isLastParameter) |
| 104 |
|
| 105 |
|
| 106 |
def matchOpeningCurlyBrace(line): |
| 107 |
return True if openingCurlyBraceRegex.match(line) is not None else False |
| 108 |
|
| 109 |
|
| 110 |
# Length returned is number of lines the declaration takes up |
| 111 |
def getFunctionDeclarationLength(remainingLines): |
| 112 |
for i in range(len(remainingLines)): |
| 113 |
currentLine = remainingLines[i] |
| 114 |
parameterInfo = matchParameter(currentLine) |
| 115 |
if parameterInfo is None: |
| 116 |
return 0 |
| 117 |
if parameterInfo.isLastParameter: |
| 118 |
if i + 1 == len(remainingLines): |
| 119 |
return 0 |
| 120 |
nextLine = remainingLines[i + 1] |
| 121 |
if not matchOpeningCurlyBrace(nextLine): |
| 122 |
return 0 |
| 123 |
return i + 1 |
| 124 |
return 0 |
| 125 |
|
| 126 |
|
| 127 |
def getParameterInfos(remainingLines, length): |
| 128 |
parameterInfos = [] |
| 129 |
for i in range(length): |
| 130 |
parameterInfos.append(matchParameter(remainingLines[i])) |
| 131 |
return parameterInfos |
| 132 |
|
| 133 |
|
| 134 |
def computeSpacing(parameterInfos): |
| 135 |
maxTypeLength = 0 |
| 136 |
maxStarsLength = 0 |
| 137 |
for parameterInfo in parameterInfos: |
| 138 |
maxTypeLength = max(maxTypeLength, len(parameterInfo.paramType)) |
| 139 |
maxStarsLength = max(maxStarsLength, parameterInfo.numStars) |
| 140 |
return (maxTypeLength, maxStarsLength) |
| 141 |
|
| 142 |
|
| 143 |
def printParameter(parameterInfo, maxTypeLength, maxStarsLength, outfile): |
| 144 |
outfile.write(f'{parameterInfo.paramType:<{maxTypeLength + 1}}') |
| 145 |
paramNamePaddedWithStars = f'{parameterInfo.name:*>{parameterInfo.numStars + len(parameterInfo.name)}}' |
| 146 |
outfile.write(f'{paramNamePaddedWithStars:>{maxStarsLength + len(parameterInfo.name)}}') |
| 147 |
|
| 148 |
|
| 149 |
def printFunctionDeclaration(remainingLines, length, useTabs, outfile): |
| 150 |
functionName, _ = matchFunctionName(remainingLines[0]) |
| 151 |
assert(functionName is not None) |
| 152 |
outfile.write(f'{functionName} (') |
| 153 |
numSpacesToParenthesis = len(functionName) + 2 |
| 154 |
whitespace = '' |
| 155 |
if useTabs: |
| 156 |
tabs = ''.ljust(numSpacesToParenthesis // 8, '\t') |
| 157 |
spaces = ''.ljust(numSpacesToParenthesis % 8) |
| 158 |
whitespace = tabs + spaces |
| 159 |
else: |
| 160 |
whitespace = ''.ljust(numSpacesToParenthesis) |
| 161 |
parameterInfos = getParameterInfos(remainingLines, length) |
| 162 |
maxTypeLength, maxStarsLength = computeSpacing(parameterInfos) |
| 163 |
numParameters = len(parameterInfos) |
| 164 |
for i in range(numParameters): |
| 165 |
parameterInfo = parameterInfos[i] |
| 166 |
if i != 0: |
| 167 |
outfile.write(whitespace) |
| 168 |
printParameter(parameterInfo, maxTypeLength, maxStarsLength, outfile) |
| 169 |
if i + 1 != numParameters: |
| 170 |
outfile.write(',\n') |
| 171 |
outfile.write(')\n') |
| 172 |
|
| 173 |
|
| 174 |
def parseContents(infile, useTabs, outfile): |
| 175 |
lines = infile.readlines() |
| 176 |
i = 0 |
| 177 |
while i < len(lines): |
| 178 |
line = lines[i] |
| 179 |
functionName, _ = matchFunctionName(line) |
| 180 |
if functionName is None: |
| 181 |
outfile.write(line) |
| 182 |
i += 1 |
| 183 |
continue |
| 184 |
remainingLines = lines[i:] |
| 185 |
length = getFunctionDeclarationLength(remainingLines) |
| 186 |
if length == 0: |
| 187 |
outfile.write(line) |
| 188 |
i += 1 |
| 189 |
continue |
| 190 |
printFunctionDeclaration(remainingLines, length, useTabs, outfile) |
| 191 |
i += length |
| 192 |
|
| 193 |
|
| 194 |
if __name__ == "__main__": |
| 195 |
parser = argparse.ArgumentParser( |
| 196 |
description='Line up parameters of C functions') |
| 197 |
parser.add_argument('infile', nargs='?', |
| 198 |
type=argparse.FileType('r'), |
| 199 |
default=sys.stdin, |
| 200 |
help='input C source file') |
| 201 |
parser.add_argument('-o', metavar='outfile', nargs='?', |
| 202 |
type=argparse.FileType('w'), |
| 203 |
default=sys.stdout, |
| 204 |
help='where to output modified file') |
| 205 |
parser.add_argument('--tabs', action='store_true', |
| 206 |
help='whether use tab characters in the output') |
| 207 |
args = parser.parse_args() |
| 208 |
parseContents(args.infile, args.tabs, args.o) |
| 209 |
args.infile.close() |
| 210 |
args.o.close() |
| 211 |
|