File: engine-parser.cpp

package info (click to toggle)
anjuta 2%3A2.32.0.0-5
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 61,524 kB
  • ctags: 29,402
  • sloc: ansic: 207,516; xml: 31,169; cpp: 11,403; sh: 10,749; perl: 7,107; makefile: 3,373; yacc: 1,018; lex: 126; sql: 97; python: 91; java: 6
file content (833 lines) | stat: -rw-r--r-- 23,804 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * anjuta
 * Copyright (C) Eran Ifrah (Main file for CodeLite www.codelite.org/ )
 * Copyright (C) Massimo Cora' 2009 <maxcvs@email.it>
 * 
 * anjuta is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * anjuta is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <libanjuta/anjuta-debug.h>
#include <string>
#include <vector>
#include <libanjuta/interfaces/ianjuta-symbol-query.h>
#include <libanjuta/interfaces/ianjuta-symbol-manager.h>

#include "engine-parser-priv.h"
#include "engine-parser.h"
#include "flex-lexer-klass-tab.h"
#include "expression-parser.h"
#include "scope-parser.h"
#include "variable-parser.h"
#include "function-parser.h"



using namespace std;

/* Singleton pattern. */
EngineParser* 
EngineParser::getInstance ()
{
	if (s_engine == NULL)
	{
		s_engine = new EngineParser ();
	}

	return s_engine;
}

EngineParser::EngineParser ()
{	
	_main_tokenizer = new CppTokenizer ();	
	_extra_tokenizer = new CppTokenizer ();	
}

EngineParser::~EngineParser ()
{
	delete _main_tokenizer;
	delete _extra_tokenizer;
}

bool 
EngineParser::nextMainToken (string &out_token, string &out_delimiter)
{
	out_token.clear ();
	
	int type(0);
	int depth(0);
	while ( (type = _main_tokenizer->yylex()) != 0 ) 
	{		
		switch (type) 
		{			
		case CLCL:
		case '.':
		case lexARROW:
			if (depth == 0) 
			{
				out_delimiter = _main_tokenizer->YYText();
				trim (out_token);
				return true;
			} else 
			{
				out_token.append (" ").append (_main_tokenizer->YYText());
			}
			break;
				
		case '<':
		case '[':
		case '(':
		case '{':
			depth++;
			out_token.append (" ").append (_main_tokenizer->YYText());
			break;
				
		case '>':
		case ']':
		case ')':
		case '}':
			depth--;
			out_token.append (" ").append (_main_tokenizer->YYText());
			break;
				
		default:
			out_token.append (" ").append (_main_tokenizer->YYText());
			break;
		}
	}
	trim (out_token);
	return false;
}

ExpressionResult 
EngineParser::parseExpression(const string &in)
{
	return parse_expression (in.c_str ());	
}

