Changeset 536


Ignore:
Timestamp:
Dec 27, 2007, 4:32:43 AM (13 years ago)
Author:
Peter Johansson
Message:

refs #289 - Parser now is fed with lines and does not read file during construction.

Location:
trunk/lib
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/BlameStats.cc

    r531 r536  
    6565  {
    6666    Parser parser(path);
    67     std::vector<Parser::line_type>::const_iterator count=parser.type().begin();
    68 
    6967    SVNblame svn_blame(path);
    7068    while (svn_blame.valid()) {
    71       add(svn_blame.author(), svn_blame.revision(), *count);
     69      add(svn_blame.author(), svn_blame.revision(),
     70          parser.parse(svn_blame.line()));
    7271      svn_blame.next_line();
    73       ++count;
    7472    }
    7573  }
  • trunk/lib/ClassicStats.cc

    r531 r536  
    6565  {
    6666    Parser parser(path);
    67     std::vector<Parser::line_type>::const_iterator count=parser.type().begin();
    68 
    6967    SVNblame svn_blame(path);
    7068    while (svn_blame.valid()) {
    71       add(svn_blame.author(), svn_blame.revision(), *count);
     69      add(svn_blame.author(), svn_blame.revision(),
     70          parser.parse(svn_blame.line()));
    7271      svn_blame.next_line();
    73       ++count;
    7472    }
    7573  }
  • trunk/lib/File.cc

    r532 r536  
    226226    SVNblame blame(path_);
    227227    Parser parser(path_);
     228    while (blame.valid()) {
     229      parser.parse(blame.line());
     230      blame.next_line();
     231    }
     232    blame.reset();
     233
    228234    std::vector<Parser::line_type>::const_iterator
    229235      line_type(parser.type().begin());
  • trunk/lib/Parser.cc

    r528 r536  
    4040
    4141  Parser::Parser(std::string path)
     42    : mode_(0)
    4243  {
    43     std::ifstream is(path.c_str());
    44     assert(is.good());
    45     const std::vector<std::pair<std::string, std::string> >* codon =
    46       Configuration::instance().codon(path);
    47     if (codon)
    48       parse(is, *codon);
    49     else
    50       text_mode(is);
    51     is.close();
     44    codon_ = Configuration::instance().codon(path);
    5245  }
    5346
    5447
    55   void Parser::parse(std::istream& is,
    56                      const std::vector<std::pair<std::string, std::string> >&
    57                      codon)
     48  Parser::line_type Parser::parse(const std::string& line)
     49  {
     50    if (codon_)
     51      type_.push_back(code_mode(line));
     52    else
     53      type_.push_back(text_mode(line));
     54    return type_.back();
     55                     
     56  }
     57
     58
     59  Parser::line_type Parser::code_mode(const std::string& str)
    5860  {
    5961    // mode zero means we are currently not in a comment
    6062    // if mode!=0 comment is closed by codon[mode-1].second -> mode=0
    6163    // if codon[x-1].start is found and x >= mode -> mode=x
    62     size_t mode = 0;
    63     std::string str;
    64     while(getline(is,str)) {
    65       line_type lt=other;
    66       for (std::string::iterator iter=str.begin(); iter!=str.end(); ++iter){
    67         for (size_t i=mode; i<codon.size(); ++i) {
    68           if ( iter==str.begin() && codon[i].first[0] == '\n' &&
    69                match_begin(iter, str.end(), codon[i].first.substr(1)) ) {
    70             iter += codon[i].first.size()-1;
    71             mode = i+1;
     64    line_type lt=other;
     65    {
     66      for (std::string::const_iterator iter=str.begin();iter!=str.end();++iter){
     67        for (size_t i=mode_; i<codon_->size(); ++i) {
     68          if ( iter==str.begin() && (*codon_)[i].first[0] == '\n' &&
     69               match_begin(iter, str.end(), (*codon_)[i].first.substr(1)) ) {
     70            iter += (*codon_)[i].first.size()-1;
     71            mode_ = i+1;
    7272            break;
    7373          }
    74           if (match_begin(iter, str.end(), codon[i].first)) {
    75             iter += codon[i].first.size();
    76             mode = i+1;
     74          if (match_begin(iter, str.end(), (*codon_)[i].first)) {
     75            iter += (*codon_)[i].first.size();
     76            mode_ = i+1;
    7777            break;
    7878          }
     
    8080        if (iter==str.end())
    8181          break;
    82         assert(mode==0 || mode-1<codon.size());
    83         if (mode && match_begin(iter,str.end(), codon[mode-1].second)){
    84           iter += codon[mode-1].second.size();
    85           mode=0;
     82        assert(mode_==0 || mode_-1<(*codon_).size());
     83        if (mode_ && match_begin(iter,str.end(), (*codon_)[mode_-1].second)){
     84          iter += (*codon_)[mode_-1].second.size();
     85          mode_=0;
    8686          if (iter==str.end())
    8787            break;
    8888        }
    89         else if (!mode && isgraph(*iter))
     89        else if (!mode_ && isgraph(*iter))
    9090          lt=code;
    91         else if (mode && lt!=code && isalnum(*iter))
     91        else if (mode_ && lt!=code && isalnum(*iter))
    9292          lt=comment;
    9393      }
    94       if (mode && codon[mode-1].second==std::string("\n"))
    95         mode=0;
    96       type_.push_back(lt);
     94      if (mode_ && (*codon_)[mode_-1].second==std::string("\n"))
     95        mode_=0;
    9796    }
     97    return lt;
    9898  }
    9999
    100100
    101   void Parser::text_mode(std::istream& is)
     101  Parser::line_type Parser::text_mode(const std::string& str)
    102102  {
    103     std::string str;
    104     while(getline(is,str)) {
    105       line_type lt=other;
    106       for (std::string::iterator iter=str.begin(); iter!=str.end(); ++iter){
    107         if (lt==other && isalnum(*iter))
    108           lt = comment;
    109       }
    110       type_.push_back(lt);
    111     }
     103    for (std::string::const_iterator iter=str.begin(); iter!=str.end(); ++iter)
     104      if (isalnum(*iter))
     105        return comment;
     106    return other;
    112107  }
    113108
  • trunk/lib/Parser.h

    r528 r536  
    3333namespace svndigest{
    3434
    35   ///
    36   /// Class parsing files
    37   ///
     35  /**
     36     \brief Class parsing files
     37     
     38     Class gets parsing rules from Configuration. Which rules depends
     39     on the filename. Typical use is then to add lines using the add
     40     function, and the class builds an internal vector<line_type>,
     41     which can be accessed through function type(void).
     42  */
    3843  class Parser
    3944  {
    4045  public:
    4146    ///
    42     /// A line is considered empty if it only contains spaces and tabs.
    43     ///
    44     /// A line is considered a comment if first and second non-white
    45     /// space characters are '/'.
    46     ///
    47     /// All other lines are considered code.
     47    /// see 'doc/readmea.txt' for info on what is code, comment, and other.
    4848    ///
    4949    enum line_type {
     
    5555    ///
    5656    /// @brief Constructor
     57    /// \param filename is used to decide which parsing rules to use
    5758    ///
    58     explicit Parser(std::string);
     59    explicit Parser(std::string filename);
     60
     61    /**
     62       \brief add a line to parser
     63
     64       \return line_type of parsed line
     65
     66       The line is parsed and added to internal vector. 
     67     */
     68    line_type parse(const std::string& line);
    5969
    6070    ///
     
    6474
    6575  private:
    66     ///
    67     /// Copy constructor (not implemented)
    68     ///
     76    // no copy allowed
    6977    Parser(const Parser& other);
     78    Parser& operator=(const Parser&);
    7079
    71     void parse(std::istream& is,
    72                const std::vector<std::pair<std::string, std::string> >& codon);
     80    line_type code_mode(const std::string& line);
     81    line_type text_mode(const std::string& line);
    7382
    74     void text_mode(std::istream&);
    75 
     83    size_t mode_;
     84   
    7685    std::vector<line_type> type_;
    77 
     86    const std::vector<std::pair<std::string, std::string> >* codon_;
    7887  };
    7988
Note: See TracChangeset for help on using the changeset viewer.