コード例 #1
0
        internal static IniProperty CreateFromLexeme(List <char> lexeme, List <IniTrivia>?leadingTrivia = null, List <IniTrivia>?trailingTrivia = null)
        {
            var propertyKeyAsChars  = IniProperty.GetKeyFromPropertyLexeme(lexeme);
            var propertyKeyAsString = new string(propertyKeyAsChars.ToArray());
            //
            var propertyValueAsChars  = IniProperty.GetValueFromPropertyLexeme(lexeme);
            var propertyValueAsString = new string(propertyValueAsChars.ToArray());
            //
            var result = new IniProperty(propertyKeyAsString, propertyValueAsString)
            {
                LeadingTrivia  = leadingTrivia ?? new List <IniTrivia>(),
                TrailingTrivia = trailingTrivia ?? new List <IniTrivia>()
            };

            return(result);
        }
コード例 #2
0
        public static MorphicResult <IniFile, MorphicUnit> CreateFromString(string contents)
        {
            var properties        = new List <IniProperty>();
            var sections          = new List <IniSection>();
            var endOfFileContents = new EndOfFileContentsStruct(new List <char>(), new List <IniTrivia>());

            IniSection?currentSection = null;

            var lexer = new IniLexer(contents);

            while (true)
            {
                IniToken iniToken = lexer.GetNextToken();

                // if we've reached the last token, capture the end of file content and break out of this loop
                if (iniToken.Kind == IniTokenKind.EndOfFile)
                {
                    endOfFileContents.Lexeme        = iniToken.Lexeme;
                    endOfFileContents.LeadingTrivia = iniToken.LeadingTrivia;

                    break;
                }

                switch (iniToken.Kind)
                {
                case IniTokenKind.Section:
                {
                    // new section
                    var newSection = IniSection.CreateFromLexeme(iniToken.Lexeme, iniToken.LeadingTrivia, iniToken.TrailingTrivia);
                    currentSection = newSection;
                    //
                    sections.Add(newSection);
                }
                break;

                case IniTokenKind.Property:
                {
                    // new property
                    var newProperty = IniProperty.CreateFromLexeme(iniToken.Lexeme, iniToken.LeadingTrivia, iniToken.TrailingTrivia);
                    //
                    if (currentSection is not null)
                    {
                        // property within a section
                        currentSection.Properties.Add(newProperty);
                    }
                    else
                    {
                        // root property (i.e. outside of any sections)
                        properties.Add(newProperty);
                    }
                }
                break;

                case IniTokenKind.Invalid:
                    return(MorphicResult.ErrorResult());
                }
            }

            var result = new IniFile(properties, sections);

            result.EndOfFileContents = endOfFileContents;

            return(MorphicResult.OkResult(result));
        }