예제 #1
0
        public override void Init(AstContext context, ParseTreeNode treeNode)
        {
            base.Init(context, treeNode);

            // All for loop parse tree node has seven children node
            if (treeNode.ChildNodes.Count != 7)
            {
                return;
            }

            // Generic for loop ("for a, b in 1, 2 do end")
            if (treeNode.ChildNodes[1].AstNode is LuaIdentifierNodeList)
            {
                IdentifierList = treeNode.ChildNodes[1].AstNode as LuaIdentifierNodeList;
                AddChild("forloop id list", IdentifierList);
                ExpressionList = treeNode.ChildNodes[3].AstNode as LuaExpressionNodeList;
                AddChild("forloop expr list", ExpressionList);
                Block = treeNode.ChildNodes[5].AstNode as LuaBlockNode;
                AddChild("forloop blck", Block);
            }
            else if (treeNode.ChildNodes[1].AstNode is LuaIdentifierNode)
            {
                Identifier = treeNode.ChildNodes[1].AstNode as LuaIdentifierNode;
                AddChild("forloop id", Identifier);
                ExpressionList = treeNode.ChildNodes[3].AstNode as LuaExpressionNodeList;
                AddChild("forloop expr list", ExpressionList);
                Block = treeNode.ChildNodes[5].AstNode as LuaBlockNode;
                AddChild("forloop blck", Block);
            }
        }
예제 #2
0
        public override void Init(AstContext context, ParseTreeNode treeNode)
        {
            base.Init(context, treeNode);

            Block = treeNode.ChildNodes[1].AstNode as LuaBlockNode;
            AddChild("do block", Block);
        }
예제 #3
0
 public void GetDeclarations(LuaBlockNode block, LuaModel model)
 {
     foreach (var variable in VariableList)
     {
         block.Locals.Add(new Declaration(variable.AsString, String.Empty, model.FilePath,
                                          new ScopeSpan(variable.Span.EndPosition, variable.EndLine, block.Span.EndPosition, block.EndLine)));
     }
 }
예제 #4
0
 public override void Init(AstContext context, ParseTreeNode treeNode)
 {
     base.Init(context, treeNode);
     if (treeNode.ChildNodes.Count != 4)
     {
         return;
     }
     Expression  = AddChild("elseif expr", treeNode.ChildNodes[1]) as LuaNode;
     ElseIfBlock = AddChild("elseif block", treeNode.ChildNodes[3]) as LuaBlockNode;
 }
예제 #5
0
        public override void Init(AstContext context, ParseTreeNode treeNode)
        {
            base.Init(context, treeNode);
            // Normally Chunk only has one child node, which is a block node
            if (treeNode.ChildNodes.Count > 0)
            {
                Block = AddChild(string.Empty, treeNode.ChildNodes[0]) as LuaBlockNode;
            }

            AsString = "LuaChunk";
        }
예제 #6
0
        private DeclarationType GetDeclarationType(LuaNode variable, LuaBlockNode block, out Declaration namespaces)
        {
            namespaces = null;
            if (variable is LuaIdentifierNode)
            {
                foreach (var localDeclaration in block.Locals)
                {
                    if (variable.AsString == localDeclaration.Name)
                    {
                        return DeclarationType.None;
                    }
                }

                foreach (var globalDeclaration in block.Globals)
                {
                    if (variable.AsString == globalDeclaration.Name)
                    {
                        return DeclarationType.None;
                    }
                }
            }
            else if (variable is LuaTableAccessNode)
            {
                foreach (var localDeclaration in block.Locals)
                {
                    //List<string> names = new List<string>(variable.AsString.Split('.'));
                    Declaration dummyDeclaration = DeclarationHelper.BuildDeclaration(variable.AsString);
                    if (dummyDeclaration.Equal(localDeclaration))
                        return DeclarationType.None;
                    if (dummyDeclaration.NameSpace != null && dummyDeclaration.NameSpace.Equal(localDeclaration))
                    {
                        namespaces = localDeclaration;
                        return DeclarationType.Local;
                    }
                }

                foreach (var globalDeclaration in block.Globals)
                {
                    //List<string> names = new List<string>(variable.AsString.Split('.'));
                    Declaration dummyDeclaration = DeclarationHelper.BuildDeclaration(variable.AsString);
                    if (dummyDeclaration.Equal(globalDeclaration))
                        return DeclarationType.None;
                    if (dummyDeclaration.NameSpace != null && dummyDeclaration.NameSpace.Equal(globalDeclaration))
                    {
                        namespaces = globalDeclaration;
                        return DeclarationType.Global;
                    }
                }
            }

            return DeclarationType.Global;
        }
