Пример #1
0
 public static List<Variable> FindUnusedVariables(Chunk c)
 {
     List<Variable> unused = new List<Variable>();
     foreach (Variable v in c.Scope.GetAllVariables())
     {
         if (v.References == 0 // wait.. wut?
             || v.References == 1)
             unused.Add(v);
     }
     return unused;
 }
Пример #2
0
 public static List<Variable> FindUnusedLocalVariables(Chunk c)
 {
     List<Variable> unused = new List<Variable>();
     foreach (Variable v in c.Scope.GetAllVariables())
     {
         if (v.References == 0
             || v.References == 1)
             if (v.IsGlobal == false)
                 unused.Add(v);
     }
     return unused;
 }
Пример #3
0
        /// <summary>
        /// Adds a dependency using the "require" function (for loading Lua modules)
        /// </summary>
        /// <param name="c"></param>
        /// <param name="name">The name of the module to reference</param>
        /// <param name="varName">Optional. The variable name to assign to the module</param>
        public static void AddModuleDependency(Chunk c, string name, string varName = "")
        {
            if (varName == "")
            {
                // FunctionCall
                StringCallExpr call = new StringCallExpr();

                Variable require = c.Scope.GetVariable("require");
                VariableExpression v = new VariableExpression();
                if (require == null)
                {
                    require = c.Scope.CreateGlobal("require");
                    require.IsGlobal = true;
                }
                v.Var = require;
                call.Base = v;

                call.Arguments.Add(new StringExpr(name) { StringType = TokenType.DoubleQuoteString });

                c.Body.Insert(0, new CallStatement() { Expression = call, Scope = c.Scope });
            }
            else
            {
                // Assignment with FunctionCall
                AssignmentStatement a = new AssignmentStatement();

                StringCallExpr call = new StringCallExpr();
                call.Scope = c.Scope;
                Variable require = c.Scope.GetVariable("require");
                VariableExpression v = new VariableExpression();
                if (require == null)
                {
                    require = c.Scope.CreateGlobal("require");
                    require.IsGlobal = true;
                }
                v.Var = require;
                call.Base = v;
                call.Arguments.Add(new StringExpr(name) { StringType = TokenType.DoubleQuoteString });
                a.IsLocal = true; // local import
                a.Rhs.Add(call);

                Variable var = c.Scope.GetVariable(varName);
                VariableExpression v2 = new VariableExpression();
                if (var == null)
                {
                    var = c.Scope.CreateLocal(varName);
                }
                v2.Var = var;
                a.Lhs.Add(v2);

                c.Body.Insert(0, a);
            }
        }
