예제 #1
0
/*!
 * \brief Constructs a new node entry which is deserialized from the specified \a stream.
 */
NodeEntry::NodeEntry(istream &stream)
    : m_expandedByDefault(true)
{
    BinaryReader reader(&stream);
    const byte version = reader.readByte();
    if (!denotesNodeEntry(version)) {
        throw ParsingException("Node entry expected.");
    }
    if (version != 0x0 && version != 0x1) {
        throw ParsingException("Entry version not supported.");
    }
    setLabel(reader.readLengthPrefixedString());
    // read extended header for version 0x1
    if (version == 0x1) {
        uint16 extendedHeaderSize = reader.readUInt16BE();
        if (extendedHeaderSize >= 1) {
            byte flags = reader.readByte();
            m_expandedByDefault = flags & 0x80;
            extendedHeaderSize -= 1;
        }
        m_extendedData = reader.readString(extendedHeaderSize);
    }
    const uint32 childCount = reader.readUInt32BE();
    for (uint32 i = 0; i != childCount; ++i) {
        Entry::parse(stream)->setParent(this);
    }
}
예제 #2
0
//=========================================================
bool Parser::Function () {
    PrintRule rule("Function");
    if (Accept(TokenType::Function) == false) {
        return false;
    }
    Expect(TokenType::Identifier);
    Expect(TokenType::OpenParentheses);

    if (Parameter()) {
        while (Accept(TokenType::Comma)) {
            if (!Parameter()) {
                throw ParsingException("Expected parameter after comma in function sig");
            }
        }
    }

    Expect(TokenType::CloseParentheses);

    SpecifiedType();

    if (!Scope()) {
        throw ParsingException("Expected scope after function");
    }

    return rule.Accept();
}
예제 #3
0
		std::unique_ptr<Expression> ArrayAccessParser::parse_static(ElsaParser* parser)
		{
			auto arr_exp = std::make_unique<ArrayAccessExpression>();

			auto identifier = parser->current_token()->get_value();
			parser->consume(TokenType::Identifier);

			auto id_exp = std::make_unique<IdentifierExpression>(identifier);

			std::unique_ptr<ElsaType> array_type;
			if (parser->current_type() != nullptr)
			{
				array_type = std::unique_ptr<ElsaType>(parser->type_checker().get_access_type(parser->current_type(), identifier));
			}
			else
			{
				array_type = std::unique_ptr<ElsaType>(parser->type_checker().get_expression_type(id_exp.get()));
			}

			if (array_type->get_struct_declaration_expression() == nullptr || array_type->get_struct_declaration_expression()->get_name(true) != L"Array")
				throw ParsingException(L"Only arrays can be accessed by index.", parser->current_token());

			id_exp->set_type(new ElsaType(array_type->get_struct_declaration_expression()->get_generic_type()));

			arr_exp->set_identifier_expression(std::move(id_exp));

			parser->consume(TokenType::LSBracket);

			auto index_expression = parser->parse_expression();
			auto index_expression_type = std::unique_ptr<ElsaType>(parser->type_checker().get_expression_type(index_expression.get()));

			if (index_expression_type->get_type() != ObjectType::Int)
				throw ParsingException(L"Only integers are supported when accessing array elements", parser->current_token());

			arr_exp->set_index_expression(std::move(index_expression));

			parser->consume(TokenType::RSBracket);

			if (parser->current_token()->get_type() == TokenType::Dot)
			{
				if (arr_exp->get_identifier_expression()->get_type()->get_type() != ObjectType::GCOPtr)
					throw ParsingException(L"Only struct fields and functions can be accessed by the '.' operator", parser->current_token());

				parser->consume(TokenType::Dot);
				parser->set_current_type(arr_exp->get_identifier_expression()->get_type());

				auto sae = StructAccessParser::parse_static(parser);
				arr_exp->set_struct_access_expression(dynamic_cast<StructAccessExpression*>(sae.release()));

				parser->set_current_type(nullptr);
			}

			return std::move(arr_exp);
		}
