mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Use the new Range class in more places
This commit is contained in:
parent
56274d7c1f
commit
3bcc9f4cbd
10 changed files with 91 additions and 90 deletions
|
@ -23,7 +23,6 @@ import typing as T
|
|||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
|
||||
from .errors import CompileError, CompilerBugError
|
||||
from . import utils
|
||||
|
||||
|
||||
|
@ -69,6 +68,8 @@ class Token:
|
|||
return Range(self.start, self.end, self.string)
|
||||
|
||||
def get_number(self) -> T.Union[int, float]:
|
||||
from .errors import CompileError, CompilerBugError
|
||||
|
||||
if self.type != TokenType.NUMBER:
|
||||
raise CompilerBugError()
|
||||
|
||||
|
@ -81,12 +82,12 @@ class Token:
|
|||
else:
|
||||
return int(string)
|
||||
except:
|
||||
raise CompileError(
|
||||
f"{str(self)} is not a valid number literal", self.start, self.end
|
||||
)
|
||||
raise CompileError(f"{str(self)} is not a valid number literal", self.range)
|
||||
|
||||
|
||||
def _tokenize(ui_ml: str):
|
||||
from .errors import CompileError
|
||||
|
||||
i = 0
|
||||
while i < len(ui_ml):
|
||||
matched = False
|
||||
|
@ -101,7 +102,8 @@ def _tokenize(ui_ml: str):
|
|||
|
||||
if not matched:
|
||||
raise CompileError(
|
||||
"Could not determine what kind of syntax is meant here", i, i
|
||||
"Could not determine what kind of syntax is meant here",
|
||||
Range(i, i, ui_ml),
|
||||
)
|
||||
|
||||
yield Token(TokenType.EOF, i, i, ui_ml)
|
||||
|
@ -117,6 +119,10 @@ class Range:
|
|||
end: int
|
||||
original_text: str
|
||||
|
||||
@property
|
||||
def length(self) -> int:
|
||||
return self.end - self.start
|
||||
|
||||
@property
|
||||
def text(self) -> str:
|
||||
return self.original_text[self.start : self.end]
|
||||
|
@ -137,3 +143,6 @@ class Range:
|
|||
|
||||
def to_json(self):
|
||||
return utils.idxs_to_range(self.start, self.end, self.original_text)
|
||||
|
||||
def overlaps(self, other: "Range") -> bool:
|
||||
return not (self.end < other.start or self.start > other.end)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue