source: trunk/lib/Parser.cc @ 512

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

remove devel printout

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1// $Id: Parser.cc 512 2007-12-08 23:29:36Z 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> >& codon)
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    size_t mode = 0;
65    std::string str;
66    while(getline(is,str)) {
67      line_type lt=empty;
68      for (std::string::iterator iter=str.begin(); iter!=str.end(); ++iter){
69        for (size_t i=mode; i<codon.size(); ++i) {
70          if (match_begin(iter, str.end(), codon[i].first)) {
71            iter += codon[i].first.size();
72            mode = i+1;
73            break;
74          }
75        }
76        if (iter==str.end())
77          break;
78        assert(mode==0 || mode-1<codon.size());
79        if (mode && match_begin(iter,str.end(), codon[mode-1].second)){
80          iter += codon[mode-1].second.size();
81          mode=0;
82          if (iter==str.end())
83            break;
84        }
85        else if (!mode && isgraph(*iter))
86          lt=code;
87        else if (mode && lt!=code && isalnum(*iter))
88          lt=comment;
89      }
90      if (mode && codon[mode-1].second==std::string("\n"))
91        mode=0;
92      type_.push_back(lt);
93    }
94  }
95
96
97  void Parser::text_mode(std::istream& is)
98  {
99    std::string str;
100    while(getline(is,str)) {
101      line_type lt=empty;
102      for (std::string::iterator iter=str.begin(); iter!=str.end(); ++iter){
103        if (lt==empty && isalnum(*iter))
104          lt = comment;
105      }
106      type_.push_back(lt);
107    }
108  }
109
110
111}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.