4
<title>Parser test</title>
5
<script type="text/javascript" src="Parser_Test.js"></script>
2
7
<body onload="newbutton();">
7
<div style="border:2px solid black;background-color:#fed;width:500;height:1200;">
8
<div id="infobox" style="padding:4px;">
11
<div style="border:2px solid black;background-color:#fed;width:500;height:1200;">
12
<div id="infobox" style="padding:4px;">
17
<script lang='Javascript'>
19
// Token class and storage definition
20
function token (kind,val,fromchar,tochar,row) {
28
var tokens = []; // Array to hold the tokens.
30
//----------------------------------------------------------
31
// Store token in tokens array
32
// Creates a new token object using the constructor
33
//----------------------------------------------------------
35
function maketoken(kind,val,from,to,rowno)
37
newtoken=new token(kind,val,from,to,rowno);
38
tokens.push(newtoken);
41
//----------------------------------------------------------
42
// Writes error from tokenizer
43
//----------------------------------------------------------
45
function error(str,val,row)
47
alert("Tokenizer Error: "+str+val+" at row "+row);
50
//----------------------------------------------------------
52
// Tokenizer partly based on ideas from the very clever tokenizer written by Douglas Cockford
53
// The tokenizer is passed a string, and a string of prefix and suffix terminators
54
//----------------------------------------------------------
56
function tokenize(instring,inprefix,insuffix){
58
var from; // index of the start of the token.
59
var i = 0; // index of the current character.
60
var length=instring.length; // length of the string
62
var c; // current character.
63
var n; // current numerical value
64
var q; // current quote character
65
var str; // current string value.
66
var row=1; // current row value
69
c = instring.charAt(i);
74
if (c <= ' '){ // White space
75
if((c=='\n')||(c=='\r')||(c == '')) row++; // Add row if this white space is a row terminator
77
c = instring.charAt(i);
78
}else if ((c >='a'&&c<='z')||(c>='A'&&c<='Z')) { // Names i.e. Text
82
c = instring.charAt(i);
83
if ((c >='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9')||c=='_'){
90
maketoken('name',str,from,i,row);
91
} else if (c >= '0' && c <= '9') { // Number token
95
c = instring.charAt(i);
96
if (c < '0' || c > '9') break;
104
c=instring.charAt(i);
105
if (c < '0' || c > '9') break;
110
if (c=='e'||c=='E') {
113
c=instring.charAt(i);
117
c=instring.charAt(i);
119
if (c < '0' || c > '9') error('Bad Exponent in Number: ',str,row);
123
c=instring.charAt(i);
124
}while(c>='0'&&c<='9');
129
error('Bad Number: ',str,row);
133
maketoken('number',n,from,i,row);
135
error('Bad Number: ',str,row);
138
} else if(c=='\''||c=='"'){ // String .. handles c style breaking codes
143
c=instring.charAt(i);
146
if((c=='\n')||(c=='\r')||(c == '')) row++; // Add row if this white space is a row terminator
147
error('Unterminated String: ',str,row);
155
error('Unterminated String: ',str,row);
157
c=instring.charAt(i);
159
if(c=='b'){ c='\b'; break; }
160
if(c=='f'){ c='\f'; break; }
161
if(c=='n'){ c='\n'; break; }
162
if(c=='r'){ c='\r'; break; }
163
if(c=='t'){ c='\t'; break; }
166
error('Unterminated String: ',str,row);
168
c = parseInt(this.substr(i + 1, 4), 16);
169
if (!isFinite(c) || c < 0) {
170
error('Unterminated String: ',str,row);
172
c = String.fromCharCode(c);
181
maketoken('string',str,from,i,row);
182
c=instring.charAt(i);
184
} else if (c=='/'&&instring.charAt(i+1)=='/'){ // Comment of // type ... does not cover block comments
187
c=instring.charAt(i);
188
if (c=='\n'||c=='\r'||c=='') {
194
} else if (c=='/'&&instring.charAt(i+1)=='*'){ // Block comment of /* type
197
c=instring.charAt(i);
198
if ((c=='*'&&instring.charAt(i+1)=='/')||(i==length)) {
200
c=instring.charAt(i);
203
if (c=='\n'||c=='\r'||c=='') {
208
}else if(inprefix.indexOf(c) >= 0) { // Multi-character Operators
212
c=instring.charAt(i);
213
if (i >= length || insuffix.indexOf(c) < 0) {
219
maketoken('operator',str,from,i,row);
220
} else { // Single-character Operators
222
maketoken('operator',c,from,i,row);
223
c = instring.charAt(i);
234
oplist=document.getElementById('infobox');
236
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;";
238
tokenize(string_tokenize,"<>+-&","=>&:");
241
// Iterate over token objects and print kind of each token and token type in window
242
printout=document.getElementById('infobox');
243
for(i=0;i<tokens.length;i++){
244
printout.innerHTML+=tokens[i].kind+" "+tokens[i].val+" ("+tokens[i].from+"-"+tokens[i].to+") at: "+tokens[i].row+"<br>";