Example #1
0
        public override void EnterFunctionDef([NotNull] DaedalusParser.FunctionDefContext context)
        {
            var fd = context;

            var statements = fd.statementBlock()?.statement();
            var localVars  = new List <Variable>();

            if (statements != null)
            {
                for (int i = 0; i < statements.Length; i++)
                {
                    for (int j = 0; j < statements[i].ChildCount; j++)
                    {
                        if (statements[i].children[j] is DaedalusParser.VarDeclContext varDecl)
                        {
                            var varType  = varDecl.typeReference().GetText();
                            var varDecls = varDecl.varDecl();
                            for (int k = 0; k < varDecls.Length; k++)
                            {
                                var innerVal = varDecls[k].varValueDecl();
                                for (int v = 0; v < innerVal.Length; v++)
                                {
                                    localVars.Add(new Variable
                                    {
                                        Type       = varDecls[k].typeReference().GetText(),
                                        Column     = innerVal[v].Start.Column,
                                        Line       = innerVal[v].Start.Line,
                                        Name       = innerVal[v].nameNode().GetText(),
                                        Definition = new Defintion
                                        {
                                            Start = new DefinitionIndex(innerVal[v].Start.Line, innerVal[v].Start.Column),
                                            End   = new DefinitionIndex(innerVal[v].Start.Line + innerVal[v].GetText().Length, innerVal[v].Stop.Column)
                                        }
                                    });
                                }
                            }
                            var varValues = varDecl.varValueDecl();
                            for (int k = 0; k < varValues.Length; k++)
                            {
                                localVars.Add(new Variable
                                {
                                    Type       = varType,
                                    Column     = varValues[k].Start.Column,
                                    Line       = varValues[k].Start.Line,
                                    Name       = varValues[k].nameNode().GetText(),
                                    Definition = new Defintion
                                    {
                                        Start = new DefinitionIndex(varValues[k].Start.Line, varValues[k].Start.Column),
                                        End   = new DefinitionIndex(varValues[k].Start.Line + varValues[k].GetText().Length, varValues[k].Stop.Column)
                                    }
                                });
                            }
                        }
                        else if (statements[i].children[j] is DaedalusParser.ConstDefContext constDef)
                        {
                            var varType     = constDef.typeReference().GetText();
                            var constValues = constDef.constValueDef();
                            for (int k = 0; k < constValues.Length; k++)
                            {
                                var innerVal = constValues[k].constValueAssignment().expressionBlock().expression();

                                localVars.Add(new Constant
                                {
                                    Type       = varType,
                                    Column     = constValues[k].Start.Column,
                                    Line       = constValues[k].Start.Line,
                                    Name       = constValues[k].nameNode().GetText(),
                                    Value      = innerVal.GetText(),
                                    Definition = new Defintion
                                    {
                                        Start = new DefinitionIndex(constValues[k].Start.Line, constValues[k].Start.Column),
                                        End   = new DefinitionIndex(constValues[k].Start.Line + constValues[k].GetText().Length, constValues[k].Stop.Column)
                                    }
                                });
                            }
                        }
                    }
                }
            }

            var parameters     = new List <Variable>();
            var funcParameters = fd.parameterList()?.parameterDecl();

            if (funcParameters != null)
            {
                foreach (var pdef in funcParameters)
                {
                    parameters.Add(new Variable
                    {
                        Name   = pdef.nameNode().GetText(),
                        Type   = pdef.typeReference().GetText(),
                        Column = pdef.nameNode().Start.Column,
                        Line   = pdef.nameNode().Start.Line,
                    });
                }
            }
            var summary = fd.symbolSummary();

            if (!_docCommentBuilderCleaned)
            {
                _docCommentBuilder.Clear();
                _docCommentBuilderCleaned = true;
            }
            if (summary?.Length > 0)
            {
                for (var i = 0; i < summary.Length; i++)
                {
                    var line = summary[i]?.GetText();
                    if (line == null)
                    {
                        continue;
                    }
                    _docCommentBuilder.AppendLine(line.TrimStart('/', ' '));
                }
                _docCommentBuilderCleaned = false;
            }
            var fncNamenode = fd.nameNode();
            var fnc         = new Function
            {
                Name           = fncNamenode.GetText(),
                ReturnType     = fd.typeReference().GetText(),
                Line           = fncNamenode.Start.Line,
                Column         = fncNamenode.Start.Column,
                Parameters     = parameters,
                LocalVariables = localVars,
                Documentation  = _docCommentBuilderCleaned ? "" : _docCommentBuilder.ToString(),
                Definition     = new Defintion {
                    Start = new DefinitionIndex(fd.Start.Line, fd.Start.Column), End = new DefinitionIndex(fd.Stop.Line, fd.Stop.Column)
                }
            };

            CurrentFunctionDef = fnc;
            GlobalFunctions.Add(fnc);
        }