Browse Source

Add OperatorDeclaration.GetOperatorType() helper method.

pull/112/head
Daniel Grunwald 15 years ago
parent
commit
edf5f6711e
  1. 72
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/OperatorDeclaration.cs
  2. 6
      NRefactory/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs
  3. 20
      NRefactory/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs

72
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/OperatorDeclaration.cs

@ -29,41 +29,45 @@ using System.Linq;
namespace ICSharpCode.NRefactory.CSharp namespace ICSharpCode.NRefactory.CSharp
{ {
public enum OperatorType { public enum OperatorType
{
// Values must correspond to Mono.CSharp.Operator.OpType
// due to the casts used in OperatorDeclaration.
// Unary operators // Unary operators
LogicalNot, LogicalNot = Mono.CSharp.Operator.OpType.LogicalNot,
OnesComplement, OnesComplement = Mono.CSharp.Operator.OpType.OnesComplement,
Increment, Increment = Mono.CSharp.Operator.OpType.Increment,
Decrement, Decrement = Mono.CSharp.Operator.OpType.Decrement,
True, True = Mono.CSharp.Operator.OpType.True,
False, False = Mono.CSharp.Operator.OpType.False,
// Unary and Binary operators // Unary and Binary operators
Addition, Addition = Mono.CSharp.Operator.OpType.Addition,
Subtraction, Subtraction = Mono.CSharp.Operator.OpType.Subtraction,
UnaryPlus, UnaryPlus = Mono.CSharp.Operator.OpType.UnaryPlus,
UnaryNegation, UnaryNegation = Mono.CSharp.Operator.OpType.UnaryNegation,
// Binary operators // Binary operators
Multiply, Multiply = Mono.CSharp.Operator.OpType.Multiply,
Division, Division = Mono.CSharp.Operator.OpType.Division,
Modulus, Modulus = Mono.CSharp.Operator.OpType.Modulus,
BitwiseAnd, BitwiseAnd = Mono.CSharp.Operator.OpType.BitwiseAnd,
BitwiseOr, BitwiseOr = Mono.CSharp.Operator.OpType.BitwiseOr,
ExclusiveOr, ExclusiveOr = Mono.CSharp.Operator.OpType.ExclusiveOr,
LeftShift, LeftShift = Mono.CSharp.Operator.OpType.LeftShift,
RightShift, RightShift = Mono.CSharp.Operator.OpType.RightShift,
Equality, Equality = Mono.CSharp.Operator.OpType.Equality,
Inequality, Inequality = Mono.CSharp.Operator.OpType.Inequality,
GreaterThan, GreaterThan = Mono.CSharp.Operator.OpType.GreaterThan,
LessThan, LessThan = Mono.CSharp.Operator.OpType.LessThan,
GreaterThanOrEqual, GreaterThanOrEqual = Mono.CSharp.Operator.OpType.GreaterThanOrEqual,
LessThanOrEqual, LessThanOrEqual = Mono.CSharp.Operator.OpType.LessThanOrEqual,
// Implicit and Explicit // Implicit and Explicit
Implicit, Implicit = Mono.CSharp.Operator.OpType.Implicit,
Explicit Explicit = Mono.CSharp.Operator.OpType.Explicit
} }
public class OperatorDeclaration : AttributedNode public class OperatorDeclaration : AttributedNode
@ -98,11 +102,25 @@ namespace ICSharpCode.NRefactory.CSharp
set { SetChildByRole (Roles.Body, value); } set { SetChildByRole (Roles.Body, value); }
} }
/// <summary>
/// Gets the operator type from the method name, or null, if the method does not represent one of the known operator types.
/// </summary>
public static OperatorType? GetOperatorType(string methodName)
{
return (OperatorType?)Mono.CSharp.Operator.GetType(methodName);
}
/// <summary>
/// Gets the method name for the operator type. ("op_Addition", "op_Implicit", etc.)
/// </summary>
public static string GetName(OperatorType type) public static string GetName(OperatorType type)
{ {
return Mono.CSharp.Operator.GetMetadataName((Mono.CSharp.Operator.OpType)type); return Mono.CSharp.Operator.GetMetadataName((Mono.CSharp.Operator.OpType)type);
} }
/// <summary>
/// Gets the token for the operator type ("+", "implicit", etc.)
/// </summary>
public static string GetToken(OperatorType type) public static string GetToken(OperatorType type)
{ {
return Mono.CSharp.Operator.GetName((Mono.CSharp.Operator.OpType)type); return Mono.CSharp.Operator.GetName((Mono.CSharp.Operator.OpType)type);

6
NRefactory/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs

@ -1626,8 +1626,10 @@ namespace ICSharpCode.NRefactory.CSharp
LPar(); LPar();
Space(policy.SpacesWithinCatchParentheses); Space(policy.SpacesWithinCatchParentheses);
catchClause.Type.AcceptVisitor(this, data); catchClause.Type.AcceptVisitor(this, data);
Space(); if (!string.IsNullOrEmpty(catchClause.VariableName)) {
WriteIdentifier(catchClause.VariableName); Space();
WriteIdentifier(catchClause.VariableName);
}
Space(policy.SpacesWithinCatchParentheses); Space(policy.SpacesWithinCatchParentheses);
RPar(); RPar();
} }

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

@ -551,25 +551,7 @@ namespace ICSharpCode.NRefactory.CSharp
if (location != null) if (location != null)
newOperator.AddChild (new CSharpTokenNode (Convert (location[0]), "operator".Length), OperatorDeclaration.OperatorKeywordRole); newOperator.AddChild (new CSharpTokenNode (Convert (location[0]), "operator".Length), OperatorDeclaration.OperatorKeywordRole);
int opLength = 1; int opLength = OperatorDeclaration.GetToken(newOperator.OperatorType).Length;
switch (newOperator.OperatorType) {
case OperatorType.LeftShift:
case OperatorType.RightShift:
case OperatorType.LessThanOrEqual:
case OperatorType.GreaterThanOrEqual:
case OperatorType.Equality:
case OperatorType.Inequality:
// case OperatorType.LogicalAnd:
// case OperatorType.LogicalOr:
opLength = 2;
break;
case OperatorType.True:
opLength = "true".Length;
break;
case OperatorType.False:
opLength = "false".Length;
break;
}
if (location != null) if (location != null)
newOperator.AddChild (new CSharpTokenNode (Convert (location[1]), opLength), OperatorDeclaration.OperatorTypeRole); newOperator.AddChild (new CSharpTokenNode (Convert (location[1]), opLength), OperatorDeclaration.OperatorTypeRole);
} }

Loading…
Cancel
Save