Пример #1
0
        static string RenderPage(Model.Directive dir, string start)
        {
            System.Text.StringBuilder txt = new System.Text.StringBuilder();
            txt.Append("<span class=\"directive_name\">@page</span> ");
            if (dir.Expression != null)
            {
                txt.AppendFormat("<span class=\"expression\">{0}</span> ", dir.Expression);
            }
            txt.Append("{\r\n");

            bool first = true;

            foreach (Model.Declaration dec in dir.Declarations)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    txt.Append(";");
                }
                txt.Append("\r\n\t" + start);
                txt.AppendFormat("<span class=\"declaration\">{0}</span>", Render(dec));
            }

            txt.Append("}");
            return(txt.ToString());
        }
Пример #2
0
        static string RenderMedia(Model.Directive dir, int nesting)
        {
            System.Text.StringBuilder txt = new System.Text.StringBuilder();
            txt.Append("<span class=\"directive_name\">@media</span>");

            bool first = true;

            foreach (Model.Medium m in dir.Mediums)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    txt.Append(", ");
                }
                txt.AppendFormat(" <span class=\"medium\">{0}</span>", m.ToString());
            }
            txt.Append(" {\r\n");

            foreach (Model.RuleSet rules in dir.RuleSets)
            {
                txt.AppendFormat("<span class=\"ruleset\">{0}</span>\r\n", Render(rules, nesting + 1));
            }

            txt.Append("}");
            return(txt.ToString());
        }
Пример #3
0
        static string RenderImport(Model.Directive dir)
        {
            System.Text.StringBuilder txt = new System.Text.StringBuilder();
            txt.Append("<span class=\"directive_name\">@import</span> ");
            if (dir.Expression != null)
            {
                txt.AppendFormat("<span class=\"expression\">{0}</span> ", Render(dir.Expression));
            }
            bool first = true;

            foreach (Model.Medium m in dir.Mediums)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    txt.Append(", ");
                }
                txt.AppendFormat(" <span class=\"medium\">{0}</span>", m.ToString());
            }
            txt.Append(";");
            return(txt.ToString());
        }
Пример #4
0
        void CSS3()
        {
            CSSDoc = new Model.CSSDocument();
            string cset = null;

            Model.RuleSet   rset = null;
            Model.Directive dir  = null;

            while (la.kind == 4)
            {
                Get();
            }
            while (la.kind == 5 || la.kind == 6)
            {
                if (la.kind == 5)
                {
                    Get();
                }
                else
                {
                    Get();
                }
            }
            while (StartOf(1))
            {
                if (StartOf(2))
                {
                    ruleset(out rset);
                    CSSDoc.RuleSets.Add(rset);
                }
                else
                {
                    directive(out dir);
                    CSSDoc.Directives.Add(dir);
                }
                while (la.kind == 5 || la.kind == 6)
                {
                    if (la.kind == 5)
                    {
                        Get();
                    }
                    else
                    {
                        Get();
                    }
                }
                while (la.kind == 4)
                {
                    Get();
                }
            }
        }
Пример #5
0
        static string RenderFontFace(Model.Directive dir, string start)
        {
            System.Text.StringBuilder txt = new System.Text.StringBuilder();
            txt.Append("<span class=\"directive_name\">@font-face</span> {");

            bool first = true;

            foreach (Model.Declaration dec in dir.Declarations)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    txt.Append(";");
                }
                txt.Append("\r\n\t" + start);
                txt.AppendFormat("<span class=\"declaration\">{0}</span>", Render(dec));
            }

            txt.Append("</span>\r\n}");
            return(txt.ToString());
        }
