//
// Copyright (c) 2011-2019 Canonical Ltd
// Copyright (c) 2006-2010 Kirill Simonov
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package yaml

import (
	
)

// The parser implements the following grammar:
//
// stream               ::= STREAM-START implicit_document? explicit_document* STREAM-END
// implicit_document    ::= block_node DOCUMENT-END*
// explicit_document    ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
// block_node_or_indentless_sequence    ::=
//                          ALIAS
//                          | properties (block_content | indentless_block_sequence)?
//                          | block_content
//                          | indentless_block_sequence
// block_node           ::= ALIAS
//                          | properties block_content?
//                          | block_content
// flow_node            ::= ALIAS
//                          | properties flow_content?
//                          | flow_content
// properties           ::= TAG ANCHOR? | ANCHOR TAG?
// block_content        ::= block_collection | flow_collection | SCALAR
// flow_content         ::= flow_collection | SCALAR
// block_collection     ::= block_sequence | block_mapping
// flow_collection      ::= flow_sequence | flow_mapping
// block_sequence       ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
// indentless_sequence  ::= (BLOCK-ENTRY block_node?)+
// block_mapping        ::= BLOCK-MAPPING_START
//                          ((KEY block_node_or_indentless_sequence?)?
//                          (VALUE block_node_or_indentless_sequence?)?)*
//                          BLOCK-END
// flow_sequence        ::= FLOW-SEQUENCE-START
//                          (flow_sequence_entry FLOW-ENTRY)*
//                          flow_sequence_entry?
//                          FLOW-SEQUENCE-END
// flow_sequence_entry  ::= flow_node | KEY flow_node? (VALUE flow_node?)?
// flow_mapping         ::= FLOW-MAPPING-START
//                          (flow_mapping_entry FLOW-ENTRY)*
//                          flow_mapping_entry?
//                          FLOW-MAPPING-END
// flow_mapping_entry   ::= flow_node | KEY flow_node? (VALUE flow_node?)?

// Peek the next token in the token queue.
func peek_token( *yaml_parser_t) *yaml_token_t {
	if .token_available || yaml_parser_fetch_more_tokens() {
		 := &.tokens[.tokens_head]
		yaml_parser_unfold_comments(, )
		return 
	}
	return nil
}

// yaml_parser_unfold_comments walks through the comments queue and joins all
// comments behind the position of the provided token into the respective
// top-level comment slices in the parser.
func yaml_parser_unfold_comments( *yaml_parser_t,  *yaml_token_t) {
	for .comments_head < len(.comments) && .start_mark.index >= .comments[.comments_head].token_mark.index {
		 := &.comments[.comments_head]
		if len(.head) > 0 {
			if .typ == yaml_BLOCK_END_TOKEN {
				// No heads on ends, so keep comment.head for a follow up token.
				break
			}
			if len(.head_comment) > 0 {
				.head_comment = append(.head_comment, '\n')
			}
			.head_comment = append(.head_comment, .head...)
		}
		if len(.foot) > 0 {
			if len(.foot_comment) > 0 {
				.foot_comment = append(.foot_comment, '\n')
			}
			.foot_comment = append(.foot_comment, .foot...)
		}
		if len(.line) > 0 {
			if len(.line_comment) > 0 {
				.line_comment = append(.line_comment, '\n')
			}
			.line_comment = append(.line_comment, .line...)
		}
		* = yaml_comment_t{}
		.comments_head++
	}
}

// Remove the next token from the queue (must be called after peek_token).
func skip_token( *yaml_parser_t) {
	.token_available = false
	.tokens_parsed++
	.stream_end_produced = .tokens[.tokens_head].typ == yaml_STREAM_END_TOKEN
	.tokens_head++
}

// Get the next event.
func yaml_parser_parse( *yaml_parser_t,  *yaml_event_t) bool {
	// Erase the event object.
	* = yaml_event_t{}

	// No events after the end of the stream or error.
	if .stream_end_produced || .error != yaml_NO_ERROR || .state == yaml_PARSE_END_STATE {
		return true
	}

	// Generate the next event.
	return yaml_parser_state_machine(, )
}

// Set parser error.
func yaml_parser_set_parser_error( *yaml_parser_t,  string,  yaml_mark_t) bool {
	.error = yaml_PARSER_ERROR
	.problem = 
	.problem_mark = 
	return false
}

func yaml_parser_set_parser_error_context( *yaml_parser_t,  string,  yaml_mark_t,  string,  yaml_mark_t) bool {
	.error = yaml_PARSER_ERROR
	.context = 
	.context_mark = 
	.problem = 
	.problem_mark = 
	return false
}