예제 #7
0
        public override void Init(AstContext context, ParseTreeNode treeNode)
        {
            base.Init(context, treeNode);
            // child #0 is usually a keyword like "local" or "local function"
            // child #i is the keyword "end"
            Type = FunctionType.Declaration;
            if (treeNode.ChildNodes.Count == 4)
            {
                Type = FunctionType.Anonymous;
            }
            else if (treeNode.ChildNodes.Count == 5)
            {
                Type = FunctionType.Declaration;
            }
            else if (treeNode.ChildNodes.Count == 6)
            {
                Type = FunctionType.LocalDeclaration;
            }

            int i = treeNode.ChildNodes.Count - 1;

            Debug.Assert(i > 1);
            var anon = treeNode.ChildNodes[i - 3];

            if (anon != null && anon.Token != null && anon.Token.KeyTerm != null)
            {
                NameNode = new LuaFuncIdentifierNode().InitAnonymous();;
            }
            else
            {
                NameNode = AddChild("Name", anon) as LuaFuncIdentifierNode;
            }
            var name = NameNode.AsString;

            if (treeNode.ChildNodes[i - 2].ChildNodes.Count > 0 &&
                treeNode.ChildNodes[i - 2].ChildNodes[0].AstNode is LuaIdentifierNodeList)
            {
                Parameters = treeNode.ChildNodes[i - 2].ChildNodes[0].AstNode as LuaIdentifierNodeList;
            }
            Body = AddChild("Body", treeNode.ChildNodes[i - 1]) as LuaBlockNode;

            if (treeNode.Comments.Count > 0)
            {
                Description = treeNode.Comments[0].Text;
                for (int k = 1; k < treeNode.Comments.Count; ++k)
                {
                    Description += "\n" + treeNode.Comments[k].Text;
                }
            }

            AsString = "<Function " + name + ">";
        }
예제 #8
0
 public void GetDeclarations(LuaBlockNode block, LuaModel model)
 {
     // require("sample.lua")
     if (Target != null &&
         (Target.AsString == "require" ||
          Target.AsString == "NPL.load") &&
         Arguments.ChildNodes.Count == 1 &&
         Arguments.ChildNodes[0] is LuaLiteralNode &&
         ((LuaLiteralNode)Arguments.ChildNodes[0]).Type == LuaType.String)
     {
         string fileName = ((LuaLiteralNode)Arguments.ChildNodes[0]).Value;
         fileName = fileName.Substring(1, fileName.Length - 2);
         try
         {
             string filePath = Path.Combine(Path.GetDirectoryName(model.FilePath), fileName);
             // project mode
             if (model.Entry != null && model.Entry.Analyzer != null && model.Entry.Analyzer.ContainsFile(filePath))
             {
                 AnalysisEntry requiredEntry = model.Entry.Analyzer.GetAnalysisEntry(filePath);
                 if (requiredEntry.Model != null)
                 {
                     block.Requires.AddRange(requiredEntry.Model.GetGlobalDeclarations());
                     model.AddIncludedFile(filePath, requiredEntry.Model);
                 }
             }
             // singleton mode
             else
             {
                 string source = File.ReadAllText(filePath);
                 Irony.Parsing.Parser parser        = new Irony.Parsing.Parser(LuaGrammar.Instance);
                 ParseTree            tree          = parser.Parse(source);
                 LuaModel             requiredModel = new LuaModel(tree, filePath);
                 block.Requires.AddRange(requiredModel.GetGlobalDeclarations());
                 model.AddIncludedFile(filePath, requiredModel);
             }
         }
         catch (Exception e)
         {
         }
     }
 }
