You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

385 lines
11 KiB

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