Gaudi Framework, version v22r2

Home   Generated: Tue May 10 2011
Public Types | Public Member Functions | Private Member Functions | Private Attributes

Gaudi::Parsers::Parser Class Reference

Parser controller. More...

#include <ParserUtils.h>

Collaboration diagram for Gaudi::Parsers::Parser:
Collaboration graph
[legend]

List of all members.

Public Types

enum  Sign { S_ASSIGN = 0, S_PLUSASSIGN = 1, S_MINUSASSIGN = 2 }
typedef std::vector
< Gaudi::Parsers::Message > 
MessagesStoreT
typedef std::map< std::string,
double > 
UnitsStoreT

Public Member Functions

 Parser (Catalogue &catalogue, std::vector< std::string > &included, std::ostream &m=std::cout)
 Default creator.
 Parser (Catalogue &catalogue, std::vector< std::string > &included, const std::vector< std::string > &searchPath, std::ostream &m=std::cout)
 Constructor from directories list for search files in.
 Parser (Catalogue &catalogue, std::vector< std::string > &included, const std::string &searchPath, std::ostream &m=std::cout)
 Constructor from directories list for search files in.
StatusCode parse (const std::string &fileName)
 Start parse file.
const std::vector< Message > & messages ()
 Return parser messages.
int errorsCount ()
 Count of errors.
UnitsStoreTunits (void)
void matchInclude (const std::string &fileName, const Position &pos)
 Do actions when include file is matched.
void matchUnits (const std::string &fileName, const Position &pos)
 Do actions when units file is matched.
long double matchUnit (const std::string &unit, const Position &pos)
 Do actions when we match unit.
void matchUnitEntry (const std::string &newUnit, double value, const Position &pos)
 Do actions when we match unit entry in units file.
void matchAssign (const std::string &objName, const std::string &propName, const Sign &oper, const std::vector< std::string > &vectorValues, const Position &pos, bool isVector)
 Do actions when we match property.
void setIsPrint (bool on, const Position &pos)
 Control printing.
void setIsPrintOptions (bool on, const Position &pos)
 Control printing options.

Private Member Functions

void initUnits ()
 Init units.
StatusCode parseFile (const std::string &fileName, const Position &pos, bool isUnitFile=false)
 Parse file from another file.
void resolveReferences ()
 Resolve references.
void addMessage (const Message::Severity &severity, const Message::Code &code, const std::string &message, const Position &pos)
 Add message.
std::string severityName (Message::Severity severity)
 String representation of severity.
bool isIncluded (const std::string &fileName)
 Test if file already included.
std::string sign (Sign aSign)
 String representation of sign.
std::string valueToString (std::vector< std::string > value, bool isVector)
 String representation of value vector.
bool isPrint ()
 Print information or not.
void printOptions ()
 Print options.
std::string posString (int line, int column)
 Convert position to string.

Private Attributes

bool m_isPrint
bool m_isPrintOptions
std::vector< std::stringm_searchPath
Cataloguem_catalogue
std::vector< std::string > & m_included
MessagesStoreT m_messages
UnitsStoreT m_units
std::ostreamm_stream

Detailed Description

Parser controller.

Author:
Alexander Mazurov
Date:
2006-02-17

Definition at line 100 of file ParserUtils.h.


Member Typedef Documentation

typedef std::vector<Gaudi::Parsers::Message> Gaudi::Parsers::Parser::MessagesStoreT

Definition at line 103 of file ParserUtils.h.

typedef std::map<std::string,double> Gaudi::Parsers::Parser::UnitsStoreT

Definition at line 104 of file ParserUtils.h.


Member Enumeration Documentation

enum Gaudi::Parsers::Parser::Sign
Enumerator:
S_ASSIGN 
S_PLUSASSIGN 
S_MINUSASSIGN 

Definition at line 105 of file ParserUtils.h.

        {
          S_ASSIGN      = 0 ,
          S_PLUSASSIGN  = 1 ,
          S_MINUSASSIGN = 2
        };

