1 //**************************************************************************
2 //**
3 //**	##   ##    ##    ##   ##   ####     ####   ###     ###
4 //**	##   ##  ##  ##  ##   ##  ##  ##   ##  ##  ####   ####
5 //**	 ## ##  ##    ##  ## ##  ##    ## ##    ## ## ## ## ##
6 //**	 ## ##  ########  ## ##  ##    ## ##    ## ##  ###  ##
7 //**	  ###   ##    ##   ###    ##  ##   ##  ##  ##       ##
8 //**	   #    ##    ##    #      ####     ####   ##       ##
9 //**
10 //**	$Id: vc_parser.cpp 4297 2010-06-03 22:49:00Z firebrand_kh $
11 //**
12 //**	Copyright (C) 1999-2006 Jānis Legzdiņš
13 //**
14 //**	This program is free software; you can redistribute it and/or
15 //**  modify it under the terms of the GNU General Public License
16 //**  as published by the Free Software Foundation; either version 2
17 //**  of the License, or (at your option) any later version.
18 //**
19 //**	This program is distributed in the hope that it will be useful,
20 //**  but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 //**  GNU General Public License for more details.
23 //**
24 //**************************************************************************
25 
26 // HEADER FILES ------------------------------------------------------------
27 
28 #include "vc_local.h"
29 
30 // MACROS ------------------------------------------------------------------
31 
32 // TYPES -------------------------------------------------------------------
33 
34 // EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
35 
36 // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
37 
38 // PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
39 
40 // EXTERNAL DATA DECLARATIONS ----------------------------------------------
41 
42 // PUBLIC DATA DEFINITIONS -------------------------------------------------
43 
44 // PRIVATE DATA DEFINITIONS ------------------------------------------------
45 
46 // CODE --------------------------------------------------------------------
47 
48 //==========================================================================
49 //
50 //	VParser::ParseDotMethodCall
51 //
52 //==========================================================================
53 
ParseDotMethodCall(VExpression * SelfExpr,VName MethodName,TLocation Loc)54 VExpression* VParser::ParseDotMethodCall(VExpression* SelfExpr,
55 	VName MethodName, TLocation Loc)
56 {
57 	guard(VParser::ParseDotMethodCall);
58 	VExpression* Args[VMethod::MAX_PARAMS + 1];
59 	int NumArgs = 0;
60 	if (!Lex.Check(TK_RParen))
61 	{
62 		do
63 		{
64 			Args[NumArgs] = ParseExpressionPriority13();
65 			if (NumArgs == VMethod::MAX_PARAMS)
66 				ParseError(Lex.Location, "Too many arguments");
67 			else
68 				NumArgs++;
69 		} while (Lex.Check(TK_Comma));
70 		Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
71 	}
72 	return new VDotInvocation(SelfExpr, MethodName, Loc, NumArgs, Args);
73 	unguard;
74 }
75 
76 //==========================================================================
77 //
78 //	VParser::ParseBaseMethodCall
79 //
80 //==========================================================================
81 
ParseBaseMethodCall(VName Name,TLocation Loc)82 VExpression* VParser::ParseBaseMethodCall(VName Name, TLocation Loc)
83 {
84 	guard(VParser::ParseBaseMethodCall);
85 	VExpression* Args[VMethod::MAX_PARAMS + 1];
86 	int NumArgs = 0;
87 	if (!Lex.Check(TK_RParen))
88 	{
89 		do
90 		{
91 			Args[NumArgs] = ParseExpressionPriority13();
92 			if (NumArgs == VMethod::MAX_PARAMS)
93 				ParseError(Lex.Location, "Too many arguments");
94 			else
95 				NumArgs++;
96 		} while (Lex.Check(TK_Comma));
97 		Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
98 	}
99 	return new VBaseInvocation(Name, NumArgs, Args, Loc);
100 	unguard;
101 }
102 
103 //==========================================================================
104 //
105 //	VParser::ParseMethodCallOrCast
106 //
107 //==========================================================================
108 
ParseMethodCallOrCast(VName Name,TLocation Loc)109 VExpression* VParser::ParseMethodCallOrCast(VName Name, TLocation Loc)
110 {
111 	guard(VParser::ParseMethodCallOrCast);
112 	VExpression* Args[VMethod::MAX_PARAMS + 1];
113 	int NumArgs = 0;
114 	if (!Lex.Check(TK_RParen))
115 	{
116 		do
117 		{
118 			Args[NumArgs] = ParseExpressionPriority13();
119 			if (NumArgs == VMethod::MAX_PARAMS)
120 				ParseError(Lex.Location, "Too many arguments");
121 			else
122 				NumArgs++;
123 		} while (Lex.Check(TK_Comma));
124 		Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
125 	}
126 	return new VCastOrInvocation(Name, Loc, NumArgs, Args);
127 	unguard;
128 }
129 
130 //==========================================================================
131 //
132 //	VParser::ParseLocalVar
133 //
134 //==========================================================================
135 
ParseLocalVar(VExpression * TypeExpr)136 VLocalDecl* VParser::ParseLocalVar(VExpression* TypeExpr)
137 {
138 	guard(VParser::ParseLocalVar);
139 	VLocalDecl* Decl = new VLocalDecl(Lex.Location);
140 	do
141 	{
142 		VLocalEntry e;
143 
144 		e.TypeExpr = TypeExpr->CreateTypeExprCopy();
145 		TLocation l = Lex.Location;
146 		while (Lex.Check(TK_Asterisk))
147 		{
148 			e.TypeExpr = new VPointerType(e.TypeExpr, l);
149 			l = Lex.Location;
150 		}
151 		if (Lex.Token != TK_Identifier)
152 		{
153 			ParseError(Lex.Location, "Invalid identifier, variable name expected");
154 			continue;
155 		}
156 		e.Loc = Lex.Location;
157 		e.Name = Lex.Name;
158 		Lex.NextToken();
159 
160 		if (Lex.Check(TK_LBracket))
161 		{
162 			TLocation SLoc = Lex.Location;
163 			VExpression* SE = ParseExpression();
164 			Lex.Expect(TK_RBracket, ERR_MISSING_RFIGURESCOPE);
165 			e.TypeExpr = new VFixedArrayType(e.TypeExpr, SE, SLoc);
166 		}
167 		//  Initialisation
168 		else if (Lex.Check(TK_Assign))
169 		{
170 			e.Value = ParseExpressionPriority13();
171 		}
172 		Decl->Vars.Append(e);
173 	} while (Lex.Check(TK_Comma));
174 	delete TypeExpr;
175 	TypeExpr = NULL;
176 	return Decl;
177 	unguard;
178 }
179 
180 //==========================================================================
181 //
182 //	VParser::ParseExpressionPriority0
183 //
184 //==========================================================================
185 
ParseExpressionPriority0()186 VExpression* VParser::ParseExpressionPriority0()
187 {
188 	guard(VParser::ParseExpressionPriority0);
189 	bool bLocals = CheckForLocal;
190 	CheckForLocal = false;
191 	TLocation l = Lex.Location;
192 	switch (Lex.Token)
193 	{
194 	case TK_IntLiteral:
195 	{
196 		vint32 Val = Lex.Number;
197 		Lex.NextToken();
198 		return new VIntLiteral(Val, l);
199 	}
200 
201 	case TK_FloatLiteral:
202 	{
203 		float Val = Lex.Float;
204 		Lex.NextToken();
205 		return new VFloatLiteral(Val, l);
206 	}
207 
208 	case TK_NameLiteral:
209 	{
210 		VName Val = Lex.Name;
211 		Lex.NextToken();
212 		return new VNameLiteral(Val, l);
213 	}
214 
215 	case TK_StringLiteral:
216 	{
217 		int Val = Package->FindString(Lex.String);
218 		Lex.NextToken();
219 		return new VStringLiteral(Val, l);
220 	}
221 
222 	case TK_Self:
223 		Lex.NextToken();
224 		return new VSelf(l);
225 
226 	case TK_None:
227 		Lex.NextToken();
228 		return new VNoneLiteral(l);
229 
230 	case TK_Null:
231 		Lex.NextToken();
232 		return new VNullLiteral(l);
233 
234 	case TK_False:
235 		Lex.NextToken();
236 		return new VIntLiteral(0, l);
237 
238 	case TK_True:
239 		Lex.NextToken();
240 		return new VIntLiteral(1, l);
241 
242 	case TK_Vector:
243 	{
244 		Lex.NextToken();
245 		Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
246 		VExpression* op1 = ParseExpressionPriority13();
247 		Lex.Expect(TK_Comma);
248 		VExpression* op2 = ParseExpressionPriority13();
249 		Lex.Expect(TK_Comma);
250 		VExpression* op3 = ParseExpressionPriority13();
251 		Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
252 		return new VVector(op1, op2, op3, l);
253 	}
254 
255 	case TK_LParen:
256 	{
257 		Lex.NextToken();
258 		VExpression* op = ParseExpressionPriority13();
259 		if (!op)
260 		{
261 			ParseError(l, "Expression expected");
262 		}
263 		Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
264 		return op;
265 	}
266 
267 	case TK_DColon:
268 	{
269 		Lex.NextToken();
270 		if (Lex.Token != TK_Identifier)
271 		{
272 			ParseError(l, "Method name expected.");
273 			break;
274 		}
275 		l = Lex.Location;
276 		VName Name = Lex.Name;
277 		Lex.NextToken();
278 		Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
279 		return ParseBaseMethodCall(Name, l);
280 	}
281 
282 	case TK_Identifier:
283 	{
284 		VName Name = Lex.Name;
285 		Lex.NextToken();
286 		if (Lex.Check(TK_LParen))
287 		{
288 			return ParseMethodCallOrCast(Name, l);
289 		}
290 
291 		if (Lex.Check(TK_DColon))
292 		{
293 			if (Lex.Token != TK_Identifier)
294 			{
295 				ParseError(Lex.Location, "Identifier expected");
296 				break;
297 			}
298 			VName Name2 = Lex.Name;
299 			Lex.NextToken();
300 			if (bLocals && Lex.Token == TK_Asterisk)
301 			{
302 				return ParseLocalVar(new VDoubleName(Name, Name2, l));
303 			}
304 			return new VDoubleName(Name, Name2, l);
305 		}
306 
307 		if (bLocals && Lex.Token == TK_Asterisk)
308 		{
309 			return ParseLocalVar(new VSingleName(Name, l));
310 		}
311 
312 		return new VSingleName(Name, l);
313 	}
314 
315 	case TK_Default:
316 	{
317 		VExpression* Expr = new VDefaultObject(new VSelf(l), l);
318 		Lex.NextToken();
319 		Lex.Expect(TK_Dot);
320 		if (Lex.Token != TK_Identifier)
321 		{
322 			ParseError(Lex.Location, "Invalid identifier, field name expacted");
323 		}
324 		VName FieldName = Lex.Name;
325 		TLocation Loc = Lex.Location;
326 		Lex.NextToken();
327 		if (Lex.Check(TK_LParen))
328 		{
329 			ParseError(Lex.Location, "Tried to call method on a default object");
330 		}
331 		return new VDotField(Expr, FieldName, Loc);
332 	}
333 
334 	case TK_Class:
335 	{
336 		Lex.NextToken();
337 		Lex.Expect(TK_Less);
338 		if (Lex.Token != TK_Identifier)
339 		{
340 			ParseError(Lex.Location, "Identifier expected");
341 			break;
342 		}
343 		VName ClassName = Lex.Name;
344 		Lex.NextToken();
345 		Lex.Expect(TK_Greater);
346 		Lex.Expect(TK_LParen);
347 		VExpression* Expr = ParseExpressionPriority13();
348 		if (!Expr)
349 		{
350 			ParseError(Lex.Location, "Expression expected");
351 		}
352 		Lex.Expect(TK_RParen);
353 		return new VDynamicClassCast(ClassName, Expr, l);
354 	}
355 
356 	default:
357 		break;
358 	}
359 
360 	return NULL;
361 	unguard;
362 }
363 
364 //==========================================================================
365 //
366 //	VParser::ParseExpressionPriority1
367 //
368 //==========================================================================
369 
ParseExpressionPriority1()370 VExpression* VParser::ParseExpressionPriority1()
371 {
372 	guard(VParser::ParseExpressionPriority1);
373 	VExpression* op = ParseExpressionPriority0();
374 	if (!op)
375 		return NULL;
376 	bool done = false;
377 	do
378 	{
379 		TLocation l = Lex.Location;
380 
381 		if (Lex.Check(TK_Arrow))
382 		{
383 			if (Lex.Token != TK_Identifier)
384 			{
385 				ParseError(Lex.Location, "Invalid identifier, field name expacted");
386 			}
387 			else
388 			{
389 				op = new VPointerField(op, Lex.Name, Lex.Location);
390 				Lex.NextToken();
391 			}
392 		}
393 		else if (Lex.Check(TK_Dot))
394 		{
395 			if (Lex.Check(TK_Default))
396 			{
397 				Lex.Expect(TK_Dot);
398 				op = new VDefaultObject(op, l);
399 			}
400 			if (Lex.Token != TK_Identifier)
401 			{
402 				ParseError(Lex.Location, "Invalid identifier, field name expacted");
403 			}
404 			else
405 			{
406 				VName FieldName = Lex.Name;
407 				TLocation Loc = Lex.Location;
408 				Lex.NextToken();
409 				if (Lex.Check(TK_LParen))
410 				{
411 					if (op->IsDefaultObject())
412 					{
413 						ParseError(Lex.Location, "Tried to call method on a default object");
414 					}
415 					op = ParseDotMethodCall(op, FieldName, Loc);
416 				}
417 				else
418 				{
419 					op = new VDotField(op, FieldName, Loc);
420 				}
421 			}
422 		}
423 		else if (Lex.Check(TK_LBracket))
424 		{
425 			VExpression* ind = ParseExpressionPriority13();
426 			Lex.Expect(TK_RBracket, ERR_BAD_ARRAY);
427 			op = new VArrayElement(op, ind, l);
428 		}
429 		else
430 		{
431 			done = true;
432 		}
433 	} while (!done);
434 
435 	return op;
436 	unguard;
437 }
438 
439 //==========================================================================
440 //
441 //	VParser::ParseExpressionPriority2
442 //
443 //==========================================================================
444 
ParseExpressionPriority2()445 VExpression* VParser::ParseExpressionPriority2()
446 {
447 	guard(VParser::ParseExpressionPriority2);
448 	VExpression*	op;
449 
450 	TLocation l = Lex.Location;
451 
452 	if (Lex.Check(TK_Plus))
453 	{
454 		op = ParseExpressionPriority2();
455 		return new VUnary(VUnary::Plus, op, l);
456 	}
457 
458 	if (Lex.Check(TK_Minus))
459 	{
460 		op = ParseExpressionPriority2();
461 		return new VUnary(VUnary::Minus, op, l);
462 	}
463 
464 	if (Lex.Check(TK_Not))
465 	{
466 		op = ParseExpressionPriority2();
467 		return new VUnary(VUnary::Not, op, l);
468 	}
469 
470 	if (Lex.Check(TK_Tilde))
471 	{
472 		op = ParseExpressionPriority2();
473 		return new VUnary(VUnary::BitInvert, op, l);
474 	}
475 
476 	if (Lex.Check(TK_And))
477 	{
478 		op = ParseExpressionPriority1();
479 		return new VUnary(VUnary::TakeAddress, op, l);
480 	}
481 
482 	if (Lex.Check(TK_Asterisk))
483 	{
484 		op = ParseExpressionPriority2();
485 		return new VPushPointed(op);
486 	}
487 
488 	if (Lex.Check(TK_Inc))
489 	{
490 		op = ParseExpressionPriority2();
491 		return new VUnaryMutator(VUnaryMutator::PreInc, op, l);
492 	}
493 
494 	if (Lex.Check(TK_Dec))
495 	{
496 		op = ParseExpressionPriority2();
497 		return new VUnaryMutator(VUnaryMutator::PreDec, op, l);
498 	}
499 
500 	op = ParseExpressionPriority1();
501 	if (!op)
502 		return NULL;
503 	l = Lex.Location;
504 
505 	if (Lex.Check(TK_Inc))
506 	{
507 		return new VUnaryMutator(VUnaryMutator::PostInc, op, l);
508 	}
509 
510 	if (Lex.Check(TK_Dec))
511 	{
512 		return new VUnaryMutator(VUnaryMutator::PostDec, op, l);
513 	}
514 
515 	return op;
516 	unguard;
517 }
518 
519 //==========================================================================
520 //
521 //	VParser::ParseExpressionPriority3
522 //
523 //==========================================================================
524 
ParseExpressionPriority3()525 VExpression* VParser::ParseExpressionPriority3()
526 {
527 	guard(VParser::ParseExpressionPriority3);
528 	VExpression* op1 = ParseExpressionPriority2();
529 	if (!op1)
530 		return NULL;
531 	bool done = false;
532 	do
533 	{
534 		TLocation l = Lex.Location;
535 		if (Lex.Check(TK_Asterisk))
536 		{
537 			VExpression* op2 = ParseExpressionPriority2();
538 			op1 = new VBinary(VBinary::Multiply, op1, op2, l);
539 		}
540 		else if (Lex.Check(TK_Slash))
541 		{
542 			VExpression* op2 = ParseExpressionPriority2();
543 			op1 = new VBinary(VBinary::Divide, op1, op2, l);
544 		}
545 		else if (Lex.Check(TK_Percent))
546 		{
547 			VExpression* op2 = ParseExpressionPriority2();
548 			op1 = new VBinary(VBinary::Modulus, op1, op2, l);
549 		}
550 		else
551 		{
552 			done = true;
553 		}
554 	}
555 	while (!done);
556 	return op1;
557 	unguard;
558 }
559 
560 //==========================================================================
561 //
562 //	VParser::ParseExpressionPriority4
563 //
564 //==========================================================================
565 
ParseExpressionPriority4()566 VExpression* VParser::ParseExpressionPriority4()
567 {
568 	guard(VParser::ParseExpressionPriority4);
569 	VExpression* op1 = ParseExpressionPriority3();
570 	if (!op1)
571 		return NULL;
572 	bool done = false;
573 	do
574 	{
575 		TLocation l = Lex.Location;
576 		if (Lex.Check(TK_Plus))
577 		{
578 			VExpression* op2 = ParseExpressionPriority3();
579 			op1 = new VBinary(VBinary::Add, op1, op2, l);
580 		}
581 		else if (Lex.Check(TK_Minus))
582 		{
583 			VExpression* op2 = ParseExpressionPriority3();
584 			op1 = new VBinary(VBinary::Subtract, op1, op2, l);
585 		}
586 		else
587 		{
588 			done = true;
589 		}
590 	}
591 	while (!done);
592 	return op1;
593 	unguard;
594 }
595 
596 //==========================================================================
597 //
598 //	VParser::ParseExpressionPriority5
599 //
600 //==========================================================================
601 
ParseExpressionPriority5()602 VExpression* VParser::ParseExpressionPriority5()
603 {
604 	guard(VParser::ParseExpressionPriority5);
605 	VExpression* op1 = ParseExpressionPriority4();
606 	if (!op1)
607 		return NULL;
608 	bool done = false;
609 	do
610 	{
611 		TLocation l = Lex.Location;
612 		if (Lex.Check(TK_LShift))
613 		{
614 			VExpression* op2 = ParseExpressionPriority4();
615 			op1 = new VBinary(VBinary::LShift, op1, op2, l);
616 		}
617 		else if (Lex.Check(TK_RShift))
618 		{
619 			VExpression* op2 = ParseExpressionPriority4();
620 			op1 = new VBinary(VBinary::RShift, op1, op2, l);
621 		}
622 		else
623 		{
624 			done = true;
625 		}
626 	}
627 	while (!done);
628 	return op1;
629 	unguard;
630 }
631 
632 //==========================================================================
633 //
634 //	VParser::ParseExpressionPriority6
635 //
636 //==========================================================================
637 
ParseExpressionPriority6()638 VExpression* VParser::ParseExpressionPriority6()
639 {
640 	guard(VParser::ParseExpressionPriority6);
641 	VExpression* op1 = ParseExpressionPriority5();
642 	if (!op1)
643 		return NULL;
644 	bool done = false;
645 	do
646 	{
647 		TLocation l = Lex.Location;
648 		if (Lex.Check(TK_Less))
649 		{
650 			VExpression* op2 = ParseExpressionPriority5();
651 			op1 = new VBinary(VBinary::Less, op1, op2, l);
652 		}
653 		else if (Lex.Check(TK_LessEquals))
654 		{
655 			VExpression* op2 = ParseExpressionPriority5();
656 			op1 = new VBinary(VBinary::LessEquals, op1, op2, l);
657 		}
658 		else if (Lex.Check(TK_Greater))
659 		{
660 			VExpression* op2 = ParseExpressionPriority5();
661 			op1 = new VBinary(VBinary::Greater, op1, op2, l);
662 		}
663 		else if (Lex.Check(TK_GreaterEquals))
664 		{
665 			VExpression* op2 = ParseExpressionPriority5();
666 			op1 = new VBinary(VBinary::GreaterEquals, op1, op2, l);
667 		}
668 		else
669 		{
670 			done = true;
671 		}
672 	}
673 	while (!done);
674 	return op1;
675 	unguard;
676 }
677 
678 //==========================================================================
679 //
680 //	VParser::ParseExpressionPriority7
681 //
682 //==========================================================================
683 
ParseExpressionPriority7()684 VExpression* VParser::ParseExpressionPriority7()
685 {
686 	guard(VParser::ParseExpressionPriority7);
687 	VExpression* op1 = ParseExpressionPriority6();
688 	if (!op1)
689 		return NULL;
690 	bool done = false;
691 	do
692 	{
693 		TLocation l = Lex.Location;
694 		if (Lex.Check(TK_Equals))
695 		{
696 			VExpression* op2 = ParseExpressionPriority6();
697 			op1 = new VBinary(VBinary::Equals, op1, op2, l);
698 		}
699 		else if (Lex.Check(TK_NotEquals))
700 		{
701 			VExpression* op2 = ParseExpressionPriority6();
702 			op1 = new VBinary(VBinary::NotEquals, op1, op2, l);
703 		}
704 		else
705 		{
706 			done = true;
707 		}
708 	} while (!done);
709 	return op1;
710 	unguard;
711 }
712 
713 //==========================================================================
714 //
715 //	VParser::ParseExpressionPriority8
716 //
717 //==========================================================================
718 
ParseExpressionPriority8()719 VExpression* VParser::ParseExpressionPriority8()
720 {
721 	guard(VParser::ParseExpressionPriority8);
722 	VExpression* op1 = ParseExpressionPriority7();
723 	if (!op1)
724 		return NULL;
725 	TLocation l = Lex.Location;
726 	while (Lex.Check(TK_And))
727 	{
728 		VExpression* op2 = ParseExpressionPriority7();
729 		op1 = new VBinary(VBinary::And, op1, op2, l);
730 		l = Lex.Location;
731 	}
732 	return op1;
733 	unguard;
734 }
735 
736 //==========================================================================
737 //
738 //	VParser::ParseExpressionPriority9
739 //
740 //==========================================================================
741 
ParseExpressionPriority9()742 VExpression* VParser::ParseExpressionPriority9()
743 {
744 	guard(VParser::ParseExpressionPriority9);
745 	VExpression* op1 = ParseExpressionPriority8();
746 	if (!op1)
747 		return NULL;
748 	TLocation l = Lex.Location;
749 	while (Lex.Check(TK_XOr))
750 	{
751 		VExpression* op2 = ParseExpressionPriority8();
752 		op1 = new VBinary(VBinary::XOr, op1, op2, l);
753 		l = Lex.Location;
754 	}
755 	return op1;
756 	unguard;
757 }
758 
759 //==========================================================================
760 //
761 //	VParser::ParseExpressionPriority10
762 //
763 //==========================================================================
764 
ParseExpressionPriority10()765 VExpression* VParser::ParseExpressionPriority10()
766 {
767 	guard(VParser::ParseExpressionPriority10);
768 	VExpression* op1 = ParseExpressionPriority9();
769 	if (!op1)
770 		return NULL;
771 	TLocation l = Lex.Location;
772 	while (Lex.Check(TK_Or))
773 	{
774 		VExpression* op2 = ParseExpressionPriority9();
775 		op1 = new VBinary(VBinary::Or, op1, op2, l);
776 		l = Lex.Location;
777 	}
778 	return op1;
779 	unguard;
780 }
781 
782 //==========================================================================
783 //
784 //	VParser::ParseExpressionPriority11
785 //
786 //==========================================================================
787 
ParseExpressionPriority11()788 VExpression* VParser::ParseExpressionPriority11()
789 {
790 	guard(VParser::ParseExpressionPriority11);
791 	VExpression* op1 = ParseExpressionPriority10();
792 	if (!op1)
793 		return NULL;
794 	TLocation l = Lex.Location;
795 	while (Lex.Check(TK_AndLog))
796 	{
797 		VExpression* op2 = ParseExpressionPriority10();
798 		op1 = new VBinaryLogical(VBinaryLogical::And, op1, op2, l);
799 		l = Lex.Location;
800 	}
801 	return op1;
802 	unguard;
803 }
804 
805 //==========================================================================
806 //
807 //	VParser::ParseExpressionPriority12
808 //
809 //==========================================================================
810 
ParseExpressionPriority12()811 VExpression* VParser::ParseExpressionPriority12()
812 {
813 	guard(VParser::ParseExpressionPriority12);
814 	VExpression* op1 = ParseExpressionPriority11();
815 	if (!op1)
816 		return NULL;
817 	TLocation l = Lex.Location;
818 	while (Lex.Check(TK_OrLog))
819 	{
820 		VExpression* op2 = ParseExpressionPriority11();
821 		op1 = new VBinaryLogical(VBinaryLogical::Or, op1, op2, l);
822 		l = Lex.Location;
823 	}
824 	return op1;
825 	unguard;
826 }
827 
828 //==========================================================================
829 //
830 //	VParser::ParseExpressionPriority13
831 //
832 //==========================================================================
833 
ParseExpressionPriority13()834 VExpression* VParser::ParseExpressionPriority13()
835 {
836 	guard(VParser::ParseExpressionPriority13);
837 	VExpression* op = ParseExpressionPriority12();
838 	if (!op)
839 		return NULL;
840 	TLocation l = Lex.Location;
841 	if (Lex.Check(TK_Quest))
842 	{
843 		VExpression* op1 = ParseExpressionPriority13();
844 		Lex.Expect(TK_Colon, ERR_MISSING_COLON);
845 		VExpression* op2 = ParseExpressionPriority13();
846 		op = new VConditional(op, op1, op2, l);
847 	}
848 	return op;
849 	unguard;
850 }
851 
852 //==========================================================================
853 //
854 //	VParser::ParseExpressionPriority14
855 //
856 //==========================================================================
857 
ParseExpressionPriority14()858 VExpression* VParser::ParseExpressionPriority14()
859 {
860 	guard(VParser::ParseExpressionPriority14);
861 	VExpression* op1 = ParseExpressionPriority13();
862 	if (!op1)
863 		return NULL;
864 	TLocation l = Lex.Location;
865 	if (Lex.Check(TK_Assign))
866 	{
867 		VExpression* op2 = ParseExpressionPriority13();
868 		return new VAssignment(VAssignment::Assign, op1, op2, l);
869 	}
870 	else if (Lex.Check(TK_AddAssign))
871 	{
872 		VExpression* op2 = ParseExpressionPriority13();
873 		return new VAssignment(VAssignment::AddAssign, op1, op2, l);
874 	}
875 	else if (Lex.Check(TK_MinusAssign))
876 	{
877 		VExpression* op2 = ParseExpressionPriority13();
878 		return new VAssignment(VAssignment::MinusAssign, op1, op2, l);
879 	}
880 	else if (Lex.Check(TK_MultiplyAssign))
881 	{
882 		VExpression* op2 = ParseExpressionPriority13();
883 		return new VAssignment(VAssignment::MultiplyAssign, op1, op2, l);
884 	}
885 	else if (Lex.Check(TK_DivideAssign))
886 	{
887 		VExpression* op2 = ParseExpressionPriority13();
888 		return new VAssignment(VAssignment::DivideAssign, op1, op2, l);
889 	}
890 	else if (Lex.Check(TK_ModAssign))
891 	{
892 		VExpression* op2 = ParseExpressionPriority13();
893 		return new VAssignment(VAssignment::ModAssign, op1, op2, l);
894 	}
895 	else if (Lex.Check(TK_AndAssign))
896 	{
897 		VExpression* op2 = ParseExpressionPriority13();
898 		return new VAssignment(VAssignment::AndAssign, op1, op2, l);
899 	}
900 	else if (Lex.Check(TK_OrAssign))
901 	{
902 		VExpression* op2 = ParseExpressionPriority13();
903 		return new VAssignment(VAssignment::OrAssign, op1, op2, l);
904 	}
905 	else if (Lex.Check(TK_XOrAssign))
906 	{
907 		VExpression* op2 = ParseExpressionPriority13();
908 		return new VAssignment(VAssignment::XOrAssign, op1, op2, l);
909 	}
910 	else if (Lex.Check(TK_LShiftAssign))
911 	{
912 		VExpression* op2 = ParseExpressionPriority13();
913 		return new VAssignment(VAssignment::LShiftAssign, op1, op2, l);
914 	}
915 	else if (Lex.Check(TK_RShiftAssign))
916 	{
917 		VExpression* op2 = ParseExpressionPriority13();
918 		return new VAssignment(VAssignment::RShiftAssign, op1, op2, l);
919 	}
920 	return op1;
921 	unguard;
922 }
923 
924 //==========================================================================
925 //
926 //	VParser::ParseExpression
927 //
928 //==========================================================================
929 
ParseExpression()930 VExpression* VParser::ParseExpression()
931 {
932 	guard(VParser::ParseExpression);
933 	CheckForLocal = false;
934 	return ParseExpressionPriority14();
935 	unguard;
936 }
937 
938 //==========================================================================
939 //
940 //	VParser::ParseStatement
941 //
942 //==========================================================================
943 
ParseStatement()944 VStatement* VParser::ParseStatement()
945 {
946 	guard(VParser::ParseStatement);
947 	TLocation l = Lex.Location;
948 	switch(Lex.Token)
949 	{
950 	case TK_EOF:
951 		ParseError(Lex.Location, ERR_UNEXPECTED_EOF);
952 		return NULL;
953 
954 	case TK_If:
955 	{
956 		Lex.NextToken();
957 		Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
958 		VExpression* e = ParseExpression();
959 		if (!e)
960 		{
961 			ParseError(Lex.Location, "If expression expected");
962 		}
963 		Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
964 		VStatement* STrue = ParseStatement();
965 		if (Lex.Check(TK_Else))
966 		{
967 			VStatement* SFalse = ParseStatement();
968 			return new VIf(e, STrue, SFalse, l);
969 		}
970 		else
971 		{
972 			return new VIf(e, STrue, l);
973 		}
974 	}
975 
976 	case TK_While:
977 	{
978 		Lex.NextToken();
979 		Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
980 		VExpression* Expr = ParseExpression();
981 		if (!Expr)
982 		{
983 			ParseError(Lex.Location, "Wile loop expression expected");
984 		}
985 		Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
986 		VStatement* Statement = ParseStatement();
987 		return new VWhile(Expr, Statement, l);
988 	}
989 
990 	case TK_Do:
991 	{
992 		Lex.NextToken();
993 		VStatement* Statement = ParseStatement();
994 		Lex.Expect(TK_While, ERR_BAD_DO_STATEMENT);
995 		Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
996 		VExpression* Expr = ParseExpression();
997 		if (!Expr)
998 		{
999 			ParseError(Lex.Location, "Do loop expression expected");
1000 		}
1001 		Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
1002 		Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
1003 		return new VDo(Expr, Statement, l);
1004 	}
1005 
1006 	case TK_For:
1007 	{
1008 		Lex.NextToken();
1009 		VFor* For = new VFor(l);
1010 		Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
1011 		do
1012 		{
1013 			VExpression* Expr = ParseExpression();
1014 			if (!Expr)
1015 			{
1016 				break;
1017 			}
1018 			For->InitExpr.Append(new VDropResult(Expr));
1019 		} while (Lex.Check(TK_Comma));
1020 		Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
1021 		For->CondExpr = ParseExpression();
1022 		Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
1023 		do
1024 		{
1025 			VExpression* Expr = ParseExpression();
1026 			if (!Expr)
1027 			{
1028 				break;
1029 			}
1030 			For->LoopExpr.Append(new VDropResult(Expr));
1031 		} while (Lex.Check(TK_Comma));
1032 		Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
1033 		VStatement* Statement = ParseStatement();
1034 		For->Statement = Statement;
1035 		return For;
1036 	}
1037 
1038 	case TK_Foreach:
1039 	{
1040 		Lex.NextToken();
1041 		VExpression* Expr = ParseExpression();
1042 		if (!Expr)
1043 		{
1044 			ParseError(Lex.Location, "Iterator expression expected");
1045 		}
1046 		VStatement* Statement = ParseStatement();
1047 		return new VForeach(Expr, Statement, l);
1048 	}
1049 
1050 	case TK_Break:
1051 		Lex.NextToken();
1052 		Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
1053 		return new VBreak(l);
1054 
1055 	case TK_Continue:
1056 		Lex.NextToken();
1057 		Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
1058 		return new VContinue(l);
1059 
1060 	case TK_Return:
1061 	{
1062 		Lex.NextToken();
1063 		VExpression* Expr = ParseExpression();
1064 		Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
1065 		return new VReturn(Expr, l);
1066 	}
1067 
1068 	case TK_Switch:
1069 	{
1070 		Lex.NextToken();
1071 		VSwitch* Switch = new VSwitch(l);
1072 		Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
1073 		Switch->Expr = ParseExpression();
1074 		if (!Switch->Expr)
1075 		{
1076 			ParseError(Lex.Location, "Switch expression expected");
1077 		}
1078 		Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
1079 
1080 		Lex.Expect(TK_LBrace, ERR_MISSING_LBRACE);
1081 		do
1082 		{
1083 			l = Lex.Location;
1084 			if (Lex.Check(TK_Case))
1085 			{
1086 				VExpression* Expr = ParseExpression();
1087 				if (!Expr)
1088 				{
1089 					ParseError(Lex.Location, "Case value expected");
1090 				}
1091 				Lex.Expect(TK_Colon, ERR_MISSING_COLON);
1092 				Switch->Statements.Append(new VSwitchCase(Switch, Expr, l));
1093 			}
1094 			else if (Lex.Check(TK_Default))
1095 			{
1096 				Lex.Expect(TK_Colon, ERR_MISSING_COLON);
1097 				Switch->Statements.Append(new VSwitchDefault(Switch, l));
1098 			}
1099 			else
1100 			{
1101 				VStatement* Statement = ParseStatement();
1102 				Switch->Statements.Append(Statement);
1103 			}
1104 		} while (!Lex.Check(TK_RBrace));
1105 		return Switch;
1106 	}
1107 
1108 	case TK_LBrace:
1109 		Lex.NextToken();
1110 		return ParseCompoundStatement();
1111 
1112 	case TK_Bool:
1113 	case TK_Byte:
1114 	case TK_Class:
1115 	case TK_Float:
1116 	case TK_Int:
1117 	case TK_Name:
1118 	case TK_State:
1119 	case TK_String:
1120 	case TK_Void:
1121 	case TK_Array:
1122 	{
1123 		VExpression* TypeExpr = ParseType();
1124 		VLocalDecl* Decl = ParseLocalVar(TypeExpr);
1125 		Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
1126 		return new VLocalVarStatement(Decl);
1127 	}
1128 
1129 	default:
1130 		CheckForLocal = true;
1131 		VExpression* Expr = ParseExpressionPriority14();
1132 		if (!Expr)
1133 		{
1134 			if (!Lex.Check(TK_Semicolon))
1135 			{
1136 				ParseError(l, "Token %s makes no sense here", VLexer::TokenNames[Lex.Token]);
1137 				Lex.NextToken();
1138 			}
1139 			return new VEmptyStatement(l);
1140 		}
1141 		else if (Expr->IsValidTypeExpression() && Lex.Token == TK_Identifier)
1142 		{
1143 			VLocalDecl* Decl = ParseLocalVar(Expr);
1144 			Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
1145 			return new VLocalVarStatement(Decl);
1146 		}
1147 		else
1148 		{
1149 			Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
1150 			return new VExpressionStatement(new VDropResult(Expr));
1151 		}
1152 	}
1153 	unguard;
1154 }
1155 
1156 //==========================================================================
1157 //
1158 //	VParser::ParseCompoundStatement
1159 //
1160 //==========================================================================
1161 
ParseCompoundStatement()1162 VCompound* VParser::ParseCompoundStatement()
1163 {
1164 	guard(VParser::ParseCompoundStatement);
1165 	VCompound* Comp = new VCompound(Lex.Location);
1166 	while (!Lex.Check(TK_RBrace))
1167 	{
1168 		Comp->Statements.Append(ParseStatement());
1169 	}
1170 	return Comp;
1171 	unguard;
1172 }
1173 
1174 //==========================================================================
1175 //
1176 //	VParser::ParseType
1177 //
1178 //==========================================================================
1179 
ParseType()1180 VExpression* VParser::ParseType()
1181 {
1182 	guard(VParser::ParseType);
1183 	TLocation l = Lex.Location;
1184 	switch (Lex.Token)
1185 	{
1186 	case TK_Void:
1187 		Lex.NextToken();
1188 		return new VTypeExpr(TYPE_Void, l);
1189 
1190 	case TK_Int:
1191 		Lex.NextToken();
1192 		return new VTypeExpr(TYPE_Int, l);
1193 
1194 	case TK_Byte:
1195 		Lex.NextToken();
1196 		return new VTypeExpr(TYPE_Byte, l);
1197 
1198 	case TK_Bool:
1199 	{
1200 		Lex.NextToken();
1201 		VFieldType ret(TYPE_Bool);
1202 		ret.BitMask = 1;
1203 		return new VTypeExpr(ret, l);
1204 	}
1205 
1206 	case TK_Float:
1207 		Lex.NextToken();
1208 		return new VTypeExpr(TYPE_Float, l);
1209 
1210 	case TK_Name:
1211 		Lex.NextToken();
1212 		return new VTypeExpr(TYPE_Name, l);
1213 
1214 	case TK_String:
1215 		Lex.NextToken();
1216 		return new VTypeExpr(TYPE_String, l);
1217 
1218 	case TK_Class:
1219 	{
1220 		Lex.NextToken();
1221 		VName MetaClassName = NAME_None;
1222 		if (Lex.Check(TK_Less))
1223 		{
1224 			if (Lex.Token != TK_Identifier)
1225 			{
1226 				ParseError(Lex.Location, "Invalid identifier, class name expected");
1227 			}
1228 			else
1229 			{
1230 				MetaClassName = Lex.Name;
1231 				Lex.NextToken();
1232 			}
1233 			Lex.Expect(TK_Greater);
1234 		}
1235 		return new VTypeExpr(TYPE_Class, l, MetaClassName);
1236 	}
1237 
1238 	case TK_State:
1239 		Lex.NextToken();
1240 		return new VTypeExpr(TYPE_State, l);
1241 
1242 	case TK_Identifier:
1243 	{
1244 		VName Name = Lex.Name;
1245 		Lex.NextToken();
1246 		if (Lex.Check(TK_DColon))
1247 		{
1248 			if (Lex.Token != TK_Identifier)
1249 			{
1250 				ParseError(Lex.Location, "Identifier expected");
1251 				return new VSingleName(Name, l);
1252 			}
1253 			VName Name2 = Lex.Name;
1254 			Lex.NextToken();
1255 			return new VDoubleName(Name, Name2, l);
1256 		}
1257 		return new VSingleName(Name, l);
1258 	}
1259 
1260 	case TK_Array:
1261 	{
1262 		Lex.NextToken();
1263 		Lex.Expect(TK_Less);
1264 		VExpression* Inner = ParseType();
1265 		if (!Inner)
1266 		{
1267 			ParseError(Lex.Location, "Inner type declaration expected");
1268 		}
1269 		while (Lex.Check(TK_Asterisk))
1270 		{
1271 			Inner = new VPointerType(Inner, Lex.Location);
1272 		}
1273 		Lex.Expect(TK_Greater);
1274 		return new VDynamicArrayType(Inner, l);
1275 	}
1276 
1277 	default:
1278 		return NULL;
1279 	}
1280 	unguard;
1281 }
1282 
1283 //==========================================================================
1284 //
1285 //	VParser::ParseMethodDef
1286 //
1287 //==========================================================================
1288 
ParseMethodDef(VExpression * RetType,VName MName,TLocation MethodLoc,VClass * InClass,vint32 Modifiers,bool Iterator)1289 void VParser::ParseMethodDef(VExpression* RetType, VName MName,
1290 	TLocation MethodLoc, VClass* InClass, vint32 Modifiers, bool Iterator)
1291 {
1292 	guard(VParser::ParseMethodDef);
1293 	if (InClass->FindMethod(MName, false))
1294 	{
1295 		ParseError(MethodLoc, "Redeclared method %s.%s", *InClass->Name, *MName);
1296 	}
1297 
1298 	VMethod* Func = new VMethod(MName, InClass, MethodLoc);
1299 	Func->Flags = TModifiers::MethodAttr(TModifiers::Check(Modifiers,
1300 		TModifiers::Native | TModifiers::Static | TModifiers::Final |
1301 		TModifiers::Spawner, MethodLoc));
1302 	Func->ReturnTypeExpr = RetType;
1303 	if (Iterator)
1304 	{
1305 		Func->Flags |= FUNC_Iterator;
1306 	}
1307 	InClass->AddMethod(Func);
1308 
1309 	do
1310 	{
1311 		if (Lex.Check(TK_VarArgs))
1312 		{
1313 			Func->Flags |= FUNC_VarArgs;
1314 			break;
1315 		}
1316 
1317 		VMethodParam& P = Func->Params[Func->NumParams];
1318 
1319 		int ParmModifiers = TModifiers::Parse(Lex);
1320 		Func->ParamFlags[Func->NumParams] = TModifiers::ParmAttr(
1321 			TModifiers::Check(ParmModifiers, TModifiers::Optional |
1322 			TModifiers::Out, Lex.Location));
1323 
1324 		P.TypeExpr = ParseType();
1325 		if (!P.TypeExpr && Func->NumParams == 0)
1326 		{
1327 			break;
1328 		}
1329 		TLocation l = Lex.Location;
1330 		while (Lex.Check(TK_Asterisk))
1331 		{
1332 			P.TypeExpr = new VPointerType(P.TypeExpr, l);;
1333 			l = Lex.Location;
1334 		}
1335 		if (Lex.Token == TK_Identifier)
1336 		{
1337 			P.Name = Lex.Name;
1338 			P.Loc = Lex.Location;
1339 			Lex.NextToken();
1340 		}
1341 		if (Func->NumParams == VMethod::MAX_PARAMS)
1342 		{
1343 			ParseError(Lex.Location, "Method parameters overflow");
1344 			continue;
1345 		}
1346 		Func->NumParams++;
1347 	} while (Lex.Check(TK_Comma));
1348 	Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
1349 
1350 	if (Lex.Check(TK_Semicolon))
1351 	{
1352 		Package->NumBuiltins++;
1353 	}
1354 	else
1355 	{
1356 		//	Self type specifier.
1357 		if (Lex.Check(TK_Self))
1358 		{
1359 			Lex.Expect(TK_LParen);
1360 			if (Lex.Token != TK_Identifier)
1361 			{
1362 				ParseError(Lex.Location, "Class name expected");
1363 			}
1364 			else
1365 			{
1366 				Func->SelfTypeName = Lex.Name;
1367 				Lex.NextToken();
1368 			}
1369 			Lex.Expect(TK_RParen);
1370 		}
1371 
1372 		Lex.Expect(TK_LBrace, ERR_MISSING_LBRACE);
1373 		Func->Statement = ParseCompoundStatement();
1374 	}
1375 	unguard;
1376 }
1377 
1378 //==========================================================================
1379 //
1380 //	VParser::ParseDelegate
1381 //
1382 //==========================================================================
1383 
ParseDelegate(VExpression * RetType,VField * Delegate)1384 void VParser::ParseDelegate(VExpression* RetType, VField* Delegate)
1385 {
1386 	guard(VParser::ParseDelegate);
1387 	VMethod* Func = new VMethod(NAME_None, Delegate, Delegate->Loc);
1388 	Func->ReturnTypeExpr = RetType;
1389 
1390 	Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
1391 	do
1392 	{
1393 		VMethodParam& P = Func->Params[Func->NumParams];
1394 
1395 		P.TypeExpr = ParseType();
1396 		if (!P.TypeExpr && Func->NumParams == 0)
1397 		{
1398 			break;
1399 		}
1400 		TLocation l = Lex.Location;
1401 		while (Lex.Check(TK_Asterisk))
1402 		{
1403 			P.TypeExpr = new VPointerType(P.TypeExpr, l);;
1404 			l = Lex.Location;
1405 		}
1406 		if (Lex.Token == TK_Identifier)
1407 		{
1408 			P.Name = Lex.Name;
1409 			P.Loc = Lex.Location;
1410 			Lex.NextToken();
1411 		}
1412 		if (Func->NumParams == VMethod::MAX_PARAMS)
1413 		{
1414 			ParseError(Lex.Location, "Method parameters overflow");
1415 			continue;
1416 		}
1417 		Func->NumParams++;
1418 	} while (Lex.Check(TK_Comma));
1419 	Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
1420 	Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
1421 
1422 	Delegate->Func = Func;
1423 	Delegate->Type = VFieldType(TYPE_Delegate);
1424 	Delegate->Type.Function = Func;
1425 	unguard;
1426 }
1427 
1428 //==========================================================================
1429 //
1430 //	VParser::ParseDefaultProperties
1431 //
1432 //==========================================================================
1433 
ParseDefaultProperties(VClass * InClass)1434 void VParser::ParseDefaultProperties(VClass* InClass)
1435 {
1436 	guard(VParser::ParseDefaultProperties);
1437 	VMethod* Func = new VMethod(NAME_None, InClass, Lex.Location);
1438 	Func->ReturnTypeExpr = new VTypeExpr(TYPE_Void, Lex.Location);
1439 	Func->ReturnType = VFieldType(TYPE_Void);
1440 	InClass->DefaultProperties = Func;
1441 
1442 	Lex.Expect(TK_LBrace, ERR_MISSING_LBRACE);
1443 	Func->Statement = ParseCompoundStatement();
1444 	unguard;
1445 }
1446 
1447 //==========================================================================
1448 //
1449 //	VParser::ParseStruct
1450 //
1451 //==========================================================================
1452 
ParseStruct(VClass * InClass,bool IsVector)1453 void VParser::ParseStruct(VClass* InClass, bool IsVector)
1454 {
1455 	guard(VParser::ParseStruct);
1456 	VName Name = Lex.Name;
1457 	TLocation StrLoc = Lex.Location;
1458 	if (Lex.Token != TK_Identifier)
1459 	{
1460 		ParseError(Lex.Location, "Struct name expected");
1461 		Name = NAME_None;
1462 	}
1463 	else
1464 	{
1465 		Lex.NextToken();
1466 	}
1467 
1468 	//	New struct
1469 	VStruct* Struct = new VStruct(Name, InClass ? (VMemberBase*)InClass :
1470 		(VMemberBase*)Package, StrLoc);
1471 	Struct->Defined = false;
1472 	Struct->IsVector = IsVector;
1473 	Struct->Fields = NULL;
1474 
1475 	if (!IsVector && Lex.Check(TK_Colon))
1476 	{
1477 		if (Lex.Token != TK_Identifier)
1478 		{
1479 			ParseError(Lex.Location, "Parent class name expected");
1480 		}
1481 		else
1482 		{
1483 			Struct->ParentStructName = Lex.Name;
1484 			Struct->ParentStructLoc = Lex.Location;
1485 			Lex.NextToken();
1486 		}
1487 	}
1488 
1489 	Lex.Expect(TK_LBrace, ERR_MISSING_LBRACE);
1490 	while (!Lex.Check(TK_RBrace))
1491 	{
1492 		vint32 Modifiers = TModifiers::Parse(Lex);
1493 
1494 		VExpression* Type = ParseType();
1495 		if (!Type)
1496 		{
1497 			ParseError(Lex.Location, "Field type expected.");
1498 			Lex.NextToken();
1499 			continue;
1500 		}
1501 
1502 		do
1503 		{
1504 			VExpression* FieldType = Type->CreateTypeExprCopy();
1505 			TLocation l = Lex.Location;
1506 			while (Lex.Check(TK_Asterisk))
1507 			{
1508 				FieldType = new VPointerType(FieldType, l);
1509 				l = Lex.Location;
1510 			}
1511 
1512 			VName FieldName(NAME_None);
1513 			TLocation FieldLoc = Lex.Location;
1514 			if (Lex.Token != TK_Identifier)
1515 			{
1516 				ParseError(Lex.Location, "Field name expected");
1517 			}
1518 			else
1519 			{
1520 				FieldName = Lex.Name;
1521 			}
1522 			Lex.NextToken();
1523 			if (Lex.Check(TK_LBracket))
1524 			{
1525 				TLocation SLoc = Lex.Location;
1526 				VExpression* e = ParseExpression();
1527 				Lex.Expect(TK_RBracket, ERR_MISSING_RFIGURESCOPE);
1528 				FieldType = new VFixedArrayType(FieldType, e, SLoc);
1529 			}
1530 			VField* fi = new VField(FieldName, Struct, FieldLoc);
1531 			fi->TypeExpr = FieldType;
1532 			fi->Flags = TModifiers::FieldAttr(TModifiers::Check(Modifiers,
1533 				TModifiers::Native | TModifiers::Private |
1534 				TModifiers::ReadOnly | TModifiers::Transient, FieldLoc));
1535 			Struct->AddField(fi);
1536 		} while (Lex.Check(TK_Comma));
1537 		delete Type;
1538 		Type = NULL;
1539 		Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
1540 	}
1541 	Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
1542 
1543 	if (InClass)
1544 	{
1545 		InClass->Structs.Append(Struct);
1546 	}
1547 	else
1548 	{
1549 		Package->ParsedStructs.Append(Struct);
1550 	}
1551 	unguard;
1552 }
1553 
1554 //==========================================================================
1555 //
1556 //	VParser::ParseStateString
1557 //
1558 //==========================================================================
1559 
ParseStateString()1560 VName VParser::ParseStateString()
1561 {
1562 	guard(VParser::ParseStateString);
1563 	VStr		StateStr;
1564 
1565 	if (Lex.Token != TK_Identifier && Lex.Token != TK_StringLiteral)
1566 	{
1567 		ParseError(Lex.Location, "Identifier expected");
1568 		return NAME_None;
1569 	}
1570 	StateStr = Lex.String;
1571 	Lex.NextToken();
1572 
1573 	if (Lex.Check(TK_DColon))
1574 	{
1575 		if (Lex.Token != TK_Identifier)
1576 		{
1577 			ParseError(Lex.Location, "Identifier expected");
1578 			return NAME_None;
1579 		}
1580 		StateStr += "::";
1581 		StateStr += *Lex.Name;
1582 		Lex.NextToken();
1583 	}
1584 
1585 	if (Lex.Check(TK_Dot))
1586 	{
1587 		if (Lex.Token != TK_Identifier)
1588 		{
1589 			ParseError(Lex.Location, "Identifier expected");
1590 			return NAME_None;
1591 		}
1592 		StateStr += ".";
1593 		StateStr += *Lex.Name;
1594 		Lex.NextToken();
1595 	}
1596 
1597 	return *StateStr;
1598 	unguard;
1599 }
1600 
1601 //==========================================================================
1602 //
1603 //	VParser::ParseStates
1604 //
1605 //==========================================================================
1606 
ParseStates(VClass * InClass)1607 void VParser::ParseStates(VClass* InClass)
1608 {
1609 	guard(VParser::ParseStates);
1610 	Lex.Expect(TK_LBrace, ERR_MISSING_LBRACE);
1611 	int StateIdx = 0;
1612 	VState* PrevState = NULL;
1613 	VState* LoopStart = NULL;
1614 	int NewLabelsStart = InClass->StateLabelDefs.Num();
1615 	while (!Lex.Check(TK_RBrace))
1616 	{
1617 		TLocation TmpLoc = Lex.Location;
1618 		VName TmpName = ParseStateString();
1619 
1620 		//	Goto command.
1621 		if (TmpName == NAME_Goto)
1622 		{
1623 			VName GotoLabel = ParseStateString();
1624 			int GotoOffset = 0;
1625 			if (Lex.Check(TK_Plus))
1626 			{
1627 				if (Lex.Token != TK_IntLiteral)
1628 				{
1629 					ParseError(Lex.Location, "Number expected");
1630 					continue;
1631 				}
1632 				GotoOffset = Lex.Number;
1633 				Lex.NextToken();
1634 			}
1635 
1636 			if (!PrevState && NewLabelsStart == InClass->StateLabelDefs.Num())
1637 			{
1638 				ParseError(Lex.Location, "Goto before first state");
1639 				continue;
1640 			}
1641 			if (PrevState)
1642 			{
1643 				PrevState->GotoLabel = GotoLabel;
1644 				PrevState->GotoOffset = GotoOffset;
1645 			}
1646 			for (int i = NewLabelsStart; i < InClass->StateLabelDefs.Num(); i++)
1647 			{
1648 				InClass->StateLabelDefs[i].GotoLabel = GotoLabel;
1649 				InClass->StateLabelDefs[i].GotoOffset = GotoOffset;
1650 			}
1651 			NewLabelsStart = InClass->StateLabelDefs.Num();
1652 			PrevState = NULL;
1653 			continue;
1654 		}
1655 
1656 		//	Stop command.
1657 		if (TmpName == NAME_Stop)
1658 		{
1659 			if (!PrevState && NewLabelsStart == InClass->StateLabelDefs.Num())
1660 			{
1661 				ParseError(Lex.Location, "Stop before first state");
1662 				continue;
1663 			}
1664 			if (PrevState)
1665 			{
1666 				PrevState->NextState = NULL;
1667 			}
1668 			for (int i = NewLabelsStart; i < InClass->StateLabelDefs.Num(); i++)
1669 			{
1670 				InClass->StateLabelDefs[i].State = NULL;
1671 			}
1672 			NewLabelsStart = InClass->StateLabelDefs.Num();
1673 			PrevState = NULL;
1674 			continue;
1675 		}
1676 
1677 		//	Wait command.
1678 		if (TmpName == NAME_Wait || TmpName == NAME_Fail)
1679 		{
1680 			if (!PrevState)
1681 			{
1682 				ParseError(Lex.Location, "%s before first state", *TmpName);
1683 				continue;
1684 			}
1685 			PrevState->NextState = PrevState;
1686 			PrevState = NULL;
1687 			continue;
1688 		}
1689 
1690 		//	Loop command.
1691 		if (TmpName == NAME_Loop)
1692 		{
1693 			if (!PrevState)
1694 			{
1695 				ParseError(Lex.Location, "Loop before first state");
1696 				continue;
1697 			}
1698 			PrevState->NextState = LoopStart;
1699 			PrevState = NULL;
1700 			continue;
1701 		}
1702 
1703 		//	Check for label.
1704 		if (Lex.Check(TK_Colon))
1705 		{
1706 			VStateLabelDef& Lbl = InClass->StateLabelDefs.Alloc();
1707 			Lbl.Loc = TmpLoc;
1708 			Lbl.Name = *TmpName;
1709 			continue;
1710 		}
1711 
1712 		char StateName[16];
1713 		sprintf(StateName, "S_%d", StateIdx);
1714 		VState* s = new VState(StateName, InClass, TmpLoc);
1715 		InClass->AddState(s);
1716 
1717 		//	Sprite name
1718 		char SprName[8];
1719 		SprName[0] = 0;
1720 		if (VStr::Length(*TmpName) != 4)
1721 		{
1722 			ParseError(Lex.Location, "Invalid sprite name");
1723 		}
1724 		else
1725 		{
1726 			SprName[0] = VStr::ToLower((*TmpName)[0]);
1727 			SprName[1] = VStr::ToLower((*TmpName)[1]);
1728 			SprName[2] = VStr::ToLower((*TmpName)[2]);
1729 			SprName[3] = VStr::ToLower((*TmpName)[3]);
1730 			SprName[4] = 0;
1731 		}
1732 		s->SpriteName = SprName;
1733 
1734 		//  Frame
1735 		VName FramesString(NAME_None);
1736 		TLocation FramesLoc;
1737 		if (Lex.Token != TK_Identifier && Lex.Token != TK_StringLiteral)
1738 		{
1739 			ParseError(Lex.Location, "Identifier expected");
1740 		}
1741 		char FChar = VStr::ToUpper(Lex.String[0]);
1742 		if (FChar < '0' || FChar < 'A' || FChar > ']')
1743 		{
1744 			ParseError(Lex.Location, "Frames must be 0-9, A-Z, [, \\ or ]");
1745 		}
1746 		s->Frame = FChar - 'A';
1747 		FramesString = Lex.String;
1748 		FramesLoc = Lex.Location;
1749 		Lex.NextToken();
1750 
1751 		//  Tics
1752 		bool Neg = Lex.Check(TK_Minus);
1753 		if (Lex.Token == TK_IntLiteral)
1754 		{
1755 			if (Neg)
1756 			{
1757 				s->Time = -Lex.Number;
1758 			}
1759 			else
1760 			{
1761 				s->Time = float(Lex.Number) / 35.0;
1762 			}
1763 			Lex.NextToken();
1764 		}
1765 		else if (Lex.Token == TK_FloatLiteral)
1766 		{
1767 			if (Neg)
1768 			{
1769 				s->Time = -Lex.Float;
1770 			}
1771 			else
1772 			{
1773 				s->Time = float(Lex.Float) / 35.0;
1774 			}
1775 			Lex.NextToken();
1776 		}
1777 		else
1778 		{
1779 			ParseError(Lex.Location, "State duration expected");
1780 		}
1781 
1782 		while (Lex.Token == TK_Identifier && !Lex.NewLine)
1783 		{
1784 			if (Lex.Name == NAME_Bright)
1785 			{
1786 				s->Frame |= VState::FF_FULLBRIGHT;
1787 				Lex.NextToken();
1788 				continue;
1789 			}
1790 			if (Lex.Name == NAME_Offset)
1791 			{
1792 				Lex.NextToken();
1793 				Lex.Expect(TK_LParen);
1794 				Neg = Lex.Check(TK_Minus);
1795 				if (Lex.Token != TK_IntLiteral)
1796 				{
1797 					ParseError(Lex.Location, "Integer expected");
1798 				}
1799 				s->Misc1 = Lex.Number * (Neg ? -1 : 1);
1800 				Lex.NextToken();
1801 				Lex.Expect(TK_Comma);
1802 				Neg = Lex.Check(TK_Minus);
1803 				if (Lex.Token != TK_IntLiteral)
1804 				{
1805 					ParseError(Lex.Location, "Integer expected");
1806 				}
1807 				s->Misc2 = Lex.Number * (Neg ? -1 : 1);
1808 				Lex.NextToken();
1809 				Lex.Expect(TK_RParen);
1810 				continue;
1811 			}
1812 			break;
1813 		}
1814 
1815 		//	Code
1816 		if (Lex.Check(TK_LBrace))
1817 		{
1818 			if (VStr::Length(*FramesString) > 1)
1819 			{
1820 				ParseError(Lex.Location, "Only states with single frame can have code block");
1821 			}
1822 			s->Function = new VMethod(NAME_None, s, s->Loc);
1823 			s->Function->ReturnTypeExpr = new VTypeExpr(TYPE_Void, Lex.Location);
1824 			s->Function->ReturnType = VFieldType(TYPE_Void);
1825 			s->Function->Statement = ParseCompoundStatement();
1826 		}
1827 		else if (!Lex.NewLine)
1828 		{
1829 			if (Lex.Token != TK_Identifier)
1830 			{
1831 				ParseError(Lex.Location, "State method name expected");
1832 			}
1833 			else
1834 			{
1835 				s->FunctionName = Lex.Name;
1836 				Lex.NextToken();
1837 			}
1838 		}
1839 
1840 		//	Link previous state.
1841 		if (PrevState)
1842 		{
1843 			PrevState->NextState = s;
1844 		}
1845 
1846 		//	Assign state to the labels.
1847 		for (int i = NewLabelsStart; i < InClass->StateLabelDefs.Num(); i++)
1848 		{
1849 			InClass->StateLabelDefs[i].State = s;
1850 			LoopStart = s;
1851 		}
1852 		NewLabelsStart = InClass->StateLabelDefs.Num();
1853 		PrevState = s;
1854 		StateIdx++;
1855 
1856 		for (size_t i = 1; i < VStr::Length(*FramesString); i++)
1857 		{
1858 			char FChar = VStr::ToUpper((*FramesString)[i]);
1859 			if (FChar < 'A' || FChar > ']')
1860 			{
1861 				ParseError(Lex.Location, "Frames must be A-Z, [, \\ or ]");
1862 			}
1863 
1864 			//	Create a new state.
1865 			sprintf(StateName, "S_%d", StateIdx);
1866 			VState* s2 = new VState(StateName, InClass, TmpLoc);
1867 			InClass->AddState(s2);
1868 			s2->SpriteName = s->SpriteName;
1869 			s2->Frame = (s->Frame & VState::FF_FULLBRIGHT) | (FChar - 'A');
1870 			s2->Time = s->Time;
1871 			s2->Misc1 = s->Misc1;
1872 			s2->Misc2 = s->Misc2;
1873 			s2->FunctionName = s->FunctionName;
1874 
1875 			//	Link previous state.
1876 			PrevState->NextState = s2;
1877 			PrevState = s2;
1878 			StateIdx++;
1879 		}
1880 	}
1881 
1882 	//	Make sure all state labels have corresponding states.
1883 	if (NewLabelsStart != InClass->StateLabelDefs.Num())
1884 	{
1885 		ParseError(Lex.Location, "State label at the end of state block");
1886 	}
1887 	if (PrevState)
1888 	{
1889 		ParseError(Lex.Location, "State block not ended");
1890 	}
1891 	unguard;
1892 }
1893 
1894 //==========================================================================
1895 //
1896 //	VParser::ParseReplication
1897 //
1898 //==========================================================================
1899 
ParseReplication(VClass * Class)1900 void VParser::ParseReplication(VClass* Class)
1901 {
1902 	guard(VParser::ParseReplication);
1903 	Lex.Expect(TK_LBrace);
1904 	while (!Lex.Check(TK_RBrace))
1905 	{
1906 		VRepInfo& RI = Class->RepInfos.Alloc();
1907 
1908 		//	Reliable or unreliable flag, currently unused.
1909 		if (Lex.Check(TK_Reliable))
1910 		{
1911 			RI.Reliable = true;
1912 		}
1913 		else if (Lex.Check(TK_Unreliable))
1914 		{
1915 			RI.Reliable = false;
1916 		}
1917 		else
1918 		{
1919 			ParseError(Lex.Location, "Expected reliable or unreliable");
1920 		}
1921 
1922 		//	Replication condition.
1923 		RI.Cond = new VMethod(NAME_None, Class, Lex.Location);
1924 		RI.Cond->ReturnType = VFieldType(TYPE_Bool);
1925 		RI.Cond->ReturnType.BitMask = 1;
1926 		RI.Cond->ReturnTypeExpr = new VTypeExpr(RI.Cond->ReturnType,
1927 			Lex.Location);
1928 		Lex.Expect(TK_If);
1929 		Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
1930 		VExpression* e = ParseExpression();
1931 		if (!e)
1932 		{
1933 			ParseError(Lex.Location, "If expression expected");
1934 		}
1935 		Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
1936 		RI.Cond->Statement = new VReturn(e, RI.Cond->Loc);
1937 
1938 		//	Fields
1939 		do
1940 		{
1941 			if (Lex.Token != TK_Identifier)
1942 			{
1943 				ParseError(Lex.Location, "Field name expected");
1944 			}
1945 			else
1946 			{
1947 				VRepField& F = RI.RepFields.Alloc();
1948 				F.Name = Lex.Name;
1949 				F.Loc = Lex.Location;
1950 				F.Member = NULL;
1951 				Lex.NextToken();
1952 			}
1953 		}
1954 		while (Lex.Check(TK_Comma));
1955 		Lex.Expect(TK_Semicolon);
1956 	}
1957 	unguard;
1958 }
1959 
1960 //==========================================================================
1961 //
1962 //	VParser::ParseClass
1963 //
1964 //==========================================================================
1965 
ParseClass()1966 void VParser::ParseClass()
1967 {
1968 	guard(VParser::ParseClass);
1969 	VName ClassName = Lex.Name;
1970 	TLocation ClassLoc = Lex.Location;
1971 	VClass* ExistingClass = NULL;
1972 
1973 	if (Lex.Token != TK_Identifier)
1974 	{
1975 		ParseError(Lex.Location, "Class name expected");
1976 		ClassName = NAME_None;
1977 	}
1978 	else
1979 	{
1980 		ExistingClass = VMemberBase::StaticFindClass(Lex.Name);
1981 	}
1982 	Lex.NextToken();
1983 
1984 	VName ParentClassName = NAME_None;
1985 	TLocation ParentClassLoc;
1986 
1987 	if (Lex.Check(TK_Colon))
1988 	{
1989 		if (Lex.Token != TK_Identifier)
1990 		{
1991 			ParseError(Lex.Location, "Parent class name expected");
1992 		}
1993 		else
1994 		{
1995 			ParentClassName = Lex.Name;
1996 			ParentClassLoc = Lex.Location;
1997 			Lex.NextToken();
1998 		}
1999 	}
2000 	else if (ClassName != NAME_Object)
2001 	{
2002 		ParseError(Lex.Location, "Parent class expected");
2003 	}
2004 
2005 	if (Lex.Check(TK_Decorate))
2006 	{
2007 		Lex.Expect(TK_Semicolon);
2008 
2009 		if (ExistingClass)
2010 		{
2011 			return;
2012 		}
2013 
2014 #ifndef IN_VCC
2015 		//	Check if it already exists n DECORATE imports.
2016 		for (int i = 0; i < VMemberBase::GDecorateClassImports.Num(); i++)
2017 		{
2018 			if (VMemberBase::GDecorateClassImports[i]->Name == ClassName)
2019 			{
2020 				Package->ParsedDecorateImportClasses.Append(
2021 					VMemberBase::GDecorateClassImports[i]);
2022 				return;
2023 			}
2024 		}
2025 #endif
2026 
2027 		//	New class.
2028 		VClass* Class = new VClass(ClassName, Package, ClassLoc);
2029 		Class->Defined = false;
2030 
2031 		if (ParentClassName != NAME_None)
2032 		{
2033 			Class->ParentClassName = ParentClassName;
2034 			Class->ParentClassLoc = ParentClassLoc;
2035 		}
2036 
2037 		//	This class is not IN this package.
2038 		Class->MemberType = MEMBER_DecorateClass;
2039 		Class->Outer = NULL;
2040 		Package->ParsedDecorateImportClasses.Append(Class);
2041 #ifndef IN_VCC
2042 		VMemberBase::GDecorateClassImports.Append(Class);
2043 #endif
2044 		return;
2045 	}
2046 
2047 	//	For engine package use native class objects.
2048 	VClass* Class;
2049 #ifndef IN_VCC
2050 	Class = NULL;
2051 	if (Package->Name == NAME_engine)
2052 	{
2053 		Class = VClass::FindClass(*ClassName);
2054 	}
2055 	if (Class)
2056 	{
2057 		//	If Defined is not set, it's a duplicate.
2058 		check(Class->Defined);
2059 		Class->Outer = Package;
2060 	}
2061 	else
2062 #endif
2063 	{
2064 		//	New class.
2065 		Class = new VClass(ClassName, Package, ClassLoc);
2066 	}
2067 	Class->Defined = false;
2068 
2069 	if (ParentClassName != NAME_None)
2070 	{
2071 		Class->ParentClassName = ParentClassName;
2072 		Class->ParentClassLoc = ParentClassLoc;
2073 	}
2074 
2075 	int ClassAttr = TModifiers::ClassAttr(TModifiers::Check(
2076 		TModifiers::Parse(Lex), TModifiers::Native | TModifiers::Abstract,
2077 		Lex.Location));
2078 	do
2079 	{
2080 		if (Lex.Check(TK_MobjInfo))
2081 		{
2082 			Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
2083 			VExpression* e = ParseExpression();
2084 			if (!e)
2085 			{
2086 				ParseError(Lex.Location, "Constant expression expected");
2087 			}
2088 			else if (Class->MobjInfoExpr)
2089 			{
2090 				ParseError(Lex.Location, "Only one Editor ID allowed");
2091 			}
2092 			else
2093 			{
2094 				Class->MobjInfoExpr = e;
2095 			}
2096 			Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
2097 		}
2098 		else if (Lex.Check(TK_ScriptId))
2099 		{
2100 			Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
2101 			VExpression* e = ParseExpression();
2102 			if (!e)
2103 			{
2104 				ParseError(Lex.Location, "Constant expression expected");
2105 			}
2106 			else if (Class->ScriptIdExpr)
2107 			{
2108 				ParseError(Lex.Location, "Only one script ID allowed");
2109 			}
2110 			else
2111 			{
2112 				Class->ScriptIdExpr = e;
2113 			}
2114 			Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
2115 		}
2116 		else if (Lex.Check(TK_Game))
2117 		{
2118 			Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
2119 			VExpression* e = ParseExpression();
2120 			if (!e)
2121 			{
2122 				ParseError(Lex.Location, "Constant expression expected");
2123 			}
2124 			else if (Class->GameExpr)
2125 			{
2126 				ParseError(Lex.Location, "Only one game expression allowed");
2127 			}
2128 			else
2129 			{
2130 				Class->GameExpr = e;
2131 			}
2132 			Lex.Expect(TK_RParen, ERR_MISSING_RPAREN);
2133 		}
2134 		else
2135 		{
2136 			break;
2137 		}
2138 	} while (1);
2139 
2140 	Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
2141 	while (!Lex.Check(TK_DefaultProperties))
2142 	{
2143 		if (Lex.Check(TK_States))
2144 		{
2145 			ParseStates(Class);
2146 			continue;
2147 		}
2148 
2149 		if (Lex.Check(TK_Enum))
2150 		{
2151 			VConstant* PrevValue = NULL;
2152 			Lex.Expect(TK_LBrace, ERR_MISSING_LBRACE);
2153 			do
2154 			{
2155 				if (Lex.Token != TK_Identifier)
2156 				{
2157 					ParseError(Lex.Location, "Identifier expected");
2158 					Lex.NextToken();
2159 					continue;
2160 				}
2161 				if (Class->FindConstant(Lex.Name))
2162 				{
2163 					ParseError(Lex.Location, "Redefined identifier %s", *Lex.Name);
2164 				}
2165 				VConstant* cDef = new VConstant(Lex.Name, Class, Lex.Location);
2166 				cDef->Type = TYPE_Int;
2167 				Lex.NextToken();
2168 				if (Lex.Check(TK_Assign))
2169 				{
2170 					cDef->ValueExpr = ParseExpression();
2171 				}
2172 				else if (PrevValue)
2173 				{
2174 					cDef->PrevEnumValue = PrevValue;
2175 				}
2176 				else
2177 				{
2178 					cDef->ValueExpr = new VIntLiteral(0, Lex.Location);
2179 				}
2180 				PrevValue = cDef;
2181 				Class->AddConstant(cDef);
2182 			} while (Lex.Check(TK_Comma));
2183 			Lex.Expect(TK_RBrace, ERR_MISSING_RBRACE);
2184 			Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
2185 			continue;
2186 		}
2187 
2188 		if (Lex.Check(TK_Const))
2189 		{
2190 			int Type = TYPE_Unknown;
2191 			if (Lex.Check(TK_Int))
2192 			{
2193 				Type = TYPE_Int;
2194 			}
2195 			else if (Lex.Check(TK_Float))
2196 			{
2197 				Type = TYPE_Float;
2198 			}
2199 			else if (Lex.Check(TK_Name))
2200 			{
2201 				Type = TYPE_Name;
2202 			}
2203 			else if (Lex.Check(TK_String))
2204 			{
2205 				Type = TYPE_String;
2206 			}
2207 			else
2208 			{
2209 				ParseError(Lex.Location, "Bad constant type");
2210 				Lex.NextToken();
2211 			}
2212 			do
2213 			{
2214 				if (Lex.Token != TK_Identifier)
2215 				{
2216 					ParseError(Lex.Location, "Const name expected");
2217 					Lex.NextToken();
2218 					continue;
2219 				}
2220 				if (Class->FindConstant(Lex.Name))
2221 				{
2222 					ParseError(Lex.Location, "Redefined identifier %s", *Lex.Name);
2223 				}
2224 				VConstant* cDef = new VConstant(Lex.Name, Class, Lex.Location);
2225 				cDef->Type = Type;
2226 				Lex.NextToken();
2227 				if (!Lex.Check(TK_Assign))
2228 				{
2229 					ParseError(Lex.Location, "Assignement operator expected");
2230 				}
2231 				cDef->ValueExpr = ParseExpression();
2232 				Class->AddConstant(cDef);
2233 			} while (Lex.Check(TK_Comma));
2234 			Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
2235 			continue;
2236 		}
2237 
2238 		if (Lex.Check(TK_Struct))
2239 		{
2240 			ParseStruct(Class, false);
2241 			continue;
2242 		}
2243 
2244 		if (Lex.Check(TK_Vector))
2245 		{
2246 			ParseStruct(Class, true);
2247 			continue;
2248 		}
2249 
2250 		if (Lex.Check(TK_Delegate))
2251 		{
2252 			VExpression* Type = ParseType();
2253 			if (!Type)
2254 			{
2255 				ParseError(Lex.Location, "Field type expected.");
2256 				continue;
2257 			}
2258 			TLocation l = Lex.Location;
2259 			while (Lex.Check(TK_Asterisk))
2260 			{
2261 				Type = new VPointerType(Type, l);
2262 				l = Lex.Location;
2263 			}
2264 
2265 			if (Lex.Token != TK_Identifier)
2266 			{
2267 				ParseError(Lex.Location, "Field name expected");
2268 				continue;
2269 			}
2270 			VField* fi = new VField(Lex.Name, Class, Lex.Location);
2271 			if (Class->FindField(Lex.Name) || Class->FindMethod(Lex.Name))
2272 			{
2273 				ParseError(Lex.Location, "Redeclared field");
2274 			}
2275 			Lex.NextToken();
2276 			Class->AddField(fi);
2277 			ParseDelegate(Type, fi);
2278 			continue;
2279 		}
2280 
2281 		if (Lex.Check(TK_Replication))
2282 		{
2283 			ParseReplication(Class);
2284 			continue;
2285 		}
2286 
2287 		int Modifiers = TModifiers::Parse(Lex);
2288 
2289 		if (Lex.Check(TK_Iterator))
2290 		{
2291 			if (Lex.Token != TK_Identifier)
2292 			{
2293 				ParseError(Lex.Location, "Method name expected");
2294 			}
2295 			VName FieldName = Lex.Name;
2296 			TLocation FieldLoc = Lex.Location;
2297 			Lex.NextToken();
2298 			Lex.Expect(TK_LParen, ERR_MISSING_LPAREN);
2299 			ParseMethodDef(new VTypeExpr(VFieldType(TYPE_Void).MakePointerType(),
2300 				Lex.Location), FieldName, FieldLoc, Class, Modifiers, true);
2301 			continue;
2302 		}
2303 
2304 		VExpression* Type = ParseType();
2305 		if (!Type)
2306 		{
2307 			ParseError(Lex.Location, "Field type expected.");
2308 			Lex.NextToken();
2309 			continue;
2310 		}
2311 
2312 		bool need_semicolon = true;
2313 		do
2314 		{
2315 			VExpression* FieldType = Type->CreateTypeExprCopy();
2316 			TLocation l = Lex.Location;
2317 			while (Lex.Check(TK_Asterisk))
2318 			{
2319 				FieldType = new VPointerType(FieldType, l);
2320 				l = Lex.Location;
2321 			}
2322 			if (Lex.Token != TK_Identifier)
2323 			{
2324 				ParseError(Lex.Location, "Field name expected");
2325 				continue;
2326 			}
2327 			VName FieldName = Lex.Name;
2328 			TLocation FieldLoc = Lex.Location;
2329 			Lex.NextToken();
2330 
2331 			if (Class->FindField(FieldName))
2332 			{
2333 				ParseError(Lex.Location, "Redeclared field");
2334 				continue;
2335 			}
2336 
2337 			if (Lex.Check(TK_LBrace))
2338 			{
2339 				Modifiers = TModifiers::Check(Modifiers,
2340 					TModifiers::Native | TModifiers::Final, FieldLoc);
2341 				VProperty* Prop = new VProperty(FieldName, Class, FieldLoc);
2342 				Prop->TypeExpr = FieldType;
2343 				Prop->Flags = TModifiers::PropAttr(Modifiers);
2344 				do
2345 				{
2346 					if (Lex.Check(TK_Get))
2347 					{
2348 						char TmpName[NAME_SIZE];
2349 						sprintf(TmpName, "get_%s", *FieldName);
2350 						VMethod* Func = new VMethod(TmpName, Class, Lex.Location);
2351 						Func->Flags = TModifiers::MethodAttr(Modifiers);
2352 						Func->ReturnTypeExpr = FieldType->CreateTypeExprCopy();
2353 
2354 						if (Modifiers & TModifiers::Native)
2355 						{
2356 							Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
2357 							Package->NumBuiltins++;
2358 						}
2359 						else
2360 						{
2361 							Lex.Expect(TK_LBrace, ERR_MISSING_LBRACE);
2362 							Func->Statement = ParseCompoundStatement();
2363 						}
2364 
2365 						if (Prop->GetFunc)
2366 						{
2367 							ParseError(FieldLoc, "Property already has a get method");
2368 							ParseError(Prop->GetFunc->Loc, "Previous get method here");
2369 						}
2370 						Prop->GetFunc = Func;
2371 						Class->AddMethod(Func);
2372 					}
2373 					else if (Lex.Check(TK_Set))
2374 					{
2375 						char TmpName[NAME_SIZE];
2376 						sprintf(TmpName, "set_%s", *FieldName);
2377 						VMethod* Func = new VMethod(TmpName, Class, Lex.Location);
2378 						Func->Flags = TModifiers::MethodAttr(Modifiers);
2379 						Func->ReturnTypeExpr = new VTypeExpr(TYPE_Void, Lex.Location);
2380 
2381 						VMethodParam& P = Func->Params[Func->NumParams];
2382 						P.TypeExpr = FieldType->CreateTypeExprCopy();
2383 						P.Name = "value";
2384 						P.Loc = Lex.Location;
2385 						Func->ParamFlags[Func->NumParams] = 0;
2386 						Func->NumParams++;
2387 
2388 						if (Modifiers & TModifiers::Native)
2389 						{
2390 							Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
2391 							Package->NumBuiltins++;
2392 						}
2393 						else
2394 						{
2395 							Lex.Expect(TK_LBrace, ERR_MISSING_LBRACE);
2396 							Func->Statement = ParseCompoundStatement();
2397 						}
2398 
2399 						if (Prop->SetFunc)
2400 						{
2401 							ParseError(FieldLoc, "Property already has a set method");
2402 							ParseError(Prop->SetFunc->Loc, "Previous set method here");
2403 						}
2404 						Prop->SetFunc = Func;
2405 						Class->AddMethod(Func);
2406 					}
2407 					else if (Lex.Check(TK_Default))
2408 					{
2409 						if (Lex.Token != TK_Identifier)
2410 						{
2411 							ParseError(Lex.Location, "Default field name expected");
2412 						}
2413 						else
2414 						{
2415 							if (Prop->DefaultFieldName != NAME_None)
2416 							{
2417 								ParseError(Lex.Location, "Property already has default field defined");
2418 							}
2419 							Prop->DefaultFieldName = Lex.Name;
2420 							Lex.NextToken();
2421 						}
2422 						Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
2423 					}
2424 					else
2425 					{
2426 						ParseError(Lex.Location, "Invalid declaration");
2427 						Lex.NextToken();
2428 					}
2429 				}
2430 				while (!Lex.Check(TK_RBrace));
2431 				Class->AddProperty(Prop);
2432 				need_semicolon = false;
2433 				break;
2434 			}
2435 
2436 			if (Lex.Check(TK_LParen))
2437 			{
2438 				ParseMethodDef(FieldType, FieldName, FieldLoc, Class,
2439 					Modifiers, false);
2440 				need_semicolon = false;
2441 				break;
2442 			}
2443 
2444 			if (Lex.Check(TK_LBracket))
2445 			{
2446 				TLocation SLoc = Lex.Location;
2447 				VExpression* e = ParseExpression();
2448 				Lex.Expect(TK_RBracket, ERR_MISSING_RFIGURESCOPE);
2449 				FieldType = new VFixedArrayType(FieldType, e, SLoc);
2450 			}
2451 
2452 			VField* fi = new VField(FieldName, Class, FieldLoc);
2453 			fi->TypeExpr = FieldType;
2454 			fi->Flags = TModifiers::FieldAttr(TModifiers::Check(Modifiers,
2455 				TModifiers::Native | TModifiers::Private |
2456 				TModifiers::ReadOnly | TModifiers::Transient, FieldLoc));
2457 			Class->AddField(fi);
2458 		} while (Lex.Check(TK_Comma));
2459 		delete Type;
2460 		Type = NULL;
2461 		if (need_semicolon)
2462 		{
2463 			Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
2464 		}
2465 	}
2466 
2467 	ParseDefaultProperties(Class);
2468 
2469 	Package->ParsedClasses.Append(Class);
2470 	unguard;
2471 }
2472 
2473 //==========================================================================
2474 //
2475 //	VParser::Parse
2476 //
2477 //==========================================================================
2478 
Parse()2479 void VParser::Parse()
2480 {
2481 	guard(VParser::Parse);
2482 	bool		done;
2483 
2484 	dprintf("Parsing\n");
2485 
2486 	Lex.NextToken();
2487 	done = false;
2488 	while (!done)
2489 	{
2490 		switch(Lex.Token)
2491 		{
2492 		case TK_EOF:
2493 			done = true;
2494 			break;
2495 
2496 		case TK_Import:
2497 		{
2498 			Lex.NextToken();
2499 			if (Lex.Token != TK_NameLiteral)
2500 			{
2501 				ParseError(Lex.Location, "Package name expected");
2502 			}
2503 			VImportedPackage& I = Package->PackagesToLoad.Alloc();
2504 			I.Name = Lex.Name;
2505 			I.Loc = Lex.Location;
2506 			Lex.NextToken();
2507 			Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
2508 			break;
2509 		}
2510 
2511 		case TK_Enum:
2512 		{
2513 			Lex.NextToken();
2514 			VConstant* PrevValue = NULL;
2515 			Lex.Expect(TK_LBrace, ERR_MISSING_LBRACE);
2516 			do
2517 			{
2518 				if (Lex.Token != TK_Identifier)
2519 				{
2520 					ParseError(Lex.Location, "Expected IDENTIFIER");
2521 				}
2522 				if (Package->FindConstant(Lex.Name))
2523 				{
2524 					ParseError(Lex.Location, "Redefined identifier %s", *Lex.Name);
2525 				}
2526 				VConstant* cDef = new VConstant(Lex.Name, Package, Lex.Location);
2527 				cDef->Type = TYPE_Int;
2528 				Lex.NextToken();
2529 				if (Lex.Check(TK_Assign))
2530 				{
2531 					cDef->ValueExpr = ParseExpression();
2532 				}
2533 				else if (PrevValue)
2534 				{
2535 					cDef->PrevEnumValue = PrevValue;
2536 				}
2537 				else
2538 				{
2539 					cDef->ValueExpr = new VIntLiteral(0, Lex.Location);
2540 				}
2541 				PrevValue = cDef;
2542 				Package->ParsedConstants.Append(cDef);
2543 			} while (Lex.Check(TK_Comma));
2544 			Lex.Expect(TK_RBrace, ERR_MISSING_RBRACE);
2545 			Lex.Expect(TK_Semicolon, ERR_MISSING_SEMICOLON);
2546 			break;
2547 		}
2548 
2549 		case TK_Class:
2550 			Lex.NextToken();
2551 			ParseClass();
2552 			break;
2553 
2554 		default:
2555 			ParseError(Lex.Location, "Invalid token \"%s\"",
2556 				VLexer::TokenNames[Lex.Token]);
2557 			Lex.NextToken();
2558 			break;
2559 		}
2560 	}
2561 
2562 	if (NumErrors)
2563 	{
2564 		BailOut();
2565 	}
2566 	unguard;
2567 }
2568