source: trunk/lib/Parser.cc @ 519

Last change on this file since 519 was 519, checked in by Jari Häkkinen, 14 years ago

trac moved to new location.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.0 KB
RevLine 
[103]1// $Id: Parser.cc 519 2007-12-23 20:14:50Z jari $
2
3/*
[408]4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
[103]5
[519]6  This file is part of svndigest, http://trac.thep.lu.se/svndigest
[103]7
[149]8  svndigest is free software; you can redistribute it and/or modify it
[103]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
[149]13  svndigest is distributed in the hope that it will be useful, but
[103]14  WITHOUT ANY WARRANTY; without even the implied warranty of
[149]15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
[103]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"
[512]25#include "Configuration.h"
[118]26#include "utility.h"
[103]27
28#include <algorithm>
[299]29#include <cassert>
[103]30#include <functional>
31#include <fstream>
32#include <iostream>
[118]33#include <string>
[261]34#include <utility>
35#include <vector>
[103]36
37namespace theplu{
[149]38namespace svndigest{
[103]39
40
[339]41  Parser::Parser(std::string path)
[103]42  {
[118]43    std::ifstream is(path.c_str());
[265]44    assert(is.good());
[512]45    const std::vector<std::pair<std::string, std::string> >* codon = 
46      Configuration::instance().codon(path); 
47    if (codon)
48      parse(is, *codon);
[261]49    else
50      text_mode(is);
51    is.close();
[234]52  }
53
54
[260]55  void Parser::parse(std::istream& is,
[514]56                     const std::vector<std::pair<std::string, std::string> >& 
57                     codon)
[260]58  {
59    // mode zero means we are currently not in a comment
60    // if mode!=0 comment is closed by codon[mode-1].second -> mode=0
61    // 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=empty;
66      for (std::string::iterator iter=str.begin(); iter!=str.end(); ++iter){
67        for (size_t i=mode; i<codon.size(); ++i) {
[514]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          }
[260]74          if (match_begin(iter, str.end(), codon[i].first)) {
[506]75            iter += codon[i].first.size();
[260]76            mode = i+1;
77            break;
78          }
79        }
[506]80        if (iter==str.end())
81          break;
[265]82        assert(mode==0 || mode-1<codon.size());
83        if (mode && match_begin(iter,str.end(), codon[mode-1].second)){
[506]84          iter += codon[mode-1].second.size();
[260]85          mode=0;
[506]86          if (iter==str.end())
87            break;
[260]88        }
[506]89        else if (!mode && isgraph(*iter))
[505]90          lt=code;
91        else if (mode && lt!=code && isalnum(*iter))
92          lt=comment;
[260]93      }
[265]94      if (mode && codon[mode-1].second==std::string("\n"))
[260]95        mode=0;
96      type_.push_back(lt);
97    }
98  }
99
100
[218]101  void Parser::text_mode(std::istream& is)
102  {
103    std::string str;
104    while(getline(is,str)) {
105      line_type lt=empty;
106      for (std::string::iterator iter=str.begin(); iter!=str.end(); ++iter){
107        if (lt==empty && isalnum(*iter))
108          lt = comment;
109      }
110      type_.push_back(lt);
111    }
112  }
113
114
[149]115}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.