-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.js
83 lines (83 loc) · 2 KB
/
Lexer.js
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
var isDigit = function (char) {
return char && /\d/.test(char);
};
var isLetter = function (char) {
return char && /[a-zA-Z]/.test(char); ///\w/.test(undefined) true!!!!!!!!!!!!!
};
var Lexer = function (chars) {
this.chars = chars;
this.line = 1;
this.peek = this.chars.shift();
this.words = {
'true': {
tag: 'true',
lexeme : 'true'
},
'false': {
tag: 'false',
lexeme : 'false'
}
};
};
Lexer.prototype.scan = function () {
//excape the whitespace
while (true) {
//excape comment
if (this.peek == '/' && this.chars[0] == '/') {
while (true) {
this.peek = this.chars.shift();
if (this.peek == '\n' || !this.chars[0]) {
this.peek = this.chars.shift();
break;
}
}
}
if(this.peek == ' ' || this.peek == '\t') {
this.peek = this.chars.shift();
continue;
}
else if (this.peek == '\n') this.line += 1;
else break;
}
//match number
if (isDigit(this.peek)) {
var value = 0;
do {
value = 10 * value + parseInt(this.peek, 10);
this.peek = this.chars.shift();
} while (isDigit(this.peek));
return {
tag: 'num',
value : value
};
}
//match work
if (isLetter(this.peek)) {
var lexeme = '';
do {
lexeme += this.peek;
this.peek = this.chars.shift();
} while (isDigit(this.peek) || isLetter(this.peek));
word = this.words[lexeme];
if (word) {
return word
} else {
word = this.words[lexeme] = {
tag: 'id',
lexeme: lexeme
};
return word;
}
return {
tag: this.peek
}
}
};
console.log(new Lexer(''.split('')).scan());
console.log(new Lexer('//d'.split('')).scan());
console.log(new Lexer('//d\na'.split('')).scan());
console.log(new Lexer('//d\n'.split('')).scan());
console.log(new Lexer(' 11 '.split('')).scan());
console.log(new Lexer(' abb '.split('')).scan());
console.log(new Lexer(' a11 '.split('')).scan());
console.log(new Lexer(' true '.split('')).scan());