ranuts 0.1.0-alpha.1 → 0.1.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/assets/img/sort/bubble.gif +0 -0
- package/assets/img/sort/complexity.png +0 -0
- package/assets/img/sort/select.gif +0 -0
- package/assets/img/sort/sort.png +0 -0
- package/assets/img/tree/balanceTree.png +0 -0
- package/dist/index.js.map +0 -1
- package/dist/index.umd.cjs.map +0 -1
- package/dist/src/file/fileInfo.d.ts +0 -7
- package/dist/src/file/fs.d.ts +0 -2
- package/dist/src/file/readDir.d.ts +0 -6
- package/dist/src/file/readFile.d.ts +0 -8
- package/dist/src/file/watchFile.d.ts +0 -8
- package/dist/src/file/writeFile.d.ts +0 -8
- package/dist/src/mode/subscribe.d.ts +0 -16
- package/dist/src/ranlog/behavior.d.ts +0 -1
- package/dist/src/ranlog/env.d.ts +0 -5
- package/dist/src/ranlog/error.d.ts +0 -2
- package/dist/src/ranlog/index.d.ts +0 -26
- package/dist/src/ranlog/performance.d.ts +0 -21
- package/dist/src/ranlog/report.d.ts +0 -7
- package/dist/src/ranlog/request.d.ts +0 -18
- package/dist/src/ranlog/utils.d.ts +0 -19
- package/dist/src/utils/filterObj.d.ts +0 -8
- package/dist/src/utils/str2Xml.d.ts +0 -8
- package/dist/src/vnode/h.d.ts +0 -6
- package/dist/src/vnode/hooks.d.ts +0 -23
- package/dist/src/vnode/htmlDomApi.d.ts +0 -33
- package/dist/src/vnode/init.d.ts +0 -2
- package/dist/src/vnode/is.d.ts +0 -5
- package/dist/src/vnode/modules/attributes.d.ts +0 -8
- package/dist/src/vnode/modules/class.d.ts +0 -8
- package/dist/src/vnode/modules/index.d.ts +0 -7
- package/dist/src/vnode/modules/listeners.d.ts +0 -14
- package/dist/src/vnode/modules/props.d.ts +0 -8
- package/dist/src/vnode/modules/style.d.ts +0 -14
- package/dist/src/vnode/vnode.d.ts +0 -31
- package/src/astParser/Parser.ts +0 -654
- package/src/astParser/Tokenizer.ts +0 -447
- package/src/astParser/nodeTypes.ts +0 -194
- package/src/astParser/utils.ts +0 -27
- package/src/babel/parser.ts +0 -10
- package/src/colors/fmt.ts +0 -29
- package/src/colors/isColorSupported.ts +0 -13
- package/src/file/appendFile.ts +0 -29
- package/src/file/fileInfo.ts +0 -22
- package/src/file/fs.ts +0 -9
- package/src/file/readDir.ts +0 -18
- package/src/file/readFile.ts +0 -26
- package/src/file/watchFile.ts +0 -31
- package/src/file/writeFile.ts +0 -38
- package/src/mode/subscribe.ts +0 -89
- package/src/ran/commit.ts +0 -0
- package/src/ran/dom.ts +0 -0
- package/src/ran/hooks.ts +0 -0
- package/src/ran/reconcile.ts +0 -0
- package/src/ran/schedule.ts +0 -0
- package/src/ranlog/behavior.ts +0 -5
- package/src/ranlog/console.ts +0 -16
- package/src/ranlog/env.ts +0 -29
- package/src/ranlog/error.ts +0 -11
- package/src/ranlog/performance.ts +0 -65
- package/src/ranlog/report.ts +0 -33
- package/src/ranlog/request.ts +0 -60
- package/src/ranlog/utils.ts +0 -92
- package/src/ranpack/ast/Declaration.ts +0 -120
- package/src/ranpack/ast/Node.ts +0 -7
- package/src/ranpack/ast/Reference.ts +0 -32
- package/src/ranpack/ast/Scope.ts +0 -80
- package/src/ranpack/bundle.ts +0 -50
- package/src/ranpack/graph.ts +0 -143
- package/src/ranpack/module.ts +0 -431
- package/src/ranpack/moduleLoader.ts +0 -73
- package/src/ranpack/plugins.ts +0 -70
- package/src/ranpack/statement.ts +0 -66
- package/src/ranpack/utils/buildScope.ts +0 -77
- package/src/ranpack/utils/findReference.ts +0 -36
- package/src/ranpack/utils/isFunctionDeclaration.ts +0 -41
- package/src/ranpack/utils/makeLegalIdentifier.ts +0 -18
- package/src/ranpack/utils/object.ts +0 -8
- package/src/ranpack/utils/resolve.ts +0 -32
- package/src/ranpack/utils/walk.ts +0 -66
- package/src/ranpack/ws.ts +0 -269
- package/src/server/connectType.ts +0 -9
- package/src/server/encodeUrl.ts +0 -46
- package/src/server/escapeHtml.ts +0 -46
- package/src/server/get.ts +0 -36
- package/src/server/jitter.ts +0 -77
- package/src/server/mimeType.ts +0 -858
- package/src/server/paresUrl.ts +0 -40
- package/src/server/send.ts +0 -89
- package/src/server/server.ts +0 -67
- package/src/server/status.ts +0 -191
- package/src/server/traverse.ts +0 -54
- package/src/server/websocket.ts +0 -200
- package/src/sort/bubble.ts +0 -21
- package/src/sort/bucket.ts +0 -11
- package/src/sort/count.ts +0 -10
- package/src/sort/heap.ts +0 -10
- package/src/sort/insert.ts +0 -20
- package/src/sort/merge.ts +0 -35
- package/src/sort/quick.ts +0 -50
- package/src/sort/radix.ts +0 -11
- package/src/sort/randomArray.ts +0 -21
- package/src/sort/select.ts +0 -23
- package/src/sort/shell.ts +0 -22
- package/src/utils/compose.ts +0 -37
- package/src/utils/filterObj.ts +0 -21
- package/src/utils/mergeObj.ts +0 -12
- package/src/utils/startTask.ts +0 -15
- package/src/utils/str2Xml.ts +0 -26
- package/src/utils/taskEnd.ts +0 -14
- package/src/vnode/h.ts +0 -108
- package/src/vnode/hooks.ts +0 -25
- package/src/vnode/htmlDomApi.ts +0 -199
- package/src/vnode/init.ts +0 -429
- package/src/vnode/is.ts +0 -17
- package/src/vnode/modules/attributes.ts +0 -70
- package/src/vnode/modules/class.ts +0 -46
- package/src/vnode/modules/listeners.ts +0 -123
- package/src/vnode/modules/props.ts +0 -41
- package/src/vnode/modules/style.ts +0 -118
- package/src/vnode/vnode.ts +0 -65
|
@@ -1,447 +0,0 @@
|
|
|
1
|
-
import { isAlpha, isDigit, isUnderline, isWhiteSpace } from './utils'
|
|
2
|
-
|
|
3
|
-
// 词法分析器,将代码划分为一个个词法单元,便于进行后续的语法分析
|
|
4
|
-
// 本质上是对代码字符串进行逐个字符的扫描,然后根据一定的语法规则进行分组。
|
|
5
|
-
/**
|
|
6
|
-
* @description: 声明一些必要的类型
|
|
7
|
-
*/
|
|
8
|
-
export enum TokenType {
|
|
9
|
-
Let = 'Let',
|
|
10
|
-
Const = 'Const',
|
|
11
|
-
Var = 'Var',
|
|
12
|
-
Assign = 'Assign',
|
|
13
|
-
Function = 'Function',
|
|
14
|
-
Class = 'Class',
|
|
15
|
-
Number = 'Number',
|
|
16
|
-
Operator = 'Operator',
|
|
17
|
-
Identifier = 'Identifier',
|
|
18
|
-
LeftParen = 'LeftParen',
|
|
19
|
-
RightParen = 'RightParen',
|
|
20
|
-
LeftCurly = 'LeftCurly',
|
|
21
|
-
RightCurly = 'RightCurly',
|
|
22
|
-
Comma = 'Comma',
|
|
23
|
-
Dot = 'Dot',
|
|
24
|
-
Semicolon = 'Semicolon',
|
|
25
|
-
StringLiteral = 'StringLiteral',
|
|
26
|
-
Return = 'Return',
|
|
27
|
-
Import = 'Import',
|
|
28
|
-
Export = 'Export',
|
|
29
|
-
Default = 'Default',
|
|
30
|
-
From = 'From',
|
|
31
|
-
As = 'As',
|
|
32
|
-
Asterisk = 'Asterisk',
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// 扫描模式
|
|
36
|
-
export enum ScanMode {
|
|
37
|
-
Normal,
|
|
38
|
-
Identifier, // 扫描标识符,单词
|
|
39
|
-
StringLiteral,
|
|
40
|
-
Number,
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// 词法分析,token的类型,值,开始位置,结束位置,文本
|
|
44
|
-
export type Token = {
|
|
45
|
-
type: TokenType
|
|
46
|
-
value?: string
|
|
47
|
-
start: number
|
|
48
|
-
end: number
|
|
49
|
-
raw?: string
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// 策略模式
|
|
53
|
-
// Token 的生成器对象,关键词的映射
|
|
54
|
-
const TOKENS_GENERATOR: Record<string, (...args: any[]) => Token> = {
|
|
55
|
-
let(start: number) {
|
|
56
|
-
return { type: TokenType.Let, value: 'let', start, end: start + 3 }
|
|
57
|
-
},
|
|
58
|
-
const(start: number) {
|
|
59
|
-
return { type: TokenType.Const, value: 'const', start, end: start + 5 }
|
|
60
|
-
},
|
|
61
|
-
var(start: number) {
|
|
62
|
-
return { type: TokenType.Var, value: 'var', start, end: start + 3 }
|
|
63
|
-
},
|
|
64
|
-
assign(start: number) {
|
|
65
|
-
return { type: TokenType.Assign, value: '=', start, end: start + 1 }
|
|
66
|
-
},
|
|
67
|
-
import(start: number) {
|
|
68
|
-
return {
|
|
69
|
-
type: TokenType.Import,
|
|
70
|
-
value: 'import',
|
|
71
|
-
start,
|
|
72
|
-
end: start + 6,
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
export(start: number) {
|
|
76
|
-
return {
|
|
77
|
-
type: TokenType.Export,
|
|
78
|
-
value: 'export',
|
|
79
|
-
start,
|
|
80
|
-
end: start + 6,
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
from(start: number) {
|
|
84
|
-
return {
|
|
85
|
-
type: TokenType.From,
|
|
86
|
-
value: 'from',
|
|
87
|
-
start,
|
|
88
|
-
end: start + 4,
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
as(start: number) {
|
|
92
|
-
return {
|
|
93
|
-
type: TokenType.As,
|
|
94
|
-
value: 'as',
|
|
95
|
-
start,
|
|
96
|
-
end: start + 2,
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
asterisk(start: number) {
|
|
100
|
-
return {
|
|
101
|
-
type: TokenType.Asterisk,
|
|
102
|
-
value: '*',
|
|
103
|
-
start,
|
|
104
|
-
end: start + 1,
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
default(start: number) {
|
|
108
|
-
return {
|
|
109
|
-
type: TokenType.Default,
|
|
110
|
-
value: 'default',
|
|
111
|
-
start,
|
|
112
|
-
end: start + 7,
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
number(start: number, value: string) {
|
|
116
|
-
return {
|
|
117
|
-
type: TokenType.Number,
|
|
118
|
-
value,
|
|
119
|
-
start,
|
|
120
|
-
end: start + value.length,
|
|
121
|
-
raw: value,
|
|
122
|
-
}
|
|
123
|
-
},
|
|
124
|
-
class(start: number) {
|
|
125
|
-
return {
|
|
126
|
-
type: TokenType.Class,
|
|
127
|
-
value: 'class',
|
|
128
|
-
start,
|
|
129
|
-
end: start + 5,
|
|
130
|
-
}
|
|
131
|
-
},
|
|
132
|
-
function(start: number) {
|
|
133
|
-
return {
|
|
134
|
-
type: TokenType.Function,
|
|
135
|
-
value: 'function',
|
|
136
|
-
start,
|
|
137
|
-
end: start + 8,
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
return(start: number) {
|
|
141
|
-
return {
|
|
142
|
-
type: TokenType.Return,
|
|
143
|
-
value: 'return',
|
|
144
|
-
start,
|
|
145
|
-
end: start + 6,
|
|
146
|
-
}
|
|
147
|
-
},
|
|
148
|
-
operator(start: number, value: string) {
|
|
149
|
-
return {
|
|
150
|
-
type: TokenType.Operator,
|
|
151
|
-
value,
|
|
152
|
-
start,
|
|
153
|
-
end: start + value.length,
|
|
154
|
-
}
|
|
155
|
-
},
|
|
156
|
-
comma(start: number) {
|
|
157
|
-
return {
|
|
158
|
-
type: TokenType.Comma,
|
|
159
|
-
value: ',',
|
|
160
|
-
start,
|
|
161
|
-
end: start + 1,
|
|
162
|
-
}
|
|
163
|
-
},
|
|
164
|
-
leftParen(start: number) {
|
|
165
|
-
return { type: TokenType.LeftParen, value: '(', start, end: start + 1 }
|
|
166
|
-
},
|
|
167
|
-
rightParen(start: number) {
|
|
168
|
-
return { type: TokenType.RightParen, value: ')', start, end: start + 1 }
|
|
169
|
-
},
|
|
170
|
-
leftCurly(start: number) {
|
|
171
|
-
return { type: TokenType.LeftCurly, value: '{', start, end: start + 1 }
|
|
172
|
-
},
|
|
173
|
-
rightCurly(start: number) {
|
|
174
|
-
return { type: TokenType.RightCurly, value: '}', start, end: start + 1 }
|
|
175
|
-
},
|
|
176
|
-
dot(start: number) {
|
|
177
|
-
return { type: TokenType.Dot, value: '.', start, end: start + 1 }
|
|
178
|
-
},
|
|
179
|
-
semicolon(start: number) {
|
|
180
|
-
return { type: TokenType.Semicolon, value: ';', start, end: start + 1 }
|
|
181
|
-
},
|
|
182
|
-
stringLiteral(start: number, value: string, raw: string) {
|
|
183
|
-
return {
|
|
184
|
-
type: TokenType.StringLiteral,
|
|
185
|
-
value,
|
|
186
|
-
start,
|
|
187
|
-
end: start + value.length + 2,
|
|
188
|
-
raw,
|
|
189
|
-
}
|
|
190
|
-
},
|
|
191
|
-
identifier(start: number, value: string) {
|
|
192
|
-
return {
|
|
193
|
-
type: TokenType.Identifier,
|
|
194
|
-
value,
|
|
195
|
-
start,
|
|
196
|
-
end: start + value.length,
|
|
197
|
-
}
|
|
198
|
-
},
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// 单字符token
|
|
202
|
-
type SingleCharTokens = '(' | ')' | '{' | '}' | '.' | ';' | ',' | '*' | '='
|
|
203
|
-
|
|
204
|
-
// 单字符到 Token 生成器的映射
|
|
205
|
-
const KNOWN_SINGLE_CHAR_TOKENS = new Map<
|
|
206
|
-
SingleCharTokens,
|
|
207
|
-
typeof TOKENS_GENERATOR[keyof typeof TOKENS_GENERATOR]
|
|
208
|
-
>([
|
|
209
|
-
['(', TOKENS_GENERATOR.leftParen],
|
|
210
|
-
[')', TOKENS_GENERATOR.rightParen],
|
|
211
|
-
['{', TOKENS_GENERATOR.leftCurly],
|
|
212
|
-
['}', TOKENS_GENERATOR.rightCurly],
|
|
213
|
-
['.', TOKENS_GENERATOR.dot],
|
|
214
|
-
[';', TOKENS_GENERATOR.semicolon],
|
|
215
|
-
[',', TOKENS_GENERATOR.comma],
|
|
216
|
-
['*', TOKENS_GENERATOR.asterisk],
|
|
217
|
-
['=', TOKENS_GENERATOR.assign],
|
|
218
|
-
])
|
|
219
|
-
// 引号token
|
|
220
|
-
const QUOTATION_TOKENS = ["'", '"', '`']
|
|
221
|
-
// 操作符token
|
|
222
|
-
const OPERATOR_TOKENS = [
|
|
223
|
-
'+',
|
|
224
|
-
'-',
|
|
225
|
-
'*',
|
|
226
|
-
'/',
|
|
227
|
-
'%',
|
|
228
|
-
'^',
|
|
229
|
-
'&',
|
|
230
|
-
'|',
|
|
231
|
-
'~',
|
|
232
|
-
'<<',
|
|
233
|
-
'>>',
|
|
234
|
-
]
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* @description: 词法分析器,分词器
|
|
238
|
-
* 在扫描字符的过程,我们需要对不同的字符各自进行不同的处理,具体的策略如下:
|
|
239
|
-
* 1. 当前字符为分隔符,如空格,直接跳过,不处理;
|
|
240
|
-
* 2. 当前字符为字母,需要继续扫描,获取完整的单词:
|
|
241
|
-
* 如果单词为语法关键字,则新建相应关键字的 Token
|
|
242
|
-
* 否则视为普通的变量名
|
|
243
|
-
* 3. 当前字符为单字符,如{、}、(、),则新建单字符对应的 Token
|
|
244
|
-
*/
|
|
245
|
-
export class Tokenizer {
|
|
246
|
-
private _tokens: Token[] = [] // 最终返回的结果,tokens数组
|
|
247
|
-
private _currentIndex: number = 0 // 扫描当前代码片段的位置
|
|
248
|
-
private _source: string // 当前传入的代码片段
|
|
249
|
-
private _scanMode = ScanMode.Normal // 扫描模式,对不同的类型做不同的操作
|
|
250
|
-
/**
|
|
251
|
-
* @description: 参数是代码片段
|
|
252
|
-
* @param {string} input
|
|
253
|
-
*/
|
|
254
|
-
constructor(input: string) {
|
|
255
|
-
this._source = input // 获取源代码
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* @description: 主程序,扫描字符串生成 token
|
|
259
|
-
*/
|
|
260
|
-
tokenize(): Token[] {
|
|
261
|
-
// 扫描
|
|
262
|
-
while (this._currentIndex < this._source.length) {
|
|
263
|
-
const currentChar = this._source[this._currentIndex]
|
|
264
|
-
const startIndex = this._currentIndex
|
|
265
|
-
// 1. 判断是否是分隔符
|
|
266
|
-
if (isWhiteSpace(currentChar)) {
|
|
267
|
-
this._currentIndex++
|
|
268
|
-
continue
|
|
269
|
-
}
|
|
270
|
-
// 2. 判断是否是字母
|
|
271
|
-
else if (isAlpha(currentChar)) {
|
|
272
|
-
// 扫描标识符
|
|
273
|
-
this.scanIdentifier()
|
|
274
|
-
continue
|
|
275
|
-
}
|
|
276
|
-
// 3. 判断是否是单字符 () {} . ; *
|
|
277
|
-
else if (KNOWN_SINGLE_CHAR_TOKENS.has(currentChar as SingleCharTokens)) {
|
|
278
|
-
// * 字符特殊处理
|
|
279
|
-
if (currentChar === '*') {
|
|
280
|
-
// 前瞻,如果是非 import/export,则认为是二元运算符,避免误判
|
|
281
|
-
const previousToken = this._getPreviousToken()
|
|
282
|
-
if (previousToken.type !== TokenType.Import && previousToken.type !== TokenType.Export) {
|
|
283
|
-
this._tokens.push(TOKENS_GENERATOR.operator(startIndex, currentChar))
|
|
284
|
-
this._currentIndex++
|
|
285
|
-
continue
|
|
286
|
-
}
|
|
287
|
-
// 否则按照 import/export 中的 * 处理
|
|
288
|
-
}
|
|
289
|
-
const token = KNOWN_SINGLE_CHAR_TOKENS.get(currentChar as SingleCharTokens)!(startIndex)
|
|
290
|
-
this._tokens.push(token)
|
|
291
|
-
this._currentIndex++
|
|
292
|
-
}
|
|
293
|
-
// 4. 判断是否为引号
|
|
294
|
-
else if (QUOTATION_TOKENS.includes(currentChar)) {
|
|
295
|
-
// 如果是引号,就去扫描字符串变量
|
|
296
|
-
this.scanStringLiteral()
|
|
297
|
-
// 跳过结尾的引号
|
|
298
|
-
this._currentIndex++
|
|
299
|
-
continue
|
|
300
|
-
}
|
|
301
|
-
// 5. 判断二元计算符
|
|
302
|
-
else if (OPERATOR_TOKENS.includes(currentChar) && this._scanMode === ScanMode.Normal) {
|
|
303
|
-
this._tokens.push(TOKENS_GENERATOR.operator(startIndex, currentChar))
|
|
304
|
-
this._currentIndex++
|
|
305
|
-
continue
|
|
306
|
-
} else if (OPERATOR_TOKENS.includes(currentChar + this._getNextChar()) && this._scanMode === ScanMode.Normal) {
|
|
307
|
-
this._tokens.push(TOKENS_GENERATOR.operator(startIndex, currentChar + this._getNextChar()))
|
|
308
|
-
this._currentIndex += 2
|
|
309
|
-
continue
|
|
310
|
-
}
|
|
311
|
-
// 6. 判断数字
|
|
312
|
-
else if (isDigit(currentChar)) {
|
|
313
|
-
this._scanNumber()
|
|
314
|
-
continue
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
this._resetCurrentIndex()
|
|
318
|
-
// 返回结果 token 数组
|
|
319
|
-
return this._getTokens()
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* @description: 设置扫描的模式
|
|
323
|
-
* @param {ScanMode} mode
|
|
324
|
-
*/
|
|
325
|
-
private _setScanMode(mode: ScanMode) {
|
|
326
|
-
this._scanMode = mode
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* @description: 将扫描模式重置成普通模式
|
|
330
|
-
*/
|
|
331
|
-
private _resetScanMode() {
|
|
332
|
-
this._scanMode = ScanMode.Normal
|
|
333
|
-
}
|
|
334
|
-
// 扫描标识符
|
|
335
|
-
scanIdentifier(): void {
|
|
336
|
-
this._setScanMode(ScanMode.Identifier)
|
|
337
|
-
// 继续扫描,直到收集完整的单词
|
|
338
|
-
let identifier = ''
|
|
339
|
-
let currentChar = this._getCurrentChar()
|
|
340
|
-
const startIndex = this._currentIndex
|
|
341
|
-
// 如果是字母,数字,下划线,收集成字符
|
|
342
|
-
while (
|
|
343
|
-
isAlpha(currentChar) ||
|
|
344
|
-
isDigit(currentChar) ||
|
|
345
|
-
isUnderline(currentChar)
|
|
346
|
-
) {
|
|
347
|
-
identifier += currentChar
|
|
348
|
-
this._currentIndex++
|
|
349
|
-
currentChar = this._getCurrentChar()
|
|
350
|
-
}
|
|
351
|
-
let token
|
|
352
|
-
// 1. 结果为关键字
|
|
353
|
-
if (identifier in TOKENS_GENERATOR) {
|
|
354
|
-
token = TOKENS_GENERATOR[identifier as keyof typeof TOKENS_GENERATOR](startIndex)
|
|
355
|
-
}
|
|
356
|
-
// 2. 结果为标识符
|
|
357
|
-
else {
|
|
358
|
-
token = TOKENS_GENERATOR['identifier'](startIndex, identifier)
|
|
359
|
-
}
|
|
360
|
-
// 词法分析加入this._tokens
|
|
361
|
-
this._tokens.push(token)
|
|
362
|
-
this._resetScanMode()
|
|
363
|
-
}
|
|
364
|
-
// 扫描字符串变量
|
|
365
|
-
scanStringLiteral(): void {
|
|
366
|
-
this._setScanMode(ScanMode.StringLiteral)
|
|
367
|
-
const startIndex = this._currentIndex
|
|
368
|
-
let currentChar = this._getCurrentChar()
|
|
369
|
-
// 记录引号
|
|
370
|
-
const startQuotation = currentChar
|
|
371
|
-
// 继续找字符串
|
|
372
|
-
this._currentIndex++
|
|
373
|
-
let str = ''
|
|
374
|
-
currentChar = this._getCurrentChar()
|
|
375
|
-
while (currentChar && currentChar !== startQuotation) {
|
|
376
|
-
str += currentChar
|
|
377
|
-
this._currentIndex++
|
|
378
|
-
currentChar = this._getCurrentChar()
|
|
379
|
-
}
|
|
380
|
-
const token = TOKENS_GENERATOR.stringLiteral(
|
|
381
|
-
startIndex,
|
|
382
|
-
str,
|
|
383
|
-
`${startQuotation}${str}${startQuotation}`,
|
|
384
|
-
)
|
|
385
|
-
// 词法分析加入this._tokens
|
|
386
|
-
this._tokens.push(token)
|
|
387
|
-
this._resetScanMode()
|
|
388
|
-
}
|
|
389
|
-
// 扫描数字
|
|
390
|
-
_scanNumber(): void {
|
|
391
|
-
this._setScanMode(ScanMode.Number)
|
|
392
|
-
const startIndex = this._currentIndex
|
|
393
|
-
let number = ''
|
|
394
|
-
let currentChar = this._getCurrentChar()
|
|
395
|
-
let isFloat = false
|
|
396
|
-
// 如果是数字,则继续扫描
|
|
397
|
-
// 需要考虑到小数点
|
|
398
|
-
while (isDigit(currentChar) || (currentChar === '.' && !isFloat)) {
|
|
399
|
-
if (currentChar === '.') {
|
|
400
|
-
isFloat = true
|
|
401
|
-
}
|
|
402
|
-
number += currentChar
|
|
403
|
-
this._currentIndex++
|
|
404
|
-
currentChar = this._getCurrentChar()
|
|
405
|
-
}
|
|
406
|
-
if (isFloat && currentChar === '.') {
|
|
407
|
-
throw new Error('Unexpected character "."')
|
|
408
|
-
}
|
|
409
|
-
const token = TOKENS_GENERATOR.number(startIndex, number)
|
|
410
|
-
// 词法分析加入this._tokens
|
|
411
|
-
this._tokens.push(token)
|
|
412
|
-
this._resetScanMode()
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* @description: 返回当前的字符
|
|
416
|
-
* @return {string}
|
|
417
|
-
*/
|
|
418
|
-
private _getCurrentChar() {
|
|
419
|
-
return this._source[this._currentIndex]
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
private _getNextChar() {
|
|
423
|
-
if (this._currentIndex + 1 < this._source.length) {
|
|
424
|
-
return this._source[this._currentIndex + 1]
|
|
425
|
-
}
|
|
426
|
-
return ''
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
private _resetCurrentIndex() {
|
|
430
|
-
this._currentIndex = 0
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
private _getTokens() {
|
|
434
|
-
return this._tokens
|
|
435
|
-
}
|
|
436
|
-
/**
|
|
437
|
-
* @description: 返回最后一个 Token
|
|
438
|
-
* @return {Token}
|
|
439
|
-
*/
|
|
440
|
-
private _getPreviousToken() {
|
|
441
|
-
// 前瞻 Token
|
|
442
|
-
if (this._tokens.length > 0) {
|
|
443
|
-
return this._tokens[this._tokens.length - 1]
|
|
444
|
-
}
|
|
445
|
-
throw new Error('Previous token not found')
|
|
446
|
-
}
|
|
447
|
-
}
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
import type { Scope } from '@/ranpack/ast/Scope';
|
|
2
|
-
|
|
3
|
-
export enum NodeType {
|
|
4
|
-
Program = 'Program',
|
|
5
|
-
VariableDeclaration = 'VariableDeclaration',
|
|
6
|
-
FunctionDeclaration = 'FunctionDeclaration',
|
|
7
|
-
Identifier = 'Identifier',
|
|
8
|
-
BlockStatement = 'BlockStatement',
|
|
9
|
-
ExpressionStatement = 'ExpressionStatement',
|
|
10
|
-
ReturnStatement = 'ReturnStatement',
|
|
11
|
-
CallExpression = 'CallExpression',
|
|
12
|
-
BinaryExpression = 'BinaryExpression',
|
|
13
|
-
MemberExpression = 'MemberExpression',
|
|
14
|
-
FunctionExpression = 'FunctionExpression',
|
|
15
|
-
Literal = 'Literal',
|
|
16
|
-
ImportDeclaration = 'ImportDeclaration',
|
|
17
|
-
ImportSpecifier = 'ImportSpecifier', // import {c, d} from 'c';
|
|
18
|
-
ImportDefaultSpecifier = 'ImportDefaultSpecifier', // import a from 'a';
|
|
19
|
-
ImportNamespaceSpecifier = 'ImportNamespaceSpecifier', // import * as b from 'b';
|
|
20
|
-
ExportDeclaration = 'ExportDeclaration',
|
|
21
|
-
ExportSpecifier = 'ExportSpecifier',
|
|
22
|
-
ExportDefaultDeclaration = 'ExportDefaultDeclaration',
|
|
23
|
-
ExportNamedDeclaration = 'ExportNamedDeclaration',
|
|
24
|
-
ExportAllDeclaration = 'ExportAllDeclaration',
|
|
25
|
-
VariableDeclarator = 'VariableDeclarator',
|
|
26
|
-
}
|
|
27
|
-
// 声明语句
|
|
28
|
-
export type Statement =
|
|
29
|
-
| ImportDeclaration
|
|
30
|
-
| ExportDeclaration
|
|
31
|
-
| VariableDeclaration
|
|
32
|
-
| FunctionDeclaration
|
|
33
|
-
| ExpressionStatement
|
|
34
|
-
| BlockStatement
|
|
35
|
-
| ReturnStatement
|
|
36
|
-
// 表达式语句
|
|
37
|
-
export type Expression =
|
|
38
|
-
| CallExpression
|
|
39
|
-
| MemberExpression
|
|
40
|
-
| Identifier
|
|
41
|
-
| Literal
|
|
42
|
-
| BinaryExpression
|
|
43
|
-
| FunctionExpression
|
|
44
|
-
// 函数类型
|
|
45
|
-
export enum FunctionType {
|
|
46
|
-
FunctionDeclaration,
|
|
47
|
-
CallExpression,
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface Node {
|
|
51
|
-
type: string
|
|
52
|
-
start: number
|
|
53
|
-
end: number
|
|
54
|
-
_scope?: Scope
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export interface Program extends Node {
|
|
58
|
-
type: NodeType.Program
|
|
59
|
-
body: Statement[]
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export interface Literal extends Node {
|
|
63
|
-
type: NodeType.Literal
|
|
64
|
-
value: string
|
|
65
|
-
raw: string
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface Identifier extends Node {
|
|
69
|
-
type: NodeType.Identifier
|
|
70
|
-
name: string
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export interface CallExpression extends Node {
|
|
74
|
-
type: NodeType.CallExpression
|
|
75
|
-
callee: Expression
|
|
76
|
-
arguments: Expression[]
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export interface MemberExpression extends Node {
|
|
80
|
-
type: NodeType.MemberExpression
|
|
81
|
-
object: Identifier | MemberExpression
|
|
82
|
-
property: Identifier
|
|
83
|
-
computed: boolean
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export interface BlockStatement extends Node {
|
|
87
|
-
type: NodeType.BlockStatement
|
|
88
|
-
body: Statement[]
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export interface ExpressionStatement extends Node {
|
|
92
|
-
type: NodeType.ExpressionStatement
|
|
93
|
-
expression: Expression
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export interface FunctionExpression extends FunctionNode {
|
|
97
|
-
type: NodeType.FunctionExpression
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export interface FunctionDeclaration extends FunctionNode {
|
|
101
|
-
type: NodeType.FunctionDeclaration
|
|
102
|
-
id: Identifier | null
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export type VariableKind = 'var' | 'let' | 'const'
|
|
106
|
-
|
|
107
|
-
export interface VariableDeclarator extends Node {
|
|
108
|
-
type: NodeType.VariableDeclarator
|
|
109
|
-
id: Identifier
|
|
110
|
-
init: Expression | Literal | null
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export interface VariableDeclaration extends Node {
|
|
114
|
-
type: NodeType.VariableDeclaration
|
|
115
|
-
kind: 'var' | 'let' | 'const'
|
|
116
|
-
declarations: VariableDeclarator[]
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export interface ImportSpecifier extends Node {
|
|
120
|
-
type: NodeType.ImportSpecifier
|
|
121
|
-
imported: Identifier
|
|
122
|
-
local: Identifier
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export interface ImportDefaultSpecifier extends Node {
|
|
126
|
-
type: NodeType.ImportDefaultSpecifier
|
|
127
|
-
local: Identifier
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export interface ImportNamespaceSpecifier extends Node {
|
|
131
|
-
type: NodeType.ImportNamespaceSpecifier
|
|
132
|
-
local: Identifier
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export type ImportSpecifiers = Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>
|
|
136
|
-
|
|
137
|
-
export interface ImportDeclaration extends Node {
|
|
138
|
-
type: NodeType.ImportDeclaration
|
|
139
|
-
specifiers: ImportSpecifiers
|
|
140
|
-
source: Literal
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export type Declaration =
|
|
144
|
-
| FunctionDeclaration
|
|
145
|
-
| VariableDeclaration
|
|
146
|
-
| ImportDeclaration
|
|
147
|
-
| ExportDeclaration
|
|
148
|
-
| VariableDeclarator
|
|
149
|
-
|
|
150
|
-
export interface ExportSpecifier extends Node {
|
|
151
|
-
type: NodeType.ExportSpecifier
|
|
152
|
-
exported: Identifier
|
|
153
|
-
local: Identifier
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export interface ExportNamedDeclaration extends Node {
|
|
157
|
-
type: NodeType.ExportNamedDeclaration
|
|
158
|
-
declaration: Declaration | null
|
|
159
|
-
specifiers: ExportSpecifier[]
|
|
160
|
-
source: Literal | null
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export interface ExportDefaultDeclaration extends Node {
|
|
164
|
-
type: NodeType.ExportDefaultDeclaration
|
|
165
|
-
declaration: Declaration | Expression
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export interface ExportAllDeclaration extends Node {
|
|
169
|
-
type: NodeType.ExportAllDeclaration
|
|
170
|
-
source: Literal
|
|
171
|
-
exported: Identifier | null
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export type ExportDeclaration =
|
|
175
|
-
| ExportNamedDeclaration
|
|
176
|
-
| ExportDefaultDeclaration
|
|
177
|
-
| ExportAllDeclaration
|
|
178
|
-
|
|
179
|
-
export interface BinaryExpression extends Node {
|
|
180
|
-
type: NodeType.BinaryExpression
|
|
181
|
-
left: Expression
|
|
182
|
-
right: Expression
|
|
183
|
-
operator: string
|
|
184
|
-
}
|
|
185
|
-
export interface FunctionNode extends Node {
|
|
186
|
-
id: Identifier | null
|
|
187
|
-
params: Expression[] | Identifier[]
|
|
188
|
-
body: BlockStatement
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
export interface ReturnStatement extends Node {
|
|
192
|
-
type: NodeType.ReturnStatement
|
|
193
|
-
argument: Expression
|
|
194
|
-
}
|
package/src/astParser/utils.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
// 分隔符
|
|
2
|
-
export function isWhiteSpace(char: string): boolean {
|
|
3
|
-
return char === ' ' || char === '\t' || char === '\n' || char === '\r'
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
// 字母
|
|
7
|
-
export function isAlpha(char: string): boolean {
|
|
8
|
-
return (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z')
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// 数字
|
|
12
|
-
export function isDigit(char: string): boolean {
|
|
13
|
-
return char >= '0' && char <= '9'
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// 下划线
|
|
17
|
-
export function isUnderline(char: string): boolean {
|
|
18
|
-
return char === '_'
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* @description: 判断是否是下一行
|
|
22
|
-
* @param {number} code code = string.charCodeAt(index)
|
|
23
|
-
* @return {boolean}
|
|
24
|
-
*/
|
|
25
|
-
export function isNewLine(code:number):boolean {
|
|
26
|
-
return code === 10 || code === 13 || code === 0x2028 || code === 0x2029
|
|
27
|
-
}
|
package/src/babel/parser.ts
DELETED
package/src/colors/fmt.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
const FMT: Record<string, Array<string>> = {
|
|
2
|
-
bold: ['\x1b[1m', '\x1b[22m'],
|
|
3
|
-
dim: ['\x1b[2m', '\x1b[22m'],
|
|
4
|
-
reset: ['\x1b[0m', '\x1b[0m'],
|
|
5
|
-
italic: ['\x1b[3m', '\x1b[23m'],
|
|
6
|
-
underline: ['\x1b[4m', '\x1b[24m'],
|
|
7
|
-
inverse: ['\x1b[7m', '\x1b[27m'],
|
|
8
|
-
hidden: ['\x1b[8m', '\x1b[28m'],
|
|
9
|
-
strikethrough: ['\x1b[9m', '\x1b[29m'],
|
|
10
|
-
black: ['\x1b[30m', '\x1b[39m'],
|
|
11
|
-
red: ['\x1b[31m', '\x1b[39m'],
|
|
12
|
-
green: ['\x1b[32m', '\x1b[39m'],
|
|
13
|
-
yellow: ['\x1b[33m', '\x1b[39m'],
|
|
14
|
-
blue: ['\x1b[34m', '\x1b[39m'],
|
|
15
|
-
magenta: ['\x1b[35m', '\x1b[39m'],
|
|
16
|
-
cyan: ['\x1b[36m', '\x1b[39m'],
|
|
17
|
-
white: ['\x1b[37m', '\x1b[39m'],
|
|
18
|
-
gray: ['\x1b[90m', '\x1b[39m'],
|
|
19
|
-
bgBlack: ['\x1b[40m', '\x1b[49m'],
|
|
20
|
-
bgRed: ['\x1b[41m', '\x1b[49m'],
|
|
21
|
-
bgGreen: ['\x1b[42m', '\x1b[49m'],
|
|
22
|
-
bgYellow: ['\x1b[43m', '\x1b[49m'],
|
|
23
|
-
bgBlue: ['\x1b[44m', '\x1b[49m'],
|
|
24
|
-
bgMagenta: ['\x1b[45m', '\x1b[49m'],
|
|
25
|
-
bgCyan: ['\x1b[46m', '\x1b[49m'],
|
|
26
|
-
bgWhite: ['\x1b[47m', '\x1b[49m'],
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export default FMT
|