Пример #4
0
        // clr.load "<assembly>"

        // ? clr.usingns(<ns>)

        // local __ns = clr.getns "<type.ns>"
        // local <type> = __ns.<type.typename>

        /// <summary>
        /// Adds a dependency to a .NET/CLR library using clr.load
        /// </summary>
        /// <param name="c"></param>
        /// <param name="assembly"></param>
        public static void AddClrDependency(Chunk c, string assembly)
        {
            // FunctionCall
            StringCallExpr call = new StringCallExpr();

            Variable require = c.Scope.GetVariable("clr");
            VariableExpression v = new VariableExpression();
            if (require == null)
            {
                require = c.Scope.CreateGlobal("clr");
                require.IsGlobal = true;
            }
            v.Var = require;
            MemberExpr me = new MemberExpr();
            me.Base = v;
            me.Indexer = ".";
            me.Ident = "load";
            call.Base = me;

            call.Arguments.Add(new StringExpr(assembly) { StringType = TokenType.DoubleQuoteString });

            c.Body.Insert(0, new CallStatement() { Expression = call, Scope = c.Scope });
        }
        public static List<DocumentationComment> Extract(Chunk c)
        {
            if (c.ScannedTokens != null && c.ScannedTokens.Count > 0)
            {
                List<DocumentationComment> cmnts = new List<DocumentationComment>();
                for (int p = 0; p < c.ScannedTokens.Count; p++)
                {
                    Token t_ = c.ScannedTokens[p];
                    Token t = t_.Leading.Count > 0 ? t_.Leading[0] : null;
                    if (t != null)
                    {
                        int i = 0;
                        DocumentationComment cmt = new DocumentationComment();
                        do
                        {
                            if (t_.Leading.Count <= i)
                                break;
                            t = t_.Leading[i++];
                            if (t.Type == TokenType.DocumentationComment)
                                cmt.Lines.Add(t.Data);
                        } while (t.Type == TokenType.WhitespaceN
                            || t.Type == TokenType.WhitespaceR
                            || t.Type == TokenType.WhitespaceSpace
                            || t.Type == TokenType.WhitespaceTab
                            || t.Type == TokenType.DocumentationComment);

                        // find the ident it's for
                        if (c.ScannedTokens.Count > p)
                        {
                            t = c.ScannedTokens[p];
                            if (t.Type == TokenType.Keyword && t.Data == "local")
                                if (c.ScannedTokens[p + 1].Type == TokenType.Keyword && c.ScannedTokens[p + 1].Data == "function")
                                {
                                    int i2 = 2;
                                    while (
                                        (c.ScannedTokens[p + i2].Type == TokenType.Symbol && c.ScannedTokens[p + i2].Data == ".")
                                        || (c.ScannedTokens[p + i2].Type == TokenType.Ident))
                                        i2++;
                                    cmt.Ident = c.ScannedTokens[p + i2 - 1].Data;
                                    p += i2;
                                }
                                else
                                {
                                    int i2 = 2;
                                    while (
                                        (c.ScannedTokens[p + i2].Type == TokenType.Symbol && c.ScannedTokens[p + i2].Data == ".")
                                        || (c.ScannedTokens[p + i2].Type == TokenType.Ident))
                                        i2++;
                                    cmt.Ident = c.ScannedTokens[p + i2 - 1].Data;
                                    p += i2;
                                }
                            else if (t.Type == TokenType.Keyword && t.Data == "function")
                            {
                                int i2 = 1;
                                while (
                                    (c.ScannedTokens[p + i2].Type == TokenType.Symbol && c.ScannedTokens[p + i2].Data == ".")
                                    || (c.ScannedTokens[p + i2].Type == TokenType.Ident))
                                    i2++;
                                cmt.Ident = c.ScannedTokens[p + i2 - 1].Data;
                                p += i2;
                            }
                            else if (t.Type == TokenType.Ident)
                            {
                                int i2 = 1;
                                while (
                                    (c.ScannedTokens[p + i2].Type == TokenType.Symbol && c.ScannedTokens[p + i2].Data == ".")
                                    || (c.ScannedTokens[p + i2].Type == TokenType.Ident))
                                    i2++;
                                cmt.Ident = c.ScannedTokens[p + i2 - 1].Data;
                                p += i2;
                            }

                        }
                        if (cmt.Ident != null
                            && string.IsNullOrEmpty(cmt.Ident) == false
                            && cmt.Lines.Count > 0)
                        {
                            //Console.WriteLine("YEP: " + cmt.Ident);
                            cmnts.Add(cmt);
                        }
                        else
                        {
                            /*
                            Console.Write("NOPE: (" + (cmt.Ident == null ? "" : cmt.Ident) + ")");
                            Console.WriteLine(
                                cmt.Ident == null ? "Ident is null"
                                : (string.IsNullOrEmpty(cmt.Ident)
                                  ? "Ident is empty"
                                  : (cmt.Lines.Count == 0
                                    ? "No doc lines"
                                    : "wut?"))
                                );
                            */
                        }
                    }
                }
                return cmnts;
            }
            return new List<DocumentationComment>();
        }
Пример #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 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);
        }
