This repository was archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlexer.ts
More file actions
174 lines (138 loc) · 5.96 KB
/
Copy pathlexer.ts
File metadata and controls
174 lines (138 loc) · 5.96 KB
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
import NaNError from "../../errors/lexing/not-a-number";
import InvalidIPv4Error from "../../errors/lexing/invalid-ipv4";
import InvalidOperatorError from "../../errors/lexing/invalid-operator";
import InvalidTokenError from "../../errors/lexing/invalid-token";
import Position from "./position";
import tokens from "./tokens";
export default class Lexer {
public constructor(public text: string, public filename: string, public position = new Position(-1, 0, -1, filename, text), public madeTokens: string[] = [], public currentChar?: string, public previousChar?: string) {
this.advance();
}
public advance() {
this.position.advance(this.currentChar || "");
[
this.previousChar,
this.currentChar
] = [
this.currentChar,
this.text[this.position.index]
];
}
public makeIPv4(firstValue: string, startPosition: Position) {
if (!firstValue || Number.isNaN(+firstValue) || +firstValue < 0 || +firstValue > 255.255) {
throw new NaNError(startPosition, this.position);
}
let cacheValue = "",
dotCount = 0,
valuePosition = this.position.copy();
const values: [ string, string, string, string ] = [ firstValue.split(".")[0], firstValue.split(".")[1], "", "" ];
while (this.currentChar && "0123456789.".includes(this.currentChar)) {
if (/\d/.test(this.currentChar)) {
if (this.previousChar === "0") {
while (this.currentChar === "0") {
this.advance();
}
throw new InvalidIPv4Error(
valuePosition,
this.position
);
}
cacheValue += this.currentChar;
} else if (this.currentChar === ".") {
valuePosition = this.position.copy().advance("").advance("");
if (!cacheValue || Number.isNaN(+cacheValue) || +cacheValue > 255) {
throw new InvalidIPv4Error(
valuePosition,
this.position
);
}
[ values[++dotCount + 1], cacheValue ] = [ cacheValue, "" ];
}
this.advance();
}
values[3] = cacheValue;
if (!dotCount || !values.every(value => value)) {
throw new InvalidIPv4Error(
this.position.advance(""),
this.position.copy().advance("").advance("")
);
}
return `${tokens.ipv4}:${values.join(".")}`;
}
public makeNumbers() {
let numberString: string = "",
dotCount: number = 0;
const startPosition = this.position.copy();
while (this.currentChar && "0123456789.e".includes(this.currentChar)) {
if (this.currentChar === "." && !"0123456789".includes(this.previousChar || "")) {
dotCount++;
numberString += ".";
} else if (this.currentChar === "." && "0123456789".includes(this.previousChar || "")) {
if (dotCount) {
this.advance();
return this.makeIPv4(numberString, startPosition);
}
dotCount++;
numberString += ".";
} else if (this.currentChar === "e" && !("123456789".includes(this.previousChar || ""))) {
throw new NaNError(startPosition, this.position);
} else {
numberString += this.currentChar;
}
this.advance();
}
if (!(+numberString && !Number.isNaN(+numberString))) {
throw new NaNError(startPosition, this.position);
}
return `${tokens.number}:${+numberString}`;
}
public makeOperators() {
if (this.currentChar === "(") {
return "OPERATOR:LPAREN";
} else if (this.currentChar === ")") {
return "OPERATOR:RPAREN";
}
if (this.madeTokens[this.madeTokens.length - 1] !== "OPERATOR:RPAREN" && this.madeTokens[this.madeTokens.length - 1].startsWith("OPERATOR:")) {
const errorPosition = this.position.copy().advance("");
throw new InvalidOperatorError(errorPosition, errorPosition);
}
const startPosition = this.position.copy().advance("");
const nextChar = this.text[this.position.index + 1] || "";
console.log(this.currentChar + nextChar);
const
operators: { [operator: string]: string } = {
"%": "MOD",
"&&": "AND",
"(": "LPAREN",
")": "RPAREN",
"*": "MUL",
"**": "POW",
"+": "PLUS",
"-": "MINUS",
"/": "DIV",
";": "SEMI",
"||": "NOT"
},
reference = ((this.currentChar || "") + (/[\t %&()*+/;^|-]/.test(nextChar) ? (nextChar) : "")).trim();
this.advance();
return reference in operators ? "OPERATOR:" + operators[reference] : (() => {
throw new InvalidOperatorError(startPosition, this.position);
})();
}
public makeTokens() {
while (this.currentChar) {
if (" \t".includes(this.currentChar)) {
this.advance();
} else if (/\d/.test(this.currentChar || "") || ("0123456789".includes(this.previousChar || "") && this.currentChar === "e") || (this.currentChar === "." && "0123456789".includes(this.previousChar || ""))) {
this.madeTokens.push(this.makeNumbers());
} else if (/[%&()*+/;^|-]/.test(this.currentChar)) {
this.madeTokens.push(this.makeOperators());
this.advance();
} else {
this.advance();
throw new InvalidTokenError(this.currentChar, this.position, this.position);
}
}
return this.madeTokens;
}
}