예제 #4
0
//=========================================================
bool Parser::While () {
    PrintRule rule("While");
    if (Accept(TokenType::While) == false) {
        return false;
    }
    if (!GroupedExpression()) {
        throw ParsingException("Expected grouped expression in while");
    }
    if (!Scope()) {
        throw ParsingException("Expected scope in while");
    }
    return rule.Accept();
}
std::unique_ptr<Expression> AnonymousFuncDeclarationParser::parse(ElsaParser* parser)
{
    auto func_dec_exp = FuncParserHelper::parse_signature(parser, true, nullptr);

    func_dec_exp->set_parent_scope(parser->current_scope());
    parser->push_new_scope(func_dec_exp.get());

    for (auto& arg : func_dec_exp->get_args())
    {
        parser->current_scope()->add_arg(arg->get_name(), arg->get_type());
    }

    parser->consume(TokenType::Equals);
    parser->consume(TokenType::GreaterThan);
    parser->consume(TokenType::LBracket);

    while (parser->current_token()->get_type() != TokenType::RBracket)
    {
        func_dec_exp->add_body_expression(parser->parse_expression());
    }

    if (!parser->type_checker().return_type_match(func_dec_exp.get()))
        throw ParsingException(L"Return type mismatch in function '" + func_dec_exp->get_name() + L"'", parser->current_token());

    parser->pop_current_scope();

    parser->consume(TokenType::RBracket);

    // If the function is passed directly no semi colon should be specified
    if(parser->current_token()->get_type() == TokenType::Semicolon)
        parser->consume(TokenType::Semicolon);

    return std::move(func_dec_exp);
}
예제 #6
0
		std::wstring ElsaType::get_name() const
		{
			switch (type_)
			{
			case ObjectType::Void: {
				return L"void";
			}
			case ObjectType::Bool: {
				return L"bool";
			}
			case ObjectType::Int: {
				return L"int";
			}
			case ObjectType::Float: {
				return L"float";
			}
			case ObjectType::Char: {
				return L"char";
			}
			case ObjectType::Byte: {
				return L"byte";
			}
			case ObjectType::GCOPtr: {
				return struct_declaration_expression_->get_name();
			}
			case ObjectType::Function: {
				return func_declaration_expression_->get_type_name();
			}
			case ObjectType::Enum: {
				return enum_declaration_expression_->get_name();
			}
			default:
				throw ParsingException("ElsaType::get_name: Can not get the type name.");
			}
		}
예제 #7
0
 void Serializer::applyStateToComponent(const Component::Ptr &theComponent,
                                        const std::string &theState,
                                        const PropertyIO &theIO)
 {
     Json::Reader myReader;
     Json::Value myRoot;
     bool myParsingSuccessful = myReader.parse(theState, myRoot);
     
     if (!myParsingSuccessful)
     {
         throw ParsingException(theState);
     }
     
     for (unsigned int i=0; i<myRoot.size(); i++)
     {
         Json::Value myComponentNode = myRoot[i];
         if(myComponentNode[PropertyIO::PROPERTY_NAME] != theComponent->getName()){continue;}
         
         for (unsigned int i=0; i < myComponentNode[PropertyIO::PROPERTIES].size(); i++)
         {
             try
             {
                 std::string myName =
                 myComponentNode[PropertyIO::PROPERTIES][i][PropertyIO::PROPERTY_NAME].asString();
                 
                 Property::Ptr myProperty = theComponent->getPropertyByName(myName);
                 theIO.writePropertyValue(myProperty, myComponentNode[PropertyIO::PROPERTIES][i]);
                 
             } catch (PropertyNotFoundException &myException)
             {
                 LOG_WARNING << myException.what();
             }
         }
     }
 }