Constructor & Destructor Documentation

Gaudi::Parsers::Parser::Parser ( Catalogue catalogue,
std::vector< std::string > &  included,
std::ostream m = std::cout 
)

Default creator.

Definition at line 42 of file Parser.cpp.

  : m_isPrint(true)
  , m_isPrintOptions(false)
  , m_catalogue(catalogue)
  , m_included(included)
  , m_stream ( m )
{
  m_searchPath = Gaudi::Parsers::Utils::extractPath("$JOBOPTSEARCHPATH");
  initUnits();
}
Gaudi::Parsers::Parser::Parser ( Catalogue catalogue,
std::vector< std::string > &  included,
const std::vector< std::string > &  searchPath,
std::ostream m = std::cout 
)

Constructor from directories list for search files in.

Definition at line 56 of file Parser.cpp.

  : m_isPrint(true)
  , m_isPrintOptions(false)
  , m_catalogue(catalogue)
  , m_included(included)
  , m_stream ( m )
{
  m_searchPath = searchPath;
  initUnits();
}
Gaudi::Parsers::Parser::Parser ( Catalogue catalogue,
std::vector< std::string > &  included,
const std::string searchPath,
std::ostream m = std::cout 
)

Constructor from directories list for search files in.

Definition at line 71 of file Parser.cpp.

  : m_isPrint(true)
  , m_isPrintOptions(false)
  , m_catalogue(catalogue)
  , m_included(included)
  , m_stream ( m )
{
  m_searchPath = Gaudi::Parsers::Utils::extractPath(searchPath);
  initUnits();
}

Member Function Documentation

void Gaudi::Parsers::Parser::addMessage ( const Message::Severity severity,
const Message::Code code,
const std::string message,
const Position pos 
) [private]

Add message.

Parameters:
severityMessage severity
codeCode of message
messageMessage
posFile position

Definition at line 410 of file Parser.cpp.

{
  Message result
    ( severity , code,
      boost::str(boost::format("%1%(%2%,%3%) : %4% #%5% : %6%") % pos.fileName()
                 % pos.line() % pos.column() % severityName(severity) % code
                 % message));
  m_messages.push_back(result);
}
int Gaudi::Parsers::Parser::errorsCount (  )

Count of errors.

Definition at line 95 of file Parser.cpp.

{
        int result=0;
        for ( MessagesStoreT::const_iterator cur=m_messages.begin();
        cur!=m_messages.end() ; ++cur)
  { if ( cur->severity() == Message::E_ERROR){ ++result; } }
        return result;
}
void Gaudi::Parsers::Parser::initUnits (  ) [private]

Init units.

Definition at line 271 of file Parser.cpp.

bool Gaudi::Parsers::Parser::isIncluded ( const std::string fileName ) [private]

Test if file already included.

Returns:
true if file already included
Parameters:
fileNameFile name

Definition at line 429 of file Parser.cpp.

{
  for(std::vector<std::string>::const_iterator cur=m_included.begin();
      cur!=m_included.end();cur++)
  { if(fileName==*cur){ return true; } }
  return false;
}
bool Gaudi::Parsers::Parser::isPrint (  ) [inline, private]

Print information or not.

Definition at line 225 of file ParserUtils.h.

{ return m_isPrint;}
void Gaudi::Parsers::Parser::matchAssign ( const std::string objName,
const std::string propName,
const Sign oper,
const std::vector< std::string > &  vectorValues,
const Position pos,
bool  isVector 
)

Do actions when we match property.

Parameters:
objNameObject name
propNameProperty name
operOperation ("=","+=","-=")
valueString value
vectorValuesVector values

Definition at line 148 of file Parser.cpp.

