コード例 #1
0
        public virtual void Parse(LookaheadTextReader input, ParserVisitor visitor)
        {
            var results = ParseCore(new SeekableTextReader(input));

            // Replay the results on the visitor
            visitor.Visit(results);
        }
コード例 #2
0
 private static void AdvanceReader(int offset, LookaheadTextReader reader)
 {
     for (int i = 0; i < offset; i++)
     {
         reader.Read();
     }
 }
コード例 #3
0
        private ParserResults SyncParseCore(LookaheadTextReader input)
        {
            SyntaxTreeBuilderVisitor listener = new SyntaxTreeBuilderVisitor();

            Parse(input, listener);
            return(listener.Results);
        }
コード例 #4
0
        protected void RunBufferReadTest(Func <LookaheadTextReader, char[], int, int, int> readMethod)
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("abcdefg");

            reader.Read(); // Reader: "bcdefg"

            // Act
            char[]         buffer = new char[4];
            int            read   = -1;
            SourceLocation actualLocation;

            using (reader.BeginLookahead()) {
                read           = readMethod(reader, buffer, 0, 4);
                actualLocation = reader.CurrentLocation;
            }

            // Assert
            Assert.AreEqual("bcde", new String(buffer), "The reader did not fill the buffer with the correct characters");
            Assert.AreEqual(4, read, "The reader did not report the correct number of read characters");
            Assert.AreEqual(5, actualLocation.AbsoluteIndex, "The reader did not correctly advance the raw index");
            Assert.AreEqual(5, actualLocation.CharacterIndex, "The reader did not correctly advance the character index");
            Assert.AreEqual(0, actualLocation.LineIndex, "The reader did not correctly advance the line index");
            Assert.AreEqual(1, reader.CurrentLocation.CharacterIndex, "The reader did not correctly restore the character index when backtracking");
            Assert.AreEqual(0, reader.CurrentLocation.LineIndex, "The reader did not correctly restore the line index when backtracking");
            Assert.AreEqual('b', reader.Peek(), "The reader did not correctly backtrack to the appropriate character");
        }
コード例 #5
0
        private void RunPeekOrReadTest(string input, int offset, bool isRead)
        {
            using (LookaheadTextReader reader = CreateReader(input)) {
                AdvanceReader(offset, reader);

                // Act
                int?actual = null;
                if (isRead)
                {
                    actual = reader.Read();
                }
                else
                {
                    actual = reader.Peek();
                }

                if (actual == null)
                {
                    Assert.Inconclusive("Actual value was never set?!");
                }

                // Asserts
                AssertReaderValueCorrect(actual.Value, input, offset, "Peek");

                if (isRead)
                {
                    AssertReaderValueCorrect(reader.Peek(), input, offset + 1, "Read");
                }
                else
                {
                    Assert.AreEqual(actual, reader.Peek(), "Peek moved the reader to the next character!");
                }
            }
        }
コード例 #6
0
        protected void RunReadUntilTest(Func <LookaheadTextReader, string> readMethod, int expectedRaw, int expectedChar, int expectedLine)
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("a\r\nbcd\r\nefg");

            reader.Read(); // Reader: "\r\nbcd\r\nefg"
            reader.Read(); // Reader: "\nbcd\r\nefg"
            reader.Read(); // Reader: "bcd\r\nefg"

            // Act
            string         read = null;
            SourceLocation actualLocation;

            using (reader.BeginLookahead())
            {
                read           = readMethod(reader);
                actualLocation = reader.CurrentLocation;
            }

            // Assert
            Assert.Equal(3, reader.CurrentLocation.AbsoluteIndex);
            Assert.Equal(0, reader.CurrentLocation.CharacterIndex);
            Assert.Equal(1, reader.CurrentLocation.LineIndex);
            Assert.Equal(expectedRaw, actualLocation.AbsoluteIndex);
            Assert.Equal(expectedChar, actualLocation.CharacterIndex);
            Assert.Equal(expectedLine, actualLocation.LineIndex);
            Assert.Equal('b', reader.Peek());
            Assert.Equal(read, readMethod(reader));
        }