예제 #8
0
		VMType ElsaType::get_vm_type() const
		{
			switch (type_)
			{
			case ObjectType::Bool:
			case ObjectType::Int:
			case ObjectType::Enum: {
				return VMType::Int;
			}
			case ObjectType::Float: {
				return VMType::Float;
			}
			case ObjectType::Char: {
				return VMType::Char;
			}
			case ObjectType::Byte: {
				return VMType::Byte;
			}
			case ObjectType::GCOPtr: {
				return VMType::GCOPtr;
			}
			case ObjectType::Function: {
				return VMType::Function;
			}
			default:
				throw ParsingException("Unsupported vm type.");
			}
		}
예제 #9
0
//=========================================================
void Parser::Parse () {
    m_streamCursor = 0;
    Block ();
    // if we didn't consume the whole stream
    if (m_streamCursor != m_tokenStream.size()) {
        throw ParsingException("Stream has remaining tokens");
    }
}
예제 #10
0
Query::Query(TokensReader& tokenReader) {
    this->printNL = true;
    this->level = 1;
    this->setLeftChild(new Predicate(tokenReader));
    if(tokenReader.getNext()->getTokenType() != Q_MARK) 
        throw ParsingException(tokenReader.getCurrent());
    this->getLeftChild()->setRightSibling(new Node(tokenReader.getCurrent()));
}
예제 #11
0
//=========================================================
bool Parser::Else () {
    PrintRule rule("Else");
    if (Accept(TokenType::Else) == false) {
        return false;
    }
    if (!If() && !Scope()) {
        throw ParsingException("Failed to parse Else");
    }
    return rule.Accept();
}
예제 #12
0
//=========================================================
bool Parser::SpecifiedType () {
    PrintRule rule("SpecifiedType");
    if (Accept(TokenType::Colon) == false) {
        return false;
    }
    if (!Type()) {
        throw ParsingException("Expected type in SpecifiedType");
    }
    return rule.Accept();
}
예제 #13
0
//=========================================================
bool Parser::Cast () {
    PrintRule rule("Cast");
    if (Accept(TokenType::As) == false) {
        return false;
    }
    if (!Type()) {
        throw ParsingException("Expected Type in Cast");
    }
    return rule.Accept();
}
예제 #14
0
//=========================================================
bool Parser::Index () {
    PrintRule rule("Index");
    if (Accept(TokenType::OpenBracket) == false) {
        return false;
    }
    if (!Expression()) {
        throw ParsingException("Expected expression in Indexing");
    }
    return rule.Accept(Expect(TokenType::CloseBracket));
}
예제 #15
0
/**
 * This is where the action goes.
 * We are tuning a string into NewOrderSingle
 */
