Browse Source

Fixed Visual Basic operator precedence and added unit tests for operator precedence.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@972 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 20 years ago
parent
commit
77020dbcce
  1. 1
      src/AddIns/DisplayBindings/FormsDesigner/Project/Src/Services/TypeResolutionService.cs
  2. 1806
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs
  3. 185
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG
  4. 127
      src/Libraries/NRefactory/Test/Parser/Expressions/BinaryOperatorExpressionTests.cs

1
src/AddIns/DisplayBindings/FormsDesigner/Project/Src/Services/TypeResolutionService.cs

@ -168,7 +168,6 @@ namespace ICSharpCode.FormsDesigner.Services
byte[] asm_data = File.ReadAllBytes(tempPath); byte[] asm_data = File.ReadAllBytes(tempPath);
asm = Assembly.Load(asm_data); asm = Assembly.Load(asm_data);
asm.LoadModule(Path.GetFileName(fileName), data); asm.LoadModule(Path.GetFileName(fileName), data);
Type[] types = asm.GetTypes();
} }
} catch (Exception ex) { } catch (Exception ex) {
MessageService.ShowError(ex, "Error calling linker for netmodule"); MessageService.ShowError(ex, "Error calling linker for netmodule");

1806
src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs

File diff suppressed because it is too large Load Diff

185
src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG

@ -1710,30 +1710,9 @@ EventMemberSpecifier<out string name>
Expr<out Expression expr> Expr<out Expression expr>
= =
ConditionalOrExpr<out expr> DisjunctionExpr<out expr>
. .
UnaryExpr<out Expression uExpr>
(.
Expression expr;
UnaryOperatorType uop = UnaryOperatorType.None;
bool isUOp = false;
.) =
{ "+" (. uop = UnaryOperatorType.Plus; isUOp = true; .)
| "-" (. uop = UnaryOperatorType.Minus; isUOp = true; .)
/* | "Not" (. uop = UnaryOperatorType.Not; isUOp = true;.) */
| "*" (. uop = UnaryOperatorType.Star; isUOp = true;.)
}
SimpleExpr<out expr>
(.
if (isUOp) {
uExpr = new UnaryOperatorExpression(expr, uop);
} else {
uExpr = expr;
}
.)
.
AssignmentOperator<out AssignmentOperatorType op> AssignmentOperator<out AssignmentOperatorType op>
(. op = AssignmentOperatorType.None; .) = (. op = AssignmentOperatorType.None; .) =
"=" (. op = AssignmentOperatorType.Assign; .) "=" (. op = AssignmentOperatorType.Assign; .)
@ -1863,61 +1842,51 @@ CastTarget<out TypeReference type>
| "CStr" (. type = new TypeReference("System.String"); .) | "CStr" (. type = new TypeReference("System.String"); .)
. .
ConditionalOrExpr<out Expression outExpr> DisjunctionExpr<out Expression outExpr>
(. Expression expr; .) = (.
ConditionalAndExpr<out outExpr> { "OrElse" ConditionalAndExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, BinaryOperatorType.LogicalOr, expr); .) } Expression expr;
. BinaryOperatorType op = BinaryOperatorType.None;
.) =
ConditionalAndExpr<out Expression outExpr> ConjunctionExpr<out outExpr>
(. Expression expr; .) = {
InclusiveOrExpr<out outExpr> { "AndAlso" InclusiveOrExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, BinaryOperatorType.LogicalAnd, expr); .) } (
. "Or" (. op = BinaryOperatorType.BitwiseOr; .)
| "OrElse" (. op = BinaryOperatorType.LogicalOr; .)
InclusiveOrExpr<out Expression outExpr> | "Xor" (. op = BinaryOperatorType.ExclusiveOr; .)
(. Expression expr; .) = )
ExclusiveOrExpr<out outExpr> { "Xor" ExclusiveOrExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, BinaryOperatorType.ExclusiveOr, expr); .) } ConjunctionExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .)
. }
.
ExclusiveOrExpr<out Expression outExpr>
(. Expression expr; .) =
AndExpr<out outExpr> { "Or" AndExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, BinaryOperatorType.BitwiseOr, expr); .) }
.
AndExpr<out Expression outExpr> ConjunctionExpr<out Expression outExpr>
(. Expression expr; .) = (.
NotExpr<out outExpr> { "And" NotExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, BinaryOperatorType.BitwiseAnd, expr); .) } Expression expr;
. BinaryOperatorType op = BinaryOperatorType.None;
.) =
NotExpr<out outExpr>
{
(
"And" (. op = BinaryOperatorType.BitwiseAnd; .)
| "AndAlso" (. op = BinaryOperatorType.LogicalAnd; .)
)
NotExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .)
}
.
NotExpr<out Expression outExpr> NotExpr<out Expression outExpr>
(. UnaryOperatorType uop = UnaryOperatorType.None; .) = (. UnaryOperatorType uop = UnaryOperatorType.None; .) =
{ "Not" (. uop = UnaryOperatorType.Not; .) } { "Not" (. uop = UnaryOperatorType.Not; .) }
EqualityExpr<out outExpr> ComparisonExpr<out outExpr>
(. if (uop != UnaryOperatorType.None) (. if (uop != UnaryOperatorType.None)
outExpr = new UnaryOperatorExpression(outExpr, uop); outExpr = new UnaryOperatorExpression(outExpr, uop);
.) .)
. .
EqualityExpr<out Expression outExpr> ComparisonExpr<out Expression outExpr>
(. (.
Expression expr; Expression expr;
BinaryOperatorType op = BinaryOperatorType.None; BinaryOperatorType op = BinaryOperatorType.None;
.) = .) =
RelationalExpr<out outExpr>
{
(
"<>" (. op = BinaryOperatorType.InEquality; .)
| "=" (. op = BinaryOperatorType.Equality; .)
| "Like" (. op = BinaryOperatorType.Like; .)
)
RelationalExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .)
}
.
RelationalExpr<out Expression outExpr>
(.
Expression expr;
BinaryOperatorType op = BinaryOperatorType.None;
.) =
ShiftExpr<out outExpr> ShiftExpr<out outExpr>
{ {
( (
@ -1925,46 +1894,63 @@ RelationalExpr<out Expression outExpr>
| ">" (. op = BinaryOperatorType.GreaterThan; .) | ">" (. op = BinaryOperatorType.GreaterThan; .)
| "<=" (. op = BinaryOperatorType.LessThanOrEqual; .) | "<=" (. op = BinaryOperatorType.LessThanOrEqual; .)
| ">=" (. op = BinaryOperatorType.GreaterThanOrEqual; .) | ">=" (. op = BinaryOperatorType.GreaterThanOrEqual; .)
| "<>" (. op = BinaryOperatorType.InEquality; .)
| "=" (. op = BinaryOperatorType.Equality; .)
| "Like" (. op = BinaryOperatorType.Like; .)
| "Is" (. op = BinaryOperatorType.ReferenceEquality; .)
| "IsNot" (. op = BinaryOperatorType.ReferenceInequality; .)
) )
ShiftExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .) ShiftExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .)
|
/* 11.5.3 */
("Is" (. op = BinaryOperatorType.ReferenceEquality; .) |
"IsNot" (. op = BinaryOperatorType.ReferenceInequality; .) )
Expr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .)
} }
. .
ShiftExpr<out Expression outExpr> ShiftExpr<out Expression outExpr>
(. (.
Expression expr; Expression expr;
BinaryOperatorType op = BinaryOperatorType.None; BinaryOperatorType op = BinaryOperatorType.None;
.) = .) =
AdditiveExpr<out outExpr> ConcatenationExpr<out outExpr>
{ {
( (
"<<" (. op = BinaryOperatorType.ShiftLeft; .) "<<" (. op = BinaryOperatorType.ShiftLeft; .)
| ">>" (. op = BinaryOperatorType.ShiftRight; .) | ">>" (. op = BinaryOperatorType.ShiftRight; .)
) )
AdditiveExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .) ConcatenationExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .)
} }
. .
ConcatenationExpr<out Expression outExpr>
(. Expression expr; .)
=
AdditiveExpr<out outExpr> { "&" AdditiveExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, BinaryOperatorType.Concat, expr); .) }
.
AdditiveExpr<out Expression outExpr> AdditiveExpr<out Expression outExpr>
(. (.
Expression expr; Expression expr;
BinaryOperatorType op = BinaryOperatorType.None; BinaryOperatorType op = BinaryOperatorType.None;
.) = .) =
MultiplicativeExpr<out outExpr> ModuloExpr<out outExpr>
{ {
( (
"+" (. op = BinaryOperatorType.Add; .) "+" (. op = BinaryOperatorType.Add; .)
| "-" (. op = BinaryOperatorType.Subtract; .) | "-" (. op = BinaryOperatorType.Subtract; .)
| "&" (. op = BinaryOperatorType.Concat; .)
) )
MultiplicativeExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .) ModuloExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .)
} }
. .
ModuloExpr<out Expression outExpr>
(. Expression expr; .)
=
IntegerDivisionExpr<out outExpr> { "Mod" IntegerDivisionExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, BinaryOperatorType.Modulus, expr); .) }
.
IntegerDivisionExpr<out Expression outExpr>
(. Expression expr; .)
=
MultiplicativeExpr<out outExpr> { "\\" MultiplicativeExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, BinaryOperatorType.DivideInteger, expr); .) }
.
MultiplicativeExpr<out Expression outExpr> MultiplicativeExpr<out Expression outExpr>
(. (.
@ -1976,13 +1962,36 @@ MultiplicativeExpr<out Expression outExpr>
( (
"*" (. op = BinaryOperatorType.Multiply; .) "*" (. op = BinaryOperatorType.Multiply; .)
| "/" (. op = BinaryOperatorType.Divide; .) | "/" (. op = BinaryOperatorType.Divide; .)
| "\\" (. op = BinaryOperatorType.DivideInteger; .)
| "Mod" (. op = BinaryOperatorType.Modulus; .)
| "^" (. op = BinaryOperatorType.Power; .)
) )
UnaryExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .) UnaryExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .)
} }
. .
UnaryExpr<out Expression uExpr>
(.
Expression expr;
UnaryOperatorType uop = UnaryOperatorType.None;
bool isUOp = false;
.) =
{ "+" (. uop = UnaryOperatorType.Plus; isUOp = true; .)
| "-" (. uop = UnaryOperatorType.Minus; isUOp = true; .)
| "*" (. uop = UnaryOperatorType.Star; isUOp = true;.)
}
ExponentiationExpr<out expr>
(.
if (isUOp) {
uExpr = new UnaryOperatorExpression(expr, uop);
} else {
uExpr = expr;
}
.)
.
ExponentiationExpr<out Expression outExpr>
(. Expression expr; .)
=
SimpleExpr<out outExpr> { "^" SimpleExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, BinaryOperatorType.Power, expr); .) }
.
ObjectCreateExpression<out Expression oce> ObjectCreateExpression<out Expression oce>
(. (.
@ -2619,7 +2628,7 @@ EmbeddedStatement<out Statement statement>
bool mustBeAssignment = la.kind == Tokens.Plus || la.kind == Tokens.Minus || bool mustBeAssignment = la.kind == Tokens.Plus || la.kind == Tokens.Minus ||
la.kind == Tokens.Not || la.kind == Tokens.Times; la.kind == Tokens.Not || la.kind == Tokens.Times;
.) .)
UnaryExpr<out expr> SimpleExpr<out expr>
( (
AssignmentOperator<out op> Expr<out val> (. expr = new AssignmentExpression(expr, op, val); .) AssignmentOperator<out op> Expr<out val> (. expr = new AssignmentExpression(expr, op, val); .)
| (. if (mustBeAssignment) Error("error in assignment."); .) | (. if (mustBeAssignment) Error("error in assignment."); .)
@ -2632,7 +2641,7 @@ EmbeddedStatement<out Statement statement>
} }
statement = new StatementExpression(expr); statement = new StatementExpression(expr);
.) .)
| "Call" UnaryExpr<out expr> (. statement = new StatementExpression(expr); .) | "Call" SimpleExpr<out expr> (. statement = new StatementExpression(expr); .)
| "Using" Identifier (. | "Using" Identifier (.
string resourcename = t.val, typeName; string resourcename = t.val, typeName;
Statement resourceAquisition = null, block = null; Statement resourceAquisition = null, block = null;

127
src/Libraries/NRefactory/Test/Parser/Expressions/BinaryOperatorExpressionTests.cs

@ -17,6 +17,64 @@ namespace ICSharpCode.NRefactory.Tests.AST
[TestFixture] [TestFixture]
public class BinaryOperatorExpressionTests public class BinaryOperatorExpressionTests
{ {
void OperatorPrecedenceTest(string strongOperator, BinaryOperatorType strongOperatorType,
string weakOperator, BinaryOperatorType weakOperatorType, bool vb)
{
string program = "a " + weakOperator + " b " + strongOperator + " c";
BinaryOperatorExpression boe;
if (vb)
boe = ParseUtilVBNet.ParseExpression<BinaryOperatorExpression>(program);
else
boe = ParseUtilCSharp.ParseExpression<BinaryOperatorExpression>(program);
Assert.AreEqual(weakOperatorType, boe.Op);
Assert.IsTrue(boe.Left is IdentifierExpression);
boe = (BinaryOperatorExpression)boe.Right;
Assert.AreEqual(strongOperatorType, boe.Op);
Assert.IsTrue(boe.Left is IdentifierExpression);
Assert.IsTrue(boe.Right is IdentifierExpression);
program = "a " + strongOperator + " b " + weakOperator + " c";
if (vb)
boe = ParseUtilVBNet.ParseExpression<BinaryOperatorExpression>(program);
else
boe = ParseUtilCSharp.ParseExpression<BinaryOperatorExpression>(program);
Assert.AreEqual(weakOperatorType, boe.Op);
Assert.IsTrue(boe.Right is IdentifierExpression);
boe = (BinaryOperatorExpression)boe.Left;
Assert.AreEqual(strongOperatorType, boe.Op);
Assert.IsTrue(boe.Left is IdentifierExpression);
Assert.IsTrue(boe.Right is IdentifierExpression);
}
void SameOperatorPrecedenceTest(string firstOperator, BinaryOperatorType firstOperatorType,
string secondOperator, BinaryOperatorType secondOperatorType, bool vb)
{
string program = "a " + secondOperator + " b " + firstOperator + " c";
BinaryOperatorExpression boe;
if (vb)
boe = ParseUtilVBNet.ParseExpression<BinaryOperatorExpression>(program);
else
boe = ParseUtilCSharp.ParseExpression<BinaryOperatorExpression>(program);
Assert.AreEqual(firstOperatorType, boe.Op);
Assert.IsTrue(boe.Right is IdentifierExpression);
boe = (BinaryOperatorExpression)boe.Left;
Assert.AreEqual(secondOperatorType, boe.Op);
Assert.IsTrue(boe.Left is IdentifierExpression);
Assert.IsTrue(boe.Right is IdentifierExpression);
program = "a " + firstOperator + " b " + secondOperator + " c";
if (vb)
boe = ParseUtilVBNet.ParseExpression<BinaryOperatorExpression>(program);
else
boe = ParseUtilCSharp.ParseExpression<BinaryOperatorExpression>(program);
Assert.AreEqual(secondOperatorType, boe.Op);
Assert.IsTrue(boe.Right is IdentifierExpression);
boe = (BinaryOperatorExpression)boe.Left;
Assert.AreEqual(firstOperatorType, boe.Op);
Assert.IsTrue(boe.Left is IdentifierExpression);
Assert.IsTrue(boe.Right is IdentifierExpression);
}
#region C# #region C#
void CSharpTestBinaryOperatorExpressionTest(string program, BinaryOperatorType op) void CSharpTestBinaryOperatorExpressionTest(string program, BinaryOperatorType op)
{ {
@ -28,6 +86,41 @@ namespace ICSharpCode.NRefactory.Tests.AST
} }
[Test]
public void CSharpOperatorPrecedenceTest()
{
SameOperatorPrecedenceTest("*", BinaryOperatorType.Multiply, "/", BinaryOperatorType.Divide, false);
SameOperatorPrecedenceTest("*", BinaryOperatorType.Multiply, "%", BinaryOperatorType.Modulus, false);
OperatorPrecedenceTest("*", BinaryOperatorType.Multiply, "+", BinaryOperatorType.Add, false);
SameOperatorPrecedenceTest("-", BinaryOperatorType.Subtract, "+", BinaryOperatorType.Add, false);
OperatorPrecedenceTest("+", BinaryOperatorType.Add, "<<", BinaryOperatorType.ShiftLeft, false);
SameOperatorPrecedenceTest(">>", BinaryOperatorType.ShiftRight, "<<", BinaryOperatorType.ShiftLeft, false);
OperatorPrecedenceTest("<<", BinaryOperatorType.ShiftLeft, "==", BinaryOperatorType.Equality, false);
SameOperatorPrecedenceTest("!=", BinaryOperatorType.InEquality, "==", BinaryOperatorType.Equality, false);
OperatorPrecedenceTest("==", BinaryOperatorType.Equality, "&", BinaryOperatorType.BitwiseAnd, false);
OperatorPrecedenceTest("&", BinaryOperatorType.BitwiseAnd, "^", BinaryOperatorType.ExclusiveOr, false);
OperatorPrecedenceTest("^", BinaryOperatorType.ExclusiveOr, "|", BinaryOperatorType.BitwiseOr, false);
OperatorPrecedenceTest("|", BinaryOperatorType.BitwiseOr, "&&", BinaryOperatorType.LogicalAnd, false);
OperatorPrecedenceTest("&&", BinaryOperatorType.LogicalAnd, "||", BinaryOperatorType.LogicalOr, false);
OperatorPrecedenceTest("||", BinaryOperatorType.LogicalOr, "??", BinaryOperatorType.NullCoalescing, false);
}
[Test]
public void CSharpSubtractionLeftToRight()
{
BinaryOperatorExpression boe = ParseUtilCSharp.ParseExpression<BinaryOperatorExpression>("a - b - c");
Assert.IsTrue(boe.Right is IdentifierExpression);
Assert.IsTrue(boe.Left is BinaryOperatorExpression);
}
[Test]
public void CSharpNullCoalescingRightToLeft()
{
BinaryOperatorExpression boe = ParseUtilCSharp.ParseExpression<BinaryOperatorExpression>("a ?? b ?? c");
Assert.IsTrue(boe.Left is IdentifierExpression);
Assert.IsTrue(boe.Right is BinaryOperatorExpression);
}
[Test] [Test]
public void CSharpBitwiseAndTest() public void CSharpBitwiseAndTest()
{ {
@ -155,6 +248,40 @@ namespace ICSharpCode.NRefactory.Tests.AST
} }
[Test]
public void VBOperatorPrecedenceTest()
{
OperatorPrecedenceTest("^", BinaryOperatorType.Power, "*", BinaryOperatorType.Multiply, true);
SameOperatorPrecedenceTest("*", BinaryOperatorType.Multiply, "/", BinaryOperatorType.Divide, true);
OperatorPrecedenceTest("/", BinaryOperatorType.Divide, "\\", BinaryOperatorType.DivideInteger, true);
OperatorPrecedenceTest("\\", BinaryOperatorType.DivideInteger, "Mod", BinaryOperatorType.Modulus, true);
OperatorPrecedenceTest("Mod", BinaryOperatorType.Modulus, "+", BinaryOperatorType.Add, true);
SameOperatorPrecedenceTest("+", BinaryOperatorType.Add, "-", BinaryOperatorType.Subtract, true);
OperatorPrecedenceTest("-", BinaryOperatorType.Subtract, "&", BinaryOperatorType.Concat, true);
OperatorPrecedenceTest("&", BinaryOperatorType.Concat, "<<", BinaryOperatorType.ShiftLeft, true);
SameOperatorPrecedenceTest("<<", BinaryOperatorType.ShiftLeft, ">>", BinaryOperatorType.ShiftRight, true);
OperatorPrecedenceTest("<<", BinaryOperatorType.ShiftLeft, "=", BinaryOperatorType.Equality, true);
SameOperatorPrecedenceTest("<>", BinaryOperatorType.InEquality, "=", BinaryOperatorType.Equality, true);
SameOperatorPrecedenceTest("<", BinaryOperatorType.LessThan, "=", BinaryOperatorType.Equality, true);
SameOperatorPrecedenceTest("<=", BinaryOperatorType.LessThanOrEqual, "=", BinaryOperatorType.Equality, true);
SameOperatorPrecedenceTest(">", BinaryOperatorType.GreaterThan, "=", BinaryOperatorType.Equality, true);
SameOperatorPrecedenceTest(">=", BinaryOperatorType.GreaterThanOrEqual, "=", BinaryOperatorType.Equality, true);
SameOperatorPrecedenceTest("Like", BinaryOperatorType.Like, "=", BinaryOperatorType.Equality, true);
SameOperatorPrecedenceTest("Is", BinaryOperatorType.ReferenceEquality, "=", BinaryOperatorType.Equality, true);
SameOperatorPrecedenceTest("IsNot", BinaryOperatorType.ReferenceInequality, "=", BinaryOperatorType.Equality, true);
OperatorPrecedenceTest("=", BinaryOperatorType.Equality, "And", BinaryOperatorType.BitwiseAnd, true);
SameOperatorPrecedenceTest("And", BinaryOperatorType.BitwiseAnd, "AndAlso", BinaryOperatorType.LogicalAnd, true);
OperatorPrecedenceTest("And", BinaryOperatorType.BitwiseAnd, "Or", BinaryOperatorType.BitwiseOr, true);
SameOperatorPrecedenceTest("Or", BinaryOperatorType.BitwiseOr, "OrElse", BinaryOperatorType.LogicalOr, true);
SameOperatorPrecedenceTest("Or", BinaryOperatorType.BitwiseOr, "Xor", BinaryOperatorType.ExclusiveOr, true);
}
[Test]
public void VBNetTest()
{
VBNetTestBinaryOperatorExpressionTest("a ^ b", BinaryOperatorType.Power);
}
[Test] [Test]
public void VBNetPowerTest() public void VBNetPowerTest()
{ {

Loading…
Cancel
Save