tokens.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. class Token(object):
  2. def __init__(self, start_mark, end_mark):
  3. self.start_mark = start_mark
  4. self.end_mark = end_mark
  5. def __repr__(self):
  6. attributes = [key for key in self.__dict__
  7. if not key.endswith('_mark')]
  8. attributes.sort()
  9. arguments = ', '.join(['%s=%r' % (key, getattr(self, key))
  10. for key in attributes])
  11. return '%s(%s)' % (self.__class__.__name__, arguments)
  12. #class BOMToken(Token):
  13. # id = '<byte order mark>'
  14. class DirectiveToken(Token):
  15. id = '<directive>'
  16. def __init__(self, name, value, start_mark, end_mark):
  17. self.name = name
  18. self.value = value
  19. self.start_mark = start_mark
  20. self.end_mark = end_mark
  21. class DocumentStartToken(Token):
  22. id = '<document start>'
  23. class DocumentEndToken(Token):
  24. id = '<document end>'
  25. class StreamStartToken(Token):
  26. id = '<stream start>'
  27. def __init__(self, start_mark=None, end_mark=None,
  28. encoding=None):
  29. self.start_mark = start_mark
  30. self.end_mark = end_mark
  31. self.encoding = encoding
  32. class StreamEndToken(Token):
  33. id = '<stream end>'
  34. class BlockSequenceStartToken(Token):
  35. id = '<block sequence start>'
  36. class BlockMappingStartToken(Token):
  37. id = '<block mapping start>'
  38. class BlockEndToken(Token):
  39. id = '<block end>'
  40. class FlowSequenceStartToken(Token):
  41. id = '['
  42. class FlowMappingStartToken(Token):
  43. id = '{'
  44. class FlowSequenceEndToken(Token):
  45. id = ']'
  46. class FlowMappingEndToken(Token):
  47. id = '}'
  48. class KeyToken(Token):
  49. id = '?'
  50. class ValueToken(Token):
  51. id = ':'
  52. class BlockEntryToken(Token):
  53. id = '-'
  54. class FlowEntryToken(Token):
  55. id = ','
  56. class AliasToken(Token):
  57. id = '<alias>'
  58. def __init__(self, value, start_mark, end_mark):
  59. self.value = value
  60. self.start_mark = start_mark
  61. self.end_mark = end_mark
  62. class AnchorToken(Token):
  63. id = '<anchor>'
  64. def __init__(self, value, start_mark, end_mark):
  65. self.value = value
  66. self.start_mark = start_mark
  67. self.end_mark = end_mark
  68. class TagToken(Token):
  69. id = '<tag>'
  70. def __init__(self, value, start_mark, end_mark):
  71. self.value = value
  72. self.start_mark = start_mark
  73. self.end_mark = end_mark
  74. class ScalarToken(Token):
  75. id = '<scalar>'
  76. def __init__(self, value, plain, start_mark, end_mark, style=None):
  77. self.value = value
  78. self.plain = plain
  79. self.start_mark = start_mark
  80. self.end_mark = end_mark
  81. self.style = style