source: trunk/lib/Parser.cc @ 514

Last change on this file since 514 was 514, checked in by Peter Johansson, 15 years ago

Fixing #230

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1// $Id: Parser.cc 514 2007-12-09 01:51:07Z 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/trac/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  {
43    std::ifstream is(path.c_str());
44    assert(is.good());
45    // Ignore trailing '.in' in file names
46    if (match_end(path.rbegin(), path.rend(), ".in"))
47      path = path.substr(0, path.size()-3);
48    const std::vector<std::pair<std::string, std::string> >* codon = 
49      Configuration::instance().codon(path); 
50    if (codon)
51      parse(is, *codon);
52    else
53      text_mode(is);
54    is.close();
55  }
56
57
58  void Parser::parse(std::istream& is,
59                     const std::vector<std::pair<std::string, std::string> >& 
60                     codon)
61  {
62    // mode zero means we are currently not in a comment
63    // if mode!=0 comment is closed by codon[mode-1].second -> mode=0
64    // if codon[x-1].start is found and x >= mode -> mode=x
65    size_t mode = 0;
66    std::string str;
67    while(getline(is,str)) {
68      line_type lt=empty;
69      for (std::string::iterator iter=str.begin(); iter!=str.end(); ++iter){
70        for (size_t i=mode; i<codon.size(); ++i) {
71          if ( iter==str.begin() && codon[i].first[0] == '\n' && 
72               match_begin(iter, str.end(), codon[i].first.substr(1)) ) {
73            iter += codon[i].first.size()-1;
74            mode = i+1;
75            break;
76          }
77          if (match_begin(iter, str.end(), codon[i].first)) {
78            iter += codon[i].first.size();
79            mode = i+1;
80            break;
81          }
82        }
83        if (iter==str.end())
84          break;
85        assert(mode==0 || mode-1<codon.size());
86        if (mode && match_begin(iter,str.end(), codon[mode-1].second)){
87          iter += codon[mode-1].second.size();
88          mode=0;
89          if (iter==str.end())
90            break;
91        }
92        else if (!mode && isgraph(*iter))
93          lt=code;
94        else if (mode && lt!=code && isalnum(*iter))
95          lt=comment;
96      }
97      if (mode && codon[mode-1].second==std::string("\n"))
98        mode=0;
99      type_.push_back(lt);
100    }
101  }
102
103
104  void Parser::text_mode(std::istream& is)
105  {
106    std::string str;
107    while(getline(is,str)) {
108      line_type lt=empty;
109      for (std::string::iterator iter=str.begin(); iter!=str.end(); ++iter){
110        if (lt==empty && isalnum(*iter))
111          lt = comment;
112      }
113      type_.push_back(lt);
114    }
115  }
116
117
118}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.