void parseNOS( const std::string &cmd, Metal::NewOrderSingle &nos) {
	// Perform split and basic validation
	std::vector<std::string> tokens;
	split( cmd, ' ', tokens);
	if( tokens.size() < 3 || tokens.size() > 4) throw ParsingException("Invalid command length. Should we 3 or 4 words.");

	// Client Order ID : Generated from current time
	std::stringstream ss;
	time_t now = std::time(0); // we memorize "now" which will be reused
	ss << now;
	nos.setField( FIX::ClOrdID( ss.str()));

	// Instrument, 3rd position
	nos.setField( FIX::Symbol( tokens[2]));

	// Side, 1st position
	FIX::Side side;
	if( tokens[0] == "buy") side = FIX::Side_BUY;
	else if( tokens[0] == "sell") side = FIX::Side_SELL;
	else throw ParsingException( "Unknown Side. Should be \"buy\" or \"sell\" ");
	nos.setField( side);

	// Transacttime : now
	FIX::UTCTIMESTAMP nowUTC;
	nos.setField( FIX::TransactTime( nowUTC));

	// Quantity, 2nd position
	nos.setField( FIX::OrderQty(std::stoi( tokens[1])));

	// Price, 4th position
	FIX::OrdType type;
	if( tokens.size() == 3) { 
		// Market Order
		type = FIX::OrdType_MARKET;
	} else { 
		// Limit Order
		type = FIX::OrdType_LIMIT;
		nos.setField( FIX::Price( std::stof( tokens[3])));
	}
	nos.setField( type);

}
예제 #16
0
//=========================================================
bool Parser::If () {
    PrintRule rule("If");
    if (Accept(TokenType::If) == false) {
        return false;
    }
    if (!GroupedExpression() || !Scope()) {
        throw ParsingException("expected group expression for if");
    }
    Else ();
    return rule.Accept();
}
예제 #17
0
//=========================================================
bool Parser::Expression4 () {
    PrintRule rule("Expression4");
    if (!Expression5()) {
        return false;
    }
    while (Accept(TokenType::Plus) || Accept(TokenType::Minus)) {
        if (!Expression5()) {
            throw ParsingException("Failed Parsing Expression 4");
        }
    }
    return rule.Accept();
}
예제 #18
0
//=========================================================
bool Parser::Expression2 () {
    PrintRule rule("Expression2");
    if (!Expression3()) {
        return false;
    }
    while (Accept(TokenType::LogicalAnd)) {
        if (!Expression3()) {
            throw ParsingException("Failed Parsing expression 2");
        }
    }
    return rule.Accept();
}
예제 #19
0
//=========================================================
bool Parser::Var () {
    PrintRule rule("Var");

    if (Accept(TokenType::Var) == false) {
        return false;
    }

    Expect(TokenType::Identifier);
    if (!SpecifiedType()) {
        throw ParsingException("No specified type provided on variable");
    }

    if (Accept(TokenType::Assignment)) {
        if (!Expression()) {
            throw ParsingException(
                "expected expression on right side of var assignment");
        }
    }

    return rule.Accept();
}
    void TakagiSugenoConsequent::parse(const std::string& consequent,
            const FuzzyEngine& engine) throw (ParsingException) {
        setFuzzyEngine(engine);
        setConsequent(consequent);

        std::stringstream ss(consequent);
        std::string output_lvar;
        ss >> output_lvar;
        if (!fuzzyEngine().outputLVar(output_lvar)) {
            throw ParsingException(FL_AT, "Output variable <" + output_lvar +
                    "> not found in fuzzy engine");
        }
        setOutputLVar(fuzzyEngine().outputLVar(output_lvar));

        std::string equal;
        ss >> equal;
        if (equal != "=") {
            throw ParsingException(FL_AT, "<=> expected but found <" + equal + ">");
        }
        std::string right_side;
        std::string token;
        while (ss >> token) {
            right_side += token + " ";
        }
        setPostfixFunction(_infix2postfix.transform(right_side));
        std::map<std::string, flScalar> variables;
        for (int i = 0; i < fuzzyEngine().numberOfInputLVars(); ++i) {
            variables[fuzzyEngine().inputLVar(i)->name()] =
                    fuzzyEngine().inputLVar(i)->input();
        }
        for (int i = 0; i < fuzzyEngine().numberOfOutputLVars(); ++i) {
            variables[fuzzyEngine().outputLVar(i)->name()] =
                    fuzzyEngine().outputLVar(i)->output().defuzzify();
        }
        try {
            _infix2postfix.evaluate(postfixFunction(), &variables);
        } catch (FuzzyException& e) {
            throw ParsingException(FL_AT, e.message(), e);
        }
    }
예제 #21
0
//=========================================================
bool Parser::FunctionType () {
    PrintRule rule("FunctionType");
    if (Accept(TokenType::Function) == false) {
        return false;
    }
    Expect(TokenType::Asterisk);
    while (Accept(TokenType::Asterisk));
    while (Accept(TokenType::Ampersand));
    Expect(TokenType::OpenParentheses);
    if (!Type()) {
        throw ParsingException("Expected type in function type");
    }
    while (Accept(TokenType::Comma)) {
        if (!Type()) {
            throw ParsingException(
                "Expected Type after comma in function type");
        }
    }
    Expect(TokenType::CloseParentheses);
    SpecifiedType();
    return rule.Accept();
}
예제 #22
0
/*!
 * \brief Constructs a new account entry for the specified account which is deserialize from
 *        the specified \a stream.
 * \throws Throws ParsingException when an parsing error occurs.
 */