// State dispatcher.
func yaml_parser_state_machine( *yaml_parser_t,  *yaml_event_t) bool {
	//trace("yaml_parser_state_machine", "state:", parser.state.String())

	switch .state {
	case yaml_PARSE_STREAM_START_STATE:
		return yaml_parser_parse_stream_start(, )

	case yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE:
		return yaml_parser_parse_document_start(, , true)

	case yaml_PARSE_DOCUMENT_START_STATE:
		return yaml_parser_parse_document_start(, , false)

	case yaml_PARSE_DOCUMENT_CONTENT_STATE:
		return yaml_parser_parse_document_content(, )

	case yaml_PARSE_DOCUMENT_END_STATE:
		return yaml_parser_parse_document_end(, )

	case yaml_PARSE_BLOCK_NODE_STATE:
		return yaml_parser_parse_node(, , true, false)

	case yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE:
		return yaml_parser_parse_node(, , true, true)

	case yaml_PARSE_FLOW_NODE_STATE:
		return yaml_parser_parse_node(, , false, false)

	case yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE:
		return yaml_parser_parse_block_sequence_entry(, , true)

	case yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE:
		return yaml_parser_parse_block_sequence_entry(, , false)

	case yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE:
		return yaml_parser_parse_indentless_sequence_entry(, )

	case yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE:
		return yaml_parser_parse_block_mapping_key(, , true)

	case yaml_PARSE_BLOCK_MAPPING_KEY_STATE:
		return yaml_parser_parse_block_mapping_key(, , false)

	case yaml_PARSE_BLOCK_MAPPING_VALUE_STATE:
		return yaml_parser_parse_block_mapping_value(, )

	case yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE:
		return yaml_parser_parse_flow_sequence_entry(, , true)

	case yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE:
		return yaml_parser_parse_flow_sequence_entry(, , false)

	case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE:
		return yaml_parser_parse_flow_sequence_entry_mapping_key(, )

	case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE:
		return yaml_parser_parse_flow_sequence_entry_mapping_value(, )

	case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE:
		return yaml_parser_parse_flow_sequence_entry_mapping_end(, )

	case yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE:
		return yaml_parser_parse_flow_mapping_key(, , true)

	case yaml_PARSE_FLOW_MAPPING_KEY_STATE:
		return yaml_parser_parse_flow_mapping_key(, , false)

	case yaml_PARSE_FLOW_MAPPING_VALUE_STATE:
		return yaml_parser_parse_flow_mapping_value(, , false)

	case yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE:
		return yaml_parser_parse_flow_mapping_value(, , true)

	default:
		panic("invalid parser state")
	}
}

// Parse the production:
// stream   ::= STREAM-START implicit_document? explicit_document* STREAM-END
//              ************
func yaml_parser_parse_stream_start( *yaml_parser_t,  *yaml_event_t) bool {
	 := peek_token()
	if  == nil {
		return false
	}
	if .typ != yaml_STREAM_START_TOKEN {
		return yaml_parser_set_parser_error(, "did not find expected <stream-start>", .start_mark)
	}
	.state = yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE
	* = yaml_event_t{
		typ:        yaml_STREAM_START_EVENT,
		start_mark: .start_mark,
		end_mark:   .end_mark,
		encoding:   .encoding,
	}
	skip_token()
	return true
}

