Example #1
0
        // Group: Loading Functions
        // __________________________________________________________________________


        /* Function: Load
         *
         * Loads the contents of a <Languages.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.MakeIdentifiersLowercase,
                                            errorList);

                if (openResult == false)
                {
                    config = null;
                    return(false);
                }

                config = new ConfigFiles.TextFile();

                TextFileLanguage currentLanguage = null;
                char[]           space           = { ' ' };
                System.Text.RegularExpressions.Match match;


                while (file.Get(out string identifier, out string value))
                {
                    //
                    // Ignore Extensions
                    //

                    if (ignoreExtensionsRegex.IsMatch(identifier))
                    {
                        currentLanguage = null;

                        var ignoredExtensions = value.Split(space);
                        NormalizeFileExtensions(ignoredExtensions);

                        config.AddIgnoredFileExtensions(ignoredExtensions, file.PropertyLocation);
                    }


                    //
                    // Language
                    //

                    else if (identifier == "language")
                    {
                        var existingLanguage = config.FindLanguage(value);

                        if (existingLanguage != null)
                        {
                            file.AddError(
                                Locale.Get("NaturalDocs.Engine", "Languages.txt.LanguageAlreadyExists(name)", value)
                                );

                            // Continue parsing.  We'll throw this into the existing language even though it shouldn't be overwriting
                            // its values because we want to find any other errors there are in the file.
                            currentLanguage = existingLanguage;
                        }

                        else
                        {
                            currentLanguage = new TextFileLanguage(value, file.PropertyLocation);
                            config.AddLanguage(currentLanguage);
                        }
                    }


                    //
                    // Alter Language
                    //

                    else if (alterLanguageRegex.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:
                        //
                        // Language: Language A
                        //    Extensions: langA
                        //
                        // Language: Language B
                        //    Extensions: langB
                        //
                        // Alter Language: Language A
                        //    Add Extensions: langB
                        //
                        // Extension langB should be part of Language A.  However, if we merged the definitions it would appear
                        // first and be overridden by Language B.  So we just create two language entries for A instead.

                        currentLanguage = new TextFileLanguage(value, file.PropertyLocation, alterLanguage: true);
                        config.AddLanguage(currentLanguage);
                    }


                    //
                    // Aliases
                    //

                    else if (aliasesRegex.IsMatch(identifier))
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else if (currentLanguage.AlterLanguage)
                        {
                            file.AddError(
                                Locale.Get("NaturalDocs.Engine", "Languages.txt.NeedAddReplaceWhenAlteringLanguage(keyword)", "Aliases")
                                );
                        }
                        else
                        {
                            var aliases = value.Split(space);
                            currentLanguage.SetAliases(aliases, file.PropertyLocation);
                        }
                    }


                    //
                    // Add/Replace Aliases
                    //

                    else if ((match = addReplaceAliasesRegex.Match(identifier)) != null && match.Success)
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else
                        {
                            TextFileLanguage.PropertyChange propertyChange;

                            if (match.Groups[1].Value == "add")
                            {
                                propertyChange = TextFileLanguage.PropertyChange.Add;
                            }
                            else if (match.Groups[1].Value == "replace")
                            {
                                propertyChange = TextFileLanguage.PropertyChange.Replace;
                            }
                            else
                            {
                                throw new NotImplementedException();
                            }

                            // If we're adding to a language that already has them, we need to combine the properties.
                            if (propertyChange == TextFileLanguage.PropertyChange.Add &&
                                currentLanguage.HasAliases)
                            {
                                var oldAliases = currentLanguage.Aliases;
                                var newAliases = value.Split(space);

                                List <string> combinedAliases = new List <string>(oldAliases.Count + newAliases.Length);
                                combinedAliases.AddRange(oldAliases);
                                combinedAliases.AddRange(newAliases);

                                currentLanguage.SetAliases(combinedAliases, file.PropertyLocation, propertyChange);
                            }

                            // Otherwise we can just add them as is.
                            else
                            {
                                var aliases = value.Split(space);
                                currentLanguage.SetAliases(aliases, file.PropertyLocation, propertyChange);
                            }
                        }
                    }



                    //
                    // File Extensions
                    //

                    else if (fileExtensionsRegex.IsMatch(identifier))
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else if (currentLanguage.AlterLanguage)
                        {
                            file.AddError(
                                Locale.Get("NaturalDocs.Engine", "Languages.txt.NeedAddReplaceWhenAlteringLanguage(keyword)", "Extensions")
                                );
                        }
                        else
                        {
                            var extensions = value.Split(space);
                            NormalizeFileExtensions(extensions);

                            currentLanguage.SetFileExtensions(extensions, file.PropertyLocation);
                        }
                    }


                    //
                    // Add/Replace File Extensions
                    //

                    else if ((match = addReplaceExtensionsRegex.Match(identifier)) != null && match.Success)
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else
                        {
                            TextFileLanguage.PropertyChange propertyChange;

                            if (match.Groups[1].Value == "add")
                            {
                                propertyChange = TextFileLanguage.PropertyChange.Add;
                            }
                            else if (match.Groups[1].Value == "replace")
                            {
                                propertyChange = TextFileLanguage.PropertyChange.Replace;
                            }
                            else
                            {
                                throw new NotImplementedException();
                            }

                            // If we're adding to a language that already has them, we need to combine the properties.
                            if (propertyChange == TextFileLanguage.PropertyChange.Add &&
                                currentLanguage.HasFileExtensions)
                            {
                                var oldExtensions = currentLanguage.FileExtensions;
                                var newExtensions = value.Split(space);
                                NormalizeFileExtensions(newExtensions);

                                List <string> combinedExtensions = new List <string>(oldExtensions.Count + newExtensions.Length);
                                combinedExtensions.AddRange(oldExtensions);
                                combinedExtensions.AddRange(newExtensions);

                                currentLanguage.SetFileExtensions(combinedExtensions, file.PropertyLocation, propertyChange);
                            }

                            // Otherwise we can just add them as is.
                            else
                            {
                                var extensions = value.Split(space);
                                NormalizeFileExtensions(extensions);

                                currentLanguage.SetFileExtensions(extensions, file.PropertyLocation, propertyChange);
                            }
                        }
                    }



                    //
                    // Shebang Strings
                    //

                    else if (shebangStringsRegex.IsMatch(identifier))
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else if (currentLanguage.AlterLanguage)
                        {
                            file.AddError(
                                Locale.Get("NaturalDocs.Engine", "Languages.txt.NeedAddReplaceWhenAlteringLanguage(keyword)", "Shebang Strings")
                                );
                        }
                        else
                        {
                            var shebangStrings = value.Split(space);
                            currentLanguage.SetShebangStrings(shebangStrings, file.PropertyLocation);
                        }
                    }


                    //
                    // Add/Replace Shebang Strings
                    //

                    else if ((match = addReplaceShebangStringsRegex.Match(identifier)) != null && match.Success)
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else
                        {
                            TextFileLanguage.PropertyChange propertyChange;

                            if (match.Groups[1].Value == "add")
                            {
                                propertyChange = TextFileLanguage.PropertyChange.Add;
                            }
                            else if (match.Groups[1].Value == "replace")
                            {
                                propertyChange = TextFileLanguage.PropertyChange.Replace;
                            }
                            else
                            {
                                throw new NotImplementedException();
                            }

                            // If we're adding to a language that already has them, we need to combine the properties.
                            if (propertyChange == TextFileLanguage.PropertyChange.Add &&
                                currentLanguage.HasShebangStrings)
                            {
                                var oldShebangStrings = currentLanguage.ShebangStrings;
                                var newShebangStrings = value.Split(space);

                                List <string> combinedShebangStrings = new List <string>(oldShebangStrings.Count + newShebangStrings.Length);
                                combinedShebangStrings.AddRange(oldShebangStrings);
                                combinedShebangStrings.AddRange(newShebangStrings);

                                currentLanguage.SetShebangStrings(combinedShebangStrings, file.PropertyLocation, propertyChange);
                            }

                            // Otherwise we can just add them as is.
                            else
                            {
                                var shebangStrings = value.Split(space);
                                currentLanguage.SetShebangStrings(shebangStrings, file.PropertyLocation, propertyChange);
                            }
                        }
                    }



                    //
                    // Simple Identifier
                    //

                    else if (identifier == "simple identifier")
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else if (nonASCIILettersRegex.IsMatch(value))
                        {
                            file.AddError(
                                Locale.Get("NaturalDocs.Engine", "Languages.txt.SimpleIdentifierMustOnlyBeASCIILetters(name)", value)
                                );
                        }
                        else
                        {
                            currentLanguage.SetSimpleIdentifier(value, file.PropertyLocation);
                        }
                    }



                    //
                    // Line Comments
                    //

                    else if (lineCommentsRegex.IsMatch(identifier))
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else
                        {
                            var lineCommentSymbols = value.Split(space);
                            currentLanguage.SetLineCommentSymbols(lineCommentSymbols, file.PropertyLocation);
                        }
                    }



                    //
                    // Block Comments
                    //

                    else if (blockCommentsRegex.IsMatch(identifier))
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else
                        {
                            var blockCommentStrings = value.Split(space);

                            if (blockCommentStrings.Length % 2 != 0)
                            {
                                file.AddError(
                                    Locale.Get("NaturalDocs.Engine", "Languages.txt.BlockCommentsMustHaveAnEvenNumberOfSymbols")
                                    );
                            }
                            else
                            {
                                List <BlockCommentSymbols> blockCommentSymbols =
                                    new List <BlockCommentSymbols>(blockCommentStrings.Length / 2);

                                for (int i = 0; i < blockCommentStrings.Length; i += 2)
                                {
                                    blockCommentSymbols.Add(
                                        new BlockCommentSymbols(blockCommentStrings[i], blockCommentStrings[i + 1])
                                        );
                                }

                                currentLanguage.SetBlockCommentSymbols(blockCommentSymbols, file.PropertyLocation);
                            }
                        }
                    }



                    //
                    // Member Operator
                    //

                    else if (memberOperatorRegex.IsMatch(identifier))
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else
                        {
                            currentLanguage.SetMemberOperator(value, file.PropertyLocation);
                        }
                    }



                    //
                    // Line Extender
                    //

                    else if (identifier == "line extender")
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else
                        {
                            currentLanguage.SetLineExtender(value, file.PropertyLocation);
                        }
                    }



                    //
                    // Enum Values
                    //

                    else if (enumValuesRegex.IsMatch(identifier))
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else
                        {
                            string lcValue = value.ToLower();

                            if (lcValue == "global")
                            {
                                currentLanguage.SetEnumValues(Language.EnumValues.Global, file.PropertyLocation);
                            }
                            else if (lcValue == "under type")
                            {
                                currentLanguage.SetEnumValues(Language.EnumValues.UnderType, file.PropertyLocation);
                            }
                            else if (lcValue == "under parent")
                            {
                                currentLanguage.SetEnumValues(Language.EnumValues.UnderParent, file.PropertyLocation);
                            }
                            else
                            {
                                file.AddError(
                                    Locale.Get("NaturalDocs.Engine", "Languages.txt.InvalidEnumValue(value)", value)
                                    );
                            }
                        }
                    }


                    //
                    // Case Sensitive
                    //

                    else if (caseSensitiveRegex.IsMatch(identifier))
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else
                        {
                            string lcValue = value.ToLower();

                            if (yesRegex.IsMatch(lcValue))
                            {
                                currentLanguage.SetCaseSensitive(true, file.PropertyLocation);
                            }
                            else if (noRegex.IsMatch(lcValue))
                            {
                                currentLanguage.SetCaseSensitive(false, file.PropertyLocation);
                            }
                            else
                            {
                                file.AddError(
                                    Locale.Get("NaturalDocs.Engine", "Languages.txt.UnrecognizedValue(keyword, value)", "Case Sensitive", value)
                                    );
                            }
                        }
                    }


                    //
                    // Prototype Enders
                    //

                    // Use identifier and not lcIdentifier to keep the case of the comment type.  The regex will compensate.
                    else if ((match = prototypeEndersRegex.Match(identifier)) != null && match.Success)
                    {
                        if (currentLanguage == null)
                        {
                            AddNeedsLanguageError(file, identifier);
                        }
                        else
                        {
                            string   commentType  = match.Groups[1].Value;
                            string[] enderStrings = value.Split(space);

                            var enders = new TextFilePrototypeEnders(commentType, file.PropertyLocation);
                            enders.AddEnderStrings(enderStrings);

                            currentLanguage.AddPrototypeEnders(enders);
                        }
                    }


                    //
                    // Deprecated keywords
                    //

                    else if (ignorePrefixesRegex.IsMatch(identifier) ||
                             identifier == "perl package" ||
                             identifier == "full language support")
                    {
                        // Ignore
                    }


                    //
                    // Unrecognized keywords
                    //

                    else
                    {
                        file.AddError(
                            Locale.Get("NaturalDocs.Engine", "Languages.txt.UnrecognizedKeyword(keyword)", identifier)
                            );
                    }
                }                          // while (file.Get)

                file.Close();
            }


            if (errorList.Count == previousErrorCount)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #2
0
        // 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);
            }
        }
Example #3
0
        // Group: Saving Functions
        // __________________________________________________________________________


        /* Function: Save
         *
         * Saves the passed information into a configuration file if it's different from the one on disk.
         *
         * Parameters:
         *
         *		filename - The <Path> where the file is located.
         *		propertySource - The <Engine.Config.PropertySource> associated with the file.  It must be
         *								 <Engine.Config.PropertySource.ProjectLanguagesFile> or
         *								 <Engine.Config.PropertySource.SystemLanguagesFile>.
         *		config - The configuration to be saved.
         *		errorList - If it couldn't successfully save the file it will add error messages to this list.  This list may be null if
         *						you don't need the error.
         *
         * Returns:
         *
         *		Whether it was able to successfully save the file without any errors.  If the file didn't need saving because
         *		the generated file was the same as the one on disk, this will still return true.
         */
        public bool Save(Path filename, Engine.Config.PropertySource propertySource, ConfigFiles.TextFile config,
                         Errors.ErrorList errorList = null)
        {
            System.Text.StringBuilder output = new System.Text.StringBuilder(1024);

            string projectOrSystem;

            if (propertySource == Engine.Config.PropertySource.ProjectLanguagesFile)
            {
                projectOrSystem = "Project";
            }
            else if (propertySource == Engine.Config.PropertySource.SystemLanguagesFile)
            {
                projectOrSystem = "System";
            }
            else
            {
                throw new InvalidOperationException();
            }


            // Header

            output.AppendLine("Format: " + Engine.Instance.VersionString);
            output.AppendLine();
            output.Append(Locale.Get("NaturalDocs.Engine", "Languages.txt." + projectOrSystem + "Header.multiline"));
            output.AppendLine();
            output.AppendLine();


            // Ignored Extensions

            if (config.HasIgnoredFileExtensions)
            {
                output.Append(Locale.Get("NaturalDocs.Engine", "Languages.txt.IgnoredExtensionsHeader.multiline"));
                output.AppendLine();

                if (config.IgnoredFileExtensions.Count == 1)
                {
                    output.AppendLine("Ignore Extension: " + config.IgnoredFileExtensions[0]);
                }
                else
                {
                    output.Append("Ignore Extensions:");

                    foreach (string ignoredExtension in config.IgnoredFileExtensions)
                    {
                        output.Append(' ');
                        output.Append(ignoredExtension);
                    }

                    output.AppendLine();
                }

                output.AppendLine();
                output.AppendLine();
            }
            else
            {
                if (propertySource == Engine.Config.PropertySource.ProjectLanguagesFile)
                {
                    output.Append(Locale.Get("NaturalDocs.Engine", "Languages.txt.IgnoredExtensionsHeader.multiline"));
                    output.AppendLine();

                    output.Append(Locale.Get("NaturalDocs.Engine", "Languages.txt.IgnoredExtensionsReference.multiline"));
                    output.AppendLine();
                    output.AppendLine();
                }
                // Add nothing for the system config file.
            }


            // Languages

            output.Append(Locale.Get("NaturalDocs.Engine", "Languages.txt.LanguagesHeader.multiline"));

            if (config.HasLanguages)
            {
                output.Append(Locale.Get("NaturalDocs.Engine", "Languages.txt.DeferredLanguagesReference.multiline"));
                output.AppendLine();

                foreach (var language in config.Languages)
                {
                    if (language.AlterLanguage)
                    {
                        output.Append("Alter ");
                    }

                    output.AppendLine("Language: " + language.Name);

                    int oldGroupNumber = 0;

                    if (language.HasFileExtensions)
                    {
                        AppendLineBreakOnGroupChange(1, ref oldGroupNumber, output);
                        AppendProperty("Extension", "Extensions", language.FileExtensionsPropertyChange,
                                       language.FileExtensions, output);
                    }
                    if (language.HasShebangStrings)
                    {
                        AppendLineBreakOnGroupChange(1, ref oldGroupNumber, output);
                        AppendProperty("Shebang String", "Shebang Strings", language.ShebangStringsPropertyChange,
                                       language.ShebangStrings, output);
                    }

                    if (language.HasSimpleIdentifier)
                    {
                        AppendLineBreakOnGroupChange(2, ref oldGroupNumber, output);
                        output.AppendLine("   Simple Identifier: " + language.SimpleIdentifier);
                    }

                    if (language.HasAliases)
                    {
                        AppendLineBreakOnGroupChange(2, ref oldGroupNumber, output);
                        AppendProperty("Alias", "Aliases", language.AliasesPropertyChange,
                                       language.Aliases, output);
                    }

                    if (language.HasLineCommentSymbols)
                    {
                        AppendLineBreakOnGroupChange(3, ref oldGroupNumber, output);
                        AppendProperty("Line Comment", language.LineCommentSymbols, output);
                    }
                    if (language.HasBlockCommentSymbols)
                    {
                        AppendLineBreakOnGroupChange(3, ref oldGroupNumber, output);
                        AppendProperty("Block Comment", language.BlockCommentSymbols, output);
                    }
                    if (language.HasMemberOperator)
                    {
                        AppendLineBreakOnGroupChange(3, ref oldGroupNumber, output);
                        output.AppendLine("   Member Operator: " + language.MemberOperator);
                    }
                    if (language.HasLineExtender)
                    {
                        AppendLineBreakOnGroupChange(3, ref oldGroupNumber, output);
                        output.AppendLine("   Line Extender: " + language.LineExtender);
                    }
                    if (language.HasEnumValues)
                    {
                        AppendLineBreakOnGroupChange(3, ref oldGroupNumber, output);
                        output.Append("   Enum Values: ");

                        switch (language.EnumValues)
                        {
                        case Language.EnumValues.Global:
                            output.AppendLine("Global");
                            break;

                        case Language.EnumValues.UnderParent:
                            output.AppendLine("Under Parent");
                            break;

                        case Language.EnumValues.UnderType:
                            output.AppendLine("Under Type");
                            break;

                        default:
                            throw new NotImplementedException();
                        }
                    }
                    if (language.HasCaseSensitive)
                    {
                        AppendLineBreakOnGroupChange(3, ref oldGroupNumber, output);
                        output.Append("   Case Sensitive: ");

                        switch (language.CaseSensitive)
                        {
                        case true:
                            output.AppendLine("Yes");
                            break;

                        case false:
                            output.AppendLine("No");
                            break;

                        default:
                            throw new NotImplementedException();
                        }
                    }

                    if (language.HasPrototypeEnders)
                    {
                        foreach (var enderGroup in language.PrototypeEnders)
                        {
                            AppendLineBreakOnGroupChange(4, ref oldGroupNumber, output);
                            AppendProperty(enderGroup.CommentType + " Prototype Ender", enderGroup.CommentType + " Prototype Enders",
                                           enderGroup.EnderStrings, output);
                        }
                    }


                    output.AppendLine();
                    output.AppendLine();
                }
            }
            else             // no languages
            {
                output.AppendLine();
            }


            output.Append(Locale.Get("NaturalDocs.Engine", "Languages.txt." + projectOrSystem + "LanguagesReference.multiline"));



            //
            // Compare with previous file and write to disk
            //

            return(ConfigFile.SaveIfDifferent(filename, output.ToString(), noErrorOnFail: (errorList == null), errorList));
        }
