summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/ASTInspector.h
blob: 209b745c8f17b49ff16a037652108e7ee145ddf5 (plain)
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
/*
 * ASTInspector.h
 *
 *  Created on: Dec 17, 2012
 *      Author: att
 */

#ifndef ASTINSPECTOR_H_
#define ASTINSPECTOR_H_

#include <map>
#include "Visitor.h"
#include "AST/AllTypesOfASTNodes.h"
/**
 * ASTInspector is a class which is used to inspect nodes of AST.
 * It's very useful. It provides user information about structure of the AST, how
 * particular nodes are built, and some other information, more statistical such as,
 * how many particular nodes is in tree. Thanks to depth awareness output can
 * be well formatted. Every node is visited in different manner.
 */

class ASTInspector: public Visitor {
public:
	ASTInspector();
	unsigned int how_many_visits() { return number_of_visits; }
	/**
	 * For instance: how_many_occurences_of("BasicExpression") return how many times ASTInspector has encountered
	 * BasicExpression node.
	 */
	unsigned int how_many_occurences_of(std::string type);

	/**
	 * get_report() returns inspection report. To get report, you first have to
	 * visit some tree, using "visit" function.
	 */
	std::string get_report() { return this->inspection_report; }

	virtual ~ASTInspector();

	/**
	 * forget_everything() erases all data which ASTInspector got during inspection.
	 * It's useful when we want to use ASTInspector for more trees than one, and data from previous
	 * shouldn't mix with data from current one.
	 */
	void forget_everything();

	void visit(Visitable * node);


private:
	unsigned int number_of_visits;
	typedef unsigned int NumberOfNodes;
	std::map<std::string, NumberOfNodes> occurences;
	std::string inspection_report;
	unsigned int depth_level;
	std::string compute_indent();

	/**
	 * write_report is an internal method used for
	 * generating report in incremental way, using
	 * notes from current visits and formatting it on right depth_level.
	 */
	void write_report(std::string visit_notes);

	void visit(ProgramNode * node);
	void visit(ImportStatement * import);
	void visit(ConstantExpression * node);
	void visit(BasicExpression * node);
	void visit(UnaryExpression * node);
	void visit(PostfixExpression * node);
	void visit(WhileNode * node);
	void visit(BasicStatement * node);
	void visit(DeclarationStatement * node);
	void visit(Assignment * node);
	void visit(IfNode * node);
	void visit(IncorrectExpression * node);
	void visit(RepeatStatement * node);
	void visit(VariableExpression * node);



};

#endif /* ASTINSPECTOR_H_ */