diff --git a/yamal.hpp b/yamal.hpp index 14ca263..bcb2964 100644 --- a/yamal.hpp +++ b/yamal.hpp @@ -272,6 +272,9 @@ public: deserialize(-1); } + inline static std::string current_comment = ""; + inline static bool current_blank_line = false; + void deserialize(int indent) { if (tokenizer::error()) return; int current_indent = -2; @@ -279,31 +282,45 @@ public: while(tokenizer::type != tokenizer::types::ENDOFFILE) { switch (tokenizer::type) { case tokenizer::types::COMMENT: + if (!current_comment.empty()) current_comment += "\n"; + current_comment += tokenizer::string; tokenizer::getNextToken(); // Ignore for now break; case tokenizer::types::BLANKLINE: tokenizer::getNextToken(); - // Ignore for now + if (tokenizer::type == tokenizer::types::BLANKLINE) { + current_blank_line = true; + tokenizer::getNextToken(); + } break; - case tokenizer::types::KEY: - if (tokenizer::indent <= indent) return; + case tokenizer::types::KEY: { + if (tokenizer::indent <= indent) + return; if (current_indent<0) current_indent = tokenizer::indent; if (tokenizer::indent != current_indent) { tokenizer::error("Wrong indent"); return; } - (*this)[tokenizer::string].deserialize(current_indent); + yamal& key = (*this)[tokenizer::string]; + if (!current_comment.empty()) { key.setComment(current_comment); current_comment = ""; } + if (current_blank_line) { key.setBlankLine(true); current_blank_line = false; } + key.deserialize(current_indent); break; + } case tokenizer::types::STRINGSCALAR: this->setQuoted(true); case tokenizer::types::SCALAR: *(this) = tokenizer::string; tokenizer::getNextToken(); return; - case tokenizer::types::VECTOR: + case tokenizer::types::VECTOR: { if (tokenizer::indent <= indent) return; if (current_indent<0) current_indent = tokenizer::indent; if (tokenizer::indent != current_indent) { tokenizer::error("Wrong indent"); return; } - (*this).emplace_back().deserialize(current_indent); + yamal& vec = (*this).emplace_back(); + if (!current_comment.empty()) { vec.setComment(current_comment); current_comment = ""; } + if (current_blank_line) { vec.setBlankLine(true); current_blank_line = false; } + vec.deserialize(current_indent); break; + } case tokenizer::types::INLINEVEC: { (*this).setInline(true); (*this).clearToVector(); @@ -385,6 +402,7 @@ private: static void ignoreWhiteSpace() { while (*buffer==32 || *buffer==9) next(); } static void getEverythingUntilEOL() { char tmp[256]; int i=0; + next(); ignoreWhiteSpace(); while (*buffer!=10 && *buffer!=13 && *buffer!=0) tmp[i++]=next(); if (*buffer==13) next(); next(); tmp[i]=0; string = tmp; } @@ -398,11 +416,11 @@ private: const char *delimiters = inlined ? " \t\r\n[]{}#," : " \t\r\n[]{}#"; while (!strchr(delimiters,*buffer)) tmp[i++]=next(); if (tmp[i-1]==':') { - printf("KEY at line %i col %i\n", line, indent); + //printf("KEY at line %i col %i\n", line, indent); tmp[i-1]=0; string=tmp; return types::KEY; } else { - printf("SCALAR at line %i col %i\n", line, indent); + //printf("SCALAR at line %i col %i\n", line, indent); tmp[i]=0; string = tmp;; return types::SCALAR; } } @@ -412,32 +430,32 @@ private: indent = col; switch (*buffer) { case 0: { - printf("ENDOFFILE at line %i col %i\n", line, indent); + //printf("ENDOFFILE at line %i col %i\n", line, indent); type = types::ENDOFFILE; break; } case '#': { - printf("COMMENT at line %i col %i\n", line, indent); + //printf("COMMENT at line %i col %i\n", line, indent); getEverythingUntilEOL(); type = types::COMMENT; break; } case '"': { - printf("STRING SCALAR at line %i col %i\n", line, indent); + //printf("STRING SCALAR at line %i col %i\n", line, indent); getEverythingUntilQuote(); type = types::STRINGSCALAR; break; } case '\r': case '\n': { - printf("BLANKLINE at line %i col %i\n", line, indent); + //printf("BLANKLINE at line %i col %i\n", line, indent); type = types::BLANKLINE; if (*buffer==13) next(); next(); break; } case '-': { if (*(buffer+1)==' ') { - printf("VECTOR at line %i col %i\n", line, indent); + //printf("VECTOR at line %i col %i\n", line, indent); type = types::VECTOR; next(); } else { @@ -446,31 +464,31 @@ private: break; } case '[': { - printf("INLINEVEC at line %i col %i\n", line, indent); + //printf("INLINEVEC at line %i col %i\n", line, indent); type = types::INLINEVEC; inlined = true; next(); break; } case ']': { - printf("INLINEVECOFF at line %i col %i\n", line, indent); + //printf("INLINEVECOFF at line %i col %i\n", line, indent); type = types::INLINEVECOFF; inlined = false; next(); break; } case '{': { - printf("INLINEKEY at line %i col %i\n", line, indent); + //printf("INLINEKEY at line %i col %i\n", line, indent); type = types::INLINEKEY; inlined = true; next(); break; } case '}': { - printf("INLINEKEYOFF at line %i col %i\n", line, indent); + //printf("INLINEKEYOFF at line %i col %i\n", line, indent); type = types::INLINEKEYOFF; inlined = false; next(); break; } case ',': { - printf("COMMA at line %i col %i\n", line, indent); + //printf("COMMA at line %i col %i\n", line, indent); type = types::COMMA; next(); break;