Skip to content

Commit

Permalink
Track which simple_keys are still possible by token_number for effici…
Browse files Browse the repository at this point in the history
…ent lookup in fetch.
  • Loading branch information
cjcullen committed Nov 4, 2019
1 parent 62001ca commit 04b5057
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
24 changes: 13 additions & 11 deletions scannerc.go
Expand Up @@ -637,14 +637,7 @@ func yaml_parser_fetch_more_tokens(parser *yaml_parser_t) bool {
if !yaml_parser_stale_simple_keys(parser) {
return false
}

for i := range parser.simple_keys {
simple_key := &parser.simple_keys[i]
if simple_key.possible && simple_key.token_number == parser.tokens_parsed {
need_more_tokens = true
break
}
}
_, need_more_tokens = parser.simple_keys_possible_by_token_number[parser.tokens_parsed]
}

// We are finished.
Expand Down Expand Up @@ -851,7 +844,6 @@ func yaml_parser_stale_simple_keys(parser *yaml_parser_t) bool {
parser.simple_keys_min_possible_index = i
continue
}

// The specification requires that a simple key
//
// - is limited to a single line,
Expand All @@ -865,6 +857,7 @@ func yaml_parser_stale_simple_keys(parser *yaml_parser_t) bool {
"could not find expected ':'")
}
simple_key.possible = false
delete(parser.simple_keys_possible_by_token_number, simple_key.token_number)
parser.simple_keys_min_possible_index = i + 1
} else {
// Once we find a key that's not stale, the rest will be on the
Expand Down Expand Up @@ -899,6 +892,8 @@ func yaml_parser_save_simple_key(parser *yaml_parser_t) bool {
if !yaml_parser_remove_simple_key(parser) {
return false
}
parser.simple_keys_possible_by_token_number[simple_key.token_number] = struct{}{}

parser.simple_keys[len(parser.simple_keys)-1] = simple_key
}
return true
Expand All @@ -914,9 +909,10 @@ func yaml_parser_remove_simple_key(parser *yaml_parser_t) bool {
"while scanning a simple key", parser.simple_keys[i].mark,
"could not find expected ':'")
}
// Remove the key from the stack.
parser.simple_keys[i].possible = false
delete(parser.simple_keys_possible_by_token_number, parser.simple_keys[i].token_number)
}
// Remove the key from the stack.
parser.simple_keys[i].possible = false
return true
}

Expand All @@ -943,6 +939,9 @@ func yaml_parser_decrease_flow_level(parser *yaml_parser_t) bool {
if parser.flow_level > 0 {
parser.flow_level--
last := len(parser.simple_keys) - 1
if parser.simple_keys[last].possible {
delete(parser.simple_keys_possible_by_token_number, parser.simple_keys[last].token_number)
}
parser.simple_keys = parser.simple_keys[:last]
if parser.simple_keys_min_possible_index > last {
parser.simple_keys_min_possible_index = last
Expand Down Expand Up @@ -1023,6 +1022,8 @@ func yaml_parser_fetch_stream_start(parser *yaml_parser_t) bool {
// Initialize the simple key stack.
parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{})

parser.simple_keys_possible_by_token_number = make(map[int]struct{})

// A simple key is allowed at the beginning of the stream.
parser.simple_key_allowed = true

Expand Down Expand Up @@ -1322,6 +1323,7 @@ func yaml_parser_fetch_value(parser *yaml_parser_t) bool {

// Remove the simple key.
simple_key.possible = false
delete(parser.simple_keys_possible_by_token_number, simple_key.token_number)

// A simple key cannot follow another simple key.
parser.simple_key_allowed = false
Expand Down
7 changes: 4 additions & 3 deletions yamlh.go
Expand Up @@ -577,9 +577,10 @@ type yaml_parser_t struct {
indent int // The current indentation level.
indents []int // The indentation levels stack.

simple_key_allowed bool // May a simple key occur at the current position?
simple_keys []yaml_simple_key_t // The stack of simple keys.
simple_keys_min_possible_index int // Where in the stack should we restart scanning for staleness?
simple_key_allowed bool // May a simple key occur at the current position?
simple_keys []yaml_simple_key_t // The stack of simple keys.
simple_keys_min_possible_index int // Where in the stack should we restart scanning for staleness?
simple_keys_possible_by_token_number map[int]struct{} // Does this token_number represent a possible simple_key?

// Parser stuff

Expand Down

0 comments on commit 04b5057

Please sign in to comment.