rapydscript-ns 0.9.0 → 0.9.2

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.
Files changed (100) hide show
  1. package/.agignore +1 -1
  2. package/.github/workflows/ci.yml +38 -38
  3. package/=template.pyj +5 -5
  4. package/CHANGELOG.md +10 -0
  5. package/HACKING.md +103 -103
  6. package/LICENSE +24 -24
  7. package/README.md +7 -6
  8. package/TODO.md +116 -1
  9. package/add-toc-to-readme +2 -2
  10. package/bin/export +75 -75
  11. package/bin/rapydscript +70 -70
  12. package/bin/web-repl-export +102 -102
  13. package/build +2 -2
  14. package/language-service/index.js +9 -9
  15. package/language-service/language-service.d.ts +1 -1
  16. package/package.json +6 -2
  17. package/publish.py +37 -37
  18. package/release/compiler.js +246 -231
  19. package/release/signatures.json +23 -23
  20. package/session.vim +4 -4
  21. package/setup.cfg +2 -2
  22. package/src/compiler.pyj +36 -36
  23. package/src/errors.pyj +30 -30
  24. package/src/lib/aes.pyj +646 -646
  25. package/src/lib/contextlib.pyj +379 -0
  26. package/src/lib/copy.pyj +120 -120
  27. package/src/lib/datetime.pyj +712 -0
  28. package/src/lib/elementmaker.pyj +83 -83
  29. package/src/lib/encodings.pyj +126 -126
  30. package/src/lib/gettext.pyj +569 -569
  31. package/src/lib/io.pyj +500 -0
  32. package/src/lib/itertools.pyj +580 -580
  33. package/src/lib/json.pyj +227 -0
  34. package/src/lib/math.pyj +193 -193
  35. package/src/lib/operator.pyj +11 -11
  36. package/src/lib/pythonize.pyj +20 -20
  37. package/src/lib/random.pyj +118 -118
  38. package/src/lib/react.pyj +74 -74
  39. package/src/lib/traceback.pyj +63 -63
  40. package/src/lib/uuid.pyj +77 -77
  41. package/src/monaco-language-service/diagnostics.js +4 -4
  42. package/src/monaco-language-service/dts.js +550 -550
  43. package/src/monaco-language-service/index.js +2 -2
  44. package/src/output/comments.pyj +45 -45
  45. package/src/output/exceptions.pyj +201 -201
  46. package/src/output/jsx.pyj +164 -164
  47. package/src/output/loops.pyj +9 -0
  48. package/src/output/treeshake.pyj +182 -182
  49. package/src/output/utils.pyj +72 -72
  50. package/src/string_interpolation.pyj +72 -72
  51. package/src/tokenizer.pyj +1 -1
  52. package/src/unicode_aliases.pyj +576 -576
  53. package/src/utils.pyj +192 -192
  54. package/test/_import_one.pyj +37 -37
  55. package/test/_import_two/__init__.pyj +11 -11
  56. package/test/_import_two/level2/deep.pyj +4 -4
  57. package/test/_import_two/other.pyj +6 -6
  58. package/test/_import_two/sub.pyj +13 -13
  59. package/test/aes_vectors.pyj +421 -421
  60. package/test/annotations.pyj +80 -80
  61. package/test/contextlib.pyj +362 -0
  62. package/test/datetime.pyj +500 -0
  63. package/test/debugger_stmt.pyj +41 -0
  64. package/test/decorators.pyj +77 -77
  65. package/test/docstrings.pyj +39 -39
  66. package/test/elementmaker_test.pyj +45 -45
  67. package/test/functions.pyj +151 -151
  68. package/test/generators.pyj +41 -41
  69. package/test/generic.pyj +370 -370
  70. package/test/imports.pyj +72 -72
  71. package/test/internationalization.pyj +73 -73
  72. package/test/io.pyj +316 -0
  73. package/test/json.pyj +196 -0
  74. package/test/lint.pyj +164 -164
  75. package/test/loops.pyj +85 -85
  76. package/test/numpy.pyj +734 -734
  77. package/test/omit_function_metadata.pyj +20 -20
  78. package/test/repl.pyj +121 -121
  79. package/test/scoped_flags.pyj +76 -76
  80. package/test/unit/index.js +66 -0
  81. package/test/unit/language-service-dts.js +543 -543
  82. package/test/unit/language-service-hover.js +455 -455
  83. package/test/unit/language-service.js +1 -1
  84. package/test/unit/web-repl.js +533 -0
  85. package/tools/compiler.d.ts +367 -0
  86. package/tools/completer.js +131 -131
  87. package/tools/gettext.js +185 -185
  88. package/tools/ini.js +65 -65
  89. package/tools/msgfmt.js +187 -187
  90. package/tools/repl.js +223 -223
  91. package/tools/test.js +118 -118
  92. package/tools/utils.js +128 -128
  93. package/tools/web_repl.js +95 -95
  94. package/try +41 -41
  95. package/web-repl/env.js +196 -196
  96. package/web-repl/index.html +163 -163
  97. package/web-repl/prism.css +139 -139
  98. package/web-repl/prism.js +113 -113
  99. package/web-repl/rapydscript.js +224 -224
  100. package/web-repl/sha1.js +25 -25
