-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.js
165 lines (142 loc) · 4.23 KB
/
token.js
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
// The tokenizer constructor
function Tokenizer(src_str) {
// Convienience to properly format each token
function tkn(pattern, constructor) {
return {
regex: new RegExp('^' + pattern),
constructor: constructor
};
}
// Escape special newline characters so we'll match them literally
function escape_sym(sym) {
sym = sym.replace("\\", "\\\\");
sym = sym.replace("+", "\\+");
sym = sym.replace("*", "\\*");
sym = sym.replace("[", "\\[");
sym = sym.replace("]", "\\]");
sym = sym.replace("(", "\\(");
sym = sym.replace(")", "\\)");
sym = sym.replace(".", "\\.");
return sym;
}
// Convienience for declaring keywords and direct symbols
function sym(word) {
return tkn(escape_sym(word), function sym(str) {
this.token_type = word;
});
}
// Add new token types here. Matches will be determined first on length
// and then on order in this list, e.g int is listed before float so that
// 123 will match int and not float though both match
var token_types = [
// Keywords
sym('function'),
sym('var'),
sym('while'),
sym('if'),
sym('else'),
sym('with'),
// Operators etc.
sym('.'),
sym('+'),
sym('-'),
sym('*'),
sym('/'),
sym('%'),
sym('('),
sym(')'),
sym('{'),
sym('}'),
sym(','),
sym(';'),
sym('='),
sym('=='),
sym('==='),
sym('!='),
sym('!=='),
sym('>'),
sym('<'),
// Identifier
tkn('[a-zA-Z_$]\\w*', function iden(str) {
this.token_type = "iden";
this.value = str;
}),
// Integer
tkn('[+-]?\\d+', function int(str) {
this.token_type = "int";
this.value = parseInt(str, 10);
}),
// float
tkn('[+-]?\\d+(\\.\\d+)?', function float(str) {
this.token_type = "float";
this.value = parseFloat(str, 10);
}),
];
// Returns the next token in the stream
function internal_next() {
// First skip past any white space and comments
var whiteSpace = /^\s/;
var c_comment = /^\/\*.*?\*\//;
var cpp_comment = /^\/\/.*?\n/;
while(true) {
if(whiteSpace.test(src_str)) {
src_str = src_str.replace(whiteSpace, '');
} else if(c_comment.test(src_str)) {
src_str = src_str.replace(c_comment, '');
} else if(cpp_comment.test(src_str)) {
src_str = src_str.replace(cpp_comment, '');
} else break;
}
if(src_str === "") {
// End of input
return null;
}
var matches = [];
function add_match(tkn) {
if(!matches[0]) matches.push(tkn);
for(var i in matches) {
if(matches[i].length < tkn.length) {
matches.splice(i, 0, tkn);
}
}
};
for(var i in token_types) {
var match = token_types[i].regex.exec(src_str);
if(match) {
var tok = new token_types[i].constructor(match[0])
tok.length = match[0].length;
add_match(tok);
}
}
if(!matches[0]) {
throw "No match for sequence beginning " +
src_str.substr(0, 3) + "...";
}
src_str = src_str.slice(matches[0].length);
return matches[0];
}
var tk_stk = [];
tk_stk.push(internal_next());
var stk_i = 0;
this.peek = function() {
return tk_stk[stk_i];
}
this.next = function() {
stk_i++;
if(!tk_stk[stk_i]) {
var ne = internal_next();
if(ne)
tk_stk.push(ne);
else
stk_i--;
}
}
// These methods allow the parser to "save" a point in
// the token stream and rewind the stream back if a rule fails
this.checkPoint = function() {
return stk_i;
}
this.rewind = function(index) {
stk_i = index;
}
}