예제 #9
0
        public void GetDeclarations(LuaBlockNode block, LuaModel model)
        {
            for (int i = 0; i < VariableList.Count; ++i)
            {
                LuaNode variable = VariableList[i];
                Declaration namespaces, sibling = null; 
                bool isDeclarationAssign = false;
                if (i < ExpressionList.Count)
                    isDeclarationAssign = DeclarationHelper.TryGetExpressionDeclaration(ExpressionList[i], block, model, out sibling);

                DeclarationType type = GetDeclarationType(variable, block, out namespaces);

                if (type == DeclarationType.Global && variable is LuaIdentifierNode)
                {
                    Declaration declaration = new Declaration(variable.AsString, String.Empty, model.FilePath, new ScopeSpan(variable.Span.EndPosition, variable.EndLine, int.MaxValue, int.MaxValue));
                    if (isDeclarationAssign) sibling.AddSibling(declaration);
                    block.Globals.Add(declaration);
                }
                else if (type == DeclarationType.Global && variable is LuaTableAccessNode)
                {
                    string[] names = variable.AsString.Split('.');
                    Declaration declaration = new Declaration(names[names.Length - 1], String.Empty, model.FilePath, new ScopeSpan(variable.Span.EndPosition, variable.EndLine,
                        int.MaxValue, int.MaxValue), namespaces);
                    if (isDeclarationAssign) sibling.AddSibling(declaration);
                    block.Globals.Add(declaration);
                }
                else if (type == DeclarationType.Local)
                {
                    string[] names = variable.AsString.Split('.');
                    Declaration declaration = new Declaration(names[names.Length - 1], String.Empty, model.FilePath, new ScopeSpan(variable.Span.EndPosition, variable.EndLine,
                        block.Span.EndPosition, block.EndLine), namespaces);
                    if (isDeclarationAssign) sibling.AddSibling(declaration);
                    block.Locals.Add(declaration);
                }
            }
        }
예제 #10
0
        public override void Init(AstContext context, ParseTreeNode treeNode)
        {
            base.Init(context, treeNode);

            Expression = AddChild("Test", treeNode.ChildNodes[1]) as LuaNode;

            IfTrue = AddChild("IfTrue", treeNode.ChildNodes[3]) as LuaBlockNode;

            foreach (ParseTreeNode variable in treeNode.ChildNodes)
            {
                if (variable.ToString() == "ElseIfClause*" && variable.ChildNodes.Count > 0)
                {
                    foreach (var elseIf in variable.ChildNodes)
                    {
                        AddChild("ElseIf", elseIf);
                    }
                }

                if (variable.ToString() == "ElseClause" && variable.ChildNodes.Count > 0)
                {
                    IfFalse = AddChild("IfFalse", variable.ChildNodes[1]) as LuaNode;
                }
            }
        }
예제 #11
0
        private void AddDeclarationsForTableField(LuaBlockNode block, LuaModel model, Declaration namespaces, LuaNode expr)
        {
            if (expr is LuaTableNode)
            {
                foreach (var field in ((LuaTableNode)expr).FieldList)
                {
                    AddDeclarationsForTableField(block, model, namespaces, field);
                }
            }

            if (expr is LuaField && ((LuaField)expr).Name != null)
            {
                LuaNode     variable    = ((LuaField)expr).Name;
                Declaration declaration = new Declaration(variable.AsString, String.Empty, model.FilePath,
                                                          new ScopeSpan(variable.Span.EndPosition,
                                                                        variable.EndLine,
                                                                        block.Span.EndPosition,
                                                                        block.EndLine),
                                                          namespaces);

                block.Locals.Add(declaration);
                AddDeclarationsForTableField(block, model, declaration, ((LuaField)expr).Expression);
            }
        }
예제 #12
0
        public void GetDeclarations(LuaBlockNode block, LuaModel model)
        {
            for (int i = 0; i < VariableList.Count && i < ExpressionList.Count; ++i)
            {
                LuaNode     variable    = VariableList[i];
                Declaration declaration = new Declaration(variable.AsString, String.Empty, model.FilePath,
                                                          new ScopeSpan(variable.Span.EndPosition,
                                                                        variable.EndLine,
                                                                        block.Span.EndPosition,
                                                                        block.EndLine));
                //block.Locals.Add(declaration);

                if (i < ExpressionList.Count)
                {
                    if (ExpressionList[i] is LuaTableNode)
                    {
                        declaration.Type = LuaDeclarationType.Table;
                        AddDeclarationsForTableField(block, model, declaration, ExpressionList[i]);
                    }

                    else if (ExpressionList[i] is LuaIdentifierNode ||
                             ExpressionList[i] is LuaTableAccessNode)
                    {
                        Declaration sibling = null;
                        if (DeclarationHelper.TryGetExpressionDeclaration(ExpressionList[i], block, model, out sibling))
                        {
                            declaration.Type = sibling.Type;
                            sibling.AddSibling(declaration);
                        }
                    }
                }

                // add declaration as the end in case of circling sibling which lead to stackoverflow
                block.Locals.Add(declaration);
            }
        }