Пример #7
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);
        }
Пример #8
0
        /// <summary>
        /// Todo: Check references to make sure that it is only used once or twice
        /// It isn't working correctly... 
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static List<Tuple<Variable, Variable>> FindMisspelledVariables(Chunk c)
        {
            List<Tuple<Variable, Variable>> misspelled = new List<Tuple<Variable, Variable>>();
            List<Variable> vars = c.Scope.GetAllVariables();
            List<Tuple<Variable, Variable>> scanned = new List<Tuple<Variable, Variable>>();
            //Console.WriteLine(vars.Count);

            Func<Variable, Variable, bool> get = new Func<Variable, Variable, bool>(delegate(Variable a, Variable b)
                {
                    bool found = false;
                    foreach (Tuple<Variable, Variable> t in scanned)
                    {
                        if (t.Item1 == a)
                        {
                            if (t.Item2 == b)
                            {
                                found = true;
                            }
                        }
                        else if (t.Item1 == b)
                        {
                            if (t.Item2 == a)
                            {
                                found = true;
                            }
                        }

                        if (found)
                            return found;
                    }
                    return false;
                });

            foreach (Variable v in vars)
            {
                foreach (Variable v2 in vars)
                {
                    if (v != v2 && get(v, v2) == false)
                    {
                        if (v.Name != v2.Name)
                        {
                            string a = v.Name.ToLower().Trim();
                            string b = v2.Name.ToLower().Trim();
                            if (a == b)
                            {
                                misspelled.Add(new Tuple<Variable, Variable>(v, v2));
                            }
                            else
                            {
                                int fails = 0;
                                int beginFailing = -1;
                                for (int i = 0; i < a.Length; i++)
                                {
                                    if (b.Length <= i)
                                    {
                                        fails++;
                                        break;
                                    }
                                    char c2 = a[i];
                                    char c3 = b[i];
                                    if (char.ToLower(c2) == char.ToLower(c3))
                                    {
                                        // do nothing, i guess
                                    }
                                    else
                                    {
                                        if (fails == 0)
                                            beginFailing = i;
                                        fails++;
                                    }
                                }
                                if (((beginFailing == a.Length - fails && a.Length - fails > 0)
                                    || (beginFailing == b.Length - fails && b.Length - fails > 0)
                                    || fails < (a.Length > 6 ? a.Length / 4 : 3))
                                    && Math.Abs(a.Length - b.Length) <= 3
                                    && (a.Length > 1 && b.Length > 1))
                                {
                                    misspelled.Add(new Tuple<Variable, Variable>(v, v2));
                                }
                            }
                        }
                        scanned.Add(new Tuple<Variable, Variable>(v, v2));
                    }
                }
            }
            return misspelled;
        }
Пример #9
0
        // TODO: Check FunctionStatement Name's also

        public static Location FindImplementation(Chunk c, Variable v)
        {
            Location ret = null;
            foreach (Statement s in c.Body)
            {
                if (s is AssignmentStatement && !(s is AugmentedAssignmentStatement)) // couldn't be defined in AugmentedAssignment
                {
                    AssignmentStatement a = s as AssignmentStatement;
                    int tokPos = 0;
                    if (a.IsLocal)
                        tokPos++;
                    foreach (Expression e in a.Lhs)
                    {
                        int t = tokPos;
                        tokPos++;
                        if (e is VariableExpression)
                        {
                            VariableExpression var = e as VariableExpression;
                            if (var.Var == v)
                            {
                                if (a.ScannedTokens.Count >= t && a.ScannedTokens[t].Type == TokenType.Ident)
                                {
                                    Token tok = a.ScannedTokens[t];
                                    ret = tok.Location;
                                    break;
                                }
                            }
                        }
                        tokPos++;
                    }
                }
                else if (s is FunctionStatement)
                {
                    FunctionStatement fs = s as FunctionStatement;
                    Variable var = null;
                    int p = 1;
                    if (fs.IsLocal)
                        p++;
                    Expression e = fs.Name;
                    while (e is IndexExpr)
                    {
                        e = ((IndexExpr)e).Index;
                        p += 2; // <varname> '.'
                    }
                    if (e is VariableExpression)
                        var = ((VariableExpression)e).Var;

                    if (var != null)
                    {
                        if (var == v)
                        {
                            if (fs.ScannedTokens.Count >= p && fs.ScannedTokens[p].Type == TokenType.Ident)
                            {
                                Token tok = fs.ScannedTokens[p];
                                ret = tok.Location;
                                break;
                            }
                        }
                    }
                }

                if (s is Chunk && ret == null)
                {
                    ret = FindImplementation(s as Chunk, v);
                }
            }
            return ret == null ? new Location() { Line = -1, Column = -1 } : ret;
        }
