Пример #1
0
        public override void ExitFunctionLit(GolangParser.FunctionLitContext context)
        {
            // functionLit
            //     : 'func' function

            string parametersSignature = "()";

            if (Signatures.TryGetValue(context.function()?.signature(), out Signature signature))
            {
                parametersSignature = signature.GenerateParameterNameList();

                if (signature.Parameters.Length != 1)
                {
                    parametersSignature = $"({parametersSignature})";
                }
            }
            else
            {
                AddWarning(context, $"Failed to find signature for function literal inside \"{m_currentFunctionName}\" function");
            }

            // Replace marker for function literal
            m_targetFile.Replace(FunctionLiteralParametersMarker, parametersSignature);

            // operand
            //     : literal
            //     | operandName
            //     | methodExpr
            //     | '(' expression ')'

            // literal
            //     : basicLit
            //     | compositeLit
            //     | functionLit

            if (!(context.Parent.Parent is GolangParser.OperandContext operandContext))
            {
                AddWarning(context, $"Could not derive parent operand context from function literal inside \"{m_currentFunctionName}\" function: \"{context.GetText()}\"");
                PopBlock();
                return;
            }

            string lambdaExpression = PopBlock(false);

            // Simplify lambda expressions that consist of a single return statement
            if (m_firstStatementIsReturn)
            {
                int index = lambdaExpression.IndexOf("=>", StringComparison.Ordinal);

                if (index > -1)
                {
                    string startBlock = $"{{{Environment.NewLine}";

                    index = lambdaExpression.IndexOf(startBlock, index, StringComparison.Ordinal);

                    if (index > -1)
                    {
                        string parameters = lambdaExpression.Substring(0, index).Trim();

                        lambdaExpression = lambdaExpression.Substring(index + startBlock.Length).Trim();

                        if (lambdaExpression.StartsWith("return ", StringComparison.Ordinal))
                        {
                            lambdaExpression = lambdaExpression.Substring(7).Trim();
                        }

                        if (lambdaExpression.EndsWith("}", StringComparison.Ordinal))
                        {
                            lambdaExpression = lambdaExpression.Substring(0, lambdaExpression.Length - 1).Trim();
                        }

                        if (lambdaExpression.EndsWith(";", StringComparison.Ordinal))
                        {
                            lambdaExpression = lambdaExpression.Substring(0, lambdaExpression.Length - 1).Trim();
                        }

                        lambdaExpression = $"{parameters} {lambdaExpression}";
                    }
                }
            }

            // Update expression operand (managed in ScannerBase_Expression.cs)
            Operands[operandContext] = lambdaExpression;
        }
Пример #2
0
 public override void EnterFunctionLit(GolangParser.FunctionLitContext context)
 {
     PushBlock();
     m_targetFile.AppendLine($"{FunctionLiteralParametersMarker} =>");
 }