// Parse the productions:
// implicit_document    ::= block_node DOCUMENT-END*
//                          *
// explicit_document    ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
//                          *************************
func yaml_parser_parse_document_start( *yaml_parser_t,  *yaml_event_t,  bool) bool {

	 := peek_token()
	if  == nil {
		return false
	}

	// Parse extra document end indicators.
	if ! {
		for .typ == yaml_DOCUMENT_END_TOKEN {
			skip_token()
			 = peek_token()
			if  == nil {
				return false
			}
		}
	}

	if  && .typ != yaml_VERSION_DIRECTIVE_TOKEN &&
		.typ != yaml_TAG_DIRECTIVE_TOKEN &&
		.typ != yaml_DOCUMENT_START_TOKEN &&
		.typ != yaml_STREAM_END_TOKEN {
		// Parse an implicit document.
		if !yaml_parser_process_directives(, nil, nil) {
			return false
		}
		.states = append(.states, yaml_PARSE_DOCUMENT_END_STATE)
		.state = yaml_PARSE_BLOCK_NODE_STATE

		var  []byte
		if len(.head_comment) > 0 {
			// [Go] Scan the header comment backwards, and if an empty line is found, break
			//      the header so the part before the last empty line goes into the
			//      document header, while the bottom of it goes into a follow up event.
			for  := len(.head_comment) - 1;  > 0; -- {
				if .head_comment[] == '\n' {
					if  == len(.head_comment)-1 {
						 = .head_comment[:]
						.head_comment = .head_comment[+1:]
						break
					} else if .head_comment[-1] == '\n' {
						 = .head_comment[:-1]
						.head_comment = .head_comment[+1:]
						break
					}
				}
			}
		}

		* = yaml_event_t{
			typ:        yaml_DOCUMENT_START_EVENT,
			start_mark: .start_mark,
			end_mark:   .end_mark,

			head_comment: ,
		}

	} else if .typ != yaml_STREAM_END_TOKEN {
		// Parse an explicit document.
		var  *yaml_version_directive_t
		var  []yaml_tag_directive_t
		 := .start_mark
		if !yaml_parser_process_directives(, &, &) {
			return false
		}
		 = peek_token()
		if  == nil {
			return false
		}
		if .typ != yaml_DOCUMENT_START_TOKEN {
			yaml_parser_set_parser_error(,
				"did not find expected <document start>", .start_mark)
			return false
		}
		.states = append(.states, yaml_PARSE_DOCUMENT_END_STATE)
		.state = yaml_PARSE_DOCUMENT_CONTENT_STATE
		 := .end_mark

		* = yaml_event_t{
			typ:               yaml_DOCUMENT_START_EVENT,
			start_mark:        ,
			end_mark:          ,
			version_directive: ,
			tag_directives:    ,
			implicit:          false,
		}
		skip_token()

	} else {
		// Parse the stream end.
		.state = yaml_PARSE_END_STATE
		* = yaml_event_t{
			typ:        yaml_STREAM_END_EVENT,
			start_mark: .start_mark,
			end_mark:   .end_mark,
		}
		skip_token()
	}

	return true
}

// Parse the productions:
// explicit_document    ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
//                                                    ***********
//
func yaml_parser_parse_document_content( *yaml_parser_t,  *yaml_event_t) bool {
	 := peek_token()
	if  == nil {
		return false
	}

	if .typ == yaml_VERSION_DIRECTIVE_TOKEN ||
		.typ == yaml_TAG_DIRECTIVE_TOKEN ||
		.typ == yaml_DOCUMENT_START_TOKEN ||
		.typ == yaml_DOCUMENT_END_TOKEN ||
		.typ == yaml_STREAM_END_TOKEN {
		.state = .states[len(.states)-1]
		.states = .states[:len(.states)-1]
		return yaml_parser_process_empty_scalar(, ,
			.start_mark)
	}
	return yaml_parser_parse_node(, , true, false)
}

// Parse the productions:
// implicit_document    ::= block_node DOCUMENT-END*
//                                     *************
// explicit_document    ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
//
func yaml_parser_parse_document_end( *yaml_parser_t,  *yaml_event_t) bool {
	 := peek_token()
	if  == nil {
		return false
	}

	 := .start_mark
	 := .start_mark

	 := true
	if .typ == yaml_DOCUMENT_END_TOKEN {
		 = .end_mark
		skip_token()
		 = false
	}

	.tag_directives = .tag_directives[:0]

	.state = yaml_PARSE_DOCUMENT_START_STATE
	* = yaml_event_t{
		typ:        yaml_DOCUMENT_END_EVENT,
		start_mark: ,
		end_mark:   ,
		implicit:   ,
	}
	yaml_parser_set_event_comments(, )
	if len(.head_comment) > 0 && len(.foot_comment) == 0 {
		.foot_comment = .head_comment
		.head_comment = nil
	}
	return true
}

func yaml_parser_set_event_comments( *yaml_parser_t,  *yaml_event_t) {
	.head_comment = .head_comment
	.line_comment = .line_comment
	.foot_comment = .foot_comment
	.head_comment = nil
	.line_comment = nil
	.foot_comment = nil
	.tail_comment = nil
	.stem_comment = nil
}

