1
// Array to hold the tokens
2
4
* Token class and storage definition
5
6
function token(kind, val, fromchar, tochar, row) {
8
9
this.from = fromchar;
13
// Array to hold the tokens
16
14
* Store token in tokens array
17
15
* Creates a new token object using the constructor
20
function maketoken(kind, val, from, to, rowno) {
17
function makeToken(kind, val, from, to, rowno) {
21
18
newtoken = new token(kind, val, from, to, rowno);
22
19
tokens.push(newtoken);
26
22
* Writes error from tokenizer
29
24
function error(str, val, row) {
30
25
alert("Tokenizer Error: " + str + val + " at row " + row);
34
28
* Tokenize function
35
29
* Tokenizer partly based on ideas from the very clever tokenizer written by Douglas Cockford
36
30
* The tokenizer is passed a string, and a string of prefix and suffix terminators
39
32
function tokenize(instring, inprefix, insuffix) {
40
33
// index of the start of the token.
80
maketoken('name', str, from, i, row);
73
makeToken('name', str, from, i, row);
82
} else if (c >= '0' && c <= '9') {
86
c = instring.charAt(i);
87
if(c < '0' || c > '9') {
98
if(c < '0' || c > '9'){
105
if(c == 'e' || c =='E') {
108
c=instring.charAt(i);
109
if(c == '-' || c == '+') {
112
c=instring.charAt(i);
114
if(c < '0' || c > '9') {
115
error('Bad Exponent in Number: ',str,row);
120
c = instring.charAt(i);
121
} while(c >= '0' && c <= '9');
123
if(c >= 'a' && c <= 'z') {
126
error('Bad Number: ',str,row);
130
maketoken('number', n, from, i, row);
132
error('Bad Number: ', str, row);
75
} else if (c >= '0' && c <= '9') {
79
c = instring.charAt(i);
80
if(c < '0' || c > '9') {
91
if(c < '0' || c > '9'){
98
if(c == 'e' || c =='E') {
101
c=instring.charAt(i);
102
if(c == '-' || c == '+') {
105
c=instring.charAt(i);
134
} else if (c == '\'' || c == '"') { // String .. handles c style breaking codes
139
c = instring.charAt(i);
141
if((c == '\n') || (c == '\r') || (c == '')) {
142
// Add row if this white space is a row terminator
145
error('Unterminated String: ', str, row);
154
error('Unterminated String: ', str, row);
156
c = instring.charAt(i);
179
error('Unterminated String: ', str, row);
181
c = parseInt(this.substr(i + 1, 4), 16);
182
if(!isFinite(c) || c < 0) {
183
error('Unterminated String: ', str, row);
185
c = String.fromCharCode(c);
194
maketoken('string', str, from, i, row);
195
c = instring.charAt(i);
196
// Comment of // type ... does not cover block comments
197
} else if (c == '/' && instring.charAt(i+1) == '/') {
200
c=instring.charAt(i);
201
if(c == '\n' || c == '\r' || c == '') {
107
if(c < '0' || c > '9') {
108
error('Bad Exponent in Number: ',str,row);
113
c = instring.charAt(i);
114
} while(c >= '0' && c <= '9');
116
if(c >= 'a' && c <= 'z') {
119
error('Bad Number: ',str,row);
123
makeToken('number', n, from, i, row);
125
error('Bad Number: ', str, row);
127
// String .. handles c style breaking codes
128
} else if (c == '\'' || c == '"') {
133
c = instring.charAt(i);
135
if((c == '\n') || (c == '\r') || (c == '')) {
136
// Add row if this white space is a row terminator
139
error('Unterminated String: ', str, row);
148
error('Unterminated String: ', str, row);
150
c = instring.charAt(i);
173
error('Unterminated String: ', str, row);
175
c = parseInt(this.substr(i + 1, 4), 16);
176
if(!isFinite(c) || c < 0) {
177
error('Unterminated String: ', str, row);
179
c = String.fromCharCode(c);
188
makeToken('string', str, from, i, row);
189
c = instring.charAt(i);
190
// Comment of // type ... does not cover block comments
191
} else if (c == '/' && instring.charAt(i+1) == '/') {
194
c=instring.charAt(i);
195
if(c == '\n' || c == '\r' || c == '') {
207
201
// Block comment of /* type
208
} else if (c == '/' && instring.charAt(i+1) == '*') {
211
c=instring.charAt(i);
212
if((c == '*' && instring.charAt(i+1) == '/') || (i == length)) {
214
c=instring.charAt(i);
217
if(c == '\n' || c == '\r' || c == '') {
222
// Multi-character Operators
223
} else if (inprefix.indexOf(c) >= 0) {
227
c=instring.charAt(i);
228
if(i >= length || insuffix.indexOf(c) < 0) {
234
// Single-character Operators
235
maketoken('operator', str, from, i, row);
238
maketoken('operator', c, from, i, row);
239
c = instring.charAt(i);
247
oplist = document.getElementById('infobox');
248
string_tokenize = "foo // Lederhosen\nsin'foo'(200.0*r)+(-21.4/1.51e-6)+/*Plodder*/(Feeeb+400)*ln(3000);\ncos(200.1-atan2(41.0,51.0));\nHello+85-4*2.6-1.51e-6;";
249
tokenize(string_tokenize, "<>+-&","=>&:");
250
// Iterate over token objects and print kind of each token and token type in window
251
printout = document.getElementById('infobox');
252
for(i = 0;i < tokens.length;i++) {
253
printout.innerHTML += tokens[i].kind + " " + tokens[i].val + " (" + tokens[i].from + "-" + tokens[i].to + ") at: " + tokens[i].row + "<br>";
202
} else if (c == '/' && instring.charAt(i+1) == '*') {
205
c=instring.charAt(i);
206
if((c == '*' && instring.charAt(i+1) == '/') || (i == length)) {
208
c=instring.charAt(i);
211
if(c == '\n' || c == '\r' || c == '') {
216
// Multi-character Operators
217
} else if (inprefix.indexOf(c) >= 0) {
221
c=instring.charAt(i);
222
if(i >= length || insuffix.indexOf(c) < 0) {
228
// Single-character Operators
229
makeToken('operator', str, from, i, row);
232
makeToken('operator', c, from, i, row);
233
c = instring.charAt(i);
238
function newButton() {
241
oplist = document.getElementById('infobox');
242
string_tokenize = "foo // Lederhosen\nsin'foo'(200.0*r)+(-21.4/1.51e-6)+/*Plodder*/(Feeeb+400)*ln(3000);\ncos(200.1-atan2(41.0,51.0));\nHello+85-4*2.6-1.51e-6;";
243
tokenize(string_tokenize, "<>+-&","=>&:");
244
// Iterate over token objects and print kind of each token and token type in window
245
printout = document.getElementById('infobox');
246
for(i = 0;i < tokens.length;i++) {
247
printout.innerHTML += tokens[i].kind + " " + tokens[i].val + " (" + tokens[i].from + "-" + tokens[i].to + ") at: " + tokens[i].row + "<br>";