public static string GetText(RdParseContext context) { string text = string.Empty; int startTokenIndex, endTokenIndex; if (RdParseUtility.GetKeywordArgumentBounds(context.Tokens, out startTokenIndex, out endTokenIndex)) { text = RdText.FromTokens(context, startTokenIndex, endTokenIndex); context.Tokens.Position = endTokenIndex; } return(text); }
private static IEnumerable <IArgumentInfo> ParseArgumentItem(RdParseContext context) { List <IArgumentInfo> arguments = null; TokenStream <RdToken> tokens = context.Tokens; tokens.Advance(1); // Past '\item'. Inside { } we can find any number of '\dots' which are keywords. Debug.Assert(tokens.CurrentToken.TokenType == RdTokenType.OpenCurlyBrace); if (tokens.CurrentToken.TokenType == RdTokenType.OpenCurlyBrace) { int startTokenIndex, endTokenIndex; if (RdParseUtility.GetKeywordArgumentBounds(tokens, out startTokenIndex, out endTokenIndex)) { TextRange range = TextRange.FromBounds(tokens[startTokenIndex].End, tokens[endTokenIndex].Start); string argumentsText = context.TextProvider.GetText(range); string[] argumentNames = argumentsText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); arguments = new List <IArgumentInfo>(); // Move past \item{} tokens.Position = endTokenIndex + 1; Debug.Assert(tokens.CurrentToken.TokenType == RdTokenType.OpenCurlyBrace); if (tokens.CurrentToken.TokenType == RdTokenType.OpenCurlyBrace) { string description = RdText.GetText(context); foreach (string n in argumentNames) { string name = n.Trim(); if (name == @"\dots") { name = "..."; } ArgumentInfo info = new ArgumentInfo(name, description.Trim()); arguments.Add(info); } } } } return(arguments); }
private static IReadOnlyList <IFunctionInfo> ParseFunctions(RdParseContext context) { IReadOnlyList <ISignatureInfo> signatureInfos = null; IReadOnlyDictionary <string, string> argumentDescriptions = null; string functionDescription = null; // Description is normally one for all similar functions bool isInternal = false; string returnValue = null; while (!context.Tokens.IsEndOfStream() && argumentDescriptions == null) { RdToken token = context.Tokens.CurrentToken; if (context.IsAtKeywordWithParameters()) { if (string.IsNullOrEmpty(functionDescription) && context.IsAtKeyword(@"\description")) { functionDescription = RdText.GetText(context); } else if (context.IsAtKeyword(@"\keyword")) { string 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 { context.Tokens.Advance(2); } } else { context.Tokens.MoveToNextToken(); } } // Merge descriptions into signatures. Add all arguments // listed in the \arguments{} section since function signature // does not always list all possible arguments. if (argumentDescriptions != null && signatureInfos != null) { foreach (ISignatureInfo sigInfo in signatureInfos) { // Add missing arguments from the \arguments{} section foreach (string name in argumentDescriptions.Keys) { // TODO: do we need HashSet here instead? Generally arguments // list is relatively short, about 10 items on average. if (sigInfo.Arguments.FirstOrDefault(x => x.Name.Equals(name)) == null) { sigInfo.Arguments.Add(new ArgumentInfo(name)); } } // Relocate ..., if any, to the end var ellipsisArgument = sigInfo.Arguments.FirstOrDefault(x => x.IsEllipsis); if (ellipsisArgument != null) { int index = sigInfo.Arguments.IndexOf(ellipsisArgument); sigInfo.Arguments.RemoveAt(index); sigInfo.Arguments.Add(ellipsisArgument); } // Add description if it is not there yet foreach (var arg in sigInfo.Arguments.Where(x => string.IsNullOrEmpty(x.Description))) { string description; if (argumentDescriptions.TryGetValue(arg.Name, out description)) { ((NamedItemInfo)arg).Description = description ?? string.Empty; } } } } // Merge signatures into function infos Dictionary <string, FunctionInfo> functionInfos = new Dictionary <string, FunctionInfo>(); if (signatureInfos != null) { Dictionary <string, List <ISignatureInfo> > functionSignatures = new Dictionary <string, List <ISignatureInfo> >(); foreach (ISignatureInfo sigInfo in signatureInfos) { FunctionInfo functionInfo; List <ISignatureInfo> sigList; if (!functionInfos.TryGetValue(sigInfo.FunctionName, out functionInfo)) { // Create function info functionInfo = new FunctionInfo(sigInfo.FunctionName, functionDescription); functionInfos[sigInfo.FunctionName] = functionInfo; functionInfo.IsInternal = isInternal; functionInfo.ReturnValue = returnValue; // 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); } } return(functionInfos.Values.ToList()); }
private static ISignatureInfo ParseSignature(string functionName, ParseContext context, IReadOnlyDictionary <string, string> argumentsDescriptions = null) { SignatureInfo info = new SignatureInfo(functionName); List <IArgumentInfo> signatureArguments = new List <IArgumentInfo>(); // RD data may contain function name(s) without braces if (context.Tokens.CurrentToken.TokenType == RTokenType.OpenBrace) { FunctionCall functionCall = new FunctionCall(); functionCall.Parse(context, context.AstRoot); for (int i = 0; i < functionCall.Arguments.Count; i++) { IAstNode arg = functionCall.Arguments[i]; string argName = null; string argDefaultValue = null; bool isEllipsis = false; bool isOptional = false; ExpressionArgument expArg = arg as ExpressionArgument; if (expArg != null) { argName = context.TextProvider.GetText(expArg.ArgumentValue); } else { NamedArgument nameArg = arg as NamedArgument; if (nameArg != null) { argName = context.TextProvider.GetText(nameArg.NameRange); argDefaultValue = RdText.CleanRawRdText(context.TextProvider.GetText(nameArg.DefaultValue)); } else { MissingArgument missingArg = arg as MissingArgument; if (missingArg != null) { argName = string.Empty; } else { EllipsisArgument ellipsisArg = arg as EllipsisArgument; if (ellipsisArg != null) { argName = "..."; isEllipsis = true; } } } } ArgumentInfo argInfo = new ArgumentInfo(argName); argInfo.DefaultValue = argDefaultValue; argInfo.IsEllipsis = isEllipsis; argInfo.IsOptional = isOptional; // TODO: actually parse if (argumentsDescriptions != null) { string description; if (argumentsDescriptions.TryGetValue(argName, out description)) { argInfo.Description = description; } } signatureArguments.Add(argInfo); } } info.Arguments = signatureArguments; return(info); }
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 bool isInternal = false; string returnValue = null; string primaryName = null; while (!context.Tokens.IsEndOfStream() && (functionDescription == null || argumentDescriptions == null || signatureInfos == null || returnValue == null)) { RdToken token = context.Tokens.CurrentToken; if (context.IsAtKeywordWithParameters()) { if (string.IsNullOrEmpty(functionDescription) && context.IsAtKeyword(@"\description")) { functionDescription = RdText.GetText(context); } else if (context.IsAtKeyword(@"\keyword")) { string 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); } } else { context.Tokens.MoveToNextToken(); } } // Merge descriptions into signatures if (argumentDescriptions != null && signatureInfos != null) { foreach (ISignatureInfo sigInfo in signatureInfos) { // Add missing arguments from the \arguments{} section foreach (var arg in sigInfo.Arguments) { string description; if (argumentDescriptions.TryGetValue(arg.Name, out 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 (ISignatureInfo sigInfo in signatureInfos) { FunctionInfo functionInfo; List <ISignatureInfo> sigList; if (!functionInfos.TryGetValue(sigInfo.FunctionName, out functionInfo)) { // Create function info functionInfo = CreateFunctionInfo(sigInfo.FunctionName, 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()); }