| 1 |
|
| 2 |
"""This file contains various functions used for analysis of SyntaxErrors""" |
| 3 |
|
| 4 |
import unicodedata |
| 5 |
|
| 6 |
from .. import debug_helper, token_utils |
| 7 |
from ..ft_gettext import current_lang |
| 8 |
|
| 9 |
_ = current_lang.translate |
| 10 |
|
| 11 |
|
| 12 |
def matching_brackets(bra, ket): |
| 13 |
return ( |
| 14 |
(bra == "(" and ket == ")") |
| 15 |
or (bra == "[" and ket == "]") |
| 16 |
or (bra == "{" and ket == "}") |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
def name_bracket(bracket): |
| 21 |
names = { |
| 22 |
"(": _("parenthesis `(`"), |
| 23 |
")": _("parenthesis `)`"), |
| 24 |
"[": _("square bracket `[`"), |
| 25 |
"]": _("square bracket `]`"), |
| 26 |
"{": _("curly bracket `{`"), |
| 27 |
"}": _("curly bracket `}`"), |
| 28 |
} |
| 29 |
return names[str(bracket)] |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
bad_quotation_marks = [ |
| 35 |
"«", "»", |
| 36 |
"‹", "›", |
| 37 |
"„", "“", |
| 38 |
"‟", "”", |
| 39 |
"❝", "❞", |
| 40 |
"❮", "❯", |
| 41 |
"⹂", "〝", |
| 42 |
"〞", """, |
| 43 |
"‚", "’", "‛", "‘", |
| 44 |
"❛", "❜", |
| 45 |
"❟", |
| 46 |
] |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
def identify_bad_quote_char(char, line): |
| 51 |
if char not in bad_quotation_marks: |
| 52 |
return |
| 53 |
|
| 54 |
|
| 55 |
try: |
| 56 |
char_name = unicodedata.name(char, "unknown") |
| 57 |
except TypeError: |
| 58 |
return |
| 59 |
|
| 60 |
hint = _("Did you mean to use a normal quote character, `'` or `\"`?\n") |
| 61 |
cause = _( |
| 62 |
"I suspect that you used a fancy unicode quotation mark\n" |
| 63 |
"whose name is {name}\n" |
| 64 |
"instead of a normal single or double quote for a string." |
| 65 |
"\n" |
| 66 |
).format(name=char_name) |
| 67 |
count = 0 |
| 68 |
for character in line: |
| 69 |
if character in bad_quotation_marks: |
| 70 |
count += 1 |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
if count == 1: |
| 75 |
if char in ["‹", "❮"]: |
| 76 |
cause += _("Or perhaps, you meant to write a less than sign, `<`.\n") |
| 77 |
elif char in ["›", "❯"]: |
| 78 |
cause += _("Or perhaps, you meant to write a greater than sign, `>`.\n") |
| 79 |
elif char in ["‚", "❟"]: |
| 80 |
cause += _("Or perhaps, you meant to write a comma.\n") |
| 81 |
|
| 82 |
return {"cause": cause, "suggest": hint} |
| 83 |
|
| 84 |
|
| 85 |
def identify_bad_math_symbol(char, line): |
| 86 |
"""Similar to identify_bad_unicode_character except that it is analyzed when |
| 87 |
we see an 'invalid decimal literal' message.""" |
| 88 |
if char not in bad_quotation_marks: |
| 89 |
return |
| 90 |
|
| 91 |
|
| 92 |
try: |
| 93 |
char_name = unicodedata.name(char, "unknown") |
| 94 |
except TypeError: |
| 95 |
return |
| 96 |
|
| 97 |
cause = _( |
| 98 |
"I suspect that you used a fancy unicode quotation mark\n" |
| 99 |
"whose name is {name}.\n" |
| 100 |
"\n" |
| 101 |
).format(name=char_name) |
| 102 |
count = 0 |
| 103 |
for character in line: |
| 104 |
if character in bad_quotation_marks: |
| 105 |
count += 1 |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if count == 1: |
| 110 |
hint = None |
| 111 |
if char in ["‹", "❮"]: |
| 112 |
cause += _("Perhaps, you meant to write a less than sign, `<`.\n") |
| 113 |
hint = _("Did you mean to write a less than sign, `<`?\n") |
| 114 |
elif char in ["›", "❯"]: |
| 115 |
cause += _("Perhaps, you meant to write a greater than sign, `>`.\n") |
| 116 |
hint = _("Did you mean to write a greater than sign, `>`?\n") |
| 117 |
elif char in ["‚", "❟"]: |
| 118 |
cause += _("Perhaps, you meant to write a comma.\n") |
| 119 |
hint = _("Did you mean to write a comma?\n") |
| 120 |
if hint: |
| 121 |
return {"cause": cause, "suggest": hint} |
| 122 |
|
| 123 |
return {} |
| 124 |
|
| 125 |
|
| 126 |
def identify_unicode_fraction(char): |
| 127 |
|
| 128 |
try: |
| 129 |
char_name = unicodedata.name(char, "unknown") |
| 130 |
except TypeError: |
| 131 |
return |
| 132 |
if "FRACTION" not in char_name: |
| 133 |
return |
| 134 |
if char_name == "FRACTION SLASH": |
| 135 |
hint = "Did you mean to use the division operator, `/`?\n" |
| 136 |
cause = _( |
| 137 |
"I suspect that you used the unicode character known as\n" |
| 138 |
"'FRACTION SLASH', which looks similar to\n" |
| 139 |
"but is different from the division operator `/`.\n" |
| 140 |
) |
| 141 |
return {"cause": cause, "suggest": hint} |
| 142 |
|
| 143 |
hint = _("Did you use a unicode fraction?\n") |
| 144 |
cause = _( |
| 145 |
"I suspect that you used the unicode character `{char}`" |
| 146 |
"meant to represent a fraction.\n" |
| 147 |
"The name of this unicode character is {name}.\n" |
| 148 |
).format(char=char, name=char_name) |
| 149 |
|
| 150 |
if not char_name.startswith("VULGAR FRACTION "): |
| 151 |
return {"cause": cause, "suggest": hint} |
| 152 |
|
| 153 |
short_name = char_name.replace("VULGAR FRACTION ", "") |
| 154 |
num, denom = short_name.split(" ") |
| 155 |
for index, word in enumerate( |
| 156 |
["ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"] |
| 157 |
): |
| 158 |
if num == word: |
| 159 |
num = index |
| 160 |
break |
| 161 |
else: |
| 162 |
return {"cause": cause, "suggest": hint} |
| 163 |
|
| 164 |
possible_choices = ( |
| 165 |
("HALF", 2), |
| 166 |
("THIRD", 3), |
| 167 |
("QUARTER", 4), |
| 168 |
("FIFTH", 5), |
| 169 |
("SIXTH", 6), |
| 170 |
("SEVENTH", 7), |
| 171 |
("EIGHTH", 8), |
| 172 |
("NINTH", 9), |
| 173 |
("TENTH", 10), |
| 174 |
) |
| 175 |
for string, denominator in possible_choices: |
| 176 |
if string in denom: |
| 177 |
break |
| 178 |
else: |
| 179 |
return {"cause": cause, "suggest": hint} |
| 180 |
|
| 181 |
hint = _("Did you mean `{num}/{denom}`?\n").format(num=num, denom=denominator) |
| 182 |
cause = _( |
| 183 |
"You used the unicode character {char} which is known as\n" |
| 184 |
"{name}\n" |
| 185 |
"I suspect that you meant to write the fraction `{num}/{denom}` instead.\n" |
| 186 |
).format(num=num, denom=denominator, char=char, name=char_name) |
| 187 |
return {"cause": cause, "suggest": hint} |
| 188 |
|
| 189 |
|
| 190 |
def highlight_single_token(token): |
| 191 |
"""Highlight a single token with ^.""" |
| 192 |
return {token.start_row: " " * token.start_col + "^" * len(token.string)} |
| 193 |
|
| 194 |
|
| 195 |
def highlight_added_token(prev_token, token): |
| 196 |
"""Highlight a newly added token after a previously existing one""" |
| 197 |
token = str(token) |
| 198 |
return {prev_token.start_row: " " * prev_token.end_col + "^" * len(token)} |
| 199 |
|
| 200 |
|
| 201 |
def highlight_two_tokens( |
| 202 |
first, second, first_marker="^", second_marker="^", between=" " |
| 203 |
): |
| 204 |
"""Highlight two individual tokens, and give the possibility to use |
| 205 |
different markers for each one. |
| 206 |
""" |
| 207 |
if first.start_row == second.start_row: |
| 208 |
if first.end_col == second.start_col and between == " ": |
| 209 |
first_marker = second_marker = "^" |
| 210 |
mark = ( |
| 211 |
" " * first.start_col |
| 212 |
+ first_marker * len(first.string) |
| 213 |
+ between * (second.start_col - first.end_col) |
| 214 |
+ second_marker * len(second.string) |
| 215 |
) |
| 216 |
return {first.start_row: mark} |
| 217 |
mark_1 = " " * first.start_col + first_marker * len(first.string) + between.strip() |
| 218 |
mark_2 = " " * second.start_col + second_marker * len(second.string) |
| 219 |
return {first.start_row: mark_1, second.start_row: mark_2} |
| 220 |
|
| 221 |
|
| 222 |
def highlight_range(first, last): |
| 223 |
"""Highlight multiple tokens with ^, from first to last.""" |
| 224 |
mark = " " * first.start_col + "^" * (last.end_col - first.start_col) |
| 225 |
return {first.start_row: mark} |
| 226 |
|
| 227 |
|
| 228 |
def get_last_token_before_specified(bad_token, tokens, specified_token): |
| 229 |
first = bad_token |
| 230 |
brackets = [] |
| 231 |
prev = first |
| 232 |
before_first = None |
| 233 |
found_first = False |
| 234 |
for tok in tokens: |
| 235 |
if tok == bad_token: |
| 236 |
found_first = True |
| 237 |
if prev.string in "([{": |
| 238 |
before_first = prev |
| 239 |
if not found_first: |
| 240 |
prev = tok |
| 241 |
continue |
| 242 |
if tok.string in "([{": |
| 243 |
brackets.append(tok.string) |
| 244 |
elif tok.string in ")]}": |
| 245 |
if not brackets: |
| 246 |
if before_first is not None and matching_brackets( |
| 247 |
before_first, tok.string |
| 248 |
): |
| 249 |
first = before_first |
| 250 |
else: |
| 251 |
return first, None |
| 252 |
else: |
| 253 |
bra = brackets.pop() |
| 254 |
if not matching_brackets(bra, tok.string): |
| 255 |
return first, None |
| 256 |
elif not brackets and tok == specified_token: |
| 257 |
return first, prev |
| 258 |
if ( |
| 259 |
tok.start_row != first.start_row or tok.is_comment() |
| 260 |
): |
| 261 |
if not brackets: |
| 262 |
debug_helper.log("get_last_token_before_specified:") |
| 263 |
debug_helper.log("line continues but not open bracket found.") |
| 264 |
ket = "|" |
| 265 |
else: |
| 266 |
bra = brackets.pop() |
| 267 |
for ket in ")]}": |
| 268 |
if matching_brackets(bra, ket): |
| 269 |
break |
| 270 |
return first, (prev, ket) |
| 271 |
prev = tok |
| 272 |
|
| 273 |
return first, None |
| 274 |
|
| 275 |
|
| 276 |
def highlight_before_specified_token(bad_token, tokens, specified_token): |
| 277 |
first, last = get_last_token_before_specified(bad_token, tokens, specified_token) |
| 278 |
if last is None: |
| 279 |
return {first.start_row: " " * first.start_col + "^" * len(first.string)} |
| 280 |
elif isinstance(last, tuple): |
| 281 |
last = last[0] |
| 282 |
marker = highlight_range(first, last) |
| 283 |
mark = marker[first.start_row] |
| 284 |
mark = mark[:-1] + "^-->" |
| 285 |
return {first.start_row: mark} |
| 286 |
|
| 287 |
return highlight_range(first, last) |
| 288 |
|
| 289 |
|
| 290 |
def get_expression_before_specified_token(bad_token, tokens, specified_token): |
| 291 |
first, last = get_last_token_before_specified(bad_token, tokens, specified_token) |
| 292 |
if last is None: |
| 293 |
return None |
| 294 |
statement_continue = False |
| 295 |
ket = "" |
| 296 |
if isinstance(last, tuple): |
| 297 |
last, ket = last |
| 298 |
statement_continue = True |
| 299 |
new_tokens = [] |
| 300 |
found_bad = False |
| 301 |
for tok in tokens: |
| 302 |
if tok == first: |
| 303 |
found_bad = True |
| 304 |
if not found_bad: |
| 305 |
clone = tok.copy() |
| 306 |
clone.string = "" |
| 307 |
new_tokens.append(clone) |
| 308 |
continue |
| 309 |
if tok is last: |
| 310 |
if statement_continue: |
| 311 |
tok = tok.copy() |
| 312 |
tok.string += "..." + ket |
| 313 |
new_tokens.append(tok) |
| 314 |
break |
| 315 |
new_tokens.append(tok) |
| 316 |
return token_utils.untokenize(new_tokens).strip() |
| 317 |
|
| 318 |
|
| 319 |
def add_mark_to_new_statement(statement_object, new_statement, mark): |
| 320 |
lines = [] |
| 321 |
for line, new_line in zip( |
| 322 |
statement_object.entire_statement.split("\n"), new_statement.split("\n") |
| 323 |
): |
| 324 |
lines.append(new_line) |
| 325 |
if line.strip() != new_line.strip(): |
| 326 |
lines.append(mark) |
| 327 |
return "\n".join(lines) |