-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlexer.h
More file actions
185 lines (158 loc) · 4.1 KB
/
lexer.h
File metadata and controls
185 lines (158 loc) · 4.1 KB
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
/* Digital Mars DMDScript source code.
* Copyright (c) 2000-2001 by Chromium Communications
* Copyright (c) 2004-2007 by Digital Mars
* All Rights Reserved.
* written by Walter Bright
* http://www.digitalmars.com/dscript/cppscript.html
*
* This software is for evaluation purposes only.
* It may not be redistributed in binary or source form,
* incorporated into any product or program,
* or used for any purpose other than evaluating it.
* To purchase a license,
* see http://www.digitalmars.com/dscript/cpplicense.html
*
* Use at your own risk. There is no warranty, express or implied.
*/
#ifndef LEXER_H
#define LEXER_H
//#pragma once
#include "root.h"
#include "port.h"
#include "lstring.h"
#include "mem.h"
#include "dscript.h"
struct StringTable;
struct Identifier;
/* Tokens:
( )
[ ]
{ }
< > <= >= == !=
=== !==
<< >> <<= >>= >>> >>>=
+ - += -=
* / % *= /= %=
& | ^ &= |= ^=
= ! ~
++ --
. : ,
? && ||
*/
enum TOK
{
TOKreserved,
// Other
TOKlparen, TOKrparen,
TOKlbracket, TOKrbracket,
TOKlbrace, TOKrbrace,
TOKcolon, TOKneg,
TOKpos,
TOKsemicolon, TOKeof,
TOKarray, TOKcall,
TOKarraylit, TOKobjectlit,
TOKcomma, TOKassert,
// Operators
TOKless, TOKgreater,
TOKlessequal, TOKgreaterequal,
TOKequal, TOKnotequal,
TOKidentity, TOKnonidentity,
TOKshiftleft, TOKshiftright,
TOKshiftleftass, TOKshiftrightass,
TOKushiftright, TOKushiftrightass,
TOKplus, TOKminus, TOKplusass, TOKminusass,
TOKmultiply, TOKdivide, TOKpercent,
TOKmultiplyass, TOKdivideass, TOKpercentass,
TOKand, TOKor, TOKxor,
TOKandass, TOKorass, TOKxorass,
TOKassign, TOKnot, TOKtilde,
TOKplusplus, TOKminusminus,
TOKdot,
TOKquestion, TOKandand, TOKoror,
// Leaf operators
TOKnumber, TOKidentifier, TOKstring,
TOKregexp, TOKreal,
// Keywords
TOKbreak, TOKcase, TOKcontinue,
TOKdefault, TOKdelete, TOKdo,
TOKelse, TOKexport, TOKfalse,
TOKfor, TOKfunction, TOKif,
TOKimport, TOKin, TOKnew,
TOKnull, TOKreturn, TOKswitch,
TOKthis, TOKtrue, TOKtypeof,
TOKvar, TOKvoid, TOKwhile,
TOKwith,
// Reserved for ECMA extensions
TOKcatch, TOKclass,
TOKconst, TOKdebugger,
TOKenum, TOKextends,
TOKfinally, TOKsuper,
TOKthrow, TOKtry,
// Java keywords reserved for unknown reasons
TOKabstract, TOKboolean,
TOKbyte, TOKchar,
TOKdouble, TOKfinal,
TOKfloat, TOKgoto,
TOKimplements, TOKinstanceof,
TOKint, TOKinterface,
TOKlong, TOKnative,
TOKpackage, TOKprivate,
TOKprotected, TOKpublic,
TOKshort, TOKstatic,
TOKsynchronized, TOKthrows,
TOKtransient,
TOKMAX
};
struct Lexer;
struct Token
{
Token *next;
dchar *ptr; // pointer to first character of this token within buffer
unsigned linnum;
enum TOK value;
// Where we saw the last line terminator
dchar *sawLineTerminator;
union
{
number_t intvalue;
real_t realvalue;
Lstring *string;
Identifier *ident;
};
static dchar *tochars[TOKMAX];
static void *operator new(size_t sz, Lexer *lex);
void print();
dchar *toDchars();
static dchar *toDchars(enum TOK);
};
struct Lexer : Mem
{
StringTable *stringtable;
Token *freelist;
const char *sourcename; // for error message strings
dchar *base; // pointer to start of buffer
dchar *end; // past end of buffer
dchar *p; // current character
unsigned currentline;
Token token;
OutBuffer stringbuffer;
int useStringtable; // use for Identifiers
ErrInfo errinfo; // syntax error information
Lexer(const char *sourcename, dchar *base, unsigned length, int useStringtable);
~Lexer();
static TOK isKeyword(dchar *s, unsigned len);
static void initKeywords();
TOK nextToken();
void insertSemicolon(dchar *loc);
void rescan();
void scan(Token *t);
Token *peek(Token *t);
unsigned escapeSequence();
Lstring *string(unsigned quote);
Lstring *regexp();
unsigned unicode();
TOK number(Token *t);
void error(int, ...);
static dchar *locToSrcline(dchar *src, Loc loc);
};
#endif