| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 |
# you may not use this file except in compliance with the License. |
| 3 |
# You may obtain a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
import sys |
| 14 |
import yaml |
| 15 |
|
| 16 |
if sys.version_info >= (3, 8): |
| 17 |
from functools import cached_property |
| 18 |
else: |
| 19 |
from .cached_property import cached_property |
| 20 |
|
| 21 |
|
| 22 |
LINE_SEPARATORS = "\0\r\n\x85\u2028\u2029" |
| 23 |
WHITESPACE_CHARS = " \t" |
| 24 |
|
| 25 |
|
| 26 |
class Pos: |
| 27 |
@classmethod |
| 28 |
def from_node(cls, node, line_ofs=0, column_ofs=0): |
| 29 |
mark = node.start_mark |
| 30 |
return cls(mark, mark.name, mark.line + line_ofs, mark.column + column_ofs) |
| 31 |
|
| 32 |
@classmethod |
| 33 |
def from_file(cls, path, text): |
| 34 |
mark = yaml.Mark(str(path), 0, 0, 0, text, 0) |
| 35 |
return cls(mark, path, 0, 0) |
| 36 |
|
| 37 |
def __init__(self, mark, path, line, column): |
| 38 |
self._mark = mark |
| 39 |
self.path = path |
| 40 |
self.line = line # Starts from 0. |
| 41 |
self.column = column # Starts from 0. |
| 42 |
|
| 43 |
def __repr__(self): |
| 44 |
return f"<Pos {self.path}:{self.line}:{self.column}>" |
| 45 |
|
| 46 |
def with_offset(self, line_ofs=0, column_ofs=0): |
| 47 |
line_ptr = self._move_ptr_by_lines(line_ofs) |
| 48 |
ptr = line_ptr + column_ofs |
| 49 |
mark = self._clone_mark(self._mark, ptr) |
| 50 |
if line_ofs: |
| 51 |
column = column_ofs # Start from new line. |
| 52 |
else: |
| 53 |
column = self.column + column_ofs |
| 54 |
return Pos(mark, self.path, self.line + line_ofs, column) |
| 55 |
|
| 56 |
def with_contents_start(self): |
| 57 |
ptr = self._mark.pointer |
| 58 |
buf = self._mark.buffer |
| 59 |
while ( |
| 60 |
ptr < len(buf) |
| 61 |
and buf[ptr] not in LINE_SEPARATORS |
| 62 |
and buf[ptr] in WHITESPACE_CHARS |
| 63 |
): |
| 64 |
ptr += 1 |
| 65 |
mark = self._clone_mark(self._mark, ptr) |
| 66 |
return Pos(mark, self.path, self.line, self.column + ptr - self._mark.pointer) |
| 67 |
|
| 68 |
@cached_property |
| 69 |
def snippet(self): |
| 70 |
return self._mark.get_snippet(max_length=100) |
| 71 |
|
| 72 |
@cached_property |
| 73 |
def body(self): |
| 74 |
return self._mark.buffer[self._mark.pointer :] |
| 75 |
|
| 76 |
def _clone_mark(self, mark, ptr): |
| 77 |
return yaml.Mark( |
| 78 |
mark.name, |
| 79 |
mark.index, |
| 80 |
mark.line, |
| 81 |
mark.column, |
| 82 |
mark.buffer, |
| 83 |
ptr, |
| 84 |
) |
| 85 |
|
| 86 |
def _move_ptr_by_lines(self, line_ofs): |
| 87 |
ptr = self._mark.pointer |
| 88 |
buf = self._mark.buffer |
| 89 |
while line_ofs > 0 and ptr < len(buf): |
| 90 |
if buf[ptr] in LINE_SEPARATORS: |
| 91 |
line_ofs -= 1 |
| 92 |
ptr += 1 |
| 93 |
return ptr |