Field::Field(AccountEntry *tiedAccount, istream &stream)
{
    BinaryReader reader(&stream);
    const int version = reader.readByte();
    if (version != 0x0 && version != 0x1) {
        throw ParsingException("Field version is not supported.");
    }
    m_name = reader.readLengthPrefixedString();
    m_value = reader.readLengthPrefixedString();
    byte type = reader.readByte();
    if (!isValidType(type)) {
        throw ParsingException("Field type is not supported.");
    }
    m_type = static_cast<FieldType>(type);
    // read extended header for version 0x1
    if (version == 0x1) {
        const uint16 extendedHeaderSize = reader.readUInt16BE();
        // currently there's nothing to read here
        m_extendedData = reader.readString(extendedHeaderSize);
    }
    m_tiedAccount = tiedAccount;
}
예제 #23
0
/*!
 * \brief Constructs a new account entry which is deserialized from the specified \a stream.
 */
AccountEntry::AccountEntry(istream &stream)
{
    BinaryReader reader(&stream);
    byte version = reader.readByte();
    if (denotesNodeEntry(version)) {
        throw ParsingException("Account entry expected.");
    }
    version ^= 0x80; // set first bit to zero
    if (version != 0x0 && version != 0x1) {
        throw ParsingException("Entry version not supported.");
    }
    setLabel(reader.readLengthPrefixedString());
    // read extended header for version 0x1
    if (version == 0x1) {
        const uint16 extendedHeaderSize = reader.readUInt16BE();
        // currently there's nothing to read here
        m_extendedData = reader.readString(extendedHeaderSize);
    }
    const uint32 fieldCount = reader.readUInt32BE();
    for (uint32 i = 0; i != fieldCount; ++i) {
        m_fields.push_back(Field(this, stream));
    }
}
예제 #24
0
// ID LEFT_PAREN ID idList RIGHT_PAREN
HeadPredicate::HeadPredicate(TokensReader& tokenReader) {
    if(tokenReader.getCurrent()->getTokenType() != ID) {
        throw ParsingException(tokenReader.getCurrent());
    }
    Node* id = new Node(tokenReader.getCurrent());
    this->setLeftChild(id);
    if(tokenReader.getNext()->getTokenType() != LEFT_PAREN) {
        throw ParsingException(tokenReader.getCurrent());
    }
    Node* leftParen = new Node(tokenReader.getCurrent());
    id->setRightSibling(leftParen);
    if(tokenReader.getNext()->getTokenType() != ID) {
        throw ParsingException(tokenReader.getCurrent());
    }
    Node* nextId = new Node(tokenReader.getCurrent());
    leftParen->setRightSibling(nextId);
    nextId->setRightSibling(new IdList(tokenReader));
    
    if(tokenReader.getCurrent()->getTokenType() != RIGHT_PAREN) {
        throw ParsingException(tokenReader.getCurrent());
    }
    Node* rightParen = new Node(tokenReader.getCurrent());
    nextId->getRightSibling()->setRightSibling(rightParen);
}
예제 #25
0
//=========================================================
bool Parser::Call () {
    PrintRule rule("Call");
    if (Accept(TokenType::OpenParentheses) == false) {
        return false;
    }
    if (Expression()) {
        while (Accept(TokenType::Comma)) {
            if (!Expression()) {
                throw ParsingException(
                    "Expected expression after comma in func call"
                );
            }
        }
    }
    return rule.Accept(Expect(TokenType::CloseParentheses));
}
예제 #26
0
//=========================================================
bool Parser::Expression5 () {
    PrintRule rule("Expression5");
    if (!Expression6()) {
        return false;
    }
    while (
        Accept(TokenType::Asterisk) ||
        Accept(TokenType::Divide)   ||
        Accept(TokenType::Modulo)
    ) {
        if (!Expression6()) {
            throw ParsingException("Failed Parsing expression 5");
        }
    }
    return rule.Accept();
}
예제 #27
0
//=========================================================
bool Parser::For () {
    PrintRule rule("For");
    if (Accept(TokenType::For) == false) {
        return false;
    }
    Expect(TokenType::OpenParentheses);
    if (Var() || Expression());
    Expect(TokenType::Semicolon);
    Expression();
    Expect(TokenType::Semicolon);
    Expression();
    Expect(TokenType::CloseParentheses);
    if (!Scope()) {
        throw ParsingException("Expected scope for For block");
    }
    return rule.Accept();
}
예제 #28
0
//=========================================================
bool Parser::Expression3 () {
    PrintRule rule("Expression3");
    if (!Expression4()) {
        return false;
    }
    while (
        Accept(TokenType::LessThan) ||
        Accept(TokenType::GreaterThan) ||
        Accept(TokenType::LessThanOrEqualTo) ||
        Accept(TokenType::GreaterThanOrEqualTo) ||
        Accept(TokenType::Equality) ||
        Accept(TokenType::Inequality)
    ) {
        if (!Expression4()) {
            throw ParsingException("Failed Parsing Expression 3");
        }
    }
    return rule.Accept();
}
예제 #29
0
SmartBatteryDataField keyToSmartField( const string &key )
{
    if  (key=="00") return ManufacturerAccess;
    else if (key=="01") return RemainingCapacityAlarm;
    else if (key=="02") return RemainingTimeAlarm;
    else if (key=="03") return BatteryMode;
    else if (key=="04") return AtRate;
    else if (key=="05") return AtRateTimeToFull;
    else if (key=="06") return AtRateTimeToEmpty;
    else if (key=="07") return AtRateOk;
    else if (key=="08") return Temperature;
    else if (key=="09") return Voltage;
    else if (key=="0A") return Current;
    else if (key=="0B") return AverageCurrent;
    else if (key=="0C") return MaxError;
    else if (key=="0D") return RelativeStateOfCharge;
    else if (key=="0E") return AbsoluteStateOfCharge;
    else if (key=="0F") return RemainingCapacity;
    else if (key=="10") return FullChargeCapacity;
    else if (key=="11") return RunTimeToEmpty;
    else if (key=="12") return AverageTimeToEmpty;
    else if (key=="13") return AverageTimeToFull;
    else if (key=="14") return ChargingCurrent;
    else if (key=="15") return ChargingVoltage;
    else if (key=="16") return BatteryStatus;
    else if (key=="17") return CycleCount;
    else if (key=="18") return DesignCapacity;
    else if (key=="19") return DesignVoltage;
    else if (key=="1A") return SpecificationInfo;
    else if (key=="1B") return ManufactureDate;
    else if (key=="1C") return SerialNumber;
    else if (key=="20") return ManufacturerName;
    else if (key=="21") return DeviceName;
    else if (key=="22") return DeviceChemistry;
    else if (key=="23") return ManufacturerData;
    else 
    {
        stringstream ss;
        ss << "Unknown field: " << key;
        throw ParsingException( ERROR_INFO, ss.str().c_str() );
    }
}
예제 #30
0
//=========================================================
bool Parser::Expression () {
    PrintRule rule("Expression");
    if (!Expression1()) {
        return false;
    }

    if (
        Accept(TokenType::Assignment)         ||
        Accept(TokenType::AssignmentPlus)     ||
        Accept(TokenType::AssignmentMinus)    ||
        Accept(TokenType::AssignmentMultiply) ||
        Accept(TokenType::AssignmentDivide)   ||
        Accept(TokenType::AssignmentModulo)
    ) {
        if (!Expression()) {
            throw ParsingException("Failed parsing base expression");
        }
    }
    return rule.Accept();
}