Ejemplo n.º 1
0
 public static ITextDocument ToDocument(this ITextBuffer self)
 {
     var ret = self as ITextDocument;
     if (ret == null)
     {
         ret = new SeekableTextReader(self);
     }
     return ret;
 }
Ejemplo n.º 2
0
        public static ITextDocument ToDocument(this ITextBuffer self)
        {
            ITextDocument ret = self as ITextDocument;

            if (ret == null)
            {
                ret = new SeekableTextReader(self);
            }
            return(ret);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        protected virtual ParserResults RunParse(string document, Func<ParserBase, Action> parserActionSelector, bool designTimeParser, Func<ParserContext, ParserBase> parserSelector = null)
        {
            parserSelector = parserSelector ?? (c => c.ActiveParser);

            // Create the source
            ParserResults results = null;
            using (SeekableTextReader reader = new SeekableTextReader(document))
            {
                try
                {
                    ParserBase codeParser = CreateCodeParser();
                    ParserBase markupParser = CreateMarkupParser();
                    ParserContext context = CreateParserContext(reader, codeParser, markupParser);
                    context.DesignTimeMode = designTimeParser;

                    codeParser.Context = context;
                    markupParser.Context = context;

                    // Run the parser
                    parserActionSelector(parserSelector(context))();
                    results = context.CompleteParse();
                }
                finally
                {
                    if (results != null && results.Document != null)
                    {
                        WriteTraceLine(String.Empty);
                        WriteTraceLine("Actual Parse Tree:");
                        WriteNode(0, results.Document);
                    }
                }
            }
            return results;
        }
Ejemplo n.º 5
0
 private ITokenizer MakeTokenizer(bool markup, SeekableTextReader seekableTextReader)
 {
     if (markup)
     {
         return MarkupTokenizerFactory(seekableTextReader);
     }
     else
     {
         return CodeTokenizerFactory(seekableTextReader);
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Parses the contents specified by the <paramref name="inputStream"/> and returns the generated code.
        /// </summary>
        /// <param name="inputStream">A <see cref="Stream"/> that represents the contents to be parsed.</param>
        /// <param name="className">The name of the generated class. When <c>null</c>, defaults to
        /// <see cref="RazorEngineHost.DefaultClassName"/> (<c>Host.DefaultClassName</c>).</param>
        /// <param name="rootNamespace">The namespace in which the generated class will reside. When <c>null</c>,
        /// defaults to <see cref="RazorEngineHost.DefaultNamespace"/> (<c>Host.DefaultNamespace</c>).</param>
        /// <param name="sourceFileName">
        /// The file name to use in line pragmas, usually the original Razor file.
        /// </param>
        /// <returns>A <see cref="GeneratorResults"/> that represents the results of parsing the content.</returns>
        /// <remarks>
        /// This overload calculates the checksum of the contents of <paramref name="inputStream"/> prior to code
        /// generation. The checksum is used for producing the <c>#pragma checksum</c> line pragma required for
        /// debugging.
        /// </remarks>
        public GeneratorResults GenerateCode(
            [NotNull] Stream inputStream,
            string className,
            string rootNamespace,
            string sourceFileName)
        {
            MemoryStream memoryStream = null;
            string checksum = null;
            try
            {
                if (!Host.DesignTimeMode)
                {
                    // We don't need to calculate the checksum in design time.

                    if (!inputStream.CanSeek)
                    {
                        memoryStream = new MemoryStream();
                        inputStream.CopyTo(memoryStream);

                        // We don't have to dispose the input stream since it is owned externally.
                        inputStream = memoryStream;
                    }

                    inputStream.Position = 0;
                    checksum = ComputeChecksum(inputStream);
                    inputStream.Position = 0;
                }

                using (var reader =
                    new StreamReader(
                        inputStream,
                        Encoding.UTF8,
                        detectEncodingFromByteOrderMarks: true,
                        bufferSize: BufferSize,
                        leaveOpen: true))
                {
                    var seekableStream = new SeekableTextReader(reader);
                    return GenerateCodeCore(
                        seekableStream,
                        className,
                        rootNamespace,
                        sourceFileName,
                        checksum,
                        cancelToken: null);
                }
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                }
            }
        }
        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();
        }