Пример #10
0
 public static List<CompletionItem> ExtractSymbols(Chunk c)
 {
     return DoChunk(c.Body);
 }
Пример #11
0
 public string Minify(Chunk c)
 {
     return DoChunk(c.Body);
 }
Пример #12
0
 public string Reconstruct(Chunk c)
 {
     return DoChunk(c.Body);
 }
Пример #13
0
 public static List<Location> FindReferences(Chunk c, Variable v)
 {
     throw new NotImplementedException();
 }
Пример #14
0
        /// <summary>
        /// Adds a dependency to a .NET/CLR library using clr.load,
        /// then it sets type to the type in the assembly.
        /// </summary>
        /// <param name="c"></param>
        /// <param name="assembly"></param>
        /// <param name="type"></param>
        public static void AddClrDependency(Chunk c, string assembly, string type)
        {
            AddClrDependency(c, assembly);

            // Assignment with FunctionCall
            AssignmentStatement a = new AssignmentStatement();

            CallExpr call = new CallExpr();
            call.Scope = c.Scope;
            Variable require = c.Scope.GetVariable("clr");
            VariableExpression v = new VariableExpression();
            if (require == null)
            {
                require = c.Scope.CreateGlobal("clr");
                require.IsGlobal = true;
            }

            string name = "", varName = "";

            if (type.Contains('.'))
            {
                name = type.Substring(0, type.LastIndexOf('.'));
                varName = type.Substring(type.LastIndexOf('.') + 1);
            }
            else
            {
                name = assembly;
                varName = type;
            }

            v.Var = require;
            MemberExpr me = new MemberExpr();
            me.Base = v;
            me.Indexer = ".";
            me.Ident = "getns";
            call.Base = me;
            call.Arguments.Add(new StringExpr(name) { StringType = TokenType.DoubleQuoteString });
            a.IsLocal = true; // local import
            MemberExpr me2 = new MemberExpr();
            me2.Base = call;
            me2.Indexer = ".";
            me2.Ident = varName;
            a.Rhs.Add(me2);

            Variable var = c.Scope.GetVariable(varName);
            VariableExpression v2 = new VariableExpression();
            if (var == null)
            {
                var = c.Scope.CreateLocal(varName);
            }
            v2.Var = var;
            a.Lhs.Add(v2);

            // Insert after the load
            c.Body.Insert(1, a);
        }
Пример #15
0
 public string Compile(Chunk c)
 {
     DoChunk(c);
     throw new NotImplementedException();
 }
Пример #16
0
 void DoChunk(Chunk c)
 {
     foreach (Statement s in c.Body)
         DoStatement(s);
 }
Пример #17
0
 public string Beautify(Chunk c)
 {
     return DoChunk(c.Body);
 }
Пример #18
0
 public string Beautify(Chunk c)
 {
     tok = c.ScannedTokens;
     index = 0;
     return DoChunk(c.Body);
 }
Пример #19
0
 public string Format(Chunk c)
 {
     return DoChunk(c.Body);
 }