/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk
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
/* 
 * Token class and storage definition									
 */
 
function token(kind, val, fromchar, tochar, row) {
    this.kind = kind;
    this.val = val;
    this.from = fromchar;
    this.to = tochar;
    this.row = row;
}

// Array to hold the tokens				
var tokens = [];            
/*
 * Store token in tokens array
 * Creates a new token object using the constructor
 */
 
function maketoken(kind, val, from, to, rowno) {
	newtoken = new token(kind, val, from, to, rowno);
	tokens.push(newtoken);
}

/*
 * Writes error from tokenizer
 */		
 
function error(str, val, row) {
	alert("Tokenizer Error: " + str + val + " at row " + row);
}

/*
 * Tokenize function 
 * Tokenizer partly based on ideas from the very clever tokenizer written by Douglas Cockford
 * The tokenizer is passed a string, and a string of prefix and suffix terminators
 */
		
function tokenize(instring, inprefix, insuffix) {	    
	// index of the start of the token.
	var from;                   	
	// index of the current character.
	var i = 0;                  	
	// length of the string
	var length = instring.length;		
    // current character.
	var c;                      	
    // current numerical value
	var n;                      	
    // current quote character
	var q;                      	
    // current string value.
	var str;                    	
	// current row value		
	var row=1;										
	c = instring.charAt(i);
	while(c) {			
        from = i;
		// White space
        if(c <= ' ') {
			// Add row if this white space is a row terminator		
        	if((c == '\n') || (c == '\r') || (c == '')) {
				row++;
			}										 																						
		    i++;
    		c = instring.charAt(i);
			// Names i.e. Text
        } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {					
			str = c;
            i++;
            for(;;) {
               	c = instring.charAt(i);
               	if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')|| (c == '_')) {
                    str += c;
                    i++;
               	} else {
                    break;
                }
            }
            maketoken('name', str, from, i, row);
			// Number token
		    } else if (c >= '0' && c <= '9') {												
		        str = c;
		        i++;
		        for(;;) {
		    		c = instring.charAt(i);
		            if(c < '0' || c > '9') {
						break;
					}
		            i++;
		            str += c;
		            }
		        if(c == '.') {
		            i++;
		            str += c;
		            for(;;){
				    	c=instring.charAt(i);
		                if(c < '0' || c > '9'){ 
							break;
						}
		                i++;
		                str += c;
		                }
		            }
		        if(c == 'e' || c =='E') {
		            i++;
		            str += c;
		    		c=instring.charAt(i);
		            if(c == '-' || c == '+') {
		                i++;
		                str += c;
				    	c=instring.charAt(i);
		            }
		            if(c < '0' || c > '9') {
						error('Bad Exponent in Number: ',str,row);
					}
		            do {
		                i++;
		                str += c;
				    	c = instring.charAt(i);
		                } while(c >= '0' && c <= '9');
		            }
		        if(c >= 'a' && c <= 'z') {
		                str += c;
		                i++;
		                error('Bad Number: ',str,row);
		            }
		        n =+ str;
		        if(isFinite(n)) {
 					maketoken('number', n, from, i, row);		            		
		        } else {
		            error('Bad Number: ', str, row);
		        }
		        } else if (c == '\'' || c == '"') {																				// String .. handles c style breaking codes
		            str = '';
		            q = c;
		            i++;
		            for(;;) {
		    		    c = instring.charAt(i);
		                if(c < ' ') {
							if((c == '\n') || (c == '\r') || (c == '')) {
								// Add row if this white space is a row terminator
								row++;
							} else {
								error('Unterminated String: ', str, row);		                		
							}
		                }
		                if (c == q) {
							break;
						}
		                if(c == '\\') {
		                    i++;
		                    if(i >= length) {
				                error('Unterminated String: ', str, row);		                		
		                    }
				    		c = instring.charAt(i);
		                    if(c == 'b') { 
								c = '\b'; 
								break; 
							}
		                    if(c == 'f') { 
								c = '\f'; 
								break; 
							}
		                    if(c == 'n') { 
								c = '\n'; 
								break; 
							}
		                    if(c == 'r') { 
								c = '\r'; 
								break; 
							}
		                    if(c == 't') { 
								c = '\t'; 
								break; 
							}
		                    if(c == 'u') {
		                        if(i >= length) {
						        	error('Unterminated String: ', str, row);		                		
		                        }
		                        c = parseInt(this.substr(i + 1, 4), 16);
		                        if(!isFinite(c) || c < 0) {
						         	error('Unterminated String: ', str, row);		                		
		                        }
		                        c = String.fromCharCode(c);
		                        i+=4;
		                        break;		                    
		                    }
		                }
		                str += c;
		                i++;
		            }
		            i++;
		            maketoken('string', str, from, i, row);
    		        c = instring.charAt(i);
				// Comment of // type ... does not cover block comments
		        } else if (c == '/' && instring.charAt(i+1) == '/') {								
		            i++;
		            for(;;) {
		    		    c=instring.charAt(i);
		                if(c == '\n' || c == '\r' || c == '') {
		                    row++;
		                    break;
		                }
		                i++;
		            }
				// Block comment of /* type
		        } else if (c == '/' && instring.charAt(i+1) == '*') {								
		            i++;
		            for(;;) {
		    		    c=instring.charAt(i);
		                if((c == '*' && instring.charAt(i+1) == '/') || (i == length)) {
		                    i+=2;
				    		c=instring.charAt(i);
		                    break;
		                }
		                if(c == '\n' || c == '\r' || c == '') {
		                    row++;
		                }
		                i++;
		            }
				// Multi-character Operators
        		} else if (inprefix.indexOf(c) >= 0) {											
            		str = c;
            		i++;
            		while(true) {
		    		    c=instring.charAt(i);
                		if(i >= length || insuffix.indexOf(c) < 0) {
                    		break;
                		}
                		str += c;
                		i++;
            		}
					// Single-character Operators
            		maketoken('operator', str, from, i, row);
        		} else {																									
            		i++;
            		maketoken('operator', c, from, i, row);
            		c = instring.charAt(i);
        		}
			}
		}
		function newbutton()
		{
			var funclist;
			var oplist;
			oplist = document.getElementById('infobox');
			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;";
			tokenize(string_tokenize, "<>+-&","=>&:");				
			// Iterate over token objects and print kind of each token and token type in window 
			printout = document.getElementById('infobox');
			for(i = 0;i < tokens.length;i++) {
				printout.innerHTML += tokens[i].kind + " " + tokens[i].val + " (" + tokens[i].from + "-" + tokens[i].to + ") at: " + tokens[i].row + "<br>";
			}	
		}