2 #include "../diff/diff.h"
6 Tokens::Tokens(const char *f_tok
,
17 Tokens::token
Tokens::match(QString
&line
) const
19 if (file_tok
.indexIn(line
) != -1){
21 }else if (hunk_tok
.indexIn(line
) != -1){
23 }else if (context_tok
.indexIn(line
) != -1){
25 }else if (added_tok
.indexIn(line
) != -1){
27 }else if (deleted_tok
.indexIn(line
) != -1){
34 TokensGit::TokensGit() : Tokens::Tokens("^diff --git a/([^ ]+) b/.*$",
35 "^@@ -([0-9]+),.* \\+([0-9]+),.*$",
47 while (_current_state
!= END_STATE
){
48 switch (_current_state
){
79 if (_current_token
== Tokens::FILE_TOK
){
80 _changeState(FILE_STATE
);
90 if (_current_token
== Tokens::FILE_TOK
){
93 }else if (_current_token
== Tokens::HUNK_TOK
){
94 _changeState(HUNK_STATE
);
102 switch (_current_token
){
103 case Tokens::HUNK_TOK
:
107 case Tokens::ADDED_TOK
:
108 _changeState(ADDED_STATE
);
110 case Tokens::DELETED_TOK
:
111 _changeState(DELETED_STATE
);
113 case Tokens::CONTEXT_TOK
:
114 _changeState(CONTEXT_STATE
);
121 void Parser::_context()
123 if (_current_token
== Tokens::CONTEXT_TOK
){
124 _addCurrentLineToContext();
129 switch (_current_token
){
130 case Tokens::FILE_TOK
:
132 _changeState(FILE_STATE
);
134 case Tokens::HUNK_TOK
:
136 _changeState(HUNK_STATE
);
138 case Tokens::ADDED_TOK
:
139 _changeState(ADDED_STATE
);
141 case Tokens::DELETED_TOK
:
142 _changeState(DELETED_STATE
);
145 _changeState(END_STATE
);
150 void Parser::_added()
152 if (_current_token
== Tokens::ADDED_TOK
){
153 _addCurrentLineToAdded();
155 }else if (_current_token
== Tokens::DELETED_TOK
){
156 _changeState(CHANGED_STATE
);
160 switch (_current_token
){
161 case Tokens::FILE_TOK
:
163 _changeState(FILE_STATE
);
165 case Tokens::HUNK_TOK
:
167 _changeState(HUNK_STATE
);
169 case Tokens::CONTEXT_TOK
:
170 _changeState(CONTEXT_STATE
);
173 _changeState(END_STATE
);
178 void Parser::_deleted()
180 if (_current_token
== Tokens::DELETED_TOK
){
181 _addCurrentLineToDeleted();
183 }else if (_current_token
== Tokens::ADDED_TOK
){
184 _changeState(CHANGED_STATE
);
188 switch (_current_token
){
189 case Tokens::FILE_TOK
:
191 _changeState(FILE_STATE
);
193 case Tokens::HUNK_TOK
:
195 _changeState(HUNK_STATE
);
197 case Tokens::CONTEXT_TOK
:
198 _changeState(CONTEXT_STATE
);
201 _changeState(END_STATE
);
206 void Parser::_changed()
208 if (_current_token
== Tokens::DELETED_TOK
){
209 _addCurrentLineToDeleted();
211 }else if (_current_token
== Tokens::ADDED_TOK
){
212 _addCurrentLineToAdded();
217 switch (_current_token
){
218 case Tokens::FILE_TOK
:
220 _changeState(FILE_STATE
);
222 case Tokens::HUNK_TOK
:
224 _changeState(HUNK_STATE
);
226 case Tokens::CONTEXT_TOK
:
227 _changeState(CONTEXT_STATE
);
230 _changeState(END_STATE
);
244 _changeState(END_STATE
);
249 void Parser::_readNextLine()
251 _current_line
= _in
->readLine();
252 if (_current_line
.isNull()){
253 _changeState(END_STATE
);
257 _current_token
= _tokens
->match(_current_line
);
258 DBG("Read line \"" << _current_line
.toStdString() << "\"");
261 void Parser::_changeState(Parser::states new_state
)
263 _current_state
= new_state
;
266 switch (_current_state
){
268 state
= "START_STATE";
271 state
= "FILE_STATE";
274 state
= "HUNK_STATE";
277 state
= "CONTEXT_STATE";
280 state
= "ADDED_STATE";
283 state
= "DELETED_STATE";
286 state
= "CHANGED_STATE";
292 DBG("State changed to " << state
);
297 QString
Parser::_capCurrentLine(int cap
)
299 switch (_current_token
){
300 case Tokens::FILE_TOK
:
301 return _tokens
->file_tok
.cap(cap
);
302 case Tokens::HUNK_TOK
:
303 return _tokens
->hunk_tok
.cap(cap
);
304 case Tokens::CONTEXT_TOK
:
305 return _tokens
->context_tok
.cap(cap
);
306 case Tokens::ADDED_TOK
:
307 return _tokens
->added_tok
.cap(cap
);
308 case Tokens::DELETED_TOK
:
309 return _tokens
->deleted_tok
.cap(cap
);
310 case Tokens::NONE_TOK
:
316 void Parser::_createNewFile()
318 if (_cur_file
!= NULL
){
319 DBG("Can't create new file - _cur_file = " << (long)_cur_file
);
323 Diff::instance()->addFile(_cur_file
= new File(_capCurrentLine(1)));
325 void Parser::_finishFile()
330 void Parser::_createNewHunk()
332 if (_cur_file
== NULL
|| _cur_hunk
!= NULL
){
333 DBG("Can't create new hunk - _cur_hunk = " << (long)_cur_hunk
334 << ", _cur_file = " << (long)_cur_file
);
339 from1
= _capCurrentLine(1).toInt();
340 from2
= _capCurrentLine(2).toInt();
342 _cur_file
->addHunk(_cur_hunk
= new Hunk(from1
, from2
));
344 void Parser::_finishHunk()
349 void Parser::_addCurrentLineToContext()
351 if (_cur_context
== NULL
)
352 _cur_context
= new Text();
353 _cur_context
->addLine(new QString(_current_line
.remove(0,1)));
355 void Parser::_finishContext()
357 if (_cur_context
== NULL
|| _cur_hunk
== NULL
)
359 _cur_hunk
->addSnippet(new Context(_cur_context
));
363 void Parser::_addCurrentLineToAdded()
365 if (_cur_added
== NULL
)
366 _cur_added
= new Text();
367 _cur_added
->addLine(new QString(_current_line
.remove(0,1)));
369 void Parser::_finishAdded()
371 if (_cur_added
== NULL
|| _cur_hunk
== NULL
){
374 _cur_hunk
->addSnippet(new Added(_cur_added
));
378 void Parser::_addCurrentLineToDeleted()
380 if (_cur_deleted
== NULL
)
381 _cur_deleted
= new Text();
382 _cur_deleted
->addLine(new QString(_current_line
.remove(0,1)));
384 void Parser::_finishDeleted()
386 if (_cur_deleted
== NULL
|| _cur_hunk
== NULL
){
389 _cur_hunk
->addSnippet(new Deleted(_cur_deleted
));
393 void Parser::_finishChanged()
395 if (_cur_deleted
== NULL
|| _cur_added
== NULL
|| _cur_hunk
== NULL
){
398 _cur_hunk
->addSnippet(new Changed(_cur_deleted
, _cur_added
));
406 Tokens
*factory(string type
)
410 ret
= new TokensGit();
411 DBG("factory(" << type
<< ") - ret: " << (long)ret
);