コード例 #1
0
        /// <summary>
        ///   Creates a <see cref = "SnapshotSpan" /> for the given text snapshot covering all text between
        ///   the start and end tokens - (startToken, endToken)
        /// </summary>
        /// <param name = "snapshot">The current snapshot.</param>
        /// <param name = "startToken">The start token of the span.</param>
        /// <param name = "endToken">The end token of the span.</param>
        /// <returns></returns>
        protected override SnapshotSpan CreateSnapshotSpan(ITextSnapshot snapshot,
                                                           IniToken startToken,
                                                           IniToken endToken)
        {
            SnapshotSpan startSnapshot = CreateSnapshotSpan(snapshot, startToken);
            SnapshotSpan endSnapshot   = CreateSnapshotSpan(snapshot, endToken);

            return(new SnapshotSpan(startSnapshot.Start, endSnapshot.End));
        }
コード例 #2
0
 private void CheckOrThrow(IniToken token)
 {
     if (_tokenizer.CurrentToken == token)
     {
         return;
     }
     _tokenizer.Stat(out var line, out var index);
     throw new ParserException(IniToken.SectionName, line, index);
 }
コード例 #3
0
        public void single_line_comment_returns_a_single_comment_token()
        {
            string text = ";TheDudeAbides";
            IEnumerable <IniToken> results = Parser.Parse(text);

            Assert.Equal(1, results.Count());
            IniToken actual = results.First();

            Assert.Equal(IniTokenType.Comment, actual.Type);
            Assert.Equal(text, actual.Text);
            Assert.Equal(0, actual.Start);
        }
コード例 #4
0
        public void single_line_comment_with_a_newline_returns_a_comment_token_and_whitespace_token()
        {
            string text = ";TheDudeAbides" + Environment.NewLine;
            IEnumerable <IniToken> results = Parser.Parse(text);

            Assert.Equal(2, results.Count());
            IniToken comment = results.First();

            Assert.Equal(IniTokenType.Comment, comment.Type);
            IniToken newline = results.Last();

            Assert.Equal(IniTokenType.Whitespace, newline.Type);
        }
コード例 #5
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));
        }
コード例 #6
0
ファイル: ParserException.cs プロジェクト: Aroize/IniParser
 public ParserException(IniToken correctToken, string line, int index)
     : base($"{correctToken}", line, index)
 {
 }
コード例 #7
0
 private IniToken SaveCurrent(IniToken currentToken)
 {
     _prevToken = currentToken;
     _stringIndex++;
     return(currentToken);
 }
コード例 #8
0
 /// <summary>
 ///   Creates a <see cref = "SnapshotSpan" /> for the given text snapshot and token.
 /// </summary>
 /// <param name = "snapshot">The snapshot.</param>
 /// <param name = "token">The token.</param>
 /// <returns></returns>
 protected override SnapshotSpan CreateSnapshotSpan(ITextSnapshot snapshot, IniToken token)
 {
     return(new SnapshotSpan(snapshot, new Span(token.Start, token.Length)));
 }