1 | // $Id: Parser.cc 519 2007-12-23 20:14:50Z jari $ |
---|
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 | |
---|
37 | namespace theplu{ |
---|
38 | namespace svndigest{ |
---|
39 | |
---|
40 | |
---|
41 | Parser::Parser(std::string path) |
---|
42 | { |
---|
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(); |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | void Parser::parse(std::istream& is, |
---|
56 | const std::vector<std::pair<std::string, std::string> >& |
---|
57 | codon) |
---|
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) { |
---|
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 | type_.push_back(lt); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
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 | |
---|
115 | }} // end of namespace svndigest and namespace theplu |
---|