-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathposition.py
53 lines (42 loc) · 1.62 KB
/
position.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
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Nougaro : a python-interpreted high-level programming language
# Copyright (C) 2021-2024 Jean Dubois (https://github.com/jd-develop) <[email protected]>
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# IMPORTS
# no imports
# ##########
# POSITION
# ##########
class Position:
"""Contain file name, index in file, line number and colon"""
def __init__(self, index: int, line_number: int, colon: int, file_name: str, file_txt: str):
"""
index starts at 0
line_number starts at 0
colon starts at 0
"""
self.index: int = index
self.line_number: int = line_number
self.colon: int = colon
self.file_name: str = file_name
self.file_txt: str = file_txt
def advance(self, current_char: str | None = None):
"""Add 1 to the index, automatically make back lines."""
self.index += 1
self.colon += 1
if current_char == '\n':
self.line_number += 1
self.colon = 0
return self
def set_file_name(self, new_file_name: str):
self.file_name = new_file_name
return self
def __repr__(self):
return f"Position at index {self.index} line {self.line_number} colon {self.colon}, in file {self.file_name}."
def copy(self):
"""Return a copy of self"""
return Position(self.index, self.line_number, self.colon, self.file_name, self.file_txt)
DEFAULT_POSITION = Position(0, 0, 0, "(builtin)", "")