stategraphparser.cpp

来自「机器人开源项目orocos的源代码」· C++ 代码 · 共 944 行 · 第 1/3 页

CPP
944
字号
    }    void StateGraphParser::seenstateend()    {        if ( curinitialstateflag )        {            if ( curtemplate->getInitialState() )                ORO_THROW(parse_exception_semantic_error( "Attempt to define more than one initial state." ));            else curtemplate->setInitialState( curstate );        }        if ( curfinalstateflag )        {            if ( curtemplate->getFinalState() )                ORO_THROW(parse_exception_semantic_error( "Attempt to define more than one final state." ));            else curtemplate->setFinalState( curstate );        }        assert( curstate );        curstate->setDefined( true );        curstate = 0;        curinitialstateflag = false;        curfinalstateflag = false;    }    void StateGraphParser::inprogram(const std::string& name)    {        // setup the progParser to parse the program body,        // dynamically assign this parser to body.        assert( progParser != 0 );        // program name, stack, line offset.        //cerr << "SGP : Stack is " << curobject->getName() <<endl;        progParser->initBodyParser( name, curobject, ln_offset );                programBody = progParser->bodyParser();    }    ProgramInterfacePtr StateGraphParser::finishProgram()    {        return progParser->bodyParserResult();    }    void StateGraphParser::seenentry()    {        if ( curstate->getEntryProgram() )            ORO_THROW( parse_exception_semantic_error( "Attempt to define entry twice in state "+ curstate->getName() ));        curstate->setEntryProgram( finishProgram() );    }    void StateGraphParser::seenexit()    {        if ( curstate->getExitProgram() )            ORO_THROW( parse_exception_semantic_error( "Attempt to define exit twice in state "+ curstate->getName() ));        curstate->setExitProgram( finishProgram() );    }    void StateGraphParser::seenhandle()    {        if ( curstate->getHandleProgram() )            ORO_THROW( parse_exception_semantic_error( "Attempt to define handle twice in state "+ curstate->getName() ));        curstate->setHandleProgram( finishProgram() );    }    void StateGraphParser::seenrun()    {        if ( curstate->getRunProgram() )            ORO_THROW( parse_exception_semantic_error( "Attempt to define run twice in state "+ curstate->getName() ));        curstate->setRunProgram( finishProgram() );    }    void StateGraphParser::seentransprog()    {        transProgram = finishProgram();        transProgram->setProgramProcessor(curtemplate->getTaskObject()->engine()->programs());    }    void StateGraphParser::seenelseprog()    {        // reuse transProgram to store else progr. See seenselect().        transProgram = finishProgram();        transProgram->setProgramProcessor(curtemplate->getTaskObject()->engine()->programs());    }    void StateGraphParser::seenelse()    {        assert( curcondition);        curcondition = new ConditionInvert( curcondition );    }    void StateGraphParser::seencondition()    {        assert( !curcondition );        curcondition = conditionparser->getParseResult();        assert( curcondition );        conditionparser->reset();        selectln = mpositer.get_position().line - ln_offset;    }    void StateGraphParser::seeneventname(iter_t s, iter_t f)    {        evname = string(s,f);        // seenselect() will use evname to see if event is causing transition        assert(evname.length());        peer    = peerparser->taskObject();        peerparser->reset();        if (peer->events()->hasEvent(evname) == false )            ORO_THROW( parse_exception_fatal_semantic_error("In state "+curstate->getName()+": Event "+evname+" not found in Task "+peer->getName() ));        argsparser =            new ArgumentsParser( *expressionparser, context, peer,                                 evname, "handle" );        argslist = argsparser->parser();    }    void StateGraphParser::seeneventargs()    {        evargs = argsparser->result();        delete argsparser;        argsparser = 0;    }    void StateGraphParser::seenselect( iter_t s, iter_t f)    {        std::string state_id(s,f);        StateInterface* next_state;        if ( curtemplate->getState( state_id ) != 0 )        {            next_state = curtemplate->getState( state_id );        }        else        {            next_state = new StateDescription(state_id,curtemplate->getTaskObject()->engine()->programs(), 1); // create an empty state            curtemplate->addState( next_state );        }        assert( next_state );        if (curcondition == 0)            curcondition = new ConditionTrue;        if (evname.empty()) {            // this transition has a lower priority than the previous one            if ( selectln == 0)                selectln = mpositer.get_position().line - ln_offset;//             if ( elsestate != 0)//                 curtemplate->transitionSet( curstate, next_state, curcondition->clone(), transProgram, elsestate, elseProgram, rank--, selectln );//             else            curtemplate->transitionSet( curstate, next_state, curcondition->clone(), transProgram, rank--, selectln );        } else {            bool res;//             if ( elsestate != 0)//                 res = curtemplate->createEventTransition( &(peer->eventService), evname, evargs, curstate, next_state, curcondition->clone(), transProgram, elsestate, elseProgram );//             else            //cerr << "Registering "<<evname<<" handler for SM."<<endl;            res = curtemplate->createEventTransition( peer->events(), evname, evargs, curstate, next_state, curcondition->clone(), transProgram );            assert( res ); // checked in seeneventname()            elsestate = 0;            elseProgram.reset();        }    }    void StateGraphParser::seenendcondition() {        delete curcondition;        curcondition = 0;        selectln = 0;        transProgram.reset();    }    void StateGraphParser::seeneventtrans() {        // cleanup all event related state.        evname.clear();        evargs.clear();    }    void StateGraphParser::seenprecondition()    {        assert( !curcondition );        curcondition = conditionparser->getParseResult();        assert( curcondition );        conditionparser->reset();        selectln = mpositer.get_position().line - ln_offset;        curtemplate->preconditionSet(curstate, curcondition, selectln );        selectln = 0;        curcondition = 0;    }    void StateGraphParser::seenstatemachineend()    {        assert( curtemplate );        assert( ! curstate );        // reclaim the defined variables:                // Check if the Initial and Final States are ok.        if ( curtemplate->getInitialState() == 0 )            ORO_THROW( parse_exception_semantic_error("No initial state defined."));        if ( curtemplate->getFinalState() == 0 )            ORO_THROW( parse_exception_semantic_error("No final state defined."));        if ( curtemplate->getStateList().empty() )            ORO_THROW( parse_exception_semantic_error("No states defined in this state machine !"));        // Check if all States are defined.        vector<string> states = curtemplate->getStateList();        for( vector<string>::const_iterator it = states.begin(); it != states.end(); ++it)        {            assert( dynamic_cast<StateDescription*>( curtemplate->getState( *it ) ) );            StateDescription* sd = static_cast<StateDescription*>( curtemplate->getState( *it ) );            if ( !sd->isDefined() )                ORO_THROW( parse_exception_semantic_error("State " + *it + " not defined, but referenced to."));        }        // retrieve _all_ defined variables and parameters, store them and cleanup the        // valuechangeparser.        valuechangeparser->store( curtemplate->getTaskObject() );        valuechangeparser->reset();        // prepend the commands for initialising the subMachine        // variables..        assert( curtemplate->getInitCommand() == 0);        if ( varinitcommands.size() > 1 )            {                CommandComposite* comcom = new CommandComposite;                for ( std::vector<CommandInterface*>::iterator i = varinitcommands.begin();                      i != varinitcommands.end(); ++i )                    comcom->add( *i );                curtemplate->setInitCommand( comcom );            }        else if (varinitcommands.size() == 1 )            curtemplate->setInitCommand( *varinitcommands.begin() );        varinitcommands.clear();        // remove temporary subMachine peers from current task.        for( StateMachine::ChildList::const_iterator it= curtemplate->getChildren().begin();             it != curtemplate->getChildren().end(); ++it ) {            ParsedStateMachine* psc = dynamic_cast<ParsedStateMachine*>( it->get() );            if (psc) {                psc->getTaskObject()->setParent(0);                // since context is not the parent of psc, it wil not delete it.                context->removeObject( psc->getTaskObject()->getName() );            }        }        // finally :         curtemplate->finish();        delete progParser;        progParser = 0;        StateMachineBuilder* scb = new StateMachineBuilder( curtemplate );        machinebuilders[curmachinename] = scb;        // save curmachinename for saveText.        curobject = 0;        curtemplate.reset();    }    std::vector<ParsedStateMachinePtr> StateGraphParser::parse( iter_t& begin, iter_t end )    {        skip_parser_t skip_parser = SKIP_PARSER;        iter_pol_t iter_policy( skip_parser );        scanner_pol_t policies( iter_policy );        scanner_t scanner( begin, end, policies );        // reset the condition-transition priority.        rank = 0;        this->storeOffset();        try {            if ( ! production.parse( scanner ) )            {                // on error, we clear all remaining data, cause we can't                // guarantee consistency...                clear();                throw file_parse_exception(                    new parse_exception_syntactic_error( "Syntax error" ),                    mpositer.get_position().file, mpositer.get_position().line,                    mpositer.get_position().column );            }            std::vector<ParsedStateMachinePtr> ret = values( rootmachines );            rootmachines.clear();            return ret;        }        catch( const parser_error<std::string, iter_t>& e )        {            // on error, we clear all remaining data, cause we can't            // guarantee consistency...            clear();            throw file_parse_exception(                new parse_exception_syntactic_error( e.descriptor ),                mpositer.get_position().file, mpositer.get_position().line,                mpositer.get_position().column );        }        catch( const parser_error<GraphSyntaxErrors, iter_t>& e )        {            // on error, we clear all remaining data, cause we can't            // guarantee consistency...            clear();            throw file_parse_exception(                new parse_exception_syntactic_error( "Expected one of: entry, handle, exit, transitions" ),                mpositer.get_position().file, mpositer.get_position().line,                mpositer.get_position().column );        }        catch( const parse_exception& e )        {            // on error, we clear all remaining data, cause we can't            // guarantee consistency...

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?