2
* Token class and storage definition
5
function token(kind, val, fromchar, tochar, row) {
13
// Array to hold the tokens
16
* Store token in tokens array
17
* Creates a new token object using the constructor
20
function makeToken(kind, val, from, to, rowno) {
21
newtoken = new token(kind, val, from, to, rowno);
22
tokens.push(newtoken);
26
* Writes error from tokenizer
29
function error(str, val, row) {
30
alert("Tokenizer Error: " + str + val + " at row " + row);
35
* Tokenizer partly based on ideas from the very clever tokenizer written by Douglas Cockford
36
* The tokenizer is passed a string, and a string of prefix and suffix terminators
39
function tokenize(instring, inprefix, insuffix) {
40
// index of the start of the token.
42
// index of the current character.
44
// length of the string
45
var length = instring.length;
48
// current numerical value
50
// current quote character
52
// current string value.
56
c = instring.charAt(i);
61
// Add row if this white space is a row terminator
62
if((c == '\n') || (c == '\r') || (c == '')) {
66
c = instring.charAt(i);
68
} else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
72
c = instring.charAt(i);
73
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')|| (c == '_')) {
80
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);
134
// String .. handles c style breaking codes
135
} else if (c == '\'' || c == '"') {
140
c = instring.charAt(i);
142
if((c == '\n') || (c == '\r') || (c == '')) {
143
// Add row if this white space is a row terminator
146
error('Unterminated String: ', str, row);
155
error('Unterminated String: ', str, row);
157
c = instring.charAt(i);
180
error('Unterminated String: ', str, row);
182
c = parseInt(this.substr(i + 1, 4), 16);
183
if(!isFinite(c) || c < 0) {
184
error('Unterminated String: ', str, row);
186
c = String.fromCharCode(c);
195
maketoken('string', str, from, i, row);
196
c = instring.charAt(i);
197
// Comment of // type ... does not cover block comments
198
} else if (c == '/' && instring.charAt(i+1) == '/') {
201
c=instring.charAt(i);
202
if(c == '\n' || c == '\r' || c == '') {
208
// Block comment of /* type
209
} else if (c == '/' && instring.charAt(i+1) == '*') {
212
c=instring.charAt(i);
213
if((c == '*' && instring.charAt(i+1) == '/') || (i == length)) {
215
c=instring.charAt(i);
218
if(c == '\n' || c == '\r' || c == '') {
223
// Multi-character Operators
224
} else if (inprefix.indexOf(c) >= 0) {
228
c=instring.charAt(i);
229
if(i >= length || insuffix.indexOf(c) < 0) {
235
// Single-character Operators
236
maketoken('operator', str, from, i, row);
239
maketoken('operator', c, from, i, row);
240
c = instring.charAt(i);
248
oplist = document.getElementById('infobox');
249
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;";
250
tokenize(string_tokenize, "<>+-&","=>&:");
251
// Iterate over token objects and print kind of each token and token type in window
252
printout = document.getElementById('infobox');
253
for(i = 0;i < tokens.length;i++) {
254
printout.innerHTML += tokens[i].kind + " " + tokens[i].val + " (" + tokens[i].from + "-" + tokens[i].to + ") at: " + tokens[i].row + "<br>";