This repository was archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenize.js
183 lines (182 loc) · 5.54 KB
/
tokenize.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const symbolMap = {
"+": "$plus",
"-": "$minus",
"*": "$star",
"/": "$slash",
"^": "$carot",
"?": "$question",
"=": "$eq",
"<": "$lt",
">": "$gt",
"\\": "$backslash",
"&": "$and",
"|": "$or",
"%": "$percent",
"'": "$quote",
"!": "$exclam"
};
const symbolRegExps = Object.keys(symbolMap).map(x => {
const res = new RegExp(`\\${x}`, "g");
res.str = symbolMap[x];
return res;
});
//@ts-check
const unicodeEscapementRegExp = /\\u\{([0-9A-F]{4,6})\}/g;
const newLineRegExp = /\n|\r\n?/;
/*
Capturing Group:
[1] Whitespace
[2] Comment
[3] String
[4] Keyword
[5] Name
[6] Number
[7] Punctuator
*/
const tokenRegExp = /(\u0020+|\t+)|(#.*)|("(?:[^"\\]|\\(?:[nr"\\]|u\{[0-9A-F]{4,6}\}))*")|\b(let|loop|if|else|func|break|import|export|match|return|def|try|on|settle|raise|importstd|meta|enter|exit|operator|hoist)\b|([A-Za-z_+\-/*%&|?^=<>'!][A-Za-z_0-9+\-/*%&|?^<>='!]*)|((?:0[box])?-?\d[\d_]*(?:\.[\d_]+)?(?:e\-?[\d_]+)?[a-z]*)|(\.{3}|[$(),.{}\[\]:])/y;
/**
* Create a token generator for a specific source.
* @param {string | Array<string>} source If string is provided, string is split along newlines. If array is provided, array is used as the array of lines.
* @param {boolean} comment Should comments be tokenized and returned from the generator?
* @returns {() => void | {id: string, lineNumber: number, columnNumber: number, string ?: string, columnTo ?: number, readonly ?: boolean, alphanumeric ?: boolean, number ?: number}} A function that generates the next token.
*/
function tokenize(source, comment = false) {
source = source.replace(new RegExp("/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/", "g"), "");
const lines = (
Array.isArray(source) ?
source :
source.split(newLineRegExp)
);
let lineNumber = 0;
let dotLast = false;
let line = lines[0];
tokenRegExp.lastIndex = 0;
return function tokenGenerator() {
if (line === undefined) {
return;
}
let columnNumber = tokenRegExp.lastIndex;
if (columnNumber >= line.length) {
tokenRegExp.lastIndex = 0;
lineNumber += 1;
line = lines[lineNumber];
return (
line === undefined ?
undefined :
tokenGenerator()
);
}
let captives = tokenRegExp.exec(line);
if (!captives) {
let res = {
id: "(error)",
lineNumber,
columnNumber,
columnTo: columnNumber,
string: line.slice(columnNumber)
}
line = undefined;
return res;
}
// Whitespace matched
if (captives[1]) {
dotLast = false;
return tokenGenerator();
}
const columnTo = tokenRegExp.lastIndex;
// Comment matched
if (captives[2]) {
dotLast = false;
return (
comment ? {
id: "(comment)",
comment: captives[2],
lineNumber,
columnNumber,
columnTo
} :
tokenGenerator()
)
}
// String Matched
if (captives[3]) {
dotLast = false;
return {
id: "(string)",
readonly: true,
string: JSON.parse(captives[3].replace(
unicodeEscapementRegExp,
function(ignore, code) {
//@ts-ignore
return String.fromCodePoint(parseInt(code, 16));
}
)),
lineNumber,
columnNumber,
columnTo
};
}
// Keyword Matched
if (captives[4]) {
dotLast = false;
return {
id: "(keyword)",
string: captives[4],
lineNumber,
columnNumber,
columnTo
};
}
// Name Matched
if (captives[5]) {
let res = captives[5];
if (/\-\d+/.test(res)) {
return {
id: "(number)",
readonly: true,
number: Number(res.replace(/(?:[^\d])([^0])[a-z]+/g, "$1").replace(/[a-z]$/, "").replace(/_/g, "")),
string: res,
lineNumber,
columnNumber,
columnTo
};
}
dotLast = false;
return {
id: res,
alphanumeric: true,
lineNumber,
columnNumber,
columnTo
};
}
// Number Matched
if (captives[6]) {
dotLast = false;
return {
id: "(number)",
readonly: true,
number: Number(captives[6].replace(/(?:[^\d])([^0])[a-z]+/g, "$1").replace(/[a-z]$/, "").replace(/_/g, "")),
string: captives[6],
lineNumber,
columnNumber,
columnTo
};
}
// Punctuator Matched
if (captives[7]) {
if (captives[7] === ".") {
dotLast = true;
} else {
dotLast = false;
}
return {
id: captives[7],
lineNumber,
columnNumber,
columnTo
}
}
}
}
export default Object.freeze(tokenize);