eyemind 0.5.1 → 0.5.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.
@@ -0,0 +1,288 @@
1
+
2
+ const RDF_NS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
3
+ const RDF_TYPE = `${RDF_NS}type`;
4
+ const RDF_FIRST = `${RDF_NS}first`;
5
+ const RDF_REST = `${RDF_NS}rest`;
6
+ const RDF_NIL = `${RDF_NS}nil`;
7
+ const RDF_REIFIES = `${RDF_NS}reifies`;
8
+ const XSD_STRING = 'http://www.w3.org/2001/XMLSchema#string';
9
+ const XSD_BOOLEAN = 'http://www.w3.org/2001/XMLSchema#boolean';
10
+ const XSD_INTEGER = 'http://www.w3.org/2001/XMLSchema#integer';
11
+ const XSD_DECIMAL = 'http://www.w3.org/2001/XMLSchema#decimal';
12
+ const XSD_DOUBLE = 'http://www.w3.org/2001/XMLSchema#double';
13
+
14
+ function iri(value) {
15
+ return { type: 'iri', value: String(value) };
16
+ }
17
+
18
+ function variable(name) {
19
+ const value = String(name);
20
+ return { type: 'var', value: value[0] === '?' || value[0] === '$' ? value.slice(1) : value };
21
+ }
22
+
23
+ function blankNode(value) {
24
+ const label = String(value);
25
+ return { type: 'blank', value: label.startsWith('_:') ? label.slice(2) : label };
26
+ }
27
+
28
+ function literal(value, datatype = null, lang = null, langDir = null) {
29
+ return { type: 'literal', value, datatype, lang, langDir };
30
+ }
31
+
32
+ function tripleTerm(s, p, o) {
33
+ return { type: 'triple', s, p, o };
34
+ }
35
+
36
+ function isVariable(term) {
37
+ return term && term.type === 'var';
38
+ }
39
+
40
+ function isIRI(term) {
41
+ return term && term.type === 'iri';
42
+ }
43
+
44
+ function isBlank(term) {
45
+ return term && term.type === 'blank';
46
+ }
47
+
48
+ function isLiteral(term) {
49
+ return term && term.type === 'literal';
50
+ }
51
+
52
+ function isTripleTerm(term) {
53
+ return term && term.type === 'triple';
54
+ }
55
+
56
+ function termEquals(a, b) {
57
+ return termKey(a) === termKey(b);
58
+ }
59
+
60
+ function literalKeyValue(value) {
61
+ if (typeof value === 'bigint') return `${value.toString()}n`;
62
+ return JSON.stringify(value);
63
+ }
64
+
65
+ function isNumericPrimitive(value) {
66
+ return typeof value === 'number' || typeof value === 'bigint';
67
+ }
68
+
69
+ function compareNumericPrimitives(a, b) {
70
+ if (typeof a === 'bigint' && typeof b === 'bigint') {
71
+ if (a < b) return -1;
72
+ if (a > b) return 1;
73
+ return 0;
74
+ }
75
+ if (typeof a === 'bigint' && typeof b === 'number' && Number.isInteger(b) && Number.isSafeInteger(b)) {
76
+ const bi = BigInt(b);
77
+ if (a < bi) return -1;
78
+ if (a > bi) return 1;
79
+ return 0;
80
+ }
81
+ if (typeof a === 'number' && typeof b === 'bigint' && Number.isInteger(a) && Number.isSafeInteger(a)) {
82
+ const ai = BigInt(a);
83
+ if (ai < b) return -1;
84
+ if (ai > b) return 1;
85
+ return 0;
86
+ }
87
+ const diff = Number(a) - Number(b);
88
+ if (diff < 0) return -1;
89
+ if (diff > 0) return 1;
90
+ return 0;
91
+ }
92
+
93
+ function termKey(term) {
94
+ if (!term) return 'null';
95
+ if (term.type === 'iri') return `I:${term.value}`;
96
+ if (term.type === 'blank') return `B:${term.value}`;
97
+ if (term.type === 'var') return `V:${term.value}`;
98
+ if (term.type === 'literal') return `L:${literalKeyValue(term.value)}^^${term.datatype || ''}@${term.lang || ''}--${term.langDir || ''}`;
99
+ if (term.type === 'triple') return `T:${termKey(term.s)} ${termKey(term.p)} ${termKey(term.o)}`;
100
+ return JSON.stringify(term);
101
+ }
102
+
103
+ function tripleKey(triple) {
104
+ return `${termKey(triple.s)} ${termKey(triple.p)} ${termKey(triple.o)}`;
105
+ }
106
+
107
+ function cloneTerm(term) {
108
+ if (!term) return term;
109
+ if (term.type === 'triple') return tripleTerm(cloneTerm(term.s), cloneTerm(term.p), cloneTerm(term.o));
110
+ return { ...term };
111
+ }
112
+
113
+ function valueToTerm(value) {
114
+ if (value && typeof value === 'object' && value.type) return value;
115
+ return literal(value, inferDatatype(value));
116
+ }
117
+
118
+ function inferDatatype(value) {
119
+ if (typeof value === 'boolean') return XSD_BOOLEAN;
120
+ if (typeof value === 'bigint') return XSD_INTEGER;
121
+ if (typeof value === 'number' && Number.isInteger(value)) return XSD_INTEGER;
122
+ if (typeof value === 'number') return XSD_DECIMAL;
123
+ if (typeof value === 'string') return XSD_STRING;
124
+ return null;
125
+ }
126
+
127
+ function termToPrimitive(term) {
128
+ if (!term) return undefined;
129
+ if (term.type === 'literal') return term.value;
130
+ if (term.type === 'iri') return term.value;
131
+ if (term.type === 'blank') return `_:${term.value}`;
132
+ if (term.type === 'var') return undefined;
133
+ if (term.type === 'triple') return term;
134
+ return term;
135
+ }
136
+
137
+ function termToString(term) {
138
+ const value = termToPrimitive(term);
139
+ if (value === undefined || value === null) return '';
140
+ if (value && value.type === 'triple') return formatTerm(value);
141
+ return String(value);
142
+ }
143
+
144
+ function booleanValue(value) {
145
+ const primitive = value && value.type ? termToPrimitive(value) : value;
146
+ if (primitive === undefined || primitive === null) return false;
147
+ if (typeof primitive === 'boolean') return primitive;
148
+ if (typeof primitive === 'bigint') return primitive !== 0n;
149
+ if (typeof primitive === 'number') return primitive !== 0 && !Number.isNaN(primitive);
150
+ if (typeof primitive === 'string') return primitive.length > 0 && primitive !== 'false';
151
+ return Boolean(primitive);
152
+ }
153
+
154
+ function comparePrimitives(a, b) {
155
+ const av = a && a.type ? termToPrimitive(a) : a;
156
+ const bv = b && b.type ? termToPrimitive(b) : b;
157
+ if (isNumericPrimitive(av) && isNumericPrimitive(bv)) return compareNumericPrimitives(av, bv);
158
+ const as = String(av);
159
+ const bs = String(bv);
160
+ if (as < bs) return -1;
161
+ if (as > bs) return 1;
162
+ return 0;
163
+ }
164
+
165
+ function escapeString(value) {
166
+ return String(value)
167
+ .replace(/\\/g, '\\\\')
168
+ .replace(/\n/g, '\\n')
169
+ .replace(/\r/g, '\\r')
170
+ .replace(/\t/g, '\\t')
171
+ .replace(/"/g, '\\"');
172
+ }
173
+
174
+ function compactIRI(value, prefixes = {}) {
175
+ if (value === RDF_TYPE) return 'a';
176
+ const entries = Object.entries(prefixes)
177
+ .filter(([, iriPrefix]) => iriPrefix && value.startsWith(iriPrefix))
178
+ .sort((a, b) => b[1].length - a[1].length);
179
+ if (entries.length > 0) {
180
+ const [prefix, iriPrefix] = entries[0];
181
+ const local = value.slice(iriPrefix.length);
182
+ if (/^[A-Za-z_][A-Za-z0-9_\-]*$/.test(local) || /^[A-Za-z0-9_\-]+$/.test(local)) {
183
+ return `${prefix}:${local}`;
184
+ }
185
+ }
186
+ return `<${value}>`;
187
+ }
188
+
189
+ // A number may drop its datatype only when the shorthand reads back as the
190
+ // same datatype: an INTEGER carries neither `.` nor exponent, a DECIMAL a
191
+ // `.`, a DOUBLE an exponent ([95]-[97]). `72` written bare for an
192
+ // xsd:double would come back as an xsd:integer, so such a literal keeps
193
+ // its datatype; a whole-numbered decimal gains the `.0` that makes it one.
194
+ function numericShorthand(value, datatype) {
195
+ const text = String(value);
196
+ const hasExponent = /[eE]/.test(text);
197
+ const hasDot = text.includes('.');
198
+ if (!datatype || datatype === XSD_INTEGER) return !hasDot && !hasExponent ? text : null;
199
+ if (datatype === XSD_DECIMAL) return hasExponent ? null : (hasDot ? text : `${text}.0`);
200
+ if (datatype === XSD_DOUBLE) return hasExponent ? text : null;
201
+ return null;
202
+ }
203
+
204
+ function formatTerm(term, prefixes = {}) {
205
+ if (term.type === 'iri') return compactIRI(term.value, prefixes);
206
+ if (term.type === 'blank') return `_:${term.value}`;
207
+ if (term.type === 'var') return `?${term.value}`;
208
+ if (term.type === 'triple') return `<<(${formatTerm(term.s, prefixes)} ${formatTerm(term.p, prefixes)} ${formatTerm(term.o, prefixes)})>>`;
209
+ if (term.type === 'literal') {
210
+ const v = term.value;
211
+ if (typeof v === 'bigint' && !term.lang && (!term.datatype || term.datatype === XSD_INTEGER)) return String(v);
212
+ if (typeof v === 'number' && Number.isFinite(v) && !term.lang) {
213
+ const shorthand = numericShorthand(v, term.datatype);
214
+ if (shorthand !== null) return shorthand;
215
+ }
216
+ if (typeof v === 'boolean' && !term.lang && (!term.datatype || term.datatype === XSD_BOOLEAN)) return v ? 'true' : 'false';
217
+ const lexical = `"${escapeString(v)}"`;
218
+ if (term.lang) return `${lexical}@${term.lang}${term.langDir ? `--${term.langDir}` : ''}`;
219
+ if (term.datatype && term.datatype !== XSD_STRING) return `${lexical}^^${compactIRI(term.datatype, prefixes)}`;
220
+ return lexical;
221
+ }
222
+ return String(term.value ?? term);
223
+ }
224
+
225
+ function formatTriple(triple, prefixes = {}) {
226
+ return `${formatTerm(triple.s, prefixes)} ${formatTerm(triple.p, prefixes)} ${formatTerm(triple.o, prefixes)} .`;
227
+ }
228
+
229
+ // A solution mapping is keyed by variable name, and `?constructor`,
230
+ // `?toString` and friends are perfectly legal VARNAMEs ([123]/[126] of
231
+ // SPARQL 1.2 RL §7.6). A plain `{}` inherits those names from
232
+ // `Object.prototype`, so `binding[name]` would answer with an inherited
233
+ // function rather than `undefined` and a rule whose body binds such a
234
+ // variable would silently never fire. Every solution mapping is therefore
235
+ // created without a prototype, through these three helpers.
236
+ function emptyBinding() {
237
+ return Object.create(null);
238
+ }
239
+
240
+ function cloneBinding(binding) {
241
+ return Object.assign(Object.create(null), binding);
242
+ }
243
+
244
+ function extendBinding(binding, name, term) {
245
+ const out = cloneBinding(binding);
246
+ out[name] = term;
247
+ return out;
248
+ }
249
+
250
+ export {
251
+ RDF_NS,
252
+ RDF_TYPE,
253
+ RDF_FIRST,
254
+ RDF_REST,
255
+ RDF_NIL,
256
+ RDF_REIFIES,
257
+ XSD_STRING,
258
+ XSD_BOOLEAN,
259
+ XSD_INTEGER,
260
+ XSD_DECIMAL,
261
+ XSD_DOUBLE,
262
+ iri,
263
+ variable,
264
+ blankNode,
265
+ literal,
266
+ tripleTerm,
267
+ isVariable,
268
+ isIRI,
269
+ isBlank,
270
+ isLiteral,
271
+ isTripleTerm,
272
+ termEquals,
273
+ termKey,
274
+ tripleKey,
275
+ cloneTerm,
276
+ valueToTerm,
277
+ inferDatatype,
278
+ termToPrimitive,
279
+ termToString,
280
+ booleanValue,
281
+ comparePrimitives,
282
+ compactIRI,
283
+ formatTerm,
284
+ formatTriple,
285
+ emptyBinding,
286
+ cloneBinding,
287
+ extendBinding,
288
+ };
@@ -0,0 +1,341 @@
1
+
2
+ class SyntaxErrorWithLocation extends Error {
3
+ constructor(message, token) {
4
+ const suffix = token && token.line ? ` at ${token.filename || '<input>'}:${token.line}:${token.column}` : '';
5
+ super(`${message}${suffix}`);
6
+ this.name = 'SyntaxError';
7
+ this.token = token;
8
+ }
9
+ }
10
+
11
+ function continuesName(c) {
12
+ if (c === undefined) return false;
13
+ return /[0-9A-Za-z_]/.test(c) || c.charCodeAt(0) > 127;
14
+ }
15
+
16
+ function tokenize(source, filenameOrOptions = '<input>') {
17
+ const options = typeof filenameOrOptions === 'object' && filenameOrOptions !== null ? filenameOrOptions : { filename: filenameOrOptions };
18
+ const filename = options.filename || '<input>';
19
+ const strictGrammar = !!options.strictGrammar;
20
+ const tokens = [];
21
+ let i = 0;
22
+ let line = 1;
23
+ let column = 1;
24
+
25
+ function current() { return source[i]; }
26
+ function peek(n = 1) { return source[i + n]; }
27
+ function startsWith(text) { return source.startsWith(text, i); }
28
+ function advance() {
29
+ const ch = source[i++];
30
+ if (ch === '\n') { line += 1; column = 1; }
31
+ else column += 1;
32
+ return ch;
33
+ }
34
+ function token(type, value, startLine, startColumn, extra = {}) {
35
+ tokens.push({ type, value, line: startLine, column: startColumn, ...extra });
36
+ }
37
+ function syntax(message, startLine, startColumn) {
38
+ throw new SyntaxErrorWithLocation(message, { line: startLine, column: startColumn, filename });
39
+ }
40
+
41
+ // Documents and numeric escapes contain Unicode scalar values only (§7).
42
+ // Scan code points so valid UTF-16 surrogate pairs remain accepted.
43
+ let scalarLine = 1;
44
+ let scalarColumn = 1;
45
+ for (const character of source) {
46
+ const code = character.codePointAt(0);
47
+ if (code >= 0xD800 && code <= 0xDFFF) syntax('Surrogate code point is not a Unicode scalar value', scalarLine, scalarColumn);
48
+ if (character === '\n') { scalarLine += 1; scalarColumn = 1; }
49
+ else scalarColumn += character.length;
50
+ }
51
+
52
+ function readNumericLiteral() {
53
+ let value = '';
54
+ const start = i;
55
+ while (i < source.length && isDigitCode(source.charCodeAt(i))) { i += 1; column += 1; }
56
+ if (source[i] === '.' && isDigitCode(source.charCodeAt(i + 1))) {
57
+ i += 1; column += 1;
58
+ while (i < source.length && isDigitCode(source.charCodeAt(i))) { i += 1; column += 1; }
59
+ }
60
+ value = source.slice(start, i);
61
+ if (current() === 'e' || current() === 'E') {
62
+ const saveI = i;
63
+ const saveLine = line;
64
+ const saveColumn = column;
65
+ let exponent = advance();
66
+ if (current() === '+' || current() === '-') exponent += advance();
67
+ if (isDigitCode(source.charCodeAt(i))) {
68
+ while (i < source.length && isDigitCode(source.charCodeAt(i))) exponent += advance();
69
+ value += exponent;
70
+ } else {
71
+ i = saveI;
72
+ line = saveLine;
73
+ column = saveColumn;
74
+ }
75
+ }
76
+ return value;
77
+ }
78
+
79
+ function readEscape(startLine, startColumn) {
80
+ advance(); // consume backslash
81
+ const esc = advance();
82
+ if (esc === 'u' || esc === 'U') {
83
+ const length = esc === 'u' ? 4 : 8;
84
+ let hex = '';
85
+ for (let j = 0; j < length; j += 1) {
86
+ if (!isHexCode(source.charCodeAt(i))) syntax(`Invalid \\${esc} escape`, startLine, startColumn);
87
+ hex += advance();
88
+ }
89
+ const codePoint = Number.parseInt(hex, 16);
90
+ if (codePoint >= 0xD800 && codePoint <= 0xDFFF) syntax(`Invalid \\${esc} escape: surrogate code point`, startLine, startColumn);
91
+ try { return String.fromCodePoint(codePoint); }
92
+ catch { syntax(`Invalid \\${esc} escape`, startLine, startColumn); }
93
+ }
94
+ if (strictGrammar && !Object.hasOwn(escapeMap, esc)) syntax(`Invalid escape \\${esc}`, startLine, startColumn);
95
+ return escapeValue(esc);
96
+ }
97
+
98
+ function readIriChar(startLine, startColumn) {
99
+ if (current() === '\\') {
100
+ advance();
101
+ const esc = advance();
102
+ if (esc !== 'u' && esc !== 'U') syntax(`Invalid IRI escape \\${esc}`, startLine, startColumn);
103
+ const length = esc === 'u' ? 4 : 8;
104
+ let hex = '';
105
+ for (let j = 0; j < length; j += 1) {
106
+ if (!isHexCode(source.charCodeAt(i))) syntax(`Invalid \\${esc} escape`, startLine, startColumn);
107
+ hex += advance();
108
+ }
109
+ const codePoint = Number.parseInt(hex, 16);
110
+ if (codePoint >= 0xD800 && codePoint <= 0xDFFF) syntax(`Invalid \\${esc} escape: surrogate code point`, startLine, startColumn);
111
+ try { return String.fromCodePoint(codePoint); }
112
+ catch { syntax(`Invalid \\${esc} escape`, startLine, startColumn); }
113
+ }
114
+ const c = current();
115
+ if (strictGrammar && (/[\u0000-\u0020]/.test(c) || /[<>"{}|^`]/.test(c))) syntax(`Invalid character in IRI reference ${JSON.stringify(c)}`, startLine, startColumn);
116
+ return advance();
117
+ }
118
+
119
+ while (i < source.length) {
120
+ const ch = current();
121
+ if (isWhitespaceCode(source.charCodeAt(i))) { advance(); continue; }
122
+ if (ch === '#') {
123
+ while (i < source.length && current() !== '\n' && current() !== '\r') advance();
124
+ continue;
125
+ }
126
+
127
+ const startLine = line;
128
+ const startColumn = column;
129
+
130
+ if (startsWith('<<(')) {
131
+ advance(); advance(); advance();
132
+ token('punct', '<<(', startLine, startColumn);
133
+ continue;
134
+ }
135
+ if (startsWith(')>>')) {
136
+ advance(); advance(); advance();
137
+ token('punct', ')>>', startLine, startColumn);
138
+ continue;
139
+ }
140
+ if (startsWith('<<')) {
141
+ advance(); advance();
142
+ token('punct', '<<', startLine, startColumn);
143
+ continue;
144
+ }
145
+ if (startsWith('>>')) {
146
+ advance(); advance();
147
+ token('punct', '>>', startLine, startColumn);
148
+ continue;
149
+ }
150
+ if (startsWith('{|')) {
151
+ advance(); advance();
152
+ token('punct', '{|', startLine, startColumn);
153
+ continue;
154
+ }
155
+ if (startsWith('|}')) {
156
+ advance(); advance();
157
+ token('punct', '|}', startLine, startColumn);
158
+ continue;
159
+ }
160
+
161
+ if (ch === '<' && looksLikeIRI(source, i)) {
162
+ let value = '';
163
+ advance();
164
+ while (i < source.length && current() !== '>') value += readIriChar(startLine, startColumn);
165
+ if (current() !== '>') syntax('Unterminated IRI', startLine, startColumn);
166
+ advance();
167
+ token('iri', value, startLine, startColumn);
168
+ continue;
169
+ }
170
+
171
+ if ((ch === '"' && startsWith('"""')) || (ch === "'" && startsWith("'''"))) {
172
+ const quote = ch;
173
+ advance(); advance(); advance();
174
+ let value = '';
175
+ while (i < source.length && !startsWith(quote.repeat(3))) {
176
+ if (current() === '\\') {
177
+ value += readEscape(startLine, startColumn);
178
+ } else {
179
+ value += advance();
180
+ }
181
+ }
182
+ if (!startsWith(quote.repeat(3))) syntax('Unterminated long string literal', startLine, startColumn);
183
+ advance(); advance(); advance();
184
+ token('string', value, startLine, startColumn, { long: true, quote });
185
+ continue;
186
+ }
187
+
188
+ if (ch === '"' || ch === "'") {
189
+ const quote = ch;
190
+ let value = '';
191
+ advance();
192
+ while (i < source.length && current() !== quote) {
193
+ if (current() === '\n' || current() === '\r') syntax('Unterminated string literal', startLine, startColumn);
194
+ if (current() === '\\') {
195
+ value += readEscape(startLine, startColumn);
196
+ } else {
197
+ value += advance();
198
+ }
199
+ }
200
+ if (current() !== quote) syntax('Unterminated string literal', startLine, startColumn);
201
+ advance();
202
+ token('string', value, startLine, startColumn, { long: false, quote });
203
+ continue;
204
+ }
205
+
206
+ if (ch === '@') {
207
+ const wordStart = i;
208
+ advance();
209
+ while (i < source.length && isLangTagCode(source.charCodeAt(i))) { i += 1; column += 1; }
210
+ const value = source.slice(wordStart, i);
211
+ if (!/^@[A-Za-z]+(?:-[A-Za-z0-9]+)*(?:--[A-Za-z]+)?$/.test(value)) syntax(`Invalid language tag ${value}`, startLine, startColumn);
212
+ token('word', value, startLine, startColumn);
213
+ continue;
214
+ }
215
+
216
+ if (ch === '?' || ch === '$') {
217
+ const varStart = i;
218
+ advance();
219
+ while (i < source.length && isVarNameCode(source.charCodeAt(i))) { i += 1; column += 1; }
220
+ if (i - varStart === 1) syntax('Expected variable name', startLine, startColumn);
221
+ token('variable', source.slice(varStart + 1, i), startLine, startColumn);
222
+ continue;
223
+ }
224
+
225
+ if (startsNumericLiteral(source, i)) {
226
+ const value = readNumericLiteral();
227
+ token('number', numericValue(value), startLine, startColumn, { lexical: value });
228
+ continue;
229
+ }
230
+
231
+ const two = ch + peek();
232
+ if ([':=', '!=', '<=', '>=', '&&', '||', '=>', '^^'].includes(two)) {
233
+ advance(); advance();
234
+ token('operator', two, startLine, startColumn);
235
+ continue;
236
+ }
237
+
238
+ if ('{}()[].,;|'.includes(ch)) {
239
+ token('punct', advance(), startLine, startColumn);
240
+ continue;
241
+ }
242
+
243
+ if ('=<>+-*/!^~'.includes(ch)) {
244
+ token('operator', advance(), startLine, startColumn);
245
+ continue;
246
+ }
247
+
248
+ const wordStart = i;
249
+ while (i < source.length) {
250
+ const c = source[i];
251
+ if (c === '\\' && source[i + 1] !== undefined) {
252
+ i += 2;
253
+ column += 2;
254
+ continue;
255
+ }
256
+ const code = source.charCodeAt(i);
257
+ // `-` and `.` belong to a name: PN_CHARS includes `-`, and
258
+ // PN_LOCAL/PN_PREFIX/BLANK_NODE_LABEL admit `.` between name
259
+ // characters ([122], [147]-[149] of SPARQL 1.2 RL 7.6). Either one
260
+ // continues the word while a name character follows, which keeps
261
+ // `:a-b`, `_:a-1` and `res:CITY_St.-denis` whole and still leaves
262
+ // the terminator in `:a .` and the operator in `?x - 1` alone.
263
+ if (c === '-' || c === '.') {
264
+ if (continuesName(source[i + 1])) {
265
+ i += 1;
266
+ column += 1;
267
+ continue;
268
+ }
269
+ break;
270
+ }
271
+ if (isWhitespaceCode(code) || '{}()[],;|'.includes(c) || '=<>+-*/!^~'.includes(c)) break;
272
+ if (c === '#') break;
273
+ i += 1;
274
+ column += 1;
275
+ }
276
+ if (i === wordStart) syntax(`Unexpected character ${JSON.stringify(ch)}`, startLine, startColumn);
277
+
278
+ const value = source.slice(wordStart, i);
279
+ if (/^[+-]?(?:(?:\d+\.\d*|\.\d+)(?:[eE][+-]?\d+)?|\d+[eE][+-]?\d+|\d+)$/.test(value)) token('number', numericValue(value), startLine, startColumn, { lexical: value });
280
+ else token('word', value, startLine, startColumn);
281
+ }
282
+
283
+ tokens.push({ type: 'eof', value: '<eof>', line, column, filename });
284
+ return tokens;
285
+ }
286
+
287
+
288
+ function numericValue(lexical) {
289
+ const value = Number(lexical);
290
+ return /^[+-]?\d+$/.test(lexical) && !Number.isSafeInteger(value) ? BigInt(lexical) : value;
291
+ }
292
+
293
+ function isDigitCode(code) {
294
+ return code >= 48 && code <= 57;
295
+ }
296
+
297
+ function isHexCode(code) {
298
+ return (code >= 48 && code <= 57) || (code >= 65 && code <= 70) || (code >= 97 && code <= 102);
299
+ }
300
+
301
+ function isWhitespaceCode(code) {
302
+ return code === 32 || code === 9 || code === 10 || code === 13 || code === 12;
303
+ }
304
+
305
+ function isLangTagCode(code) {
306
+ return (code >= 65 && code <= 90) || (code >= 97 && code <= 122) || (code >= 48 && code <= 57) || code === 45;
307
+ }
308
+
309
+ // [146] VARNAME admits PN_CHARS_U and digits but, unlike PN_CHARS, not
310
+ // `-`: `?n-1` is `?n` minus `1`, which [109] AdditiveExpression then reads
311
+ // through its NumericLiteralNegative alternative.
312
+ function isVarNameCode(code) {
313
+ return (code >= 65 && code <= 90) || (code >= 97 && code <= 122) || (code >= 48 && code <= 57) || code === 95;
314
+ }
315
+
316
+ function startsNumericLiteral(source, i) {
317
+ const ch = source[i];
318
+ const next = source[i + 1];
319
+ if (isDigitCode(ch.charCodeAt(0))) return true;
320
+ if (ch === '.' && next !== undefined && isDigitCode(next.charCodeAt(0))) return true;
321
+ return false;
322
+ }
323
+
324
+ function looksLikeIRI(source, i) {
325
+ const next = source[i + 1];
326
+ if (next === undefined || /[\s=]/.test(next)) return false;
327
+ for (let j = i + 1; j < source.length; j += 1) {
328
+ const c = source[j];
329
+ if (c === '>') return true;
330
+ if (/\s/.test(c)) return false;
331
+ }
332
+ return false;
333
+ }
334
+
335
+ const escapeMap = { n: '\n', r: '\r', t: '\t', b: '\b', f: '\f', '"': '"', "'": "'", '\\': '\\' };
336
+
337
+ function escapeValue(esc) {
338
+ return escapeMap[esc] ?? esc;
339
+ }
340
+
341
+ export { tokenize, SyntaxErrorWithLocation };