source: trunk/lib/Parser.cc @ 536

Last change on this file since 536 was 536, checked in by Peter Johansson, 14 years ago

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

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: Parser.cc 536 2007-12-27 03:32:43Z peter $
2
3/*
4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://trac.thep.lu.se/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Parser.h"
25#include "Configuration.h"
26#include "utility.h"
27
28#include <algorithm>
29#include <cassert>
30#include <functional>
31#include <fstream>
32#include <iostream>
33#include <string>
34#include <utility>
35#include <vector>
36
37namespace theplu{
38namespace svndigest{
39
40
41  Parser::Parser(std::string path)
42    : mode_(0)
43  {
44    codon_ = Configuration::instance().codon(path); 
45  }
46
47
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)
60  {
61    // mode zero means we are currently not in a comment
62    // if mode!=0 comment is closed by codon[mode-1].second -> mode=0
63    // if codon[x-1].start is found and x >= mode -> mode=x
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;
72            break;
73          }
74          if (match_begin(iter, str.end(), (*codon_)[i].first)) {
75            iter += (*codon_)[i].first.size();
76            mode_ = i+1;
77            break;
78          }
79        }
80        if (iter==str.end())
81          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;
86          if (iter==str.end())
87            break;
88        }
89        else if (!mode_ && isgraph(*iter))
90          lt=code;
91        else if (mode_ && lt!=code && isalnum(*iter))
92          lt=comment;
93      }
94      if (mode_ && (*codon_)[mode_-1].second==std::string("\n"))
95        mode_=0;
96    }
97    return lt;
98  }
99
100
101  Parser::line_type Parser::text_mode(const std::string& str)
102  {
103    for (std::string::const_iterator iter=str.begin(); iter!=str.end(); ++iter)
104      if (isalnum(*iter))
105        return comment;
106    return other;
107  }
108
109
110}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.