示例#1
0
        public Document Parse(string Html)
        {
            impliedTags = new Stack <ImpliedTag>();
            Document document = new Document();

            currentNode = document;

            Html = Html.Replace("&nbsp;", "\u00A0");

            LaxParser laxParser = new LaxParser();

            laxParser.StartDocument       += Lax_StartDocument;
            laxParser.StartElement        += Lax_StartElement;
            laxParser.Characters          += Lax_Characters;
            laxParser.IgnorableWhitespace += Lax_Characters;
            laxParser.EndElement          += Lax_EndElement;
            laxParser.EndDocument         += Lax_EndDocument;
            laxParser.Comment             += Lax_Comment;
            laxParser.Error += Lax_Error;

            byte[]       byteArray = Encoding.UTF8.GetBytes(Html);
            MemoryStream stream    = new MemoryStream(byteArray);

            laxParser.Parse(new StreamReader(stream));

            return(document);
        }
示例#2
0
        public static CompileResult Compile(string code)
        {
            var response = new CompileResult();
            try
            {
                try
                {
                    code = code.Trim(" \t\n\r".ToCharArray());
                    response.Source = code;

                    //Parse
                    var parser = new LaxParser();
                    var block = parser.ParseBlock(code);

                    //Run the code interpreted
                    var interpreter = new Interpreter(block);

                    foreach (var s in block.Statements)
                    {
                        var format = new CodeFormat(extraParentheses: true);

                        var result = interpreter.Evaluate(s);
                        if (result == null)
                            response.Result += "\n";
                        else
                            response.Result += CodeFormat.Format(result.ValueType.Type) + " " + CodeFormat.Format(result) + " " + CodeFormat.Format(result.ValueType.Unit) + "\n";

                        try
                        {
                            response.Interpreted += format.FormatExpression(s) + "\n";
                        }
                        catch (Exception ex)
                        {
                            response.Interpreted += ex.Message + "\n";
                        }

                    }

                    ExpressionOptimizer.PreCaclulate(block);

                    //Compile the code
                    var compileStart = DateTime.UtcNow;
                    var compiler = new CompilerNET();
                    var dyn = compiler.Compile(block);
                    var compiletime = DateTime.UtcNow - compileStart;

                    response.Result += "\n\nCompiled in : " + compiletime.ToStringMicro();
                    response.Result += "\n\nCompiled return value: " + dyn();

            #if DEBUGx
                    response.Result += SpeedTest.Run(block, dyn);
            #endif

                    return response;
                }
                catch (LaxError le)
                {
                    var ef = new ErrorFormat(le, code) { ExtraLines = 2 };
                    response.Error = ef.Format();
                    return response;
                }
            }
            catch (Exception ex)
            {
                response.Error = ex.GetType().Name + "\n" + ex.Message + "\n" + ex.StackTrace;
                return response;
            }
        }