Ejemplo n.º 1
0
        public override void EnterTypeCaseClause(GoParser.TypeCaseClauseContext context)
        {
            // typeCaseClause
            //     : typeSwitchCase ':' statementList

            // typeSwitchCase
            //     : 'case' typeList | 'default'

            // typeList
            //     : type ( ',' type )*

            if (context.typeSwitchCase().typeList() is null)
            {
                GoParser.TypeSwitchStmtContext parent = context.Parent as GoParser.TypeSwitchStmtContext;
                string identifier = parent == null ? "" : SanitizedIdentifier(parent.typeSwitchGuard().IDENTIFIER().GetText());

                m_typeSwitchDefaultCase.Peek().Append($"{Environment.NewLine}{Spacing()}default:{Environment.NewLine}{Spacing()}{{{Environment.NewLine}");

                if (!string.IsNullOrEmpty(identifier))
                {
                    m_typeSwitchDefaultCase.Peek().Append($"{Spacing(1)}var {identifier} = {string.Format(TypeSwitchExpressionMarker, m_typeSwitchExpressionLevel)};{Environment.NewLine}");
                }
            }
            else
            {
                m_targetFile.Append($"{Environment.NewLine}{Spacing()}case {string.Format(TypeSwitchCaseTypeMarker, m_typeSwitchExpressionLevel)}{Environment.NewLine}");
            }

            IndentLevel++;

            PushBlock();
        }
Ejemplo n.º 2
0
        public override void ExitTypeCaseClause(GoParser.TypeCaseClauseContext context)
        {
            // typeCaseClause
            //     : typeSwitchCase ':' statementList

            // typeSwitchCase
            //     : 'case' typeList | 'default'

            // typeList
            //     : type ( ',' type )*

            IndentLevel--;

            if (context.typeSwitchCase().typeList() is null)
            {
                m_typeSwitchDefaultCase.Peek().Append($"{PopBlock(false)}{Spacing(1)}break;{Environment.NewLine}{Spacing()}}}");
            }
            else
            {
                string caseBlock = $"{PopBlock(false)}{Spacing(1)}break;";
                m_targetFile.Append(caseBlock);

                GoParser.TypeSwitchStmtContext parent = context.Parent as GoParser.TypeSwitchStmtContext;
                string identifier = parent == null ? "_" : SanitizedIdentifier(parent.typeSwitchGuard().IDENTIFIER().GetText());

                GoParser.TypeListContext typeList            = context.typeSwitchCase().typeList();
                StringBuilder            caseTypeExpressions = new StringBuilder();
                HashSet <string>         typeNames           = new HashSet <string>();

                for (int i = 0; i < typeList.type_().Length; i++)
                {
                    if (Types.TryGetValue(typeList.type_(i), out TypeInfo typeInfo))
                    {
                        string typeName = typeInfo.TypeName;

                        if (typeNames.Add(typeName))
                        {
                            string caseExpression = i > 0 ? $"{Environment.NewLine}{caseBlock}{Environment.NewLine}{Spacing()}case " : "";

                            if (typeName.Equals("nil", StringComparison.Ordinal))
                            {
                                caseTypeExpressions.Append($"{caseExpression}null:");
                            }
                            else
                            {
                                caseTypeExpressions.Append($"{caseExpression}{typeName} {identifier}:");
                            }
                        }
                        else
                        {
                            AddWarning(typeList, $"Skipped duplicate type info (from C# perspective) for type switch case statement: {typeList.GetText()}");
                        }
                    }
                    else
                    {
                        AddWarning(typeList, $"Failed to find type info for type switch case statement: {typeList.GetText()}");
                    }
                }

                // Replace type switch case type marker
                m_targetFile.Replace(string.Format(TypeSwitchCaseTypeMarker, m_typeSwitchExpressionLevel), caseTypeExpressions.ToString());
            }
        }