Include tag stack size when serializing external scanner

This commit is contained in:
Max Brunsfeld 2018-06-18 10:04:46 -07:00
parent bd235b794b
commit 331b0a4b00
2 changed files with 24 additions and 20 deletions

View File

@ -26,41 +26,44 @@ struct Scanner {
unsigned serialize(char *buffer) { unsigned serialize(char *buffer) {
unsigned i = 0; unsigned i = 0;
unsigned n = tags.size();
for (unsigned j = 0, n = tags.size(); j < n; j++) { buffer[i++] = n;
for (unsigned j = 0; j < n; j++) {
Tag &tag = tags[j]; Tag &tag = tags[j];
buffer[i] = static_cast<char>(tag.type); buffer[i++] = static_cast<char>(tag.type);
i++;
if (tag.type == CUSTOM) { if (tag.type == CUSTOM) {
buffer[i++] = tag.custom_tag_name.size(); unsigned name_length = tag.custom_tag_name.size();
tag.custom_tag_name.copy(&buffer[i], tag.custom_tag_name.size()); buffer[i++] = name_length;
i += tag.custom_tag_name.size(); tag.custom_tag_name.copy(&buffer[i], name_length);
i += name_length;
} }
} }
return i; return i;
} }
void deserialize(const char *buffer, unsigned length) { void deserialize(const char *buffer, unsigned length) {
tags.clear(); tags.clear();
if (length > 0) {
unsigned i = 0; unsigned i = 0;
while (i < length) { unsigned n = buffer[i++];
Tag tag(static_cast<TagType>(buffer[i]), ""); tags.resize(n);
i++; for (unsigned j = 0; j < n; j++) {
Tag &tag = tags[j];
tag.type = static_cast<TagType>(buffer[i++]);
if (tag.type == CUSTOM) { if (tag.type == CUSTOM) {
unsigned length = buffer[i++]; unsigned name_length = buffer[i++];
tag.custom_tag_name.assign(&buffer[i], &buffer[i + length]); tag.custom_tag_name.assign(&buffer[i], &buffer[i + name_length]);
i += length; i += name_length;
}
} }
tags.push_back(tag);
} }
} }
string scan_tag_name(TSLexer *lexer) { string scan_tag_name(TSLexer *lexer) {
string tag_name; string tag_name;
while (iswalnum(lexer->lookahead) || lexer->lookahead == '-' || lexer->lookahead == ':') { while (iswalnum(lexer->lookahead) ||
lexer->lookahead == '-' ||
lexer->lookahead == ':') {
tag_name += towupper(lexer->lookahead); tag_name += towupper(lexer->lookahead);
lexer->advance(lexer, false); lexer->advance(lexer, false);
} }

View File

@ -310,6 +310,7 @@ struct Tag {
TagType type; TagType type;
string custom_tag_name; string custom_tag_name;
Tag() : type(DIV) {}
Tag(TagType type, const string &name) : type(type), custom_tag_name(name) {} Tag(TagType type, const string &name) : type(type), custom_tag_name(name) {}
bool operator==(const Tag &other) const { bool operator==(const Tag &other) const {