// Parse the productions:
// block_node_or_indentless_sequence    ::=
//                          ALIAS
//                          *****
//                          | properties (block_content | indentless_block_sequence)?
//                            **********  *
//                          | block_content | indentless_block_sequence
//                            *
// block_node           ::= ALIAS
//                          *****
//                          | properties block_content?
//                            ********** *
//                          | block_content
//                            *
// flow_node            ::= ALIAS
//                          *****
//                          | properties flow_content?
//                            ********** *
//                          | flow_content
//                            *
// properties           ::= TAG ANCHOR? | ANCHOR TAG?
//                          *************************
// block_content        ::= block_collection | flow_collection | SCALAR
//                                                               ******
// flow_content         ::= flow_collection | SCALAR
//                                            ******
func yaml_parser_parse_node( *yaml_parser_t,  *yaml_event_t, ,  bool) bool {
	//defer trace("yaml_parser_parse_node", "block:", block, "indentless_sequence:", indentless_sequence)()

	 := peek_token()
	if  == nil {
		return false
	}

	if .typ == yaml_ALIAS_TOKEN {
		.state = .states[len(.states)-1]
		.states = .states[:len(.states)-1]
		* = yaml_event_t{
			typ:        yaml_ALIAS_EVENT,
			start_mark: .start_mark,
			end_mark:   .end_mark,
			anchor:     .value,
		}
		yaml_parser_set_event_comments(, )
		skip_token()
		return true
	}

	 := .start_mark
	 := .start_mark

	var  bool
	var , ,  []byte
	var  yaml_mark_t
	if .typ == yaml_ANCHOR_TOKEN {
		 = .value
		 = .start_mark
		 = .end_mark
		skip_token()
		 = peek_token()
		if  == nil {
			return false
		}
		if .typ == yaml_TAG_TOKEN {
			 = true
			 = .value
			 = .suffix
			 = .start_mark
			 = .end_mark
			skip_token()
			 = peek_token()
			if  == nil {
				return false
			}
		}
	} else if .typ == yaml_TAG_TOKEN {
		 = true
		 = .value
		 = .suffix
		 = .start_mark
		 = .start_mark
		 = .end_mark
		skip_token()
		 = peek_token()
		if  == nil {
			return false
		}
		if .typ == yaml_ANCHOR_TOKEN {
			 = .value
			 = .end_mark
			skip_token()
			 = peek_token()
			if  == nil {
				return false
			}
		}
	}

	var  []byte
	if  {
		if len() == 0 {
			 = 
			 = nil
		} else {
			for  := range .tag_directives {
				if bytes.Equal(.tag_directives[].handle, ) {
					 = append([]byte(nil), .tag_directives[].prefix...)
					 = append(, ...)
					break
				}
			}
			if len() == 0 {
				yaml_parser_set_parser_error_context(,
					"while parsing a node", ,
					"found undefined tag handle", )
				return false
			}
		}
	}

	 := len() == 0
	if  && .typ == yaml_BLOCK_ENTRY_TOKEN {
		 = .end_mark
		.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE
		* = yaml_event_t{
			typ:        yaml_SEQUENCE_START_EVENT,
			start_mark: ,
			end_mark:   ,
			anchor:     ,
			tag:        ,
			implicit:   ,
			style:      yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE),
		}
		return true
	}
	if .typ == yaml_SCALAR_TOKEN {
		var ,  bool
		 = .end_mark
		if (len() == 0 && .style == yaml_PLAIN_SCALAR_STYLE) || (len() == 1 && [0] == '!') {
			 = true
		} else if len() == 0 {
			 = true
		}
		.state = .states[len(.states)-1]
		.states = .states[:len(.states)-1]

		* = yaml_event_t{
			typ:             yaml_SCALAR_EVENT,
			start_mark:      ,
			end_mark:        ,
			anchor:          ,
			tag:             ,
			value:           .value,
			implicit:        ,
			quoted_implicit: ,
			style:           yaml_style_t(.style),
		}
		yaml_parser_set_event_comments(, )
		skip_token()
		return true
	}
	if .typ == yaml_FLOW_SEQUENCE_START_TOKEN {
		// [Go] Some of the events below can be merged as they differ only on style.
		 = .end_mark
		.state = yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE
		* = yaml_event_t{
			typ:        yaml_SEQUENCE_START_EVENT,
			start_mark: ,
			end_mark:   ,
			anchor:     ,
			tag:        ,
			implicit:   ,
			style:      yaml_style_t(yaml_FLOW_SEQUENCE_STYLE),
		}
		yaml_parser_set_event_comments(, )
		return true
	}
	if .typ == yaml_FLOW_MAPPING_START_TOKEN {
		 = .end_mark
		.state = yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE
		* = yaml_event_t{
			typ:        yaml_MAPPING_START_EVENT,
			start_mark: ,
			end_mark:   ,
			anchor:     ,
			tag:        ,
			implicit:   ,
			style:      yaml_style_t(yaml_FLOW_MAPPING_STYLE),
		}
		yaml_parser_set_event_comments(, )
		return true
	}
	if  && .typ == yaml_BLOCK_SEQUENCE_START_TOKEN {
		 = .end_mark
		.state = yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE
		* = yaml_event_t{
			typ:        yaml_SEQUENCE_START_EVENT,
			start_mark: ,
			end_mark:   ,
			anchor:     ,
			tag:        ,
			implicit:   ,
			style:      yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE),
		}
		if .stem_comment != nil {
			.head_comment = .stem_comment
			.stem_comment = nil
		}
		return true
	}
	if  && .typ == yaml_BLOCK_MAPPING_START_TOKEN {
		 = .end_mark
		.state = yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE
		* = yaml_event_t{
			typ:        yaml_MAPPING_START_EVENT,
			start_mark: ,
			end_mark:   ,
			anchor:     ,
			tag:        ,
			implicit:   ,
			style:      yaml_style_t(yaml_BLOCK_MAPPING_STYLE),
		}
		if .stem_comment != nil {
			.head_comment = .stem_comment
			.stem_comment = nil
		}
		return true
	}
	if len() > 0 || len() > 0 {
		.state = .states[len(.states)-1]
		.states = .states[:len(.states)-1]

		* = yaml_event_t{
			typ:             yaml_SCALAR_EVENT,
			start_mark:      ,
			end_mark:        ,
			anchor:          ,
			tag:             ,
			implicit:        ,
			quoted_implicit: false,
			style:           yaml_style_t(yaml_PLAIN_SCALAR_STYLE),
		}
		return true
	}

	 := "while parsing a flow node"
	if  {
		 = "while parsing a block node"
	}
	yaml_parser_set_parser_error_context(, , ,
		"did not find expected node content", .start_mark)
	return false
}

