public override object Visit (Switch switchStatement) { var result = new SwitchStatement (); var location = LocationsBag.GetLocations (switchStatement); result.AddChild (new CSharpTokenNode (Convert (switchStatement.loc), "switch".Length), SwitchStatement.Roles.Keyword); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), SwitchStatement.Roles.LPar); result.AddChild ((INode)switchStatement.Expr.Accept (this), SwitchStatement.Roles.Expression); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), SwitchStatement.Roles.RPar); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[2]), 1), SwitchStatement.Roles.LBrace); foreach (var section in switchStatement.Sections) { var newSection = new MonoDevelop.CSharp.Dom.SwitchSection (); foreach (var caseLabel in section.Labels) { var newLabel = new CaseLabel (); newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), "case".Length), SwitchStatement.Roles.Keyword); if (caseLabel.Label != null) newLabel.AddChild ((INode)caseLabel.Label.Accept (this), SwitchStatement.Roles.Expression); newSection.AddChild (newLabel, MonoDevelop.CSharp.Dom.SwitchSection.CaseLabelRole); } newSection.AddChild ((INode)section.Block.Accept (this), MonoDevelop.CSharp.Dom.SwitchSection.Roles.Body); result.AddChild (newSection, SwitchStatement.SwitchSectionRole); } if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[3]), 1), SwitchStatement.Roles.RBrace); return result; }
public override object Visit(Switch switchStatement) { var result = new SwitchStatement(); var location = LocationsBag.GetLocations(switchStatement); result.AddChild(new CSharpTokenNode(Convert(switchStatement.loc), SwitchStatement.SwitchKeywordRole), SwitchStatement.SwitchKeywordRole); if (location != null) result.AddChild(new CSharpTokenNode(Convert(location [0]), Roles.LPar), Roles.LPar); if (switchStatement.Expr != null) result.AddChild((Expression)switchStatement.Expr.Accept(this), Roles.Expression); if (location != null && location.Count > 1) result.AddChild(new CSharpTokenNode(Convert(location [1]), Roles.RPar), Roles.RPar); if (location != null && location.Count > 2) result.AddChild(new CSharpTokenNode(Convert(location [2]), Roles.LBrace), Roles.LBrace); SwitchSection newSection = null; bool lastWasCase = false, added = true; if (switchStatement.Block != null) { foreach (var child in switchStatement.Block.Statements) { var statement = child.Accept(this); var caseLabel = statement as CaseLabel; if (caseLabel != null) { if (!lastWasCase) { newSection = new SwitchSection(); added = false; } newSection.AddChild(caseLabel, SwitchSection.CaseLabelRole); lastWasCase = true; } else { if (lastWasCase) { result.AddChild(newSection, SwitchStatement.SwitchSectionRole); lastWasCase = false; added = true; } newSection.AddChild((Statement)statement, Roles.EmbeddedStatement); } } } if (!added) result.AddChild(newSection, SwitchStatement.SwitchSectionRole); if (location != null && location.Count > 3) { result.AddChild(new CSharpTokenNode(Convert(location [3]), Roles.RBrace), Roles.RBrace); } else { // parser error, set end node to max value. result.AddChild(new ErrorNode(), Roles.Error); } return result; }
public override object Visit (Switch switchStatement) { var result = new SwitchStatement (); var location = LocationsBag.GetLocations (switchStatement); result.AddChild (new CSharpTokenNode (Convert (switchStatement.loc), "switch".Length), SwitchStatement.Roles.Keyword); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), SwitchStatement.Roles.LPar); result.AddChild ((Expression)switchStatement.Expr.Accept (this), SwitchStatement.Roles.Expression); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), SwitchStatement.Roles.RPar); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[2]), 1), SwitchStatement.Roles.LBrace); foreach (var section in switchStatement.Sections) { var newSection = new SwitchSection (); foreach (var caseLabel in section.Labels) { var newLabel = new CaseLabel (); newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), "case".Length), SwitchStatement.Roles.Keyword); if (caseLabel.Label != null) newLabel.AddChild ((Expression)caseLabel.Label.Accept (this), SwitchStatement.Roles.Expression); newSection.AddChild (newLabel, SwitchSection.CaseLabelRole); } var blockStatement = section.Block; var bodyBlock = new BlockStatement (); int curLocal = 0; AddBlockChildren (bodyBlock, blockStatement, ref curLocal); foreach (var statement in bodyBlock.Statements) { statement.Remove (); newSection.AddChild (statement, SwitchSection.Roles.EmbeddedStatement); } result.AddChild (newSection, SwitchStatement.SwitchSectionRole); } if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[3]), 1), SwitchStatement.Roles.RBrace); return result; }
public override object Visit(Switch switchStatement) { var result = new SwitchStatement (); var location = LocationsBag.GetLocations (switchStatement); result.AddChild (new CSharpTokenNode (Convert (switchStatement.loc), SwitchStatement.SwitchKeywordRole), SwitchStatement.SwitchKeywordRole); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar); if (switchStatement.Expr != null) result.AddChild ((Expression)switchStatement.Expr.Accept (this), Roles.Expression); if (location != null && location.Count > 1) result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar); if (location != null && location.Count > 2) result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.LBrace), Roles.LBrace); if (switchStatement.Sections != null) { foreach (var section in switchStatement.Sections) { var newSection = new SwitchSection (); if (section.Labels != null) { foreach (var caseLabel in section.Labels) { var newLabel = new CaseLabel (); if (caseLabel.Label != null) { newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), CaseLabel.CaseKeywordRole), CaseLabel.CaseKeywordRole); if (caseLabel.Label != null) newLabel.AddChild ((Expression)caseLabel.Label.Accept (this), Roles.Expression); var colonLocation = LocationsBag.GetLocations (caseLabel); if (colonLocation != null) newLabel.AddChild (new CSharpTokenNode (Convert (colonLocation [0]), Roles.Colon), Roles.Colon); } else { newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), CaseLabel.DefaultKeywordRole), CaseLabel.DefaultKeywordRole); newLabel.AddChild (new CSharpTokenNode (new TextLocation (caseLabel.Location.Row, caseLabel.Location.Column + "default".Length), Roles.Colon), Roles.Colon); } newSection.AddChild (newLabel, SwitchSection.CaseLabelRole); } } var blockStatement = section.Block; var bodyBlock = new BlockStatement (); int curLocal = 0; AddBlockChildren (bodyBlock, blockStatement, ref curLocal); foreach (var statement in bodyBlock.Statements) { statement.Remove (); newSection.AddChild (statement, Roles.EmbeddedStatement); } result.AddChild (newSection, SwitchStatement.SwitchSectionRole); } } if (location != null && location.Count > 3) { result.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.RBrace), Roles.RBrace); } else { // parser error, set end node to max value. result.AddChild (new ErrorNode (), Roles.Error); } return result; }
IEnumerable<Statement> TransformNode(ILNode node) { if (node is ILLabel) { yield return new Ast.LabelStatement { Label = ((ILLabel)node).Name }; } else if (node is ILExpression) { List<ILRange> ilRanges = ((ILExpression)node).GetILRanges(); AstNode codeExpr = TransformExpression((ILExpression)node); if (codeExpr != null) { codeExpr = codeExpr.WithAnnotation(ilRanges); if (codeExpr is Ast.Expression) { yield return new Ast.ExpressionStatement { Expression = (Ast.Expression)codeExpr }; } else if (codeExpr is Ast.Statement) { yield return (Ast.Statement)codeExpr; } else { throw new Exception(); } } } else if (node is ILLoop) { yield return new Ast.ForStatement { EmbeddedStatement = TransformBlock(((ILLoop)node).ContentBlock) }; /* } else if (node is Branch) { yield return new Ast.LabelStatement { Label = ((Branch)node).FirstBasicBlock.Label }; Ast.BlockStatement trueBlock = new Ast.BlockStatement(); trueBlock.AddStatement(new Ast.GotoStatement(((Branch)node).TrueSuccessor.Label)); Ast.BlockStatement falseBlock = new Ast.BlockStatement(); falseBlock.AddStatement(new Ast.GotoStatement(((Branch)node).FalseSuccessor.Label)); Ast.IfElseStatement ifElseStmt = new Ast.IfElseStatement { Condition = MakeBranchCondition((Branch)node), TrueStatement = trueBlock, FalseStatement = falseBlock }; yield return ifElseStmt; */ } else if (node is ILCondition) { ILCondition conditionalNode = (ILCondition)node; if (conditionalNode.FalseBlock.Body.Any()) { // Swap bodies yield return new Ast.IfElseStatement { Condition = new UnaryOperatorExpression(UnaryOperatorType.Not, MakeBranchCondition(conditionalNode.Condition)), TrueStatement = TransformBlock(conditionalNode.FalseBlock), FalseStatement = TransformBlock(conditionalNode.TrueBlock) }; } else { yield return new Ast.IfElseStatement { Condition = MakeBranchCondition(conditionalNode.Condition), TrueStatement = TransformBlock(conditionalNode.TrueBlock), FalseStatement = TransformBlock(conditionalNode.FalseBlock) }; } } else if (node is ILSwitch) { ILSwitch ilSwitch = (ILSwitch)node; SwitchStatement switchStmt = new SwitchStatement() { Expression = (Expression)TransformExpression(ilSwitch.Condition.Arguments[0]) }; for (int i = 0; i < ilSwitch.CaseBlocks.Count; i++) { switchStmt.AddChild(new SwitchSection() { CaseLabels = new[] { new CaseLabel() { Expression = new PrimitiveExpression(i) } }, Statements = new[] { TransformBlock(ilSwitch.CaseBlocks[i]) } }, SwitchStatement.SwitchSectionRole); } yield return switchStmt; } else if (node is ILTryCatchBlock) { ILTryCatchBlock tryCatchNode = ((ILTryCatchBlock)node); List<Ast.CatchClause> catchClauses = new List<CatchClause>(); foreach (var catchClause in tryCatchNode.CatchBlocks) { catchClauses.Add(new Ast.CatchClause { Type = AstBuilder.ConvertType(catchClause.ExceptionType), VariableName = "exception", Body = TransformBlock(catchClause) }); } yield return new Ast.TryCatchStatement { TryBlock = TransformBlock(tryCatchNode.TryBlock), CatchClauses = catchClauses, FinallyBlock = tryCatchNode.FinallyBlock != null ? TransformBlock(tryCatchNode.FinallyBlock) : null }; } else if (node is ILBlock) { yield return TransformBlock((ILBlock)node); } else { throw new Exception("Unknown node type"); } }