void 
EngineParser::setSymbolManager (IAnjutaSymbolManager *manager)
{
	static IAnjutaSymbolField query_parent_scope_fields[] =
	{
		IANJUTA_SYMBOL_FIELD_ID, IANJUTA_SYMBOL_FIELD_NAME,
		IANJUTA_SYMBOL_FIELD_KIND, IANJUTA_SYMBOL_FIELD_TYPE
	};
	static IAnjutaSymbolField query_search_fields[] =
	{
		IANJUTA_SYMBOL_FIELD_ID, IANJUTA_SYMBOL_FIELD_NAME,
		IANJUTA_SYMBOL_FIELD_KIND
	};
	static IAnjutaSymbolField query_scope_fields[] =
	{
		IANJUTA_SYMBOL_FIELD_ID, IANJUTA_SYMBOL_FIELD_NAME,
		IANJUTA_SYMBOL_FIELD_SIGNATURE
	};
	static IAnjutaSymbolField query_search_in_scope_fields[] =
	{
		IANJUTA_SYMBOL_FIELD_ID, IANJUTA_SYMBOL_FIELD_NAME,
		IANJUTA_SYMBOL_FIELD_KIND, IANJUTA_SYMBOL_FIELD_RETURNTYPE,
		IANJUTA_SYMBOL_FIELD_SIGNATURE, IANJUTA_SYMBOL_FIELD_TYPE_NAME
	};
	_query_search =
		ianjuta_symbol_manager_create_query (manager,
		                                     IANJUTA_SYMBOL_QUERY_SEARCH,
		                                     IANJUTA_SYMBOL_QUERY_DB_PROJECT, NULL);
	ianjuta_symbol_query_set_filters (_query_search,
	                                  (IAnjutaSymbolType)
	                                  (IANJUTA_SYMBOL_TYPE_SCOPE_CONTAINER |
	                                   IANJUTA_SYMBOL_TYPE_TYPEDEF), TRUE, NULL);
	ianjuta_symbol_query_set_fields (_query_search,
	                                 G_N_ELEMENTS (query_search_fields),
	                                 query_search_fields, NULL);
	_query_scope =
		ianjuta_symbol_manager_create_query (manager,
		                                     IANJUTA_SYMBOL_QUERY_SEARCH_SCOPE,
		                                     IANJUTA_SYMBOL_QUERY_DB_PROJECT, NULL);
	ianjuta_symbol_query_set_fields (_query_scope,
	                                 G_N_ELEMENTS (query_scope_fields),
	                                 query_scope_fields, NULL);
	_query_search_in_scope =
		ianjuta_symbol_manager_create_query (manager,
		                                     IANJUTA_SYMBOL_QUERY_SEARCH_IN_SCOPE,
		                                     IANJUTA_SYMBOL_QUERY_DB_PROJECT, NULL);
	ianjuta_symbol_query_set_fields (_query_search_in_scope,
	                                 G_N_ELEMENTS (query_search_in_scope_fields),
	                                 query_search_in_scope_fields, NULL);
	_query_parent_scope =
		ianjuta_symbol_manager_create_query (manager,
		                                     IANJUTA_SYMBOL_QUERY_SEARCH_PARENT_SCOPE,
		                                     IANJUTA_SYMBOL_QUERY_DB_PROJECT, NULL);
	ianjuta_symbol_query_set_fields (_query_parent_scope,
	                                 G_N_ELEMENTS (query_parent_scope_fields),
	                                 query_parent_scope_fields, NULL);
}

void 
EngineParser::trim (string& str, string trimChars /* = "{};\r\n\t\v " */)
{
  	string::size_type pos = str.find_last_not_of (trimChars);
	
  	if (pos != string::npos) 
	{
    	str.erase(pos + 1);
    	pos = str.find_first_not_of (trimChars);
    	if(pos != string::npos) 
		{			
			  str.erase(0, pos);
		}
  	}
  	else 
	{
		str.erase(str.begin(), str.end());
	}
}

/**
 * @return NULL on global 
 */
void
EngineParser::getNearestClassInCurrentScopeChainByFileLine (const char* full_file_path, 
                                                            unsigned long linenum,
                                                            string &out_type_name)
{
	IAnjutaIterable *iter;

	/* Find current scope of given file and line */
	iter = ianjuta_symbol_query_search_scope (_query_scope, full_file_path,
	                                          linenum, NULL);
	if (iter == NULL)
		return;

	/* FIXME: this method doesn't take into consideration 
	 * classes with same name on multiple namespaces 
	 */
	if (iter != NULL)
	{
		do 
		{
			IAnjutaIterable *parent_iter;
			IAnjutaSymbol *node = IANJUTA_SYMBOL (iter);
			DEBUG_PRINT ("sym_name = %s", ianjuta_symbol_get_string (node, IANJUTA_SYMBOL_FIELD_NAME, NULL));
			if (ianjuta_symbol_get_sym_type (node, NULL) == IANJUTA_SYMBOL_TYPE_CLASS)
			{
				out_type_name = ianjuta_symbol_get_string (node, IANJUTA_SYMBOL_FIELD_NAME, NULL);
				break;
			}
			parent_iter =
				ianjuta_symbol_query_search_parent_scope (_query_parent_scope,
				                                          node, NULL);
			g_object_unref (iter);
			iter = parent_iter;
		} while (iter);
		if (iter)
			g_object_unref (iter);
	}
}

