private void ReadToken_WithLeadingBlanks_BlanksAreRemoved()
        {
            // arrange
            string sourceBody = "\"\"\"\r\n\t\r\n\t\r\n\thelloWorld_123\r\n\t\tfoo\r\n\tbar\"\"\"";
            var    source     = new Source(sourceBody);
            var    lexer      = new Lexer();

            // act
            SyntaxToken token = lexer.Read(source);

            // assert
            Assert.NotNull(token);
            Assert.NotNull(token.Next);
            Assert.NotNull(token.Next.Next);
            Assert.Null(token.Next.Next.Next);

            SyntaxToken blockStringToken = token.Next;

            Assert.Equal(TokenKind.BlockString, blockStringToken.Kind);
            Assert.Equal("helloWorld_123\n\tfoo\nbar", blockStringToken.Value);
            Assert.Equal(1, blockStringToken.Line);
            Assert.Equal(1, blockStringToken.Column);
            Assert.Equal(0, blockStringToken.Start);
            Assert.Equal(36, blockStringToken.End);
            Assert.Equal(TokenKind.StartOfFile, blockStringToken.Previous.Kind);
        }
        private void ReadToken_SingleLine_ParsesCorrectly()
        {
            // arrange
            string sourceBody = "\"\"\"helloWorld_123\"\"\"";
            var    source     = new Source(sourceBody);
            var    lexer      = new Lexer();

            // act
            SyntaxToken token = lexer.Read(source);

            // assert
            Assert.NotNull(token);
            Assert.NotNull(token.Next);
            Assert.NotNull(token.Next.Next);
            Assert.Null(token.Next.Next.Next);

            SyntaxToken blockStringToken = token.Next;

            Assert.Equal(TokenKind.BlockString, blockStringToken.Kind);
            Assert.Equal("helloWorld_123", blockStringToken.Value);
            Assert.Equal(1, blockStringToken.Line);
            Assert.Equal(1, blockStringToken.Column);
            Assert.Equal(0, blockStringToken.Start);
            Assert.Equal(19, blockStringToken.End);
            Assert.Equal(TokenKind.StartOfFile, blockStringToken.Previous.Kind);
        }
        private void ReadToken_WithEscapedTrippleQuote2_EscapeIsReplacedWithActualQuotes()
        {
            // arrange
            string sourceBody = "\"\"\"hello\\\"\"\"World_123\r\n\t\tfoo\r\n\tbar\"\"\"";
            var    source     = new Source(sourceBody);
            var    lexer      = new Lexer();

            // act
            SyntaxToken token = lexer.Read(source);

            // assert
            Assert.NotNull(token);
            Assert.NotNull(token.Next);
            Assert.NotNull(token.Next.Next);
            Assert.Null(token.Next.Next.Next);

            SyntaxToken blockStringToken = token.Next;

            Assert.Equal(TokenKind.BlockString, blockStringToken.Kind);
            Assert.Equal("hello\"\"\"World_123\n\tfoo\nbar", blockStringToken.Value);
            Assert.Equal(1, blockStringToken.Line);
            Assert.Equal(1, blockStringToken.Column);
            Assert.Equal(0, blockStringToken.Start);
            Assert.Equal(34, blockStringToken.End);
            Assert.Equal(TokenKind.StartOfFile, blockStringToken.Previous.Kind);
        }
Ejemplo n.º 4
0
        public void EnsureTokensAreDoublyLinked()
        {
            // arrange
            Source source = new Source(@"type foo");
            Lexer  lexer  = new Lexer();

            // act
            SyntaxToken token = lexer.Read(source);

            // assert
            Assert.Equal(TokenKind.StartOfFile, token.Kind);
            Assert.Null(token.Previous);
            Assert.NotNull(token.Next);

            Assert.Equal(TokenKind.Name, token.Next.Kind);
            Assert.Equal(token, token.Next.Previous);
            Assert.NotNull(token.Next.Next);

            Assert.Equal(TokenKind.Name, token.Next.Next.Kind);
            Assert.Equal(token.Next, token.Next.Next.Previous);
            Assert.NotNull(token.Next.Next.Next);

            Assert.Equal(TokenKind.EndOfFile, token.Next.Next.Next.Kind);
            Assert.Equal(token.Next.Next, token.Next.Next.Next.Previous);
            Assert.Null(token.Next.Next.Next.Next);
        }
Ejemplo n.º 5
0
        public void SourceIsNull_ArgumentNullException()
        {
            // arrange
            Lexer lexer = new Lexer();

            // act
            Action action = () => lexer.Read(null);

            // assert
            Assert.Throws <ArgumentNullException>(action);
        }
Ejemplo n.º 6
0
        public DocumentNode Parse(ISource source, ParserOptions options)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            SyntaxToken start = _lexer.Read(source);

            if (start.Kind != TokenKind.StartOfFile)
            {
                throw new InvalidOperationException(
                          "The first token must be a start of file token.");
            }
            return(ParseDocument(source, start,
                                 options ?? ParserOptions.Default));
        }
Ejemplo n.º 7
0
        public void UnexpectedTokenSequence()
        {
            // arrange
            Source source = new Source("\"foo");
            Lexer  lexer  = new Lexer();

            // act
            Action action = () => lexer.Read(source);

            // assert
            Assert.Equal("Unexpected token sequence.",
                         Assert.Throws <SyntaxException>(action).Message);
        }
Ejemplo n.º 8
0
        public void UnexpectedCharacter()
        {
            // arrange
            Source source = new Source("~");
            Lexer  lexer  = new Lexer();

            // act
            Action action = () => lexer.Read(source);

            // assert
            Assert.Equal("Unexpected character.",
                         Assert.Throws <SyntaxException>(action).Message);
        }