当前位置: 首页>>代码示例>>C#>>正文


C# Scanner.Eos方法代码示例

本文整理汇总了C#中Scanner.Eos方法的典型用法代码示例。如果您正苦于以下问题:C# Scanner.Eos方法的具体用法?C# Scanner.Eos怎么用?C# Scanner.Eos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Scanner的用法示例。


在下文中一共展示了Scanner.Eos方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ParseTemplate

        /**
        * Breaks up the given `template` string into a tree of tokens. If the `tags`
        * argument is given here it must be an array with two string values: the
        * opening and closing tags used in the template (e.g. [ "<%", "%>" ]). Of
        * course, the default is to use mustaches (i.e. mustache.tags).
        *
        * A token is an array with at least 4 elements. The first element is the
        * mustache symbol that was used inside the tag, e.g. "#" or "&". If the tag
        * did not contain a symbol (i.e. {{myValue}}) this element is "Name". For
        * all text that appears outside a symbol this element is "text".
        *
        * The second element of a token is its "Value". For mustache tags this is
        * whatever else was inside the tag besides the opening symbol. For text tokens
        * this is the text itself.
        *
        * The third and fourth elements of the token are the Start and End indices,
        * respectively, of the token in the original template.
        *
        * Tokens that are the root node of a subtree contain two more elements: 1) an
        * array of tokens in the subtree and 2) the index in the original template at
        * which the closing tag for that section begins.
        */
        private static List<Token> ParseTemplate(string template, Tags tags = null)
        {
            if (!template.Any())
                return new List<Token>();

            var sections = new Stack<Token>(); // Stack to hold section tokens
            var tokens = new List<Token>(); // Buffer to hold the tokens
            var spaces = new Stack<int>(); // Indices of whitespace tokens on the current line
            var hasTag = false; // Is there a {{tag}} on the current line?
            var nonSpace = false; // Is there a non-space char on the current line?

            // Strips all whitespace tokens array for the current line
            // if there was a {{#tag}} on it and otherwise only space.
            Action stripSpace = () =>
            {
                if (hasTag && !nonSpace)
                {
                    while (spaces.Any())
                        tokens.RemoveAt(spaces.Pop());
                }
                else
                {
                    spaces.Clear();
                }

                hasTag = false;
                nonSpace = false;
            };

            // TODO: this `= null` is to avoid "Use of unassigned local variable" C# compiler error.
            Regex openingTagRe = null;
            Regex closingTagRe = null;
            Regex closingCurlyRe = null;
            Action<Tags> compileTags = delegate(Tags tagsToCompile)
            {
                openingTagRe = new Regex(Regex.Escape(tagsToCompile.Opener) + "\\s*");
                closingTagRe = new Regex("\\s*" + Regex.Escape(tagsToCompile.Closer));
                closingCurlyRe = new Regex("\\s*" + Regex.Escape('}' + tagsToCompile.Closer));
            };

            if (tags == null)
                compileTags(MustacheTags);
            else
                compileTags(tags);

            //var Start, Type, Value, chr, token, openSection;
            var scanner = new Scanner(template);
            Token openSection = null;
            while (!scanner.Eos())
            {
                var start = scanner._pos;
                var value = scanner.ScanUntil(openingTagRe);
                var valueLength = value.Length;
                if (valueLength > 0)
                {
                    for (var i = 0; i < valueLength; ++i)
                    {
                        string chr = "" + value[i];

                        if (IsWhitespace(chr))
                        {
                            spaces.Push(tokens.Count);
                        }
                        else
                        {
                            nonSpace = true;
                        }

                        tokens.Add(new Token {Type = "text", Value = chr, Start = start, End = start + 1});
                        start += 1;

                        // Check for whitespace on the current line.
                        if (chr == "\n")
                            stripSpace();
                    }
                }

                // Match the opening tag.
//.........这里部分代码省略.........
开发者ID:vahpetr,项目名称:MustacheCs2,代码行数:101,代码来源:Mustache.cs


注:本文中的Scanner.Eos方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。