private static IReadOnlyList <IFunctionInfo> ParseFunctions(RdParseContext context) { IReadOnlyList <ISignatureInfo> signatureInfos = null; IReadOnlyDictionary <string, string> argumentDescriptions = null; var aliases = new List <string>(); string functionDescription = null; // Description is normally one for all similar functions var isInternal = false; string returnValue = null; string primaryName = null; while (!context.Tokens.IsEndOfStream() && (functionDescription == null || argumentDescriptions == null || signatureInfos == null || returnValue == null)) { var token = context.Tokens.CurrentToken; if (context.IsAtKeywordWithParameters()) { if (string.IsNullOrEmpty(functionDescription) && context.IsAtKeyword(@"\description")) { functionDescription = RdText.GetText(context); } else if (context.IsAtKeyword(@"\keyword")) { var keyword = RdText.GetText(context); if (!string.IsNullOrEmpty(keyword) && keyword.Contains("internal")) { isInternal = true; } } else if (string.IsNullOrEmpty(returnValue) && context.IsAtKeyword(@"\value")) { returnValue = RdText.GetText(context); } else if (argumentDescriptions == null && context.IsAtKeyword(@"\arguments")) { // Extract arguments and their descriptions argumentDescriptions = RdArgumentDescription.ExtractArgumentDecriptions(context); } else if (signatureInfos == null && context.IsAtKeyword(@"\usage")) { // Extract signatures with function names signatureInfos = RdFunctionSignature.ExtractSignatures(context); } else if (context.IsAtKeyword(@"\alias")) { var alias = RdText.GetText(context); if (!string.IsNullOrWhiteSpace(alias)) { aliases.Add(alias); } } else if (primaryName == null && context.IsAtKeyword(@"\name")) { primaryName = RdText.GetText(context); } else { context.Tokens.Advance(2); } } if (token == context.Tokens.CurrentToken) { // If token is not recognized, move on context.Tokens.MoveToNextToken(); } } // Merge descriptions into signatures if (argumentDescriptions != null && signatureInfos != null) { foreach (var sigInfo in signatureInfos) { // Add missing arguments from the \arguments{} section foreach (var arg in sigInfo.Arguments) { if (argumentDescriptions.TryGetValue(arg.Name, out var description)) { ((NamedItemInfo)arg).Description = description ?? string.Empty; } } } } // Merge signatures into function infos var functionInfos = new Dictionary <string, FunctionInfo>(); if (signatureInfos != null) { var functionSignatures = new Dictionary <string, List <ISignatureInfo> >(); foreach (var sigInfo in signatureInfos) { List <ISignatureInfo> sigList; if (!functionInfos.TryGetValue(sigInfo.FunctionName, out FunctionInfo functionInfo)) { // Create function info functionInfo = CreateFunctionInfo(sigInfo.FunctionName, context.PackageName, functionDescription, returnValue, isInternal); functionInfos[sigInfo.FunctionName] = functionInfo; // Create list of signatures for this function sigList = new List <ISignatureInfo>(); functionSignatures[sigInfo.FunctionName] = sigList; functionInfo.Signatures = sigList; } else { sigList = functionSignatures[sigInfo.FunctionName]; } sigList.Add(sigInfo); } } // Propage to aliases if (!string.IsNullOrWhiteSpace(primaryName)) { FunctionInfo functionInfo; if (functionInfos.TryGetValue(primaryName, out functionInfo)) { foreach (var alias in aliases) { if (!functionInfos.ContainsKey(alias)) { functionInfos[alias] = new FunctionInfo(alias, functionInfo); } } } } return(functionInfos.Values.ToList()); }
/// <summary> /// Extracts argument names and descriptions from /// the RD '\arguments{...} construct /// </summary> public static IReadOnlyDictionary <string, string> ExtractArgumentDecriptions(RdParseContext context) { // \arguments{ // \item{formula}{ // A linear model formula specifying the log - linear model. // See \code{\link{ loglm} } for its interpretation. // } // \item{data}{ // Numeric array or data frame.In the first case it specifies the // array of frequencies; in then second it provides the data frame // from which the variables occurring in the formula are // preferentially obtained in the usual way. // } // \item{start, param, eps, iter, print}{ // Arguments passed to \code{\link{ loglin} }. // } // \item{\dots}{ // arguments passed to the default method. // } // } Dictionary <string, string> argumentDescriptions = new Dictionary <string, string>(); TokenStream <RdToken> tokens = context.Tokens; // '\arguments{' is expected Debug.Assert(tokens.NextToken.TokenType == RdTokenType.OpenCurlyBrace); if (tokens.NextToken.TokenType == RdTokenType.OpenCurlyBrace) { // Move past '\arguments' tokens.MoveToNextToken(); int startTokenIndex, endTokenIndex; if (RdParseUtility.GetKeywordArgumentBounds(tokens, out startTokenIndex, out endTokenIndex)) { // Now that we know bounds of \arguments{...} go through // inner '\item' elements and fetch description and all // argument names the description applies to. // // Example: // // \item{start, param, eps, iter, print}{Arguments // passed to \code{\link{ loglin} }.} // while (!tokens.IsEndOfStream() && tokens.Position < endTokenIndex) { RdToken token = tokens.CurrentToken; if (context.IsAtKeyword(@"\item")) { IEnumerable <IArgumentInfo> args = ParseArgumentItem(context); if (args == null) { break; } foreach (var a in args) { argumentDescriptions[a.Name] = a.Description; } } else { tokens.MoveToNextToken(); } } } tokens.Position = endTokenIndex; } return(argumentDescriptions); }