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

View File

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