Example #4
0
        // Group: Saving Functions
        // __________________________________________________________________________


        /* Function: Save
         *
         * Saves the passed information into a configuration file if it's different from the one on disk.
         *
         * Parameters:
         *
         *		filename - The <Path> where the file should be saved.
         *		propertySource - The <Engine.Config.PropertySource> associated with the file.  It must be
         *								 <Engine.Config.PropertySource.ProjectCommentsFile> or
         *								 <Engine.Config.PropertySource.SystemCommentsFile>.
         *		config - The configuration to be saved.
         *		errorList - If it couldn't successfully save the file it will add error messages to this list.  This list may be null if
         *						you don't need the error.
         *
         * Returns:
         *
         *		Whether it was able to successfully save the file without any errors.  If the file didn't need saving because
         *		the generated file was the same as the one on disk, this will still return true.
         */
        public bool Save(Path filename, Engine.Config.PropertySource propertySource, ConfigFiles.TextFile config,
                         Errors.ErrorList errorList = null)
        {
            System.Text.StringBuilder output = new System.Text.StringBuilder(1024);

            string projectOrSystem;

            if (propertySource == Engine.Config.PropertySource.ProjectCommentsFile)
            {
                projectOrSystem = "Project";
            }
            else if (propertySource == Engine.Config.PropertySource.SystemCommentsFile)
            {
                projectOrSystem = "System";
            }
            else
            {
                throw new InvalidOperationException();
            }


            // Header

            output.AppendLine("Format: " + Engine.Instance.VersionString);
            output.AppendLine();
            output.Append(Locale.Get("NaturalDocs.Engine", "Comments.txt." + projectOrSystem + "Header.multiline"));
            output.AppendLine();
            output.AppendLine();


            // Ignored Keywords

            if (config.HasIgnoredKeywords)
            {
                output.Append(Locale.Get("NaturalDocs.Engine", "Comments.txt.IgnoredKeywordsHeader.multiline"));
                output.AppendLine();
                output.AppendLine("Ignore Keywords:");

                foreach (var ignoredKeywordGroup in config.IgnoredKeywordGroups)
                {
                    AppendKeywordGroup(output, ignoredKeywordGroup, "   ");
                }

                output.AppendLine();
                output.AppendLine();
            }
            else
            {
                if (propertySource == Engine.Config.PropertySource.ProjectCommentsFile)
                {
                    output.Append(Locale.Get("NaturalDocs.Engine", "Comments.txt.IgnoredKeywordsHeader.multiline"));
                    output.AppendLine();
                    output.Append(Locale.Get("NaturalDocs.Engine", "Comments.txt.IgnoredKeywordsReference.multiline"));
                    output.AppendLine();
                    output.AppendLine();
                }
                // Add nothing for the system config file.
            }


            // Tags

            // Tags are undocumented so don't include any of the syntax reference.  Do include the content if it's there though.

            //output.Append( Locale.Get("NaturalDocs.Engine", "Comments.txt.TagsHeader.multiline") );
            //output.AppendLine();

            if (config.HasTags)
            {
                output.AppendLine("Tags:");

                foreach (var tag in config.Tags)
                {
                    output.AppendLine("   " + tag);
                }

                output.AppendLine();
                output.AppendLine();
            }
            //else
            //{
            //output.Append( Locale.Get("NaturalDocs.Engine", "Comments.txt.TagsReference.multiline") );
            //}

            //output.AppendLine();
            //output.AppendLine();


            // Comment Types

            output.Append(Locale.Get("NaturalDocs.Engine", "Comments.txt.CommentTypesHeader.multiline"));

            if (config.HasCommentTypes)
            {
                output.Append(Locale.Get("NaturalDocs.Engine", "Comments.txt.DeferredCommentTypesReference.multiline"));
                output.AppendLine();

                foreach (var commentType in config.CommentTypes)
                {
                    if (commentType.AlterType == true)
                    {
                        output.Append("Alter ");
                    }

                    output.AppendLine("Comment Type: " + commentType.Name);
                    int oldGroupNumber = 0;

                    if (commentType.HasDisplayName)
                    {
                        AppendLineBreakOnGroupChange(1, ref oldGroupNumber, output);
                        output.AppendLine("   Display Name: " + commentType.DisplayName);
                    }
                    if (commentType.HasPluralDisplayName)
                    {
                        AppendLineBreakOnGroupChange(1, ref oldGroupNumber, output);
                        output.AppendLine("   Plural Display Name: " + commentType.PluralDisplayName);
                    }
                    if (commentType.HasDisplayNameFromLocale)
                    {
                        AppendLineBreakOnGroupChange(1, ref oldGroupNumber, output);
                        output.AppendLine("   Display Name from Locale: " + commentType.DisplayNameFromLocale);
                    }
                    if (commentType.HasPluralDisplayNameFromLocale)
                    {
                        AppendLineBreakOnGroupChange(1, ref oldGroupNumber, output);
                        output.AppendLine("   Plural Display Name from Locale: " + commentType.PluralDisplayNameFromLocale);
                    }
                    if (commentType.HasSimpleIdentifier)
                    {
                        AppendLineBreakOnGroupChange(1, ref oldGroupNumber, output);
                        output.AppendLine("   Simple Identifier: " + commentType.SimpleIdentifier);
                    }

                    if (commentType.HasScope)
                    {
                        AppendLineBreakOnGroupChange(2, ref oldGroupNumber, output);

                        output.Append("   Scope: ");

                        switch ((CommentType.ScopeValue)commentType.Scope)
                        {
                        case CommentType.ScopeValue.Normal:
                            output.AppendLine("Normal");
                            break;

                        case CommentType.ScopeValue.Start:
                            output.AppendLine("Start");
                            break;

                        case CommentType.ScopeValue.End:
                            output.AppendLine("End");
                            break;

                        case CommentType.ScopeValue.AlwaysGlobal:
                            output.AppendLine("Always Global");
                            break;

                        default:
                            throw new NotImplementedException();
                        }
                    }

                    if (commentType.HasFlags)
                    {
                        AppendLineBreakOnGroupChange(2, ref oldGroupNumber, output);

                        output.Append("   Flags: ");

                        var           flagsValue  = (CommentType.FlagValue)commentType.Flags;
                        List <string> flagStrings = new List <string>(7);

                        if ((flagsValue & CommentType.FlagValue.Code) != 0)
                        {
                            flagStrings.Add("Code");
                        }
                        if ((flagsValue & CommentType.FlagValue.File) != 0)
                        {
                            flagStrings.Add("File");
                        }
                        if ((flagsValue & CommentType.FlagValue.Documentation) != 0)
                        {
                            flagStrings.Add("Documentation");
                        }

                        if ((flagsValue & CommentType.FlagValue.VariableType) != 0)
                        {
                            flagStrings.Add("Variable Type");
                        }

                        if ((flagsValue & CommentType.FlagValue.ClassHierarchy) != 0)
                        {
                            flagStrings.Add("Class Hierarchy");
                        }
                        if ((flagsValue & CommentType.FlagValue.DatabaseHierarchy) != 0)
                        {
                            flagStrings.Add("Database Hierarchy");
                        }

                        if ((flagsValue & CommentType.FlagValue.Enum) != 0)
                        {
                            flagStrings.Add("Enum");
                        }

                        for (int i = 0; i < flagStrings.Count; i++)
                        {
                            if (i > 0)
                            {
                                output.Append(", ");
                            }

                            output.Append(flagStrings[i]);
                        }

                        output.AppendLine();
                    }

                    if (commentType.HasKeywordGroups)
                    {
                        foreach (var keywordGroup in commentType.KeywordGroups)
                        {
                            output.AppendLine();

                            if (keywordGroup.IsLanguageAgnostic)
                            {
                                output.AppendLine("   Keywords:");
                            }
                            else
                            {
                                output.AppendLine("   " + keywordGroup.LanguageName + " Keywords:");
                            }

                            AppendKeywordGroup(output, keywordGroup, "      ");
                        }
                    }

                    output.AppendLine();
                    output.AppendLine();
                }
            }
            else             // no comment types
            {
                output.AppendLine();
            }

            output.Append(Locale.Get("NaturalDocs.Engine", "Comments.txt." + projectOrSystem + "CommentTypesReference.multiline"));


            // Compare with previous file and write to disk

            return(ConfigFile.SaveIfDifferent(filename, output.ToString(), noErrorOnFail: (errorList == null), errorList));
        }