Skip to content

Source

source

COMMENTS = '(?:\\/\\/[^\\n\\r]+?(?:\\*\\)|[\\n\\r]))' module-attribute

WHITESPACES = re.compile('(?:\\s|' + COMMENTS + ')*') module-attribute

Location dataclass

Source code in xdsl/frontend/listlang/source.py
11
12
13
@dataclass
class Location:
    pos: int

pos: int instance-attribute

__init__(pos: int) -> None

Located dataclass

Bases: Generic[_LocatedCovT]

Source code in xdsl/frontend/listlang/source.py
19
20
21
22
23
24
25
@dataclass(frozen=True)
class Located(Generic[_LocatedCovT]):
    loc: Location
    value: _LocatedCovT

    def __bool__(self) -> bool:
        return bool(self.value)

loc: Location instance-attribute

value: _LocatedCovT instance-attribute

__init__(loc: Location, value: _LocatedCovT) -> None

__bool__() -> bool

Source code in xdsl/frontend/listlang/source.py
24
25
def __bool__(self) -> bool:
    return bool(self.value)

CodeCursor

Source code in xdsl/frontend/listlang/source.py
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
class CodeCursor:
    code: str
    pos: int

    def __init__(self, code: str):
        self.code = code
        self.pos = 0

    def _whitespace_end(self) -> int:
        match = WHITESPACES.match(self.code, self.pos)
        assert match is not None
        return match.end()

    def skip_whitespaces(self):
        self.pos = self._whitespace_end()

    def next_regex(self, regex: re.Pattern[str]) -> Located[re.Match[str] | None]:
        match = self.peek_regex(regex)
        if match.value is not None:
            self.pos = match.value.end()
        return match

    def peek_regex(self, regex: re.Pattern[str]) -> Located[re.Match[str] | None]:
        pos = self._whitespace_end()
        return Located(Location(pos), regex.match(self.code, pos))

code: str = code instance-attribute

pos: int = 0 instance-attribute

__init__(code: str)

Source code in xdsl/frontend/listlang/source.py
32
33
34
def __init__(self, code: str):
    self.code = code
    self.pos = 0

skip_whitespaces()

Source code in xdsl/frontend/listlang/source.py
41
42
def skip_whitespaces(self):
    self.pos = self._whitespace_end()

next_regex(regex: re.Pattern[str]) -> Located[re.Match[str] | None]

Source code in xdsl/frontend/listlang/source.py
44
45
46
47
48
def next_regex(self, regex: re.Pattern[str]) -> Located[re.Match[str] | None]:
    match = self.peek_regex(regex)
    if match.value is not None:
        self.pos = match.value.end()
    return match

peek_regex(regex: re.Pattern[str]) -> Located[re.Match[str] | None]

Source code in xdsl/frontend/listlang/source.py
50
51
52
def peek_regex(self, regex: re.Pattern[str]) -> Located[re.Match[str] | None]:
    pos = self._whitespace_end()
    return Located(Location(pos), regex.match(self.code, pos))

ParseError dataclass

Bases: Exception

Source code in xdsl/frontend/listlang/source.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@dataclass
class ParseError(Exception):
    position: int
    msg: str

    @staticmethod
    def from_loc(loc: Location, msg: str) -> "ParseError":
        return ParseError(loc.pos, msg)

    def line_column(self, code: str) -> tuple[int, int]:
        if self.position < 0 or self.position > len(code):
            raise ValueError("error position is out of code range")

        line = code.count("\n", 0, self.position) + 1

        last_newline = code.rfind("\n", 0, self.position)
        column = self.position - last_newline
        return line, column

position: int instance-attribute

msg: str instance-attribute

__init__(position: int, msg: str) -> None

from_loc(loc: Location, msg: str) -> ParseError staticmethod

Source code in xdsl/frontend/listlang/source.py
60
61
62
@staticmethod
def from_loc(loc: Location, msg: str) -> "ParseError":
    return ParseError(loc.pos, msg)

line_column(code: str) -> tuple[int, int]

Source code in xdsl/frontend/listlang/source.py
64
65
66
67
68
69
70
71
72
def line_column(self, code: str) -> tuple[int, int]:
    if self.position < 0 or self.position > len(code):
        raise ValueError("error position is out of code range")

    line = code.count("\n", 0, self.position) + 1

    last_newline = code.rfind("\n", 0, self.position)
    column = self.position - last_newline
    return line, column