| 1 |
# # header |
| 2 |
# coding: utf-8 |
| 3 |
|
| 4 |
from __future__ import unicode_literals |
| 5 |
|
| 6 |
if False: # MYPY |
| 7 |
from typing import Text, Any, Dict, Optional, List # NOQA |
| 8 |
from .error import StreamMark # NOQA |
| 9 |
|
| 10 |
SHOWLINES = True |
| 11 |
|
| 12 |
|
| 13 |
class Token(object): |
| 14 |
__slots__ = "start_mark", "end_mark", "_comment" |
| 15 |
|
| 16 |
def __init__(self, start_mark, end_mark): |
| 17 |
# type: (StreamMark, StreamMark) -> None |
| 18 |
self.start_mark = start_mark |
| 19 |
self.end_mark = end_mark |
| 20 |
|
| 21 |
def __repr__(self): |
| 22 |
# type: () -> Any |
| 23 |
# attributes = [key for key in self.__slots__ if not key.endswith('_mark') and |
| 24 |
# hasattr('self', key)] |
| 25 |
attributes = [key for key in self.__slots__ if not key.endswith("_mark")] |
| 26 |
attributes.sort() |
| 27 |
arguments = ", ".join( |
| 28 |
["%s=%r" % (key, getattr(self, key)) for key in attributes] |
| 29 |
) |
| 30 |
if SHOWLINES: |
| 31 |
try: |
| 32 |
arguments += ", line: " + str(self.start_mark.line) |
| 33 |
except: # NOQA |
| 34 |
pass |
| 35 |
try: |
| 36 |
arguments += ", comment: " + str(self._comment) |
| 37 |
except: # NOQA |
| 38 |
pass |
| 39 |
return "{}({})".format(self.__class__.__name__, arguments) |
| 40 |
|
| 41 |
def add_post_comment(self, comment): |
| 42 |
# type: (Any) -> None |
| 43 |
if not hasattr(self, "_comment"): |
| 44 |
self._comment = [None, None] |
| 45 |
self._comment[0] = comment |
| 46 |
|
| 47 |
def add_pre_comments(self, comments): |
| 48 |
# type: (Any) -> None |
| 49 |
if not hasattr(self, "_comment"): |
| 50 |
self._comment = [None, None] |
| 51 |
assert self._comment[1] is None |
| 52 |
self._comment[1] = comments |
| 53 |
|
| 54 |
def get_comment(self): |
| 55 |
# type: () -> Any |
| 56 |
return getattr(self, "_comment", None) |
| 57 |
|
| 58 |
@property |
| 59 |
def comment(self): |
| 60 |
# type: () -> Any |
| 61 |
return getattr(self, "_comment", None) |
| 62 |
|
| 63 |
def move_comment(self, target, empty=False): |
| 64 |
# type: (Any, bool) -> Any |
| 65 |
"""move a comment from this token to target (normally next token) |
| 66 |
used to combine e.g. comments before a BlockEntryToken to the |
| 67 |
ScalarToken that follows it |
| 68 |
empty is a special for empty values -> comment after key |
| 69 |
""" |
| 70 |
c = self.comment |
| 71 |
if c is None: |
| 72 |
return |
| 73 |
# don't push beyond last element |
| 74 |
if isinstance(target, (StreamEndToken, DocumentStartToken)): |
| 75 |
return |
| 76 |
delattr(self, "_comment") |
| 77 |
tc = target.comment |
| 78 |
if not tc: # target comment, just insert |
| 79 |
# special for empty value in key: value issue 25 |
| 80 |
if empty: |
| 81 |
c = [c[0], c[1], None, None, c[0]] |
| 82 |
target._comment = c |
| 83 |
# nprint('mco2:', self, target, target.comment, empty) |
| 84 |
return self |
| 85 |
if c[0] and tc[0] or c[1] and tc[1]: |
| 86 |
raise NotImplementedError("overlap in comment %r %r" % (c, tc)) |
| 87 |
if c[0]: |
| 88 |
tc[0] = c[0] |
| 89 |
if c[1]: |
| 90 |
tc[1] = c[1] |
| 91 |
return self |
| 92 |
|
| 93 |
def split_comment(self): |
| 94 |
# type: () -> Any |
| 95 |
"""split the post part of a comment, and return it |
| 96 |
as comment to be added. Delete second part if [None, None] |
| 97 |
abc: # this goes to sequence |
| 98 |
# this goes to first element |
| 99 |
- first element |
| 100 |
""" |
| 101 |
comment = self.comment |
| 102 |
if comment is None or comment[0] is None: |
| 103 |
return None # nothing to do |
| 104 |
ret_val = [comment[0], None] |
| 105 |
if comment[1] is None: |
| 106 |
delattr(self, "_comment") |
| 107 |
return ret_val |
| 108 |
|
| 109 |
|
| 110 |
# class BOMToken(Token): |
| 111 |
# id = '<byte order mark>' |
| 112 |
|
| 113 |
|
| 114 |
class DirectiveToken(Token): |
| 115 |
__slots__ = "name", "value" |
| 116 |
id = "<directive>" |
| 117 |
|
| 118 |
def __init__(self, name, value, start_mark, end_mark): |
| 119 |
# type: (Any, Any, Any, Any) -> None |
| 120 |
Token.__init__(self, start_mark, end_mark) |
| 121 |
self.name = name |
| 122 |
self.value = value |
| 123 |
|
| 124 |
|
| 125 |
class DocumentStartToken(Token): |
| 126 |
__slots__ = () |
| 127 |
id = "<document start>" |
| 128 |
|
| 129 |
|
| 130 |
class DocumentEndToken(Token): |
| 131 |
__slots__ = () |
| 132 |
id = "<document end>" |
| 133 |
|
| 134 |
|
| 135 |
class StreamStartToken(Token): |
| 136 |
__slots__ = ("encoding",) |
| 137 |
id = "<stream start>" |
| 138 |
|
| 139 |
def __init__(self, start_mark=None, end_mark=None, encoding=None): |
| 140 |
# type: (Any, Any, Any) -> None |
| 141 |
Token.__init__(self, start_mark, end_mark) |
| 142 |
self.encoding = encoding |
| 143 |
|
| 144 |
|
| 145 |
class StreamEndToken(Token): |
| 146 |
__slots__ = () |
| 147 |
id = "<stream end>" |
| 148 |
|
| 149 |
|
| 150 |
class BlockSequenceStartToken(Token): |
| 151 |
__slots__ = () |
| 152 |
id = "<block sequence start>" |
| 153 |
|
| 154 |
|
| 155 |
class BlockMappingStartToken(Token): |
| 156 |
__slots__ = () |
| 157 |
id = "<block mapping start>" |
| 158 |
|
| 159 |
|
| 160 |
class BlockEndToken(Token): |
| 161 |
__slots__ = () |
| 162 |
id = "<block end>" |
| 163 |
|
| 164 |
|
| 165 |
class FlowSequenceStartToken(Token): |
| 166 |
__slots__ = () |
| 167 |
id = "[" |
| 168 |
|
| 169 |
|
| 170 |
class FlowMappingStartToken(Token): |
| 171 |
__slots__ = () |
| 172 |
id = "{" |
| 173 |
|
| 174 |
|
| 175 |
class FlowSequenceEndToken(Token): |
| 176 |
__slots__ = () |
| 177 |
id = "]" |
| 178 |
|
| 179 |
|
| 180 |
class FlowMappingEndToken(Token): |
| 181 |
__slots__ = () |
| 182 |
id = "}" |
| 183 |
|
| 184 |
|
| 185 |
class KeyToken(Token): |
| 186 |
__slots__ = () |
| 187 |
id = "?" |
| 188 |
|
| 189 |
# def x__repr__(self): |
| 190 |
# return 'KeyToken({})'.format( |
| 191 |
# self.start_mark.buffer[self.start_mark.index:].split(None, 1)[0]) |
| 192 |
|
| 193 |
|
| 194 |
class ValueToken(Token): |
| 195 |
__slots__ = () |
| 196 |
id = ":" |
| 197 |
|
| 198 |
|
| 199 |
class BlockEntryToken(Token): |
| 200 |
__slots__ = () |
| 201 |
id = "-" |
| 202 |
|
| 203 |
|
| 204 |
class FlowEntryToken(Token): |
| 205 |
__slots__ = () |
| 206 |
id = "," |
| 207 |
|
| 208 |
|
| 209 |
class AliasToken(Token): |
| 210 |
__slots__ = ("value",) |
| 211 |
id = "<alias>" |
| 212 |
|
| 213 |
def __init__(self, value, start_mark, end_mark): |
| 214 |
# type: (Any, Any, Any) -> None |
| 215 |
Token.__init__(self, start_mark, end_mark) |
| 216 |
self.value = value |
| 217 |
|
| 218 |
|
| 219 |
class AnchorToken(Token): |
| 220 |
__slots__ = ("value",) |
| 221 |
id = "<anchor>" |
| 222 |
|
| 223 |
def __init__(self, value, start_mark, end_mark): |
| 224 |
# type: (Any, Any, Any) -> None |
| 225 |
Token.__init__(self, start_mark, end_mark) |
| 226 |
self.value = value |
| 227 |
|
| 228 |
|
| 229 |
class TagToken(Token): |
| 230 |
__slots__ = ("value",) |
| 231 |
id = "<tag>" |
| 232 |
|
| 233 |
def __init__(self, value, start_mark, end_mark): |
| 234 |
# type: (Any, Any, Any) -> None |
| 235 |
Token.__init__(self, start_mark, end_mark) |
| 236 |
self.value = value |
| 237 |
|
| 238 |
|
| 239 |
class ScalarToken(Token): |
| 240 |
__slots__ = "value", "plain", "style" |
| 241 |
id = "<scalar>" |
| 242 |
|
| 243 |
def __init__(self, value, plain, start_mark, end_mark, style=None): |
| 244 |
# type: (Any, Any, Any, Any, Any) -> None |
| 245 |
Token.__init__(self, start_mark, end_mark) |
| 246 |
self.value = value |
| 247 |
self.plain = plain |
| 248 |
self.style = style |
| 249 |
|
| 250 |
|
| 251 |
class CommentToken(Token): |
| 252 |
__slots__ = "value", "pre_done" |
| 253 |
id = "<comment>" |
| 254 |
|
| 255 |
def __init__(self, value, start_mark, end_mark): |
| 256 |
# type: (Any, Any, Any) -> None |
| 257 |
Token.__init__(self, start_mark, end_mark) |
| 258 |
self.value = value |
| 259 |
|
| 260 |
def reset(self): |
| 261 |
# type: () -> None |
| 262 |
if hasattr(self, "pre_done"): |
| 263 |
delattr(self, "pre_done") |
| 264 |
|
| 265 |
def __repr__(self): |
| 266 |
# type: () -> Any |
| 267 |
v = "{!r}".format(self.value) |
| 268 |
if SHOWLINES: |
| 269 |
try: |
| 270 |
v += ", line: " + str(self.start_mark.line) |
| 271 |
v += ", col: " + str(self.start_mark.column) |
| 272 |
except: # NOQA |
| 273 |
pass |
| 274 |
return "CommentToken({})".format(v) |
| 275 |
|
| 276 |
def __eq__(self, other): |
| 277 |
# type: (Any) -> bool |
| 278 |
if self.start_mark != other.start_mark: |
| 279 |
return False |
| 280 |
if self.end_mark != other.end_mark: |
| 281 |
return False |
| 282 |
if self.value != other.value: |
| 283 |
return False |
| 284 |
return True |
| 285 |
|
| 286 |
def __ne__(self, other): |
| 287 |
# type: (Any) -> bool |
| 288 |
return not self.__eq__(other) |