Ejemplo n.º 1
0
        private TemplateOperation ParseInternal(string str, params ArgumentDescription[] args)
        {
            LanguageProvider lp = new CSharpLanguageProvider();

            lp.Start(args);

            IndexToPosition lcf = new IndexToPosition(str);
            int pos = 0;
            int codeStart;
            while (str.Length > pos && 0 <= (codeStart = str.IndexOf("<%", pos)))
            {
                if (pos < codeStart)
                {
                    lp.Literal(str.Substring(pos, codeStart - pos), lcf.Find(pos));
                }

                pos = codeStart + 2;

                bool isExprMode = false;
                if (str.Length > pos && str[pos] == '=')
                {
                    pos++;
                    isExprMode = true;
                }

                int endIndex = str.IndexOf("%>", pos);
                if (endIndex == -1)
                {
                    throw new TemplateCompilationException(
                        "Couldn't find a matching \"%>\"",
                        lcf.Find(pos)
                        );
                }

                Position sourcePos = lcf.Find(pos);
                string code = str.Substring(pos, endIndex - pos);
                if (isExprMode)
                    lp.Expression(code, sourcePos);
                else
                    lp.Code(code, sourcePos);
                
                pos = endIndex + 2;

                // skip trailing CRLF? Only if the current code block is
                // not in expression mode, and if there's no significant text
                // on the same line as the <% or the %>.
                if (!isExprMode
                    && IsLinePrefixOnlyWhitespace(str, codeStart)
                    && IsLineSuffixOnlyWhitespace(str, pos))
                {
                    while (pos < str.Length && str[pos] != '\n')
                        pos++;
                    if (pos < str.Length)
                        pos++;
                }
            }

            if (pos < str.Length)
            {
                lp.Literal(str.Substring(pos), lcf.Find(pos));
            }

            return lp.End();
        }
Ejemplo n.º 2
0
        private TemplateOperation ParseInternal(string str, params ArgumentDescription[] args)
        {
            LanguageProvider lp = new CSharpLanguageProvider();

            lp.Start(args);

            IndexToPosition lcf = new IndexToPosition(str);
            int             pos = 0;
            int             codeStart;

            while (str.Length > pos && 0 <= (codeStart = str.IndexOf("<%", pos)))
            {
                if (pos < codeStart)
                {
                    lp.Literal(str.Substring(pos, codeStart - pos), lcf.Find(pos));
                }

                pos = codeStart + 2;

                bool isExprMode = false;
                if (str.Length > pos && str[pos] == '=')
                {
                    pos++;
                    isExprMode = true;
                }

                int endIndex = str.IndexOf("%>", pos);
                if (endIndex == -1)
                {
                    throw new TemplateCompilationException(
                              "Couldn't find a matching \"%>\"",
                              lcf.Find(pos)
                              );
                }

                Position sourcePos = lcf.Find(pos);
                string   code      = str.Substring(pos, endIndex - pos);
                if (isExprMode)
                {
                    lp.Expression(code, sourcePos);
                }
                else
                {
                    lp.Code(code, sourcePos);
                }

                pos = endIndex + 2;

                // skip trailing CRLF? Only if the current code block is
                // not in expression mode, and if there's no significant text
                // on the same line as the <% or the %>.
                if (!isExprMode &&
                    IsLinePrefixOnlyWhitespace(str, codeStart) &&
                    IsLineSuffixOnlyWhitespace(str, pos))
                {
                    while (pos < str.Length && str[pos] != '\n')
                    {
                        pos++;
                    }
                    if (pos < str.Length)
                    {
                        pos++;
                    }
                }
            }

            if (pos < str.Length)
            {
                lp.Literal(str.Substring(pos), lcf.Find(pos));
            }

            return(lp.End());
        }