feat: add display UI with splash screen, dashboard, and RSSI sparkline
Implement full SSD1306 OLED display support: - Splash screen on boot (1.5s) with project name, version, board - Dashboard showing radio state, config, counters, RSSI/SNR - RSSI history sparkline (64-sample ring buffer, 2px-wide bars) - DisplayOn/DisplayOff host commands routed via dedicated channel - Pure render functions against generic DrawTarget (no hardware coupling) - Display on/off control bypasses radio task entirely Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
17624 files changed
+4564928
-38
No files matched your search
+1
@@ -0,0 +1 @@
|
||||
{"files":{".cargo_vcs_info.json":"e4d043ab30089ea20f457c17dc44ebdb07e2c2c25025e8dff7f29872b9e04d97",".github/FUNDING.yml":"b017158736b3c9751a2d21edfce7fe61c8954e2fced8da8dd3013c2f3e295bd9",".github/workflows/ci.yml":"9a9f80a475b96b7cc6d8925107b7d9cdb2c15607522ee4e92987d0db988a8937","Cargo.lock":"f7227d3003000147f176e9654b59a7f0cae6ff0936b806883a5dae9bd4e96f2e","Cargo.toml":"7408af188c9656c1c2a6012f61026df0e2dfc0275e0edf1e01f1d59b3e39c194","Cargo.toml.orig":"a5c32b74b82af9a566766581cca85f83936dd4c18e998f25cda46defaf399389","LICENSE-APACHE":"62c7a1e35f56406896d7aa7ca52d0cc0d272ac022b5d2796e7d6905db8a3636a","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","README.md":"9fb9e04cc2b889e838404d352920bc09c5a76a36536a2d1b44006472afdd0f8a","build.rs":"baeb20b52f6b536be8657a566591a507bb2e34a45cf8baa42b135510a0c3c729","rust-toolchain.toml":"6bbb61302978c736b2da03e4fb40e3beab908f85d533ab46fd541e637b5f3e0f","src/detection.rs":"ed9a5f9a979ab01247d7a68eeb1afa3c13209334c5bfff0f9289cb07e5bb4e8b","src/extra.rs":"29f094473279a29b71c3cc9f5fa27c2e2c30c670390cf7e4b7cf451486cc857e","src/fallback.rs":"416a3d24349c163d47f62ac8ae61c28ba6dc4990f04f3d5dacde030b86582d06","src/lib.rs":"8479c33411bdc08fc24ff451047f0579b312ce799c4f57e12f5fb68dad9fea77","src/location.rs":"9225c5a55f03b56cce42bc55ceb509e8216a5e0b24c94aa1cd071b04e3d6c15f","src/marker.rs":"c11c5a1be8bdf18be3fcd224393f350a9aae7ce282e19ce583c84910c6903a8f","src/num.rs":"82d625cbcd255965e46231ac3af1b74ab8bff9787c799e8ed1f978de146cb0b5","src/parse.rs":"328c5880557525807be3d44ac0d10402e3632c90e4cadd4a9f53f1da96b0c3bf","src/probe.rs":"2b57e8ebf46a7c60ee2762f23f16d24ee9ddb8f1acd0a7faf7a99cf2e4187151","src/probe/proc_macro_span.rs":"53853f0c70170c9695294b8867821664d9b8f1c901957b003003a3c26987abbe","src/probe/proc_macro_span_file.rs":"e7fb4cf8852d9d589bfa3321d8d5cdc8cccb45606ec816a6b8a08f73e416b9ce","src/probe/proc_macro_span_location.rs":"e022386204b6e042b3c55a6c809923849a2f915199bcbf3be72d0b7c8ffa7f83","src/rcvec.rs":"d6e4c1ea42f75e63465fce50ca83f9257536ccdc0345fe78b0037aa2c08c5e37","src/rustc_literal_escaper.rs":"fb3f2f93a09b84f539e6ef514d4b403ee16f2112ff04642099170a60828318ee","src/wrapper.rs":"21c083c855576524415c0f737c87641c60e83a74f3cc6221871f9a39e6d8506f","tests/comments.rs":"11520f6baee23b9258db904f4c256fd3877493f754e2b99041f73a330e74a911","tests/features.rs":"7e52c0c801019b271bf11a994c2e1799a1429b0c1a3a34e551a23971797fe412","tests/marker.rs":"f16299460587d6c65603ed809f1a3b81853e4b99d6cb44d0b68bb07259d7e9f8","tests/test.rs":"7d58c3a754241ffdec84e4e9ac799852071db71d77359de164ca7ba6217020e9","tests/test_fmt.rs":"b7743b612af65f2c88cbe109d50a093db7aa7e87f9e37bf45b7bbaeb240aa020","tests/test_size.rs":"7ed5f07c3ccc74c4d09f3c1c43542fbbe23417146761558a0fd4659e19ae1afc"},"package":"8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"git": {
|
||||
"sha1": "58ab776b95a4c2865554badbb6629c50971a9118"
|
||||
},
|
||||
"path_in_vcs": ""
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
github: dtolnay
|
||||
+232
@@ -0,0 +1,232 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
schedule: [cron: "40 1 * * *"]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
env:
|
||||
RUSTFLAGS: -Dwarnings
|
||||
|
||||
jobs:
|
||||
pre_ci:
|
||||
uses: dtolnay/.github/.github/workflows/pre_ci.yml@master
|
||||
|
||||
test:
|
||||
name: Rust ${{matrix.rust}}
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
rust: [1.80.0, stable, beta]
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@master
|
||||
with:
|
||||
toolchain: ${{matrix.rust}}
|
||||
components: rust-src
|
||||
- run: cargo test
|
||||
- run: cargo test --no-default-features
|
||||
- run: cargo test --features span-locations
|
||||
- name: RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo test
|
||||
run: cargo test
|
||||
env:
|
||||
RUSTFLAGS: --cfg procmacro2_semver_exempt ${{env.RUSTFLAGS}}
|
||||
- name: RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo test --no-default-features
|
||||
run: cargo test --no-default-features
|
||||
env:
|
||||
RUSTFLAGS: --cfg procmacro2_semver_exempt ${{env.RUSTFLAGS}}
|
||||
|
||||
nightly:
|
||||
name: Rust nightly
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
with:
|
||||
components: rust-src
|
||||
- name: Enable type layout randomization
|
||||
run: echo RUSTFLAGS=${RUSTFLAGS}\ -Zrandomize-layout\ --cfg=randomize_layout >> $GITHUB_ENV
|
||||
- run: cargo check
|
||||
env:
|
||||
RUSTFLAGS: --cfg procmacro2_nightly_testing ${{env.RUSTFLAGS}}
|
||||
- run: cargo test
|
||||
- run: cargo test --no-default-features
|
||||
- run: cargo test --no-default-features --test features -- --ignored make_sure_no_proc_macro # run the ignored test to make sure the `proc-macro` feature is disabled
|
||||
- run: cargo test --features span-locations
|
||||
- run: cargo test --manifest-path tests/ui/Cargo.toml
|
||||
- name: RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo test
|
||||
run: cargo test
|
||||
env:
|
||||
RUSTFLAGS: --cfg procmacro2_semver_exempt ${{env.RUSTFLAGS}}
|
||||
- name: RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo test --no-default-features
|
||||
run: cargo test --no-default-features
|
||||
env:
|
||||
RUSTFLAGS: --cfg procmacro2_semver_exempt ${{env.RUSTFLAGS}}
|
||||
- name: RUSTFLAGS='-Z allow-features=' cargo test
|
||||
run: cargo test
|
||||
env:
|
||||
RUSTFLAGS: -Z allow-features= --cfg procmacro2_backtrace ${{env.RUSTFLAGS}}
|
||||
- uses: actions/upload-artifact@v6
|
||||
if: always()
|
||||
with:
|
||||
name: Cargo.lock
|
||||
path: Cargo.lock
|
||||
continue-on-error: true
|
||||
|
||||
layout:
|
||||
name: Layout
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
with:
|
||||
components: rust-src
|
||||
- run: cargo test --test test_size
|
||||
- run: cargo test --test test_size --features span-locations
|
||||
- run: cargo test --test test_size --no-default-features
|
||||
- run: cargo test --test test_size --no-default-features --features span-locations
|
||||
|
||||
msrv:
|
||||
name: Rust 1.68.0
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@1.68.0
|
||||
with:
|
||||
components: rust-src
|
||||
- run: cargo check
|
||||
- run: cargo check --no-default-features
|
||||
- run: cargo check --features span-locations
|
||||
- name: RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo check
|
||||
run: cargo check
|
||||
env:
|
||||
RUSTFLAGS: --cfg procmacro2_semver_exempt ${{env.RUSTFLAGS}}
|
||||
- name: RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo check --no-default-features
|
||||
run: cargo check --no-default-features
|
||||
env:
|
||||
RUSTFLAGS: --cfg procmacro2_semver_exempt ${{env.RUSTFLAGS}}
|
||||
|
||||
minimal:
|
||||
name: Minimal versions
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
- run: cargo generate-lockfile -Z minimal-versions
|
||||
- run: cargo check --locked
|
||||
|
||||
webassembly:
|
||||
name: WebAssembly
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
with:
|
||||
target: wasm32-unknown-unknown
|
||||
components: rust-src
|
||||
- name: Ignore WebAssembly linker warning
|
||||
run: echo RUSTFLAGS=${RUSTFLAGS}\ -Alinker_messages >> $GITHUB_ENV
|
||||
- run: cargo test --target wasm32-unknown-unknown --no-run
|
||||
|
||||
fuzz:
|
||||
name: Fuzz
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
with:
|
||||
components: rust-src
|
||||
- uses: dtolnay/install@cargo-fuzz
|
||||
- run: cargo fuzz check
|
||||
- run: cargo check --no-default-features --features afl
|
||||
working-directory: fuzz
|
||||
- uses: dtolnay/install@honggfuzz
|
||||
- name: Run apt install binutils-dev libunwind-dev
|
||||
run: |
|
||||
sudo sed -i 's/^update_initramfs=yes$/update_initramfs=no/' /etc/initramfs-tools/update-initramfs.conf
|
||||
sudo rm -f /var/lib/man-db/auto-update
|
||||
sudo apt-get update
|
||||
sudo apt-get install binutils-dev libunwind-dev
|
||||
- run: cargo hfuzz build --no-default-features --features honggfuzz
|
||||
working-directory: fuzz
|
||||
|
||||
doc:
|
||||
name: Documentation
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
env:
|
||||
RUSTDOCFLAGS: -Dwarnings
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
with:
|
||||
components: rust-src
|
||||
- uses: dtolnay/install@cargo-docs-rs
|
||||
- run: cargo docs-rs
|
||||
|
||||
clippy:
|
||||
name: Clippy
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name != 'pull_request'
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
with:
|
||||
components: clippy, rust-src
|
||||
- run: cargo clippy --tests -- -Dclippy::all -Dclippy::pedantic
|
||||
- run: cargo clippy --tests --all-features -- -Dclippy::all -Dclippy::pedantic
|
||||
|
||||
miri:
|
||||
name: Miri
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@miri
|
||||
- run: cargo miri setup
|
||||
- run: cargo miri test
|
||||
env:
|
||||
MIRIFLAGS: -Zmiri-strict-provenance
|
||||
|
||||
outdated:
|
||||
name: Outdated
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name != 'pull_request'
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- uses: dtolnay/install@cargo-outdated
|
||||
- run: cargo outdated --workspace --exit-code 1
|
||||
- run: cargo outdated --manifest-path fuzz/Cargo.toml --exit-code 1
|
||||
+251
@@ -0,0 +1,251 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "adler2"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-deque"
|
||||
version = "0.8.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
|
||||
dependencies = [
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-epoch"
|
||||
version = "0.9.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "filetime"
|
||||
version = "0.2.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f98844151eee8917efc50bd9e8318cb963ae8b297431495d3f758616ea5c57db"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"libredox",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b375d6465b98090a5f25b1c7703f3859783755aa9a80433b36e0379a3ec2f369"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.180"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc"
|
||||
|
||||
[[package]]
|
||||
name = "libredox"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d0b95e02c851351f877147b7deea7b1afb1df71b63aa5f8270716e0c5720616"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039"
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.8.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
|
||||
dependencies = [
|
||||
"adler2",
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.105"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.106"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"quote",
|
||||
"rayon",
|
||||
"rustversion",
|
||||
"tar",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.105",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rayon"
|
||||
version = "1.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "368f01d005bf8fd9b1206fb6fa653e6c4a81ceb1466406b81792d87c5677a58f"
|
||||
dependencies = [
|
||||
"either",
|
||||
"rayon-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rayon-core"
|
||||
version = "1.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
|
||||
dependencies = [
|
||||
"crossbeam-deque",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49f3fe0889e69e2ae9e41f4d6c4c0181701d00e4697b356fb1f74173a5e0ee27"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "146c9e247ccc180c1f61615433868c99f3de3ae256a30a43b49f67c2d9171f34"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
||||
|
||||
[[package]]
|
||||
name = "simd-adler32"
|
||||
version = "0.3.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e320a6c5ad31d271ad523dcf3ad13e2767ad8b1cb8f047f75a8aeaf8da139da2"
|
||||
|
||||
[[package]]
|
||||
name = "tar"
|
||||
version = "0.4.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d863878d212c87a19c1a610eb53bb01fe12951c0501cf5a0d65f724914a667a"
|
||||
dependencies = [
|
||||
"filetime",
|
||||
"libc",
|
||||
"xattr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "xattr"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rustix",
|
||||
]
|
||||
Vendored
+105
@@ -0,0 +1,105 @@
|
||||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g., crates.io) dependencies.
|
||||
#
|
||||
# If you are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
edition = "2021"
|
||||
rust-version = "1.68"
|
||||
name = "proc-macro2"
|
||||
version = "1.0.106"
|
||||
authors = [
|
||||
"David Tolnay <dtolnay@gmail.com>",
|
||||
"Alex Crichton <alex@alexcrichton.com>",
|
||||
]
|
||||
build = "build.rs"
|
||||
autolib = false
|
||||
autobins = false
|
||||
autoexamples = false
|
||||
autotests = false
|
||||
autobenches = false
|
||||
description = "A substitute implementation of the compiler's `proc_macro` API to decouple token-based libraries from the procedural macro use case."
|
||||
documentation = "https://docs.rs/proc-macro2"
|
||||
readme = "README.md"
|
||||
keywords = [
|
||||
"macros",
|
||||
"syn",
|
||||
]
|
||||
categories = ["development-tools::procedural-macro-helpers"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/dtolnay/proc-macro2"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
rustc-args = ["--cfg=procmacro2_semver_exempt"]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
rustdoc-args = [
|
||||
"--cfg=procmacro2_semver_exempt",
|
||||
"--generate-link-to-definition",
|
||||
"--generate-macro-expansion",
|
||||
"--extern-html-root-url=core=https://doc.rust-lang.org",
|
||||
"--extern-html-root-url=alloc=https://doc.rust-lang.org",
|
||||
"--extern-html-root-url=std=https://doc.rust-lang.org",
|
||||
"--extern-html-root-url=proc_macro=https://doc.rust-lang.org",
|
||||
]
|
||||
|
||||
[package.metadata.playground]
|
||||
features = ["span-locations"]
|
||||
|
||||
[features]
|
||||
default = ["proc-macro"]
|
||||
nightly = []
|
||||
proc-macro = []
|
||||
span-locations = []
|
||||
|
||||
[lib]
|
||||
name = "proc_macro2"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[test]]
|
||||
name = "comments"
|
||||
path = "tests/comments.rs"
|
||||
|
||||
[[test]]
|
||||
name = "features"
|
||||
path = "tests/features.rs"
|
||||
|
||||
[[test]]
|
||||
name = "marker"
|
||||
path = "tests/marker.rs"
|
||||
|
||||
[[test]]
|
||||
name = "test"
|
||||
path = "tests/test.rs"
|
||||
|
||||
[[test]]
|
||||
name = "test_fmt"
|
||||
path = "tests/test_fmt.rs"
|
||||
|
||||
[[test]]
|
||||
name = "test_size"
|
||||
path = "tests/test_size.rs"
|
||||
|
||||
[dependencies.unicode-ident]
|
||||
version = "1.0"
|
||||
|
||||
[dev-dependencies.flate2]
|
||||
version = "1.0"
|
||||
|
||||
[dev-dependencies.quote]
|
||||
version = "1.0"
|
||||
default-features = false
|
||||
|
||||
[dev-dependencies.rayon]
|
||||
version = "1.0"
|
||||
|
||||
[dev-dependencies.rustversion]
|
||||
version = "1"
|
||||
|
||||
[dev-dependencies.tar]
|
||||
version = "0.4"
|
||||
Vendored
+176
@@ -0,0 +1,176 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
Vendored
+94
@@ -0,0 +1,94 @@
|
||||
# proc-macro2
|
||||
|
||||
[<img alt="github" src="https://img.shields.io/badge/github-dtolnay/proc--macro2-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/dtolnay/proc-macro2)
|
||||
[<img alt="crates.io" src="https://img.shields.io/crates/v/proc-macro2.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/proc-macro2)
|
||||
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-proc--macro2-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/proc-macro2)
|
||||
[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/dtolnay/proc-macro2/ci.yml?branch=master&style=for-the-badge" height="20">](https://github.com/dtolnay/proc-macro2/actions?query=branch%3Amaster)
|
||||
|
||||
A wrapper around the procedural macro API of the compiler's `proc_macro` crate.
|
||||
This library serves two purposes:
|
||||
|
||||
- **Bring proc-macro-like functionality to other contexts like build.rs and
|
||||
main.rs.** Types from `proc_macro` are entirely specific to procedural macros
|
||||
and cannot ever exist in code outside of a procedural macro. Meanwhile
|
||||
`proc_macro2` types may exist anywhere including non-macro code. By developing
|
||||
foundational libraries like [syn] and [quote] against `proc_macro2` rather
|
||||
than `proc_macro`, the procedural macro ecosystem becomes easily applicable to
|
||||
many other use cases and we avoid reimplementing non-macro equivalents of
|
||||
those libraries.
|
||||
|
||||
- **Make procedural macros unit testable.** As a consequence of being specific
|
||||
to procedural macros, nothing that uses `proc_macro` can be executed from a
|
||||
unit test. In order for helper libraries or components of a macro to be
|
||||
testable in isolation, they must be implemented using `proc_macro2`.
|
||||
|
||||
[syn]: https://github.com/dtolnay/syn
|
||||
[quote]: https://github.com/dtolnay/quote
|
||||
|
||||
## Usage
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0"
|
||||
```
|
||||
|
||||
The skeleton of a typical procedural macro typically looks like this:
|
||||
|
||||
```rust
|
||||
extern crate proc_macro;
|
||||
|
||||
#[proc_macro_derive(MyDerive)]
|
||||
pub fn my_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input = proc_macro2::TokenStream::from(input);
|
||||
|
||||
let output: proc_macro2::TokenStream = {
|
||||
/* transform input */
|
||||
};
|
||||
|
||||
proc_macro::TokenStream::from(output)
|
||||
}
|
||||
```
|
||||
|
||||
If parsing with [Syn], you'll use [`parse_macro_input!`] instead to propagate
|
||||
parse errors correctly back to the compiler when parsing fails.
|
||||
|
||||
[`parse_macro_input!`]: https://docs.rs/syn/2.0/syn/macro.parse_macro_input.html
|
||||
|
||||
## Unstable features
|
||||
|
||||
The default feature set of proc-macro2 tracks the most recent stable compiler
|
||||
API. Functionality in `proc_macro` that is not yet stable is not exposed by
|
||||
proc-macro2 by default.
|
||||
|
||||
To opt into the additional APIs available in the most recent nightly compiler,
|
||||
the `procmacro2_semver_exempt` config flag must be passed to rustc. We will
|
||||
polyfill those nightly-only APIs back to Rust 1.68.0. As these are unstable APIs
|
||||
that track the nightly compiler, minor versions of proc-macro2 may make breaking
|
||||
changes to them at any time.
|
||||
|
||||
```
|
||||
RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build
|
||||
```
|
||||
|
||||
Note that this must not only be done for your crate, but for any crate that
|
||||
depends on your crate. This infectious nature is intentional, as it serves as a
|
||||
reminder that you are outside of the normal semver guarantees.
|
||||
|
||||
Semver exempt methods are marked as such in the proc-macro2 documentation.
|
||||
|
||||
<br>
|
||||
|
||||
#### License
|
||||
|
||||
<sup>
|
||||
Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
|
||||
2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
|
||||
</sup>
|
||||
|
||||
<br>
|
||||
|
||||
<sub>
|
||||
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||
for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
|
||||
be dual licensed as above, without any additional terms or conditions.
|
||||
</sub>
|
||||
Vendored
+267
@@ -0,0 +1,267 @@
|
||||
#![allow(unknown_lints)]
|
||||
#![allow(unexpected_cfgs)]
|
||||
#![allow(clippy::uninlined_format_args)]
|
||||
|
||||
use std::env;
|
||||
use std::ffi::OsString;
|
||||
use std::fs;
|
||||
use std::io::ErrorKind;
|
||||
use std::iter;
|
||||
use std::path::Path;
|
||||
use std::process::{self, Command, Stdio};
|
||||
use std::str;
|
||||
|
||||
fn main() {
|
||||
let rustc = rustc_minor_version().unwrap_or(u32::MAX);
|
||||
|
||||
if rustc >= 80 {
|
||||
println!("cargo:rustc-check-cfg=cfg(fuzzing)");
|
||||
println!("cargo:rustc-check-cfg=cfg(no_is_available)");
|
||||
println!("cargo:rustc-check-cfg=cfg(no_literal_byte_character)");
|
||||
println!("cargo:rustc-check-cfg=cfg(no_literal_c_string)");
|
||||
println!("cargo:rustc-check-cfg=cfg(no_source_text)");
|
||||
println!("cargo:rustc-check-cfg=cfg(proc_macro_span)");
|
||||
println!("cargo:rustc-check-cfg=cfg(proc_macro_span_file)");
|
||||
println!("cargo:rustc-check-cfg=cfg(proc_macro_span_location)");
|
||||
println!("cargo:rustc-check-cfg=cfg(procmacro2_backtrace)");
|
||||
println!("cargo:rustc-check-cfg=cfg(procmacro2_build_probe)");
|
||||
println!("cargo:rustc-check-cfg=cfg(procmacro2_nightly_testing)");
|
||||
println!("cargo:rustc-check-cfg=cfg(procmacro2_semver_exempt)");
|
||||
println!("cargo:rustc-check-cfg=cfg(randomize_layout)");
|
||||
println!("cargo:rustc-check-cfg=cfg(span_locations)");
|
||||
println!("cargo:rustc-check-cfg=cfg(super_unstable)");
|
||||
println!("cargo:rustc-check-cfg=cfg(wrap_proc_macro)");
|
||||
}
|
||||
|
||||
let semver_exempt = cfg!(procmacro2_semver_exempt);
|
||||
if semver_exempt {
|
||||
// https://github.com/dtolnay/proc-macro2/issues/147
|
||||
println!("cargo:rustc-cfg=procmacro2_semver_exempt");
|
||||
}
|
||||
|
||||
if semver_exempt || cfg!(feature = "span-locations") {
|
||||
// Provide methods Span::start and Span::end which give the line/column
|
||||
// location of a token. This is behind a cfg because tracking location
|
||||
// inside spans is a performance hit.
|
||||
println!("cargo:rustc-cfg=span_locations");
|
||||
}
|
||||
|
||||
if rustc < 57 {
|
||||
// Do not use proc_macro::is_available() to detect whether the proc
|
||||
// macro API is available vs needs to be polyfilled. Instead, use the
|
||||
// proc macro API unconditionally and catch the panic that occurs if it
|
||||
// isn't available.
|
||||
println!("cargo:rustc-cfg=no_is_available");
|
||||
}
|
||||
|
||||
if rustc < 66 {
|
||||
// Do not call libproc_macro's Span::source_text. Always return None.
|
||||
println!("cargo:rustc-cfg=no_source_text");
|
||||
}
|
||||
|
||||
if rustc < 79 {
|
||||
// Do not call Literal::byte_character nor Literal::c_string. They can
|
||||
// be emulated by way of Literal::from_str.
|
||||
println!("cargo:rustc-cfg=no_literal_byte_character");
|
||||
println!("cargo:rustc-cfg=no_literal_c_string");
|
||||
}
|
||||
|
||||
if !cfg!(feature = "proc-macro") {
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
return;
|
||||
}
|
||||
|
||||
let proc_macro_span;
|
||||
let consider_rustc_bootstrap;
|
||||
if compile_probe_unstable("proc_macro_span", false) {
|
||||
// This is a nightly or dev compiler, so it supports unstable features
|
||||
// regardless of RUSTC_BOOTSTRAP. No need to rerun build script if
|
||||
// RUSTC_BOOTSTRAP is changed.
|
||||
proc_macro_span = true;
|
||||
consider_rustc_bootstrap = false;
|
||||
} else if let Some(rustc_bootstrap) = env::var_os("RUSTC_BOOTSTRAP") {
|
||||
if compile_probe_unstable("proc_macro_span", true) {
|
||||
// This is a stable or beta compiler for which the user has set
|
||||
// RUSTC_BOOTSTRAP to turn on unstable features. Rerun build script
|
||||
// if they change it.
|
||||
proc_macro_span = true;
|
||||
consider_rustc_bootstrap = true;
|
||||
} else if rustc_bootstrap == "1" {
|
||||
// This compiler does not support the proc macro Span API in the
|
||||
// form that proc-macro2 expects. No need to pay attention to
|
||||
// RUSTC_BOOTSTRAP.
|
||||
proc_macro_span = false;
|
||||
consider_rustc_bootstrap = false;
|
||||
} else {
|
||||
// This is a stable or beta compiler for which RUSTC_BOOTSTRAP is
|
||||
// set to restrict the use of unstable features by this crate.
|
||||
proc_macro_span = false;
|
||||
consider_rustc_bootstrap = true;
|
||||
}
|
||||
} else {
|
||||
// Without RUSTC_BOOTSTRAP, this compiler does not support the proc
|
||||
// macro Span API in the form that proc-macro2 expects, but try again if
|
||||
// the user turns on unstable features.
|
||||
proc_macro_span = false;
|
||||
consider_rustc_bootstrap = true;
|
||||
}
|
||||
|
||||
if proc_macro_span || !semver_exempt {
|
||||
// Wrap types from libproc_macro rather than polyfilling the whole API.
|
||||
// Enabled as long as procmacro2_semver_exempt is not set, because we
|
||||
// can't emulate the unstable API without emulating everything else.
|
||||
// Also enabled unconditionally on nightly, in which case the
|
||||
// procmacro2_semver_exempt surface area is implemented by using the
|
||||
// nightly-only proc_macro API.
|
||||
println!("cargo:rustc-cfg=wrap_proc_macro");
|
||||
}
|
||||
|
||||
if proc_macro_span {
|
||||
// Enable non-dummy behavior of Span::byte_range and Span::join methods
|
||||
// which requires an unstable compiler feature. Enabled when building
|
||||
// with nightly, unless `-Z allow-feature` in RUSTFLAGS disallows
|
||||
// unstable features.
|
||||
println!("cargo:rustc-cfg=proc_macro_span");
|
||||
}
|
||||
|
||||
if proc_macro_span || (rustc >= 88 && compile_probe_stable("proc_macro_span_location")) {
|
||||
// Enable non-dummy behavior of Span::start and Span::end methods on
|
||||
// Rust 1.88+.
|
||||
println!("cargo:rustc-cfg=proc_macro_span_location");
|
||||
}
|
||||
|
||||
if proc_macro_span || (rustc >= 88 && compile_probe_stable("proc_macro_span_file")) {
|
||||
// Enable non-dummy behavior of Span::file and Span::local_file methods
|
||||
// on Rust 1.88+.
|
||||
println!("cargo:rustc-cfg=proc_macro_span_file");
|
||||
}
|
||||
|
||||
if semver_exempt && proc_macro_span {
|
||||
// Implement the semver exempt API in terms of the nightly-only
|
||||
// proc_macro API.
|
||||
println!("cargo:rustc-cfg=super_unstable");
|
||||
}
|
||||
|
||||
if consider_rustc_bootstrap {
|
||||
println!("cargo:rerun-if-env-changed=RUSTC_BOOTSTRAP");
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_probe_unstable(feature: &str, rustc_bootstrap: bool) -> bool {
|
||||
// RUSTC_STAGE indicates that this crate is being compiled as a dependency
|
||||
// of a multistage rustc bootstrap. This environment uses Cargo in a highly
|
||||
// non-standard way with issues such as:
|
||||
//
|
||||
// https://github.com/rust-lang/cargo/issues/11138
|
||||
// https://github.com/rust-lang/rust/issues/114839
|
||||
//
|
||||
env::var_os("RUSTC_STAGE").is_none() && do_compile_probe(feature, rustc_bootstrap)
|
||||
}
|
||||
|
||||
fn compile_probe_stable(feature: &str) -> bool {
|
||||
env::var_os("RUSTC_STAGE").is_some() || do_compile_probe(feature, true)
|
||||
}
|
||||
|
||||
fn do_compile_probe(feature: &str, rustc_bootstrap: bool) -> bool {
|
||||
println!("cargo:rerun-if-changed=src/probe/{}.rs", feature);
|
||||
|
||||
let rustc = cargo_env_var("RUSTC");
|
||||
let out_dir = cargo_env_var("OUT_DIR");
|
||||
let out_subdir = Path::new(&out_dir).join("probe");
|
||||
let probefile = Path::new("src")
|
||||
.join("probe")
|
||||
.join(feature)
|
||||
.with_extension("rs");
|
||||
|
||||
if let Err(err) = fs::create_dir(&out_subdir) {
|
||||
if err.kind() != ErrorKind::AlreadyExists {
|
||||
eprintln!("Failed to create {}: {}", out_subdir.display(), err);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|wrapper| !wrapper.is_empty());
|
||||
let rustc_workspace_wrapper =
|
||||
env::var_os("RUSTC_WORKSPACE_WRAPPER").filter(|wrapper| !wrapper.is_empty());
|
||||
let mut rustc = rustc_wrapper
|
||||
.into_iter()
|
||||
.chain(rustc_workspace_wrapper)
|
||||
.chain(iter::once(rustc));
|
||||
let mut cmd = Command::new(rustc.next().unwrap());
|
||||
cmd.args(rustc);
|
||||
|
||||
if !rustc_bootstrap {
|
||||
cmd.env_remove("RUSTC_BOOTSTRAP");
|
||||
}
|
||||
|
||||
cmd.stderr(Stdio::null())
|
||||
.arg("--cfg=procmacro2_build_probe")
|
||||
.arg("--edition=2021")
|
||||
.arg("--crate-name=proc_macro2")
|
||||
.arg("--crate-type=lib")
|
||||
.arg("--cap-lints=allow")
|
||||
.arg("--emit=dep-info,metadata")
|
||||
.arg("--out-dir")
|
||||
.arg(&out_subdir)
|
||||
.arg(probefile);
|
||||
|
||||
if let Some(target) = env::var_os("TARGET") {
|
||||
cmd.arg("--target").arg(target);
|
||||
}
|
||||
|
||||
// If Cargo wants to set RUSTFLAGS, use that.
|
||||
if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
|
||||
if !rustflags.is_empty() {
|
||||
for arg in rustflags.split('\x1f') {
|
||||
cmd.arg(arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let success = match cmd.status() {
|
||||
Ok(status) => status.success(),
|
||||
Err(_) => false,
|
||||
};
|
||||
|
||||
// Clean up to avoid leaving nondeterministic absolute paths in the dep-info
|
||||
// file in OUT_DIR, which causes nonreproducible builds in build systems
|
||||
// that treat the entire OUT_DIR as an artifact.
|
||||
if let Err(err) = fs::remove_dir_all(&out_subdir) {
|
||||
// libc::ENOTEMPTY
|
||||
// Some filesystems (NFSv3) have timing issues under load where '.nfs*'
|
||||
// dummy files can continue to get created for a short period after the
|
||||
// probe command completes, breaking remove_dir_all.
|
||||
// To be replaced with ErrorKind::DirectoryNotEmpty (Rust 1.83+).
|
||||
const ENOTEMPTY: i32 = 39;
|
||||
|
||||
if !(err.kind() == ErrorKind::NotFound
|
||||
|| (cfg!(target_os = "linux") && err.raw_os_error() == Some(ENOTEMPTY)))
|
||||
{
|
||||
eprintln!("Failed to clean up {}: {}", out_subdir.display(), err);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
success
|
||||
}
|
||||
|
||||
fn rustc_minor_version() -> Option<u32> {
|
||||
let rustc = cargo_env_var("RUSTC");
|
||||
let output = Command::new(rustc).arg("--version").output().ok()?;
|
||||
let version = str::from_utf8(&output.stdout).ok()?;
|
||||
let mut pieces = version.split('.');
|
||||
if pieces.next() != Some("rustc 1") {
|
||||
return None;
|
||||
}
|
||||
pieces.next()?.parse().ok()
|
||||
}
|
||||
|
||||
fn cargo_env_var(key: &str) -> OsString {
|
||||
env::var_os(key).unwrap_or_else(|| {
|
||||
eprintln!(
|
||||
"Environment variable ${} is not set during execution of build script",
|
||||
key,
|
||||
);
|
||||
process::exit(1);
|
||||
})
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
[toolchain]
|
||||
components = ["rust-src"]
|
||||
Vendored
+75
@@ -0,0 +1,75 @@
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Once;
|
||||
|
||||
static WORKS: AtomicUsize = AtomicUsize::new(0);
|
||||
static INIT: Once = Once::new();
|
||||
|
||||
pub(crate) fn inside_proc_macro() -> bool {
|
||||
match WORKS.load(Ordering::Relaxed) {
|
||||
1 => return false,
|
||||
2 => return true,
|
||||
_ => {}
|
||||
}
|
||||
|
||||
INIT.call_once(initialize);
|
||||
inside_proc_macro()
|
||||
}
|
||||
|
||||
pub(crate) fn force_fallback() {
|
||||
WORKS.store(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub(crate) fn unforce_fallback() {
|
||||
initialize();
|
||||
}
|
||||
|
||||
#[cfg(not(no_is_available))]
|
||||
fn initialize() {
|
||||
let available = proc_macro::is_available();
|
||||
WORKS.store(available as usize + 1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
// Swap in a null panic hook to avoid printing "thread panicked" to stderr,
|
||||
// then use catch_unwind to determine whether the compiler's proc_macro is
|
||||
// working. When proc-macro2 is used from outside of a procedural macro all
|
||||
// of the proc_macro crate's APIs currently panic.
|
||||
//
|
||||
// The Once is to prevent the possibility of this ordering:
|
||||
//
|
||||
// thread 1 calls take_hook, gets the user's original hook
|
||||
// thread 1 calls set_hook with the null hook
|
||||
// thread 2 calls take_hook, thinks null hook is the original hook
|
||||
// thread 2 calls set_hook with the null hook
|
||||
// thread 1 calls set_hook with the actual original hook
|
||||
// thread 2 calls set_hook with what it thinks is the original hook
|
||||
//
|
||||
// in which the user's hook has been lost.
|
||||
//
|
||||
// There is still a race condition where a panic in a different thread can
|
||||
// happen during the interval that the user's original panic hook is
|
||||
// unregistered such that their hook is incorrectly not called. This is
|
||||
// sufficiently unlikely and less bad than printing panic messages to stderr
|
||||
// on correct use of this crate. Maybe there is a libstd feature request
|
||||
// here. For now, if a user needs to guarantee that this failure mode does
|
||||
// not occur, they need to call e.g. `proc_macro2::Span::call_site()` from
|
||||
// the main thread before launching any other threads.
|
||||
#[cfg(no_is_available)]
|
||||
fn initialize() {
|
||||
use std::panic::{self, PanicInfo};
|
||||
|
||||
type PanicHook = dyn Fn(&PanicInfo) + Sync + Send + 'static;
|
||||
|
||||
let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
|
||||
let sanity_check = &*null_hook as *const PanicHook;
|
||||
let original_hook = panic::take_hook();
|
||||
panic::set_hook(null_hook);
|
||||
|
||||
let works = panic::catch_unwind(proc_macro::Span::call_site).is_ok();
|
||||
WORKS.store(works as usize + 1, Ordering::Relaxed);
|
||||
|
||||
let hopefully_null_hook = panic::take_hook();
|
||||
panic::set_hook(original_hook);
|
||||
if sanity_check != &*hopefully_null_hook {
|
||||
panic!("observed race condition in proc_macro2::inside_proc_macro");
|
||||
}
|
||||
}
|
||||
Vendored
+151
@@ -0,0 +1,151 @@
|
||||
//! Items which do not have a correspondence to any API in the proc_macro crate,
|
||||
//! but are necessary to include in proc-macro2.
|
||||
|
||||
use crate::fallback;
|
||||
use crate::imp;
|
||||
use crate::marker::{ProcMacroAutoTraits, MARKER};
|
||||
use crate::Span;
|
||||
use core::fmt::{self, Debug};
|
||||
|
||||
/// Invalidate any `proc_macro2::Span` that exist on the current thread.
|
||||
///
|
||||
/// The implementation of `Span` uses thread-local data structures and this
|
||||
/// function clears them. Calling any method on a `Span` on the current thread
|
||||
/// created prior to the invalidation will return incorrect values or crash.
|
||||
///
|
||||
/// This function is useful for programs that process more than 2<sup>32</sup>
|
||||
/// bytes of Rust source code on the same thread. Just like rustc, proc-macro2
|
||||
/// uses 32-bit source locations, and these wrap around when the total source
|
||||
/// code processed by the same thread exceeds 2<sup>32</sup> bytes (4
|
||||
/// gigabytes). After a wraparound, `Span` methods such as `source_text()` can
|
||||
/// return wrong data.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// As of late 2023, there is 200 GB of Rust code published on crates.io.
|
||||
/// Looking at just the newest version of every crate, it is 16 GB of code. So a
|
||||
/// workload that involves parsing it all would overflow a 32-bit source
|
||||
/// location unless spans are being invalidated.
|
||||
///
|
||||
/// ```
|
||||
/// use flate2::read::GzDecoder;
|
||||
/// use std::ffi::OsStr;
|
||||
/// use std::io::{BufReader, Read};
|
||||
/// use std::str::FromStr;
|
||||
/// use tar::Archive;
|
||||
///
|
||||
/// rayon::scope(|s| {
|
||||
/// for krate in every_version_of_every_crate() {
|
||||
/// s.spawn(move |_| {
|
||||
/// proc_macro2::extra::invalidate_current_thread_spans();
|
||||
///
|
||||
/// let reader = BufReader::new(krate);
|
||||
/// let tar = GzDecoder::new(reader);
|
||||
/// let mut archive = Archive::new(tar);
|
||||
/// for entry in archive.entries().unwrap() {
|
||||
/// let mut entry = entry.unwrap();
|
||||
/// let path = entry.path().unwrap();
|
||||
/// if path.extension() != Some(OsStr::new("rs")) {
|
||||
/// continue;
|
||||
/// }
|
||||
/// let mut content = String::new();
|
||||
/// entry.read_to_string(&mut content).unwrap();
|
||||
/// match proc_macro2::TokenStream::from_str(&content) {
|
||||
/// Ok(tokens) => {/* ... */},
|
||||
/// Err(_) => continue,
|
||||
/// }
|
||||
/// }
|
||||
/// });
|
||||
/// }
|
||||
/// });
|
||||
/// #
|
||||
/// # fn every_version_of_every_crate() -> Vec<std::fs::File> {
|
||||
/// # Vec::new()
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function is not applicable to and will panic if called from a
|
||||
/// procedural macro.
|
||||
#[cfg(span_locations)]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))]
|
||||
pub fn invalidate_current_thread_spans() {
|
||||
crate::imp::invalidate_current_thread_spans();
|
||||
}
|
||||
|
||||
/// An object that holds a [`Group`]'s `span_open()` and `span_close()` together
|
||||
/// in a more compact representation than holding those 2 spans individually.
|
||||
///
|
||||
/// [`Group`]: crate::Group
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct DelimSpan {
|
||||
inner: DelimSpanEnum,
|
||||
_marker: ProcMacroAutoTraits,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
enum DelimSpanEnum {
|
||||
#[cfg(wrap_proc_macro)]
|
||||
Compiler {
|
||||
join: proc_macro::Span,
|
||||
open: proc_macro::Span,
|
||||
close: proc_macro::Span,
|
||||
},
|
||||
Fallback(fallback::Span),
|
||||
}
|
||||
|
||||
impl DelimSpan {
|
||||
pub(crate) fn new(group: &imp::Group) -> Self {
|
||||
#[cfg(wrap_proc_macro)]
|
||||
let inner = match group {
|
||||
imp::Group::Compiler(group) => DelimSpanEnum::Compiler {
|
||||
join: group.span(),
|
||||
open: group.span_open(),
|
||||
close: group.span_close(),
|
||||
},
|
||||
imp::Group::Fallback(group) => DelimSpanEnum::Fallback(group.span()),
|
||||
};
|
||||
|
||||
#[cfg(not(wrap_proc_macro))]
|
||||
let inner = DelimSpanEnum::Fallback(group.span());
|
||||
|
||||
DelimSpan {
|
||||
inner,
|
||||
_marker: MARKER,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a span covering the entire delimited group.
|
||||
pub fn join(&self) -> Span {
|
||||
match &self.inner {
|
||||
#[cfg(wrap_proc_macro)]
|
||||
DelimSpanEnum::Compiler { join, .. } => Span::_new(imp::Span::Compiler(*join)),
|
||||
DelimSpanEnum::Fallback(span) => Span::_new_fallback(*span),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a span for the opening punctuation of the group only.
|
||||
pub fn open(&self) -> Span {
|
||||
match &self.inner {
|
||||
#[cfg(wrap_proc_macro)]
|
||||
DelimSpanEnum::Compiler { open, .. } => Span::_new(imp::Span::Compiler(*open)),
|
||||
DelimSpanEnum::Fallback(span) => Span::_new_fallback(span.first_byte()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a span for the closing punctuation of the group only.
|
||||
pub fn close(&self) -> Span {
|
||||
match &self.inner {
|
||||
#[cfg(wrap_proc_macro)]
|
||||
DelimSpanEnum::Compiler { close, .. } => Span::_new(imp::Span::Compiler(*close)),
|
||||
DelimSpanEnum::Fallback(span) => Span::_new_fallback(span.last_byte()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for DelimSpan {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
Debug::fmt(&self.join(), f)
|
||||
}
|
||||
}
|
||||
Vendored
+1279
File diff suppressed because it is too large.
Load diff
Vendored
+1527
File diff suppressed because it is too large.
Load diff
Vendored
+29
@@ -0,0 +1,29 @@
|
||||
use core::cmp::Ordering;
|
||||
|
||||
/// A line-column pair representing the start or end of a `Span`.
|
||||
///
|
||||
/// This type is semver exempt and not exposed by default.
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct LineColumn {
|
||||
/// The 1-indexed line in the source file on which the span starts or ends
|
||||
/// (inclusive).
|
||||
pub line: usize,
|
||||
/// The 0-indexed column (in UTF-8 characters) in the source file on which
|
||||
/// the span starts or ends (inclusive).
|
||||
pub column: usize,
|
||||
}
|
||||
|
||||
impl Ord for LineColumn {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.line
|
||||
.cmp(&other.line)
|
||||
.then(self.column.cmp(&other.column))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for LineColumn {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
use alloc::rc::Rc;
|
||||
use core::marker::PhantomData;
|
||||
use core::panic::{RefUnwindSafe, UnwindSafe};
|
||||
|
||||
// Zero sized marker with the correct set of autotrait impls we want all proc
|
||||
// macro types to have.
|
||||
#[derive(Copy, Clone)]
|
||||
#[cfg_attr(
|
||||
all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)),
|
||||
derive(PartialEq, Eq)
|
||||
)]
|
||||
pub(crate) struct ProcMacroAutoTraits(PhantomData<Rc<()>>);
|
||||
|
||||
pub(crate) const MARKER: ProcMacroAutoTraits = ProcMacroAutoTraits(PhantomData);
|
||||
|
||||
impl UnwindSafe for ProcMacroAutoTraits {}
|
||||
impl RefUnwindSafe for ProcMacroAutoTraits {}
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
// TODO: use NonZero<char> in Rust 1.89+
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct NonZeroChar(char);
|
||||
|
||||
impl NonZeroChar {
|
||||
pub fn new(ch: char) -> Option<Self> {
|
||||
if ch == '\0' {
|
||||
None
|
||||
} else {
|
||||
Some(NonZeroChar(ch))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(self) -> char {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
Vendored
+991
@@ -0,0 +1,991 @@
|
||||
use crate::fallback::{
|
||||
self, is_ident_continue, is_ident_start, Group, Ident, LexError, Literal, Span, TokenStream,
|
||||
TokenStreamBuilder,
|
||||
};
|
||||
use crate::{Delimiter, Punct, Spacing, TokenTree};
|
||||
use alloc::borrow::ToOwned as _;
|
||||
use alloc::string::ToString as _;
|
||||
use alloc::vec::Vec;
|
||||
use core::char;
|
||||
use core::str::{Bytes, CharIndices, Chars};
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||
pub(crate) struct Cursor<'a> {
|
||||
pub(crate) rest: &'a str,
|
||||
#[cfg(span_locations)]
|
||||
pub(crate) off: u32,
|
||||
}
|
||||
|
||||
impl<'a> Cursor<'a> {
|
||||
pub(crate) fn advance(&self, bytes: usize) -> Cursor<'a> {
|
||||
let (_front, rest) = self.rest.split_at(bytes);
|
||||
Cursor {
|
||||
rest,
|
||||
#[cfg(span_locations)]
|
||||
off: self.off + _front.chars().count() as u32,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn starts_with(&self, s: &str) -> bool {
|
||||
self.rest.starts_with(s)
|
||||
}
|
||||
|
||||
pub(crate) fn starts_with_char(&self, ch: char) -> bool {
|
||||
self.rest.starts_with(ch)
|
||||
}
|
||||
|
||||
pub(crate) fn starts_with_fn<Pattern>(&self, f: Pattern) -> bool
|
||||
where
|
||||
Pattern: FnMut(char) -> bool,
|
||||
{
|
||||
self.rest.starts_with(f)
|
||||
}
|
||||
|
||||
pub(crate) fn is_empty(&self) -> bool {
|
||||
self.rest.is_empty()
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.rest.len()
|
||||
}
|
||||
|
||||
fn as_bytes(&self) -> &'a [u8] {
|
||||
self.rest.as_bytes()
|
||||
}
|
||||
|
||||
fn bytes(&self) -> Bytes<'a> {
|
||||
self.rest.bytes()
|
||||
}
|
||||
|
||||
fn chars(&self) -> Chars<'a> {
|
||||
self.rest.chars()
|
||||
}
|
||||
|
||||
fn char_indices(&self) -> CharIndices<'a> {
|
||||
self.rest.char_indices()
|
||||
}
|
||||
|
||||
fn parse(&self, tag: &str) -> Result<Cursor<'a>, Reject> {
|
||||
if self.starts_with(tag) {
|
||||
Ok(self.advance(tag.len()))
|
||||
} else {
|
||||
Err(Reject)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Reject;
|
||||
type PResult<'a, O> = Result<(Cursor<'a>, O), Reject>;
|
||||
|
||||
fn skip_whitespace(input: Cursor) -> Cursor {
|
||||
let mut s = input;
|
||||
|
||||
while !s.is_empty() {
|
||||
let byte = s.as_bytes()[0];
|
||||
if byte == b'/' {
|
||||
if s.starts_with("//")
|
||||
&& (!s.starts_with("///") || s.starts_with("////"))
|
||||
&& !s.starts_with("//!")
|
||||
{
|
||||
let (cursor, _) = take_until_newline_or_eof(s);
|
||||
s = cursor;
|
||||
continue;
|
||||
} else if s.starts_with("/**/") {
|
||||
s = s.advance(4);
|
||||
continue;
|
||||
} else if s.starts_with("/*")
|
||||
&& (!s.starts_with("/**") || s.starts_with("/***"))
|
||||
&& !s.starts_with("/*!")
|
||||
{
|
||||
match block_comment(s) {
|
||||
Ok((rest, _)) => {
|
||||
s = rest;
|
||||
continue;
|
||||
}
|
||||
Err(Reject) => return s,
|
||||
}
|
||||
}
|
||||
}
|
||||
match byte {
|
||||
b' ' | 0x09..=0x0d => {
|
||||
s = s.advance(1);
|
||||
continue;
|
||||
}
|
||||
b if b.is_ascii() => {}
|
||||
_ => {
|
||||
let ch = s.chars().next().unwrap();
|
||||
if is_whitespace(ch) {
|
||||
s = s.advance(ch.len_utf8());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn block_comment(input: Cursor) -> PResult<&str> {
|
||||
if !input.starts_with("/*") {
|
||||
return Err(Reject);
|
||||
}
|
||||
|
||||
let mut depth = 0usize;
|
||||
let bytes = input.as_bytes();
|
||||
let mut i = 0usize;
|
||||
let upper = bytes.len() - 1;
|
||||
|
||||
while i < upper {
|
||||
if bytes[i] == b'/' && bytes[i + 1] == b'*' {
|
||||
depth += 1;
|
||||
i += 1; // eat '*'
|
||||
} else if bytes[i] == b'*' && bytes[i + 1] == b'/' {
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
return Ok((input.advance(i + 2), &input.rest[..i + 2]));
|
||||
}
|
||||
i += 1; // eat '/'
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
Err(Reject)
|
||||
}
|
||||
|
||||
fn is_whitespace(ch: char) -> bool {
|
||||
// Rust treats left-to-right mark and right-to-left mark as whitespace
|
||||
ch.is_whitespace() || ch == '\u{200e}' || ch == '\u{200f}'
|
||||
}
|
||||
|
||||
fn word_break(input: Cursor) -> Result<Cursor, Reject> {
|
||||
match input.chars().next() {
|
||||
Some(ch) if is_ident_continue(ch) => Err(Reject),
|
||||
Some(_) | None => Ok(input),
|
||||
}
|
||||
}
|
||||
|
||||
// Rustc's representation of a macro expansion error in expression position or
|
||||
// type position.
|
||||
const ERROR: &str = "(/*ERROR*/)";
|
||||
|
||||
pub(crate) fn token_stream(mut input: Cursor) -> Result<TokenStream, LexError> {
|
||||
let mut tokens = TokenStreamBuilder::new();
|
||||
let mut stack = Vec::new();
|
||||
|
||||
loop {
|
||||
input = skip_whitespace(input);
|
||||
|
||||
if let Ok((rest, ())) = doc_comment(input, &mut tokens) {
|
||||
input = rest;
|
||||
continue;
|
||||
}
|
||||
|
||||
#[cfg(span_locations)]
|
||||
let lo = input.off;
|
||||
|
||||
let Some(first) = input.bytes().next() else {
|
||||
return match stack.last() {
|
||||
None => Ok(tokens.build()),
|
||||
#[cfg(span_locations)]
|
||||
Some((lo, _frame)) => Err(LexError {
|
||||
span: Span { lo: *lo, hi: *lo },
|
||||
}),
|
||||
#[cfg(not(span_locations))]
|
||||
Some(_frame) => Err(LexError { span: Span {} }),
|
||||
};
|
||||
};
|
||||
|
||||
if let Some(open_delimiter) = match first {
|
||||
b'(' if !input.starts_with(ERROR) => Some(Delimiter::Parenthesis),
|
||||
b'[' => Some(Delimiter::Bracket),
|
||||
b'{' => Some(Delimiter::Brace),
|
||||
_ => None,
|
||||
} {
|
||||
input = input.advance(1);
|
||||
let frame = (open_delimiter, tokens);
|
||||
#[cfg(span_locations)]
|
||||
let frame = (lo, frame);
|
||||
stack.push(frame);
|
||||
tokens = TokenStreamBuilder::new();
|
||||
} else if let Some(close_delimiter) = match first {
|
||||
b')' => Some(Delimiter::Parenthesis),
|
||||
b']' => Some(Delimiter::Bracket),
|
||||
b'}' => Some(Delimiter::Brace),
|
||||
_ => None,
|
||||
} {
|
||||
let Some(frame) = stack.pop() else {
|
||||
return Err(lex_error(input));
|
||||
};
|
||||
#[cfg(span_locations)]
|
||||
let (lo, frame) = frame;
|
||||
let (open_delimiter, outer) = frame;
|
||||
if open_delimiter != close_delimiter {
|
||||
return Err(lex_error(input));
|
||||
}
|
||||
input = input.advance(1);
|
||||
let mut g = Group::new(open_delimiter, tokens.build());
|
||||
g.set_span(Span {
|
||||
#[cfg(span_locations)]
|
||||
lo,
|
||||
#[cfg(span_locations)]
|
||||
hi: input.off,
|
||||
});
|
||||
tokens = outer;
|
||||
tokens.push_token_from_parser(TokenTree::Group(crate::Group::_new_fallback(g)));
|
||||
} else {
|
||||
let (rest, mut tt) = match leaf_token(input) {
|
||||
Ok((rest, tt)) => (rest, tt),
|
||||
Err(Reject) => return Err(lex_error(input)),
|
||||
};
|
||||
tt.set_span(crate::Span::_new_fallback(Span {
|
||||
#[cfg(span_locations)]
|
||||
lo,
|
||||
#[cfg(span_locations)]
|
||||
hi: rest.off,
|
||||
}));
|
||||
tokens.push_token_from_parser(tt);
|
||||
input = rest;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn lex_error(cursor: Cursor) -> LexError {
|
||||
#[cfg(not(span_locations))]
|
||||
let _ = cursor;
|
||||
LexError {
|
||||
span: Span {
|
||||
#[cfg(span_locations)]
|
||||
lo: cursor.off,
|
||||
#[cfg(span_locations)]
|
||||
hi: cursor.off,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn leaf_token(input: Cursor) -> PResult<TokenTree> {
|
||||
if let Ok((input, l)) = literal(input) {
|
||||
// must be parsed before ident
|
||||
Ok((input, TokenTree::Literal(crate::Literal::_new_fallback(l))))
|
||||
} else if let Ok((input, p)) = punct(input) {
|
||||
Ok((input, TokenTree::Punct(p)))
|
||||
} else if let Ok((input, i)) = ident(input) {
|
||||
Ok((input, TokenTree::Ident(i)))
|
||||
} else if input.starts_with(ERROR) {
|
||||
let rest = input.advance(ERROR.len());
|
||||
let repr = crate::Literal::_new_fallback(Literal::_new(ERROR.to_owned()));
|
||||
Ok((rest, TokenTree::Literal(repr)))
|
||||
} else {
|
||||
Err(Reject)
|
||||
}
|
||||
}
|
||||
|
||||
fn ident(input: Cursor) -> PResult<crate::Ident> {
|
||||
if [
|
||||
"r\"", "r#\"", "r##", "b\"", "b\'", "br\"", "br#", "c\"", "cr\"", "cr#",
|
||||
]
|
||||
.iter()
|
||||
.any(|prefix| input.starts_with(prefix))
|
||||
{
|
||||
Err(Reject)
|
||||
} else {
|
||||
ident_any(input)
|
||||
}
|
||||
}
|
||||
|
||||
fn ident_any(input: Cursor) -> PResult<crate::Ident> {
|
||||
let raw = input.starts_with("r#");
|
||||
let rest = input.advance((raw as usize) << 1);
|
||||
|
||||
let (rest, sym) = ident_not_raw(rest)?;
|
||||
|
||||
if !raw {
|
||||
let ident =
|
||||
crate::Ident::_new_fallback(Ident::new_unchecked(sym, fallback::Span::call_site()));
|
||||
return Ok((rest, ident));
|
||||
}
|
||||
|
||||
match sym {
|
||||
"_" | "super" | "self" | "Self" | "crate" => return Err(Reject),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let ident =
|
||||
crate::Ident::_new_fallback(Ident::new_raw_unchecked(sym, fallback::Span::call_site()));
|
||||
Ok((rest, ident))
|
||||
}
|
||||
|
||||
fn ident_not_raw(input: Cursor) -> PResult<&str> {
|
||||
let mut chars = input.char_indices();
|
||||
|
||||
match chars.next() {
|
||||
Some((_, ch)) if is_ident_start(ch) => {}
|
||||
_ => return Err(Reject),
|
||||
}
|
||||
|
||||
let mut end = input.len();
|
||||
for (i, ch) in chars {
|
||||
if !is_ident_continue(ch) {
|
||||
end = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok((input.advance(end), &input.rest[..end]))
|
||||
}
|
||||
|
||||
pub(crate) fn literal(input: Cursor) -> PResult<Literal> {
|
||||
let rest = literal_nocapture(input)?;
|
||||
let end = input.len() - rest.len();
|
||||
Ok((rest, Literal::_new(input.rest[..end].to_string())))
|
||||
}
|
||||
|
||||
fn literal_nocapture(input: Cursor) -> Result<Cursor, Reject> {
|
||||
if let Ok(ok) = string(input) {
|
||||
Ok(ok)
|
||||
} else if let Ok(ok) = byte_string(input) {
|
||||
Ok(ok)
|
||||
} else if let Ok(ok) = c_string(input) {
|
||||
Ok(ok)
|
||||
} else if let Ok(ok) = byte(input) {
|
||||
Ok(ok)
|
||||
} else if let Ok(ok) = character(input) {
|
||||
Ok(ok)
|
||||
} else if let Ok(ok) = float(input) {
|
||||
Ok(ok)
|
||||
} else if let Ok(ok) = int(input) {
|
||||
Ok(ok)
|
||||
} else {
|
||||
Err(Reject)
|
||||
}
|
||||
}
|
||||
|
||||
fn literal_suffix(input: Cursor) -> Cursor {
|
||||
match ident_not_raw(input) {
|
||||
Ok((input, _)) => input,
|
||||
Err(Reject) => input,
|
||||
}
|
||||
}
|
||||
|
||||
fn string(input: Cursor) -> Result<Cursor, Reject> {
|
||||
if let Ok(input) = input.parse("\"") {
|
||||
cooked_string(input)
|
||||
} else if let Ok(input) = input.parse("r") {
|
||||
raw_string(input)
|
||||
} else {
|
||||
Err(Reject)
|
||||
}
|
||||
}
|
||||
|
||||
fn cooked_string(mut input: Cursor) -> Result<Cursor, Reject> {
|
||||
let mut chars = input.char_indices();
|
||||
|
||||
while let Some((i, ch)) = chars.next() {
|
||||
match ch {
|
||||
'"' => {
|
||||
let input = input.advance(i + 1);
|
||||
return Ok(literal_suffix(input));
|
||||
}
|
||||
'\r' => match chars.next() {
|
||||
Some((_, '\n')) => {}
|
||||
_ => break,
|
||||
},
|
||||
'\\' => match chars.next() {
|
||||
Some((_, 'x')) => {
|
||||
backslash_x_char(&mut chars)?;
|
||||
}
|
||||
Some((_, 'n' | 'r' | 't' | '\\' | '\'' | '"' | '0')) => {}
|
||||
Some((_, 'u')) => {
|
||||
backslash_u(&mut chars)?;
|
||||
}
|
||||
Some((newline, ch @ ('\n' | '\r'))) => {
|
||||
input = input.advance(newline + 1);
|
||||
trailing_backslash(&mut input, ch as u8)?;
|
||||
chars = input.char_indices();
|
||||
}
|
||||
_ => break,
|
||||
},
|
||||
_ch => {}
|
||||
}
|
||||
}
|
||||
Err(Reject)
|
||||
}
|
||||
|
||||
fn raw_string(input: Cursor) -> Result<Cursor, Reject> {
|
||||
let (input, delimiter) = delimiter_of_raw_string(input)?;
|
||||
let mut bytes = input.bytes().enumerate();
|
||||
while let Some((i, byte)) = bytes.next() {
|
||||
match byte {
|
||||
b'"' if input.rest[i + 1..].starts_with(delimiter) => {
|
||||
let rest = input.advance(i + 1 + delimiter.len());
|
||||
return Ok(literal_suffix(rest));
|
||||
}
|
||||
b'\r' => match bytes.next() {
|
||||
Some((_, b'\n')) => {}
|
||||
_ => break,
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Err(Reject)
|
||||
}
|
||||
|
||||
fn byte_string(input: Cursor) -> Result<Cursor, Reject> {
|
||||
if let Ok(input) = input.parse("b\"") {
|
||||
cooked_byte_string(input)
|
||||
} else if let Ok(input) = input.parse("br") {
|
||||
raw_byte_string(input)
|
||||
} else {
|
||||
Err(Reject)
|
||||
}
|
||||
}
|
||||
|
||||
fn cooked_byte_string(mut input: Cursor) -> Result<Cursor, Reject> {
|
||||
let mut bytes = input.bytes().enumerate();
|
||||
while let Some((offset, b)) = bytes.next() {
|
||||
match b {
|
||||
b'"' => {
|
||||
let input = input.advance(offset + 1);
|
||||
return Ok(literal_suffix(input));
|
||||
}
|
||||
b'\r' => match bytes.next() {
|
||||
Some((_, b'\n')) => {}
|
||||
_ => break,
|
||||
},
|
||||
b'\\' => match bytes.next() {
|
||||
Some((_, b'x')) => {
|
||||
backslash_x_byte(&mut bytes)?;
|
||||
}
|
||||
Some((_, b'n' | b'r' | b't' | b'\\' | b'0' | b'\'' | b'"')) => {}
|
||||
Some((newline, b @ (b'\n' | b'\r'))) => {
|
||||
input = input.advance(newline + 1);
|
||||
trailing_backslash(&mut input, b)?;
|
||||
bytes = input.bytes().enumerate();
|
||||
}
|
||||
_ => break,
|
||||
},
|
||||
b if b.is_ascii() => {}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
Err(Reject)
|
||||
}
|
||||
|
||||
fn delimiter_of_raw_string(input: Cursor) -> PResult<&str> {
|
||||
for (i, byte) in input.bytes().enumerate() {
|
||||
match byte {
|
||||
b'"' => {
|
||||
if i > 255 {
|
||||
// https://github.com/rust-lang/rust/pull/95251
|
||||
return Err(Reject);
|
||||
}
|
||||
return Ok((input.advance(i + 1), &input.rest[..i]));
|
||||
}
|
||||
b'#' => {}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
Err(Reject)
|
||||
}
|
||||
|
||||
fn raw_byte_string(input: Cursor) -> Result<Cursor, Reject> {
|
||||
let (input, delimiter) = delimiter_of_raw_string(input)?;
|
||||
let mut bytes = input.bytes().enumerate();
|
||||
while let Some((i, byte)) = bytes.next() {
|
||||
match byte {
|
||||
b'"' if input.rest[i + 1..].starts_with(delimiter) => {
|
||||
let rest = input.advance(i + 1 + delimiter.len());
|
||||
return Ok(literal_suffix(rest));
|
||||
}
|
||||
b'\r' => match bytes.next() {
|
||||
Some((_, b'\n')) => {}
|
||||
_ => break,
|
||||
},
|
||||
other => {
|
||||
if !other.is_ascii() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(Reject)
|
||||
}
|
||||
|
||||
fn c_string(input: Cursor) -> Result<Cursor, Reject> {
|
||||
if let Ok(input) = input.parse("c\"") {
|
||||
cooked_c_string(input)
|
||||
} else if let Ok(input) = input.parse("cr") {
|
||||
raw_c_string(input)
|
||||
} else {
|
||||
Err(Reject)
|
||||
}
|
||||
}
|
||||
|
||||
fn raw_c_string(input: Cursor) -> Result<Cursor, Reject> {
|
||||
let (input, delimiter) = delimiter_of_raw_string(input)?;
|
||||
let mut bytes = input.bytes().enumerate();
|
||||
while let Some((i, byte)) = bytes.next() {
|
||||
match byte {
|
||||
b'"' if input.rest[i + 1..].starts_with(delimiter) => {
|
||||
let rest = input.advance(i + 1 + delimiter.len());
|
||||
return Ok(literal_suffix(rest));
|
||||
}
|
||||
b'\r' => match bytes.next() {
|
||||
Some((_, b'\n')) => {}
|
||||
_ => break,
|
||||
},
|
||||
b'\0' => break,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Err(Reject)
|
||||
}
|
||||
|
||||
fn cooked_c_string(mut input: Cursor) -> Result<Cursor, Reject> {
|
||||
let mut chars = input.char_indices();
|
||||
|
||||
while let Some((i, ch)) = chars.next() {
|
||||
match ch {
|
||||
'"' => {
|
||||
let input = input.advance(i + 1);
|
||||
return Ok(literal_suffix(input));
|
||||
}
|
||||
'\r' => match chars.next() {
|
||||
Some((_, '\n')) => {}
|
||||
_ => break,
|
||||
},
|
||||
'\\' => match chars.next() {
|
||||
Some((_, 'x')) => {
|
||||
backslash_x_nonzero(&mut chars)?;
|
||||
}
|
||||
Some((_, 'n' | 'r' | 't' | '\\' | '\'' | '"')) => {}
|
||||
Some((_, 'u')) => {
|
||||
if backslash_u(&mut chars)? == '\0' {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Some((newline, ch @ ('\n' | '\r'))) => {
|
||||
input = input.advance(newline + 1);
|
||||
trailing_backslash(&mut input, ch as u8)?;
|
||||
chars = input.char_indices();
|
||||
}
|
||||
_ => break,
|
||||
},
|
||||
'\0' => break,
|
||||
_ch => {}
|
||||
}
|
||||
}
|
||||
Err(Reject)
|
||||
}
|
||||
|
||||
fn byte(input: Cursor) -> Result<Cursor, Reject> {
|
||||
let input = input.parse("b'")?;
|
||||
let mut bytes = input.bytes().enumerate();
|
||||
let ok = match bytes.next().map(|(_, b)| b) {
|
||||
Some(b'\\') => match bytes.next().map(|(_, b)| b) {
|
||||
Some(b'x') => backslash_x_byte(&mut bytes).is_ok(),
|
||||
Some(b'n' | b'r' | b't' | b'\\' | b'0' | b'\'' | b'"') => true,
|
||||
_ => false,
|
||||
},
|
||||
b => b.is_some(),
|
||||
};
|
||||
if !ok {
|
||||
return Err(Reject);
|
||||
}
|
||||
let (offset, _) = bytes.next().ok_or(Reject)?;
|
||||
if !input.chars().as_str().is_char_boundary(offset) {
|
||||
return Err(Reject);
|
||||
}
|
||||
let input = input.advance(offset).parse("'")?;
|
||||
Ok(literal_suffix(input))
|
||||
}
|
||||
|
||||
fn character(input: Cursor) -> Result<Cursor, Reject> {
|
||||
let input = input.parse("'")?;
|
||||
let mut chars = input.char_indices();
|
||||
let ok = match chars.next().map(|(_, ch)| ch) {
|
||||
Some('\\') => match chars.next().map(|(_, ch)| ch) {
|
||||
Some('x') => backslash_x_char(&mut chars).is_ok(),
|
||||
Some('u') => backslash_u(&mut chars).is_ok(),
|
||||
Some('n' | 'r' | 't' | '\\' | '0' | '\'' | '"') => true,
|
||||
_ => false,
|
||||
},
|
||||
ch => ch.is_some(),
|
||||
};
|
||||
if !ok {
|
||||
return Err(Reject);
|
||||
}
|
||||
let (idx, _) = chars.next().ok_or(Reject)?;
|
||||
let input = input.advance(idx).parse("'")?;
|
||||
Ok(literal_suffix(input))
|
||||
}
|
||||
|
||||
macro_rules! next_ch {
|
||||
($chars:ident @ $pat:pat) => {
|
||||
match $chars.next() {
|
||||
Some((_, ch)) => match ch {
|
||||
$pat => ch,
|
||||
_ => return Err(Reject),
|
||||
},
|
||||
None => return Err(Reject),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn backslash_x_char<I>(chars: &mut I) -> Result<(), Reject>
|
||||
where
|
||||
I: Iterator<Item = (usize, char)>,
|
||||
{
|
||||
next_ch!(chars @ '0'..='7');
|
||||
next_ch!(chars @ '0'..='9' | 'a'..='f' | 'A'..='F');
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn backslash_x_byte<I>(chars: &mut I) -> Result<(), Reject>
|
||||
where
|
||||
I: Iterator<Item = (usize, u8)>,
|
||||
{
|
||||
next_ch!(chars @ b'0'..=b'9' | b'a'..=b'f' | b'A'..=b'F');
|
||||
next_ch!(chars @ b'0'..=b'9' | b'a'..=b'f' | b'A'..=b'F');
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn backslash_x_nonzero<I>(chars: &mut I) -> Result<(), Reject>
|
||||
where
|
||||
I: Iterator<Item = (usize, char)>,
|
||||
{
|
||||
let first = next_ch!(chars @ '0'..='9' | 'a'..='f' | 'A'..='F');
|
||||
let second = next_ch!(chars @ '0'..='9' | 'a'..='f' | 'A'..='F');
|
||||
if first == '0' && second == '0' {
|
||||
Err(Reject)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn backslash_u<I>(chars: &mut I) -> Result<char, Reject>
|
||||
where
|
||||
I: Iterator<Item = (usize, char)>,
|
||||
{
|
||||
next_ch!(chars @ '{');
|
||||
let mut value = 0;
|
||||
let mut len = 0;
|
||||
for (_, ch) in chars {
|
||||
let digit = match ch {
|
||||
'0'..='9' => ch as u8 - b'0',
|
||||
'a'..='f' => 10 + ch as u8 - b'a',
|
||||
'A'..='F' => 10 + ch as u8 - b'A',
|
||||
'_' if len > 0 => continue,
|
||||
'}' if len > 0 => return char::from_u32(value).ok_or(Reject),
|
||||
_ => break,
|
||||
};
|
||||
if len == 6 {
|
||||
break;
|
||||
}
|
||||
value *= 0x10;
|
||||
value += u32::from(digit);
|
||||
len += 1;
|
||||
}
|
||||
Err(Reject)
|
||||
}
|
||||
|
||||
fn trailing_backslash(input: &mut Cursor, mut last: u8) -> Result<(), Reject> {
|
||||
let mut whitespace = input.bytes().enumerate();
|
||||
loop {
|
||||
if last == b'\r' && whitespace.next().map_or(true, |(_, b)| b != b'\n') {
|
||||
return Err(Reject);
|
||||
}
|
||||
match whitespace.next() {
|
||||
Some((_, b @ (b' ' | b'\t' | b'\n' | b'\r'))) => {
|
||||
last = b;
|
||||
}
|
||||
Some((offset, _)) => {
|
||||
*input = input.advance(offset);
|
||||
return Ok(());
|
||||
}
|
||||
None => return Err(Reject),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn float(input: Cursor) -> Result<Cursor, Reject> {
|
||||
let mut rest = float_digits(input)?;
|
||||
if let Some(ch) = rest.chars().next() {
|
||||
if is_ident_start(ch) {
|
||||
rest = ident_not_raw(rest)?.0;
|
||||
}
|
||||
}
|
||||
word_break(rest)
|
||||
}
|
||||
|
||||
fn float_digits(input: Cursor) -> Result<Cursor, Reject> {
|
||||
let mut chars = input.chars().peekable();
|
||||
match chars.next() {
|
||||
Some(ch) if '0' <= ch && ch <= '9' => {}
|
||||
_ => return Err(Reject),
|
||||
}
|
||||
|
||||
let mut len = 1;
|
||||
let mut has_dot = false;
|
||||
let mut has_exp = false;
|
||||
while let Some(&ch) = chars.peek() {
|
||||
match ch {
|
||||
'0'..='9' | '_' => {
|
||||
chars.next();
|
||||
len += 1;
|
||||
}
|
||||
'.' => {
|
||||
if has_dot {
|
||||
break;
|
||||
}
|
||||
chars.next();
|
||||
if chars
|
||||
.peek()
|
||||
.map_or(false, |&ch| ch == '.' || is_ident_start(ch))
|
||||
{
|
||||
return Err(Reject);
|
||||
}
|
||||
len += 1;
|
||||
has_dot = true;
|
||||
}
|
||||
'e' | 'E' => {
|
||||
chars.next();
|
||||
len += 1;
|
||||
has_exp = true;
|
||||
break;
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
|
||||
if !(has_dot || has_exp) {
|
||||
return Err(Reject);
|
||||
}
|
||||
|
||||
if has_exp {
|
||||
let token_before_exp = if has_dot {
|
||||
Ok(input.advance(len - 1))
|
||||
} else {
|
||||
Err(Reject)
|
||||
};
|
||||
let mut has_sign = false;
|
||||
let mut has_exp_value = false;
|
||||
while let Some(&ch) = chars.peek() {
|
||||
match ch {
|
||||
'+' | '-' => {
|
||||
if has_exp_value {
|
||||
break;
|
||||
}
|
||||
if has_sign {
|
||||
return token_before_exp;
|
||||
}
|
||||
chars.next();
|
||||
len += 1;
|
||||
has_sign = true;
|
||||
}
|
||||
'0'..='9' => {
|
||||
chars.next();
|
||||
len += 1;
|
||||
has_exp_value = true;
|
||||
}
|
||||
'_' => {
|
||||
chars.next();
|
||||
len += 1;
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
if !has_exp_value {
|
||||
return token_before_exp;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(input.advance(len))
|
||||
}
|
||||
|
||||
fn int(input: Cursor) -> Result<Cursor, Reject> {
|
||||
let mut rest = digits(input)?;
|
||||
if let Some(ch) = rest.chars().next() {
|
||||
if is_ident_start(ch) {
|
||||
rest = ident_not_raw(rest)?.0;
|
||||
}
|
||||
}
|
||||
word_break(rest)
|
||||
}
|
||||
|
||||
fn digits(mut input: Cursor) -> Result<Cursor, Reject> {
|
||||
let base = if input.starts_with("0x") {
|
||||
input = input.advance(2);
|
||||
16
|
||||
} else if input.starts_with("0o") {
|
||||
input = input.advance(2);
|
||||
8
|
||||
} else if input.starts_with("0b") {
|
||||
input = input.advance(2);
|
||||
2
|
||||
} else {
|
||||
10
|
||||
};
|
||||
|
||||
let mut len = 0;
|
||||
let mut empty = true;
|
||||
for b in input.bytes() {
|
||||
match b {
|
||||
b'0'..=b'9' => {
|
||||
let digit = (b - b'0') as u64;
|
||||
if digit >= base {
|
||||
return Err(Reject);
|
||||
}
|
||||
}
|
||||
b'a'..=b'f' => {
|
||||
let digit = 10 + (b - b'a') as u64;
|
||||
if digit >= base {
|
||||
break;
|
||||
}
|
||||
}
|
||||
b'A'..=b'F' => {
|
||||
let digit = 10 + (b - b'A') as u64;
|
||||
if digit >= base {
|
||||
break;
|
||||
}
|
||||
}
|
||||
b'_' => {
|
||||
if empty && base == 10 {
|
||||
return Err(Reject);
|
||||
}
|
||||
len += 1;
|
||||
continue;
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
len += 1;
|
||||
empty = false;
|
||||
}
|
||||
if empty {
|
||||
Err(Reject)
|
||||
} else {
|
||||
Ok(input.advance(len))
|
||||
}
|
||||
}
|
||||
|
||||
fn punct(input: Cursor) -> PResult<Punct> {
|
||||
let (rest, ch) = punct_char(input)?;
|
||||
if ch == '\'' {
|
||||
let (after_lifetime, _ident) = ident_any(rest)?;
|
||||
if after_lifetime.starts_with_char('\'')
|
||||
|| (after_lifetime.starts_with_char('#') && !rest.starts_with("r#"))
|
||||
{
|
||||
Err(Reject)
|
||||
} else {
|
||||
Ok((rest, Punct::new('\'', Spacing::Joint)))
|
||||
}
|
||||
} else {
|
||||
let kind = match punct_char(rest) {
|
||||
Ok(_) => Spacing::Joint,
|
||||
Err(Reject) => Spacing::Alone,
|
||||
};
|
||||
Ok((rest, Punct::new(ch, kind)))
|
||||
}
|
||||
}
|
||||
|
||||
fn punct_char(input: Cursor) -> PResult<char> {
|
||||
if input.starts_with("//") || input.starts_with("/*") {
|
||||
// Do not accept `/` of a comment as a punct.
|
||||
return Err(Reject);
|
||||
}
|
||||
|
||||
let mut chars = input.chars();
|
||||
let Some(first) = chars.next() else {
|
||||
return Err(Reject);
|
||||
};
|
||||
let recognized = "~!@#$%^&*-=+|;:,<.>/?'";
|
||||
if recognized.contains(first) {
|
||||
Ok((input.advance(first.len_utf8()), first))
|
||||
} else {
|
||||
Err(Reject)
|
||||
}
|
||||
}
|
||||
|
||||
fn doc_comment<'a>(input: Cursor<'a>, tokens: &mut TokenStreamBuilder) -> PResult<'a, ()> {
|
||||
#[cfg(span_locations)]
|
||||
let lo = input.off;
|
||||
let (rest, (comment, inner)) = doc_comment_contents(input)?;
|
||||
let fallback_span = Span {
|
||||
#[cfg(span_locations)]
|
||||
lo,
|
||||
#[cfg(span_locations)]
|
||||
hi: rest.off,
|
||||
};
|
||||
let span = crate::Span::_new_fallback(fallback_span);
|
||||
|
||||
let mut scan_for_bare_cr = comment;
|
||||
while let Some(cr) = scan_for_bare_cr.find('\r') {
|
||||
let rest = &scan_for_bare_cr[cr + 1..];
|
||||
if !rest.starts_with('\n') {
|
||||
return Err(Reject);
|
||||
}
|
||||
scan_for_bare_cr = rest;
|
||||
}
|
||||
|
||||
let mut pound = Punct::new('#', Spacing::Alone);
|
||||
pound.set_span(span);
|
||||
tokens.push_token_from_parser(TokenTree::Punct(pound));
|
||||
|
||||
if inner {
|
||||
let mut bang = Punct::new('!', Spacing::Alone);
|
||||
bang.set_span(span);
|
||||
tokens.push_token_from_parser(TokenTree::Punct(bang));
|
||||
}
|
||||
|
||||
let doc_ident = crate::Ident::_new_fallback(Ident::new_unchecked("doc", fallback_span));
|
||||
let mut equal = Punct::new('=', Spacing::Alone);
|
||||
equal.set_span(span);
|
||||
let mut literal = crate::Literal::_new_fallback(Literal::string(comment));
|
||||
literal.set_span(span);
|
||||
let mut bracketed = TokenStreamBuilder::with_capacity(3);
|
||||
bracketed.push_token_from_parser(TokenTree::Ident(doc_ident));
|
||||
bracketed.push_token_from_parser(TokenTree::Punct(equal));
|
||||
bracketed.push_token_from_parser(TokenTree::Literal(literal));
|
||||
let group = Group::new(Delimiter::Bracket, bracketed.build());
|
||||
let mut group = crate::Group::_new_fallback(group);
|
||||
group.set_span(span);
|
||||
tokens.push_token_from_parser(TokenTree::Group(group));
|
||||
|
||||
Ok((rest, ()))
|
||||
}
|
||||
|
||||
fn doc_comment_contents(input: Cursor) -> PResult<(&str, bool)> {
|
||||
if input.starts_with("//!") {
|
||||
let input = input.advance(3);
|
||||
let (input, s) = take_until_newline_or_eof(input);
|
||||
Ok((input, (s, true)))
|
||||
} else if input.starts_with("/*!") {
|
||||
let (input, s) = block_comment(input)?;
|
||||
Ok((input, (&s[3..s.len() - 2], true)))
|
||||
} else if input.starts_with("///") {
|
||||
let input = input.advance(3);
|
||||
if input.starts_with_char('/') {
|
||||
return Err(Reject);
|
||||
}
|
||||
let (input, s) = take_until_newline_or_eof(input);
|
||||
Ok((input, (s, false)))
|
||||
} else if input.starts_with("/**") && !input.rest[3..].starts_with('*') {
|
||||
let (input, s) = block_comment(input)?;
|
||||
Ok((input, (&s[3..s.len() - 2], false)))
|
||||
} else {
|
||||
Err(Reject)
|
||||
}
|
||||
}
|
||||
|
||||
fn take_until_newline_or_eof(input: Cursor) -> (Cursor, &str) {
|
||||
let chars = input.char_indices();
|
||||
|
||||
for (i, ch) in chars {
|
||||
if ch == '\n' {
|
||||
return (input.advance(i), &input.rest[..i]);
|
||||
} else if ch == '\r' && input.rest[i + 1..].starts_with('\n') {
|
||||
return (input.advance(i + 1), &input.rest[..i]);
|
||||
}
|
||||
}
|
||||
|
||||
(input.advance(input.len()), input.rest)
|
||||
}
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
#[cfg(proc_macro_span)]
|
||||
pub(crate) mod proc_macro_span;
|
||||
|
||||
#[cfg(proc_macro_span_file)]
|
||||
pub(crate) mod proc_macro_span_file;
|
||||
|
||||
#[cfg(proc_macro_span_location)]
|
||||
pub(crate) mod proc_macro_span_location;
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
// This code exercises the surface area that we expect of Span's unstable API.
|
||||
// If the current toolchain is able to compile it, then proc-macro2 is able to
|
||||
// offer these APIs too.
|
||||
|
||||
#![cfg_attr(procmacro2_build_probe, no_std)]
|
||||
#![cfg_attr(procmacro2_build_probe, feature(proc_macro_span))]
|
||||
|
||||
extern crate alloc;
|
||||
extern crate proc_macro;
|
||||
extern crate std;
|
||||
|
||||
use alloc::string::String;
|
||||
use core::ops::{Range, RangeBounds};
|
||||
use proc_macro::{Literal, Span};
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn byte_range(this: &Span) -> Range<usize> {
|
||||
this.byte_range()
|
||||
}
|
||||
|
||||
pub fn start(this: &Span) -> Span {
|
||||
this.start()
|
||||
}
|
||||
|
||||
pub fn end(this: &Span) -> Span {
|
||||
this.end()
|
||||
}
|
||||
|
||||
pub fn line(this: &Span) -> usize {
|
||||
this.line()
|
||||
}
|
||||
|
||||
pub fn column(this: &Span) -> usize {
|
||||
this.column()
|
||||
}
|
||||
|
||||
pub fn file(this: &Span) -> String {
|
||||
this.file()
|
||||
}
|
||||
|
||||
pub fn local_file(this: &Span) -> Option<PathBuf> {
|
||||
this.local_file()
|
||||
}
|
||||
|
||||
pub fn join(this: &Span, other: Span) -> Option<Span> {
|
||||
this.join(other)
|
||||
}
|
||||
|
||||
pub fn subspan<R: RangeBounds<usize>>(this: &Literal, range: R) -> Option<Span> {
|
||||
this.subspan(range)
|
||||
}
|
||||
|
||||
// Include in sccache cache key.
|
||||
#[cfg(procmacro2_build_probe)]
|
||||
const _: Option<&str> = option_env!("RUSTC_BOOTSTRAP");
|
||||
@@ -0,0 +1,19 @@
|
||||
// The subset of Span's API stabilized in Rust 1.88.
|
||||
|
||||
#![cfg_attr(procmacro2_build_probe, no_std)]
|
||||
|
||||
extern crate alloc;
|
||||
extern crate proc_macro;
|
||||
extern crate std;
|
||||
|
||||
use alloc::string::String;
|
||||
use proc_macro::Span;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn file(this: &Span) -> String {
|
||||
this.file()
|
||||
}
|
||||
|
||||
pub fn local_file(this: &Span) -> Option<PathBuf> {
|
||||
this.local_file()
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// The subset of Span's API stabilized in Rust 1.88.
|
||||
|
||||
#![cfg_attr(procmacro2_build_probe, no_std)]
|
||||
|
||||
extern crate alloc;
|
||||
extern crate proc_macro;
|
||||
extern crate std;
|
||||
|
||||
use proc_macro::Span;
|
||||
|
||||
pub fn start(this: &Span) -> Span {
|
||||
this.start()
|
||||
}
|
||||
|
||||
pub fn end(this: &Span) -> Span {
|
||||
this.end()
|
||||
}
|
||||
|
||||
pub fn line(this: &Span) -> usize {
|
||||
this.line()
|
||||
}
|
||||
|
||||
pub fn column(this: &Span) -> usize {
|
||||
this.column()
|
||||
}
|
||||
Vendored
+146
@@ -0,0 +1,146 @@
|
||||
use alloc::rc::Rc;
|
||||
use alloc::vec::{self, Vec};
|
||||
use core::mem;
|
||||
use core::panic::RefUnwindSafe;
|
||||
use core::slice;
|
||||
|
||||
pub(crate) struct RcVec<T> {
|
||||
inner: Rc<Vec<T>>,
|
||||
}
|
||||
|
||||
pub(crate) struct RcVecBuilder<T> {
|
||||
inner: Vec<T>,
|
||||
}
|
||||
|
||||
pub(crate) struct RcVecMut<'a, T> {
|
||||
inner: &'a mut Vec<T>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct RcVecIntoIter<T> {
|
||||
inner: vec::IntoIter<T>,
|
||||
}
|
||||
|
||||
impl<T> RcVec<T> {
|
||||
pub(crate) fn is_empty(&self) -> bool {
|
||||
self.inner.is_empty()
|
||||
}
|
||||
|
||||
pub(crate) fn len(&self) -> usize {
|
||||
self.inner.len()
|
||||
}
|
||||
|
||||
pub(crate) fn iter(&self) -> slice::Iter<T> {
|
||||
self.inner.iter()
|
||||
}
|
||||
|
||||
pub(crate) fn make_mut(&mut self) -> RcVecMut<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
RcVecMut {
|
||||
inner: Rc::make_mut(&mut self.inner),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_mut(&mut self) -> Option<RcVecMut<T>> {
|
||||
let inner = Rc::get_mut(&mut self.inner)?;
|
||||
Some(RcVecMut { inner })
|
||||
}
|
||||
|
||||
pub(crate) fn make_owned(mut self) -> RcVecBuilder<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
let vec = if let Some(owned) = Rc::get_mut(&mut self.inner) {
|
||||
mem::take(owned)
|
||||
} else {
|
||||
Vec::clone(&self.inner)
|
||||
};
|
||||
RcVecBuilder { inner: vec }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> RcVecBuilder<T> {
|
||||
pub(crate) fn new() -> Self {
|
||||
RcVecBuilder { inner: Vec::new() }
|
||||
}
|
||||
|
||||
pub(crate) fn with_capacity(cap: usize) -> Self {
|
||||
RcVecBuilder {
|
||||
inner: Vec::with_capacity(cap),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn push(&mut self, element: T) {
|
||||
self.inner.push(element);
|
||||
}
|
||||
|
||||
pub(crate) fn extend(&mut self, iter: impl IntoIterator<Item = T>) {
|
||||
self.inner.extend(iter);
|
||||
}
|
||||
|
||||
pub(crate) fn as_mut(&mut self) -> RcVecMut<T> {
|
||||
RcVecMut {
|
||||
inner: &mut self.inner,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn build(self) -> RcVec<T> {
|
||||
RcVec {
|
||||
inner: Rc::new(self.inner),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> RcVecMut<'a, T> {
|
||||
pub(crate) fn push(&mut self, element: T) {
|
||||
self.inner.push(element);
|
||||
}
|
||||
|
||||
pub(crate) fn extend(&mut self, iter: impl IntoIterator<Item = T>) {
|
||||
self.inner.extend(iter);
|
||||
}
|
||||
|
||||
pub(crate) fn as_mut(&mut self) -> RcVecMut<T> {
|
||||
RcVecMut { inner: self.inner }
|
||||
}
|
||||
|
||||
pub(crate) fn take(self) -> RcVecBuilder<T> {
|
||||
let vec = mem::take(self.inner);
|
||||
RcVecBuilder { inner: vec }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for RcVec<T> {
|
||||
fn clone(&self) -> Self {
|
||||
RcVec {
|
||||
inner: Rc::clone(&self.inner),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoIterator for RcVecBuilder<T> {
|
||||
type Item = T;
|
||||
type IntoIter = RcVecIntoIter<T>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
RcVecIntoIter {
|
||||
inner: self.inner.into_iter(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Iterator for RcVecIntoIter<T> {
|
||||
type Item = T;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.inner.next()
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.inner.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> RefUnwindSafe for RcVec<T> where T: RefUnwindSafe {}
|
||||
+701
@@ -0,0 +1,701 @@
|
||||
// Vendored from rustc-literal-escaper v0.0.5
|
||||
// https://github.com/rust-lang/literal-escaper/tree/v0.0.5
|
||||
|
||||
//! Utilities for validating (raw) string, char, and byte literals and
|
||||
//! turning escape sequences into the values they represent.
|
||||
|
||||
use crate::num::NonZeroChar;
|
||||
use core::ffi::CStr;
|
||||
use core::num::NonZeroU8;
|
||||
use core::ops::Range;
|
||||
use core::str::Chars;
|
||||
|
||||
/// Errors and warnings that can occur during string, char, and byte unescaping.
|
||||
///
|
||||
/// Mostly relating to malformed escape sequences, but also a few other problems.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum EscapeError {
|
||||
/// Expected 1 char, but 0 were found.
|
||||
ZeroChars,
|
||||
/// Expected 1 char, but more than 1 were found.
|
||||
MoreThanOneChar,
|
||||
|
||||
/// Escaped '\' character without continuation.
|
||||
LoneSlash,
|
||||
/// Invalid escape character (e.g. '\z').
|
||||
InvalidEscape,
|
||||
/// Raw '\r' encountered.
|
||||
BareCarriageReturn,
|
||||
/// Raw '\r' encountered in raw string.
|
||||
BareCarriageReturnInRawString,
|
||||
/// Unescaped character that was expected to be escaped (e.g. raw '\t').
|
||||
EscapeOnlyChar,
|
||||
|
||||
/// Numeric character escape is too short (e.g. '\x1').
|
||||
TooShortHexEscape,
|
||||
/// Invalid character in numeric escape (e.g. '\xz')
|
||||
InvalidCharInHexEscape,
|
||||
/// Character code in numeric escape is non-ascii (e.g. '\xFF').
|
||||
OutOfRangeHexEscape,
|
||||
|
||||
/// '\u' not followed by '{'.
|
||||
NoBraceInUnicodeEscape,
|
||||
/// Non-hexadecimal value in '\u{..}'.
|
||||
InvalidCharInUnicodeEscape,
|
||||
/// '\u{}'
|
||||
EmptyUnicodeEscape,
|
||||
/// No closing brace in '\u{..}', e.g. '\u{12'.
|
||||
UnclosedUnicodeEscape,
|
||||
/// '\u{_12}'
|
||||
LeadingUnderscoreUnicodeEscape,
|
||||
/// More than 6 characters in '\u{..}', e.g. '\u{10FFFF_FF}'
|
||||
OverlongUnicodeEscape,
|
||||
/// Invalid in-bound unicode character code, e.g. '\u{DFFF}'.
|
||||
LoneSurrogateUnicodeEscape,
|
||||
/// Out of bounds unicode character code, e.g. '\u{FFFFFF}'.
|
||||
OutOfRangeUnicodeEscape,
|
||||
|
||||
/// Unicode escape code in byte literal.
|
||||
UnicodeEscapeInByte,
|
||||
/// Non-ascii character in byte literal, byte string literal, or raw byte string literal.
|
||||
NonAsciiCharInByte,
|
||||
|
||||
/// `\0` in a C string literal.
|
||||
NulInCStr,
|
||||
|
||||
/// After a line ending with '\', the next line contains whitespace
|
||||
/// characters that are not skipped.
|
||||
UnskippedWhitespaceWarning,
|
||||
|
||||
/// After a line ending with '\', multiple lines are skipped.
|
||||
MultipleSkippedLinesWarning,
|
||||
}
|
||||
|
||||
impl EscapeError {
|
||||
/// Returns true for actual errors, as opposed to warnings.
|
||||
pub fn is_fatal(&self) -> bool {
|
||||
!matches!(
|
||||
self,
|
||||
EscapeError::UnskippedWhitespaceWarning | EscapeError::MultipleSkippedLinesWarning
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Check a raw string literal for validity
|
||||
///
|
||||
/// Takes the contents of a raw string literal (without quotes)
|
||||
/// and produces a sequence of characters or errors,
|
||||
/// which are returned by invoking `callback`.
|
||||
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
|
||||
pub fn check_raw_str(src: &str, callback: impl FnMut(Range<usize>, Result<char, EscapeError>)) {
|
||||
str::check_raw(src, callback);
|
||||
}
|
||||
|
||||
/// Check a raw byte string literal for validity
|
||||
///
|
||||
/// Takes the contents of a raw byte string literal (without quotes)
|
||||
/// and produces a sequence of bytes or errors,
|
||||
/// which are returned by invoking `callback`.
|
||||
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
|
||||
pub fn check_raw_byte_str(src: &str, callback: impl FnMut(Range<usize>, Result<u8, EscapeError>)) {
|
||||
<[u8]>::check_raw(src, callback);
|
||||
}
|
||||
|
||||
/// Check a raw C string literal for validity
|
||||
///
|
||||
/// Takes the contents of a raw C string literal (without quotes)
|
||||
/// and produces a sequence of characters or errors,
|
||||
/// which are returned by invoking `callback`.
|
||||
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
|
||||
pub fn check_raw_c_str(
|
||||
src: &str,
|
||||
callback: impl FnMut(Range<usize>, Result<NonZeroChar, EscapeError>),
|
||||
) {
|
||||
CStr::check_raw(src, callback);
|
||||
}
|
||||
|
||||
/// Trait for checking raw string literals for validity
|
||||
trait CheckRaw {
|
||||
/// Unit type of the implementing string type (`char` for string, `u8` for byte string)
|
||||
type RawUnit;
|
||||
|
||||
/// Converts chars to the unit type of the literal type
|
||||
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError>;
|
||||
|
||||
/// Takes the contents of a raw literal (without quotes)
|
||||
/// and produces a sequence of `Result<Self::RawUnit, EscapeError>`
|
||||
/// which are returned via `callback`.
|
||||
///
|
||||
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
|
||||
fn check_raw(
|
||||
src: &str,
|
||||
mut callback: impl FnMut(Range<usize>, Result<Self::RawUnit, EscapeError>),
|
||||
) {
|
||||
let mut chars = src.chars();
|
||||
while let Some(c) = chars.next() {
|
||||
let start = src.len() - chars.as_str().len() - c.len_utf8();
|
||||
let res = match c {
|
||||
'\r' => Err(EscapeError::BareCarriageReturnInRawString),
|
||||
_ => Self::char2raw_unit(c),
|
||||
};
|
||||
let end = src.len() - chars.as_str().len();
|
||||
callback(start..end, res);
|
||||
}
|
||||
|
||||
// Unfortunately, it is a bit unclear whether the following equivalent code is slower or faster: bug 141855
|
||||
// src.char_indices().for_each(|(pos, c)| {
|
||||
// callback(
|
||||
// pos..pos + c.len_utf8(),
|
||||
// if c == '\r' {
|
||||
// Err(EscapeError::BareCarriageReturnInRawString)
|
||||
// } else {
|
||||
// Self::char2raw_unit(c)
|
||||
// },
|
||||
// );
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckRaw for str {
|
||||
type RawUnit = char;
|
||||
|
||||
#[inline]
|
||||
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
|
||||
Ok(c)
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckRaw for [u8] {
|
||||
type RawUnit = u8;
|
||||
|
||||
#[inline]
|
||||
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
|
||||
char2byte(c)
|
||||
}
|
||||
}
|
||||
|
||||
/// Turn an ascii char into a byte
|
||||
#[inline]
|
||||
fn char2byte(c: char) -> Result<u8, EscapeError> {
|
||||
// do NOT do: c.try_into().ok_or(EscapeError::NonAsciiCharInByte)
|
||||
if c.is_ascii() {
|
||||
Ok(c as u8)
|
||||
} else {
|
||||
Err(EscapeError::NonAsciiCharInByte)
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckRaw for CStr {
|
||||
type RawUnit = NonZeroChar;
|
||||
|
||||
#[inline]
|
||||
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
|
||||
NonZeroChar::new(c).ok_or(EscapeError::NulInCStr)
|
||||
}
|
||||
}
|
||||
|
||||
/// Unescape a char literal
|
||||
///
|
||||
/// Takes the contents of a char literal (without quotes),
|
||||
/// and returns an unescaped char or an error.
|
||||
#[inline]
|
||||
pub fn unescape_char(src: &str) -> Result<char, EscapeError> {
|
||||
str::unescape_single(&mut src.chars())
|
||||
}
|
||||
|
||||
/// Unescape a byte literal
|
||||
///
|
||||
/// Takes the contents of a byte literal (without quotes),
|
||||
/// and returns an unescaped byte or an error.
|
||||
#[inline]
|
||||
pub fn unescape_byte(src: &str) -> Result<u8, EscapeError> {
|
||||
<[u8]>::unescape_single(&mut src.chars())
|
||||
}
|
||||
|
||||
/// Unescape a string literal
|
||||
///
|
||||
/// Takes the contents of a string literal (without quotes)
|
||||
/// and produces a sequence of escaped characters or errors,
|
||||
/// which are returned by invoking `callback`.
|
||||
pub fn unescape_str(src: &str, callback: impl FnMut(Range<usize>, Result<char, EscapeError>)) {
|
||||
str::unescape(src, callback)
|
||||
}
|
||||
|
||||
/// Unescape a byte string literal
|
||||
///
|
||||
/// Takes the contents of a byte string literal (without quotes)
|
||||
/// and produces a sequence of escaped bytes or errors,
|
||||
/// which are returned by invoking `callback`.
|
||||
pub fn unescape_byte_str(src: &str, callback: impl FnMut(Range<usize>, Result<u8, EscapeError>)) {
|
||||
<[u8]>::unescape(src, callback)
|
||||
}
|
||||
|
||||
/// Unescape a C string literal
|
||||
///
|
||||
/// Takes the contents of a C string literal (without quotes)
|
||||
/// and produces a sequence of escaped MixedUnits or errors,
|
||||
/// which are returned by invoking `callback`.
|
||||
pub fn unescape_c_str(
|
||||
src: &str,
|
||||
callback: impl FnMut(Range<usize>, Result<MixedUnit, EscapeError>),
|
||||
) {
|
||||
CStr::unescape(src, callback)
|
||||
}
|
||||
|
||||
/// Enum representing either a char or a byte
|
||||
///
|
||||
/// Used for mixed utf8 string literals, i.e. those that allow both unicode
|
||||
/// chars and high bytes.
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub enum MixedUnit {
|
||||
/// Used for ASCII chars (written directly or via `\x00`..`\x7f` escapes)
|
||||
/// and Unicode chars (written directly or via `\u` escapes).
|
||||
///
|
||||
/// For example, if '¥' appears in a string it is represented here as
|
||||
/// `MixedUnit::Char('¥')`, and it will be appended to the relevant byte
|
||||
/// string as the two-byte UTF-8 sequence `[0xc2, 0xa5]`
|
||||
Char(NonZeroChar),
|
||||
|
||||
/// Used for high bytes (`\x80`..`\xff`).
|
||||
///
|
||||
/// For example, if `\xa5` appears in a string it is represented here as
|
||||
/// `MixedUnit::HighByte(0xa5)`, and it will be appended to the relevant
|
||||
/// byte string as the single byte `0xa5`.
|
||||
HighByte(NonZeroU8),
|
||||
}
|
||||
|
||||
impl From<NonZeroChar> for MixedUnit {
|
||||
#[inline]
|
||||
fn from(c: NonZeroChar) -> Self {
|
||||
MixedUnit::Char(c)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NonZeroU8> for MixedUnit {
|
||||
#[inline]
|
||||
fn from(byte: NonZeroU8) -> Self {
|
||||
if byte.get().is_ascii() {
|
||||
MixedUnit::Char(NonZeroChar::new(byte.get() as char).unwrap())
|
||||
} else {
|
||||
MixedUnit::HighByte(byte)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<char> for MixedUnit {
|
||||
type Error = EscapeError;
|
||||
|
||||
#[inline]
|
||||
fn try_from(c: char) -> Result<Self, EscapeError> {
|
||||
NonZeroChar::new(c)
|
||||
.map(MixedUnit::Char)
|
||||
.ok_or(EscapeError::NulInCStr)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for MixedUnit {
|
||||
type Error = EscapeError;
|
||||
|
||||
#[inline]
|
||||
fn try_from(byte: u8) -> Result<Self, EscapeError> {
|
||||
NonZeroU8::new(byte)
|
||||
.map(From::from)
|
||||
.ok_or(EscapeError::NulInCStr)
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for unescaping escape sequences in strings
|
||||
trait Unescape {
|
||||
/// Unit type of the implementing string type (`char` for string, `u8` for byte string)
|
||||
type Unit;
|
||||
|
||||
/// Result of unescaping the zero char ('\0')
|
||||
const ZERO_RESULT: Result<Self::Unit, EscapeError>;
|
||||
|
||||
/// Converts non-zero bytes to the unit type
|
||||
fn nonzero_byte2unit(b: NonZeroU8) -> Self::Unit;
|
||||
|
||||
/// Converts chars to the unit type
|
||||
fn char2unit(c: char) -> Result<Self::Unit, EscapeError>;
|
||||
|
||||
/// Converts the byte of a hex escape to the unit type
|
||||
fn hex2unit(b: u8) -> Result<Self::Unit, EscapeError>;
|
||||
|
||||
/// Converts the result of a unicode escape to the unit type
|
||||
fn unicode2unit(r: Result<char, EscapeError>) -> Result<Self::Unit, EscapeError>;
|
||||
|
||||
/// Unescape a single unit (single quote syntax)
|
||||
fn unescape_single(chars: &mut Chars<'_>) -> Result<Self::Unit, EscapeError> {
|
||||
let res = match chars.next().ok_or(EscapeError::ZeroChars)? {
|
||||
'\\' => Self::unescape_1(chars),
|
||||
'\n' | '\t' | '\'' => Err(EscapeError::EscapeOnlyChar),
|
||||
'\r' => Err(EscapeError::BareCarriageReturn),
|
||||
c => Self::char2unit(c),
|
||||
}?;
|
||||
if chars.next().is_some() {
|
||||
return Err(EscapeError::MoreThanOneChar);
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Unescape the first unit of a string (double quoted syntax)
|
||||
fn unescape_1(chars: &mut Chars<'_>) -> Result<Self::Unit, EscapeError> {
|
||||
// Previous character was '\\', unescape what follows.
|
||||
let c = chars.next().ok_or(EscapeError::LoneSlash)?;
|
||||
if c == '0' {
|
||||
Self::ZERO_RESULT
|
||||
} else {
|
||||
simple_escape(c)
|
||||
.map(|b| Self::nonzero_byte2unit(b))
|
||||
.or_else(|c| match c {
|
||||
'x' => Self::hex2unit(hex_escape(chars)?),
|
||||
'u' => Self::unicode2unit({
|
||||
let value = unicode_escape(chars)?;
|
||||
if value > char::MAX as u32 {
|
||||
Err(EscapeError::OutOfRangeUnicodeEscape)
|
||||
} else {
|
||||
char::from_u32(value).ok_or(EscapeError::LoneSurrogateUnicodeEscape)
|
||||
}
|
||||
}),
|
||||
_ => Err(EscapeError::InvalidEscape),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Unescape a string literal
|
||||
///
|
||||
/// Takes the contents of a raw string literal (without quotes)
|
||||
/// and produces a sequence of `Result<Self::Unit, EscapeError>`
|
||||
/// which are returned via `callback`.
|
||||
fn unescape(
|
||||
src: &str,
|
||||
mut callback: impl FnMut(Range<usize>, Result<Self::Unit, EscapeError>),
|
||||
) {
|
||||
let mut chars = src.chars();
|
||||
while let Some(c) = chars.next() {
|
||||
let start = src.len() - chars.as_str().len() - c.len_utf8();
|
||||
let res = match c {
|
||||
'\\' => {
|
||||
if let Some(b'\n') = chars.as_str().as_bytes().first() {
|
||||
let _ = chars.next();
|
||||
// skip whitespace for backslash newline, see [Rust language reference]
|
||||
// (https://doc.rust-lang.org/reference/tokens.html#string-literals).
|
||||
let callback_err = |range, err| callback(range, Err(err));
|
||||
skip_ascii_whitespace(&mut chars, start, callback_err);
|
||||
continue;
|
||||
} else {
|
||||
Self::unescape_1(&mut chars)
|
||||
}
|
||||
}
|
||||
'"' => Err(EscapeError::EscapeOnlyChar),
|
||||
'\r' => Err(EscapeError::BareCarriageReturn),
|
||||
c => Self::char2unit(c),
|
||||
};
|
||||
let end = src.len() - chars.as_str().len();
|
||||
callback(start..end, res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Interpret a non-nul ASCII escape
|
||||
///
|
||||
/// Parses the character of an ASCII escape (except nul) without the leading backslash.
|
||||
#[inline] // single use in Unescape::unescape_1
|
||||
fn simple_escape(c: char) -> Result<NonZeroU8, char> {
|
||||
// Previous character was '\\', unescape what follows.
|
||||
Ok(NonZeroU8::new(match c {
|
||||
'"' => b'"',
|
||||
'n' => b'\n',
|
||||
'r' => b'\r',
|
||||
't' => b'\t',
|
||||
'\\' => b'\\',
|
||||
'\'' => b'\'',
|
||||
_ => Err(c)?,
|
||||
})
|
||||
.unwrap())
|
||||
}
|
||||
|
||||
/// Interpret a hexadecimal escape
|
||||
///
|
||||
/// Parses the two hexadecimal characters of a hexadecimal escape without the leading r"\x".
|
||||
#[inline] // single use in Unescape::unescape_1
|
||||
fn hex_escape(chars: &mut impl Iterator<Item = char>) -> Result<u8, EscapeError> {
|
||||
let hi = chars.next().ok_or(EscapeError::TooShortHexEscape)?;
|
||||
let hi = hi.to_digit(16).ok_or(EscapeError::InvalidCharInHexEscape)?;
|
||||
|
||||
let lo = chars.next().ok_or(EscapeError::TooShortHexEscape)?;
|
||||
let lo = lo.to_digit(16).ok_or(EscapeError::InvalidCharInHexEscape)?;
|
||||
|
||||
Ok((hi * 16 + lo) as u8)
|
||||
}
|
||||
|
||||
/// Interpret a unicode escape
|
||||
///
|
||||
/// Parse the braces with hexadecimal characters (and underscores) part of a unicode escape.
|
||||
/// This r"{...}" normally comes after r"\u" and cannot start with an underscore.
|
||||
#[inline] // single use in Unescape::unescape_1
|
||||
fn unicode_escape(chars: &mut impl Iterator<Item = char>) -> Result<u32, EscapeError> {
|
||||
if chars.next() != Some('{') {
|
||||
return Err(EscapeError::NoBraceInUnicodeEscape);
|
||||
}
|
||||
|
||||
// First character must be a hexadecimal digit.
|
||||
let mut value: u32 = match chars.next().ok_or(EscapeError::UnclosedUnicodeEscape)? {
|
||||
'_' => return Err(EscapeError::LeadingUnderscoreUnicodeEscape),
|
||||
'}' => return Err(EscapeError::EmptyUnicodeEscape),
|
||||
c => c
|
||||
.to_digit(16)
|
||||
.ok_or(EscapeError::InvalidCharInUnicodeEscape)?,
|
||||
};
|
||||
|
||||
// First character is valid, now parse the rest of the number
|
||||
// and closing brace.
|
||||
let mut n_digits = 1;
|
||||
loop {
|
||||
match chars.next() {
|
||||
None => return Err(EscapeError::UnclosedUnicodeEscape),
|
||||
Some('_') => continue,
|
||||
Some('}') => {
|
||||
// Incorrect syntax has higher priority for error reporting
|
||||
// than unallowed value for a literal.
|
||||
return if n_digits > 6 {
|
||||
Err(EscapeError::OverlongUnicodeEscape)
|
||||
} else {
|
||||
Ok(value)
|
||||
};
|
||||
}
|
||||
Some(c) => {
|
||||
let digit: u32 = c
|
||||
.to_digit(16)
|
||||
.ok_or(EscapeError::InvalidCharInUnicodeEscape)?;
|
||||
n_digits += 1;
|
||||
if n_digits > 6 {
|
||||
// Stop updating value since we're sure that it's incorrect already.
|
||||
continue;
|
||||
}
|
||||
value = value * 16 + digit;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Interpret a string continuation escape (https://doc.rust-lang.org/reference/expressions/literal-expr.html#string-continuation-escapes)
|
||||
///
|
||||
/// Skip ASCII whitespace, except for the formfeed character
|
||||
/// (see [this issue](https://github.com/rust-lang/rust/issues/136600)).
|
||||
/// Warns on unescaped newline and following non-ASCII whitespace.
|
||||
#[inline] // single use in Unescape::unescape
|
||||
fn skip_ascii_whitespace(
|
||||
chars: &mut Chars<'_>,
|
||||
start: usize,
|
||||
mut callback: impl FnMut(Range<usize>, EscapeError),
|
||||
) {
|
||||
let rest = chars.as_str();
|
||||
let first_non_space = rest
|
||||
.bytes()
|
||||
.position(|b| b != b' ' && b != b'\t' && b != b'\n' && b != b'\r')
|
||||
.unwrap_or(rest.len());
|
||||
let (space, rest) = rest.split_at(first_non_space);
|
||||
// backslash newline adds 2 bytes
|
||||
let end = start + 2 + first_non_space;
|
||||
if space.contains('\n') {
|
||||
callback(start..end, EscapeError::MultipleSkippedLinesWarning);
|
||||
}
|
||||
*chars = rest.chars();
|
||||
if let Some(c) = chars.clone().next() {
|
||||
if c.is_whitespace() {
|
||||
// for error reporting, include the character that was not skipped in the span
|
||||
callback(
|
||||
start..end + c.len_utf8(),
|
||||
EscapeError::UnskippedWhitespaceWarning,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Unescape for str {
|
||||
type Unit = char;
|
||||
|
||||
const ZERO_RESULT: Result<Self::Unit, EscapeError> = Ok('\0');
|
||||
|
||||
#[inline]
|
||||
fn nonzero_byte2unit(b: NonZeroU8) -> Self::Unit {
|
||||
b.get().into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn hex2unit(b: u8) -> Result<Self::Unit, EscapeError> {
|
||||
if b.is_ascii() {
|
||||
Ok(b as char)
|
||||
} else {
|
||||
Err(EscapeError::OutOfRangeHexEscape)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unicode2unit(r: Result<char, EscapeError>) -> Result<Self::Unit, EscapeError> {
|
||||
r
|
||||
}
|
||||
}
|
||||
|
||||
impl Unescape for [u8] {
|
||||
type Unit = u8;
|
||||
|
||||
const ZERO_RESULT: Result<Self::Unit, EscapeError> = Ok(b'\0');
|
||||
|
||||
#[inline]
|
||||
fn nonzero_byte2unit(b: NonZeroU8) -> Self::Unit {
|
||||
b.get()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
|
||||
char2byte(c)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn hex2unit(b: u8) -> Result<Self::Unit, EscapeError> {
|
||||
Ok(b)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unicode2unit(_r: Result<char, EscapeError>) -> Result<Self::Unit, EscapeError> {
|
||||
Err(EscapeError::UnicodeEscapeInByte)
|
||||
}
|
||||
}
|
||||
|
||||
impl Unescape for CStr {
|
||||
type Unit = MixedUnit;
|
||||
|
||||
const ZERO_RESULT: Result<Self::Unit, EscapeError> = Err(EscapeError::NulInCStr);
|
||||
|
||||
#[inline]
|
||||
fn nonzero_byte2unit(b: NonZeroU8) -> Self::Unit {
|
||||
b.into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
|
||||
c.try_into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn hex2unit(byte: u8) -> Result<Self::Unit, EscapeError> {
|
||||
byte.try_into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unicode2unit(r: Result<char, EscapeError>) -> Result<Self::Unit, EscapeError> {
|
||||
Self::char2unit(r?)
|
||||
}
|
||||
}
|
||||
|
||||
/// Enum of the different kinds of literal
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum Mode {
|
||||
/// `'a'`
|
||||
Char,
|
||||
|
||||
/// `b'a'`
|
||||
Byte,
|
||||
|
||||
/// `"hello"`
|
||||
Str,
|
||||
/// `r"hello"`
|
||||
RawStr,
|
||||
|
||||
/// `b"hello"`
|
||||
ByteStr,
|
||||
/// `br"hello"`
|
||||
RawByteStr,
|
||||
|
||||
/// `c"hello"`
|
||||
CStr,
|
||||
/// `cr"hello"`
|
||||
RawCStr,
|
||||
}
|
||||
|
||||
impl Mode {
|
||||
pub fn in_double_quotes(self) -> bool {
|
||||
match self {
|
||||
Mode::Str
|
||||
| Mode::RawStr
|
||||
| Mode::ByteStr
|
||||
| Mode::RawByteStr
|
||||
| Mode::CStr
|
||||
| Mode::RawCStr => true,
|
||||
Mode::Char | Mode::Byte => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prefix_noraw(self) -> &'static str {
|
||||
match self {
|
||||
Mode::Char | Mode::Str | Mode::RawStr => "",
|
||||
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => "b",
|
||||
Mode::CStr | Mode::RawCStr => "c",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check a literal only for errors
|
||||
///
|
||||
/// Takes the contents of a literal (without quotes)
|
||||
/// and produces a sequence of only errors,
|
||||
/// which are returned by invoking `error_callback`.
|
||||
///
|
||||
/// NB Does not produce any output other than errors
|
||||
pub fn check_for_errors(
|
||||
src: &str,
|
||||
mode: Mode,
|
||||
mut error_callback: impl FnMut(Range<usize>, EscapeError),
|
||||
) {
|
||||
match mode {
|
||||
Mode::Char => {
|
||||
let mut chars = src.chars();
|
||||
if let Err(e) = str::unescape_single(&mut chars) {
|
||||
error_callback(0..(src.len() - chars.as_str().len()), e);
|
||||
}
|
||||
}
|
||||
Mode::Byte => {
|
||||
let mut chars = src.chars();
|
||||
if let Err(e) = <[u8]>::unescape_single(&mut chars) {
|
||||
error_callback(0..(src.len() - chars.as_str().len()), e);
|
||||
}
|
||||
}
|
||||
Mode::Str => unescape_str(src, |range, res| {
|
||||
if let Err(e) = res {
|
||||
error_callback(range, e);
|
||||
}
|
||||
}),
|
||||
Mode::ByteStr => unescape_byte_str(src, |range, res| {
|
||||
if let Err(e) = res {
|
||||
error_callback(range, e);
|
||||
}
|
||||
}),
|
||||
Mode::CStr => unescape_c_str(src, |range, res| {
|
||||
if let Err(e) = res {
|
||||
error_callback(range, e);
|
||||
}
|
||||
}),
|
||||
Mode::RawStr => check_raw_str(src, |range, res| {
|
||||
if let Err(e) = res {
|
||||
error_callback(range, e);
|
||||
}
|
||||
}),
|
||||
Mode::RawByteStr => check_raw_byte_str(src, |range, res| {
|
||||
if let Err(e) = res {
|
||||
error_callback(range, e);
|
||||
}
|
||||
}),
|
||||
Mode::RawCStr => check_raw_c_str(src, |range, res| {
|
||||
if let Err(e) = res {
|
||||
error_callback(range, e);
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
Vendored
+988
@@ -0,0 +1,988 @@
|
||||
use crate::detection::inside_proc_macro;
|
||||
use crate::fallback::{self, FromStr2 as _};
|
||||
#[cfg(span_locations)]
|
||||
use crate::location::LineColumn;
|
||||
#[cfg(proc_macro_span)]
|
||||
use crate::probe::proc_macro_span;
|
||||
#[cfg(all(span_locations, proc_macro_span_file))]
|
||||
use crate::probe::proc_macro_span_file;
|
||||
#[cfg(all(span_locations, proc_macro_span_location))]
|
||||
use crate::probe::proc_macro_span_location;
|
||||
use crate::{Delimiter, Punct, Spacing, TokenTree};
|
||||
#[cfg(all(span_locations, not(proc_macro_span_file)))]
|
||||
use alloc::borrow::ToOwned as _;
|
||||
use alloc::string::{String, ToString as _};
|
||||
use alloc::vec::Vec;
|
||||
use core::ffi::CStr;
|
||||
use core::fmt::{self, Debug, Display};
|
||||
#[cfg(span_locations)]
|
||||
use core::ops::Range;
|
||||
use core::ops::RangeBounds;
|
||||
#[cfg(span_locations)]
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) enum TokenStream {
|
||||
Compiler(DeferredTokenStream),
|
||||
Fallback(fallback::TokenStream),
|
||||
}
|
||||
|
||||
// Work around https://github.com/rust-lang/rust/issues/65080.
|
||||
// In `impl Extend<TokenTree> for TokenStream` which is used heavily by quote,
|
||||
// we hold on to the appended tokens and do proc_macro::TokenStream::extend as
|
||||
// late as possible to batch together consecutive uses of the Extend impl.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct DeferredTokenStream {
|
||||
stream: proc_macro::TokenStream,
|
||||
extra: Vec<proc_macro::TokenTree>,
|
||||
}
|
||||
|
||||
pub(crate) enum LexError {
|
||||
Compiler(proc_macro::LexError),
|
||||
Fallback(fallback::LexError),
|
||||
|
||||
// Rustc was supposed to return a LexError, but it panicked instead.
|
||||
// https://github.com/rust-lang/rust/issues/58736
|
||||
CompilerPanic,
|
||||
}
|
||||
|
||||
#[cold]
|
||||
fn mismatch(line: u32) -> ! {
|
||||
#[cfg(procmacro2_backtrace)]
|
||||
{
|
||||
let backtrace = std::backtrace::Backtrace::force_capture();
|
||||
panic!("compiler/fallback mismatch L{}\n\n{}", line, backtrace)
|
||||
}
|
||||
#[cfg(not(procmacro2_backtrace))]
|
||||
{
|
||||
panic!("compiler/fallback mismatch L{}", line)
|
||||
}
|
||||
}
|
||||
|
||||
impl DeferredTokenStream {
|
||||
fn new(stream: proc_macro::TokenStream) -> Self {
|
||||
DeferredTokenStream {
|
||||
stream,
|
||||
extra: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.stream.is_empty() && self.extra.is_empty()
|
||||
}
|
||||
|
||||
fn evaluate_now(&mut self) {
|
||||
// If-check provides a fast short circuit for the common case of `extra`
|
||||
// being empty, which saves a round trip over the proc macro bridge.
|
||||
// Improves macro expansion time in winrt by 6% in debug mode.
|
||||
if !self.extra.is_empty() {
|
||||
self.stream.extend(self.extra.drain(..));
|
||||
}
|
||||
}
|
||||
|
||||
fn into_token_stream(mut self) -> proc_macro::TokenStream {
|
||||
self.evaluate_now();
|
||||
self.stream
|
||||
}
|
||||
}
|
||||
|
||||
impl TokenStream {
|
||||
pub(crate) fn new() -> Self {
|
||||
if inside_proc_macro() {
|
||||
TokenStream::Compiler(DeferredTokenStream::new(proc_macro::TokenStream::new()))
|
||||
} else {
|
||||
TokenStream::Fallback(fallback::TokenStream::new())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_str_checked(src: &str) -> Result<Self, LexError> {
|
||||
if inside_proc_macro() {
|
||||
Ok(TokenStream::Compiler(DeferredTokenStream::new(
|
||||
proc_macro::TokenStream::from_str_checked(src)?,
|
||||
)))
|
||||
} else {
|
||||
Ok(TokenStream::Fallback(
|
||||
fallback::TokenStream::from_str_checked(src)?,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_empty(&self) -> bool {
|
||||
match self {
|
||||
TokenStream::Compiler(tts) => tts.is_empty(),
|
||||
TokenStream::Fallback(tts) => tts.is_empty(),
|
||||
}
|
||||
}
|
||||
|
||||
fn unwrap_nightly(self) -> proc_macro::TokenStream {
|
||||
match self {
|
||||
TokenStream::Compiler(s) => s.into_token_stream(),
|
||||
TokenStream::Fallback(_) => mismatch(line!()),
|
||||
}
|
||||
}
|
||||
|
||||
fn unwrap_stable(self) -> fallback::TokenStream {
|
||||
match self {
|
||||
TokenStream::Compiler(_) => mismatch(line!()),
|
||||
TokenStream::Fallback(s) => s,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for TokenStream {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
TokenStream::Compiler(tts) => Display::fmt(&tts.clone().into_token_stream(), f),
|
||||
TokenStream::Fallback(tts) => Display::fmt(tts, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<proc_macro::TokenStream> for TokenStream {
|
||||
fn from(inner: proc_macro::TokenStream) -> Self {
|
||||
TokenStream::Compiler(DeferredTokenStream::new(inner))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TokenStream> for proc_macro::TokenStream {
|
||||
fn from(inner: TokenStream) -> Self {
|
||||
match inner {
|
||||
TokenStream::Compiler(inner) => inner.into_token_stream(),
|
||||
TokenStream::Fallback(inner) => {
|
||||
proc_macro::TokenStream::from_str_unchecked(&inner.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<fallback::TokenStream> for TokenStream {
|
||||
fn from(inner: fallback::TokenStream) -> Self {
|
||||
TokenStream::Fallback(inner)
|
||||
}
|
||||
}
|
||||
|
||||
// Assumes inside_proc_macro().
|
||||
fn into_compiler_token(token: TokenTree) -> proc_macro::TokenTree {
|
||||
match token {
|
||||
TokenTree::Group(tt) => proc_macro::TokenTree::Group(tt.inner.unwrap_nightly()),
|
||||
TokenTree::Punct(tt) => {
|
||||
let spacing = match tt.spacing() {
|
||||
Spacing::Joint => proc_macro::Spacing::Joint,
|
||||
Spacing::Alone => proc_macro::Spacing::Alone,
|
||||
};
|
||||
let mut punct = proc_macro::Punct::new(tt.as_char(), spacing);
|
||||
punct.set_span(tt.span().inner.unwrap_nightly());
|
||||
proc_macro::TokenTree::Punct(punct)
|
||||
}
|
||||
TokenTree::Ident(tt) => proc_macro::TokenTree::Ident(tt.inner.unwrap_nightly()),
|
||||
TokenTree::Literal(tt) => proc_macro::TokenTree::Literal(tt.inner.unwrap_nightly()),
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TokenTree> for TokenStream {
|
||||
fn from(token: TokenTree) -> Self {
|
||||
if inside_proc_macro() {
|
||||
TokenStream::Compiler(DeferredTokenStream::new(proc_macro::TokenStream::from(
|
||||
into_compiler_token(token),
|
||||
)))
|
||||
} else {
|
||||
TokenStream::Fallback(fallback::TokenStream::from(token))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromIterator<TokenTree> for TokenStream {
|
||||
fn from_iter<I: IntoIterator<Item = TokenTree>>(tokens: I) -> Self {
|
||||
if inside_proc_macro() {
|
||||
TokenStream::Compiler(DeferredTokenStream::new(
|
||||
tokens.into_iter().map(into_compiler_token).collect(),
|
||||
))
|
||||
} else {
|
||||
TokenStream::Fallback(tokens.into_iter().collect())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromIterator<TokenStream> for TokenStream {
|
||||
fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
|
||||
let mut streams = streams.into_iter();
|
||||
match streams.next() {
|
||||
Some(TokenStream::Compiler(mut first)) => {
|
||||
first.evaluate_now();
|
||||
first.stream.extend(streams.map(|s| match s {
|
||||
TokenStream::Compiler(s) => s.into_token_stream(),
|
||||
TokenStream::Fallback(_) => mismatch(line!()),
|
||||
}));
|
||||
TokenStream::Compiler(first)
|
||||
}
|
||||
Some(TokenStream::Fallback(mut first)) => {
|
||||
first.extend(streams.map(|s| match s {
|
||||
TokenStream::Fallback(s) => s,
|
||||
TokenStream::Compiler(_) => mismatch(line!()),
|
||||
}));
|
||||
TokenStream::Fallback(first)
|
||||
}
|
||||
None => TokenStream::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Extend<TokenTree> for TokenStream {
|
||||
fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, tokens: I) {
|
||||
match self {
|
||||
TokenStream::Compiler(tts) => {
|
||||
// Here is the reason for DeferredTokenStream.
|
||||
for token in tokens {
|
||||
tts.extra.push(into_compiler_token(token));
|
||||
}
|
||||
}
|
||||
TokenStream::Fallback(tts) => tts.extend(tokens),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Extend<TokenStream> for TokenStream {
|
||||
fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
|
||||
match self {
|
||||
TokenStream::Compiler(tts) => {
|
||||
tts.evaluate_now();
|
||||
tts.stream
|
||||
.extend(streams.into_iter().map(TokenStream::unwrap_nightly));
|
||||
}
|
||||
TokenStream::Fallback(tts) => {
|
||||
tts.extend(streams.into_iter().map(TokenStream::unwrap_stable));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for TokenStream {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
TokenStream::Compiler(tts) => Debug::fmt(&tts.clone().into_token_stream(), f),
|
||||
TokenStream::Fallback(tts) => Debug::fmt(tts, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LexError {
|
||||
pub(crate) fn span(&self) -> Span {
|
||||
match self {
|
||||
LexError::Compiler(_) | LexError::CompilerPanic => Span::call_site(),
|
||||
LexError::Fallback(e) => Span::Fallback(e.span()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<proc_macro::LexError> for LexError {
|
||||
fn from(e: proc_macro::LexError) -> Self {
|
||||
LexError::Compiler(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<fallback::LexError> for LexError {
|
||||
fn from(e: fallback::LexError) -> Self {
|
||||
LexError::Fallback(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for LexError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
LexError::Compiler(e) => Debug::fmt(e, f),
|
||||
LexError::Fallback(e) => Debug::fmt(e, f),
|
||||
LexError::CompilerPanic => {
|
||||
let fallback = fallback::LexError::call_site();
|
||||
Debug::fmt(&fallback, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for LexError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
LexError::Compiler(e) => Display::fmt(e, f),
|
||||
LexError::Fallback(e) => Display::fmt(e, f),
|
||||
LexError::CompilerPanic => {
|
||||
let fallback = fallback::LexError::call_site();
|
||||
Display::fmt(&fallback, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) enum TokenTreeIter {
|
||||
Compiler(proc_macro::token_stream::IntoIter),
|
||||
Fallback(fallback::TokenTreeIter),
|
||||
}
|
||||
|
||||
impl IntoIterator for TokenStream {
|
||||
type Item = TokenTree;
|
||||
type IntoIter = TokenTreeIter;
|
||||
|
||||
fn into_iter(self) -> TokenTreeIter {
|
||||
match self {
|
||||
TokenStream::Compiler(tts) => {
|
||||
TokenTreeIter::Compiler(tts.into_token_stream().into_iter())
|
||||
}
|
||||
TokenStream::Fallback(tts) => TokenTreeIter::Fallback(tts.into_iter()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for TokenTreeIter {
|
||||
type Item = TokenTree;
|
||||
|
||||
fn next(&mut self) -> Option<TokenTree> {
|
||||
let token = match self {
|
||||
TokenTreeIter::Compiler(iter) => iter.next()?,
|
||||
TokenTreeIter::Fallback(iter) => return iter.next(),
|
||||
};
|
||||
Some(match token {
|
||||
proc_macro::TokenTree::Group(tt) => {
|
||||
TokenTree::Group(crate::Group::_new(Group::Compiler(tt)))
|
||||
}
|
||||
proc_macro::TokenTree::Punct(tt) => {
|
||||
let spacing = match tt.spacing() {
|
||||
proc_macro::Spacing::Joint => Spacing::Joint,
|
||||
proc_macro::Spacing::Alone => Spacing::Alone,
|
||||
};
|
||||
let mut o = Punct::new(tt.as_char(), spacing);
|
||||
o.set_span(crate::Span::_new(Span::Compiler(tt.span())));
|
||||
TokenTree::Punct(o)
|
||||
}
|
||||
proc_macro::TokenTree::Ident(s) => {
|
||||
TokenTree::Ident(crate::Ident::_new(Ident::Compiler(s)))
|
||||
}
|
||||
proc_macro::TokenTree::Literal(l) => {
|
||||
TokenTree::Literal(crate::Literal::_new(Literal::Compiler(l)))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
match self {
|
||||
TokenTreeIter::Compiler(tts) => tts.size_hint(),
|
||||
TokenTreeIter::Fallback(tts) => tts.size_hint(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub(crate) enum Span {
|
||||
Compiler(proc_macro::Span),
|
||||
Fallback(fallback::Span),
|
||||
}
|
||||
|
||||
impl Span {
|
||||
pub(crate) fn call_site() -> Self {
|
||||
if inside_proc_macro() {
|
||||
Span::Compiler(proc_macro::Span::call_site())
|
||||
} else {
|
||||
Span::Fallback(fallback::Span::call_site())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn mixed_site() -> Self {
|
||||
if inside_proc_macro() {
|
||||
Span::Compiler(proc_macro::Span::mixed_site())
|
||||
} else {
|
||||
Span::Fallback(fallback::Span::mixed_site())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(super_unstable)]
|
||||
pub(crate) fn def_site() -> Self {
|
||||
if inside_proc_macro() {
|
||||
Span::Compiler(proc_macro::Span::def_site())
|
||||
} else {
|
||||
Span::Fallback(fallback::Span::def_site())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn resolved_at(&self, other: Span) -> Span {
|
||||
match (self, other) {
|
||||
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.resolved_at(b)),
|
||||
(Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.resolved_at(b)),
|
||||
(Span::Compiler(_), Span::Fallback(_)) => mismatch(line!()),
|
||||
(Span::Fallback(_), Span::Compiler(_)) => mismatch(line!()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn located_at(&self, other: Span) -> Span {
|
||||
match (self, other) {
|
||||
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.located_at(b)),
|
||||
(Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.located_at(b)),
|
||||
(Span::Compiler(_), Span::Fallback(_)) => mismatch(line!()),
|
||||
(Span::Fallback(_), Span::Compiler(_)) => mismatch(line!()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn unwrap(self) -> proc_macro::Span {
|
||||
match self {
|
||||
Span::Compiler(s) => s,
|
||||
Span::Fallback(_) => panic!("proc_macro::Span is only available in procedural macros"),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(span_locations)]
|
||||
pub(crate) fn byte_range(&self) -> Range<usize> {
|
||||
match self {
|
||||
#[cfg(proc_macro_span)]
|
||||
Span::Compiler(s) => proc_macro_span::byte_range(s),
|
||||
#[cfg(not(proc_macro_span))]
|
||||
Span::Compiler(_) => 0..0,
|
||||
Span::Fallback(s) => s.byte_range(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(span_locations)]
|
||||
pub(crate) fn start(&self) -> LineColumn {
|
||||
match self {
|
||||
#[cfg(proc_macro_span_location)]
|
||||
Span::Compiler(s) => LineColumn {
|
||||
line: proc_macro_span_location::line(s),
|
||||
column: proc_macro_span_location::column(s).saturating_sub(1),
|
||||
},
|
||||
#[cfg(not(proc_macro_span_location))]
|
||||
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
|
||||
Span::Fallback(s) => s.start(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(span_locations)]
|
||||
pub(crate) fn end(&self) -> LineColumn {
|
||||
match self {
|
||||
#[cfg(proc_macro_span_location)]
|
||||
Span::Compiler(s) => {
|
||||
let end = proc_macro_span_location::end(s);
|
||||
LineColumn {
|
||||
line: proc_macro_span_location::line(&end),
|
||||
column: proc_macro_span_location::column(&end).saturating_sub(1),
|
||||
}
|
||||
}
|
||||
#[cfg(not(proc_macro_span_location))]
|
||||
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
|
||||
Span::Fallback(s) => s.end(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(span_locations)]
|
||||
pub(crate) fn file(&self) -> String {
|
||||
match self {
|
||||
#[cfg(proc_macro_span_file)]
|
||||
Span::Compiler(s) => proc_macro_span_file::file(s),
|
||||
#[cfg(not(proc_macro_span_file))]
|
||||
Span::Compiler(_) => "<token stream>".to_owned(),
|
||||
Span::Fallback(s) => s.file(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(span_locations)]
|
||||
pub(crate) fn local_file(&self) -> Option<PathBuf> {
|
||||
match self {
|
||||
#[cfg(proc_macro_span_file)]
|
||||
Span::Compiler(s) => proc_macro_span_file::local_file(s),
|
||||
#[cfg(not(proc_macro_span_file))]
|
||||
Span::Compiler(_) => None,
|
||||
Span::Fallback(s) => s.local_file(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn join(&self, other: Span) -> Option<Span> {
|
||||
let ret = match (self, other) {
|
||||
#[cfg(proc_macro_span)]
|
||||
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(proc_macro_span::join(a, b)?),
|
||||
(Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.join(b)?),
|
||||
_ => return None,
|
||||
};
|
||||
Some(ret)
|
||||
}
|
||||
|
||||
#[cfg(super_unstable)]
|
||||
pub(crate) fn eq(&self, other: &Span) -> bool {
|
||||
match (self, other) {
|
||||
(Span::Compiler(a), Span::Compiler(b)) => a.eq(b),
|
||||
(Span::Fallback(a), Span::Fallback(b)) => a.eq(b),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn source_text(&self) -> Option<String> {
|
||||
match self {
|
||||
#[cfg(not(no_source_text))]
|
||||
Span::Compiler(s) => s.source_text(),
|
||||
#[cfg(no_source_text)]
|
||||
Span::Compiler(_) => None,
|
||||
Span::Fallback(s) => s.source_text(),
|
||||
}
|
||||
}
|
||||
|
||||
fn unwrap_nightly(self) -> proc_macro::Span {
|
||||
match self {
|
||||
Span::Compiler(s) => s,
|
||||
Span::Fallback(_) => mismatch(line!()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<proc_macro::Span> for crate::Span {
|
||||
fn from(proc_span: proc_macro::Span) -> Self {
|
||||
crate::Span::_new(Span::Compiler(proc_span))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<fallback::Span> for Span {
|
||||
fn from(inner: fallback::Span) -> Self {
|
||||
Span::Fallback(inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Span {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Span::Compiler(s) => Debug::fmt(s, f),
|
||||
Span::Fallback(s) => Debug::fmt(s, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn debug_span_field_if_nontrivial(debug: &mut fmt::DebugStruct, span: Span) {
|
||||
match span {
|
||||
Span::Compiler(s) => {
|
||||
debug.field("span", &s);
|
||||
}
|
||||
Span::Fallback(s) => fallback::debug_span_field_if_nontrivial(debug, s),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) enum Group {
|
||||
Compiler(proc_macro::Group),
|
||||
Fallback(fallback::Group),
|
||||
}
|
||||
|
||||
impl Group {
|
||||
pub(crate) fn new(delimiter: Delimiter, stream: TokenStream) -> Self {
|
||||
match stream {
|
||||
TokenStream::Compiler(tts) => {
|
||||
let delimiter = match delimiter {
|
||||
Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
|
||||
Delimiter::Bracket => proc_macro::Delimiter::Bracket,
|
||||
Delimiter::Brace => proc_macro::Delimiter::Brace,
|
||||
Delimiter::None => proc_macro::Delimiter::None,
|
||||
};
|
||||
Group::Compiler(proc_macro::Group::new(delimiter, tts.into_token_stream()))
|
||||
}
|
||||
TokenStream::Fallback(stream) => {
|
||||
Group::Fallback(fallback::Group::new(delimiter, stream))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn delimiter(&self) -> Delimiter {
|
||||
match self {
|
||||
Group::Compiler(g) => match g.delimiter() {
|
||||
proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
|
||||
proc_macro::Delimiter::Bracket => Delimiter::Bracket,
|
||||
proc_macro::Delimiter::Brace => Delimiter::Brace,
|
||||
proc_macro::Delimiter::None => Delimiter::None,
|
||||
},
|
||||
Group::Fallback(g) => g.delimiter(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn stream(&self) -> TokenStream {
|
||||
match self {
|
||||
Group::Compiler(g) => TokenStream::Compiler(DeferredTokenStream::new(g.stream())),
|
||||
Group::Fallback(g) => TokenStream::Fallback(g.stream()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn span(&self) -> Span {
|
||||
match self {
|
||||
Group::Compiler(g) => Span::Compiler(g.span()),
|
||||
Group::Fallback(g) => Span::Fallback(g.span()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn span_open(&self) -> Span {
|
||||
match self {
|
||||
Group::Compiler(g) => Span::Compiler(g.span_open()),
|
||||
Group::Fallback(g) => Span::Fallback(g.span_open()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn span_close(&self) -> Span {
|
||||
match self {
|
||||
Group::Compiler(g) => Span::Compiler(g.span_close()),
|
||||
Group::Fallback(g) => Span::Fallback(g.span_close()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_span(&mut self, span: Span) {
|
||||
match (self, span) {
|
||||
(Group::Compiler(g), Span::Compiler(s)) => g.set_span(s),
|
||||
(Group::Fallback(g), Span::Fallback(s)) => g.set_span(s),
|
||||
(Group::Compiler(_), Span::Fallback(_)) => mismatch(line!()),
|
||||
(Group::Fallback(_), Span::Compiler(_)) => mismatch(line!()),
|
||||
}
|
||||
}
|
||||
|
||||
fn unwrap_nightly(self) -> proc_macro::Group {
|
||||
match self {
|
||||
Group::Compiler(g) => g,
|
||||
Group::Fallback(_) => mismatch(line!()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<fallback::Group> for Group {
|
||||
fn from(g: fallback::Group) -> Self {
|
||||
Group::Fallback(g)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Group {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Group::Compiler(group) => Display::fmt(group, formatter),
|
||||
Group::Fallback(group) => Display::fmt(group, formatter),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Group {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Group::Compiler(group) => Debug::fmt(group, formatter),
|
||||
Group::Fallback(group) => Debug::fmt(group, formatter),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) enum Ident {
|
||||
Compiler(proc_macro::Ident),
|
||||
Fallback(fallback::Ident),
|
||||
}
|
||||
|
||||
impl Ident {
|
||||
#[track_caller]
|
||||
pub(crate) fn new_checked(string: &str, span: Span) -> Self {
|
||||
match span {
|
||||
Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new(string, s)),
|
||||
Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_checked(string, s)),
|
||||
}
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
pub(crate) fn new_raw_checked(string: &str, span: Span) -> Self {
|
||||
match span {
|
||||
Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new_raw(string, s)),
|
||||
Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_raw_checked(string, s)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn span(&self) -> Span {
|
||||
match self {
|
||||
Ident::Compiler(t) => Span::Compiler(t.span()),
|
||||
Ident::Fallback(t) => Span::Fallback(t.span()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_span(&mut self, span: Span) {
|
||||
match (self, span) {
|
||||
(Ident::Compiler(t), Span::Compiler(s)) => t.set_span(s),
|
||||
(Ident::Fallback(t), Span::Fallback(s)) => t.set_span(s),
|
||||
(Ident::Compiler(_), Span::Fallback(_)) => mismatch(line!()),
|
||||
(Ident::Fallback(_), Span::Compiler(_)) => mismatch(line!()),
|
||||
}
|
||||
}
|
||||
|
||||
fn unwrap_nightly(self) -> proc_macro::Ident {
|
||||
match self {
|
||||
Ident::Compiler(s) => s,
|
||||
Ident::Fallback(_) => mismatch(line!()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<fallback::Ident> for Ident {
|
||||
fn from(inner: fallback::Ident) -> Self {
|
||||
Ident::Fallback(inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Ident {
|
||||
fn eq(&self, other: &Ident) -> bool {
|
||||
match (self, other) {
|
||||
(Ident::Compiler(t), Ident::Compiler(o)) => t.to_string() == o.to_string(),
|
||||
(Ident::Fallback(t), Ident::Fallback(o)) => t == o,
|
||||
(Ident::Compiler(_), Ident::Fallback(_)) => mismatch(line!()),
|
||||
(Ident::Fallback(_), Ident::Compiler(_)) => mismatch(line!()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PartialEq<T> for Ident
|
||||
where
|
||||
T: ?Sized + AsRef<str>,
|
||||
{
|
||||
fn eq(&self, other: &T) -> bool {
|
||||
let other = other.as_ref();
|
||||
match self {
|
||||
Ident::Compiler(t) => t.to_string() == other,
|
||||
Ident::Fallback(t) => t == other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Ident {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Ident::Compiler(t) => Display::fmt(t, f),
|
||||
Ident::Fallback(t) => Display::fmt(t, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Ident {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Ident::Compiler(t) => Debug::fmt(t, f),
|
||||
Ident::Fallback(t) => Debug::fmt(t, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) enum Literal {
|
||||
Compiler(proc_macro::Literal),
|
||||
Fallback(fallback::Literal),
|
||||
}
|
||||
|
||||
macro_rules! suffixed_numbers {
|
||||
($($name:ident => $kind:ident,)*) => ($(
|
||||
pub(crate) fn $name(n: $kind) -> Literal {
|
||||
if inside_proc_macro() {
|
||||
Literal::Compiler(proc_macro::Literal::$name(n))
|
||||
} else {
|
||||
Literal::Fallback(fallback::Literal::$name(n))
|
||||
}
|
||||
}
|
||||
)*)
|
||||
}
|
||||
|
||||
macro_rules! unsuffixed_integers {
|
||||
($($name:ident => $kind:ident,)*) => ($(
|
||||
pub(crate) fn $name(n: $kind) -> Literal {
|
||||
if inside_proc_macro() {
|
||||
Literal::Compiler(proc_macro::Literal::$name(n))
|
||||
} else {
|
||||
Literal::Fallback(fallback::Literal::$name(n))
|
||||
}
|
||||
}
|
||||
)*)
|
||||
}
|
||||
|
||||
impl Literal {
|
||||
pub(crate) fn from_str_checked(repr: &str) -> Result<Self, LexError> {
|
||||
if inside_proc_macro() {
|
||||
let literal = proc_macro::Literal::from_str_checked(repr)?;
|
||||
Ok(Literal::Compiler(literal))
|
||||
} else {
|
||||
let literal = fallback::Literal::from_str_checked(repr)?;
|
||||
Ok(Literal::Fallback(literal))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn from_str_unchecked(repr: &str) -> Self {
|
||||
if inside_proc_macro() {
|
||||
Literal::Compiler(proc_macro::Literal::from_str_unchecked(repr))
|
||||
} else {
|
||||
Literal::Fallback(unsafe { fallback::Literal::from_str_unchecked(repr) })
|
||||
}
|
||||
}
|
||||
|
||||
suffixed_numbers! {
|
||||
u8_suffixed => u8,
|
||||
u16_suffixed => u16,
|
||||
u32_suffixed => u32,
|
||||
u64_suffixed => u64,
|
||||
u128_suffixed => u128,
|
||||
usize_suffixed => usize,
|
||||
i8_suffixed => i8,
|
||||
i16_suffixed => i16,
|
||||
i32_suffixed => i32,
|
||||
i64_suffixed => i64,
|
||||
i128_suffixed => i128,
|
||||
isize_suffixed => isize,
|
||||
|
||||
f32_suffixed => f32,
|
||||
f64_suffixed => f64,
|
||||
}
|
||||
|
||||
unsuffixed_integers! {
|
||||
u8_unsuffixed => u8,
|
||||
u16_unsuffixed => u16,
|
||||
u32_unsuffixed => u32,
|
||||
u64_unsuffixed => u64,
|
||||
u128_unsuffixed => u128,
|
||||
usize_unsuffixed => usize,
|
||||
i8_unsuffixed => i8,
|
||||
i16_unsuffixed => i16,
|
||||
i32_unsuffixed => i32,
|
||||
i64_unsuffixed => i64,
|
||||
i128_unsuffixed => i128,
|
||||
isize_unsuffixed => isize,
|
||||
}
|
||||
|
||||
pub(crate) fn f32_unsuffixed(f: f32) -> Literal {
|
||||
if inside_proc_macro() {
|
||||
Literal::Compiler(proc_macro::Literal::f32_unsuffixed(f))
|
||||
} else {
|
||||
Literal::Fallback(fallback::Literal::f32_unsuffixed(f))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn f64_unsuffixed(f: f64) -> Literal {
|
||||
if inside_proc_macro() {
|
||||
Literal::Compiler(proc_macro::Literal::f64_unsuffixed(f))
|
||||
} else {
|
||||
Literal::Fallback(fallback::Literal::f64_unsuffixed(f))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn string(string: &str) -> Literal {
|
||||
if inside_proc_macro() {
|
||||
Literal::Compiler(proc_macro::Literal::string(string))
|
||||
} else {
|
||||
Literal::Fallback(fallback::Literal::string(string))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn character(ch: char) -> Literal {
|
||||
if inside_proc_macro() {
|
||||
Literal::Compiler(proc_macro::Literal::character(ch))
|
||||
} else {
|
||||
Literal::Fallback(fallback::Literal::character(ch))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn byte_character(byte: u8) -> Literal {
|
||||
if inside_proc_macro() {
|
||||
Literal::Compiler({
|
||||
#[cfg(not(no_literal_byte_character))]
|
||||
{
|
||||
proc_macro::Literal::byte_character(byte)
|
||||
}
|
||||
|
||||
#[cfg(no_literal_byte_character)]
|
||||
{
|
||||
let fallback = fallback::Literal::byte_character(byte);
|
||||
proc_macro::Literal::from_str_unchecked(&fallback.repr)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Literal::Fallback(fallback::Literal::byte_character(byte))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn byte_string(bytes: &[u8]) -> Literal {
|
||||
if inside_proc_macro() {
|
||||
Literal::Compiler(proc_macro::Literal::byte_string(bytes))
|
||||
} else {
|
||||
Literal::Fallback(fallback::Literal::byte_string(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn c_string(string: &CStr) -> Literal {
|
||||
if inside_proc_macro() {
|
||||
Literal::Compiler({
|
||||
#[cfg(not(no_literal_c_string))]
|
||||
{
|
||||
proc_macro::Literal::c_string(string)
|
||||
}
|
||||
|
||||
#[cfg(no_literal_c_string)]
|
||||
{
|
||||
let fallback = fallback::Literal::c_string(string);
|
||||
proc_macro::Literal::from_str_unchecked(&fallback.repr)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Literal::Fallback(fallback::Literal::c_string(string))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn span(&self) -> Span {
|
||||
match self {
|
||||
Literal::Compiler(lit) => Span::Compiler(lit.span()),
|
||||
Literal::Fallback(lit) => Span::Fallback(lit.span()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_span(&mut self, span: Span) {
|
||||
match (self, span) {
|
||||
(Literal::Compiler(lit), Span::Compiler(s)) => lit.set_span(s),
|
||||
(Literal::Fallback(lit), Span::Fallback(s)) => lit.set_span(s),
|
||||
(Literal::Compiler(_), Span::Fallback(_)) => mismatch(line!()),
|
||||
(Literal::Fallback(_), Span::Compiler(_)) => mismatch(line!()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
|
||||
match self {
|
||||
#[cfg(proc_macro_span)]
|
||||
Literal::Compiler(lit) => proc_macro_span::subspan(lit, range).map(Span::Compiler),
|
||||
#[cfg(not(proc_macro_span))]
|
||||
Literal::Compiler(_lit) => None,
|
||||
Literal::Fallback(lit) => lit.subspan(range).map(Span::Fallback),
|
||||
}
|
||||
}
|
||||
|
||||
fn unwrap_nightly(self) -> proc_macro::Literal {
|
||||
match self {
|
||||
Literal::Compiler(s) => s,
|
||||
Literal::Fallback(_) => mismatch(line!()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<fallback::Literal> for Literal {
|
||||
fn from(s: fallback::Literal) -> Self {
|
||||
Literal::Fallback(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Literal {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Literal::Compiler(t) => Display::fmt(t, f),
|
||||
Literal::Fallback(t) => Display::fmt(t, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Literal {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Literal::Compiler(t) => Debug::fmt(t, f),
|
||||
Literal::Fallback(t) => Debug::fmt(t, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(span_locations)]
|
||||
pub(crate) fn invalidate_current_thread_spans() {
|
||||
if inside_proc_macro() {
|
||||
panic!(
|
||||
"proc_macro2::extra::invalidate_current_thread_spans is not available in procedural macros"
|
||||
);
|
||||
} else {
|
||||
crate::fallback::invalidate_current_thread_spans();
|
||||
}
|
||||
}
|
||||
Vendored
+105
@@ -0,0 +1,105 @@
|
||||
#![allow(clippy::assertions_on_result_states, clippy::uninlined_format_args)]
|
||||
|
||||
use proc_macro2::{Delimiter, Literal, Spacing, TokenStream, TokenTree};
|
||||
|
||||
// #[doc = "..."] -> "..."
|
||||
fn lit_of_outer_doc_comment(tokens: &TokenStream) -> Literal {
|
||||
lit_of_doc_comment(tokens, false)
|
||||
}
|
||||
|
||||
// #![doc = "..."] -> "..."
|
||||
fn lit_of_inner_doc_comment(tokens: &TokenStream) -> Literal {
|
||||
lit_of_doc_comment(tokens, true)
|
||||
}
|
||||
|
||||
fn lit_of_doc_comment(tokens: &TokenStream, inner: bool) -> Literal {
|
||||
let mut iter = tokens.clone().into_iter();
|
||||
match iter.next().unwrap() {
|
||||
TokenTree::Punct(punct) => {
|
||||
assert_eq!(punct.as_char(), '#');
|
||||
assert_eq!(punct.spacing(), Spacing::Alone);
|
||||
}
|
||||
_ => panic!("wrong token {:?}", tokens),
|
||||
}
|
||||
if inner {
|
||||
match iter.next().unwrap() {
|
||||
TokenTree::Punct(punct) => {
|
||||
assert_eq!(punct.as_char(), '!');
|
||||
assert_eq!(punct.spacing(), Spacing::Alone);
|
||||
}
|
||||
_ => panic!("wrong token {:?}", tokens),
|
||||
}
|
||||
}
|
||||
iter = match iter.next().unwrap() {
|
||||
TokenTree::Group(group) => {
|
||||
assert_eq!(group.delimiter(), Delimiter::Bracket);
|
||||
assert!(iter.next().is_none(), "unexpected token {:?}", tokens);
|
||||
group.stream().into_iter()
|
||||
}
|
||||
_ => panic!("wrong token {:?}", tokens),
|
||||
};
|
||||
match iter.next().unwrap() {
|
||||
TokenTree::Ident(ident) => assert_eq!(ident.to_string(), "doc"),
|
||||
_ => panic!("wrong token {:?}", tokens),
|
||||
}
|
||||
match iter.next().unwrap() {
|
||||
TokenTree::Punct(punct) => {
|
||||
assert_eq!(punct.as_char(), '=');
|
||||
assert_eq!(punct.spacing(), Spacing::Alone);
|
||||
}
|
||||
_ => panic!("wrong token {:?}", tokens),
|
||||
}
|
||||
match iter.next().unwrap() {
|
||||
TokenTree::Literal(literal) => {
|
||||
assert!(iter.next().is_none(), "unexpected token {:?}", tokens);
|
||||
literal
|
||||
}
|
||||
_ => panic!("wrong token {:?}", tokens),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closed_immediately() {
|
||||
let stream = "/**/".parse::<TokenStream>().unwrap();
|
||||
let tokens = stream.into_iter().collect::<Vec<_>>();
|
||||
assert!(tokens.is_empty(), "not empty -- {:?}", tokens);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incomplete() {
|
||||
assert!("/*/".parse::<TokenStream>().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lit() {
|
||||
let stream = "/// doc".parse::<TokenStream>().unwrap();
|
||||
let lit = lit_of_outer_doc_comment(&stream);
|
||||
assert_eq!(lit.to_string(), "\" doc\"");
|
||||
|
||||
let stream = "//! doc".parse::<TokenStream>().unwrap();
|
||||
let lit = lit_of_inner_doc_comment(&stream);
|
||||
assert_eq!(lit.to_string(), "\" doc\"");
|
||||
|
||||
let stream = "/** doc */".parse::<TokenStream>().unwrap();
|
||||
let lit = lit_of_outer_doc_comment(&stream);
|
||||
assert_eq!(lit.to_string(), "\" doc \"");
|
||||
|
||||
let stream = "/*! doc */".parse::<TokenStream>().unwrap();
|
||||
let lit = lit_of_inner_doc_comment(&stream);
|
||||
assert_eq!(lit.to_string(), "\" doc \"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn carriage_return() {
|
||||
let stream = "///\r\n".parse::<TokenStream>().unwrap();
|
||||
let lit = lit_of_outer_doc_comment(&stream);
|
||||
assert_eq!(lit.to_string(), "\"\"");
|
||||
|
||||
let stream = "/**\r\n*/".parse::<TokenStream>().unwrap();
|
||||
let lit = lit_of_outer_doc_comment(&stream);
|
||||
assert_eq!(lit.to_string(), "\"\\r\\n\"");
|
||||
|
||||
"///\r".parse::<TokenStream>().unwrap_err();
|
||||
"///\r \n".parse::<TokenStream>().unwrap_err();
|
||||
"/**\r \n*/".parse::<TokenStream>().unwrap_err();
|
||||
}
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
#![allow(clippy::assertions_on_constants, clippy::ignore_without_reason)]
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn make_sure_no_proc_macro() {
|
||||
assert!(
|
||||
!cfg!(feature = "proc-macro"),
|
||||
"still compiled with proc_macro?"
|
||||
);
|
||||
}
|
||||
Vendored
+97
@@ -0,0 +1,97 @@
|
||||
#![allow(clippy::extra_unused_type_parameters)]
|
||||
|
||||
use proc_macro2::{
|
||||
Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
|
||||
};
|
||||
|
||||
macro_rules! assert_impl {
|
||||
($ty:ident is $($marker:ident) and +) => {
|
||||
#[test]
|
||||
#[allow(non_snake_case)]
|
||||
fn $ty() {
|
||||
fn assert_implemented<T: $($marker +)+>() {}
|
||||
assert_implemented::<$ty>();
|
||||
}
|
||||
};
|
||||
|
||||
($ty:ident is not $($marker:ident) or +) => {
|
||||
#[test]
|
||||
#[allow(non_snake_case)]
|
||||
fn $ty() {
|
||||
$(
|
||||
{
|
||||
// Implemented for types that implement $marker.
|
||||
#[allow(dead_code)]
|
||||
trait IsNotImplemented {
|
||||
fn assert_not_implemented() {}
|
||||
}
|
||||
impl<T: $marker> IsNotImplemented for T {}
|
||||
|
||||
// Implemented for the type being tested.
|
||||
trait IsImplemented {
|
||||
fn assert_not_implemented() {}
|
||||
}
|
||||
impl IsImplemented for $ty {}
|
||||
|
||||
// If $ty does not implement $marker, there is no ambiguity
|
||||
// in the following trait method call.
|
||||
<$ty>::assert_not_implemented();
|
||||
}
|
||||
)+
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
assert_impl!(Delimiter is Send and Sync);
|
||||
assert_impl!(Spacing is Send and Sync);
|
||||
|
||||
assert_impl!(Group is not Send or Sync);
|
||||
assert_impl!(Ident is not Send or Sync);
|
||||
assert_impl!(LexError is not Send or Sync);
|
||||
assert_impl!(Literal is not Send or Sync);
|
||||
assert_impl!(Punct is not Send or Sync);
|
||||
assert_impl!(Span is not Send or Sync);
|
||||
assert_impl!(TokenStream is not Send or Sync);
|
||||
assert_impl!(TokenTree is not Send or Sync);
|
||||
|
||||
#[cfg(procmacro2_semver_exempt)]
|
||||
mod semver_exempt {
|
||||
use proc_macro2::LineColumn;
|
||||
|
||||
assert_impl!(LineColumn is Send and Sync);
|
||||
}
|
||||
|
||||
mod unwind_safe {
|
||||
#[cfg(procmacro2_semver_exempt)]
|
||||
use proc_macro2::LineColumn;
|
||||
use proc_macro2::{
|
||||
Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
|
||||
};
|
||||
use std::panic::{RefUnwindSafe, UnwindSafe};
|
||||
|
||||
macro_rules! assert_unwind_safe {
|
||||
($($types:ident)*) => {
|
||||
$(
|
||||
assert_impl!($types is UnwindSafe and RefUnwindSafe);
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
assert_unwind_safe! {
|
||||
Delimiter
|
||||
Group
|
||||
Ident
|
||||
LexError
|
||||
Literal
|
||||
Punct
|
||||
Spacing
|
||||
Span
|
||||
TokenStream
|
||||
TokenTree
|
||||
}
|
||||
|
||||
#[cfg(procmacro2_semver_exempt)]
|
||||
assert_unwind_safe! {
|
||||
LineColumn
|
||||
}
|
||||
}
|
||||
Vendored
+1093
File diff suppressed because it is too large.
Load diff
Vendored
+28
@@ -0,0 +1,28 @@
|
||||
#![allow(clippy::from_iter_instead_of_collect)]
|
||||
|
||||
use proc_macro2::{Delimiter, Group, Ident, Span, TokenStream, TokenTree};
|
||||
use std::iter;
|
||||
|
||||
#[test]
|
||||
fn test_fmt_group() {
|
||||
let ident = Ident::new("x", Span::call_site());
|
||||
let inner = TokenStream::from_iter(iter::once(TokenTree::Ident(ident)));
|
||||
let parens_empty = Group::new(Delimiter::Parenthesis, TokenStream::new());
|
||||
let parens_nonempty = Group::new(Delimiter::Parenthesis, inner.clone());
|
||||
let brackets_empty = Group::new(Delimiter::Bracket, TokenStream::new());
|
||||
let brackets_nonempty = Group::new(Delimiter::Bracket, inner.clone());
|
||||
let braces_empty = Group::new(Delimiter::Brace, TokenStream::new());
|
||||
let braces_nonempty = Group::new(Delimiter::Brace, inner.clone());
|
||||
let none_empty = Group::new(Delimiter::None, TokenStream::new());
|
||||
let none_nonempty = Group::new(Delimiter::None, inner);
|
||||
|
||||
// Matches libproc_macro.
|
||||
assert_eq!("()", parens_empty.to_string());
|
||||
assert_eq!("(x)", parens_nonempty.to_string());
|
||||
assert_eq!("[]", brackets_empty.to_string());
|
||||
assert_eq!("[x]", brackets_nonempty.to_string());
|
||||
assert_eq!("{ }", braces_empty.to_string());
|
||||
assert_eq!("{ x }", braces_nonempty.to_string());
|
||||
assert_eq!("", none_empty.to_string());
|
||||
assert_eq!("x", none_nonempty.to_string());
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
#![allow(unused_attributes)]
|
||||
|
||||
extern crate proc_macro;
|
||||
|
||||
use std::mem;
|
||||
|
||||
#[cfg_attr(not(target_pointer_width = "64"), ignore = "only applicable to 64-bit")]
|
||||
#[cfg_attr(randomize_layout, ignore = "disabled due to randomized layout")]
|
||||
#[test]
|
||||
fn test_proc_macro_size() {
|
||||
assert_eq!(mem::size_of::<proc_macro::Span>(), 4);
|
||||
assert_eq!(mem::size_of::<Option<proc_macro::Span>>(), 4);
|
||||
assert_eq!(mem::size_of::<proc_macro::Group>(), 20);
|
||||
assert_eq!(mem::size_of::<proc_macro::Ident>(), 12);
|
||||
assert_eq!(mem::size_of::<proc_macro::Punct>(), 8);
|
||||
assert_eq!(mem::size_of::<proc_macro::Literal>(), 16);
|
||||
assert_eq!(mem::size_of::<proc_macro::TokenStream>(), 4);
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_pointer_width = "64"), ignore = "only applicable to 64-bit")]
|
||||
#[cfg_attr(randomize_layout, ignore = "disabled due to randomized layout")]
|
||||
#[cfg_attr(wrap_proc_macro, ignore = "wrapper mode")]
|
||||
#[cfg_attr(span_locations, ignore = "span locations are on")]
|
||||
#[test]
|
||||
fn test_proc_macro2_fallback_size_without_locations() {
|
||||
assert_eq!(mem::size_of::<proc_macro2::Span>(), 0);
|
||||
assert_eq!(mem::size_of::<Option<proc_macro2::Span>>(), 1);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Group>(), 16);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Ident>(), 24);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Punct>(), 8);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Literal>(), 24);
|
||||
assert_eq!(mem::size_of::<proc_macro2::TokenStream>(), 8);
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_pointer_width = "64"), ignore = "only applicable to 64-bit")]
|
||||
#[cfg_attr(randomize_layout, ignore = "disabled due to randomized layout")]
|
||||
#[cfg_attr(wrap_proc_macro, ignore = "wrapper mode")]
|
||||
#[cfg_attr(not(span_locations), ignore = "span locations are off")]
|
||||
#[test]
|
||||
fn test_proc_macro2_fallback_size_with_locations() {
|
||||
assert_eq!(mem::size_of::<proc_macro2::Span>(), 8);
|
||||
assert_eq!(mem::size_of::<Option<proc_macro2::Span>>(), 12);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Group>(), 24);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Ident>(), 32);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Punct>(), 16);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Literal>(), 32);
|
||||
assert_eq!(mem::size_of::<proc_macro2::TokenStream>(), 8);
|
||||
}
|
||||
|
||||
#[rustversion::attr(before(1.71), ignore = "requires Rust 1.71+")]
|
||||
#[cfg_attr(not(target_pointer_width = "64"), ignore = "only applicable to 64-bit")]
|
||||
#[cfg_attr(randomize_layout, ignore = "disabled due to randomized layout")]
|
||||
#[cfg_attr(not(wrap_proc_macro), ignore = "fallback mode")]
|
||||
#[cfg_attr(span_locations, ignore = "span locations are on")]
|
||||
#[test]
|
||||
fn test_proc_macro2_wrapper_size_without_locations() {
|
||||
assert_eq!(mem::size_of::<proc_macro2::Span>(), 4);
|
||||
assert_eq!(mem::size_of::<Option<proc_macro2::Span>>(), 8);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Group>(), 24);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Ident>(), 24);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Punct>(), 12);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Literal>(), 24);
|
||||
assert_eq!(mem::size_of::<proc_macro2::TokenStream>(), 32);
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_pointer_width = "64"), ignore = "only applicable to 64-bit")]
|
||||
#[cfg_attr(randomize_layout, ignore = "disabled due to randomized layout")]
|
||||
#[cfg_attr(not(wrap_proc_macro), ignore = "fallback mode")]
|
||||
#[cfg_attr(not(span_locations), ignore = "span locations are off")]
|
||||
#[test]
|
||||
fn test_proc_macro2_wrapper_size_with_locations() {
|
||||
assert_eq!(mem::size_of::<proc_macro2::Span>(), 12);
|
||||
assert_eq!(mem::size_of::<Option<proc_macro2::Span>>(), 12);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Group>(), 32);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Ident>(), 32);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Punct>(), 20);
|
||||
assert_eq!(mem::size_of::<proc_macro2::Literal>(), 32);
|
||||
assert_eq!(mem::size_of::<proc_macro2::TokenStream>(), 32);
|
||||
}
|
||||
Reference in new issue
Block a user