bool
EngineParser::getTypeNameAndScopeByToken (ExpressionResult &result, 
    									  string &token,
    									  string &op,
    									  const string& full_file_path, 
    									  unsigned long linenum,
    									  const string& above_text,
    									  string &out_type_name, 
    									  string &out_type_scope)
{
	if (result.m_isaType) 
	{
		DEBUG_PRINT ("*** Found a cast expression");
		/*
		 * Handle type (usually when casting is found)
		 */
		if (result.m_isPtr && op == ".") 
		{
			DEBUG_PRINT ("Did you mean to use '->' instead of '.' ?");
			return false;
		}
		
		if (!result.m_isPtr && op == "->") 
		{
			DEBUG_PRINT ("Can not use '->' operator on a non pointer object");
			return false;
		}

		out_type_scope = result.m_scope.empty() ? "" : result.m_scope.c_str();
		out_type_name = result.m_name.c_str();
		return true;
	} 
	else if (result.m_isThis) 
	{
		DEBUG_PRINT ("*** Found 'this'");
		
		/* special handle for 'this' keyword */
		if (op == "::") 
		{
			DEBUG_PRINT ("'this' can not be used with operator ::");
			return false;
		}

		if (result.m_isPtr && op == ".") 
		{
			DEBUG_PRINT ("Did you mean to use '->' instead of '.' ?");
			return false;
		}
			
		if (!result.m_isPtr && op == "->") 
		{
			DEBUG_PRINT ("Can not use '->' operator on a non pointer object");
			return false;
		}

		/* reaching this point we are quite sure that the easiest tests about "this"
		 * calling are passed. Go on finding for the first symbol of type class that
		 * is reachable through the scopes chain.
		 */	

		/* will we find a good class scope? */
		out_type_scope = result.m_scope.empty() ? "" : result.m_scope.c_str();
		out_type_name = ""; 

		getNearestClassInCurrentScopeChainByFileLine (full_file_path.c_str (), linenum, out_type_name);
		if (out_type_name.empty ()) 
		{
			DEBUG_PRINT ("'this' has not a type name");
			return false;
		}
		
		return true;
	}
	else if (op == "::")
	{
		out_type_name = token;
		out_type_scope = result.m_scope.empty() ? "" : result.m_scope.c_str();
		return true;
	}
	else 
	{
		/*
		 * Found an identifier (can be a local variable, a global one etc)
		 */			
		DEBUG_PRINT ("*** Found an identifier or local variable...");

		/* optimize scope'll clear the scopes leaving the local variables */
		string optimized_scope = optimizeScope(above_text);

		VariableList li;
		std::map<std::string, std::string> ignoreTokens;
		get_variables(optimized_scope, li, ignoreTokens, false);

		/* here the trick is to start from the end of the found variables
		 * up to the begin. This because the local variable declaration should be found
		 * just above to the statement line 
		 */
		for (VariableList::reverse_iterator iter = li.rbegin(); iter != li.rend(); iter++) 
		{
			Variable var = (*iter);
		
			if (token == var.m_name) 
			{
				DEBUG_PRINT ("We found the variable type to parse... it's \"%s"
					"\" with typescope \"%s\"", var.m_type.c_str(), var.m_typeScope.c_str());
				out_type_name = var.m_type;
				out_type_scope = var.m_typeScope;

				return true;
			}
		}

		IAnjutaIterable* curr_scope_iter = 
			ianjuta_symbol_query_search_scope (_query_scope,
			                                   full_file_path.c_str (),
			                                   linenum, NULL);

		if (curr_scope_iter != NULL)
		{
			IAnjutaSymbol *node = IANJUTA_SYMBOL (curr_scope_iter);

			/* try to get the signature from the symbol and test if the 
			 * variable searched is found there.
			 */
			const gchar * signature = ianjuta_symbol_get_string (node, IANJUTA_SYMBOL_FIELD_SIGNATURE, NULL);
			if (signature == NULL)
			{
				g_object_unref (curr_scope_iter);
				return false;
			}
			
			DEBUG_PRINT ("Signature is %s", signature);

			get_variables(signature, li, ignoreTokens, false);
			
			for (VariableList::reverse_iterator iter = li.rbegin(); iter != li.rend(); iter++) 
			{
				Variable var = (*iter);
			
				if (token == var.m_name) 
				{
				DEBUG_PRINT ("We found the variable type to parse from signature... it's \"%s"
					"\" with typescope \"%s\"", var.m_type.c_str (), var.m_typeScope.c_str ());
					out_type_name = var.m_type;
					out_type_scope = var.m_typeScope;

					g_object_unref (curr_scope_iter);
					return true;
				}
			}			

			g_object_unref (curr_scope_iter);
		}
		
		/* if we reach this point it's likely that we missed the right var type */
		DEBUG_PRINT ("## Wrong detection of the variable type");
	}
	return false;
}