// Parse the productions:
// block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
//                    ********************  *********** *             *********
//
func yaml_parser_parse_block_sequence_entry( *yaml_parser_t,  *yaml_event_t,  bool) bool {
	if  {
		 := peek_token()
		if  == nil {
			return false
		}
		.marks = append(.marks, .start_mark)
		skip_token()
	}

	 := peek_token()
	if  == nil {
		return false
	}

	if .typ == yaml_BLOCK_ENTRY_TOKEN {
		 := .end_mark
		 := len(.head_comment)
		skip_token()
		yaml_parser_split_stem_comment(, )
		 = peek_token()
		if  == nil {
			return false
		}
		if .typ != yaml_BLOCK_ENTRY_TOKEN && .typ != yaml_BLOCK_END_TOKEN {
			.states = append(.states, yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE)
			return yaml_parser_parse_node(, , true, false)
		} else {
			.state = yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE
			return yaml_parser_process_empty_scalar(, , )
		}
	}
	if .typ == yaml_BLOCK_END_TOKEN {
		.state = .states[len(.states)-1]
		.states = .states[:len(.states)-1]
		.marks = .marks[:len(.marks)-1]

		* = yaml_event_t{
			typ:        yaml_SEQUENCE_END_EVENT,
			start_mark: .start_mark,
			end_mark:   .end_mark,
		}

		skip_token()
		return true
	}

	 := .marks[len(.marks)-1]
	.marks = .marks[:len(.marks)-1]
	return yaml_parser_set_parser_error_context(,
		"while parsing a block collection", ,
		"did not find expected '-' indicator", .start_mark)
}

// Parse the productions:
// indentless_sequence  ::= (BLOCK-ENTRY block_node?)+
//                           *********** *
func yaml_parser_parse_indentless_sequence_entry( *yaml_parser_t,  *yaml_event_t) bool {
	 := peek_token()
	if  == nil {
		return false
	}

	if .typ == yaml_BLOCK_ENTRY_TOKEN {
		 := .end_mark
		 := len(.head_comment)
		skip_token()
		yaml_parser_split_stem_comment(, )
		 = peek_token()
		if  == nil {
			return false
		}
		if .typ != yaml_BLOCK_ENTRY_TOKEN &&
			.typ != yaml_KEY_TOKEN &&
			.typ != yaml_VALUE_TOKEN &&
			.typ != yaml_BLOCK_END_TOKEN {
			.states = append(.states, yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE)
			return yaml_parser_parse_node(, , true, false)
		}
		.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE
		return yaml_parser_process_empty_scalar(, , )
	}
	.state = .states[len(.states)-1]
	.states = .states[:len(.states)-1]

	* = yaml_event_t{
		typ:        yaml_SEQUENCE_END_EVENT,
		start_mark: .start_mark,
		end_mark:   .start_mark, // [Go] Shouldn't this be token.end_mark?
	}
	return true
}

// Split stem comment from head comment.
//
// When a sequence or map is found under a sequence entry, the former head comment
// is assigned to the underlying sequence or map as a whole, not the individual
// sequence or map entry as would be expected otherwise. To handle this case the
// previous head comment is moved aside as the stem comment.
func yaml_parser_split_stem_comment( *yaml_parser_t,  int) {
	if  == 0 {
		return
	}

	 := peek_token()
	if  == nil || .typ != yaml_BLOCK_SEQUENCE_START_TOKEN && .typ != yaml_BLOCK_MAPPING_START_TOKEN {
		return
	}

	.stem_comment = .head_comment[:]
	if len(.head_comment) ==  {
		.head_comment = nil
	} else {
		// Copy suffix to prevent very strange bugs if someone ever appends
		// further bytes to the prefix in the stem_comment slice above.
		.head_comment = append([]byte(nil), .head_comment[+1:]...)
	}
}

