parseProtoServices function
Implementation
List<ProtoService> parseProtoServices(String content) {
final services = <ProtoService>[];
final serviceStartRegex = RegExp(r'service\s+(\w+)\s*\{');
final rpcRegex = RegExp(
r'rpc\s+(\w+)\s*\(\s*(stream\s+)?([\w\.]+)?\s*\)\s*returns\s*\(\s*(stream\s+)?([\w\.]+)?\s*\)\s*(?:;|\{[\s\S]*?\})',
);
for (final serviceMatch in serviceStartRegex.allMatches(content)) {
final serviceName = serviceMatch.group(1)!;
final bodyStartIndex = serviceMatch.end;
// Find matching brace manually to handle nested braces
int braceCount = 1;
int bodyEndIndex = -1;
for (int i = bodyStartIndex; i < content.length; i++) {
if (content[i] == '{') braceCount++;
if (content[i] == '}') braceCount--;
if (braceCount == 0) {
bodyEndIndex = i;
break;
}
}
if (bodyEndIndex != -1) {
final serviceBody = content.substring(bodyStartIndex, bodyEndIndex);
final methods = <ProtoMethod>[];
for (final rpcMatch in rpcRegex.allMatches(serviceBody)) {
methods.add(
ProtoMethod(
name: rpcMatch.group(1)!,
isClientStreaming: rpcMatch.group(2) != null,
requestType: rpcMatch.group(3) ?? 'void',
isServerStreaming: rpcMatch.group(4) != null,
responseType: rpcMatch.group(5) ?? 'void',
),
);
}
services.add(ProtoService(name: serviceName, methods: methods));
}
}
return services;
}