/**
 * Find a searchable scope (or container) from a type_name and type_scope strings.
 * This function should usually be used to determine first token's scope.
 */
IAnjutaIterable *
EngineParser::getCurrentSearchableScope (string &type_name, string &type_scope)
{
	// FIXME: case of more results now it's hardcoded to 1
	IAnjutaIterable *curr_searchable_scope =
		ianjuta_symbol_query_search (_query_search, type_name.c_str(), NULL);
	
	if (curr_searchable_scope != NULL)
	{
		IAnjutaSymbol *node;

		node = IANJUTA_SYMBOL (curr_searchable_scope);

		const gchar *skind = ianjuta_symbol_get_string (node,
		    					IANJUTA_SYMBOL_FIELD_KIND, NULL);
		
		DEBUG_PRINT ("Current Searchable Scope name \"%s\" kind \"%s\" and id %d",
		             ianjuta_symbol_get_string (node, IANJUTA_SYMBOL_FIELD_NAME, NULL), skind,
		             ianjuta_symbol_get_int (node, IANJUTA_SYMBOL_FIELD_ID, NULL));

		/* is it a typedef? In that case find the parent struct */
		if (g_strcmp0 (ianjuta_symbol_get_string (node,
		    IANJUTA_SYMBOL_FIELD_KIND, NULL), "typedef") == 0)
		{
			DEBUG_PRINT ("It's a TYPEDEF... trying to find the associated struct...!");

			curr_searchable_scope = switchTypedefToStruct (IANJUTA_ITERABLE (node));
			
			node = IANJUTA_SYMBOL (curr_searchable_scope);
			DEBUG_PRINT ("(NEW) Current Searchable Scope %s and id %d",
						ianjuta_symbol_get_string (node, IANJUTA_SYMBOL_FIELD_NAME, NULL), 
						ianjuta_symbol_get_int (node, IANJUTA_SYMBOL_FIELD_ID, NULL));
		}
	}
	else
	{
		DEBUG_PRINT ("Current Searchable Scope NULL");
	}

	return curr_searchable_scope;
}

/**
 * @param test Must be searched with SYMINFO_KIND 
 * @return or the same test iterator or a new struct. In that second case the input 
 * iterator test is unreffed.
 * 
 */