コード例 #7
0
        protected void RunReadUntilTest(Func <LookaheadTextReader, string> readMethod, int expectedRaw, int expectedChar, int expectedLine)
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("a\r\nbcd\r\nefg");

            reader.Read(); // Reader: "\r\nbcd\r\nefg"
            reader.Read(); // Reader: "\nbcd\r\nefg"
            reader.Read(); // Reader: "bcd\r\nefg"

            // Act
            string         read = null;
            SourceLocation actualLocation;

            using (reader.BeginLookahead()) {
                read           = readMethod(reader);
                actualLocation = reader.CurrentLocation;
            }

            // Assert
            Assert.AreEqual(3, reader.CurrentLocation.AbsoluteIndex, "The reader did not correctly restore the raw index when backtracking");
            Assert.AreEqual(0, reader.CurrentLocation.CharacterIndex, "The reader did not correctly restore the character index when backtracking");
            Assert.AreEqual(1, reader.CurrentLocation.LineIndex, "The reader did not correctly restore the line index when backtracking");
            Assert.AreEqual(expectedRaw, actualLocation.AbsoluteIndex, "The reader did not correctly advance the raw index");
            Assert.AreEqual(expectedChar, actualLocation.CharacterIndex, "The reader did not correctly advance the character index");
            Assert.AreEqual(expectedLine, actualLocation.LineIndex, "The reader did not correctly advance the line index");
            Assert.AreEqual('b', reader.Peek(), "The reader did not correctly backtrack to the appropriate character");
            Assert.AreEqual(read, readMethod(reader));
        }
コード例 #8
0
        protected void RunBufferReadTest(Func <LookaheadTextReader, char[], int, int, int> readMethod)
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("abcdefg");

            reader.Read(); // Reader: "bcdefg"

            // Act
            char[]         buffer = new char[4];
            int            read   = -1;
            SourceLocation actualLocation;

            using (reader.BeginLookahead())
            {
                read           = readMethod(reader, buffer, 0, 4);
                actualLocation = reader.CurrentLocation;
            }

            // Assert
            Assert.Equal("bcde", new String(buffer));
            Assert.Equal(4, read);
            Assert.Equal(5, actualLocation.AbsoluteIndex);
            Assert.Equal(5, actualLocation.CharacterIndex);
            Assert.Equal(0, actualLocation.LineIndex);
            Assert.Equal(1, reader.CurrentLocation.CharacterIndex);
            Assert.Equal(0, reader.CurrentLocation.LineIndex);
            Assert.Equal('b', reader.Peek());
        }
コード例 #9
0
        private void RunPeekOrReadTest(string input, int offset, bool isRead)
        {
            using (LookaheadTextReader reader = CreateReader(input))
            {
                AdvanceReader(offset, reader);

                // Act
                int?actual = null;
                if (isRead)
                {
                    actual = reader.Read();
                }
                else
                {
                    actual = reader.Peek();
                }

                Assert.NotNull(actual);

                // Asserts
                AssertReaderValueCorrect(actual.Value, input, offset, "Peek");

                if (isRead)
                {
                    AssertReaderValueCorrect(reader.Peek(), input, offset + 1, "Read");
                }
                else
                {
                    Assert.Equal(actual, reader.Peek());
                }
            }
        }
コード例 #10
0
        protected void RunCancelBacktrackOutsideLookaheadTest()
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("abcdefg");

            // Act and Assert
            Assert.Throws <InvalidOperationException>(() => reader.CancelBacktrack(), RazorResources.CancelBacktrack_Must_Be_Called_Within_Lookahead);
        }
コード例 #11
0
        protected void RunReadToEndTest()
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("abcdefg");

            // Act
            string str = reader.ReadToEnd();

            // Assert
            Assert.Equal("abcdefg", str);
        }
コード例 #12
0
 private static void RunAll(
     Action <StringBuilder, LookaheadTextReader>[] readerCommands,
     StringBuilder builder,
     LookaheadTextReader reader
     )
 {
     foreach (Action <StringBuilder, LookaheadTextReader> readerCommand in readerCommands)
     {
         readerCommand(builder, reader);
     }
 }
コード例 #13
0
        protected void RunReadToEndTest()
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("abcdefg");

            // Act
            string str = reader.ReadToEnd();

            // Assert
            Assert.AreEqual("abcdefg", str, "The text read from the reader did not match the text provided to it");
        }
コード例 #14
0
        protected void RunSourceLocationTest(string input, SourceLocation expected, Action <LookaheadTextReader> readerAction)
        {
            // Arrange
            LookaheadTextReader reader = CreateReader(input);

            readerAction(reader);

            // Act
            SourceLocation actual = reader.CurrentLocation;

            // Assert
            Assert.Equal(expected, actual);
        }
コード例 #15
0
        protected void RunLookaheadTest(string input, string expected, params Action <StringBuilder, LookaheadTextReader>[] readerCommands)
        {
            // Arrange
            StringBuilder builder = new StringBuilder();

            using (LookaheadTextReader reader = CreateReader(input)) {
                RunAll(readerCommands, builder, reader);
            }

            if (expected != null)
            {
                Assert.AreEqual(expected, builder.ToString(), "The reader did not backtrack correctly");
            }
        }
