File: componentfile_preprocess.h

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (119 lines) | stat: -rw-r--r-- 2,392 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
/*
	$Id: componentfile_preprocess.h,v 1.5 2001/09/08 19:12:50 japj Exp $
	
	ClanGUI, copyrights by various people. Have a look in the CREDITS file.
	
	This sourcecode is distributed using the Library GNU Public Licence,
	version 2 or (at your option) any later version. Please read LICENSE
	for details.
*/

#ifndef header_componentfile_preprocess
#define header_componentfile_preprocess

#include <string>
#include <map>
#include <list>
#include <vector>

/*
	Preprocessor functionality:

	Implements template support, by preprocessing the component script file,
	expanding template instantiations with template bodies, and removing template
	definition bodies. Example;

File before:	template <X, Y, TEXT>
				label std_label
				{
					x = X;
					y = Y;
					text = TEXT;
				}

				template <X, Y, TEXT>
				frame my_frame
				{
					x = X;
					y = Y;

					label1 = std_label<X, Y, TEXT>;
				}

				my_thing = my_frame<10, 10, testing>;

<============================================================>

File after:		frame my_thing
				{
					x = 10;
					y = 10;

					label label1
					{
						x = 10;
						y = 10;
						text = testing;
					}
				}
*/

class CL_Componentfile_Preprocess
{
public:
	CL_Componentfile_Preprocess(const std::string &data, const std::string &filename);

	operator const std::string &() { return data; }
private:
	std::string data;

	enum EParseState
	{
		PARSE_GLOBAL,
		PARSE_TEMPLATE,
		PARSE_TEMPLATE_ARG,
		PARSE_TEMPLATE_NAMEDEF,
		PARSE_TEMPLATE_BODY,
		PARSE_TEMPLATE_INSTANTIATION,
		PARSE_TEMPLATE_INSTANTIATION_ARG
	};

	struct TemplateDefinition
	{
		int file_offset;
		int name_offset;
		int definition_copy_offset;
		std::string definition;
		typedef	std::map<std::string, std::pair<int, std::list<int> > > CONV_MAP;
		CONV_MAP conversions;
	};

	struct TemplateReplacement
	{
		int offset;
		std::string new_text;
		int erase_length;
	};

	class SortTemplateDefinitionByLocationDescending
	{
	public:
		bool operator()(TemplateDefinition *x, TemplateDefinition *y) const
		{
			return x->file_offset > y->file_offset;
		}
	};
	void perform_replacements(std::string &s, std::vector<TemplateReplacement> &replacements, int global_offset);

	class SortTemplateReplacementByLocationDescending
	{
	public:
		bool operator()(const TemplateReplacement &x, const TemplateReplacement &y) const
		{
			return x.offset > y.offset;
		}
	};
};

#endif