Пример #6
0
        void directive(out Model.Directive dir)
        {
            dir = new Model.Directive();
            Model.Declaration dec = null;
            Model.RuleSet rset = null;
            Model.Expression exp = null;
            Model.Directive dr = null;
            string ident = null;
            Model.Medium m;

            Expect(23);
            dir.Name = "@";
            if (la.kind == 24) {
                Get();
                dir.Name += "-";
            }
            identity(out ident);
            dir.Name += ident;
            switch (dir.Name.ToLower()) {
                case "@media": dir.Type = Model.DirectiveType.Media; break;
                case "@import": dir.Type = Model.DirectiveType.Import; break;
                case "@charset": dir.Type = Model.DirectiveType.Charset; break;
                case "@page": dir.Type = Model.DirectiveType.Page; break;
                case "@font-face": dir.Type = Model.DirectiveType.FontFace; break;
                case "@namespace": dir.Type = Model.DirectiveType.Namespace; break;
                default: dir.Type = Model.DirectiveType.Other; break;
            }

            while (la.kind == 4) {
                Get();
            }
            if (StartOf(4)) {
                if (StartOf(5)) {
                    medium(out m);
                    dir.Mediums.Add(m);
                    while (la.kind == 4) {
                        Get();
                    }
                    while (la.kind == 25) {
                        Get();
                        while (la.kind == 4) {
                            Get();
                        }
                        medium(out m);
                        dir.Mediums.Add(m);
                        while (la.kind == 4) {
                            Get();
                        }
                    }
                } else {
                    expr(out exp);
                    dir.Expression = exp;
                    while (la.kind == 4) {
                        Get();
                    }
                }
            }
            if (la.kind == 26) {
                Get();
                while (la.kind == 4) {
                    Get();
                }
                if (StartOf(6)) {
                    while (StartOf(1)) {
                        if (dir.Type == Model.DirectiveType.Page || dir.Type == Model.DirectiveType.FontFace)
                        {
                            declaration(out dec);
                            dir.Declarations.Add(dec);
                            while (la.kind == 4) {
                                Get();
                            }
                            while (la.kind == 27) {
                                Get();
                                while (la.kind == 4) {
                                    Get();
                                }
                                if (la.val.Equals("}")) { Get(); return; }
                                declaration(out dec);
                                dir.Declarations.Add(dec);
                                while (la.kind == 4) {
                                    Get();
                                }
                            }
                            if (la.kind == 27) {
                                Get();
                                while (la.kind == 4) {
                                    Get();
                                }
                            }
                        } else if (StartOf(2)) {
                            ruleset(out rset);
                            dir.RuleSets.Add(rset);
                            while (la.kind == 4) {
                                Get();
                            }
                        } else {
                            directive(out dr);
                            dir.Directives.Add(dr);
                            while (la.kind == 4) {
                                Get();
                            }
                        }
                    }
                }
                Expect(28);
                while (la.kind == 4) {
                    Get();
                }
            } else if (la.kind == 27) {
                Get();
                while (la.kind == 4) {
                    Get();
                }
            } else SynErr(50);
        }
