@jpoly1219/context-extractor 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
package/dist/utils.js ADDED
@@ -0,0 +1,350 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.getAllOCamlFiles = exports.getAllTSFiles = exports.supportsHole = exports.isQLIdentifier = exports.isQLLabel = exports.isQLKeyword = exports.isQLLiteral = exports.isQLPredefined = exports.isQLLocalTypeAccess = exports.isQLInterface = exports.isQLArray = exports.isQLUnion = exports.isQLTuple = exports.isQLFunction = exports.parseCodeQLTypesAndLocations = exports.parseCodeQLLocationsAndTypes = exports.parseCodeQLTypes = exports.parseCodeQLVars = exports.parseCodeQLRelevantTypes = exports.removeLines = exports.parseTypeArrayString = exports.escapeQuotes = exports.isTypeAlias = exports.isPrimitive = exports.isFunction = exports.isObject = exports.isArray = exports.isUnion = exports.isTuple = exports.extractSnippet = exports.formatTypeSpan = exports.indexOfRegexGroup = void 0;
27
+ const fs = __importStar(require("fs"));
28
+ const path = __importStar(require("path"));
29
+ const types_1 = require("./types");
30
+ const indexOfRegexGroup = (match, n) => {
31
+ return match.reduce((acc, curr, i) => {
32
+ if (i < 1 || i >= n)
33
+ return acc;
34
+ return acc + curr.length;
35
+ }, match.index);
36
+ };
37
+ exports.indexOfRegexGroup = indexOfRegexGroup;
38
+ const formatTypeSpan = (typeSpan) => {
39
+ const formatted = typeSpan.split("").reduce((acc, curr) => {
40
+ // if (curr === "\n" || (curr === " " && acc.slice(-1) === " ")) return acc;
41
+ if (curr === "\n")
42
+ return acc;
43
+ if (curr === " " && acc.slice(-1) === " ")
44
+ return acc;
45
+ // if (curr === "{") return acc + "{ ";
46
+ // if (curr === "}") return acc + " }";
47
+ return acc + curr;
48
+ }, "");
49
+ return formatted;
50
+ };
51
+ exports.formatTypeSpan = formatTypeSpan;
52
+ const extractSnippet = (documentContent, start, end) => {
53
+ const lines = documentContent.split('\n');
54
+ const snippet = [];
55
+ for (let lineNumber = start.line; lineNumber <= end.line; lineNumber++) {
56
+ const line = lines[lineNumber];
57
+ // console.log(line, lineNumber)
58
+ if (line == undefined)
59
+ continue;
60
+ if (lineNumber === start.line && lineNumber === end.line) {
61
+ // Single-line range
62
+ snippet.push(line.substring(start.character, end.character));
63
+ }
64
+ else if (lineNumber === start.line) {
65
+ // Starting line of the range
66
+ snippet.push(line.substring(start.character));
67
+ }
68
+ else if (lineNumber === end.line) {
69
+ // Ending line of the range
70
+ snippet.push(line.substring(0, end.character));
71
+ }
72
+ else {
73
+ // Entire line within the range
74
+ snippet.push(line);
75
+ }
76
+ }
77
+ return snippet.join('\n');
78
+ };
79
+ exports.extractSnippet = extractSnippet;
80
+ const isTuple = (typeSpan) => {
81
+ return typeSpan[0] === "[" && typeSpan[typeSpan.length - 1] === "]";
82
+ };
83
+ exports.isTuple = isTuple;
84
+ const isUnion = (typeSpan) => {
85
+ return typeSpan.includes(" | ");
86
+ };
87
+ exports.isUnion = isUnion;
88
+ const isArray = (typeSpan) => {
89
+ return typeSpan.slice(-2) === "[]";
90
+ };
91
+ exports.isArray = isArray;
92
+ const isObject = (typeSpan) => {
93
+ return typeSpan[0] === "{" && typeSpan[typeSpan.length - 1] === "}";
94
+ };
95
+ exports.isObject = isObject;
96
+ // this is a very rudimentary check, so it should be expanded upon
97
+ const isFunction = (typeSpan) => {
98
+ return typeSpan.includes("=>");
99
+ };
100
+ exports.isFunction = isFunction;
101
+ const isPrimitive = (typeSpan) => {
102
+ const primitives = ["string", "number", "boolean"];
103
+ return primitives.includes(typeSpan);
104
+ };
105
+ exports.isPrimitive = isPrimitive;
106
+ const isTypeAlias = (typeSpan) => {
107
+ const caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
108
+ return caps.includes(typeSpan[0]);
109
+ };
110
+ exports.isTypeAlias = isTypeAlias;
111
+ const escapeQuotes = (typeSpan) => {
112
+ return typeSpan.replace(/"/g, `\\"`);
113
+ };
114
+ exports.escapeQuotes = escapeQuotes;
115
+ const parseTypeArrayString = (typeStr) => {
116
+ // Remove all spaces
117
+ const cleaned = typeStr.replace(/\s/g, '');
118
+ // Remove the outermost square brackets
119
+ const inner = cleaned.slice(1, -1);
120
+ // const inner = cleaned.slice(-1) === ";" ? cleaned.slice(1, -2) : cleaned.slice(1, -1);
121
+ // Split the string, respecting nested structures
122
+ const result = [];
123
+ let currentItem = '';
124
+ let nestLevel = 0;
125
+ for (const char of inner) {
126
+ if (char === '[')
127
+ nestLevel++;
128
+ if (char === ']')
129
+ nestLevel--;
130
+ if (char === ',' && nestLevel === 0) {
131
+ // check if currentItem is a name: type pair or just type
132
+ if (currentItem.includes(":")) {
133
+ result.push(currentItem.split(":")[1]);
134
+ }
135
+ else {
136
+ result.push(currentItem);
137
+ }
138
+ currentItem = '';
139
+ }
140
+ else {
141
+ currentItem += char;
142
+ }
143
+ }
144
+ if (currentItem.includes(":")) {
145
+ result.push(currentItem.split(":")[1]);
146
+ }
147
+ else {
148
+ result.push(currentItem);
149
+ }
150
+ return result;
151
+ };
152
+ exports.parseTypeArrayString = parseTypeArrayString;
153
+ const removeLines = (fileContent) => {
154
+ const lines = fileContent.split("\n");
155
+ const filtered = [];
156
+ for (let i = 0; i < lines.length; i++) {
157
+ const line = lines[i];
158
+ if (!(line.split(" ").includes("import") || line.split(" ").includes("from") || line.split(" ").includes("export"))) {
159
+ filtered.push(line);
160
+ }
161
+ }
162
+ return filtered;
163
+ };
164
+ exports.removeLines = removeLines;
165
+ const parseCodeQLRelevantTypes = (table) => {
166
+ const m = new Map();
167
+ const rows = table["#select"]["tuples"];
168
+ rows.forEach(row => {
169
+ const typeDeclaration = row[0]["label"];
170
+ const typeName = row[1];
171
+ const typeDefinition = row[2]["label"];
172
+ const typeQLClass = row[3];
173
+ const componentName = row[4]["label"];
174
+ const componentQLClass = row[5];
175
+ if (!m.has(typeName)) {
176
+ m.set(typeName, {
177
+ typeAliasDeclaration: typeDeclaration,
178
+ typeName: typeName,
179
+ typeDefinition: typeDefinition,
180
+ typeQLClass: typeQLClass,
181
+ components: [{ typeName: componentName, typeQLClass: componentQLClass }]
182
+ });
183
+ }
184
+ else {
185
+ const value = m.get(typeName);
186
+ value.components.push({ typeName: componentName, typeQLClass: componentQLClass });
187
+ m.set(typeName, value);
188
+ }
189
+ });
190
+ return m;
191
+ };
192
+ exports.parseCodeQLRelevantTypes = parseCodeQLRelevantTypes;
193
+ const parseCodeQLVars = (table) => {
194
+ const m = new Map();
195
+ const rows = table["#select"]["tuples"];
196
+ rows.forEach(row => {
197
+ const declaration = row[0]["label"];
198
+ const bindingPattern = row[1]["label"];
199
+ const typeAnnotation = row[2]["label"];
200
+ ;
201
+ const init = row[3]["label"];
202
+ const qlClass = row[4];
203
+ const functionReturnType = row[5];
204
+ const functionReturnTypeQLClass = row[6];
205
+ const componentName = row[7]["label"];
206
+ const componentQLClass = row[8];
207
+ if (!m.has(bindingPattern)) {
208
+ m.set(bindingPattern, {
209
+ constDeclaration: declaration,
210
+ bindingPattern: bindingPattern,
211
+ typeAnnotation: typeAnnotation,
212
+ init: init,
213
+ typeQLClass: qlClass,
214
+ functionReturnType: functionReturnType,
215
+ functionReturnTypeQLClass: functionReturnTypeQLClass,
216
+ components: [{ typeName: componentName, typeQLClass: componentQLClass }]
217
+ });
218
+ }
219
+ else {
220
+ const value = m.get(bindingPattern);
221
+ value.components.push({ typeName: componentName, typeQLClass: componentQLClass });
222
+ m.set(bindingPattern, value);
223
+ }
224
+ });
225
+ return m;
226
+ };
227
+ exports.parseCodeQLVars = parseCodeQLVars;
228
+ const parseCodeQLTypes = (table) => {
229
+ const arr = [];
230
+ const rows = table["#select"]["tuples"];
231
+ rows.forEach(row => {
232
+ const typeName = row[0];
233
+ const typeQLClass = row[1];
234
+ arr.push({ typeName: typeName, typeQLClass: typeQLClass });
235
+ });
236
+ return arr;
237
+ };
238
+ exports.parseCodeQLTypes = parseCodeQLTypes;
239
+ const parseCodeQLLocationsAndTypes = (table) => {
240
+ const locationToTypes = new Map();
241
+ const rows = table["#select"]["tuples"];
242
+ rows.forEach(row => {
243
+ const typeName = row[0];
244
+ const locatedFile = row[1];
245
+ if (!locationToTypes.has(locatedFile)) {
246
+ locationToTypes.set(locatedFile, [typeName]);
247
+ }
248
+ else {
249
+ const pair = locationToTypes.get(locatedFile);
250
+ pair.push(typeName);
251
+ locationToTypes.set(locatedFile, pair);
252
+ }
253
+ });
254
+ return locationToTypes;
255
+ };
256
+ exports.parseCodeQLLocationsAndTypes = parseCodeQLLocationsAndTypes;
257
+ const parseCodeQLTypesAndLocations = (table) => {
258
+ const typeToLocation = new Map();
259
+ const rows = table["#select"]["tuples"];
260
+ rows.forEach(row => {
261
+ const typeName = row[0];
262
+ const locatedFile = row[1];
263
+ if (!typeToLocation.has(typeName)) {
264
+ typeToLocation.set(typeName, locatedFile);
265
+ }
266
+ else {
267
+ // NOTE: this should technically be a name collision
268
+ typeToLocation.set(typeName, locatedFile);
269
+ }
270
+ });
271
+ return typeToLocation;
272
+ };
273
+ exports.parseCodeQLTypesAndLocations = parseCodeQLTypesAndLocations;
274
+ const isQLFunction = (typeQLClass) => {
275
+ return typeQLClass === "FunctionTypeExpr";
276
+ };
277
+ exports.isQLFunction = isQLFunction;
278
+ const isQLTuple = (typeQLClass) => {
279
+ return typeQLClass === "TupleTypeExpr";
280
+ };
281
+ exports.isQLTuple = isQLTuple;
282
+ const isQLUnion = (typeQLClass) => {
283
+ return typeQLClass === "UnionTypeExpr";
284
+ };
285
+ exports.isQLUnion = isQLUnion;
286
+ const isQLArray = (typeQLClass) => {
287
+ return typeQLClass === "ArrayTypeExpr";
288
+ };
289
+ exports.isQLArray = isQLArray;
290
+ const isQLInterface = (typeQLClass) => {
291
+ return typeQLClass === "InterfaceTypeExpr";
292
+ };
293
+ exports.isQLInterface = isQLInterface;
294
+ const isQLLocalTypeAccess = (typeQLClass) => {
295
+ return typeQLClass === "LocalTypeAccess";
296
+ };
297
+ exports.isQLLocalTypeAccess = isQLLocalTypeAccess;
298
+ const isQLPredefined = (typeQLClass) => {
299
+ return typeQLClass === "PredefinedTypeExpr";
300
+ };
301
+ exports.isQLPredefined = isQLPredefined;
302
+ const isQLLiteral = (typeQLClass) => {
303
+ return typeQLClass === "LiteralTypeExpr";
304
+ };
305
+ exports.isQLLiteral = isQLLiteral;
306
+ const isQLKeyword = (typeQLClass) => {
307
+ return typeQLClass === "KeywordTypeExpr";
308
+ };
309
+ exports.isQLKeyword = isQLKeyword;
310
+ const isQLLabel = (typeQLClass) => {
311
+ return typeQLClass === "Label";
312
+ };
313
+ exports.isQLLabel = isQLLabel;
314
+ const isQLIdentifier = (typeQLClass) => {
315
+ return typeQLClass === "Identifier";
316
+ };
317
+ exports.isQLIdentifier = isQLIdentifier;
318
+ const supportsHole = (lang) => {
319
+ const supportedLangs = [types_1.Language.OCaml];
320
+ return supportedLangs.includes(lang);
321
+ };
322
+ exports.supportsHole = supportsHole;
323
+ const getAllTSFiles = (dirPath, arrayOfFiles = []) => {
324
+ const files = fs.readdirSync(dirPath);
325
+ files.forEach((file) => {
326
+ const filePath = path.join(dirPath, file);
327
+ if (fs.statSync(filePath).isDirectory()) {
328
+ getAllTSFiles(filePath, arrayOfFiles);
329
+ }
330
+ else if (filePath.endsWith(".ts")) {
331
+ arrayOfFiles.push(filePath);
332
+ }
333
+ });
334
+ return arrayOfFiles;
335
+ };
336
+ exports.getAllTSFiles = getAllTSFiles;
337
+ const getAllOCamlFiles = (dirPath, arrayOfFiles = []) => {
338
+ const files = fs.readdirSync(dirPath);
339
+ files.forEach((file) => {
340
+ const filePath = path.join(dirPath, file);
341
+ if (fs.statSync(filePath).isDirectory()) {
342
+ getAllTSFiles(filePath, arrayOfFiles);
343
+ }
344
+ else if (filePath.endsWith(".ml")) {
345
+ arrayOfFiles.push(filePath);
346
+ }
347
+ });
348
+ return arrayOfFiles;
349
+ };
350
+ exports.getAllOCamlFiles = getAllOCamlFiles;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jpoly1219/context-extractor",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Extract relevant context from an incomplete program sketch.",
5
5
  "repository": {
6
6
  "type": "git",