{
  // --------------------------------------------------------------------------
  if(isPrint())
  {
    m_stream
      << boost::format("%2% %3% %4%;%|72t|%5% %1%")
      % posString(pos.line(),pos.column())
      % (objName+"."+propName)
      % sign(oper)
      % valueToString(vectorValue , isVector)
      % GPP_COMMENT
      << std::endl ;
  }
  // --------------------------------------------------------------------------
  // --------------------------------------------------------------------------
  if (oper == S_ASSIGN)
  {
    // ------------------------------------------------------------------------
    PropertyEntry* assignProp;
    if(isVector){
      assignProp = new PropertyEntry(propName,vectorValue,pos);
    }else{
      assignProp = new PropertyEntry(propName,vectorValue[0],pos);
    }
    m_catalogue.addProperty(objName,*assignProp);
    delete assignProp;
    // ------------------------------------------------------------------------
  }
  else
  {
    // += or -=
    // ------------------------------------------------------------------------
    PropertyEntry foundProp;
    StatusCode ok;
    ok = m_catalogue.findProperty(objName,propName,foundProp);
    if (ok.isFailure())
    {
      foundProp = PropertyEntry(propName,std::vector<std::string>());
    }
    if(oper == S_PLUSASSIGN)
    {
      ok = foundProp.addValues(vectorValue);
      if(ok.isFailure()){
        addMessage
          ( Message::E_ERROR,
            Message::C_CANNOTADDTONOTVECTOR,
            boost::str
            ( boost::format
              ("Cannot add values to not vector property \"%1%.%2%\"")
              %  objName % propName),pos);
        return;
      }
      m_catalogue.addProperty(objName,foundProp);
    }
    // ------------------------------------------------------------------------
    if(oper == S_MINUSASSIGN)
    {
      int count=0;
      ok = foundProp.removeValues(vectorValue,count);
      if(ok.isFailure()){
        addMessage
          ( Message::E_ERROR,
            Message::C_CANNOTREMOVEFROMNOTVECTOR,
            boost::str
            ( boost::format
              ( "Cannot remove values from not vector property \"%1%.%2%\"" )
              %  objName % propName),pos);
        return;
      }
      // ----------------------------------------------------------------------
      if (count == 0)
      {
        addMessage
          ( Message::E_WARNING,
            Message::C_ZEROREMOVED,
            boost::str
            ( boost::format
              ( "Nothing removed from property \"%1%.%2%\"" )
              %  objName % propName),pos);
      }
      else
      {
        m_catalogue.addProperty(objName,foundProp);
      }

    }
    // ------------------------------------------------------------------------
  }
}
void Gaudi::Parsers::Parser::matchInclude ( const std::string fileName,
const Position pos 
)

Do actions when include file is matched.

Parameters:
fileNameFile to include

Definition at line 105 of file Parser.cpp.

{ parseFile(fileName,pos).ignore(); }
long double Gaudi::Parsers::Parser::matchUnit ( const std::string unit,
const Position pos 
)

Do actions when we match unit.

Returns:
Value of unit (if unit recognized), 1 if not recognized
Parameters:
unitUnit name

Definition at line 114 of file Parser.cpp.

{
  UnitsStoreT::const_iterator u = m_units.find(unit);
  if ( u==m_units.end())
  {
    addMessage
      ( Message::E_ERROR ,
        Message::C_UNITNOTFOUND,
        boost::str(boost::format("Cann't find unit \"%1%\"")%unit),pos);
    return 1;
  }
  return u->second;
}
void Gaudi::Parsers::Parser::matchUnitEntry ( const std::string newUnit,
double  value,
const Position pos 
)

Do actions when we match unit entry in units file.

Parameters:
newUnitNew unit
valuevalue of new unit
posPosition of entry

Definition at line 130 of file Parser.cpp.

{
  if(isPrint())
  {
    m_stream
      << boost::format("%4% %2%  = %3%; %|78t|%1%")
      % posString(pos.line(), pos.column())
      % newUnit
      % value
      % GPP_COMMENT
      << std::endl ;
  }
  m_units[newUnit] = value ;
}
void Gaudi::Parsers::Parser::matchUnits ( const std::string fileName,
const Position pos 
)