コード例 #16
0
        protected void RunLookaheadTest(string input, string expected, params Action <StringBuilder, LookaheadTextReader>[] readerCommands)
        {
            // Arrange
            var builder = new StringBuilder();

            using (LookaheadTextReader reader = CreateReader(input))
            {
                RunAll(readerCommands, builder, reader);
            }

            if (expected != null)
            {
                Assert.Equal(expected, builder.ToString());
            }
        }
コード例 #17
0
        protected internal virtual ParserResults ParseTemplateCore(LookaheadTextReader input, CancellationToken?cancelToken)
        {
            // Setup the consumer
            SyntaxTreeBuilderVisitor treeBuilder = new SyntaxTreeBuilderVisitor()
            {
                CancelToken = cancelToken
            };

            // Construct the parser
            RazorParser parser = CreateParser();

            Debug.Assert(parser != null);
            parser.Parse(input, treeBuilder);

            // Return the results
            return(treeBuilder.Results);
        }
コード例 #18
0
        public virtual void Parse(LookaheadTextReader input, ParserVisitor visitor)
        {
            // Setup the parser context
            ParserContext context = new ParserContext(input, CodeParser, MarkupParser, MarkupParser, visitor)
            {
                DesignTimeMode = DesignTimeMode
            };

            MarkupParser.Context = context;
            CodeParser.Context   = context;

            // Execute the context
            try {
                MarkupParser.ParseDocument();
            }
            finally {
                context.OnComplete();
            }
        }
コード例 #19
0
        protected internal virtual GeneratorResults GenerateCodeCore(LookaheadTextReader input, string className, string rootNamespace, string sourceFileName, CancellationToken?cancelToken)
        {
            className     = className ?? Host.DefaultClassName;
            rootNamespace = rootNamespace ?? Host.DefaultNamespace;

            // Set up the code generator
            RazorCodeGenerator generator = CreateCodeGenerator(className, rootNamespace, sourceFileName);

            generator.DesignTimeMode = Host.DesignTimeMode;

            // Setup the consumers
            SyntaxTreeBuilderVisitor treeBuilder = new SyntaxTreeBuilderVisitor()
            {
                CancelToken = cancelToken
            };
            VisitorPair consumer = new VisitorPair(treeBuilder, generator);

            // Run the parser
            RazorParser parser = CreateParser();

            Debug.Assert(parser != null);
            parser.Parse(input, consumer);

            // Post process code
            Host.PostProcessGeneratedCode(generator.GeneratedCode, generator.GeneratedNamespace, generator.GeneratedClass, generator.GeneratedExecuteMethod);

            // Extract design-time mappings
            IDictionary <int, GeneratedCodeMapping> designTimeLineMappings = null;

            if (Host.DesignTimeMode)
            {
                designTimeLineMappings = generator.CodeMappings;
            }

            // Collect results and return
            return(new GeneratorResults(treeBuilder.Results, generator.GeneratedCode, designTimeLineMappings));
        }
コード例 #20
0
ファイル: ParserContext.cs プロジェクト: 316729240/M5
        public ParserContext(LookaheadTextReader source, ParserBase codeParser, MarkupParser markupParser, ParserBase activeParser, ParserVisitor visitor)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (codeParser == null)
            {
                throw new ArgumentNullException("codeParser");
            }
            if (markupParser == null)
            {
                throw new ArgumentNullException("markupParser");
            }
            if (activeParser == null)
            {
                throw new ArgumentNullException("activeParser");
            }
            if (visitor == null)
            {
                throw new ArgumentNullException("visitor");
            }
            if (activeParser != codeParser && activeParser != markupParser)
            {
                throw new ArgumentException(RazorResources.ActiveParser_Must_Be_Code_Or_Markup_Parser, "activeParser");
            }

            CaptureOwnerTask();

            Source       = source;
            CodeParser   = codeParser;
            MarkupParser = markupParser;
            ActiveParser = activeParser;
            _visitorStack.Push(visitor);
            ResetBuffers();
        }
コード例 #21
0
 public virtual ParserContext CreateParserRun(LookaheadTextReader input, ParserBase codeParser, MarkupParser markupParser, ParserVisitor listener)
 {
     return(new ParserContext(input, codeParser, markupParser, SelectActiveParser(codeParser, markupParser), listener));
 }
コード例 #22
0
 protected void Read(StringBuilder builder, LookaheadTextReader reader)
 {
     builder.Append((char)reader.Read());
 }
コード例 #23
0
 protected void CancelBacktrack(StringBuilder builder, LookaheadTextReader reader)
 {
     reader.CancelBacktrack();
 }
コード例 #24
0
 public virtual ParserResults Parse(LookaheadTextReader input)
 {
     return(ParseCore(new SeekableTextReader(input)));
 }
コード例 #25
0
 public virtual ParserResults Parse(LookaheadTextReader input)
 {
     return(SyncParseCore(input));
 }