// Parse the productions:
// block_mapping        ::= BLOCK-MAPPING_START
//                          *******************
//                          ((KEY block_node_or_indentless_sequence?)?
//                            *** *
//                          (VALUE block_node_or_indentless_sequence?)?)*
//
//                          BLOCK-END
//                          *********
//
func yaml_parser_parse_block_mapping_key( *yaml_parser_t,  *yaml_event_t,  bool) bool {
	if  {
		 := peek_token()
		if  == nil {
			return false
		}
		.marks = append(.marks, .start_mark)
		skip_token()
	}

	 := peek_token()
	if  == nil {
		return false
	}

	// [Go] A tail comment was left from the prior mapping value processed. Emit an event
	//      as it needs to be processed with that value and not the following key.
	if len(.tail_comment) > 0 {
		* = yaml_event_t{
			typ:          yaml_TAIL_COMMENT_EVENT,
			start_mark:   .start_mark,
			end_mark:     .end_mark,
			foot_comment: .tail_comment,
		}
		.tail_comment = nil
		return true
	}

	if .typ == yaml_KEY_TOKEN {
		 := .end_mark
		skip_token()
		 = peek_token()
		if  == nil {
			return false
		}
		if .typ != yaml_KEY_TOKEN &&
			.typ != yaml_VALUE_TOKEN &&
			.typ != yaml_BLOCK_END_TOKEN {
			.states = append(.states, yaml_PARSE_BLOCK_MAPPING_VALUE_STATE)
			return yaml_parser_parse_node(, , true, true)
		} else {
			.state = yaml_PARSE_BLOCK_MAPPING_VALUE_STATE
			return yaml_parser_process_empty_scalar(, , )
		}
	} else if .typ == yaml_BLOCK_END_TOKEN {
		.state = .states[len(.states)-1]
		.states = .states[:len(.states)-1]
		.marks = .marks[:len(.marks)-1]
		* = yaml_event_t{
			typ:        yaml_MAPPING_END_EVENT,
			start_mark: .start_mark,
			end_mark:   .end_mark,
		}
		yaml_parser_set_event_comments(, )
		skip_token()
		return true
	}

	 := .marks[len(.marks)-1]
	.marks = .marks[:len(.marks)-1]
	return yaml_parser_set_parser_error_context(,
		"while parsing a block mapping", ,
		"did not find expected key", .start_mark)
}

// Parse the productions:
// block_mapping        ::= BLOCK-MAPPING_START
//
//                          ((KEY block_node_or_indentless_sequence?)?
//
//                          (VALUE block_node_or_indentless_sequence?)?)*
//                           ***** *
//                          BLOCK-END
//
//
func yaml_parser_parse_block_mapping_value( *yaml_parser_t,  *yaml_event_t) bool {
	 := peek_token()
	if  == nil {
		return false
	}
	if .typ == yaml_VALUE_TOKEN {
		 := .end_mark
		skip_token()
		 = peek_token()
		if  == nil {
			return false
		}
		if .typ != yaml_KEY_TOKEN &&
			.typ != yaml_VALUE_TOKEN &&
			.typ != yaml_BLOCK_END_TOKEN {
			.states = append(.states, yaml_PARSE_BLOCK_MAPPING_KEY_STATE)
			return yaml_parser_parse_node(, , true, true)
		}
		.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE
		return yaml_parser_process_empty_scalar(, , )
	}
	.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE
	return yaml_parser_process_empty_scalar(, , .start_mark)
}

// Parse the productions:
// flow_sequence        ::= FLOW-SEQUENCE-START
//                          *******************
//                          (flow_sequence_entry FLOW-ENTRY)*
//                           *                   **********
//                          flow_sequence_entry?
//                          *
//                          FLOW-SEQUENCE-END
//                          *****************
// flow_sequence_entry  ::= flow_node | KEY flow_node? (VALUE flow_node?)?
//                          *
//
func yaml_parser_parse_flow_sequence_entry( *yaml_parser_t,  *yaml_event_t,  bool) bool {
	if  {
		 := peek_token()
		if  == nil {
			return false
		}
		.marks = append(.marks, .start_mark)
		skip_token()
	}
	 := peek_token()
	if  == nil {
		return false
	}
	if .typ != yaml_FLOW_SEQUENCE_END_TOKEN {
		if ! {
			if .typ == yaml_FLOW_ENTRY_TOKEN {
				skip_token()
				 = peek_token()
				if  == nil {
					return false
				}
			} else {
				 := .marks[len(.marks)-1]
				.marks = .marks[:len(.marks)-1]
				return yaml_parser_set_parser_error_context(,
					"while parsing a flow sequence", ,
					"did not find expected ',' or ']'", .start_mark)
			}
		}

		if .typ == yaml_KEY_TOKEN {
			.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE
			* = yaml_event_t{
				typ:        yaml_MAPPING_START_EVENT,
				start_mark: .start_mark,
				end_mark:   .end_mark,
				implicit:   true,
				style:      yaml_style_t(yaml_FLOW_MAPPING_STYLE),
			}
			skip_token()
			return true
		} else if .typ != yaml_FLOW_SEQUENCE_END_TOKEN {
			.states = append(.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE)
			return yaml_parser_parse_node(, , false, false)
		}
	}

	.state = .states[len(.states)-1]
	.states = .states[:len(.states)-1]
	.marks = .marks[:len(.marks)-1]

	* = yaml_event_t{
		typ:        yaml_SEQUENCE_END_EVENT,
		start_mark: .start_mark,
		end_mark:   .end_mark,
	}
	yaml_parser_set_event_comments(, )

	skip_token()
	return true
}

