Attribute Parser

  • + 1 comment

    You can use a deque to keep track of the token stack and use a deque to string mapping function to resolve it to an attributeDB entry:

    string d_to_string(const deque<string> &d) {
        string s;
        for (const auto &t:d) {
            if (t[0] == '<') {
                if (s.size())
                    s += '.';
                s += t.substr(1);
            } else
                s += '~' + t;
        }
        return s;
    }
    
    [...]
        cin.ignore();
        deque<string> tags;
    [...]
               if (word[0] == '<') {
                    // tag::end -> pop
                    if (word[1] == '/') {
                        tags.pop_back();
                    // tag::begin -> push
                    } else {
    					// the "- (word[word.size()-1] == '>')" trick is needed to pass half the cases
    					// this is due to no-attr tags of the form "<tag>" and "<tag>" being in the deque
    					// instead of "<tag"
                        tags.push_back(word.substr(0, word.size() - (word[word.size()-1] == '>')));
                    }
                // attr::val
                } else if (word[0] == '"') {
                    size_t pos = word.find_last_of('"');
                    attr_db.insert({d_to_string(tags)], word.substr(1,pos-1)});
                    tags.pop_back();
                // attr::name
                } else if (word[0] != '=') {
                    tags.push_back(word);
                }
    [...]