Closes PY-31 "Unclosed string literals are not highlighted as erorrs".

This commit is contained in:
Dmitry Cheryasov
2008-07-06 02:54:37 +04:00
parent 234f403a41
commit e1db15d9d7
2 changed files with 54 additions and 0 deletions
@@ -36,6 +36,7 @@ public class PythonLanguage extends Language {
_annotators.add(DocStringAnnotator.class);
_annotators.add(ImportAnnotator.class);
_annotators.add(UnresolvedReferenceAnnotator.class);
_annotators.add(StringConstantAnnotator.class);
}
@@ -0,0 +1,53 @@
package com.jetbrains.python.validation;
import com.jetbrains.python.psi.PyStringLiteralExpression;
/**
* Looks for well-formedness of string constants.
* User: dcheryasov
* Date: Jul 5, 2008
* Time: 11:58:57 PM
*/
public class StringConstantAnnotator extends PyAnnotator {
public void visitPyStringLiteralExpression(final PyStringLiteralExpression node) {
String s = node.getText();
String msg = "";
boolean ok = true;
boolean esc = false;
int index = 0;
// skip 'unicode' and 'raw' modifiers
char first_quote = s.charAt(index);
if ((first_quote == 'u') || (first_quote == 'U')) index += 1;
first_quote = s.charAt(index);
if ((first_quote == 'r') || (first_quote == 'R')) index += 1;
first_quote = s.charAt(index);
// s can't begin with non-quote, else parser would not say it's a string
index += 1;
if (index >= s.length()) { // sole opening quote
msg = "No closing quote [" + first_quote + "]";
ok = false;
}
else {
while (ok && (index < s.length()-1)) {
char c = s.charAt(index);
if (esc) esc = false;
else {
if (c == first_quote) {
msg = "Premature closing quote [" + first_quote + "]";
ok = false;
}
else if (c == '\\') esc = true;
}
index += 1;
}
if (ok && (esc || (s.charAt(index) != first_quote))) {
msg = "Missing closing quote [" + first_quote + "]";
ok = false;
}
}
//
if (! ok) {
getHolder().createErrorAnnotation(node, msg);
}
}
}