/* Function: Duplicate * Creates an independent copy of the keyword group and all its properties. */ public TextFileKeywordGroup Duplicate() { TextFileKeywordGroup copy = new TextFileKeywordGroup(propertyLocation, languageName); copy.keywordDefinitions = new List <TextFileKeywordDefinition>(keywordDefinitions.Count); copy.keywordDefinitions.AddRange(keywordDefinitions); // this works because it's a struct return(copy); }
// Group: Functions // __________________________________________________________________________ /* Function: AddIgnoredKeywordGroup * Adds a set of ignored keyword to the file. There can be more than one. */ public void AddIgnoredKeywordGroup(TextFileKeywordGroup keywordGroup) { if (ignoredKeywordGroups == null) { ignoredKeywordGroups = new List <TextFileKeywordGroup>(); } ignoredKeywordGroups.Add(keywordGroup); }
/* Function: AddKeywordGroup * Adds a keyword group to the comment type. */ public void AddKeywordGroup(TextFileKeywordGroup keywordGroup) { if (keywordGroups == null) { keywordGroups = new List <TextFileKeywordGroup>(); } keywordGroups.Add(keywordGroup); }
/* Function: AppendKeywordGroup * A function used only by <Save()> that adds a keyword group to the passed StringBuilder. */ private void AppendKeywordGroup(System.Text.StringBuilder output, TextFileKeywordGroup keywordGroup, string linePrefix) { foreach (var keywordDefinition in keywordGroup.KeywordDefinitions) { output.Append(linePrefix + keywordDefinition.Keyword); if (keywordDefinition.HasPlural) { output.Append(", " + keywordDefinition.Plural); } output.AppendLine(); } }
// Group: Loading Functions // __________________________________________________________________________ /* Function: Load * * Loads the contents of a <Comments.txt> file into a <ConfigFiles.TextFile>, returning whether it was successful. If it * was unsuccessful config will be null and it will place errors on the errorList. * * Parameters: * * filename - The <Path> where the file is located. * propertySource - The <Engine.Config.PropertySource> associated with the file. * errorList - If it couldn't successfully parse the file it will add error messages to this list. * config - The contents of the file as a <ConfigFiles.TextFile>. */ public bool Load(Path filename, Engine.Config.PropertySource propertySource, Errors.ErrorList errorList, out ConfigFiles.TextFile config) { int previousErrorCount = errorList.Count; using (ConfigFile file = new ConfigFile()) { bool openResult = file.Open(filename, propertySource, ConfigFile.FileFormatFlags.CondenseIdentifierWhitespace | ConfigFile.FileFormatFlags.CondenseValueWhitespace | ConfigFile.FileFormatFlags.SupportsNullValueLines | ConfigFile.FileFormatFlags.SupportsRawValueLines | ConfigFile.FileFormatFlags.MakeIdentifiersLowercase, errorList); if (openResult == false) { config = null; return(false); } config = new ConfigFiles.TextFile(); TextFileCommentType currentCommentType = null; TextFileKeywordGroup currentKeywordGroup = null; bool inKeywords = false; bool inTags = false; while (file.Get(out string identifier, out string value)) { // // Identifierless lines // if (identifier == null) { // Keywords if (inKeywords) { // Separate keywords string keyword, pluralKeyword; int commaIndex = value.IndexOf(','); if (commaIndex == -1) { keyword = value; pluralKeyword = null; } else { keyword = value.Substring(0, commaIndex).TrimEnd(); pluralKeyword = value.Substring(commaIndex + 1).TrimStart(); if (pluralKeyword.IndexOf(',') != -1) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.NoMoreThanTwoKeywordsOnALine") ); } } // Check for banned characters int bannedCharIndex = keyword.IndexOfAny(BannedKeywordChars); char bannedChar = '\0'; if (bannedCharIndex != -1) { bannedChar = keyword[bannedCharIndex]; } else if (pluralKeyword != null) { bannedCharIndex = pluralKeyword.IndexOfAny(BannedKeywordChars); if (bannedCharIndex != -1) { bannedChar = pluralKeyword[bannedCharIndex]; } } if (bannedChar != '\0') { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.KeywordsCannotContain(char)", bannedChar) ); // Continue parsing } // Add to config currentKeywordGroup.Add(keyword, pluralKeyword); } // Tags, only a single value allowed else if (inTags) { if (value.IndexOf(',') != -1) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.NoMoreThanOneTagOnALine") ); } int bannedChar = value.IndexOfAny(BannedKeywordChars); if (bannedChar != -1) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.TagsCannotContain(char)", value[bannedChar]) ); // Continue parsing } config.AddTag(value, file.PropertyLocation); } // Raw line else { file.AddError( Locale.Get("NaturalDocs.Engine", "ConfigFile.LineNotInIdentifierValueFormat") ); } // Continue so we don't need to put all the identifier handling code in an else. continue; } // If we're here the line has an identifier currentKeywordGroup = null; inKeywords = false; inTags = false; // // Ignore Keywords // if (ignoreKeywordsRegex.IsMatch(identifier)) { currentCommentType = null; currentKeywordGroup = new TextFileKeywordGroup(file.PropertyLocation); config.AddIgnoredKeywordGroup(currentKeywordGroup); inKeywords = true; if (!string.IsNullOrEmpty(value)) { string[] ignoredKeywordsArray = commaSeparatorRegex.Split(value); foreach (string ignoredKeyword in ignoredKeywordsArray) { int bannedChar = ignoredKeyword.IndexOfAny(BannedKeywordChars); if (bannedChar != -1) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.KeywordsCannotContain(char)", ignoredKeyword[bannedChar]) ); // Continue parsing } currentKeywordGroup.Add(ignoredKeyword); } } } // // Tags // else if (tagsRegex.IsMatch(identifier)) { currentCommentType = null; inTags = true; if (!string.IsNullOrEmpty(value)) { string[] tagsArray = commaSeparatorRegex.Split(value); foreach (string tag in tagsArray) { int bannedChar = tag.IndexOfAny(BannedKeywordChars); if (bannedChar != -1) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.TagsCannotContain(char)", tag[bannedChar]) ); // Continue parsing } config.AddTag(tag, file.PropertyLocation); } } } // // Comment Type // else if (commentTypeRegex.IsMatch(identifier)) { var existingCommentType = config.FindCommentType(value); if (existingCommentType != null) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.CommentTypeAlreadyExists(name)", value) ); // Continue parsing. We'll throw this into the existing type even though it shouldn't be overwriting // its values because we want to find any other errors there are in the file. currentCommentType = existingCommentType; } else { // There is no 1.6, but this covers all the 2.0 prereleases. if (file.Version < "1.6" && String.Compare(value, "generic", true) == 0) { value = "Information"; } currentCommentType = new TextFileCommentType(value, file.PropertyLocation); config.AddCommentType(currentCommentType); } } // // Alter Comment Type // else if (alterCommentTypeRegex.IsMatch(identifier)) { // We don't check if the name exists because it may exist in a different file. We also don't check if it exists // in the current file because using Alter is valid (if unnecessary) in that case and we don't want to combine // their definitions. Why? Consider this: // // Comment Type: Comment Type A // Keyword: Keyword A // // Comment Type: Comment Type B // Keyword: Keyword B // // Alter Comment Type: Comment Type A // Keyword: Keyword B // // Keyword B should be part of Comment Type A. However, if we merged the definitions it would appear // first and be overridden by Comment Type B. So we just create two comment type entries for A instead. currentCommentType = new TextFileCommentType(value, file.PropertyLocation, alterType: true); config.AddCommentType(currentCommentType); } // // (Plural) Display Name (From Locale) // else if (displayNameRegex.IsMatch(identifier)) { if (currentCommentType == null) { AddNeedsCommentTypeError(file, identifier); } else if (currentCommentType.HasDisplayNameFromLocale) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.CannotDefineXWhenYIsDefined(x,y)", "Display Name", "Display Name from Locale") ); } else { currentCommentType.SetDisplayName(value, file.PropertyLocation); } } else if (pluralDisplayNameRegex.IsMatch(identifier)) { if (currentCommentType == null) { AddNeedsCommentTypeError(file, identifier); } else if (currentCommentType.HasPluralDisplayNameFromLocale) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.CannotDefineXWhenYIsDefined(x,y)", "Plural Display Name", "Plural Display Name from Locale") ); } else { currentCommentType.SetPluralDisplayName(value, file.PropertyLocation); } } else if (displayNameFromLocaleRegex.IsMatch(identifier)) { if (currentCommentType == null) { AddNeedsCommentTypeError(file, identifier); } else if (currentCommentType.HasDisplayName) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.CannotDefineXWhenYIsDefined(x,y)", "Display Name from Locale", "Display Name") ); } else { currentCommentType.SetDisplayNameFromLocale(value, file.PropertyLocation); } } else if (pluralDisplayNameFromLocaleRegex.IsMatch(identifier)) { if (currentCommentType == null) { AddNeedsCommentTypeError(file, identifier); } else if (currentCommentType.HasPluralDisplayName) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.CannotDefineXWhenYIsDefined(x,y)", "Plural Display Name from Locale", "Plural Display Name") ); } else { currentCommentType.SetPluralDisplayNameFromLocale(value, file.PropertyLocation); } } // // Simple Identifier // else if (identifier == "simple identifier") { if (currentCommentType == null) { AddNeedsCommentTypeError(file, identifier); } else if (nonASCIILettersRegex.IsMatch(value)) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.SimpleIdentifierMustOnlyBeASCIILetters(name)", value) ); } else { currentCommentType.SetSimpleIdentifier(value, file.PropertyLocation); } } // // Scope // else if (identifier == "scope") { if (currentCommentType == null) { AddNeedsCommentTypeError(file, identifier); } else { value = value.ToLower(); if (value == "normal") { currentCommentType.SetScope(CommentType.ScopeValue.Normal, file.PropertyLocation); } else if (startRegex.IsMatch(value)) { currentCommentType.SetScope(CommentType.ScopeValue.Start, file.PropertyLocation); } else if (endRegex.IsMatch(value)) { currentCommentType.SetScope(CommentType.ScopeValue.End, file.PropertyLocation); } else if (alwaysGlobalRegex.IsMatch(value)) { currentCommentType.SetScope(CommentType.ScopeValue.AlwaysGlobal, file.PropertyLocation); } else { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.UnrecognizedValue(keyword, value)", "Scope", value) ); } } } // // Flags // else if (flagsRegex.IsMatch(identifier)) { if (currentCommentType == null) { AddNeedsCommentTypeError(file, identifier); } else { value = value.ToLower(); if (!string.IsNullOrEmpty(value)) { string[] flagStrings = commaSeparatorRegex.Split(value); // Merge them with the existing flags instead of overwriting them since they may also be set by the // Class Hierarchy property. CommentType.FlagValue flagsValue = currentCommentType.Flags ?? default; foreach (string flagString in flagStrings) { if (flagString == "code") { flagsValue |= CommentType.FlagValue.Code; } else if (flagString == "file") { flagsValue |= CommentType.FlagValue.File; } else if (documentationRegex.IsMatch(flagString)) { flagsValue |= CommentType.FlagValue.Documentation; } else if (variableTypeRegex.IsMatch(flagString)) { flagsValue |= CommentType.FlagValue.VariableType; } else if (classHierarchyRegex.IsMatch(flagString)) { flagsValue |= CommentType.FlagValue.ClassHierarchy; } else if (databaseHierarchyRegex.IsMatch(flagString)) { flagsValue |= CommentType.FlagValue.DatabaseHierarchy; } else if (enumRegex.IsMatch(flagString)) { flagsValue |= CommentType.FlagValue.Enum; } else if (string.IsNullOrEmpty(flagString) == false) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.UnrecognizedValue(keyword, value)", "Flags", flagString) ); } } currentCommentType.SetFlags(flagsValue, file.PropertyLocation); } } } // // Class Hierarchy (deprecated, convert to flag) // else if (classHierarchyRegex.IsMatch(identifier)) { if (currentCommentType == null) { AddNeedsCommentTypeError(file, identifier); } else { value = value.ToLower(); // Merge it with the existing flags since they may already be set. CommentType.FlagValue flagsValue = currentCommentType.Flags ?? default; if (yesRegex.IsMatch(value)) { flagsValue |= CommentType.FlagValue.ClassHierarchy; } else if (noRegex.IsMatch(value)) { flagsValue &= ~CommentType.FlagValue.ClassHierarchy; } else { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.UnrecognizedValue(keyword, value)", "Class Hierarchy", value) ); } currentCommentType.SetFlags(flagsValue, file.PropertyLocation); } } // // Keywords // else if (keywordsRegex.IsMatch(identifier)) { if (currentCommentType == null) { AddNeedsCommentTypeError(file, identifier); } else { currentKeywordGroup = new TextFileKeywordGroup(file.PropertyLocation); currentCommentType.AddKeywordGroup(currentKeywordGroup); inKeywords = true; if (!string.IsNullOrEmpty(value)) { string[] keywordsArray = commaSeparatorRegex.Split(value); foreach (string keyword in keywordsArray) { int bannedChar = keyword.IndexOfAny(BannedKeywordChars); if (bannedChar != -1) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.KeywordsCannotContain(char)", keyword[bannedChar]) ); // Continue parsing } currentKeywordGroup.Add(keyword); } } } } // // Language-Specific Keywords // // This must be tested after ignored and language-general keywords so that their modifiers ("add" or "ignore") don't // get mistaken for language names. else if (languageSpecificKeywordsRegex.IsMatch(identifier)) { if (currentCommentType == null) { AddNeedsCommentTypeError(file, identifier); } else { var match = languageSpecificKeywordsRegex.Match(identifier); var languageName = match.Groups[1].ToString(); currentKeywordGroup = new TextFileKeywordGroup(file.PropertyLocation, languageName); currentCommentType.AddKeywordGroup(currentKeywordGroup); inKeywords = true; if (!string.IsNullOrEmpty(value)) { string[] keywordsArray = commaSeparatorRegex.Split(value); foreach (string keyword in keywordsArray) { int bannedChar = keyword.IndexOfAny(BannedKeywordChars); if (bannedChar != -1) { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.KeywordsCannotContain(char)", keyword[bannedChar]) ); // Continue parsing } currentKeywordGroup.Add(keyword); } } } } // // Deprecated keywords: Can Group With, Page Title if First // else if (identifier == "index" || identifier == "index with" || breakListsRegex.IsMatch(identifier) || identifier == "can group with" || identifier == "page title if first") { // Ignore and continue } // // Unrecognized keywords // else { file.AddError( Locale.Get("NaturalDocs.Engine", "Comments.txt.UnrecognizedKeyword(keyword)", identifier) ); } } // while (file.Get) file.Close(); } if (errorList.Count == previousErrorCount) { return(true); } else { config = null; return(false); } }