Do actions when units file is matched.

Parameters:
fileNameFile to include

Definition at line 109 of file Parser.cpp.

{ parseFile(fileName,pos,true).ignore(); }
const std::vector<Message>& Gaudi::Parsers::Parser::messages (  ) [inline]

Return parser messages.

Definition at line 128 of file ParserUtils.h.

{return m_messages;}
StatusCode Gaudi::Parsers::Parser::parse ( const std::string fileName )

Start parse file.

Returns:
StatusCode::SUCCESS if no errors
Parameters:
fileNameFile to parse

Definition at line 85 of file Parser.cpp.

{
  m_stream << GPP_COMMENT + std::string(80,'=') << std::endl;
  parseFile(fileName,Position()).ignore();
  resolveReferences();
  if ( m_isPrintOptions ){ printOptions(); }
  m_stream << GPP_COMMENT + std::string(80,'=') << std::endl;
  return errorsCount()==0?StatusCode::SUCCESS:StatusCode::FAILURE;
}
StatusCode Gaudi::Parsers::Parser::parseFile ( const std::string fileName,
const Position pos,
bool  isUnitFile = false 
) [private]

Parse file from another file.

Returns:
StatusCode::SUCCESS if no *syntax* errors appears
Parameters:
fileNameFile to parse Information about from what file we call parsing of file

Definition at line 289 of file Parser.cpp.

{
  StatusCode  ok;
  std::vector<std::string> sp = m_searchPath;
  if(pos.fileName().length()>0){
    // Add current file directory to search path
    sp.insert(sp.begin(),
              fs::path(pos.fileName(),fs::native).branch_path()
              .native_directory_string());
  }
  std::string fileToParse;
  ok = Gaudi::Parsers::Utils::searchFile(fileName,true,sp,fileToParse);
  if(ok.isFailure()){
    addMessage( Message::E_ERROR, Message::C_FILENOTFOUND,
               boost::str(boost::format("Couldn't find file \"%1%\"") % fileName),pos);
    return StatusCode::FAILURE;
  }

  ok = isIncluded(fileToParse);
  if(ok.isSuccess())
  {
    const std::string _msg =
      ( boost::format("Skip already included file  \"%1%\"") % fileToParse ).str() ;
    addMessage ( Message::E_WARNING , Message::C_OK , _msg , pos ) ;
    if ( isPrint() )
    {
      m_stream
        << boost::format("%3% skip already included file  \"%2%\" %|78t|%1%")
        % posString(pos.line(), pos.column())
        % fileToParse
        % GPP_COMMENT
        << std::endl ;
    }
    return StatusCode::SUCCESS;
  }
  std::string input;
  ok = Gaudi::Parsers::Utils::readFile(fileToParse,input);
  if(ok.isFailure())
  {
    addMessage
      ( Message::E_ERROR, Message::C_FILENOTOPENED,
       boost::str
       (boost::format("Couldn't open file \"%1%\"") % fileToParse),pos);
    return StatusCode::FAILURE;
  }
  m_included.push_back(fileToParse);


  IteratorT beginpos(input.begin(), input.end(), fileToParse);
  IteratorT endpos;

  boost::spirit::parse_info<IteratorT> info;
  SkipperGrammar grSkipper;

  if(!isUnitsFile){
    m_stream
      << boost::format("%3% include \"%2%\" %|78t|%1%")
      % posString(pos.line(), pos.column())
      % fileToParse
      % GPP_COMMENT
      << std::endl ;
    ParserGrammar grParser(this);
    info = boost::spirit::parse(beginpos, endpos, grParser >> end_p,grSkipper);
  }else{
    m_stream
      << boost::format("#units \"%3%\" %|72t|%2% %1%")
      % posString(pos.line(), pos.column())
      % GPP_COMMENT
      % fileToParse
      << std::endl ;
    UnitsFileGrammar grParser(this);
    info = boost::spirit::parse(beginpos, endpos, grParser >> end_p,grSkipper);
  }
  boost::spirit::file_position stoppos = info.stop.get_position();
  if (!info.full) {
    addMessage(Message::E_ERROR, Message::C_SYNTAXERROR,
               "Syntax error",Position(stoppos.file,stoppos.line,stoppos.column));
    return StatusCode::FAILURE;
  }
  {
    std::string _msg =
      ( boost::format("Parsed file \"%2%\" %|78t|%1%")
        % posString(stoppos.line, stoppos.column)
        % fileToParse ) .str() ;
    addMessage ( Message::E_VERBOSE ,
                 Message::C_OK ,
                 _msg ,
                 Position(stoppos.file,stoppos.line,stoppos.column) ) ;
    if ( isPrint() )
    {
      m_stream
        << boost::format("%3% end  \"%2%\" %|78t|%1%")
        % posString(stoppos.line, stoppos.column)
        % fileToParse
        % GPP_COMMENT
        << std::endl ;
    }
  }
  return StatusCode::SUCCESS;
}
std::string Gaudi::Parsers::Parser::posString ( int  line,
int  column 
) [private]

