Example #1
0
        public void ConstructorInitializesProperties()
        {
            // Arrange
            SeekableTextReader expectedBuffer = new SeekableTextReader(TextReader.Null);
            CSharpCodeParser expectedCodeParser = new CSharpCodeParser();
            HtmlMarkupParser expectedMarkupParser = new HtmlMarkupParser();

            // Act
            ParserContext context = new ParserContext(expectedBuffer, expectedCodeParser, expectedMarkupParser, expectedCodeParser);

            // Assert
            Assert.NotNull(context.Source);
            Assert.Same(expectedCodeParser, context.CodeParser);
            Assert.Same(expectedMarkupParser, context.MarkupParser);
            Assert.Same(expectedCodeParser, context.ActiveParser);
        }
Example #2
0
        private ParserResults ParseCore(ITextDocument input)
        {
            // Setup the parser context
            ParserContext context = new ParserContext(input, CodeParser, MarkupParser, MarkupParser)
            {
                DesignTimeMode = DesignTimeMode
            };

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

            // Execute the parse
            MarkupParser.ParseDocument();

            // Get the result
            ParserResults results = context.CompleteParse();

            // Rewrite whitespace if supported
            Block current = results.Document;
            foreach (ISyntaxTreeRewriter rewriter in Optimizers)
            {
                current = rewriter.Rewrite(current);
            }

            // Link the leaf nodes into a chain
            Span prev = null;
            foreach (Span node in current.Flatten())
            {
                node.Previous = prev;
                if (prev != null)
                {
                    prev.Next = node;
                }
                prev = node;
            }

            // Return the new result
            return new ParserResults(current, results.ParserErrors);
        }
Example #3
0
        private static List<Span> ParseDocument(string documentContents, IList<RazorError> errors = null)
        {
            errors = errors ?? new List<RazorError>();
            var markupParser = new HtmlMarkupParser();
            var codeParser = new TestMvcCSharpRazorCodeParser();
            var context = new ParserContext(new SeekableTextReader(documentContents), codeParser, markupParser, markupParser);
            codeParser.Context = context;
            markupParser.Context = context;
            markupParser.ParseDocument();

            ParserResults results = context.CompleteParse();
            foreach (RazorError error in results.ParserErrors)
            {
                errors.Add(error);
            }
            return results.Document.Flatten().ToList();
        }
Example #4
0
        private ParserContext SetupTestContext(string document,
                                               Action<TextReader> positioningAction,
                                               ParserBase codeParser,
                                               ParserBase markupParser,
                                               ParserBase activeParser)
        {
            var context = new ParserContext(
                new SeekableTextReader(new StringReader(document)),
                codeParser,
                markupParser,
                activeParser,
                new ErrorSink());

            positioningAction(context.Source);
            return context;
        }
Example #5
0
        private ParserResults ParseCore(ITextDocument input)
        {
            // Setup the parser context
            var errorSink = new ParserErrorSink();
            var context = new ParserContext(input, CodeParser, MarkupParser, MarkupParser, errorSink)
            {
                DesignTimeMode = DesignTimeMode
            };

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

            // Execute the parse
            MarkupParser.ParseDocument();

            // Get the result
            var results = context.CompleteParse();

            // Rewrite whitespace if supported
            var rewritingContext = new RewritingContext(results.Document, errorSink);
            foreach (ISyntaxTreeRewriter rewriter in Optimizers)
            {
                rewriter.Rewrite(rewritingContext);
            }

            var descriptors = Enumerable.Empty<TagHelperDescriptor>();

            if (TagHelperDescriptorResolver != null)
            {
                descriptors = GetTagHelperDescriptors(rewritingContext.SyntaxTree, rewritingContext.ErrorSink);
                var tagHelperProvider = new TagHelperDescriptorProvider(descriptors);

                var tagHelperParseTreeRewriter = new TagHelperParseTreeRewriter(tagHelperProvider);
                // Rewrite the document to utilize tag helpers
                tagHelperParseTreeRewriter.Rewrite(rewritingContext);
            }

            var syntaxTree = rewritingContext.SyntaxTree;

            // Link the leaf nodes into a chain
            Span prev = null;
            foreach (Span node in syntaxTree.Flatten())
            {
                node.Previous = prev;
                if (prev != null)
                {
                    prev.Next = node;
                }
                prev = node;
            }

            // Return the new result
            return new ParserResults(syntaxTree, descriptors, errorSink);
        }
        private static List<Span> ParseDocument(
            string documentContents,
            List<RazorError> errors = null,
            List<LineMapping> lineMappings = null)
        {
            errors = errors ?? new List<RazorError>();
            var markupParser = new HtmlMarkupParser();
            var codeParser = new TestMvcCSharpRazorCodeParser();
            var reader = new SeekableTextReader(documentContents);
            var context = new ParserContext(
                reader,
                codeParser,
                markupParser,
                markupParser,
                new ErrorSink());
            codeParser.Context = context;
            markupParser.Context = context;
            markupParser.ParseDocument();

            var results = context.CompleteParse();
            errors.AddRange(results.ParserErrors);
            return results.Document.Flatten().ToList();
        }