CJsonNode CNetScheduleStructuredOutputParser::ParseJSON(const string& json) { m_Ch = (m_NSOutput = json).c_str(); while (isspace((unsigned char) *m_Ch)) ++m_Ch; CJsonNode root; switch (*m_Ch) { case '[': ++m_Ch; root = ParseArray(']'); break; case '{': ++m_Ch; root = ParseObject('}'); break; default: INVALID_FORMAT_ERROR(); } while (isspace((unsigned char) *m_Ch)) ++m_Ch; if (*m_Ch != '\0') { INVALID_FORMAT_ERROR(); } return root; }
static def_t *GetArrayDef (const char *name, type_t *type, int *elementCount) { def_t *def; int count; int regCount; if (type->type == ev_field) { return GetFieldArrayDef(name, type, elementCount); } count = ParseArray(name, type); def = NewVarDef(name, type); // Precede the array register storage with the element count // for run-time boundary checking. G_INT(numpr_globals) = count-1; numpr_globals++; def->ofs = numpr_globals; regCount = count*type_size[type->type]; do { pr_global_defs[numpr_globals] = def; numpr_globals++; } while (--regCount); *elementCount = count; return def; }
void MessagePackReader::ParseArray16() { log_trace(); unsigned short length; if (is_.Read( reinterpret_cast<char*>(&length), 2 ) != 2) anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated"); length = _msgpack_be16( length ); // compiling for mingw had problem when this was combined with the next line ParseArray(length); }
float3 TdfParser::GetFloat3(float3 def, std::string const& location){ std::string s=SGetValueDef("",location); if(s.empty()){ return def; } float3 ret; ParseArray(s,&ret.x,3); return ret; }
void MessagePackReader::ParseArray32() { log_trace(); unsigned length; if (is_.Read( reinterpret_cast<char*>(&length), 4 ) != 4) anyrpc_throw(AnyRpcErrorTermination, "Parsing was terminated"); length = _msgpack_be32( length ); ParseArray( length ); }
SEXP ParseValue(yajl_val node, int bigint){ if(YAJL_IS_NULL(node)){ return R_NilValue; } if(YAJL_IS_STRING(node)){ SEXP tmp = PROTECT(allocVector(STRSXP, 1)); SET_STRING_ELT(tmp, 0, mkCharCE(YAJL_GET_STRING(node), CE_UTF8)); UNPROTECT(1); return tmp; } if(YAJL_IS_INTEGER(node)){ long long int val = YAJL_GET_INTEGER(node); /* 2^53 is highest int stored as double without loss */ if(bigint && (val > 9007199254740992 || val < -9007199254740992)){ char buf[32]; #ifdef _WIN32 snprintf(buf, 32, "%I64d", val); #else snprintf(buf, 32, "%lld", val); #endif return mkString(buf); /* see .Machine$integer.max in R */ } else if(val > 2147483647 || val < -2147483647){ return ScalarReal(val); } else { return ScalarInteger(val); } } if(YAJL_IS_DOUBLE(node)){ return(ScalarReal(YAJL_GET_DOUBLE(node))); } if(YAJL_IS_NUMBER(node)){ /* A number that is not int or double (very rare) */ /* This seems to correctly round to Inf/0/-Inf */ return(ScalarReal(YAJL_GET_DOUBLE(node))); } if(YAJL_IS_TRUE(node)){ return(ScalarLogical(1)); } if(YAJL_IS_FALSE(node)){ return(ScalarLogical(0)); } if(YAJL_IS_OBJECT(node)){ return(ParseObject(node, bigint)); } if(YAJL_IS_ARRAY(node)){ return(ParseArray(node, bigint)); } error("Invalid YAJL node type."); }
void JsonReader::ParseValue() { log_trace(); // Look at the next character to determine the type of parsing to perform switch (is_.Peek()) { case 'n': ParseNull(); break; case 't': ParseTrue(); break; case 'f': ParseFalse(); break; case '"': ParseString(); break; case '{': ParseMap(); break; case '[': ParseArray(); break; default : ParseNumber(); } }
Json* Json::Parser::ParsePrimary(){ NextChar(); Kind kind = KindPreview(ch); Json* json = NULL; switch(kind){ case kString: { json = ParseString(); break;} case kNumber: { json = ParseNumber(); break;} case kFalse: { json = ParseFalse(); break; } case kTrue: { json = ParseTrue(); break; } case kArray: { json = ParseArray(); break; } case kObject: { json = ParseObject(); break; } case kNull: break; default: break; }; return json; }
/** * parse every possible value. this method parses every possible value (defined by the xmlrpc standard) * and returns it. This is done by calling the special functions ParsePrimitive(), ParseArray() * and ParseStruct(). * @param p XmlParser holding the xml document * @return the parsed Value */ Value XmlRpcParser::Parse(XmlParser& p) { Value v; p.PassTag("value"); if(p.Tag("struct")) { v=ParseStruct(p); p.PassEnd(); //struct } else if(p.Tag("array")) { v=ParseArray(p); p.PassEnd(); //array } else v=ParsePrimitive(p); p.PassEnd(); //value return Value(v); }
static def_t *GetFieldArrayDef (const char *name, type_t *type, int *elementCount) { def_t *def; int count; count = ParseArray(name, type); def = NewVarDef(name, type); def->ofs = numpr_globals; pr_global_defs[numpr_globals] = def; G_INT(numpr_globals) = pr.size_fields; numpr_globals++; pr.size_fields += type_size[type->aux_type->type]*count; *elementCount = count; return def; }
/** * @brief Parse a JSON value and fills it with data from the lexer. * * This function is a bit different from the other two process functions ParseArray() and * ParseObject(), because it takes its value parameter by reference. This is because when * entering the function it is not clear yet which type of value the current lexer token is, so a * new instance has to be created and stored in the pointer. */ bool JsonProcessor::ParseValue ( Lexer::iterator& ct, Lexer::iterator& end, JsonValue*& value ) { // proper usage of this function is to hand over a null pointer to a json value, which will be // assigned to a newly created value instance depending on the token type, so check for this // here. we don't want to overwrite existing values! assert (value == nullptr); // check all possible valid lexer token types and turn them into json values if (ct->IsSymbol()) { // the lexer only returns null, true or false as symbols, so this is safe if (ct->value().compare("null") == 0) { value = new JsonValueNull(); } else { value = new JsonValueBool(ct->value()); } ++ct; return true; } if (ct->IsNumber()) { value = new JsonValueNumber(ct->value()); ++ct; return true; } if (ct->IsString()) { value = new JsonValueString(ct->value()); ++ct; return true; } if (ct->IsBracket("[")) { value = new JsonValueArray(); return ParseArray (ct, end, JsonValueToArray(value)); } if (ct->IsBracket("{")) { value = new JsonValueObject(); return ParseObject (ct, end, JsonValueToObject(value)); } // if the lexer token is not a fitting json value, we have an error LOG_WARN << "JSON value contains invalid characters at " + ct->at() + ": '" + ct->value() + "'."; return false; }
CJsonNode CExecAndParseStructuredOutput::ParseNode() { switch (*m_Ch) { case '[': ++m_Ch; return ParseArray(); case '{': ++m_Ch; return ParseObject(false); case '\'': case '"': return CJsonNode::NewStringNode(ParseString()); } size_t max_len = GetRemainder(); size_t len = 1; switch (*m_Ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': while (len <= max_len && isdigit(m_Ch[len])) ++len; { CJsonNode::TNumber val(NStr::StringToInt8(CTempString(m_Ch, len))); m_Ch += len; return CJsonNode::NewNumberNode(val); } case 'F': case 'f': case 'N': case 'n': case 'T': case 't': case 'Y': case 'y': while (len <= max_len && isalpha(m_Ch[len])) ++len; { bool val(NStr::StringToBool(CTempString(m_Ch, len))); m_Ch += len; return CJsonNode::NewBooleanNode(val); } } return CJsonNode(); }
SymType* Parser::ParseType(bool IsInTypeBlock) { t = sc.GetNextToken(); bool found = false; _Table::iterator it = TableStack.Find(t.GetValue(), found); if (found){ if (IsInTypeBlock) return new SymTypeAlias(t.GetValue(), (SymType*)it->second); return (SymType*)it->second; } if (t.GetValue() == "record") { vector<string>* flds = new vector<string>; return new SymTypeRecord(IntToStr(NotExplType++), ParseRecDecl(flds), flds); } if (t.GetValue() == "array") return ParseArray(); throw Error("unknown symbol in type statement", t); }
bool ParseValue(ParserT *parser, JsonNodeT **node_p) { TokenT *token; JsonNodeT *node = *node_p; if (!node) (*node_p) = node = NewInstance(JsonNodeT); if ((token = ParserMatch(parser, TOK_LBRACE))) { node->type = JSON_OBJECT; node->u.object.num = token->size; if (token->size) node->u.object.item = NewTable(JsonPairT, token->size); return ParseObject(parser, node); } else if ((token = ParserMatch(parser, TOK_LBRACKET))) { node->type = JSON_ARRAY; node->u.array.num = token->size; if (token->size) node->u.array.item = NewTable(JsonNodeT *, token->size); return ParseArray(parser, node); }
AST* ParseExpression(ParserState* parser) { switch(parser->currentToken.type) { case TOKEN_INTREAD: Match(parser, TOKEN_INTREAD); return CreateASTNode(SEM_INTREAD, VALUE_EMPTY); case TOKEN_READ: Match(parser, TOKEN_READ); return CreateASTNode(SEM_READ, VALUE_EMPTY); case TOKEN_ARRAY: return ParseArray(parser); case TOKEN_OBJECT: case TOKEN_NEW: return ParseObject(parser); case TOKEN_FUNCTION: return ParseFunctionDefinition(parser); default: return ParseArithmeticalExpression(parser); } assert(0); return NULL; }
//top level parse function sgdm::StackGuard<JsonValue>&& JsonParser::ParsePrimary(){ switch(currentTok){ case tok_endl: return std::move(Error("primary fail")); case tok_identifier: return std::move(ParseName()); case tok_integer: return std::move(ParseInteger()); case tok_double: return std::move(ParseDouble()); case '[': return std::move(ParseArray(alloc)); case '{': return std::move(ParseObject(alloc)); default: return std::move(Error("unknown token found near place" + std::to_string(indexCount))); } sgdm::StackGuard<JsonValue>&& JsonParser::Parse(){ getNextTok(); return std::move(ParsePrimary(alloc)); }
int MIValue::Parse(String const &s, int i) { // if starts with '"', it's a string // if starts with '[', it's an array // if starts with '{', it's a tuple // otherwise, it can be a sequence of pair name="value" which is stored like a tuple // latter case is an example o bad design of MI interface.... Clear(); while(s[i] && isspace(s[i])) i++; if(s[i] == '"') return ParseString(s, i); else if(s[i] == '<') return ParseAngle(s, i); else if(s[i] == '[') return ParseArray(s, i); else if(s[i] == '{') return ParseTuple(s, i); else { String name; MIValue val; type = MITuple; while(s[i]) { i = ParsePair(name, val, s, i); tuple.AddPick(name, pick(val)); while(s[i] && isspace(s[i])) i++; if(s[i] != ',') break; i++; } return i; } }
static const char *ParseValue(ParseArgs& args, const char *data) { data = SkipWS(data); switch (*data) { case '"': return ParseString(args, data); case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '-': return ParseNumber(args, data); case '{': return ParseObject(args, data); case '[': return ParseArray(args, data); case 't': return ParseKeyword(args, data, "true", Type_Bool); case 'f': return ParseKeyword(args, data, "false", Type_Bool); case 'n': return ParseKeyword(args, data, "null", Type_Null); default: return NULL; } }
CJsonNode CNetScheduleStructuredOutputParser::ParseValue() { size_t max_len = GetRemainder(); size_t len = 0; switch (*m_Ch) { /* Array */ case '[': ++m_Ch; return ParseArray(']'); /* Object */ case '{': ++m_Ch; return ParseObject('}'); /* String */ case '\'': case '"': return CJsonNode::NewStringNode(ParseString(max_len)); /* Number */ case '-': // Check that there's at least one digit after the minus sign. if (max_len <= 1 || !isdigit((unsigned char) m_Ch[1])) { ++m_Ch; break; } len = 1; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': // Skim through the integer part. do if (++len >= max_len) return CJsonNode::NewIntegerNode(ParseInt(len)); while (isdigit((unsigned char) m_Ch[len])); // Stumbled upon a non-digit character -- check // if it's a fraction part or an exponent part. switch (m_Ch[len]) { case '.': if (++len == max_len || !isdigit((unsigned char) m_Ch[len])) { NCBI_THROW2(CStringException, eFormat, "At least one digit after the decimal " "point is required", GetPosition()); } for (;;) { if (++len == max_len) return CJsonNode::NewDoubleNode(ParseDouble(len)); if (!isdigit((unsigned char) m_Ch[len])) { if (m_Ch[len] == 'E' || m_Ch[len] == 'e') break; return CJsonNode::NewDoubleNode(ParseDouble(len)); } } /* FALL THROUGH */ case 'E': case 'e': if (++len == max_len || (m_Ch[len] == '-' || m_Ch[len] == '+' ? ++len == max_len || !isdigit((unsigned char) m_Ch[len]) : !isdigit((unsigned char) m_Ch[len]))) { m_Ch += len; NCBI_THROW2(CStringException, eFormat, "Invalid exponent specification", GetPosition()); } while (++len < max_len && isdigit((unsigned char) m_Ch[len])) ; return CJsonNode::NewDoubleNode(ParseDouble(len)); default: return CJsonNode::NewIntegerNode(ParseInt(len)); } /* Constant */ case 'F': case 'f': case 'N': case 'n': case 'T': case 't': case 'Y': case 'y': while (len <= max_len && isalpha((unsigned char) m_Ch[len])) ++len; { CTempString val(m_Ch, len); m_Ch += len; return val == "null" ? CJsonNode::NewNullNode() : CJsonNode::NewBooleanNode(NStr::StringToBool(val)); } } INVALID_FORMAT_ERROR(); }
bool json::Reader::ParseValue(ConfigValue& value) { SkipSpaces(); bool b = true; char c = *_cur; switch(c) { case '{': b = ParseObject(value); break; case '[': b = ParseArray(value); break; case '"': { std::string str; b = ParseString(str); if(b) value.SetString(str.c_str()); else Error("Failed to parse string"); } break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '-': b = ParseNumber(value); break; case 't': // true if(*(++_cur) != 'r' || *(++_cur) != 'u' || *(++_cur) != 'e') { Error("Expected \"true\""); return false; } ++_cur; value.SetBool(true); break; case 'f': // false if(*(++_cur) != 'a' || *(++_cur) != 'l' || *(++_cur) != 's' || *(++_cur) != 'e') { Error("Expected \"false\""); return false; } ++_cur; value.SetBool(false); break; case 'n': // null if(*(++_cur) != 'u' || *(++_cur) != 'l' || *(++_cur) != 'l') { Error("Expected \"null\""); return false; } ++_cur; value.SetNull(); break; default: b = false; }; return b; }
LLSDValue Parser::Parse(ticpp::Node * node, bool returnEmpty) { if(!node) { boost::throw_exception(SyntaxException("LLSD XML ERROR")); } if(node->Type() != TiXmlNode::ELEMENT || node->Type() == TiXmlNode::TEXT) { if(returnEmpty) { return LLSDValue(); } return Parse(node->NextSibling()); } std::string nodeN = node->Value(); if(LookupTable.count(nodeN) > 0) { LLSDValue result; ticpp::Node * tmp = node->FirstChild(false); switch(LookupTable[nodeN]) { case LLSDValue::VT_INTEGER: if(tmp && tmp->Type() == TiXmlNode::TEXT) { result.xml_decode(LLSDValue::VT_INTEGER, tmp->Value()); } else { result = Int32(0); } return result; case LLSDValue::VT_REAL: if(tmp && tmp->Type() == TiXmlNode::TEXT) { result.xml_decode(LLSDValue::VT_REAL, tmp->Value()); } else { result = Real64(0); } return result; case LLSDValue::VT_STRING: if(tmp && tmp->Type() == TiXmlNode::TEXT) { result.xml_decode(LLSDValue::VT_STRING, tmp->Value()); } else { result = ""; } return result; case LLSDValue::VT_DATE: if(tmp && tmp->Type() == TiXmlNode::TEXT) { result.xml_decode(LLSDValue::VT_DATE, tmp->Value()); } else { result = LLDate(); } return result; case LLSDValue::VT_UUID: if(tmp && tmp->Type() == TiXmlNode::TEXT) { result.xml_decode(LLSDValue::VT_UUID, tmp->Value()); } else { result = LLUUID::Zero; } return result; case LLSDValue::VT_BOOLEAN: if(tmp && tmp->Type() == TiXmlNode::TEXT) { result.xml_decode(LLSDValue::VT_BOOLEAN, tmp->Value()); } else { result = false; } return result; case LLSDValue::VT_BINARY: if(tmp && tmp->Type() == TiXmlNode::TEXT) { result = base64_decode<LLSDValue::Binary>(tmp->Value()); } else { result = LLSDValue::Binary(); } return result; case LLSDValue::VT_URI: if(tmp && tmp->Type() == TiXmlNode::TEXT) { result = LLURI(tmp->Value()); } else { result = LLURI(); } return result; case LLSDValue::VT_MAP: return ParseMap(node); case LLSDValue::VT_ARRAY: return ParseArray(node); case LLSDValue::VT_UNDEF: default: return result; } } ticpp::Iterator<ticpp::Node> elems; for(elems = elems.begin(node); elems != elems.end(); ++elems) { return Parse(elems.Get()); } return LLSDValue(); }
Term* Parser::ParseTermBase() { Token t = lexer->Get(); int n = 0; double d = 0.0; switch (t.type()) { case Token::IntLiteral: { if (dump_tokens) Print("%s", t.symbol().data()); char nstr[256], *p = nstr; for (int i = 0; i < (int) t.symbol().length(); i++) if (t.symbol()[i] != '_') *p++ = t.symbol()[i]; *p++ = '\0'; // handle hex notation: if (nstr[1] == 'x') n = xtol(nstr+2); else n = atol(nstr); return new(__FILE__, __LINE__) TermNumber(n); } case Token::FloatLiteral: { if (dump_tokens) Print("%s", t.symbol().data()); char nstr[256], *p = nstr; for (int i = 0; i < (int) t.symbol().length(); i++) if (t.symbol()[i] != '_') *p++ = t.symbol()[i]; *p++ = '\0'; d = atof(nstr); return new(__FILE__, __LINE__) TermNumber(d); } case Token::StringLiteral: if (dump_tokens) Print("%s", t.symbol().data()); return new(__FILE__, __LINE__) TermText(t.symbol()(1, t.symbol().length()-2)); case Token::AlphaIdent: if (dump_tokens) Print("%s", t.symbol().data()); return new(__FILE__, __LINE__) TermText(t.symbol()); case Token::Keyword: if (dump_tokens) Print("%s", t.symbol().data()); switch (t.key()) { case KEY_FALSE: return new(__FILE__, __LINE__) TermBool(0); case KEY_TRUE: return new(__FILE__, __LINE__) TermBool(1); case KEY_MINUS: { Token next = lexer->Get(); if (next.type() == Token::IntLiteral) { if (dump_tokens) Print("%s", next.symbol().data()); char nstr[256], *p = nstr; for (int i = 0; i < (int) next.symbol().length(); i++) if (next.symbol()[i] != '_') *p++ = next.symbol()[i]; *p++ = '\0'; n = -1 * atol(nstr); return new(__FILE__, __LINE__) TermNumber(n); } else if (next.type() == Token::FloatLiteral) { if (dump_tokens) Print("%s", next.symbol().data()); char nstr[256], *p = nstr; for (int i = 0; i < (int) next.symbol().length(); i++) if (next.symbol()[i] != '_') *p++ = next.symbol()[i]; *p++ = '\0'; d = -1.0 * atof(nstr); return new(__FILE__, __LINE__) TermNumber(d); } else { lexer->PutBack(); return error("(Parse) illegal token '-': number expected", next); } } break; default: lexer->PutBack(); return 0; } case Token::LParen: return ParseArray(); case Token::LBrace: return ParseStruct(); case Token::CharLiteral: return error("(Parse) illegal token ", t); default: lexer->PutBack(); return 0; } }
static void ParseValue( JsnHandler* reader, JsnStreamIn* stream, const JsnFragment& name ) { switch( stream->Peek() ) { case 't': ParseTrue( stream ); reader->AddProperty( name, JsnFragment( kJsn_True ) ); break; case 'f': ParseFalse( stream ); reader->AddProperty( name, JsnFragment( kJsn_False ) ); break; case 'n': ParseNull( stream ); reader->AddProperty( name, JsnFragment( kJsn_Null ) ); break; case '"': { JsnFragment value = ParseString( stream ); reader->AddProperty( name, value ); break; } case '-': case '.': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { JsnFragment value = ParseNumber( stream ); reader->AddProperty( name, value ); break; } case '[': { JsnHandler* child_reader = reader->BeginArray( name ); ParseArray( child_reader, stream ); reader->EndArray( child_reader ); break; } case '{': { JsnHandler* child_reader = reader->BeginObject( name ); ParseObject( child_reader, stream ); reader->EndObject( child_reader ); break; } default: stream->SetError( "Unexpected character" ); break; } }
wxPdfObject* wxPdfParser::ParseObject() { wxPdfObject* obj; m_tokens->NextValidToken(); int type = m_tokens->GetTokenType(); switch (type) { case TOKEN_START_DICTIONARY: { wxPdfDictionary* dic = ParseDictionary(); int pos = m_tokens->Tell(); // be careful in the trailer. May not be a "next" token. if (m_tokens->NextToken() && m_tokens->GetStringValue() == _T("stream")) { int ch = m_tokens->ReadChar(); if (ch != '\n') ch = m_tokens->ReadChar(); if (ch != '\n') m_tokens->BackOnePosition(ch); wxPdfStream* stream = new wxPdfStream(m_tokens->Tell()); stream->SetDictionary(dic); obj = stream; } else { m_tokens->Seek(pos); obj = dic; } } break; case TOKEN_START_ARRAY: { obj = ParseArray(); } break; case TOKEN_NUMBER: { obj = new wxPdfNumber(m_tokens->GetStringValue()); } break; case TOKEN_STRING: { wxString token = m_tokens->GetStringValue(); // Decrypt if necessary if (m_encrypted) { m_decryptor->Encrypt(m_objNum, m_objGen, token); } wxPdfString* strObj = new wxPdfString(token); strObj->SetIsHexString(m_tokens->IsHexString()); obj = strObj; } break; case TOKEN_NAME: { obj = new wxPdfName(m_tokens->GetStringValue()); } break; case TOKEN_REFERENCE: { int num = m_tokens->GetReference(); obj = new wxPdfIndirectReference(num, m_tokens->GetGeneration()); } break; case TOKEN_BOOLEAN: { obj = new wxPdfBoolean((m_tokens->GetStringValue() == _T("true"))); } break; case TOKEN_NULL: { obj = new wxPdfNull(); } break; default: { wxString token = m_tokens->GetStringValue(); obj = new wxPdfLiteral(-type, m_tokens->GetStringValue()); } break; } return obj; }