Convert position to string.

Returns:
string representation of position
Parameters:
lineLine
columnColumn

Definition at line 552 of file Parser.cpp.

{ return boost::str(boost::format("(%1%,%2%)") % line % column); }
void Gaudi::Parsers::Parser::printOptions (  ) [private]

Print options.

Definition at line 543 of file Parser.cpp.

void Gaudi::Parsers::Parser::resolveReferences (  ) [private]

Resolve references.

Definition at line 441 of file Parser.cpp.

{
  Catalogue::CatalogueT cat = m_catalogue.catalogue();
  // ----------------------------------------------------------------------------
  for( Catalogue::CatalogueT::const_iterator curObj = cat.begin();
       curObj!=cat.end();curObj++)
  {
    std::string objName = curObj->first;
    // ------------------------------------------------------------------------
    for( std::vector<PropertyEntry>::const_iterator curProp =
           curObj->second.begin();curProp != curObj->second.end(); curProp++)
    {
      std::string value = curProp->value();
      if ( (value.length()>0) && (value[0]=='@'))
      {
        // --------------------------------------------------------------------
        std::vector<std::string> objAndProp;
        std::string refprop(value.begin()+1,value.end());
        ba::split(objAndProp,
                  refprop,
                  ba::is_any_of("."));
        PropertyEntry foundProperty;
        StatusCode ok;
        ok = m_catalogue.findProperty(objAndProp[0],objAndProp[1],
                                      foundProperty);
        if(ok.isFailure())
        {
          addMessage
            ( Message::E_ERROR,
              Message::C_PROPERTYNOTFOUND,
              boost::str(boost::format("Cannot find property \"%1%.%2%\"")
                         % objAndProp[0] % objAndProp[1]),curProp->position());
        }
        else
        {
          // -------------------------------------------------------------------
          if((ba::to_lower_copy(objAndProp[0]) == objName)
             &&
             (ba::to_lower_copy(objAndProp[1])
              == curProp->name()))
          {
            // ----------------------------------------------------------------
            addMessage
              ( Message::E_ERROR,
                Message::C_BADREFERENCE,
                boost::str(boost::format("Reference to self \"%1%.%2%\"")
                           % objAndProp[0] % objAndProp[1]),curProp->position());
            // ----------------------------------------------------------------
          }
          else
          {
            PropertyEntry property = foundProperty;
            property.setName(curProp->name());
            m_catalogue.addProperty(objName,property);
          }
          // ------------------------------------------------------------------
        }
        // --------------------------------------------------------------------
      }
    }
    // ------------------------------------------------------------------------
  }
}
void Gaudi::Parsers::Parser::setIsPrint ( bool  on,
const Position pos 
)

