From d4c4f08a6f1bb2e90c65986d898c398349b69074 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Wed, 7 Jun 2023 02:54:00 +0900 Subject: [PATCH] trial --- Cargo.lock | 401 +++++++++++++++++++++++++++----------- Cargo.toml | 10 +- crates/py2erg/Cargo.toml | 1 - crates/py2erg/ast_util.rs | 19 +- crates/py2erg/convert.rs | 370 ++++++++++++++++++++--------------- src/analyze.rs | 2 +- 6 files changed, 504 insertions(+), 299 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 064389d..cb84057 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,12 +61,34 @@ dependencies = [ "nix", ] +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "block-padding", + "generic-array", +] + +[[package]] +name = "block-padding" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" + [[package]] name = "cc" version = "1.0.79" @@ -79,12 +101,49 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + +[[package]] +name = "cpufeatures" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e4c1eaa2012c47becbbad2ab175484c2a84d1185b566fb2cc5b8707343dfe58" +dependencies = [ + "libc", +] + [[package]] name = "crunchy" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn 1.0.109", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + [[package]] name = "either" version = "1.8.1" @@ -93,9 +152,9 @@ checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" [[package]] name = "els" -version = "0.1.26-nightly.0" +version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cf50f787a8376384af8e7b074c489329669eeb1264f15f1a7f136102a003f2f" +checksum = "ad69bdb783d8f54317278b3d32b138b05fe0708c9a22454cd9505f39edf4d044" dependencies = [ "erg_common", "erg_compiler", @@ -105,22 +164,31 @@ dependencies = [ ] [[package]] -name = "erg_common" -version = "0.6.14-nightly.0" +name = "embed-doc-image" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0d10554accd6de5e3f41e3db5931348320c89b9745a82635887f850dd77aadb" +checksum = "af36f591236d9d822425cb6896595658fa558fcebf5ee8accac1d4b92c47166e" +dependencies = [ + "base64", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "erg_common" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abef65c68abb0bbf94d43bdedddc44349ff02f8a21b510dedf66f4be24587a0c" dependencies = [ "backtrace-on-stack-overflow", - "hermit-abi", - "libc", - "winapi", ] [[package]] name = "erg_compiler" -version = "0.6.14-nightly.0" +version = "0.6.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3570aa70c9f488516c6286280ae4d3343aed382b6c7881b784195af64350aa50" +checksum = "09c82d01efbb07c5da0d5ee35ce6fc053e09512dca0275136e5cf2c07bd7ee43" dependencies = [ "erg_common", "erg_parser", @@ -128,9 +196,9 @@ dependencies = [ [[package]] name = "erg_parser" -version = "0.6.14-nightly.0" +version = "0.6.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfd44c03719e8051c7997263298e2b792da6138f818646d0eeae359f92bf0c60" +checksum = "b572d85a2a33d729a8dd03a655ee1c36b9e86f4e85deb22f9043542e4b8fb41f" dependencies = [ "erg_common", "unicode-xid", @@ -145,27 +213,33 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "gimli" version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad0a93d233ebf96623465aad4046a8d3aa4da22d4f4beba5388838c8a434bbb4" -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - -[[package]] -name = "hexf-parse" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df" - [[package]] name = "idna" version = "0.3.0" @@ -189,6 +263,15 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "itertools" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.10.5" @@ -204,42 +287,21 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" +[[package]] +name = "keccak" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f6d5ed8676d904364de097082f4e7d240b571b67989ced0240f08b7f966f940" +dependencies = [ + "cpufeatures", +] + [[package]] name = "lalrpop-util" version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f35c735096c0293d313e8f2a641627472b83d01b937177fe76e5e2708d31e0d" -[[package]] -name = "lexical-parse-float" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "683b3a5ebd0130b8fb52ba0bdc718cc56815b6a097e28ae5a6997d0ad17dc05f" -dependencies = [ - "lexical-parse-integer", - "lexical-util", - "static_assertions", -] - -[[package]] -name = "lexical-parse-integer" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d0994485ed0c312f6d965766754ea177d07f9c00c9b82a5ee62ed5b47945ee9" -dependencies = [ - "lexical-util", - "static_assertions", -] - -[[package]] -name = "lexical-util" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5255b9ff16ff898710eb9eb63cb39248ea8a5bb036bea8085b1a767ff6c4e3fc" -dependencies = [ - "static_assertions", -] - [[package]] name = "libc" version = "0.2.144" @@ -269,10 +331,63 @@ dependencies = [ ] [[package]] -name = "matches" -version = "0.1.10" +name = "malachite" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5" +checksum = "f6cf7f4730c30071ba374fac86ad35b1cb7a0716f774737768667ea3fa1828e3" +dependencies = [ + "malachite-base", + "malachite-nz", + "malachite-q", +] + +[[package]] +name = "malachite-base" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b06bfa98a4b4802af5a4263b4ad4660e28e51e8490f6354eb9336c70767e1c5" +dependencies = [ + "itertools 0.9.0", + "rand 0.7.3", + "rand_chacha", + "ryu", + "sha3", +] + +[[package]] +name = "malachite-bigint" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a5110aee54537b0cef214efbebdd7df79b7408db8eef4f6a4b6db9d0d8fc01b" +dependencies = [ + "derive_more", + "malachite", + "num-integer", + "num-traits", + "paste", +] + +[[package]] +name = "malachite-nz" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e21c64b7af5be3dc8cef16f786243faf59459fe4ba93b44efdeb264e5ade4" +dependencies = [ + "embed-doc-image", + "itertools 0.9.0", + "malachite-base", +] + +[[package]] +name = "malachite-q" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3755e541d5134b5016594c9043094172c4dda9259b3ce824a7b8101941850360" +dependencies = [ + "itertools 0.9.0", + "malachite-base", + "malachite-nz", +] [[package]] name = "memchr" @@ -311,17 +426,6 @@ dependencies = [ "memoffset", ] -[[package]] -name = "num-bigint" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" -dependencies = [ - "autocfg", - "num-integer", - "num-traits", -] - [[package]] name = "num-integer" version = "0.1.45" @@ -356,6 +460,18 @@ version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + +[[package]] +name = "paste" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" + [[package]] name = "percent-encoding" version = "2.2.0" @@ -388,7 +504,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1181c94580fa345f50f19d738aaa39c0ed30a600d95cb2d3e23f94266f14fbf" dependencies = [ "phf_shared", - "rand", + "rand 0.8.5", ] [[package]] @@ -411,6 +527,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro2" version = "1.0.58" @@ -426,7 +548,6 @@ version = "0.0.29" dependencies = [ "erg_common", "erg_compiler", - "rustpython-ast", "rustpython-parser", ] @@ -450,13 +571,45 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core 0.5.1", + "rand_hc", +] + [[package]] name = "rand" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ - "rand_core", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core 0.5.1", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", ] [[package]] @@ -465,9 +618,19 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", +] + [[package]] name = "ruff_source_location" version = "0.0.0" +source = "git+https://github.com/RustPython/Parser#b2f95e284852cb25d6e510c127260d03573d9f0c" dependencies = [ "memchr", "once_cell", @@ -477,6 +640,7 @@ dependencies = [ [[package]] name = "ruff_text_size" version = "0.0.0" +source = "git+https://github.com/RustPython/Parser#b2f95e284852cb25d6e510c127260d03573d9f0c" [[package]] name = "rustc-demangle" @@ -490,38 +654,37 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + [[package]] name = "rustpython-ast" version = "0.2.0" +source = "git+https://github.com/RustPython/Parser#b2f95e284852cb25d6e510c127260d03573d9f0c" dependencies = [ "is-macro", - "num-bigint", - "rustpython-literal", + "malachite-bigint", "rustpython-parser-core", "static_assertions", ] -[[package]] -name = "rustpython-literal" -version = "0.2.0" -dependencies = [ - "hexf-parse", - "is-macro", - "lexical-parse-float", - "num-traits", - "unic-ucd-category", -] - [[package]] name = "rustpython-parser" version = "0.2.0" +source = "git+https://github.com/RustPython/Parser#b2f95e284852cb25d6e510c127260d03573d9f0c" dependencies = [ "anyhow", "is-macro", - "itertools", + "itertools 0.10.5", "lalrpop-util", "log", - "num-bigint", + "malachite-bigint", "num-traits", "phf", "phf_codegen", @@ -537,8 +700,10 @@ dependencies = [ [[package]] name = "rustpython-parser-core" version = "0.2.0" +source = "git+https://github.com/RustPython/Parser#b2f95e284852cb25d6e510c127260d03573d9f0c" dependencies = [ "is-macro", + "memchr", "ruff_source_location", "ruff_text_size", ] @@ -549,6 +714,12 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" +[[package]] +name = "semver" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" + [[package]] name = "serde" version = "1.0.163" @@ -591,6 +762,18 @@ dependencies = [ "syn 2.0.16", ] +[[package]] +name = "sha3" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81199417d4e5de3f04b1e871023acea7389672c4135918f05aa9cbf2f2fa809" +dependencies = [ + "block-buffer", + "digest", + "keccak", + "opaque-debug", +] + [[package]] name = "siphasher" version = "0.3.10" @@ -649,6 +832,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + [[package]] name = "unic-char-property" version = "0.9.0" @@ -681,18 +870,6 @@ dependencies = [ "unic-ucd-version", ] -[[package]] -name = "unic-ucd-category" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b8d4591f5fcfe1bd4453baaf803c40e1b1e69ff8455c47620440b46efef91c0" -dependencies = [ - "matches", - "unic-char-property", - "unic-char-range", - "unic-ucd-version", -] - [[package]] name = "unic-ucd-ident" version = "0.9.0" @@ -761,23 +938,13 @@ dependencies = [ ] [[package]] -name = "winapi" -version = "0.3.9" +name = "version_check" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" diff --git a/Cargo.toml b/Cargo.toml index c228898..8b7ed51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,12 +22,10 @@ edition = "2021" repository = "https://github.com/mtshiba/pylyzer" [workspace.dependencies] -erg_common = { version = "0.6.14-nightly.0", features = ["py_compat", "els"] } -erg_compiler = { version = "0.6.14-nightly.0", features = ["py_compat", "els"] } -els = { version = "0.1.26-nightly.0", features = ["py_compat"] } -# rustpython-parser = { git = "https://github.com/RustPython/Parser.git", rev = "4de0cb18277f45aecd1cfdcc5ac69ab6bd9e2854", features = ["rustpython-literal"] } -rustpython-ast = { path = "../RustPython-Parser/ast", features = ["rustpython-literal"] } -rustpython-parser = { path = "../RustPython-Parser/parser" } +erg_common = { version = "0.6.14", features = ["py_compat", "els"] } +erg_compiler = { version = "0.6.14", features = ["py_compat", "els"] } +els = { version = "0.1.26", features = ["py_compat"] } +rustpython-parser = { git = "https://github.com/RustPython/Parser", version = "0.2.0", features = ["all-nodes-with-ranges"] } # erg_compiler = { git = "https://github.com/erg-lang/erg", branch = "main", features = ["py_compat", "els"] } # erg_common = { git = "https://github.com/erg-lang/erg", branch = "main", features = ["py_compat", "els"] } # els = { git = "https://github.com/erg-lang/erg", branch = "main", features = ["py_compat"] } diff --git a/crates/py2erg/Cargo.toml b/crates/py2erg/Cargo.toml index 94461c3..77471b6 100644 --- a/crates/py2erg/Cargo.toml +++ b/crates/py2erg/Cargo.toml @@ -14,7 +14,6 @@ debug = ["erg_compiler/debug", "erg_common/debug"] [dependencies] rustpython-parser = { workspace = true } -rustpython-ast = { workspace = true } erg_common = { workspace = true } erg_compiler = { workspace = true } diff --git a/crates/py2erg/ast_util.rs b/crates/py2erg/ast_util.rs index d784758..5dec411 100644 --- a/crates/py2erg/ast_util.rs +++ b/crates/py2erg/ast_util.rs @@ -1,19 +1,10 @@ -use erg_common::log; -use rustpython_parser::ast::{Boolop, Cmpop, Constant, ExpressionType, Keyword, StringGroup}; +use rustpython_parser::ast::Expr; -pub fn number_to_string(num: &Constant) -> String { - num.to_string() -} - -pub fn comp_to_string(comp: &Cmpop) -> String { - cmpop.as_str() -} - -pub fn accessor_name(expr: ExpressionType) -> Option { +pub fn accessor_name(expr: Expr) -> Option { match expr { - ExpressionType::Identifier { name } => Some(name), - ExpressionType::Attribute { value, name } => { - accessor_name(value.node).map(|value| format!("{value}.{name}")) + Expr::Name(name) => Some(name.id.to_string()), + Expr::Attribute(attr) => { + accessor_name(*attr.value).map(|value| format!("{value}.{}", attr.attr)) } _ => None, } diff --git a/crates/py2erg/convert.rs b/crates/py2erg/convert.rs index 98de5da..895b079 100644 --- a/crates/py2erg/convert.rs +++ b/crates/py2erg/convert.rs @@ -19,16 +19,14 @@ use erg_compiler::erg_parser::ast::{ VisModifierSpec, }; use erg_compiler::erg_parser::desugar::Desugarer; -use erg_compiler::erg_parser::token::{Token, TokenKind, AS, DOT, EQUAL}; +use erg_compiler::erg_parser::token::{Token, TokenKind, AS, COLON, DOT, EQUAL}; use erg_compiler::erg_parser::Parser; use erg_compiler::error::{CompileError, CompileErrors}; use rustpython_parser::ast::located::{ - self as py_ast, Boolop, Cmpop, StmtImport, Keyword, Located, ModModule, Operator, - String, Suite, Unaryop, Arguments, Arg, Stmt, ExprConstant -}; -use rustpython_parser::source_code::{ - SourceLocation as PyLocation, SourceLocator, SourceRange as PySourceRange, + self as py_ast, Alias, Arg, Arguments, Boolop, Cmpop, ExprConstant, Keyword, Located, + ModModule, Operator, Stmt, String, Suite, Unaryop, }; +use rustpython_parser::source_code::{SourceLocation as PyLocation, SourceRange as PySourceRange}; use crate::ast_util::accessor_name; use crate::error::*; @@ -328,14 +326,14 @@ impl ASTConverter { TokenKind::Symbol, cont, loc.row.get(), - loc.column.get() - 1, + loc.column.to_zero_indexed(), ); let name = VarName::new(token); let dot = Token::new( TokenKind::Dot, ".", loc.row.get(), - loc.column.get() - 1, + loc.column.to_zero_indexed(), ); Identifier::new(VisModifierSpec::Public(dot), name) } @@ -345,15 +343,15 @@ impl ASTConverter { let token = Token::new( TokenKind::Symbol, name, - loc.row.get() as u32, - loc.column.get() as u32 - 1, + loc.row.get(), + loc.column.to_zero_indexed(), ); let name = VarName::new(token); let dot = Token::new( TokenKind::Dot, ".", - loc.row.get() as u32, - loc.column.get() as u32 - 1, + loc.row.get(), + loc.column.to_zero_indexed(), ); Identifier::new(VisModifierSpec::Public(dot), name) } @@ -378,28 +376,25 @@ impl ASTConverter { NonDefaultParamSignature::new(pat, t_spec) } - fn convert_default_param( - &mut self, - kw: Arg, - default: py_ast::Expr, - ) -> DefaultParamSignature { + fn convert_default_param(&mut self, kw: Arg, default: py_ast::Expr) -> DefaultParamSignature { let sig = self.convert_nd_param(kw); let default = self.convert_expr(default); DefaultParamSignature::new(sig, default) } fn convert_params(&mut self, params: Arguments) -> Params { - let non_defaults_len = params.args.len() - params.defaults.len(); - let mut non_default_names = params.args; - let defaults_names = non_default_names.split_off(non_defaults_len); - let non_defaults = non_default_names + let non_defaults_len = params.args.len() - params.defaults(); + let mut args = params.into_python_arguments(); + let defaults = params.defaults(); + let non_defaults = args + .args .into_iter() .map(|p| self.convert_nd_param(p)) .collect(); let defaults = defaults_names .into_iter() - .zip(params.defaults.into_iter()) - .map(|(kw, default)| self.convert_default_param(kw, default)) + .zip(params.defaults()) + .map(|(kw, default)| self.convert_default_param(kw, default.clone())) .collect(); Params::new(non_defaults, None, defaults, None) } @@ -442,13 +437,12 @@ impl ASTConverter { ) -> (NonDefaultParamSignature, Vec) { match expr { py_ast::Expr::Name(expr) => ( - self.convert_for_param(expr.name, expr.location()), + self.convert_for_param(expr.id.to_string(), expr.location()), vec![], ), py_ast::Expr::Tuple(expr) => { let tmp = fresh_varname(); - let tmp_name = - VarName::from_str_and_line((&tmp).into(), expr.location().row.get()); + let tmp_name = VarName::from_str_and_line((&tmp).into(), expr.location().row.get()); let tmp_expr = Expr::Accessor(Accessor::Ident(Identifier::new( VisModifierSpec::Public(DOT), tmp_name.clone(), @@ -466,9 +460,9 @@ impl ASTConverter { Self::param_pattern_to_var(param.pat), param.t_spec, )); - let method = tmp_expr.clone().attr_expr( - self.convert_ident("__getitem__".to_string(), expr.location()), - ); + let method = tmp_expr + .clone() + .attr_expr(self.convert_ident("__getitem__".to_string(), expr.location())); let tuple_acc = method.call1(Expr::Literal(index)); let body = DefBody::new(EQUAL, Block::new(vec![tuple_acc]), DefId(0)); let def = Expr::Def(Def::new(sig, body)); @@ -550,7 +544,7 @@ impl ASTConverter { "Optional" => { let loc = args.location(); let t = self.convert_type_spec(args); - let ident = Identifier::private_with_line("NoneType".into(), loc.row() as u32); + let ident = Identifier::private_with_line("NoneType".into(), loc.row.get()); let none = TypeSpec::mono(ident); TypeSpec::or(t, none) } @@ -719,8 +713,11 @@ impl ASTConverter { let py_ast::Expr::Tuple(tuple) = args else { return Self::gen_dummy_type_spec(args.location()); }; - let (l_brace, r_brace) = - Self::gen_enclosure_tokens(TokenKind::LBrace, tuple.elts.iter(), args.location()); + let (l_brace, r_brace) = Self::gen_enclosure_tokens( + TokenKind::LBrace, + tuple.elts.iter(), + args.location(), + ); let key_t = self.convert_expr(tuple.elts.remove(0)); let key_t = match Parser::validate_const_expr(key_t) { Ok(key_t) => key_t, @@ -764,9 +761,13 @@ impl ASTConverter { let py_ast::Expr::Tuple(tuple) = args else { return Self::gen_dummy_type_spec(args.location()); }; - let parens = - Self::gen_enclosure_tokens(TokenKind::LParen, tuple.elts.iter(), args.location()); - let tys = tuple.elts + let parens = Self::gen_enclosure_tokens( + TokenKind::LParen, + tuple.elts.iter(), + args.location(), + ); + let tys = tuple + .elts .into_iter() .map(|elem| self.convert_type_spec(elem)) .collect(); @@ -788,34 +789,37 @@ impl ASTConverter { } py_ast::Expr::Attribute(attr) => { let namespace = Box::new(self.convert_expr(*attr.value)); - let t = self.convert_ident(attr.attr, expr.location()); + let t = self.convert_ident(attr.attr.to_string(), expr.location()); let predecl = PreDeclTypeSpec::Attr { namespace, t }; TypeSpec::PreDeclTy(predecl) } + // value[slice] py_ast::Expr::Subscript(subs) => match *subs.value { - py_ast::Expr::Name(name) => self.convert_compound_type_spec(name, *subs.slice), + py_ast::Expr::Name(name) => { + self.convert_compound_type_spec(name.id.to_string(), *subs.slice) + } py_ast::Expr::Attribute(attr) => { - match accessor_name(value.node).as_ref().map(|s| &s[..]) { + match accessor_name(attr.value).as_ref().map(|s| &s[..]) { Some("typing" | "collections.abc") => { - self.convert_compound_type_spec(name, *b) + self.convert_compound_type_spec(attr.attr.to_string(), *subs.slice) } _ => { - log!(err "unknown: .{name}"); - Self::gen_dummy_type_spec(a.location) + log!(err "unknown: {subs:?}"); + Self::gen_dummy_type_spec(subs.value.location()) } } } other => { log!(err "unknown: {other:?}"); - Self::gen_dummy_type_spec(a.location) + Self::gen_dummy_type_spec(other.location()) } }, py_ast::Expr::BinOp(bin) => { match bin.op { // A | B Operator::BitOr => { - let lhs = self.convert_type_spec(*a); - let rhs = self.convert_type_spec(*b); + let lhs = self.convert_type_spec(*bin.left); + let rhs = self.convert_type_spec(*bin.right); TypeSpec::or(lhs, rhs) } _ => Self::gen_dummy_type_spec(expr.location()), @@ -868,78 +872,86 @@ impl ASTConverter { Expr::UnaryOp(UnaryOp::new(mut_op, expr)) } - fn convert_expr(&mut self, expr: py_ast::Expr) -> Expr { - match expr { - py_ast::Expr::Number { value } => { - let (kind, cont) = match value { - Number::Integer { value } if value >= 0.into() => { - (TokenKind::NatLit, value.to_string()) - } - Number::Integer { value } => (TokenKind::IntLit, value.to_string()), - Number::Float { value } => (TokenKind::RatioLit, value.to_string()), - Number::Complex { .. } => { - return Expr::Dummy(Dummy::new(None, vec![])); - } + fn convert_const(&mut self, const_: ExprConstant) -> Expr { + match const_.value { + py_ast::Constant::Int(i) => { + let kind = if i >= 0.into() { + TokenKind::NatLit + } else { + TokenKind::IntLit }; let token = Token::new( kind, - cont, - expr.location().start().row() as u32, - expr.location().start().column() as u32 - 1, + i.to_string(), + const_.location().row.get(), + const_.location().column.to_zero_indexed(), ); Expr::Literal(Literal::new(token)) } - py_ast::Expr::Constant(cons) => match cons.value { + py_ast::Constant::Float(f) => { + let token = Token::new( + TokenKind::RatioLit, + f.to_string(), + const_.location().row.get(), + const_.location().column.to_zero_indexed(), + ); + Expr::Literal(Literal::new(token)) } - py_ast::Expr::String { value } => { - let StringGroup::Constant{ value } = value else { - return Expr::Dummy(Dummy::new(None, vec![])); - }; + py_ast::Constant::Complex { real: _, imag: _ } => Expr::Dummy(Dummy::new(None, vec![])), + py_ast::Constant::Str(value) => { let value = format!("\"{value}\""); // column - 2 because of the quotes let token = Token::new( TokenKind::StrLit, value, - expr.location().start().row() as u32, - expr.location().start().column() as u32 - 2, + const_.location().row.get(), + const_.location().column.to_zero_indexed() - 1, ); Expr::Literal(Literal::new(token)) } - py_ast::Expr::False => Expr::Literal(Literal::new(Token::new( - TokenKind::BoolLit, - "False", - expr.location().start().row() as u32, - expr.location().start().column() as u32 - 1, - ))), - py_ast::Expr::True => Expr::Literal(Literal::new(Token::new( - TokenKind::BoolLit, - "True", - expr.location().start().row() as u32, - expr.location().start().column() as u32 - 1, - ))), - py_ast::Expr::None => Expr::Literal(Literal::new(Token::new( + py_ast::Constant::Bool(b) => { + let cont = if b { "True" } else { "False" }; + Expr::Literal(Literal::new(Token::new( + TokenKind::BoolLit, + cont, + const_.location().row.get(), + const_.location().column.to_zero_indexed(), + ))) + } + py_ast::Constant::None => Expr::Literal(Literal::new(Token::new( TokenKind::NoneLit, "None", - expr.location().start().row() as u32, - expr.location().start().column() as u32 - 1, + const_.location().row.get(), + const_.location().column.to_zero_indexed(), ))), - py_ast::Expr::Ellipsis => Expr::Literal(Literal::new(Token::new( + py_ast::Constant::Ellipsis => Expr::Literal(Literal::new(Token::new( TokenKind::EllipsisLit, "...", - expr.location().start().row() as u32, - expr.location().start().column() as u32 - 1, + const_.location().row.get(), + const_.location().column.to_zero_indexed(), ))), + // Bytes, Tuple + other => { + log!(err "unknown: {other:?}"); + Expr::Dummy(Dummy::new(None, vec![])) + } + } + } + + fn convert_expr(&mut self, expr: py_ast::Expr) -> Expr { + match expr { + py_ast::Expr::Constant(const_) => self.convert_const(const_), py_ast::Expr::Name(name) => { - let ident = self.convert_ident(name, expr.location().start()); + let ident = self.convert_ident(name.id.to_string(), expr.location()); Expr::Accessor(Accessor::Ident(ident)) } py_ast::Expr::Attribute(attr) => { - let obj = self.convert_expr(*value); + let obj = self.convert_expr(*attr.value); let attr_name_loc = PyLocation::new( obj.ln_end().unwrap_or(1) as usize, obj.col_end().unwrap_or(1) as usize + 2, ); - let name = self.convert_attr_ident(name, attr_name_loc); + let name = self.convert_attr_ident(attr.attr.to_string(), attr_name_loc); obj.attr_expr(name) } py_ast::Expr::IfExp(if_) => { @@ -966,11 +978,13 @@ impl ASTConverter { } py_ast::Expr::Call(call) => { let function = self.convert_expr(*call.func); - let pos_args = call.args + let pos_args = call + .args .into_iter() .map(|ex| PosArg::new(self.convert_expr(ex))) .collect::>(); - let kw_args = call.keywords + let kw_args = call + .keywords .into_iter() .map(|Keyword { arg, value, range }| { let name = arg.unwrap_or_default(); @@ -1002,13 +1016,13 @@ impl ASTConverter { function.call_expr(args) } py_ast::Expr::BinOp(bin) => { - let lhs = self.convert_expr(*a); - let rhs = self.convert_expr(*b); - let op = op_to_token(op); + let lhs = self.convert_expr(*bin.left); + let rhs = self.convert_expr(*bin.right); + let op = op_to_token(bin.op); Expr::BinOp(BinOp::new(op, lhs, rhs)) } py_ast::Expr::UnaryOp(un) => { - let rhs = self.convert_expr(*a); + let rhs = self.convert_expr(*un.operand); let (kind, cont) = match un.op { Unaryop::UAdd => (TokenKind::PrePlus, "+"), // Unaryop::Not => (TokenKind::PreBitNot, "not"), @@ -1059,12 +1073,10 @@ impl ASTConverter { Expr::Lambda(Lambda::new(sig, op, Block::new(body), DefId(0))) } py_ast::Expr::List(list) => { - let (l_sqbr, r_sqbr) = Self::gen_enclosure_tokens( - TokenKind::LSqBr, - list.elts.iter(), - expr.location(), - ); - let elements = list.elts + let (l_sqbr, r_sqbr) = + Self::gen_enclosure_tokens(TokenKind::LSqBr, list.elts.iter(), expr.location()); + let elements = list + .elts .into_iter() .map(|ex| PosArg::new(self.convert_expr(ex))) .collect::>(); @@ -1073,12 +1085,10 @@ impl ASTConverter { Self::mutate_expr(arr) } py_ast::Expr::Set(set) => { - let (l_brace, r_brace) = Self::gen_enclosure_tokens( - TokenKind::LBrace, - set.elts.iter(), - expr.location(), - ); - let elements = set.elts + let (l_brace, r_brace) = + Self::gen_enclosure_tokens(TokenKind::LBrace, set.elts.iter(), expr.location()); + let elements = set + .elts .into_iter() .map(|ex| PosArg::new(self.convert_expr(ex))) .collect::>(); @@ -1092,7 +1102,8 @@ impl ASTConverter { dict.values.iter(), expr.location(), ); - let kvs = dict.keys + let kvs = dict + .keys .into_iter() .zip(dict.values.into_iter()) .map(|(k, v)| { @@ -1107,7 +1118,8 @@ impl ASTConverter { Self::mutate_expr(dict) } py_ast::Expr::Tuple(tuple) => { - let elements = tuple.elts + let elements = tuple + .elts .into_iter() .map(|ex| PosArg::new(self.convert_expr(ex))) .collect::>(); @@ -1115,11 +1127,10 @@ impl ASTConverter { Expr::Tuple(Tuple::Normal(NormalTuple::new(elems))) } py_ast::Expr::Subscript(subs) => { - let obj = self.convert_expr(*a); - let method = obj.attr_expr( - self.convert_ident("__getitem__".to_string(), expr.location()), - ); - method.call1(self.convert_expr(*b)) + let obj = self.convert_expr(*subs.value); + let method = + obj.attr_expr(self.convert_ident("__getitem__".to_string(), expr.location())); + method.call1(self.convert_expr(*subs.slice)) } _other => { log!(err "unimplemented: {:?}", _other); @@ -1266,7 +1277,8 @@ impl ASTConverter { self.namespace.last().unwrap().into(), sig.ln_begin().unwrap_or(0), ); - let class_spec = TypeSpec::mono(class_ident); + let class_ident_expr = Expr::Accessor(Accessor::Ident(class_ident.clone())); + let class_spec = TypeSpecWithOp::new(COLON, TypeSpec::mono(class_ident), class_ident_expr); let sig = Signature::Subr(SubrSignature::new( set! { Decorator(Expr::static_local("Override")) }, call_ident, @@ -1290,7 +1302,8 @@ impl ASTConverter { let params = Params::new(vec![], None, vec![], None); let class_ident = Identifier::public_with_line(DOT, self.namespace.last().unwrap().into(), line as u32); - let class_spec = TypeSpec::mono(class_ident); + let class_ident_expr = Expr::Accessor(Accessor::Ident(class_ident.clone())); + let class_spec = TypeSpecWithOp::new(COLON, TypeSpec::mono(class_ident), class_ident_expr); let sig = Signature::Subr(SubrSignature::new( set! { Decorator(Expr::static_local("Override")) }, call_ident, @@ -1433,11 +1446,20 @@ impl ASTConverter { .map(|ex| Decorator(self.convert_expr(ex))) .collect::>(); self.register_name_info(&name, NameKind::Function); - let func_name_loc = PyLocation{ row: loc.row.get(), column: loc.column.get() + 4 }; + let func_name_loc = PyLocation { + row: loc.row, + column: loc.column.saturating_add(4), + }; let ident = self.convert_ident(name, func_name_loc); self.namespace.push(ident.inspect().to_string()); let params = self.convert_params(params); - let return_t = returns.map(|ret| self.convert_type_spec(ret)); + let return_t = returns.map(|ret| { + TypeSpecWithOp::new( + COLON, + self.convert_type_spec(ret.clone()), + self.convert_expr(ret), + ) + }); let sig = Signature::Subr(SubrSignature::new( decos, ident, @@ -1485,7 +1507,10 @@ impl ASTConverter { .collect::>(); let inherit = !bases.is_empty(); self.register_name_info(&name, NameKind::Class); - let class_name_loc = PyLocation::new(loc.row(), loc.column() + 6); + let class_name_loc = PyLocation { + row: loc.row, + column: loc.column.saturating_add(6), + }; let ident = self.convert_ident(name, class_name_loc); let sig = Signature::Var(VarSignature::new(VarPattern::Ident(ident.clone()), None)); self.namespace.push(ident.inspect().to_string()); @@ -1550,16 +1575,17 @@ impl ASTConverter { Expr::Def(def) } else { // no registration because it's just a type ascription - let ident = self.convert_ident(name, stmt.location()); + let ident = self.convert_ident(name.id.to_string(), stmt.location()); let tasc = TypeAscription::new(Expr::Accessor(Accessor::Ident(ident)), t_spec); Expr::TypeAscription(tasc) } } py_ast::Expr::Attribute(attr) => { - let attr = self - .convert_expr(*attr.value) - .attr(self.convert_attr_ident(attr.attr, ann_assign.target.location())); + let attr = self.convert_expr(*attr.value).attr(self.convert_attr_ident( + attr.attr.to_string(), + ann_assign.target.location(), + )); if let Some(value) = ann_assign.value { let expr = self.convert_expr(*value); let redef = ReDef::new(attr, expr); @@ -1579,8 +1605,8 @@ impl ASTConverter { py_ast::Expr::Name(name) => { let block = Block::new(vec![self.convert_expr(*assign.value)]); let body = DefBody::new(EQUAL, block, DefId(0)); - self.register_name_info(&name, NameKind::Variable); - let ident = self.convert_ident(name, stmt.location); + self.register_name_info(&name.id, NameKind::Variable); + let ident = self.convert_ident(name.id.to_string(), stmt.location()); let sig = Signature::Var(VarSignature::new(VarPattern::Ident(ident), None)); let def = Def::new(sig, body); @@ -1663,8 +1689,9 @@ impl ASTConverter { py_ast::Expr::Name(name) => { let body = DefBody::new(EQUAL, Block::new(vec![value.clone()]), DefId(0)); - self.register_name_info(&name, NameKind::Variable); - let ident = self.convert_ident(name, stmt.location()); + self.register_name_info(&name.id, NameKind::Variable); + let ident = + self.convert_ident(name.id.to_string(), stmt.location()); let sig = Signature::Var(VarSignature::new( VarPattern::Ident(ident), None, @@ -1682,17 +1709,17 @@ impl ASTConverter { } py_ast::Stmt::AugAssign(aug_assign) => { let op = op_to_token(aug_assign.op); - match aug_assign.target { - py_ast::Expr::Identifier { name } => { - let val = self.convert_expr(*value); - let prev_ident = self.convert_ident(name.clone(), stmt.location()); + match *aug_assign.target { + py_ast::Expr::Name(name) => { + let val = self.convert_expr(*aug_assign.value); + let prev_ident = self.convert_ident(name.id.to_string(), stmt.location()); if self - .get_name(&name) + .get_name(name.id.as_str()) .map(|info| info.defined_block_id == self.cur_block_id()) .unwrap_or(false) { - self.register_name_info(&name, NameKind::Variable); - let ident = self.convert_ident(name.clone(), stmt.location()); + self.register_name_info(&name.id, NameKind::Variable); + let ident = self.convert_ident(name.id.to_string(), stmt.location()); let bin = BinOp::new(op, Expr::Accessor(Accessor::Ident(prev_ident)), val); let sig = @@ -1702,7 +1729,7 @@ impl ASTConverter { let def = Def::new(sig, body); Expr::Def(def) } else { - let ident = self.convert_ident(name.clone(), stmt.location()); + let ident = self.convert_ident(name.id.to_string(), stmt.location()); let bin = BinOp::new(op, Expr::Accessor(Accessor::Ident(prev_ident)), val); let redef = ReDef::new(Accessor::Ident(ident), Expr::BinOp(bin)); @@ -1710,10 +1737,10 @@ impl ASTConverter { } } py_ast::Expr::Attribute(attr) => { - let val = self.convert_expr(*value); + let val = self.convert_expr(*aug_assign.value); let attr = self .convert_expr(*attr) - .attr(self.convert_attr_ident(name, target.location)); + .attr(self.convert_attr_ident(name, aug_assign.target.location())); let bin = BinOp::new(op, Expr::Accessor(attr.clone()), val); let adef = ReDef::new(attr, Expr::BinOp(bin)); Expr::ReDef(adef) @@ -1724,8 +1751,21 @@ impl ASTConverter { } } } - py_ast::Stmt::FunctionDef(func_def) => self.convert_funcdef(name, *args, body, decorator_list, returns, range), - py_ast::Stmt::ClassDef(class_def) => self.convert_classdef(name, body, bases, decorator_list, range), + py_ast::Stmt::FunctionDef(func_def) => self.convert_funcdef( + func_def.name.to_string(), + *func_def.args, + func_def.body, + func_def.decorator_list, + func_def.returns.map(|x| *x), + func_def.range, + ), + py_ast::Stmt::ClassDef(class_def) => self.convert_classdef( + class_def.name.to_string(), + class_def.body, + class_def.bases, + class_def.decorator_list, + class_def.location(), + ), py_ast::Stmt::For(for_) => { let iter = self.convert_expr(*for_.iter); let block = self.convert_for_body(Some(*for_.target), for_.body); @@ -1769,8 +1809,9 @@ impl ASTConverter { } } py_ast::Stmt::Return(return_) => { - let value = return_.value - .map(|val| self.convert_expr(val)) + let value = return_ + .value + .map(|val| self.convert_expr(*val)) .unwrap_or_else(|| Expr::Tuple(Tuple::Normal(NormalTuple::new(Args::empty())))); if dont_call_return { value @@ -1813,7 +1854,9 @@ impl ASTConverter { let def = if let Some(alias) = name.asname { self.register_name_info(&alias, NameKind::Variable); let var = VarSignature::new( - VarPattern::Ident(self.convert_ident(alias, stmt.location())), + VarPattern::Ident( + self.convert_ident(alias.to_string(), stmt.location()), + ), None, ); Def::new( @@ -1823,7 +1866,9 @@ impl ASTConverter { } else { self.register_name_info(&name.name, NameKind::Variable); let var = VarSignature::new( - VarPattern::Ident(self.convert_ident(name.name, stmt.location())), + VarPattern::Ident( + self.convert_ident(name.name.to_string(), stmt.location()), + ), None, ); Def::new( @@ -1836,9 +1881,13 @@ impl ASTConverter { Expr::Dummy(Dummy::new(None, imports)) } // from module import foo, bar - py_ast::Stmt::ImportFrom(import_from) => self.convert_from_import(module, names, stmt.location), + py_ast::Stmt::ImportFrom(import_from) => self.convert_from_import( + import_from.module, + import_from.names, + import_from.location(), + ), py_ast::Stmt::Try(try_) => { - let chunks = self.convert_block(body, BlockKind::Try).into_iter(); + let chunks = self.convert_block(try_.body, BlockKind::Try).into_iter(); let dummy = match (orelse, finalbody) { (Some(orelse), Some(finalbody)) => chunks .chain(self.convert_block(orelse, BlockKind::Try).into_iter()) @@ -1857,7 +1906,7 @@ impl ASTConverter { py_ast::Stmt::With(with) => { let item = with.items.remove(0); let context_expr = self.convert_expr(item.context_expr); - let body = self.convert_for_body(item.optional_vars, body); + let body = self.convert_for_body(item.optional_vars.map(|x| *x), with.body); let with_ident = self.convert_ident("with".to_string(), stmt.location()); let with_acc = Expr::Accessor(Accessor::Ident(with_ident)); with_acc.call2(context_expr, Expr::Lambda(body)) @@ -1893,8 +1942,8 @@ impl ASTConverter { */ fn convert_from_import( &mut self, - module: Option, - names: Vec, + module: Option, + names: Vec, location: PyLocation, ) -> Expr { let import_acc = Expr::Accessor(Accessor::Ident( @@ -1912,20 +1961,21 @@ impl ASTConverter { let mod_name = Expr::Literal(Literal::new(Token::new( TokenKind::StrLit, cont, - location.row() as u32, - location.column() as u32 - 1, + location.row.get(), + location.column.to_zero_indexed(), ))); let call = import_acc.clone().call1(mod_name); let mut exprs = vec![]; let mut imports = vec![]; - // `from module import ` - let mut loc = PyLocation::new(location.row(), location.column() + 5 + module.len() + 8); for name in names { - let name_path = self.cfg.input.resolve_py(&module_path.join(&name.symbol)); - let true_name = self.convert_ident(name.symbol.clone(), loc); - let alias = if let Some(alias) = name.alias { + let name_path = self + .cfg + .input + .resolve_py(&module_path.join(name.name.as_str())); + let true_name = self.convert_ident(name.name.to_string(), name.location()); + let alias = if let Some(alias) = name.asname { // ` as ` - for _ in 0..name.symbol.len() + 4 { + for _ in 0..name.name.len() + 4 { loc.go_right(); } self.register_name_info(&alias, NameKind::Variable); @@ -1937,21 +1987,21 @@ impl ASTConverter { } VarSignature::new(VarPattern::Ident(ident), None) } else { - self.register_name_info(&name.symbol, NameKind::Variable); - let ident = self.convert_ident(name.symbol.clone(), loc); - for _ in 0..name.symbol.len() + 2 { + self.register_name_info(&name.name, NameKind::Variable); + let ident = self.convert_ident(name.name.to_string(), name.location()); + for _ in 0..name.name.len() + 2 { loc.go_right(); } VarSignature::new(VarPattern::Ident(ident), None) }; // from foo import bar, baz (if bar, baz is a module) ==> bar = import "foo/bar"; baz = import "foo/baz" if let Ok(_path) = name_path { - let cont = format!("\"{module}/{}\"", name.symbol); + let cont = format!("\"{module}/{}\"", name.name); let mod_name = Expr::Literal(Literal::new(Token::new( TokenKind::StrLit, cont, - location.row() as u32, - location.column() as u32 - 1, + location.row.get(), + location.column.to_zero_indexed(), ))); let call = import_acc.clone().call1(mod_name); let def = Def::new( diff --git a/src/analyze.rs b/src/analyze.rs index b8086ff..906b22b 100644 --- a/src/analyze.rs +++ b/src/analyze.rs @@ -14,7 +14,7 @@ use erg_compiler::error::{CompileError, CompileErrors}; use erg_compiler::lower::ASTLowerer; use erg_compiler::module::SharedCompilerResource; use py2erg::{dump_decl_er, reserve_decl_er, ShadowingMode}; -use rustpython_parser::{Parse, ast as py_ast}; +use rustpython_parser::{ast as py_ast, Parse}; use crate::handle_err;