IAnjutaIterable *
EngineParser::switchTypedefToStruct (IAnjutaIterable * test,
                         			 IAnjutaSymbolField sym_info 
                                     /*= (SymExtraInfo)(SYMINFO_SIMPLE | SYMINFO_KIND)*/)
{
	IAnjutaSymbol *node = IANJUTA_SYMBOL (test);	
	IAnjutaIterable *new_struct;

	DEBUG_PRINT ("Switching TYPEDEF (%d) ==> to STRUCT",
	             ianjuta_symbol_get_int (node, IANJUTA_SYMBOL_FIELD_ID, NULL));
	new_struct = ianjuta_symbol_query_search_parent_scope (_query_parent_scope,
	                                                       node, NULL);
	                                         
	if (new_struct != NULL)
	{
		/* kill the old one */
		g_object_unref (test);

		test = new_struct;
	}
	else 
	{
		DEBUG_PRINT ("Couldn't find a parent for typedef. We'll return the same object");
	}	

	return test;
}

IAnjutaIterable *
EngineParser::switchMemberToContainer (IAnjutaIterable * test)
{
	IAnjutaSymbol *node = IANJUTA_SYMBOL (test);	
	IAnjutaIterable *new_container;
	const gchar* sym_type_name =
		ianjuta_symbol_get_string (node, IANJUTA_SYMBOL_FIELD_TYPE_NAME, NULL);

	DEBUG_PRINT ("Switching container with type_name %s", sym_type_name);

	/* hopefully we'll find a new container for the type_name of test param */
	new_container = ianjuta_symbol_query_search (_query_search,
	                                             sym_type_name, NULL);
	if (new_container != NULL)
	{
		g_object_unref (test);

		test = new_container;

		DEBUG_PRINT (".. found new container with n items %d", 
		             ianjuta_iterable_get_length (test, NULL));
	}
	else 
	{
		DEBUG_PRINT ("Couldn't find a container to substitute sym_type_name %s",
		             sym_type_name);
	}	

	return test;
}

/* FIXME TODO: error processing. Find out a way to notify the caller of the occurred 
 * error. The "cout" method cannot be used
 */
