summaryrefslogtreecommitdiff
path: root/source4/lib/appweb/ejs-2.0/ejs/ejsLex.c
blob: fbfee6e4d59abdf5c1870dc4ad7117337d943f3b (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
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
/*
 *	@file 	ejsLex.c
 *	@brief 	EJS Lexical Analyser
 *	@overview EJS lexical analyser. This implementes a lexical analyser 
 *		for a subset of the JavaScript language.
 */
/********************************* Copyright **********************************/
/*
 *	@copy	default.g
 *	
 *	Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
 *	Portions Copyright (c) GoAhead Software, 1995-2000. All Rights Reserved.
 *	
 *	This software is distributed under commercial and open source licenses.
 *	You may use the GPL open source license described below or you may acquire 
 *	a commercial license from Mbedthis Software. You agree to be fully bound 
 *	by the terms of either license. Consult the LICENSE.TXT distributed with 
 *	this software for full details.
 *	
 *	This software is open source; 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 2 of the License, or (at your 
 *	option) any later version. See the GNU General Public License for more 
 *	details at: http://www.mbedthis.com/downloads/gplLicense.html
 *	
 *	This program is distributed WITHOUT ANY WARRANTY; without even the 
 *	implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *	
 *	This GPL license does NOT permit incorporating this software into 
 *	proprietary programs. If you are unable to comply with the GPL, you must
 *	acquire a commercial license to use this software. Commercial licenses 
 *	for this software and support services are available from Mbedthis 
 *	Software at http://www.mbedthis.com 
 *	
 *	@end
 */
/********************************** Includes **********************************/

#include	"ejs.h"

#if BLD_FEATURE_EJS

/****************************** Forward Declarations **************************/

static int 		getLexicalToken(Ejs *ep, int state);
static int 		tokenAddChar(Ejs *ep, int c);
static int 		inputGetc(Ejs *ep);
static void		inputPutback(Ejs *ep, int c);
static int		charConvert(Ejs *ep, int base, int maxDig);
static void 	parseNumber(Ejs *ep, EjsType type);

/************************************* Code ***********************************/
/*
 *	Open a new input script
 */

int ejsLexOpenScript(Ejs *ep, const char *script)
{
	EjsInput	*ip;

	mprAssert(ep);
	mprAssert(script);

	if ((ip = mprAllocTypeZeroed(ep, EjsInput)) == NULL) {
		return MPR_ERR_MEMORY;
	}
	ip->next = ep->input;
	ep->input = ip;
	ip->procName = ep->proc ? ep->proc->procName : NULL;
	ip->fileName = ep->fileName ? ep->fileName : NULL;

/*
 *	Create the parse token buffer and script buffer
 */
	ip->tokServp = ip->tokbuf;
	ip->tokEndp = ip->tokbuf;

	ip->script = script;
	ip->scriptSize = strlen(script);
	ip->scriptServp = (char*) ip->script;

	ip->lineNumber = 1;
	ip->lineColumn = 0;

	ip->putBackIndex = -1;

	return 0;
}

/******************************************************************************/
/*
 *	Close the input script
 */

void ejsLexCloseScript(Ejs *ep)
{
	EjsInput	*ip;

	mprAssert(ep);

	ip = ep->input;
	mprAssert(ip);
	ep->input = ip->next;

	mprFree(ip);
}

/******************************************************************************/
/*
 *	Initialize an input state structure
 */

int ejsInitInputState(EjsInput *ip)
{
	mprAssert(ip);

	memset(ip, 0, sizeof(*ip));
	ip->putBackIndex = -1;

	return 0;
}
/******************************************************************************/
/*
 *	Save the input state
 */

void ejsLexSaveInputState(Ejs *ep, EjsInput *state)
{
	EjsInput	*ip;
	int			i;

	mprAssert(ep);

	ip = ep->input;
	mprAssert(ip);

	*state = *ip;

	for (i = 0; i <= ip->putBackIndex; i++) {
		mprStrcpy(state->putBack[i].tokbuf, EJS_MAX_TOKEN, 
			ip->putBack[i].tokbuf);
		state->putBack[i].tid = ip->putBack[i].tid;
	}

	mprStrcpy(state->line, sizeof(state->line), ip->line);

	state->lineColumn = ip->lineColumn;
	state->lineNumber = ip->lineNumber;
}

/******************************************************************************/
/*
 *	Restore the input state
 */

void ejsLexRestoreInputState(Ejs *ep, EjsInput *state)
{
	EjsInput	*ip;
	EjsToken	*tp;
	int			i;

	mprAssert(ep);
	mprAssert(state);

	ip = ep->input;
	mprAssert(ip);

	mprStrcpy(ip->tokbuf, sizeof(ip->tokbuf), state->tokbuf);
	ip->tokServp = state->tokServp;
	ip->tokEndp = state->tokEndp;

	ip->script = state->script;
	ip->scriptServp = state->scriptServp;
	ip->scriptSize = state->scriptSize;

	ip->putBackIndex = state->putBackIndex;
	for (i = 0; i <= ip->putBackIndex; i++) {
		tp = &ip->putBack[i];
		tp->tid = state->putBack[i].tid;
		mprStrcpy(tp->tokbuf, sizeof(tp->tokbuf), state->putBack[i].tokbuf);
	}

	mprStrcpy(ip->line, sizeof(ip->line), state->line);

	ip->lineColumn = state->lineColumn;
	ip->lineNumber = state->lineNumber;
}

/******************************************************************************/
/*
 *	Free a saved input state
 */

void ejsLexFreeInputState(Ejs *ep, EjsInput *state)
{
	mprAssert(ep);
	mprAssert(state);

	state->putBackIndex = -1;
	state->lineColumn = 0;
}

/******************************************************************************/
/*
 *	Get the next EJS token
 */

int ejsLexGetToken(Ejs *ep, int state)
{
	mprAssert(ep);

	ep->tid = getLexicalToken(ep, state);
	return ep->tid;
}

/******************************************************************************/

/*
 *	Check for reserved words "if", "else", "var", "for", "delete", "function", 
 *	"class", "extends", "public", "private", "protected", "try", "catch", 
 *	"finally", "throw", "return", "get", "set", "this", "module", "each"
 *
 *	The "new" and "in" reserved words are handled below. The "true", "false", 
 *	"null" "typeof" and "undefined" reserved words are handled as global 
 *	objects.
 *
 *	Other reserved words not supported:
 *		"break", "case", "continue", "default", "do", 
 *		"instanceof", "switch", "while", "with"
 *
 *	ECMA extensions reserved words (not supported):
 *		"abstract", "boolean", "byte", "char", "const",
 *		"debugger", "double", "enum", "export", 
 *		"final", "float", "goto", "implements", "import", "int",
 *		"interface", "long", "native", "package", 
 *		"short", "static", "super", "synchronized", "transient", "volatile"
 *
 *	FUTURE -- use a hash lookup
 */

static int checkReservedWord(Ejs *ep, int state, int c, int tid)
{
	/*	FUTURE -- probably should return for all tokens != EJS_TOK_ID */
	/*	FUTURE -- Should have a hash for this. MUCH faster. */

	if (!isalpha(ep->token[0]) || tid == EJS_TOK_LITERAL) {
		return tid;
	}
	if (state == EJS_STATE_STMT) {
		/*	FUTURE OPT -- convert to hash lookup */
		if (strcmp(ep->token, "if") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_IF;
		} else if (strcmp(ep->token, "else") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_ELSE;
		} else if (strcmp(ep->token, "var") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_VAR;
		} else if (strcmp(ep->token, "new") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_NEW;
		} else if (strcmp(ep->token, "for") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_FOR;
		} else if (strcmp(ep->token, "delete") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_DELETE;
		} else if (strcmp(ep->token, "function") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_FUNCTION;
		} else if (strcmp(ep->token, "class") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_CLASS;
		} else if (strcmp(ep->token, "module") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_MODULE;
		} else if (strcmp(ep->token, "extends") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_EXTENDS;
		} else if (strcmp(ep->token, "try") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_TRY;
		} else if (strcmp(ep->token, "catch") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_CATCH;
		} else if (strcmp(ep->token, "finally") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_FINALLY;
		} else if (strcmp(ep->token, "throw") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_THROW;
		} else if (strcmp(ep->token, "public") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_PUBLIC;
		} else if (strcmp(ep->token, "protected") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_PROTECTED;
		} else if (strcmp(ep->token, "private") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_PRIVATE;
		} else if (strcmp(ep->token, "get") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_GET;
		} else if (strcmp(ep->token, "set") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_SET;
		} else if (strcmp(ep->token, "extends") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_EXTENDS;
		} else if (strcmp(ep->token, "try") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_TRY;
		} else if (strcmp(ep->token, "catch") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_CATCH;
		} else if (strcmp(ep->token, "finally") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_FINALLY;
		} else if (strcmp(ep->token, "throw") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_THROW;
		} else if (strcmp(ep->token, "public") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_PUBLIC;
		} else if (strcmp(ep->token, "protected") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_PROTECTED;
		} else if (strcmp(ep->token, "private") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_PRIVATE;
		} else if (strcmp(ep->token, "get") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_GET;
		} else if (strcmp(ep->token, "set") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_SET;
		} else if (strcmp(ep->token, "each") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_EACH;
		} else if (strcmp(ep->token, "return") == 0) {
			if ((c == ';') || (c == '(')) {
				inputPutback(ep, c);
			}
			return EJS_TOK_RETURN;
		}

	} else if (state == EJS_STATE_EXPR) {
		if (strcmp(ep->token, "new") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_NEW;
		} else if (strcmp(ep->token, "in") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_IN;
		} else if (strcmp(ep->token, "function") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_FUNCTION;
		}

	} else if (state == EJS_STATE_DEC) {
		if (strcmp(ep->token, "extends") == 0) {
			inputPutback(ep, c);
			return EJS_TOK_EXTENDS;
		}
	}
	return tid;
}

/******************************************************************************/
/*
 *	Get the next EJS token
 */

static int getLexicalToken(Ejs *ep, int state)
{
	EjsType		type;
	EjsInput	*ip;
	int			done, tid, c, quote, style, idx, isHex;

	mprAssert(ep);
	ip = ep->input;
	mprAssert(ip);

	ep->tid = -1;
	tid = -1;
	type = BLD_FEATURE_NUM_TYPE_ID;
	isHex = 0;

	/*
 	 *	Use a putback tokens first. Don't free strings as caller needs access.
	 */
	if (ip->putBackIndex >= 0) {
		idx = ip->putBackIndex;
		tid = ip->putBack[idx].tid;
		ep->token = (char*) ip->putBack[idx].tokbuf;
		tid = checkReservedWord(ep, state, 0, tid);
		ip->putBackIndex--;
		return tid;
	}
	ep->token = ip->tokServp = ip->tokEndp = ip->tokbuf;
	*ip->tokServp = '\0';

	if ((c = inputGetc(ep)) < 0) {
		return EJS_TOK_EOF;
	}

	/*
 	 *	Main lexical analyser
	 */
	for (done = 0; !done; ) {
		switch (c) {
		case -1:
			return EJS_TOK_EOF;

		case ' ':
		case '\t':
		case '\r':
			do {
				if ((c = inputGetc(ep)) < 0)
					break;
			} while (c == ' ' || c == '\t' || c == '\r');
			break;

		case '\n':
			return EJS_TOK_NEWLINE;

		case '(':
			tokenAddChar(ep, c);
			return EJS_TOK_LPAREN;

		case ')':
			tokenAddChar(ep, c);
			return EJS_TOK_RPAREN;

		case '[':
			tokenAddChar(ep, c);
			return EJS_TOK_LBRACKET;

		case ']':
			tokenAddChar(ep, c);
			return EJS_TOK_RBRACKET;

		case '.':
			tokenAddChar(ep, c);
			return EJS_TOK_PERIOD;

		case '{':
			tokenAddChar(ep, c);
			return EJS_TOK_LBRACE;

		case '}':
			tokenAddChar(ep, c);
			return EJS_TOK_RBRACE;

		case '+':
			if ((c = inputGetc(ep)) < 0) {
				ejsSyntaxError(ep, 0);
				return EJS_TOK_ERR;
			}
			if (c != '+' ) {
				inputPutback(ep, c);
				tokenAddChar(ep, EJS_EXPR_PLUS);
				return EJS_TOK_EXPR;
			}
			tokenAddChar(ep, EJS_EXPR_INC);
			return EJS_TOK_INC_DEC;

		case '-':
			if ((c = inputGetc(ep)) < 0) {
				ejsSyntaxError(ep, 0);
				return EJS_TOK_ERR;
			}
			if (c != '-' ) {
				inputPutback(ep, c);
				tokenAddChar(ep, EJS_EXPR_MINUS);
				return EJS_TOK_EXPR;
			}
			tokenAddChar(ep, EJS_EXPR_DEC);
			return EJS_TOK_INC_DEC;

		case '*':
			tokenAddChar(ep, EJS_EXPR_MUL);
			return EJS_TOK_EXPR;

		case '%':
			tokenAddChar(ep, EJS_EXPR_MOD);
			return EJS_TOK_EXPR;

		case '/':
			/*
			 *	Handle the division operator and comments
			 */
			if ((c = inputGetc(ep)) < 0) {
				ejsSyntaxError(ep, 0);
				return EJS_TOK_ERR;
			}
			if (c != '*' && c != '/') {
				inputPutback(ep, c);
				tokenAddChar(ep, EJS_EXPR_DIV);
				return EJS_TOK_EXPR;
			}
			style = c;
			/*
			 *	Eat comments. Both C and C++ comment styles are supported.
			 */
			while (1) {
				if ((c = inputGetc(ep)) < 0) {
					if (style == '/') {
						return EJS_TOK_EOF;
					}
					ejsSyntaxError(ep, 0);
					return EJS_TOK_ERR;
				}
				if (c == '\n' && style == '/') {
					break;
				} else if (c == '*') {
					c = inputGetc(ep);
					if (style == '/') {
						if (c == '\n') {
							break;
						}
					} else {
						if (c == '/') {
							break;
						}
					}
				}
			}
			/*
			 *	Continue looking for a token, so get the next character
			 */
			if ((c = inputGetc(ep)) < 0) {
				return EJS_TOK_EOF;
			}
			break;

		case '<':									/* < and <= */
			if ((c = inputGetc(ep)) < 0) {
				ejsSyntaxError(ep, 0);
				return EJS_TOK_ERR;
			}
			if (c == '<') {
				tokenAddChar(ep, EJS_EXPR_LSHIFT);
				return EJS_TOK_EXPR;
			} else if (c == '=') {
				tokenAddChar(ep, EJS_EXPR_LESSEQ);
				return EJS_TOK_EXPR;
			}
			tokenAddChar(ep, EJS_EXPR_LESS);
			inputPutback(ep, c);
			return EJS_TOK_EXPR;

		case '>':									/* > and >= */
			if ((c = inputGetc(ep)) < 0) {
				ejsSyntaxError(ep, 0);
				return EJS_TOK_ERR;
			}
			if (c == '>') {
				tokenAddChar(ep, EJS_EXPR_RSHIFT);
				return EJS_TOK_EXPR;
			} else if (c == '=') {
				tokenAddChar(ep, EJS_EXPR_GREATEREQ);
				return EJS_TOK_EXPR;
			}
			tokenAddChar(ep, EJS_EXPR_GREATER);
			inputPutback(ep, c);
			return EJS_TOK_EXPR;

		case '=':									/* "==" */
			if ((c = inputGetc(ep)) < 0) {
				ejsSyntaxError(ep, 0);
				return EJS_TOK_ERR;
			}
			if (c == '=') {
				tokenAddChar(ep, EJS_EXPR_EQ);
				return EJS_TOK_EXPR;
			}
			inputPutback(ep, c);
			return EJS_TOK_ASSIGNMENT;

		case '!':									/* "!=" or "!"*/
			if ((c = inputGetc(ep)) < 0) {
				ejsSyntaxError(ep, 0);
				return EJS_TOK_ERR;
			}
			if (c == '=') {
				tokenAddChar(ep, EJS_EXPR_NOTEQ);
				return EJS_TOK_EXPR;
			}
			inputPutback(ep, c);
			tokenAddChar(ep, EJS_EXPR_BOOL_COMP);
			return EJS_TOK_EXPR;

		case ';':
			tokenAddChar(ep, c);
			return EJS_TOK_SEMI;

		case ',':
			tokenAddChar(ep, c);
			return EJS_TOK_COMMA;

		case ':':
			tokenAddChar(ep, c);
			return EJS_TOK_COLON;

		case '|':									/* "||" */
			if ((c = inputGetc(ep)) < 0 || c != '|') {
				ejsSyntaxError(ep, 0);
				return EJS_TOK_ERR;
			}
			tokenAddChar(ep, EJS_COND_OR);
			return EJS_TOK_LOGICAL;

		case '&':									/* "&&" */
			if ((c = inputGetc(ep)) < 0 || c != '&') {
				ejsSyntaxError(ep, 0);
				return EJS_TOK_ERR;
			}
			tokenAddChar(ep, EJS_COND_AND);
			return EJS_TOK_LOGICAL;

		case '\"':									/* String quote */
		case '\'':
			quote = c;
			if ((c = inputGetc(ep)) < 0) {
				ejsSyntaxError(ep, 0);
				return EJS_TOK_ERR;
			}

			while (c != quote) {
				/*
				 *	Check for escape sequence characters
				 */
				if (c == '\\') {
					c = inputGetc(ep);

					if (isdigit(c)) {
						/*
						 *	Octal support, \101 maps to 65 = 'A'. Put first 
						 *	char back so converter will work properly.
						 */
						inputPutback(ep, c);
						c = charConvert(ep, 8, 3);

					} else {
						switch (c) {
						case 'n':
							c = '\n'; break;
						case 'b':
							c = '\b'; break;
						case 'f':
							c = '\f'; break;
						case 'r':
							c = '\r'; break;
						case 't':
							c = '\t'; break;
						case 'x':
							/*
							 *	Hex support, \x41 maps to 65 = 'A'
							 */
							c = charConvert(ep, 16, 2);
							break;
						case 'u':
							/*
							 *	Unicode support, \x0401 maps to 65 = 'A'
							 */
							c = charConvert(ep, 16, 2);
							c = c*16 + charConvert(ep, 16, 2);

							break;
						case '\'':
						case '\"':
						case '\\':
							break;
						default:
							if (tokenAddChar(ep, '\\') < 0) {
								return EJS_TOK_ERR;
							}
						}
					}
					if (tokenAddChar(ep, c) < 0) {
						return EJS_TOK_ERR;
					}
				} else {
					if (tokenAddChar(ep, c) < 0) {
						return EJS_TOK_ERR;
					}
				}
				if ((c = inputGetc(ep)) < 0) {
					ejsSyntaxError(ep, "Unmatched Quote");
					return EJS_TOK_ERR;
				}
			}
			return EJS_TOK_LITERAL;

		case '0': 
			if (tokenAddChar(ep, c) < 0) {
				return EJS_TOK_ERR;
			}
			if ((c = inputGetc(ep)) < 0) {
				break;
			}
			if (tolower(c) == 'x') {
				if (tokenAddChar(ep, c) < 0) {
					return EJS_TOK_ERR;
				}
				if ((c = inputGetc(ep)) < 0) {
					break;
				}
				isHex = 1;
				if (! isxdigit(c)) {
					parseNumber(ep, type);
					inputPutback(ep, c);
					return EJS_TOK_NUMBER;
				}
			} else if (! isdigit(c)) {
#if BLD_FEATURE_FLOATING_POINT
				if (c == '.' || tolower(c) == 'e' || c == '+' || c == '-') {
					/* Fall through */
					type = EJS_TYPE_FLOAT;
				} else
#endif
				{
					parseNumber(ep, type);
					inputPutback(ep, c);
					return EJS_TOK_NUMBER;
				}
			}
			/* Fall through to get more digits */

		case '1': case '2': case '3': case '4': 
		case '5': case '6': case '7': case '8': case '9':
			if (isHex) {
				do {
					if (tokenAddChar(ep, c) < 0) {
						return EJS_TOK_ERR;
					}
					if ((c = inputGetc(ep)) < 0) {
						break;
					}
				} while (isxdigit(c));

			} else {
#if BLD_FEATURE_FLOATING_POINT
				do {
					if (tokenAddChar(ep, c) < 0) {
						return EJS_TOK_ERR;
					}
					if ((c = inputGetc(ep)) < 0) {
						break;
					}
					c = tolower(c);
					if (c == '.' || c == 'e' || c == 'f') {
						type = EJS_TYPE_FLOAT;
					}
				} while (isdigit(c) || c == '.' || c == 'e' || 
						c == 'f' ||
					((type == EJS_TYPE_FLOAT) && (c == '+' || c == '-')));
#else
				do {
					if (tokenAddChar(ep, c) < 0) {
						return EJS_TOK_ERR;
					}
					if ((c = inputGetc(ep)) < 0) {
						break;
					}
				} while (isdigit(c));
#endif
			}

			parseNumber(ep, type);
			inputPutback(ep, c);
			return EJS_TOK_NUMBER;

		default:
			/*
			 *	Identifiers or a function names
			 */
			while (1) {
				if (c == '\\') {
					if ((c = inputGetc(ep)) < 0) {
						break;
					}
					if (c == '\n' || c == '\r') {
						break;
					}
				} else if (tokenAddChar(ep, c) < 0) {
						break;
				}
				if ((c = inputGetc(ep)) < 0) {
					break;
				}
				if (!isalnum(c) && c != '$' && c != '_' && 
						c != '\\' && c != '@') {
					break;
				}
			}
			if (*ep->token == '\0') {
				c = inputGetc(ep);
				break;
			}

			if (! isalpha((int) *ep->token) && *ep->token != '$' && 
					*ep->token != '_' && *ep->token != '@') {
				ejsError(ep, EJS_SYNTAX_ERROR, "Invalid identifier %s", 
					ep->token);
				return EJS_TOK_ERR;
			}

			tid = checkReservedWord(ep, state, c, EJS_TOK_ID);
			if (tid != EJS_TOK_ID) {
				return tid;
			}

			/* 
			 *	Skip white space after token to find out whether this is
			 * 	a function or not.
			 */ 
			while (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
				if ((c = inputGetc(ep)) < 0)
					break;
			}

			tid = EJS_TOK_ID;
			if ((strlen(ep->token) + 1) >= EJS_MAX_ID) {
				ejsError(ep, EJS_SYNTAX_ERROR, 
					"Identifier too big. Max is %d letters.", EJS_MAX_ID);
				return EJS_TOK_ERR;
			}
			done++;
		}
	}

	/*
	 *	Putback the last extra character for next time
	 */
	inputPutback(ep, c);
	return tid;
}

/******************************************************************************/

static void parseNumber(Ejs *ep, EjsType type)
{
	switch (type) {
	case EJS_TYPE_INT:
		ep->tokenNumber.integer = ejsParseInteger(ep->token);
		ep->tokenNumber.type = type;
		break;

#if BLD_FEATURE_FLOATING_POINT
	case EJS_TYPE_FLOAT:
		ep->tokenNumber.floating = atof(ep->token);
		ep->tokenNumber.type = type;
		break;
#endif

#if BLD_FEATURE_INT64
	case EJS_TYPE_INT64:
		ep->tokenNumber.integer64 = ejsParseInteger64(ep->token);
		ep->tokenNumber.type = type;
		break;
#endif
	}
}

/******************************************************************************/
/*
 *	Convert a hex or octal character back to binary, return original char if 
 *	not a hex digit
 */

static int charConvert(Ejs *ep, int base, int maxDig)
{
	int		i, c, lval, convChar;

	lval = 0;
	for (i = 0; i < maxDig; i++) {
		if ((c = inputGetc(ep)) < 0) {
			break;
		}
		/*
		 *	Initialize to out of range value
		 */
		convChar = base;
		if (isdigit(c)) {
			convChar = c - '0';
		} else if (c >= 'a' && c <= 'f') {
			convChar = c - 'a' + 10;
		} else if (c >= 'A' && c <= 'F') {
			convChar = c - 'A' + 10;
		}
		/*
		 *	If unexpected character then return it to buffer.
		 */
		if (convChar >= base) {
			inputPutback(ep, c);
			break;
		}
		lval = (lval * base) + convChar;
	}
	return lval;
}

/******************************************************************************/
/*
 *	Putback the last token read. Accept at most one push back token.
 */

void ejsLexPutbackToken(Ejs *ep, int tid, char *string)
{
	EjsInput	*ip;
	EjsToken	*tp;
	int			idx;

	mprAssert(ep);
	ip = ep->input;
	mprAssert(ip);

	ip->putBackIndex += 1;

	mprAssert(ip->putBackIndex < EJS_TOKEN_STACK);
	idx = ip->putBackIndex;

	tp = &ip->putBack[idx];
	tp->tid = tid;

	mprStrcpy(tp->tokbuf, sizeof(tp->tokbuf), string);
}

/******************************************************************************/
/*
 *	Add a character to the token buffer
 */

static int tokenAddChar(Ejs *ep, int c)
{
	EjsInput	*ip;

	mprAssert(ep);
	ip = ep->input;
	mprAssert(ip);

	if (ip->tokEndp >= &ip->tokbuf[sizeof(ip->tokbuf) - 1]) {
		ejsSyntaxError(ep, "Token too big");
		return -1;
	}
	*ip->tokEndp++ = c;
	*ip->tokEndp = '\0';

	return 0;
}

/******************************************************************************/
/*
 *	Get another input character
 */

static int inputGetc(Ejs *ep)
{
	EjsInput	*ip;
	int			c;

	mprAssert(ep);
	ip = ep->input;

	if (ip->scriptSize <= 0) {
		return -1;
	}

	c = (uchar) (*ip->scriptServp++);
	ip->scriptSize--;

	/*
	 *	For debugging, accumulate the line number and the currenly parsed line
	 */
	if (c == '\n') {
#if 0 && BLD_DEBUG
		if (ip->lineColumn > 0) {
			printf("PARSED: %s\n", ip->line);
		}
#endif
		ip->lineNumber++;
		ip->lineColumn = 0;
	} else if ((ip->lineColumn + 2) < sizeof(ip->line)) {
		ip->line[ip->lineColumn++] = c;
		ip->line[ip->lineColumn] = '\0';
	}
	return c;
}

/******************************************************************************/
/*
 *	Putback a character onto the input queue
 */

static void inputPutback(Ejs *ep, int c)
{
	EjsInput	*ip;

	mprAssert(ep);

	if (c > 0) {
		ip = ep->input;
		*--ip->scriptServp = c;
		ip->scriptSize++;
		if (--(ip->lineColumn) < 0) {
			ip->lineColumn = 0;
		}
		mprAssert(ip->line);
		mprAssert(ip->lineColumn >= 0);
		mprAssert(ip->lineColumn < sizeof(ip->line));
		ip->line[ip->lineColumn] = '\0';
	}
}

/******************************************************************************/

#else
void ejsLexDummy() {}

/******************************************************************************/
#endif /* BLD_FEATURE_EJS */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim:tw=78
 * vim600: sw=4 ts=4 fdm=marker
 * vim<600: sw=4 ts=4
 */