-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.js
167 lines (142 loc) · 4.66 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
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
const TokenType = require('./token-types')
const CharToTokenType = {
"as": TokenType.AS,
"}": TokenType.CL_CURLY,
")": TokenType.CL_PAREN,
"/": TokenType.DIV, //change constants to another object. Make this for tokenizing only
"=" : TokenType.EQ,
"-": TokenType.MINUS,
"*": TokenType.MULT,
"{": TokenType.OP_CURLY,
"(": TokenType.OP_PAREN,
"+": TokenType.PLUS
};
const Keywords = {
"FOR": "repeat",
"VAR": "var",
"SAVE": "save",
"AS": "as"
}
class Token {
constructor(type, value) {
this.type = type;
this.value = value;
}
}
class Lexer {
constructor(inputString) {
this.currentPosition = 0;
this.inputString = inputString;
}
parseInt() {
var finalInt = "";
while (!isNaN(parseInt(this.inputString[this.currentPosition]))) {
finalInt += this.inputString[this.currentPosition];
this.currentPosition++
}
return finalInt ? new Token(TokenType.NUMBER, parseInt(finalInt)) : false
}
parseKeyword(keyword, tokenType) {
for (let index = 0; index < keyword.length; index++) {
if (
!this.inputString ||
this.inputString[this.currentPosition + index] != keyword[index]
) {
return false;
}
}
this.currentPosition += keyword.length;
return new Token(tokenType, keyword);
}
parsePrint() {
var printMatch = this.inputString
.substr(
this.currentPosition,
"print".length
) == "print";
if (printMatch) {
this.currentPosition += 5;
return new Token(TokenType.PRINT);
}
return false;
}
parseString() {
if (this.inputString[this.currentPosition] != "\"") {
return false;
}
var finalString = "";
var addedIndex = 1;
while (this.currentPosition + addedIndex < this.inputString.length &&
this.inputString[this.currentPosition + addedIndex] != "\"") {
finalString += this.inputString[this.currentPosition + addedIndex]
addedIndex++;
}
if (this.currentPosition + addedIndex > this.inputString.length) {
throw new Error("String never ended.");
}
this.currentPosition += (finalString.length + 2)
return new Token(TokenType.STRING, finalString);
}
parseChar(char){
if (this.inputString[this.currentPosition] == char) {
this.currentPosition++;
return new Token(CharToTokenType[char], char);
}
}
isChar(char){
return char.toUpperCase() != char.toLowerCase();
}
parseIdentifier(){
if(!this.inputString && !this.inputString[this.currentPosition]){
return;
}
var str = "";
while(this.currentPosition < this.inputString.length && this.isChar(this.inputString[this.currentPosition])){
str+=this.inputString[this.currentPosition++]
}
return str.length && new Token(TokenType.IDENTIFIER, str);
}
discardWhitespace(){
while(this.currentPosition < this.inputString.length
&& (this.inputString[this.currentPosition] == " "
|| this.inputString[this.currentPosition] == "\n"
|| this.inputString[this.currentPosition] == "\t")){
this.currentPosition++;
}
}
error() {
throw new Error(`Invalid token found at pos ${this.currentPosition}: "${this.inputString[this.currentPosition]}"`)
}
parseIntoToken() {
return this.parseInt() ||
this.parseKeyword(Keywords.FOR, TokenType.FOR) ||
this.parseKeyword(Keywords.VAR, TokenType.VAR) ||
this.parseKeyword(Keywords.SAVE, TokenType.SAVE) ||
this.parseKeyword(Keywords.AS, TokenType.AS) ||
this.parseString() ||
this.parsePrint() ||
this.parseIdentifier() ||
this.parseChar("=") ||
this.parseChar("(") ||
this.parseChar(")") ||
this.parseChar("{") ||
this.parseChar("}") ||
this.parseChar("+") ||
this.parseChar("-") ||
this.parseChar("/") ||
this.parseChar("*") ||
this.error()
}
readTokens() {
var tokens = [];
while (this.currentPosition < this.inputString.length) {
this.discardWhitespace()
tokens.push(this.parseIntoToken());
}
if(process.env.debug) {
console.log(JSON.stringify(tokens));
}
return tokens;
}
}
module.exports = Lexer;