Example #1
0
/* values not in quotes are literals */
void PJson::process_char(In& source, State& state, bool& out_move)
{
    bool is_literal = this->lookup.table[*source.pointer()];

    if(is_literal) {

        bool likely_fp = false;
        In::Notify dec ('.', likely_fp);

        auto block = source.read_block_until(error, ',', ' ', '}', ']', dec);

        this->process_literal(block, state, likely_fp ? Type::FP64 : Type::Null);

        if(*source.pointer() == '}' || *source.pointer() == ']') {
            /* The bracket has a double meaning here, first it delimits the literal,
             * and second it closes the scope. So make sure it will be processed. */
            out_move = false;
        }
    }
}
Example #2
0
pair<Lib::MemBlock, Value> PJson::read(In& source)
{
    State state;
    while(!state.complete) {

        /* literals can be 'un-quoted', values in 'square-brackets' don't have a name */
        if(!state.reading_value && this->scope_type != Type::Array) {

            source.move_until(error, '\"', ':', '{', '[', '}', ']', '/');

        } else {
            source.move_while(error, ' ', ',', '\n', '\t', '\r');
        }

        bool move = true;
        char c = *source.pointer();
        switch(c) {
            case '\"': this->process_quote(source, state);
                       break;

            case ':' : if(state.reading_value) {
                           this->throw_exception(state, "Redundant semicolon.");
                        }
                        state.reading_value = true;
                        break;
            case '{' :
            case '}' :
            case '[' :
            case ']' : this->process_bracket(c, state);
                       break;
            case '/' : skip_comment(source);
                       break;

            default  : this->process_char(source, state, move);
        }

        if(move) {
            source.move(1, error);
        }
    }

    return { state.name, state.value };
}