|
| 1 | +from spellchecker import SpellChecker |
| 2 | + |
| 3 | +from visionscript.usage import ( |
| 4 | + language_grammar_reference, |
| 5 | + lowercase_language_grammar_reference, |
| 6 | +) |
| 7 | + |
| 8 | +spell = SpellChecker() |
| 9 | + |
| 10 | + |
| 11 | +def handle_unexpected_characters(e, code, interactive=False): |
| 12 | + # if line doesn't end with ], add it |
| 13 | + if not code.strip().endswith("]"): |
| 14 | + code += "]" |
| 15 | + |
| 16 | + return |
| 17 | + |
| 18 | + # if space between statement and [, remove it |
| 19 | + # get position of [ |
| 20 | + position = code.find("[") |
| 21 | + |
| 22 | + if code[position - 1] == " ": |
| 23 | + code = code[: position - 1] + code[position:] |
| 24 | + |
| 25 | + return |
| 26 | + |
| 27 | + # replace all “ with " |
| 28 | + code = code.replace("“", '"') |
| 29 | + code = code.replace("”", '"') |
| 30 | + |
| 31 | + # raise error if character not in grammar |
| 32 | + if e.char not in ["[", "]", "'", '"', ",", " ", '"', '"', "\n", "\t", "\r"]: |
| 33 | + print(f"Syntax error on line {e.line}, column {e.column}.") |
| 34 | + print(f"Unexpected character: {e.char!r}") |
| 35 | + exit(1) |
| 36 | + |
| 37 | + # raise error if class doesn't exist |
| 38 | + line = e.line |
| 39 | + column = e.column |
| 40 | + |
| 41 | + # check if function name in grammar |
| 42 | + function_name = code.strip().split("\n")[line - 1].split("[")[0].strip() |
| 43 | + |
| 44 | + language_grammar_reference_keys = language_grammar_reference.keys() |
| 45 | + |
| 46 | + if function_name in language_grammar_reference_keys: |
| 47 | + print(f"Syntax error on line {line}, column {column}.") |
| 48 | + print(f"Unexpected character: {e.char!r}") |
| 49 | + exit(1) |
| 50 | + |
| 51 | + spell.known(lowercase_language_grammar_reference) |
| 52 | + spell.word_frequency.load_words(lowercase_language_grammar_reference) |
| 53 | + |
| 54 | + alternatives = spell.candidates(function_name) |
| 55 | + |
| 56 | + if len(alternatives) == 0: |
| 57 | + print(f"Function {function_name} does not exist.") |
| 58 | + exit(1) |
| 59 | + |
| 60 | + print(f"Function '{function_name}' does not exist. Did you mean one of these?") |
| 61 | + print("-" * 10) |
| 62 | + |
| 63 | + for item in list(alternatives): |
| 64 | + if item.lower() in lowercase_language_grammar_reference: |
| 65 | + print( |
| 66 | + list(language_grammar_reference.keys())[ |
| 67 | + lowercase_language_grammar_reference.index(item.lower()) |
| 68 | + ] |
| 69 | + ) |
| 70 | + |
| 71 | + if interactive is False: |
| 72 | + exit(1) |
| 73 | + |
| 74 | + |
| 75 | +def handle_unexpected_token(e, interactive=False): |
| 76 | + line = e.line |
| 77 | + column = e.column |
| 78 | + |
| 79 | + print(f"Syntax error on line {line}, column {column}.") |
| 80 | + print(f"Unexpected token: {e.token!r}") |
| 81 | + if interactive is False: |
| 82 | + exit(1) |
0 commit comments