Exemple #1
0
        public void ShouldParseScriptFile(string file, string version, bool compareTrees)
        {
            const string prefix = "Escape.Tests.Scripts.";

            var assembly     = Assembly.GetExecutingAssembly();
            var scriptPath   = prefix + file;
            var sw           = new Stopwatch();
            var tempFilePath = NodePath != null
                             ? Path.GetTempFileName() : null;

            try
            {
                using (var stream = assembly.GetManifestResourceStream(scriptPath))
                {
                    Assert.True(stream != null, string.Format("Script resource '{0}' not found", scriptPath));
                    using (var sr = new StreamReader(stream))
                    {
                        var source = sr.ReadToEnd();
                        sw.Reset();
                        sw.Start();
                        var parser  = new JavaScriptParser();
                        var program = parser.Parse(source);
                        var bsfp    = new ByteSizeFormatProvider();
                        Console.WriteLine("Parsed {0} {1} ({3}) in {2} ms", file, version, sw.ElapsedMilliseconds, bsfp.Format("SZ1", source.Length, null));
                        Assert.NotNull(program);

                        if (!compareTrees)
                        {
                            Console.WriteLine("WARNING! AST compairson skipped for {0} {1}.", file, version);
                            return;
                        }

                        if (tempFilePath == null)
                        {
                            Console.WriteLine(
                                "If Node.js is installed in the system path " +
                                "then a more comprehensive test can be carried " +
                                "out comparing Esprima's and Escape's AST.");
                            return;
                        }

                        File.WriteAllText(tempFilePath, source);
                        sw.Restart();
                        var esparseOutput = Esparse(tempFilePath);

                        Console.WriteLine(
                            "Esparse.js for {0} {1} took {2} ms, producing {3} of pretty-printed AST JSON",
                            file, version, sw.ElapsedMilliseconds,
                            bsfp.Format("SZ1", esparseOutput.Length, null));

                        var sb  = new StringBuilder(esparseOutput.Length * 110 / 100);
                        var esw = new StringWriter(sb);
                        JsonEncoder.Encode(program, false, esw);
                        esw.WriteLine();
                        Assert.Equal(esparseOutput.Length, sb.Length);
                        Assert.True(esparseOutput == sb.ToString(), "AST JSON mismatch");
                    }
                }
            }
            finally
            {
                if (tempFilePath != null)
                {
                    File.Delete(tempFilePath);
                }
            }
        }
Exemple #2
0
        static void Run(IEnumerable <string> args)
        {
            var options         = new ParserOptions();
            var includeLocation = false;
            var outputEncoding  = (Encoding)null;
            var inputEncoding   = (Encoding)null;
            var stats           = false;
            var gc = false;

            using (var arg = args.GetEnumerator())
            {
                bool popped;
                for (popped = arg.MoveNext(); popped; popped = arg.MoveNext())
                {
                    if (arg.Current == "--comment")
                    {
                        options.Comment = true;
                    }
                    else if (arg.Current == "--tokens")
                    {
                        options.Tokens = true;
                    }
                    else if (arg.Current == "--tolerant")
                    {
                        options.Tolerant = true;
                    }
                    else if (arg.Current == "--loc")
                    {
                        includeLocation = true;
                    }
                    else if (arg.Current == "--stats")
                    {
                        stats = true;
                    }
                    else if (arg.Current == "--gc")
                    {
                        gc = true;
                    }
                    else if (arg.Current == "--output-encoding")
                    {
                        if (!arg.MoveNext())
                        {
                            throw new Exception("Missing output encoding value specification.");
                        }
                        outputEncoding = Encoding.GetEncoding(arg.Current);
                    }
                    else if (arg.Current == "--input-encoding")
                    {
                        if (!arg.MoveNext())
                        {
                            throw new Exception("Missing input encoding value specification.");
                        }
                        inputEncoding = Encoding.GetEncoding(arg.Current);
                    }
                    else
                    {
                        if (arg.Current == "--")
                        {
                            while (arg.MoveNext()) /* NOP */ } {
                            popped = false;
                    }
                    break;
                }
            }

            var sourceFilePath = popped ? arg.Current : null;

            var source = !string.IsNullOrEmpty(sourceFilePath) &&
                         sourceFilePath != "-"
                           ? inputEncoding != null
                             ? File.ReadAllText(sourceFilePath, inputEncoding)
                             : File.ReadAllText(sourceFilePath)
                           : Console.In.ReadToEnd();

            Escape.Ast.Program program = null;

            var durations    = new TimeSpan[stats ? 4 : 1];
            var allocations  = gc && stats ? new long[durations.Length] : null;
            var nullHeapSize = (long?)null;

            for (var i = 0; i < durations.Length; i++)
            {
                var heapStartSize = allocations != null
                                      ? GC.GetTotalMemory(true)
                                      : nullHeapSize;

                var sw = Stopwatch.StartNew();
                program      = new JavaScriptParser().Parse(source, options);
                durations[i] = sw.Elapsed;

                if (heapStartSize != nullHeapSize)
                {
                    allocations[i] = GC.GetTotalMemory(true) - heapStartSize.Value;
                    if (i < durations.Length - 1)
                    {
                        program = null;     // so it can be collected
                    }
                }
            }

            Debug.Assert(program != null);

            using (var stdout = OpenStdOut(outputEncoding))
            {
                JsonEncoder.Encode(program, includeLocation, stdout);
                stdout.Flush();
            }

            if (stats)
            {
                var ms = from d in durations.Skip(1 /* cold */)
                         select d.TotalMilliseconds;
                var duration = TimeSpan.FromMilliseconds(ms.Average());
                var format   = duration.Days == 0 && duration.Hours == 0
                               ? "mm':'ss'.'ffff"
                               : null;
                Console.Error.WriteLine("Time: {0}; [{1}]",
                                        duration.ToString(format),
                                        string.Join(", ", from d in durations select d.ToString(format)));
            }

            if (allocations != null)
            {
                var bsfp = new ByteSizeFormatProvider();
                Console.Error.WriteLine("Heap: {0}; [{1}]",
                                        bsfp.Format("SZ2", allocations.Select(a => (double)a).Average(), null),
                                        string.Join(", ", allocations));
            }
        }
    }