-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathneo.tokenize.js
161 lines (131 loc) · 4.27 KB
/
neo.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
// neo.tokenize.js
// Douglas Crockford
// 2018-09-24
// Public Domain
/*property
alphameric, column_nr, column_to, comment, exec, freeze, fromCodePoint, id,
isArray, lastIndex, length, line_nr, make, normalize, number, parse,
readonly, replace, slice, split, string, text
*/
import big_float from "./big_float.js";
const rx_unicode_escapement = /\\u\{([0-9A-F]{4,6})\}/g;
// 'rx_crfl' matches linefeed, carriage return, and carriage return linefeed.
// We are still messing with device codes for mid 20th Century electromechanical
// teletype machines.
const rx_crlf = /\n|\r\n?/;
// 'rx_token' matches a Neo token: comment, name, number, string, punctuator.
const rx_token = /(\u0020+)|(#.*)|([a-zA-Z](?:\u0020[a-zA-Z]|[0-9a-zA-Z])*\??)|(-?\d+(?:\.\d+)?(?:e\-?\d+)?)|("(?:[^"\\]|\\(?:[nr"\\]|u\{[0-9A-F]{4,6}\}))*")|(\.(?:\.\.)?|\/\\?|\\\/?|>>?|<<?|\[\]?|\{\}?|[()}\].,:?!;~≈=≠≤≥&|+\-*%ƒ$@\^_'`])/y;
//. Capture Group
//. [1] Whitespace
//. [2] Comment
//. [3] Alphameric
//. [4] Number
//. [5] String
//. [6] Punctuator
export default Object.freeze(function tokenize(source, comment = false) {
// 'tokenize' takes a source and produces from it an array of token objects.
// If the 'source' is not an array, then it is split into lines at the carriage
// return/linefeed. If 'comment' is true then comments are included as token
// objects. The parser does not want to see comments, but a software tool might.
const lines = (
Array.isArray(source)
? source
: source.split(rx_crlf)
);
let line_nr = 0;
let line = lines[0];
rx_token.lastIndex = 0;
return function token_generator() {
if (line === undefined) {
return;
}
let column_nr = rx_token.lastIndex;
if (column_nr >= line.length) {
rx_token.lastIndex = 0;
line_nr += 1;
line = lines[line_nr];
return (
line === undefined
? undefined
: token_generator()
);
}
let captives = rx_token.exec(line);
// Nothing matched.
if (!captives) {
return {
id: "(error)",
line_nr,
column_nr,
string: line.slice(column_nr)
};
}
// Whitespace matched.
if (captives[1]) {
return token_generator();
}
// A comment matched.
if (captives[2]) {
return (
comment
? {
id: "(comment)",
comment: captives[2],
line_nr,
column_nr,
column_to: rx_token.lastIndex
}
: token_generator()
);
}
// A name matched.
if (captives[3]) {
return {
id: captives[3],
alphameric: true,
line_nr,
column_nr,
column_to: rx_token.lastIndex
};
}
// A number literal matched.
if (captives[4]) {
return {
id: "(number)",
readonly: true,
number: big_float.normalize(big_float.make(captives[4])),
text: captives[4],
line_nr,
column_nr,
column_to: rx_token.lastIndex
};
}
// A text literal matched.
if (captives[5]) {
// We use '.replace' to convert '\u{xxxxxx}' to a codepoint
// and 'JSON.parse' to process the remaining escapes and remove the quotes.
return {
id: "(text)",
readonly: true,
text: JSON.parse(captives[5].replace(
rx_unicode_escapement,
function (ignore, code) {
return String.fromCodePoint(parseInt(code, 16));
}
)),
line_nr,
column_nr,
column_to: rx_token.lastIndex
};
}
// A punctuator matched.
if (captives[6]) {
return {
id: captives[6],
line_nr,
column_nr,
column_to: rx_token.lastIndex
};
}
};
});