summaryrefslogtreecommitdiff
path: root/hgehelp/parser.cpp
blob: 0386ef9f9078e78c42183b341efd9c8722f82ce6 (plain) (blame)
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
/*
** Haaf's Game Engine 1.7
** Copyright (C) 2003-2007, Relish Games
** hge.relishgames.com
**
** Resource script parser implementation
*/

#include "parser.h"


HGE *RScriptParser::hge=0;


struct keyword
{
	const char*	word;
	int		code;
};

keyword keytable[]=
{
	{ "=",			TTEQUALS		},
	{ ":",			TTBASED			},
	{ ",",			TTSEPARATOR		},
	{ "{",			TTOPENBLOCK		},
	{ "}",			TTCLOSEBLOCK	},
	{ "true",		TTBOOL			},
	{ "false",		TTBOOL			},

	{ "Include",	TTRES_INCLUDE	},
	{ "Resource",	TTRES_RESOURCE	},
	{ "Texture",	TTRES_TEXTURE	},
	{ "Sound",		TTRES_SOUND		},
	{ "Music",		TTRES_MUSIC		},
	{ "Stream",		TTRES_STREAM	},
	{ "Target",		TTRES_TARGET	},
	{ "Sprite",		TTRES_SPRITE	},
	{ "Animation",	TTRES_ANIMATION	},
	{ "Font",		TTRES_FONT		},
	{ "Particle",	TTRES_PARTICLE	},
	{ "Distortion",	TTRES_DISTORT	},
	{ "StringTable",TTRES_STRTABLE	},

	{ "filename",	TTPAR_FILENAME	},
	{ "resgroup",	TTPAR_RESGROUP	},
	{ "mipmap",		TTPAR_MIPMAP	},
	{ "amplify",	TTPAR_AMPLIFY	},
	{ "size",		TTPAR_SIZE		},
	{ "zbuffer",	TTPAR_ZBUFFER	},
	{ "texture",	TTPAR_TEXTURE	},
	{ "rect",		TTPAR_RECT		},
	{ "hotspot",	TTPAR_HOTSPOT	},
	{ "blendmode",	TTPAR_BLENDMODE	},
	{ "color",		TTPAR_COLOR		},
	{ "zorder",		TTPAR_ZORDER	},
	{ "flip",		TTPAR_FLIP		},
	{ "scale",		TTPAR_SCALE		},
	{ "proportion",	TTPAR_PROPORTION},
	{ "rotation",	TTPAR_ROTATION	},
	{ "frames",		TTPAR_FRAMES	},
	{ "fps",		TTPAR_FPS		},
	{ "mode",		TTPAR_MODE		},
	{ "tracking",	TTPAR_TRACKING	},
	{ "spacing",	TTPAR_SPACING	},
	{ "sprite",		TTPAR_SPRITE	},
	{ "mesh",		TTPAR_MESH		},

	{ "COLORMUL",	TTCON_COLORMUL	},
	{ "COLORADD",	TTCON_COLORADD	},
	{ "ALPHABLEND",	TTCON_ALPHABLND	},
	{ "ALPHAADD",	TTCON_ALPHAADD	},
	{ "ZWRITE",		TTCON_ZWRITE	},
	{ "NOZWRITE",	TTCON_NOZWRITE	},
	{ "FORWARD",	TTCON_FORWARD	},
	{ "REVERSE",	TTCON_REVERSE	},
	{ "PINGPONG",	TTCON_PINGPONG	},
	{ "NOPINGPONG",	TTCON_NOPINGPONG},
	{ "LOOP",		TTCON_LOOP		},
	{ "NOLOOP",		TTCON_NOLOOP	},
	{ "CIRCLE",		TTCON_CIRCLE	},
	{ "RECT",		TTCON_RECT		},
	{ "ALPHA",		TTCON_ALPHA		},

	{ NULL,			TTNONE			}
};

RScriptParser::RScriptParser(char *name, char *scr)
{
	hge=hgeCreate(HGE_VERSION);

	scriptname=name;
	script=scr;
	tokenvalue[0]=0;
	tokentype=TTNONE;
	line=1;
}

int RScriptParser::get_token()
{
	int i;

	// Skip whitespaces and comments

	for(;;)
	{
		while(*script==' ' || *script=='\t' || *script=='\n' || *script=='\r')
		{
			if(*script=='\n') line++;
			script++;
		}
		if(*script==';') while(*script && *script!='\n' && *script!='\r') script++;
		else break;
	}

	// End of script

	if(!*script) { tokentype=TTEND; tokenvalue[0]=0; return tokentype; }

	// Number

	if((*script>='0' && *script<='9') || *script=='.' || *script=='-')
	{
		tokentype=TTNUMBER;
		for(i=0;(*script>='0' && *script<='9') || *script=='.' || *script=='-';i++)
			 tokenvalue[i]=*script++;

		// Hexadecimal number starting with decimal digit

		if((*script>='A' && *script<='F') || (*script>='a' && *script<='f'))
		{
			tokentype=TTSTRING;
			for(; (*script>='A' && *script<='F') || (*script>='a' && *script<='f') ; i++)
				 tokenvalue[i]=*script++;
		}

		tokenvalue[i]=0;
		return tokentype;
	}

	// Quoted string

	if(*script=='"')
	{
		tokentype=TTSTRING;
		script++;
		for(i=0;*script && *script!='"' && *script!='\n' && *script!='\r';i++)
			 tokenvalue[i]=*script++;
		tokenvalue[i]=0;
		if(*script) script++;
		return tokentype;
	}

	// Keyword

	for(i=0;keytable[i].word;i++)
		if(!strtkcmp(keytable[i].word, script))
		{
			tokentype = keytable[i].code;
			strcpy(tokenvalue,keytable[i].word);
			script+=strlen(keytable[i].word);
			return tokentype;
		}

	// Unquoted string or hexadecimal number

	tokentype=TTSTRING;
	for(i=0;
		*script && *script!=' ' && *script!='\t' && *script!='\n' && *script!='\r'
		&& *script!=',' && *script!='=' && *script!='{' && *script!='}' && *script!=':';
		i++)
		tokenvalue[i]=*script++;
	tokenvalue[i]=0;
	return tokentype;
}

bool RScriptParser::strtkcmp(const char* str, const char* mem)
{
	int i,len=strlen(str);
	for(i=0;i<len;i++)
	{
		if(!mem[i]) return true;
		if(mem[i] != str[i]) return true;
	}
	return false;
}

DWORD RScriptParser::tkn_hex()
{
	int i;
	DWORD dw=0;
	char chr;
	for(i=0; tokenvalue[i]; i++)
	{
		chr=tokenvalue[i];
		if(chr >= 'a') chr-='a'-':';
		if(chr >= 'A') chr-='A'-':';
		chr-='0';
		if(chr>0xF) chr=0xF;
		dw=(dw << 4) | chr;
	}
	return dw;
}

void RScriptParser::ScriptPostError(const char *msg1, const char *msg2)
{
	hge->System_Log("%s, line %d: %s'%s'%s",
		get_name(), get_line(), msg1, tokenvalue[0] ? tkn_string():"<EOF>", msg2);
}