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
);
88 if (_current_token
== Tokens::FILE_TOK
){
91 }else if (_current_token
== Tokens::HUNK_TOK
){
92 _changeState(HUNK_STATE
);
100 switch (_current_token
){
101 case Tokens::HUNK_TOK
:
105 case Tokens::ADDED_TOK
:
106 _changeState(ADDED_STATE
);
108 case Tokens::DELETED_TOK
:
109 _changeState(DELETED_STATE
);
111 case Tokens::CONTEXT_TOK
:
112 _changeState(CONTEXT_STATE
);
119 void Parser::_context()
121 if (_current_token
== Tokens::CONTEXT_TOK
){
122 _addCurrentLineToContext();
127 switch (_current_token
){
128 case Tokens::FILE_TOK
:
130 _changeState(FILE_STATE
);
132 case Tokens::HUNK_TOK
:
134 _changeState(HUNK_STATE
);
136 case Tokens::ADDED_TOK
:
137 _changeState(ADDED_STATE
);
139 case Tokens::DELETED_TOK
:
140 _changeState(DELETED_STATE
);
143 _changeState(END_STATE
);
148 void Parser::_added()
150 if (_current_token
== Tokens::ADDED_TOK
){
151 _addCurrentLineToAdded();
153 }else if (_current_token
== Tokens::DELETED_TOK
){
154 _changeState(CHANGED_STATE
);
158 switch (_current_token
){
159 case Tokens::FILE_TOK
:
161 _changeState(FILE_STATE
);
163 case Tokens::HUNK_TOK
:
165 _changeState(HUNK_STATE
);
167 case Tokens::CONTEXT_TOK
:
168 _changeState(CONTEXT_STATE
);
171 _changeState(END_STATE
);
176 void Parser::_deleted()
178 if (_current_token
== Tokens::DELETED_TOK
){
179 _addCurrentLineToDeleted();
181 }else if (_current_token
== Tokens::ADDED_TOK
){
182 _changeState(CHANGED_STATE
);
186 switch (_current_token
){
187 case Tokens::FILE_TOK
:
189 _changeState(FILE_STATE
);
191 case Tokens::HUNK_TOK
:
193 _changeState(HUNK_STATE
);
195 case Tokens::CONTEXT_TOK
:
196 _changeState(CONTEXT_STATE
);
199 _changeState(END_STATE
);
204 void Parser::_changed()
206 if (_current_token
== Tokens::DELETED_TOK
){
207 _addCurrentLineToDeleted();
209 }else if (_current_token
== Tokens::ADDED_TOK
){
210 _addCurrentLineToAdded();
215 switch (_current_token
){
216 case Tokens::FILE_TOK
:
218 _changeState(FILE_STATE
);
220 case Tokens::HUNK_TOK
:
222 _changeState(HUNK_STATE
);
224 case Tokens::CONTEXT_TOK
:
225 _changeState(CONTEXT_STATE
);
228 _changeState(END_STATE
);
242 _changeState(END_STATE
);
247 void Parser::_readNextLine()
249 _current_line
= _in
->readLine();
250 _current_token
= _tokens
->match(_current_line
);
251 DBG("Read line \"" << _current_line
.toStdString() << "\"");
254 void Parser::_changeState(Parser::states new_state
)
256 _current_state
= new_state
;
259 switch (_current_state
){
261 state
= "START_STATE";
264 state
= "FILE_STATE";
267 state
= "HUNK_STATE";
270 state
= "CONTEXT_STATE";
273 state
= "ADDED_STATE";
276 state
= "DELETED_STATE";
279 state
= "CHANGED_STATE";
285 DBG("State changed to " << state
);
290 QString
Parser::_capCurrentLine(int cap
)
292 switch (_current_token
){
293 case Tokens::FILE_TOK
:
294 return _tokens
->file_tok
.cap(cap
);
295 case Tokens::HUNK_TOK
:
296 return _tokens
->hunk_tok
.cap(cap
);
297 case Tokens::CONTEXT_TOK
:
298 return _tokens
->context_tok
.cap(cap
);
299 case Tokens::ADDED_TOK
:
300 return _tokens
->added_tok
.cap(cap
);
301 case Tokens::DELETED_TOK
:
302 return _tokens
->deleted_tok
.cap(cap
);
303 case Tokens::NONE_TOK
:
309 void Parser::_createNewFile()
311 if (_cur_file
!= NULL
)
314 Diff::instance()->addFile(_cur_file
= new File(_capCurrentLine(1)));
316 void Parser::_finishFile()
321 void Parser::_createNewHunk()
323 if (_cur_file
== NULL
|| _cur_hunk
!= NULL
)
327 from1
= _capCurrentLine(1).toInt();
328 from2
= _capCurrentLine(2).toInt();
330 _cur_file
->addHunk(_cur_hunk
= new Hunk(from1
, from2
));
332 void Parser::_finishHunk()
337 void Parser::_addCurrentLineToContext()
339 if (_cur_context
== NULL
)
340 _cur_context
= new Text();
341 _cur_context
->addLine(new QString(_current_line
.remove(0,1)));
343 void Parser::_finishContext()
345 if (_cur_context
== NULL
|| _cur_hunk
== NULL
)
347 _cur_hunk
->addSnippet(new Context(_cur_context
));
351 void Parser::_addCurrentLineToAdded()
353 if (_cur_added
== NULL
)
354 _cur_added
= new Text();
355 _cur_added
->addLine(new QString(_current_line
.remove(0,1)));
357 void Parser::_finishAdded()
359 if (_cur_added
== NULL
|| _cur_hunk
== NULL
){
362 _cur_hunk
->addSnippet(new Added(_cur_added
));
366 void Parser::_addCurrentLineToDeleted()
368 if (_cur_deleted
== NULL
)
369 _cur_deleted
= new Text();
370 _cur_deleted
->addLine(new QString(_current_line
.remove(0,1)));
372 void Parser::_finishDeleted()
374 if (_cur_deleted
== NULL
|| _cur_hunk
== NULL
){
377 _cur_hunk
->addSnippet(new Deleted(_cur_deleted
));
381 void Parser::_finishChanged()
383 if (_cur_deleted
== NULL
|| _cur_added
== NULL
|| _cur_hunk
== NULL
){
386 _cur_hunk
->addSnippet(new Changed(_cur_deleted
, _cur_added
));
394 Tokens
*factory(string type
)
398 ret
= new TokensGit();
399 DBG("factory(" << type
<< ") - ret: " << (long)ret
);