//
// Parse the productions:
// flow_sequence_entry  ::= flow_node | KEY flow_node? (VALUE flow_node?)?
//                                      *** *
//
func yaml_parser_parse_flow_sequence_entry_mapping_key( *yaml_parser_t,  *yaml_event_t) bool {
	 := peek_token()
	if  == nil {
		return false
	}
	if .typ != yaml_VALUE_TOKEN &&
		.typ != yaml_FLOW_ENTRY_TOKEN &&
		.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
		.states = append(.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE)
		return yaml_parser_parse_node(, , false, false)
	}
	 := .end_mark
	skip_token()
	.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE
	return yaml_parser_process_empty_scalar(, , )
}

// Parse the productions:
// flow_sequence_entry  ::= flow_node | KEY flow_node? (VALUE flow_node?)?
//                                                      ***** *
//
func yaml_parser_parse_flow_sequence_entry_mapping_value( *yaml_parser_t,  *yaml_event_t) bool {
	 := peek_token()
	if  == nil {
		return false
	}
	if .typ == yaml_VALUE_TOKEN {
		skip_token()
		 := peek_token()
		if  == nil {
			return false
		}
		if .typ != yaml_FLOW_ENTRY_TOKEN && .typ != yaml_FLOW_SEQUENCE_END_TOKEN {
			.states = append(.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE)
			return yaml_parser_parse_node(, , false, false)
		}
	}
	.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE
	return yaml_parser_process_empty_scalar(, , .start_mark)
}

// Parse the productions:
// flow_sequence_entry  ::= flow_node | KEY flow_node? (VALUE flow_node?)?
//                                                                      *
//
func yaml_parser_parse_flow_sequence_entry_mapping_end( *yaml_parser_t,  *yaml_event_t) bool {
	 := peek_token()
	if  == nil {
		return false
	}
	.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE
	* = yaml_event_t{
		typ:        yaml_MAPPING_END_EVENT,
		start_mark: .start_mark,
		end_mark:   .start_mark, // [Go] Shouldn't this be end_mark?
	}
	return true
}

// Parse the productions:
// flow_mapping         ::= FLOW-MAPPING-START
//                          ******************
//                          (flow_mapping_entry FLOW-ENTRY)*
//                           *                  **********
//                          flow_mapping_entry?
//                          ******************
//                          FLOW-MAPPING-END
//                          ****************
// flow_mapping_entry   ::= flow_node | KEY flow_node? (VALUE flow_node?)?
//                          *           *** *
//
func yaml_parser_parse_flow_mapping_key( *yaml_parser_t,  *yaml_event_t,  bool) bool {
	if  {
		 := peek_token()
		.marks = append(.marks, .start_mark)
		skip_token()
	}

	 := peek_token()
	if  == nil {
		return false
	}

	if .typ != yaml_FLOW_MAPPING_END_TOKEN {
		if ! {
			if .typ == yaml_FLOW_ENTRY_TOKEN {
				skip_token()
				 = peek_token()
				if  == nil {
					return false
				}
			} else {
				 := .marks[len(.marks)-1]
				.marks = .marks[:len(.marks)-1]
				return yaml_parser_set_parser_error_context(,
					"while parsing a flow mapping", ,
					"did not find expected ',' or '}'", .start_mark)
			}
		}

		if .typ == yaml_KEY_TOKEN {
			skip_token()
			 = peek_token()
			if  == nil {
				return false
			}
			if .typ != yaml_VALUE_TOKEN &&
				.typ != yaml_FLOW_ENTRY_TOKEN &&
				.typ != yaml_FLOW_MAPPING_END_TOKEN {
				.states = append(.states, yaml_PARSE_FLOW_MAPPING_VALUE_STATE)
				return yaml_parser_parse_node(, , false, false)
			} else {
				.state = yaml_PARSE_FLOW_MAPPING_VALUE_STATE
				return yaml_parser_process_empty_scalar(, , .start_mark)
			}
		} else if .typ != yaml_FLOW_MAPPING_END_TOKEN {
			.states = append(.states, yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE)
			return yaml_parser_parse_node(, , false, false)
		}
	}

	.state = .states[len(.states)-1]
	.states = .states[:len(.states)-1]
	.marks = .marks[:len(.marks)-1]
	* = yaml_event_t{
		typ:        yaml_MAPPING_END_EVENT,
		start_mark: .start_mark,
		end_mark:   .end_mark,
	}
	yaml_parser_set_event_comments(, )
	skip_token()
	return true
}