IAnjutaIterable *
EngineParser::processExpression(const string& stmt, 
    							const string& above_text,
    							const string& full_file_path, 
    							unsigned long linenum)
{
	ExpressionResult result;
	string current_token;
	string op;
	string type_name;
	string type_scope;

	DEBUG_PRINT ("Setting text %s to the tokenizer", stmt.c_str ());
	_main_tokenizer->setText (stmt.c_str ());

	/* get the first token */
	nextMainToken (current_token, op);		

	DEBUG_PRINT ("First main token \"%s\" with op \"%s\"", current_token.c_str (), op.c_str ());

	/* parse the current sub-expression of a statement and fill up 
	 * ExpressionResult object
	 */
	result = parseExpression (current_token);

	/* fine. Get the type name and type scope given the above result for the first 
	 * and most important token.
	 * The type_scope is for instance 'std' in this statement:
	 * (std::foo_util)klass->
	 */	
	bool process_res = getTypeNameAndScopeByToken (result, 
    									  current_token,
    									  op,
    									  full_file_path, 
    									  linenum,
    									  above_text,
    									  type_name, 
    									  type_scope);
	if (process_res == false)
	{
		DEBUG_PRINT ("Initial statement processing failed. "
			"I cannot continue. ");
		return NULL;
	}
	
	DEBUG_PRINT ("Searching for curr_searchable_scope with type_name \"%s\""
				" and type_scope \"%s\"", type_name.c_str (), type_scope.c_str ());

	/* at this time we're enough ready to issue a first query to our db. 
	 * We absolutely need to find the searchable object scope of the first result 
	 * type. By this one we can iterate the tree of scopes and reach a result.
	 */	
	IAnjutaIterable *curr_searchable_scope =
		getCurrentSearchableScope (type_name, type_scope);

	if (curr_searchable_scope == NULL)
	{
		DEBUG_PRINT ("curr_searchable_scope failed to process, check the problem please");
		return NULL;
	}	
	
	/* fine. Have we more tokens left? */
	while (nextMainToken (current_token, op) == 1) 
	{
		DEBUG_PRINT("Next main token \"%s\" with op \"%s\"",current_token.c_str (), op.c_str ());

		/* parse the current sub-expression of a statement and fill up 
	 	 * ExpressionResult object
	 	 */
		result = parseExpression (current_token);
		
		if (process_res == false || curr_searchable_scope == NULL)
		{
			DEBUG_PRINT ("No luck with the NEXT token, the NEXT token failed and then "
				"I cannot continue. ");

			if (curr_searchable_scope != NULL)
				g_object_unref (curr_searchable_scope );
			return NULL;
		}
		
		/* check if the name of the result is valuable or not */
		IAnjutaSymbol *node;
		IAnjutaIterable * iter;

		node = IANJUTA_SYMBOL (curr_searchable_scope);
		
		iter = ianjuta_symbol_query_search_in_scope (_query_search_in_scope,
		                                             result.m_name.c_str (),
		                                             node, NULL);
		
		if (iter == NULL)
		{
			DEBUG_PRINT ("Warning, the result.m_name %s "
				"does not belong to scope (id %d)", result.m_name.c_str (), 
			             ianjuta_symbol_get_int (node, IANJUTA_SYMBOL_FIELD_ID, NULL));
			
			if (curr_searchable_scope != NULL)
				g_object_unref (curr_searchable_scope );
			
			return NULL;
		}
		else 
		{
			gchar *sym_kind;
			DEBUG_PRINT ("Good element %s", result.m_name.c_str ());
			
			node = IANJUTA_SYMBOL (iter);
			sym_kind = (gchar*)ianjuta_symbol_get_string (node, 
		    										IANJUTA_SYMBOL_FIELD_KIND, NULL);
			
			DEBUG_PRINT (".. it has sym_kind \"%s\"", sym_kind);

			/* the same check as in the engine-core on sdb_engine_add_new_sym_type () */
			if (g_strcmp0 (sym_kind, "member") == 0 || 
	    		g_strcmp0 (sym_kind, "variable") == 0 || 
	    		g_strcmp0 (sym_kind, "field") == 0)
			{
				iter = switchMemberToContainer (iter);
				node = IANJUTA_SYMBOL (iter);
				sym_kind = (gchar*)ianjuta_symbol_get_string (node, 
		    										IANJUTA_SYMBOL_FIELD_KIND, NULL);				
			}
			
			/* check for any typedef */
			if (g_strcmp0 (ianjuta_symbol_get_string (node, 
		    										IANJUTA_SYMBOL_FIELD_KIND, NULL),
	    											"typedef") == 0)
			{			
				iter = switchTypedefToStruct (iter);
				node = IANJUTA_SYMBOL (iter);
				sym_kind = (gchar*)ianjuta_symbol_get_string (node, 
		    										IANJUTA_SYMBOL_FIELD_KIND, NULL);				
			}
			
			/* is it a function or a method? */
			if (g_strcmp0 (sym_kind, "function") == 0 ||
			    g_strcmp0 (sym_kind, "method") == 0 ||
			    g_strcmp0 (sym_kind, "prototype") == 0)
			{

				string func_ret_type_name = 
					ianjuta_symbol_get_string (node, IANJUTA_SYMBOL_FIELD_RETURNTYPE, NULL);

				string func_signature = 
					ianjuta_symbol_get_string (node, IANJUTA_SYMBOL_FIELD_SIGNATURE, NULL);
				
				func_ret_type_name += " " + result.m_name + func_signature + "{}";

				FunctionList li;
				std::map<std::string, std::string> ignoreTokens;
				get_functions (func_ret_type_name, li, ignoreTokens);

				DEBUG_PRINT ("Functions found are...");
/*				
				for (FunctionList::reverse_iterator func_iter = li.rbegin(); 
				     func_iter != li.rend(); 
				     func_iter++) 
				{
					Function var = (*func_iter);
					var.print ();			
				}
*/
			
				g_object_unref (iter);

				DEBUG_PRINT ("Going to look for the following function ret type %s",
							func_ret_type_name.c_str ());

				iter = getCurrentSearchableScope (li.front().m_returnValue.m_type,
				                                  type_scope);
			}			               
			
			/* remove the 'old' curr_searchable_scope and replace with 
			 * this new one
			 */			
			g_object_unref (curr_searchable_scope);			
			curr_searchable_scope = iter;
			continue;
		}
	}

	DEBUG_PRINT ("END of expression processing. Returning curr_searchable_scope");
	return curr_searchable_scope;
}

