PY-11481 Correctly detect escaped quotes at the end of string literals

This commit is contained in:
Mikhail Golubev
2014-09-01 13:30:35 +04:00
parent eb7442a6c3
commit bb0c3b4cab
3 changed files with 17 additions and 3 deletions
@@ -58,9 +58,17 @@ public class StringLiteralQuotesAnnotator extends PyAnnotator {
private boolean checkQuotedString(@NotNull ASTNode stringNode, @NotNull String nodeText) {
final char firstQuote = nodeText.charAt(0);
final int lastChar = nodeText.length() - 1;
if (lastChar == 0 || nodeText.charAt(lastChar) != firstQuote ||
(nodeText.charAt(lastChar - 1) == '\\' && (lastChar == 1 || nodeText.charAt(lastChar - 2) != '\\'))) {
final char lastChar = nodeText.charAt(nodeText.length() - 1);
int precedingBackslashCount = 0;
for (int i = nodeText.length() - 2; i >= 0; i--) {
if (nodeText.charAt(i) == '\\') {
precedingBackslashCount++;
}
else {
break;
}
}
if (nodeText.length() == 1 || lastChar != firstQuote || precedingBackslashCount % 2 != 0) {
getHolder().createErrorAnnotation(stringNode, PyBundle.message("ANN.missing.closing.quote", firstQuote));
return true;
}
@@ -0,0 +1,2 @@
S1 = 'This literal contains even number of backslashes\\\\\\'
S2 = <error descr="Missing closing quote [']">'This literal contains odd number of backslashes\\\\\\\'</error>
@@ -174,6 +174,10 @@ public class PythonHighlightingTest extends PyTestCase {
doTest(true, false);
}
public void testMultipleEscapedBackslashes() {
doTest(true, false);
}
public void testUnsupportedFeaturesInPython3() {
doTest(LanguageLevel.PYTHON30, true, false);
}