-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathpybricksMicroPython.ts
More file actions
541 lines (514 loc) · 14.2 KB
/
Copy pathpybricksMicroPython.ts
File metadata and controls
541 lines (514 loc) · 14.2 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// Copied from https://github.com/microsoft/monaco-languages/blob/d7cc098c481059f63d51ce3753975c8ca8ab6030/src/python/python.ts
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type * as monaco from 'monaco-editor';
/** The Pybricks MicroPython language identifier. */
export const pybricksMicroPythonId = 'pybricks-micropython';
export const conf: monaco.languages.LanguageConfiguration = {
comments: {
lineComment: '#',
blockComment: ["'''", "'''"],
},
brackets: [
['{', '}'],
['[', ']'],
['(', ')'],
],
autoClosingPairs: [
{ open: '{', close: '}' },
{ open: '[', close: ']' },
{ open: '(', close: ')' },
{ open: '"', close: '"', notIn: ['string'] },
{ open: "'", close: "'", notIn: ['string', 'comment'] },
],
surroundingPairs: [
{ open: '{', close: '}' },
{ open: '[', close: ']' },
{ open: '(', close: ')' },
{ open: '"', close: '"' },
{ open: "'", close: "'" },
],
onEnterRules: [
{
beforeText: new RegExp(
'^\\s*(?:def|class|for|if|elif|else|while|try|with|finally|except|async).*?:\\s*$',
),
action: { indentAction: <monaco.languages.IndentAction.Indent>1 },
},
],
folding: {
offSide: true,
markers: {
start: new RegExp('^\\s*#region\\b'),
end: new RegExp('^\\s*#endregion\\b'),
},
},
};
export const language = <monaco.languages.IMonarchLanguage>{
defaultToken: '',
tokenPostfix: '.python',
// https://docs.python.org/3/reference/lexical_analysis.html#keywords
keywords: <ReadonlyArray<string>>[
'False',
'None',
'True',
'and',
'as',
'assert',
'async',
'await',
'break',
'class',
'continue',
'def',
'del',
'elif',
'else',
'except',
'finally',
'for',
'from',
'global',
'if',
'import',
'in',
'is',
'lambda',
'nonlocal',
'not',
'or',
'pass',
'raise',
'return',
'try',
'while',
'with',
'yield',
],
// https://docs.python.org/3/library/functions.html#built-in-funcs
builtins: <ReadonlyArray<string>>[
'abs',
'all',
'any',
'ascii',
'bin',
'bool',
'breakpoint',
'bytearray',
'bytes',
'callable',
'chr',
'classmethod',
'compile',
'complex',
'delattr',
'dict',
'dir',
'divmod',
'enumerate',
'eval',
'exec',
'filter',
'float',
'format',
'frozenset',
'getattr',
'globals',
'hasattr',
'hash',
'help',
'hex',
'id',
'input',
'int',
'isinstance',
'issubclass',
'iter',
'len',
'list',
'locals',
'map',
'max',
'memoryview',
'min',
'next',
'object',
'oct',
'open',
'ord',
'pow',
'print',
'property',
'reversed',
'range',
'repr',
'reversed',
'round',
'self',
'set',
'setattr',
'slice',
'sorted',
'staticmethod',
'str',
'sum',
'super',
'tuple',
'type',
'vars',
'zip',
'__import__',
],
brackets: [
{ open: '{', close: '}', token: 'delimiter.curly' },
{ open: '[', close: ']', token: 'delimiter.bracket' },
{ open: '(', close: ')', token: 'delimiter.parenthesis' },
],
tokenizer: {
root: [
{ include: '@whitespace' },
{ include: '@numbers' },
{ include: '@strings' },
[/@[a-zA-Z_]\w*/, 'tag'],
[/\.\.\./, 'keyword'],
[/->/, 'delimiter'],
{ include: '@operators' },
[/[,:.;=]/, 'delimiter'],
[/[{}[\]()]/, '@brackets'],
[
/[a-zA-Z_]\w*/,
{
cases: {
'@keywords': 'keyword',
'@builtins': 'support.function',
'[A-Z_]+': 'support.constant',
'@default': 'identifier',
},
},
],
],
// Deal with white space, including single and multi-line comments
whitespace: [
[/\s+/, 'white'],
[/(^#.*$)/, 'comment'],
[/'''/, 'string', '@endDocString'],
[/"""/, 'string', '@endDblDocString'],
],
endDocString: [
[/[^']+/, 'string'],
[/\\'/, 'string'],
[/'''/, 'string', '@popall'],
[/'/, 'string'],
],
endDblDocString: [
[/[^"]+/, 'string'],
[/\\"/, 'string'],
[/"""/, 'string', '@popall'],
[/"/, 'string'],
],
// Recognize binary, octal, hex, decimals, imaginary, and scientific notation
numbers: [
[/\b0[bB](0|1|_)+/, 'constant.numeric.bin'],
[/\b0[oO]([0-7]|_)+/, 'constant.numeric.oct'],
[/\b0[xX]([abcdef]|[ABCDEF]|\d|_)+/, 'constant.numeric.hex'],
[/\b(\d[\d_]*\.|\.)?\d[\d_]*([eE][+-]?[\d_]+)?[jJ]?/, 'constant.numeric'],
],
// Recognize strings, including those broken across lines with \ (but not without)
strings: [
[/'$/, 'string.escape', '@popall'],
[/([fF]|[fF][rR]|[rR][fF])?'/, 'string.escape', '@fStringBody'],
[/[rRuUbB]?'/, 'string.escape', '@stringBody'],
[/"$/, 'string.escape', '@popall'],
[/([fF]|[fF][rR]|[rR][fF])?"/, 'string.escape', '@dblFStringBody'],
[/[rRuUbB]?"/, 'string.escape', '@dblStringBody'],
],
fStringBody: [
[/([^\\'{}]|\{\{|\}\}(?!\}[^}]))+$/, 'string', '@popall'],
[/([^\\'{}]|\{\{|\}\}(?!\}[^}]))+/, 'string'],
[/\\./, 'string'],
[/'/, 'string.escape', '@popall'],
[/\\$/, 'string'],
[
/\{/,
{
token: 'delimiter.curly',
next: '@fStringReplacement',
nextEmbedded: pybricksMicroPythonId,
},
],
[/\}/, 'delimiter.curly'],
],
dblFStringBody: [
[/([^\\"{}]|\{\{|\}\}(?!\}[^}]))+$/, 'string', '@popall'],
[/([^\\"{}]|\{\{|\}\}(?!\}[^}]))+/, 'string'],
[/\\./, 'string'],
[/"/, 'string.escape', '@popall'],
[/\\$/, 'string'],
[
/\{/,
{
token: 'delimiter.curly',
next: '@fStringReplacement',
nextEmbedded: pybricksMicroPythonId,
},
],
[/\}/, 'delimiter.curly'],
],
stringBody: [
[/[^\\']+$/, 'string', '@popall'],
[/[^\\']+/, 'string'],
[/\\./, 'string'],
[/'/, 'string.escape', '@popall'],
[/\\$/, 'string'],
],
dblStringBody: [
[/[^\\"]+$/, 'string', '@popall'],
[/[^\\"]+/, 'string'],
[/\\./, 'string'],
[/"/, 'string.escape', '@popall'],
[/\\$/, 'string'],
],
fStringReplacement: [
[/\}/, { token: '@rematch', next: '@pop', nextEmbedded: '@pop' }],
],
operators: [
[
/(\*\*|\/\/|<<|>>|:=|<=|>=|==|!=|\+=|-=|\*-|\/=|\.\.=|%=|@=|&=|\|=|\^=|>>=|<<=|\*\*=|[+\-*/%@&|^~<>])/,
'keyword.operator',
],
],
attributes: [
[/\b/, '@pop'],
[/[a-zA-Z_]\w*/, 'attribute'],
],
},
};
/**
* Creates a new template for the given parameters.
*
* @param hubClassName The hub class name, e.g. `"MoveHub"`.
* @param deviceClassNames A list of device class names, e.g. `["Motor"]`.
* @returns The template.
*/
function createTemplate(hubClassName: string, deviceClassNames: string[]): string {
return `from pybricks.hubs import ${hubClassName}
from pybricks.${
hubClassName === 'EV3Brick' ? 'ev3devices' : 'pupdevices'
} import ${deviceClassNames.join(', ')}
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.robotics import DriveBase
from pybricks.tools import wait, StopWatch
hub = ${hubClassName}()
`;
}
type HubLabel =
| 'movehub'
| 'cityhub'
| 'technichub'
| 'inventorhub'
| 'primehub'
| 'essentialhub'
| 'ev3';
const templateSnippets: Array<
Required<
Pick<monaco.languages.CompletionItem, 'label' | 'documentation' | 'insertText'>
> & { label: HubLabel }
> = [
{
label: 'technichub',
documentation: 'Template for Technic hub program.',
insertText: createTemplate('TechnicHub', ['Motor']),
},
{
label: 'cityhub',
documentation: 'Template for City hub program.',
insertText: createTemplate('CityHub', ['DCMotor', 'Light']),
},
{
label: 'movehub',
documentation: 'Template for BOOST Move hub program.',
insertText: createTemplate('MoveHub', ['Motor', 'ColorDistanceSensor']),
},
{
label: 'inventorhub',
documentation: 'Template for MINDSTORMS Robot Inventor hub program.',
insertText: createTemplate('InventorHub', [
'Motor',
'ColorSensor',
'UltrasonicSensor',
]),
},
{
label: 'primehub',
documentation: 'Template for SPIKE Prime program.',
insertText: createTemplate('PrimeHub', [
'Motor',
'ColorSensor',
'UltrasonicSensor',
'ForceSensor',
]),
},
{
label: 'essentialhub',
documentation: 'Template for SPIKE Essential program.',
insertText: createTemplate('EssentialHub', [
'Motor',
'ColorSensor',
'ColorLightMatrix',
]),
},
{
label: 'ev3',
documentation: 'Template for MINDSTORMS EV3 program.',
insertText: createTemplate('EV3Brick', [
'Motor',
'ColorSensor',
'GyroSensor',
'InfraredSensor',
'TouchSensor',
'UltrasonicSensor',
]),
},
];
/**
* Gets the template text for a Pybricks MicroPython file.
* @param hub The hub label.
*/
export function getPybricksMicroPythonFileTemplate(
hub: HubLabel | undefined,
): string | undefined {
return templateSnippets.find((t) => t.label === hub)?.insertText;
}
export const templateSnippetCompletions = <monaco.languages.CompletionItemProvider>{
provideCompletionItems: (model, position, _context, _token) => {
// templates snippets are only available on the first line
if (position.lineNumber !== 1) {
return undefined;
}
const range = {
startLineNumber: position.lineNumber,
startColumn: 1,
endLineNumber: position.lineNumber,
endColumn: position.column,
};
const textUntilPosition = model.getValueInRange(range);
const items = templateSnippets
.filter((x) => x.label.startsWith(textUntilPosition))
.map<monaco.languages.CompletionItem>((x) => ({
detail: x.insertText,
kind: <monaco.languages.CompletionItemKind.Snippet>27,
range,
...x,
}));
if (!items) {
return undefined;
}
return { suggestions: items };
},
};
// old snippets from ace editor
// eslint-disable-next-line
const _unused = `
snippet imp
import \${1:module}
snippet from
from \${1:package} import \${2:module}
# Module Docstring
snippet docs
'''
File: \${1:FILENAME:file_name}
Author: \${2:author}
Date: \${3:date}
Description: \${4}
'''
snippet wh
while \${1:condition}:
\${2:# TODO: write code...}
# dowh - does the same as do...while in other languages
snippet dowh
while True:
\${1:# TODO: write code...}
if \${2:condition}:
break
snippet with
with \${1:expr} as \${2:var}:
\${3:# TODO: write code...}
# New Class
snippet cl
class \${1:ClassName}(\${2:object}):
"""\${3:docstring for $1}"""
def __init__(self, \${4:arg}):
\${5:super($1, self).__init__()}
self.$4 = $4
\${6}
# New Function
snippet def
def \${1:fname}(\${2:\`indent('.') ? 'self' : ''\`}):
"""\${3:docstring for $1}"""
\${4:# TODO: write code...}
snippet deff
def \${1:fname}(\${2:\`indent('.') ? 'self' : ''\`}):
\${3:# TODO: write code...}
# New Method
snippet defs
def \${1:mname}(self, \${2:arg}):
\${3:# TODO: write code...}
# New Property
snippet property
@property
def \${1:pname}():
\${2:return self._$1}
# Ifs
snippet if
if \${1:condition}:
\${2:# TODO: write code...}
snippet el
else:
\${1:# TODO: write code...}
snippet ei
elif \${1:condition}:
\${2:# TODO: write code...}
# For
snippet for
for \${1:item} in \${2:items}:
\${3:# TODO: write code...}
# Lambda
snippet ld
\${1:var} = lambda \${2:vars} : \${3:action}
snippet .
self.
snippet try Try/Except
try:
\${1:# TODO: write code...}
except \${2:Exception} as \${3:e}:
\${4:raise $3}
snippet try Try/Except/Else
try:
\${1:# TODO: write code...}
except \${2:Exception} as \${3:e}:
\${4:raise $3}
else:
\${5:# TODO: write code...}
snippet try Try/Except/Finally
try:
\${1:# TODO: write code...}
except \${2:Exception} as \${3:e}:
\${4:raise $3}
finally:
\${5:# TODO: write code...}
snippet try Try/Except/Else/Finally
try:
\${1:# TODO: write code...}
except \${2:Exception} as \${3:e}:
\${4:raise $3}
else:
\${5:# TODO: write code...}
finally:
\${6:# TODO: write code...}
snippet "
"""
\${1:doc}
"""
`;