예제 #13
0
 public static bool TryGetExpressionDeclaration(LuaNode expr, LuaBlockNode block, LuaModel model, out Declaration declaration)
 {
     declaration = null;
     if (expr is LuaIdentifierNode)
     {
         foreach (var localDeclaration in block.Locals)
         {
             if (expr.AsString == localDeclaration.Name)
             {
                 declaration = localDeclaration;
                 return(true);
             }
         }
         foreach (var globalDeclaration in block.Globals)
         {
             if (expr.AsString == globalDeclaration.Name)
             {
                 declaration = globalDeclaration;
                 return(true);
             }
         }
         foreach (var globalDeclaration in block.Requires)
         {
             if (expr.AsString == globalDeclaration.Name)
             {
                 declaration = globalDeclaration;
                 // clear siblings in case of adding duplicate declaration
                 declaration.ClearSiblingsinFile(model.FilePath);
                 return(true);
             }
         }
     }
     else if (expr is LuaTableAccessNode)
     {
         foreach (var localDeclaration in block.Locals)
         {
             Declaration dummyDeclaration = BuildDeclaration(expr.AsString);
             if (dummyDeclaration.Equal(localDeclaration))
             {
                 declaration = localDeclaration;
                 return(true);
             }
         }
         foreach (var globalDeclaration in block.Globals)
         {
             Declaration dummyDeclaration = BuildDeclaration(expr.AsString);
             if (dummyDeclaration.Equal(globalDeclaration))
             {
                 declaration = globalDeclaration;
                 return(true);
             }
         }
         foreach (var globalDeclaration in block.Requires)
         {
             Declaration dummyDeclaration = BuildDeclaration(expr.AsString);
             if (dummyDeclaration.Equal(globalDeclaration))
             {
                 declaration = globalDeclaration;
                 // clear siblings in case of adding duplicate declaration
                 declaration.ClearSiblingsinFile(model.FilePath);
                 return(true);
             }
         }
     }
     return(false);
 }
예제 #14
0
        public void GetDeclarations(LuaBlockNode block, LuaModel model)
        {
            // add parameter declaration
            if (Parameters != null && Body != null)
            {
                foreach (var identifier in Parameters.Identifiers)
                {
                    Body.Locals.Add(new Declaration(identifier.Identifier, String.Empty, model.FilePath,
                                                    new ScopeSpan(identifier.Span.EndPosition, identifier.EndLine, Body.Span.EndPosition, Body.EndLine)));
                }
            }

            // Anonymous Function
            if (Type == FunctionType.Anonymous)
            {
                return;
            }
            // function declaration
            else if (Type == FunctionType.Declaration)
            {
                if (NameNode.Namespaces == null)
                {
                    block.Globals.Add(new Declaration(NameNode.Name, Description, model.FilePath,
                                                      new ScopeSpan(NameNode.Span.EndPosition, NameNode.EndLine, int.MaxValue, int.MaxValue), LuaDeclarationType.Function));
                }
                else
                {
                    // if its namespace is in local scope, add it to local declaration list
                    foreach (var local in block.Locals)
                    {
                        if (local.Equal(BuildDeclaration(NameNode.Namespaces)))
                        {
                            block.Locals.Add(new Declaration(NameNode.Name, Description, model.FilePath,
                                                             new ScopeSpan(NameNode.Span.EndPosition, NameNode.EndLine, block.Span.EndPosition, block.EndLine), LuaDeclarationType.Function, local));
                            break;
                        }
                    }
                    // if its namespace is in global scope, add it to global declaration list
                    foreach (var global in block.Globals)
                    {
                        if (global.Equal(BuildDeclaration(NameNode.Namespaces)))
                        {
                            block.Globals.Add(new Declaration(NameNode.Name, Description, model.FilePath,
                                                              new ScopeSpan(NameNode.Span.EndPosition, NameNode.EndLine, block.Span.EndPosition, block.EndLine), LuaDeclarationType.Function, global));
                            break;
                        }
                    }
                }
            }
            // local function declaration
            else if (Type == FunctionType.LocalDeclaration)
            {
                Declaration declaration = new Declaration(NameNode.Name, Description, model.FilePath,
                                                          new ScopeSpan(NameNode.Span.EndPosition, NameNode.EndLine, block.Span.EndPosition, block.EndLine), LuaDeclarationType.Function);

                //foreach (var local in block.Locals)
                //{
                //    if (local.Equal(NameNode.Namespaces))
                //    {
                //        declaration.NameSpaces.Add(local);
                //        break;
                //    }
                //}
                block.Locals.Add(declaration);
            }
        }