diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml deleted file mode 100644 index cad5473..0000000 --- a/.github/workflows/demo.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Demo - -on: - push: - branches: [ "master" ] - pull_request: - branches: [ "master" ] - -permissions: - contents: write - -env: - CARGO_TERM_COLOR: always - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - name: Use nightly Rust - run: rustup default nightly - - name: Add wasm32 target - run: rustup target add wasm32-unknown-unknown - - name: Install wasm-bindgen CLI - run: cargo install wasm-bindgen-cli - - name: Build demo - run: cargo build -p as3_parser_demo --release --target wasm32-unknown-unknown - - name: Update demo/dist - run: wasm-bindgen --target web --out-dir demo/dist target/wasm32-unknown-unknown/release/as3_parser_demo.wasm - - name: Git commit report - run: | - git config --global user.name 'hydroper' - git config --global user.email 'matheusdiasdesouzads@gmail.com' - git switch -C demo - git rm --cached -r . - git add demo - git rm --cached -r demo/src - git rm --cached demo/Cargo.toml - git commit -m "Automated report" - git push origin -f demo diff --git a/.gitignore b/.gitignore deleted file mode 100644 index c41cc9e..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index f33f668..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,639 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "aho-corasick" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" -dependencies = [ - "memchr", -] - -[[package]] -name = "anstream" -version = "0.6.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" -dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" - -[[package]] -name = "anstyle-parse" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" -dependencies = [ - "windows-sys", -] - -[[package]] -name = "anstyle-wincon" -version = "3.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" -dependencies = [ - "anstyle", - "windows-sys", -] - -[[package]] -name = "anyhow" -version = "1.0.82" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" - -[[package]] -name = "as3_parser" -version = "1.0.17" -dependencies = [ - "bitflags", - "by_address", - "bytes", - "conv", - "file_paths", - "htmlentity", - "hydroper_source_text", - "late_format", - "lazy-regex", - "lazy_static", - "maplit", - "num-bigint", - "num-derive", - "num-traits", - "serde", - "serde_json", - "unicode-general-category", -] - -[[package]] -name = "as3_parser_demo" -version = "0.1.0" -dependencies = [ - "as3_parser", - "maplit", - "serde", - "serde_json", - "wasm-bindgen", - "web-sys", -] - -[[package]] -name = "as3_parser_test" -version = "0.4.0" -dependencies = [ - "as3_parser", - "clap", - "file_paths", - "maplit", - "serde", - "serde_json", -] - -[[package]] -name = "asdoc-example" -version = "0.1.0" -dependencies = [ - "as3_parser", -] - -[[package]] -name = "autocfg" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" - -[[package]] -name = "bitflags" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" -dependencies = [ - "serde", -] - -[[package]] -name = "bumpalo" -version = "3.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" - -[[package]] -name = "by_address" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64fa3c856b712db6612c019f14756e64e4bcea13337a6b33b696333a9eaa2d06" - -[[package]] -name = "bytes" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "clap" -version = "4.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" -dependencies = [ - "clap_builder", - "clap_derive", -] - -[[package]] -name = "clap_builder" -version = "4.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" -dependencies = [ - "anstream", - "anstyle", - "clap_lex", - "strsim", -] - -[[package]] -name = "clap_derive" -version = "4.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "clap_lex" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" - -[[package]] -name = "colorchoice" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" - -[[package]] -name = "conv" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ff10625fd0ac447827aa30ea8b861fead473bb60aeb73af6c1c58caf0d1299" -dependencies = [ - "custom_derive", -] - -[[package]] -name = "custom_derive" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef8ae57c4978a2acd8b869ce6b9ca1dfe817bff704c220209fdef2c0b75a01b9" - -[[package]] -name = "file_paths" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6655a548f719048f152b0349acf5f1b0fa7ce430acb49d4afc1d42513f160549" -dependencies = [ - "lazy-regex", -] - -[[package]] -name = "heck" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" - -[[package]] -name = "htmlentity" -version = "1.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab11d4e4796b618b77cabd8f0729eb8033891609820303af715e9306dbb95cd" -dependencies = [ - "anyhow", - "lazy_static", - "thiserror", -] - -[[package]] -name = "hydroper_source_text" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cce3d21a188729674efc26164f9a201d567ab2fe28b6789cb802d7ca66ee7aaf" - -[[package]] -name = "itoa" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" - -[[package]] -name = "js-sys" -version = "0.3.69" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" -dependencies = [ - "wasm-bindgen", -] - -[[package]] -name = "late_format" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c20a4c2785e13b46810cdd5b6df71b2bbacd6195d18d37b99098c6f52631dc6d" -dependencies = [ - "lazy-regex", - "maplit", -] - -[[package]] -name = "lazy-regex" -version = "3.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d12be4595afdf58bd19e4a9f4e24187da2a66700786ff660a418e9059937a4c" -dependencies = [ - "lazy-regex-proc_macros", - "once_cell", - "regex", -] - -[[package]] -name = "lazy-regex-proc_macros" -version = "3.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44bcd58e6c97a7fcbaffcdc95728b393b8d98933bfadad49ed4097845b57ef0b" -dependencies = [ - "proc-macro2", - "quote", - "regex", - "syn", -] - -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - -[[package]] -name = "log" -version = "0.4.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" - -[[package]] -name = "maplit" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" - -[[package]] -name = "memchr" -version = "2.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" - -[[package]] -name = "num-bigint" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" -dependencies = [ - "autocfg", - "num-integer", - "num-traits", -] - -[[package]] -name = "num-derive" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "num-integer" -version = "0.1.46" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" -dependencies = [ - "num-traits", -] - -[[package]] -name = "num-traits" -version = "0.2.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" -dependencies = [ - "autocfg", -] - -[[package]] -name = "once_cell" -version = "1.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" - -[[package]] -name = "proc-macro2" -version = "1.0.79" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "regex" -version = "1.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "regex-automata" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" - -[[package]] -name = "ryu" -version = "1.0.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" - -[[package]] -name = "serde" -version = "1.0.197" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.197" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.115" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" -dependencies = [ - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "strsim" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" - -[[package]] -name = "syn" -version = "2.0.58" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "thiserror" -version = "1.0.58" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.58" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "unicode-general-category" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2281c8c1d221438e373249e065ca4989c4c36952c211ff21a0ee91c44a3869e7" - -[[package]] -name = "unicode-ident" -version = "1.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" - -[[package]] -name = "utf8parse" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" - -[[package]] -name = "wasm-bindgen" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" -dependencies = [ - "cfg-if", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" - -[[package]] -name = "web-sys" -version = "0.3.69" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - -[[package]] -name = "windows-sys" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.52.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index d173730..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[workspace] -members = [ - "crates/parser", - "crates/parser_test", - "demo", - "examples/asdoc", -] -resolver = "2" \ No newline at end of file diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 9820eb1..0000000 --- a/LICENSE +++ /dev/null @@ -1,202 +0,0 @@ - - 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 - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright 2024 Hydroper - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index e9b92ac..0000000 --- a/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# ActionScript 3 Parser - -

- - - -

- -ActionScript 3 parser in the Rust language. - -[Online demo](https://hydroper.github.io/as3parser/demo) - -## Documentation - -Refer to the [wiki](https://github.com/hydroper/as3parser/wiki) of this repository for various informations, including a getting started point. - -## License - -Apache License 2.0, copyright 2024 Hydroper diff --git a/as3/promise/AggregateError.as b/as3/promise/AggregateError.as deleted file mode 100644 index c09f54c..0000000 --- a/as3/promise/AggregateError.as +++ /dev/null @@ -1,10 +0,0 @@ -package { - public class AggregateError extends Error { - public var errors: Array; - - public function AggregateError(errors: Array, message: String = "") { - super(message); - this.errors = errors.slice(0); - } - } -} \ No newline at end of file diff --git a/as3/promise/Promise.as b/as3/promise/Promise.as deleted file mode 100644 index 147b551..0000000 --- a/as3/promise/Promise.as +++ /dev/null @@ -1,354 +0,0 @@ -package { - import flash.utils.setTimeout; - import __AS3__.promise.*; - - /** - * The Promise object represents the eventual completion (or failure) - * of an asynchronous operation and its resulting value. - * - * For more information, consult [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). - * - * # Examples - * - * ``` - * const promise = new Promise.<*>(function(resolve: Function, reject: Function): void { - * // - * }); - * - * promise - * .then(function(): * {}) - * .catch(function(error: *): * {}) - * .finally(function(): void {}); - * ``` - */ - public final class Promise. { - // Implementation based on - // https://github.com/taylorhakes/promise-polyfill - - private var m_state:Number = 0; - private var m_handled:Boolean = false; - private var m_value:* = undefined; - private var m_deferreds:Vector. = new Vector.; - - private static function bindFunction(fn:Function, thisArg:*):Function { - return function(...argumentsList):void { - fn.apply(thisArg, argumentsList); - }; - } - - public function Promise(fn: Function) { - doResolve(fn, this); - } - - private static function handle(self:Promise., deferred:PromiseHandler):void { - while (self.m_state === 3) { - self = self.m_value; - } - if (self.m_state === 0) { - self.m_deferreds.push(deferred); - return; - } - self.m_handled = true; - Promise.._immediateFn(function():void { - var cb:Function = self.m_state === 1 ? deferred.onFulfilled : deferred.onRejected; - if (cb === null) { - (self.m_state === 1 ? Promise..privateResolve : Promise..privateReject)(deferred.promise, self.m_value); - return; - } - var ret:* = undefined; - try { - ret = cb(self.m_value); - } - catch (e:*) { - Promise..privateReject(deferred.promise, e); - return; - } - Promise..privateResolve(deferred.promise, ret); - }); - } - - private static function privateResolve(self:Promise., newValue:*):void { - try { - // Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure - if (newValue === self) { - throw new TypeError('A promise cannot be resolved with itself.'); - } - if (newValue is Promise) { - self.m_state = 3; - self.m_value = newValue; - Promise..finale(self); - return; - } - self.m_state = 1; - self.m_value = newValue; - Promise..finale(self); - } - catch (e:*) { - Promise..privateReject(self, e); - } - } - - private static function privateReject(self:Promise., newValue:*):void { - self.m_state = 2; - self.m_value = newValue; - Promise..finale(self); - } - - private static function finale(self:Promise.):void { - if (self.m_state === 2 && self.m_deferreds.length === 0) { - Promise.._immediateFn(function():void { - if (!self.m_handled) { - Promise.._unhandledRejectionFn(self.m_value); - } - }); - } - - for (var i:Number = 0, len:Number = self.m_deferreds.length; i < len; i++) { - handle(self, self.m_deferreds[i]); - } - self.m_deferreds = null; - } - - /** - * Takes a potentially misbehaving resolver function and make sure - * `onFulfilled` and `onRejected` are only called once. - * - * Makes no guarantees about asynchrony. - */ - private static function doResolve(fn:Function, self:Promise.):void { - var done:Boolean = false; - try { - fn( - function(value:*):* { - if (done) return; - done = true; - Promise..privateResolve(self, value); - }, - function(reason:*):* { - if (done) return; - done = true; - Promise..privateReject(self, reason); - } - ); - } - catch (ex:*) { - if (done) return; - done = true; - Promise..privateReject(self, ex); - } - } - - /** - * The `Promise.allSettled()` static method takes an iterable of promises - * as input and returns a single Promise. - * This returned promise fulfills when all of the input's promises settle - * (including when an empty iterable is passed), with an array of objects that - * describe the outcome of each promise. - * - * # Example - * - * ``` - * const promise1 = Promise..resolve(3); - * const promise2 = new Promise.(function(resolve: Function, reject: Function): void { - * setTimeout(reject, 100, "foo"); - * }); - * Promise..allSettled([promise1, promise2]) - * .then(function(results: Array): void { - * for each (const result in results) { - * trace(result.status); - * } - * }); - * // Expected output: - * // "fulfilled" - * // "rejected" - * ``` - * - * @return A `Promise` that is: - * - * - **Already fulfilled,** if the iterable passed is empty. - * - **Asynchronously fulfilled,** when all promises in the given - * iterable have settled (either fulfilled or rejected). - * The fulfillment value is an array of objects, each describing the - * outcome of one promise in the iterable, in the order of the promises passed, - * regardless of completion order. Each outcome object has the following properties: - * - `status`: A string, either `"fulfilled"` or `"rejected"`, indicating the eventual state of the promise. - * - `value`: Only present if `status` is `"fulfilled"`. The value that the promise was fulfilled with. - * - `reason`: Only present if `status` is `"rejected"`. The reason that the promise was rejected with. - * - * If the iterable passed is non-empty but contains no pending promises, - * the returned promise is still asynchronously (instead of synchronously) fulfilled. - */ - public static function allSettled(promises: Array): Promise. { - return new Promise(function(resolve:Function, reject:Function):void { - var args:Array = promises.slice(0); - if (args.length === 0) { - resolve([]); - return; - } - var remaining:Number = args.length; - function res(i:Number, val:*):void { - if (val is Promise) { - Promise(val).then( - function(val:*):* { - res(i, val); - }, - function(e:*):* { - args[i] = { status: 'rejected', reason: e }; - if (--remaining === 0) { - resolve(args); - } - } - ); - return; - } - args[i] = { status: 'fulfilled', value: val }; - if (--remaining === 0) { - resolve(args); - } - } - for (var i:Number = 0; i < args.length; ++i) { - res(i, args[i]); - } - }); - } // allSettled - - /** - * [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any) - */ - public static function any(promises:Array):Promise. { - return new Promise(function(resolve:Function, reject:Function):void { - var args:Array = promises.slice(0); - if (args.length === 0) { - reject(undefined); - return; - } - var rejectionReasons:Array = []; - for (var i:Number = 0; i < args.length; ++i) { - try { - Promise..resolve(args[i]) - .then(resolve) - .catch(function(error:*):* { - rejectionReasons.push(error); - if (rejectionReasons.length === args.length) { - reject( - new AggregateError( - rejectionReasons, - 'All promises were rejected' - ) - ); - } - }); - } - catch (ex:*) { - reject(ex); - } - } - }); - } // any - - public function finally(callback:Function):Promise. { - return this.then( - function(value:*):* { - return Promise..resolve(callback()).then(function(_:*):* { - return value; - }); - }, - function(reason:*):* { - return Promise..resolve(callback()).then(function(_:*):* { - return Promise..reject(reason); - }); - } - ); - } - - public function catch(onRejected: Function):Promise. { - return this.then(null, onRejected); - } - - public function then(onFulfilled: function(T): U, onRejected: function(*): E = null):Promise.<*> { - var prom = new Promise.<*>(function(_a:*, _b:*):void {}); - Promise..handle(this, new PromiseHandler(onFulfilled, onRejected, prom)); - return prom; - } - - /** - * [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) - */ - public static function all(promises: Array):Promise. { - return new Promise(function(resolve:Function, reject:Function):void { - var args:Array = promises.slice(0); - if (args.length === 0) { - resolve([]); - return; - } - var remaining:Number = args.length; - - function res(i:Number, val:*):void { - try { - if (val is Promise) { - Promise(val).then( - function(val): * { - res(i, val); - }, - reject - ); - return; - } - args[i] = val; - if (--remaining === 0) { - resolve(args); - } - } - catch (ex:*) { - reject(ex); - } - } - - for (var i:Number = 0; i < args.length; i++) { - res(i, args[i]); - } - }); - } // all - - /** - * [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve) - */ - public static function resolve(value: *):Promise. { - if (value is Promise) { - return Promise.(value); - } - - return new Promise.(function(resolve: Function, reject: Function): void { - resolve(value); - }); - } - - /** - * [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject) - */ - public static function reject(value: *): Promise. { - return new Promise(function(resolve: Function, reject: Function): void { - reject(value); - }); - } - - /** - * [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race) - */ - public static function race(promises:Array):Promise. { - return new Promise.(function(resolve:Function, reject:Function):void { - for each (var arg:* in promises) { - Promise..resolve(arg).then(resolve, reject); - } - }); - } - - private static function _immediateFn(fn:Function):void { - setTimeout(fn, 0); - } - - private static function _unhandledRejectionFn(err:*):void { - trace("Possible unhandled Promise rejection:", err); - } - } -} diff --git a/as3/promise/PromiseHandler.as b/as3/promise/PromiseHandler.as deleted file mode 100644 index 708f554..0000000 --- a/as3/promise/PromiseHandler.as +++ /dev/null @@ -1,16 +0,0 @@ -package __AS3__.promise { - /** - * @private - */ - public final class PromiseHandler { - public var onFulfilled: Function; - public var onRejected: Function; - public var promise: Promise; - - public function PromiseHandler(onFulfilled: Function, onRejected: Function, promise: Promise) { - this.onFulfilled = onFulfilled; - this.onRejected = onRejected; - this.promise = promise; - } - } -} diff --git a/crates/parser/Cargo.toml b/crates/parser/Cargo.toml deleted file mode 100644 index e3e195b..0000000 --- a/crates/parser/Cargo.toml +++ /dev/null @@ -1,34 +0,0 @@ -[package] -name = "as3_parser" -version = "1.0.18" -edition = "2021" -authors = ["hydroper "] -repository = "https://github.com/hydroper/as3parser" -keywords = ["actionscript", "as3", "parser"] -description = "ActionScript 3 parser" -license = "Apache-2.0" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[lib] -name = "as3_parser" -path = "lib.rs" - -[dependencies] -bitflags = { version = "2.4.1", features = ["serde"] } -bytes = "1" -file_paths = "1.0.0" -conv = "0.3.3" -htmlentity = "1.3.1" -late_format = "1.0.0" -maplit = "1.0.2" -num-bigint = "0.4" -num-derive = "0.4.1" -num-traits = "0.2.17" -lazy-regex = "3.0.2" -lazy_static = "1.4.0" -unicode-general-category = "0.6.0" -by_address = "1.1.0" -serde = { version = "1.0.192", features = ["rc", "derive"] } -serde_json = "1.0.108" -hydroper_source_text = "1.0.3" diff --git a/crates/parser/README.md b/crates/parser/README.md deleted file mode 100644 index 61237d1..0000000 --- a/crates/parser/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# ActionScript 3 Parser - -Refer to the [crate repository](https://github.com/hydroper/as3parser) for full details. diff --git a/crates/parser/compilation_unit.rs b/crates/parser/compilation_unit.rs deleted file mode 100644 index 93b1777..0000000 --- a/crates/parser/compilation_unit.rs +++ /dev/null @@ -1,8 +0,0 @@ -//! Defines the compilation unit, comments, and source locations. - -mod compilation_unit; -pub use compilation_unit::*; -mod comment; -pub use comment::*; -mod location; -pub use location::*; \ No newline at end of file diff --git a/crates/parser/compilation_unit/comment.rs b/crates/parser/compilation_unit/comment.rs deleted file mode 100644 index 6046ab7..0000000 --- a/crates/parser/compilation_unit/comment.rs +++ /dev/null @@ -1,59 +0,0 @@ -use crate::ns::*; - -pub struct Comment { - pub(crate) multiline: bool, - pub(crate) content: RefCell, - pub(crate) location: RefCell, -} - -impl Comment { - pub fn new(multiline: bool, content: String, location: Location) -> Self { - Self { - multiline, - content: RefCell::new(content), - location: RefCell::new(location), - } - } - - pub fn multiline(&self) -> bool { - self.multiline - } - - /// The content of the comment. - /// * If it is a multi-line comment, it includes all the characters after `/*` until `*/` (exclusive). - /// * If it is a single-line comment, it includes all the characters after `//` - /// until the next line terminator (exclusive) or end of program. - pub fn content(&self) -> String { - self.content.borrow().clone() - } - - pub fn set_content(&self, content: String) { - self.content.replace(content); - } - - pub fn location(&self) -> Location { - self.location.borrow().clone() - } - - pub fn set_location(&self, location: Location) { - self.location.replace(location); - } - - /// Indicates whether the comment is an AsDoc comment preceding - /// a specific location. - pub fn is_asdoc(&self, location_to_precede: &Location) -> bool { - if self.multiline && self.content.borrow().starts_with('*') { - let mut i: usize = self.location.borrow().last_offset; - for (i_1, ch) in self.location.borrow().compilation_unit().text()[i..].char_indices() { - i = i_1; - if !(CharacterValidator::is_whitespace(ch) || CharacterValidator::is_line_terminator(ch)) { - break; - } - } - i += self.location.borrow().last_offset; - location_to_precede.first_offset == i - } else { - false - } - } -} \ No newline at end of file diff --git a/crates/parser/compilation_unit/compilation_unit.rs b/crates/parser/compilation_unit/compilation_unit.rs deleted file mode 100644 index fe900a5..0000000 --- a/crates/parser/compilation_unit/compilation_unit.rs +++ /dev/null @@ -1,245 +0,0 @@ -use std::{any::Any, cell::RefMut}; -use crate::ns::*; -use hydroper_source_text::SourceText; - -/// `CompilationUnit` identifies an AS3 compilation unit and contains -/// a source text. -pub struct CompilationUnit { - pub(crate) file_path: Option, - pub(crate) source_text: SourceText, - pub(crate) compiler_options: RefCell>>, - pub(crate) diagnostics: RefCell>, - pub(crate) error_count: Cell, - pub(crate) warning_count: Cell, - pub(crate) invalidated: Cell, - pub(crate) comments: RefCell>>, - pub(crate) included_from: RefCell>>, - pub(crate) nested_compilation_units: RefCell>>, -} - -impl Default for CompilationUnit { - fn default() -> Self { - Self { - file_path: None, - source_text: SourceText::new("".into()), - compiler_options: RefCell::new(None), - diagnostics: RefCell::new(vec![]), - invalidated: Cell::new(false), - error_count: Cell::new(0), - warning_count: Cell::new(0), - comments: RefCell::new(vec![]), - nested_compilation_units: RefCell::new(vec![]), - included_from: RefCell::new(None), - } - } -} - -impl CompilationUnit { - /// Constructs a source file in unparsed and non verified state. - pub fn new(file_path: Option, text: String) -> Rc { - Rc::new(Self { - file_path, - source_text: SourceText::new(text), - compiler_options: RefCell::new(None), - diagnostics: RefCell::new(vec![]), - invalidated: Cell::new(false), - error_count: Cell::new(0), - warning_count: Cell::new(0), - comments: RefCell::new(vec![]), - nested_compilation_units: RefCell::new(vec![]), - included_from: RefCell::new(None), - }) - } - - /// File path of the source or `None` if not a file. - pub fn file_path(&self) -> Option { - self.file_path.clone() - } - - /// Source text. - pub fn text(&self) -> &String { - &self.source_text.contents - } - - /// Compiler options. - pub fn compiler_options(&self) -> Option> { - self.compiler_options.borrow().clone() - } - - /// Set compiler options. - pub fn set_compiler_options(&self, options: Option>) { - self.compiler_options.replace(options); - } - - /// Whether the source contains any errors after parsing - /// and/or verification. - pub fn invalidated(&self) -> bool { - self.invalidated.get() - } - - /// The comments present in the source file. To get mutable access to the - /// collection of comments, use the `comments_mut()` method instead. - pub fn comments(&self) -> Vec> { - let mut collection = vec![]; - for c in self.comments.borrow().iter() { - collection.push(c.clone()); - } - collection - } - - /// The comments present in the source file, as a mutable collection. - pub fn comments_mut(&self) -> RefMut>> { - self.comments.borrow_mut() - } - - /// Contributes a comment if there is no other comment - /// in the same location. - pub fn add_comment(&self, comment: Rc) { - let mut dup = false; - let i = comment.location.borrow().first_offset(); - for c1 in self.comments.borrow().iter() { - if c1.location.borrow().first_offset == i { - dup = true; - break; - } - } - if !dup { - self.comments.borrow_mut().push(comment); - } - } - - /// Diagnostics of the source file after parsing and/or - /// verification. - pub fn diagnostics(&self) -> Vec { - self.diagnostics.borrow().clone() - } - - /// Diagnostics of the source file after parsing and/or - /// verification, including those of nested compilation units. - pub fn nested_diagnostics(&self) -> Vec { - let mut result = self.diagnostics(); - for unit in self.nested_compilation_units.borrow().iter() { - result.extend(unit.nested_diagnostics()); - } - result - } - - /// Sort diagnostics from the compilation unit - /// and any nested compilation units. - pub fn sort_diagnostics(&self) { - self.diagnostics.borrow_mut().sort(); - for unit in self.nested_compilation_units.borrow().iter() { - unit.sort_diagnostics(); - } - } - - /// Determines whether to skip contributing an error when it - /// occurs at the same offset of another error. - pub fn prevent_equal_offset_error(&self, location: &Location) -> bool { - let diag_list = self.diagnostics.borrow(); - for diag in diag_list.iter() { - if diag.is_warning() { - continue; - } - if diag.location.first_offset == location.first_offset { - return true; - } - } - false - } - - /// Determines whether to skip contributing a warning when it - /// occurs at the same offset of another warning. - pub fn prevent_equal_offset_warning(&self, location: &Location) -> bool { - let diag_list = self.diagnostics.borrow(); - for diag in diag_list.iter() { - if diag.is_error() { - continue; - } - if diag.location.first_offset == location.first_offset { - return true; - } - } - false - } - - /// If this compilation unit is subsequent of an include directive in another - /// compilation unit, returns the compilation unit of that include directive. - pub fn included_from(&self) -> Option> { - self.included_from.borrow().clone() - } - - pub(crate) fn set_included_from(&self, included_from: Option>) { - self.included_from.replace(included_from); - } - - pub(crate) fn include_directive_is_circular(&self, file_path: &str) -> bool { - if canonicalize_path(&self.file_path.clone().unwrap_or("".into())) == canonicalize_path(file_path) { - return true; - } - if let Some(included_from) = self.included_from() { - return included_from.include_directive_is_circular(file_path); - } - return false; - } - - pub fn nested_compilation_units(&self) -> Vec> { - let mut result = vec![]; - for unit in self.nested_compilation_units.borrow().iter() { - result.push(unit.clone()); - } - result - } - - pub fn add_nested_compilation_unit(&self, unit: Rc) { - self.nested_compilation_units.borrow_mut().push(unit); - } - - pub fn add_diagnostic(&self, diagnostic: Diagnostic) { - if diagnostic.is_warning() { - self.warning_count.set(self.warning_count.get() + 1); - } else { - self.error_count.set(self.error_count.get() + 1); - self.invalidated.set(true); - } - self.diagnostics.borrow_mut().push(diagnostic); - } - - pub fn error_count(&self) -> u32 { - self.error_count.get() - } - - pub fn warning_count(&self) -> u32 { - self.warning_count.get() - } - - /// Retrieves line number from an offset. The resulting line number - /// is counted from one. - pub fn get_line_number(&self, offset: usize) -> usize { - self.source_text.get_line_number(offset) - } - - /// Returns the zero based column of an offset. - pub fn get_column(&self, offset: usize) -> usize { - self.source_text.get_column(offset) - } - - /// Retrieves offset from line number (counted from one). - pub fn get_line_offset(&self, line: usize) -> Option { - self.source_text.get_line_offset(line) - } - - /// Retrieves the offset from the corresponding line of an offset. - pub fn get_line_offset_from_offset(&self, offset: usize) -> usize { - self.source_text.get_line_offset_from_offset(offset) - } - - pub fn get_line_indent(&self, line: usize) -> usize { - let line_offset = self.get_line_offset(line).unwrap(); - CharacterValidator::indent_count(&self.source_text.contents[line_offset..]) - } -} - -fn canonicalize_path(path: &str) -> String { - std::path::Path::new(path).canonicalize().unwrap_or(std::path::PathBuf::new()).to_string_lossy().into_owned() -} \ No newline at end of file diff --git a/crates/parser/compilation_unit/location.rs b/crates/parser/compilation_unit/location.rs deleted file mode 100644 index 44d46ed..0000000 --- a/crates/parser/compilation_unit/location.rs +++ /dev/null @@ -1,176 +0,0 @@ -use std::cmp::Ordering; -use std::fmt::Debug; -use serde::{Serialize, Deserialize, Serializer}; -use std::rc::Rc; -use crate::compilation_unit::*; -use crate::util::{CharacterReader, count_first_whitespace_characters}; - -/// Represents a source location. This location includes -/// spanning lines and columns and the reference compilation unit. -#[derive(Clone, Deserialize)] -pub struct Location { - /// The compilation unit that this location belongs to. - #[serde(skip)] - pub(crate) compilation_unit: Rc, - - /// First UTF-8 offset. - #[serde(skip)] - pub(crate) first_offset: usize, - - /// Last UTF-8 offset. - #[serde(skip)] - pub(crate) last_offset: usize, -} - -impl Serialize for Location { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str(&format!("{}:{}-{}:{}", self.first_line_number(), self.first_column() + 1, self.last_line_number(), self.last_column() + 1)) - } -} - -impl Debug for Location { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, - "Location(first_line_number={}, first_column={}, first_offset={}, last_line_number={}, last_column={}, last_offset={})", - self.first_line_number(), - self.first_column(), - self.first_offset, - self.last_line_number(), - self.last_column(), - self.last_offset - ) - } -} - -impl Eq for Location {} - -impl PartialEq for Location { - fn eq(&self, other: &Self) -> bool { - Rc::ptr_eq(&self.compilation_unit, &other.compilation_unit) && - self.first_offset == other.first_offset && - self.last_offset == other.last_offset - } -} - -impl Ord for Location { - fn cmp(&self, other: &Self) -> Ordering { - self.partial_cmp(other).unwrap_or(Ordering::Equal) - } -} - -impl PartialOrd for Location { - fn partial_cmp(&self, other: &Self) -> Option { - self.first_offset.partial_cmp(&other.first_offset) - } -} - -impl Location { - /// Builds a location. - pub fn with_offsets( - compilation_unit: &Rc, - first_offset: usize, - last_offset: usize, - ) -> Self { - Self { - compilation_unit: compilation_unit.clone(), - first_offset, - last_offset, - } - } - - /// Builds a location. - pub fn with_offset(compilation_unit: &Rc, offset: usize) -> Self { - Self::with_offsets(compilation_unit, offset, offset) - } - - /// Build a location by combining two locations. `self` - /// serves as the first location, while `other` serves as the - /// last location. - pub fn combine_with(&self, other: Location) -> Self { - Self { - compilation_unit: self.compilation_unit.clone(), - first_offset: self.first_offset, - last_offset: other.last_offset, - } - } - - /// Build a location by combining two locations. `self` - /// serves as the first location, while the first column and first line - /// of `other` serve as the last location. - pub fn combine_with_start_of(&self, other: Location) -> Self { - Self { - compilation_unit: self.compilation_unit.clone(), - first_offset: self.first_offset, - last_offset: other.first_offset, - } - } - - /// The compilation unit that this location belongs to. - pub fn compilation_unit(&self) -> Rc { - self.compilation_unit.clone() - } - - /// First line number, counted from one. - pub fn first_line_number(&self) -> usize { - self.compilation_unit.get_line_number(self.first_offset) - } - - /// Last line number, counted from one. - pub fn last_line_number(&self) -> usize { - self.compilation_unit.get_line_number(self.last_offset) - } - - // The first byte offset of this location. - pub fn first_offset(&self) -> usize { - self.first_offset - } - - // The last byte offset of this location. - pub fn last_offset(&self) -> usize { - self.last_offset - } - - /// Zero based first column of the location in code points. - pub fn first_column(&self) -> usize { - self.compilation_unit.get_column(self.first_offset) - } - - /// Zero based last column of the location in code points. - pub fn last_column(&self) -> usize { - self.compilation_unit.get_column(self.last_offset) - } - - pub fn character_count(&self) -> usize { - self.compilation_unit.text()[self.first_offset..self.last_offset].chars().count() - } - - /// Indicates whether a previous location and a next location - /// have a line break in between. - pub fn line_break(&self, other: &Self) -> bool { - self.last_line_number() != other.first_line_number() - } - - /// Returns the source text comprising the source location. - pub fn text(&self) -> String { - self.compilation_unit.text()[self.first_offset..self.last_offset].to_owned() - } - - /// Shifts a count of characters off this location until end-of-file. - pub fn shift_until_eof(&self, count: usize) -> Location { - let mut ch = CharacterReader::from(&self.compilation_unit.text()[self.first_offset..]); - for _ in 0..count { - if ch.next().is_none() { - break; - } - } - Self::with_offsets(&self.compilation_unit, self.first_offset + ch.index(), self.last_offset) - } - - /// Shifts the count of whitespace characters in a text off this location. - pub fn shift_whitespace(&self, text: &str) -> Location { - self.shift_until_eof(count_first_whitespace_characters(text)) - } -} \ No newline at end of file diff --git a/crates/parser/diagnostics.rs b/crates/parser/diagnostics.rs deleted file mode 100644 index 34522bf..0000000 --- a/crates/parser/diagnostics.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! Defines the diagnostics produced by the parser. - -mod diagnostics; -pub use diagnostics::*; -mod diagnostic_kind; -pub use diagnostic_kind::*; \ No newline at end of file diff --git a/crates/parser/diagnostics/diagnostic_kind.rs b/crates/parser/diagnostics/diagnostic_kind.rs deleted file mode 100644 index 6931fe4..0000000 --- a/crates/parser/diagnostics/diagnostic_kind.rs +++ /dev/null @@ -1,77 +0,0 @@ -#[repr(i32)] -#[derive(Eq, PartialEq, Clone, Copy)] -pub enum DiagnosticKind { - InvalidEscapeValue = 1024, - UnexpectedEnd = 1025, - UnallowedNumericSuffix = 1026, - StringLiteralMustBeTerminatedBeforeLineBreak = 1027, - Expecting = 1028, - ExpectingIdentifier = 1029, - ExpectingExpression = 1030, - ExpectingXmlName = 1031, - ExpectingXmlAttributeValue = 1032, - IllegalNullishCoalescingLeftOperand = 1033, - WrongParameterPosition = 1034, - DuplicateRestParameter = 1035, - NotAllowedHere = 1036, - MalformedRestParameter = 1037, - IllegalForInInitializer = 1038, - MultipleForInBindings = 1039, - UndefinedLabel = 1040, - IllegalContinue = 1041, - IllegalBreak = 1042, - ExpressionMustNotFollowLineBreak = 1043, - TokenMustNotFollowLineBreak = 1044, - ExpectingStringLiteral = 1045, - DuplicateAttribute = 1046, - DuplicateAccessModifier = 1047, - ExpectingDirectiveKeyword = 1048, - UnallowedAttribute = 1049, - UseDirectiveMustContainPublic = 1050, - MalformedEnumMember = 1051, - FunctionMayNotBeGenerator = 1052, - FunctionMayNotBeAsynchronous = 1053, - FunctionMustNotContainBody = 1054, - FunctionMustContainBody = 1055, - FunctionMustNotContainAnnotations = 1056, - NestedClassesNotAllowed = 1057, - UnexpectedDirective = 1058, - FailedParsingAsDocTag = 1059, - UnrecognizedAsDocTag = 1060, - UnrecognizedProxy = 1061, - EnumMembersMustBeConst = 1062, - ConstructorMustNotSpecifyResultType = 1063, - UnrecognizedMetadataSyntax = 1064, - FailedToIncludeFile = 1065, - ParentSourceIsNotAFile = 1066, - CircularIncludeDirective = 1067, - MalformedDestructuring = 1068, - XmlPrefixNotDefined = 1069, - RedefiningXmlAttribute = 1070, - InvalidXmlPi = 1071, - XmlPiUnknownAttribute = 1072, - XmlPiVersion = 1073, - XmlPiEncoding = 1074, - XmlMustConsistOfExactly1Element = 1075, - XmlNameAtMostOneColon = 1076, - UnexpectedCharacter = 1077, - InputEndedBeforeReachingClosingQuoteForString = 1078, - InputEndedBeforeReachingClosingSeqForCData = 1079, - InputEndedBeforeReachingClosingSeqForPi = 1080, - InputEndedBeforeReachingClosingSeqForXmlComment = 1081, - InputEndedBeforeReachingClosingSeqForMultiLineComment = 1082, - InputEndedBeforeReachingClosingSlashForRegExp = 1083, - InputEndedBeforeReachingClosingQuoteForAttributeValue = 1084, - ExpectingEitherSemicolonOrNewLineHere = 1085, - CssInvalidHexEscape = 1086, - ExpectingDirective = 1087, - ExpectingStatement = 1088, - Unexpected = 1089, - XmlClosingTagNameMustBeEquals = 1090, -} - -impl DiagnosticKind { - pub fn id(&self) -> i32 { - *self as i32 - } -} \ No newline at end of file diff --git a/crates/parser/diagnostics/diagnostics.rs b/crates/parser/diagnostics/diagnostics.rs deleted file mode 100644 index 351b58c..0000000 --- a/crates/parser/diagnostics/diagnostics.rs +++ /dev/null @@ -1,175 +0,0 @@ -use std::any::Any; - -use maplit::hashmap; -use crate::ns::*; - -#[path = "diagnostics_english_resources.rs"] -mod diagnostics_english_resources; - -/// Represents a diagnostic originated from a compilation unit. -/// -/// Arguments are formatted using integer keys counted from 1 (one). -#[derive(Clone)] -pub struct Diagnostic { - pub(crate) location: Location, - pub(crate) kind: DiagnosticKind, - pub(crate) is_warning: bool, - pub(crate) is_verify_error: bool, - pub(crate) arguments: Vec>, - pub(crate) custom_kind: RefCell>>, -} - -impl Eq for Diagnostic {} - -impl PartialEq for Diagnostic { - fn eq(&self, other: &Self) -> bool { - self.location == other.location && - self.kind == other.kind - } -} - -impl Ord for Diagnostic { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.location.cmp(&other.location) - } -} - -impl PartialOrd for Diagnostic { - fn partial_cmp(&self, other: &Self) -> Option { - self.location.partial_cmp(&other.location) - } -} - -impl Diagnostic { - pub fn new_syntax_error(location: &Location, kind: DiagnosticKind, arguments: Vec>) -> Self { - Self { - location: location.clone(), - kind, - is_verify_error: false, - is_warning: false, - arguments, - custom_kind: RefCell::new(None), - } - } - - pub fn new_verify_error(location: &Location, kind: DiagnosticKind, arguments: Vec>) -> Self { - Self { - location: location.clone(), - kind, - is_verify_error: true, - is_warning: false, - arguments, - custom_kind: RefCell::new(None), - } - } - - pub fn new_warning(location: &Location, kind: DiagnosticKind, arguments: Vec>) -> Self { - Self { - location: location.clone(), - kind, - is_verify_error: false, - is_warning: true, - arguments, - custom_kind: RefCell::new(None), - } - } - - pub fn location(&self) -> Location { - self.location.clone() - } - - pub fn kind(&self) -> DiagnosticKind { - self.kind.clone() - } - - pub fn is_warning(&self) -> bool { - self.is_warning - } - - pub fn is_error(&self) -> bool { - !self.is_warning - } - - pub fn is_syntax_error(&self) -> bool { - !self.is_verify_error && !self.is_warning - } - - pub fn is_verify_error(&self) -> bool { - self.is_verify_error - } - - pub fn arguments(&self) -> Vec> { - self.arguments.clone() - } - - pub fn id(&self) -> i32 { - self.kind.id() - } - - pub fn custom_kind(&self) -> Option> { - self.custom_kind.borrow().clone() - } - - pub fn set_custom_kind(&self, id: Option>) { - self.custom_kind.replace(id); - } - - /// Formats the diagnostic by overriding the message text. - pub fn format_with_message(&self, message: &str, id: Option) -> String { - let category = (if self.is_verify_error { - "Verify error" - } else if self.is_warning { - "Warning" - } else { - "Syntax error" - }).to_owned(); - - let file_path = self.location.compilation_unit.file_path.clone().map_or("".to_owned(), |s| format!("{s}:")); - let line = self.location.first_line_number(); - let column = self.location.first_column() + 1; - if let Some(id) = id { - format!("{file_path}{line}:{column}: {category} #{}: {message}", id.to_string()) - } else { - format!("{file_path}{line}:{column}: {category}: {message}") - } - } - - /// Formats the diagnostic in English. - pub fn format_english(&self) -> String { - self.format_with_message(&self.format_message_english(), Some(self.id())) - } - - pub fn format_message_english(&self) -> String { - self.format_message(&diagnostics_english_resources::DATA) - } - - pub fn format_message(&self, messages: &HashMap) -> String { - let mut string_arguments: HashMap = hashmap!{}; - let mut i = 1; - for argument in &self.arguments { - string_arguments.insert(i.to_string(), argument.to_string()); - i += 1; - } - use late_format::LateFormat; - let Some(msg) = messages.get(&self.id()) else { - let id = self.id(); - panic!("Message resource is missing for ID {id}"); - }; - msg.late_format(string_arguments) - } -} - -/// The `diagarg![...]` literal is used for initializing -/// diagnostic arguments. -/// -/// For example: `diagarg![token, "foo".to_owned()]`. -pub macro diagarg { - ($($value:expr),*) => { vec![ $(Rc::new($value)),* ] }, -} - -pub trait DiagnosticArgument: Any + ToString + 'static { -} - -impl DiagnosticArgument for String {} - -impl DiagnosticArgument for Token {} \ No newline at end of file diff --git a/crates/parser/diagnostics/diagnostics_english_resources.rs b/crates/parser/diagnostics/diagnostics_english_resources.rs deleted file mode 100644 index dd28ee6..0000000 --- a/crates/parser/diagnostics/diagnostics_english_resources.rs +++ /dev/null @@ -1,76 +0,0 @@ -use lazy_static::lazy_static; -use maplit::hashmap; -use crate::ns::*; - -lazy_static! { - pub static ref DATA: HashMap = hashmap! { - // DiagnosticKind::K.id() => ".".into(), - DiagnosticKind::InvalidEscapeValue.id() => "Invalid escape value.".into(), - DiagnosticKind::UnexpectedEnd.id() => "Unexpected end-of-file.".into(), - DiagnosticKind::UnallowedNumericSuffix.id() => "Unallowed numeric suffix.".into(), - DiagnosticKind::StringLiteralMustBeTerminatedBeforeLineBreak.id() => "A string literal must be terminated before the line break.".into(), - DiagnosticKind::Expecting.id() => "Expecting {1} before {2}.".into(), - DiagnosticKind::ExpectingIdentifier.id() => "Expecting identifier before {1}.".into(), - DiagnosticKind::ExpectingExpression.id() => "Expecting expression before {1}.".into(), - DiagnosticKind::ExpectingXmlName.id() => "Expecting XML name before {1}.".into(), - DiagnosticKind::ExpectingXmlAttributeValue.id() => "Expecting XML attribute value before {1}.".into(), - DiagnosticKind::IllegalNullishCoalescingLeftOperand.id() => "Illegal nullish coalescing left operand.".into(), - DiagnosticKind::WrongParameterPosition.id() => "Wrong parameter position.".into(), - DiagnosticKind::DuplicateRestParameter.id() => "Duplicate rest parameter.".into(), - DiagnosticKind::NotAllowedHere.id() => "{1} not allowed here.".into(), - DiagnosticKind::MalformedRestParameter.id() => "Malformed rest parameter.".into(), - DiagnosticKind::IllegalForInInitializer.id() => "Illegal 'for..in' initializer.".into(), - DiagnosticKind::MultipleForInBindings.id() => "Multiple 'for..in' bindings are not allowed.".into(), - DiagnosticKind::UndefinedLabel.id() => "Undefined label '{1}'.".into(), - DiagnosticKind::IllegalContinue.id() => "Illegal continue statement.".into(), - DiagnosticKind::IllegalBreak.id() => "Illegal break statement.".into(), - DiagnosticKind::ExpressionMustNotFollowLineBreak.id() => "Expression must not follow line break.".into(), - DiagnosticKind::TokenMustNotFollowLineBreak.id() => "Token must not follow line break.".into(), - DiagnosticKind::ExpectingStringLiteral.id() => "Expecting string literal before {1}.".into(), - DiagnosticKind::DuplicateAttribute.id() => "Duplicate attribute.".into(), - DiagnosticKind::DuplicateAccessModifier.id() => "Duplicate access modifier.".into(), - DiagnosticKind::ExpectingDirectiveKeyword.id() => "Expecting either 'var', 'const', 'function', 'class' or 'interface'.".into(), - DiagnosticKind::UnallowedAttribute.id() => "Unallowed attribute.".into(), - DiagnosticKind::UseDirectiveMustContainPublic.id() => "Use directive must contain the 'public' attribute.".into(), - DiagnosticKind::MalformedEnumMember.id() => "Malformed enumeration member.".into(), - DiagnosticKind::FunctionMayNotBeGenerator.id() => "Function may not be generator.".into(), - DiagnosticKind::FunctionMayNotBeAsynchronous.id() => "Function may not be asynchronous.".into(), - DiagnosticKind::FunctionMustNotContainBody.id() => "Function must not contain body.".into(), - DiagnosticKind::FunctionMustContainBody.id() => "Function must contain body.".into(), - DiagnosticKind::FunctionMustNotContainAnnotations.id() => "Function must not contain annotations.".into(), - DiagnosticKind::NestedClassesNotAllowed.id() => "Nested classes are not allowed.".into(), - DiagnosticKind::UnexpectedDirective.id() => "Unexpected directive.".into(), - DiagnosticKind::FailedParsingAsDocTag.id() => "Failed parsing contents of ASDoc tag: '@{1}'.".into(), - DiagnosticKind::UnrecognizedAsDocTag.id() => "Unrecognized ASDoc tag: '@{1}'.".into(), - DiagnosticKind::UnrecognizedProxy.id() => "Unrecognized proxy: '{1}'.".into(), - DiagnosticKind::EnumMembersMustBeConst.id() => "Enumeration members must be 'const'.".into(), - DiagnosticKind::UnrecognizedMetadataSyntax.id() => "Unrecognized meta-data syntax.".into(), - DiagnosticKind::FailedToIncludeFile.id() => "Failed to include file.".into(), - DiagnosticKind::ParentSourceIsNotAFile.id() => "Parent source is not a file.".into(), - DiagnosticKind::CircularIncludeDirective.id() => "Circular include directive.".into(), - DiagnosticKind::MalformedDestructuring.id() => "Malformed destructuring.".into(), - DiagnosticKind::XmlPrefixNotDefined.id() => "Prefix not defined: '{1}'.".into(), - DiagnosticKind::RedefiningXmlAttribute.id() => "Redefining attribute: '{1}'.".into(), - DiagnosticKind::InvalidXmlPi.id() => "Invalid processing instruction.".into(), - DiagnosticKind::XmlPiUnknownAttribute.id() => "Unknown attribute at processing instruction: '{1}'.".into(), - DiagnosticKind::XmlPiVersion.id() => "XML version must be '1.0'.".into(), - DiagnosticKind::XmlPiEncoding.id() => "XML encoding must be either 'utf-8' or 'utf-16'.".into(), - DiagnosticKind::XmlMustConsistOfExactly1Element.id() => "Document must consist of exactly one element.".into(), - DiagnosticKind::XmlNameAtMostOneColon.id() => "XML name may have at most one colon.".into(), - DiagnosticKind::UnexpectedCharacter.id() => "Unexpected character. '{1}' is not allowed here".into(), - DiagnosticKind::InputEndedBeforeReachingClosingQuoteForString.id() => "Input ended before reaching the closing quotation mark for a string literal.".into(), - DiagnosticKind::InputEndedBeforeReachingClosingSeqForCData.id() => "Input ended before reaching the closing ']]>' for a CDATA.".into(), - DiagnosticKind::InputEndedBeforeReachingClosingSeqForPi.id() => "Input ended before reaching the closing '?>' for a processing instruction.".into(), - DiagnosticKind::InputEndedBeforeReachingClosingSeqForXmlComment.id() => "Input ended before reaching the closing '-->' for a comment.".into(), - DiagnosticKind::InputEndedBeforeReachingClosingSeqForMultiLineComment.id() => "Input ended before reaching the closing '*/' for a comment.".into(), - DiagnosticKind::InputEndedBeforeReachingClosingSlashForRegExp.id() => "Input ended before reaching the closing slash for a regular expression.".into(), - DiagnosticKind::InputEndedBeforeReachingClosingQuoteForAttributeValue.id() => "Input ended before reaching the closing quotation mark for an attribute value.".into(), - DiagnosticKind::ExpectingEitherSemicolonOrNewLineHere.id() => "Expecting either a semicolon or a new line here.".into(), - DiagnosticKind::CssInvalidHexEscape.id() => "Invalid hexadecimal escape: '\\{1}'.".into(), - DiagnosticKind::ExpectingDirective.id() => "Expecting directive before {1}.".into(), - DiagnosticKind::ExpectingStatement.id() => "Expecting statement before {1}.".into(), - DiagnosticKind::Unexpected.id() => "Unexpected {1}.".into(), - DiagnosticKind::XmlClosingTagNameMustBeEquals.id() => "Closing tag name must be equals '{1}'.".into(), - // DiagnosticKind::K.id() => ".".into(), - }; -} diff --git a/crates/parser/lib.rs b/crates/parser/lib.rs deleted file mode 100644 index c82a279..0000000 --- a/crates/parser/lib.rs +++ /dev/null @@ -1,11 +0,0 @@ -#![feature(decl_macro)] -#![feature(try_blocks)] - -pub mod tree; -pub mod compilation_unit; -pub mod diagnostics; -pub mod operator; -pub mod parser; -pub mod util; - -pub mod ns; \ No newline at end of file diff --git a/crates/parser/ns.rs b/crates/parser/ns.rs deleted file mode 100644 index 09adc4b..0000000 --- a/crates/parser/ns.rs +++ /dev/null @@ -1,8 +0,0 @@ -//! The `ns` module is an union of all of the parser modules. - -pub use crate::tree::*; -pub use crate::compilation_unit::*; -pub use crate::diagnostics::*; -pub use crate::operator::*; -pub use crate::parser::*; -pub use crate::util::*; \ No newline at end of file diff --git a/crates/parser/operator.rs b/crates/parser/operator.rs deleted file mode 100644 index bbad3db..0000000 --- a/crates/parser/operator.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! Defines the ActionScript 3 operators. - -mod operator; -pub use operator::*; -mod operator_precedence; -pub use operator_precedence::*; \ No newline at end of file diff --git a/crates/parser/operator/operator.rs b/crates/parser/operator/operator.rs deleted file mode 100644 index 69cd19a..0000000 --- a/crates/parser/operator/operator.rs +++ /dev/null @@ -1,127 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// Represents an ActionScript operator. -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] -pub enum Operator { - PostIncrement, - PostDecrement, - NonNull, - Delete, - Void, - Typeof, - Await, - Yield, - PreIncrement, - PreDecrement, - Positive, - Negative, - BitwiseNot, - LogicalNot, - - Power, - Multiply, - Divide, - Remainder, - Add, - Subtract, - ShiftLeft, - ShiftRight, - ShiftRightUnsigned, - Lt, - Gt, - Le, - Ge, - Instanceof, - In, - NotIn, - Is, - IsNot, - As, - Equals, - NotEquals, - StrictEquals, - StrictNotEquals, - BitwiseAnd, - BitwiseXor, - BitwiseOr, - LogicalAnd, - LogicalXor, - LogicalOr, - NullCoalescing, -} - -/// Represents binary operator associativity. -#[derive(Copy, Clone, PartialEq, Eq)] -pub enum BinaryAssociativity { - LeftToRight, - RightToLeft, -} - -/// Represents an ActionScript binary operator. -#[derive(Copy, Clone, PartialEq, Eq)] -pub struct BinaryOperator(pub Operator, pub OperatorPrecedence, pub BinaryAssociativity); - -impl BinaryOperator { - pub fn operator(&self) -> Operator { - self.0 - } - - pub fn precedence(&self) -> OperatorPrecedence { - self.1 - } - - pub fn associativity(&self) -> BinaryAssociativity { - self.2 - } - - pub fn right_precedence(&self) -> OperatorPrecedence { - if self.operator() == Operator::NullCoalescing { - OperatorPrecedence::BitwiseOr - } else { - self.precedence().add(if self.associativity() == BinaryAssociativity::LeftToRight { 1 } else { 0 }).unwrap() - } - } -} - -impl TryFrom for BinaryOperator { - type Error = (); - /// Constructs `BinaryOperator` from abstract operator. - fn try_from(value: Operator) -> Result { - match value { - Operator::Multiply => Ok(BinaryOperator(value, OperatorPrecedence::Multiplicative, BinaryAssociativity::LeftToRight)), - Operator::Divide => Ok(BinaryOperator(value, OperatorPrecedence::Multiplicative, BinaryAssociativity::LeftToRight)), - Operator::Remainder => Ok(BinaryOperator(value, OperatorPrecedence::Multiplicative, BinaryAssociativity::LeftToRight)), - Operator::Add => Ok(BinaryOperator(value, OperatorPrecedence::Additive, BinaryAssociativity::LeftToRight)), - Operator::Subtract => Ok(BinaryOperator(value, OperatorPrecedence::Additive, BinaryAssociativity::LeftToRight)), - Operator::ShiftLeft => Ok(BinaryOperator(value, OperatorPrecedence::Shift, BinaryAssociativity::LeftToRight)), - Operator::ShiftRight => Ok(BinaryOperator(value, OperatorPrecedence::Shift, BinaryAssociativity::LeftToRight)), - Operator::ShiftRightUnsigned => Ok(BinaryOperator(value, OperatorPrecedence::Shift, BinaryAssociativity::LeftToRight)), - Operator::Lt => Ok(BinaryOperator(value, OperatorPrecedence::Relational, BinaryAssociativity::LeftToRight)), - Operator::Gt => Ok(BinaryOperator(value, OperatorPrecedence::Relational, BinaryAssociativity::LeftToRight)), - Operator::Le => Ok(BinaryOperator(value, OperatorPrecedence::Relational, BinaryAssociativity::LeftToRight)), - Operator::Ge => Ok(BinaryOperator(value, OperatorPrecedence::Relational, BinaryAssociativity::LeftToRight)), - Operator::As => Ok(BinaryOperator(value, OperatorPrecedence::Relational, BinaryAssociativity::LeftToRight)), - Operator::In => Ok(BinaryOperator(value, OperatorPrecedence::Relational, BinaryAssociativity::LeftToRight)), - Operator::NotIn => Ok(BinaryOperator(value, OperatorPrecedence::Relational, BinaryAssociativity::LeftToRight)), - Operator::Instanceof => Ok(BinaryOperator(value, OperatorPrecedence::Relational, BinaryAssociativity::LeftToRight)), - Operator::Is => Ok(BinaryOperator(value, OperatorPrecedence::Relational, BinaryAssociativity::LeftToRight)), - Operator::IsNot => Ok(BinaryOperator(value, OperatorPrecedence::Relational, BinaryAssociativity::LeftToRight)), - Operator::Equals => Ok(BinaryOperator(value, OperatorPrecedence::Equality, BinaryAssociativity::LeftToRight)), - Operator::NotEquals => Ok(BinaryOperator(value, OperatorPrecedence::Equality, BinaryAssociativity::LeftToRight)), - Operator::StrictEquals => Ok(BinaryOperator(value, OperatorPrecedence::Equality, BinaryAssociativity::LeftToRight)), - Operator::StrictNotEquals => Ok(BinaryOperator(value, OperatorPrecedence::Equality, BinaryAssociativity::LeftToRight)), - Operator::BitwiseAnd => Ok(BinaryOperator(value, OperatorPrecedence::BitwiseAnd, BinaryAssociativity::LeftToRight)), - Operator::BitwiseXor => Ok(BinaryOperator(value, OperatorPrecedence::BitwiseXor, BinaryAssociativity::LeftToRight)), - Operator::BitwiseOr => Ok(BinaryOperator(value, OperatorPrecedence::BitwiseOr, BinaryAssociativity::LeftToRight)), - Operator::LogicalAnd => Ok(BinaryOperator(value, OperatorPrecedence::LogicalAnd, BinaryAssociativity::LeftToRight)), - Operator::LogicalXor => Ok(BinaryOperator(value, OperatorPrecedence::LogicalXor, BinaryAssociativity::LeftToRight)), - Operator::LogicalOr => Ok(BinaryOperator(value, OperatorPrecedence::LogicalOrAndOther, BinaryAssociativity::LeftToRight)), - Operator::NullCoalescing => Ok(BinaryOperator(value, OperatorPrecedence::LogicalOrAndOther, BinaryAssociativity::LeftToRight)), - - Operator::Power => Ok(BinaryOperator(value, OperatorPrecedence::Exponentiation, BinaryAssociativity::RightToLeft)), - - _ => Err(()), - } - } -} \ No newline at end of file diff --git a/crates/parser/operator/operator_precedence.rs b/crates/parser/operator/operator_precedence.rs deleted file mode 100644 index 68b0abe..0000000 --- a/crates/parser/operator/operator_precedence.rs +++ /dev/null @@ -1,77 +0,0 @@ -use num_derive::FromPrimitive; -use num_traits::FromPrimitive; - -#[derive(FromPrimitive)] -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] -#[repr(u32)] -pub enum OperatorPrecedence { - Postfix = 16, - Unary = 15, - Exponentiation = 14, - Multiplicative = 13, - Additive = 12, - Shift = 11, - Relational = 10, - Equality = 9, - BitwiseAnd = 8, - BitwiseXor = 7, - BitwiseOr = 6, - LogicalAnd = 5, - LogicalXor = 4, - /// Includes logical OR and nullish coalescing (`??`). - LogicalOrAndOther = 3, - /// Includes assignment operators, conditional operator, function expression and `yield` operator. - AssignmentAndOther = 2, - List = 1, -} - -impl OperatorPrecedence { - pub fn add(&self, value: u32) -> Option { - FromPrimitive::from_u32(*self as u32 + value) - } - - pub fn value_of(&self) -> u32 { - *self as u32 - } - - pub fn includes(&self, other: &Self) -> bool { - *self <= *other - } -} - -impl TryFrom for OperatorPrecedence { - type Error = (); - fn try_from(value: u32) -> Result { - if let Some(v) = FromPrimitive::from_u32(value as u32) { Ok(v) } else { Err(()) } - } -} - -#[derive(FromPrimitive)] -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] -#[repr(u32)] -pub enum CssOperatorPrecedence { - Unary = 3, - MultiValue = 2, - Array = 1, -} - -impl CssOperatorPrecedence { - pub fn add(&self, value: u32) -> Option { - FromPrimitive::from_u32(*self as u32 + value) - } - - pub fn value_of(&self) -> u32 { - *self as u32 - } - - pub fn includes(&self, other: &Self) -> bool { - *self <= *other - } -} - -impl TryFrom for CssOperatorPrecedence { - type Error = (); - fn try_from(value: u32) -> Result { - if let Some(v) = FromPrimitive::from_u32(value as u32) { Ok(v) } else { Err(()) } - } -} \ No newline at end of file diff --git a/crates/parser/parser.rs b/crates/parser/parser.rs deleted file mode 100644 index 897a3e2..0000000 --- a/crates/parser/parser.rs +++ /dev/null @@ -1,23 +0,0 @@ -//! Defines the parser and the tokenizer. -//! -//! Using the methods of the `ParserFacade` structure is the most common way of parsing -//! programs until end-of-file. - -mod character_validator; -pub use character_validator::*; -mod context; -pub use context::*; -mod reserved_word; -pub use reserved_word::*; -mod parser; -pub use parser::*; -mod css_parser; -pub use css_parser::*; -mod css_tokenizer; -pub use css_tokenizer::*; -mod parser_error; -pub use parser_error::*; -mod token; -pub use token::*; -mod tokenizer; -pub use tokenizer::*; \ No newline at end of file diff --git a/crates/parser/parser/character_validator.rs b/crates/parser/parser/character_validator.rs deleted file mode 100644 index ef06b61..0000000 --- a/crates/parser/parser/character_validator.rs +++ /dev/null @@ -1,147 +0,0 @@ -//use lazy_regex::{Lazy, Regex, lazy_regex}; -use unicode_general_category::{get_general_category, GeneralCategory}; - -// pub(crate) static CR_OR_CRLF_REGEX: Lazy = lazy_regex!(r"\r\n?"); - -/// The `CharacterValidator` structure defines static methods for character -/// validation. -pub struct CharacterValidator; - -impl CharacterValidator { - /// Returns the count of indentation characters in a string. - pub fn indent_count(string: &str) -> usize { - let mut n: usize = 0; - for ch in string.chars() { - if !CharacterValidator::is_whitespace(ch) { - break; - } - n += 1; - } - n - } - - pub fn is_whitespace(ch: char) -> bool { - if ch == '\x20' || ch == '\x09' || ch == '\x08' - || ch == '\x0C' || ch == '\u{A0}' { - return true; - } - let category = get_general_category(ch); - category == GeneralCategory::SpaceSeparator - } - - pub fn is_line_terminator(ch: char) -> bool { - ch == '\x0A' || ch == '\x0D' || ch == '\u{2028}' || ch == '\u{2029}' - } - - pub fn is_bin_digit(ch: char) -> bool { - ch == '\x30' || ch == '\x31' - } - - pub fn is_dec_digit(ch: char) -> bool { - ch >= '\x30' && ch <= '\x39' - } - - pub fn is_hex_digit(ch: char) -> bool { - CharacterValidator::is_dec_digit(ch) || (ch >= '\x41' && ch <= '\x46') || (ch >= '\x61' && ch <= '\x66') - } - - /// Returns the mathematical value of a hexadecimal digit. - pub fn hex_digit_mv(ch: char) -> Option { - if ch >= 'A' && ch <= 'F' { - Some((ch as u32) - 0x41 + 10) - } else if ch >= 'a' && ch <= 'f' { - Some((ch as u32) - 0x61 + 10) - } else if ch >= '0' && ch <= '9' { - Some((ch as u32) - 0x30) - } else { - None - } - } - - /// Returns the mathematical value of a binary digit. - pub fn bin_digit_mv(ch: char) -> Option { - if ch >= '0' && ch <= '1' { - Some((ch as u32) - 0x30) - } else { - None - } - } - - pub fn is_css_identifier_start(ch: char) -> bool { - (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') - } - - pub fn is_css_identifier_part(ch: char) -> bool { - (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || - (ch >= '0' && ch <= '9') || ch == '_' || ch == '-' - } - - pub fn is_identifier_start(ch: char) -> bool { - if ch == '\x5f' || ch == '\x24' { - return true; - } - let category = get_general_category(ch); - [ - GeneralCategory::LowercaseLetter, - GeneralCategory::UppercaseLetter, - GeneralCategory::ModifierLetter, - GeneralCategory::OtherLetter, - GeneralCategory::TitlecaseLetter, - GeneralCategory::LetterNumber, - ].contains(&category) - } - - pub fn is_identifier_part(ch: char) -> bool { - if ch == '\x5f' || ch == '\x24' { - return true; - } - let category = get_general_category(ch); - [ - GeneralCategory::LowercaseLetter, - GeneralCategory::UppercaseLetter, - GeneralCategory::ModifierLetter, - GeneralCategory::OtherLetter, - GeneralCategory::TitlecaseLetter, - GeneralCategory::LetterNumber, - GeneralCategory::NonspacingMark, - GeneralCategory::SpacingMark, - GeneralCategory::ConnectorPunctuation, - GeneralCategory::DecimalNumber, - ].contains(&category) - } - - pub fn is_xml_name_start(ch: char) -> bool { - if ch == '\x5f' || ch == ':' { - return true; - } - let category = get_general_category(ch); - [ - GeneralCategory::LowercaseLetter, - GeneralCategory::UppercaseLetter, - GeneralCategory::ModifierLetter, - GeneralCategory::OtherLetter, - GeneralCategory::TitlecaseLetter, - GeneralCategory::LetterNumber, - ].contains(&category) - } - - pub fn is_xml_name_part(ch: char) -> bool { - if ch == '\x5f' || ch == ':' || ch == '.' || ch == '-' { - return true; - } - let category = get_general_category(ch); - [ - GeneralCategory::LowercaseLetter, - GeneralCategory::UppercaseLetter, - GeneralCategory::ModifierLetter, - GeneralCategory::OtherLetter, - GeneralCategory::TitlecaseLetter, - GeneralCategory::LetterNumber, - GeneralCategory::DecimalNumber, - ].contains(&category) - } - - pub fn is_xml_whitespace(ch: char) -> bool { - ch == '\x20' || ch == '\x09' || ch == '\x0A' || ch == '\x0D' - } -} \ No newline at end of file diff --git a/crates/parser/parser/context.rs b/crates/parser/parser/context.rs deleted file mode 100644 index 3132d5b..0000000 --- a/crates/parser/parser/context.rs +++ /dev/null @@ -1,174 +0,0 @@ -use crate::ns::*; - -/// Context used to control the parsing of an expression. -#[derive(Clone)] -pub struct ParserExpressionContext { - pub min_precedence: OperatorPrecedence, - pub allow_in: bool, - pub allow_assignment: bool, -} - -impl Default for ParserExpressionContext { - fn default() -> Self { - Self { - min_precedence: OperatorPrecedence::List, - allow_in: true, - allow_assignment: true, - } - } -} - -#[derive(Clone)] -pub enum ParserDirectiveContext { - Default, - TopLevel, - PackageBlock, - ClassBlock { - name: String, - }, - InterfaceBlock, - EnumBlock, - ConstructorBlock { - super_statement_found: Rc>, - }, - WithControl { - super_statement_found: Option>>, - to_be_labeled: Option, - control_context: ParserControlFlowContext, - labels: HashMap, - }, -} - -impl ParserDirectiveContext { - pub fn may_contain_super_statement(&self) -> bool { - matches!(self, Self::ConstructorBlock { .. }) || matches!(self, Self::WithControl { .. }) - } - - pub fn super_statement_found(&self) -> bool { - match self { - Self::ConstructorBlock { super_statement_found } => super_statement_found.get(), - Self::WithControl { super_statement_found, .. } => super_statement_found.as_ref().or(Some(&Rc::new(Cell::new(false)))).unwrap().get(), - _ => false, - } - } - - pub fn set_super_statement_found(&self, value: bool) { - match self { - Self::ConstructorBlock { super_statement_found } => { super_statement_found.set(value) }, - Self::WithControl { super_statement_found, .. } => { - if let Some(found) = super_statement_found.as_ref() { - found.set(value); - } - }, - _ => {}, - } - } - - pub fn function_name_is_constructor(&self, name: &(String, Location)) -> bool { - if let ParserDirectiveContext::ClassBlock { name: ref name_1 } = self { - &name.0 == name_1 - } else { - false - } - } - - pub fn is_top_level_or_package(&self) -> bool { - matches!(self, ParserDirectiveContext::TopLevel) || matches!(self, ParserDirectiveContext::PackageBlock) - } - - pub fn is_type_block(&self) -> bool { - match self { - Self::ClassBlock { .. } | - Self::InterfaceBlock | - Self::EnumBlock => true, - _ => false, - } - } - - pub fn clone_control(&self) -> Self { - match self { - Self::WithControl { .. } => self.clone(), - _ => Self::Default, - } - } - - pub fn override_control_context(&self, label_only: bool, mut context: ParserControlFlowContext) -> Self { - let mut prev_context = None; - let mut label = None; - let mut super_statement_found: Option>> = None; - let mut labels = match self { - Self::WithControl { control_context, labels, to_be_labeled: label1, super_statement_found: super_found_1 } => { - prev_context = Some(control_context.clone()); - label = label1.clone(); - super_statement_found = super_found_1.clone(); - labels.clone() - }, - _ => HashMap::new(), - }; - if let Some(label) = label.clone() { - labels.insert(label, context.clone()); - } - if label_only { - context = prev_context.unwrap_or(ParserControlFlowContext { - breakable: false, - iteration: false, - }); - } - Self::WithControl { control_context: context, labels, to_be_labeled: None, super_statement_found } - } - - pub fn put_label(&self, label: String) -> Self { - match self { - Self::WithControl { control_context, labels, to_be_labeled: _, super_statement_found } => Self::WithControl { - to_be_labeled: Some(label), - control_context: control_context.clone(), - labels: labels.clone(), - super_statement_found: super_statement_found.clone(), - }, - _ => Self::WithControl { - to_be_labeled: Some(label), - control_context: ParserControlFlowContext { - breakable: false, - iteration: false, - }, - labels: HashMap::new(), - super_statement_found: match self { - Self::ConstructorBlock { super_statement_found } => Some(super_statement_found.clone()), - _ => None, - }, - }, - } - } - - pub fn is_label_defined(&self, label: String) -> bool { - self.resolve_label(label).is_some() - } - - pub fn resolve_label(&self, label: String) -> Option { - if let Self::WithControl { labels, .. } = &self { labels.get(&label).map(|c| c.clone()) } else { None } - } - - pub fn is_break_allowed(&self, label: Option) -> bool { - if let Some(label) = label { - let context = self.resolve_label(label); - if let Some(context) = context { context.breakable } else { false } - } else { - if let Self::WithControl { control_context, .. } = &self { control_context.breakable } else { false } - } - } - - pub fn is_continue_allowed(&self, label: Option) -> bool { - if let Some(label) = label { - let context = self.resolve_label(label); - if let Some(context) = context { context.iteration } else { false } - } else { - if let Self::WithControl { control_context, .. } = &self { control_context.iteration } else { false } - } - } -} - -#[derive(Clone)] -pub struct ParserControlFlowContext { - pub breakable: bool, - pub iteration: bool, -} \ No newline at end of file diff --git a/crates/parser/parser/css_parser.rs b/crates/parser/parser/css_parser.rs deleted file mode 100644 index c7f64a1..0000000 --- a/crates/parser/parser/css_parser.rs +++ /dev/null @@ -1,847 +0,0 @@ -use crate::ns::*; -use num_traits::ToPrimitive; - -pub struct CssParser<'input> { - tokenizer: CssTokenizer<'input>, - previous_token: (Token, Location), - token: (Token, Location), - locations: Vec, - expecting_token_error: bool, -} - -impl<'input> CssParser<'input> { - /// Constructs a tokenizer. - pub fn new(compilation_unit: &'input Rc, options: &ParserOptions) -> Self { - Self { - tokenizer: CssTokenizer::new(compilation_unit, options), - previous_token: (Token::Eof, Location::with_offset(&compilation_unit, 0)), - token: (Token::Eof, Location::with_offset(&compilation_unit, 0)), - locations: vec![], - expecting_token_error: false, - } - } - - fn options(&self) -> ParserOptions { - ParserOptions { - ..default() - } - } - - fn compilation_unit(&self) -> &Rc { - self.tokenizer.compilation_unit() - } - - fn token_location(&self) -> Location { - self.token.1.clone() - } - - fn mark_location(&mut self) { - self.locations.push(self.token.1.clone()); - } - - fn duplicate_location(&mut self) { - self.locations.push(self.locations.last().unwrap().clone()); - } - - fn push_location(&mut self, location: &Location) { - self.locations.push(location.clone()); - } - - fn pop_location(&mut self) -> Location { - self.locations.pop().unwrap().combine_with(self.previous_token.1.clone()) - } - - fn add_syntax_error(&self, location: &Location, kind: DiagnosticKind, arguments: Vec>) { - if self.compilation_unit().prevent_equal_offset_error(location) { - return; - } - self.compilation_unit().add_diagnostic(Diagnostic::new_syntax_error(location, kind, arguments)); - } - - /* - fn add_warning(&self, location: &Location, kind: DiagnosticKind, arguments: Vec>) { - if self.compilation_unit().prevent_equal_offset_warning(location) { - return; - } - self.compilation_unit().add_diagnostic(Diagnostic::new_warning(location, kind, arguments)); - } - */ - - fn next(&mut self) { - self.previous_token = self.token.clone(); - self.token = self.tokenizer.scan(); - } - - fn peek(&self, token: Token) -> bool { - self.token.0 == token - } - - fn peek_identifier(&self) -> Option<(String, Location)> { - if let Token::Identifier(id) = self.token.0.clone() { - let location = self.token.1.clone(); - Some((id, location)) - } else { - None - } - } - - fn peek_keyword(&self, name: &str) -> bool { - if let Token::Identifier(id) = self.token.0.clone() { id == name && self.token.1.character_count() == name.len() } else { false } - } - - fn consume(&mut self, token: Token) -> bool { - if self.token.0 == token { - self.next(); - true - } else { - false - } - } - - fn consume_identifier(&mut self) -> Option<(String, Location)> { - if let Token::Identifier(id) = self.token.0.clone() { - let location = self.token.1.clone(); - self.next(); - Some((id, location)) - } else { - None - } - } - - fn consume_keyword(&mut self, name: &str) -> bool { - if let Token::Identifier(name1) = self.token.0.clone() { - if name1 == name { - self.next(); - return true; - } - } - false - } - - /// Expects a token in non-greedy mode: if it fails, does not skip any token. - fn expect(&mut self, token: Token) { - if self.token.0 != token { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]); - } else { - self.expecting_token_error = false; - self.next(); - } - } - - fn expect_identifier(&mut self) -> (String, Location) { - if let Token::Identifier(id) = self.token.0.clone() { - self.expecting_token_error = false; - let location = self.token.1.clone(); - self.next(); - (id, location) - } else { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![self.token.0.clone()]); - (INVALIDATED_IDENTIFIER.to_owned(), self.tokenizer.cursor_location()) - } - } - - fn expect_unitless_number(&mut self) -> Option { - if let Token::CssNumber { value, .. } = self.token.0.clone() { - self.expecting_token_error = false; - self.next(); - Some(value) - } else { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]); - None - } - } - - fn expect_string(&mut self) -> (String, Location) { - if let Token::String(v) = self.token.0.clone() { - self.expecting_token_error = false; - let location = self.token.1.clone(); - self.next(); - (v, location) - } else { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingStringLiteral, diagarg![self.token.0.clone()]); - ("".into(), self.tokenizer.cursor_location()) - } - } - - pub fn expect_eof(&mut self) { - self.expect(Token::Eof); - } - - fn create_invalidated_directive(&self, location: &Location) -> Rc { - Rc::new(CssDirective::Invalidated(InvalidatedNode { - location: location.clone(), - })) - } - - fn create_invalidated_property_value(&self, location: &Location) -> Rc { - Rc::new(CssPropertyValue::Invalidated(InvalidatedNode { - location: location.clone(), - })) - } - - fn _create_invalidated_selector(&self, location: &Location) -> Rc { - Rc::new(CssSelector::Invalidated(InvalidatedNode { - location: location.clone(), - })) - } - - fn create_invalidated_selector_condition(&self, location: &Location) -> Rc { - Rc::new(CssSelectorCondition::Invalidated(InvalidatedNode { - location: location.clone(), - })) - } - - fn create_invalidated_media_query_condition(&self, location: &Location) -> Rc { - Rc::new(CssMediaQueryCondition::Invalidated(InvalidatedNode { - location: location.clone(), - })) - } - - fn eof(&self) -> bool { - matches!(self.token.0, Token::Eof) - } - - pub fn parse_document(&mut self) -> Rc { - self.mark_location(); - let just_eof = self.peek(Token::Eof); - let mut directives: Vec> = vec![]; - while !self.eof() { - directives.push(self.parse_directive()); - } - let loc = self.pop_location(); - Rc::new(CssDocument { - location: if just_eof { - self.token.1.clone() - } else { - loc - }, - directives, - }) - } - - fn parse_directive(&mut self) -> Rc { - if let Some(rule) = self.parse_opt_rule() { - Rc::new(CssDirective::Rule(rule)) - } else if self.peek(Token::CssAtNamespace) { - self.mark_location(); - self.next(); - let prefix = self.expect_identifier(); - let uri = if self.expecting_token_error { - (String::new(), self.tokenizer.cursor_location()) - } else { - self.expect_string() - }; - if !self.expecting_token_error { - self.expect(Token::CssSemicolons); - } - let loc = self.pop_location(); - Rc::new(CssDirective::NamespaceDefinition(CssNamespaceDefinition { - location: loc, - prefix, - uri, - })) - } else if self.peek(Token::CssAtMedia) { - self.parse_media_query() - } else if self.peek(Token::CssAtFontFace) { - self.parse_font_face() - } else { - self.add_syntax_error(&self.token.1, DiagnosticKind::ExpectingDirective, diagarg![self.token.0.clone()]); - let d = self.create_invalidated_directive(&self.tokenizer.cursor_location()); - self.next(); - d - } - } - - fn parse_media_query(&mut self) -> Rc { - self.mark_location(); - self.next(); - let mut conditions: Vec> = vec![]; - let condition = self.parse_opt_media_query_condition(); - if let Some(condition) = condition { - conditions.push(condition); - } else { - self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]); - } - loop { - if let Some(condition) = self.parse_opt_media_query_condition() { - conditions.push(condition); - } else if self.eof() || self.peek(Token::BlockOpen) { - break; - } else if !self.consume(Token::Comma) { - self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]); - self.next(); - } - } - let mut rules: Vec> = vec![]; - self.expect(Token::BlockOpen); - if !self.expecting_token_error { - while !(self.eof() || self.peek(Token::BlockClose)) { - if let Some(rule) = self.parse_opt_rule() { - rules.push(Rc::new(rule)); - } else { - self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]); - self.next(); - } - } - self.expect(Token::BlockClose); - } - Rc::new(CssDirective::MediaQuery(CssMediaQuery { - location: self.pop_location(), - conditions, - rules, - })) - } - - fn parse_font_face(&mut self) -> Rc { - self.mark_location(); - self.next(); - let mut properties: Vec> = vec![]; - self.expect(Token::BlockOpen); - if !self.expecting_token_error { - self.consume(Token::CssSemicolons); - while !(self.eof() || self.peek(Token::BlockClose)) { - properties.push(self.parse_property()); - if !self.consume(Token::CssSemicolons) { - break; - } - } - self.expect(Token::BlockClose); - } - Rc::new(CssDirective::FontFace(CssFontFace { - location: self.pop_location(), - properties, - })) - } - - fn parse_opt_media_query_condition(&mut self) -> Option> { - let mut base: Option> = None; - if self.peek_keyword("only") { - self.mark_location(); - self.next(); - let id = self.expect_identifier(); - base = Some(Rc::new(CssMediaQueryCondition::OnlyId { - location: self.pop_location(), - id, - })); - } - if let Some(id) = self.consume_identifier() { - base = Some(Rc::new(CssMediaQueryCondition::Id(id))); - } - if self.peek(Token::ParenOpen) { - self.mark_location(); - let property = self.parse_arguments().unwrap().parse_property(); - let loc = self.pop_location(); - base = Some(Rc::new(CssMediaQueryCondition::ParenProperty((property, loc)))); - } - if let Some(mut base) = base.clone() { - while self.consume_keyword("and") { - self.push_location(&base.location()); - if let Some(right) = self.parse_opt_media_query_condition() { - base = Rc::new(CssMediaQueryCondition::And { - location: self.pop_location(), - left: base, - right, - }); - } else { - self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]); - base = Rc::new(CssMediaQueryCondition::And { - location: self.pop_location(), - left: base, - right: self.create_invalidated_media_query_condition(&self.tokenizer.cursor_location()), - }); - } - } - return Some(base); - } - base - } - - fn parse_arguments(&mut self) -> Result { - if !self.peek(Token::ParenOpen) { - self.add_syntax_error(&self.token.1, DiagnosticKind::Expecting, diagarg![Token::ParenOpen, self.token.0.clone()]); - return Err(ParserError::Common); - } - let (byte_range, token) = self.tokenizer.scan_arguments(); - self.previous_token = self.token.clone(); - self.token = token; - self.next(); - Ok(CssParserFacade(self.compilation_unit(), ParserOptions { - byte_range: Some(byte_range), - ..self.options() - })) - } - - fn parse_opt_rule(&mut self) -> Option { - let mut selectors: Vec> = vec![self.parse_opt_selector()?]; - while self.consume(Token::Comma) { - if let Some(s) = self.parse_opt_selector() { - selectors.push(s); - } else { - self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]); - } - } - let mut properties: Vec> = vec![]; - self.expect(Token::BlockOpen); - if !self.expecting_token_error { - self.consume(Token::CssSemicolons); - while !(self.eof() || self.peek(Token::BlockClose)) { - properties.push(self.parse_property()); - if !self.consume(Token::CssSemicolons) { - break; - } - } - self.expect(Token::BlockClose); - } - self.push_location(&selectors[0].location()); - Some(CssRule { - location: self.pop_location(), - selectors, - properties, - }) - } - - fn parse_opt_selector(&mut self) -> Option> { - let mut base = self.parse_opt_base_selector()?; - - // Parse descendant combinators - while let Some(right) = self.parse_opt_base_selector() { - self.push_location(&base.location()); - base = Rc::new(CssSelector::Combinator(CssCombinatorSelector { - location: self.pop_location(), - left: base, - right, - combinator_type: CssCombinatorType::Descendant, - })); - } - - Some(base) - } - - fn parse_opt_base_selector(&mut self) -> Option> { - self.mark_location(); - let mut namespace_prefix: Option<(String, Location)> = None; - let mut element_name: Option<(String, Location)> = self.consume_identifier(); - let mut conditions: Vec> = vec![]; - if self.consume(Token::Pipe) { - namespace_prefix = element_name.clone(); - element_name = Some(self.expect_identifier()); - } - // Parse conditions as long as they are not separated by whitespace - while (element_name.is_none() && conditions.is_empty()) || (self.token.1.first_offset() - self.previous_token.1.last_offset() == 0) { - if let Some(condition) = self.parse_opt_selector_condition() { - conditions.push(condition); - } else { - break; - } - } - if element_name.is_none() && conditions.is_empty() { - self.pop_location(); - return None; - } - Some(Rc::new(CssSelector::Base(CssBaseSelector { - location: self.pop_location(), - namespace_prefix, - element_name, - conditions, - }))) - } - - fn parse_selector_condition(&mut self) -> Rc { - let Some(c) = self.parse_opt_selector_condition() else { - self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]); - return self.create_invalidated_selector_condition(&self.tokenizer.cursor_location()); - }; - c - } - - fn parse_opt_selector_condition(&mut self) -> Option> { - if self.peek(Token::Dot) { - self.mark_location(); - self.next(); - let class_name = self.expect_identifier().0; - return Some(Rc::new(CssSelectorCondition::Class((class_name, self.pop_location())))); - } - if let Token::CssHashWord(id_value) = self.token.0.clone() { - let loc = self.token.1.clone(); - self.next(); - return Some(Rc::new(CssSelectorCondition::Id((id_value, loc)))); - } - if self.peek(Token::Colon) { - self.mark_location(); - self.next(); - if self.consume_keyword("not") { - let condition = if let Ok(a) = self.parse_arguments() { - a.parse_selector_condition() - } else { - self.duplicate_location(); - let loc = self.pop_location(); - self.create_invalidated_selector_condition(&loc) - }; - return Some(Rc::new(CssSelectorCondition::Not { - location: self.pop_location(), - condition, - })); - } else { - let name = self.expect_identifier().0; - return Some(Rc::new(CssSelectorCondition::Pseudo((name, self.pop_location())))); - } - } - if self.peek(Token::ColonColon) { - self.mark_location(); - self.next(); - let name = self.expect_identifier().0; - return Some(Rc::new(CssSelectorCondition::PseudoElement((name, self.pop_location())))); - } - if self.peek(Token::SquareOpen) { - self.mark_location(); - self.next(); - let name = self.expect_identifier(); - let mut operator: Option = None; - let mut value: Option<(String, Location)> = None; - while let Some(t1) = self.consume_attribute_operator() { - operator = Some(t1); - } - while let Token::String(s1) = self.token.0.clone() { - value = Some((s1, self.token.1.clone())); - self.next(); - } - self.expect(Token::SquareClose); - return Some(Rc::new(CssSelectorCondition::Attribute { - location: self.pop_location(), - name, - operator, - value, - })); - } - None - } - - fn consume_attribute_operator(&mut self) -> Option { - if self.consume(Token::CssBeginsWith) { - Some(CssAttributeOperator::BeginsWith) - } else if self.consume(Token::CssEndsWith) { - Some(CssAttributeOperator::EndsWith) - } else if self.consume(Token::CssContains) { - Some(CssAttributeOperator::Contains) - } else if self.consume(Token::CssListMatch) { - Some(CssAttributeOperator::ListMatch) - } else if self.consume(Token::CssHreflangMatch) { - Some(CssAttributeOperator::HreflangMatch) - } else if self.consume(Token::Assign) { - Some(CssAttributeOperator::Equals) - } else { - None - } - } - - fn parse_property(&mut self) -> Rc { - self.mark_location(); - let name = self.expect_identifier(); - let mut value = self.create_invalidated_property_value(&self.tokenizer.cursor_location()); - if !self.expecting_token_error { - self.expect(Token::Colon); - if !self.expecting_token_error { - value = self.parse_property_value(CssOperatorPrecedence::Array); - } - } - Rc::new(CssProperty::new(self.pop_location(), name, value)) - } - - fn parse_property_value(&mut self, min_precedence: CssOperatorPrecedence) -> Rc { - let Some(v) = self.parse_opt_property_value(min_precedence) else { - self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]); - return self.create_invalidated_property_value(&self.tokenizer.cursor_location()); - }; - v - } - - fn parse_opt_property_value(&mut self, min_precedence: CssOperatorPrecedence) -> Option> { - let base: Option>; - let t1 = self.token.0.clone(); - - // #HHH - // #HHHHHH - if let Token::CssHashWord(word) = t1 { - self.mark_location(); - self.next(); - let loc = self.pop_location(); - if let Ok(v) = CssColorPropertyValue::from_hex(loc.clone(), &word) { - base = Some(Rc::new(CssPropertyValue::Color(v))); - } else { - base = Some(self.create_invalidated_property_value(&loc)); - } - // "..." - // '...' - } else if let Token::String(value) = t1 { - self.mark_location(); - self.next(); - base = Some(Rc::new(CssPropertyValue::String(CssStringPropertyValue { - location: self.pop_location(), - value - }))); - // DECIMAL - } else if let Token::CssNumber { value, unit } = t1 { - self.mark_location(); - self.next(); - let loc = self.pop_location(); - base = Some(Rc::new(CssPropertyValue::Number(CssNumberPropertyValue { - location: loc, - value, - unit, - }))); - } else if let Some(id) = self.peek_identifier() { - self.mark_location(); - self.next(); - let color_int = css_color_constant_to_int(&id.0); - // COLOR_NAME such as "red" - if let Some(color_int) = color_int { - base = Some(Rc::new(CssPropertyValue::Color(CssColorPropertyValue { - location: self.pop_location(), - color_int, - }))); - // rgb(...) - } else if id.0 == "rgb" && self.peek(Token::ParenOpen) { - if let Some(color_int) = self.parse_arguments().unwrap().parse_rgb() { - base = Some(Rc::new(CssPropertyValue::RgbColor(CssRgbColorPropertyValue { - location: self.pop_location(), - color_int, - }))); - } else { - let loc = self.pop_location(); - base = Some(self.create_invalidated_property_value(&loc)); - } - } else if id.0 == "ClassReference" && self.peek(Token::ParenOpen) { - let name = self.parse_arguments().unwrap().parse_text(); - base = Some(Rc::new(CssPropertyValue::ClassReference(CssClassReferencePropertyValue { - location: self.pop_location(), - name, - }))); - } else if id.0 == "PropertyReference" && self.peek(Token::ParenOpen) { - let name = self.parse_arguments().unwrap().parse_text(); - base = Some(Rc::new(CssPropertyValue::PropertyReference(CssPropertyReferencePropertyValue { - location: self.pop_location(), - name, - }))); - } else if id.0 == "url" && self.peek(Token::ParenOpen) { - let url = self.parse_arguments().unwrap().parse_text(); - let mut format: Option<(String, Location)> = None; - if self.consume_keyword("format") { - if let Ok(a) = self.parse_arguments() { - format = Some(a.parse_text()); - } - } - base = Some(Rc::new(CssPropertyValue::Url(CssUrlPropertyValue { - location: self.pop_location(), - url, - format, - }))); - } else if id.0 == "local" && self.peek(Token::ParenOpen) { - let name = self.parse_arguments().unwrap().parse_text(); - base = Some(Rc::new(CssPropertyValue::Local(CssLocalPropertyValue { - location: self.pop_location(), - name, - }))); - } else if id.0 == "Embed" && self.peek(Token::ParenOpen) { - let entries = self.parse_arguments().unwrap().parse_embed_entries(); - base = Some(Rc::new(CssPropertyValue::Embed(CssEmbedPropertyValue { - location: self.pop_location(), - entries, - }))); - } else { - if self.peek(Token::ParenOpen) { - self.add_syntax_error(&self.token_location(), DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]); - self.parse_arguments().unwrap(); - } - base = Some(Rc::new(CssPropertyValue::Identifier(CssIdentifierPropertyValue { - location: self.pop_location(), - value: id.0, - }))); - } - } else if self.peek(Token::Plus) || self.peek(Token::Minus) - || self.peek(Token::Times) || self.peek(Token::Div) { - base = Some(self.create_invalidated_property_value(&self.token.1)); - self.next(); - } else { - return None; - } - - let mut base = base.unwrap(); - - loop { - if self.peek(Token::Comma) && min_precedence.includes(&CssOperatorPrecedence::Array) { - self.push_location(&base.location()); - let mut elements: Vec> = vec![base]; - while self.consume(Token::Comma) { - elements.push(self.parse_property_value(CssOperatorPrecedence::MultiValue)); - } - base = Rc::new(CssPropertyValue::Array(CssArrayPropertyValue { - location: self.pop_location(), - elements, - })); - } else if min_precedence.includes(&CssOperatorPrecedence::MultiValue) { - if let Some(mv1) = self.parse_opt_property_value(CssOperatorPrecedence::MultiValue.add(1).unwrap()) { - self.push_location(&base.location()); - let mut values: Vec> = vec![base, mv1]; - while let Some(mv1) = self.parse_opt_property_value(CssOperatorPrecedence::MultiValue.add(1).unwrap()) { - values.push(mv1); - } - base = Rc::new(CssPropertyValue::MultiValue(CssMultiValuePropertyValue { - location: self.pop_location(), - values, - })); - } else { - break; - } - } else { - break; - } - } - - Some(base) - } - - fn parse_embed_entry(&mut self) -> Rc { - self.mark_location(); - if let Some(key) = self.consume_identifier() { - if self.consume(Token::Assign) { - let value = self.expect_string(); - Rc::new(CssEmbedEntry { - location: self.pop_location(), - key: Some(key), - value, - }) - } else { - Rc::new(CssEmbedEntry { - location: self.pop_location(), - key: None, - value: key, - }) - } - } else { - let value = self.expect_string(); - Rc::new(CssEmbedEntry { - location: self.pop_location(), - key: None, - value, - }) - } - } -} - -fn rgb_bytes_to_integer(r: f64, g: f64, b: f64) -> u32 { - (calc_rgb_byte(r) << 16) | (calc_rgb_byte(g) << 8) | calc_rgb_byte(b) -} - -fn calc_rgb_byte(value: f64) -> u32 { - // Integer - if value.round() == value { - value.round().to_u32().unwrap_or(0).clamp(0, 255) - // Float - } else { - (value * 255.0).round().to_u32().unwrap_or(0).clamp(0, 255) - } -} - -/// A simplified interface for executing the CSS parser. -pub struct CssParserFacade<'input>(pub &'input Rc, pub ParserOptions); - -impl<'input> CssParserFacade<'input> { - fn create_parser(&self) -> CssParser<'input> { - CssParser::new(self.0, &self.1) - } - - /// Parses `CssDocument` until end-of-file. - pub fn parse_document(&self) -> Rc { - let mut parser = self.create_parser(); - parser.next(); - parser.parse_document() - } - - /// Parses either a string or return source text as is. - pub fn parse_text(&self) -> (String, Location) { - let mut parser = self.create_parser(); - while parser.tokenizer.consume_whitespace() { - // Consumed whitespace - } - let d = parser.tokenizer.characters().peek_or_zero(); - if ['"', '\''].contains(&d) { - parser.next(); - let mut v: (String, Location) = ("".into(), parser.tokenizer.cursor_location()); - while let Token::String(v1) = parser.token.0.clone() { - v = (v1, parser.token.1.clone()); - parser.next(); - } - parser.expect_eof(); - v - } else { - let mut s = String::new(); - let i = parser.tokenizer.characters().index(); - while let Some(ch) = parser.tokenizer.characters_mut().next() { - s.push(ch); - } - let j = parser.tokenizer.characters().index(); - (s, Location::with_offsets(parser.compilation_unit(), i, j)) - } - } - - /// Parses `CssSelectorCondition` until end-of-file. - pub fn parse_selector_condition(&self) -> Rc { - let mut parser = self.create_parser(); - parser.next(); - let r = parser.parse_selector_condition(); - parser.expect_eof(); - r - } - - pub fn parse_property(&self) -> Rc { - let mut parser = self.create_parser(); - parser.next(); - let r = parser.parse_property(); - parser.expect_eof(); - r - } - - pub fn parse_property_value(&self) -> Rc { - let mut parser = self.create_parser(); - parser.next(); - let r = parser.parse_property_value(CssOperatorPrecedence::Array); - parser.expect_eof(); - r - } - - pub fn parse_rgb(&self) -> Option { - let mut parser = self.create_parser(); - parser.next(); - let r = parser.expect_unitless_number()?; - let g: f64; - let b: f64; - if parser.consume(Token::Comma) { - g = parser.expect_unitless_number()?; - parser.expect(Token::Comma); - b = parser.expect_unitless_number()?; - } else { - g = parser.expect_unitless_number()?; - b = parser.expect_unitless_number()?; - } - parser.expect_eof(); - Some(rgb_bytes_to_integer(r, g, b)) - } - - pub fn parse_embed_entries(&self) -> Vec> { - let mut parser = self.create_parser(); - let mut entries: Vec> = vec![]; - parser.next(); - if !parser.eof() { - entries.push(parser.parse_embed_entry()); - } - while parser.consume(Token::Comma) { - entries.push(parser.parse_embed_entry()); - } - parser.expect_eof(); - entries - } -} diff --git a/crates/parser/parser/css_tokenizer.rs b/crates/parser/parser/css_tokenizer.rs deleted file mode 100644 index 8c12cae..0000000 --- a/crates/parser/parser/css_tokenizer.rs +++ /dev/null @@ -1,434 +0,0 @@ -use crate::ns::*; -use std::str::FromStr; - -pub struct CssTokenizer<'input> { - compilation_unit: Rc, - characters: CharacterReader<'input>, -} - -impl<'input> CssTokenizer<'input> { - /// Constructs a tokenizer. - pub fn new(compilation_unit: &'input Rc, options: &ParserOptions) -> Self { - let text: &'input str = compilation_unit.text(); - let compilation_unit = compilation_unit.clone(); - let characters: CharacterReader<'input>; - if let Some(range) = options.byte_range { - characters = CharacterReader::from_offset(&text[0..range.1], range.0); - } else { - characters = CharacterReader::from(text); - } - Self { - compilation_unit, - characters, - } - } - - pub fn compilation_unit(&self) -> &Rc { - &self.compilation_unit - } - - pub fn characters(&self) -> &CharacterReader<'input> { - &self.characters - } - - pub fn characters_mut(&mut self) -> &mut CharacterReader<'input> { - &mut self.characters - } - - fn character_ahead_location(&self) -> Location { - if self.characters.reached_end() { - return self.cursor_location(); - } - let offset = self.characters.index(); - let mut next_characters = self.characters.clone(); - next_characters.next().unwrap(); - Location::with_offsets(&self.compilation_unit, offset, next_characters.index()) - } - - pub fn cursor_location(&self) -> Location { - let offset = self.characters.index(); - Location::with_offset(&self.compilation_unit, offset) - } - - fn add_syntax_error(&self, location: &Location, kind: DiagnosticKind, arguments: Vec>) { - if self.compilation_unit().prevent_equal_offset_error(location) { - return; - } - self.compilation_unit().add_diagnostic(Diagnostic::new_syntax_error(location, kind, arguments)); - } - - fn add_unexpected_error(&self) { - if self.characters.has_remaining() { - self.add_syntax_error(&self.character_ahead_location(), DiagnosticKind::UnexpectedCharacter, diagarg![self.characters.peek_or_zero().to_string()]) - } else { - self.add_syntax_error(&self.cursor_location(), DiagnosticKind::UnexpectedEnd, vec![]) - } - } - - fn add_unexpected_eof_error(&self, kind: DiagnosticKind) { - self.add_syntax_error(&self.cursor_location(), kind, vec![]); - } - - pub fn scan(&mut self) -> (Token, Location) { - while self.consume_whitespace() || self.consume_comment() { - // Do nothing - } - let start = self.cursor_location(); - let ch = self.characters.peek_or_zero(); - - if let Some(id) = self.consume_css_id() { - return (Token::Identifier(id), start.combine_with(self.cursor_location())); - } - - // DECIMAL - // DECIMAL.PART - if CharacterValidator::is_dec_digit(ch) { - self.characters.next(); - while CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - self.characters.next(); - } - if self.characters.peek_or_zero() == '.' { - self.characters.next(); - if !CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - self.add_unexpected_error(); - } - while CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - self.characters.next(); - } - } - return self.finish_number(start); - } - - if ch == '#' { - self.characters.next(); - let mut word = String::new(); - loop { - let ch = self.characters.peek_or_zero(); - if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || - (ch >= '0' && ch <= '9') || ch == '-' || ch == '_' { - word.push(ch); - self.characters.next(); - } else { - break; - } - } - if word.is_empty() { - self.add_unexpected_error(); - word = INVALIDATED_IDENTIFIER.to_owned(); - } - return (Token::CssHashWord(word), start.combine_with(self.cursor_location())); - } - - if ch == '@' { - // @namespace - if self.characters.peek_seq(10) == "@namespace" { - self.characters.skip_count_in_place(10); - return (Token::CssAtNamespace, start.combine_with(self.cursor_location())); - } - // @font-face - if self.characters.peek_seq(10) == "@font-face" { - self.characters.skip_count_in_place(10); - return (Token::CssAtFontFace, start.combine_with(self.cursor_location())); - } - // @media - if self.characters.peek_seq(6) == "@media" { - self.characters.skip_count_in_place(6); - return (Token::CssAtMedia, start.combine_with(self.cursor_location())); - } - } - - if ch == '!' && self.characters.peek_seq(10) == "!important" { - self.characters.skip_count_in_place(10); - return (Token::CssImportant, start.combine_with(self.cursor_location())); - } - - match ch { - // . - // .DECIMAL - '.' => { - self.characters.next(); - if CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - while CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - self.characters.next(); - } - return self.finish_number(start); - } - (Token::Dot, start.combine_with(self.cursor_location())) - }, - '"' | '\'' => { - self.scan_string(ch, start) - }, - ';' => { - while self.characters.peek_or_zero() == ';' { - self.characters.next(); - } - (Token::CssSemicolons, start.combine_with(self.cursor_location())) - }, - '^' => { - self.characters.next(); - if self.characters.peek_or_zero() != '=' { - self.add_unexpected_error(); - self.characters.next(); - self.scan() - } else { - self.characters.next(); - (Token::CssBeginsWith, start.combine_with(self.cursor_location())) - } - }, - '$' => { - self.characters.next(); - if self.characters.peek_or_zero() != '=' { - self.add_unexpected_error(); - self.characters.next(); - self.scan() - } else { - self.characters.next(); - (Token::CssEndsWith, start.combine_with(self.cursor_location())) - } - }, - '*' => { - self.characters.next(); - if self.characters.peek_or_zero() == '=' { - self.characters.next(); - (Token::CssContains, start.combine_with(self.cursor_location())) - } else { - (Token::Times, start.combine_with(self.cursor_location())) - } - }, - '~' => { - self.characters.next(); - if self.characters.peek_or_zero() == '=' { - self.characters.next(); - (Token::CssListMatch, start.combine_with(self.cursor_location())) - } else { - (Token::Tilde, start.combine_with(self.cursor_location())) - } - }, - '|' => { - self.characters.next(); - if self.characters.peek_or_zero() == '=' { - self.characters.next(); - (Token::CssHreflangMatch, start.combine_with(self.cursor_location())) - } else { - (Token::Pipe, start.combine_with(self.cursor_location())) - } - }, - '{' => { - self.characters.next(); - (Token::BlockOpen, start.combine_with(self.cursor_location())) - }, - '}' => { - self.characters.next(); - (Token::BlockClose, start.combine_with(self.cursor_location())) - }, - '[' => { - self.characters.next(); - (Token::SquareOpen, start.combine_with(self.cursor_location())) - }, - ']' => { - self.characters.next(); - (Token::SquareClose, start.combine_with(self.cursor_location())) - }, - '(' => { - self.characters.next(); - (Token::ParenOpen, start.combine_with(self.cursor_location())) - }, - ')' => { - self.characters.next(); - (Token::ParenClose, start.combine_with(self.cursor_location())) - }, - ',' => { - self.characters.next(); - (Token::Comma, start.combine_with(self.cursor_location())) - }, - '%' => { - self.characters.next(); - (Token::Percent, start.combine_with(self.cursor_location())) - }, - '=' => { - self.characters.next(); - (Token::Assign, start.combine_with(self.cursor_location())) - }, - ':' => { - self.characters.next(); - if self.characters.peek_or_zero() == ':' { - self.characters.next(); - (Token::ColonColon, start.combine_with(self.cursor_location())) - } else { - (Token::Colon, start.combine_with(self.cursor_location())) - } - }, - '>' => { - self.characters.next(); - (Token::Gt, start.combine_with(self.cursor_location())) - }, - '+' => { - self.characters.next(); - (Token::Plus, start.combine_with(self.cursor_location())) - }, - _ => { - if self.characters.reached_end() { - return (Token::Eof, start); - } - self.add_unexpected_error(); - self.characters.next(); - self.scan() - }, - } - } - - pub fn consume_whitespace(&mut self) -> bool { - let ch = self.characters.peek_or_zero(); - if [' ', '\t', '\n', '\r'].contains(&ch) { - self.characters.next(); - true - } else { - false - } - } - - fn consume_comment(&mut self) -> bool { - if self.characters.peek_or_zero() == '/' && self.characters.peek_at_or_zero(1) == '*' { - let start = self.cursor_location(); - self.characters.skip_count_in_place(2); - loop { - if self.characters.peek_or_zero() == '*' && self.characters.peek_at_or_zero(1) == '/' { - self.characters.skip_count_in_place(2); - break; - } else if self.characters.has_remaining() { - self.characters.skip_in_place(); - } else { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingSeqForMultiLineComment); - break; - } - } - - let location = start.combine_with(self.cursor_location()); - let i = location.first_offset() + 2; - let j = decrease_last_offset(i, location.last_offset(), 2); - - self.compilation_unit.add_comment(Rc::new(Comment { - multiline: true, - content: RefCell::new(self.compilation_unit.text()[i..j].to_owned()), - location: RefCell::new(location), - })); - - true - } else { - false - } - } - - fn consume_css_id(&mut self) -> Option { - let i = self.characters.index(); - let mut prefix_n = 0; - if self.characters.peek_or_zero() == '_' { - prefix_n += 1; - if self.characters.peek_at_or_zero(prefix_n) == '_' { - prefix_n += 1; - if self.characters.peek_at_or_zero(prefix_n) == '_' { - prefix_n += 1; - } - } - } else if self.characters.peek_or_zero() == '-' { - prefix_n += 1; - } - if CharacterValidator::is_css_identifier_start(self.characters.peek_at_or_zero(prefix_n)) { - self.characters.skip_count_in_place(prefix_n + 1); - while CharacterValidator::is_css_identifier_part(self.characters.peek_or_zero()) { - self.characters.next(); - } - return Some(self.compilation_unit.text()[i..self.characters.index()].to_owned()); - } - None - } - - fn finish_number(&mut self, start: Location) -> (Token, Location) { - let digits = &self.compilation_unit.text()[start.first_offset..self.characters.index()]; - let mut mv = f64::from_str(digits).unwrap_or(f64::NAN); - let mut unit: Option = None; - if self.characters.peek_or_zero() == '%' { - self.characters.next(); - mv /= 100.0; - } else { - unit = self.consume_css_id(); - } - (Token::CssNumber { - value: mv, - unit, - }, start.combine_with(self.cursor_location())) - } - - fn scan_string(&mut self, delim: char, start: Location) -> (Token, Location) { - let mut builder = String::new(); - self.characters.next(); - loop { - let ch = self.characters.peek_or_zero(); - if ch == delim { - self.characters.next(); - break; - } else if ch == '\\' { - let mut loc = self.cursor_location(); - self.characters.next(); - let mut digits = String::new(); - for _ in 0..6 { - let ch = self.characters.peek_or_zero(); - if CharacterValidator::is_hex_digit(ch) { - digits.push(ch); - self.characters.next(); - } else { - break; - } - } - if digits.is_empty() { - self.add_unexpected_error(); - } else { - loc = loc.combine_with(self.cursor_location()); - let mv = u32::from_str_radix(&digits, 16).ok().and_then(|mv| char::from_u32(mv)); - if let Some(mv) = mv { - builder.push(mv); - } else { - self.add_syntax_error(&loc, DiagnosticKind::CssInvalidHexEscape, diagarg![digits]); - } - } - } else if self.characters.reached_end() { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingQuoteForString); - break; - } else { - builder.push(ch); - self.characters.next(); - } - } - let loc = start.combine_with(self.cursor_location()); - (Token::String(builder), loc) - } - - pub fn scan_arguments(&mut self) -> ((usize, usize), (Token, Location)) { - let i = self.characters.index(); - let mut j: usize; - let mut nesting = 1; - let token: (Token, Location); - loop { - j = self.characters.index(); - let ch = self.characters.peek_or_zero(); - if ch == ')' { - self.characters.next(); - nesting -= 1; - if nesting == 0 { - token = (Token::ParenClose, Location::with_offsets(&self.compilation_unit, j, self.characters.index())); - break; - } - } else if ch == '(' { - self.characters.next(); - nesting += 1; - } else if self.characters.reached_end() { - self.add_syntax_error(&self.cursor_location(), DiagnosticKind::Expecting, diagarg![Token::ParenClose, Token::Eof]); - token = (Token::Eof, self.cursor_location()); - break; - } else { - self.characters.next(); - } - } - ((i, j), token) - } -} \ No newline at end of file diff --git a/crates/parser/parser/parser.rs b/crates/parser/parser/parser.rs deleted file mode 100644 index b71d433..0000000 --- a/crates/parser/parser/parser.rs +++ /dev/null @@ -1,5428 +0,0 @@ -use crate::ns::*; -use lazy_regex::*; - -pub struct Parser<'input> { - tokenizer: Tokenizer<'input>, - previous_token: (Token, Location), - token: (Token, Location), - locations: Vec, - activations: Vec, - ignore_xml_whitespace: bool, - expecting_token_error: bool, -} - -impl<'input> Parser<'input> { - /// Constructs a parser. - pub fn new(compilation_unit: &'input Rc, options: &ParserOptions) -> Self { - Self { - tokenizer: Tokenizer::new(compilation_unit, options), - previous_token: (Token::Eof, Location::with_offset(&compilation_unit, 0)), - token: (Token::Eof, Location::with_offset(&compilation_unit, 0)), - locations: vec![], - activations: vec![], - ignore_xml_whitespace: options.ignore_xml_whitespace, - expecting_token_error: false, - } - } - - fn options(&self) -> ParserOptions { - ParserOptions { - ignore_xml_whitespace: self.ignore_xml_whitespace, - ..default() - } - } - - fn compilation_unit(&self) -> &Rc { - self.tokenizer.compilation_unit() - } - - fn token_location(&self) -> Location { - self.token.1.clone() - } - - fn mark_location(&mut self) { - self.locations.push(self.token.1.clone()); - } - - fn duplicate_location(&mut self) { - self.locations.push(self.locations.last().unwrap().clone()); - } - - fn push_location(&mut self, location: &Location) { - self.locations.push(location.clone()); - } - - fn pop_location(&mut self) -> Location { - self.locations.pop().unwrap().combine_with(self.previous_token.1.clone()) - } - - fn add_syntax_error(&self, location: &Location, kind: DiagnosticKind, arguments: Vec>) { - if self.compilation_unit().prevent_equal_offset_error(location) { - return; - } - self.compilation_unit().add_diagnostic(Diagnostic::new_syntax_error(location, kind, arguments)); - } - - fn patch_syntax_error(&self, original: DiagnosticKind, kind: DiagnosticKind, arguments: Vec>) { - if self.compilation_unit().diagnostics.borrow().is_empty() { - return; - } - if self.compilation_unit().diagnostics.borrow().last().unwrap().kind == original { - let loc = self.compilation_unit().diagnostics.borrow_mut().pop().unwrap().location(); - self.compilation_unit().add_diagnostic(Diagnostic::new_syntax_error(&loc, kind, arguments)); - } - } - - /* - fn add_warning(&self, location: &Location, kind: DiagnosticKind, arguments: Vec>) { - if self.compilation_unit().prevent_equal_offset_warning(location) { - return; - } - self.compilation_unit().add_diagnostic(Diagnostic::new_warning(location, kind, arguments)); - } - */ - - fn next(&mut self) { - self.previous_token = self.token.clone(); - self.token = self.tokenizer.scan_ie_div(); - } - - fn next_ie_xml_tag(&mut self) { - self.previous_token = self.token.clone(); - self.token = self.tokenizer.scan_ie_xml_tag(); - } - - fn next_ie_xml_content(&mut self) { - self.previous_token = self.token.clone(); - self.token = self.tokenizer.scan_ie_xml_content(); - } - - fn peek(&self, token: Token) -> bool { - self.token.0 == token - } - - fn peek_identifier(&self, reserved_words: bool) -> Option<(String, Location)> { - if let Token::Identifier(id) = self.token.0.clone() { - let location = self.token.1.clone(); - Some((id, location)) - } else { - if reserved_words { - if let Some(id) = self.token.0.reserved_word_name() { - let location = self.token.1.clone(); - return Some((id, location)); - } - } - None - } - } - - fn peek_context_keyword(&self, name: &str) -> bool { - if let Token::Identifier(id) = self.token.0.clone() { id == name && self.token.1.character_count() == name.len() } else { false } - } - - fn consume(&mut self, token: Token) -> bool { - if self.token.0 == token { - self.next(); - true - } else { - false - } - } - - fn consume_and_ie_xml_tag(&mut self, token: Token) -> bool { - if self.token.0 == token { - self.next_ie_xml_tag(); - true - } else { - false - } - } - - fn consume_and_ie_xml_content(&mut self, token: Token) -> bool { - if self.token.0 == token { - self.next_ie_xml_content(); - true - } else { - false - } - } - - fn consume_identifier(&mut self, reserved_words: bool) -> Option<(String, Location)> { - if let Token::Identifier(id) = self.token.0.clone() { - let location = self.token.1.clone(); - self.next(); - Some((id, location)) - } else { - if reserved_words { - if let Some(id) = self.token.0.reserved_word_name() { - let location = self.token.1.clone(); - self.next(); - return Some((id, location)); - } - } - None - } - } - - fn _consume_context_keyword(&mut self, name: &str) -> bool { - if let Token::Identifier(id) = self.token.0.clone() { - if id == name && self.token.1.character_count() == name.len() { - self.next(); - true - } else { - false - } - } else { - false - } - } - - fn expect(&mut self, token: Token) { - if self.token.0 != token { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]); - let expecting_identifier_name = token.is_identifier_name(); - while self.token.0 != Token::Eof && (if expecting_identifier_name { self.token.0.is_identifier_name() } else { true }) { - self.next(); - if self.token.0 == token { - return; - } - } - } else { - self.expecting_token_error = false; - self.next(); - } - } - - /// Expects a token; but if it fails, does not skip any token. - fn non_greedy_expect(&mut self, token: Token) { - if self.token.0 != token { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]); - } else { - self.expecting_token_error = false; - self.next(); - } - } - - fn non_greedy_expect_virtual_semicolon(&mut self) { - self.expecting_token_error = false; - if !self.parse_semicolon() { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingEitherSemicolonOrNewLineHere, vec![]); - } - } - - fn expect_and_ie_xml_tag(&mut self, token: Token) { - if self.token.0 != token { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]); - while self.token.0 != Token::Eof { - self.next_ie_xml_tag(); - if self.token.0 == token { - return; - } - } - } else { - self.expecting_token_error = false; - self.next_ie_xml_tag(); - } - } - - /// Expects a token; but if it fails, does not skip any token. - fn non_greedy_expect_and_ie_xml_tag(&mut self, token: Token) { - if self.token.0 != token { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]); - } else { - self.expecting_token_error = false; - self.next_ie_xml_tag(); - } - } - - fn expect_and_ie_xml_content(&mut self, token: Token) { - if self.token.0 != token { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]); - while self.token.0 != Token::Eof { - self.next_ie_xml_content(); - if self.token.0 == token { - return; - } - } - } else { - self.expecting_token_error = false; - self.next_ie_xml_content(); - } - } - - fn non_greedy_expect_and_ie_xml_content(&mut self, token: Token) { - if self.token.0 != token { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]); - } else { - self.expecting_token_error = false; - self.next_ie_xml_content(); - } - } - - fn expect_identifier(&mut self, reserved_words: bool) -> (String, Location) { - if let Token::Identifier(id) = self.token.0.clone() { - self.expecting_token_error = false; - let location = self.token.1.clone(); - self.next(); - (id, location) - } else { - if reserved_words { - if let Some(id) = self.token.0.reserved_word_name() { - self.expecting_token_error = false; - let location = self.token.1.clone(); - self.next(); - return (id, location); - } - } - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![self.token.0.clone()]); - /* - while self.token.0 != Token::Eof && self.token.0.is_identifier_name() { - if let Some(id) = self.consume_identifier(reserved_words) { - return id; - } else { - self.next(); - } - } - */ - (INVALIDATED_IDENTIFIER.to_owned(), self.tokenizer.cursor_location()) - } - } - - fn _expect_context_keyword(&mut self, name: &str) { - if let Token::Identifier(id) = self.token.0.clone() { - if id == name && self.token.1.character_count() == name.len() { - self.expecting_token_error = false; - self.next(); - return; - } - } - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![format!("'{name}'"), self.token.0.clone()]); - while self.token.0 != Token::Eof && self.token.0.is_identifier_name() { - if self._consume_context_keyword(name) { - return; - } else { - self.next(); - } - } - } - - fn non_greedy_expect_context_keyword(&mut self, name: &str) { - if let Token::Identifier(id) = self.token.0.clone() { - if id == name && self.token.1.character_count() == name.len() { - self.expecting_token_error = false; - self.next(); - return; - } - } - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![format!("'{name}'"), self.token.0.clone()]); - } - - /// Expects a greater-than symbol. If the facing token is not greater-than, - /// but starts with a greater-than symbol, the first character is shifted off - /// from the facing token. - fn _expect_type_parameters_gt(&mut self) { - self.expecting_token_error = false; - if !self.consume_type_parameters_gt() { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![Token::Gt, self.token.0.clone()]); - while self.token.0 != Token::Eof { - self.next(); - if self.consume_type_parameters_gt() { - return; - } - } - } - } - - fn non_greedy_expect_type_parameters_gt(&mut self) { - self.expecting_token_error = false; - if !self.consume_type_parameters_gt() { - self.expecting_token_error = true; - self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![Token::Gt, self.token.0.clone()]); - } - } - - /// Consumes a greater-than symbol. If the facing token is not greater-than, - /// but starts with a greater-than symbol, the first character is shifted off - /// from the facing token. - fn consume_type_parameters_gt(&mut self) -> bool { - match self.token.0 { - Token::Gt => { - self.next(); - true - }, - Token::Ge => { - self.token.0 = Token::Assign; - self.token.1.first_offset += 1; - true - }, - Token::RightShift => { - self.token.0 = Token::Gt; - self.token.1.first_offset += 1; - true - }, - Token::RightShiftAssign => { - self.token.0 = Token::Ge; - self.token.1.first_offset += 1; - true - }, - Token::UnsignedRightShift => { - self.token.0 = Token::RightShift; - self.token.1.first_offset += 1; - true - }, - Token::UnsignedRightShiftAssign => { - self.token.0 = Token::RightShiftAssign; - self.token.1.first_offset += 1; - true - }, - _ => { - false - }, - } - } - - fn offending_token_is_inline_or_higher_indented(&self) -> bool { - if !self.previous_token.1.line_break(&self.token.1) { - return true; - } - let i1 = self.compilation_unit().get_line_indent(self.previous_token.1.first_line_number()); - let i2 = self.compilation_unit().get_line_indent(self.token.1.first_line_number()); - i2 > i1 - } - - pub fn expect_eof(&mut self) { - self.expect(Token::Eof) - } - - fn create_invalidated_expression(&self, location: &Location) -> Rc { - Rc::new(Expression::Invalidated(InvalidatedNode { - location: location.clone(), - })) - } - - fn create_invalidated_directive(&self, location: &Location) -> Rc { - Rc::new(Directive::Invalidated(InvalidatedNode { - location: location.clone(), - })) - } - - pub fn parse_expression(&mut self, context: ParserExpressionContext) -> Rc { - if let Some(exp) = self.parse_opt_expression(context) { - exp - } else { - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingExpression, diagarg![self.token.0.clone()]); - self.create_invalidated_expression(&self.tokenizer.cursor_location()) - } - } - - pub fn parse_opt_expression(&mut self, context: ParserExpressionContext) -> Option> { - let exp: Option> = self.parse_opt_start_expression(context.clone()); - - // Parse subexpressions - if let Some(exp) = exp { - return Some(self.parse_subexpressions(exp, context.clone())); - } - None - } - - fn parse_subexpressions(&mut self, mut base: Rc, context: ParserExpressionContext) -> Rc { - loop { - if self.consume(Token::Dot) { - base = self.parse_dot_subexpression(base); - } else if self.consume(Token::OptionalChaining) { - base = self.parse_optional_chaining(base); - } else if self.peek(Token::SquareOpen) { - let asdoc = self.parse_asdoc(); - self.next(); - self.push_location(&base.location()); - let key = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() }); - self.non_greedy_expect(Token::SquareClose); - base = Rc::new(Expression::ComputedMember(ComputedMemberExpression { - base, asdoc, key, location: self.pop_location() - })); - } else if self.consume(Token::Descendants) { - self.push_location(&base.location()); - let id = self.parse_qualified_identifier(); - base = Rc::new(Expression::Descendants(DescendantsExpression { - location: self.pop_location(), - base, - identifier: id, - })); - } else if self.peek(Token::ParenOpen) { - self.push_location(&base.location()); - let arguments = self.parse_arguments(); - base = Rc::new(Expression::Call(CallExpression { - location: self.pop_location(), - base, - arguments, - })); - } else if self.peek(Token::Increment) && !self.previous_token.1.line_break(&self.token.1) { - self.push_location(&base.location()); - self.next(); - base = Rc::new(Expression::Unary(UnaryExpression { - location: self.pop_location(), - expression: base, - operator: Operator::PostIncrement, - })); - } else if self.peek(Token::Decrement) && !self.previous_token.1.line_break(&self.token.1) { - self.push_location(&base.location()); - self.next(); - base = Rc::new(Expression::Unary(UnaryExpression { - location: self.pop_location(), - expression: base, - operator: Operator::PostDecrement, - })); - } else if self.peek(Token::Exclamation) && !self.previous_token.1.line_break(&self.token.1) { - self.push_location(&base.location()); - self.next(); - base = Rc::new(Expression::Unary(UnaryExpression { - location: self.pop_location(), - expression: base, operator: Operator::NonNull, - })); - // `not in` - } else if self.token.0 == Token::Not && context.allow_in && context.min_precedence.includes(&OperatorPrecedence::Relational) && !self.previous_token.1.line_break(&self.token.1) { - self.push_location(&base.location()); - self.next(); - self.non_greedy_expect(Token::In); - base = self.parse_binary_operator(base, Operator::NotIn, OperatorPrecedence::Relational.add(1).unwrap(), context.clone()); - // ConditionalExpression - } else if self.peek(Token::Question) && context.min_precedence.includes(&OperatorPrecedence::AssignmentAndOther) { - self.push_location(&base.location()); - self.next(); - let consequent = self.parse_expression(ParserExpressionContext { - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..context.clone() - }); - let mut alternative = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - self.non_greedy_expect(Token::Colon); - if !self.expecting_token_error { - alternative = self.parse_expression(ParserExpressionContext { - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..context.clone() - }); - } - base = Rc::new(Expression::Conditional(ConditionalExpression { - location: self.pop_location(), - test: base, consequent, alternative, - })); - } else if let Some(binary_operator) = self.check_binary_operator(context.clone()) { - let BinaryOperator(operator, required_precedence, _) = binary_operator; - if context.min_precedence.includes(&required_precedence) { - self.next(); - base = self.parse_binary_operator(base, operator, binary_operator.right_precedence(), context.clone()); - } else { - break; - } - // AssignmentExpression - } else if self.peek(Token::Assign) && context.min_precedence.includes(&OperatorPrecedence::AssignmentAndOther) && context.allow_assignment { - self.push_location(&base.location()); - self.next(); - let left = base.clone(); - if !left.is_valid_assignment_left_hand_side() { - self.add_syntax_error(&left.location(), DiagnosticKind::MalformedDestructuring, vec![]) - } - let right = self.parse_expression(ParserExpressionContext { - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..context.clone() - }); - base = Rc::new(Expression::Assignment(AssignmentExpression { - location: self.pop_location(), - left, compound: None, right, - })); - // CompoundAssignment and LogicalAssignment - } else if let Some(compound) = self.token.0.compound_assignment() { - if context.min_precedence.includes(&OperatorPrecedence::AssignmentAndOther) && context.allow_assignment { - self.push_location(&base.location()); - self.next(); - let left = base.clone(); - let right = self.parse_expression(ParserExpressionContext { - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..context.clone() - }); - base = Rc::new(Expression::Assignment(AssignmentExpression { - location: self.pop_location(), - left, compound: Some(compound), right, - })); - } else { - break; - } - } else if self.peek(Token::Comma) && context.min_precedence.includes(&OperatorPrecedence::List) { - self.push_location(&base.location()); - self.next(); - let right = self.parse_expression(ParserExpressionContext { - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..context.clone() - }); - base = Rc::new(Expression::Sequence(SequenceExpression { - location: self.pop_location(), - left: base, right, - })); - } else { - break; - } - } - - base - } - - fn parse_binary_operator(&mut self, base: Rc, mut operator: Operator, right_precedence: OperatorPrecedence, context: ParserExpressionContext) -> Rc { - // The left operand of a null-coalescing operation must not be - // a logical AND, XOR or OR operation. - if operator == Operator::NullCoalescing { - if let Expression::Unary(UnaryExpression { expression, operator, .. }) = base.as_ref() { - if [Operator::LogicalAnd, Operator::LogicalXor, Operator::LogicalOr].contains(&operator) { - self.add_syntax_error(&expression.location(), DiagnosticKind::IllegalNullishCoalescingLeftOperand, vec![]); - } - } - } - - if operator == Operator::Is && self.consume(Token::Not) { - operator = Operator::IsNot; - } - - self.push_location(&base.location()); - let right = self.parse_expression(ParserExpressionContext { - min_precedence: right_precedence, - ..context - }); - Rc::new(Expression::Binary(BinaryExpression { - location: self.pop_location(), - left: base, operator, right, - })) - } - - fn check_binary_operator(&self, context: ParserExpressionContext) -> Option { - if let Some(operator) = self.token.0.to_binary_operator() { - if operator == Operator::In && !context.allow_in { - return None; - } - BinaryOperator::try_from(operator).ok() - } else { - None - } - } - - fn parse_optional_chaining(&mut self, base: Rc) -> Rc { - self.push_location(&base.location()); - self.duplicate_location(); - let mut operation = Rc::new(Expression::OptionalChainingPlaceholder(OptionalChainingPlaceholder { - location: base.location(), - })); - if self.peek(Token::ParenOpen) { - let arguments: Vec> = self.parse_arguments(); - if arguments.len() == 1 && self.peek(Token::ColonColon) { - self.duplicate_location(); - let ql = self.pop_location(); - let q = Rc::new(Expression::Paren(ParenExpression { - location: ql.clone(), - expression: arguments[0].clone(), - })); - let identifier = self.finish_qualified_identifier(false, ql, q); - operation = Rc::new(Expression::Member(MemberExpression { - location: self.pop_location(), - base: operation, - identifier, - })); - } else { - operation = Rc::new(Expression::Call(CallExpression { - location: self.pop_location(), - base: operation, arguments - })); - } - } else if self.peek(Token::SquareOpen) { - let asdoc = self.parse_asdoc(); - self.next(); - let key = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() }); - self.non_greedy_expect(Token::SquareClose); - operation = Rc::new(Expression::ComputedMember(ComputedMemberExpression { - location: self.pop_location(), - base: operation, asdoc, key, - })); - } else { - let identifier = self.parse_qualified_identifier(); - operation = Rc::new(Expression::Member(MemberExpression { - location: self.pop_location(), - base: operation, identifier - })); - } - - // Parse postfix subexpressions - operation = self.parse_optional_chaining_subexpressions(operation); - - Rc::new(Expression::OptionalChaining(OptionalChainingExpression { - location: self.pop_location(), - base, expression: operation, - })) - } - - fn parse_optional_chaining_subexpressions(&mut self, mut base: Rc) -> Rc { - loop { - if self.consume(Token::Dot) { - base = self.parse_dot_subexpression(base); - } else if self.consume(Token::OptionalChaining) { - base = self.parse_optional_chaining(base); - } else if self.peek(Token::SquareOpen) { - self.next(); - self.push_location(&base.location()); - let key = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() }); - self.non_greedy_expect(Token::SquareClose); - base = Rc::new(Expression::ComputedMember(ComputedMemberExpression { - base, asdoc: None, key, location: self.pop_location() - })); - } else if self.consume(Token::Descendants) { - self.push_location(&base.location()); - let id = self.parse_qualified_identifier(); - base = Rc::new(Expression::Descendants(DescendantsExpression { - location: self.pop_location(), - base, - identifier: id, - })); - } else if self.peek(Token::ParenOpen) { - self.push_location(&base.location()); - let arguments = self.parse_arguments(); - base = Rc::new(Expression::Call(CallExpression { - location: self.pop_location(), - base, - arguments, - })); - } else if self.peek(Token::Exclamation) && !self.previous_token.1.line_break(&self.token.1) { - self.push_location(&base.location()); - self.next(); - base = Rc::new(Expression::Unary(UnaryExpression { - location: self.pop_location(), - expression: base, operator: Operator::NonNull, - })); - } else { - break; - } - } - - base - } - - fn parse_dot_subexpression(&mut self, base: Rc) -> Rc { - self.push_location(&base.location()); - if self.peek(Token::ParenOpen) { - let paren_location = self.token_location(); - let paren_exp = self.parse_paren_list_expression(); - if !matches!(paren_exp.as_ref(), Expression::Sequence(_)) && self.peek(Token::ColonColon) { - let q = Rc::new(Expression::Paren(ParenExpression { - location: paren_location.clone(), - expression: paren_exp.clone(), - })); - let id = self.finish_qualified_identifier(false, paren_location, q); - Rc::new(Expression::Member(MemberExpression { - location: self.pop_location(), - base, identifier: id - })) - } else { - Rc::new(Expression::Filter(FilterExpression { - location: self.pop_location(), - base, test: paren_exp - })) - } - } else if self.consume(Token::Lt) { - let mut arguments = vec![]; - arguments.push(self.parse_type_expression()); - while self.consume(Token::Comma) { - arguments.push(self.parse_type_expression()); - } - self.non_greedy_expect_type_parameters_gt(); - Rc::new(Expression::WithTypeArguments(ExpressionWithTypeArguments { - location: self.pop_location(), - base, arguments - })) - } else { - let id = self.parse_qualified_identifier(); - Rc::new(Expression::Member(MemberExpression { - location: self.pop_location(), - base, identifier: id - })) - } - } - - /// Ensures a parameter list consists of zero or more required parameters followed by - /// zero or more optional parameters optionally followed by a rest parameter. - fn validate_parameter_list(&mut self, params: Vec<(ParameterKind, Location)>) { - let mut least_kind = ParameterKind::Required; - let mut has_rest = false; - for (param_kind, param_loc) in params { - if !least_kind.may_be_followed_by(param_kind) { - self.add_syntax_error(¶m_loc, DiagnosticKind::WrongParameterPosition, vec![]); - } - least_kind = param_kind; - if param_kind == ParameterKind::Rest && has_rest { - self.add_syntax_error(¶m_loc, DiagnosticKind::DuplicateRestParameter, vec![]); - } - has_rest = param_kind == ParameterKind::Rest; - } - } - - fn parse_opt_start_expression(&mut self, context: ParserExpressionContext) -> Option> { - if let Token::Identifier(id) = self.token.0.clone() { - let id_location = self.token_location(); - self.next(); - Some(self.parse_expression_starting_with_identifier((id, id_location))) - } else if self.peek(Token::Null) { - self.mark_location(); - self.next(); - Some(Rc::new(Expression::NullLiteral(NullLiteral { - location: self.pop_location(), - }))) - } else if self.peek(Token::False) { - self.mark_location(); - self.next(); - Some(Rc::new(Expression::BooleanLiteral(BooleanLiteral { - location: self.pop_location(), - value: false, - }))) - } else if self.peek(Token::True) { - self.mark_location(); - self.next(); - Some(Rc::new(Expression::BooleanLiteral(BooleanLiteral { - location: self.pop_location(), - value: true, - }))) - } else if let Token::Number(n, suffix) = self.token.0.clone() { - self.mark_location(); - self.next(); - Some(Rc::new(Expression::NumericLiteral(NumericLiteral { - location: self.pop_location(), - value: n, - suffix, - }))) - } else if let Token::String(ref s) = self.token.0.clone() { - self.mark_location(); - self.next(); - Some(Rc::new(Expression::StringLiteral(StringLiteral { - location: self.pop_location(), - value: s.clone(), - }))) - } else if self.peek(Token::This) { - self.mark_location(); - self.next(); - Some(Rc::new(Expression::ThisLiteral(ThisLiteral { - location: self.pop_location(), - }))) - } else if self.peek(Token::Div) || self.peek(Token::DivideAssign) { - self.mark_location(); - self.token = self.tokenizer.scan_regexp_literal(self.token.1.clone(), if self.peek(Token::DivideAssign) { "=".into() } else { "".into() }); - let Token::RegExp { ref body, ref flags } = self.token.0.clone() else { - panic!(); - }; - self.next(); - Some(Rc::new(Expression::RegExpLiteral(RegExpLiteral { - location: self.pop_location(), - body: body.clone(), flags: flags.clone(), - }))) - // `@` - } else if self.peek(Token::Attribute) { - self.mark_location(); - let id = self.parse_qualified_identifier(); - Some(Rc::new(Expression::QualifiedIdentifier(id))) - // Parentheses - } else if self.peek(Token::ParenOpen) { - Some(self.parse_paren_list_expr_or_qual_id()) - // XMLList, XMLElement, XMLMarkup - } else if self.peek(Token::Lt) { - if let Some(token) = self.tokenizer.scan_xml_markup(self.token_location()) { - self.token = token; - } - let start = self.token_location(); - if let Token::XmlMarkup(content) = &self.token.0.clone() { - self.mark_location(); - self.next(); - Some(Rc::new(Expression::XmlMarkup(XmlMarkupExpression { - location: self.pop_location(), - markup: content.clone(), - }))) - } else { - Some(self.parse_xml_element_or_xml_list(start)) - } - // ArrayInitializer - } else if self.peek(Token::SquareOpen) { - Some(self.parse_array_initializer()) - // NewExpression - } else if self.peek(Token::New) && context.min_precedence.includes(&OperatorPrecedence::Unary) { - let start = self.token_location(); - self.next(); - Some(self.parse_new_expression(start)) - } else if self.peek(Token::BlockOpen) { - Some(self.parse_object_initializer()) - } else if self.peek(Token::Function) && context.min_precedence.includes(&OperatorPrecedence::AssignmentAndOther) { - Some(self.parse_function_expression(context.clone())) - // SuperExpression - } else if self.peek(Token::Super) && context.min_precedence.includes(&OperatorPrecedence::Postfix) { - Some(self.parse_super_expression_followed_by_property_operator()) - // AwaitExpression - } else if self.peek(Token::Await) && context.min_precedence.includes(&OperatorPrecedence::Unary) { - self.mark_location(); - let operator_token = self.token.clone(); - self.next(); - let base = self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::Unary, - ..default() - }); - if let Some(activation) = self.activations.last_mut() { - activation.uses_await = true; - } else { - self.add_syntax_error(&operator_token.1, DiagnosticKind::NotAllowedHere, diagarg![operator_token.0]); - } - Some(Rc::new(Expression::Unary(UnaryExpression { - location: self.pop_location(), - expression: base, operator: Operator::Await, - }))) - // YieldExpression - } else if self.peek(Token::Yield) && context.min_precedence.includes(&OperatorPrecedence::AssignmentAndOther) { - self.mark_location(); - let operator_token = self.token.clone(); - self.next(); - let base = self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - }); - if let Some(activation) = self.activations.last_mut() { - activation.uses_yield = true; - } else { - self.add_syntax_error(&operator_token.1, DiagnosticKind::NotAllowedHere, diagarg![operator_token.0]); - } - Some(Rc::new(Expression::Unary(UnaryExpression { - location: self.pop_location(), - expression: base, operator: Operator::Yield, - }))) - // Miscellaneous prefix unary expressions - } else if let Some((operator, subexp_precedence)) = self.check_prefix_operator() { - if context.min_precedence.includes(&OperatorPrecedence::Unary) { - self.mark_location(); - self.next(); - let base = self.parse_expression(ParserExpressionContext { min_precedence: subexp_precedence, ..default() }); - Some(Rc::new(Expression::Unary(UnaryExpression { - location: self.pop_location(), - expression: base, operator, - }))) - } else { - None - } - // ImportMeta - } else if self.peek(Token::Import) && context.min_precedence.includes(&OperatorPrecedence::Postfix) { - self.mark_location(); - self.next(); - self.non_greedy_expect(Token::Dot); - self.non_greedy_expect_context_keyword("meta"); - Some(Rc::new(Expression::ImportMeta(ImportMeta { - location: self.pop_location(), - }))) - // QualifiedIdentifier - } else if - self.peek(Token::Times) - || self.peek(Token::Public) || self.peek(Token::Private) - || self.peek(Token::Protected) || self.peek(Token::Internal) { - let id = self.parse_qualified_identifier(); - Some(Rc::new(Expression::QualifiedIdentifier(id))) - } else { - None - } - } - - fn parse_expression_starting_with_identifier(&mut self, id: (String, Location)) -> Rc { - let id_location = id.1.clone(); - let id = id.0; - - /* - // EmbedExpression - if self.peek(Token::BlockOpen) && id == "embed" && self.previous_token.1.character_count() == "embed".len() { - return self.finish_embed_expression(id_location); - } - */ - - let id = Rc::new(Expression::QualifiedIdentifier(QualifiedIdentifier { - location: id_location.clone(), - attribute: false, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id((id, id_location.clone())), - })); - if self.peek(Token::ColonColon) { - self.push_location(&id_location.clone()); - let ql = self.pop_location(); - let id = self.finish_qualified_identifier(false, ql, id); - Rc::new(Expression::QualifiedIdentifier(id)) - } else { - id - } - } - - fn check_prefix_operator(&self) -> Option<(Operator, OperatorPrecedence)> { - match self.token.0 { - Token::Delete => Some((Operator::Delete, OperatorPrecedence::Postfix)), - Token::Void => Some((Operator::Void, OperatorPrecedence::Unary)), - Token::Typeof => Some((Operator::Typeof, OperatorPrecedence::Unary)), - Token::Increment => Some((Operator::PreIncrement, OperatorPrecedence::Postfix)), - Token::Decrement => Some((Operator::PreDecrement, OperatorPrecedence::Postfix)), - Token::Plus => Some((Operator::Positive, OperatorPrecedence::Unary)), - Token::Minus => Some((Operator::Negative, OperatorPrecedence::Unary)), - Token::Tilde => Some((Operator::BitwiseNot, OperatorPrecedence::Unary)), - Token::Exclamation => Some((Operator::LogicalNot, OperatorPrecedence::Unary)), - _ => None, - } - } - - fn parse_function_expression(&mut self, context: ParserExpressionContext) -> Rc { - self.mark_location(); - self.next(); - let mut name = None; - if let Token::Identifier(id) = self.token.0.clone() { - name = Some((id, self.token.1.clone())); - self.next(); - } - let common = self.parse_function_common(true, ParserDirectiveContext::Default, context.allow_in); - Rc::new(Expression::Function(FunctionExpression { - location: self.pop_location(), - name, - common, - })) - } - - fn parse_function_common(&mut self, function_expr: bool, block_context: ParserDirectiveContext, allow_in: bool) -> Rc { - self.mark_location(); - self.duplicate_location(); - let mut params: Vec> = vec![]; - let mut return_annotation = Some(self.create_invalidated_expression(&self.tokenizer.cursor_location())); - self.non_greedy_expect(Token::ParenOpen); - if !self.expecting_token_error { - if !self.peek(Token::ParenClose) { - params.push(self.parse_parameter()); - while self.consume(Token::Comma) { - params.push(self.parse_parameter()); - } - } - self.non_greedy_expect(Token::ParenClose); - if !self.expecting_token_error { - return_annotation = if self.consume(Token::Colon) { Some(self.parse_type_expression()) } else { None }; - } - self.validate_parameter_list(params.iter().map(|p| (p.kind, p.location.clone())).collect::>()); - } - - let signature_location = self.pop_location(); - - // Enter activation - self.activations.push(ParserActivation::new()); - - // Body - let body = if self.peek(Token::BlockOpen) { - Some(FunctionBody::Block(Rc::new(self.parse_block(block_context)))) - } else if !(self.offending_token_is_inline_or_higher_indented() || self.peek(Token::ParenOpen)) { - None - } else { - self.parse_opt_expression(ParserExpressionContext { - allow_in, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - }).map(|e| FunctionBody::Expression(e)) - }; - - // Body is required by function expressions - if body.is_none() && function_expr { - self.non_greedy_expect(Token::BlockOpen); - } - - // Exit activation - let activation = self.activations.pop().unwrap(); - - Rc::new(FunctionCommon { - location: self.pop_location(), - contains_await: activation.uses_await, - contains_yield: activation.uses_yield, - signature: FunctionSignature { - location: signature_location, - parameters: params, - result_type: return_annotation, - }, - body, - }) - } - - fn parse_parameter(&mut self) -> Rc { - self.mark_location(); - let rest = self.consume(Token::Ellipsis); - let binding: Rc = Rc::new(self.parse_variable_binding(true)); - let has_initializer = binding.initializer.is_some(); - let location = self.pop_location(); - if rest && has_initializer { - self.add_syntax_error(&location.clone(), DiagnosticKind::MalformedRestParameter, vec![]); - } - Rc::new(Parameter { - location, - destructuring: binding.destructuring.clone(), - default_value: binding.initializer.clone(), - kind: if rest { - ParameterKind::Rest - } else if has_initializer { - ParameterKind::Optional - } else { - ParameterKind::Required - }, - }) - } - - fn parse_object_initializer(&mut self) -> Rc { - self.mark_location(); - self.non_greedy_expect(Token::BlockOpen); - let mut fields: Vec> = vec![]; - while !self.peek(Token::BlockClose) { - fields.push(self.parse_field()); - if !self.consume(Token::Comma) { - break; - } - } - self.non_greedy_expect(Token::BlockClose); - - Rc::new(Expression::ObjectInitializer(ObjectInitializer { - location: self.pop_location(), - fields, - })) - } - - fn parse_field(&mut self) -> Rc { - if self.peek(Token::Ellipsis) { - self.mark_location(); - self.next(); - let subexp = self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - }); - return Rc::new(InitializerField::Rest((subexp, self.pop_location()))); - } - - let name = self.parse_field_name(); - - let non_null = self.consume(Token::Exclamation); - let mut value = None; - - if self.consume(Token::Colon) { - value = Some(self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - })); - } else if !matches!(name.0, FieldName::Identifier(_)) { - self.non_greedy_expect(Token::Colon); - } - - Rc::new(InitializerField::Field { - name, - non_null, - value, - }) - } - - fn parse_field_name(&mut self) -> (FieldName, Location) { - if let Token::String(value) = &self.token.0.clone() { - let location = self.token_location(); - self.next(); - (FieldName::StringLiteral(Rc::new(Expression::StringLiteral(StringLiteral { - location: location.clone(), - value: value.clone(), - }))), location) - } else if let Token::Number(value, suffix) = &self.token.0.clone() { - let location = self.token_location(); - self.next(); - (FieldName::NumericLiteral(Rc::new(Expression::NumericLiteral(NumericLiteral { - location: location.clone(), - value: value.clone(), - suffix: *suffix, - }))), location) - } else if self.peek(Token::SquareOpen) { - self.mark_location(); - self.next(); - let key_expr = self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::List, - ..default() - }); - self.non_greedy_expect(Token::SquareClose); - let location = self.pop_location(); - (FieldName::Brackets(key_expr), location) - } else { - let id = self.parse_non_attribute_qualified_identifier(); - let l = id.location.clone(); - (FieldName::Identifier(id), l) - } - } - - fn parse_new_expression(&mut self, start: Location) -> Rc { - self.push_location(&start); - if self.consume(Token::Lt) { - let element_type = self.parse_type_expression(); - self.non_greedy_expect_type_parameters_gt(); - let mut elements: Vec = vec![]; - self.non_greedy_expect(Token::SquareOpen); - if !self.expecting_token_error { - while !self.peek(Token::SquareClose) { - if self.peek(Token::Ellipsis) { - self.mark_location(); - self.next(); - elements.push(Element::Rest((self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - }), self.pop_location()))); - } else { - elements.push(Element::Expression(self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - }))); - } - if !self.consume(Token::Comma) { - break; - } - } - self.non_greedy_expect(Token::SquareClose); - } - Rc::new(Expression::VectorLiteral(VectorLiteral { - location: self.pop_location(), - element_type, - elements, - })) - } else { - let base = self.parse_new_subexpression(); - let arguments = if self.peek(Token::ParenOpen) { Some(self.parse_arguments()) } else { None }; - Rc::new(Expression::New(NewExpression { - location: self.pop_location(), - base, arguments, - })) - } - } - - fn parse_new_expression_start(&mut self) -> Rc { - if self.peek(Token::New) { - let start = self.token_location(); - self.next(); - self.parse_new_expression(start) - } else if self.peek(Token::Super) { - self.parse_super_expression_followed_by_property_operator() - } else { - self.parse_primary_expression() - } - } - - fn parse_super_expression_followed_by_property_operator(&mut self) -> Rc { - self.mark_location(); - self.duplicate_location(); - self.next(); - let arguments = if self.peek(Token::ParenOpen) { Some(self.parse_arguments()) } else { None }; - let super_expr = Rc::new(Expression::Super(SuperExpression { - location: self.pop_location(), - object: arguments, - })); - - if self.consume(Token::SquareOpen) { - let key = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() }); - self.non_greedy_expect(Token::SquareClose); - Rc::new(Expression::ComputedMember(ComputedMemberExpression { - location: self.pop_location(), - base: super_expr, asdoc: None, key, - })) - } else { - self.non_greedy_expect(Token::Dot); - let identifier = self.parse_qualified_identifier(); - Rc::new(Expression::Member(MemberExpression { - location: self.pop_location(), - base: super_expr, identifier, - })) - } - } - - fn parse_arguments(&mut self) -> Vec> { - self.non_greedy_expect(Token::ParenOpen); - let mut arguments = vec![]; - if !self.peek(Token::ParenClose) { - arguments.push(self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - })); - while self.consume(Token::Comma) { - arguments.push(self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - })); - } - } - self.non_greedy_expect(Token::ParenClose); - arguments - } - - fn parse_new_subexpression(&mut self) -> Rc { - let mut base = self.parse_new_expression_start(); - loop { - if self.consume(Token::SquareOpen) { - self.push_location(&base.location()); - let key = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() }); - self.non_greedy_expect(Token::SquareClose); - base = Rc::new(Expression::ComputedMember(ComputedMemberExpression { - location: self.pop_location(), - base, asdoc: None, key, - })); - } else if self.consume(Token::Dot) { - self.push_location(&base.location()); - if self.consume(Token::Lt) { - let mut arguments = vec![]; - arguments.push(self.parse_type_expression()); - while self.consume(Token::Comma) { - arguments.push(self.parse_type_expression()); - } - self.non_greedy_expect_type_parameters_gt(); - base = Rc::new(Expression::WithTypeArguments(ExpressionWithTypeArguments { - location: self.pop_location(), - base, arguments - })); - } else { - let identifier = self.parse_qualified_identifier(); - base = Rc::new(Expression::Member(MemberExpression { - location: self.pop_location(), - base, identifier, - })); - } - } else { - break; - } - } - base - } - - fn parse_primary_expression(&mut self) -> Rc { - if let Token::Identifier(id) = self.token.0.clone() { - let id_location = self.token_location(); - self.next(); - - /* - // EmbedExpression - if self.peek(Token::BlockOpen) && id == "embed" && self.previous_token.1.character_count() == "embed".len() { - return self.finish_embed_expression(id_location); - } - */ - - let id = Rc::new(Expression::QualifiedIdentifier(QualifiedIdentifier { - location: id_location.clone(), - attribute: false, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id((id, id_location.clone())), - })); - if self.peek(Token::ColonColon) { - self.push_location(&id_location.clone()); - let ql = self.pop_location(); - let id = self.finish_qualified_identifier(false, ql, id); - Rc::new(Expression::QualifiedIdentifier(id)) - } else { - id - } - } else if self.peek(Token::Null) { - self.mark_location(); - self.next(); - Rc::new(Expression::NullLiteral(NullLiteral { - location: self.pop_location(), - })) - } else if self.peek(Token::False) { - self.mark_location(); - self.next(); - Rc::new(Expression::BooleanLiteral(BooleanLiteral { - location: self.pop_location(), - value: false, - })) - } else if self.peek(Token::True) { - self.mark_location(); - self.next(); - Rc::new(Expression::BooleanLiteral(BooleanLiteral { - location: self.pop_location(), - value: true, - })) - } else if let Token::Number(n, suffix) = self.token.0.clone() { - self.mark_location(); - self.next(); - Rc::new(Expression::NumericLiteral(NumericLiteral { - location: self.pop_location(), - value: n, - suffix, - })) - } else if let Token::String(ref s) = self.token.0.clone() { - self.mark_location(); - self.next(); - Rc::new(Expression::StringLiteral(StringLiteral { - location: self.pop_location(), - value: s.clone(), - })) - } else if self.peek(Token::This) { - self.mark_location(); - self.next(); - Rc::new(Expression::ThisLiteral(ThisLiteral { - location: self.pop_location(), - })) - } else if self.peek(Token::Div) || self.peek(Token::DivideAssign) { - self.mark_location(); - self.token = self.tokenizer.scan_regexp_literal(self.token.1.clone(), if self.peek(Token::DivideAssign) { "=".into() } else { "".into() }); - let Token::RegExp { ref body, ref flags } = self.token.0.clone() else { - panic!(); - }; - self.next(); - Rc::new(Expression::RegExpLiteral(RegExpLiteral { - location: self.pop_location(), - body: body.clone(), flags: flags.clone(), - })) - // `@` - } else if self.peek(Token::Attribute) { - self.mark_location(); - let id = self.parse_qualified_identifier(); - Rc::new(Expression::QualifiedIdentifier(id)) - // Parentheses - } else if self.peek(Token::ParenOpen) { - return self.parse_paren_list_expr_or_qual_id(); - // XMLList, XMLElement, XMLMarkup - } else if self.peek(Token::Lt) { - if let Some(token) = self.tokenizer.scan_xml_markup(self.token_location()) { - self.token = token; - } - let start = self.token_location(); - if let Token::XmlMarkup(content) = &self.token.0.clone() { - self.mark_location(); - self.next(); - Rc::new(Expression::XmlMarkup(XmlMarkupExpression { - location: self.pop_location(), - markup: content.clone(), - })) - } else { - self.parse_xml_element_or_xml_list(start) - } - // ArrayInitializer - } else if self.peek(Token::SquareOpen) { - self.parse_array_initializer() - } else if self.peek(Token::BlockOpen) { - self.parse_object_initializer() - // QualifiedIdentifier - } else if - self.peek(Token::Times) - || self.peek(Token::Public) || self.peek(Token::Private) - || self.peek(Token::Protected) || self.peek(Token::Internal) { - let id = self.parse_qualified_identifier(); - Rc::new(Expression::QualifiedIdentifier(id)) - } else { - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingExpression, diagarg![self.token.0.clone()]); - self.create_invalidated_expression(&self.tokenizer.cursor_location()) - } - } - - /* - fn finish_embed_expression(&mut self, start: Location) -> Rc { - self.push_location(&start); - let descriptor = self.parse_object_initializer().clone(); - let Expression::ObjectInitializer(descriptor) = descriptor.as_ref() else { - panic!(); - }; - return Rc::new(Expression::Embed(EmbedExpression { - location: self.pop_location(), - description: descriptor.clone(), - })); - } - */ - - fn parse_array_initializer(&mut self) -> Rc { - self.mark_location(); - - let asdoc = self.parse_asdoc(); - - self.non_greedy_expect(Token::SquareOpen); - - let mut elements: Vec = vec![]; - - while !self.peek(Token::SquareClose) { - let mut ellipses = false; - while self.consume(Token::Comma) { - elements.push(Element::Elision); - ellipses = true; - } - if !ellipses { - if self.peek(Token::Ellipsis) { - self.mark_location(); - self.next(); - elements.push(Element::Rest((self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - }), self.pop_location()))); - } else { - elements.push(Element::Expression(self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - }))); - } - } - if !self.consume(Token::Comma) { - break; - } - } - self.non_greedy_expect(Token::SquareClose); - Rc::new(Expression::ArrayLiteral(ArrayLiteral { - location: self.pop_location(), - asdoc, - elements, - })) - } - - fn parse_xml_element_or_xml_list(&mut self, start: Location) -> Rc { - self.next_ie_xml_tag(); - if self.consume_and_ie_xml_content(Token::Gt) { - self.push_location(&start); - let content = self.parse_xml_content(); - self.non_greedy_expect_and_ie_xml_tag(Token::XmlLtSlash); - self.non_greedy_expect(Token::Gt); - return Rc::new(Expression::XmlList(XmlListExpression { - location: self.pop_location(), - content, - })); - } - - self.push_location(&start); - let element = Rc::new(self.parse_xml_element(start, true)); - return Rc::new(Expression::Xml(XmlExpression { - location: self.pop_location(), - element, - })); - } - - /// Parses XMLElement starting from its XMLTagContent. - fn parse_xml_element(&mut self, start: Location, ends_at_ie_div: bool) -> XmlElement { - self.push_location(&start); - let name = self.parse_xml_tag_name(); - let mut attributes: Vec> = vec![]; - let mut attribute_expression: Option> = None; - while self.consume_and_ie_xml_tag(Token::XmlWhitespace) { - if self.consume(Token::BlockOpen) { - let expr = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::AssignmentAndOther, ..default() }); - self.expect_and_ie_xml_tag(Token::BlockClose); - attribute_expression = Some(expr); - self.consume_and_ie_xml_tag(Token::XmlWhitespace); - break; - } else if matches!(self.token.0, Token::XmlName(_)) { - self.mark_location(); - let name = self.parse_xml_name(); - self.consume_and_ie_xml_tag(Token::XmlWhitespace); - self.non_greedy_expect_and_ie_xml_tag(Token::Assign); - let mut value = XmlAttributeValue::Value(("".into(), self.token.1.clone())); - if !self.expecting_token_error { - self.consume_and_ie_xml_tag(Token::XmlWhitespace); - if self.consume(Token::BlockOpen) { - let expr = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::AssignmentAndOther, ..default() }); - self.expect_and_ie_xml_tag(Token::BlockClose); - value = XmlAttributeValue::Expression(expr); - } else { - value = XmlAttributeValue::Value(self.parse_xml_attribute_value()); - } - } - attributes.push(Rc::new(XmlAttribute { - location: self.pop_location(), - name, value - })); - } else { - break; - } - } - - let mut content: Option>> = None; - let mut closing_name: Option = None; - - let is_empty; - - if ends_at_ie_div { - is_empty = self.consume(Token::XmlSlashGt); - } else { - is_empty = self.consume_and_ie_xml_content(Token::XmlSlashGt); - } - - if !is_empty { - self.expect_and_ie_xml_content(Token::Gt); - content = Some(self.parse_xml_content()); - self.non_greedy_expect_and_ie_xml_tag(Token::XmlLtSlash); - closing_name = Some(self.parse_xml_tag_name()); - self.consume_and_ie_xml_tag(Token::XmlWhitespace); - if ends_at_ie_div { - self.non_greedy_expect(Token::Gt); - } else { - self.non_greedy_expect_and_ie_xml_content(Token::Gt); - } - } - - XmlElement { - location: self.pop_location(), - name, - attributes, - attribute_expression, - content, - closing_name, - } - } - - fn parse_xml_attribute_value(&mut self) -> (String, Location) { - if let Token::XmlAttributeValue(value) = self.token.0.clone() { - let location = self.token_location(); - self.next_ie_xml_tag(); - return (value, location); - } else { - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingXmlAttributeValue, diagarg![self.token.0.clone()]); - ("".into(), self.tokenizer.cursor_location()) - } - } - - fn parse_xml_tag_name(&mut self) -> XmlTagName { - if self.consume(Token::BlockOpen) { - let expr = self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - }); - self.expect_and_ie_xml_tag(Token::BlockClose); - XmlTagName::Expression(expr) - } else { - XmlTagName::Name(self.parse_xml_name()) - } - } - - fn parse_xml_name(&mut self) -> (String, Location) { - if let Token::XmlName(name) = self.token.0.clone() { - let name_location = self.token_location(); - self.next_ie_xml_tag(); - return (name, name_location); - } else { - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingXmlName, diagarg![self.token.0.clone()]); - (INVALIDATED_IDENTIFIER.into(), self.tokenizer.cursor_location()) - } - } - - /// Parses XMLContent until a ` Vec> { - let mut content = vec![]; - while !self.peek(Token::XmlLtSlash) { - if self.consume(Token::BlockOpen) { - let expr = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::AssignmentAndOther, ..default() }); - self.expect_and_ie_xml_content(Token::BlockClose); - content.push(Rc::new(XmlContent::Expression(expr))); - } else if let Token::XmlMarkup(markup) = self.token.0.clone() { - let location = self.token_location(); - self.next_ie_xml_content(); - content.push(Rc::new(XmlContent::Markup((markup, location)))); - } else if let Token::XmlText(text) = self.token.0.clone() { - if self.tokenizer.characters().reached_end() { - self.expect_and_ie_xml_content(Token::XmlLtSlash); - break; - } - let location = self.token_location(); - self.next_ie_xml_content(); - content.push(Rc::new(XmlContent::Characters((text, location)))); - } else if self.consume_and_ie_xml_tag(Token::Lt) { - let start = self.token_location(); - let element = self.parse_xml_element(start, false); - content.push(Rc::new(XmlContent::Element(Rc::new(element)))); - } else if self.peek(Token::Eof) { - break; - } else { - self.expect_and_ie_xml_content(Token::XmlLtSlash); - } - } - content - } - - fn finish_paren_list_expr_or_qual_id(&mut self, start: Location, left: Rc) -> Rc { - if self.peek(Token::ColonColon) && !matches!(left.as_ref(), Expression::Sequence(_)) { - self.push_location(&start); - let ql = self.pop_location(); - let left = Rc::new(Expression::Paren(ParenExpression { - location: ql.clone(), - expression: left, - })); - let id = self.finish_qualified_identifier(false, ql, left); - return Rc::new(Expression::QualifiedIdentifier(id)); - } - self.push_location(&start); - return Rc::new(Expression::Paren(ParenExpression { - location: self.pop_location(), - expression: left, - })); - } - - /// Parses either a ParenListExpression, (), or a QualifiedIdentifier - fn parse_paren_list_expr_or_qual_id(&mut self) -> Rc { - let start = self.token_location(); - self.non_greedy_expect(Token::ParenOpen); - - let expr = self.parse_expression(ParserExpressionContext { - min_precedence: OperatorPrecedence::List, - allow_in: true, - ..default() - }); - - self.non_greedy_expect(Token::ParenClose); - self.finish_paren_list_expr_or_qual_id(start, expr) - } - - fn parse_opt_reserved_namespace(&mut self) -> Option> { - let loc = self.token.1.clone(); - if self.consume(Token::Public) { - Some(Rc::new(Expression::ReservedNamespace(ReservedNamespaceExpression::Public(loc)))) - } else if self.consume(Token::Private) { - Some(Rc::new(Expression::ReservedNamespace(ReservedNamespaceExpression::Private(loc)))) - } else if self.consume(Token::Protected) { - Some(Rc::new(Expression::ReservedNamespace(ReservedNamespaceExpression::Protected(loc)))) - } else if self.consume(Token::Internal) { - Some(Rc::new(Expression::ReservedNamespace(ReservedNamespaceExpression::Internal(loc)))) - } else { - None - } - } - - fn parse_qualified_identifier(&mut self) -> QualifiedIdentifier { - self.mark_location(); - - let attribute = self.consume(Token::Attribute); - if attribute && self.peek(Token::SquareOpen) { - let brackets = self.parse_brackets(); - return QualifiedIdentifier { - location: self.pop_location(), - attribute, - qualifier: None, - id: QualifiedIdentifierIdentifier::Brackets(brackets), - }; - } - - // public, private, protected, internal - if let Some(qual) = self.parse_opt_reserved_namespace() { - if self.peek(Token::ColonColon) { - let ql = self.pop_location(); - return self.finish_qualified_identifier(attribute, ql, qual); - } else { - let id = QualifiedIdentifier { - location: self.pop_location(), - attribute, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id((qual.to_reserved_namespace_string().unwrap(), qual.location())), - }; - return id; - } - } - - let mut id: Option = None; - - // IdentifierName - if let Token::Identifier(id_1) = self.token.0.clone() { - id = Some(id_1); - } else { - if let Some(id_1) = self.token.0.reserved_word_name() { - id = Some(id_1); - } else if self.peek(Token::Times) { - id = Some("*".to_owned()); - } - } - - if let Some(id) = id { - let id_location = self.token_location(); - self.next(); - if self.peek(Token::ColonColon) { - let id = QualifiedIdentifier { - location: id_location.clone(), - attribute: false, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id((id, id_location.clone())), - }; - let id = Rc::new(Expression::QualifiedIdentifier(id)); - let ql = self.pop_location(); - return self.finish_qualified_identifier(attribute, ql, id); - } else { - let id = QualifiedIdentifier { - location: self.pop_location(), - attribute, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id((id, id_location.clone())), - }; - return id; - } - } - - // (q)::x - if self.peek(Token::ParenOpen) { - let qual = self.parse_paren_expression(); - let ql = self.pop_location(); - let qual = Rc::new(Expression::Paren(ParenExpression { - location: ql.clone(), - expression: qual, - })); - return self.finish_qualified_identifier(attribute, ql, qual); - } - - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![self.token.0.clone()]); - QualifiedIdentifier { - location: self.pop_location(), - attribute: false, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id(("".into(), self.tokenizer.cursor_location())), - } - } - - fn parse_non_attribute_qualified_identifier(&mut self) -> QualifiedIdentifier { - self.mark_location(); - - let attribute = false; - - // public, private, protected, internal - if let Some(qual) = self.parse_opt_reserved_namespace() { - if self.peek(Token::ColonColon) { - let ql = self.pop_location(); - return self.finish_qualified_identifier(attribute, ql, qual); - } else { - let id = QualifiedIdentifier { - location: self.pop_location(), - attribute, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id((qual.to_reserved_namespace_string().unwrap(), qual.location())), - }; - return id; - } - } - - let mut id: Option = None; - - // IdentifierName - if let Token::Identifier(id_1) = self.token.0.clone() { - id = Some(id_1); - } else { - if let Some(id_1) = self.token.0.reserved_word_name() { - id = Some(id_1); - } else if self.peek(Token::Times) { - id = Some("*".to_owned()); - } - } - - if let Some(id) = id { - let id_location = self.token_location(); - self.next(); - if self.peek(Token::ColonColon) { - let id = QualifiedIdentifier { - location: id_location.clone(), - attribute: false, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id((id, id_location.clone())), - }; - let id = Rc::new(Expression::QualifiedIdentifier(id)); - let ql = self.pop_location(); - return self.finish_qualified_identifier(attribute, ql, id); - } else { - let id = QualifiedIdentifier { - location: self.pop_location(), - attribute, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id((id, id_location.clone())), - }; - return id; - } - } - - // (q)::x - if self.peek(Token::ParenOpen) { - let qual = self.parse_paren_expression(); - let ql = self.pop_location(); - let qual = Rc::new(Expression::Paren(ParenExpression { - location: ql.clone(), - expression: qual, - })); - return self.finish_qualified_identifier(attribute, ql, qual); - } - - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![self.token.0.clone()]); - QualifiedIdentifier { - location: self.pop_location(), - attribute: false, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id(("".into(), self.tokenizer.cursor_location())), - } - } - - /// Expects a colon-colon and finishes a qualified identifier. - fn finish_qualified_identifier(&mut self, attribute: bool, start_location: Location, qual: Rc) -> QualifiedIdentifier { - self.push_location(&start_location); - self.non_greedy_expect(Token::ColonColon); - - // `::` may be followed by one of { IdentifierName, `*`, Brackets } - - // IdentifierName - if let Some(id) = self.consume_identifier(true) { - QualifiedIdentifier { - location: self.pop_location(), - attribute, - qualifier: Some(qual), - id: QualifiedIdentifierIdentifier::Id(id), - } - // `*` - } else if self.peek(Token::Times) { - let id_location = self.token_location(); - self.next(); - QualifiedIdentifier { - location: self.pop_location(), - attribute, - qualifier: Some(qual), - id: QualifiedIdentifierIdentifier::Id(("*".into(), id_location)), - } - // Brackets - } else if self.peek(Token::SquareOpen) { - let brackets = self.parse_brackets(); - QualifiedIdentifier { - location: self.pop_location(), - attribute, - qualifier: Some(qual), - id: QualifiedIdentifierIdentifier::Brackets(brackets), - } - } else { - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![self.token.0.clone()]); - QualifiedIdentifier { - location: self.pop_location(), - attribute, - qualifier: Some(qual), - id: QualifiedIdentifierIdentifier::Id(("".into(), self.tokenizer.cursor_location())), - } - } - } - - fn parse_brackets(&mut self) -> Rc { - self.non_greedy_expect(Token::SquareOpen); - let expr = self.parse_expression(ParserExpressionContext { - min_precedence: OperatorPrecedence::List, - allow_in: true, - ..default() - }); - self.non_greedy_expect(Token::SquareClose); - expr - } - - fn parse_paren_expression(&mut self) -> Rc { - self.non_greedy_expect(Token::ParenOpen); - let expr = self.parse_expression(ParserExpressionContext { - min_precedence: OperatorPrecedence::AssignmentAndOther, - allow_in: true, - ..default() - }); - self.non_greedy_expect(Token::ParenClose); - expr - } - - fn parse_paren_list_expression(&mut self) -> Rc { - self.non_greedy_expect(Token::ParenOpen); - let expr = self.parse_expression(ParserExpressionContext { - min_precedence: OperatorPrecedence::List, - allow_in: true, - ..default() - }); - self.non_greedy_expect(Token::ParenClose); - expr - } - - fn parse_typed_destructuring(&mut self) -> TypedDestructuring { - self.mark_location(); - let mut destructuring: Rc; - if self.peek(Token::BlockOpen) { - destructuring = self.parse_object_initializer(); - } else if self.peek(Token::SquareOpen) { - destructuring = self.parse_array_initializer(); - } else { - let id = self.expect_identifier(true); - let id = QualifiedIdentifier { - location: id.1.clone(), - attribute: false, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id(id.clone()), - }; - destructuring = Rc::new(Expression::QualifiedIdentifier(id)); - } - if self.consume(Token::Exclamation) { - self.push_location(&destructuring.location()); - destructuring = Rc::new(Expression::Unary(UnaryExpression { - location: self.pop_location(), - operator: Operator::NonNull, - expression: destructuring.clone(), - })); - } - if !destructuring.is_valid_destructuring() { - self.add_syntax_error(&destructuring.location(), DiagnosticKind::MalformedDestructuring, vec![]) - } - let type_annotation = if self.consume(Token::Colon) { Some(self.parse_type_expression()) } else { None }; - TypedDestructuring { - location: self.pop_location(), - destructuring, - type_annotation, - } - } - - pub fn parse_type_expression(&mut self) -> Rc { - let start = self.token_location(); - let (mut base, wrap_nullable) = self.parse_type_expression_start(); - - loop { - if self.consume(Token::Dot) { - base = self.parse_dot_subexpression(base); - } else if self.consume(Token::Question) { - self.push_location(&base.location()); - base = Rc::new(Expression::NullableType(NullableTypeExpression { - location: self.pop_location(), - base, - })); - } else if self.consume(Token::Exclamation) { - self.push_location(&base.location()); - base = Rc::new(Expression::NonNullableType(NonNullableTypeExpression { - location: self.pop_location(), - base, - })); - } else { - break; - } - } - - if wrap_nullable { - self.push_location(&start); - base = Rc::new(Expression::NullableType(NullableTypeExpression { - location: self.pop_location(), - base, - })); - } - - base - } - - fn parse_type_expression_start(&mut self) -> (Rc, bool) { - // Allow a `?` prefix to wrap a type into nullable. - let wrap_nullable = self.consume(Token::Question); - - // Parenthesized - if self.peek(Token::ParenOpen) { - self.mark_location(); - let expression = self.parse_type_expression(); - (Rc::new(Expression::Paren(ParenExpression { - location: self.pop_location(), - expression, - })), wrap_nullable) - } - // `function` - else if self.peek(Token::Function) { - (self.parse_function_type_expression(), wrap_nullable) - // `void` - } else if self.peek(Token::Void) { - self.mark_location(); - self.next(); - (Rc::new(Expression::VoidType(VoidTypeExpression { - location: self.pop_location(), - })), wrap_nullable) - // [T] - // [T1, T2, ...Tn] - } else if self.peek(Token::SquareOpen) { - let mut elements = vec![]; - self.mark_location(); - self.next(); - elements.push(self.parse_type_expression()); - if self.consume(Token::SquareClose) { - (Rc::new(Expression::ArrayType(ArrayTypeExpression { - location: self.pop_location(), - expression: elements[0].clone(), - })), wrap_nullable) - } else { - self.non_greedy_expect(Token::Comma); - elements.push(self.parse_type_expression()); - while self.consume(Token::Comma) { - if self.peek(Token::SquareClose) { - break; - } - elements.push(self.parse_type_expression()); - } - self.non_greedy_expect(Token::SquareClose); - (Rc::new(Expression::TupleType(TupleTypeExpression { - location: self.pop_location(), - expressions: elements, - })), wrap_nullable) - } - } else if self.peek(Token::Times) { - let location = self.token_location(); - self.next(); - (Rc::new(Expression::AnyType(AnyTypeExpression { - location, - })), wrap_nullable) - // Identifier - } else { - let id = self.parse_qualified_identifier(); - (Rc::new(Expression::QualifiedIdentifier(id)), wrap_nullable) - } - } - - fn parse_function_type_expression(&mut self) -> Rc { - self.mark_location(); - self.next(); - - let mut parameters = vec![]; - self.non_greedy_expect(Token::ParenOpen); - if !self.expecting_token_error { - if !self.peek(Token::ParenClose) { - parameters.push(self.parse_function_type_parameter()); - while self.consume(Token::Comma) { - parameters.push(self.parse_function_type_parameter()); - } - } - self.non_greedy_expect(Token::ParenClose); - self.validate_parameter_list(parameters.iter().map(|p| (p.kind, p.location.clone())).collect::>()); - } - - let mut result_type = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - self.non_greedy_expect(Token::Colon); - if !self.expecting_token_error { - result_type = self.parse_type_expression(); - } - Rc::new(Expression::FunctionType(FunctionTypeExpression { - location: self.pop_location(), - parameters, - result_type: Some(result_type), - })) - } - - fn parse_function_type_parameter(&mut self) -> Rc { - self.mark_location(); - let rest = self.consume(Token::Ellipsis); - let type_expression: Option> = if rest && self.peek(Token::ParenClose) { - None - } else { - Some(self.parse_type_expression()) - }; - let optional = !rest && self.consume(Token::Assign); - let location = self.pop_location(); - Rc::new(FunctionTypeParameter { - location, - type_expression, - kind: if rest { - ParameterKind::Rest - } else if optional { - ParameterKind::Optional - } else { - ParameterKind::Required - }, - }) - } - - fn parse_variable_binding(&mut self, allow_in: bool) -> VariableBinding { - let destructuring = self.parse_typed_destructuring(); - let initializer = if self.consume(Token::Assign) { - Some(self.parse_expression(ParserExpressionContext { - allow_in, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - })) - } else { - None - }; - VariableBinding { - destructuring, - initializer, - } - } - - fn parse_semicolon(&mut self) -> bool { - self.consume(Token::Semicolon) || self.peek(Token::BlockClose) || self.previous_token.1.line_break(&self.token.1) - } - - fn parse_substatement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - self.parse_statement(context) - } - - fn parse_statement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - // ExpressionStatement or LabeledStatement - if let Token::Identifier(id) = &self.token.0.clone() { - let id = (id.clone(), self.token_location()); - self.next(); - self.parse_statement_starting_with_identifier(context, id) - // SuperStatement or ExpressionStatement with `super` - } else if self.peek(Token::Super) { - self.mark_location(); - self.next(); - let arguments = if self.peek(Token::ParenOpen) { Some(self.parse_arguments()) } else { None }; - let mut semicolon = false; - if arguments.is_some() { - semicolon = self.parse_semicolon(); - } - if !semicolon && (self.peek(Token::Dot) || self.peek(Token::SquareOpen)) { - if !(self.peek(Token::Dot) || self.peek(Token::SquareOpen)) { - self.non_greedy_expect(Token::Dot); - } - self.duplicate_location(); - // ExpressionStatement (`super`...) - let mut expr = Rc::new(Expression::Super(SuperExpression { - location: self.pop_location(), - object: arguments, - })); - expr = self.parse_subexpressions(expr, ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::List, - ..default() - }); - let semicolon = self.parse_semicolon(); - (Rc::new(Directive::ExpressionStatement(ExpressionStatement { - location: self.pop_location(), - expression: expr, - })), semicolon) - } else { - // SuperStatement - let node = Rc::new(Directive::SuperStatement(SuperStatement { - location: self.pop_location(), - arguments: arguments.unwrap(), - })); - - // Check whether super statement is allowed here - let allowed_here; - if context.may_contain_super_statement() { - allowed_here = !context.super_statement_found(); - context.set_super_statement_found(true); - } else { - allowed_here = false; - } - - if !allowed_here { - self.add_syntax_error(&node.location(), DiagnosticKind::NotAllowedHere, diagarg![Token::Super]); - } - - (node, semicolon) - } - // EmptyStatement - } else if self.peek(Token::Semicolon) { - self.mark_location(); - self.next(); - (Rc::new(Directive::EmptyStatement(EmptyStatement { - location: self.pop_location(), - })), true) - // Block - } else if self.peek(Token::BlockOpen) { - let context = if context.is_top_level_or_package() || context.is_type_block() { - context.clone() - } else { - context.override_control_context(true, ParserControlFlowContext { - breakable: true, - iteration: false, - }) - }; - let block = self.parse_block(context); - (Rc::new(Directive::Block(block)), true) - // IfStatement - } else if self.peek(Token::If) { - self.parse_if_statement(context) - // SwitchStatement - // `switch type` - } else if self.peek(Token::Switch) { - self.parse_switch_statement(context) - // DoStatement - } else if self.peek(Token::Do) { - self.parse_do_statement(context) - // WhileStatement - } else if self.peek(Token::While) { - self.parse_while_statement(context) - // ForStatement - // `for..in` - // `for each` - } else if self.peek(Token::For) { - self.parse_for_statement(context) - // WithStatement - } else if self.peek(Token::With) { - self.parse_with_statement(context) - // BreakStatement - } else if self.peek(Token::Break) { - self.parse_break_statement(context) - // ContinueStatement - } else if self.peek(Token::Continue) { - self.parse_continue_statement(context) - // ReturnStatement - } else if self.peek(Token::Return) { - self.parse_return_statement(context) - // ThrowStatement - } else if self.peek(Token::Throw) { - self.parse_throw_statement(context) - // TryStatement - } else if self.peek(Token::Try) { - self.parse_try_statement(context) - // `default xml namespace = expression` - } else if self.peek(Token::Default) { - self.parse_default_xml_namespace_statement() - // ExpressionStatement - } else { - self.mark_location(); - - // Store offset for patching error - let i = self.tokenizer.characters().index(); - - let exp = self.parse_expression(ParserExpressionContext { - allow_in: true, min_precedence: OperatorPrecedence::List, ..default() - }); - - // Patch error - if i == self.tokenizer.characters().index() { - self.patch_syntax_error(DiagnosticKind::ExpectingExpression, DiagnosticKind::ExpectingStatement, diagarg![self.token.0.clone()]); - } - - let semicolon = if exp.is_invalidated() { - self.next(); - true - } else { self.parse_semicolon() }; - (Rc::new(Directive::ExpressionStatement(ExpressionStatement { - location: self.pop_location(), - expression: exp, - })), semicolon) - } - } - - fn parse_statement_starting_with_identifier(&mut self, context: ParserDirectiveContext, id: (String, Location)) -> (Rc, bool) { - self.push_location(&id.1); - let id_location = id.1.clone(); - - // LabeledStatement - if self.consume(Token::Colon) { - let (substatement, semicolon) = self.parse_substatement(context.put_label(id.0.clone())); - let labeled = Rc::new(Directive::LabeledStatement(LabeledStatement { - location: self.pop_location(), - label: id.clone(), - substatement, - })); - return (labeled, semicolon); - } - - let mut exp: Rc; - - /* - // EmbedExpression - if self.peek(Token::BlockOpen) && id.0 == "embed" && self.previous_token.1.character_count() == "embed".len() { - exp = self.finish_embed_expression(id_location); - } else { - */ - { - let id = Rc::new(Expression::QualifiedIdentifier(QualifiedIdentifier { - location: id_location.clone(), - attribute: false, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id(id.clone()), - })); - if self.peek(Token::ColonColon) { - self.push_location(&id_location.clone()); - let ql = self.pop_location(); - let id = self.finish_qualified_identifier(false, ql, id); - exp = Rc::new(Expression::QualifiedIdentifier(id)); - } else { - exp = id; - } - } - - exp = self.parse_subexpressions(exp, ParserExpressionContext { - allow_in: true, min_precedence: OperatorPrecedence::List, ..default() - }); - let semicolon = self.parse_semicolon(); - (Rc::new(Directive::ExpressionStatement(ExpressionStatement { - location: self.pop_location(), - expression: exp, - })), semicolon) - } - - fn parse_qualified_identifier_statement_or_normal_config(&mut self, context: ParserDirectiveContext, id: (String, Location), asdoc: Option>) -> (Rc, bool) { - self.push_location(&id.1); - let id_location = id.1.clone(); - let id = Rc::new(Expression::QualifiedIdentifier(QualifiedIdentifier { - location: id_location.clone(), - attribute: false, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id(id.clone()), - })); - self.push_location(&id_location.clone()); - let ql = self.pop_location(); - let id = self.finish_qualified_identifier(false, ql, id); - let mut exp = Rc::new(Expression::QualifiedIdentifier(id)); - exp = self.parse_subexpressions(exp, ParserExpressionContext { - allow_in: true, min_precedence: OperatorPrecedence::List, ..default() - }); - - // Parse CONFIG::VAR_NAME - if let Some(result) = self.parse_opt_normal_config(&exp, asdoc.clone(), context.clone()) { - return result; - } - - let semicolon = self.parse_semicolon(); - (Rc::new(Directive::ExpressionStatement(ExpressionStatement { - location: self.pop_location(), - expression: exp, - })), semicolon) - } - - fn parse_opt_normal_config(&mut self, exp: &Rc, asdoc: Option>, context: ParserDirectiveContext) -> Option<(Rc, bool)> { - if self.peek_annotatable_directive_identifier_name() { - match exp.to_normal_configuration_identifier(self) { - Ok(Some((q, constant_name, metadata))) => { - self.push_location(&exp.location()); - let mut context = AnnotatableContext { - start_location: exp.location(), - asdoc: self.parse_asdoc().or(asdoc), - attributes: metadata, - context, - directive_context_keyword: None, - }; - self.parse_attribute_keywords_or_expressions(&mut context); - let (directive, semicolon) = self.parse_annotatable_directive(context); - return Some((Rc::new(Directive::NormalConfigurationDirective(NormalConfigurationDirective { - location: self.pop_location(), - namespace: q, - constant_name, - directive, - })), semicolon)); - }, - Ok(None) => {}, - Err(MetadataRefineError1(MetadataRefineError::Syntax, loc)) => { - self.add_syntax_error(&loc, DiagnosticKind::UnrecognizedMetadataSyntax, diagarg![]); - }, - } - } - if self.peek(Token::BlockOpen) { - if let Some((q, constant_name)) = exp.to_normal_configuration_identifier_no_metadata() { - self.push_location(&exp.location()); - let block = self.parse_block(context); - return Some((Rc::new(Directive::NormalConfigurationDirective(NormalConfigurationDirective { - location: self.pop_location(), - namespace: q, - constant_name, - directive: Rc::new(Directive::Block(block)), - })), true)); - } - } - None - } - - fn parse_block(&mut self, context: ParserDirectiveContext) -> Block { - self.mark_location(); - self.non_greedy_expect(Token::BlockOpen); - let mut directives = vec![]; - if !self.expecting_token_error { - let mut semicolon = false; - while !self.peek(Token::BlockClose) && !self.peek(Token::Eof) { - if !directives.is_empty() && !semicolon { - self.non_greedy_expect_virtual_semicolon(); - } - let (directive, semicolon_1) = self.parse_directive(context.clone()); - directives.push(directive); - semicolon = semicolon_1; - } - self.non_greedy_expect(Token::BlockClose); - } - Block { - location: self.pop_location(), - directives, - } - } - - fn parse_if_statement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - let context = context.override_control_context(true, ParserControlFlowContext { - breakable: true, - iteration: false, - }); - self.mark_location(); - self.next(); - let mut test = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - let mut consequent: Rc = self.create_invalidated_directive(&self.tokenizer.cursor_location()); - let mut alternative: Option> = None; - let semicolon; - self.non_greedy_expect(Token::ParenOpen); - if self.expecting_token_error { - semicolon = self.parse_semicolon(); - } else { - test = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() }); - consequent = self.create_invalidated_directive(&self.tokenizer.cursor_location()); - self.non_greedy_expect(Token::ParenClose); - if self.expecting_token_error { - semicolon = self.parse_semicolon(); - } else { - let (consequent_1, semicolon_1) = self.parse_substatement(context.clone()); - consequent = consequent_1; - if self.peek(Token::Else) { - if !semicolon_1 { - self.non_greedy_expect_virtual_semicolon(); - } - self.next(); - let (alternative_2, semicolon_2) = self.parse_substatement(context.clone()); - alternative = Some(alternative_2); - semicolon = semicolon_2; - } else { - semicolon = semicolon_1; - } - } - } - (Rc::new(Directive::IfStatement(IfStatement { - location: self.pop_location(), - test, consequent, alternative, - })), semicolon) - } - - fn parse_switch_statement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - self.mark_location(); - self.next(); - if self.peek_context_keyword("type") { - self.forbid_line_break_before_token(); - self.next(); - return self.parse_switch_type_statement(context); - } - let context = context.override_control_context(false, ParserControlFlowContext { - breakable: true, - iteration: false, - }); - let mut discriminant = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - let mut cases: Vec = vec![]; - self.non_greedy_expect(Token::ParenOpen); - if !self.expecting_token_error { - discriminant = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() }); - self.non_greedy_expect(Token::ParenClose); - if !self.expecting_token_error { - self.non_greedy_expect(Token::BlockOpen); - if !self.expecting_token_error { - cases = self.parse_case_elements(context); - self.non_greedy_expect(Token::BlockClose); - } - } - } - (Rc::new(Directive::SwitchStatement(SwitchStatement { - location: self.pop_location(), - discriminant, cases, - })), true) - } - - fn parse_case_elements(&mut self, context: ParserDirectiveContext) -> Vec { - let mut cases = vec![]; - let mut semicolon = false; - while !self.peek(Token::BlockClose) { - if !cases.is_empty() && !semicolon { - self.non_greedy_expect_virtual_semicolon(); - } - if !(self.peek(Token::Case) || self.peek(Token::Default)) { - break; - } - self.mark_location(); - let mut labels = vec![]; - loop { - if self.peek(Token::Case) { - self.mark_location(); - self.next(); - let exp = self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::List, - ..default() - }); - self.non_greedy_expect(Token::Colon); - labels.push(CaseLabel::Case((exp, self.pop_location()))); - } else if self.peek(Token::Default) { - self.mark_location(); - self.next(); - self.non_greedy_expect(Token::Colon); - labels.push(CaseLabel::Default(self.pop_location())); - } else { - break; - } - } - let mut directives = vec![]; - semicolon = false; - while !(self.peek(Token::BlockClose) || self.peek(Token::Case) || self.peek(Token::Default)) { - if !directives.is_empty() && !semicolon { - self.non_greedy_expect_virtual_semicolon(); - } - let (directive, semicolon_1) = self.parse_directive(context.clone()); - directives.push(directive); - semicolon = semicolon_1; - } - cases.push(Case { - location: self.pop_location(), - labels, - directives, - }); - } - cases - } - - fn parse_switch_type_statement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - let context = context.override_control_context(true, ParserControlFlowContext { - breakable: true, - iteration: false, - }); - let mut discriminant = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - let mut cases: Vec = vec![]; - self.non_greedy_expect(Token::ParenOpen); - if !self.expecting_token_error { - discriminant = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() }); - self.non_greedy_expect(Token::ParenClose); - if !self.expecting_token_error { - self.non_greedy_expect(Token::BlockOpen); - if !self.expecting_token_error { - cases = self.parse_type_case_elements(context); - self.non_greedy_expect(Token::BlockClose); - } - } - } - (Rc::new(Directive::SwitchTypeStatement(SwitchTypeStatement { - location: self.pop_location(), - discriminant, cases, - })), true) - } - - fn parse_type_case_elements(&mut self, context: ParserDirectiveContext) -> Vec { - let mut cases = vec![]; - while !self.peek(Token::BlockClose) && !self.peek(Token::Eof) { - if self.peek(Token::Default) { - self.mark_location(); - self.next(); - let block = Rc::new(self.parse_block(context.clone())); - cases.push(TypeCase { - location: self.pop_location(), - parameter: None, - block, - }); - } else { - self.mark_location(); - self.non_greedy_expect(Token::Case); - if !self.expecting_token_error { - self.non_greedy_expect(Token::ParenOpen); - if !self.expecting_token_error { - let parameter = Some(self.parse_typed_destructuring()); - self.non_greedy_expect(Token::ParenClose); - if !self.expecting_token_error { - let block = Rc::new(self.parse_block(context.clone())); - cases.push(TypeCase { - location: self.pop_location(), - parameter, - block, - }); - } - } - } - } - } - cases - } - - fn parse_do_statement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - let context = context.override_control_context(false, ParserControlFlowContext { - breakable: true, - iteration: true, - }); - self.mark_location(); - self.next(); - - // Body - let (body, semicolon_1) = self.parse_substatement(context); - if !semicolon_1 { - self.non_greedy_expect_virtual_semicolon(); - } - - let mut test = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - self.non_greedy_expect(Token::While); - if !self.expecting_token_error { - test = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - self.non_greedy_expect(Token::ParenOpen); - if !self.expecting_token_error { - test = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() }); - self.non_greedy_expect(Token::ParenClose); - } - } - - let semicolon = self.parse_semicolon(); - (Rc::new(Directive::DoStatement(DoStatement { - location: self.pop_location(), - body, test, - })), semicolon) - } - - fn parse_while_statement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - let context = context.override_control_context(false, ParserControlFlowContext { - breakable: true, - iteration: true, - }); - self.mark_location(); - self.next(); - - // Test - let mut test = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - let mut body = self.create_invalidated_directive(&self.tokenizer.cursor_location()); - let semicolon: bool; - self.non_greedy_expect(Token::ParenOpen); - if !self.expecting_token_error { - test = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() }); - body = self.create_invalidated_directive(&self.tokenizer.cursor_location()); - self.non_greedy_expect(Token::ParenClose); - if !self.expecting_token_error { - let (body_1, semicolon_1) = self.parse_substatement(context); - body = body_1; - semicolon = semicolon_1; - } else { - semicolon = self.parse_semicolon(); - } - } else { - semicolon = self.parse_semicolon(); - } - - (Rc::new(Directive::WhileStatement(WhileStatement { - location: self.pop_location(), - test, body, - })), semicolon) - } - - /// Parses `for`, `for..in` or `for each`. - fn parse_for_statement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - let context = context.override_control_context(false, ParserControlFlowContext { - breakable: true, - iteration: true, - }); - self.mark_location(); - self.next(); - - // `for each` - if self.peek_context_keyword("each") { - self.forbid_line_break_before_token(); - self.next(); - return self.parse_for_each_statement(context); - } - - self.non_greedy_expect(Token::ParenOpen); - if self.expecting_token_error { - let body = self.create_invalidated_directive(&self.tokenizer.cursor_location()); - let semicolon = self.parse_semicolon(); - return (Rc::new(Directive::ForStatement(ForStatement { - location: self.pop_location(), - init: None, test: None, update: None, body, - })), semicolon); - } - - let init_variable = if self.peek(Token::Var) || self.peek(Token::Const) { - Some(self.parse_simple_variable_definition(false)) - } else { - None - }; - - if init_variable.is_some() && self.consume(Token::In) { - return self.parse_for_in_statement_with_left_variable(context, init_variable.unwrap()); - } - - let mut init_exp = if init_variable.is_none() && !self.peek(Token::Semicolon) { - self.parse_opt_expression(ParserExpressionContext { - allow_in: false, - min_precedence: OperatorPrecedence::Postfix, - ..default() - }) - } else { - None - }; - - if init_exp.is_some() && self.consume(Token::In) { - return self.parse_for_in_statement_with_left_exp(context, init_exp.unwrap()); - } - - if init_exp.is_none() && init_variable.is_none() && !self.peek(Token::Semicolon) { - init_exp = Some(self.parse_expression(ParserExpressionContext { - allow_in: false, min_precedence: OperatorPrecedence::List, ..default() - })); - } else if let Some(exp) = init_exp.as_ref() { - init_exp = Some(self.parse_subexpressions(exp.clone(), ParserExpressionContext { - allow_in: false, min_precedence: OperatorPrecedence::List, ..default() - })); - } - - let init = if let Some(exp) = init_exp.as_ref() { - Some(ForInitializer::Expression(exp.clone())) - } else if let Some(variable) = init_variable.as_ref() { - Some(ForInitializer::VariableDefinition(Rc::new(variable.clone()))) - } else { - None - }; - - self.non_greedy_expect(Token::Semicolon); - let test = if self.peek(Token::Semicolon) { - None - } else { - Some(self.parse_expression(ParserExpressionContext { - allow_in: true, min_precedence: OperatorPrecedence::List, ..default() - })) - }; - self.non_greedy_expect(Token::Semicolon); - let update = if self.peek(Token::ParenClose) { - None - } else { - Some(self.parse_expression(ParserExpressionContext { - allow_in: true, min_precedence: OperatorPrecedence::List, ..default() - })) - }; - self.non_greedy_expect(Token::ParenClose); - - // Body - let (body, semicolon) = self.parse_substatement(context); - - (Rc::new(Directive::ForStatement(ForStatement { - location: self.pop_location(), - init, test, update, body, - })), semicolon) - } - - fn parse_for_each_statement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - self.non_greedy_expect(Token::ParenOpen); - if self.expecting_token_error { - let left = ForInBinding::Expression(self.create_invalidated_expression(&self.tokenizer.cursor_location())); - let right = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - let body = self.create_invalidated_directive(&self.tokenizer.cursor_location()); - let semicolon = self.parse_semicolon(); - return (Rc::new(Directive::ForInStatement(ForInStatement { - location: self.pop_location(), - each: true, left, right, body, - })), semicolon); - } - - let left = if self.peek(Token::Var) || self.peek(Token::Const) { - self.mark_location(); - let kind = (if self.peek(Token::Var) { VariableDefinitionKind::Var } else { VariableDefinitionKind::Const }, self.token_location()); - self.next(); - let binding = self.parse_variable_binding(false); - if let Some(init) = &binding.initializer { - self.add_syntax_error(&init.location(), DiagnosticKind::IllegalForInInitializer, vec![]); - } - ForInBinding::VariableDefinition(Rc::new(SimpleVariableDefinition { - location: self.pop_location(), - kind, - bindings: vec![Rc::new(binding)], - })) - } else { - ForInBinding::Expression(self.parse_expression(ParserExpressionContext { - allow_in: false, min_precedence: OperatorPrecedence::Postfix, ..default() - })) - }; - let mut right = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - self.non_greedy_expect(Token::In); - if !self.expecting_token_error { - right = self.parse_expression(ParserExpressionContext { - allow_in: true, min_precedence: OperatorPrecedence::List, ..default() - }); - } - self.non_greedy_expect(Token::ParenClose); - - // Body - let (body, semicolon) = self.parse_substatement(context); - - (Rc::new(Directive::ForInStatement(ForInStatement { - location: self.pop_location(), - each: true, left, right, body, - })), semicolon) - } - - fn parse_for_in_statement_with_left_variable(&mut self, context: ParserDirectiveContext, left: SimpleVariableDefinition) -> (Rc, bool) { - let variable_binding = left.bindings[0].clone(); - - if let Some(init) = &variable_binding.initializer { - self.add_syntax_error(&init.location(), DiagnosticKind::IllegalForInInitializer, vec![]); - } - - if left.bindings.len() > 1 { - self.add_syntax_error(&left.kind.1.clone(), DiagnosticKind::MultipleForInBindings, vec![]); - } - - let right = self.parse_expression(ParserExpressionContext { - allow_in: true, min_precedence: OperatorPrecedence::List, ..default() - }); - self.non_greedy_expect(Token::ParenClose); - - // Body - let (body, semicolon) = self.parse_substatement(context); - - (Rc::new(Directive::ForInStatement(ForInStatement { - location: self.pop_location(), - each: false, left: ForInBinding::VariableDefinition(Rc::new(left)), right, body, - })), semicolon) - } - - fn parse_for_in_statement_with_left_exp(&mut self, context: ParserDirectiveContext, left: Rc) -> (Rc, bool) { - let right = self.parse_expression(ParserExpressionContext { - allow_in: true, min_precedence: OperatorPrecedence::List, ..default() - }); - self.non_greedy_expect(Token::ParenClose); - - // Body - let (body, semicolon) = self.parse_substatement(context); - - (Rc::new(Directive::ForInStatement(ForInStatement { - location: self.pop_location(), - each: false, left: ForInBinding::Expression(left), right, body, - })), semicolon) - } - - fn parse_simple_variable_definition(&mut self, allow_in: bool) -> SimpleVariableDefinition { - self.mark_location(); - let kind: VariableDefinitionKind; - let kind_location = self.token_location(); - if self.consume(Token::Const) { - kind = VariableDefinitionKind::Const; - } else { - self.non_greedy_expect(Token::Var); - kind = VariableDefinitionKind::Var; - } - let mut bindings = vec![Rc::new(self.parse_variable_binding(allow_in))]; - while self.consume(Token::Comma) { - bindings.push(Rc::new(self.parse_variable_binding(allow_in))); - } - SimpleVariableDefinition { - location: self.pop_location(), - kind: (kind, kind_location), - bindings, - } - } - - fn parse_with_statement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - let context = context.override_control_context(true, ParserControlFlowContext { - breakable: true, - iteration: false, - }); - self.mark_location(); - self.next(); - - let mut object = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - self.non_greedy_expect(Token::ParenOpen); - if !self.expecting_token_error { - object = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() }); - } - self.non_greedy_expect(Token::ParenClose); - - // Body - let (body, semicolon) = self.parse_substatement(context); - - (Rc::new(Directive::WithStatement(WithStatement { - location: self.pop_location(), - object, body, - })), semicolon) - } - - fn parse_break_statement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - self.mark_location(); - self.next(); - - let label = if self.previous_token.1.line_break(&self.token.1) { None } else { self.consume_identifier(false) }; - let label_location = label.clone().map(|label| label.1.clone()); - let label = label.map(|label| label.0.clone()); - - let semicolon = self.parse_semicolon(); - - let node = Rc::new(Directive::BreakStatement(BreakStatement { - location: self.pop_location(), - label: label.clone().map(|l| (l.clone(), label_location.clone().unwrap())), - })); - - if label.is_some() && !context.is_label_defined(label.clone().unwrap()) { - self.add_syntax_error(&label_location.unwrap(), DiagnosticKind::UndefinedLabel, diagarg![label.clone().unwrap()]); - } else if !context.is_break_allowed(label) { - self.add_syntax_error(&node.location(), DiagnosticKind::IllegalBreak, vec![]); - } - - (node, semicolon) - } - - fn parse_continue_statement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - self.mark_location(); - self.next(); - - let label = if self.previous_token.1.line_break(&self.token.1) { None } else { self.consume_identifier(false) }; - let label_location = label.clone().map(|label| label.1.clone()); - let label = label.map(|label| label.0.clone()); - - let semicolon = self.parse_semicolon(); - - let node = Rc::new(Directive::ContinueStatement(ContinueStatement { - location: self.pop_location(), - label: label.clone().map(|l| (l.clone(), label_location.clone().unwrap())), - })); - - if label.is_some() && !context.is_label_defined(label.clone().unwrap()) { - self.add_syntax_error(&label_location.unwrap(), DiagnosticKind::UndefinedLabel, diagarg![label.clone().unwrap()]); - } else if !context.is_continue_allowed(label) { - self.add_syntax_error(&node.location(), DiagnosticKind::IllegalContinue, vec![]); - } - - (node, semicolon) - } - - fn parse_return_statement(&mut self, _context: ParserDirectiveContext) -> (Rc, bool) { - self.mark_location(); - self.next(); - - let expression = if self.previous_token.1.line_break(&self.token.1) { None } else { - self.parse_opt_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::List, - ..default() - }) - }; - - let semicolon = self.parse_semicolon(); - - let node = Rc::new(Directive::ReturnStatement(ReturnStatement { - location: self.pop_location(), - expression, - })); - - (node, semicolon) - } - - fn parse_throw_statement(&mut self, _context: ParserDirectiveContext) -> (Rc, bool) { - self.mark_location(); - self.next(); - - let line_break = self.previous_token.1.line_break(&self.token.1); - - let expression = self.parse_expression(ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::List, - ..default() - }); - - if line_break { - self.add_syntax_error(&expression.location(), DiagnosticKind::ExpressionMustNotFollowLineBreak, vec![]); - } - - let semicolon = self.parse_semicolon(); - - let node = Rc::new(Directive::ThrowStatement(ThrowStatement { - location: self.pop_location(), - expression, - })); - - (node, semicolon) - } - - fn parse_try_statement(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - self.mark_location(); - self.next(); - let context = context.clone_control(); - let block = Rc::new(self.parse_block(context.clone())); - let mut catch_clauses: Vec = vec![]; - let mut finally_clause: Option = None; - let mut found_catch = false; - loop { - if self.peek(Token::Catch) { - found_catch = true; - self.mark_location(); - self.next(); - self.non_greedy_expect(Token::ParenOpen); - if !self.expecting_token_error { - let parameter = self.parse_typed_destructuring(); - self.non_greedy_expect(Token::ParenClose); - if !self.expecting_token_error { - let block = Rc::new(self.parse_block(context.clone())); - catch_clauses.push(CatchClause { - location: self.pop_location(), - parameter, - block, - }); - } - } - } else if self.peek(Token::Finally) { - self.mark_location(); - self.next(); - let block = Rc::new(self.parse_block(context.clone())); - finally_clause = Some(FinallyClause { - location: self.pop_location(), - block, - }); - break; - } else { - break; - } - } - if !found_catch && finally_clause.is_none() { - self.non_greedy_expect(Token::Catch); - } - - let node = Rc::new(Directive::TryStatement(TryStatement { - location: self.pop_location(), - block, catch_clauses, finally_clause, - })); - - (node, true) - } - - fn parse_default_xml_namespace_statement(&mut self) -> (Rc, bool) { - self.mark_location(); - self.next(); - - let mut expression = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - self.forbid_line_break_before_token(); - self.non_greedy_expect_context_keyword("xml"); - if !self.expecting_token_error { - expression = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - self.forbid_line_break_before_token(); - self.non_greedy_expect_context_keyword("namespace"); - if !self.expecting_token_error { - expression = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - self.non_greedy_expect(Token::Assign); - - if !self.expecting_token_error { - expression = self.parse_expression(ParserExpressionContext { - allow_in: true, - allow_assignment: false, - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - }); - } - } - } - - let semicolon = self.parse_semicolon(); - - let node = Rc::new(Directive::DefaultXmlNamespaceStatement(DefaultXmlNamespaceStatement { - location: self.pop_location(), - right: expression, - })); - - (node, semicolon) - } - - fn forbid_line_break_before_token(&mut self) { - if self.previous_token.1.line_break(&self.token.1) { - self.add_syntax_error(&self.token.1.clone(), DiagnosticKind::TokenMustNotFollowLineBreak, vec![]); - } - } - - fn parse_directive(&mut self, context: ParserDirectiveContext) -> (Rc, bool) { - let asdoc: Option> = if self.peek(Token::SquareOpen) { None } else { self.parse_asdoc() }; - // ConfigurationDirective or Statement - if let Token::Identifier(id) = &self.token.0 { - let id = (id.clone(), self.token_location()); - self.next(); - - if id.0 == "include" && id.1.character_count() == "include".len() && matches!(self.token.0, Token::String(_)) && !self.previous_token.1.line_break(&self.token.1) { - return self.parse_include_directive(context, id.1); - } - - if self.peek(Token::BlockOpen) && &id.0 == "configuration" && id.1.character_count() == "configuration".len() { - return self.parse_configuration_directive(context, id.1); - } - - // If there is a line break or offending token is "::", - // do not proceed into parsing an expression attribute or annotatble directive. - let eligible_attribute_or_directive - = !self.previous_token.1.line_break(&self.token.1) - && !(matches!(self.token.0, Token::ColonColon)); - - if eligible_attribute_or_directive && (self.peek_annotatable_directive_identifier_name() || self.lookbehind_is_annotatable_directive_identifier_name()) { - let mut context1: AnnotatableContext; - - if ["enum", "type", "namespace"].contains(&id.0.as_ref()) - && id.1.character_count() == id.0.len() - && self.token.0.is_identifier_name() { - context1 = AnnotatableContext { - start_location: id.1.clone(), - asdoc, - attributes: vec![], - context: context.clone(), - directive_context_keyword: Some(id.clone()), - }; - // self.parse_attribute_keywords_or_expressions(&mut context); - } else { - let mut first_attr_expr = self.parse_expression_starting_with_identifier(id); - first_attr_expr = self.parse_subexpressions(first_attr_expr, ParserExpressionContext { - allow_in: true, min_precedence: OperatorPrecedence::List, ..default() - }); - - // Do not proceed into parsing an annotatable directive - // if there is a line break after an expression attribute, - // or if the offending token is not an identifier name, - // or if the expression attribute is not a valid access modifier. - if !first_attr_expr.valid_access_modifier() || self.previous_token.1.line_break(&self.token.1) || !(matches!(self.token.0, Token::Identifier(_)) || self.token.0.is_reserved_word()) { - self.push_location(&first_attr_expr.location()); - - // Parse CONFIG::VAR_NAME - if let Some(result) = self.parse_opt_normal_config(&first_attr_expr, asdoc.clone(), context.clone()) { - return result; - } - - let semicolon = self.parse_semicolon(); - return (Rc::new(Directive::ExpressionStatement(ExpressionStatement { - location: self.pop_location(), - expression: first_attr_expr, - })), semicolon); - } - - let first_attr = self.keyword_or_expression_attribute_from_expression(&first_attr_expr); - - context1 = AnnotatableContext { - start_location: first_attr.location(), - asdoc, - attributes: vec![first_attr], - context: context.clone(), - directive_context_keyword: None, - }; - self.parse_attribute_keywords_or_expressions(&mut context1); - } - return self.parse_annotatable_directive(context1); - } else if self.peek(Token::ColonColon) { - self.parse_qualified_identifier_statement_or_normal_config(context, id, asdoc) - } else { - self.parse_statement_starting_with_identifier(context, id) - } - } else if self.peek(Token::Import) { - self.parse_import_directive_or_expression_statement(context) - } else if self.peek(Token::SquareOpen) { - self.mark_location(); - let exp = self.parse_expression(ParserExpressionContext { - allow_in: true, min_precedence: OperatorPrecedence::List, ..default() - }); - if self.peek_annotatable_directive_identifier_name() { - match exp.to_metadata(self) { - Ok(Some(metadata)) => { - let mut context = AnnotatableContext { - start_location: self.pop_location(), - asdoc: self.parse_asdoc(), - attributes: metadata, - context: context.clone(), - directive_context_keyword: None, - }; - self.parse_attribute_keywords_or_expressions(&mut context); - return self.parse_annotatable_directive(context); - }, - Ok(None) => {}, - Err(MetadataRefineError1(MetadataRefineError::Syntax, loc)) => { - self.add_syntax_error(&loc, DiagnosticKind::UnrecognizedMetadataSyntax, diagarg![]); - }, - } - } - let semicolon = self.parse_semicolon(); - (Rc::new(Directive::ExpressionStatement(ExpressionStatement { - location: self.pop_location(), - expression: exp, - })), semicolon) - } else if self.peek(Token::Public) || self.peek(Token::Private) || self.peek(Token::Protected) - || self.peek(Token::Internal) || self.peek(Token::Var) || self.peek(Token::Const) - || self.peek(Token::Function) || self.peek(Token::Class) || self.peek(Token::Interface) { - let is_public = self.peek(Token::Public); - let rns = self.parse_opt_reserved_namespace(); - let mut attributes: Vec = vec![]; - if let Some(rns) = rns { - // The public += ns.*; directive - if self.peek(Token::AddAssign) && is_public { - return self.parse_package_concat_directive(&rns.location(), context); - } - - // Do not proceed into parsing an annotatable directive - // if there is a "::" token. - if matches!(self.token.0, Token::ColonColon) { - self.push_location(&rns.location()); - let rns = Rc::new(Expression::QualifiedIdentifier(self.finish_qualified_identifier(false, rns.location(), rns))); - let rns = self.parse_subexpressions(rns, ParserExpressionContext { - allow_in: true, min_precedence: OperatorPrecedence::List, ..default() - }); - let semicolon = self.parse_semicolon(); - return (Rc::new(Directive::ExpressionStatement(ExpressionStatement { - location: self.pop_location(), - expression: rns, - })), semicolon); - } - attributes.push(rns.to_reserved_namespace_attribute().unwrap()); - } - let mut context = AnnotatableContext { - start_location: self.token_location(), - asdoc, - attributes, - context: context.clone(), - directive_context_keyword: None, - }; - self.parse_attribute_keywords_or_expressions(&mut context); - return self.parse_annotatable_directive(context); - } else if self.peek(Token::Use) { - self.parse_use_namespace_directive() - } else { - let i = self.tokenizer.characters().index(); - let r = self.parse_statement(context); - if i == self.tokenizer.characters().index() { - self.patch_syntax_error(DiagnosticKind::ExpectingStatement, DiagnosticKind::ExpectingDirective, diagarg![self.token.0.clone()]); - } - r - } - } - - fn parse_directives(&mut self, context: ParserDirectiveContext) -> Vec> { - let mut directives = vec![]; - let mut semicolon = false; - while !self.peek(Token::Eof) { - if !directives.is_empty() && !semicolon { - self.non_greedy_expect_virtual_semicolon(); - } - let (directive, semicolon_1) = self.parse_directive(context.clone()); - directives.push(directive); - semicolon = semicolon_1; - } - directives - } - - fn parse_expression_attribute(&mut self, id: &(String, Location)) -> Rc { - let mut result = Rc::new(Expression::QualifiedIdentifier(QualifiedIdentifier { - location: id.1.clone(), - attribute: false, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id(id.clone()), - })); - loop { - if self.peek(Token::Dot) { - self.push_location(&result.location()); - self.next(); - let id = self.parse_qualified_identifier(); - result = Rc::new(Expression::Member(MemberExpression { - location: self.pop_location(), - base: result, identifier: id - })); - } else if self.consume(Token::SquareOpen) { - self.push_location(&result.location()); - let key = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() }); - self.non_greedy_expect(Token::SquareClose); - result = Rc::new(Expression::ComputedMember(ComputedMemberExpression { - base: result, asdoc: None, key, location: self.pop_location() - })); - } else { - break; - } - } - result - } - - fn report_modifier_errors(&self, context: &AnnotatableContext) { - let mut i = 0usize; - while i < context.attributes.len() { - let a = &context.attributes[i]; - if Attribute::has(&context.attributes[..i], &a) { - self.add_syntax_error(&a.location(), DiagnosticKind::DuplicateAttribute, diagarg![]); - } - if Attribute::is_duplicate_access_modifier(&context.attributes[..i], &a) { - self.add_syntax_error(&a.location(), DiagnosticKind::DuplicateAccessModifier, diagarg![]); - } - i += 1; - } - } - - fn parse_annotatable_directive(&mut self, context: AnnotatableContext) -> (Rc, bool) { - if self.peek(Token::Var) || self.peek(Token::Const) { - self.report_modifier_errors(&context); - self.parse_variable_definition(context) - } else if self.consume(Token::Function) { - self.report_modifier_errors(&context); - self.parse_function_definition(context) - } else if self.consume(Token::Class) { - self.report_modifier_errors(&context); - self.parse_class_definition(context) - } else if context.has_directive_context_keyword("enum") { - self.report_modifier_errors(&context); - self.parse_enum_definition(context) - } else if context.has_directive_context_keyword("namespace") { - self.report_modifier_errors(&context); - self.parse_namespace_definition(context) - } else if self.consume(Token::Interface) { - self.report_modifier_errors(&context); - self.parse_interface_definition(context) - } else if context.has_directive_context_keyword("type") { - self.report_modifier_errors(&context); - self.parse_type_definition(context) - } else { - // In case there is a series of inline modifiers, - // report semicolon error between each. - let mut i = 0usize; - let mut error = false; - while i < context.attributes.len() { - if !context.attributes[i].is_metadata() { - let loc1 = context.attributes[i].location(); - if i + 1 < context.attributes.len() { - let loc2 = context.attributes[i + 1].location(); - if !loc1.line_break(&loc2) { - self.add_syntax_error(&loc2, DiagnosticKind::ExpectingEitherSemicolonOrNewLineHere, vec![]); - error = true; - } - } - } - i += 1; - } - - if !error { - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingDirectiveKeyword, diagarg![self.token.0.clone()]); - } - self.push_location(&context.start_location); - let loc = self.pop_location(); - (self.create_invalidated_directive(&loc), true) - } - } - - pub(crate) fn refine_metadata(&self, exp: &Rc, asdoc: Option>) -> Result, MetadataRefineError> { - if let Expression::Call(CallExpression { base, arguments, .. }) = exp.as_ref() { - let Ok(name) = self.refine_metadata_name(base) else { - return Err(MetadataRefineError::Syntax); - }; - Ok(Rc::new(Metadata { - location: exp.location(), - asdoc, - name, - entries: Some(self.refine_metadata_entries(arguments)?), - })) - } else { - if let Ok(name) = self.refine_metadata_name(exp) { - Ok(Rc::new(Metadata { - location: exp.location(), - asdoc, - name, - entries: None, - })) - } else { - Err(MetadataRefineError::Syntax) - } - } - } - - fn refine_metadata_name(&self, exp: &Rc) -> Result<(String, Location), MetadataRefineError> { - if let Expression::QualifiedIdentifier(id) = exp.as_ref() { - if id.attribute { - return Err(MetadataRefineError::Syntax); - } - let qual = id.qualifier.as_ref().and_then(|q| q.to_identifier_name().map(|n| n.0)); - if id.qualifier.is_some() && qual.is_none() { - return Err(MetadataRefineError::Syntax); - } - if let QualifiedIdentifierIdentifier::Id((s, _)) = &id.id { - if s == "*" { Err(MetadataRefineError::Syntax) } else { Ok((if let Some(q) = qual { format!("{q}::{s}") } else { s.to_string() }, exp.location())) } - } else { - Err(MetadataRefineError::Syntax) - } - } else { - Err(MetadataRefineError::Syntax) - } - } - - fn refine_metadata_entries(&self, list: &Vec>) -> Result>, MetadataRefineError> { - let mut r = Vec::>::new(); - for entry in list { - r.push(self.refine_metadata_entry(&entry)?); - } - Ok(r) - } - - fn refine_metadata_entry(&self, exp: &Rc) -> Result, MetadataRefineError> { - match exp.as_ref() { - Expression::Assignment(AssignmentExpression { compound, left, right, location }) => { - if compound.is_some() { - return Err(MetadataRefineError::Syntax); - } - let key = self.refine_metadata_name(left)?; - if matches!(right.as_ref(), Expression::QualifiedIdentifier(_)) { - return Err(MetadataRefineError::Syntax); - } - let value = self.refine_metadata_value(right)?; - Ok(Rc::new(MetadataEntry { - location: location.clone(), - key: Some(key), - value: Rc::new(value), - })) - }, - _ => { - let value = self.refine_metadata_value(exp)?; - Ok(Rc::new(MetadataEntry { - location: exp.location(), - key: None, - value: Rc::new(value), - })) - }, - } - } - - fn refine_metadata_value(&self, exp: &Rc) -> Result { - match exp.as_ref() { - Expression::QualifiedIdentifier(_) => { - let name = self.refine_metadata_name(&exp)?; - Ok(MetadataValue::IdentifierString(name)) - }, - Expression::StringLiteral(StringLiteral { value, .. }) => Ok(MetadataValue::String((value.clone(), exp.location()))), - _ => Err(MetadataRefineError::Syntax), - } - } - - fn parse_package_concat_directive(&mut self, start: &Location, context: ParserDirectiveContext) -> (Rc, bool) { - self.push_location(start); - self.next(); - let mut package_name: Vec<(String, Location)> = vec![self.expect_identifier(false)]; - let mut import_specifier = ImportSpecifier::Wildcard(self.token_location()); - - if !self.peek(Token::Dot) { - self.non_greedy_expect(Token::Dot); - } - - while self.consume(Token::Dot) { - if self.peek(Token::Times) { - import_specifier = ImportSpecifier::Wildcard(self.token_location()); - self.next(); - break; - } else if self.peek(Token::Power) { - import_specifier = ImportSpecifier::Recursive(self.token_location()); - self.next(); - break; - } else { - let id1 = self.expect_identifier(true); - if !self.peek(Token::Dot) { - import_specifier = ImportSpecifier::Identifier(id1.clone()); - break; - } else { - package_name.push(id1.clone()); - } - } - } - - let semicolon = self.parse_semicolon(); - - let node = Rc::new(Directive::PackageConcatDirective(PackageConcatDirective { - location: self.pop_location(), - package_name, - import_specifier, - })); - - if !(matches!(context, ParserDirectiveContext::PackageBlock)) { - self.add_syntax_error(&node.location(), DiagnosticKind::UnexpectedDirective, vec![]); - } - - (node, semicolon) - } - - fn parse_import_directive_or_expression_statement(&mut self, _context: ParserDirectiveContext) -> (Rc, bool) { - self.mark_location(); - self.next(); - if self.consume(Token::Dot) { - self.duplicate_location(); - self.non_greedy_expect_context_keyword("meta"); - let mut expression = Rc::new(Expression::ImportMeta(ImportMeta { - location: self.pop_location(), - })); - expression = self.parse_subexpressions(expression, ParserExpressionContext { - allow_in: true, - min_precedence: OperatorPrecedence::List, - ..default() - }); - let semicolon = self.parse_semicolon(); - (Rc::new(Directive::ExpressionStatement(ExpressionStatement { - location: self.pop_location(), - expression, - })), semicolon) - } else { - let mut alias: Option<(String, Location)> = None; - let mut package_name: Vec<(String, Location)> = vec![]; - let mut import_specifier = ImportSpecifier::Wildcard(self.token_location()); - let id1 = self.expect_identifier(false); - if self.consume(Token::Assign) { - alias = Some(id1.clone()); - package_name.push(self.expect_identifier(false)); - } else { - package_name.push(id1); - } - - if !self.peek(Token::Dot) { - self.non_greedy_expect(Token::Dot); - } - - while self.consume(Token::Dot) { - if self.peek(Token::Times) { - import_specifier = ImportSpecifier::Wildcard(self.token_location()); - self.next(); - break; - } else if self.peek(Token::Power) { - import_specifier = ImportSpecifier::Recursive(self.token_location()); - self.next(); - break; - } else { - let id1 = self.expect_identifier(true); - if !self.peek(Token::Dot) { - import_specifier = ImportSpecifier::Identifier(id1.clone()); - break; - } else { - package_name.push(id1.clone()); - } - } - } - - let semicolon = self.parse_semicolon(); - - let node = Rc::new(Directive::ImportDirective(ImportDirective { - location: self.pop_location(), - alias, - package_name, - import_specifier, - })); - - (node, semicolon) - } - } - - fn parse_include_directive(&mut self, context: ParserDirectiveContext, start: Location) -> (Rc, bool) { - self.push_location(&start); - let source_path_location = self.token_location(); - let Token::String(source) = &self.token.0.clone() else { - panic!(); - }; - let source = source.clone(); - self.next(); - let semicolon = self.parse_semicolon(); - - let nested_compilation_unit: Rc; - - // Select origin file path - let origin_file_path = if let Some(file_path) = self.tokenizer.compilation_unit().file_path.clone() { - Some(file_path) - } else { - std::env::current_dir().ok().map(|d| d.to_string_lossy().into_owned()) - }; - - // Resolve source - if let Some(origin_file_path) = origin_file_path { - let sub_file_path = file_paths::FlexPath::from_n_native([origin_file_path.as_ref(), "..", source.as_ref()]).to_string_with_flex_separator(); - if self.tokenizer.compilation_unit().include_directive_is_circular(&sub_file_path) { - self.add_syntax_error(&source_path_location.clone(), DiagnosticKind::CircularIncludeDirective, vec![]); - - // Use a placeholder compilation unit - nested_compilation_unit = CompilationUnit::new(None, "".into()); - } else { - if let Ok(content) = std::fs::read_to_string(&sub_file_path) { - nested_compilation_unit = CompilationUnit::new(Some(sub_file_path.clone()), content); - } else { - self.add_syntax_error(&source_path_location.clone(), DiagnosticKind::FailedToIncludeFile, vec![]); - - // Use a placeholder compilation unit - nested_compilation_unit = CompilationUnit::new(None, "".into()); - } - } - } else { - self.add_syntax_error(&source_path_location.clone(), DiagnosticKind::ParentSourceIsNotAFile, vec![]); - - // Use a placeholder compilation unit - nested_compilation_unit = CompilationUnit::new(None, "".into()); - } - - // Let it be such that the sub compilation unit is subsequent of - // the super compilation unit. - nested_compilation_unit.set_included_from(Some(self.tokenizer.compilation_unit().clone())); - - // Add sub compilation unit to super compilation unit - self.tokenizer.compilation_unit().add_nested_compilation_unit(nested_compilation_unit.clone()); - - // Parse directives from replacement source - let (nested_packages, nested_directives) = parse_include_directive_source(nested_compilation_unit.clone(), context); - - // Delegate sub compilation unit errors to super compilation unit - if nested_compilation_unit.invalidated() { - self.tokenizer.compilation_unit().invalidated.set(true); - } - - let node = Rc::new(Directive::IncludeDirective(IncludeDirective { - location: self.pop_location(), - source, - nested_packages, - nested_directives, - nested_compilation_unit: nested_compilation_unit.clone(), - })); - - (node, semicolon) - } - - fn parse_use_namespace_directive(&mut self) -> (Rc, bool) { - self.mark_location(); - self.next(); - let mut expression = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - self.non_greedy_expect_context_keyword("namespace"); - if !self.expecting_token_error { - expression = self.parse_expression(ParserExpressionContext { - min_precedence: OperatorPrecedence::List, - ..default() - }); - } - let semicolon = self.parse_semicolon(); - - let node = Rc::new(Directive::UseNamespaceDirective(UseNamespaceDirective { - location: self.pop_location(), - expression, - })); - - (node, semicolon) - } - - fn parse_variable_definition(&mut self, context: AnnotatableContext) -> (Rc, bool) { - let AnnotatableContext { start_location, asdoc, attributes, context, .. } = context; - let has_static = Attribute::find_static(&attributes).is_some(); - self.push_location(&start_location); - let kind_location = self.token_location(); - let kind = if self.consume(Token::Const) { - VariableDefinitionKind::Const - } else { - self.non_greedy_expect(Token::Var); - VariableDefinitionKind::Var - }; - let mut bindings = vec![Rc::new(self.parse_variable_binding(true))]; - while self.consume(Token::Comma) { - bindings.push(Rc::new(self.parse_variable_binding(true))); - } - - // Forbid destructuring bindings in enumerations. - if !has_static && matches!(context, ParserDirectiveContext::EnumBlock) { - if kind != VariableDefinitionKind::Const { - self.add_syntax_error(&kind_location, DiagnosticKind::EnumMembersMustBeConst, diagarg![]); - } - for binding in &bindings { - let malformed = !matches!(binding.destructuring.destructuring.as_ref(), Expression::QualifiedIdentifier(_)) - || binding.destructuring.type_annotation.is_some(); - if malformed { - self.add_syntax_error(&binding.location(), DiagnosticKind::MalformedEnumMember, diagarg![]); - } - } - } - - for a in &attributes { - if a.is_metadata() { - continue; - } - match a { - Attribute::Static(_) => { - if !context.is_type_block() { - // Unallowed attribute - self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]); - } - }, - Attribute::Expression(_) | - Attribute::Public(_) | - Attribute::Private(_) | - Attribute::Protected(_) | - Attribute::Internal(_) => { - self.verify_visibility(&a, &context); - }, - _ => { - // Unallowed attribute - self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]); - }, - } - } - - let semicolon = self.parse_semicolon(); - let node = Rc::new(Directive::VariableDefinition(VariableDefinition { - location: self.pop_location(), - asdoc, - attributes, - kind: (kind, kind_location), - bindings, - })); - - (node, semicolon) - } - - fn parse_function_definition(&mut self, context: AnnotatableContext) -> (Rc, bool) { - let AnnotatableContext { start_location, asdoc, attributes, context, .. } = context; - let has_native = Attribute::find_native(&attributes).is_some(); - let has_abstract = Attribute::find_abstract(&attributes).is_some(); - self.push_location(&start_location); - let mut name = self.expect_identifier(true); - let mut getter = false; - let mut setter = false; - if self.peek_identifier(true).is_some() { - getter = Token::is_context_keyword(&self.previous_token, "get"); - setter = Token::is_context_keyword(&self.previous_token, "set"); - if getter || setter { - name = self.expect_identifier(true); - } - } - let constructor = !getter && !setter && context.function_name_is_constructor(&name); - let name = if getter { - FunctionName::Getter(name) - } else if setter { - FunctionName::Setter(name) - } else if constructor { - FunctionName::Constructor(name) - } else { - FunctionName::Identifier(name) - }; - let block_context = if constructor { - ParserDirectiveContext::ConstructorBlock { super_statement_found: Rc::new(Cell::new(false)) } - } else { - ParserDirectiveContext::Default - }; - let common = self.parse_function_common(false, block_context, true); - let semicolon = if common.has_block_body() { true } else { self.parse_semicolon() }; - - /* - if constructor && common.signature.result_type.is_some() { - self.add_syntax_error(&name.location(), DiagnosticKind::ConstructorMustNotSpecifyResultType, diagarg![]); - } - */ - - // Not all kinds of functions may be generators. - if common.contains_yield && (constructor || getter || setter) { - self.add_syntax_error(&name.location(), DiagnosticKind::FunctionMayNotBeGenerator, diagarg![]); - } - - // Not all kinds of functions may be asynchronous. - if common.contains_await && (constructor || getter || setter) { - self.add_syntax_error(&name.location(), DiagnosticKind::FunctionMayNotBeAsynchronous, diagarg![]); - } - - let interface_method = matches!(context, ParserDirectiveContext::InterfaceBlock); - - // Body verification. - // - // Note that interface methods must never have a body unlike in Java. - if (interface_method || has_native || has_abstract) && common.body.is_some() { - self.add_syntax_error(&name.location(), DiagnosticKind::FunctionMustNotContainBody, diagarg![]); - } else if !(interface_method || has_native || has_abstract) && common.body.is_none() { - self.add_syntax_error(&name.location(), DiagnosticKind::FunctionMustContainBody, diagarg![]); - } - - // Interface methods must not contain any annotations except for meta-data. - if !attributes.is_empty() && interface_method { - if !attributes.last().unwrap().is_metadata() { - self.add_syntax_error(&name.location(), DiagnosticKind::FunctionMustNotContainAnnotations, diagarg![]); - } - } - - for a in &attributes { - if a.is_metadata() { - continue; - } - match a { - Attribute::Static(_) => { - if !context.is_type_block() { - // Unallowed attribute - self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]); - } - }, - Attribute::Final(_) | - Attribute::Override(_) | - Attribute::Abstract(_) => { - if !context.is_type_block() || constructor { - // Unallowed attribute - self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]); - } - }, - - Attribute::Native(_) => {}, - - Attribute::Expression(_) | - Attribute::Public(_) | - Attribute::Private(_) | - Attribute::Protected(_) | - Attribute::Internal(_) => { - self.verify_visibility(&a, &context); - }, - _ => { - // Unallowed attribute - self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]); - }, - } - } - - let node = Rc::new(Directive::FunctionDefinition(FunctionDefinition { - location: self.pop_location(), - asdoc, - attributes, - name: name.clone(), - common, - })); - - (node, semicolon) - } - - fn parse_class_definition(&mut self, context: AnnotatableContext) -> (Rc, bool) { - let AnnotatableContext { start_location, asdoc, attributes, context, .. } = context; - self.push_location(&start_location); - let name = self.expect_identifier(true); - let type_parameters = self.parse_type_parameters_opt(); - let mut extends_clause: Option> = None; - if self.consume(Token::Extends) { - extends_clause = Some(self.parse_type_expression()); - } - let mut implements_clause: Option>> = None; - if self.consume(Token::Implements) { - implements_clause = Some(self.parse_type_expression_list()); - } - let block = Rc::new(self.parse_block(ParserDirectiveContext::ClassBlock { - name: name.0.clone(), - })); - - for a in &attributes { - if a.is_metadata() { - continue; - } - match a { - Attribute::Static(_) => {}, - Attribute::Final(_) => {}, - Attribute::Dynamic(_) => {}, - Attribute::Abstract(_) => {}, - - Attribute::Expression(_) | - Attribute::Public(_) | - Attribute::Private(_) | - Attribute::Protected(_) | - Attribute::Internal(_) => { - self.verify_visibility(&a, &context); - }, - _ => { - // Unallowed attribute - self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]); - }, - } - } - - // Nested classes not allowed - if !context.is_top_level_or_package() { - self.add_syntax_error(&name.1, DiagnosticKind::NestedClassesNotAllowed, diagarg![]); - } - - let node = Rc::new(Directive::ClassDefinition(ClassDefinition { - location: self.pop_location(), - asdoc, - attributes, - name: name.clone(), - type_parameters, - extends_clause, - implements_clause, - block, - })); - - (node, true) - } - - fn parse_enum_definition(&mut self, context: AnnotatableContext) -> (Rc, bool) { - let AnnotatableContext { start_location, asdoc, mut attributes, context, .. } = context; - self.push_location(&start_location); - let name = self.expect_identifier(true); - let mut as_clause: Option> = None; - if self.consume(Token::As) { - as_clause = Some(self.parse_type_expression()); - } - let block = Rc::new(self.parse_block(ParserDirectiveContext::EnumBlock)); - - for a in &attributes { - if a.is_metadata() { - continue; - } - match a { - Attribute::Expression(_) | - Attribute::Public(_) | - Attribute::Private(_) | - Attribute::Protected(_) | - Attribute::Internal(_) => { - self.verify_visibility(&a, &context); - }, - _ => { - // Unallowed attribute - self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]); - }, - } - } - - // Nested classes not allowed - if !context.is_top_level_or_package() { - self.add_syntax_error(&name.1, DiagnosticKind::NestedClassesNotAllowed, diagarg![]); - } - - let mut is_set = false; - let metadata = Attribute::find_metadata(&attributes); - for metadata in metadata { - if metadata.name.0 == "Set" { - is_set = true; - Attribute::remove_metadata(&mut attributes, &metadata); - } - } - - let node = Rc::new(Directive::EnumDefinition(EnumDefinition { - location: self.pop_location(), - asdoc, - attributes, - is_set, - name: name.clone(), - as_clause, - block, - })); - - (node, true) - } - - fn parse_interface_definition(&mut self, context: AnnotatableContext) -> (Rc, bool) { - let AnnotatableContext { start_location, asdoc, attributes, context, .. } = context; - self.push_location(&start_location); - let name = self.expect_identifier(true); - let type_parameters = self.parse_type_parameters_opt(); - let mut extends_clause: Option>> = None; - if self.consume(Token::Extends) { - extends_clause = Some(self.parse_type_expression_list()); - } - let block = Rc::new(self.parse_block(ParserDirectiveContext::InterfaceBlock)); - - // Interface block must only contain function definitions - for directive in block.directives.iter() { - if !(matches!(directive.as_ref(), Directive::FunctionDefinition(_))) { - self.add_syntax_error(&directive.location(), DiagnosticKind::UnexpectedDirective, diagarg![]); - } - } - - for a in &attributes { - if a.is_metadata() { - continue; - } - match a { - Attribute::Expression(_) | - Attribute::Public(_) | - Attribute::Private(_) | - Attribute::Protected(_) | - Attribute::Internal(_) => { - self.verify_visibility(&a, &context); - }, - _ => { - // Unallowed attribute - self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]); - }, - } - } - - // Nested classes not allowed - if !context.is_top_level_or_package() { - self.add_syntax_error(&name.1, DiagnosticKind::NestedClassesNotAllowed, diagarg![]); - } - - let node = Rc::new(Directive::InterfaceDefinition(InterfaceDefinition { - location: self.pop_location(), - asdoc, - attributes, - name: name.clone(), - type_parameters, - extends_clause, - block, - })); - - (node, true) - } - - fn parse_type_definition(&mut self, context: AnnotatableContext) -> (Rc, bool) { - let AnnotatableContext { start_location, asdoc, attributes, context, .. } = context; - self.push_location(&start_location); - let left = self.expect_identifier(true); - let mut right = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - self.non_greedy_expect(Token::Assign); - if !self.expecting_token_error { - right = self.parse_type_expression(); - } - - for a in &attributes { - if a.is_metadata() { - continue; - } - match a { - Attribute::Expression(_) | - Attribute::Public(_) | - Attribute::Private(_) | - Attribute::Protected(_) | - Attribute::Internal(_) => { - self.verify_visibility(&a, &context); - }, - _ => { - // Unallowed attribute - self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]); - }, - } - } - - // Nested classes not allowed - if !context.is_top_level_or_package() { - self.add_syntax_error(&left.1, DiagnosticKind::NestedClassesNotAllowed, diagarg![]); - } - - let semicolon = self.parse_semicolon(); - - let node = Rc::new(Directive::TypeDefinition(TypeDefinition { - location: self.pop_location(), - asdoc, - attributes, - left: left.clone(), - right, - })); - - (node, semicolon) - } - - fn parse_namespace_definition(&mut self, context: AnnotatableContext) -> (Rc, bool) { - let AnnotatableContext { start_location, asdoc, attributes, context, .. } = context; - self.push_location(&start_location); - let left = self.expect_identifier(true); - let mut right: Option> = None; - if self.consume(Token::Assign) { - right = Some(self.parse_expression(ParserExpressionContext { - min_precedence: OperatorPrecedence::AssignmentAndOther, - ..default() - })); - } - - for a in &attributes { - if a.is_metadata() { - continue; - } - match a { - Attribute::Expression(_) | - Attribute::Public(_) | - Attribute::Private(_) | - Attribute::Protected(_) | - Attribute::Internal(_) => { - self.verify_visibility(&a, &context); - }, - Attribute::Static(_) => {}, - _ => { - // Unallowed attribute - self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]); - }, - } - } - - let semicolon = self.parse_semicolon(); - - let node = Rc::new(Directive::NamespaceDefinition(NamespaceDefinition { - location: self.pop_location(), - asdoc, - attributes, - left: left.clone(), - right, - })); - - (node, semicolon) - } - - fn parse_type_expression_list(&mut self) -> Vec> { - let mut list = vec![self.parse_type_expression()]; - while self.consume(Token::Comma) { - list.push(self.parse_type_expression()); - } - list - } - - fn verify_visibility(&self, a: &Attribute, context: &ParserDirectiveContext) { - let mut unallowed = false; - match a { - Attribute::Expression(_) => {}, - Attribute::Public(_) => {}, - Attribute::Private(_) | - Attribute::Protected(_) => { - if !context.is_type_block() { - unallowed = true; - } - }, - Attribute::Internal(_) => {}, - _ => {} - } - if unallowed { - // Unallowed attribute - self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]); - } - } - - fn parse_type_parameters_opt(&mut self) -> Option>> { - if !self.consume(Token::Dot) { - return None; - } - let mut list: Vec> = vec![]; - self.non_greedy_expect(Token::Lt); - if !self.expecting_token_error { - list.push(self.parse_type_parameter()); - while self.consume(Token::Comma) { - list.push(self.parse_type_parameter()); - } - self.non_greedy_expect_type_parameters_gt(); - } - Some(list) - } - - fn parse_type_parameter(&mut self) -> Rc { - self.mark_location(); - let name = self.expect_identifier(false); - Rc::new(TypeParameter { - location: self.pop_location(), - name, - }) - } - - fn parse_configuration_directive(&mut self, context: ParserDirectiveContext, start_location: Location) -> (Rc, bool) { - self.push_location(&start_location); - self.non_greedy_expect(Token::BlockOpen); - let subdirective = self.parse_configuration_subdirective(context.clone()); - self.non_greedy_expect(Token::BlockClose); - (Rc::new(Directive::ConfigurationDirective(ConfigurationDirective { - location: self.pop_location(), - directive: subdirective, - })), true) - } - - fn parse_configuration_subdirective(&mut self, context: ParserDirectiveContext) -> Rc { - if self.peek(Token::If) { - self.mark_location(); - self.next(); - let mut test = self.create_invalidated_expression(&self.tokenizer.cursor_location()); - self.non_greedy_expect(Token::ParenOpen); - if !self.expecting_token_error { - test = self.parse_configuration_expression(); - } - self.non_greedy_expect(Token::ParenClose); - let consequent = Rc::new(Directive::Block(self.parse_block(context.clone()))); - let mut alternative: Option> = None; - if self.consume(Token::Else) { - alternative = Some(self.parse_configuration_subdirective(context.clone())); - } - Rc::new(Directive::IfStatement(IfStatement { - location: self.pop_location(), - test, - consequent, - alternative, - })) - } else { - Rc::new(Directive::Block(self.parse_block(context.clone()))) - } - } - - fn parse_configuration_expression(&mut self) -> Rc { - let mut base = self.parse_configuration_primary_expression(); - if self.consume(Token::LogicalAnd) { - self.push_location(&base.location()); - let right = self.parse_configuration_expression(); - base = Rc::new(Expression::Binary(BinaryExpression { - location: self.pop_location(), - operator: Operator::LogicalAnd, - left: base.clone(), - right, - })); - } else if self.consume(Token::LogicalOr) { - self.push_location(&base.location()); - let right = self.parse_configuration_expression(); - base = Rc::new(Expression::Binary(BinaryExpression { - location: self.pop_location(), - operator: Operator::LogicalOr, - left: base.clone(), - right, - })); - } - base - } - - fn parse_configuration_primary_expression(&mut self) -> Rc { - if let Token::Identifier(_) = &self.token.0.clone() { - self.mark_location(); - let mut id = self.expect_identifier(false); - let mut qual: Option> = None; - if self.consume(Token::ColonColon) { - qual = Some(Rc::new(Expression::QualifiedIdentifier(QualifiedIdentifier { - location: id.1.clone(), - attribute: false, - qualifier: None, - id: QualifiedIdentifierIdentifier::Id(id.clone()), - }))); - id = self.expect_identifier(true); - } - let id_location = self.pop_location(); - let id = Rc::new(Expression::QualifiedIdentifier(QualifiedIdentifier { - location: id_location.clone(), - attribute: false, - qualifier: qual, - id: QualifiedIdentifierIdentifier::Id(id.clone()), - })); - let equality: Option = if self.consume(Token::Assign) { - Some(Operator::Equals) - } else if self.consume(Token::NotEquals) { - Some(Operator::NotEquals) - } else { - None - }; - if let Some(equality) = equality { - self.push_location(&id.location()); - self.mark_location(); - let mut value: String = "".into(); - if let Some((value_1, _)) = self.consume_identifier(false) { - value = value_1; - } else { - if let Token::String(s) = &self.token.0 { - value = s.clone(); - self.next(); - } else { - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingStringLiteral, diagarg![self.token.0.clone()]); - while self.token.0 != Token::Eof { - self.next(); - if let Token::String(s) = self.token.0.clone() { - self.pop_location(); - self.mark_location(); - value = s; - self.next(); - } - } - } - } - let right = Rc::new(Expression::StringLiteral(StringLiteral { - location: self.pop_location(), - value, - })); - Rc::new(Expression::Binary(BinaryExpression { - location: self.pop_location(), - operator: equality, - left: id.clone(), - right, - })) - } else { - id - } - } else if self.peek(Token::ParenOpen) { - self.mark_location(); - self.next(); - let expression = self.parse_configuration_expression(); - self.non_greedy_expect(Token::ParenClose); - Rc::new(Expression::Paren(ParenExpression { - location: self.pop_location(), - expression, - })) - } else if self.peek(Token::Exclamation) { - self.mark_location(); - self.next(); - let expression = self.parse_configuration_primary_expression(); - Rc::new(Expression::Unary(UnaryExpression { - location: self.pop_location(), - operator: Operator::LogicalNot, - expression, - })) - } else { - self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingExpression, diagarg![self.token.0.clone()]); - self.create_invalidated_expression(&self.tokenizer.cursor_location()) - } - } - - fn keyword_or_expression_attribute_from_expression(&self, expr: &Rc) -> Attribute { - match expr.as_ref() { - Expression::QualifiedIdentifier(id) => { - if id.qualifier.is_some() || id.attribute { - return Attribute::Expression(expr.clone()); - } - match &id.id { - QualifiedIdentifierIdentifier::Id((id, location)) => { - if let Some(attr) = Attribute::from_identifier_name(&id, &location) { - return attr; - } - Attribute::Expression(expr.clone()) - }, - _ => Attribute::Expression(expr.clone()), - } - }, - _ => Attribute::Expression(expr.clone()), - } - } - - fn keyword_attribute_from_previous_token(&self) -> Option { - self.previous_token.0.to_attribute(&self.previous_token.1) - } - - fn _keyword_or_expression_attribute_from_previous_token(&mut self) -> Option { - if let Some(a) = self.keyword_attribute_from_previous_token() { - return Some(a); - } - match &self.previous_token.0 { - Token::Identifier(id) => Some(Attribute::Expression(self.parse_expression_attribute(&(id.clone(), self.previous_token.1.clone())))), - _ => None, - } - } - - fn parse_keyword_or_expression_attribute(&mut self) -> Option { - if let Some(a) = self.token.0.to_attribute(&self.token.1) { - self.next(); - return Some(a); - } - match &self.token.0 { - Token::Identifier(_) => { - let id = self.expect_identifier(false); - Some(Attribute::Expression(self.parse_expression_attribute(&id))) - }, - _ => None, - } - } - - fn peek_annotatable_directive_identifier_name(&self) -> bool { - if self.token.0.to_attribute(&self.token.1).is_some() { - return true; - } - match self.token.0 { - Token::Identifier(_) => true, - Token::Var | - Token::Const | - Token::Function | - Token::Class | - Token::Interface => true, - _ => false, - } - } - - fn lookbehind_is_annotatable_directive_identifier_name(&self) -> bool { - self.keyword_attribute_from_previous_token().is_some() - || matches!(&self.previous_token.0, Token::Identifier(_)) - || Token::is_context_keyword(&self.previous_token, "enum") - || Token::is_context_keyword(&self.previous_token, "type") - || Token::is_context_keyword(&self.previous_token, "namespace") - } - - fn parse_attribute_keywords_or_expressions(&mut self, context: &mut AnnotatableContext) { - if context.directive_context_keyword.is_some() { - unreachable!(); - } - loop { - if let Some(a) = self.parse_keyword_or_expression_attribute() { - if let Attribute::Expression(e) = &a { - let id = e.to_identifier_name(); - if let Some(id) = id { - if ["enum", "type", "namespace"].contains(&id.0.as_ref()) { - context.directive_context_keyword = Some(id); - break; - } - } - } - let last_attribute_is_identifier = context.attributes.last().map_or(false, |a| !a.is_metadata()); - if last_attribute_is_identifier { - self.forbid_line_break_before_token(); - } - context.attributes.push(a); - // self.next(); - } else { - if let Some(id) = self.peek_identifier(false) { - self.forbid_line_break_before_token(); - if ["enum", "type", "namespace"].contains(&id.0.as_ref()) { - self.next(); - context.directive_context_keyword = Some(id); - } - } - break; - } - } - // For meta-data that are not one of certain Flex meta-data, - // delegate the respective ASDoc to the annotatable directive. - let mut new_attributes = Vec::::new(); - for attr in &context.attributes { - if let Attribute::Metadata(metadata) = attr { - if !is_flex_documentable_meta_data(&metadata.name.0) && metadata.asdoc.is_some() { - new_attributes.push(Attribute::Metadata(Rc::new(Metadata { - location: metadata.location.clone(), - asdoc: None, - name: metadata.name.clone(), - entries: metadata.entries.clone(), - }))); - context.asdoc = metadata.asdoc.clone(); - } else { - new_attributes.push(attr.clone()); - } - } else { - new_attributes.push(attr.clone()); - } - } - context.attributes = new_attributes; - } - - pub fn parse_package_definition(&mut self) -> Rc { - self.mark_location(); - let asdoc = self.parse_asdoc(); - self.non_greedy_expect(Token::Package); - let mut name = vec![]; - if let Some(name1) = self.consume_identifier(false) { - name.push(name1.clone()); - while self.consume(Token::Dot) { - name.push(self.expect_identifier(true)); - } - } - let block = Rc::new(self.parse_block(ParserDirectiveContext::PackageBlock)); - Rc::new(PackageDefinition { - location: self.pop_location(), - asdoc, - name, - block, - }) - } - - pub fn parse_program(&mut self) -> Rc { - self.mark_location(); - let just_eof = self.peek(Token::Eof); - let mut packages = vec![]; - while self.peek(Token::Package) { - packages.push(self.parse_package_definition()); - } - let directives = self.parse_directives(ParserDirectiveContext::TopLevel); - Rc::new(Program { - location: if just_eof { - self.pop_location(); - self.token.1.clone() - } else { - self.pop_location() - }, - packages, - directives, - }) - } - - pub fn parse_asdoc(&mut self) -> Option> { - let comments = self.compilation_unit().comments.borrow(); - let last_comment = comments.last().map(|last_comment| last_comment.clone()); - drop(comments); - last_comment.and_then(|comment| { - if comment.is_asdoc(&self.token.1) { - self.compilation_unit().comments_mut().pop(); - let location = comment.location(); - let comment_prefix_length: usize = 3; - let location1 = Location::with_offsets(self.compilation_unit(), location.first_offset + comment_prefix_length, location.last_offset - 2); - let content = &comment.content.borrow()[1..]; - let (main_body, tags) = self.parse_asdoc_content(&location1, content); - Some(Rc::new(AsDoc { - location, - main_body, - tags, - })) - } else { - None - } - }) - } - - fn parse_asdoc_content(&mut self, location: &Location, content: &str) -> (Option<(String, Location)>, Vec<(AsDocTag, Location)>) { - let lines = self.split_asdoc_lines(location, content); - - let mut main_body: Option<(String, Location)> = None; - let mut tags: Vec<(AsDocTag, Location)> = vec![]; - let mut i = 0; - let line_count = lines.len(); - - let mut building_content_tag_name: Option<(String, Location)> = None; - let mut building_content: Vec<(String, Location)> = vec![]; - let mut inside_code_block = false; - - while i < line_count { - let line = &lines[i]; - let tag = if inside_code_block { None } else { - regex_captures!(r"^([\s\t]*\@)([^\s\t]+)(.*)", &line.content) - }; - if let Some((_, tag_prefix, tag_name, tag_content)) = tag { - self.parse_asdoc_tag_or_main_body( - &mut building_content_tag_name, - &mut building_content, - &mut main_body, - &mut tags, - ); - if regex_is_match!(r"^[\s\t]*```([^`]|$)", &tag_content) { - inside_code_block = true; - } - let tag_name_location = Location::with_offsets(self.compilation_unit(), line.location.first_offset() + tag_prefix.len() - 1, line.location.first_offset() + tag_prefix.len() + tag_name.len()); - building_content_tag_name = Some((tag_name.into(), tag_name_location)); - let tag_content_location = Location::with_offsets(self.compilation_unit(), line.location.first_offset() + tag_prefix.len() + tag_name.len(), line.location.last_offset()); - building_content.push((tag_content.into(), tag_content_location)); - - if ["private", "inheritDoc"].contains(&tag_name) { - self.parse_asdoc_tag_or_main_body( - &mut building_content_tag_name, - &mut building_content, - &mut main_body, - &mut tags, - ); - building_content_tag_name = None; - building_content.clear(); - } - } else { - if regex_is_match!(r"^[\s\t]*```([^`]|$)", &line.content) { - inside_code_block = !inside_code_block; - } - building_content.push((line.content.clone(), line.location.clone())); - } - i += 1; - } - - self.parse_asdoc_tag_or_main_body( - &mut building_content_tag_name, - &mut building_content, - &mut main_body, - &mut tags, - ); - - (main_body, tags) - } - - fn split_asdoc_lines(&mut self, location: &Location, content: &str) -> Vec { - let mut builder = String::new(); - let mut lines = vec![]; - let mut _line_number = location.first_line_number(); - let mut index = location.first_offset(); - let mut line_first_offset = index; - let mut characters = content.chars(); - while let Some(ch) = characters.next() { - if CharacterValidator::is_line_terminator(ch) { - lines.push(ParserAsDocLine { - content: builder, - location: Location::with_offsets(self.compilation_unit(), line_first_offset, index), - }); - index += ch.len_utf8(); - // sequence - if ch == '\r' && characters.clone().next().unwrap_or('\x00') == '\n' { - index += '\n'.len_utf8(); - characters.next(); - } - builder = String::new(); - _line_number += 1; - line_first_offset = index; - } else { - builder.push(ch); - index += ch.len_utf8(); - } - } - lines.push(ParserAsDocLine { - content: builder, - location: Location::with_offsets(self.compilation_unit(), line_first_offset, index), - }); - for line in &mut lines { - let line_content = line.content.to_owned(); - let prefix = regex_captures!(r"^\s*(\*\s?)", &line_content); - if let Some((prefix, _)) = prefix { - line.content = line.content[prefix.len()..].to_owned(); - line.location = Location::with_offsets(self.compilation_unit(), line.location.first_offset() + prefix.len(), line.location.last_offset()); - } - } - - lines - } - - fn parse_asdoc_tag_or_main_body( - &self, - building_content_tag_name: &mut Option<(String, Location)>, - building_content: &mut Vec<(String, Location)>, - main_body: &mut Option<(String, Location)>, - tags: &mut Vec<(AsDocTag, Location)> - ) { - if let Some((tag_name, ref tag_location)) = building_content_tag_name.as_ref() { - match tag_name.as_ref() { - // @author Author text - "author" => { - let (content, location) = join_asdoc_content(building_content); - // Content must be non empty - if regex_is_match!(r"^\s*$", &content) { - self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagarg![tag_name.clone()]); - } - let location = tag_location.combine_with(location); - tags.push((AsDocTag::Author(content), location)); - }, - - // @copy reference - "copy" => { - let (content, c_location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(c_location.clone()); - let reference_loc = c_location.shift_whitespace(&self.compilation_unit().text()[c_location.first_offset()..c_location.last_offset()]); - if let Some(reference) = self.parse_asdoc_reference(&content, &reference_loc, &tag_location, &tag_name) { - tags.push((AsDocTag::Copy(reference), location)); - } - }, - - // @created Date text - "created" => { - let (content, location) = join_asdoc_content(building_content); - // Content must be non empty - if regex_is_match!(r"^\s*$", &content) { - self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagarg![tag_name.clone()]); - } - let location = tag_location.combine_with(location); - tags.push((AsDocTag::Created(content), location)); - }, - - // @default value - "default" => { - let (reference, location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(location); - tags.push((AsDocTag::Default(reference), location)); - }, - - // @deprecated - "deprecated" => { - let (text, location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(location); - - let mut message: Option = None; - - if !regex_is_match!(r"^\s*$", &text) { - message = Some(text.clone()); - } - - tags.push((AsDocTag::Deprecated { message }, location)); - }, - - // @eventType typeOrConstant - "eventType" => { - let (_, c_location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(c_location.clone()); - let reference_loc = c_location.shift_whitespace(&self.compilation_unit().text()[c_location.first_offset()..c_location.last_offset()]); - let parser_options = ParserOptions { - byte_range: Some((reference_loc.first_offset(), reference_loc.last_offset())), - ..self.options() - }; - let exp = ParserFacade(self.compilation_unit(), parser_options).parse_expression(); - tags.push((AsDocTag::EventType(exp), location)); - }, - - // @example text - "example" => { - let (text, location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(location); - tags.push((AsDocTag::Example(text), location)); - }, - - // @inheritDoc - "inheritDoc" => { - let (text, location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(location); - - // Content must be empty - if !regex_is_match!(r"^\s*$", &text) { - self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagarg![tag_name.clone()]); - } - - tags.push((AsDocTag::InheritDoc, location)); - }, - - // @internal text - "internal" => { - let (text, location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(location); - - // Content must be non empty - if regex_is_match!(r"^\s*$", &text) { - self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagarg![tag_name.clone()]); - } - - tags.push((AsDocTag::Internal(text), location)); - }, - - // @langversion text - "langversion" => { - let (text, location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(location); - - // Content must be non empty - if regex_is_match!(r"^\s*$", &text) { - self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagarg![tag_name.clone()]); - } - - tags.push((AsDocTag::Langversion(text), location)); - }, - - // @param paramName description - "param" => { - let (content, location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(location); - - if let Some((_, name, description)) = regex_captures!(r"(?x) ([^\s]+) (.*)", &content) { - tags.push((AsDocTag::Param { name: name.into(), description: description.trim_start().into() }, location)); - } else { - tags.push((AsDocTag::Param { name: content, description: "".into() }, location)); - } - }, - - // @playerversion text - "playerversion" => { - let (text, location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(location); - - // Content must be non empty - if regex_is_match!(r"^\s*$", &text) { - self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagarg![tag_name.clone()]); - } - - tags.push((AsDocTag::Playerversion(text), location)); - }, - - // @private - "private" => { - let (text, location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(location); - - // Content must be empty - if !regex_is_match!(r"^\s*$", &text) { - self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagarg![tag_name.clone()]); - } - - tags.push((AsDocTag::Private, location)); - }, - - // @productversion text - "productversion" => { - let (text, location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(location); - - // Content must be non empty - if regex_is_match!(r"^\s*$", &text) { - self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagarg![tag_name.clone()]); - } - - tags.push((AsDocTag::Productversion(text), location)); - }, - - // @return text - "return" => { - let (text, location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(location); - tags.push((AsDocTag::Return(text), location)); - }, - - // @see reference [displayText] - "see" => { - let (content, c_location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(c_location.clone()); - let reference: String; - let display_text: Option; - let mut reference_loc = c_location.shift_whitespace(&self.compilation_unit().text()[c_location.first_offset()..c_location.last_offset()]); - if let Some((_, reference_1, display_text_1)) = regex_captures!(r"(?x) ([^\s]+) (.*)", &content) { - reference = reference_1.to_owned(); - reference_loc = Location::with_offsets(self.compilation_unit(), reference_loc.first_offset(), reference_loc.first_offset() + reference.len()); - display_text = Some(display_text_1.trim().to_owned()); - } else { - reference = content; - display_text = None; - } - if let Some(reference) = self.parse_asdoc_reference(&reference, &reference_loc, &tag_location, &tag_name) { - tags.push((AsDocTag::See { reference, display_text }, location)); - } - }, - - // @throws className description - "throws" => { - let (class_name_and_description, c_location) = join_asdoc_content(building_content); - let location = tag_location.combine_with(c_location.clone()); - - let class_name_and_description = regex_captures!(r"^([^\s]+)(\s.*)?", &class_name_and_description); - - if let Some((_, class_name, description)) = class_name_and_description { - let description = description.trim().to_owned(); - let description = if description.is_empty() { - None - } else { - Some(description) - }; - let mut reference_loc = c_location.shift_whitespace(&self.compilation_unit().text()[c_location.first_offset()..c_location.last_offset()]); - reference_loc = Location::with_offsets(self.compilation_unit(), reference_loc.first_offset(), reference_loc.first_offset() + class_name.len()); - let parser_options = ParserOptions { - byte_range: Some((reference_loc.first_offset(), reference_loc.last_offset())), - ..self.options() - }; - let exp = ParserFacade(self.compilation_unit(), parser_options).parse_type_expression(); - tags.push((AsDocTag::Throws { class_reference: exp, description }, location)); - } else { - self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagarg![tag_name.clone()]); - } - }, - - // @version Version text - "version" => { - let (content, location) = join_asdoc_content(building_content); - // Content must be non empty - if regex_is_match!(r"^\s*$", &content) { - self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagarg![tag_name.clone()]); - } - let location = tag_location.combine_with(location); - tags.push((AsDocTag::Version(content), location)); - }, - - // Unrecognized tag - _ => { - self.add_syntax_error(&tag_location, DiagnosticKind::UnrecognizedAsDocTag, diagarg![tag_name.clone()]); - }, - } - } else if !building_content.is_empty() { - let content = join_asdoc_content(building_content); - if !content.0.is_empty() { - *main_body = Some(content); - } - } - - *building_content_tag_name = None; - building_content.clear(); - } - - fn parse_asdoc_reference(&self, reference: &str, reference_loc: &Location, tag_location: &Location, tag_name: &str) -> Option> { - let split: Vec<&str> = reference.split("#").collect(); - if split.len() > 2 { - self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagarg![tag_name.to_owned()]); - return None; - } - let mut base: Option> = None; - let base_text: String = split[0].to_owned(); - let instance_property_text: Option<(String, Location)> = split.get(1).and_then(|&f| if f.is_empty() { None } else { - Some((f.to_owned(), Location::with_offsets(self.compilation_unit(), reference_loc.first_offset() + base_text.len() + 1, reference_loc.last_offset()))) - }); - - if !base_text.is_empty() { - let parser_options = ParserOptions { - byte_range: Some((reference_loc.first_offset(), reference_loc.first_offset() + base_text.len())), - ..self.options() - }; - let exp = ParserFacade(self.compilation_unit(), parser_options).parse_expression(); - base = Some(exp); - } - - let mut instance_property: Option> = None; - if let Some(text) = instance_property_text { - let parser_options = ParserOptions { - byte_range: Some((text.1.first_offset(), text.1.last_offset())), - ..self.options() - }; - let exp = ParserFacade(self.compilation_unit(), parser_options).parse_qualified_identifier(); - instance_property = Some(Rc::new(exp)); - } - - if base.is_none() && instance_property.is_none() { - self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagarg![tag_name.to_owned()]); - return None; - } - Some(Rc::new(AsDocReference { base, instance_property, })) - } - - /// Parses MXMLElement starting from its XMLTagContent. - fn parse_mxml_element(&mut self, start: Location, namespace: &Rc, encoding: &mut String) -> MxmlElement { - self.push_location(&start); - let namespace = Rc::new(MxmlNamespace::new(Some(namespace))); - let name = self.parse_xml_name(); - let mut attributes: Vec> = vec![]; - let mut plain_attributes: Vec = vec![]; - while self.consume_and_ie_xml_tag(Token::XmlWhitespace) { - if matches!(self.token.0, Token::XmlName(_)) { - self.mark_location(); - let name = self.parse_xml_name(); - self.consume_and_ie_xml_tag(Token::XmlWhitespace); - self.non_greedy_expect_and_ie_xml_tag(Token::Assign); - let mut value = ("".into(), self.token.1.clone()); - if !self.expecting_token_error { - self.consume_and_ie_xml_tag(Token::XmlWhitespace); - value = self.parse_xml_attribute_value(); - } - let attrib = PlainMxmlAttribute { - location: self.pop_location(), - name, - value, - }; - self.process_mxml_xmlns_attribute(&mut attributes, &attrib, &namespace); - plain_attributes.push(attrib); - } else { - break; - } - } - - for attrib in &plain_attributes { - self.process_mxml_attribute(&mut attributes, &attrib, &namespace); - } - - let name = self.process_mxml_tag_name(name, &namespace); - - let mut content: Option>> = None; - let mut closing_name: Option = None; - - let is_empty = self.consume_and_ie_xml_content(Token::XmlSlashGt); - - if !is_empty { - self.expect_and_ie_xml_content(Token::Gt); - content = Some(self.parse_mxml_content(false, &namespace, encoding)); - self.non_greedy_expect_and_ie_xml_tag(Token::XmlLtSlash); - let name_1 = self.parse_xml_name(); - let closing_name_1 = self.process_mxml_tag_name(name_1, &namespace); - if let Ok(equal) = name.equals_name(&closing_name_1, &namespace) { - if !equal { - self.add_syntax_error(&closing_name_1.location, DiagnosticKind::XmlClosingTagNameMustBeEquals, diagarg![name.to_string(&namespace)]); - } - } - closing_name = Some(closing_name_1); - self.consume_and_ie_xml_tag(Token::XmlWhitespace); - self.non_greedy_expect_and_ie_xml_content(Token::Gt); - } - - if let Some(content) = content.as_mut() { - self.filter_mxml_whitespace_out(content); - } - - MxmlElement { - location: self.pop_location(), - name, - attributes, - content, - closing_name, - namespace, - } - } - - /// Filters whitespace chunks out of a content list when - /// they include at least one child element. - fn filter_mxml_whitespace_out(&self, content: &mut Vec>) { - if !self.ignore_xml_whitespace { - return; - } - let mut inc_el = false; - for node in content.iter() { - inc_el = matches!(node.as_ref(), MxmlContent::Element(_)); - if inc_el { - break; - } - } - if inc_el { - let mut indices: Vec = vec![]; - for i in 0..content.len() { - let MxmlContent::Characters((ch, _)) = content[i].as_ref() else { - continue; - }; - if ch.trim().is_empty() { - indices.push(i); - } - } - for i in indices.iter().rev() { - content.remove(*i); - } - } - } - - fn process_mxml_xmlns_attribute(&mut self, output: &mut Vec>, attribute: &PlainMxmlAttribute, namespace: &Rc) { - // xml="uri" - if attribute.name.0 == "xmlns" { - let attribute_value = unescape_xml(&attribute.value.0); - namespace.set(MxmlNamespace::DEFAULT_NAMESPACE, &attribute_value); - output.push(Rc::new(MxmlAttribute { - location: attribute.location.clone(), - name: MxmlName { - location: attribute.name.1.clone(), - prefix: None, - name: "xmlns".into(), - }, - value: (attribute_value, attribute.value.1.clone()), - xmlns: true, - })); - // xmlns:prefix="uri" - } else if attribute.name.0.starts_with("xmlns:") { - let attribute_value = unescape_xml(&attribute.value.0); - namespace.set(&attribute.name.0[6..], &attribute_value); - if attribute.name.0[6..].find(':').is_some() { - self.add_syntax_error(&attribute.name.1, DiagnosticKind::XmlNameAtMostOneColon, vec![]); - } - output.push(Rc::new(MxmlAttribute { - location: attribute.location.clone(), - name: MxmlName { - location: attribute.name.1.clone(), - prefix: Some("xmlns".into()), - name: attribute.name.0[6..].to_owned(), - }, - value: (attribute_value, attribute.value.1.clone()), - xmlns: true, - })); - } - } - - fn process_mxml_attribute(&mut self, output: &mut Vec>, attribute: &PlainMxmlAttribute, namespace: &Rc) { - // attrib="value" - if !(attribute.name.0 == "xmlns" || attribute.name.0.starts_with("xmlns:")) { - let attribute_value = unescape_xml(&attribute.value.0); - let split = attribute.name.0.split(':').collect::>(); - if split.len() > 2 { - self.add_syntax_error(&attribute.name.1, DiagnosticKind::XmlNameAtMostOneColon, vec![]); - } - let prefix: Option = if split.len() > 1 { - Some(split[split.len() - 2].to_owned()) - } else { - None - }; - let name = split.last().unwrap(); - let attrib = Rc::new(MxmlAttribute { - location: attribute.location.clone(), - name: MxmlName { - location: attribute.name.1.clone(), - prefix, - name: (*name).to_owned(), - }, - value: (attribute_value, attribute.value.1.clone()), - xmlns: false, - }); - match attrib.name.resolve_prefix(namespace) { - Ok(_) => { - for prev_attrib in output.iter() { - if prev_attrib.name.equals_name(&attrib.name, namespace).unwrap_or(false) { - self.add_syntax_error(&attrib.name.location, DiagnosticKind::RedefiningXmlAttribute, diagarg![attrib.name.name.clone()]); - } - } - }, - Err(MxmlNameError::PrefixNotDefined(prefix)) => { - self.add_syntax_error(&attrib.name.location, DiagnosticKind::XmlPrefixNotDefined, diagarg![prefix]); - }, - } - output.push(attrib); - } - } - - fn process_mxml_tag_name(&mut self, name: (String, Location), namespace: &Rc) -> MxmlName { - let split = name.0.split(':').collect::>(); - if split.len() > 2 { - self.add_syntax_error(&name.1, DiagnosticKind::XmlNameAtMostOneColon, vec![]); - } - let prefix: Option = if split.len() > 1 { - Some(split[split.len() - 2].to_owned()) - } else { - None - }; - let name_str = split.last().unwrap(); - let name = MxmlName { - location: name.1.clone(), - prefix, - name: (*name_str).to_owned(), - }; - match name.resolve_prefix(namespace) { - Ok(_) => {}, - Err(MxmlNameError::PrefixNotDefined(prefix)) => { - self.add_syntax_error(&name.location, DiagnosticKind::XmlPrefixNotDefined, diagarg![prefix]); - }, - } - name - } - - /// Parses XMLContent until either the `, encoding: &mut String) -> Vec> { - let mut content = vec![]; - while if until_eof { self.tokenizer.characters().has_remaining() } else { !self.peek(Token::XmlLtSlash) } { - if let Token::XmlMarkup(markup) = self.token.0.clone() { - let location = self.token_location(); - self.next_ie_xml_content(); - // XMLCDATA - if markup.starts_with(" { - self.add_syntax_error(&location, DiagnosticKind::XmlPiUnknownAttribute, diagarg![name.clone()]); - }, - XmlPiError::Version => { - self.add_syntax_error(&location, DiagnosticKind::XmlPiVersion, vec![]); - }, - XmlPiError::Encoding => { - self.add_syntax_error(&location, DiagnosticKind::XmlPiEncoding, vec![]); - }, - } - } - content.push(Rc::new(MxmlContent::ProcessingInstruction { - location, - name, - data: if data.is_empty() { None } else { Some(data) }, - })); - } - } else if let Token::XmlText(text) = self.token.0.clone() { - let location = self.token_location(); - self.next_ie_xml_content(); - content.push(Rc::new(MxmlContent::Characters((unescape_xml(&text), location)))); - } else if self.consume_and_ie_xml_tag(Token::Lt) { - let start = self.token_location(); - let element = self.parse_mxml_element(start, namespace, encoding); - content.push(Rc::new(MxmlContent::Element(Rc::new(element)))); - } else if !until_eof { - self.non_greedy_expect_and_ie_xml_content(Token::XmlLtSlash); - if !self.tokenizer.characters().has_remaining() { - break; - } - } else if self.peek(Token::XmlLtSlash) { - self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![Token::Eof, self.token.0.clone()]); - self.next_ie_xml_tag(); - let _ = self.parse_xml_name(); - self.consume_and_ie_xml_tag(Token::XmlWhitespace); - self.non_greedy_expect_and_ie_xml_content(Token::Gt); - } - } - content - } - - fn parse_mxml(&mut self) -> Rc { - self.mark_location(); - let ns = Rc::new(MxmlNamespace::new(None)); - let mut encoding = "utf-8".to_owned(); - let mut content = self.parse_mxml_content(true, &ns, &mut encoding); - self.filter_mxml_whitespace_out(&mut content); - - let mut element_count = 0usize; - let mut character_count = 0usize; - - for node in content.iter() { - match node.as_ref() { - MxmlContent::Characters(_) | - MxmlContent::CData(_) => { - character_count += 1; - }, - MxmlContent::Element(_) => { - element_count += 1; - }, - _ => {}, - } - } - let location = self.pop_location(); - if element_count != 1 || character_count != 0 { - self.add_syntax_error(&location, DiagnosticKind::XmlMustConsistOfExactly1Element, vec![]); - } - Rc::new(Mxml { - location, - version: XmlVersion::Version10, - encoding, - content, - }) - } -} - -fn parse_include_directive_source(nested_compilation_unit: Rc, context: ParserDirectiveContext) -> (Vec>, Vec>) { - let mut parser = Parser::new(&nested_compilation_unit, &ParserOptions { - ..default() - }); - parser.next(); - let mut packages = vec![]; - if matches!(context, ParserDirectiveContext::TopLevel) { - while parser.peek(Token::Package) { - packages.push(parser.parse_package_definition()); - } - } - (packages, parser.parse_directives(context)) -} - -fn join_asdoc_content(content: &Vec<(String, Location)>) -> (String, Location) { - // Ignore first empty lines - let mut i = 0usize; - for content1 in content.iter() { - if content1.0.trim().is_empty() { - i += 1; - } else { - break; - } - } - - // Ignore last empty lines - let mut j = content.len(); - for content1 in content.iter().rev() { - if content1.0.trim().is_empty() { - j -= 1; - } else { - break; - } - } - - if i > j { - i = j; - } - - let s: Vec = content[i..j].iter().map(|c| c.0.clone()).collect(); - let s = s.join("\n").trim().to_owned(); - let location = if i == j { - content[i].1.clone() - } else { - content[i].1.combine_with(content[i..j].last().unwrap().1.clone()) - }; - (s, location) -} - -fn process_xml_pi(cu: &Rc, byte_range: (usize, usize), name: &str, encoding: &mut String) -> Vec { - if name != "xml" { - return vec![]; - } - let mut parser = Parser::new(&cu, &ParserOptions { - byte_range: Some(byte_range), - ..default() - }); - let mut errors = Vec::::new(); - parser.next_ie_xml_tag(); - while parser.consume_and_ie_xml_tag(Token::XmlWhitespace) { - if matches!(parser.token.0, Token::XmlName(_)) { - let name = parser.parse_xml_name(); - parser.consume_and_ie_xml_tag(Token::XmlWhitespace); - parser.expect_and_ie_xml_tag(Token::Assign); - parser.consume_and_ie_xml_tag(Token::XmlWhitespace); - let value = parser.parse_xml_attribute_value(); - match name.0.as_ref() { - "version" => { - if value.0 != "1.0" { - errors.push(XmlPiError::Version); - } - }, - "encoding" => { - let v = value.0.to_lowercase(); - if ["utf-8", "utf-16"].contains(&v.as_str()) { - *encoding = v; - } else { - errors.push(XmlPiError::Encoding); - } - }, - _ => { - errors.push(XmlPiError::UnknownAttribute(name.0.clone())); - }, - } - } else { - break; - } - } - parser.expect_eof(); - errors -} - -fn is_flex_documentable_meta_data(name: &str) -> bool { - ["Event", "SkinState"].contains(&name) -} - -enum XmlPiError { - UnknownAttribute(String), - Version, - Encoding, -} - -struct ParserAsDocLine { - content: String, - location: Location, -} - -#[derive(Clone)] -struct ParserActivation { - uses_yield: bool, - uses_await: bool, -} - -impl ParserActivation { - pub fn new() -> Self { - Self { - uses_yield: false, - uses_await: false, - } - } -} - -#[derive(Clone)] -struct AnnotatableContext { - start_location: Location, - asdoc: Option>, - attributes: Vec, - context: ParserDirectiveContext, - /// Previous token as a directive context keyword. - directive_context_keyword: Option<(String, Location)>, -} - -impl AnnotatableContext { - pub fn has_directive_context_keyword(&self, name: &str) -> bool { - if let Some((ref k, _)) = self.directive_context_keyword { - k == name - } else { - false - } - } -} - -struct PlainMxmlAttribute { - pub location: Location, - pub name: (String, Location), - pub value: (String, Location), -} - -/// A simplified interface for executing the parser. -pub struct ParserFacade<'input>(pub &'input Rc, pub ParserOptions); - -pub struct ParserOptions { - /// For MXML, indicates whether to ignore XML whitespace chunks when at - /// least one element appears. Default: true. - pub ignore_xml_whitespace: bool, - /// Indicates the range of characters that shall be parsed, - /// the first and last byte indices respectively. - pub byte_range: Option<(usize, usize)>, -} - -impl Default for ParserOptions { - fn default() -> Self { - Self { - ignore_xml_whitespace: true, - byte_range: None, - } - } -} - -impl<'input> ParserFacade<'input> { - fn create_parser(&self) -> Parser<'input> { - Parser::new(self.0, &self.1) - } - - /// Parses `Program` until end-of-file. - pub fn parse_program(&self) -> Rc { - let mut parser = self.create_parser(); - parser.next(); - parser.parse_program() - } - - /// Parses `ListExpression^allowIn` and expects end-of-file. - pub fn parse_expression(&self) -> Rc { - let mut parser = self.create_parser(); - parser.next(); - let exp = parser.parse_expression(ParserExpressionContext { - ..default() - }); - parser.expect_eof(); - exp - } - - /// Parses a qualified identifier and expects end-of-file. - pub fn parse_qualified_identifier(&self) -> QualifiedIdentifier { - let mut parser = self.create_parser(); - parser.next(); - let exp = parser.parse_qualified_identifier(); - parser.expect_eof(); - exp - } - - /// Parses `TypeExpression` and expects end-of-file. - pub fn parse_type_expression(&self) -> Rc { - let mut parser = self.create_parser(); - parser.next(); - let exp = parser.parse_type_expression(); - parser.expect_eof(); - exp - } - - /// Parses `Directives` until end-of-file. - pub fn parse_directives(&self, context: ParserDirectiveContext) -> Vec> { - let mut parser = self.create_parser(); - parser.next(); - parser.parse_directives(context) - } - - /// Parses `Mxml` until end-of-file. - pub fn parse_mxml(&self) -> Rc { - let mut parser = self.create_parser(); - parser.next_ie_xml_content(); - parser.parse_mxml() - } -} diff --git a/crates/parser/parser/parser_error.rs b/crates/parser/parser/parser_error.rs deleted file mode 100644 index 7921630..0000000 --- a/crates/parser/parser/parser_error.rs +++ /dev/null @@ -1,20 +0,0 @@ -use crate::ns::*; - -/// Indicates a fatal syntax error that leads the parser -/// to complete without a resulting node. -#[derive(Copy, Clone, Debug)] -pub enum ParserError { - Common, -} - -/// Returns the identifier name that is specially reserved -/// for invalidated identifiers that could not be parsed. -pub const INVALIDATED_IDENTIFIER: &'static str = "\x00"; - -#[derive(Clone)] -pub(crate) enum MetadataRefineError { - Syntax, -} - -#[derive(Clone)] -pub(crate) struct MetadataRefineError1(pub MetadataRefineError, pub Location); diff --git a/crates/parser/parser/reserved_word.rs b/crates/parser/parser/reserved_word.rs deleted file mode 100644 index 9a04596..0000000 --- a/crates/parser/parser/reserved_word.rs +++ /dev/null @@ -1,110 +0,0 @@ -use crate::ns::*; - -/// ActionScript reserved word validation. -pub struct As3ReservedWord; - -impl As3ReservedWord { - /// Checks if an *IdentifierName* is a reserved word. - pub fn test(name: &str) -> bool { - As3ReservedWord::token(name).is_some() - } - - /// Attempts to convert an *IdentifierName* into a reserved word token. - pub fn token(name: &str) -> Option { - match name.len() { - 1 => None, - 2 => { - match name { - "as" => Some(Token::As), - "do" => Some(Token::Do), - "if" => Some(Token::If), - "in" => Some(Token::In), - "is" => Some(Token::Is), - _ => None, - } - }, - 3 => { - match name { - "for" => Some(Token::For), - "new" => Some(Token::New), - "not" => Some(Token::Not), - "try" => Some(Token::Try), - "use" => Some(Token::Use), - "var" => Some(Token::Var), - _ => None, - } - }, - 4 => { - match name { - "case" => Some(Token::Case), - "else" => Some(Token::Else), - "null" => Some(Token::Null), - "this" => Some(Token::This), - "true" => Some(Token::True), - "void" => Some(Token::Void), - "with" => Some(Token::With), - _ => None, - } - }, - 5 => { - match name { - "await" => Some(Token::Await), - "break" => Some(Token::Break), - "catch" => Some(Token::Catch), - "class" => Some(Token::Class), - "const" => Some(Token::Const), - "false" => Some(Token::False), - "super" => Some(Token::Super), - "throw" => Some(Token::Throw), - "while" => Some(Token::While), - "yield" => Some(Token::Yield), - _ => None, - } - }, - 6 => { - match name { - "delete" => Some(Token::Delete), - "import" => Some(Token::Import), - "public" => Some(Token::Public), - "return" => Some(Token::Return), - "switch" => Some(Token::Switch), - "typeof" => Some(Token::Typeof), - _ => None, - } - }, - 7 => { - match name { - "default" => Some(Token::Default), - "extends" => Some(Token::Extends), - "finally" => Some(Token::Finally), - "package" => Some(Token::Package), - "private" => Some(Token::Private), - _ => None, - } - }, - 8 => { - match name { - "continue" => Some(Token::Continue), - "function" => Some(Token::Function), - "internal" => Some(Token::Internal), - _ => None, - } - }, - 9 => { - match name { - "interface" => Some(Token::Interface), - "protected" => Some(Token::Protected), - _ => None, - } - }, - 10 => { - match name { - "implements" => Some(Token::Implements), - "instanceof" => Some(Token::Instanceof), - _ => None, - } - }, - _ => None, - } - } -} \ No newline at end of file diff --git a/crates/parser/parser/token.rs b/crates/parser/parser/token.rs deleted file mode 100644 index f0913ae..0000000 --- a/crates/parser/parser/token.rs +++ /dev/null @@ -1,470 +0,0 @@ -use crate::ns::*; - -/// Represents a lexical token. -#[derive(Clone, PartialEq, Debug)] -pub enum Token { - Eof, - Identifier(String), - String(String), - /// Numeric literal token. - /// The numeric value is in character representation, which may be parsed - /// through data type specific methods such as [`NumericLiteral::parse_double()`]. - Number(String, NumberSuffix), - RegExp { - body: String, - flags: String, - }, - - CssNumber { - value: f64, - unit: Option, - }, - CssHashWord(String), - CssBeginsWith, - CssEndsWith, - CssContains, - CssListMatch, - CssHreflangMatch, - CssAtNamespace, - CssAtMedia, - CssAtFontFace, - CssImportant, - CssSemicolons, - - // Punctuator - ColonColon, - /// The `@` token. - Attribute, - /// The `..` token. - Descendants, - /// The `...` token. - Ellipsis, - ParenOpen, - ParenClose, - SquareOpen, - SquareClose, - BlockOpen, - BlockClose, - Dot, - Semicolon, - Comma, - Lt, - Gt, - /// `<=` - Le, - /// `>=` - Ge, - Equals, - NotEquals, - StrictEquals, - StrictNotEquals, - Plus, - Minus, - Times, - Div, - Percent, - Increment, - Decrement, - LeftShift, - RightShift, - UnsignedRightShift, - Ampersand, - Hat, - Pipe, - Tilde, - LogicalAnd, - LogicalXor, - LogicalOr, - Question, - Exclamation, - Colon, - Assign, - AddAssign, - SubtractAssign, - MultiplyAssign, - DivideAssign, - RemainderAssign, - LeftShiftAssign, - RightShiftAssign, - UnsignedRightShiftAssign, - BitwiseAndAssign, - BitwiseXorAssign, - BitwiseOrAssign, - LogicalAndAssign, - LogicalXorAssign, - LogicalOrAssign, - /// `**` - Power, - /// `**=` - PowerAssign, - /// `??` - NullCoalescing, - /// `??=` - NullCoalescingAssign, - /// `?.` - OptionalChaining, - - // Reserved words - As, - Await, - Break, - Case, - Catch, - Class, - Const, - Continue, - Default, - Delete, - Do, - Else, - Extends, - False, - Finally, - For, - Function, - If, - Implements, - Import, - In, - Instanceof, - Interface, - Internal, - Is, - New, - Not, - Null, - Package, - Private, - Protected, - Public, - Return, - Super, - Switch, - This, - Throw, - True, - Try, - Typeof, - Use, - Var, - Void, - While, - With, - Yield, - - XmlWhitespace, - XmlLtSlash, - XmlSlashGt, - XmlText(String), - XmlName(String), - XmlMarkup(String), - XmlAttributeValue(String), -} - -impl ToString for Token { - /// Converts the token into a readable string. - /// - /// The method `Token::to_string` returns the following possible values: - /// - /// * `"end of program"` - /// * `"identifier"` - /// * `"string"` for string literal - /// * `"number"` for numeric literal - /// * `"regular expression"` for regular expression literal - /// * `"'keyword'"` for reserved words - /// * `"'punctuator'"` for various punctuators - /// * `"punctuator"` for various punctuators - /// * `"XML whitespace"` - /// * `"''"` - /// * `"XML text"` - /// * `"XML name"` - /// * `"XML markup"` - /// * `"XML attribute value"` - fn to_string(&self) -> String { - (match self { - Token::Eof => "end-of-file", - Token::Identifier(_) => "identifier", - Token::String(_) => "string", - Token::Number(_, _) => "number", - Token::RegExp { .. } => "regular expression", - - Token::CssNumber { .. } => "number", - Token::CssHashWord(_) => "hash-word", - Token::CssBeginsWith => "'^='", - Token::CssEndsWith => "'$='", - Token::CssContains => "'*='", - Token::CssListMatch => "'~='", - Token::CssHreflangMatch => "'|='", - Token::CssAtNamespace => "at-namespace", - Token::CssAtMedia => "at-media", - Token::CssAtFontFace => "at-font-face", - Token::CssImportant => "'!important'", - Token::CssSemicolons => "semicolon", - - // Punctuators - Token::ColonColon => "colon-colon", - Token::Attribute => "'@'", - Token::Descendants => "'..'", - Token::Ellipsis => "'...'", - Token::ParenOpen => "paren-open", - Token::ParenClose => "paren-close", - Token::SquareOpen => "square-open", - Token::SquareClose => "square-close", - Token::BlockOpen => "block-open", - Token::BlockClose => "block-close", - Token::Dot => "dot", - Token::Semicolon => "semicolon", - Token::Comma => "comma", - Token::Lt => "less-than", - Token::Gt => "greater-than", - Token::Le => "'<='", - Token::Ge => "'>='", - Token::Equals => "'=='", - Token::NotEquals => "'!='", - Token::StrictEquals => "'==='", - Token::StrictNotEquals => "'!=='", - Token::Plus => "plus", - Token::Minus => "minus", - Token::Times => "times", - Token::Div => "slash", - Token::Percent => "percent", - Token::Increment => "'++'", - Token::Decrement => "'--'", - Token::LeftShift => "'<<'", - Token::RightShift => "'>>'", - Token::UnsignedRightShift => "'>>>'", - Token::Ampersand => "ampersand", - Token::Hat => "hat", - Token::Pipe => "pipe", - Token::Tilde => "tilde", - Token::LogicalAnd => "'&&'", - Token::LogicalXor => "'^^'", - Token::LogicalOr => "'||'", - Token::Question => "question-mark", - Token::Exclamation => "exclamation-mark", - Token::Colon => "colon", - Token::Assign => "'='", - Token::AddAssign => "'+='", - Token::SubtractAssign => "'-='", - Token::MultiplyAssign => "'*='", - Token::DivideAssign => "'/='", - Token::RemainderAssign => "'%='", - Token::LeftShiftAssign => "'<<='", - Token::RightShiftAssign => "'>>='", - Token::UnsignedRightShiftAssign => "'>>>='", - Token::BitwiseAndAssign => "'&='", - Token::BitwiseXorAssign => "'^='", - Token::BitwiseOrAssign => "'|='", - Token::LogicalAndAssign => "'&&='", - Token::LogicalXorAssign => "'^^='", - Token::LogicalOrAssign => "'||='", - Token::Power => "'**'", - Token::PowerAssign => "'**='", - Token::NullCoalescing => "'??'", - Token::NullCoalescingAssign => "'??='", - Token::OptionalChaining => "'?.'", - - // Reserved words - Token::As => "'as'", - Token::Await => "'await'", - Token::Break => "'break'", - Token::Case => "'case'", - Token::Catch => "'catch'", - Token::Class => "'class'", - Token::Const => "'const'", - Token::Continue => "'continue'", - Token::Default => "'default'", - Token::Delete => "'delete'", - Token::Do => "'do'", - Token::Else => "'else'", - Token::Extends => "'extends'", - Token::False => "'false'", - Token::Finally => "'finally'", - Token::For => "'for'", - Token::Function => "'function'", - Token::If => "'if'", - Token::Implements => "'implements'", - Token::Import => "'import'", - Token::In => "'in'", - Token::Instanceof => "'instanceof'", - Token::Interface => "'interface'", - Token::Internal => "'internal'", - Token::Is => "'is'", - Token::New => "'new'", - Token::Not => "'not'", - Token::Null => "'null'", - Token::Package => "'package'", - Token::Private => "'private'", - Token::Protected => "'protected'", - Token::Public => "'public'", - Token::Return => "'return'", - Token::Super => "'super'", - Token::Switch => "'switch'", - Token::This => "'this'", - Token::Throw => "'throw'", - Token::True => "'true'", - Token::Try => "'try'", - Token::Typeof => "'typeof'", - Token::Use => "'use'", - Token::Var => "'var'", - Token::Void => "'void'", - Token::While => "'while'", - Token::With => "'with'", - Token::Yield => "'yield'", - - Token::XmlWhitespace => "XML whitespace", - Token::XmlLtSlash => "' "'/>'", - Token::XmlText(_) => "XML text", - Token::XmlName(_) => "XML name", - Token::XmlMarkup(_) => "XML markup", - Token::XmlAttributeValue(_) => "XML attribute value", - }).into() - } -} - -impl Token { - pub fn is_context_keyword(token: &(Token, Location), keyword: &str) -> bool { - if let Token::Identifier(name) = &token.0 { - name == keyword && token.1.character_count() == name.len() - } else { - false - } - } - - /// Indicates whether the token is a reserved word. - pub fn is_reserved_word(&self) -> bool { - self.reserved_word_name().is_some() - } - - pub fn is_identifier_name(&self) -> bool { - matches!(self, Token::Identifier(_)) || self.is_reserved_word() - } - - /// Tests whether the token is a reserved word and returns - /// its *IdentifierName* string. - pub fn reserved_word_name(&self) -> Option { - match *self { - Token::As => Some("as".into()), - Token::Await => Some("await".into()), - Token::Break => Some("break".into()), - Token::Case => Some("case".into()), - Token::Catch => Some("catch".into()), - Token::Class => Some("class".into()), - Token::Const => Some("const".into()), - Token::Continue => Some("continue".into()), - Token::Default => Some("default".into()), - Token::Delete => Some("delete".into()), - Token::Do => Some("do".into()), - Token::Else => Some("else".into()), - Token::Extends => Some("extends".into()), - Token::False => Some("false".into()), - Token::Finally => Some("finally".into()), - Token::For => Some("for".into()), - Token::Function => Some("function".into()), - Token::If => Some("if".into()), - Token::Implements => Some("implements".into()), - Token::Import => Some("import".into()), - Token::In => Some("in".into()), - Token::Instanceof => Some("instanceof".into()), - Token::Interface => Some("interface".into()), - Token::Internal => Some("internal".into()), - Token::Is => Some("is".into()), - Token::New => Some("new".into()), - Token::Not => Some("not".into()), - Token::Null => Some("null".into()), - Token::Package => Some("package".into()), - Token::Private => Some("private".into()), - Token::Protected => Some("protected".into()), - Token::Public => Some("public".into()), - Token::Return => Some("return".into()), - Token::Super => Some("super".into()), - Token::Switch => Some("switch".into()), - Token::This => Some("this".into()), - Token::Throw => Some("throw".into()), - Token::True => Some("true".into()), - Token::Try => Some("try".into()), - Token::Typeof => Some("typeof".into()), - Token::Use => Some("use".into()), - Token::Var => Some("var".into()), - Token::Void => Some("void".into()), - Token::While => Some("while".into()), - Token::With => Some("with".into()), - Token::Yield => Some("yield".into()), - _ => None, - } - } - - /// Converts a compound assignment, a logical assignment, or a nullish coalescing assignment to an *Operator* value. - pub fn compound_assignment(&self) -> Option { - match self { - Self::AddAssign => Some(Operator::Add), - Self::SubtractAssign => Some(Operator::Subtract), - Self::MultiplyAssign => Some(Operator::Multiply), - Self::DivideAssign => Some(Operator::Divide), - Self::RemainderAssign => Some(Operator::Remainder), - Self::PowerAssign => Some(Operator::Power), - Self::LeftShiftAssign => Some(Operator::ShiftLeft), - Self::RightShiftAssign => Some(Operator::ShiftRight), - Self::UnsignedRightShiftAssign => Some(Operator::ShiftRightUnsigned), - Self::BitwiseAndAssign => Some(Operator::BitwiseAnd), - Self::BitwiseXorAssign => Some(Operator::BitwiseXor), - Self::BitwiseOrAssign => Some(Operator::BitwiseOr), - Self::LogicalAndAssign => Some(Operator::LogicalAnd), - Self::LogicalXorAssign => Some(Operator::LogicalXor), - Self::LogicalOrAssign => Some(Operator::LogicalOr), - Self::NullCoalescingAssign => Some(Operator::NullCoalescing), - _ => None, - } - } - - /// Converts this token into a binary operator, excluding - /// `not in`, and `is not`. - pub fn to_binary_operator(&self) -> Option { - match self { - Self::Times => Some(Operator::Multiply), - Self::Div => Some(Operator::Divide), - Self::Percent => Some(Operator::Remainder), - Self::Plus => Some(Operator::Add), - Self::Minus => Some(Operator::Subtract), - Self::LeftShift => Some(Operator::ShiftLeft), - Self::RightShift => Some(Operator::ShiftRight), - Self::UnsignedRightShift => Some(Operator::ShiftRightUnsigned), - Self::Lt => Some(Operator::Lt), - Self::Gt => Some(Operator::Gt), - Self::Le => Some(Operator::Le), - Self::Ge => Some(Operator::Ge), - Self::As => Some(Operator::As), - Self::In => Some(Operator::In), - Self::Is => Some(Operator::Is), - Self::Instanceof => Some(Operator::Instanceof), - Self::Equals => Some(Operator::Equals), - Self::NotEquals => Some(Operator::NotEquals), - Self::StrictEquals => Some(Operator::StrictEquals), - Self::StrictNotEquals => Some(Operator::StrictNotEquals), - Self::Ampersand => Some(Operator::BitwiseAnd), - Self::Hat => Some(Operator::BitwiseXor), - Self::Pipe => Some(Operator::BitwiseOr), - Self::LogicalAnd => Some(Operator::LogicalAnd), - Self::LogicalXor => Some(Operator::LogicalXor), - Self::LogicalOr => Some(Operator::LogicalOr), - Self::NullCoalescing => Some(Operator::NullCoalescing), - Self::Power => Some(Operator::Power), - _ => None, - } - } - - pub(crate) fn to_attribute(&self, location: &Location) -> Option { - match self { - Self::Public => Some(Attribute::Public(location.clone())), - Self::Private => Some(Attribute::Private(location.clone())), - Self::Protected => Some(Attribute::Protected(location.clone())), - Self::Internal => Some(Attribute::Internal(location.clone())), - Self::Identifier(ref name) => { - Attribute::from_identifier_name(name, &location) - }, - _ => None, - } - } -} \ No newline at end of file diff --git a/crates/parser/parser/tokenizer.rs b/crates/parser/parser/tokenizer.rs deleted file mode 100644 index c1fb275..0000000 --- a/crates/parser/parser/tokenizer.rs +++ /dev/null @@ -1,1307 +0,0 @@ -use crate::ns::*; - -pub struct Tokenizer<'input> { - compilation_unit: Rc, - characters: CharacterReader<'input>, -} - -impl<'input> Tokenizer<'input> { - /// Constructs a tokenizer. - pub fn new(compilation_unit: &'input Rc, options: &ParserOptions) -> Self { - let text: &'input str = compilation_unit.text(); - let compilation_unit = compilation_unit.clone(); - let characters: CharacterReader<'input>; - if let Some(range) = options.byte_range { - characters = CharacterReader::from_offset(&text[0..range.1], range.0); - } else { - characters = CharacterReader::from(text); - } - Self { - compilation_unit, - characters, - } - } - - pub fn compilation_unit(&self) -> &Rc { - &self.compilation_unit - } - - pub fn characters(&self) -> &CharacterReader<'input> { - &self.characters - } - - fn add_syntax_error(&self, location: &Location, kind: DiagnosticKind, arguments: Vec>) { - if self.compilation_unit.prevent_equal_offset_error(location) { - return; - } - self.compilation_unit.add_diagnostic(Diagnostic::new_syntax_error(location, kind, arguments)); - } - - /// Scans for an *InputElementDiv* token. - pub fn scan_ie_div(&mut self) -> (Token, Location) { - loop { - let ch = self.characters.peek_or_zero(); - if CharacterValidator::is_whitespace(ch) { - self.characters.next(); - } else if self.consume_line_terminator() || self.consume_comment() { - // Consumed line terminator or comment - } else { - break; - } - } - if let Some(result) = self.scan_identifier() { - return result; - } - if let Some(result) = self.scan_dot_or_numeric_literal() { - return result; - } - if let Some(result) = self.scan_string_literal(false) { - return result; - } - let start = self.cursor_location(); - match self.characters.peek_or_zero() { - ',' => { - // Comma - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::Comma, location); - }, - '(' => { - // ParenOpen - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::ParenOpen, location); - }, - ')' => { - // ParenClose - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::ParenClose, location); - }, - '[' => { - // SquareOpen - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::SquareOpen, location); - }, - ']' => { - // SquareClose - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::SquareClose, location); - }, - '{' => { - // BlockOpen - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::BlockOpen, location); - }, - '}' => { - // BlockClose - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::BlockClose, location); - }, - ':' => { - self.characters.next(); - // ColonColon - if self.characters.peek_or_zero() == ':' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::ColonColon, location); - } - // Colon - let location = start.combine_with(self.cursor_location()); - return (Token::Colon, location); - }, - '=' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // StrictEquals - if ch == '=' && self.characters.peek_at_or_zero(1) == '=' { - self.characters.skip_count_in_place(2); - let location = start.combine_with(self.cursor_location()); - return (Token::StrictEquals, location); - } - // Equals - if ch == '=' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::Equals, location); - } - // Assign - let location = start.combine_with(self.cursor_location()); - return (Token::Assign, location); - }, - '!' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // StrictNotEquals - if ch == '=' && self.characters.peek_at_or_zero(1) == '=' { - self.characters.skip_count_in_place(2); - let location = start.combine_with(self.cursor_location()); - return (Token::StrictNotEquals, location); - } - // NotEquals - if ch == '=' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::NotEquals, location); - } - // Exclamation - let location = start.combine_with(self.cursor_location()); - return (Token::Exclamation, location); - }, - '?' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // OptionalChaining - if ch == '.' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::OptionalChaining, location); - } - // NullCoalescingAssign - if ch == '?' && self.characters.peek_at_or_zero(1) == '=' { - self.characters.skip_count_in_place(2); - let location = start.combine_with(self.cursor_location()); - return (Token::NullCoalescingAssign, location); - } - // NullCoalescing - if ch == '?' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::NullCoalescing, location); - } - // Question - let location = start.combine_with(self.cursor_location()); - return (Token::Question, location); - }, - ';' => { - // Semicolon - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::Semicolon, location); - }, - '<' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // Le - if ch == '=' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::Le, location); - } - // LeftShiftAssign - if ch == '<' && self.characters.peek_at_or_zero(1) == '=' { - self.characters.skip_count_in_place(2); - let location = start.combine_with(self.cursor_location()); - return (Token::LeftShiftAssign, location); - } - // LeftShift - if ch == '<' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::LeftShift, location); - } - // Lt - let location = start.combine_with(self.cursor_location()); - return (Token::Lt, location); - }, - '>' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // Ge - if ch == '=' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::Ge, location); - } - // RightShiftAssign - if ch == '>' && self.characters.peek_at_or_zero(1) == '=' { - self.characters.skip_count_in_place(2); - let location = start.combine_with(self.cursor_location()); - return (Token::RightShiftAssign, location); - } - // UnsignedRightShiftAssign - if ch == '>' && self.characters.peek_seq(3) == ">>=" { - self.characters.skip_count_in_place(3); - let location = start.combine_with(self.cursor_location()); - return (Token::UnsignedRightShiftAssign, location); - } - // UnsignedRightShift - if ch == '>' && self.characters.peek_at_or_zero(1) == '>' { - self.characters.skip_count_in_place(2); - let location = start.combine_with(self.cursor_location()); - return (Token::UnsignedRightShift, location); - } - // RightShift - if ch == '>' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::RightShift, location); - } - // Gt - let location = start.combine_with(self.cursor_location()); - return (Token::Gt, location); - }, - '@' => { - // Attribute - self.characters.next(); - if let Some(token) = self.scan_string_literal(true) { - return token; - } - let location = start.combine_with(self.cursor_location()); - return (Token::Attribute, location); - }, - '+' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // Increment - if ch == '+' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::Increment, location); - } - // AddAssign - if ch == '=' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::AddAssign, location); - } - // Plus - let location = start.combine_with(self.cursor_location()); - return (Token::Plus, location); - }, - '-' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // Decrement - if ch == '-' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::Decrement, location); - } - // SubtractAssign - if ch == '=' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::SubtractAssign, location); - } - // Minus - let location = start.combine_with(self.cursor_location()); - return (Token::Minus, location); - }, - '*' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // PowerAssign - if ch == '*' && self.characters.peek_at_or_zero(1) == '=' { - self.characters.skip_count_in_place(2); - let location = start.combine_with(self.cursor_location()); - return (Token::PowerAssign, location); - } - // Power - if ch == '*' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::Power, location); - } - // MultiplyAssign - if ch == '=' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::MultiplyAssign, location); - } - // Times - let location = start.combine_with(self.cursor_location()); - return (Token::Times, location); - }, - '/' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // DivideAssign - if ch == '=' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::DivideAssign, location); - } - // Div - let location = start.combine_with(self.cursor_location()); - return (Token::Div, location); - }, - '%' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // RemainderAssign - if ch == '=' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::RemainderAssign, location); - } - // Percent - let location = start.combine_with(self.cursor_location()); - return (Token::Percent, location); - }, - '&' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // LogicalAndAssign - if ch == '&' && self.characters.peek_at_or_zero(1) == '=' { - self.characters.skip_count_in_place(2); - let location = start.combine_with(self.cursor_location()); - return (Token::LogicalAndAssign, location); - } - // LogicalAnd - if ch == '&' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::LogicalAnd, location); - } - // BitwiseAndAssign - if ch == '=' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::BitwiseAndAssign, location); - } - // BitwiseAnd - let location = start.combine_with(self.cursor_location()); - return (Token::Ampersand, location); - }, - '^' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // LogicalXorAssign - if ch == '^' && self.characters.peek_at_or_zero(1) == '=' { - self.characters.skip_count_in_place(2); - let location = start.combine_with(self.cursor_location()); - return (Token::LogicalXorAssign, location); - } - // LogicalXor - if ch == '^' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::LogicalXor, location); - } - // BitwiseXorAssign - if ch == '=' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::BitwiseXorAssign, location); - } - // BitwiseXor - let location = start.combine_with(self.cursor_location()); - return (Token::Hat, location); - }, - '|' => { - self.characters.next(); - let ch = self.characters.peek_or_zero(); - // LogicalOrAssign - if ch == '|' && self.characters.peek_at_or_zero(1) == '=' { - self.characters.skip_count_in_place(2); - let location = start.combine_with(self.cursor_location()); - return (Token::LogicalOrAssign, location); - } - // LogicalOr - if ch == '|' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::LogicalOr, location); - } - // BitwiseOrAssign - if ch == '=' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::BitwiseOrAssign, location); - } - // BitwiseOr - let location = start.combine_with(self.cursor_location()); - return (Token::Pipe, location); - }, - '~' => { - // BitwiseNot - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::Tilde, location); - }, - _ => { - if self.characters.has_remaining() { - self.add_unexpected_error(); - self.characters.next(); - return self.scan_ie_div(); - // Eof - } else { - return (Token::Eof, start) - } - }, - } - } - - /// Scans regular expression after a `/` or `/=` token has been scanned by - /// `scan_ie_div`. - pub fn scan_regexp_literal(&mut self, start: Location, mut body: String) -> (Token, Location) { - loop { - let ch = self.characters.peek_or_zero(); - if ch == '/' { - self.characters.next(); - break; - } else if ch == '\\' { - self.characters.next(); - body.push('\\'); - let ch = self.characters.peek_or_zero(); - if self.characters.reached_end() { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingSlashForRegExp); - break; - } else if CharacterValidator::is_line_terminator(ch) { - self.add_unexpected_error(); - self.consume_line_terminator(); - } else { - self.characters.next(); - body.push(ch); - } - } else if CharacterValidator::is_line_terminator(ch) { - body.push('\n'); - self.consume_line_terminator(); - } else if self.characters.reached_end() { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingSlashForRegExp); - break; - } else { - body.push(ch); - self.characters.next(); - } - } - - let mut flags = String::new(); - while let Some((ch, _)) = self.consume_identifier_part() { - flags.push(ch); - } - - let location = start.combine_with(self.cursor_location()); - (Token::RegExp { body, flags }, location) - } - - fn character_ahead_location(&self) -> Location { - if self.characters.reached_end() { - return self.cursor_location(); - } - let offset = self.characters.index(); - let mut next_characters = self.characters.clone(); - next_characters.next().unwrap(); - Location::with_offsets(&self.compilation_unit, offset, next_characters.index()) - } - - pub fn cursor_location(&self) -> Location { - let offset = self.characters.index(); - Location::with_offset(&self.compilation_unit, offset) - } - - fn add_unexpected_error(&self) { - if self.characters.has_remaining() { - self.add_syntax_error(&self.character_ahead_location(), DiagnosticKind::UnexpectedCharacter, diagarg![self.characters.peek_or_zero().to_string()]) - } else { - self.add_syntax_error(&self.cursor_location(), DiagnosticKind::UnexpectedEnd, vec![]) - } - } - - fn add_unexpected_eof_error(&self, kind: DiagnosticKind) { - self.add_syntax_error(&self.cursor_location(), kind, vec![]); - } - - // LineTerminator - fn consume_line_terminator(&mut self) -> bool { - let ch = self.characters.peek_or_zero(); - if ch == '\x0D' && self.characters.peek_at_or_zero(1) == '\x0A' { - self.characters.skip_count_in_place(2); - // self.line_number += 1; - return true; - } - if CharacterValidator::is_line_terminator(ch) { - self.characters.next(); - // self.line_number += 1; - return true; - } - false - } - - fn consume_comment(&mut self) -> bool { - let ch = self.characters.peek_or_zero(); - if ch != '/' { - return false; - } - let ch2 = self.characters.peek_at_or_zero(1); - if ch2 == '/' { - let start = self.cursor_location(); - self.characters.skip_count_in_place(2); - while !CharacterValidator::is_line_terminator(self.characters.peek_or_zero()) && self.characters.has_remaining() { - self.characters.skip_in_place(); - } - let location = start.combine_with(self.cursor_location()); - self.consume_line_terminator(); - - self.compilation_unit.add_comment(Rc::new(Comment { - multiline: false, - content: RefCell::new(self.compilation_unit.text()[(location.first_offset() + 2)..location.last_offset()].to_owned()), - location: RefCell::new(location), - })); - - return true; - } - if ch2 == '*' { - let start = self.cursor_location(); - self.characters.skip_count_in_place(2); - - loop { - if self.characters.peek_or_zero() == '*' && self.characters.peek_at_or_zero(1) == '/' { - self.characters.skip_count_in_place(2); - break; - } else if self.consume_line_terminator() { - // Consumed LineTerminator - } else if self.characters.has_remaining() { - self.characters.skip_in_place(); - } else { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingSeqForMultiLineComment); - break; - } - } - - let location = start.combine_with(self.cursor_location()); - - let i = location.first_offset() + 2; - let j = decrease_last_offset(i, location.last_offset(), 2); - - self.compilation_unit.add_comment(Rc::new(Comment { - multiline: true, - content: RefCell::new(self.compilation_unit.text()[i..j].to_owned()), - location: RefCell::new(location), - })); - - return true; - } - false - } - - fn scan_identifier(&mut self) -> Option<(Token, Location)> { - let start = self.cursor_location(); - let mut escaped = false; - let Some((ch, escaped_2)) = self.consume_identifier_start() else { - return None; - }; - escaped = escaped || escaped_2; - let mut name = String::new(); - name.push(ch); - while let Some((ch, escaped_2)) = self.consume_identifier_part() { - escaped = escaped || escaped_2; - name.push(ch); - } - - let location = start.combine_with(self.cursor_location()); - if !escaped { - if let Some(token) = As3ReservedWord::token(name.as_ref()) { - return Some((token, location)); - } - } - Some((Token::Identifier(name), location)) - } - - /// Returns a tuple in the form (*character*, *escaped*). - fn consume_identifier_start(&mut self) -> Option<(char, bool)> { - let ch = self.characters.peek_or_zero(); - if CharacterValidator::is_identifier_start(ch) { - self.characters.next(); - return Some((ch, false)); - } - if self.characters.peek_or_zero() == '\\' { - self.characters.next(); - return Some((self.expect_unicode_escape_sequence(), true)); - } - None - } - - /// Returns a tuple in the form (*character*, *escaped*). - fn consume_identifier_part(&mut self) -> Option<(char, bool)> { - let ch = self.characters.peek_or_zero(); - if CharacterValidator::is_identifier_part(ch) { - self.characters.next(); - return Some((ch, false)); - } - if self.characters.peek_or_zero() == '\\' { - self.characters.next(); - return Some((self.expect_unicode_escape_sequence(), true)); - } - None - } - - /// Expects UnicodeEscapeSequence starting from `u`. - fn expect_unicode_escape_sequence(&mut self) -> char { - let start = self.cursor_location(); - if self.characters.peek_or_zero() != 'u' { - self.add_unexpected_error(); - return '\x5F'; - } - self.characters.next(); - - // Scan \uXXXX - if CharacterValidator::is_hex_digit(self.characters.peek_or_zero()) { - let r = char::from_u32(self.expect_hex_digit() << 12 - | (self.expect_hex_digit() << 8) - | (self.expect_hex_digit() << 4) - | self.expect_hex_digit()); - let Some(r) = r else { - self.add_syntax_error(&start.combine_with(self.cursor_location()), DiagnosticKind::InvalidEscapeValue, vec![]); - return '\x5F'; - }; - return r; - } - - // Scan \u{} - if self.characters.peek_or_zero() != '{' { - self.add_unexpected_error(); - return '\x5F'; - } - self.characters.next(); - while CharacterValidator::is_hex_digit(self.characters.peek_or_zero()) { - self.characters.next(); - } - if self.characters.peek_or_zero() != '}' { - self.add_unexpected_error(); - return '\x5F'; - } - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - let r = u32::from_str_radix(&self.compilation_unit.text()[(start.first_offset + 2)..(location.last_offset - 1)], 16); - let Ok(r) = r else { - self.add_syntax_error(&location, DiagnosticKind::InvalidEscapeValue, vec![]); - return '\x5F'; - }; - let r = char::from_u32(r); - let Some(r) = r else { - self.add_syntax_error(&location, DiagnosticKind::InvalidEscapeValue, vec![]); - return '\x5F'; - }; - r - } - - fn expect_hex_digit(&mut self) -> u32 { - let ch = self.characters.peek_or_zero(); - let mv = CharacterValidator::hex_digit_mv(ch); - if mv.is_none() { - self.add_unexpected_error(); - return 0x5F; - } - self.characters.next(); - mv.unwrap() - } - - fn scan_dot_or_numeric_literal(&mut self) -> Option<(Token, Location)> { - let start = self.cursor_location(); - let ch = self.characters.peek_or_zero(); - let mut initial_dot = false; - if ch == '.' { - initial_dot = true; - self.characters.next(); - - let seq = self.characters.peek_seq(2); - // Ellipsis - if seq == ".." { - self.characters.skip_count_in_place(2); - return Some((Token::Ellipsis, start.combine_with(self.cursor_location()))); - } - let ch = seq.get(..1).map(|ch| ch.chars().next().unwrap()).unwrap_or('\x00'); - // Descendants - if ch == '.' { - self.characters.next(); - return Some((Token::Descendants, start.combine_with(self.cursor_location()))); - } - // Dot - if !CharacterValidator::is_dec_digit(ch) { - return Some((Token::Dot, start.combine_with(self.cursor_location()))); - } - - // NumericLiteral - while CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - self.characters.next(); - self.consume_underscore_followed_by_dec_digit(); - } - } else if ch == '0' { - self.characters.next(); - let ch_2 = self.characters.peek_or_zero(); - - // HexLiteral - if ['X', 'x'].contains(&ch_2) { - self.characters.next(); - return self.scan_hex_literal(start.clone()); - } - - // BinLiteral; - if ['B', 'b'].contains(&ch_2) { - self.characters.next(); - return self.scan_bin_literal(start.clone()); - } - } else if CharacterValidator::is_dec_digit(ch) { - while CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - self.characters.next(); - self.consume_underscore_followed_by_dec_digit(); - } - } else { - return None; - } - - if !initial_dot && self.characters.peek_or_zero() == '.' { - self.characters.next(); - /* - if !CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - self.add_unexpected_error(); - } - */ - while CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - self.characters.next(); - self.consume_underscore_followed_by_dec_digit(); - } - } - - // Decimal exponent - if ['E', 'e'].contains(&self.characters.peek_or_zero()) { - self.characters.next(); - if ['+', '-'].contains(&self.characters.peek_or_zero()) { - self.characters.next(); - } - if !CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - self.add_unexpected_error(); - } - while CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - self.characters.next(); - self.consume_underscore_followed_by_dec_digit(); - } - } - - let string = self.compilation_unit.text()[start.first_offset..self.characters.index()].to_owned(); - - let mut suffix = NumberSuffix::None; - if self.characters.peek_or_zero() == 'f' || self.characters.peek_or_zero() == 'F' { - suffix = NumberSuffix::F; - self.characters.next(); - } - self.unallow_numeric_suffix(); - - let location = start.combine_with(self.cursor_location()); - - Some((Token::Number(string, suffix), location)) - } - - fn scan_hex_literal(&mut self, start: Location) -> Option<(Token, Location)> { - if !CharacterValidator::is_hex_digit(self.characters.peek_or_zero()) { - self.add_unexpected_error(); - } - while CharacterValidator::is_hex_digit(self.characters.peek_or_zero()) { - self.characters.next(); - self.consume_underscore_followed_by_hex_digit(); - } - - let suffix = NumberSuffix::None; - self.unallow_numeric_suffix(); - - let location = start.combine_with(self.cursor_location()); - let s = self.compilation_unit.text()[location.first_offset..location.last_offset].to_owned(); - Some((Token::Number(s, suffix), location)) - } - - fn scan_bin_literal(&mut self, start: Location) -> Option<(Token, Location)> { - if !CharacterValidator::is_bin_digit(self.characters.peek_or_zero()) { - self.add_unexpected_error(); - } - while CharacterValidator::is_bin_digit(self.characters.peek_or_zero()) { - self.characters.next(); - self.consume_underscore_followed_by_bin_digit(); - } - - let suffix = NumberSuffix::None; - self.unallow_numeric_suffix(); - - let location = start.combine_with(self.cursor_location()); - let s = self.compilation_unit.text()[location.first_offset..location.last_offset].to_owned(); - Some((Token::Number(s, suffix), location)) - } - - fn consume_underscore_followed_by_dec_digit(&mut self) { - if self.characters.peek_or_zero() == '_' { - self.characters.next(); - if !CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - self.add_unexpected_error(); - } - self.characters.next(); - } - } - - fn consume_underscore_followed_by_hex_digit(&mut self) { - if self.characters.peek_or_zero() == '_' { - self.characters.next(); - if !CharacterValidator::is_hex_digit(self.characters.peek_or_zero()) { - self.add_unexpected_error(); - } - self.characters.next(); - } - } - - fn consume_underscore_followed_by_bin_digit(&mut self) { - if self.characters.peek_or_zero() == '_' { - self.characters.next(); - if !CharacterValidator::is_bin_digit(self.characters.peek_or_zero()) { - self.add_unexpected_error(); - } - self.characters.next(); - } - } - - fn unallow_numeric_suffix(&self) { - if CharacterValidator::is_identifier_start(self.characters.peek_or_zero()) { - self.add_unexpected_error(); - } - } - - fn scan_string_literal(&mut self, raw: bool) -> Option<(Token, Location)> { - let delim = self.characters.peek_or_zero(); - if !['"', '\''].contains(&delim) { - return None; - } - let mut start = self.cursor_location(); - // Include the "@" punctuator as part of raw string literals - if raw { - start = Location::with_offset(&start.compilation_unit(), start.first_offset() - 1); - } - - self.characters.next(); - - // Triple string literal - if self.characters.peek_or_zero() == delim && self.characters.peek_at_or_zero(1) == delim { - self.characters.skip_count_in_place(2); - return self.scan_triple_string_literal(delim, start, raw); - } - - let mut value = String::new(); - - if raw { - loop { - let ch = self.characters.peek_or_zero(); - if ch == delim { - self.characters.next(); - break; - } else if CharacterValidator::is_line_terminator(ch) { - self.add_syntax_error(&self.character_ahead_location(), DiagnosticKind::StringLiteralMustBeTerminatedBeforeLineBreak, vec![]); - self.consume_line_terminator(); - } else if !self.characters.has_remaining() { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingQuoteForString); - break; - } else { - value.push(ch); - self.characters.next(); - } - } - } else { - loop { - if let Some(s) = self.consume_escape_sequence() { - value.push_str(&s); - } else { - let ch = self.characters.peek_or_zero(); - if ch == delim { - self.characters.next(); - break; - } else if CharacterValidator::is_line_terminator(ch) { - self.add_syntax_error(&self.character_ahead_location(), DiagnosticKind::StringLiteralMustBeTerminatedBeforeLineBreak, vec![]); - self.consume_line_terminator(); - } else if !self.characters.has_remaining() { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingQuoteForString); - break; - } else { - value.push(ch); - self.characters.next(); - } - } - } - } - - let location = start.combine_with(self.cursor_location()); - Some((Token::String(value), location)) - } - - fn scan_triple_string_literal(&mut self, delim: char, start: Location, raw: bool) -> Option<(Token, Location)> { - let mut lines: Vec = vec![]; - let mut builder = String::new(); - - if raw { - loop { - let ch = self.characters.peek_or_zero(); - if ch == delim && self.characters.peek_at_or_zero(1) == delim && self.characters.peek_at_or_zero(2) == delim { - self.characters.skip_count_in_place(3); - lines.push(builder.clone()); - break; - } else if CharacterValidator::is_line_terminator(ch) { - lines.push(builder.clone()); - builder.clear(); - self.consume_line_terminator(); - } else if !self.characters.has_remaining() { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingQuoteForString); - lines.push(builder.clone()); - builder.clear(); - break; - } else { - builder.push(ch); - self.characters.next(); - } - } - } else { - loop { - if let Some(s) = self.consume_escape_sequence() { - builder.push_str(&s); - } else { - let ch = self.characters.peek_or_zero(); - if ch == delim && self.characters.peek_at_or_zero(1) == delim && self.characters.peek_at_or_zero(2) == delim { - self.characters.skip_count_in_place(3); - lines.push(builder.clone()); - break; - } else if CharacterValidator::is_line_terminator(ch) { - lines.push(builder.clone()); - builder.clear(); - self.consume_line_terminator(); - } else if !self.characters.has_remaining() { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingQuoteForString); - lines.push(builder.clone()); - builder.clear(); - break; - } else { - builder.push(ch); - self.characters.next(); - } - } - } - } - - let location = start.combine_with(self.cursor_location()); - - if lines[0].is_empty() && lines.len() > 1 { - lines.remove(0); - } - - let last_line = lines.pop().unwrap(); - - let base_indent = CharacterValidator::indent_count(&last_line); - - let mut lines: Vec = lines.iter().map(|line| { - let indent = CharacterValidator::indent_count(line); - line[usize::min(base_indent, indent)..].to_owned() - }).collect(); - - let last_line = last_line[base_indent..].to_owned(); - if !last_line.is_empty() { - lines.push(last_line); - } - - let value = lines.join("\n"); - Some((Token::String(value), location)) - } - - fn consume_escape_sequence(&mut self) -> Option { - if self.characters.peek_or_zero() != '\\' { - return None; - } - self.characters.next(); - if !self.characters.has_remaining() { - self.add_unexpected_error(); - return Some("".into()); - } - if self.consume_line_terminator() { - return Some("".into()); - } - let ch = self.characters.peek_or_zero(); - match ch { - '\'' | '"' | '\\' => { - self.characters.next(); - Some(ch.into()) - }, - 'u' => { - Some(self.expect_unicode_escape_sequence().into()) - }, - 'x' => { - self.characters.next(); - let v = (self.expect_hex_digit() << 4) | self.expect_hex_digit(); - let v = char::from_u32(v).unwrap(); - Some(v.into()) - }, - 'b' => { - self.characters.next(); - Some('\x08'.into()) - }, - 'f' => { - self.characters.next(); - Some('\x0C'.into()) - }, - 'n' => { - self.characters.next(); - Some('\x0A'.into()) - }, - 'r' => { - self.characters.next(); - Some('\x0D'.into()) - }, - 't' => { - self.characters.next(); - Some('\x09'.into()) - }, - 'v' => { - self.characters.next(); - Some('\x0B'.into()) - }, - '0' => { - self.characters.next(); - if CharacterValidator::is_dec_digit(self.characters.peek_or_zero()) { - self.add_unexpected_error(); - } - Some('\x00'.into()) - }, - ch => { - if CharacterValidator::is_dec_digit(ch) { - self.add_unexpected_error(); - } - self.characters.next(); - Some(ch.into()) - }, - } - } - - /// Scans for an *InputElementXMLTag* token. - pub fn scan_ie_xml_tag(&mut self) -> (Token, Location) { - let start = self.cursor_location(); - let ch = self.characters.peek_or_zero(); - - // XmlName - if CharacterValidator::is_xml_name_start(ch) { - self.characters.next(); - while CharacterValidator::is_xml_name_part(self.characters.peek_or_zero()) { - self.characters.next(); - } - let location = start.combine_with(self.cursor_location()); - let name = self.compilation_unit.text()[location.first_offset..location.last_offset].to_owned(); - return (Token::XmlName(name), location); - } - - // XmlWhitespace - if CharacterValidator::is_xml_whitespace(ch) { - while CharacterValidator::is_xml_whitespace(self.characters.peek_or_zero()) { - if !self.consume_line_terminator() { - self.characters.next(); - } - } - let location = start.combine_with(self.cursor_location()); - return (Token::XmlWhitespace, location); - } - - match ch { - // Assign - '=' => { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - (Token::Assign, location) - }, - - // Gt - '>' => { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - (Token::Gt, location) - }, - - // XmlSlashGt - '/' => { - self.characters.next(); - if self.characters.peek_or_zero() != '>' { - self.add_unexpected_error(); - /* - while self.characters.has_remaining() { - self.characters.next(); - if self.characters.peek_or_zero() == '>' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::XmlSlashGt, location); - } - } - */ - let location = start.combine_with(self.cursor_location()); - return (Token::XmlSlashGt, location); - } - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - (Token::XmlSlashGt, location) - }, - - // XmlAttributeValue - '"' | '\'' => { - let delim = ch; - self.characters.next(); - while self.characters.peek_or_zero() != delim && self.characters.has_remaining() { - if !self.consume_line_terminator() { - self.characters.next(); - } - } - if self.characters.reached_end() { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingQuoteForAttributeValue); - let value = self.compilation_unit.text()[(start.first_offset + 1)..self.cursor_location().first_offset].to_owned(); - let location = start.combine_with(self.cursor_location()); - return (Token::XmlAttributeValue(value), location); - } - let value = self.compilation_unit.text()[(start.first_offset + 1)..self.cursor_location().first_offset].to_owned(); - self.characters.next(); - - let location = start.combine_with(self.cursor_location()); - (Token::XmlAttributeValue(value), location) - }, - - // BlockOpen - '{' => { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - (Token::BlockOpen, location) - }, - - _ => { - if self.characters.reached_end() { - return (Token::Eof, self.cursor_location()); - } - self.add_unexpected_error(); - self.characters.next(); - self.scan_ie_xml_tag() - }, - } - } - - /// Scans for an *InputElementXMLContent* token. - pub fn scan_ie_xml_content(&mut self) -> (Token, Location) { - let start = self.cursor_location(); - let ch = self.characters.peek_or_zero(); - - match ch { - '<' => { - self.characters.next(); - - // XmlMarkup - if let Some(r) = self.scan_xml_markup(start.clone()) { - return r; - } - - // XmlLtSlash - if self.characters.peek_or_zero() == '/' { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - return (Token::XmlLtSlash, location); - } - - // Lt - let location = start.combine_with(self.cursor_location()); - (Token::Lt, location) - }, - - // BlockOpen - '{' => { - self.characters.next(); - let location = start.combine_with(self.cursor_location()); - (Token::BlockOpen, location) - }, - - // XmlName - _ => { - if self.characters.reached_end() { - return (Token::Eof, self.cursor_location()); - } - loop { - let ch = self.characters.peek_or_zero(); - if ['<', '{'].contains(&ch) { - break; - } - if CharacterValidator::is_line_terminator(ch) { - self.consume_line_terminator(); - } else if self.characters.has_remaining() { - self.characters.next(); - } else { - break; - } - } - - let location = start.combine_with(self.cursor_location()); - let content = self.compilation_unit.text()[location.first_offset..location.last_offset].to_owned(); - (Token::XmlText(content), location) - }, - } - } - - /// Attempts to scan a XMLMarkup token after a `<` character. - pub fn scan_xml_markup(&mut self, start: Location) -> Option<(Token, Location)> { - // XMLComment - if self.characters.peek_seq(3) == "!--" { - self.characters.skip_count_in_place(3); - loop { - if self.characters.peek_or_zero() == '-' && self.characters.peek_seq(3) == "-->" { - self.characters.skip_count_in_place(3); - break; - } else if CharacterValidator::is_line_terminator(self.characters.peek_or_zero()) { - self.consume_line_terminator(); - } else if self.characters.reached_end() { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingSeqForXmlComment); - break; - } else { - self.characters.next(); - } - } - - let location = start.combine_with(self.cursor_location()); - let content = self.compilation_unit.text()[location.first_offset..location.last_offset].to_owned(); - - return Some((Token::XmlMarkup(content), location)); - } - - // XMLCDATA - if self.characters.peek_seq(8) == "![CDATA[" { - self.characters.skip_count_in_place(8); - loop { - if self.characters.peek_or_zero() == ']' && self.characters.peek_seq(3) == "]]>" { - self.characters.skip_count_in_place(3); - break; - } else if CharacterValidator::is_line_terminator(self.characters.peek_or_zero()) { - self.consume_line_terminator(); - } else if self.characters.reached_end() { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingSeqForCData); - break; - } else { - self.characters.next(); - } - } - - let location = start.combine_with(self.cursor_location()); - let content = self.compilation_unit.text()[location.first_offset..location.last_offset].to_owned(); - - return Some((Token::XmlMarkup(content), location)); - } - - // XMLPI - if self.characters.peek_or_zero() == '?' { - self.characters.next(); - loop { - if self.characters.peek_or_zero() == '?' && self.characters.peek_at_or_zero(1) == '>' { - self.characters.skip_count_in_place(2); - break; - } else if CharacterValidator::is_line_terminator(self.characters.peek_or_zero()) { - self.consume_line_terminator(); - } else if self.characters.reached_end() { - self.add_unexpected_eof_error(DiagnosticKind::InputEndedBeforeReachingClosingSeqForPi); - break; - } else { - self.characters.next(); - } - } - - let location = start.combine_with(self.cursor_location()); - let content = self.compilation_unit.text()[location.first_offset..location.last_offset].to_owned(); - - return Some((Token::XmlMarkup(content), location)); - } - - None - } -} \ No newline at end of file diff --git a/crates/parser/tree.rs b/crates/parser/tree.rs deleted file mode 100644 index e18f45d..0000000 --- a/crates/parser/tree.rs +++ /dev/null @@ -1,165 +0,0 @@ -//! Defines the syntactic nodes produced by the parser. - -mod invalidated_node; -pub use invalidated_node::*; - -// Expressions -mod expression; -pub use expression::*; -mod qualified_identifier; -pub use qualified_identifier::*; -mod paren_expression; -pub use paren_expression::*; -mod null_literal; -pub use null_literal::*; -mod boolean_literal; -pub use boolean_literal::*; -mod numeric_literal; -pub use numeric_literal::*; -mod string_literal; -pub use string_literal::*; -mod this_literal; -pub use this_literal::*; -mod regexp_literal; -pub use regexp_literal::*; -mod xml_expression; -pub use xml_expression::*; -mod array_literal; -pub use array_literal::*; -mod vector_literal; -pub use vector_literal::*; -mod object_initializer; -pub use object_initializer::*; -mod function_expression; -pub use function_expression::*; -mod import_meta; -pub use import_meta::*; -mod new_expression; -pub use new_expression::*; -mod member_expression; -pub use member_expression::*; -mod computed_member_expression; -pub use computed_member_expression::*; -mod descendants_expression; -pub use descendants_expression::*; -mod filter_expression; -pub use filter_expression::*; -mod super_expression; -pub use super_expression::*; -mod call_expression; -pub use call_expression::*; -mod expression_with_type_arguments; -pub use expression_with_type_arguments::*; -mod unary_expression; -pub use unary_expression::*; -mod optional_chaining_expression; -pub use optional_chaining_expression::*; -mod binary_expression; -pub use binary_expression::*; -mod conditional_expression; -pub use conditional_expression::*; -mod assignment_expression; -pub use assignment_expression::*; -mod sequence_expression; -pub use sequence_expression::*; -mod type_expression; -pub use type_expression::*; -mod reserved_namespace_expression; -pub use reserved_namespace_expression::*; - -// Destructuring -mod destructuring; -pub use destructuring::*; - -// Statements -mod empty_statement; -pub use empty_statement::*; -mod expression_statement; -pub use expression_statement::*; -mod super_statement; -pub use super_statement::*; -mod block; -pub use block::*; -mod labeled_statement; -pub use labeled_statement::*; -mod if_statement; -pub use if_statement::*; -mod switch_statement; -pub use switch_statement::*; -mod do_statement; -pub use do_statement::*; -mod while_statement; -pub use while_statement::*; -mod for_statement; -pub use for_statement::*; -mod continue_statement; -pub use continue_statement::*; -mod break_statement; -pub use break_statement::*; -mod with_statement; -pub use with_statement::*; -mod return_statement; -pub use return_statement::*; -mod throw_statement; -pub use throw_statement::*; -mod try_statement; -pub use try_statement::*; -mod default_xml_namespace_statement; -pub use default_xml_namespace_statement::*; - -// Directives -mod directive; -pub use directive::*; -mod configuration_directive; -pub use configuration_directive::*; -mod import_directive; -pub use import_directive::*; -mod use_namespace_directive; -pub use use_namespace_directive::*; -mod include_directive; -pub use include_directive::*; -mod normal_configuration_directive; -pub use normal_configuration_directive::*; -mod package_concat_directive; -pub use package_concat_directive::*; -mod directive_injection_node; -pub use directive_injection_node::*; - -// Miscellaneous -mod attributes; -pub use attributes::*; -mod asdoc; -pub use asdoc::*; -mod type_parameter; -pub use type_parameter::*; - -// Definitions -mod variable_definition; -pub use variable_definition::*; -mod function_definition; -pub use function_definition::*; -mod class_definition; -pub use class_definition::*; -mod enum_definition; -pub use enum_definition::*; -mod interface_definition; -pub use interface_definition::*; -mod type_definition; -pub use type_definition::*; -mod namespace_definition; -pub use namespace_definition::*; -mod package_definition; -pub use package_definition::*; -mod program; -pub use program::*; - -// MXML document -mod mxml; -pub use mxml::*; - -// CSS -mod css; -pub use css::*; - -mod tree_semantics; -pub use tree_semantics::*; \ No newline at end of file diff --git a/crates/parser/tree/array_literal.rs b/crates/parser/tree/array_literal.rs deleted file mode 100644 index 3ddf080..0000000 --- a/crates/parser/tree/array_literal.rs +++ /dev/null @@ -1,18 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ArrayLiteral { - pub location: Location, - /// ASDoc. Always ignore this field; it is used solely - /// when parsing meta-data. - pub asdoc: Option>, - pub elements: Vec, -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum Element { - Elision, - Expression(Rc), - Rest((Rc, Location)), -} \ No newline at end of file diff --git a/crates/parser/tree/asdoc.rs b/crates/parser/tree/asdoc.rs deleted file mode 100644 index 95d7756..0000000 --- a/crates/parser/tree/asdoc.rs +++ /dev/null @@ -1,52 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct AsDoc { - pub location: Location, - pub main_body: Option<(String, Location)>, - pub tags: Vec<(AsDocTag, Location)>, -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum AsDocTag { - Author(String), - Copy(Rc), - Created(String), - Default(String), - Deprecated { - message: Option, - }, - EventType(Rc), - Example(String), - InheritDoc, - Internal(String), - Langversion(String), - Param { - name: String, - description: String, - }, - Playerversion(String), - Private, - Productversion(String), - Return(String), - See { - reference: Rc, - display_text: Option, - }, - Throws { - class_reference: Rc, - description: Option, - }, - Version(String), -} - -/// An ASDoc reference consisting of an optional base and -/// an optional instance property fragment (`#x`). -#[derive(Clone, Serialize, Deserialize)] -pub struct AsDocReference { - /// Base expression. - pub base: Option>, - /// Instance property fragment following the hash character. - pub instance_property: Option>, -} \ No newline at end of file diff --git a/crates/parser/tree/assignment_expression.rs b/crates/parser/tree/assignment_expression.rs deleted file mode 100644 index fbcfe64..0000000 --- a/crates/parser/tree/assignment_expression.rs +++ /dev/null @@ -1,14 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct AssignmentExpression { - pub location: Location, - pub compound: Option, - /// Assignment left-hand side. - /// - /// If the left-hand side is an `ObjectInitializer` or an `ArrayLiteral` - /// and there is no compound assignment, it is a destructuring pattern. - pub left: Rc, - pub right: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/attributes.rs b/crates/parser/tree/attributes.rs deleted file mode 100644 index 6e248d0..0000000 --- a/crates/parser/tree/attributes.rs +++ /dev/null @@ -1,170 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub enum Attribute { - Metadata(Rc), - Expression(Rc), - Public(Location), - Private(Location), - Protected(Location), - Internal(Location), - Final(Location), - Native(Location), - Static(Location), - Abstract(Location), - Override(Location), - Dynamic(Location), -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct Metadata { - pub location: Location, - pub asdoc: Option>, - pub name: (String, Location), - pub entries: Option>>, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct MetadataEntry { - pub location: Location, - pub key: Option<(String, Location)>, - pub value: Rc, -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum MetadataValue { - IdentifierString((String, Location)), - String((String, Location)), -} - -impl MetadataValue { - pub fn location(&self) -> Location { - match self { - Self::IdentifierString((_, l)) => l.clone(), - Self::String((_, l)) => l.clone(), - } - } -} - -impl Attribute { - pub fn location(&self) -> Location { - match self { - Self::Expression(m) => m.location(), - Self::Metadata(m) => m.location.clone(), - Self::Public(a) => a.clone(), - Self::Private(a) => a.clone(), - Self::Protected(a) => a.clone(), - Self::Internal(a) => a.clone(), - Self::Final(a) => a.clone(), - Self::Native(a) => a.clone(), - Self::Static(a) => a.clone(), - Self::Abstract(a) => a.clone(), - Self::Override(a) => a.clone(), - Self::Dynamic(a) => a.clone(), - } - } - - pub fn has_access_modifier(list: &[Attribute]) -> bool { - for a in list { - match a { - Self::Expression(_) | - Self::Public(_) | - Self::Private(_) | - Self::Protected(_) | - Self::Internal(_) => return true, - _ => {} - } - } - false - } - - pub fn remove_metadata(list: &mut Vec, metadata: &Rc) { - for i in 0..list.len() { - if let Attribute::Metadata(metadata_1) = &list[i] { - if Rc::ptr_eq(&metadata_1, metadata) { - list.remove(i); - break; - } - } - } - } - - pub fn find_metadata(list: &[Attribute]) -> Vec> { - let mut r = vec![]; - for a in list { - match &a { - Self::Metadata(e) => { - r.push(e.clone()); - }, - _ => {}, - } - } - r - } - pub fn find_expression(list: &[Attribute]) -> Option> { for a in list { match &a { Self::Expression(e) => return Some(e.clone()), _ => {} } }; None } - pub fn find_public(list: &[Attribute]) -> Option { for a in list { match &a { Self::Public(l) => return Some(l.clone()), _ => {} } }; None } - pub fn find_private(list: &[Attribute]) -> Option { for a in list { match &a { Self::Private(l) => return Some(l.clone()), _ => {} } }; None } - pub fn find_protected(list: &[Attribute]) -> Option { for a in list { match &a { Self::Protected(l) => return Some(l.clone()), _ => {} } }; None } - pub fn find_internal(list: &[Attribute]) -> Option { for a in list { match &a { Self::Internal(l) => return Some(l.clone()), _ => {} } }; None } - pub fn find_final(list: &[Attribute]) -> Option { for a in list { match &a { Self::Final(l) => return Some(l.clone()), _ => {} } }; None } - pub fn find_native(list: &[Attribute]) -> Option { for a in list { match &a { Self::Native(l) => return Some(l.clone()), _ => {} } }; None } - pub fn find_static(list: &[Attribute]) -> Option { for a in list { match &a { Self::Static(l) => return Some(l.clone()), _ => {} } }; None } - pub fn find_abstract(list: &[Attribute]) -> Option { for a in list { match &a { Self::Abstract(l) => return Some(l.clone()), _ => {} } }; None } - pub fn find_override(list: &[Attribute]) -> Option { for a in list { match &a { Self::Override(l) => return Some(l.clone()), _ => {} } }; None } - pub fn find_dynamic(list: &[Attribute]) -> Option { for a in list { match &a { Self::Dynamic(l) => return Some(l.clone()), _ => {} } }; None } - - pub fn has(list: &[Attribute], attribute: &Attribute) -> bool { - match attribute { - Self::Public(_) => Self::find_public(list).is_some(), - Self::Private(_) => Self::find_private(list).is_some(), - Self::Protected(_) => Self::find_protected(list).is_some(), - Self::Internal(_) => Self::find_internal(list).is_some(), - Self::Final(_) => Self::find_final(list).is_some(), - Self::Native(_) => Self::find_native(list).is_some(), - Self::Static(_) => Self::find_static(list).is_some(), - Self::Abstract(_) => Self::find_abstract(list).is_some(), - Self::Override(_) => Self::find_override(list).is_some(), - Self::Dynamic(_) => Self::find_dynamic(list).is_some(), - _ => false, - } - } - - pub fn is_duplicate_access_modifier(list: &[Attribute], attribute: &Attribute) -> bool { - match attribute { - Self::Expression(_) | - Self::Public(_) | - Self::Private(_) | - Self::Protected(_) | - Self::Internal(_) => Self::find_expression(list).is_some() || Self::find_public(list).is_some() || Self::find_private(list).is_some() || Self::find_protected(list).is_some() || Self::find_internal(list).is_some(), - _ => false, - } - } - - pub fn is_metadata(&self) -> bool { matches!(self, Self::Metadata(_)) } - pub fn is_public(&self) -> bool { matches!(self, Self::Public(_)) } - pub fn is_private(&self) -> bool { matches!(self, Self::Private(_)) } - pub fn is_protected(&self) -> bool { matches!(self, Self::Protected(_)) } - pub fn is_internal(&self) -> bool { matches!(self, Self::Internal(_)) } - pub fn is_final(&self) -> bool { matches!(self, Self::Final(_)) } - pub fn is_native(&self) -> bool { matches!(self, Self::Native(_)) } - pub fn is_static(&self) -> bool { matches!(self, Self::Static(_)) } - pub fn is_abstract(&self) -> bool { matches!(self, Self::Abstract(_)) } - pub fn is_override(&self) -> bool { matches!(self, Self::Override(_)) } - pub fn is_dynamic(&self) -> bool { matches!(self, Self::Dynamic(_)) } - - pub fn from_identifier_name(name: &str, location: &Location) -> Option { - if location.character_count() != name.chars().count() { - return None; - } - match name.as_ref() { - "final" => Some(Attribute::Final(location.clone())), - "native" => Some(Attribute::Native(location.clone())), - "static" => Some(Attribute::Static(location.clone())), - "abstract" => Some(Attribute::Abstract(location.clone())), - "override" => Some(Attribute::Override(location.clone())), - "dynamic" => Some(Attribute::Dynamic(location.clone())), - _ => None, - } - } -} \ No newline at end of file diff --git a/crates/parser/tree/binary_expression.rs b/crates/parser/tree/binary_expression.rs deleted file mode 100644 index 26bee27..0000000 --- a/crates/parser/tree/binary_expression.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct BinaryExpression { - pub location: Location, - pub operator: Operator, - pub left: Rc, - pub right: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/block.rs b/crates/parser/tree/block.rs deleted file mode 100644 index 1349d32..0000000 --- a/crates/parser/tree/block.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// Block statement. -#[derive(Clone, Serialize, Deserialize)] -pub struct Block { - pub location: Location, - pub directives: Vec>, -} \ No newline at end of file diff --git a/crates/parser/tree/boolean_literal.rs b/crates/parser/tree/boolean_literal.rs deleted file mode 100644 index 537f41f..0000000 --- a/crates/parser/tree/boolean_literal.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct BooleanLiteral { - pub location: Location, - pub value: bool, -} \ No newline at end of file diff --git a/crates/parser/tree/break_statement.rs b/crates/parser/tree/break_statement.rs deleted file mode 100644 index 4b75262..0000000 --- a/crates/parser/tree/break_statement.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct BreakStatement { - pub location: Location, - pub label: Option<(String, Location)>, -} \ No newline at end of file diff --git a/crates/parser/tree/call_expression.rs b/crates/parser/tree/call_expression.rs deleted file mode 100644 index 2f502b2..0000000 --- a/crates/parser/tree/call_expression.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct CallExpression { - pub location: Location, - pub base: Rc, - pub arguments: Vec>, -} \ No newline at end of file diff --git a/crates/parser/tree/class_definition.rs b/crates/parser/tree/class_definition.rs deleted file mode 100644 index fb9d6b3..0000000 --- a/crates/parser/tree/class_definition.rs +++ /dev/null @@ -1,14 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ClassDefinition { - pub location: Location, - pub asdoc: Option>, - pub attributes: Vec, - pub name: (String, Location), - pub type_parameters: Option>>, - pub extends_clause: Option>, - pub implements_clause: Option>>, - pub block: Rc, -} diff --git a/crates/parser/tree/computed_member_expression.rs b/crates/parser/tree/computed_member_expression.rs deleted file mode 100644 index c869f5e..0000000 --- a/crates/parser/tree/computed_member_expression.rs +++ /dev/null @@ -1,12 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ComputedMemberExpression { - pub location: Location, - pub base: Rc, - /// ASDoc. Always ignore this field; it is used solely - /// when parsing meta-data. - pub asdoc: Option>, - pub key: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/conditional_expression.rs b/crates/parser/tree/conditional_expression.rs deleted file mode 100644 index cbf1f8d..0000000 --- a/crates/parser/tree/conditional_expression.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ConditionalExpression { - pub location: Location, - pub test: Rc, - pub consequent: Rc, - pub alternative: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/configuration_directive.rs b/crates/parser/tree/configuration_directive.rs deleted file mode 100644 index c22fccf..0000000 --- a/crates/parser/tree/configuration_directive.rs +++ /dev/null @@ -1,33 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// The `configuration {}` directive. -/// -/// # Syntax -/// -/// The directive consists of a block -/// of `if..else` branches, whose -/// condition is one of the following expressions: -/// -/// ```plain -/// // Check whether constant is "true" -/// q::x -/// x -/// // Check whether constant is "v" -/// k="v" -/// k=v // QualifiedIdentifier == StringLiteral -/// // Check whether constant is not "v" -/// k!="v" -/// k!=v // QualifiedIdentifier != StringLiteral -/// -/// x && y -/// x || y -/// -/// (x) -/// !x -/// ``` -#[derive(Clone, Serialize, Deserialize)] -pub struct ConfigurationDirective { - pub location: Location, - pub directive: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/continue_statement.rs b/crates/parser/tree/continue_statement.rs deleted file mode 100644 index 70338d7..0000000 --- a/crates/parser/tree/continue_statement.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ContinueStatement { - pub location: Location, - pub label: Option<(String, Location)>, -} \ No newline at end of file diff --git a/crates/parser/tree/css.rs b/crates/parser/tree/css.rs deleted file mode 100644 index c10a14f..0000000 --- a/crates/parser/tree/css.rs +++ /dev/null @@ -1,471 +0,0 @@ -use std::{marker::PhantomData, str::FromStr}; - -use crate::ns::*; -use num_traits::ToPrimitive; -use serde::{Serialize, Deserialize}; - -/// CSS3 selector combinators. -/// -/// See also: [CSS3 selectors: combinators](http://www.w3.org/TR/css3-selectors/#combinators). -#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] -pub enum CssCombinatorType { - Descendant, - Child, - Preceded, - Sibling, -} - -impl ToString for CssCombinatorType { - /// Symbol that represents the combinator type. - fn to_string(&self) -> String { - match self { - Self::Descendant => " ".into(), - Self::Child => ">".into(), - Self::Preceded => "+".into(), - Self::Sibling => "~".into(), - } - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum CssDirective { - Invalidated(InvalidatedNode), - FontFace(CssFontFace), - MediaQuery(CssMediaQuery), - NamespaceDefinition(CssNamespaceDefinition), - Rule(CssRule), -} - -impl CssDirective { - pub fn location(&self) -> Location { - match self { - Self::Invalidated(v) => v.location.clone(), - Self::FontFace(v) => v.location.clone(), - Self::MediaQuery(v) => v.location.clone(), - Self::NamespaceDefinition(v) => v.location.clone(), - Self::Rule(v) => v.location.clone(), - } - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum CssPropertyValue { - Invalidated(InvalidatedNode), - /// Example: `yellow, #fff` - Array(CssArrayPropertyValue), - /// Example: `1px solid red` - MultiValue(CssMultiValuePropertyValue), - /// Example: `yellow`, `#fff` - Color(CssColorPropertyValue), - /// Example: `10, 10.0, 10pt` - Number(CssNumberPropertyValue), - /// Example: `rgb(10% 10% 10%)`, `rgb(10%, 10%, 10%)` - RgbColor(CssRgbColorPropertyValue), - /// Example: `"string"` - String(CssStringPropertyValue), - /// Example: `solid`, `_serif` - Identifier(CssIdentifierPropertyValue), - /// `ClassReference(...)` - ClassReference(CssClassReferencePropertyValue), - /// `PropertyReference(...)` - PropertyReference(CssPropertyReferencePropertyValue), - //// `url(...) [format(...)]` - Url(CssUrlPropertyValue), - /// `local(...)` - Local(CssLocalPropertyValue), - /// `Embed(...)` - Embed(CssEmbedPropertyValue), -} - -impl CssPropertyValue { - pub fn location(&self) -> Location { - match self { - Self::Invalidated(v) => v.location.clone(), - Self::Array(v) => v.location.clone(), - Self::MultiValue(v) => v.location.clone(), - Self::Color(v) => v.location.clone(), - Self::Number(v) => v.location.clone(), - Self::RgbColor(v) => v.location.clone(), - Self::String(v) => v.location.clone(), - Self::Identifier(v) => v.location.clone(), - Self::ClassReference(v) => v.location.clone(), - Self::PropertyReference(v) => v.location.clone(), - Self::Url(v) => v.location.clone(), - Self::Local(v) => v.location.clone(), - Self::Embed(v) => v.location.clone(), - } - } - - pub fn as_array(&self) -> Option<&CssArrayPropertyValue> { - let Self::Array(v) = self else { return None; }; - Some(v) - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum CssSelector { - Invalidated(InvalidatedNode), - Base(CssBaseSelector), - Combinator(CssCombinatorSelector), -} - -impl CssSelector { - pub fn location(&self) -> Location { - match self { - Self::Invalidated(v) => v.location.clone(), - Self::Base(v) => v.location.clone(), - Self::Combinator(v) => v.location.clone(), - } - } -} - -/// Array property values are comma-separated values in CSS properties. -/// -/// For example: -/// -/// ```css -/// fillColors: #FFFFFF, #CCCCCC, #FFFFFF, #EEEEEE; -/// ``` -#[derive(Clone, Serialize, Deserialize)] -pub struct CssArrayPropertyValue { - pub location: Location, - pub elements: Vec>, -} - -/// Multi-value property values are space-separated values in CSS properties. -/// -/// For example: -/// -/// ```css -/// 1px solid blue -/// ``` -#[derive(Clone, Serialize, Deserialize)] -pub struct CssMultiValuePropertyValue { - pub location: Location, - pub values: Vec>, -} - -/// A CSS base selector. -#[derive(Clone, Serialize, Deserialize)] -pub struct CssBaseSelector { - pub location: Location, - pub namespace_prefix: Option<(String, Location)>, - pub element_name: Option<(String, Location)>, - pub conditions: Vec>, -} - -/// Supported condition types for [`CssSelectorCondition`]. -#[derive(Clone, Serialize, Deserialize)] -pub enum CssSelectorCondition { - Invalidated(InvalidatedNode), - /// For example: `s|Label.className` - Class((String, Location)), - /// For example: `s|Label#idValue` - Id((String, Location)), - /// For example: `s|Label:loadingState` - Pseudo((String, Location)), - /// For example: `s|Label::loadingState` - PseudoElement((String, Location)), - /// For example: `s|Panel:not(:first-child)` - Not { - location: Location, - condition: Rc, - }, - /// For example: `s|Label[loadingState]` - Attribute { - location: Location, - name: (String, Location), - operator: Option, - value: Option<(String, Location)>, - }, -} - -impl CssSelectorCondition { - pub fn location(&self) -> Location { - match self { - Self::Invalidated(v) => v.location.clone(), - Self::Class((_, l)) => l.clone(), - Self::Id((_, l)) => l.clone(), - Self::Pseudo((_, l)) => l.clone(), - Self::PseudoElement((_, l)) => l.clone(), - Self::Not { location, .. } => location.clone(), - Self::Attribute { location, .. } => location.clone(), - } - } -} - -#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] -pub enum CssAttributeOperator { - Equals, - BeginsWith, - EndsWith, - Contains, - ListMatch, - HreflangMatch, -} - -impl ToString for CssAttributeOperator { - fn to_string(&self) -> String { - match self { - Self::Equals => "=".into(), - Self::BeginsWith => "^=".into(), - Self::EndsWith => "$=".into(), - Self::Contains => "*=".into(), - Self::ListMatch => "~=".into(), - Self::HreflangMatch => "|=".into(), - } - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssColorPropertyValue { - pub location: Location, - pub color_int: u32, -} - -impl CssColorPropertyValue { - pub fn from_hex(location: Location, token_text: &str) -> Result { - let mut token_text = if token_text.starts_with('#') { token_text.to_owned() } else { - "#".to_owned() + token_text - }; - if token_text.len() == 4 { - let mut six = String::new(); - let chars: Vec<_> = token_text.chars().collect(); - six.push('#'); - six.push(chars[1]); - six.push(chars[1]); - six.push(chars[2]); - six.push(chars[2]); - six.push(chars[3]); - six.push(chars[3]); - token_text = six; - } - Ok(Self { - location, - color_int: u32::from_str_radix(&token_text[1..], 16).map_err(|_| ParserError::Common)?.clamp(0x000000, 0xFFFFFF), - }) - } - - pub fn text(&self) -> String { - self.location.text() - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssNumberPropertyValue { - pub location: Location, - pub value: f64, - pub unit: Option, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssRgbColorPropertyValue { - pub location: Location, - pub color_int: u32, -} - -impl CssRgbColorPropertyValue { - pub fn from_raw_arguments(location: &Location, raw_arguments: &[String]) -> Result { - Ok(CssRgbColorPropertyValue { - location: location.clone(), - color_int: (Self::parse_component(&raw_arguments[0])? << 16) - | (Self::parse_component(&raw_arguments[1])? << 8) - | Self::parse_component(&raw_arguments[2])?, - }) - } - - fn parse_component(input: &str) -> Result { - let i = input.find('%'); - let v: u32; - if let Some(i) = i { - let percent = f64::from_str(&input[..i]).map_err(|_| ParserError::Common)?.clamp(0.0, 100.0); - v = (255.0 * (percent / 100.0)).round().to_u32().ok_or(ParserError::Common)?; - } else if input.contains('.') { - let ratio = f64::from_str(input).map_err(|_| ParserError::Common)?.clamp(0.0, 1.0); - v = (255.0 * ratio).round().to_u32().ok_or(ParserError::Common)?; - } else { - v = u32::from_str(input).map_err(|_| ParserError::Common)?; - } - Ok(v.clamp(0, 255)) - } -} - -/// A CSS text is a string value written without quotes. -#[derive(Clone, Serialize, Deserialize)] -pub struct CssStringPropertyValue { - pub location: Location, - pub value: String, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssIdentifierPropertyValue { - pub location: Location, - pub value: String, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssClassReferencePropertyValue { - pub location: Location, - /// Name or "null". - pub name: (String, Location), -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssPropertyReferencePropertyValue { - pub location: Location, - pub name: (String, Location), -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssUrlPropertyValue { - pub location: Location, - pub url: (String, Location), - pub format: Option<(String, Location)>, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssLocalPropertyValue { - pub location: Location, - pub name: (String, Location), -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssEmbedPropertyValue { - pub location: Location, - pub entries: Vec>, -} - -/// Represents a key-value entry for an `Embed` function call property value. -/// It may be a keyless entry. -#[derive(Clone, Serialize, Deserialize)] -pub struct CssEmbedEntry { - pub location: Location, - pub key: Option<(String, Location)>, - pub value: (String, Location), -} - -/// A CSS selector containing a combinator. -#[derive(Clone, Serialize, Deserialize)] -pub struct CssCombinatorSelector { - pub location: Location, - pub left: Rc, - pub right: Rc, - pub combinator_type: CssCombinatorType, -} - -/// The root object of a CSS DOM. The CSS3 DOM objects serve not only IDE -/// features in code model, but also CSS compilation. -#[derive(Clone, Serialize, Deserialize)] -pub struct CssDocument { - pub location: Location, - pub directives: Vec>, -} - -/// CSS DOM for an `@font-face` statement. -#[derive(Clone, Serialize, Deserialize)] -pub struct CssFontFace { - pub location: Location, - pub properties: Vec>, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssProperty { - pub location: Location, - pub name: (String, Location), - pub value: Rc, - #[serde(skip)] - _phantom: PhantomData<()>, -} - -impl CssProperty { - pub fn new(location: Location, name: (String, Location), value: Rc) -> Self { - Self { - location, - name: (Self::normalize(&name.0), name.1), - value, - _phantom: PhantomData::default(), - } - } - - /// Normalize CSS property names to camel-case style names. Names already in - /// camel-case will be returned as-is. - fn normalize(name: &str) -> String { - let mut split = name.split('-').map(|s| s.to_owned()).collect::>(); - let mut v = split[0].chars(); - let mut v1 = String::new(); - if let Some(ch) = v.next() { - v1.push_str(&ch.to_lowercase().to_string()); - for ch in v { - v1.push(ch); - } - } - split[0] = v1; - for i in 1..split.len() { - let mut v = split[i].chars(); - let mut v1 = String::new(); - if let Some(ch) = v.next() { - v1.push_str(&ch.to_uppercase().to_string()); - for ch in v { - v1.push(ch); - } - } - split[i] = v1; - } - split.join("") - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssMediaQuery { - pub location: Location, - pub conditions: Vec>, - pub rules: Vec>, -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum CssMediaQueryCondition { - Invalidated(InvalidatedNode), - /// Identifier. Example: "screen". - Id((String, Location)), - /// The `only` keyword followed by an identifier. - /// Example: "only screen". - OnlyId { - location: Location, - id: (String, Location), - }, - /// A parenthesized property, such as - /// `(application-dpi: 240)`. - ParenProperty((Rc, Location)), - /// A `condition1 and condition2` expression. - And { - location: Location, - left: Rc, - right: Rc, - }, -} - -impl CssMediaQueryCondition { - pub fn location(&self) -> Location { - match self { - Self::Invalidated(v) => v.location.clone(), - Self::Id((_, l)) => l.clone(), - Self::OnlyId { location, .. } => location.clone(), - Self::ParenProperty((_, l)) => l.clone(), - Self::And { location, .. } => location.clone(), - } - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssRule { - pub location: Location, - pub selectors: Vec>, - pub properties: Vec>, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CssNamespaceDefinition { - pub location: Location, - pub prefix: (String, Location), - pub uri: (String, Location), -} \ No newline at end of file diff --git a/crates/parser/tree/default_xml_namespace_statement.rs b/crates/parser/tree/default_xml_namespace_statement.rs deleted file mode 100644 index 08084e2..0000000 --- a/crates/parser/tree/default_xml_namespace_statement.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct DefaultXmlNamespaceStatement { - pub location: Location, - pub right: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/descendants_expression.rs b/crates/parser/tree/descendants_expression.rs deleted file mode 100644 index d4e7bce..0000000 --- a/crates/parser/tree/descendants_expression.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct DescendantsExpression { - pub location: Location, - pub base: Rc, - pub identifier: QualifiedIdentifier, -} \ No newline at end of file diff --git a/crates/parser/tree/destructuring.rs b/crates/parser/tree/destructuring.rs deleted file mode 100644 index 3205c3d..0000000 --- a/crates/parser/tree/destructuring.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct TypedDestructuring { - pub location: Location, - pub destructuring: Rc, - pub type_annotation: Option>, -} \ No newline at end of file diff --git a/crates/parser/tree/directive.rs b/crates/parser/tree/directive.rs deleted file mode 100644 index 2f0af6d..0000000 --- a/crates/parser/tree/directive.rs +++ /dev/null @@ -1,82 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// Directive attached with a source location. -#[derive(Clone, Serialize, Deserialize)] -pub enum Directive { - EmptyStatement(EmptyStatement), - ExpressionStatement(ExpressionStatement), - SuperStatement(SuperStatement), - Block(Block), - LabeledStatement(LabeledStatement), - IfStatement(IfStatement), - SwitchStatement(SwitchStatement), - SwitchTypeStatement(SwitchTypeStatement), - DoStatement(DoStatement), - WhileStatement(WhileStatement), - ForStatement(ForStatement), - ForInStatement(ForInStatement), - BreakStatement(BreakStatement), - ContinueStatement(ContinueStatement), - WithStatement(WithStatement), - ReturnStatement(ReturnStatement), - ThrowStatement(ThrowStatement), - DefaultXmlNamespaceStatement(DefaultXmlNamespaceStatement), - TryStatement(TryStatement), - Invalidated(InvalidatedNode), - ConfigurationDirective(ConfigurationDirective), - ImportDirective(ImportDirective), - UseNamespaceDirective(UseNamespaceDirective), - IncludeDirective(IncludeDirective), - NormalConfigurationDirective(NormalConfigurationDirective), - PackageConcatDirective(PackageConcatDirective), - DirectiveInjection(DirectiveInjectionNode), - VariableDefinition(VariableDefinition), - FunctionDefinition(FunctionDefinition), - ClassDefinition(ClassDefinition), - EnumDefinition(EnumDefinition), - InterfaceDefinition(InterfaceDefinition), - TypeDefinition(TypeDefinition), - NamespaceDefinition(NamespaceDefinition), -} - -impl Directive { - pub fn location(&self) -> Location { - match self { - Self::EmptyStatement(d) => d.location.clone(), - Self::ExpressionStatement(d) => d.location.clone(), - Self::SuperStatement(d) => d.location.clone(), - Self::Block(d) => d.location.clone(), - Self::LabeledStatement(d) => d.location.clone(), - Self::IfStatement(d) => d.location.clone(), - Self::SwitchStatement(d) => d.location.clone(), - Self::SwitchTypeStatement(d) => d.location.clone(), - Self::DoStatement(d) => d.location.clone(), - Self::WhileStatement(d) => d.location.clone(), - Self::ForStatement(d) => d.location.clone(), - Self::ForInStatement(d) => d.location.clone(), - Self::BreakStatement(d) => d.location.clone(), - Self::ContinueStatement(d) => d.location.clone(), - Self::WithStatement(d) => d.location.clone(), - Self::ReturnStatement(d) => d.location.clone(), - Self::ThrowStatement(d) => d.location.clone(), - Self::DefaultXmlNamespaceStatement(d) => d.location.clone(), - Self::TryStatement(d) => d.location.clone(), - Self::Invalidated(d) => d.location.clone(), - Self::ConfigurationDirective(d) => d.location.clone(), - Self::ImportDirective(d) => d.location.clone(), - Self::UseNamespaceDirective(d) => d.location.clone(), - Self::IncludeDirective(d) => d.location.clone(), - Self::NormalConfigurationDirective(d) => d.location.clone(), - Self::PackageConcatDirective(d) => d.location.clone(), - Self::DirectiveInjection(d) => d.location.clone(), - Self::VariableDefinition(d) => d.location.clone(), - Self::FunctionDefinition(d) => d.location.clone(), - Self::ClassDefinition(d) => d.location.clone(), - Self::EnumDefinition(d) => d.location.clone(), - Self::InterfaceDefinition(d) => d.location.clone(), - Self::TypeDefinition(d) => d.location.clone(), - Self::NamespaceDefinition(d) => d.location.clone(), - } - } -} \ No newline at end of file diff --git a/crates/parser/tree/directive_injection_node.rs b/crates/parser/tree/directive_injection_node.rs deleted file mode 100644 index bb73b64..0000000 --- a/crates/parser/tree/directive_injection_node.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// Node that allows modification to the directive sequence. -#[derive(Clone, Serialize, Deserialize)] -pub struct DirectiveInjectionNode { - pub location: Location, - pub directives: RefCell>>, -} \ No newline at end of file diff --git a/crates/parser/tree/do_statement.rs b/crates/parser/tree/do_statement.rs deleted file mode 100644 index 2c6400e..0000000 --- a/crates/parser/tree/do_statement.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// The `do..while` statement. -#[derive(Clone, Serialize, Deserialize)] -pub struct DoStatement { - pub location: Location, - pub body: Rc, - pub test: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/empty_statement.rs b/crates/parser/tree/empty_statement.rs deleted file mode 100644 index 73ac431..0000000 --- a/crates/parser/tree/empty_statement.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct EmptyStatement { - pub location: Location, -} \ No newline at end of file diff --git a/crates/parser/tree/enum_definition.rs b/crates/parser/tree/enum_definition.rs deleted file mode 100644 index b1892eb..0000000 --- a/crates/parser/tree/enum_definition.rs +++ /dev/null @@ -1,13 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct EnumDefinition { - pub location: Location, - pub asdoc: Option>, - pub attributes: Vec, - pub is_set: bool, - pub name: (String, Location), - pub as_clause: Option>, - pub block: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/expression.rs b/crates/parser/tree/expression.rs deleted file mode 100644 index 25e77da..0000000 --- a/crates/parser/tree/expression.rs +++ /dev/null @@ -1,276 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// Expression attached with a source location. -#[derive(Clone, Serialize, Deserialize)] -pub enum Expression { - QualifiedIdentifier(QualifiedIdentifier), - Paren(ParenExpression), - NullLiteral(NullLiteral), - BooleanLiteral(BooleanLiteral), - NumericLiteral(NumericLiteral), - StringLiteral(StringLiteral), - ThisLiteral(ThisLiteral), - RegExpLiteral(RegExpLiteral), - Xml(XmlExpression), - XmlMarkup(XmlMarkupExpression), - XmlList(XmlListExpression), - ArrayLiteral(ArrayLiteral), - VectorLiteral(VectorLiteral), - ObjectInitializer(ObjectInitializer), - Function(FunctionExpression), - ImportMeta(ImportMeta), - New(NewExpression), - Member(MemberExpression), - ComputedMember(ComputedMemberExpression), - Descendants(DescendantsExpression), - Filter(FilterExpression), - Super(SuperExpression), - Call(CallExpression), - WithTypeArguments(ExpressionWithTypeArguments), - Unary(UnaryExpression), - OptionalChaining(OptionalChainingExpression), - OptionalChainingPlaceholder(OptionalChainingPlaceholder), - Binary(BinaryExpression), - Conditional(ConditionalExpression), - Assignment(AssignmentExpression), - Sequence(SequenceExpression), - NullableType(NullableTypeExpression), - NonNullableType(NonNullableTypeExpression), - AnyType(AnyTypeExpression), - VoidType(VoidTypeExpression), - ArrayType(ArrayTypeExpression), - TupleType(TupleTypeExpression), - FunctionType(FunctionTypeExpression), - Invalidated(InvalidatedNode), - ReservedNamespace(ReservedNamespaceExpression), -} - -impl Expression { - pub fn location(&self) -> Location { - match self { - Self::QualifiedIdentifier(e) => e.location.clone(), - Self::Paren(e) => e.location.clone(), - Self::NullLiteral(e) => e.location.clone(), - Self::BooleanLiteral(e) => e.location.clone(), - Self::NumericLiteral(e) => e.location.clone(), - Self::StringLiteral(e) => e.location.clone(), - Self::ThisLiteral(e) => e.location.clone(), - Self::RegExpLiteral(e) => e.location.clone(), - Self::Xml(e) => e.location.clone(), - Self::XmlMarkup(e) => e.location.clone(), - Self::XmlList(e) => e.location.clone(), - Self::ArrayLiteral(e) => e.location.clone(), - Self::VectorLiteral(e) => e.location.clone(), - Self::ObjectInitializer(e) => e.location.clone(), - Self::Function(e) => e.location.clone(), - Self::ImportMeta(e) => e.location.clone(), - Self::New(e) => e.location.clone(), - Self::Member(e) => e.location.clone(), - Self::ComputedMember(e) => e.location.clone(), - Self::Descendants(e) => e.location.clone(), - Self::Filter(e) => e.location.clone(), - Self::Super(e) => e.location.clone(), - Self::Call(e) => e.location.clone(), - Self::WithTypeArguments(e) => e.location.clone(), - Self::Unary(e) => e.location.clone(), - Self::OptionalChaining(e) => e.location.clone(), - Self::OptionalChainingPlaceholder(e) => e.location.clone(), - Self::Binary(e) => e.location.clone(), - Self::Conditional(e) => e.location.clone(), - Self::Assignment(e) => e.location.clone(), - Self::Sequence(e) => e.location.clone(), - Self::NullableType(e) => e.location.clone(), - Self::NonNullableType(e) => e.location.clone(), - Self::AnyType(e) => e.location.clone(), - Self::VoidType(e) => e.location.clone(), - Self::ArrayType(e) => e.location.clone(), - Self::TupleType(e) => e.location.clone(), - Self::FunctionType(e) => e.location.clone(), - Self::Invalidated(e) => e.location.clone(), - Self::ReservedNamespace(e) => e.location(), - } - } - - pub(crate) fn to_metadata(&self, parser: &Parser) -> Result>, MetadataRefineError1> { - match self { - Self::ArrayLiteral(ArrayLiteral { elements, asdoc, .. }) => { - if elements.len() != 1 { - return Ok(None); - } - if let Element::Expression(ref exp) = elements[0] { - Ok(Some(vec![Attribute::Metadata(parser.refine_metadata(exp, asdoc.clone()).map_err(|e| MetadataRefineError1(e, exp.location()))?)])) - } else { - Ok(None) - } - }, - Self::ComputedMember(ComputedMemberExpression { base, asdoc, key, .. }) => { - let a = base.to_metadata(parser)?; - if a.is_none() { - return Ok(None); - } - let mut a = a.unwrap(); - if matches!(key.as_ref(), Self::Sequence(_)) { - return Ok(None); - } - a.push(Attribute::Metadata(parser.refine_metadata(key, asdoc.clone()).map_err(|e| MetadataRefineError1(e, key.location()))?)); - Ok(Some(a)) - }, - _ => Ok(None), - } - } - - pub fn to_identifier_name_or_asterisk(&self) -> Option<(String, Location)> { - match self { - Self::QualifiedIdentifier(id) => id.to_identifier_name_or_asterisk(), - _ => None, - } - } - - pub fn to_identifier_name(&self) -> Option<(String, Location)> { - match self { - Self::QualifiedIdentifier(id) => id.to_identifier_name(), - _ => None, - } - } - - pub fn valid_access_modifier(&self) -> bool { - match self { - Self::QualifiedIdentifier(id) => id.is_identifier_token(), - Self::Member(e) => e.base.valid_access_modifier(), - Self::ComputedMember(e) => e.base.valid_access_modifier(), - _ => false, - } - } - - pub(crate) fn to_reserved_namespace_string(&self) -> Option { - if let Self::ReservedNamespace(e) = self { - Some(e.to_string()) - } else { - None - } - } - - pub(crate) fn to_reserved_namespace_attribute(&self) -> Option { - if let Self::ReservedNamespace(e) = self { - Some(e.to_attribute()) - } else { - None - } - } - - pub fn is_invalidated(&self) -> bool { - matches!(self, Self::Invalidated(_)) - } - - pub fn is_non_null_operation(&self) -> bool { - match self { - Self::Unary(expr) => expr.operator == Operator::NonNull, - _ => false, - } - } - - pub fn is_valid_assignment_left_hand_side(&self) -> bool { - match self { - Self::Invalidated(_) => true, - Self::Unary(e) => e.expression.is_valid_assignment_left_hand_side(), - Self::ArrayLiteral(_) | Self::ObjectInitializer(_) => self.is_valid_destructuring(), - _ => true, - } - } - - pub fn is_valid_destructuring(&self) -> bool { - match self { - Self::Invalidated(_) => true, - Self::QualifiedIdentifier(id) => !id.attribute && id.qualifier.is_none() && match &id.id { - QualifiedIdentifierIdentifier::Id(id) => id.0 != "*", - _ => false, - }, - Self::ArrayLiteral(expr) => { - for el in &expr.elements { - match el { - Element::Elision => {}, - Element::Expression(expr) => { - if !expr.is_valid_destructuring() { - return false; - } - }, - Element::Rest((expr, _)) => { - if !expr.is_valid_destructuring() { - return false; - } - }, - } - } - true - }, - Self::ObjectInitializer(init) => { - for field in init.fields.iter() { - match field.as_ref() { - InitializerField::Field { value, .. } => { - if let Some(val) = value { - if !val.is_valid_destructuring() { - return false; - } - } - }, - InitializerField::Rest((expr, _)) => { - if !expr.is_valid_destructuring() { - return false; - } - }, - } - } - true - }, - Self::Unary(expr) => expr.operator == Operator::NonNull && expr.expression.is_valid_destructuring(), - _ => false, - } - } - - /// `CONFIG::VAR_NAME` - pub(crate) fn to_normal_configuration_identifier(&self, parser: &Parser) -> Result)>, MetadataRefineError1> { - if let Self::QualifiedIdentifier(id) = self { - if id.attribute { - return Ok(None); - } - if let Some(q) = &id.qualifier { - if let Some(q) = q.to_identifier_name() { - if let QualifiedIdentifierIdentifier::Id(id) = &id.id { - return Ok(Some((q, id.clone(), vec![]))); - } - } - } - } - if let Self::ComputedMember(ComputedMemberExpression { base, asdoc, key, .. }) = self { - let a = base.to_normal_configuration_identifier(parser)?; - if a.is_none() { - return Ok(None); - } - let (ns, name, mut a) = a.unwrap(); - if matches!(key.as_ref(), Self::Sequence(_)) { - return Ok(None); - } - a.push(Attribute::Metadata(parser.refine_metadata(key, asdoc.clone()).map_err(|e| MetadataRefineError1(e, key.location()))?)); - return Ok(Some((ns, name, a))); - } - Ok(None) - } - - /// `CONFIG::VAR_NAME` - pub(crate) fn to_normal_configuration_identifier_no_metadata(&self) -> Option<((String, Location), (String, Location))> { - if let Self::QualifiedIdentifier(id) = self { - if id.attribute { - return None; - } - if let Some(q) = &id.qualifier { - if let Some(q) = q.to_identifier_name() { - if let QualifiedIdentifierIdentifier::Id(id) = &id.id { - return Some((q, id.clone())); - } - } - } - } - None - } -} \ No newline at end of file diff --git a/crates/parser/tree/expression_statement.rs b/crates/parser/tree/expression_statement.rs deleted file mode 100644 index 779fc81..0000000 --- a/crates/parser/tree/expression_statement.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ExpressionStatement { - pub location: Location, - pub expression: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/expression_with_type_arguments.rs b/crates/parser/tree/expression_with_type_arguments.rs deleted file mode 100644 index 8cf5e84..0000000 --- a/crates/parser/tree/expression_with_type_arguments.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// The `o.<...>` expression. -#[derive(Clone, Serialize, Deserialize)] -pub struct ExpressionWithTypeArguments { - pub location: Location, - pub base: Rc, - pub arguments: Vec>, -} \ No newline at end of file diff --git a/crates/parser/tree/filter_expression.rs b/crates/parser/tree/filter_expression.rs deleted file mode 100644 index 734b6e7..0000000 --- a/crates/parser/tree/filter_expression.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// Filter operation `o.(condition)`. -#[derive(Clone, Serialize, Deserialize)] -pub struct FilterExpression { - pub location: Location, - pub base: Rc, - pub test: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/for_statement.rs b/crates/parser/tree/for_statement.rs deleted file mode 100644 index f648a97..0000000 --- a/crates/parser/tree/for_statement.rs +++ /dev/null @@ -1,32 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ForStatement { - pub location: Location, - pub init: Option, - pub test: Option>, - pub update: Option>, - pub body: Rc, -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum ForInitializer { - Expression(Rc), - VariableDefinition(Rc), -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct ForInStatement { - pub location: Location, - pub each: bool, - pub left: ForInBinding, - pub right: Rc, - pub body: Rc, -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum ForInBinding { - Expression(Rc), - VariableDefinition(Rc), -} \ No newline at end of file diff --git a/crates/parser/tree/function_definition.rs b/crates/parser/tree/function_definition.rs deleted file mode 100644 index 6da842e..0000000 --- a/crates/parser/tree/function_definition.rs +++ /dev/null @@ -1,111 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct FunctionDefinition { - pub location: Location, - pub asdoc: Option>, - pub attributes: Vec, - pub name: FunctionName, - pub common: Rc, -} - -impl FunctionDefinition { - /// Indicates whether the function definition is not a getter, setter, - /// or constructor. - pub fn is_normal(&self) -> bool { - matches!(self.name, FunctionName::Identifier(_)) - } - pub fn is_getter(&self) -> bool { - matches!(self.name, FunctionName::Getter(_)) - } - pub fn is_setter(&self) -> bool { - matches!(self.name, FunctionName::Setter(_)) - } - pub fn is_constructor(&self) -> bool { - matches!(self.name, FunctionName::Constructor(_)) - } - pub fn name_identifier(&self) -> (String, Location) { - match &self.name { - FunctionName::Identifier(name) => name.clone(), - FunctionName::Getter(name) => name.clone(), - FunctionName::Setter(name) => name.clone(), - FunctionName::Constructor(name) => name.clone(), - } - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum FunctionName { - Identifier((String, Location)), - Getter((String, Location)), - Setter((String, Location)), - /// A `FunctionName` is a `Constructor` variant - /// when the corresponding function definition is a constructor. - Constructor((String, Location)), -} - -impl FunctionName { - pub fn location(&self) -> Location { - match self { - Self::Identifier((_, l)) => l.clone(), - Self::Getter((_, l)) => l.clone(), - Self::Setter((_, l)) => l.clone(), - Self::Constructor((_, l)) => l.clone(), - } - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct FunctionCommon { - pub location: Location, - /// Indicates whether the corresponding function - /// contains the `yield` operator. - pub contains_yield: bool, - /// Indicates whether the corresponding function - /// contains the `await` operator. - pub contains_await: bool, - pub signature: FunctionSignature, - pub body: Option, -} - -impl FunctionCommon { - pub(crate) fn has_block_body(&self) -> bool { - if let Some(ref body) = self.body { matches!(body, FunctionBody::Block(_)) } else { false } - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct FunctionSignature { - pub location: Location, - pub parameters: Vec>, - pub result_type: Option>, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct Parameter { - pub location: Location, - pub kind: ParameterKind, - pub destructuring: TypedDestructuring, - pub default_value: Option>, -} - -#[derive(Copy, Clone, Serialize, Deserialize, PartialEq, Eq)] -#[repr(u32)] -pub enum ParameterKind { - Required = 1, - Optional = 2, - Rest = 3, -} - -impl ParameterKind { - pub fn may_be_followed_by(&self, other: Self) -> bool { - (*self as u32) <= (other as u32) - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum FunctionBody { - Expression(Rc), - Block(Rc), -} \ No newline at end of file diff --git a/crates/parser/tree/function_expression.rs b/crates/parser/tree/function_expression.rs deleted file mode 100644 index 377512e..0000000 --- a/crates/parser/tree/function_expression.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct FunctionExpression { - pub location: Location, - pub name: Option<(String, Location)>, - pub common: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/if_statement.rs b/crates/parser/tree/if_statement.rs deleted file mode 100644 index cf84ca0..0000000 --- a/crates/parser/tree/if_statement.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct IfStatement { - pub location: Location, - pub test: Rc, - pub consequent: Rc, - pub alternative: Option>, -} \ No newline at end of file diff --git a/crates/parser/tree/import_directive.rs b/crates/parser/tree/import_directive.rs deleted file mode 100644 index 40b6cdf..0000000 --- a/crates/parser/tree/import_directive.rs +++ /dev/null @@ -1,27 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ImportDirective { - pub location: Location, - pub alias: Option<(String, Location)>, - pub package_name: Vec<(String, Location)>, - pub import_specifier: ImportSpecifier, -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum ImportSpecifier { - Wildcard(Location), - Recursive(Location), - Identifier((String, Location)), -} - -impl ImportSpecifier { - pub fn location(&self) -> Location { - match self { - Self::Wildcard(l) | - Self::Recursive(l) => l.clone(), - Self::Identifier((_, l)) => l.clone(), - } - } -} \ No newline at end of file diff --git a/crates/parser/tree/import_meta.rs b/crates/parser/tree/import_meta.rs deleted file mode 100644 index be968ce..0000000 --- a/crates/parser/tree/import_meta.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// The `import.meta` expression. -#[derive(Clone, Serialize, Deserialize)] -pub struct ImportMeta { - pub location: Location, -} \ No newline at end of file diff --git a/crates/parser/tree/include_directive.rs b/crates/parser/tree/include_directive.rs deleted file mode 100644 index 01faef3..0000000 --- a/crates/parser/tree/include_directive.rs +++ /dev/null @@ -1,12 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct IncludeDirective { - pub location: Location, - pub source: String, - #[serde(skip)] - pub nested_compilation_unit: Rc, - pub nested_packages: Vec>, - pub nested_directives: Vec>, -} \ No newline at end of file diff --git a/crates/parser/tree/interface_definition.rs b/crates/parser/tree/interface_definition.rs deleted file mode 100644 index 00dcb67..0000000 --- a/crates/parser/tree/interface_definition.rs +++ /dev/null @@ -1,13 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct InterfaceDefinition { - pub location: Location, - pub asdoc: Option>, - pub attributes: Vec, - pub name: (String, Location), - pub type_parameters: Option>>, - pub extends_clause: Option>>, - pub block: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/invalidated_node.rs b/crates/parser/tree/invalidated_node.rs deleted file mode 100644 index f15957d..0000000 --- a/crates/parser/tree/invalidated_node.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// Represents a construct that failed to parse. -#[derive(Clone, Serialize, Deserialize)] -pub struct InvalidatedNode { - pub location: Location, -} \ No newline at end of file diff --git a/crates/parser/tree/labeled_statement.rs b/crates/parser/tree/labeled_statement.rs deleted file mode 100644 index 81cb248..0000000 --- a/crates/parser/tree/labeled_statement.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct LabeledStatement { - pub location: Location, - pub label: (String, Location), - pub substatement: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/member_expression.rs b/crates/parser/tree/member_expression.rs deleted file mode 100644 index 66bb70e..0000000 --- a/crates/parser/tree/member_expression.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct MemberExpression { - pub location: Location, - pub base: Rc, - pub identifier: QualifiedIdentifier, -} \ No newline at end of file diff --git a/crates/parser/tree/mxml.rs b/crates/parser/tree/mxml.rs deleted file mode 100644 index d7dfbca..0000000 --- a/crates/parser/tree/mxml.rs +++ /dev/null @@ -1,217 +0,0 @@ -use std::collections::BTreeMap; - -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct Mxml { - pub location: Location, - pub version: XmlVersion, - pub encoding: String, - pub content: Vec>, -} - -#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum XmlVersion { - /// XML version 1.0. - Version10, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct MxmlElement { - pub location: Location, - pub name: MxmlName, - /// Attribute list, including `xmlns` and `xmlns:` namespace prefixes. - pub attributes: Vec>, - /// The namespace mapping relative to the XML element. - #[serde(skip)] - pub namespace: Rc, - pub content: Option>>, - pub closing_name: Option, -} - -impl MxmlElement { - pub fn inner_text(&self) -> String { - let mut j = String::new(); - if let Some(c) = self.content.as_ref() { - for c1 in c.iter() { - j.push_str(&c1.inner_text()); - } - } - j - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct MxmlAttribute { - pub location: Location, - /// Indicates whether the attribute is a `xmlns` or `xmlns:` attribute. - pub xmlns: bool, - pub name: MxmlName, - /// Attribute value. The location data includes the quotes. - pub value: (String, Location), -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct MxmlName { - pub location: Location, - /// The unresolved prefix of the name. - pub prefix: Option, - pub name: String, -} - -impl MxmlName { - pub fn resolve_prefix(&self, namespace: &Rc) -> Result { - let Some(p) = self.prefix.as_ref() else { - return if let Some(v) = namespace.get(MxmlNamespace::DEFAULT_NAMESPACE) { - Ok(v) - } else { - Err(MxmlNameError::PrefixNotDefined(MxmlNamespace::DEFAULT_NAMESPACE.into())) - }; - }; - if let Some(v) = namespace.get(p) { - Ok(v) - } else { - Err(MxmlNameError::PrefixNotDefined(p.clone())) - } - } - - pub fn resolve_name(&self, namespace: &Rc) -> Result<(String, String), MxmlNameError> { - let p = self.resolve_prefix(namespace)?; - Ok((p, self.name.clone())) - } - - pub fn equals_name(&self, other: &Self, namespace: &Rc) -> Result { - if self.name != other.name { - return Ok(false); - } - let p1 = self.resolve_prefix(namespace)?; - let p2 = other.resolve_prefix(namespace)?; - Ok(&p1 == &p2) - } - - pub fn to_string(&self, namespace: &Rc) -> String { - self.resolve_name(namespace).map(|(uri, localname)| { - if uri.is_empty() { - return localname; - } - format!("{uri}:{localname}") - }).unwrap_or("[error]".into()) - } -} - -#[derive(Clone)] -pub enum MxmlNameError { - PrefixNotDefined(String), -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum MxmlContent { - Characters((String, Location)), - /// A CDATA construct, including the first `` characters. - CData((String, Location)), - /// A comment construct, including the first `` characters. - Comment((String, Location)), - ProcessingInstruction { - location: Location, - name: String, - data: Option, - }, - Element(Rc), -} - -impl MxmlContent { - pub fn location(&self) -> Location { - match self { - Self::Characters((_, l)) => l.clone(), - Self::CData((_, l)) => l.clone(), - Self::Comment((_, l)) => l.clone(), - Self::ProcessingInstruction { location: l, .. } => l.clone(), - Self::Element(e) => e.location.clone(), - } - } - - pub fn inner_text(&self) -> String { - match self { - Self::Characters((data, _)) => data.clone(), - Self::CData((data, _)) => data[" String::new(), - Self::ProcessingInstruction { .. } => String::new(), - Self::Element(e) => e.inner_text(), - } - } -} - -/// Mapping of namespace prefixes. -#[derive(Clone, PartialEq)] -pub struct MxmlNamespace { - parent: Option>, - mappings: RefCell>, -} - -impl Default for MxmlNamespace { - fn default() -> Self { - Self::new(None) - } -} - -impl MxmlNamespace { - /// Returns the prefix used for the default XML namespace. - pub const DEFAULT_NAMESPACE: &'static str = ""; - - /// Constructs an empty set of namespace mappings. - pub fn new(parent: Option<&Rc>) -> Self { - let mut ns = Self { - parent: parent.map(|p| p.clone()), - mappings: RefCell::new(BTreeMap::new()), - }; - if ns.parent.is_none() { - ns.mappings.get_mut().insert(Self::DEFAULT_NAMESPACE.into(), "".into()); - ns.mappings.get_mut().insert("xmlns".into(), "http://www.w3.org/xml/xmlns".into()); - } - ns - } - - pub fn includes(&self, prefix: &str) -> bool { - self.mappings.borrow().contains_key(prefix) || match &self.parent { - Some(p) => p.includes(prefix), - None => false, - } - } - - /// Retrieves the value of a prefix either in the actual - /// set of mappings or in the parent set of mappings. - pub fn get(&self, prefix: &str) -> Option { - if let Some(value) = self.mappings.borrow().get(prefix) { - return Some(value.clone()); - } - self.parent.as_ref().and_then(|p| p.get(prefix)) - } - - pub fn set(&self, prefix: &str, value: &str) { - self.mappings.borrow_mut().insert(prefix.to_owned(), value.to_owned()); - } - - pub fn delete(&self, prefix: &str) -> bool { - self.mappings.borrow_mut().remove(prefix).is_some() - } - - pub fn clear(&self) { - self.mappings.borrow_mut().clear(); - } - - /// Returns the actual set of prefix mappings. - pub fn listing(&self) -> BTreeMap { - self.mappings.borrow().clone() - } - - /// Returns a concatenation of the parent set of prefix mappings - /// and the actual set of prefix mappings. - pub fn full_listing(&self) -> BTreeMap { - let mut listing = self.parent.as_ref().map_or(BTreeMap::new(), |p| p.full_listing()); - listing.extend(self.listing()); - listing - } -} diff --git a/crates/parser/tree/namespace_definition.rs b/crates/parser/tree/namespace_definition.rs deleted file mode 100644 index 1f77880..0000000 --- a/crates/parser/tree/namespace_definition.rs +++ /dev/null @@ -1,11 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct NamespaceDefinition { - pub location: Location, - pub asdoc: Option>, - pub attributes: Vec, - pub left: (String, Location), - pub right: Option>, -} \ No newline at end of file diff --git a/crates/parser/tree/new_expression.rs b/crates/parser/tree/new_expression.rs deleted file mode 100644 index 862e2da..0000000 --- a/crates/parser/tree/new_expression.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct NewExpression { - pub location: Location, - pub base: Rc, - pub arguments: Option>>, -} \ No newline at end of file diff --git a/crates/parser/tree/normal_configuration_directive.rs b/crates/parser/tree/normal_configuration_directive.rs deleted file mode 100644 index 3083db8..0000000 --- a/crates/parser/tree/normal_configuration_directive.rs +++ /dev/null @@ -1,13 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// Represents a `CONFIG::x ...` directive. -#[derive(Clone, Serialize, Deserialize)] -pub struct NormalConfigurationDirective { - pub location: Location, - /// The namespace, most commonly the `CONFIG` identifier. - pub namespace: (String, Location), - /// The constant name without including the qualifier. - pub constant_name: (String, Location), - pub directive: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/null_literal.rs b/crates/parser/tree/null_literal.rs deleted file mode 100644 index 6f286b7..0000000 --- a/crates/parser/tree/null_literal.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct NullLiteral { - pub location: Location, -} \ No newline at end of file diff --git a/crates/parser/tree/numeric_literal.rs b/crates/parser/tree/numeric_literal.rs deleted file mode 100644 index b695ebd..0000000 --- a/crates/parser/tree/numeric_literal.rs +++ /dev/null @@ -1,157 +0,0 @@ -use crate::ns::*; -use num_bigint::BigInt; -use serde::{Serialize, Deserialize}; -use std::str::FromStr; -use conv::ValueFrom; -use num_traits::ToPrimitive; - -#[derive(Clone, Serialize, Deserialize)] -pub struct NumericLiteral { - pub location: Location, - /// The numeric value in character representation. Such representation may be parsed - /// through data type specific methods such as [`NumericLiteral::parse_double()`]. - pub value: String, - pub suffix: NumberSuffix, -} - -#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq)] -pub enum NumberSuffix { - None, - F, -} - -impl NumericLiteral { - /// Parses a double-precision floating point either in - /// decimal, binary (`0b`) or hexadecimal (`0x`) notation. - pub fn parse_double(&self, negative: bool) -> Result { - let s = self.value.replace('_', ""); - if s.starts_with('0') { - if s[1..].starts_with('x') || s[1..].starts_with('X') { - let n = u64::from_str_radix(&(if negative { "-" } else { "" }.to_owned() + &s[2..]), 16); - return n.map_err(|_| ParserError::Common) - .and_then(|n| f64::value_from(n).map_err(|_| ParserError::Common)); - } else if s[1..].starts_with('b') || s[1..].starts_with('B') { - let n = u64::from_str_radix(&(if negative { "-" } else { "" }.to_owned() + &s[2..]), 2); - return n.map_err(|_| ParserError::Common) - .and_then(|n| f64::value_from(n).map_err(|_| ParserError::Common)); - } - } - f64::from_str(&(if negative { "-" } else { "" }.to_owned() + &s)).map_err(|_| ParserError::Common) - } - - /// Parses a single-precision floating point either in - /// decimal, binary (`0b`) or hexadecimal (`0x`) notation. - pub fn parse_float(&self, negative: bool) -> Result { - let s = self.value.replace('_', ""); - if s.starts_with('0') { - if s[1..].starts_with('x') || s[1..].starts_with('X') { - let n = u64::from_str_radix(&(if negative { "-" } else { "" }.to_owned() + &s[2..]), 16); - return n.map_err(|_| ParserError::Common) - .and_then(|n| f32::value_from(n).map_err(|_| ParserError::Common)); - } else if s[1..].starts_with('b') || s[1..].starts_with('B') { - let n = u64::from_str_radix(&(if negative { "-" } else { "" }.to_owned() + &s[2..]), 2); - return n.map_err(|_| ParserError::Common) - .and_then(|n| f32::value_from(n).map_err(|_| ParserError::Common)); - } - } - f32::from_str(&(if negative { "-" } else { "" }.to_owned() + &s)).map_err(|_| ParserError::Common) - } - - /// Parses a signed long either in - /// decimal, binary (`0b`) or hexadecimal (`0x`) notation. - pub fn parse_long(&self, negative: bool) -> Result { - let s = self.value.replace('_', ""); - if s.starts_with('0') { - if s[1..].starts_with('x') || s[1..].starts_with('X') { - let n = i64::from_str_radix(&(if negative { "-" } else { "" }.to_owned() + &s[2..]), 16); - return n.map_err(|_| ParserError::Common); - } else if s[1..].starts_with('b') || s[1..].starts_with('B') { - let n = i64::from_str_radix(&(if negative { "-" } else { "" }.to_owned() + &s[2..]), 2); - return n.map_err(|_| ParserError::Common); - } - } - i64::from_str(&s).map_err(|_| ParserError::Common) - } - - /// Parses a signed integer either in - /// decimal, binary (`0b`) or hexadecimal (`0x`) notation. - pub fn parse_int(&self, negative: bool) -> Result { - let s = self.value.replace('_', ""); - if s.starts_with('0') { - if s[1..].starts_with('x') || s[1..].starts_with('X') { - let n = i32::from_str_radix(&(if negative { "-" } else { "" }.to_owned() + &s[2..]), 16); - return n.map_err(|_| ParserError::Common); - } else if s[1..].starts_with('b') || s[1..].starts_with('B') { - let n = i32::from_str_radix(&(if negative { "-" } else { "" }.to_owned() + &s[2..]), 2); - return n.map_err(|_| ParserError::Common); - } - } - i32::from_str(&s).map_err(|_| ParserError::Common) - } - - /// Parses an unsigned integer either in - /// decimal, binary (`0b`) or hexadecimal (`0x`) notation. - pub fn parse_uint(&self) -> Result { - let s = self.value.replace('_', ""); - if s.starts_with('0') { - if s[1..].starts_with('x') || s[1..].starts_with('X') { - let n = u32::from_str_radix(&s[2..], 16); - return n.map_err(|_| ParserError::Common); - } else if s[1..].starts_with('b') || s[1..].starts_with('B') { - let n = u32::from_str_radix(&s[2..], 2); - return n.map_err(|_| ParserError::Common); - } - } - u32::from_str(&s).map_err(|_| ParserError::Common) - } - - /// Parses a big integer either in - /// decimal, binary (`0b`) or hexadecimal (`0x`) notation. - pub fn parse_big_int(&self, negative: bool) -> Result { - let s = self.value.replace('_', ""); - if s.starts_with('0') { - if s[1..].starts_with('x') || s[1..].starts_with('X') { - let mut digits: Vec = vec![]; - for ch in s[2..].chars() { - digits.push(CharacterValidator::hex_digit_mv(ch).unwrap().to_u8().unwrap()); - } - let n = BigInt::from_radix_be(if negative { num_bigint::Sign::Minus } else { num_bigint::Sign::Plus }, &digits, 16); - return n.map_or(Err(ParserError::Common), |n| Ok(n)); - } else if s[1..].starts_with('b') || s[1..].starts_with('B') { - let mut digits: Vec = vec![]; - for ch in s[2..].chars() { - digits.push(CharacterValidator::bin_digit_mv(ch).unwrap().to_u8().unwrap()); - } - let n = BigInt::from_radix_be(if negative { num_bigint::Sign::Minus } else { num_bigint::Sign::Plus }, &digits, 2); - return n.map_or(Err(ParserError::Common), |n| Ok(n)); - } - } - BigInt::from_str(&s).map_err(|_| ParserError::Common) - } -} - -mod tests { - #[allow(unused)] - use crate::ns::*; - #[allow(unused)] - use std::rc::Rc; - - #[test] - fn test_minimum_maximum() { - // Long.MIN_VALUE - let literal = NumericLiteral { - location: Location::with_offset(&Rc::new(CompilationUnit::default()), 0), - value: "0x8000_0000_0000_0000".to_owned(), - suffix: NumberSuffix::None, - }; - assert_eq!(i64::MIN, literal.parse_long(true).unwrap()); - - // Long.MAX_VALUE - let literal = NumericLiteral { - location: Location::with_offset(&Rc::new(CompilationUnit::default()), 0), - value: "0x7FFF_FFFF_FFFF_FFFF".to_owned(), - suffix: NumberSuffix::None, - }; - assert_eq!(i64::MAX, literal.parse_long(false).unwrap()); - } -} \ No newline at end of file diff --git a/crates/parser/tree/object_initializer.rs b/crates/parser/tree/object_initializer.rs deleted file mode 100644 index d7e6cbb..0000000 --- a/crates/parser/tree/object_initializer.rs +++ /dev/null @@ -1,63 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ObjectInitializer { - pub location: Location, - pub fields: Vec>, -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum InitializerField { - Field { - name: (FieldName, Location), - /// Non-null operator used for destructuring. - non_null: bool, - value: Option>, - }, - Rest((Rc, Location)), -} - -impl InitializerField { - pub fn location(&self) -> Location { - match self { - Self::Field { ref name, ref value, .. } => { - value.clone().map_or(name.1.clone(), |v| name.1.combine_with(v.location())) - }, - Self::Rest((_, ref l)) => l.clone(), - } - } - - pub fn shorthand(&self) -> Option<&QualifiedIdentifier> { - if let Self::Field { name, .. } = self { - if let FieldName::Identifier(qid) = &name.0 { - Some(qid) - } else { - None - } - } else { - None - } - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum FieldName { - Identifier(QualifiedIdentifier), - Brackets(Rc), - StringLiteral(Rc), - NumericLiteral(Rc), -} - -impl FieldName { - pub(crate) fn id(&self) -> Option<&QualifiedIdentifier> { - let Self::Identifier(id) = &self else { - return None; - }; - Some(id) - } - - pub fn id_equals(&self, name: &str) -> bool { - self.id().map(|name1| name == name1.to_identifier_name_or_asterisk().map(|id| id.0.clone()).unwrap_or("".into())).unwrap_or(false) - } -} \ No newline at end of file diff --git a/crates/parser/tree/optional_chaining_expression.rs b/crates/parser/tree/optional_chaining_expression.rs deleted file mode 100644 index b78c630..0000000 --- a/crates/parser/tree/optional_chaining_expression.rs +++ /dev/null @@ -1,21 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// An expression followed by optional chaining operations. -#[derive(Clone, Serialize, Deserialize)] -pub struct OptionalChainingExpression { - pub location: Location, - pub base: Rc, - /// Optional chaining operations. - /// - /// An `OptionalChainingPlaceholder` node is is the topmost expression - /// in the `expression` field. - pub expression: Rc, -} - -/// Internal expression used as the topmost expression -/// of a sequence of optional chaining operations. -#[derive(Clone, Serialize, Deserialize)] -pub struct OptionalChainingPlaceholder { - pub location: Location, -} \ No newline at end of file diff --git a/crates/parser/tree/package_concat_directive.rs b/crates/parser/tree/package_concat_directive.rs deleted file mode 100644 index dfed500..0000000 --- a/crates/parser/tree/package_concat_directive.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// The `public += ns.*;` directive. -#[derive(Clone, Serialize, Deserialize)] -pub struct PackageConcatDirective { - pub location: Location, - pub package_name: Vec<(String, Location)>, - pub import_specifier: ImportSpecifier, -} \ No newline at end of file diff --git a/crates/parser/tree/package_definition.rs b/crates/parser/tree/package_definition.rs deleted file mode 100644 index 8b447ce..0000000 --- a/crates/parser/tree/package_definition.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct PackageDefinition { - pub location: Location, - pub asdoc: Option>, - pub name: Vec<(String, Location)>, - pub block: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/paren_expression.rs b/crates/parser/tree/paren_expression.rs deleted file mode 100644 index ee3a330..0000000 --- a/crates/parser/tree/paren_expression.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ParenExpression { - pub location: Location, - pub expression: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/program.rs b/crates/parser/tree/program.rs deleted file mode 100644 index 2cabb41..0000000 --- a/crates/parser/tree/program.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct Program { - pub location: Location, - pub packages: Vec>, - pub directives: Vec>, -} \ No newline at end of file diff --git a/crates/parser/tree/qualified_identifier.rs b/crates/parser/tree/qualified_identifier.rs deleted file mode 100644 index 34d2cf2..0000000 --- a/crates/parser/tree/qualified_identifier.rs +++ /dev/null @@ -1,49 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct QualifiedIdentifier { - pub location: Location, - pub attribute: bool, - pub qualifier: Option>, - pub id: QualifiedIdentifierIdentifier, -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum QualifiedIdentifierIdentifier { - Id((String, Location)), - Brackets(Rc), -} - -impl QualifiedIdentifier { - pub fn to_identifier_name_or_asterisk(&self) -> Option<(String, Location)> { - if self.attribute || self.qualifier.is_some() { - None - } else { - if let QualifiedIdentifierIdentifier::Id(id) = &self.id { - Some(id.clone()) - } else { - None - } - } - } - - pub fn to_identifier_name(&self) -> Option<(String, Location)> { - if self.attribute || self.qualifier.is_some() { - None - } else { - if let QualifiedIdentifierIdentifier::Id(id) = &self.id { - if id.0 == "*" { None } else { Some(id.clone()) } - } else { - None - } - } - } - - pub fn is_identifier_token(&self) -> bool { - self.qualifier.is_none() && !self.attribute && match &self.id { - QualifiedIdentifierIdentifier::Id((id, _)) => id != "*", - _ => false, - } - } -} \ No newline at end of file diff --git a/crates/parser/tree/regexp_literal.rs b/crates/parser/tree/regexp_literal.rs deleted file mode 100644 index 366bf7a..0000000 --- a/crates/parser/tree/regexp_literal.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct RegExpLiteral { - pub location: Location, - pub body: String, - pub flags: String, -} \ No newline at end of file diff --git a/crates/parser/tree/reserved_namespace_expression.rs b/crates/parser/tree/reserved_namespace_expression.rs deleted file mode 100644 index 0e6e509..0000000 --- a/crates/parser/tree/reserved_namespace_expression.rs +++ /dev/null @@ -1,41 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub enum ReservedNamespaceExpression { - Public(Location), - Private(Location), - Protected(Location), - Internal(Location), -} - -impl ReservedNamespaceExpression { - pub fn location(&self) -> Location { - match self { - Self::Public(l) => l.clone(), - Self::Private(l) => l.clone(), - Self::Protected(l) => l.clone(), - Self::Internal(l) => l.clone(), - } - } - - pub fn to_attribute(&self) -> Attribute { - match self { - Self::Public(l) => Attribute::Public(l.clone()), - Self::Private(l) => Attribute::Private(l.clone()), - Self::Protected(l) => Attribute::Protected(l.clone()), - Self::Internal(l) => Attribute::Internal(l.clone()), - } - } -} - -impl ToString for ReservedNamespaceExpression { - fn to_string(&self) -> String { - match self { - Self::Public(_) => "public".into(), - Self::Private(_) => "private".into(), - Self::Protected(_) => "protected".into(), - Self::Internal(_) => "internal".into(), - } - } -} \ No newline at end of file diff --git a/crates/parser/tree/return_statement.rs b/crates/parser/tree/return_statement.rs deleted file mode 100644 index d31d41a..0000000 --- a/crates/parser/tree/return_statement.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ReturnStatement { - pub location: Location, - pub expression: Option>, -} \ No newline at end of file diff --git a/crates/parser/tree/sequence_expression.rs b/crates/parser/tree/sequence_expression.rs deleted file mode 100644 index c6b9dfc..0000000 --- a/crates/parser/tree/sequence_expression.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// Sequence expression (`x, y`). -#[derive(Clone, Serialize, Deserialize)] -pub struct SequenceExpression { - pub location: Location, - pub left: Rc, - pub right: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/string_literal.rs b/crates/parser/tree/string_literal.rs deleted file mode 100644 index 2361c16..0000000 --- a/crates/parser/tree/string_literal.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct StringLiteral { - pub location: Location, - pub value: String, -} \ No newline at end of file diff --git a/crates/parser/tree/super_expression.rs b/crates/parser/tree/super_expression.rs deleted file mode 100644 index 8d47cdd..0000000 --- a/crates/parser/tree/super_expression.rs +++ /dev/null @@ -1,18 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// Super expression. -/// -/// The super expression must always be followed by a property operator. -/// When the super expression appears in evaluation, the immediately -/// following property operator is limited to access a property from the base class -/// or invoke a method of the base class. -/// -/// ``` -/// super.f() -/// ``` -#[derive(Clone, Serialize, Deserialize)] -pub struct SuperExpression { - pub location: Location, - pub object: Option>>, -} \ No newline at end of file diff --git a/crates/parser/tree/super_statement.rs b/crates/parser/tree/super_statement.rs deleted file mode 100644 index 22a520a..0000000 --- a/crates/parser/tree/super_statement.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct SuperStatement { - pub location: Location, - pub arguments: Vec>, -} \ No newline at end of file diff --git a/crates/parser/tree/switch_statement.rs b/crates/parser/tree/switch_statement.rs deleted file mode 100644 index 652ec62..0000000 --- a/crates/parser/tree/switch_statement.rs +++ /dev/null @@ -1,46 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct SwitchStatement { - pub location: Location, - pub discriminant: Rc, - pub cases: Vec, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct Case { - pub location: Location, - pub labels: Vec, - pub directives: Vec>, -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum CaseLabel { - Case((Rc, Location)), - Default(Location), -} - -impl CaseLabel { - pub fn location(&self) -> Location { - match self { - Self::Case((_, l)) => l.clone(), - Self::Default(l) => l.clone(), - } - } -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct SwitchTypeStatement { - pub location: Location, - pub discriminant: Rc, - pub cases: Vec, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct TypeCase { - pub location: Location, - /// Case parameter. If `None`, designates a `default {}` case. - pub parameter: Option, - pub block: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/this_literal.rs b/crates/parser/tree/this_literal.rs deleted file mode 100644 index 4d4eeaf..0000000 --- a/crates/parser/tree/this_literal.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ThisLiteral { - pub location: Location, -} \ No newline at end of file diff --git a/crates/parser/tree/throw_statement.rs b/crates/parser/tree/throw_statement.rs deleted file mode 100644 index 48b71ff..0000000 --- a/crates/parser/tree/throw_statement.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct ThrowStatement { - pub location: Location, - pub expression: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/tree_semantics.rs b/crates/parser/tree/tree_semantics.rs deleted file mode 100644 index f65afa6..0000000 --- a/crates/parser/tree/tree_semantics.rs +++ /dev/null @@ -1,230 +0,0 @@ -use crate::ns::*; -use by_address::ByAddress; - -const LARGE_BYTES: usize = 26_000; - -/// Represents a mapping of nodes to meaning (*symbols*). -/// -/// A limited set of nodes may be mapped to symbols within this -/// structure through using the implemented `TreeSemanticsAccessor` -/// methods, such as `.get()` and `.set()`. -pub struct TreeSemantics { - common: TreeSemantics1, - large_units: RefCell>, TreeSemantics1>>, -} - -impl TreeSemantics { - pub fn new() -> Self { - Self { - common: TreeSemantics1::new(), - large_units: RefCell::new(HashMap::new()), - } - } -} - -/// Defines access methods for the `TreeSemantics` structure, -/// used for attaching semantics to the syntactic tree, -/// where `T` is the node type, and `S` is the symbol type. -pub trait TreeSemanticsAccessor { - fn get(&self, node: &Rc) -> Option; - fn set(&self, node: &Rc, symbol: Option); - fn delete(&self, node: &Rc) -> bool; - fn has(&self, node: &Rc) -> bool; -} - -macro impl_semantics_with_loc_call { - (struct $tree_semantics_id:ident, $($nodetype:ident),*$(,)?) => { - $( - impl TreeSemanticsAccessor<$nodetype, S> for $tree_semantics_id { - fn get(&self, node: &Rc<$nodetype>) -> Option { - let cu = node.location().compilation_unit(); - if cu.text().len() < LARGE_BYTES { - self.common.get(node) - } else { - let large_units = self.large_units.borrow(); - let m1 = large_units.get(&ByAddress(cu)); - m1.and_then(|m1| m1.get(node)) - } - } - fn set(&self, node: &Rc<$nodetype>, symbol: Option) { - let cu = node.location().compilation_unit(); - if cu.text().len() < LARGE_BYTES { - self.common.set(node, symbol); - } else { - let mut large_units = self.large_units.borrow_mut(); - let m1 = large_units.get_mut(&ByAddress(cu.clone())); - if let Some(m1) = m1 { - m1.set(node, symbol); - } else { - let m1 = TreeSemantics1::new(); - m1.set(node, symbol); - large_units.insert(ByAddress(cu), m1); - } - } - } - fn delete(&self, node: &Rc<$nodetype>) -> bool { - let cu = node.location().compilation_unit(); - if cu.text().len() < LARGE_BYTES { - self.common.delete(node) - } else { - let mut large_units = self.large_units.borrow_mut(); - let m1 = large_units.get_mut(&ByAddress(cu)); - m1.map(|m1| m1.delete(node)).unwrap_or(false) - } - } - fn has(&self, node: &Rc<$nodetype>) -> bool { - let cu = node.location().compilation_unit(); - if cu.text().len() < LARGE_BYTES { - self.common.has(node) - } else { - let large_units = self.large_units.borrow(); - let m1 = large_units.get(&ByAddress(cu)); - m1.map(|m1| m1.has(node)).unwrap_or(false) - } - } - } - )* - }, -} - -macro impl_semantics_with_loc_field { - (struct $tree_semantics_id:ident, $($nodetype:ident),*$(,)?) => { - $( - impl TreeSemanticsAccessor<$nodetype, S> for $tree_semantics_id { - fn get(&self, node: &Rc<$nodetype>) -> Option { - let cu = node.location.compilation_unit(); - if cu.text().len() < LARGE_BYTES { - self.common.get(node) - } else { - let large_units = self.large_units.borrow(); - let m1 = large_units.get(&ByAddress(cu)); - m1.and_then(|m1| m1.get(node)) - } - } - fn set(&self, node: &Rc<$nodetype>, symbol: Option) { - let cu = node.location.compilation_unit(); - if cu.text().len() < LARGE_BYTES { - self.common.set(node, symbol); - } else { - let mut large_units = self.large_units.borrow_mut(); - let m1 = large_units.get_mut(&ByAddress(cu.clone())); - if let Some(m1) = m1 { - m1.set(node, symbol); - } else { - let m1 = TreeSemantics1::new(); - m1.set(node, symbol); - large_units.insert(ByAddress(cu), m1); - } - } - } - fn delete(&self, node: &Rc<$nodetype>) -> bool { - let cu = node.location.compilation_unit(); - if cu.text().len() < LARGE_BYTES { - self.common.delete(node) - } else { - let mut large_units = self.large_units.borrow_mut(); - let m1 = large_units.get_mut(&ByAddress(cu)); - m1.map(|m1| m1.delete(node)).unwrap_or(false) - } - } - fn has(&self, node: &Rc<$nodetype>) -> bool { - let cu = node.location.compilation_unit(); - if cu.text().len() < LARGE_BYTES { - self.common.has(node) - } else { - let large_units = self.large_units.borrow(); - let m1 = large_units.get(&ByAddress(cu)); - m1.map(|m1| m1.has(node)).unwrap_or(false) - } - } - } - )* - }, -} - -macro impl_semantics_1 { - (struct $tree_semantics_1_id:ident, fn $new_id:ident, $($nodetype:ident),*$(,)?) => { - #[allow(non_snake_case)] - struct $tree_semantics_1_id { - $($nodetype: RefCell>, Option>>,)* - } - - impl $tree_semantics_1_id { - pub fn $new_id() -> Self { - Self { - $($nodetype: RefCell::new(HashMap::new()),)* - } - } - } - - $( - impl TreeSemanticsAccessor<$nodetype, S> for $tree_semantics_1_id { - fn get(&self, node: &Rc<$nodetype>) -> Option { - self.$nodetype.borrow().get(&NodeAsKey(node.clone())).map(|v| v.clone().unwrap()) - } - fn set(&self, node: &Rc<$nodetype>, symbol: Option) { - self.$nodetype.borrow_mut().insert(NodeAsKey(node.clone()), symbol); - } - fn delete(&self, node: &Rc<$nodetype>) -> bool { - self.$nodetype.borrow_mut().remove(&NodeAsKey(node.clone())).is_some() - } - fn has(&self, node: &Rc<$nodetype>) -> bool { - self.$nodetype.borrow().contains_key(&NodeAsKey(node.clone())) - } - } - )* - }, -} - -impl_semantics_with_loc_call!( - struct TreeSemantics, - Expression, - InitializerField, - Directive, - MxmlContent, - CssDirective, - CssMediaQueryCondition, - CssSelectorCondition, - CssPropertyValue, - CssSelector, -); - -impl_semantics_with_loc_field!( - struct TreeSemantics, - FunctionCommon, - Block, - Program, - SimpleVariableDefinition, - Mxml, - MxmlElement, - MxmlAttribute, - CssProperty, - CssRule, - CssDocument, - QualifiedIdentifier, -); - -impl_semantics_1!( - struct TreeSemantics1, - fn new, - Expression, - InitializerField, - Directive, - FunctionCommon, - Block, - Program, - SimpleVariableDefinition, - QualifiedIdentifier, - Mxml, - MxmlContent, - MxmlElement, - MxmlAttribute, - CssDirective, - CssRule, - CssMediaQueryCondition, - CssSelectorCondition, - CssPropertyValue, - CssSelector, - CssProperty, - CssDocument, -); \ No newline at end of file diff --git a/crates/parser/tree/try_statement.rs b/crates/parser/tree/try_statement.rs deleted file mode 100644 index 31a1922..0000000 --- a/crates/parser/tree/try_statement.rs +++ /dev/null @@ -1,23 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct TryStatement { - pub location: Location, - pub block: Rc, - pub catch_clauses: Vec, - pub finally_clause: Option, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct CatchClause { - pub location: Location, - pub parameter: TypedDestructuring, - pub block: Rc, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct FinallyClause { - pub location: Location, - pub block: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/type_definition.rs b/crates/parser/tree/type_definition.rs deleted file mode 100644 index 95dce41..0000000 --- a/crates/parser/tree/type_definition.rs +++ /dev/null @@ -1,11 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct TypeDefinition { - pub location: Location, - pub asdoc: Option>, - pub attributes: Vec, - pub left: (String, Location), - pub right: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/type_expression.rs b/crates/parser/tree/type_expression.rs deleted file mode 100644 index 0b0eec9..0000000 --- a/crates/parser/tree/type_expression.rs +++ /dev/null @@ -1,56 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct NullableTypeExpression { - pub location: Location, - pub base: Rc, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct NonNullableTypeExpression { - pub location: Location, - pub base: Rc, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct AnyTypeExpression { - pub location: Location, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct VoidTypeExpression { - pub location: Location, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct ArrayTypeExpression { - pub location: Location, - pub expression: Rc, -} - -/// A tuple type expression consisting of at least two elements. -#[derive(Clone, Serialize, Deserialize)] -pub struct TupleTypeExpression { - pub location: Location, - pub expressions: Vec>, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct FunctionTypeExpression { - pub location: Location, - pub parameters: Vec>, - pub result_type: Option>, -} - -/// ```plain -/// function(T, T=, ...) -/// function(...[T]) -/// ``` -#[derive(Clone, Serialize, Deserialize)] -pub struct FunctionTypeParameter { - pub location: Location, - pub kind: ParameterKind, - /// Possibly `None` for the rest parameter. - pub type_expression: Option>, -} \ No newline at end of file diff --git a/crates/parser/tree/type_parameter.rs b/crates/parser/tree/type_parameter.rs deleted file mode 100644 index adf800c..0000000 --- a/crates/parser/tree/type_parameter.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -/// A type parameter as in `function f.(): void {}`. -#[derive(Clone, Serialize, Deserialize)] -pub struct TypeParameter { - pub location: Location, - pub name: (String, Location), -} \ No newline at end of file diff --git a/crates/parser/tree/unary_expression.rs b/crates/parser/tree/unary_expression.rs deleted file mode 100644 index 64fbcc3..0000000 --- a/crates/parser/tree/unary_expression.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct UnaryExpression { - pub location: Location, - pub operator: Operator, - pub expression: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/use_namespace_directive.rs b/crates/parser/tree/use_namespace_directive.rs deleted file mode 100644 index 7317395..0000000 --- a/crates/parser/tree/use_namespace_directive.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct UseNamespaceDirective { - pub location: Location, - pub expression: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/variable_definition.rs b/crates/parser/tree/variable_definition.rs deleted file mode 100644 index 234492f..0000000 --- a/crates/parser/tree/variable_definition.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct VariableDefinition { - pub location: Location, - pub asdoc: Option>, - pub attributes: Vec, - pub kind: (VariableDefinitionKind, Location), - pub bindings: Vec>, -} - -#[derive(Copy, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub enum VariableDefinitionKind { - Var, - Const, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct SimpleVariableDefinition { - pub location: Location, - pub kind: (VariableDefinitionKind, Location), - pub bindings: Vec>, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct VariableBinding { - pub destructuring: TypedDestructuring, - pub initializer: Option>, -} - -impl VariableBinding { - pub fn location(&self) -> Location { - self.initializer.as_ref().map_or(self.destructuring.location.clone(), |init| self.destructuring.location.combine_with(init.location())) - } -} \ No newline at end of file diff --git a/crates/parser/tree/vector_literal.rs b/crates/parser/tree/vector_literal.rs deleted file mode 100644 index e506ed8..0000000 --- a/crates/parser/tree/vector_literal.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct VectorLiteral { - pub location: Location, - pub element_type: Rc, - pub elements: Vec, -} \ No newline at end of file diff --git a/crates/parser/tree/while_statement.rs b/crates/parser/tree/while_statement.rs deleted file mode 100644 index 2f20a97..0000000 --- a/crates/parser/tree/while_statement.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct WhileStatement { - pub location: Location, - pub test: Rc, - pub body: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/with_statement.rs b/crates/parser/tree/with_statement.rs deleted file mode 100644 index 0f254c3..0000000 --- a/crates/parser/tree/with_statement.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct WithStatement { - pub location: Location, - pub object: Rc, - pub body: Rc, -} \ No newline at end of file diff --git a/crates/parser/tree/xml_expression.rs b/crates/parser/tree/xml_expression.rs deleted file mode 100644 index 06857a8..0000000 --- a/crates/parser/tree/xml_expression.rs +++ /dev/null @@ -1,57 +0,0 @@ -use crate::ns::*; -use serde::{Serialize, Deserialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct XmlExpression { - pub location: Location, - pub element: Rc, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct XmlMarkupExpression { - pub location: Location, - pub markup: String, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct XmlListExpression { - pub location: Location, - pub content: Vec>, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct XmlElement { - pub location: Location, - pub name: XmlTagName, - pub attributes: Vec>, - pub attribute_expression: Option>, - pub content: Option>>, - pub closing_name: Option, -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum XmlTagName { - Name((String, Location)), - Expression(Rc), -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct XmlAttribute { - pub location: Location, - pub name: (String, Location), - pub value: XmlAttributeValue, -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum XmlAttributeValue { - Value((String, Location)), - Expression(Rc), -} - -#[derive(Clone, Serialize, Deserialize)] -pub enum XmlContent { - Characters((String, Location)), - Markup((String, Location)), - Element(Rc), - Expression(Rc), -} \ No newline at end of file diff --git a/crates/parser/util.rs b/crates/parser/util.rs deleted file mode 100644 index 256a627..0000000 --- a/crates/parser/util.rs +++ /dev/null @@ -1,36 +0,0 @@ -//! Miscellaneous. - -mod arena; -pub use arena::*; - -pub use by_address::ByAddress as NodeAsKey; - -mod character_reader; -pub use character_reader::*; - -mod escaping; -pub use escaping::*; - -mod css; -pub use css::*; - -pub use std::cell::{Cell, RefCell}; -pub use std::collections::{HashMap, HashSet}; -pub use std::rc::{Rc, Weak}; - -pub fn default() -> T { - T::default() -} - -/// Counts the first whitespace characters of a string. -pub fn count_first_whitespace_characters(input: &str) -> usize { - input.chars().count() - input.trim_start().chars().count() -} - -/// Decreases the last offset of a range without ever going behind the first offset. -pub fn decrease_last_offset(first_offset: usize, mut last_offset: usize, count: usize) -> usize { - for _ in 0..count { - last_offset = if first_offset < last_offset { last_offset - 1 } else { last_offset }; - } - last_offset -} \ No newline at end of file diff --git a/crates/parser/util/arena.rs b/crates/parser/util/arena.rs deleted file mode 100644 index e9027f4..0000000 --- a/crates/parser/util/arena.rs +++ /dev/null @@ -1,19 +0,0 @@ -use std::{cell::RefCell, rc::{Rc, Weak}}; - -pub struct Arena { - data: RefCell>>, -} - -impl Arena { - pub fn new() -> Self { - Self { - data: RefCell::new(vec![]), - } - } - - pub fn allocate(&self, value: T) -> Weak { - let obj = Rc::new(value); - self.data.borrow_mut().push(obj.clone()); - Rc::downgrade(&obj) - } -} \ No newline at end of file diff --git a/crates/parser/util/character_reader.rs b/crates/parser/util/character_reader.rs deleted file mode 100644 index c6af904..0000000 --- a/crates/parser/util/character_reader.rs +++ /dev/null @@ -1,138 +0,0 @@ -use std::str::CharIndices; - -/// `CharacterReader` may be used for iterating characters from left-to-right -/// in a string with miscellaneous operation methods. -#[derive(Clone)] -pub struct CharacterReader<'a> { - length: usize, - start_offset: usize, - char_indices: CharIndices<'a>, -} - -impl<'a> CharacterReader<'a> { - /// Constructs a `CharacterReader` from a string starting at the given offset. - pub fn from_offset(value: &'a str, offset: usize) -> Self { - CharacterReader { length: value.len(), start_offset: offset, char_indices: value[offset..].char_indices() } - } - - /// Indicates if there are remaining code points to read. - pub fn has_remaining(&self) -> bool { - self.clone().char_indices.next().is_some() - } - - /// Indicates if the reader has reached the end of the string. - pub fn reached_end(&self) -> bool { - self.clone().char_indices.next().is_none() - } - - /// Skips a code point in the reader. This is equivalent to - /// calling `next()`. - pub fn skip_in_place(&mut self) { - self.next(); - } - - /// Skips the given number of code points in the reader. - pub fn skip_count_in_place(&mut self, count: usize) { - for _ in 0..count { - if self.next().is_none() { - break; - } - } - } - - /// Returns the current byte offset in the string. - pub fn index(&self) -> usize { - self.clone().char_indices.next().map_or(self.length, |(i, _)| self.start_offset + i) - } - - /// Returns the next code point. If there are no code points - /// available, returns U+00. - pub fn next_or_zero(&mut self) -> char { - self.char_indices.next().map_or('\x00', |(_, cp)| cp) - } - - /// Peeks the next code point. - pub fn peek(&self) -> Option { - self.clone().char_indices.next().map(|(_, cp)| cp) - } - - /// Peeks the next code point. If there are no code points - /// available, returns U+00. - pub fn peek_or_zero(&self) -> char { - self.clone().next_or_zero() - } - - /// Peeks the next code point at the given zero based code point index. - pub fn peek_at(&self, index: usize) -> Option { - let mut indices = self.clone().char_indices; - for _ in 0..index { - if indices.next().is_none() { - break; - } - } - indices.next().map(|(_, cp)| cp) - } - - /// Peeks the next code point at the given zero based code point index. - /// If there are no code points available, returns U+00. - pub fn peek_at_or_zero(&self, index: usize) -> char { - self.peek_at(index).unwrap_or('\x00') - } - - /// Peeks a number of code points until the string's end. - pub fn peek_seq(&self, num_code_points: u64) -> String { - let mut r = String::new(); - let mut next_indices = self.char_indices.clone(); - for _ in 0..num_code_points { - match next_indices.next() { - None => { - break; - }, - Some(cp) => { - r.push(cp.1); - } - } - } - r - } -} - -impl<'a> From<&'a str> for CharacterReader<'a> { - /// Constructs a `CharacterReader` from a string. - fn from(value: &'a str) -> Self { - CharacterReader { length: value.len(), start_offset: 0, char_indices: value.char_indices() } - } -} - -impl<'a> From<&'a String> for CharacterReader<'a> { - /// Constructs a `CharacterReader` from a string. - fn from(value: &'a String) -> Self { - CharacterReader { length: value.len(), start_offset: 0, char_indices: value.char_indices() } - } -} - -impl<'a> Iterator for CharacterReader<'a> { - type Item = char; - - fn next(&mut self) -> Option { - self.char_indices.next().map(|(_, cp)| cp) - } -} - -#[cfg(test)] -mod test { - use super::CharacterReader; - #[test] - fn test() { - let mut reader = CharacterReader::from("foo"); - assert!(reader.has_remaining()); - assert_eq!(reader.peek_seq(5), "foo"); - assert_eq!(reader.peek_seq(1), "f"); - assert_eq!(reader.peek_or_zero(), 'f'); - for _ in 0..3 { - reader.next(); - } - assert_eq!(reader.peek_or_zero(), '\x00'); - assert!(reader.reached_end()); - } -} \ No newline at end of file diff --git a/crates/parser/util/css.rs b/crates/parser/util/css.rs deleted file mode 100644 index fa1fbf8..0000000 --- a/crates/parser/util/css.rs +++ /dev/null @@ -1,163 +0,0 @@ -use std::collections::HashMap; -use lazy_static::lazy_static; -use maplit::hashmap; - -/// Converts a CSS [color constant](http://www.w3schools.com/css/css_colorsfull.asp) into an integer. -pub fn css_color_constant_to_int(name: &str) -> Option { - COLOR_MAP.get(&name.to_lowercase()).map(|i| *i) -} - -lazy_static! { - /// Map color constant names to 24-bit RGB integer values. - /// - /// See also: [CSS colors](http://www.w3schools.com/css/css_colorsfull.asp) - static ref COLOR_MAP: HashMap = hashmap! { - "black".into() => 0x000000, - "navy".into() => 0x000080, - "darkblue".into() => 0x00008b, - "mediumblue".into() => 0x0000cd, - "blue".into() => 0x0000ff, - "darkgreen".into() => 0x006400, - "green".into() => 0x008000, - "teal".into() => 0x008080, - "darkcyan".into() => 0x008b8b, - "deepskyblue".into() => 0x00bfff, - "darkturquoise".into() => 0x00ced1, - "mediumspringgreen".into() => 0x00fa9a, - "lime".into() => 0x00ff00, - "springgreen".into() => 0x00ff7f, - "aqua".into() => 0x00ffff, - "cyan".into() => 0x00ffff, - "midnightblue".into() => 0x191970, - "dodgerblue".into() => 0x1e90ff, - "lightseagreen".into() => 0x20b2aa, - "forestgreen".into() => 0x228b22, - "seagreen".into() => 0x2e8b57, - "darkslategray".into() => 0x2f4f4f, - "darkslategrey".into() => 0x2f4f4f, - "limegreen".into() => 0x32cd32, - "mediumseagreen".into() => 0x3cb371, - "turquoise".into() => 0x40e0d0, - "royalblue".into() => 0x4169e1, - "steelblue".into() => 0x4682b4, - "darkslateblue".into() => 0x483d8b, - "mediumturquoise".into() => 0x48d1cc, - "indigo ".into() => 0x4b0082, - "darkolivegreen".into() => 0x556b2f, - "cadetblue".into() => 0x5f9ea0, - "cornflowerblue".into() => 0x6495ed, - "mediumaquamarine".into() => 0x66cdaa, - "dimgray".into() => 0x696969, - "dimgrey".into() => 0x696969, - "slateblue".into() => 0x6a5acd, - "olivedrab".into() => 0x6b8e23, - "slategray".into() => 0x708090, - "slategrey".into() => 0x708090, - "lightslategray".into() => 0x778899, - "lightslategrey".into() => 0x778899, - "mediumslateblue".into() => 0x7b68ee, - "lawngreen".into() => 0x7cfc00, - "chartreuse".into() => 0x7fff00, - "aquamarine".into() => 0x7fffd4, - "maroon".into() => 0x800000, - "purple".into() => 0x800080, - "olive".into() => 0x808000, - "gray".into() => 0x808080, - "grey".into() => 0x808080, - "skyblue".into() => 0x87ceeb, - "lightskyblue".into() => 0x87cefa, - "blueviolet".into() => 0x8a2be2, - "darkred".into() => 0x8b0000, - "darkmagenta".into() => 0x8b008b, - "saddlebrown".into() => 0x8b4513, - "darkseagreen".into() => 0x8fbc8f, - "lightgreen".into() => 0x90ee90, - "mediumpurple".into() => 0x9370d8, - "darkviolet".into() => 0x9400d3, - "palegreen".into() => 0x98fb98, - "darkorchid".into() => 0x9932cc, - "yellowgreen".into() => 0x9acd32, - "sienna".into() => 0xa0522d, - "brown".into() => 0xa52a2a, - "darkgray".into() => 0xa9a9a9, - "darkgrey".into() => 0xa9a9a9, - "lightblue".into() => 0xadd8e6, - "greenyellow".into() => 0xadff2f, - "paleturquoise".into() => 0xafeeee, - "lightsteelblue".into() => 0xb0c4de, - "powderblue".into() => 0xb0e0e6, - "firebrick".into() => 0xb22222, - "darkgoldenrod".into() => 0xb8860b, - "mediumorchid".into() => 0xba55d3, - "rosybrown".into() => 0xbc8f8f, - "darkkhaki".into() => 0xbdb76b, - "silver".into() => 0xc0c0c0, - "mediumvioletred".into() => 0xc71585, - "indianred ".into() => 0xcd5c5c, - "peru".into() => 0xcd853f, - "chocolate".into() => 0xd2691e, - "tan".into() => 0xd2b48c, - "lightgray".into() => 0xd3d3d3, - "lightgrey".into() => 0xd3d3d3, - "palevioletred".into() => 0xd87093, - "thistle".into() => 0xd8bfd8, - "orchid".into() => 0xda70d6, - "goldenrod".into() => 0xdaa520, - "crimson".into() => 0xdc143c, - "gainsboro".into() => 0xdcdcdc, - "plum".into() => 0xdda0dd, - "burlywood".into() => 0xdeb887, - "lightcyan".into() => 0xe0ffff, - "lavender".into() => 0xe6e6fa, - "darksalmon".into() => 0xe9967a, - "violet".into() => 0xee82ee, - "palegoldenrod".into() => 0xeee8aa, - "lightcoral".into() => 0xf08080, - "khaki".into() => 0xf0e68c, - "aliceblue".into() => 0xf0f8ff, - "honeydew".into() => 0xf0fff0, - "azure".into() => 0xf0ffff, - "sandybrown".into() => 0xf4a460, - "wheat".into() => 0xf5deb3, - "beige".into() => 0xf5f5dc, - "whitesmoke".into() => 0xf5f5f5, - "mintcream".into() => 0xf5fffa, - "ghostwhite".into() => 0xf8f8ff, - "salmon".into() => 0xfa8072, - "antiquewhite".into() => 0xfaebd7, - "linen".into() => 0xfaf0e6, - "lightgoldenrodyellow".into() => 0xfafad2, - "oldlace".into() => 0xfdf5e6, - "red".into() => 0xff0000, - "fuchsia".into() => 0xff00ff, - "magenta".into() => 0xff00ff, - "deeppink".into() => 0xff1493, - "orangered".into() => 0xff4500, - "tomato".into() => 0xff6347, - "hotpink".into() => 0xff69b4, - "coral".into() => 0xff7f50, - "darkorange".into() => 0xff8c00, - "lightsalmon".into() => 0xffa07a, - "orange".into() => 0xffa500, - "lightpink".into() => 0xffb6c1, - "pink".into() => 0xffc0cb, - "gold".into() => 0xffd700, - "peachpuff".into() => 0xffdab9, - "navajowhite".into() => 0xffdead, - "moccasin".into() => 0xffe4b5, - "bisque".into() => 0xffe4c4, - "mistyrose".into() => 0xffe4e1, - "blanchedalmond".into() => 0xffebcd, - "papayawhip".into() => 0xffefd5, - "lavenderblush".into() => 0xfff0f5, - "seashell".into() => 0xfff5ee, - "cornsilk".into() => 0xfff8dc, - "lemonchiffon".into() => 0xfffacd, - "floralwhite".into() => 0xfffaf0, - "snow".into() => 0xfffafa, - "yellow".into() => 0xffff00, - "lightyellow".into() => 0xffffe0, - "ivory".into() => 0xfffff0, - "white".into() => 0xffffff, - }; -} \ No newline at end of file diff --git a/crates/parser/util/escaping.rs b/crates/parser/util/escaping.rs deleted file mode 100644 index ffa020f..0000000 --- a/crates/parser/util/escaping.rs +++ /dev/null @@ -1,11 +0,0 @@ -/// Escapes XML special characters. -pub fn escape_xml(characters: &str) -> String { - let escaped = htmlentity::entity::encode(characters.as_ref(), &htmlentity::entity::EncodeType::NamedOrHex, &htmlentity::entity::CharacterSet::SpecialChars); - String::from_utf8_lossy(&escaped.bytes().into_owned()).into_owned() -} - -/// Unescapes XML entities conforming to HTML entities. -pub fn unescape_xml(input: &str) -> String { - let unescaped = htmlentity::entity::decode(input.as_ref()); - String::from_utf8_lossy(&unescaped.bytes().into_owned()).into_owned() -} \ No newline at end of file diff --git a/crates/parser_test/Cargo.toml b/crates/parser_test/Cargo.toml deleted file mode 100644 index ba62f77..0000000 --- a/crates/parser_test/Cargo.toml +++ /dev/null @@ -1,23 +0,0 @@ -[package] -name = "as3_parser_test" -version = "0.4.0" -edition = "2021" -authors = ["hydroper "] -repository = "https://github.com/hydroper/as3_parser" -keywords = ["actionscript", "as3", "parser"] -description = "ActionScript 3 Parser Test" -license = "Apache-2.0" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[[bin]] -name = "as3_parser_test" -path = "main.rs" - -[dependencies] -clap = { version = "4.4.8", features = ["derive"] } -file_paths = "1.0.0" -as3_parser = { path = "../parser", version = "1" } -maplit = "1.0.2" -serde = { version = "1.0.192", features = ["rc", "derive"] } -serde_json = "1.0.108" diff --git a/crates/parser_test/main.rs b/crates/parser_test/main.rs deleted file mode 100644 index 6abee8f..0000000 --- a/crates/parser_test/main.rs +++ /dev/null @@ -1,62 +0,0 @@ -use clap::Parser; -use file_paths::FlexPath; -use std::{env, fs, io}; -use as3_parser::ns::*; - -#[derive(Parser, Debug)] -#[command(author, version, about, long_about = None)] -struct Arguments { - #[arg(short, long)] - source_path: String, - - #[arg(short, long)] - file_log: bool, - - #[arg(short, long)] - mxml: bool, - - #[arg(short, long)] - css: bool, -} - -fn main() -> io::Result<()> { - let arguments = Arguments::parse(); - let source_path = FlexPath::from_n_native([env::current_dir().unwrap().to_string_lossy().into_owned().as_ref(), arguments.source_path.as_ref()]).to_string_with_flex_separator(); - - // Canonicalize path - // let source_path = std::path::Path::new(&source_path).canonicalize().unwrap().to_string_lossy().into_owned(); - - let source_path_ast_json = FlexPath::new_native(&source_path).change_extension(".tree").to_string_with_flex_separator(); - let source_path_diagnostics = FlexPath::new_native(&source_path).change_extension(".diag").to_string_with_flex_separator(); - let source_content = fs::read_to_string(&source_path)?; - let compilation_unit = CompilationUnit::new(Some(source_path), source_content); - if arguments.mxml { - let document = ParserFacade(&compilation_unit, default()).parse_mxml(); - if arguments.file_log { - fs::write(&source_path_ast_json, serde_json::to_string_pretty(&document).unwrap())?; - } - } else if arguments.css { - let document = CssParserFacade(&compilation_unit, default()).parse_document(); - if arguments.file_log { - fs::write(&source_path_ast_json, serde_json::to_string_pretty(&document).unwrap())?; - } - } else { - let program = ParserFacade(&compilation_unit, default()).parse_program(); - if arguments.file_log { - fs::write(&source_path_ast_json, serde_json::to_string_pretty(&program).unwrap())?; - } - } - let mut diagnostics = vec![]; - compilation_unit.sort_diagnostics(); - for diagnostic in compilation_unit.nested_diagnostics() { - diagnostics.push(diagnostic.format_english()); - } - if arguments.file_log { - fs::write(&source_path_diagnostics, diagnostics.join("\n"))?; - } else { - for diagnostic in diagnostics { - println!("{diagnostic}"); - } - } - Ok(()) -} \ No newline at end of file diff --git a/demo/Cargo.toml b/demo/Cargo.toml deleted file mode 100644 index 7614571..0000000 --- a/demo/Cargo.toml +++ /dev/null @@ -1,18 +0,0 @@ -[package] -name = "as3_parser_demo" -version = "0.1.0" -edition = "2021" - -[lib] -crate-type = ["cdylib"] - -[dependencies] -as3_parser = { path = "../crates/parser", version = "1" } -maplit = "1.0.2" -serde = { version = "1.0.192", features = ["rc", "derive"] } -serde_json = "1.0.108" -wasm-bindgen = "0.2.92" - -[dependencies.web-sys] -version = "0.3.4" -features = ["Document", "Window"] \ No newline at end of file diff --git a/demo/dist/as3_parser_demo.d.ts b/demo/dist/as3_parser_demo.d.ts new file mode 100644 index 0000000..68bc12b --- /dev/null +++ b/demo/dist/as3_parser_demo.d.ts @@ -0,0 +1,40 @@ +/* tslint:disable */ +/* eslint-disable */ +/** +* @param {string} input +* @param {string} source_type +* @returns {string} +*/ +export function parse(input: string, source_type: string): string; + +export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; + +export interface InitOutput { + readonly memory: WebAssembly.Memory; + readonly parse: (a: number, b: number, c: number, d: number, e: number) => void; + readonly __wbindgen_add_to_stack_pointer: (a: number) => number; + readonly __wbindgen_malloc: (a: number, b: number) => number; + readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; + readonly __wbindgen_free: (a: number, b: number, c: number) => void; +} + +export type SyncInitInput = BufferSource | WebAssembly.Module; +/** +* Instantiates the given `module`, which can either be bytes or +* a precompiled `WebAssembly.Module`. +* +* @param {SyncInitInput} module +* +* @returns {InitOutput} +*/ +export function initSync(module: SyncInitInput): InitOutput; + +/** +* If `module_or_path` is {RequestInfo} or {URL}, makes a request and +* for everything else, calls `WebAssembly.instantiate` directly. +* +* @param {InitInput | Promise} module_or_path +* +* @returns {Promise} +*/ +export default function __wbg_init (module_or_path?: InitInput | Promise): Promise; diff --git a/demo/dist/as3_parser_demo.js b/demo/dist/as3_parser_demo.js new file mode 100644 index 0000000..0e1f671 --- /dev/null +++ b/demo/dist/as3_parser_demo.js @@ -0,0 +1,199 @@ +let wasm; + +let WASM_VECTOR_LEN = 0; + +let cachedUint8Memory0 = null; + +function getUint8Memory0() { + if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { + cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8Memory0; +} + +const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } ); + +const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' + ? function (arg, view) { + return cachedTextEncoder.encodeInto(arg, view); +} + : function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; +}); + +function passStringToWasm0(arg, malloc, realloc) { + + if (realloc === undefined) { + const buf = cachedTextEncoder.encode(arg); + const ptr = malloc(buf.length, 1) >>> 0; + getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); + WASM_VECTOR_LEN = buf.length; + return ptr; + } + + let len = arg.length; + let ptr = malloc(len, 1) >>> 0; + + const mem = getUint8Memory0(); + + let offset = 0; + + for (; offset < len; offset++) { + const code = arg.charCodeAt(offset); + if (code > 0x7F) break; + mem[ptr + offset] = code; + } + + if (offset !== len) { + if (offset !== 0) { + arg = arg.slice(offset); + } + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; + const view = getUint8Memory0().subarray(ptr + offset, ptr + len); + const ret = encodeString(arg, view); + + offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; + } + + WASM_VECTOR_LEN = offset; + return ptr; +} + +let cachedInt32Memory0 = null; + +function getInt32Memory0() { + if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) { + cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); + } + return cachedInt32Memory0; +} + +const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } ); + +if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); }; + +function getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); +} +/** +* @param {string} input +* @param {string} source_type +* @returns {string} +*/ +export function parse(input, source_type) { + let deferred3_0; + let deferred3_1; + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(source_type, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + wasm.parse(retptr, ptr0, len0, ptr1, len1); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + deferred3_0 = r0; + deferred3_1 = r1; + return getStringFromWasm0(r0, r1); + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } +} + +async function __wbg_load(module, imports) { + if (typeof Response === 'function' && module instanceof Response) { + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + + } catch (e) { + if (module.headers.get('Content-Type') != 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { + throw e; + } + } + } + + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); + + } else { + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + + } else { + return instance; + } + } +} + +function __wbg_get_imports() { + const imports = {}; + imports.wbg = {}; + + return imports; +} + +function __wbg_init_memory(imports, maybe_memory) { + +} + +function __wbg_finalize_init(instance, module) { + wasm = instance.exports; + __wbg_init.__wbindgen_wasm_module = module; + cachedInt32Memory0 = null; + cachedUint8Memory0 = null; + + + return wasm; +} + +function initSync(module) { + if (wasm !== undefined) return wasm; + + const imports = __wbg_get_imports(); + + __wbg_init_memory(imports); + + if (!(module instanceof WebAssembly.Module)) { + module = new WebAssembly.Module(module); + } + + const instance = new WebAssembly.Instance(module, imports); + + return __wbg_finalize_init(instance, module); +} + +async function __wbg_init(input) { + if (wasm !== undefined) return wasm; + + if (typeof input === 'undefined') { + input = new URL('as3_parser_demo_bg.wasm', import.meta.url); + } + const imports = __wbg_get_imports(); + + if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) { + input = fetch(input); + } + + __wbg_init_memory(imports); + + const { instance, module } = await __wbg_load(await input, imports); + + return __wbg_finalize_init(instance, module); +} + +export { initSync } +export default __wbg_init; diff --git a/demo/dist/as3_parser_demo_bg.wasm b/demo/dist/as3_parser_demo_bg.wasm new file mode 100644 index 0000000..7d86391 Binary files /dev/null and b/demo/dist/as3_parser_demo_bg.wasm differ diff --git a/demo/dist/as3_parser_demo_bg.wasm.d.ts b/demo/dist/as3_parser_demo_bg.wasm.d.ts new file mode 100644 index 0000000..e814710 --- /dev/null +++ b/demo/dist/as3_parser_demo_bg.wasm.d.ts @@ -0,0 +1,8 @@ +/* tslint:disable */ +/* eslint-disable */ +export const memory: WebAssembly.Memory; +export function parse(a: number, b: number, c: number, d: number, e: number): void; +export function __wbindgen_add_to_stack_pointer(a: number): number; +export function __wbindgen_malloc(a: number, b: number): number; +export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number; +export function __wbindgen_free(a: number, b: number, c: number): void; diff --git a/demo/src/lib.rs b/demo/src/lib.rs deleted file mode 100644 index 80e49c8..0000000 --- a/demo/src/lib.rs +++ /dev/null @@ -1,58 +0,0 @@ -use serde::{Deserialize, Serialize}; -use wasm_bindgen::prelude::*; -use as3_parser::ns::*; - -#[derive(Serialize, Deserialize)] -struct ParserResult { - program: Option>, - mxml: Option>, - css: Option>, - diagnostics: Vec, -} - -#[derive(Serialize, Deserialize)] -struct ParserDiagnosticResult { - warning: bool, - column1: usize, - column2: usize, - line1: usize, - line2: usize, - message: String, -} - -#[wasm_bindgen] -pub fn parse(input: &str, source_type: &str) -> String { - let compilation_unit = CompilationUnit::new(None, input.to_owned()); - - let mut program: Option> = None; - let mut mxml: Option> = None; - let mut css: Option> = None; - - let source_type = source_type.to_lowercase(); - - if source_type == "mxml" { - mxml = Some(ParserFacade(&compilation_unit, default()).parse_mxml()); - } else if source_type == "css" { - css = Some(CssParserFacade(&compilation_unit, default()).parse_document()); - } else { - program = Some(ParserFacade(&compilation_unit, default()).parse_program()); - } - let mut diagnostics = vec![]; - compilation_unit.sort_diagnostics(); - for diagnostic in compilation_unit.nested_diagnostics() { - diagnostics.push(ParserDiagnosticResult { - warning: diagnostic.is_warning(), - column1: diagnostic.location().first_column() + 1, - column2: diagnostic.location().last_column() + 1, - line1: diagnostic.location().first_line_number(), - line2: diagnostic.location().last_line_number(), - message: diagnostic.format_message_english(), - }); - } - serde_json::to_string_pretty(&ParserResult { - program, - mxml, - css, - diagnostics, - }).unwrap() -} \ No newline at end of file diff --git a/examples/asdoc/.gitignore b/examples/asdoc/.gitignore deleted file mode 100644 index ea8c4bf..0000000 --- a/examples/asdoc/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target diff --git a/examples/asdoc/Cargo.toml b/examples/asdoc/Cargo.toml deleted file mode 100644 index d7c7409..0000000 --- a/examples/asdoc/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "asdoc-example" -version = "0.1.0" -edition = "2021" - -[dependencies] -as3_parser = { path = "../../crates/parser", version = "1" } - -[[example]] -name = "asdoc" -path = "example.rs" \ No newline at end of file diff --git a/examples/asdoc/Example.as b/examples/asdoc/Example.as deleted file mode 100644 index d73fc3d..0000000 --- a/examples/asdoc/Example.as +++ /dev/null @@ -1,10 +0,0 @@ -package q1 { - public namespace q1ns - public class C1 { - /** - * @private - * Comment for q1.q1ns::x - */ - q1.q1ns var x - } -} \ No newline at end of file diff --git a/examples/asdoc/example.rs b/examples/asdoc/example.rs deleted file mode 100644 index 2e10ca1..0000000 --- a/examples/asdoc/example.rs +++ /dev/null @@ -1,61 +0,0 @@ -use std::env; -use as3_parser::ns::*; - -fn main() { - // Define source path - let source_path = env::current_dir().unwrap().join("Example.as").to_string_lossy().into_owned(); - - // Read source content - let source_content = include_str!("Example.as").to_owned(); - - // Create compilation unit - let compilation_unit = CompilationUnit::new(Some(source_path), source_content); - - // Parse program - let program = ParserFacade(&compilation_unit, default()).parse_program(); - visit_program(&program); - - // Report diagnostics - compilation_unit.sort_diagnostics(); - for diagnostic in compilation_unit.nested_diagnostics() { - println!("{}", diagnostic.format_english()); - } -} - -fn visit_program(program: &Rc) { - for package in program.packages.iter() { - for directive in package.block.directives.iter() { - // directive: Rc - - match directive.as_ref() { - Directive::ClassDefinition(defn) => { - visit_class(&defn); - }, - - _ => {}, - } - } - } -} - -fn visit_class(defn: &ClassDefinition) { - for directive in defn.block.directives.iter() { - if let Directive::VariableDefinition(defn) = directive.as_ref() { - // Print any found main body and @private tags - if let Some(asdoc) = &defn.asdoc { - print_asdoc(asdoc); - } - } - } -} - -fn print_asdoc(asdoc: &Rc) { - if let Some((text, loc)) = &asdoc.main_body { - println!("Found main body at {}:{}\n\n{}\n\n", loc.first_line_number(), loc.first_column() + 1, text); - } - for (tag, loc) in &asdoc.tags { - if matches!(tag, AsDocTag::Private) { - println!("Found @private tag at {}:{}\n\n", loc.first_line_number(), loc.first_column() + 1); - } - } -} \ No newline at end of file diff --git a/tests/parser/ASDoc.as b/tests/parser/ASDoc.as deleted file mode 100644 index c14a1cc..0000000 --- a/tests/parser/ASDoc.as +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Main body. - * @copy C1#x - * @inheritDoc - * @see C1#x X description - */ -var x - -/** - * N1. - */ -[N1] -/** - * N2. Lorem - * ipsum. - */ -[N2] -var y - -/** - * @throws SyntaxError If a syntax error occurs. - */ -function m() { -} \ No newline at end of file diff --git a/tests/parser/ASDoc.diag b/tests/parser/ASDoc.diag deleted file mode 100644 index e69de29..0000000 diff --git a/tests/parser/ASDoc.tree b/tests/parser/ASDoc.tree deleted file mode 100644 index f8c14d8..0000000 --- a/tests/parser/ASDoc.tree +++ /dev/null @@ -1,235 +0,0 @@ -{ - "location": "7:1-24:2", - "packages": [], - "directives": [ - { - "VariableDefinition": { - "location": "7:1-7:6", - "asdoc": { - "location": "1:1-6:4", - "main_body": [ - "Main body.", - "2:4-2:14" - ], - "tags": [ - [ - { - "Copy": { - "base": { - "QualifiedIdentifier": { - "location": "3:10-3:12", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "C1", - "3:10-3:12" - ] - } - } - }, - "instance_property": { - "location": "3:13-3:14", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "x", - "3:13-3:14" - ] - } - } - } - }, - "3:4-3:14" - ], - [ - "InheritDoc", - "4:4-4:15" - ], - [ - { - "See": { - "reference": { - "base": { - "QualifiedIdentifier": { - "location": "5:9-5:11", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "C1", - "5:9-5:11" - ] - } - } - }, - "instance_property": { - "location": "5:12-5:13", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "x", - "5:12-5:13" - ] - } - } - }, - "display_text": "X description" - } - }, - "5:4-5:27" - ] - ] - }, - "attributes": [], - "kind": [ - "Var", - "7:1-7:4" - ], - "bindings": [ - { - "destructuring": { - "location": "7:5-7:6", - "destructuring": { - "QualifiedIdentifier": { - "location": "7:5-7:6", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "x", - "7:5-7:6" - ] - } - } - }, - "type_annotation": null - }, - "initializer": null - } - ] - } - }, - { - "VariableDefinition": { - "location": "12:1-18:6", - "asdoc": { - "location": "13:1-16:4", - "main_body": [ - "N2. Lorem\nipsum.", - "14:4-15:10" - ], - "tags": [] - }, - "attributes": [ - { - "Metadata": { - "location": "12:2-12:4", - "asdoc": null, - "name": [ - "N1", - "12:2-12:4" - ], - "entries": null - } - }, - { - "Metadata": { - "location": "17:2-17:4", - "asdoc": null, - "name": [ - "N2", - "17:2-17:4" - ], - "entries": null - } - } - ], - "kind": [ - "Var", - "18:1-18:4" - ], - "bindings": [ - { - "destructuring": { - "location": "18:5-18:6", - "destructuring": { - "QualifiedIdentifier": { - "location": "18:5-18:6", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "y", - "18:5-18:6" - ] - } - } - }, - "type_annotation": null - }, - "initializer": null - } - ] - } - }, - { - "FunctionDefinition": { - "location": "23:1-24:2", - "asdoc": { - "location": "20:1-22:4", - "main_body": null, - "tags": [ - [ - { - "Throws": { - "class_reference": { - "QualifiedIdentifier": { - "location": "21:12-21:23", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "SyntaxError", - "21:12-21:23" - ] - } - } - }, - "description": "If a syntax error occurs." - } - }, - "21:4-21:49" - ] - ] - }, - "attributes": [], - "name": { - "Identifier": [ - "m", - "23:10-23:11" - ] - }, - "common": { - "location": "23:11-24:2", - "contains_yield": false, - "contains_await": false, - "signature": { - "location": "23:11-23:13", - "parameters": [], - "result_type": null - }, - "body": { - "Block": { - "location": "23:14-24:2", - "metadata": null, - "directives": [] - } - } - } - } - } - ] -} \ No newline at end of file diff --git a/tests/parser/Attributes.as b/tests/parser/Attributes.as deleted file mode 100644 index 96fc965..0000000 --- a/tests/parser/Attributes.as +++ /dev/null @@ -1,17 +0,0 @@ -q.x var y - -[N1] internal var z - -[N1(x = "y")] {} - -dynamic final class C1 {} - -/** - * Foo - */ -[N1] -/** - * @eventType FooEvent.FOO - */ -[Event(name = "foo", type = "FooEvent")] -class C1 {} \ No newline at end of file diff --git a/tests/parser/Attributes.diag b/tests/parser/Attributes.diag deleted file mode 100644 index e69de29..0000000 diff --git a/tests/parser/Attributes.tree b/tests/parser/Attributes.tree deleted file mode 100644 index ee105c8..0000000 --- a/tests/parser/Attributes.tree +++ /dev/null @@ -1,293 +0,0 @@ -{ - "location": "5:1-17:12", - "packages": [], - "directives": [ - { - "VariableDefinition": { - "location": "1:1-1:10", - "asdoc": null, - "attributes": [ - { - "Expression": { - "Member": { - "location": "1:1-1:4", - "base": { - "QualifiedIdentifier": { - "location": "1:1-1:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "q", - "1:1-1:2" - ] - } - } - }, - "identifier": { - "location": "1:3-1:4", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "x", - "1:3-1:4" - ] - } - } - } - } - } - ], - "kind": [ - "Var", - "1:5-1:8" - ], - "bindings": [ - { - "destructuring": { - "location": "1:9-1:10", - "destructuring": { - "QualifiedIdentifier": { - "location": "1:9-1:10", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "y", - "1:9-1:10" - ] - } - } - }, - "type_annotation": null - }, - "initializer": null - } - ] - } - }, - { - "VariableDefinition": { - "location": "3:1-3:20", - "asdoc": null, - "attributes": [ - { - "Metadata": { - "location": "3:2-3:4", - "asdoc": null, - "name": [ - "N1", - "3:2-3:4" - ], - "entries": null - } - }, - { - "Internal": "3:6-3:14" - } - ], - "kind": [ - "Var", - "3:15-3:18" - ], - "bindings": [ - { - "destructuring": { - "location": "3:19-3:20", - "destructuring": { - "QualifiedIdentifier": { - "location": "3:19-3:20", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "z", - "3:19-3:20" - ] - } - } - }, - "type_annotation": null - }, - "initializer": null - } - ] - } - }, - { - "Block": { - "location": "5:15-5:17", - "metadata": [ - { - "Metadata": { - "location": "5:2-5:13", - "asdoc": null, - "name": [ - "N1", - "5:2-5:4" - ], - "entries": [ - { - "location": "5:5-5:12", - "key": [ - "x", - "5:5-5:6" - ], - "value": { - "String": [ - "y", - "5:9-5:12" - ] - } - } - ] - } - } - ], - "directives": [] - } - }, - { - "ClassDefinition": { - "location": "7:1-7:26", - "asdoc": null, - "attributes": [ - { - "Dynamic": "7:1-7:8" - }, - { - "Final": "7:9-7:14" - } - ], - "name": [ - "C1", - "7:21-7:23" - ], - "type_parameters": null, - "extends_clause": null, - "implements_clause": null, - "block": { - "location": "7:24-7:26", - "metadata": null, - "directives": [] - } - } - }, - { - "ClassDefinition": { - "location": "12:1-17:12", - "asdoc": { - "location": "9:1-11:4", - "main_body": [ - "Foo", - "10:4-10:7" - ], - "tags": [] - }, - "attributes": [ - { - "Metadata": { - "location": "12:2-12:4", - "asdoc": null, - "name": [ - "N1", - "12:2-12:4" - ], - "entries": null - } - }, - { - "Metadata": { - "location": "16:2-16:40", - "asdoc": { - "location": "13:1-15:4", - "main_body": null, - "tags": [ - [ - { - "EventType": { - "Member": { - "location": "14:15-14:27", - "base": { - "QualifiedIdentifier": { - "location": "14:15-14:23", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "FooEvent", - "14:15-14:23" - ] - } - } - }, - "identifier": { - "location": "14:24-14:27", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "FOO", - "14:24-14:27" - ] - } - } - } - } - }, - "14:4-14:27" - ] - ] - }, - "name": [ - "Event", - "16:2-16:7" - ], - "entries": [ - { - "location": "16:8-16:20", - "key": [ - "name", - "16:8-16:12" - ], - "value": { - "String": [ - "foo", - "16:15-16:20" - ] - } - }, - { - "location": "16:22-16:39", - "key": [ - "type", - "16:22-16:26" - ], - "value": { - "String": [ - "FooEvent", - "16:29-16:39" - ] - } - } - ] - } - } - ], - "name": [ - "C1", - "17:7-17:9" - ], - "type_parameters": null, - "extends_clause": null, - "implements_clause": null, - "block": { - "location": "17:10-17:12", - "metadata": null, - "directives": [] - } - } - } - ] -} \ No newline at end of file diff --git a/tests/parser/Configuration.as b/tests/parser/Configuration.as deleted file mode 100644 index 38d8419..0000000 --- a/tests/parser/Configuration.as +++ /dev/null @@ -1,21 +0,0 @@ -package foo.bar { - /** - * Comment 1 (overriden by the "Communication facility" comment). - */ - CONFIG::DEBUG - /** - * Communication facility. - */ - [Adherence(type = "efficient")] - /** - * Dispatched when a message is received. - */ - [Event(name = "received", type = "foo.bar.MessageEvent")] - public class CommunicationCenter extends EventDispatcher { - CONFIG::DEBUG { - protected var x: T1, y: T2, z: T3; - protected function f1(): void {} - } - CONFIG::RELEASE protected var w: T4; - } -} \ No newline at end of file diff --git a/tests/parser/Configuration.diag b/tests/parser/Configuration.diag deleted file mode 100644 index e69de29..0000000 diff --git a/tests/parser/Configuration.tree b/tests/parser/Configuration.tree deleted file mode 100644 index f9cdbf8..0000000 --- a/tests/parser/Configuration.tree +++ /dev/null @@ -1,386 +0,0 @@ -{ - "location": "5:5-21:2", - "packages": [ - { - "location": "5:5-21:2", - "asdoc": null, - "name": [ - [ - "foo", - "1:9-1:12" - ], - [ - "bar", - "1:13-1:16" - ] - ], - "block": { - "location": "5:5-21:2", - "metadata": null, - "directives": [ - { - "NormalConfigurationDirective": { - "location": "14:62-20:6", - "namespace": [ - "CONFIG", - "5:5-5:11" - ], - "constant_name": [ - "DEBUG", - "5:13-5:18" - ], - "directive": { - "ClassDefinition": { - "location": "15:9-20:6", - "asdoc": { - "location": "6:5-8:8", - "main_body": [ - "Communication facility.", - "7:8-7:31" - ], - "tags": [] - }, - "attributes": [ - { - "Metadata": { - "location": "9:6-9:35", - "asdoc": null, - "name": [ - "Adherence", - "9:6-9:15" - ], - "entries": [ - { - "location": "9:16-9:34", - "key": [ - "type", - "9:16-9:20" - ], - "value": { - "String": [ - "efficient", - "9:23-9:34" - ] - } - } - ] - } - }, - { - "Metadata": { - "location": "13:6-13:61", - "asdoc": { - "location": "10:5-12:8", - "main_body": [ - "Dispatched when a message is received.", - "11:8-11:46" - ], - "tags": [] - }, - "name": [ - "Event", - "13:6-13:11" - ], - "entries": [ - { - "location": "13:12-13:29", - "key": [ - "name", - "13:12-13:16" - ], - "value": { - "String": [ - "received", - "13:19-13:29" - ] - } - }, - { - "location": "13:31-13:60", - "key": [ - "type", - "13:31-13:35" - ], - "value": { - "String": [ - "foo.bar.MessageEvent", - "13:38-13:60" - ] - } - } - ] - } - }, - { - "Public": "14:5-14:11" - } - ], - "name": [ - "CommunicationCenter", - "14:18-14:37" - ], - "type_parameters": null, - "extends_clause": { - "QualifiedIdentifier": { - "location": "14:46-14:61", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "EventDispatcher", - "14:46-14:61" - ] - } - } - }, - "implements_clause": null, - "block": { - "location": "19:9-20:6", - "metadata": null, - "directives": [ - { - "NormalConfigurationDirective": { - "location": "15:9-18:10", - "namespace": [ - "CONFIG", - "15:9-15:15" - ], - "constant_name": [ - "DEBUG", - "15:17-15:22" - ], - "directive": { - "Block": { - "location": "15:23-18:10", - "metadata": null, - "directives": [ - { - "VariableDefinition": { - "location": "16:23-16:47", - "asdoc": null, - "attributes": [ - { - "Protected": "16:13-16:22" - } - ], - "kind": [ - "Var", - "16:23-16:26" - ], - "bindings": [ - { - "destructuring": { - "location": "16:27-16:32", - "destructuring": { - "QualifiedIdentifier": { - "location": "16:27-16:28", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "x", - "16:27-16:28" - ] - } - } - }, - "type_annotation": { - "QualifiedIdentifier": { - "location": "16:30-16:32", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "T1", - "16:30-16:32" - ] - } - } - } - }, - "initializer": null - }, - { - "destructuring": { - "location": "16:34-16:39", - "destructuring": { - "QualifiedIdentifier": { - "location": "16:34-16:35", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "y", - "16:34-16:35" - ] - } - } - }, - "type_annotation": { - "QualifiedIdentifier": { - "location": "16:37-16:39", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "T2", - "16:37-16:39" - ] - } - } - } - }, - "initializer": null - }, - { - "destructuring": { - "location": "16:41-16:46", - "destructuring": { - "QualifiedIdentifier": { - "location": "16:41-16:42", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "z", - "16:41-16:42" - ] - } - } - }, - "type_annotation": { - "QualifiedIdentifier": { - "location": "16:44-16:46", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "T3", - "16:44-16:46" - ] - } - } - } - }, - "initializer": null - } - ] - } - }, - { - "FunctionDefinition": { - "location": "17:23-17:45", - "asdoc": null, - "attributes": [ - { - "Protected": "17:13-17:22" - } - ], - "name": { - "Identifier": [ - "f1", - "17:32-17:34" - ] - }, - "common": { - "location": "17:34-17:45", - "contains_yield": false, - "contains_await": false, - "signature": { - "location": "17:34-17:42", - "parameters": [], - "result_type": { - "VoidType": { - "location": "17:38-17:42" - } - } - }, - "body": { - "Block": { - "location": "17:43-17:45", - "metadata": null, - "directives": [] - } - } - } - } - } - ] - } - } - } - }, - { - "NormalConfigurationDirective": { - "location": "19:9-19:45", - "namespace": [ - "CONFIG", - "19:9-19:15" - ], - "constant_name": [ - "RELEASE", - "19:17-19:24" - ], - "directive": { - "VariableDefinition": { - "location": "19:9-19:45", - "asdoc": null, - "attributes": [ - { - "Protected": "19:25-19:34" - } - ], - "kind": [ - "Var", - "19:35-19:38" - ], - "bindings": [ - { - "destructuring": { - "location": "19:39-19:44", - "destructuring": { - "QualifiedIdentifier": { - "location": "19:39-19:40", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "w", - "19:39-19:40" - ] - } - } - }, - "type_annotation": { - "QualifiedIdentifier": { - "location": "19:42-19:44", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "T4", - "19:42-19:44" - ] - } - } - } - }, - "initializer": null - } - ] - } - } - } - } - ] - } - } - } - } - } - ] - } - } - ], - "directives": [] -} \ No newline at end of file diff --git a/tests/parser/E4X.as b/tests/parser/E4X.as deleted file mode 100644 index fb18845..0000000 --- a/tests/parser/E4X.as +++ /dev/null @@ -1,18 +0,0 @@ -default xml namespace = myxmlns; - - ; - ; - ; - - - - - - Lorem ipsum - Lorem ipsum - {c} - - .(filterCondition) - ..d; - -<>; \ No newline at end of file diff --git a/tests/parser/E4X.diag b/tests/parser/E4X.diag deleted file mode 100644 index e69de29..0000000 diff --git a/tests/parser/E4X.tree b/tests/parser/E4X.tree deleted file mode 100644 index 44f7a88..0000000 --- a/tests/parser/E4X.tree +++ /dev/null @@ -1,324 +0,0 @@ -{ - "location": "1:1-18:7", - "packages": [], - "directives": [ - { - "DefaultXmlNamespaceStatement": { - "location": "1:1-1:33", - "right": { - "QualifiedIdentifier": { - "location": "1:25-1:32", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "myxmlns", - "1:25-1:32" - ] - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "3:1-3:19", - "expression": { - "XmlMarkup": { - "location": "3:1-3:17", - "markup": "" - } - } - } - }, - { - "ExpressionStatement": { - "location": "4:1-4:8", - "expression": { - "XmlMarkup": { - "location": "4:1-4:6", - "markup": "" - } - } - } - }, - { - "ExpressionStatement": { - "location": "5:1-5:27", - "expression": { - "XmlMarkup": { - "location": "5:1-5:25", - "markup": "" - } - } - } - }, - { - "ExpressionStatement": { - "location": "6:1-16:9", - "expression": { - "Descendants": { - "location": "6:1-16:8", - "base": { - "Filter": { - "location": "6:1-15:23", - "base": { - "Xml": { - "location": "6:1-14:5", - "element": { - "location": "6:1-14:5", - "name": { - "Name": [ - "t", - "6:2-6:3" - ] - }, - "attributes": [], - "attribute_expression": null, - "content": [ - { - "Characters": [ - "\r\n ", - "6:4-7:5" - ] - }, - { - "Element": { - "location": "7:6-7:9", - "name": { - "Name": [ - "t", - "7:6-7:7" - ] - }, - "attributes": [], - "attribute_expression": null, - "content": null, - "closing_name": null - } - }, - { - "Characters": [ - "\r\n ", - "7:9-8:5" - ] - }, - { - "Element": { - "location": "8:6-8:13", - "name": { - "Name": [ - "t", - "8:6-8:7" - ] - }, - "attributes": [], - "attribute_expression": { - "QualifiedIdentifier": { - "location": "8:9-8:10", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "c", - "8:9-8:10" - ] - } - } - }, - "content": null, - "closing_name": null - } - }, - { - "Characters": [ - "\r\n ", - "8:13-9:5" - ] - }, - { - "Element": { - "location": "9:6-9:15", - "name": { - "Name": [ - "t", - "9:6-9:7" - ] - }, - "attributes": [ - { - "location": "9:8-9:13", - "name": [ - "k", - "9:8-9:9" - ], - "value": { - "Value": [ - "v", - "9:10-9:13" - ] - } - } - ], - "attribute_expression": null, - "content": null, - "closing_name": null - } - }, - { - "Characters": [ - "\r\n ", - "9:15-10:5" - ] - }, - { - "Element": { - "location": "10:6-10:15", - "name": { - "Name": [ - "t", - "10:6-10:7" - ] - }, - "attributes": [ - { - "location": "10:8-10:13", - "name": [ - "k", - "10:8-10:9" - ], - "value": { - "Expression": { - "QualifiedIdentifier": { - "location": "10:11-10:12", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "v", - "10:11-10:12" - ] - } - } - } - } - } - ], - "attribute_expression": null, - "content": null, - "closing_name": null - } - }, - { - "Characters": [ - "\r\n Lorem ipsum\r\n ", - "10:15-12:5" - ] - }, - { - "Element": { - "location": "12:6-12:23", - "name": { - "Name": [ - "t", - "12:6-12:7" - ] - }, - "attributes": [], - "attribute_expression": null, - "content": [ - { - "Characters": [ - "Lorem ipsum", - "12:8-12:19" - ] - } - ], - "closing_name": { - "Name": [ - "t", - "12:21-12:22" - ] - } - } - }, - { - "Characters": [ - "\r\n ", - "12:23-13:5" - ] - }, - { - "Expression": { - "QualifiedIdentifier": { - "location": "13:6-13:7", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "c", - "13:6-13:7" - ] - } - } - } - }, - { - "Characters": [ - "\r\n", - "13:8-14:1" - ] - } - ], - "closing_name": { - "Name": [ - "t", - "14:3-14:4" - ] - } - } - } - }, - "test": { - "QualifiedIdentifier": { - "location": "15:7-15:22", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "filterCondition", - "15:7-15:22" - ] - } - } - } - } - }, - "identifier": { - "location": "16:7-16:8", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "d", - "16:7-16:8" - ] - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "18:1-18:7", - "expression": { - "XmlList": { - "location": "18:1-18:6", - "content": [] - } - } - } - } - ] -} \ No newline at end of file diff --git a/tests/parser/MXML1.diag b/tests/parser/MXML1.diag deleted file mode 100644 index e69de29..0000000 diff --git a/tests/parser/MXML1.mxml b/tests/parser/MXML1.mxml deleted file mode 100644 index 5d922ab..0000000 --- a/tests/parser/MXML1.mxml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/tests/parser/MXML1.tree b/tests/parser/MXML1.tree deleted file mode 100644 index d8eebd2..0000000 --- a/tests/parser/MXML1.tree +++ /dev/null @@ -1,215 +0,0 @@ -{ - "location": "1:1-16:17", - "version": "Version10", - "encoding": "utf-8", - "content": [ - { - "ProcessingInstruction": { - "location": "1:1-1:22", - "name": "xml", - "data": " version=\"1.0\"" - } - }, - { - "Comment": [ - "", - "2:1-2:37" - ] - }, - { - "Element": { - "location": "3:2-16:17", - "name": { - "location": "3:2-3:15", - "prefix": "s", - "name": "Application" - }, - "attributes": [ - { - "location": "3:16-3:56", - "xmlns": true, - "name": { - "location": "3:16-3:24", - "prefix": "xmlns", - "name": "fx" - }, - "value": [ - "http://ns.adobe.com/mxml/2009", - "3:25-3:56" - ] - }, - { - "location": "4:5-4:48", - "xmlns": true, - "name": { - "location": "4:5-4:12", - "prefix": "xmlns", - "name": "s" - }, - "value": [ - "library://ns.adobe.com/flex/spark", - "4:13-4:48" - ] - }, - { - "location": "5:5-5:46", - "xmlns": true, - "name": { - "location": "5:5-5:13", - "prefix": "xmlns", - "name": "mx" - }, - "value": [ - "library://ns.adobe.com/flex/mx", - "5:14-5:46" - ] - } - ], - "content": [ - { - "Element": { - "location": "6:6-8:16", - "name": { - "location": "6:6-6:14", - "prefix": "s", - "name": "layout" - }, - "attributes": [], - "content": [ - { - "Element": { - "location": "7:10-7:28", - "name": { - "location": "7:10-7:26", - "prefix": "s", - "name": "VerticalLayout" - }, - "attributes": [], - "content": null, - "closing_name": null - } - } - ], - "closing_name": { - "location": "8:7-8:15", - "prefix": "s", - "name": "layout" - } - } - }, - { - "Element": { - "location": "9:6-14:65", - "name": { - "location": "9:6-9:14", - "prefix": "s", - "name": "Button" - }, - "attributes": [ - { - "location": "10:9-10:21", - "xmlns": false, - "name": { - "location": "10:9-10:11", - "prefix": null, - "name": "id" - }, - "value": [ - "button1", - "10:12-10:21" - ] - }, - { - "location": "11:9-11:28", - "xmlns": false, - "name": { - "location": "11:9-11:14", - "prefix": null, - "name": "label" - }, - "value": [ - "Click here!", - "11:15-11:28" - ] - }, - { - "location": "12:9-12:20", - "xmlns": false, - "name": { - "location": "12:9-12:14", - "prefix": null, - "name": "width" - }, - "value": [ - "100", - "12:15-12:20" - ] - }, - { - "location": "13:9-13:22", - "xmlns": false, - "name": { - "location": "13:9-13:17", - "prefix": null, - "name": "fontSize" - }, - "value": [ - "12", - "13:18-13:22" - ] - }, - { - "location": "14:9-14:63", - "xmlns": false, - "name": { - "location": "14:9-14:14", - "prefix": null, - "name": "click" - }, - "value": [ - "text1.text='&& Thanks for the click!';", - "14:15-14:63" - ] - } - ], - "content": null, - "closing_name": null - } - }, - { - "Element": { - "location": "15:6-15:29", - "name": { - "location": "15:6-15:16", - "prefix": "s", - "name": "TextArea" - }, - "attributes": [ - { - "location": "15:17-15:27", - "xmlns": false, - "name": { - "location": "15:17-15:19", - "prefix": null, - "name": "id" - }, - "value": [ - "text1", - "15:20-15:27" - ] - } - ], - "content": null, - "closing_name": null - } - } - ], - "closing_name": { - "location": "16:3-16:16", - "prefix": "s", - "name": "Application" - } - } - } - ] -} \ No newline at end of file diff --git a/tests/parser/MXML2.diag b/tests/parser/MXML2.diag deleted file mode 100644 index e69de29..0000000 diff --git a/tests/parser/MXML2.mxml b/tests/parser/MXML2.mxml deleted file mode 100644 index 5c28b9b..0000000 --- a/tests/parser/MXML2.mxml +++ /dev/null @@ -1,4 +0,0 @@ - - - & Text & - \ No newline at end of file diff --git a/tests/parser/MXML2.tree b/tests/parser/MXML2.tree deleted file mode 100644 index 5eb2621..0000000 --- a/tests/parser/MXML2.tree +++ /dev/null @@ -1,38 +0,0 @@ -{ - "location": "1:1-4:11", - "version": "Version10", - "encoding": "utf-8", - "content": [ - { - "ProcessingInstruction": { - "location": "1:1-1:22", - "name": "xml", - "data": " version=\"1.0\"" - } - }, - { - "Element": { - "location": "2:2-4:11", - "name": { - "location": "2:2-2:9", - "prefix": null, - "name": "invalid" - }, - "attributes": [], - "content": [ - { - "Characters": [ - "\r\n & Text &\r\n", - "2:10-4:1" - ] - } - ], - "closing_name": { - "location": "4:3-4:10", - "prefix": null, - "name": "invalid" - } - } - } - ] -} \ No newline at end of file diff --git a/tests/parser/Misc.as b/tests/parser/Misc.as deleted file mode 100644 index ba1c996..0000000 --- a/tests/parser/Misc.as +++ /dev/null @@ -1,19 +0,0 @@ -namespace ns1 -namespace ns2 = "http://www.samsung.com" - -new [] - -v is T -v is not T -v instanceof T -k in o -k not in o - -x ? y : z - -10 ; -10f ; - -/(?:)/gi ; - -/=/ \ No newline at end of file diff --git a/tests/parser/Misc.diag b/tests/parser/Misc.diag deleted file mode 100644 index e69de29..0000000 diff --git a/tests/parser/Misc.tree b/tests/parser/Misc.tree deleted file mode 100644 index 893b52e..0000000 --- a/tests/parser/Misc.tree +++ /dev/null @@ -1,341 +0,0 @@ -{ - "location": "10:1-19:4", - "packages": [], - "directives": [ - { - "NamespaceDefinition": { - "location": "1:1-1:14", - "asdoc": null, - "attributes": [], - "left": [ - "ns1", - "1:11-1:14" - ], - "right": null - } - }, - { - "NamespaceDefinition": { - "location": "2:1-2:41", - "asdoc": null, - "attributes": [], - "left": [ - "ns2", - "2:11-2:14" - ], - "right": { - "StringLiteral": { - "location": "2:17-2:41", - "value": "http://www.samsung.com" - } - } - } - }, - { - "ExpressionStatement": { - "location": "4:1-4:11", - "expression": { - "VectorLiteral": { - "location": "4:1-4:11", - "element_type": { - "QualifiedIdentifier": { - "location": "4:6-4:7", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "T", - "4:6-4:7" - ] - } - } - }, - "elements": [] - } - } - } - }, - { - "ExpressionStatement": { - "location": "6:1-6:7", - "expression": { - "Binary": { - "location": "6:1-6:7", - "operator": "Is", - "left": { - "QualifiedIdentifier": { - "location": "6:1-6:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "v", - "6:1-6:2" - ] - } - } - }, - "right": { - "QualifiedIdentifier": { - "location": "6:6-6:7", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "T", - "6:6-6:7" - ] - } - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "7:1-7:11", - "expression": { - "Binary": { - "location": "7:1-7:11", - "operator": "IsNot", - "left": { - "QualifiedIdentifier": { - "location": "7:1-7:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "v", - "7:1-7:2" - ] - } - } - }, - "right": { - "QualifiedIdentifier": { - "location": "7:10-7:11", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "T", - "7:10-7:11" - ] - } - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "8:1-8:15", - "expression": { - "Binary": { - "location": "8:1-8:15", - "operator": "Instanceof", - "left": { - "QualifiedIdentifier": { - "location": "8:1-8:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "v", - "8:1-8:2" - ] - } - } - }, - "right": { - "QualifiedIdentifier": { - "location": "8:14-8:15", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "T", - "8:14-8:15" - ] - } - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "9:1-9:7", - "expression": { - "Binary": { - "location": "9:1-9:7", - "operator": "In", - "left": { - "QualifiedIdentifier": { - "location": "9:1-9:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "k", - "9:1-9:2" - ] - } - } - }, - "right": { - "QualifiedIdentifier": { - "location": "9:6-9:7", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "o", - "9:6-9:7" - ] - } - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "10:1-10:11", - "expression": { - "Binary": { - "location": "10:1-10:11", - "operator": "NotIn", - "left": { - "QualifiedIdentifier": { - "location": "10:1-10:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "k", - "10:1-10:2" - ] - } - } - }, - "right": { - "QualifiedIdentifier": { - "location": "10:10-10:11", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "o", - "10:10-10:11" - ] - } - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "12:1-12:10", - "expression": { - "Conditional": { - "location": "12:1-12:10", - "test": { - "QualifiedIdentifier": { - "location": "12:1-12:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "x", - "12:1-12:2" - ] - } - } - }, - "consequent": { - "QualifiedIdentifier": { - "location": "12:5-12:6", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "y", - "12:5-12:6" - ] - } - } - }, - "alternative": { - "QualifiedIdentifier": { - "location": "12:9-12:10", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "z", - "12:9-12:10" - ] - } - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "14:1-14:6", - "expression": { - "NumericLiteral": { - "location": "14:1-14:3", - "value": "10", - "suffix": "None" - } - } - } - }, - { - "ExpressionStatement": { - "location": "15:1-15:7", - "expression": { - "NumericLiteral": { - "location": "15:1-15:4", - "value": "10", - "suffix": "F" - } - } - } - }, - { - "ExpressionStatement": { - "location": "17:1-17:12", - "expression": { - "RegExpLiteral": { - "location": "17:1-17:9", - "body": "(?:)", - "flags": "gi" - } - } - } - }, - { - "ExpressionStatement": { - "location": "19:1-19:4", - "expression": { - "RegExpLiteral": { - "location": "19:1-19:4", - "body": "=", - "flags": "" - } - } - } - } - ] -} \ No newline at end of file diff --git a/tests/parser/Qualifier.as b/tests/parser/Qualifier.as deleted file mode 100644 index 837c5ba..0000000 --- a/tests/parser/Qualifier.as +++ /dev/null @@ -1,9 +0,0 @@ -* -; (q)::x -o.* -o.public -o.public::x -o.(q)::x -o.q::x -o.q::[k] -o.@x; \ No newline at end of file diff --git a/tests/parser/Qualifier.diag b/tests/parser/Qualifier.diag deleted file mode 100644 index e69de29..0000000 diff --git a/tests/parser/Qualifier.tree b/tests/parser/Qualifier.tree deleted file mode 100644 index d38b679..0000000 --- a/tests/parser/Qualifier.tree +++ /dev/null @@ -1,351 +0,0 @@ -{ - "location": "1:1-9:6", - "packages": [], - "directives": [ - { - "ExpressionStatement": { - "location": "1:1-2:2", - "expression": { - "QualifiedIdentifier": { - "location": "1:1-1:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "*", - "1:1-1:2" - ] - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "2:3-2:9", - "expression": { - "QualifiedIdentifier": { - "location": "2:3-2:9", - "attribute": false, - "qualifier": { - "Paren": { - "location": "2:3-2:6", - "expression": { - "QualifiedIdentifier": { - "location": "2:4-2:5", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "q", - "2:4-2:5" - ] - } - } - } - } - }, - "id": { - "Id": [ - "x", - "2:8-2:9" - ] - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "3:1-3:4", - "expression": { - "Member": { - "location": "3:1-3:4", - "base": { - "QualifiedIdentifier": { - "location": "3:1-3:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "o", - "3:1-3:2" - ] - } - } - }, - "identifier": { - "location": "3:3-3:4", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "*", - "3:3-3:4" - ] - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "4:1-4:9", - "expression": { - "Member": { - "location": "4:1-4:9", - "base": { - "QualifiedIdentifier": { - "location": "4:1-4:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "o", - "4:1-4:2" - ] - } - } - }, - "identifier": { - "location": "4:3-4:9", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "public", - "4:3-4:9" - ] - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "5:1-5:12", - "expression": { - "Member": { - "location": "5:1-5:12", - "base": { - "QualifiedIdentifier": { - "location": "5:1-5:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "o", - "5:1-5:2" - ] - } - } - }, - "identifier": { - "location": "5:3-5:12", - "attribute": false, - "qualifier": { - "ReservedNamespace": { - "Public": "5:3-5:9" - } - }, - "id": { - "Id": [ - "x", - "5:11-5:12" - ] - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "6:1-6:9", - "expression": { - "Member": { - "location": "6:1-6:9", - "base": { - "QualifiedIdentifier": { - "location": "6:1-6:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "o", - "6:1-6:2" - ] - } - } - }, - "identifier": { - "location": "6:3-6:9", - "attribute": false, - "qualifier": { - "Paren": { - "location": "6:3-6:4", - "expression": { - "QualifiedIdentifier": { - "location": "6:4-6:5", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "q", - "6:4-6:5" - ] - } - } - } - } - }, - "id": { - "Id": [ - "x", - "6:8-6:9" - ] - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "7:1-7:7", - "expression": { - "Member": { - "location": "7:1-7:7", - "base": { - "QualifiedIdentifier": { - "location": "7:1-7:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "o", - "7:1-7:2" - ] - } - } - }, - "identifier": { - "location": "7:3-7:7", - "attribute": false, - "qualifier": { - "QualifiedIdentifier": { - "location": "7:3-7:4", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "q", - "7:3-7:4" - ] - } - } - }, - "id": { - "Id": [ - "x", - "7:6-7:7" - ] - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "8:1-8:9", - "expression": { - "Member": { - "location": "8:1-8:9", - "base": { - "QualifiedIdentifier": { - "location": "8:1-8:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "o", - "8:1-8:2" - ] - } - } - }, - "identifier": { - "location": "8:3-8:9", - "attribute": false, - "qualifier": { - "QualifiedIdentifier": { - "location": "8:3-8:4", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "q", - "8:3-8:4" - ] - } - } - }, - "id": { - "Brackets": { - "QualifiedIdentifier": { - "location": "8:7-8:8", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "k", - "8:7-8:8" - ] - } - } - } - } - } - } - } - } - }, - { - "ExpressionStatement": { - "location": "9:1-9:6", - "expression": { - "Member": { - "location": "9:1-9:5", - "base": { - "QualifiedIdentifier": { - "location": "9:1-9:2", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "o", - "9:1-9:2" - ] - } - } - }, - "identifier": { - "location": "9:3-9:5", - "attribute": true, - "qualifier": null, - "id": { - "Id": [ - "x", - "9:4-9:5" - ] - } - } - } - } - } - } - ] -} \ No newline at end of file diff --git a/tests/parser/README.md b/tests/parser/README.md deleted file mode 100644 index b09ad3d..0000000 --- a/tests/parser/README.md +++ /dev/null @@ -1,17 +0,0 @@ -# Parsing tests - -To test parsing a program producing output to the command line, run: - -``` -cargo run --bin as3_parser_test -- --source-path tests/parser/Demo.as -``` - -To test parsing a program producing output to two files `.ast.json` and `.diag`, run: - -``` -cargo run --bin as3_parser_test -- --source-path tests/parser/Demo.as --file-log -``` - -For parsing MXML, pass the `--mxml` flag. - -For parsing CSS, pass the `--css` flag. \ No newline at end of file diff --git a/tests/parser/Super.as b/tests/parser/Super.as deleted file mode 100644 index 817fde3..0000000 --- a/tests/parser/Super.as +++ /dev/null @@ -1,12 +0,0 @@ -super() // ERROR -class C2 extends C1 { - function C2() { - if (true) { - super() - } - } - override protected function m(): void { - super.m() - super(this).m() - } -} \ No newline at end of file diff --git a/tests/parser/Super.diag b/tests/parser/Super.diag deleted file mode 100644 index 1eac2e5..0000000 --- a/tests/parser/Super.diag +++ /dev/null @@ -1 +0,0 @@ -C:\Users\mathe\UnsyncDocuments\as3parser\tests\parser\Super.as:1:1: Syntax error #1036: 'super' not allowed here \ No newline at end of file diff --git a/tests/parser/Super.tree b/tests/parser/Super.tree deleted file mode 100644 index 24fa1b4..0000000 --- a/tests/parser/Super.tree +++ /dev/null @@ -1,215 +0,0 @@ -{ - "location": "1:1-12:2", - "packages": [], - "directives": [ - { - "SuperStatement": { - "location": "1:1-1:8", - "arguments": [] - } - }, - { - "ClassDefinition": { - "location": "2:1-12:2", - "asdoc": null, - "attributes": [], - "name": [ - "C2", - "2:7-2:9" - ], - "type_parameters": null, - "extends_clause": { - "QualifiedIdentifier": { - "location": "2:18-2:20", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "C1", - "2:18-2:20" - ] - } - } - }, - "implements_clause": null, - "block": { - "location": "2:21-12:2", - "metadata": null, - "directives": [ - { - "FunctionDefinition": { - "location": "3:5-7:6", - "asdoc": null, - "attributes": [], - "name": { - "Constructor": [ - "C2", - "3:14-3:16" - ] - }, - "common": { - "location": "3:16-7:6", - "contains_yield": false, - "contains_await": false, - "signature": { - "location": "3:16-3:18", - "parameters": [], - "result_type": null - }, - "body": { - "Block": { - "location": "3:19-7:6", - "metadata": null, - "directives": [ - { - "IfStatement": { - "location": "4:9-6:10", - "test": { - "BooleanLiteral": { - "location": "4:13-4:17", - "value": true - } - }, - "consequent": { - "Block": { - "location": "4:19-6:10", - "metadata": null, - "directives": [ - { - "SuperStatement": { - "location": "5:13-5:20", - "arguments": [] - } - } - ] - } - }, - "alternative": null - } - } - ] - } - } - } - } - }, - { - "FunctionDefinition": { - "location": "8:5-11:6", - "asdoc": null, - "attributes": [ - { - "Override": "8:5-8:13" - }, - { - "Protected": "8:14-8:23" - } - ], - "name": { - "Identifier": [ - "m", - "8:33-8:34" - ] - }, - "common": { - "location": "8:34-11:6", - "contains_yield": false, - "contains_await": false, - "signature": { - "location": "8:34-8:42", - "parameters": [], - "result_type": { - "VoidType": { - "location": "8:38-8:42" - } - } - }, - "body": { - "Block": { - "location": "8:43-11:6", - "metadata": null, - "directives": [ - { - "ExpressionStatement": { - "location": "9:9-9:18", - "expression": { - "Call": { - "location": "9:9-9:18", - "base": { - "Member": { - "location": "9:9-9:16", - "base": { - "Super": { - "location": "9:9-9:14", - "object": null - } - }, - "identifier": { - "location": "9:15-9:16", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "m", - "9:15-9:16" - ] - } - } - } - }, - "arguments": [] - } - } - } - }, - { - "ExpressionStatement": { - "location": "10:9-10:24", - "expression": { - "Call": { - "location": "10:9-10:24", - "base": { - "Member": { - "location": "10:9-10:22", - "base": { - "Super": { - "location": "10:9-10:20", - "object": [ - { - "ThisLiteral": { - "location": "10:15-10:19" - } - } - ] - } - }, - "identifier": { - "location": "10:21-10:22", - "attribute": false, - "qualifier": null, - "id": { - "Id": [ - "m", - "10:21-10:22" - ] - } - } - } - }, - "arguments": [] - } - } - } - } - ] - } - } - } - } - } - ] - } - } - } - ] -} \ No newline at end of file diff --git a/tests/parser/css/mediaQuery.css b/tests/parser/css/mediaQuery.css deleted file mode 100644 index 5e64391..0000000 --- a/tests/parser/css/mediaQuery.css +++ /dev/null @@ -1,5 +0,0 @@ -@media (application-dpi: 240) and (os-platform: "Windows"),,,,,,,,, { - .myStyleName1 { - fontSize: 30 - } -} \ No newline at end of file diff --git a/tests/parser/css/mediaQuery.diag b/tests/parser/css/mediaQuery.diag deleted file mode 100644 index e69de29..0000000 diff --git a/tests/parser/css/mediaQuery.tree b/tests/parser/css/mediaQuery.tree deleted file mode 100644 index 209ae54..0000000 --- a/tests/parser/css/mediaQuery.tree +++ /dev/null @@ -1,92 +0,0 @@ -{ - "location": "1:1-5:2", - "directives": [ - { - "MediaQuery": { - "location": "1:1-5:2", - "conditions": [ - { - "And": { - "location": "1:8-1:59", - "left": { - "ParenProperty": [ - { - "location": "1:9-1:29", - "name": [ - "applicationDpi", - "1:9-1:24" - ], - "value": { - "Number": { - "location": "1:26-1:29", - "value": 240.0, - "unit": null - } - } - }, - "1:8-1:30" - ] - }, - "right": { - "ParenProperty": [ - { - "location": "1:36-1:58", - "name": [ - "osPlatform", - "1:36-1:47" - ], - "value": { - "String": { - "location": "1:49-1:58", - "value": "Windows" - } - } - }, - "1:35-1:59" - ] - } - } - } - ], - "rules": [ - { - "location": "2:5-4:6", - "selectors": [ - { - "Base": { - "location": "2:5-2:18", - "namespace_prefix": null, - "element_name": null, - "conditions": [ - { - "Class": [ - "myStyleName1", - "2:5-2:18" - ] - } - ] - } - } - ], - "properties": [ - { - "location": "3:9-3:21", - "name": [ - "fontSize", - "3:9-3:17" - ], - "value": { - "Number": { - "location": "3:19-3:21", - "value": 30.0, - "unit": null - } - } - } - ] - } - ] - } - } - ] -} \ No newline at end of file diff --git a/tests/parser/css/selectors.css b/tests/parser/css/selectors.css deleted file mode 100644 index fb64f56..0000000 --- a/tests/parser/css/selectors.css +++ /dev/null @@ -1,9 +0,0 @@ -.myStyleName1.anotherOne s|VGroup, #id1 { - fontSize: 24 -} - -.myStyleName1[attr^="."] { - color: red -} - -#myId1 #myId2 #myId3 {} \ No newline at end of file diff --git a/tests/parser/css/selectors.diag b/tests/parser/css/selectors.diag deleted file mode 100644 index e69de29..0000000 diff --git a/tests/parser/css/selectors.tree b/tests/parser/css/selectors.tree deleted file mode 100644 index 143402b..0000000 --- a/tests/parser/css/selectors.tree +++ /dev/null @@ -1,200 +0,0 @@ -{ - "location": "1:1-9:24", - "directives": [ - { - "Rule": { - "location": "1:1-3:2", - "selectors": [ - { - "Combinator": { - "location": "1:1-1:34", - "left": { - "Base": { - "location": "1:1-1:25", - "namespace_prefix": null, - "element_name": null, - "conditions": [ - { - "Class": [ - "myStyleName1", - "1:1-1:14" - ] - }, - { - "Class": [ - "anotherOne", - "1:14-1:25" - ] - } - ] - } - }, - "right": { - "Base": { - "location": "1:26-1:34", - "namespace_prefix": [ - "s", - "1:26-1:27" - ], - "element_name": [ - "VGroup", - "1:28-1:34" - ], - "conditions": [] - } - }, - "combinator_type": "Descendant" - } - }, - { - "Base": { - "location": "1:36-1:40", - "namespace_prefix": null, - "element_name": null, - "conditions": [ - { - "Id": [ - "id1", - "1:36-1:40" - ] - } - ] - } - } - ], - "properties": [ - { - "location": "2:5-2:17", - "name": [ - "fontSize", - "2:5-2:13" - ], - "value": { - "Number": { - "location": "2:15-2:17", - "value": 24.0, - "unit": null - } - } - } - ] - } - }, - { - "Rule": { - "location": "5:1-7:2", - "selectors": [ - { - "Base": { - "location": "5:1-5:25", - "namespace_prefix": null, - "element_name": null, - "conditions": [ - { - "Class": [ - "myStyleName1", - "5:1-5:14" - ] - }, - { - "Attribute": { - "location": "5:14-5:25", - "name": [ - "attr", - "5:15-5:19" - ], - "operator": "BeginsWith", - "value": [ - ".", - "5:21-5:24" - ] - } - } - ] - } - } - ], - "properties": [ - { - "location": "6:5-6:15", - "name": [ - "color", - "6:5-6:10" - ], - "value": { - "Color": { - "location": "6:12-6:15", - "color_int": 16711680 - } - } - } - ] - } - }, - { - "Rule": { - "location": "9:1-9:24", - "selectors": [ - { - "Combinator": { - "location": "9:1-9:21", - "left": { - "Combinator": { - "location": "9:1-9:14", - "left": { - "Base": { - "location": "9:1-9:7", - "namespace_prefix": null, - "element_name": null, - "conditions": [ - { - "Id": [ - "myId1", - "9:1-9:7" - ] - } - ] - } - }, - "right": { - "Base": { - "location": "9:8-9:14", - "namespace_prefix": null, - "element_name": null, - "conditions": [ - { - "Id": [ - "myId2", - "9:8-9:14" - ] - } - ] - } - }, - "combinator_type": "Descendant" - } - }, - "right": { - "Base": { - "location": "9:15-9:21", - "namespace_prefix": null, - "element_name": null, - "conditions": [ - { - "Id": [ - "myId3", - "9:15-9:21" - ] - } - ] - } - }, - "combinator_type": "Descendant" - } - } - ], - "properties": [] - } - } - ] -} \ No newline at end of file