⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.cpp

📁 2009 ROBOCUP 仿真2DSERVER 源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    // check if the stream is already on the stack    for ( StreamStack::iterator i = m_stack.begin();          i != m_stack.end();          ++i )    {        if ( full_name.native_file_string() == i->m_name )        {            // file already included so ignore it            return true;        }    }    if ( boost::filesystem::exists( full_name ) )    {        if ( ! boost::filesystem::is_directory( full_name ) )        {            boost::filesystem::ifstream strm( full_name );            if ( strm.is_open() && strm.good() )            {                bool rval = parse( strm,                                   full_name.native_file_string() );                strm.close();                return rval;            }            else            {                m_builder.includeFailed( full_name.native_file_string(),                                         "read failed",                                         m_stack.front().m_name,                                         m_stack.front().m_lineno );                return false;            }        }        else        {            m_builder.includeFailed( full_name.native_file_string(),                                     "cannot include a directory",                                     m_stack.front().m_name,                                     m_stack.front().m_lineno );            return false;        }    }    else    {        m_builder.includeFailed( full_name.native_file_string(),                                 "file not found",                                 m_stack.front().m_name,                                 m_stack.front().m_lineno );        return false;    }}boolParser::setParamName( const char * begin,                      const char * end ){    m_curr_param = cleanString( begin, end );    m_curr_module.clear();    return true;}voidParser::appendParamName( const char * begin,                         const char * end ){    if ( m_curr_module.empty() )    {        m_curr_module = m_curr_param;    }    else    {        m_curr_module += "::" + m_curr_param;    }    m_curr_param = cleanString( begin, end );}boolParser::buildBool( bool value ){    m_builder.buildParam( m_curr_module, m_curr_param, value,                          m_stack.front().m_name,                          m_stack.front().m_lineno );    return true;}boolParser::buildInt( int value ){    m_builder.buildParam( m_curr_module, m_curr_param, value,                          m_stack.front().m_name, m_stack.front().m_lineno );    return true;}boolParser::buildReal( double value ){    m_builder.buildParam( m_curr_module, m_curr_param, value,                          m_stack.front().m_name, m_stack.front().m_lineno );    return true;}boolParser::buildString( const char* begin, const char* end ){    m_builder.buildParam( m_curr_module, m_curr_param,                          cleanString( begin, end ),                          m_stack.front().m_name, m_stack.front().m_lineno );    return true;}voidParser::requestGenericHelp(){    m_builder.requestGenericHelp();}voidParser::requestDetailedHelp(){    if ( m_curr_module.empty() )    {        m_curr_module = m_curr_param;    }    else    {        m_curr_module += "::" + m_curr_param;    }    m_builder.requestDetailedHelp( m_curr_module );}voidParser::parseError( size_t pos,                    const std::string& buffer ){    std::string what;    if ( pos + 10 > buffer.size() )    {        what = std::string( buffer.begin() + pos, buffer.end() );    }    else    {        what = std::string( buffer.begin() + pos,                            buffer.begin() + pos + 10 );    }    std::string::iterator newline = std::find( what.begin(),                                               what.end(),                                               '\n' );    if ( newline != what.end() )    {        what = std::string( what.begin(), newline );    }    m_builder.parseError( what,                          "unknown parse error",                          m_stack.front().m_name,                          m_stack.front().m_lineno );}boolParser::boostParse(){    using namespace rcss::conf;    boost::spirit::assertion< Errors > expect_assign( ASSIGN_EXPECTED );    boost::spirit::assertion< Errors > expect_delim( DELIM_EXPECTED );    boost::spirit::assertion< Errors > expect_value( VALUE_EXPECTED );    boost::spirit::assertion< Errors > expect_string( STRING_EXPECTED );    boost::spirit::guard< Errors > conf_guard;    std::istream & in = * m_stack.front().m_strm;    typedef std::istreambuf_iterator< char > iter_t;    std::string buffer;    std::copy( iter_t( in ), iter_t(), std::back_inserter( buffer ) );    boost::spirit::rule<> ws_p = +boost::spirit::chset_p( " \t\0" );    boost::spirit::rule<> newline_p = boost::spirit::eol_p;    boost::spirit::rule<> comment_p = (                                       boost::spirit::comment_p( "/*", "*/" )                                       | boost::spirit::comment_p( "//" )                                       | boost::spirit::comment_p( "#" ) );    boost::spirit::rule<> junk_p        = ( ws_p            | comment_p[ boost::bind( &Parser::countNewLines, this, _1, _2 ) ]            | newline_p[ boost::bind( &Parser::countNewLines, this, _1, _2 ) ]            );    boost::spirit::rule<> ignore_p = *junk_p;    boost::spirit::rule<> minus_p = boost::spirit::ch_p( '-' );    boost::spirit::rule<> assign_p = boost::spirit::ch_p( '=' );    boost::spirit::rule<> simple_str_p        = ( +(~boost::spirit::chset_p( ":\"'= \t\n-" ) )            >> *(~boost::spirit::chset_p( ":\"'= \t\n" ) )            );    boost::spirit::rule<> pqsb_p = ~boost::spirit::ch_p( '"' );    boost::spirit::rule<> qsb_p = ( boost::spirit::str_p( "\\\"" )                                    | pqsb_p                                    );    boost::spirit::rule<> qstr_p = '"' >> *qsb_p >> '"';    boost::spirit::rule<> pqsb2_p = ~boost::spirit::ch_p( '\'' );    boost::spirit::rule<> qsb2_p = ( boost::spirit::str_p( "\\'" )                                     | pqsb2_p                                     );    boost::spirit::rule<> qstr2_p = '\'' >> (*qsb2_p) >> '\'';    boost::spirit::rule<> string_p = ( simple_str_p                                       | qstr_p                                       | qstr2_p );    boost::spirit::rule<> flag_p = ( boost::spirit::as_lower_d[ "help" ]                                     | boost::spirit::as_lower_d[ "include" ]                                     );    boost::spirit::rule<> includerule_p        = ( boost::spirit::as_lower_d[ "include" ]            >> ignore_p            >> expect_assign( assign_p )            >> ignore_p            >> expect_string( string_p[ boost::bind( &Parser::include,                                                     this,                                                     _1, _2 ) ] )            );    boost::spirit::rule<> delim_p = boost::spirit::str_p( "::" );    boost::spirit::rule<> param_name_p        = ( (simple_str_p - flag_p)[ boost::bind( &Parser::setParamName,                                                  this,                                                  _1, _2 ) ]            >> ( expect_delim( delim_p )                 >> ( simple_str_p - boost::spirit::as_lower_d[ "help" ]                      )[ boost::bind( &Parser::appendParamName,                                      this,                                      _1, _2 ) ]                 )            >> *( delim_p                  >> ( simple_str_p - boost::spirit::as_lower_d[ "help" ]                       )[ boost::bind( &Parser::appendParamName,                                       this,                                       _1, _2 ) ]                  )            );    boost::spirit::rule<> module_name_p        = ( ( simple_str_p - flag_p )[ boost::bind( &Parser::setParamName,                                                    this,                                                    _1, _2 ) ]            >> *( delim_p                  >> ( simple_str_p - boost::spirit::as_lower_d[ "help" ]                       )[ boost::bind( &Parser::appendParamName,                                       this,                                       _1, _2 ) ]                  )            );    boost::spirit::rule<> true_p = ( boost::spirit::as_lower_d[ "on" ]                                     | boost::spirit::as_lower_d[ "true" ]                                     );    boost::spirit::rule<> false_p = ( boost::spirit::as_lower_d[ "off" ]                                      | boost::spirit::as_lower_d[ "false" ]                                      );    boost::spirit::rule<> param_p        = ( param_name_p            >> ignore_p            >> expect_assign( assign_p )            >> ignore_p            >> expect_value( true_p[ boost::bind( &Parser::buildBool,                                                  this,                                                  true ) ]                             | false_p[ boost::bind( &Parser::buildBool,                                                     this,                                                     false ) ]                             | boost::spirit::strict_real_p[ boost::bind( &Parser::buildReal,                                                                          this,                                                                          _1 ) ]                             | boost::spirit::int_p[ boost::bind( &Parser::buildInt,                                                                  this,                                                                  _1 ) ]                             | string_p[ boost::bind( &Parser::buildString,                                                      this,                                                      _1, _2 ) ]                             )            );    boost::spirit::rule<> data_p        = ( boost::spirit::as_lower_d[ "help" ][ boost::bind( &Parser::requestGenericHelp,                                                              this ) ]            | ( module_name_p                >> delim_p                >> boost::spirit::as_lower_d[ "help" ]                )[ boost::bind( &Parser::requestDetailedHelp,                                this ) ]            | param_p            | includerule_p            );    boost::spirit::rule<> item_p = ( junk_p                                     | ( !minus_p                                         >> !minus_p                                         >> data_p )                                     );    boost::spirit::rule<> input_p        = conf_guard( *item_p )[ boost::bind( &ParseErrorHandler::parseError,                                              this,                                              _1, _2 ) ];    // 			BOOST_SPIRIT_DEBUG_RULE(ws_p);    // 			BOOST_SPIRIT_DEBUG_RULE(newline_p);    // 			BOOST_SPIRIT_DEBUG_RULE(comment_p);    // 			BOOST_SPIRIT_DEBUG_RULE(string_p);    // 			BOOST_SPIRIT_DEBUG_RULE(input_p);    // 			BOOST_SPIRIT_DEBUG_RULE(item_p);    // 			BOOST_SPIRIT_DEBUG_RULE(param_p);    // 			BOOST_SPIRIT_DEBUG_RULE(data_p);    // 			BOOST_SPIRIT_DEBUG_RULE(junk_p);    // 			BOOST_SPIRIT_DEBUG_RULE(ignore_p);    // 			BOOST_SPIRIT_DEBUG_RULE(includerule_p);    // 			BOOST_SPIRIT_DEBUG_RULE(loadrule_p);    // 			BOOST_SPIRIT_DEBUG_RULE(pathrule_p);    // 			BOOST_SPIRIT_DEBUG_RULE(param_name_p);    // 			BOOST_SPIRIT_DEBUG_RULE(module_name_p);    // 			BOOST_SPIRIT_DEBUG_RULE(true_p);    // 			BOOST_SPIRIT_DEBUG_RULE(false_p);    // 			BOOST_SPIRIT_DEBUG_RULE(flag_p);    // 			BOOST_SPIRIT_DEBUG_RULE(boost::spirit::as_lower_d[ "help" ]);    // 			BOOST_SPIRIT_DEBUG_RULE(delim_p);    // 			BOOST_SPIRIT_DEBUG_RULE(minus_p);    // 			BOOST_SPIRIT_DEBUG_RULE(assign_p);    // 			BOOST_SPIRIT_DEBUG_RULE(simple_str_p);    // 			BOOST_SPIRIT_DEBUG_RULE(qstr_p);    // 			BOOST_SPIRIT_DEBUG_RULE(qstr2_p);    // 			BOOST_SPIRIT_DEBUG_RULE(string_p);    // 			BOOST_SPIRIT_DEBUG_RULE(boost::spirit::int_p);    // 			BOOST_SPIRIT_DEBUG_RULE(includerule_p);    boost::spirit::parse_info<> info = boost::spirit::parse( buffer.c_str(),                                                             input_p );    if( ! info.full )    {        parseError( info.length, buffer );    }    return info.full;}}}

⌨️ 快捷键说明

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