@@ -0,0 +1,367 @@
1
+ // compiler.d.ts — Type declarations for the RapydScript-N compiler
2
+
3
+ // -- Token & Position ---------------------------------------------------
4
+
5
+ export interface AST_Position {
6
+ line: number;
7
+ col: number;
8
+ pos: number;
9
+ }
10
+
11
+ export interface AST_Token {
12
+ type: string;
13
+ value: string;
14
+ line: number;
15
+ col: number;
16
+ pos: number;
17
+ nlb: boolean;
18
+ }
19
+
20
+ // -- AST Nodes ----------------------------------------------------------
21
+
22
+ export interface AST_Node {
23
+ start: AST_Token;
24
+ end: AST_Token;
25
+ print(output: OutputStream): void;
26
+ walk(visitor: TreeWalker): boolean | undefined;
27
+ _walk(visitor: TreeWalker): boolean | undefined;
28
+ clone(): this;
29
+ _dump(depth?: number, omit?: string[], offset?: number, include_name?: boolean): void;
30
+ }
31
+
32
+ export interface TreeWalker {
33
+ _visit(node: AST_Node): boolean | undefined;
34
+ }
35
+
36
+ export interface AST_Toplevel extends AST_Node {
37
+ body: AST_Node[];
38
+ globals: Record<string, unknown>;
39
+ baselib: Record<string, boolean>;
40
+ imports: Record<string, AST_ImportedModule>;
41
+ imported_module_ids: string[];
42
+ nonlocalvars: string[];
43
+ localvars: string[];
44
+ shebang: string | null;
45
+ import_order: number;
46
+ module_id: string;
47
+ exports: unknown[];
48
+ classes: Record<string, unknown>;
49
+ scoped_flags: Record<string, boolean>;
50
+ filename: string;
51
+ srchash: string;
52
+ }
53
+
54
+ export interface AST_ImportedModule {
55
+ filename: string;
56
+ body: AST_Node[] | null;
57
+ src_code: string | null;
58
+ localvars: string[];
59
+ nonlocalvars: string[];
60
+ needed_names: Set<string> | null;
61
+ }
62
+
63
+ // -- Errors -------------------------------------------------------------
64
+
65
+ export interface RapydScriptSyntaxError extends Error {
66
+ message: string;
67
+ filename: string;
68
+ line: number;
69
+ col: number;
70
+ pos: number;
71
+ is_eof: boolean;
72
+ toString(): string;
73
+ }
74
+
75
+ export interface RapydScriptSyntaxErrorConstructor {
76
+ new (message: string, filename: string, line: number, col: number, pos: number, is_eof?: boolean): RapydScriptSyntaxError;
77
+ (message: string, filename: string, line: number, col: number, pos: number, is_eof?: boolean): RapydScriptSyntaxError;
78
+ }
79
+
80
+ export type ImportError = RapydScriptSyntaxError;
81
+ export interface ImportErrorConstructor extends RapydScriptSyntaxErrorConstructor {}
82
+
83
+ export interface DefaultsError extends Error {
84
+ message: string;
85
+ }
86
+
87
+ export interface DefaultsErrorConstructor {
88
+ new (name: string, defs: Record<string, unknown>): DefaultsError;
89
+ (name: string, defs: Record<string, unknown>): DefaultsError;
90
+ }
91
+
92
+ // -- Parse Options ------------------------------------------------------
93
+
94
+ export interface ParseOptions {
95
+ filename?: string | null;
96
+ module_id?: string;
97
+ toplevel?: AST_Toplevel | null;
98
+ for_linting?: boolean;
99
+ import_dirs?: string[];
100
+ classes?: Record<string, unknown>;
101
+ scoped_flags?: ScopedFlags;
102
+ discard_asserts?: boolean;
103
+ module_cache_dir?: string;
104
+ basedir?: string;
105
+ libdir?: string;
106
+ imported_modules?: Record<string, unknown>;
107
+ importing_modules?: Record<string, unknown>;
108
+ }
109
+
110
+ export interface ScopedFlags {
111
+ dict_literals?: boolean;
112
+ overload_getitem?: boolean;
113
+ bound_methods?: boolean;
114
+ hash_literals?: boolean;
115
+ overload_operators?: boolean;
116
+ truthiness?: boolean;
117
+ jsx?: boolean;
118
+ strict_arithmetic?: boolean;
119
+ [flag: string]: boolean | undefined;
120
+ }
121
+
122
+ // -- Output Stream Options ----------------------------------------------
123
+
124
+ export interface OutputStreamOptions {
125
+ indent_start?: number;
126
+ indent_level?: number;
127
+ quote_keys?: boolean;
128
+ space_colon?: boolean;
129
+ ascii_only?: boolean;
130
+ width?: number;
131
+ max_line_len?: number;
132
+ ie_proof?: boolean;
133
+ beautify?: boolean;
134
+ source_map?: unknown | null;
135
+ bracketize?: boolean;
136
+ semicolons?: boolean;
137
+ comments?: boolean | ((node: AST_Node) => boolean);
138
+ preserve_line?: boolean;
139
+ omit_baselib?: boolean;
140
+ baselib_plain?: string | null;
141
+ private_scope?: boolean;
142
+ keep_docstrings?: boolean;
143
+ discard_asserts?: boolean;
144
+ module_cache_dir?: string;
145
+ js_version?: number;
146
+ write_name?: boolean;
147
+ omit_function_metadata?: boolean;
148
+ pythonize_strings?: boolean;
149
+ repl_mode?: boolean;
150
+ }
151
+
152
+ export interface OutputStream {
153
+ options: Required<OutputStreamOptions>;
154
+ current_col: number;
155
+ current_line: number;
156
+ current_pos: number;
157
+
158
+ print(str: string): void;
159
+ get(): string;
160
+ toString(): string;
161
+ space(): void;
162
+ indent(half?: boolean): void;
163
+ with_indent(col: number, proceed: () => void): void;
164
+ indentation(): string;
165
+ set_indentation(val: number): void;
166
+ newline(): void;
167
+ semicolon(): void;
168
+ force_semicolon(): void;
169
+ next_indent(): number;
170
+ end_statement(): void;
171
+ with_block(cont: () => void): void;
172
+ with_parens(cont: () => void): void;
173
+ with_square(cont: () => void): void;
174
+ comma(): void;
175
+ colon(): void;
176
+ assign(name: string | AST_Node): void;
177
+ current_width(): number;
178
+ should_break(): boolean;
179
+ last(): string;
180
+ print_string(str: string): void;
181
+ print_name(name: string): void;
182
+ make_name(name: string): string;
183
+ make_indent(back?: number): string;
184
+ last_char(): string;
185
+ push_node(node: AST_Node): void;
186
+ pop_node(): AST_Node;
187
+ stack(): AST_Node[];
188
+ parent(n?: number): AST_Node | undefined;
189
+ line(): number;
190
+ col(): number;
191
+ pos(): number;
192
+ }
193
+
194
+ export interface OutputStreamConstructor {
195
+ new (options?: OutputStreamOptions): OutputStream;
196
+ (options?: OutputStreamOptions): OutputStream;
197
+ }
198
+
199
+ // -- Tree Shake Context -------------------------------------------------
200
+
201
+ export interface TreeShakeContext {
202
+ parse: CompilerExports["parse"];
203
+ import_dirs?: string[];
204
+ basedir?: string;
205
+ libdir?: string;
206
+ discard_asserts?: boolean;
207
+ module_cache_dir?: string;
208
+ }
209
+
210
+ // -- Tokenizer ----------------------------------------------------------
211
+
212
+ export interface Tokenizer {
213
+ next(): AST_Token;
214
+ peek(): AST_Token;
215
+ }
216
+
217
+ // -- Compiler Exports (from create_compiler) ----------------------------
218
+
219
+ export interface CompilerExports {
220
+ parse(code: string, options?: ParseOptions): AST_Toplevel;
221
+ OutputStream: OutputStreamConstructor;
222
+ tree_shake(ast: AST_Toplevel, context: TreeShakeContext): void;
223
+ tokenizer(code: string, filename?: string): Tokenizer;
224
+ string_template(template: string, vars: Record<string, unknown>): string;
225
+
226
+ DefaultsError: DefaultsErrorConstructor;
227
+ SyntaxError: RapydScriptSyntaxErrorConstructor;
228
+ ImportError: ImportErrorConstructor;
229
+
230
+ ALL_KEYWORDS: string[];
231
+ IDENTIFIER_PAT: RegExp;
232
+ NATIVE_CLASSES: Record<string, Record<string, unknown>>;
233
+ compile_time_decorators: Record<string, unknown>;
234
+
235
+ // All AST node constructors are exported as AST_* properties
236
+ AST_Node: new (...args: unknown[]) => AST_Node;
237
+ AST_Token: new (...args: unknown[]) => AST_Token;
238
+ AST_Toplevel: new (...args: unknown[]) => AST_Toplevel;
239
+ AST_Statement: new (...args: unknown[]) => AST_Node;
240
+ AST_Debugger: new (...args: unknown[]) => AST_Node;
241
+ AST_Directive: new (...args: unknown[]) => AST_Node;
242
+ AST_SimpleStatement: new (...args: unknown[]) => AST_Node;
243
+ AST_AnnotatedAssign: new (...args: unknown[]) => AST_Node;
244
+ AST_Assert: new (...args: unknown[]) => AST_Node;
245
+ AST_Block: new (...args: unknown[]) => AST_Node;
246
+ AST_BlockStatement: new (...args: unknown[]) => AST_Node;
247
+ AST_EmptyStatement: new (...args: unknown[]) => AST_Node;
248
+ AST_StatementWithBody: new (...args: unknown[]) => AST_Node;
249
+ AST_DWLoop: new (...args: unknown[]) => AST_Node;
250
+ AST_Do: new (...args: unknown[]) => AST_Node;
251
+ AST_While: new (...args: unknown[]) => AST_Node;
252
+ AST_ForIn: new (...args: unknown[]) => AST_Node;
253
+ AST_ForJS: new (...args: unknown[]) => AST_Node;
254
+ AST_ListComprehension: new (...args: unknown[]) => AST_Node;
255
+ AST_SetComprehension: new (...args: unknown[]) => AST_Node;
256
+ AST_DictComprehension: new (...args: unknown[]) => AST_Node;
257
+ AST_GeneratorComprehension: new (...args: unknown[]) => AST_Node;
258
+ AST_With: new (...args: unknown[]) => AST_Node;
259
+ AST_WithClause: new (...args: unknown[]) => AST_Node;
260
+ AST_Match: new (...args: unknown[]) => AST_Node;
261
+ AST_MatchPattern: new (...args: unknown[]) => AST_Node;
262
+ AST_MatchWildcard: new (...args: unknown[]) => AST_Node;
263
+ AST_MatchCapture: new (...args: unknown[]) => AST_Node;
264
+ AST_MatchLiteral: new (...args: unknown[]) => AST_Node;
265
+ AST_MatchOr: new (...args: unknown[]) => AST_Node;
266
+ AST_MatchAs: new (...args: unknown[]) => AST_Node;
267
+ AST_MatchStar: new (...args: unknown[]) => AST_Node;
268
+ AST_MatchSequence: new (...args: unknown[]) => AST_Node;
269
+ AST_MatchMapping: new (...args: unknown[]) => AST_Node;
270
+ AST_MatchClass: new (...args: unknown[]) => AST_Node;
271
+ AST_MatchCase: new (...args: unknown[]) => AST_Node;
272
+ AST_Scope: new (...args: unknown[]) => AST_Node;
273
+ AST_Import: new (...args: unknown[]) => AST_Node;
274
+ AST_Imports: new (...args: unknown[]) => AST_Node;
275
+ AST_Decorator: new (...args: unknown[]) => AST_Node;
276
+ AST_Lambda: new (...args: unknown[]) => AST_Node;
277
+ AST_Function: new (...args: unknown[]) => AST_Node;
278
+ AST_Class: new (...args: unknown[]) => AST_Node;
279
+ AST_Method: new (...args: unknown[]) => AST_Node;
280
+ AST_Jump: new (...args: unknown[]) => AST_Node;
281
+ AST_Exit: new (...args: unknown[]) => AST_Node;
282
+ AST_Return: new (...args: unknown[]) => AST_Node;
283
+ AST_Yield: new (...args: unknown[]) => AST_Node;
284
+ AST_Await: new (...args: unknown[]) => AST_Node;
285
+ AST_Throw: new (...args: unknown[]) => AST_Node;
286
+ AST_LoopControl: new (...args: unknown[]) => AST_Node;
287
+ AST_Break: new (...args: unknown[]) => AST_Node;
288
+ AST_Continue: new (...args: unknown[]) => AST_Node;
289
+ AST_If: new (...args: unknown[]) => AST_Node;
290
+ AST_Try: new (...args: unknown[]) => AST_Node;
291
+ AST_Catch: new (...args: unknown[]) => AST_Node;
292
+ AST_Except: new (...args: unknown[]) => AST_Node;
293
+ AST_Finally: new (...args: unknown[]) => AST_Node;
294
+ AST_Else: new (...args: unknown[]) => AST_Node;
295
+ AST_Definitions: new (...args: unknown[]) => AST_Node;
296
+ AST_Var: new (...args: unknown[]) => AST_Node;
297
+ AST_VarDef: new (...args: unknown[]) => AST_Node;
298
+ AST_BaseCall: new (...args: unknown[]) => AST_Node;
299
+ AST_Call: new (...args: unknown[]) => AST_Node;
300
+ AST_ClassCall: new (...args: unknown[]) => AST_Node;
301
+ AST_Super: new (...args: unknown[]) => AST_Node;
302
+ AST_New: new (...args: unknown[]) => AST_Node;
303
+ AST_Seq: new (...args: unknown[]) => AST_Node;
304
+ AST_PropAccess: new (...args: unknown[]) => AST_Node;
305
+ AST_Dot: new (...args: unknown[]) => AST_Node;
306
+ AST_Sub: new (...args: unknown[]) => AST_Node;
307
+ AST_ItemAccess: new (...args: unknown[]) => AST_Node;
308
+ AST_Splice: new (...args: unknown[]) => AST_Node;
309
+ AST_Unary: new (...args: unknown[]) => AST_Node;
310
+ AST_UnaryPrefix: new (...args: unknown[]) => AST_Node;
311
+ AST_Binary: new (...args: unknown[]) => AST_Node;
312
+ AST_Existential: new (...args: unknown[]) => AST_Node;
313
+ AST_Conditional: new (...args: unknown[]) => AST_Node;
314
+ AST_Assign: new (...args: unknown[]) => AST_Node;
315
+ AST_NamedExpr: new (...args: unknown[]) => AST_Node;
316
+ AST_Starred: new (...args: unknown[]) => AST_Node;
317
+ AST_Array: new (...args: unknown[]) => AST_Node;
318
+ AST_Object: new (...args: unknown[]) => AST_Node;
319
+ AST_ExpressiveObject: new (...args: unknown[]) => AST_Node;
320
+ AST_ObjectProperty: new (...args: unknown[]) => AST_Node;
321
+ AST_ObjectKeyVal: new (...args: unknown[]) => AST_Node;
322
+ AST_ObjectSpread: new (...args: unknown[]) => AST_Node;
323
+ AST_Spread: new (...args: unknown[]) => AST_Node;
324
+ AST_Set: new (...args: unknown[]) => AST_Node;
325
+ AST_SetItem: new (...args: unknown[]) => AST_Node;
326
+ AST_Symbol: new (...args: unknown[]) => AST_Node;
327
+ AST_SymbolAlias: new (...args: unknown[]) => AST_Node;
328
+ AST_SymbolDeclaration: new (...args: unknown[]) => AST_Node;
329
+ AST_SymbolVar: new (...args: unknown[]) => AST_Node;
330
+ AST_ImportedVar: new (...args: unknown[]) => AST_Node;
331
+ AST_SymbolNonlocal: new (...args: unknown[]) => AST_Node;
332
+ AST_SymbolFunarg: new (...args: unknown[]) => AST_Node;
333
+ AST_SymbolDefun: new (...args: unknown[]) => AST_Node;
334
+ AST_SymbolLambda: new (...args: unknown[]) => AST_Node;
335
+ AST_SymbolCatch: new (...args: unknown[]) => AST_Node;
336
+ AST_SymbolRef: new (...args: unknown[]) => AST_Node;
337
+ AST_This: new (...args: unknown[]) => AST_Node;
338
+ AST_Constant: new (...args: unknown[]) => AST_Node;
339
+ AST_String: new (...args: unknown[]) => AST_Node;
340
+ AST_Verbatim: new (...args: unknown[]) => AST_Node;
341
+ AST_Number: new (...args: unknown[]) => AST_Node;
342
+ AST_RegExp: new (...args: unknown[]) => AST_Node;
343
+ AST_Atom: new (...args: unknown[]) => AST_Node;
344
+ AST_Null: new (...args: unknown[]) => AST_Node;
345
+ AST_Ellipsis: new (...args: unknown[]) => AST_Node;
346
+ AST_NaN: new (...args: unknown[]) => AST_Node;
347
+ AST_Undefined: new (...args: unknown[]) => AST_Node;
348
+ AST_Hole: new (...args: unknown[]) => AST_Node;
349
+ AST_Infinity: new (...args: unknown[]) => AST_Node;
350
+ AST_Boolean: new (...args: unknown[]) => AST_Node;
351
+ AST_False: new (...args: unknown[]) => AST_Node;
352
+ AST_True: new (...args: unknown[]) => AST_Node;
353
+ AST_JSXElement: new (...args: unknown[]) => AST_Node;
354
+ AST_JSXFragment: new (...args: unknown[]) => AST_Node;
355
+ AST_JSXAttribute: new (...args: unknown[]) => AST_Node;
356
+ AST_JSXSpread: new (...args: unknown[]) => AST_Node;
357
+ AST_JSXText: new (...args: unknown[]) => AST_Node;
358
+ AST_JSXExprContainer: new (...args: unknown[]) => AST_Node;
359
+
360
+ [key: `AST_${string}`]: new (...args: unknown[]) => AST_Node;
361
+ }
362
+
363
+ // -- Module Exports -----------------------------------------------------
364
+
365
+ export declare function create_compiler(): CompilerExports;
366
+ export declare function set_virtual_files(vf: Record<string, string>): void;
367
+ export declare function clear_virtual_files(): void;
@@ -1,131 +1,131 @@
1
- /* vim:fileencoding=utf-8
2
- *
3
- * Copyright (C) 2016 Kovid Goyal <kovid at kovidgoyal.net>
4
- *
5
- * Distributed under terms of the BSD license
6
- */
7
-
8
-
9
- module.exports = function(compiler, options) {
10
- "use strict";
11
- var all_keywords = compiler.ALL_KEYWORDS.split(' ');
12
- var vm = require('vm');
13
- options = options || {};
14
- if (!options.enum_global) options.enum_global = "var global = Function('return this')(); Object.getOwnPropertyNames(global);";
15
-
16
- function global_names(ctx) {
17
- try {
18
- var ans = vm.runInContext(options.enum_global, ctx);
19
- ans = ans.concat(all_keywords);
20
- ans.sort();
21
- var seen = {};
22
- ans.filter(function (item) {
23
- if (Object.prototype.hasOwnProperty.call(seen, item)) return false;
24
- seen[item] = true;
25
- return true;
26
- });
27
- return ans;
28
- } catch(e) {
29
- console.log(e.stack || e.toString());
30
- }
31
- return [];
32
- }
33
-
34
- function object_names(obj, prefix) {
35
- if (obj === null || obj === undefined) return [];
36
- var groups = [], prefix_len = prefix.length, p;
37
-
38
- function prefix_filter(name) { return (prefix_len) ? (name.substr(0, prefix_len) === prefix) : true; }
39
-
40
- function add(o) {
41
- var items = Object.getOwnPropertyNames(o).filter(prefix_filter);
42
- if (items.length) groups.push(items);
43
- }
44
-
45
- if (typeof obj === 'object' || typeof obj === 'function') {
46
- add(obj);
47
- p = Object.getPrototypeOf(obj);
48
- } else p = obj.constructor ? obj.constructor.prototype : null;
49
-
50
- // Walk the prototype chain
51
- try {
52
- var sentinel = 5;
53
- while (p !== null && sentinel > 0) {
54
- add(p);
55
- p = Object.getPrototypeOf(p);
56
- // Circular refs possible? Let's guard against that.
57
- sentinel--;
58
- }
59
- } catch (e) {
60
- // console.error("completion error walking prototype chain:" + e);
61
- }
62
- if (!groups.length) return [];
63
- var seen = {}, ans = [];
64
- function uniq(name) {
65
- if (Object.prototype.hasOwnProperty.call(seen, name)) return false;
66
- seen[name] = true;
67
- return true;
68
- }
69
- for (var i = 0; i < groups.length; i++) {
70
- var group = groups[i];
71
- group.sort();
72
- ans = ans.concat(group.filter(uniq));
73
- ans.push(''); // group separator
74
-
75
- }
76
- while (ans.length && ans[ans.length - 1] === '') ans.pop();
77
- return ans;
78
- }
79
-
80
- function prefix_matches(prefix, items) {
81
- var len = prefix.length;
82
- var ans = items.filter(function(item) { return item.substr(0, len) === prefix; });
83
- ans.sort();
84
- return ans;
85
- }
86
-
87
- function find_completions(line, ctx) {
88
- var t;
89
- try {
90
- t = compiler.tokenizer(line, '<repl>');
91
- } catch(e) { return []; }
92
- var tokens = [], token;
93
- while (true) {
94
- try {
95
- token = t();
96
- } catch (e) { return []; }
97
- if (token.type === 'eof') break;
98
- if (token.type === 'punc' && '(){},;:'.indexOf(token.value) > -1)
99
- tokens = [];
100
- tokens.push(token);
101
- }
102
- if (!tokens.length) {
103
- // New line or trailing space
104
- return [global_names(ctx), ''];
105
- }
106
- var last_tok = tokens[tokens.length - 1];
107
- if (last_tok.value === '.' || (last_tok.type === 'name' && compiler.IDENTIFIER_PAT.test(last_tok.value))) {
108
- last_tok = last_tok.value;
109
- if (last_tok === '.') {
110
- tokens.push({'value':''});
111
- last_tok = '';
112
- }
113
- if (tokens.length > 1 && tokens[tokens.length - 2].value === '.') {
114
- // A compound expression
115
- var prefix = '', result;
116
- tokens.slice(0, tokens.length - 2).forEach(function (tok) { prefix += tok.value; });
117
- if (prefix) {
118
- try {
119
- result = vm.runInContext(prefix, ctx, {'displayErrors':false});
120
- } catch(e) { return []; }
121
- return [object_names(result, last_tok), last_tok];
122
- }
123
- } else {
124
- return [prefix_matches(last_tok, global_names(ctx)), last_tok];
125
- }
126
- }
127
- return [];
128
- }
129
-
130
- return find_completions;
131
- };
1
+ /* vim:fileencoding=utf-8
2
+ *
3
+ * Copyright (C) 2016 Kovid Goyal <kovid at kovidgoyal.net>
4
+ *
5
+ * Distributed under terms of the BSD license
6
+ */
7
+
8
+
9
+ module.exports = function(compiler, options) {
10
+ "use strict";
11
+ var all_keywords = compiler.ALL_KEYWORDS.split(' ');
12
+ var vm = require('vm');
13
+ options = options || {};
14
+ if (!options.enum_global) options.enum_global = "var global = Function('return this')(); Object.getOwnPropertyNames(global);";
15
+
16
+ function global_names(ctx) {
17
+ try {
18
+ var ans = vm.runInContext(options.enum_global, ctx);
19
+ ans = ans.concat(all_keywords);
20
+ ans.sort();
21
+ var seen = {};
22
+ ans.filter(function (item) {
23
+ if (Object.prototype.hasOwnProperty.call(seen, item)) return false;
24
+ seen[item] = true;
25
+ return true;
26
+ });
27
+ return ans;
28
+ } catch(e) {
29
+ console.log(e.stack || e.toString());
30
+ }
31
+ return [];
32
+ }
33
+
34
+ function object_names(obj, prefix) {
35
+ if (obj === null || obj === undefined) return [];
36
+ var groups = [], prefix_len = prefix.length, p;
37
+
38
+ function prefix_filter(name) { return (prefix_len) ? (name.substr(0, prefix_len) === prefix) : true; }
39
+
40
+ function add(o) {
41
+ var items = Object.getOwnPropertyNames(o).filter(prefix_filter);
42
+ if (items.length) groups.push(items);
43
+ }
44
+
45
+ if (typeof obj === 'object' || typeof obj === 'function') {
46
+ add(obj);
47
+ p = Object.getPrototypeOf(obj);
48
+ } else p = obj.constructor ? obj.constructor.prototype : null;
49
+
50
+ // Walk the prototype chain
51
+ try {
52
+ var sentinel = 5;
53
+ while (p !== null && sentinel > 0) {
54
+ add(p);
55
+ p = Object.getPrototypeOf(p);
56
+ // Circular refs possible? Let's guard against that.
57
+ sentinel--;
58
+ }
59
+ } catch (e) {
60
+ // console.error("completion error walking prototype chain:" + e);
61
+ }
62
+ if (!groups.length) return [];
63
+ var seen = {}, ans = [];
64
+ function uniq(name) {
65
+ if (Object.prototype.hasOwnProperty.call(seen, name)) return false;
66
+ seen[name] = true;
67
+ return true;
68
+ }
69
+ for (var i = 0; i < groups.length; i++) {
70
+ var group = groups[i];
71
+ group.sort();
72
+ ans = ans.concat(group.filter(uniq));
73
+ ans.push(''); // group separator
74
+
75
+ }
76
+ while (ans.length && ans[ans.length - 1] === '') ans.pop();
77
+ return ans;
78
+ }
79
+
80
+ function prefix_matches(prefix, items) {
81
+ var len = prefix.length;
82
+ var ans = items.filter(function(item) { return item.substr(0, len) === prefix; });
83
+ ans.sort();
84
+ return ans;
85
+ }
86
+
87
+ function find_completions(line, ctx) {
88
+ var t;
89
+ try {
90
+ t = compiler.tokenizer(line, '<repl>');
91
+ } catch(e) { return []; }
92
+ var tokens = [], token;
93
+ while (true) {
94
+ try {
95
+ token = t();
96
+ } catch (e) { return []; }
97
+ if (token.type === 'eof') break;
98
+ if (token.type === 'punc' && '(){},;:'.indexOf(token.value) > -1)
99
+ tokens = [];
100
+ tokens.push(token);
101
+ }
102
+ if (!tokens.length) {
103
+ // New line or trailing space
104
+ return [global_names(ctx), ''];
105
+ }
106
+ var last_tok = tokens[tokens.length - 1];
107
+ if (last_tok.value === '.' || (last_tok.type === 'name' && compiler.IDENTIFIER_PAT.test(last_tok.value))) {
108
+ last_tok = last_tok.value;
109
+ if (last_tok === '.') {
110
+ tokens.push({'value':''});
111
+ last_tok = '';
112
+ }
113
+ if (tokens.length > 1 && tokens[tokens.length - 2].value === '.') {
114
+ // A compound expression
115
+ var prefix = '', result;
116
+ tokens.slice(0, tokens.length - 2).forEach(function (tok) { prefix += tok.value; });
117
+ if (prefix) {
118
+ try {
119
+ result = vm.runInContext(prefix, ctx, {'displayErrors':false});
120
+ } catch(e) { return []; }
121
+ return [object_names(result, last_tok), last_tok];
122
+ }
123
+ } else {
124
+ return [prefix_matches(last_tok, global_names(ctx)), last_tok];
125
+ }
126
+ }
127
+ return [];
128
+ }
129
+
130
+ return find_completions;
131
+ };