Control printing.

Definition at line 244 of file Parser.cpp.

{
  // ignore the printout if the full print is activated
  if ( on && m_isPrintOptions ) { return ; }
  m_isPrint = on;
  m_stream
    << boost::format("%3% printing is %2% %|78t|%1%")
    % posString(pos.line(),pos.column())
    % (on?"ON":"OFF")
    % GPP_COMMENT
    << std::endl ;
}
void Gaudi::Parsers::Parser::setIsPrintOptions ( bool  on,
const Position pos 
)

Control printing options.

Definition at line 258 of file Parser.cpp.

{
  m_isPrintOptions = on;
  m_stream
    << boost::format ("%3% printing options is %2% %|78t|%1%")
    % posString(pos.line(),pos.column())
    % (on?"ON":"OFF")
    % GPP_COMMENT
    << std::endl ;
  // deactivate the printout if the print of all options is activated
  if ( m_isPrintOptions && m_isPrint ) { setIsPrint ( false , pos ) ; }
}
std::string Gaudi::Parsers::Parser::severityName ( Message::Severity  severity ) [private]

String representation of severity.

Returns:
Representation of severity
Parameters:
severitySeverity

Definition at line 393 of file Parser.cpp.

                          {
  switch(severity)
  {
  case Message::E_ERROR   :
    return "ERROR"     ;
  case Message::E_WARNING :
    return "WARNING"   ;
  case Message::E_NOTICE  :
    return "NOTICE"    ;
  case Message::E_VERBOSE :
    return "VERBOSE"   ;
  default:
    return "UNDEFINED" ;
  }
}
std::string Gaudi::Parsers::Parser::sign ( Sign  aSign ) [private]

String representation of sign.

Definition at line 507 of file Parser.cpp.

{
  switch(aSign)
  {
  case S_ASSIGN:
    return "=";
  case S_PLUSASSIGN:
    return "+=";
  case S_MINUSASSIGN:
    return "-=";
  default:
    return "unknown_operation";
  }
}
UnitsStoreT& Gaudi::Parsers::Parser::units ( void   ) [inline]

Definition at line 131 of file ParserUtils.h.

{return m_units;}
std::string Gaudi::Parsers::Parser::valueToString ( std::vector< std::string value,
bool  isVector 
) [private]

String representation of value vector.

Definition at line 525 of file Parser.cpp.

{
  if ( !isVector){ return value[0]; }
  //
  std::string result;
  std::string delim;
  result+=" [ ";
  for ( std::vector<std::string>::const_iterator cur = value.begin();
        cur != value.end(); cur++ )
  {
    result += delim + *cur;
    delim = " , ";
  }
  return result + " ] ";
}

Member Data Documentation

Catalogue& Gaudi::Parsers::Parser::m_catalogue [private]

Definition at line 178 of file ParserUtils.h.

std::vector<std::string>& Gaudi::Parsers::Parser::m_included [private]

Definition at line 179 of file ParserUtils.h.

bool Gaudi::Parsers::Parser::m_isPrint [private]

Definition at line 175 of file ParserUtils.h.

bool Gaudi::Parsers::Parser::m_isPrintOptions [private]

Definition at line 176 of file ParserUtils.h.

MessagesStoreT Gaudi::Parsers::Parser::m_messages [private]

Definition at line 180 of file ParserUtils.h.

std::vector<std::string> Gaudi::Parsers::Parser::m_searchPath [private]

Definition at line 177 of file ParserUtils.h.

std::ostream& Gaudi::Parsers::Parser::m_stream [private]

Definition at line 182 of file ParserUtils.h.

UnitsStoreT Gaudi::Parsers::Parser::m_units [private]

Definition at line 181 of file ParserUtils.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Tue May 10 2011 18:55:29 for Gaudi Framework, version v22r2 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004