/**
 * @return The visible scope until pchStopWord is encountered
 */
string 
EngineParser::optimizeScope(const string& srcString)
{
	string wxcurrScope;
	std::vector<std::string> scope_stack;
	std::string currScope;

	int type;

	/* Initialize the scanner with the string to search */
	const char * scannerText =  srcString.c_str ();
	_extra_tokenizer->setText (scannerText);
	bool changedLine = false;
	bool prepLine = false;
	int curline = 0;
	while (true) 
	{
		type = _extra_tokenizer->yylex();

		/* Eof ? */
		if (type == 0) 
		{
			if (!currScope.empty())
				scope_stack.push_back(currScope);
			break;
		}

		/* eat up all tokens until next line */
		if ( prepLine && _extra_tokenizer->lineno() == curline) 
		{
			currScope += " ";
			currScope += _extra_tokenizer->YYText();
			continue;
		}

		prepLine = false;

		/* Get the current line number, it will help us detect preprocessor lines */
		changedLine = (_extra_tokenizer->lineno() > curline);
		if (changedLine) 
		{
			currScope += "\n";
		}

		curline = _extra_tokenizer->lineno();
		switch (type) 
		{
		case (int)'(':
			currScope += "\n";
			scope_stack.push_back(currScope);
			currScope = "(\n";
			break;
		case (int)'{':
			currScope += "\n";
			scope_stack.push_back(currScope);
			currScope = "{\n";
			break;
		case (int)')':
			// Discard the current scope since it is completed
			if ( !scope_stack.empty() ) {
				currScope = scope_stack.back();
				scope_stack.pop_back();
				currScope += "()";
			} else
				currScope.clear();
			break;
		case (int)'}':
			/* Discard the current scope since it is completed */
			if ( !scope_stack.empty() ) {
				currScope = scope_stack.back();
				scope_stack.pop_back();
				currScope += "\n{}\n";
			} else {
				currScope.clear();
			}
			break;
		case (int)'#':
			if (changedLine) {
				/* We are at the start of a new line
				 * consume everything until new line is found or end of text
				 */
				currScope += " ";
				currScope += _extra_tokenizer->YYText();
				prepLine = true;
				break;
			}
		default:
			currScope += " ";
			currScope += _extra_tokenizer->YYText();
			break;
		}
	}

	_extra_tokenizer->reset();

	if (scope_stack.empty())
		return srcString;

	currScope.clear();
	size_t i = 0;
	for (; i < scope_stack.size(); i++)
		currScope += scope_stack.at(i);

	/* if the current scope is not empty, terminate it with ';' and return */
	if ( currScope.empty() == false ) {
		currScope += ";";
		return currScope.c_str();
	}

	return srcString;
}

/************ C FUNCTIONS ************/

void
engine_parser_init (IAnjutaSymbolManager * manager)
{
	EngineParser::getInstance ()->setSymbolManager (manager);
}

IAnjutaIterable *
engine_parser_process_expression (const gchar *stmt, const gchar * above_text,
    const gchar * full_file_path, gulong linenum)
{
	IAnjutaIterable *iter = 
		EngineParser::getInstance ()->processExpression (stmt, 
		    											above_text,  
	    												full_file_path, 
		    											linenum);

	return iter;
}