Browse Source

Fixed parsing of switch statements.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
578bb6c062
  1. 58
      ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs

58
ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs

@ -1659,40 +1659,44 @@ namespace ICSharpCode.NRefactory.CSharp
if (switchStatement.Expr != null) if (switchStatement.Expr != null)
result.AddChild ((Expression)switchStatement.Expr.Accept (this), SwitchStatement.Roles.Expression); result.AddChild ((Expression)switchStatement.Expr.Accept (this), SwitchStatement.Roles.Expression);
if (location != null && location.Count > 1) if (location != null && location.Count > 1)
result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), SwitchStatement.Roles.RPar); result.AddChild (new CSharpTokenNode (Convert (location [1]), 1), SwitchStatement.Roles.RPar);
if (location != null && location.Count > 2) if (location != null && location.Count > 2)
result.AddChild (new CSharpTokenNode (Convert (location[2]), 1), SwitchStatement.Roles.LBrace); result.AddChild (new CSharpTokenNode (Convert (location [2]), 1), SwitchStatement.Roles.LBrace);
foreach (var section in switchStatement.Sections) { if (switchStatement.Sections != null) {
var newSection = new SwitchSection (); foreach (var section in switchStatement.Sections) {
foreach (var caseLabel in section.Labels) { var newSection = new SwitchSection ();
var newLabel = new CaseLabel (); if (section.Labels != null) {
if (caseLabel.Label != null) { foreach (var caseLabel in section.Labels) {
newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), "case".Length), SwitchStatement.Roles.Keyword); var newLabel = new CaseLabel ();
newLabel.AddChild ((Expression)caseLabel.Label.Accept (this), SwitchStatement.Roles.Expression); if (caseLabel.Label != null) {
var colonLocation = LocationsBag.GetLocations (caseLabel); newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), "case".Length), SwitchStatement.Roles.Keyword);
if (colonLocation != null) newLabel.AddChild ((Expression)caseLabel.Label.Accept (this), SwitchStatement.Roles.Expression);
newLabel.AddChild (new CSharpTokenNode (Convert (colonLocation [0]), 1), SwitchStatement.Roles.Colon); var colonLocation = LocationsBag.GetLocations (caseLabel);
} else { if (colonLocation != null)
newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), "default".Length), SwitchStatement.Roles.Keyword); newLabel.AddChild (new CSharpTokenNode (Convert (colonLocation [0]), 1), SwitchStatement.Roles.Colon);
newLabel.AddChild (new CSharpTokenNode (new TextLocation (caseLabel.Location.Row, caseLabel.Location.Column + "default".Length), 1), SwitchStatement.Roles.Colon); } else {
newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), "default".Length), SwitchStatement.Roles.Keyword);
newLabel.AddChild (new CSharpTokenNode (new TextLocation (caseLabel.Location.Row, caseLabel.Location.Column + "default".Length), 1), SwitchStatement.Roles.Colon);
}
newSection.AddChild (newLabel, SwitchSection.CaseLabelRole);
}
} }
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);
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);
} }
result.AddChild (newSection, SwitchStatement.SwitchSectionRole);
} }
if (location != null && location.Count > 3) { if (location != null && location.Count > 3) {
result.AddChild (new CSharpTokenNode (Convert (location[3]), 1), SwitchStatement.Roles.RBrace); result.AddChild (new CSharpTokenNode (Convert (location [3]), 1), SwitchStatement.Roles.RBrace);
} else { } else {
// parser error, set end node to max value. // parser error, set end node to max value.
result.AddChild (new ErrorNode (), AstNode.Roles.Error); result.AddChild (new ErrorNode (), AstNode.Roles.Error);

Loading…
Cancel
Save