forked from KJlmfe/KJCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scanner2.html
179 lines (177 loc) · 8.8 KB
/
Scanner2.html
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
<!DOCTYPE>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript">
/*log打印函数 每次在原log上追加str和换行*/
function log(str) {
var log_node = document.getElementById("log");
var text_node = document.createTextNode(str);
var br_node = document.createElement("br");
log_node.appendChild(text_node);
log_node.appendChild(br_node);
}
</script>
<script type="text/javascript">
/* 字符串字符处理雷 */
function Reader(str) {
this.data = str;
this.currPos = 0;
this.length = str.length;
}
/* 获取字符串下一个字符 */
Reader.prototype.nextChar = function() {
if(this.currPos >= this.length) {
return -1;
}
return this.data[this.currPos++];
}
/* 回退当前读取位置指针 n是回退的步伐 默认为1步*/
Reader.prototype.retract = function(n) {
if(n === undefined) {
n = 1;
}
this.currPos -= n;
if(this.currPos < 0) {
this.currPos = 0;
}
}
</script>
<script type="text/javascript">
/* 定义Token类型 */
function Token(type, text) {
this.type = type;
this.text = text;
}
/* 定义关键字 保留字 */
Token.tokens = {};
Token.tokens.EOS_TOKEN = 1; //end of stream
// using + 1 allows adding a new token easily later
Token.tokens.COLON_TOKEN = Token.tokens.EOS_TOKEN + 1; //:
Token.tokens.SEMICOLON_TOKEN = Token.tokens.COLON_TOKEN + 1; //;
Token.tokens.LEFTPAREN_TOKEN = Token.tokens.SEMICOLON_TOKEN + 1; //(
Token.tokens.RIGHTPAREN_TOKEN = Token.tokens.LEFTPAREN_TOKEN + 1; //)
Token.tokens.LEFTBRACE_TOKEN = Token.tokens.RIGHTPAREN_TOKEN + 1; //{
Token.tokens.RIGHTBRACE_TOKEN = Token.tokens.LEFTBRACE_TOKEN + 1; //}
Token.tokens.MOD_TOKEN = Token.tokens.RIGHTBRACE_TOKEN + 1; //%
Token.tokens.VAR_TOKEN = Token.tokens.MOD_TOKEN + 1; //var
Token.tokens.TYPE_TOKEN = Token.tokens.VAR_TOKEN + 1; //int bool
Token.tokens.BOOLLITERAL_TOKEN = Token.tokens.TYPE_TOKEN + 1; //true false
Token.tokens.IF_TOKEN = Token.tokens.BOOLLITERAL_TOKEN + 1; //if
Token.tokens.ELSE_TOKEN = Token.tokens.IF_TOKEN + 1; //else
Token.tokens.WHILE_TOKEN = Token.tokens.ELSE_TOKEN + 1; //while
Token.tokens.PRINT_TOKEN = Token.tokens.WHILE_TOKEN + 1; //print
Token.tokens.IDENTIFIER_TOKEN = Token.tokens.PRINT_TOKEN + 1; //id
Token.backwardMap = {}; //for inverse look-up
for(var x in Token.tokens) {
Token.backwardMap[Token.tokens[x]] = x;
}
</script>
<script type="text/javascript">
function Scanner(reader) {
this.reader = reader;
this.currentToken = new Token();
this.currLine = 0;
this.bufferStr;
this.state = Scanner.START_STATE;
}
Scanner.START_STATE = 0;
Scanner.IDENTIFIER_STATE = Scanner.START_STATE + 1;
Scanner.prototype.makeToken = function(type, text) {
this.currentToken.type = type;
this.currentToken.text = text;
return type;
};
Scanner.prototype.nextToken = function() {
switch(this.state) {
case Scanner.START_STATE:
while(true) {
var next_char = this.reader.nextChar();
/* 遇到字母开头 表明可能是标示符或者是保留字 跳转状态*/
if((next_char >= 'a' && next_char <= 'z') || (next_char >= 'A' && next_char <= 'Z')) {
this.state = Scanner.IDENTIFIER_STATE;
this.bufferStr = next_char; //记录单词首字符
return this.nextToken();
}
switch(next_char) {
case -1 : return this.makeToken(Token.tokens.EOS_TOKEN);
case ':': return this.makeToken(Token.tokens.COLON_TOKEN);
case ';': return this.makeToken(Token.tokens.SEMICOLON_TOKEN);
case '(': return this.makeToken(Token.tokens.LEFTPAREN_TOKEN);
case ')': return this.makeToken(Token.tokens.RIGHTPAREN_TOKEN);
case '{': return this.makeToken(Token.tokens.LEFTBRACE_TOKEN);
case '}': return this.makeToken(Token.tokens.RIGHTBRACE_TOKEN);
case '%': return this.makeToken(Token.tokens.MOD_TOKEN);
case '\r': case '\n':
this.currLine++;
default:
//ignore other char
}
}
break;
case Scanner.IDENTIFIER_STATE:
var next_char = this.reader.nextChar();
if((next_char >= 'a' && next_char <= 'z') || (next_char >= 'A' && next_char <= 'Z')) {
this.bufferStr += next_char;
return this.nextToken();
} else {
/* 读取了连续由字母构成的单词 则一个单词字元读取结束 切换状态到开始状态 以便下次读取新的字元*/
this.state = Scanner.START_STATE;
/* 由于多读入了一个非字母字符来判断是否单词结束 所以要对字符读取器回退一个字符 */
this.reader.retract();
}
switch(this.bufferStr) {
case "var":
return this.makeToken(Token.tokens.VAR_TOKEN);
case "int": case "bool":
return this.makeToken(Token.tokens.TYPE_TOKEN, this.bufferStr);
case "true": case "false": case "TRUE": case "FALSE":
return this.makeToken(Token.tokens.BOOLLITERAL_TOKEN, this.bufferStr.toLowerCase());
case "if":
return this.makeToken(Token.tokens.IF_TOKEN);
case "else":
return this.makeToken(Token.tokens.ELSE_TOKEN);
case "while":
return this.makeToken(Token.tokens.WHILE_TOKEN);
case "print" :
return this.makeToken(Token.tokens.PRINT_TOKEN);
default:
return this.makeToken(Token.tokens.IDENTIFIER_TOKEN, this.bufferStr);
}
break;
default:
break;
}
};
</script>
<script type="text/javascript">
window.onload = function () {
var textarea = document.getElementById("source_code");
var run_button = document.getElementById("run");
/* 测试Scanner类 逐个读取Token 且输出每次Token到log*/
run_button.onclick = function() {
var code_to_be_compiled = textarea.value;
var reader = new Reader(code_to_be_compiled);
var scanner = new Scanner(reader);
while(true) {
var next_token = scanner.nextToken();
if(next_token == Token.tokens.EOS_TOKEN) {
break;
}
var logStr = "Reader Token: " + Token.backwardMap[next_token];
if(scanner.currentToken.text !== undefined) {
logStr += "(" + scanner.currentToken.text + ")";
}
log(logStr);
}
};
};
</script>
</head>
<body>
<textarea id="source_code" style="width:600px; height:200px"></textarea>
<button id="run">Run</button>
<div id="log">
</div>
</body>
</html>