<s> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> from error import MarkedYAMLError <EOL> from tokens import * <EOL> from events import * <EOL> from scanner import * <EOL> class ParserError ( MarkedYAMLError ) : <EOL> pass <EOL> class Parser ( object ) : <EOL> DEFAULT_TAGS = { <EOL> u'<STR_LIT:!>' : u'<STR_LIT:!>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> } <EOL> def __init__ ( self ) : <EOL> self . current_event = None <EOL> self . yaml_version = None <EOL> self . tag_handles = { } <EOL> self . states = [ ] <EOL> self . marks = [ ] <EOL> self . state = self . parse_stream_start <EOL> def dispose ( self ) : <EOL> self . states = [ ] <EOL> self . state = None <EOL> def check_event ( self , * choices ) : <EOL> if self . current_event is None : <EOL> if self . state : <EOL> self . current_event = self . state ( ) <EOL> if self . current_event is not None : <EOL> if not choices : <EOL> return True <EOL> for choice in choices : <EOL> if isinstance ( self . current_event , choice ) : <EOL> return True <EOL> return False <EOL> def peek_event ( self ) : <EOL> if self . current_event is None : <EOL> if self . state : <EOL> self . current_event = self . state ( ) <EOL> return self . current_event <EOL> def get_event ( self ) : <EOL> if self . current_event is None : <EOL> if self . state : <EOL> self . current_event = self . state ( ) <EOL> value = self . current_event <EOL> self . current_event = None <EOL> return value <EOL> def parse_stream_start ( self ) : <EOL> token = self . get_token ( ) <EOL> event = StreamStartEvent ( token . start_mark , token . end_mark , <EOL> encoding = token . encoding ) <EOL> self . state = self . parse_implicit_document_start <EOL> return event <EOL> def parse_implicit_document_start ( self ) : <EOL> if not self . check_token ( DirectiveToken , DocumentStartToken , <EOL> StreamEndToken ) : <EOL> self . tag_handles = self . DEFAULT_TAGS <EOL> token = self . peek_token ( ) <EOL> start_mark = end_mark = token . start_mark <EOL> event = DocumentStartEvent ( start_mark , end_mark , <EOL> explicit = False ) <EOL> self . states . append ( self . parse_document_end ) <EOL> self . state = self . parse_block_node <EOL> return event <EOL> else : <EOL> return self . parse_document_start ( ) <EOL> def parse_document_start ( self ) : <EOL> while self . check_token ( DocumentEndToken ) : <EOL> self . get_token ( ) <EOL> if not self . check_token ( StreamEndToken ) : <EOL> token = self . peek_token ( ) <EOL> start_mark = token . start_mark <EOL> version , tags = self . process_directives ( ) <EOL> if not self . check_token ( DocumentStartToken ) : <EOL> raise ParserError ( None , None , <EOL> "<STR_LIT>" <EOL> % self . peek_token ( ) . id , <EOL> self . peek_token ( ) . start_mark ) <EOL> token = self . get_token ( ) <EOL> end_mark = token . end_mark <EOL> event = DocumentStartEvent ( start_mark , end_mark , <EOL> explicit = True , version = version , tags = tags ) <EOL> self . states . append ( self . parse_document_end ) <EOL> self . state = self . parse_document_content <EOL> else : <EOL> token = self . get_token ( ) <EOL> event = StreamEndEvent ( token . start_mark , token . end_mark ) <EOL> assert not self . states <EOL> assert not self . marks <EOL> self . state = None <EOL> return event <EOL> def parse_document_end ( self ) : <EOL> token = self . peek_token ( ) <EOL> start_mark = end_mark = token . start_mark <EOL> explicit = False <EOL> if self . check_token ( DocumentEndToken ) : <EOL> token = self . get_token ( ) <EOL> end_mark = token . end_mark <EOL> explicit = True <EOL> event = DocumentEndEvent ( start_mark , end_mark , <EOL> explicit = explicit ) <EOL> self . state = self . parse_document_start <EOL> return event <EOL> def parse_document_content ( self ) : <EOL> if self . check_token ( DirectiveToken , <EOL> DocumentStartToken , DocumentEndToken , StreamEndToken ) : <EOL> event = self . process_empty_scalar ( self . peek_token ( ) . start_mark ) <EOL> self . state = self . states . pop ( ) <EOL> return event <EOL> else : <EOL> return self . parse_block_node ( ) <EOL> def process_directives ( self ) : <EOL> self . yaml_version = None <EOL> self . tag_handles = { } <EOL> while self . check_token ( DirectiveToken ) : <EOL> token = self . get_token ( ) <EOL> if token . name == u'<STR_LIT>' : <EOL> if self . yaml_version is not None : <EOL> raise ParserError ( None , None , <EOL> "<STR_LIT>" , token . start_mark ) <EOL> major , minor = token . value <EOL> if major != <NUM_LIT:1> : <EOL> raise ParserError ( None , None , <EOL> "<STR_LIT>" , <EOL> token . start_mark ) <EOL> self . yaml_version = token . value <EOL> elif token . name == u'<STR_LIT>' : <EOL> handle , prefix = token . value <EOL> if handle in self . tag_handles : <EOL> raise ParserError ( None , None , <EOL> "<STR_LIT>" % handle . encode ( '<STR_LIT:utf-8>' ) , <EOL> token . start_mark ) <EOL> self . tag_handles [ handle ] = prefix <EOL> if self . tag_handles : <EOL> value = self . yaml_version , self . tag_handles . copy ( ) <EOL> else : <EOL> value = self . yaml_version , None <EOL> for key in self . DEFAULT_TAGS : <EOL> if key not in self . tag_handles : <EOL> self . tag_handles [ key ] = self . DEFAULT_TAGS [ key ] <EOL> return value <EOL> def parse_block_node ( self ) : <EOL> return self . parse_node ( block = True ) <EOL> def parse_flow_node ( self ) : <EOL> return self . parse_node ( ) <EOL> def parse_block_node_or_indentless_sequence ( self ) : <EOL> return self . parse_node ( block = True , indentless_sequence = True ) <EOL> def parse_node ( self , block = False , indentless_sequence = False ) : <EOL> if self . check_token ( AliasToken ) : <EOL> token = self . get_token ( ) <EOL> event = AliasEvent ( token . value , token . start_mark , token . end_mark ) <EOL> self . state = self . states . pop ( ) <EOL> else : <EOL> anchor = None <EOL> tag = None <EOL> start_mark = end_mark = tag_mark = None <EOL> if self . check_token ( AnchorToken ) : <EOL> token = self . get_token ( ) <EOL> start_mark = token . start_mark <EOL> end_mark = token . end_mark <EOL> anchor = token . value <EOL> if self . check_token ( TagToken ) : <EOL> token = self . get_token ( ) <EOL> tag_mark = token . start_mark <EOL> end_mark = token . end_mark <EOL> tag = token . value <EOL> elif self . check_token ( TagToken ) : <EOL> token = self . get_token ( ) <EOL> start_mark = tag_mark = token . start_mark <EOL> end_mark = token . end_mark <EOL> tag = token . value <EOL> if self . check_token ( AnchorToken ) : <EOL> token = self . get_token ( ) <EOL> end_mark = token . end_mark <EOL> anchor = token . value <EOL> if tag is not None : <EOL> handle , suffix = tag <EOL> if handle is not None : <EOL> if handle not in self . tag_handles : <EOL> raise ParserError ( "<STR_LIT>" , start_mark , <EOL> "<STR_LIT>" % handle . encode ( '<STR_LIT:utf-8>' ) , <EOL> tag_mark ) <EOL> tag = self . tag_handles [ handle ] + suffix <EOL> else : <EOL> tag = suffix <EOL> if start_mark is None : <EOL> start_mark = end_mark = self . peek_token ( ) . start_mark <EOL> event = None <EOL> implicit = ( tag is None or tag == u'<STR_LIT:!>' ) <EOL> if indentless_sequence and self . check_token ( BlockEntryToken ) : <EOL> end_mark = self . peek_token ( ) . end_mark <EOL> event = SequenceStartEvent ( anchor , tag , implicit , <EOL> start_mark , end_mark ) <EOL> self . state = self . parse_indentless_sequence_entry <EOL> else : <EOL> if self . check_token ( ScalarToken ) : <EOL> token = self . get_token ( ) <EOL> end_mark = token . end_mark <EOL> if ( token . plain and tag is None ) or tag == u'<STR_LIT:!>' : <EOL> implicit = ( True , False ) <EOL> elif tag is None : <EOL> implicit = ( False , True ) <EOL> else : <EOL> implicit = ( False , False ) <EOL> event = ScalarEvent ( anchor , tag , implicit , token . value , <EOL> start_mark , end_mark , style = token . style ) <EOL> self . state = self . states . pop ( ) <EOL> elif self . check_token ( FlowSequenceStartToken ) : <EOL> end_mark = self . peek_token ( ) . end_mark <EOL> event = SequenceStartEvent ( anchor , tag , implicit , <EOL> start_mark , end_mark , flow_style = True ) <EOL> self . state = self . parse_flow_sequence_first_entry <EOL> elif self . check_token ( FlowMappingStartToken ) : <EOL> end_mark = self . peek_token ( ) . end_mark <EOL> event = MappingStartEvent ( anchor , tag , implicit , <EOL> start_mark , end_mark , flow_style = True ) <EOL> self . state = self . parse_flow_mapping_first_key <EOL> elif block and self . check_token ( BlockSequenceStartToken ) : <EOL> end_mark = self . peek_token ( ) . start_mark <EOL> event = SequenceStartEvent ( anchor , tag , implicit , <EOL> start_mark , end_mark , flow_style = False ) <EOL> self . state = self . parse_block_sequence_first_entry <EOL> elif block and self . check_token ( BlockMappingStartToken ) : <EOL> end_mark = self . peek_token ( ) . start_mark <EOL> event = MappingStartEvent ( anchor , tag , implicit , <EOL> start_mark , end_mark , flow_style = False ) <EOL> self . state = self . parse_block_mapping_first_key <EOL> elif anchor is not None or tag is not None : <EOL> event = ScalarEvent ( anchor , tag , ( implicit , False ) , u'<STR_LIT>' , <EOL> start_mark , end_mark ) <EOL> self . state = self . states . pop ( ) <EOL> else : <EOL> if block : <EOL> node = '<STR_LIT>' <EOL> else : <EOL> node = '<STR_LIT>' <EOL> token = self . peek_token ( ) <EOL> raise ParserError ( "<STR_LIT>" % node , start_mark , <EOL> "<STR_LIT>" % token . id , <EOL> token . start_mark ) <EOL> return event <EOL> def parse_block_sequence_first_entry ( self ) : <EOL> token = self . get_token ( ) <EOL> self . marks . append ( token . start_mark ) <EOL> return self . parse_block_sequence_entry ( ) <EOL> def parse_block_sequence_entry ( self ) : <EOL> if self . check_token ( BlockEntryToken ) : <EOL> token = self . get_token ( ) <EOL> if not self . check_token ( BlockEntryToken , BlockEndToken ) : <EOL> self . states . append ( self . parse_block_sequence_entry ) <EOL> return self . parse_block_node ( ) <EOL> else : <EOL> self . state = self . parse_block_sequence_entry <EOL> return self . process_empty_scalar ( token . end_mark ) <EOL> if not self . check_token ( BlockEndToken ) : <EOL> token = self . peek_token ( ) <EOL> raise ParserError ( "<STR_LIT>" , self . marks [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" % token . id , token . start_mark ) <EOL> token = self . get_token ( ) <EOL> event = SequenceEndEvent ( token . start_mark , token . end_mark ) <EOL> self . state = self . states . pop ( ) <EOL> self . marks . pop ( ) <EOL> return event <EOL> def parse_indentless_sequence_entry ( self ) : <EOL> if self . check_token ( BlockEntryToken ) : <EOL> token = self . get_token ( ) <EOL> if not self . check_token ( BlockEntryToken , <EOL> KeyToken , ValueToken , BlockEndToken ) : <EOL> self . states . append ( self . parse_indentless_sequence_entry ) <EOL> return self . parse_block_node ( ) <EOL> else : <EOL> self . state = self . parse_indentless_sequence_entry <EOL> return self . process_empty_scalar ( token . end_mark ) <EOL> token = self . peek_token ( ) <EOL> event = SequenceEndEvent ( token . start_mark , token . start_mark ) <EOL> self . state = self . states . pop ( ) <EOL> return event <EOL> def parse_block_mapping_first_key ( self ) : <EOL> token = self . get_token ( ) <EOL> self . marks . append ( token . start_mark ) <EOL> return self . parse_block_mapping_key ( ) <EOL> def parse_block_mapping_key ( self ) : <EOL> if self . check_token ( KeyToken ) : <EOL> token = self . get_token ( ) <EOL> if not self . check_token ( KeyToken , ValueToken , BlockEndToken ) : <EOL> self . states . append ( self . parse_block_mapping_value ) <EOL> return self . parse_block_node_or_indentless_sequence ( ) <EOL> else : <EOL> self . state = self . parse_block_mapping_value <EOL> return self . process_empty_scalar ( token . end_mark ) <EOL> if not self . check_token ( BlockEndToken ) : <EOL> token = self . peek_token ( ) <EOL> raise ParserError ( "<STR_LIT>" , self . marks [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" % token . id , token . start_mark ) <EOL> token = self . get_token ( ) <EOL> event = MappingEndEvent ( token . start_mark , token . end_mark ) <EOL> self . state = self . states . pop ( ) <EOL> self . marks . pop ( ) <EOL> return event <EOL> def parse_block_mapping_value ( self ) : <EOL> if self . check_token ( ValueToken ) : <EOL> token = self . get_token ( ) <EOL> if not self . check_token ( KeyToken , ValueToken , BlockEndToken ) : <EOL> self . states . append ( self . parse_block_mapping_key ) <EOL> return self . parse_block_node_or_indentless_sequence ( ) <EOL> else : <EOL> self . state = self . parse_block_mapping_key <EOL> return self . process_empty_scalar ( token . end_mark ) <EOL> else : <EOL> self . state = self . parse_block_mapping_key <EOL> token = self . peek_token ( ) <EOL> return self . process_empty_scalar ( token . start_mark ) <EOL> def parse_flow_sequence_first_entry ( self ) : <EOL> token = self . get_token ( ) <EOL> self . marks . append ( token . start_mark ) <EOL> return self . parse_flow_sequence_entry ( first = True ) <EOL> def parse_flow_sequence_entry ( self , first = False ) : <EOL> if not self . check_token ( FlowSequenceEndToken ) : <EOL> if not first : <EOL> if self . check_token ( FlowEntryToken ) : <EOL> self . get_token ( ) <EOL> else : <EOL> token = self . peek_token ( ) <EOL> raise ParserError ( "<STR_LIT>" , self . marks [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" % token . id , token . start_mark ) <EOL> if self . check_token ( KeyToken ) : <EOL> token = self . peek_token ( ) <EOL> event = MappingStartEvent ( None , None , True , <EOL> token . start_mark , token . end_mark , <EOL> flow_style = True ) <EOL> self . state = self . parse_flow_sequence_entry_mapping_key <EOL> return event <EOL> elif not self . check_token ( FlowSequenceEndToken ) : <EOL> self . states . append ( self . parse_flow_sequence_entry ) <EOL> return self . parse_flow_node ( ) <EOL> token = self . get_token ( ) <EOL> event = SequenceEndEvent ( token . start_mark , token . end_mark ) <EOL> self . state = self . states . pop ( ) <EOL> self . marks . pop ( ) <EOL> return event <EOL> def parse_flow_sequence_entry_mapping_key ( self ) : <EOL> token = self . get_token ( ) <EOL> if not self . check_token ( ValueToken , <EOL> FlowEntryToken , FlowSequenceEndToken ) : <EOL> self . states . append ( self . parse_flow_sequence_entry_mapping_value ) <EOL> return self . parse_flow_node ( ) <EOL> else : <EOL> self . state = self . parse_flow_sequence_entry_mapping_value <EOL> return self . process_empty_scalar ( token . end_mark ) <EOL> def parse_flow_sequence_entry_mapping_value ( self ) : <EOL> if self . check_token ( ValueToken ) : <EOL> token = self . get_token ( ) <EOL> if not self . check_token ( FlowEntryToken , FlowSequenceEndToken ) : <EOL> self . states . append ( self . parse_flow_sequence_entry_mapping_end ) <EOL> return self . parse_flow_node ( ) <EOL> else : <EOL> self . state = self . parse_flow_sequence_entry_mapping_end <EOL> return self . process_empty_scalar ( token . end_mark ) <EOL> else : <EOL> self . state = self . parse_flow_sequence_entry_mapping_end <EOL> token = self . peek_token ( ) <EOL> return self . process_empty_scalar ( token . start_mark ) <EOL> def parse_flow_sequence_entry_mapping_end ( self ) : <EOL> self . state = self . parse_flow_sequence_entry <EOL> token = self . peek_token ( ) <EOL> return MappingEndEvent ( token . start_mark , token . start_mark ) <EOL> def parse_flow_mapping_first_key ( self ) : <EOL> token = self . get_token ( ) <EOL> self . marks . append ( token . start_mark ) <EOL> return self . parse_flow_mapping_key ( first = True ) <EOL> def parse_flow_mapping_key ( self , first = False ) : <EOL> if not self . check_token ( FlowMappingEndToken ) : <EOL> if not first : <EOL> if self . check_token ( FlowEntryToken ) : <EOL> self . get_token ( ) <EOL> else : <EOL> token = self . peek_token ( ) <EOL> raise ParserError ( "<STR_LIT>" , self . marks [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" % token . id , token . start_mark ) <EOL> if self . check_token ( KeyToken ) : <EOL> token = self . get_token ( ) <EOL> if not self . check_token ( ValueToken , <EOL> FlowEntryToken , FlowMappingEndToken ) : <EOL> self . states . append ( self . parse_flow_mapping_value ) <EOL> return self . parse_flow_node ( ) <EOL> else : <EOL> self . state = self . parse_flow_mapping_value <EOL> return self . process_empty_scalar ( token . end_mark ) <EOL> elif not self . check_token ( FlowMappingEndToken ) : <EOL> self . states . append ( self . parse_flow_mapping_empty_value ) <EOL> return self . parse_flow_node ( ) <EOL> token = self . get_token ( ) <EOL> event = MappingEndEvent ( token . start_mark , token . end_mark ) <EOL> self . state = self . states . pop ( ) <EOL> self . marks . pop ( ) <EOL> return event <EOL> def parse_flow_mapping_value ( self ) : <EOL> if self . check_token ( ValueToken ) : <EOL> token = self . get_token ( ) <EOL> if not self . check_token ( FlowEntryToken , FlowMappingEndToken ) : <EOL> self . states . append ( self . parse_flow_mapping_key ) <EOL> return self . parse_flow_node ( ) <EOL> else : <EOL> self . state = self . parse_flow_mapping_key <EOL> return self . process_empty_scalar ( token . end_mark ) <EOL> else : <EOL> self . state = self . parse_flow_mapping_key <EOL> token = self . peek_token ( ) <EOL> return self . process_empty_scalar ( token . start_mark ) <EOL> def parse_flow_mapping_empty_value ( self ) : <EOL> self . state = self . parse_flow_mapping_key <EOL> return self . process_empty_scalar ( self . peek_token ( ) . start_mark ) <EOL> def process_empty_scalar ( self , mark ) : <EOL> return ScalarEvent ( None , None , ( True , False ) , u'<STR_LIT>' , mark , mark ) </s>
