From d559921e3b9e08090f8554651b23948714dce272 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Mon, 26 Oct 2020 17:39:45 -0700 Subject: [PATCH 1/6] Parse repeated argument tuples in callLikeExpr --- src/QsCompiler/TextProcessor/QsExpressionParsing.fs | 9 +++++---- src/QsCompiler/TextProcessor/QsKeywords.fs | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/QsCompiler/TextProcessor/QsExpressionParsing.fs b/src/QsCompiler/TextProcessor/QsExpressionParsing.fs index 5ff61f310f..220d9e39dc 100644 --- a/src/QsCompiler/TextProcessor/QsExpressionParsing.fs +++ b/src/QsCompiler/TextProcessor/QsExpressionParsing.fs @@ -383,10 +383,11 @@ let private argumentTuple = /// Expects tuple brackets around the argument even if the argument consists of a single tuple item. /// Note that this parser has a dependency on the arrayItemExpr, identifier, and tupleItem expr parsers - /// meaning they process the left-most part of the call-like expression and thus need to be evaluated *after* the callLikeExpr parser. -let internal callLikeExpr = - attempt ((itemAccessExpr <|> identifier <|> tupledItem expr) .>>. argumentTuple) // identifier needs to come *after* arrayItemExpr - |>> fun (callable, arg) -> applyBinary CallLikeExpression () callable arg - +let internal callLikeExpr = + // identifier needs to come *after* arrayItemExpr + itemAccessExpr <|> identifier <|> tupledItem expr .>>. many1 argumentTuple |> attempt + |>> List.Cons + |>> List.reduce (applyBinary CallLikeExpression ()) // processing terms of operator precedence parsers diff --git a/src/QsCompiler/TextProcessor/QsKeywords.fs b/src/QsCompiler/TextProcessor/QsKeywords.fs index a8c33c839f..d84d4066cd 100644 --- a/src/QsCompiler/TextProcessor/QsKeywords.fs +++ b/src/QsCompiler/TextProcessor/QsKeywords.fs @@ -284,7 +284,7 @@ let qsSetIntersection = QsOperator.New("*" , 20, true) // The call combinator binds stronger than all operators. // All modifiers bind stronger than the call combinator. // The array item combinator binds stronger than all modifiers. -let qsCallCombinator = QsOperator.New("(", ")" , 900 , false) // Op()() is invalid and requires parentheses +let qsCallCombinator = QsOperator.New("(", ")" , 900 , true) // Op()() is fine let qsAdjointModifier = QsOperator.New(qsAdjointFunctor.id , 950 , false) let qsControlledModifier = QsOperator.New(qsControlledFunctor.id , 951 , false) let qsUnwrapModifier = QsOperator.New("!" , 1000, true) From 752723db4f585095e7548e28a813a64ada2df6cd Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Mon, 26 Oct 2020 18:18:10 -0700 Subject: [PATCH 2/6] Add call tests with some parens removed --- src/QsCompiler/Tests.Compiler/SyntaxTests.fs | 105 +++++++++++++------ 1 file changed, 74 insertions(+), 31 deletions(-) diff --git a/src/QsCompiler/Tests.Compiler/SyntaxTests.fs b/src/QsCompiler/Tests.Compiler/SyntaxTests.fs index 85894fe784..f0873be5d6 100644 --- a/src/QsCompiler/Tests.Compiler/SyntaxTests.fs +++ b/src/QsCompiler/Tests.Compiler/SyntaxTests.fs @@ -385,37 +385,80 @@ let ``Complex literal tests`` () = [] let ``Call tests`` () = [ - ("x()", true, toExpr (CallLikeExpression (toIdentifier "x", UnitValue |> toExpr)), []); - ("x(1,2)", true, toExpr (CallLikeExpression (toIdentifier "x", - toTuple [ toInt 1; - toInt 2])), []); - ("Adjoint x()", true, toExpr (CallLikeExpression (AdjointApplication (toIdentifier "x") |> toExpr, - UnitValue |> toExpr)), []); - ("Controlled x()", true, toExpr (CallLikeExpression (ControlledApplication (toIdentifier "x") |> toExpr, - UnitValue |> toExpr)), []); - - ("(x(_,1))(2)", true, toExpr (CallLikeExpression (toTuple [toExpr (CallLikeExpression (toIdentifier "x", - toTuple [ MissingExpr |> toExpr; - toInt 1]) - )], toTuple [ toInt 2 ])), []); - ("(x(_,1))(1,2)", true, toExpr (CallLikeExpression (toTuple [toExpr (CallLikeExpression (toIdentifier "x", - toTuple [ MissingExpr |> toExpr; - toInt 1]) - )], toTuple [ toInt 1; - toInt 2 ])), []); - ("(x(1,(2, _)))(2)", true, toExpr (CallLikeExpression (toTuple [toExpr (CallLikeExpression (toIdentifier "x", - toTuple [ toInt 1; - toTuple [ toInt 2; - MissingExpr |> toExpr]; - ]) - )], toTuple [ toInt 2 ])), []); - ("(x(_,(2, _)))(1,2)", true, toExpr (CallLikeExpression (toTuple [toExpr (CallLikeExpression (toIdentifier "x", - toTuple [ MissingExpr |> toExpr; - toTuple [ toInt 2; - MissingExpr |> toExpr]; - ]) - )], toTuple [ toInt 1; - toInt 2 ])), []); + "x()", true, CallLikeExpression (toIdentifier "x", toExpr UnitValue) |> toExpr, [] + "x(1,2)", true, CallLikeExpression (toIdentifier "x", toTuple [ toInt 1; toInt 2 ]) |> toExpr, [] + "Adjoint x()", + true, + CallLikeExpression (toIdentifier "x" |> AdjointApplication |> toExpr, toExpr UnitValue) |> toExpr, + [] + "Controlled x()", + true, + CallLikeExpression (toIdentifier "x" |> ControlledApplication |> toExpr, toExpr UnitValue) |> toExpr, + [] + "(x(_,1))(2)", + true, + CallLikeExpression + (toTuple [ CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr ], + toTuple [ toInt 2 ]) + |> toExpr, + [] + "x(_,1)(2)", + true, + CallLikeExpression + (CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr, + toTuple [ toInt 2 ]) + |> toExpr, + [] + "(x(_,1))(1,2)", + true, + CallLikeExpression + (toTuple [ CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr ], + toTuple [ toInt 1; toInt 2 ]) + |> toExpr, + [] + "x(_,1)(1,2)", + true, + CallLikeExpression + (CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr, + toTuple [ toInt 1; toInt 2 ]) + |> toExpr, + [] + "(x(1,(2, _)))(2)", + true, + CallLikeExpression + ([ CallLikeExpression (toIdentifier "x", toTuple [ toInt 1; toTuple [ toInt 2; toExpr MissingExpr ] ]) + |> toExpr ] + |> toTuple, + toTuple [ toInt 2 ]) + |> toExpr, + [] + "x(1,(2, _))(2)", + true, + CallLikeExpression + (CallLikeExpression (toIdentifier "x", toTuple [ toInt 1; toTuple [ toInt 2; toExpr MissingExpr ] ]) + |> toExpr, + toTuple [ toInt 2 ]) + |> toExpr, + [] + "(x(_,(2, _)))(1,2)", + true, + CallLikeExpression + ([ CallLikeExpression + (toIdentifier "x", toTuple [ toExpr MissingExpr; toTuple [ toInt 2; toExpr MissingExpr ] ]) + |> toExpr ] + |> toTuple, + toTuple [ toInt 1; toInt 2 ]) + |> toExpr, + [] + "x(_,(2, _))(1,2)", + true, + CallLikeExpression + (CallLikeExpression + (toIdentifier "x", toTuple [ toExpr MissingExpr; toTuple [ toInt 2; toExpr MissingExpr ] ]) + |> toExpr, + toTuple [ toInt 1; toInt 2 ]) + |> toExpr, + [] ] |> List.iter testExpr From 107488f572a6e2bea09b8e1a14e11ba834e1152b Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Tue, 27 Oct 2020 08:54:18 -0700 Subject: [PATCH 3/6] Add more call tests --- src/QsCompiler/Tests.Compiler/SyntaxTests.fs | 108 +++++++++++++------ 1 file changed, 76 insertions(+), 32 deletions(-) diff --git a/src/QsCompiler/Tests.Compiler/SyntaxTests.fs b/src/QsCompiler/Tests.Compiler/SyntaxTests.fs index f0873be5d6..44a686010a 100644 --- a/src/QsCompiler/Tests.Compiler/SyntaxTests.fs +++ b/src/QsCompiler/Tests.Compiler/SyntaxTests.fs @@ -395,68 +395,112 @@ let ``Call tests`` () = true, CallLikeExpression (toIdentifier "x" |> ControlledApplication |> toExpr, toExpr UnitValue) |> toExpr, [] + "f(1)(2)", + true, + (CallLikeExpression (toIdentifier "f", toTuple [ toInt 1 ]) |> toExpr, toTuple [ toInt 2 ]) + |> CallLikeExpression + |> toExpr, + [] + "f(1)(2)(3)", + true, + ((CallLikeExpression (toIdentifier "f", toTuple [ toInt 1 ]) |> toExpr, toTuple [ toInt 2 ]) + |> CallLikeExpression + |> toExpr, + toTuple [ toInt 3 ]) + |> CallLikeExpression + |> toExpr, + [] + "f(1)(2)[3]", + true, + (CallLikeExpression (toIdentifier "f", toTuple [ toInt 1 ]) |> toExpr, toTuple [ toInt 2 ]) + |> CallLikeExpression + |> toExpr, + [] + "(f(1)(2))[3]", + true, + ((CallLikeExpression (toIdentifier "f", toTuple [ toInt 1 ]) |> toExpr, toTuple [ toInt 2 ]) + |> CallLikeExpression + |> toExpr + |> List.singleton + |> toTuple, + toInt 3) + |> ArrayItem + |> toExpr, + [] + "(f(1)(2))[3](4)", + true, + ((CallLikeExpression (toIdentifier "f", toTuple [ toInt 1 ]) |> toExpr, toTuple [ toInt 2 ]) + |> CallLikeExpression + |> toExpr + |> List.singleton + |> toTuple, + toInt 3) + |> ArrayItem + |> toExpr + |> (fun left -> CallLikeExpression (left, toTuple [ toInt 4 ]) |> toExpr), + [] "(x(_,1))(2)", true, - CallLikeExpression - (toTuple [ CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr ], - toTuple [ toInt 2 ]) + (toTuple [ CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr ], + toTuple [ toInt 2 ]) + |> CallLikeExpression |> toExpr, [] "x(_,1)(2)", true, - CallLikeExpression - (CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr, - toTuple [ toInt 2 ]) + (CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr, + toTuple [ toInt 2 ]) + |> CallLikeExpression |> toExpr, [] "(x(_,1))(1,2)", true, - CallLikeExpression - (toTuple [ CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr ], - toTuple [ toInt 1; toInt 2 ]) + (toTuple [ CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr ], + toTuple [ toInt 1; toInt 2 ]) + |> CallLikeExpression |> toExpr, [] "x(_,1)(1,2)", true, - CallLikeExpression - (CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr, - toTuple [ toInt 1; toInt 2 ]) + (CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr, + toTuple [ toInt 1; toInt 2 ]) + |> CallLikeExpression |> toExpr, [] "(x(1,(2, _)))(2)", true, - CallLikeExpression - ([ CallLikeExpression (toIdentifier "x", toTuple [ toInt 1; toTuple [ toInt 2; toExpr MissingExpr ] ]) - |> toExpr ] - |> toTuple, - toTuple [ toInt 2 ]) + ([ CallLikeExpression (toIdentifier "x", toTuple [ toInt 1; toTuple [ toInt 2; toExpr MissingExpr ] ]) + |> toExpr ] + |> toTuple, + toTuple [ toInt 2 ]) + |> CallLikeExpression |> toExpr, [] "x(1,(2, _))(2)", true, - CallLikeExpression - (CallLikeExpression (toIdentifier "x", toTuple [ toInt 1; toTuple [ toInt 2; toExpr MissingExpr ] ]) - |> toExpr, - toTuple [ toInt 2 ]) + (CallLikeExpression (toIdentifier "x", toTuple [ toInt 1; toTuple [ toInt 2; toExpr MissingExpr ] ]) + |> toExpr, + toTuple [ toInt 2 ]) + |> CallLikeExpression |> toExpr, [] "(x(_,(2, _)))(1,2)", true, - CallLikeExpression - ([ CallLikeExpression - (toIdentifier "x", toTuple [ toExpr MissingExpr; toTuple [ toInt 2; toExpr MissingExpr ] ]) - |> toExpr ] - |> toTuple, - toTuple [ toInt 1; toInt 2 ]) + ([ (toIdentifier "x", toTuple [ toExpr MissingExpr; toTuple [ toInt 2; toExpr MissingExpr ] ]) + |> CallLikeExpression + |> toExpr ] + |> toTuple, + toTuple [ toInt 1; toInt 2 ]) + |> CallLikeExpression |> toExpr, [] "x(_,(2, _))(1,2)", true, - CallLikeExpression - (CallLikeExpression - (toIdentifier "x", toTuple [ toExpr MissingExpr; toTuple [ toInt 2; toExpr MissingExpr ] ]) - |> toExpr, - toTuple [ toInt 1; toInt 2 ]) + ((toIdentifier "x", toTuple [ toExpr MissingExpr; toTuple [ toInt 2; toExpr MissingExpr ] ]) + |> CallLikeExpression + |> toExpr, + toTuple [ toInt 1; toInt 2 ]) + |> CallLikeExpression |> toExpr, [] ] From dac72d343397b78ddf7cc761fe85101f43eaf4a4 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Tue, 27 Oct 2020 09:02:11 -0700 Subject: [PATCH 4/6] Clean up tests --- src/QsCompiler/Tests.Compiler/SyntaxTests.fs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/QsCompiler/Tests.Compiler/SyntaxTests.fs b/src/QsCompiler/Tests.Compiler/SyntaxTests.fs index 44a686010a..991e96bc0f 100644 --- a/src/QsCompiler/Tests.Compiler/SyntaxTests.fs +++ b/src/QsCompiler/Tests.Compiler/SyntaxTests.fs @@ -418,10 +418,9 @@ let ``Call tests`` () = [] "(f(1)(2))[3]", true, - ((CallLikeExpression (toIdentifier "f", toTuple [ toInt 1 ]) |> toExpr, toTuple [ toInt 2 ]) - |> CallLikeExpression - |> toExpr - |> List.singleton + ([ (CallLikeExpression (toIdentifier "f", toTuple [ toInt 1 ]) |> toExpr, toTuple [ toInt 2 ]) + |> CallLikeExpression + |> toExpr ] |> toTuple, toInt 3) |> ArrayItem @@ -429,10 +428,9 @@ let ``Call tests`` () = [] "(f(1)(2))[3](4)", true, - ((CallLikeExpression (toIdentifier "f", toTuple [ toInt 1 ]) |> toExpr, toTuple [ toInt 2 ]) - |> CallLikeExpression - |> toExpr - |> List.singleton + ([ (CallLikeExpression (toIdentifier "f", toTuple [ toInt 1 ]) |> toExpr, toTuple [ toInt 2 ]) + |> CallLikeExpression + |> toExpr ] |> toTuple, toInt 3) |> ArrayItem From 4ebc6b395cc83bc3186491d19f37e3f12ed5eb19 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Tue, 27 Oct 2020 09:41:55 -0700 Subject: [PATCH 5/6] attempt is redundant --- src/QsCompiler/TextProcessor/QsExpressionParsing.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/QsCompiler/TextProcessor/QsExpressionParsing.fs b/src/QsCompiler/TextProcessor/QsExpressionParsing.fs index 220d9e39dc..7e08a4ac74 100644 --- a/src/QsCompiler/TextProcessor/QsExpressionParsing.fs +++ b/src/QsCompiler/TextProcessor/QsExpressionParsing.fs @@ -385,7 +385,7 @@ let private argumentTuple = /// meaning they process the left-most part of the call-like expression and thus need to be evaluated *after* the callLikeExpr parser. let internal callLikeExpr = // identifier needs to come *after* arrayItemExpr - itemAccessExpr <|> identifier <|> tupledItem expr .>>. many1 argumentTuple |> attempt + itemAccessExpr <|> identifier <|> tupledItem expr .>>. many1 argumentTuple |>> List.Cons |>> List.reduce (applyBinary CallLikeExpression ()) From 23c0ca2f48eb02c0bfb174fd86ce67f7d6eb2b81 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Wed, 28 Oct 2020 08:45:17 -0700 Subject: [PATCH 6/6] Add named item call tests --- src/QsCompiler/Tests.Compiler/SyntaxTests.fs | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/QsCompiler/Tests.Compiler/SyntaxTests.fs b/src/QsCompiler/Tests.Compiler/SyntaxTests.fs index 991e96bc0f..12429f1787 100644 --- a/src/QsCompiler/Tests.Compiler/SyntaxTests.fs +++ b/src/QsCompiler/Tests.Compiler/SyntaxTests.fs @@ -437,6 +437,33 @@ let ``Call tests`` () = |> toExpr |> (fun left -> CallLikeExpression (left, toTuple [ toInt 4 ]) |> toExpr), [] + "f(1)(2)::X", + true, + (CallLikeExpression (toIdentifier "f", toTuple [ toInt 1 ]) |> toExpr, toTuple [ toInt 2 ]) + |> CallLikeExpression + |> toExpr, + [] + "(f(1)(2))::X", + true, + ([ (CallLikeExpression (toIdentifier "f", toTuple [ toInt 1 ]) |> toExpr, toTuple [ toInt 2 ]) + |> CallLikeExpression + |> toExpr ] + |> toTuple, + toSymbol "X") + |> NamedItem + |> toExpr, + [] + "(f(1)(2))::X(4)", + true, + ([ (CallLikeExpression (toIdentifier "f", toTuple [ toInt 1 ]) |> toExpr, toTuple [ toInt 2 ]) + |> CallLikeExpression + |> toExpr ] + |> toTuple, + toSymbol "X") + |> NamedItem + |> toExpr + |> (fun left -> CallLikeExpression (left, toTuple [ toInt 4 ]) |> toExpr), + [] "(x(_,1))(2)", true, (toTuple [ CallLikeExpression (toIdentifier "x", toTuple [ toExpr MissingExpr; toInt 1 ]) |> toExpr ],