Skip to content

Commit 10e21f5

Browse files
author
Mikhail Arkhipov
authored
Fix matmul operator parsing (microsoft#1863)
* Remove stale reference * Don't suppress LHS diagnostics on augmented assign * Revert "Don't suppress LHS diagnostics on augmented assign" This reverts commit 6109ac7. * Add test * Partial * Fix based on grouping * Undo * Formatting
1 parent 226221e commit 10e21f5

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/Parsing/Impl/Tokens/Tokenizer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,8 +1822,14 @@ private Token NextOperator(int ch) {
18221822
case '~':
18231823
return Tokens.TwiddleToken;
18241824
case '@':
1825-
if (LanguageVersion >= PythonLanguageVersion.V35 && NextChar('=')) {
1826-
return Tokens.MatMultiplyEqualToken;
1825+
if (LanguageVersion >= PythonLanguageVersion.V35) {
1826+
if (NextChar('=')) {
1827+
return Tokens.MatMultiplyEqualToken;
1828+
}
1829+
if (GroupingLevel > 0) {
1830+
// @ can't be a decorator here.
1831+
return Tokens.MatMultiplyToken;
1832+
}
18271833
}
18281834
return Tokens.AtToken;
18291835
}

src/Parsing/Test/ParserTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,24 @@ public void MatMulOperator() {
15501550
}
15511551
}
15521552

1553+
[TestMethod, Priority(0)]
1554+
public void MatMulMultilineOperator() {
1555+
foreach (var version in V35AndUp) {
1556+
CheckAst(
1557+
ParseFile("MatMulOperator2.py", ErrorSink.Null, version),
1558+
CheckSuite(
1559+
CheckAssignment(Fob, CheckParenExpr(CheckBinaryExpression(One, PythonOperator.MatMultiply, Two)))
1560+
)
1561+
);
1562+
}
1563+
1564+
foreach (var version in V3Versions.Except(V35AndUp)) {
1565+
ParseErrors("MatMulOperator2.py", version, new[] {
1566+
new ErrorResult("unexpected token '<newline>'", new SourceSpan(2, 6, 3, 1))
1567+
});
1568+
}
1569+
}
1570+
15531571
[TestMethod, Priority(0)]
15541572
public void GroupingRecovery() {
15551573
foreach (var version in AllVersions) {
@@ -4851,7 +4869,7 @@ private Action<Statement> CheckDelStmt(params Action<Expression>[] deletes) {
48514869
};
48524870
}
48534871

4854-
private Action<Expression> CheckParenExpr(Action<Expression> value) {
4872+
private static Action<Expression> CheckParenExpr(Action<Expression> value) {
48554873
return expr => {
48564874
Assert.AreEqual(typeof(ParenthesisExpression), expr.GetType());
48574875
var paren = (ParenthesisExpression)expr;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fob = (
2+
1
3+
@ 2
4+
)

0 commit comments

Comments
 (0)