source: pyyaml/trunk/lib/yaml/tokens.py @ 222

Revision 222, 2.5 KB checked in by xi, 8 years ago (diff)

Subclass all base classes from object.

Hold references to the objects being represented (should fix #22).

The value of a mapping node is represented as a list of pairs (key, value)
now.

Sort dictionary items (fix #23).

Recursive structures are now loaded and dumped correctly, including complex
structures like recursive tuples (fix #5). Thanks Peter Murphy for the patches.
To make it possible, representer functions are allowed to be generators.
In this case, the first generated value is an object. Other values produced
by the representer are ignored.

Make Representer not try to guess !!pairs when a list is represented.
You need to construct a !!pairs node explicitly now.

Do not check for duplicate mapping keys as it didn't work correctly anyway.

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