internal static int FindNextChars(this RazorParser parser, int startIndex, params char[] chars)
        {
            var index   = 0;
            var current = RazorParser.Null;
            var cbuffer = new char[chars.Length];

            fixed(char *p = parser.template)
            {
                while (startIndex < parser.templateLength)
                {
                    if (index == chars.Length)
                    {
                        break;
                    }
                    current = *(p + startIndex);
                    startIndex++;
                    if (parser.IsTerminateChar(current) && !chars.Contains(current))
                    {
                        continue;
                    }
                    cbuffer[index] = current;
                    index++;
                }
            }

            return(RazorHelper.IsEqual(chars, cbuffer) ? startIndex : -1);
        }
Exemple #2
0
        private void Process()
        {
            _razorIndex      = _parser.charIndex;
            _charBeforeRazor = _parser.GetCharacterAtIndex(_razorIndex - 1);
            _razorCounter    = 1;
            var nextChar = _parser.NextChar();

            while (nextChar == RazorParser.RazorChr)
            {
                _razorCounter++;
                _razorIndex = _parser.charIndex++;
                nextChar    = _parser.NextChar();
            }

            if (_razorCounter > 1)
            {
                var razorDisplayCount = (int)Math.Floor(_razorCounter / 2.0);
                _parser.WriteHtml(String.Join(string.Empty, Enumerable.Range(0, razorDisplayCount).Select(x => RazorParser.RazorChr)));
                if (_razorCounter % 2 == 0)
                {
                    return;
                }
                else
                {
                    _charBeforeRazor = _parser.GetCharacterAtIndex(_razorIndex - 1);
                }
            }

            if (_parser.IsTerminateChar(nextChar))
            {
                throw new InvalidOperationException("Invalid operation detected. Razor syntax is incomplete");
            }
            if (nextChar == RazorParser.ExplicitBlockStart)
            {
                _blockType         = BlockType.ExplicitExpression;
                _parser.charIndex += 2;
                _ecounter          = 1;
            }
            else if (nextChar == RazorParser.TextChr)
            {
                _blockType         = BlockType.Text;
                _parser.charIndex += 2;
            }
            else if (nextChar == RazorParser.CommentChar)
            {
                _parser.charIndex += 2;
                _blockType         = BlockType.Comment;
                if (_root != null)
                {
                    _parser.WritePlainCode("/*");
                }
                else
                {
                    _parser.WritePlainHtml("<!--");
                }
            }
            else if (nextChar == RazorParser.BlockStartBrace)
            {
                _blockType         = BlockType.Code;
                _parser.charIndex += 2;
                _bcounter          = 1;
            }
            else
            {
                _parser.charIndex++;
                var index               = _parser.charIndex;
                int operandIfIndex      = _parser.FindNextChars(index, "if("),
                    operandWithIndex    = _parser.FindNextChars(index, "with("),
                    operandForIndex     = _parser.FindNextChars(index, "for("),
                    operandWhileIndex   = _parser.FindNextChars(index, "while("),
                    operandSwitchIndex  = _parser.FindNextChars(index, "switch("),
                    operandHelperIndex  = _parser.FindNextChars(index, "helper "),
                    operandDoWhileIndex = _parser.FindNextChars(index, "do{");

                if (operandIfIndex > -1)
                {
                    _blockType = BlockType.If;
                    _parser.WritePlainCode("if(");
                    _parser.charIndex = operandIfIndex;
                }
                else if (operandHelperIndex > -1)
                {
                    _blockType     = BlockType.Helper;
                    _parser.helper = true;
                    _parser.WritePlainCode("function ");
                    _parser.charIndex = operandHelperIndex;
                }
                else if (operandForIndex > -1)
                {
                    _blockType = BlockType.For;
                    _parser.WritePlainCode("for(");
                    _parser.charIndex = operandForIndex;
                }
                else if (operandWhileIndex > -1)
                {
                    _blockType = BlockType.While;
                    _parser.WritePlainCode("while(");
                    _parser.charIndex = operandWhileIndex;
                }
                else if (operandWithIndex > -1)
                {
                    _blockType = BlockType.With;
                    _parser.WritePlainCode("with(");
                    _parser.charIndex = operandWithIndex;
                }
                else if (operandSwitchIndex > -1)
                {
                    _blockType = BlockType.Switch;
                    _parser.WritePlainCode("switch(");
                    _parser.charIndex = operandSwitchIndex;
                }
                else if (operandDoWhileIndex > -1)
                {
                    _blockType = BlockType.DoWhile;
                    _parser.WritePlainCode("do{");
                    _parser.charIndex = operandDoWhileIndex;
                    _bcounter         = 1;
                }
                else
                {
                    _blockType = BlockType.Expression;
                }
            }
            Parse();
        }