Skip to content

Commit 0e2a0f9

Browse files
committed
pybricksMicropython/lib: replace parser
This replaces the Python parsing library. The python-ast package has really poor performance (many seconds to parse a 500 line file). The new package can parse a file about 100 times faster.
1 parent a673a45 commit 0e2a0f9

File tree

6 files changed

+40
-159
lines changed

6 files changed

+40
-159
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
## [Unreleased]
66

7+
### Fixed
8+
- Fixed UI temporary freeze when downloading and running programs ([support#750]).
9+
10+
[support#750]: https://github.com/pybricks/support/issues/750
11+
712
## [2.0.0-beta.6] - 2022-10-21
813

914
### Added

config/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,5 +802,6 @@ module.exports = function (webpackEnv) {
802802
// Turn off performance processing because we utilize
803803
// our own hints via the FileSizeReporter
804804
performance: false,
805+
ignoreWarnings: [/Failed to parse source map/],
805806
};
806807
};

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@pybricks/jedi": "^1.2.0",
1919
"@pybricks/mpy-cross-v5": "^2.0.0",
2020
"@pybricks/mpy-cross-v6": "^2.0.0",
21+
"@qoretechnologies/python-parser": "^0.4.10",
2122
"@reduxjs/toolkit": "^1.8.6",
2223
"@shopify/react-i18n": "^7.2.4",
2324
"@svgr/webpack": "^6.5.0",
@@ -38,7 +39,6 @@
3839
"@types/web-locks-api": "^0.0.2",
3940
"@types/wicg-file-system-access": "^2020.9.5",
4041
"@types/zen-push": "^0.1.1",
41-
"assert": "^2.0.0",
4242
"babel-jest": "^29.2.1",
4343
"babel-loader": "^8.2.3",
4444
"babel-plugin-macros": "^3.0.1",
@@ -86,7 +86,6 @@
8686
"prompts": "^2.4.2",
8787
"prop-types": "^15.8.1",
8888
"pyodide": "0.21.3",
89-
"python-ast": "^0.1.0",
9089
"react": "^16.13.1",
9190
"react-app-polyfill": "^3.0.0",
9291
"react-aria": "^3.20.0",
@@ -118,7 +117,6 @@
118117
"typescript": "~4.8.4",
119118
"usehooks-ts": "^2.9.1",
120119
"user-agent-data-types": "^0.3.0",
121-
"util": "^0.12.5",
122120
"web-vitals": "^3.0.4",
123121
"webpack": "^5.74.0",
124122
"webpack-dev-server": "^4.11.1",
@@ -169,7 +167,6 @@
169167
},
170168
"packageManager": "[email protected]",
171169
"resolutions": {
172-
"antlr4ts@^0.5.0-alpha.3": "0.5.0-alpha.4",
173170
"[email protected]": "patch:mq-polyfill@npm:1.1.8#.yarn/patches/mq-polyfill-npm-1.1.8-62fe162439.patch",
174171
"react-error-overlay": "6.0.9",
175172
"react-dev-utils@^12.0.1": "patch:react-dev-utils@npm:12.0.1#.yarn/patches/react-dev-utils-npm-12.0.1-83ba06e3ee.patch",

src/pybricksMicropython/lib.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ from .r import x
9696
from ..r import x
9797
from ...r import x
9898
from ....r import x
99+
100+
# import q
101+
# from q import q
102+
"""
103+
import q
104+
from q import q
105+
"""
106+
'''
107+
import q
108+
from q import q
109+
'''
99110
`;
100111

101112
const modules = findImportedModules(script);

src/pybricksMicropython/lib.ts

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
// Copyright (c) 2022 The Pybricks Authors
33

4-
import { parse, walk } from 'python-ast';
4+
import { parse, walk } from '@qoretechnologies/python-parser';
55
import type { FileContents, FileStorageDb } from '../fileStorage';
66

77
/** The Python file extension ('.py') */
@@ -82,47 +82,17 @@ export function findImportedModules(py: string): ReadonlySet<string> {
8282
const tree = parse(py);
8383

8484
// find all import statements in the syntax tree and collect imported modules
85-
walk(
86-
{
87-
enterImport_stmt: (ctx) => {
88-
// import statements have two forms:
89-
// import_stmt: import_name | import_from;
90-
91-
// import_name: 'import' dotted_as_names
92-
const name = ctx.import_name();
93-
94-
if (name) {
95-
// dotted_as_names: dotted_as_name (',' dotted_as_name)*;
96-
// dotted_as_name: dotted_name ('as' NAME)?;
97-
for (const dottedAsName of name
98-
.dotted_as_names()
99-
.dotted_as_name()) {
100-
// dotted_name: NAME ('.' NAME)*;
101-
modules.add(dottedAsName.dotted_name().text);
102-
}
85+
walk(tree, {
86+
onEnterNode(node, _ancestors) {
87+
if (node.type === 'import') {
88+
for (const name of node.names) {
89+
modules.add(name.path);
10390
}
104-
105-
// import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+) 'import' ('*' | '(' import_as_names ')' | import_as_names ));
106-
const from = ctx.import_from();
107-
108-
if (from) {
109-
// the leading dots aren't included in dotted_name, so
110-
// we need to collect them separately
111-
const leadingDots = from
112-
.DOT()
113-
.concat(from.ELLIPSIS())
114-
.map((n) => n.symbol.text)
115-
.join('');
116-
117-
// dotted_name: NAME ('.' NAME)*;
118-
const dottedName = from.dotted_name()?.text ?? '';
119-
120-
modules.add(leadingDots + dottedName);
121-
}
122-
},
91+
} else if (node.type === 'from') {
92+
modules.add(node.base);
93+
}
12394
},
124-
tree,
125-
);
95+
});
12696

12797
return modules;
12898
}

yarn.lock

Lines changed: 12 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,7 @@ __metadata:
24122412
"@pybricks/jedi": ^1.2.0
24132413
"@pybricks/mpy-cross-v5": ^2.0.0
24142414
"@pybricks/mpy-cross-v6": ^2.0.0
2415+
"@qoretechnologies/python-parser": ^0.4.10
24152416
"@reduxjs/toolkit": ^1.8.6
24162417
"@shopify/react-i18n": ^7.2.4
24172418
"@svgr/webpack": ^6.5.0
@@ -2434,7 +2435,6 @@ __metadata:
24342435
"@types/zen-push": ^0.1.1
24352436
"@typescript-eslint/eslint-plugin": ^5.40.1
24362437
"@typescript-eslint/parser": ^5.40.1
2437-
assert: ^2.0.0
24382438
babel-jest: ^29.2.1
24392439
babel-loader: ^8.2.3
24402440
babel-plugin-macros: ^3.0.1
@@ -2493,7 +2493,6 @@ __metadata:
24932493
prompts: ^2.4.2
24942494
prop-types: ^15.8.1
24952495
pyodide: 0.21.3
2496-
python-ast: ^0.1.0
24972496
react: ^16.13.1
24982497
react-app-polyfill: ^3.0.0
24992498
react-aria: ^3.20.0
@@ -2525,7 +2524,6 @@ __metadata:
25252524
typescript: ~4.8.4
25262525
usehooks-ts: ^2.9.1
25272526
user-agent-data-types: ^0.3.0
2528-
util: ^0.12.5
25292527
web-vitals: ^3.0.4
25302528
webpack: ^5.74.0
25312529
webpack-dev-server: ^4.11.1
@@ -2537,6 +2535,15 @@ __metadata:
25372535
languageName: unknown
25382536
linkType: soft
25392537

2538+
"@qoretechnologies/python-parser@npm:^0.4.10":
2539+
version: 0.4.10
2540+
resolution: "@qoretechnologies/python-parser@npm:0.4.10"
2541+
dependencies:
2542+
lodash: ^4.17.15
2543+
checksum: ab0d91719a8301b64f9c7fb1a7711721d52d78325abbf0c7c75940a48fd4a3d2b07456c95f8507af19faa333bf87116dd62f18e6c8e76486a57673e3224ac3ee
2544+
languageName: node
2545+
linkType: hard
2546+
25402547
"@react-aria/breadcrumbs@npm:^3.3.2":
25412548
version: 3.3.2
25422549
resolution: "@react-aria/breadcrumbs@npm:3.3.2"
@@ -5425,13 +5432,6 @@ __metadata:
54255432
languageName: node
54265433
linkType: hard
54275434

5428-
"antlr4ts@npm:0.5.0-alpha.4":
5429-
version: 0.5.0-alpha.4
5430-
resolution: "antlr4ts@npm:0.5.0-alpha.4"
5431-
checksum: 37948499d59477f5b5a8ea71dfb8b5330e71d5a7cee60f57351dd744219b8619fa6aac1a5b6ec1a9991846e8ddc9ca47680eb166c59b44333369b3115e7aa358
5432-
languageName: node
5433-
linkType: hard
5434-
54355435
"anymatch@npm:^3.0.3, anymatch@npm:~3.1.2":
54365436
version: 3.1.2
54375437
resolution: "anymatch@npm:3.1.2"
@@ -5580,18 +5580,6 @@ __metadata:
55805580
languageName: node
55815581
linkType: hard
55825582

5583-
"assert@npm:^2.0.0":
5584-
version: 2.0.0
5585-
resolution: "assert@npm:2.0.0"
5586-
dependencies:
5587-
es6-object-assign: ^1.1.0
5588-
is-nan: ^1.2.1
5589-
object-is: ^1.0.1
5590-
util: ^0.12.0
5591-
checksum: bb91f181a86d10588ee16c5e09c280f9811373974c29974cbe401987ea34e966699d7989a812b0e19377b511ea0bc627f5905647ce569311824848ede382cae8
5592-
languageName: node
5593-
linkType: hard
5594-
55955583
"ast-types-flow@npm:^0.0.7":
55965584
version: 0.0.7
55975585
resolution: "ast-types-flow@npm:0.0.7"
@@ -5645,13 +5633,6 @@ __metadata:
56455633
languageName: node
56465634
linkType: hard
56475635

5648-
"available-typed-arrays@npm:^1.0.5":
5649-
version: 1.0.5
5650-
resolution: "available-typed-arrays@npm:1.0.5"
5651-
checksum: 20eb47b3cefd7db027b9bbb993c658abd36d4edd3fe1060e83699a03ee275b0c9b216cc076ff3f2db29073225fb70e7613987af14269ac1fe2a19803ccc97f1a
5652-
languageName: node
5653-
linkType: hard
5654-
56555636
"axe-core@npm:^4.3.5":
56565637
version: 4.4.1
56575638
resolution: "axe-core@npm:4.4.1"
@@ -7500,7 +7481,7 @@ __metadata:
75007481
languageName: node
75017482
linkType: hard
75027483

7503-
"es-abstract@npm:^1.19.0, es-abstract@npm:^1.19.1, es-abstract@npm:^1.19.2, es-abstract@npm:^1.19.5, es-abstract@npm:^1.20.0":
7484+
"es-abstract@npm:^1.19.0, es-abstract@npm:^1.19.1, es-abstract@npm:^1.19.2, es-abstract@npm:^1.19.5":
75047485
version: 1.20.4
75057486
resolution: "es-abstract@npm:1.20.4"
75067487
dependencies:
@@ -7559,13 +7540,6 @@ __metadata:
75597540
languageName: node
75607541
linkType: hard
75617542

7562-
"es6-object-assign@npm:^1.1.0":
7563-
version: 1.1.0
7564-
resolution: "es6-object-assign@npm:1.1.0"
7565-
checksum: 8d4fdf63484d78b5c64cacc2c2e1165bc7b6a64b739d2a9db6a4dc8641d99cc9efb433cdd4dc3d3d6b00bfa6ce959694e4665e3255190339945c5f33b692b5d8
7566-
languageName: node
7567-
linkType: hard
7568-
75697543
"escalade@npm:^3.1.1":
75707544
version: 3.1.1
75717545
resolution: "escalade@npm:3.1.1"
@@ -8371,15 +8345,6 @@ __metadata:
83718345
languageName: node
83728346
linkType: hard
83738347

8374-
"for-each@npm:^0.3.3":
8375-
version: 0.3.3
8376-
resolution: "for-each@npm:0.3.3"
8377-
dependencies:
8378-
is-callable: ^1.1.3
8379-
checksum: 6c48ff2bc63362319c65e2edca4a8e1e3483a2fabc72fbe7feaf8c73db94fc7861bd53bc02c8a66a0c1dd709da6b04eec42e0abdd6b40ce47305ae92a25e5d28
8380-
languageName: node
8381-
linkType: hard
8382-
83838348
"fork-ts-checker-webpack-plugin@npm:^6.5.0":
83848349
version: 6.5.2
83858350
resolution: "fork-ts-checker-webpack-plugin@npm:6.5.2"
@@ -9291,7 +9256,7 @@ __metadata:
92919256
languageName: node
92929257
linkType: hard
92939258

9294-
"is-callable@npm:^1.1.3, is-callable@npm:^1.1.4, is-callable@npm:^1.2.7":
9259+
"is-callable@npm:^1.1.4, is-callable@npm:^1.2.7":
92959260
version: 1.2.7
92969261
resolution: "is-callable@npm:1.2.7"
92979262
checksum: 61fd57d03b0d984e2ed3720fb1c7a897827ea174bd44402878e059542ea8c4aeedee0ea0985998aa5cc2736b2fa6e271c08587addb5b3959ac52cf665173d1ac
@@ -9346,15 +9311,6 @@ __metadata:
93469311
languageName: node
93479312
linkType: hard
93489313

9349-
"is-generator-function@npm:^1.0.7":
9350-
version: 1.0.10
9351-
resolution: "is-generator-function@npm:1.0.10"
9352-
dependencies:
9353-
has-tostringtag: ^1.0.0
9354-
checksum: d54644e7dbaccef15ceb1e5d91d680eb5068c9ee9f9eb0a9e04173eb5542c9b51b5ab52c5537f5703e48d5fddfd376817c1ca07a84a407b7115b769d4bdde72b
9355-
languageName: node
9356-
linkType: hard
9357-
93589314
"is-glob@npm:^4.0.0, is-glob@npm:^4.0.1, is-glob@npm:^4.0.3, is-glob@npm:~4.0.1":
93599315
version: 4.0.3
93609316
resolution: "is-glob@npm:4.0.3"
@@ -9392,16 +9348,6 @@ __metadata:
93929348
languageName: node
93939349
linkType: hard
93949350

9395-
"is-nan@npm:^1.2.1":
9396-
version: 1.3.2
9397-
resolution: "is-nan@npm:1.3.2"
9398-
dependencies:
9399-
call-bind: ^1.0.0
9400-
define-properties: ^1.1.3
9401-
checksum: 5dfadcef6ad12d3029d43643d9800adbba21cf3ce2ec849f734b0e14ee8da4070d82b15fdb35138716d02587c6578225b9a22779cab34888a139cc43e4e3610a
9402-
languageName: node
9403-
linkType: hard
9404-
94059351
"is-negative-zero@npm:^2.0.2":
94069352
version: 2.0.2
94079353
resolution: "is-negative-zero@npm:2.0.2"
@@ -9518,19 +9464,6 @@ __metadata:
95189464
languageName: node
95199465
linkType: hard
95209466

9521-
"is-typed-array@npm:^1.1.3, is-typed-array@npm:^1.1.9":
9522-
version: 1.1.9
9523-
resolution: "is-typed-array@npm:1.1.9"
9524-
dependencies:
9525-
available-typed-arrays: ^1.0.5
9526-
call-bind: ^1.0.2
9527-
es-abstract: ^1.20.0
9528-
for-each: ^0.3.3
9529-
has-tostringtag: ^1.0.0
9530-
checksum: 11910f1e58755fef43bf0074e52fa5b932bf101ec65d613e0a83d40e8e4c6e3f2ee142d624ebc7624c091d3bbe921131f8db7d36ecbbb71909f2fe310c1faa65
9531-
languageName: node
9532-
linkType: hard
9533-
95349467
"is-weakref@npm:^1.0.2":
95359468
version: 1.0.2
95369469
resolution: "is-weakref@npm:1.0.2"
@@ -12645,15 +12578,6 @@ __metadata:
1264512578
languageName: node
1264612579
linkType: hard
1264712580

12648-
"python-ast@npm:^0.1.0":
12649-
version: 0.1.0
12650-
resolution: "python-ast@npm:0.1.0"
12651-
dependencies:
12652-
antlr4ts: ^0.5.0-alpha.3
12653-
checksum: 1fc453e89f932a1e7f6f57a0f9dc304252093e8aa6c934a426ef30c7e2c9fd0c05aad168fa3ad053ba3bb88362a2fb5097c6e9d360351539f5027ff2af0e5eab
12654-
languageName: node
12655-
linkType: hard
12656-
1265712581
"qs@npm:6.10.3":
1265812582
version: 6.10.3
1265912583
resolution: "qs@npm:6.10.3"
@@ -15032,19 +14956,6 @@ __metadata:
1503214956
languageName: node
1503314957
linkType: hard
1503414958

15035-
"util@npm:^0.12.0, util@npm:^0.12.5":
15036-
version: 0.12.5
15037-
resolution: "util@npm:0.12.5"
15038-
dependencies:
15039-
inherits: ^2.0.3
15040-
is-arguments: ^1.0.4
15041-
is-generator-function: ^1.0.7
15042-
is-typed-array: ^1.1.3
15043-
which-typed-array: ^1.1.2
15044-
checksum: 705e51f0de5b446f4edec10739752ac25856541e0254ea1e7e45e5b9f9b0cb105bc4bd415736a6210edc68245a7f903bf085ffb08dd7deb8a0e847f60538a38a
15045-
languageName: node
15046-
linkType: hard
15047-
1504814959
"utila@npm:~0.4":
1504914960
version: 0.4.0
1505014961
resolution: "utila@npm:0.4.0"
@@ -15407,20 +15318,6 @@ __metadata:
1540715318
languageName: node
1540815319
linkType: hard
1540915320

15410-
"which-typed-array@npm:^1.1.2":
15411-
version: 1.1.8
15412-
resolution: "which-typed-array@npm:1.1.8"
15413-
dependencies:
15414-
available-typed-arrays: ^1.0.5
15415-
call-bind: ^1.0.2
15416-
es-abstract: ^1.20.0
15417-
for-each: ^0.3.3
15418-
has-tostringtag: ^1.0.0
15419-
is-typed-array: ^1.1.9
15420-
checksum: bedf4d30a738e848404fe67fe0ace33433a7298cf3f5a4d4b2c624ba99c4d25f06a7fd6f3566c3d16af5f8a54f0c6293cbfded5b1208ce11812753990223b45a
15421-
languageName: node
15422-
linkType: hard
15423-
1542415321
"which@npm:^1.3.1":
1542515322
version: 1.3.1
1542615323
resolution: "which@npm:1.3.1"

0 commit comments

Comments
 (0)