Пример #7
0
        void directive(out Model.Directive dir)
        {
            dir = new Model.Directive();
            Model.Declaration dec   = null;
            Model.RuleSet     rset  = null;
            Model.Expression  exp   = null;
            Model.Directive   dr    = null;
            string            ident = null;

            Model.Medium m;

            Expect(23);
            dir.Name = "@";
            if (la.kind == 24)
            {
                Get();
                dir.Name += "-";
            }
            identity(out ident);
            dir.Name += ident;
            switch (dir.Name.ToLower())
            {
            case "@media": dir.Type = Model.DirectiveType.Media; break;

            case "@import": dir.Type = Model.DirectiveType.Import; break;

            case "@charset": dir.Type = Model.DirectiveType.Charset; break;

            case "@page": dir.Type = Model.DirectiveType.Page; break;

            case "@font-face": dir.Type = Model.DirectiveType.FontFace; break;

            case "@namespace": dir.Type = Model.DirectiveType.Namespace; break;

            default: dir.Type = Model.DirectiveType.Other; break;
            }

            while (la.kind == 4)
            {
                Get();
            }
            if (StartOf(4))
            {
                if (StartOf(5))
                {
                    medium(out m);
                    dir.Mediums.Add(m);
                    while (la.kind == 4)
                    {
                        Get();
                    }
                    while (la.kind == 25)
                    {
                        Get();
                        while (la.kind == 4)
                        {
                            Get();
                        }
                        medium(out m);
                        dir.Mediums.Add(m);
                        while (la.kind == 4)
                        {
                            Get();
                        }
                    }
                }
                else
                {
                    expr(out exp);
                    dir.Expression = exp;
                    while (la.kind == 4)
                    {
                        Get();
                    }
                }
            }
            if (la.kind == 26)
            {
                Get();
                while (la.kind == 4)
                {
                    Get();
                }
                if (StartOf(6))
                {
                    while (StartOf(1))
                    {
                        if (dir.Type == Model.DirectiveType.Page || dir.Type == Model.DirectiveType.FontFace)
                        {
                            declaration(out dec);
                            dir.Declarations.Add(dec);
                            while (la.kind == 4)
                            {
                                Get();
                            }
                            while (la.kind == 27)
                            {
                                Get();
                                while (la.kind == 4)
                                {
                                    Get();
                                }
                                if (la.val.Equals("}"))
                                {
                                    Get(); return;
                                }
                                declaration(out dec);
                                dir.Declarations.Add(dec);
                                while (la.kind == 4)
                                {
                                    Get();
                                }
                            }
                            if (la.kind == 27)
                            {
                                Get();
                                while (la.kind == 4)
                                {
                                    Get();
                                }
                            }
                        }
                        else if (StartOf(2))
                        {
                            ruleset(out rset);
                            dir.RuleSets.Add(rset);
                            while (la.kind == 4)
                            {
                                Get();
                            }
                        }
                        else
                        {
                            directive(out dr);
                            dir.Directives.Add(dr);
                            while (la.kind == 4)
                            {
                                Get();
                            }
                        }
                    }
                }
                Expect(28);
                while (la.kind == 4)
                {
                    Get();
                }
            }
            else if (la.kind == 27)
            {
                Get();
                while (la.kind == 4)
                {
                    Get();
                }
            }
            else
            {
                SynErr(50);
            }
        }
Пример #8
0
        static string Render(Model.Directive dir, int nesting)
        {
            string start = "";

            for (int i = 0; i < nesting; i++)
            {
                start += "\t";
            }

            switch (dir.Type)
            {
            case Model.DirectiveType.Charset: return(RenderCharSet(dir, start));

            case Model.DirectiveType.Page: return(RenderPage(dir, start));

            case Model.DirectiveType.Media: return(RenderMedia(dir, nesting));

            case Model.DirectiveType.Import: return(RenderImport(dir));

            case Model.DirectiveType.FontFace: return(RenderFontFace(dir, start));
            }

            System.Text.StringBuilder txt = new System.Text.StringBuilder();

            txt.AppendFormat("<span class=\"directive_name\">{0}</span> ", dir.Name);

            if (dir.Expression != null)
            {
                txt.AppendFormat("<span class=\"expression\">{0}</span> ", dir.Expression);
            }

            bool first = true;

            foreach (Model.Medium m in dir.Mediums)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    txt.Append(", ");
                }
                txt.AppendFormat(" <span class=\"medium\">{0}</span>", m.ToString());
            }

            bool HasBlock = (dir.Declarations.Count > 0 || dir.Directives.Count > 0 || dir.RuleSets.Count > 0);

            if (!HasBlock)
            {
                txt.Append(";");
                return(txt.ToString());
            }

            txt.Append(" {\r\n" + start);

            foreach (Model.Directive dr in dir.Directives)
            {
                txt.AppendFormat("<span class=\"directive\">{0}</span>\r\n", Render(dr, nesting + 1));
            }

            foreach (Model.RuleSet rules in dir.RuleSets)
            {
                txt.AppendFormat("<span class=\"ruleset\">{0}</span>\r\n", rules.ToString(nesting + 1));
            }

            first = true;
            foreach (Model.Declaration dec in dir.Declarations)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    txt.Append(";");
                }
                txt.Append("\r\n\t" + start);
                txt.AppendFormat("<span class=\"declaration\">{0}</span>", Render(dec));
            }

            txt.Append("\r\n}");
            return(txt.ToString());
        }
Пример #9
0
 static string Render(Model.Directive dir)
 {
     return(Render(dir, 0));
 }
Пример #10
0
 static string RenderCharSet(Model.Directive dir, string start)
 {
     return(string.Format("{2}<span class=\"directive_name\">{0}</span> <span class=\"expression\">{1}</span>", dir.Name, Render(dir.Expression), start));
 }