Beispiel #1
0
        static void Main(string[] args)
        {
            TestRename();

            while (true)
            {
                string line = Console.ReadLine();
                try
                {
                    Lexer       l = new Lexer();
                    TokenReader r = l.Lex(line);
                    //Console.WriteLine("---------------------------------");
                    foreach (Token t in r.tokens)
                    {
                        Console.WriteLine(t.Print());
                        foreach (Token t2 in t.Leading)
                        {
                            Console.WriteLine("    " + t2.Print());
                        }
                    }
                    //Console.WriteLine("- PARSER OUTPUT -");
                    Parser p = new Parser(r);
                    Chunk  c = p.Parse();
                    //dump(c.Body);
                    Console.WriteLine("- Beautified -");
                    Visitors.Beautifier b = new Visitors.Beautifier();
                    Console.WriteLine(b.Beautify(c));
                    Console.WriteLine("- Lua Compatible -");
                    Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput();
                    Console.WriteLine(lco.Format(c));
                    Console.WriteLine("- Exact reconstruction -");
                    Console.WriteLine(new Visitors.ExactReconstruction().Reconstruct(c));
                    Console.WriteLine("- Minified -");
                    Console.WriteLine(new Visitors.Minifier().Minify(c));
                }
                catch (LuaSourceException ex)
                {
                    Console.WriteLine(line);
                    Console.WriteLine(" ".Repeat(ex.Column - 1) + "^");
                    Console.WriteLine("<stdin>:" + ex.Line + ":" + ex.Column + ":" + ex.Message);
                    Console.WriteLine(ex.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
Beispiel #2
0
        public static int lua_load(LuaState L, lua_Reader reader, object data,
                                   CharPtr chunkname)
        {
            ZIO z = new ZIO();
            int status;
            lua_lock(L);
            if (chunkname == null) chunkname = "?";

            #if OVERRIDE_LOAD || true
            //#if false
            if (data is LoadS)
            {
                LoadS d = data as LoadS;
                if (d.size > 0 && d.s.chars[0] != LUA_SIGNATURE[0]) // if its not binary
                {
                    Lexer l = new Lexer();
                    try
                    {
                        //Console.WriteLine(d.s);
                        TokenReader tr = l.Lex(d.s);
                        Parser p = new Parser(tr);
                        Ast.Chunk c = p.Parse();

                        Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput();
                        string s = lco.Format(c);
                        d.s = s;
                        d.size = (lu_mem)s.Length;
                    }
                    catch (LuaSourceException ex)
                    {
                        throw ex;
                    }
                }
                else
                {
                    d.s.index = 0;

                    // Why isn't the size equal to the chars.Length?
                    Debug.WriteLine("Binary data: d.size=" + d.size + " d.s.chars.Length=" + d.s.chars.Length);
                    Debug.WriteLine("Equal: " + (d.size == d.s.chars.Length));
                    //Debug.Assert(d.size == d.s.chars.Length);
                    d.size = (uint)d.s.chars.Length;
                }
            }
            else if (data is LoadF)
            {
                LoadF lf = data as LoadF;

                if (lf.f.ReadByte() != LUA_SIGNATURE[0]) // if its not binary
                {
                    lf.f.Position = 0;
                    MemoryStream ms = new MemoryStream();
                    while (lf.f.Position < lf.f.Length)
                        ms.WriteByte((byte)lf.f.ReadByte());
                    ms.Position = 0;

                    // not binary file
                    ms.Position = 0;
                    StringBuilder sb = new StringBuilder();
                    while (ms.Position < ms.Length)
                        sb.Append((char)ms.ReadByte());

                    try
                    {
                        Lexer l = new Lexer();
                        TokenReader tr = l.Lex(sb.ToString());
                        Parser p = new Parser(tr);
                        Ast.Chunk c = p.Parse();
                        Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput();
                        string s = lco.Format(c);
                        ms = new MemoryStream();
                        // TODO: there HAS to be a better way...
                        foreach (char c2 in s)
                            ms.WriteByte((byte)c2);
                        ms.Position = 0;
                        lf.f = ms;
                    }
                    catch (LuaSourceException ex)
                    {
                        lua_pushstring(L, ex.GenerateMessage(chunkname));
                        return 1;
                        //throw ex;
                    }
                }
                else
                {
                    lf.f.Position = 0; // reset the read character
                }
            }
            #endif
            luaZ_init(L, z, reader, data);
            status = luaD_protectedparser(L, z, chunkname);
            lua_unlock(L);
            if (data is LoadF)
            {
                LoadF f = data as LoadF;
                if (f.f != null)
                {
                    f.f.Close();
                    f.f.Dispose();
                }
            }
            return status;
        }
Beispiel #3
0
        public static int lua_load(LuaState L, lua_Reader reader, object data,
                                   CharPtr chunkname)
        {
            ZIO z = new ZIO();
            int status;

            lua_lock(L);
            if (chunkname == null)
            {
                chunkname = "?";
            }

            #if OVERRIDE_LOAD || true
            //#if false
            if (data is LoadS)
            {
                LoadS d = data as LoadS;
                if (d.size > 0 && d.s.chars[0] != LUA_SIGNATURE[0]) // if its not binary
                {
                    Lexer l = new Lexer();
                    try
                    {
                        //Console.WriteLine(d.s);
                        TokenReader tr = l.Lex(d.s);
                        Parser      p  = new Parser(tr);
                        Ast.Chunk   c  = p.Parse();

                        Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput();
                        string s = lco.Format(c);
                        d.s    = s;
                        d.size = (lu_mem)s.Length;
                    }
                    catch (LuaSourceException ex)
                    {
                        throw ex;
                    }
                }
                else
                {
                    d.s.index = 0;

                    // Why isn't the size equal to the chars.Length?
                    Debug.WriteLine("Binary data: d.size=" + d.size + " d.s.chars.Length=" + d.s.chars.Length);
                    Debug.WriteLine("Equal: " + (d.size == d.s.chars.Length));
                    //Debug.Assert(d.size == d.s.chars.Length);
                    d.size = (uint)d.s.chars.Length;
                }
            }
            else if (data is LoadF)
            {
                LoadF lf = data as LoadF;

                if (lf.f.ReadByte() != LUA_SIGNATURE[0]) // if its not binary
                {
                    lf.f.Position = 0;
                    MemoryStream ms = new MemoryStream();
                    while (lf.f.Position < lf.f.Length)
                    {
                        ms.WriteByte((byte)lf.f.ReadByte());
                    }
                    ms.Position = 0;

                    // not binary file
                    ms.Position = 0;
                    StringBuilder sb = new StringBuilder();
                    while (ms.Position < ms.Length)
                    {
                        sb.Append((char)ms.ReadByte());
                    }

                    try
                    {
                        Lexer       l  = new Lexer();
                        TokenReader tr = l.Lex(sb.ToString());
                        Parser      p  = new Parser(tr);
                        Ast.Chunk   c  = p.Parse();
                        Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput();
                        string s = lco.Format(c);
                        ms = new MemoryStream();
                        // TODO: there HAS to be a better way...
                        foreach (char c2 in s)
                        {
                            ms.WriteByte((byte)c2);
                        }
                        ms.Position = 0;
                        lf.f        = ms;
                    }
                    catch (LuaSourceException ex)
                    {
                        lua_pushstring(L, ex.GenerateMessage(chunkname));
                        return(1);
                        //throw ex;
                    }
                }
                else
                {
                    lf.f.Position = 0; // reset the read character
                }
            }
            #endif
            luaZ_init(L, z, reader, data);
            status = luaD_protectedparser(L, z, chunkname);
            lua_unlock(L);
            if (data is LoadF)
            {
                LoadF f = data as LoadF;
                if (f.f != null)
                {
                    f.f.Close();
                    f.f.Dispose();
                }
            }
            return(status);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            TestRename();
            TestInline();
            TestFindImpl();
            TestFindRef();
            TestExtractDocComments();

            while (true)
            {
                string line = Console.ReadLine();
                try
                {
                    Lexer       l = new Lexer();
                    TokenReader r = l.Lex(line);
                    //Console.WriteLine("---------------------------------");
                    foreach (Token t in r.tokens)
                    {
                        Console.WriteLine(t.Print());
                        foreach (Token t2 in t.Leading)
                        {
                            Console.WriteLine("    " + t2.Print());
                        }
                    }
                    //Console.WriteLine("- PARSER OUTPUT -");
                    Parser p = new Parser(r);
                    Chunk  c = p.Parse();
                    Console.WriteLine("- Simplifying -");
                    c = (Chunk)c.Simplify();
                    Console.WriteLine("- Success! -");
                    //dump(c.Body);
                    Console.WriteLine("- Basic Beautifier (No Token Stream) -");
                    Visitors.BasicBeautifier b = new Visitors.BasicBeautifier();
                    Console.WriteLine(b.Beautify(c));
                    Console.WriteLine("- Lua Compatible -");
                    Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput();
                    Console.WriteLine(lco.Format(c));
                    Console.WriteLine("- Exact reconstruction -");
                    Console.WriteLine(new Visitors.ExactReconstruction().Reconstruct(c));
                    Console.WriteLine("- Minified -");
                    Console.WriteLine(new Visitors.Minifier().Minify(c));
                    Console.WriteLine("- Beautifier -");
                    Console.WriteLine(new Visitors.Beautifier().Beautify(c));
                    Console.WriteLine("- NonModifiedAstBeautifer -");
                    Console.WriteLine(new Visitors.NonModifiedAstBeautifier().Beautify(c));
                    Console.WriteLine("- Misspelled Variables -");
                    List <Tuple <Variable, Variable> > vars = Refactoring.FindMisspelledVariables(c);
                    //Console.WriteLine(vars.Count);
                    foreach (Tuple <Variable, Variable> v in vars)
                    {
                        Console.WriteLine(v.Item1.Name + " is close to " + v.Item2.Name);
                        Console.Write("\t");
                        if (v.Item1.References > v.Item2.References)
                        {
                            Console.WriteLine(v.Item1.Name + " is the best match with " + v.Item1.References + " references");
                        }
                        else if (v.Item1.References < v.Item2.References)
                        {
                            Console.WriteLine(v.Item2.Name + " is the best match with " + v.Item2.References + " references");
                        }
                        else
                        {
                            Console.WriteLine("Both have the same amount of references (" + v.Item1.References + ")!");
                        }
                    }
                    List <Variable> unused = Refactoring.FindUnusedVariables(c);
                    Console.WriteLine("- Unused Variables -");
                    foreach (Variable v in unused)
                    {
                        Console.WriteLine("  " + v.Name);
                    }

                    unused = Refactoring.FindUnusedLocalVariables(c);
                    Console.WriteLine("- Unused Local Variables -");
                    foreach (Variable v in unused)
                    {
                        Console.WriteLine("  " + v.Name);
                    }
                    Refactoring.AddModuleDependency(c, "module1");
                    Refactoring.AddModuleDependency(c, "module2", "local_module2");

                    Refactoring.AddClrDependency(c, "AClrLib", "AClrLib.NamespaceA.AClrType");

                    Console.WriteLine("- With Added Dependencies -");
                    Console.WriteLine(new Visitors.Beautifier().Beautify(c));

                    c.Scope.ObfuscateLocals();
                    Console.WriteLine("- With obfuscated locals");
                    Console.WriteLine(new Visitors.Beautifier().Beautify(c));
                }
                catch (LuaSourceException ex)
                {
                    Console.WriteLine(line);
                    Console.WriteLine(" ".Repeat(ex.Column - 1) + "^");
                    Console.WriteLine("<stdin>:" + ex.Line + ":" + ex.Column + ":" + ex.Message);
                    Console.WriteLine(ex.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            TestRename();
            TestInline();
            TestFindImpl();
            TestFindRef();
            TestExtractDocComments();

            while (true)
            {
                string line = Console.ReadLine();
                try
                {
                    Lexer l = new Lexer();
                    TokenReader r = l.Lex(line);
                    //Console.WriteLine("---------------------------------");
                    foreach (Token t in r.tokens)
                    {
                        Console.WriteLine(t.Print());
                        foreach (Token t2 in t.Leading)
                            Console.WriteLine("    " + t2.Print());
                    }
                    //Console.WriteLine("- PARSER OUTPUT -");
                    Parser p = new Parser(r);
                    Chunk c = p.Parse();
                    Console.WriteLine("- Simplifying -");
                    c = (Chunk)c.Simplify();
                    Console.WriteLine("- Success! -");
                    //dump(c.Body);
                    Console.WriteLine("- Basic Beautifier (No Token Stream) -");
                    Visitors.BasicBeautifier b = new Visitors.BasicBeautifier();
                    Console.WriteLine(b.Beautify(c));
                    Console.WriteLine("- Lua Compatible -");
                    Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput();
                    Console.WriteLine(lco.Format(c));
                    Console.WriteLine("- Exact reconstruction -");
                    Console.WriteLine(new Visitors.ExactReconstruction().Reconstruct(c));
                    Console.WriteLine("- Minified -");
                    Console.WriteLine(new Visitors.Minifier().Minify(c));
                    Console.WriteLine("- Beautifier -");
                    Console.WriteLine(new Visitors.Beautifier().Beautify(c));
                    Console.WriteLine("- NonModifiedAstBeautifer -");
                    Console.WriteLine(new Visitors.NonModifiedAstBeautifier().Beautify(c));
                    Console.WriteLine("- Misspelled Variables -");
                    List<Tuple<Variable, Variable>> vars = Refactoring.FindMisspelledVariables(c);
                    //Console.WriteLine(vars.Count);
                    foreach (Tuple<Variable, Variable> v in vars)
                    {
                        Console.WriteLine(v.Item1.Name + " is close to " + v.Item2.Name);
                        Console.Write("\t");
                        if (v.Item1.References > v.Item2.References)
                            Console.WriteLine(v.Item1.Name + " is the best match with " + v.Item1.References + " references");
                        else if (v.Item1.References < v.Item2.References)
                            Console.WriteLine(v.Item2.Name + " is the best match with " + v.Item2.References + " references");
                        else
                            Console.WriteLine("Both have the same amount of references (" + v.Item1.References + ")!");

                    }
                    List<Variable> unused = Refactoring.FindUnusedVariables(c);
                    Console.WriteLine("- Unused Variables -");
                    foreach (Variable v in unused)
                        Console.WriteLine("  " + v.Name);

                    unused = Refactoring.FindUnusedLocalVariables(c);
                    Console.WriteLine("- Unused Local Variables -");
                    foreach (Variable v in unused)
                        Console.WriteLine("  " + v.Name);
                    Refactoring.AddModuleDependency(c, "module1");
                    Refactoring.AddModuleDependency(c, "module2", "local_module2");

                    Refactoring.AddClrDependency(c, "AClrLib", "AClrLib.NamespaceA.AClrType");

                    Console.WriteLine("- With Added Dependencies -");
                    Console.WriteLine(new Visitors.Beautifier().Beautify(c));

                    c.Scope.ObfuscateLocals();
                    Console.WriteLine("- With obfuscated locals");
                    Console.WriteLine(new Visitors.Beautifier().Beautify(c));
                }
                catch (LuaSourceException ex)
                {
                    Console.WriteLine(line);
                    Console.WriteLine(" ".Repeat(ex.Column - 1) + "^");
                    Console.WriteLine("<stdin>:" + ex.Line + ":" + ex.Column + ":" + ex.Message);
                    Console.WriteLine(ex.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
Beispiel #6
0
        public static int lua_load(LuaState L, lua_Reader reader, object data,
                                   CharPtr chunkname)
        {
            ZIO z = new ZIO();
            int status;

            lua_lock(L);
            if (chunkname == null)
            {
                chunkname = "?";
            }

            if (data is LoadS)
            {
                LoadS d = data as LoadS;
                if (d.size > 0)
                {
                    Lexer l = new Lexer();
                    try
                    {
                        //Console.WriteLine(d.s);
                        TokenReader tr = l.Lex(d.s);
                        Parser      p  = new Parser(tr);
                        Ast.Chunk   c  = p.Parse();

                        Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput();
                        string s = lco.Format(c);
                        d.s    = s;
                        d.size = (lu_mem)s.Length;
                    }
                    catch (LuaSourceException ex)
                    {
                        throw ex;
                    }
                }
            }
            else if (data is LoadF)
            {
                LoadF lf = data as LoadF;

                MemoryStream ms = new MemoryStream();
                while (lf.f.Position < lf.f.Length)
                {
                    ms.WriteByte((byte)lf.f.ReadByte());
                }
                ms.Position = 0;
                if (ms.ReadByte() != 27)
                {
                    // not binary file
                    ms.Position = 0;
                    StringBuilder sb = new StringBuilder();
                    while (ms.Position < ms.Length)
                    {
                        sb.Append((char)ms.ReadByte());
                    }

                    try
                    {
                        Lexer       l  = new Lexer();
                        TokenReader tr = l.Lex(sb.ToString());
                        Parser      p  = new Parser(tr);
                        Ast.Chunk   c  = p.Parse();

                        Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput();
                        string s = lco.Format(c);
                        ms = new MemoryStream();
                        // TODO: there HAS to be a better way...
                        foreach (char c2 in s)
                        {
                            ms.WriteByte((byte)c2);
                        }
                        ms.Position = 0;
                        lf.f        = ms;
                    }
                    catch (LuaSourceException ex)
                    {
                        throw ex;
                    }
                }
            }

            luaZ_init(L, z, reader, data);
            status = luaD_protectedparser(L, z, chunkname);
            lua_unlock(L);
            return(status);
        }