// Parse the productions:
// flow_mapping_entry   ::= flow_node | KEY flow_node? (VALUE flow_node?)?
//                                   *                  ***** *
//
func yaml_parser_parse_flow_mapping_value( *yaml_parser_t,  *yaml_event_t,  bool) bool {
	 := peek_token()
	if  == nil {
		return false
	}
	if  {
		.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE
		return yaml_parser_process_empty_scalar(, , .start_mark)
	}
	if .typ == yaml_VALUE_TOKEN {
		skip_token()
		 = peek_token()
		if  == nil {
			return false
		}
		if .typ != yaml_FLOW_ENTRY_TOKEN && .typ != yaml_FLOW_MAPPING_END_TOKEN {
			.states = append(.states, yaml_PARSE_FLOW_MAPPING_KEY_STATE)
			return yaml_parser_parse_node(, , false, false)
		}
	}
	.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE
	return yaml_parser_process_empty_scalar(, , .start_mark)
}

// Generate an empty scalar event.
func yaml_parser_process_empty_scalar( *yaml_parser_t,  *yaml_event_t,  yaml_mark_t) bool {
	* = yaml_event_t{
		typ:        yaml_SCALAR_EVENT,
		start_mark: ,
		end_mark:   ,
		value:      nil, // Empty
		implicit:   true,
		style:      yaml_style_t(yaml_PLAIN_SCALAR_STYLE),
	}
	return true
}

var default_tag_directives = []yaml_tag_directive_t{
	{[]byte("!"), []byte("!")},
	{[]byte("!!"), []byte("tag:yaml.org,2002:")},
}

// Parse directives.
func yaml_parser_process_directives( *yaml_parser_t,
	 **yaml_version_directive_t,
	 *[]yaml_tag_directive_t) bool {

	var  *yaml_version_directive_t
	var  []yaml_tag_directive_t

	 := peek_token()
	if  == nil {
		return false
	}

	for .typ == yaml_VERSION_DIRECTIVE_TOKEN || .typ == yaml_TAG_DIRECTIVE_TOKEN {
		if .typ == yaml_VERSION_DIRECTIVE_TOKEN {
			if  != nil {
				yaml_parser_set_parser_error(,
					"found duplicate %YAML directive", .start_mark)
				return false
			}
			if .major != 1 || .minor != 1 {
				yaml_parser_set_parser_error(,
					"found incompatible YAML document", .start_mark)
				return false
			}
			 = &yaml_version_directive_t{
				major: .major,
				minor: .minor,
			}
		} else if .typ == yaml_TAG_DIRECTIVE_TOKEN {
			 := yaml_tag_directive_t{
				handle: .value,
				prefix: .prefix,
			}
			if !yaml_parser_append_tag_directive(, , false, .start_mark) {
				return false
			}
			 = append(, )
		}

		skip_token()
		 = peek_token()
		if  == nil {
			return false
		}
	}

	for  := range default_tag_directives {
		if !yaml_parser_append_tag_directive(, default_tag_directives[], true, .start_mark) {
			return false
		}
	}

	if  != nil {
		* = 
	}
	if  != nil {
		* = 
	}
	return true
}

// Append a tag directive to the directives stack.
func yaml_parser_append_tag_directive( *yaml_parser_t,  yaml_tag_directive_t,  bool,  yaml_mark_t) bool {
	for  := range .tag_directives {
		if bytes.Equal(.handle, .tag_directives[].handle) {
			if  {
				return true
			}
			return yaml_parser_set_parser_error(, "found duplicate %TAG directive", )
		}
	}

	// [Go] I suspect the copy is unnecessary. This was likely done
	// because there was no way to track ownership of the data.
	 := yaml_tag_directive_t{
		handle: make([]byte, len(.handle)),
		prefix: make([]byte, len(.prefix)),
	}
	copy(.handle, .handle)
	copy(.prefix, .prefix)
	.tag_directives = append(.tag_directives, )
	return true
}