zoukankan      html  css  js  c++  java
  • [源码学习]RazorTemplateEngine.GenerateCodeCore之CreateParser()方法

    接上篇的GenerateCodeCore方法中

    View Code
                // Run the parser
                RazorParser parser = CreateParser();
                Debug.Assert(parser != null);
                parser.Parse(input, consumer);

    RazorParser parser = CreateParser();中的CreateParser代码如下:

    View Code
            protected internal virtual RazorParser CreateParser() {
                return new RazorParser(Host.DecorateCodeParser(Host.CodeLanguage.CreateCodeParser()),
                                                              Host.DecorateMarkupParser(Host.CreateMarkupParser())) {
                                                                  DesignTimeMode = Host.DesignTimeMode
                                                              };
            }

    Host是RazorTemplateEngine的属性,类型是RazorEngineHost

    View Code
            /// <summary>
            /// The RazorEngineHost which defines the environment in which the generated template code will live
            /// </summary>
            public RazorEngineHost Host { get; private set; }

    在构造函数中传入并赋值,传入的是RazorEngineHost的派生类WebPageRazorHost

            /// <summary>
            /// Constructs a new RazorTemplateEngine with the specified host
            /// </summary>
            /// <param name="host">The host which defines the environment in which the generated template code will live</param>
            public RazorTemplateEngine(RazorEngineHost host) {
                if (host == null) { throw new ArgumentNullException("host"); }
    
                Host = host;
            }

    传入的是RazorEngineHost的派生类WebPageRazorHost,在System.Web.WebPages.Razor命名空间中,这个类的GetCodeLanguage
    调用了RazorCodeLanguage.GetLanguageByExtension确定了CodeLanguage的实际类型,override MarkupParser CreateMarkupParser()确定了MarkupParser的实际类型。

    以下只研究一下CodeLanguage是CSharpRazorCodeLanguage的情况。

    传入参数时调用Host的两个方法Host.DecorateCodeParser和Host.DecorateMarkupParser,只是看起来两个判断是否为null用的方法

            /// <summary>
            /// Gets an instance of the code parser and is provided an opportunity to decorate or replace it
            /// </summary>
            /// <param name="incomingCodeParser">The code parser</param>
            /// <returns>Either the same code parser, after modifications, or a different code parser</returns>
            public virtual ParserBase DecorateCodeParser(ParserBase incomingCodeParser) {
                if (incomingCodeParser == null) { throw new ArgumentNullException("incomingCodeParser"); }
                return incomingCodeParser;
            }
    
            /// <summary>
            /// Gets an instance of the markup parser and is provided an opportunity to decorate or replace it
            /// </summary>
            /// <param name="incomingMarkupParser">The markup parser</param>
            /// <returns>Either the same markup parser, after modifications, or a different markup parser</returns>
            public virtual MarkupParser DecorateMarkupParser(MarkupParser incomingMarkupParser) {
                if (incomingMarkupParser == null) { throw new ArgumentNullException("incomingMarkupParser"); }
                return incomingMarkupParser;
            }

    第一个方法的参数Host.CodeLanguage.CreateCodeParser()即CSharpRazorCodeLanguage.CreateCodeParser()

            /// <summary>
            /// Constructs a new instance of the code parser for this language
            /// </summary>
            public override ParserBase CreateCodeParser() {
                return new CSharpCodeParser();
            }

    第二个方法的参数Host.CreateMarkupParser()即WebPageRazorHost.CreateMarkupParser()

            public override MarkupParser CreateMarkupParser() {
                return new HtmlMarkupParser();
            }

    RazorParser的构造函数直接就是两行赋值语句,下篇分析parser.Parse(input, consumer);吧

  • 相关阅读:
    巧用boost库实现字符串映射消息处理函数
    Apache Continuum 1.3.6 GA 发布下载
    NetBeans 时事通讯(刊号 # 98 Apr 08, 2010)
    source insight
    vim 树形目录插件NERDTree安装及简单用法 心灵净土 博客频道 CSDN.NET
    Janus: Vim Distribution
    vim中的复制与粘贴 | WangYan BLog
    ctagsrevised
    vi的复制粘贴命令_简简单单_百度空间
    贴个自个用的vimrc zmlovelx(帅得不敢出门 c/c++群31843264) 博客频道 CSDN.NET
  • 原文地址:https://www.cnblogs.com/llcto/p/2922335.html
Copyright © 2011-2022 走看看