public override void EnterTypeSwitchStmt(GoParser.TypeSwitchStmtContext context) { // typeSwitchStmt // : 'switch' (simpleStmt ';') ? typeSwitchGuard '{' typeCaseClause * '}' // typeSwitchGuard // : ( IDENTIFIER ':=' )? primaryExpr '.' '(' 'type' ')' m_typeSwitchExpressionLevel++; if (!(context.simpleStmt() is null) && context.simpleStmt().emptyStmt() is null) { if (!(context.simpleStmt().shortVarDecl() is null)) { // Any declared variable will be scoped to switch statement, so create a sub-block for it m_targetFile.AppendLine($"{Spacing()}{{"); IndentLevel++; // Handle storing of current values of any redeclared variables m_targetFile.Append(OpenRedeclaredVariableBlock(context.simpleStmt().shortVarDecl().identifierList(), m_typeSwitchExpressionLevel)); } m_targetFile.Append(string.Format(TypeSwitchStatementMarker, m_typeSwitchExpressionLevel)); } m_targetFile.Append($"{Spacing()}switch ({string.Format(TypeSwitchExpressionMarker, m_typeSwitchExpressionLevel)}){Environment.NewLine}{Spacing()}{{"); IndentLevel++; m_typeSwitchDefaultCase.Push(new StringBuilder()); }
public override void ExitTypeSwitchStmt(GoParser.TypeSwitchStmtContext context) { // typeSwitchStmt // : 'switch'(simpleStmt ';') ? typeSwitchGuard '{' typeCaseClause * '}' // typeSwitchGuard // : ( IDENTIFIER ':=' )? primaryExpr '.' '(' 'type' ')' // Default case always needs to be last case clause in SwitchExpression - Go allows its declaration anywhere m_targetFile.Append($"{m_typeSwitchDefaultCase.Pop()}{CheckForCommentsRight(context)}"); if (PrimaryExpressions.TryGetValue(context.typeSwitchGuard().primaryExpr(), out ExpressionInfo expression)) { // Replace type switch expression marker m_targetFile.Replace(string.Format(TypeSwitchExpressionMarker, m_typeSwitchExpressionLevel), $"{expression.Text}.type()"); } else { AddWarning(context, $"Failed to find primary expression for type switch statement: {context.typeSwitchGuard().GetText()}"); } if (!(context.simpleStmt() is null) && context.simpleStmt().emptyStmt() is null) { if (m_simpleStatements.TryGetValue(context.simpleStmt(), out string statement)) { m_targetFile.Replace(string.Format(TypeSwitchStatementMarker, m_typeSwitchExpressionLevel), $"{statement}{Environment.NewLine}"); } else { AddWarning(context, $"Failed to find simple statement for type switch statement: {context.simpleStmt().GetText()}"); } // Close any locally scoped declared variable sub-block if (!(context.simpleStmt().shortVarDecl() is null)) { // Handle restoration of previous values of any redeclared variables m_targetFile.Append(CloseRedeclaredVariableBlock(context.simpleStmt().shortVarDecl().identifierList(), m_typeSwitchExpressionLevel)); IndentLevel--; m_targetFile.AppendLine(); m_targetFile.Append($"{Spacing()}}}"); } } IndentLevel--; m_targetFile.Append($"{Spacing()}}}{CheckForCommentsRight(context)}"); m_typeSwitchExpressionLevel--; }