-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMethods.py
45 lines (36 loc) · 1.25 KB
/
Methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import sys
from Context import Context
from Error import Error
from Token import Position
from termcolor import colored
"""Methods for global use, only used for error printing"""
def fail(error: Error):
"""Print the given error in red to stdout"""
print()
print(colored(str(error), 'red'))
print(''
f'Context:\n'
f'\tstage --> {error.stage}\n'
f'{error.context}'
)
arrow_str(error.pos, error.context)
def arrow_str(pos: Position, context: Context):
"""Show an arrow under the Error, also print the three lines closest to the error"""
if pos.line > 0:
line_before = f'{(pos.line + 1) - 1}\t|\t{context.file_text.splitlines()[pos.line - 1]}'
print(line_before)
print('\t|')
line = f'{(pos.line + 1)}\t|\t{context.file_text.splitlines()[pos.line]}'
arrow_line = '\t|\t'
arrow = ' '
for i in range(pos.column - 1):
arrow += ' '
for i in range(pos.len):
arrow += '^'
print(line)
print(arrow_line, end='')
print(colored(arrow, 'red'))
if pos.line + 1 < len(context.file_text.splitlines()):
line_after = f'{(pos.line + 1) + 1}\t|\t{context.file_text.splitlines()[pos.line + 1]}'
print(f'{line_after}')
print()