Compare commits
98 Commits
edea6d9db1
...
9ead94a6b9
| Author | SHA1 | Date |
|---|---|---|
|
|
9ead94a6b9 | |
|
|
389d791ac1 | |
|
|
e367920922 | |
|
|
0f6d9b301f | |
|
|
7f276a4139 | |
|
|
218f488e76 | |
|
|
1b85a2f654 | |
|
|
6043a4add5 | |
|
|
ef7bf95447 | |
|
|
d9f2d99c3a | |
|
|
d0cee40524 | |
|
|
b7d50f8222 | |
|
|
183d3e85c6 | |
|
|
08ed56b673 | |
|
|
3bc3ca180f | |
|
|
bd8270bd5d | |
|
|
e0c3253ab6 | |
|
|
15dde037b7 | |
|
|
0ee456756a | |
|
|
d9ae985282 | |
|
|
3271ca514c | |
|
|
6e27474da4 | |
|
|
927b0f52c1 | |
|
|
dd97937feb | |
|
|
68149d9ddf | |
|
|
1e82d2321f | |
|
|
0a8bf37e40 | |
|
|
469b8d3875 | |
|
|
ab76b1d43e | |
|
|
f808fef3f1 | |
|
|
32d453ebef | |
|
|
f3b3d5d3b6 | |
|
|
ca0edfc5bc | |
|
|
0fd9dc432e | |
|
|
237207a447 | |
|
|
b119ca33c7 | |
|
|
0885665baf | |
|
|
055de5c4ea | |
|
|
d988a10d9d | |
|
|
713977390d | |
|
|
6dde4b41c1 | |
|
|
8a9db43424 | |
|
|
7290806a2b | |
|
|
e4c06e56ee | |
|
|
863bc784cc | |
|
|
cebe3e9cb0 | |
|
|
5740172ddb | |
|
|
f6dadf881a | |
|
|
9c54dee454 | |
|
|
09dd19508d | |
|
|
79a6455d8f | |
|
|
2a41796405 | |
|
|
58998b4246 | |
|
|
14b371b3b6 | |
|
|
e772c4b2c2 | |
|
|
94693cce6c | |
|
|
c72fbfd148 | |
|
|
8ae065f582 | |
|
|
2185206f1b | |
|
|
68da1f4e12 | |
|
|
9279e52c00 | |
|
|
a07f16f283 | |
|
|
38f90e6491 | |
|
|
0d0df920ee | |
|
|
b8303689db | |
|
|
a6a5e42b6b | |
|
|
19c55b78f4 | |
|
|
96fe865ac1 | |
|
|
f962f5c36b | |
|
|
e1fb8c2a8e | |
|
|
23aa3a58fe | |
|
|
5b5465488f | |
|
|
116a15e8ea | |
|
|
124d3383e8 | |
|
|
a91b6f82f5 | |
|
|
8223dccc24 | |
|
|
6179bebbf0 | |
|
|
5154531083 | |
|
|
03973bbac1 | |
|
|
7e96a43e5f | |
|
|
eb595bc28b | |
|
|
b2cc3bb4c7 | |
|
|
cd350c538e | |
|
|
8d359f9969 | |
|
|
aed758101c | |
|
|
a9a8aff13b | |
|
|
804f314df7 | |
|
|
c1a8bc8577 | |
|
|
a3e0bd95bc | |
|
|
6abe437c70 | |
|
|
0133661ad4 | |
|
|
61b8f1ffb9 | |
|
|
0cccee936e | |
|
|
2bc8281f19 | |
|
|
6f3c152e73 | |
|
|
6e019fb3ac | |
|
|
3332511290 | |
|
|
6422737cf3 |
|
|
@ -0,0 +1,20 @@
|
||||||
|
name: Publish
|
||||||
|
|
||||||
|
on:
|
||||||
|
release:
|
||||||
|
types: [created]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
name: Publish to crates.io
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
env:
|
||||||
|
CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: Swatinem/rust-cache@v2
|
||||||
|
with:
|
||||||
|
key: publish
|
||||||
|
- run: cargo publish --token ${CRATES_TOKEN}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
name: Cargo build & test
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- develop
|
||||||
|
- 'releases/**'
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
env:
|
||||||
|
CARGO_TERM_COLOR: always
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build_and_test:
|
||||||
|
name: Cargo test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- run: cargo test --verbose --all-features
|
||||||
|
|
@ -1,2 +1 @@
|
||||||
/target
|
/target
|
||||||
/Cargo.lock
|
|
||||||
|
|
|
||||||
33
CHANGELOG.md
33
CHANGELOG.md
|
|
@ -9,6 +9,33 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
- Macro strings
|
||||||
|
- Function parameters/arguments
|
||||||
|
- Variables:
|
||||||
|
- Integer (scoreboard)
|
||||||
|
- Boolean (data storage)
|
||||||
|
- Integer and boolean arrays (scoreboard and data storage)
|
||||||
|
- Integer map (scoreboard)
|
||||||
|
- Boolean map (tag)
|
||||||
|
- Member access (e.g. `.objective` to get objective name where int is stored)
|
||||||
|
- Return statement
|
||||||
|
- internal `print` function
|
||||||
|
- While loop
|
||||||
|
- `for` keyword
|
||||||
|
- Example: barebones compiler
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Change the syntax to set the tag type from `tag "[name]" of "[type]"` to `tag<"[type]"> "[name]"`
|
||||||
|
- Remove the keyword `of`
|
||||||
|
- Option to deduplicate source files during serialization when using `SerdeWrapper`
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
|
||||||
|
## [0.1.0] - 2024-10-01
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
- Functions
|
- Functions
|
||||||
- without arguments
|
- without arguments
|
||||||
- Raw commands
|
- Raw commands
|
||||||
|
|
@ -20,7 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- lua blocks
|
- lua blocks
|
||||||
- imports
|
- imports
|
||||||
- group
|
- group
|
||||||
|
- Tags
|
||||||
|
|
||||||
### Changed
|
[unreleased]: https://github.com/moritz-hoelting/shulkerscript-lang/compare/v0.1.0...HEAD
|
||||||
|
[0.1.0]: https://github.com/moritz-hoelting/shulkerscript-lang/releases/tag/v0.1.0
|
||||||
### Removed
|
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
# Code of Conduct - Shulkerscript Lang
|
||||||
|
|
||||||
|
## Our Pledge
|
||||||
|
|
||||||
|
In the interest of fostering an open and welcoming environment, we as
|
||||||
|
contributors and maintainers pledge to make participation in our project and
|
||||||
|
our community a harassment-free experience for everyone, regardless of age, body
|
||||||
|
size, disability, ethnicity, sex characteristics, gender identity and expression,
|
||||||
|
level of experience, education, socio-economic status, nationality, personal
|
||||||
|
appearance, race, religion, or sexual identity and orientation.
|
||||||
|
|
||||||
|
## Our Standards
|
||||||
|
|
||||||
|
Examples of behavior that contributes to a positive environment for our
|
||||||
|
community include:
|
||||||
|
|
||||||
|
* Demonstrating empathy and kindness toward other people
|
||||||
|
* Being respectful of differing opinions, viewpoints, and experiences
|
||||||
|
* Giving and gracefully accepting constructive feedback
|
||||||
|
* Accepting responsibility and apologizing to those affected by our mistakes,
|
||||||
|
and learning from the experience
|
||||||
|
* Focusing on what is best not just for us as individuals, but for the
|
||||||
|
overall community
|
||||||
|
|
||||||
|
Examples of unacceptable behavior include:
|
||||||
|
|
||||||
|
* The use of sexualized language or imagery, and sexual attention or
|
||||||
|
advances
|
||||||
|
* Trolling, insulting or derogatory comments, and personal or political attacks
|
||||||
|
* Public or private harassment
|
||||||
|
* Publishing others' private information, such as a physical or email
|
||||||
|
address, without their explicit permission
|
||||||
|
* Other conduct which could reasonably be considered inappropriate in a
|
||||||
|
professional setting
|
||||||
|
|
||||||
|
## Our Responsibilities
|
||||||
|
|
||||||
|
Project maintainers are responsible for clarifying and enforcing our standards of
|
||||||
|
acceptable behavior and will take appropriate and fair corrective action in
|
||||||
|
response to any instances of unacceptable behavior.
|
||||||
|
|
||||||
|
Project maintainers have the right and responsibility to remove, edit, or reject
|
||||||
|
comments, commits, code, wiki edits, issues, and other contributions that are
|
||||||
|
not aligned to this Code of Conduct, or to ban
|
||||||
|
temporarily or permanently any contributor for other behaviors that they deem
|
||||||
|
inappropriate, threatening, offensive, or harmful.
|
||||||
|
|
||||||
|
## Scope
|
||||||
|
|
||||||
|
This Code of Conduct applies within all community spaces, and also applies when
|
||||||
|
an individual is officially representing the community in public spaces.
|
||||||
|
Examples of representing our community include using an official e-mail address,
|
||||||
|
posting via an official social media account, or acting as an appointed
|
||||||
|
representative at an online or offline event.
|
||||||
|
|
||||||
|
## Enforcement
|
||||||
|
|
||||||
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||||
|
reported to the community leaders responsible for enforcement at [shulkerscript@hoelting.dev](mailto:shulkerscript@hoelting.dev).
|
||||||
|
All complaints will be reviewed and investigated promptly and fairly.
|
||||||
|
|
||||||
|
All community leaders are obligated to respect the privacy and security of the
|
||||||
|
reporter of any incident.
|
||||||
|
|
||||||
|
## Attribution
|
||||||
|
|
||||||
|
This Code of Conduct is adapted from the [Contributor Covenant](https://contributor-covenant.org/), version
|
||||||
|
[1.4](https://www.contributor-covenant.org/version/1/4/code-of-conduct/code_of_conduct.md) and
|
||||||
|
[2.0](https://www.contributor-covenant.org/version/2/0/code_of_conduct/code_of_conduct.md),
|
||||||
|
and was generated by [contributing-gen](https://github.com/bttger/contributing-gen).
|
||||||
|
|
@ -0,0 +1,146 @@
|
||||||
|
<!-- omit in toc -->
|
||||||
|
# Contributing to Shulkerscript Lang
|
||||||
|
|
||||||
|
First off, thanks for taking the time to contribute! ❤️
|
||||||
|
|
||||||
|
All types of contributions are encouraged and valued. See the [Table of Contents](#table-of-contents) for different ways to help and details about how this project handles them. Please make sure to read the relevant section before making your contribution. It will make it a lot easier for us maintainers and smooth out the experience for all involved. The community looks forward to your contributions. 🎉
|
||||||
|
|
||||||
|
> And if you like the project, but just don't have time to contribute, that's fine. There are other easy ways to support the project and show your appreciation, which we would also be very happy about:
|
||||||
|
> - Star the project
|
||||||
|
> - Tweet about it
|
||||||
|
> - Refer this project in your project's readme
|
||||||
|
> - Mention the project at local meetups and tell your friends/colleagues
|
||||||
|
|
||||||
|
<!-- omit in toc -->
|
||||||
|
## Table of Contents
|
||||||
|
|
||||||
|
- [Code of Conduct](#code-of-conduct)
|
||||||
|
- [I Have a Question](#i-have-a-question)
|
||||||
|
- [I Want To Contribute](#i-want-to-contribute)
|
||||||
|
- [Reporting Bugs](#reporting-bugs)
|
||||||
|
- [Suggesting Enhancements](#suggesting-enhancements)
|
||||||
|
- [Your First Code Contribution](#your-first-code-contribution)
|
||||||
|
- [Improving The Documentation](#improving-the-documentation)
|
||||||
|
- [Styleguides](#styleguides)
|
||||||
|
- [Commit Messages](#commit-messages)
|
||||||
|
- [Join The Project Team](#join-the-project-team)
|
||||||
|
|
||||||
|
|
||||||
|
## Code of Conduct
|
||||||
|
|
||||||
|
This project and everyone participating in it is governed by the
|
||||||
|
[Shulkerscript Lang Code of Conduct](https://github.com/moritz-hoelting/shulkerscript-lang/blob/main/CODE_OF_CONDUCT.md).
|
||||||
|
By participating, you are expected to uphold this code. Please report unacceptable behavior
|
||||||
|
to .
|
||||||
|
|
||||||
|
|
||||||
|
## I Have a Question
|
||||||
|
|
||||||
|
> If you want to ask a question, we assume that you have read the available [Documentation](https://shulkerscript.hoelting.dev/).
|
||||||
|
> When asking about a missing feature, please make sure that it is not already on the [Roadmap](https://shulkerscript.hoelting.dev/roadmap).
|
||||||
|
|
||||||
|
Before you ask a question, it is best to search for existing [Issues](https://github.com/moritz-hoelting/shulkerscript-lang/issues) and [Discussions](https://github.com/moritz-hoelting/shulkerscript-lang/discussions) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first.
|
||||||
|
|
||||||
|
If you then still feel the need to ask a question and need clarification, we recommend either [writing an email](mailto:shulkerscript@hoelting.dev) or opening a discussion on GitHub:
|
||||||
|
|
||||||
|
- Open a [Q&A Discussion](https://github.com/moritz-hoelting/shulkerscript-lang/discussions/new?category=q-a).
|
||||||
|
- Provide as much context as you can about what you're running into.
|
||||||
|
- Provide project and platform versions (Windows/Linux/macOS), depending on what seems relevant.
|
||||||
|
|
||||||
|
We will then take care of the issue as soon as possible.
|
||||||
|
|
||||||
|
## I Want To Contribute
|
||||||
|
|
||||||
|
> ### Legal Notice <!-- omit in toc -->
|
||||||
|
> When contributing to this project, you must agree that you have authored 100% of the content, that you have the necessary rights to the content and that the content you contribute may be provided under the project license.
|
||||||
|
|
||||||
|
### Reporting Bugs
|
||||||
|
|
||||||
|
<!-- omit in toc -->
|
||||||
|
#### Before Submitting a Bug Report
|
||||||
|
|
||||||
|
A good bug report shouldn't leave others needing to chase you up for more information. Therefore, we ask you to investigate carefully, collect information and describe the issue in detail in your report. Please complete the following steps in advance to help us fix any potential bug as fast as possible.
|
||||||
|
|
||||||
|
- Make sure that you are using the latest version.
|
||||||
|
- Determine if your bug is really a bug and not an error on your side e.g. using incompatible environment components/versions (Make sure that you have read the [documentation](https://shulkerscript.hoelting.dev/). If you are looking for support, you might want to check [this section](#i-have-a-question)).
|
||||||
|
- To see if other users have experienced (and potentially already solved) the same issue you are having, check if there is not already a bug report existing for your bug or error in the [bug tracker](https://github.com/moritz-hoelting/shulkerscript-lang/issues?q=label%3Abug).
|
||||||
|
- Collect information about the bug:
|
||||||
|
- Stack trace (Traceback)
|
||||||
|
- OS, Platform and Version (Windows, Linux, macOS, x86, ARM)
|
||||||
|
- Version of the Library.
|
||||||
|
- Possibly your input and the output
|
||||||
|
- Can you reliably reproduce the issue? And can you also reproduce it with older versions?
|
||||||
|
|
||||||
|
<!-- omit in toc -->
|
||||||
|
#### How Do I Submit a Good Bug Report?
|
||||||
|
|
||||||
|
> You must never report security related issues, vulnerabilities or bugs including sensitive information to the issue tracker, or elsewhere in public. Instead sensitive bugs must be sent by email to [shulkerscript@hoelting.dev](mailto:shulkerscript@hoelting.dev).
|
||||||
|
<!-- You may add a PGP key to allow the messages to be sent encrypted as well. -->
|
||||||
|
|
||||||
|
We use GitHub issues to track bugs and errors. If you run into an issue with the project:
|
||||||
|
|
||||||
|
- Open an [Issue](https://github.com/moritz-hoelting/shulkerscript-lang/issues/new). (Since we can't be sure at this point whether it is a bug or not, we ask you not to talk about a bug yet and not to label the issue.)
|
||||||
|
- Explain the behavior you would expect and the actual behavior.
|
||||||
|
- Please provide as much context as possible and describe the *reproduction steps* that someone else can follow to recreate the issue on their own. This usually includes your code. For good bug reports you should isolate the problem and create a reduced test case.
|
||||||
|
- Provide the information you collected in the previous section.
|
||||||
|
|
||||||
|
Once it's filed:
|
||||||
|
|
||||||
|
- The project team will label the issue accordingly.
|
||||||
|
- A team member will try to reproduce the issue with your provided steps. If there are no reproduction steps or no obvious way to reproduce the issue, the team will ask you for those steps and mark the issue as `needs-repro`. Bugs with the `needs-repro` tag will not be addressed until they are reproduced.
|
||||||
|
- If the team is able to reproduce the issue, it will be marked `needs-fix`, as well as possibly other tags (such as `critical`), and the issue will be left to be [implemented by someone](#your-first-code-contribution).
|
||||||
|
|
||||||
|
<!-- You might want to create an issue template for bugs and errors that can be used as a guide and that defines the structure of the information to be included. If you do so, reference it here in the description. -->
|
||||||
|
|
||||||
|
|
||||||
|
### Suggesting Enhancements
|
||||||
|
|
||||||
|
This section guides you through submitting an enhancement suggestion for Shulkerscript Lang, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions.
|
||||||
|
|
||||||
|
<!-- omit in toc -->
|
||||||
|
#### Before Submitting an Enhancement
|
||||||
|
|
||||||
|
- Make sure that you are using the latest version.
|
||||||
|
- Read the [documentation](https://shulkerscript.hoelting.dev/) carefully and find out if the functionality is already covered, maybe by an individual configuration.
|
||||||
|
- Perform a [search](https://github.com/moritz-hoelting/shulkerscript-lang/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one.
|
||||||
|
- Find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Keep in mind that we want features that will be useful to the majority of our users and not just a small subset. If you're just targeting a minority of users, consider writing an add-on/plugin library.
|
||||||
|
|
||||||
|
<!-- omit in toc -->
|
||||||
|
#### How Do I Submit a Good Enhancement Suggestion?
|
||||||
|
|
||||||
|
Enhancement suggestions are tracked as [GitHub issues](https://github.com/moritz-hoelting/shulkerscript-lang/issues).
|
||||||
|
|
||||||
|
- Use a **clear and descriptive title** for the issue to identify the suggestion.
|
||||||
|
- Provide a **step-by-step description of the suggested enhancement** in as many details as possible.
|
||||||
|
- **Describe the current behavior** and **explain which behavior you expected to see instead** and why. At this point you can also tell which alternatives do not work for you.
|
||||||
|
- **Explain why this enhancement would be useful** to most Shulkerscript lang users. You may also want to point out the other projects that solved it better and which could serve as inspiration.
|
||||||
|
|
||||||
|
<!-- You might want to create an issue template for enhancement suggestions that can be used as a guide and that defines the structure of the information to be included. If you do so, reference it here in the description. -->
|
||||||
|
|
||||||
|
### Your First Code Contribution
|
||||||
|
Think about which part of the project your idea fits into. Do you want to work on [the cli](https://github.com/moritz-hoelting/shulkerscript-cli), [the language](https://github.com/moritz-hoelting/shulkerscript-lang) or [the datapack generation](https://github.com/moritz-hoelting/shulkerbox)?
|
||||||
|
|
||||||
|
- Make sure you have [Rust](https://www.rust-lang.org/tools/install) installed.
|
||||||
|
- Fork the repository and clone it to your local machine.
|
||||||
|
- Create a new branch for your feature or bug fix.
|
||||||
|
- Make your changes and commit them to your branch.
|
||||||
|
- Push your changes to your fork.
|
||||||
|
- Open a pull request in the original repository and describe the changes you made.
|
||||||
|
|
||||||
|
### Improving The Documentation
|
||||||
|
If you want to improve the documentation, you can do so by editing the [documentation repository](https://github.com/moritz-hoelting/shulkerscript-webpage).
|
||||||
|
|
||||||
|
- Make sure you have [Node.js](https://nodejs.org/en/download) and [PNPM](https://pnpm.io/installation) installed.
|
||||||
|
- Fork the repository and clone it to your local machine.
|
||||||
|
- Create a new branch for your changes.
|
||||||
|
- Start the development server with `pnpm dev --open`.
|
||||||
|
- Make your changes and commit them to your branch.
|
||||||
|
- Push your changes to your fork.
|
||||||
|
- Open a pull request in the original repository and describe the changes you made.
|
||||||
|
|
||||||
|
## Join The Project Team
|
||||||
|
We are always looking for new contributors to the project. If you are interested in joining the project team, please contact us at [shulkerscript@hoelting.dev](mailto:shulkerscript@hoelting.dev).
|
||||||
|
|
||||||
|
<!-- omit in toc -->
|
||||||
|
## Attribution
|
||||||
|
This guide is based on the **contributing-gen**. [Make your own](https://github.com/bttger/contributing-gen)!
|
||||||
File diff suppressed because it is too large
Load Diff
65
Cargo.toml
65
Cargo.toml
|
|
@ -4,34 +4,59 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
authors = ["Moritz Hölting <moritz@hoelting.dev>"]
|
authors = ["Moritz Hölting <moritz@hoelting.dev>"]
|
||||||
categories = ["compilers"]
|
description = "Shulkerscript language implementation with compiler"
|
||||||
description = "ShulkerScript language implementation with compiler"
|
categories = ["compilers", "game-development"]
|
||||||
|
keywords = ["minecraft", "datapack", "mcfunction"]
|
||||||
repository = "https://github.com/moritz-hoelting/shulkerscript-lang"
|
repository = "https://github.com/moritz-hoelting/shulkerscript-lang"
|
||||||
|
homepage = "https://shulkerscript.hoelting.dev/"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
license = "MIT"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["lua", "shulkerbox"]
|
default = ["fs_access", "lua", "shulkerbox", "zip"]
|
||||||
serde = ["dep:serde", "shulkerbox?/serde"]
|
fs_access = ["shulkerbox?/fs_access"]
|
||||||
shulkerbox = ["dep:shulkerbox"]
|
|
||||||
lua = ["dep:mlua"]
|
lua = ["dep:mlua"]
|
||||||
|
serde = ["dep:serde", "dep:serde_json", "shulkerbox?/serde"]
|
||||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
shulkerbox = ["dep:shulkerbox", "dep:chksum-md5", "dep:serde_json", "dep:oxford_join"]
|
||||||
path-absolutize = { version = "3.1.1", features = ["use_unix_paths_on_wasm"] }
|
zip = ["shulkerbox?/zip"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chksum-md5 = "0.0.0"
|
cfg-if = "1.0.0"
|
||||||
colored = "2.1.0"
|
chksum-md5 = { version = "0.1.0", optional = true }
|
||||||
derive_more = { version = "0.99.17", default-features = false, features = ["deref", "from", "deref_mut"] }
|
colored = "3.0.0"
|
||||||
|
derive_more = { version = "2.0.1", default-features = false, features = [ "deref", "deref_mut", "from"] }
|
||||||
enum-as-inner = "0.6.0"
|
enum-as-inner = "0.6.0"
|
||||||
getset = "0.1.2"
|
getset = "0.1.2"
|
||||||
mlua = { version = "0.9.7", features = ["lua54", "vendored"], optional = true }
|
itertools = "0.14.0"
|
||||||
path-absolutize = "3.1.1"
|
mlua = { version = "0.11.3", features = ["lua54", "vendored"], optional = true }
|
||||||
serde = { version = "1.0.197", features = ["derive", "rc"], optional = true }
|
oxford_join = { version = "0.7.1", optional = true }
|
||||||
shulkerbox = { git = "https://github.com/moritz-hoelting/shulkerbox", default-features = false, optional = true, rev = "a2d20dab8ea97bbd873edafb23afaad34292457f" }
|
pathdiff = "0.2.3"
|
||||||
strum = { version = "0.26.2", features = ["derive"] }
|
serde = { version = "1.0.217", features = ["derive"], optional = true }
|
||||||
strum_macros = "0.26.2"
|
serde_json = { version = "1.0.138", optional = true }
|
||||||
thiserror = "1.0.58"
|
# shulkerbox = { version = "0.1.0", default-features = false, optional = true }
|
||||||
tracing = "0.1.40"
|
shulkerbox = { git = "https://github.com/moritz-hoelting/shulkerbox", rev = "e1bc953b7a1692c65f1ed2c43fa3b0c607df8090", default-features = false, optional = true }
|
||||||
|
strsim = "0.11.1"
|
||||||
|
strum = { version = "0.27.0", features = ["derive"] }
|
||||||
|
thiserror = "2.0.11"
|
||||||
|
tracing = "0.1.41"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
assert-struct = "0.2.0"
|
||||||
|
serde_json = "1.0.138"
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "compiler"
|
||||||
|
required-features = ["fs_access", "shulkerbox"]
|
||||||
|
|
||||||
|
[[test]]
|
||||||
|
name = "parsing"
|
||||||
|
required-features = ["shulkerbox"]
|
||||||
|
|
||||||
|
[[test]]
|
||||||
|
name = "transpiling"
|
||||||
|
required-features = ["shulkerbox"]
|
||||||
|
|
||||||
|
[package.metadata.cargo-feature-combinations]
|
||||||
|
exclude_features = [ "default" ]
|
||||||
|
|
@ -0,0 +1,201 @@
|
||||||
|
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 Moritz Hölting <moritz@hoelting.dev>
|
||||||
|
|
||||||
|
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.
|
||||||
14
README.md
14
README.md
|
|
@ -1,15 +1,23 @@
|
||||||
# ShulkerScript Language
|
# Shulkerscript Language
|
||||||
|
|
||||||
ShulkerScript is a simple, easy-to-use scripting language for Minecraft datapacks. It is designed to be easy to learn and use, while still being powerful enough to create complex scripts, while not being hindered by Minecraft command limitations.
|
Shulkerscript is a simple, easy-to-use scripting language for Minecraft datapacks. It is designed to be easy to learn and use, while still being powerful enough to create complex scripts, while not being hindered by Minecraft command limitations.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Add the following to your dependencies in `Cargo.toml`:
|
Add the following to your dependencies in `Cargo.toml`:
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
shulkerscript-lang = { git = "https://github.com/moritz-hoelting/shulkerscript-lang" }
|
shulkerscript = "0.1.0"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## VS Code Extension
|
||||||
|
|
||||||
|
A VS Code extension is available [here](https://marketplace.visualstudio.com/items?itemName=moritz-hoelting.shulkerscript-lang) to provide syntax highlighting and snippets for Shulkerscript files.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Read the [documentation](https://shulkerscript.hoelting.dev) for more information on the language and cli.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
### Functions
|
### Functions
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
//! This example demonstrates how to compile a shulkerscript file into a datapack using the shulkerscript compiler.
|
||||||
|
//! Most basic version of a shulkerscript compiler, which takes a single input file and places the resulting datapack in the specified output directory.
|
||||||
|
//!
|
||||||
|
//! For a ready-to-use compiler, see the `shulkerscript-cli` crate.
|
||||||
|
|
||||||
|
use shulkerscript::{
|
||||||
|
base::{FsProvider, PrintHandler},
|
||||||
|
compile,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut args = std::env::args();
|
||||||
|
let _ = args.next().unwrap();
|
||||||
|
let input = args.next().expect("Expect path to shulkerscript file");
|
||||||
|
|
||||||
|
let main_namespace = args.next().expect("Expect main namespace name");
|
||||||
|
|
||||||
|
let output = args.next().expect("Expect path to output directory");
|
||||||
|
|
||||||
|
let code = compile(
|
||||||
|
&PrintHandler::new(),
|
||||||
|
&FsProvider::default(),
|
||||||
|
main_namespace,
|
||||||
|
shulkerbox::datapack::Datapack::LATEST_FORMAT,
|
||||||
|
&[("main".to_string(), &input)],
|
||||||
|
)
|
||||||
|
.expect("failed to compile");
|
||||||
|
|
||||||
|
code.place(output).expect("failed to place datapack");
|
||||||
|
}
|
||||||
522
grammar.md
522
grammar.md
|
|
@ -1,43 +1,145 @@
|
||||||
# Grammar of the ShulkerScript language
|
# Grammar of the Shulkerscript language
|
||||||
|
|
||||||
## Table of contents
|
## Program
|
||||||
|
|
||||||
### Program
|
|
||||||
```ebnf
|
```ebnf
|
||||||
Program: Namespace Declaration*;
|
Program:
|
||||||
|
Namespace
|
||||||
|
Declaration*
|
||||||
|
;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Namespace
|
## Declaration
|
||||||
|
|
||||||
```ebnf
|
```ebnf
|
||||||
Namespace: 'namespace' StringLiteral;
|
Declaration:
|
||||||
|
Function
|
||||||
|
| Import
|
||||||
|
| TagDeclaration
|
||||||
|
| ('pub'? VariableDeclaration ';')
|
||||||
|
;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Declaration
|
## Namespace
|
||||||
|
|
||||||
```ebnf
|
```ebnf
|
||||||
Declaration: FunctionDeclaration | Import;
|
Namespace:
|
||||||
|
'namespace' StringLiteral ';' ;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Import
|
## Function
|
||||||
```ebnf
|
|
||||||
Import: 'from' StringLiteral 'import' Identifier;
|
|
||||||
```
|
|
||||||
|
|
||||||
### FunctionDeclaration
|
|
||||||
```ebnf
|
```ebnf
|
||||||
Function:
|
Function:
|
||||||
Annotation* 'pub'? 'fn' Identifier '(' ParameterList? ')' Block
|
Annotation* 'pub'? 'fn' Identifier '(' FunctionParameterList? ')' Block
|
||||||
;
|
|
||||||
ParameterList:
|
|
||||||
Identifier (',' Identifier)* ','?
|
|
||||||
;
|
;
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Annotation
|
## Import
|
||||||
|
|
||||||
```ebnf
|
```ebnf
|
||||||
Annotation: '#[' Identifier ('=' StringLiteral)? ']';
|
Import:
|
||||||
|
'from' StringLiteral 'import' ('*' | Identifier (',' Identifier)*) ';'
|
||||||
|
;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Statement
|
## TagDeclaration
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
TagDeclaration:
|
||||||
|
'tag' ('<' StringLiteral '>')? StringLiteral 'replace'? '[' (StringLiteral (',' StringLiteral)*)? ']'
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## VariableDeclaration
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
VariableDeclaration:
|
||||||
|
SingleVariableDeclaration
|
||||||
|
| ArrayVariableDeclaration
|
||||||
|
| ScoreVariableDeclaration
|
||||||
|
| TagVariableDeclaration
|
||||||
|
| ComptimeValueDeclaration
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## StringLiteral
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
StringLiteral:
|
||||||
|
'"' TEXT '"';
|
||||||
|
```
|
||||||
|
|
||||||
|
## Annotation
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Annotation:
|
||||||
|
'#[' AnnotationAssignment ']'
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Block
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Block:
|
||||||
|
'{' Statement* '}'
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## FunctionParameterList
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
FunctionParameterList:
|
||||||
|
FunctionArgument (',' FunctionArgument)* ','?
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## ArrayVariableDeclaration
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
ArrayVariableDeclaration:
|
||||||
|
('int' | 'bool') '[' integer ']' identifier VariableDeclarationAssignment?
|
||||||
|
```
|
||||||
|
|
||||||
|
## ComptimeValueDeclaration
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
ComptimeValueDeclaration:
|
||||||
|
'val' identifier VariableDeclarationAssignment?
|
||||||
|
```
|
||||||
|
|
||||||
|
## ScoreVariableDeclaration
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
ScoreVariableDeclaration:
|
||||||
|
'int' ('<' StringLiteral '>')? identifier '[' AnyStringLiteral? ']' VariableDeclarationAssignment?
|
||||||
|
```
|
||||||
|
|
||||||
|
## SingleVariableDeclaration
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
SingleVariableDeclaration:
|
||||||
|
('int' | 'bool') identifier VariableDeclarationAssignment?
|
||||||
|
```
|
||||||
|
|
||||||
|
## TagVariableDeclaration
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
TagVariableDeclaration:
|
||||||
|
'bool' identifier '[' AnyStringLiteral? ']' VariableDeclarationAssignment?
|
||||||
|
```
|
||||||
|
|
||||||
|
## AnnotationAssignment
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
AnnotationAssignment:
|
||||||
|
Identifier AnnotationValue
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Statement
|
||||||
|
|
||||||
```ebnf
|
```ebnf
|
||||||
Statement:
|
Statement:
|
||||||
Block
|
Block
|
||||||
|
|
@ -45,113 +147,361 @@ Statement:
|
||||||
| Conditional
|
| Conditional
|
||||||
| Grouping
|
| Grouping
|
||||||
| DocComment
|
| DocComment
|
||||||
|
| ExecuteBlock
|
||||||
|
| WhileLoop
|
||||||
| Semicolon
|
| Semicolon
|
||||||
| Run
|
|
||||||
;
|
;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Block
|
## FunctionArgument
|
||||||
```ebnf
|
|
||||||
Block: '{' Statement* '}';
|
|
||||||
```
|
|
||||||
|
|
||||||
### Run
|
|
||||||
```ebnf
|
```ebnf
|
||||||
Run:
|
FunctionArgument:
|
||||||
'run' Expression ';'
|
FunctionVariableType Identifier
|
||||||
;
|
;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Conditional
|
## VariableDeclarationAssignment
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
VariableDeclarationAssignment:
|
||||||
|
'=' Expression
|
||||||
|
```
|
||||||
|
|
||||||
|
## AnyStringLiteral
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
AnyStringLiteral: StringLiteral | TemplateStringLiteral ;
|
||||||
|
```
|
||||||
|
|
||||||
|
## AnnotationValue
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
AnnotationValue:
|
||||||
|
'=' Expression
|
||||||
|
| '(' AnnotationAssignment ( ',' AnnotationAssignment )* ')'
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conditional
|
||||||
|
|
||||||
```ebnf
|
```ebnf
|
||||||
Conditional:
|
Conditional:
|
||||||
'if' ParenthizedCondition Block ('else' Block)?
|
'if' Parenthized
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## ExecuteBlock
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
ExecuteBlock:
|
||||||
|
(ExecuteBlockHead ExecuteBlockTail)
|
||||||
|
| (Conditional Block Else)
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Grouping
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Grouping:
|
||||||
|
'group' Block
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Semicolon
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Semicolon:
|
||||||
|
SemicolonStatement ';'
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## WhileLoop
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
WhileLoop:
|
||||||
|
'while' '(' Expression ')' Block
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## FunctionVariableType
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
FunctionVariableType:
|
||||||
|
'macro' | 'int' | 'bool' | 'val'
|
||||||
;
|
;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Condition
|
## Expression
|
||||||
|
|
||||||
```ebnf
|
```ebnf
|
||||||
Condition:
|
Expression:
|
||||||
PrimaryCondition
|
Primary | Binary ;
|
||||||
BinaryCondition
|
```
|
||||||
|
|
||||||
|
## TemplateStringLiteral
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
TemplateStringLiteral:
|
||||||
|
'`' ( TemplateStringLiteralText | '$(' Expression ')' )* '`';
|
||||||
|
```
|
||||||
|
|
||||||
|
## Else
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Else:
|
||||||
|
'else' Block
|
||||||
;
|
;
|
||||||
```
|
```
|
||||||
|
|
||||||
#### PrimaryCondition
|
## ExecuteBlockHead
|
||||||
|
|
||||||
```ebnf
|
```ebnf
|
||||||
PrimaryCondition:
|
ExecuteBlockHead:
|
||||||
ConditionalPrefix
|
Conditional
|
||||||
| ParenthesizedCondition
|
| Align
|
||||||
|
| Anchored
|
||||||
|
| As
|
||||||
|
| AsAt
|
||||||
|
| At
|
||||||
|
| Facing
|
||||||
|
| In
|
||||||
|
| On
|
||||||
|
| Positioned
|
||||||
|
| Rotated
|
||||||
|
| Store
|
||||||
|
| Summon
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## ExecuteBlockTail
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
ExecuteBlockTail:
|
||||||
|
ExecuteBlock
|
||||||
|
| Block
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## SemicolonStatement
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
SemicolonStatement:
|
||||||
|
(Expression | VariableDeclaration | Assignment | ReturnStatement)
|
||||||
|
';'
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Binary
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Binary:
|
||||||
|
Expression BinaryOperator Expression
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Primary
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Primary:
|
||||||
|
Identifier
|
||||||
|
| Prefix
|
||||||
|
| Parenthesized
|
||||||
|
| Indexed
|
||||||
|
| Integer
|
||||||
|
| Boolean
|
||||||
| StringLiteral
|
| StringLiteral
|
||||||
;
|
| FunctionCall
|
||||||
|
| MemberAccess
|
||||||
|
| TemplateStringLiteral
|
||||||
|
| LuaCode
|
||||||
```
|
```
|
||||||
|
|
||||||
#### ConditionalPrefix
|
## TemplateStringLiteralText
|
||||||
|
|
||||||
```ebnf
|
```ebnf
|
||||||
ConditionalPrefix:
|
TemplateStringLiteralText:
|
||||||
ConditionalPrefixOperator PrimaryCondition
|
TEXT ;
|
||||||
;
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### ConditionalPrefixOperator
|
## Align
|
||||||
``` ebnf
|
|
||||||
ConditionalPrefixOperator: '!';
|
```ebnf
|
||||||
|
Align:
|
||||||
|
'align' '(' AnyStringLiteral ')' ;
|
||||||
```
|
```
|
||||||
|
|
||||||
#### BinaryCondition
|
## Anchored
|
||||||
``` ebnf
|
|
||||||
BinaryCondition:
|
```ebnf
|
||||||
Condition ConditionalBinaryOperator Condition
|
Anchored:
|
||||||
;
|
'anchored' '(' AnyStringLiteral ')' ;
|
||||||
```
|
```
|
||||||
|
|
||||||
#### ConditionalBinaryOperator
|
## As
|
||||||
``` ebnf
|
|
||||||
ConditionalBinaryOperator:
|
```ebnf
|
||||||
'&&'
|
As:
|
||||||
|
'as' '(' AnyStringLiteral ')' ;
|
||||||
|
```
|
||||||
|
|
||||||
|
## AsAt
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
AsAt:
|
||||||
|
'asat' '(' AnyStringLiteral ')' ;
|
||||||
|
```
|
||||||
|
|
||||||
|
## At
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
At:
|
||||||
|
'at' '(' AnyStringLiteral ')' ;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Facing
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Facing:
|
||||||
|
'facing' '(' AnyStringLiteral ')' ;
|
||||||
|
```
|
||||||
|
|
||||||
|
## In
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
In:
|
||||||
|
'in' '(' AnyStringLiteral ')' ;
|
||||||
|
```
|
||||||
|
|
||||||
|
## On
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
On:
|
||||||
|
'on' '(' AnyStringLiteral ')' ;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Positioned
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Positioned:
|
||||||
|
'positioned' '(' AnyStringLiteral ')' ;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Rotated
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Rotated:
|
||||||
|
'rotated' '(' AnyStringLiteral ')' ;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Store
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Store:
|
||||||
|
'store' '(' AnyStringLiteral ')' ;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Summon
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Summon:
|
||||||
|
'summon' '(' AnyStringLiteral ')' ;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Assignment
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Assignment:
|
||||||
|
AssignmentDestination '=' Expression
|
||||||
|
```
|
||||||
|
|
||||||
|
## ReturnStatement
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
ReturnStatement:
|
||||||
|
`return` Expression ;
|
||||||
|
```
|
||||||
|
|
||||||
|
## BinaryOperator
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
BinaryOperator:
|
||||||
|
'+'
|
||||||
|
| '-'
|
||||||
|
| '*'
|
||||||
|
| '/'
|
||||||
|
| '%'
|
||||||
|
| '=='
|
||||||
|
| '!='
|
||||||
|
| '<'
|
||||||
|
| '<='
|
||||||
|
| '>'
|
||||||
|
| '>='
|
||||||
|
| '&&'
|
||||||
| '||'
|
| '||'
|
||||||
;
|
;
|
||||||
```
|
```
|
||||||
|
|
||||||
#### ParenthizedCondition
|
## FunctionCall
|
||||||
```ebnf
|
|
||||||
ParenthizedCondition:
|
|
||||||
'(' Condition ')'
|
|
||||||
;
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
### Grouping
|
|
||||||
``` ebnf
|
|
||||||
Grouping:
|
|
||||||
'group' Block
|
|
||||||
;
|
|
||||||
```
|
|
||||||
|
|
||||||
### Expression
|
|
||||||
```ebnf
|
|
||||||
Expression:
|
|
||||||
Primary
|
|
||||||
;
|
|
||||||
```
|
|
||||||
|
|
||||||
### Primary
|
|
||||||
```ebnf
|
|
||||||
Primary:
|
|
||||||
FunctionCall
|
|
||||||
;
|
|
||||||
```
|
|
||||||
|
|
||||||
### FunctionCall
|
|
||||||
```ebnf
|
```ebnf
|
||||||
FunctionCall:
|
FunctionCall:
|
||||||
Identifier '(' (Expression (',' Expression)*)? ')'
|
Identifier '(' (Expression (',' Expression)*)? ')'
|
||||||
;
|
;
|
||||||
```
|
```
|
||||||
|
|
||||||
### LuaCode
|
## Indexed
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Indexed:
|
||||||
|
PrimaryExpression '[' Expression ']'
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## LuaCode
|
||||||
|
|
||||||
```ebnf
|
```ebnf
|
||||||
LuaCode:
|
LuaCode:
|
||||||
'lua' '(' (Expression (',' Expression)*)? ')' '{' (.*?)* '}'
|
'lua' '(' (Expression (',' Expression)*)? ')' '{' (.*?)* '}'
|
||||||
|
```
|
||||||
|
|
||||||
|
## MemberAccess
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
MemberAccess:
|
||||||
|
Primary '.' Identifier
|
||||||
|
```
|
||||||
|
|
||||||
|
## Parenthesized
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Parenthesized:
|
||||||
|
'(' Expression ')'
|
||||||
;
|
;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Prefix
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
Prefix:
|
||||||
|
PrefixOperator Primary
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## AssignmentDestination
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
AssignmentDestination:
|
||||||
|
Identifier
|
||||||
|
| Identifier '[' Expression ']'
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
## PrefixOperator
|
||||||
|
|
||||||
|
```ebnf
|
||||||
|
PrefixOperator:
|
||||||
|
'!' | '-' | 'run'
|
||||||
|
;
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,136 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import re
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
from collections import defaultdict, deque
|
||||||
|
|
||||||
|
ebnf_blocks = []
|
||||||
|
rule_defs = {}
|
||||||
|
rule_deps = defaultdict(set)
|
||||||
|
|
||||||
|
ebnf_fence_start = re.compile(r"^\s*///\s*```\s*ebnf\s*$")
|
||||||
|
ebnf_fence_end = re.compile(r"^\s*///\s*```\s*$")
|
||||||
|
doc_comment_prefix = re.compile(r"^\s*///\s?(.*)$")
|
||||||
|
|
||||||
|
rule_start_pattern = re.compile(r"^\s*([A-Za-z_]\w*)\s*:")
|
||||||
|
rule_ref_pattern = re.compile(r"\b([A-Za-z_]\w*)\b")
|
||||||
|
|
||||||
|
|
||||||
|
def find_project_root() -> Path | None:
|
||||||
|
current = Path.cwd()
|
||||||
|
while current != current.parent:
|
||||||
|
cargo_toml = current / "Cargo.toml"
|
||||||
|
if cargo_toml.exists():
|
||||||
|
text = cargo_toml.read_text(encoding="utf-8")
|
||||||
|
if re.search(r'(?m)^\s*name\s*=\s*"shulkerscript"\s*$', text):
|
||||||
|
return current
|
||||||
|
current = current.parent
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
root_dir = find_project_root()
|
||||||
|
if not root_dir:
|
||||||
|
raise SystemExit(
|
||||||
|
"Could not find Cargo.toml of package 'shulkerscript' in this or any parent directory."
|
||||||
|
)
|
||||||
|
|
||||||
|
if Path.cwd() != root_dir:
|
||||||
|
os.chdir(root_dir)
|
||||||
|
print(f"Changed working directory to {root_dir}")
|
||||||
|
|
||||||
|
previous_rules = set()
|
||||||
|
|
||||||
|
with open("grammar.md", "r", encoding="utf-8") as f:
|
||||||
|
rule_header_pattern = re.compile(r"## (\w+)")
|
||||||
|
for line in f:
|
||||||
|
m = rule_header_pattern.match(line)
|
||||||
|
if m:
|
||||||
|
previous_rules.add(m.group(1))
|
||||||
|
|
||||||
|
for path in Path(".").rglob("*.rs"):
|
||||||
|
with path.open(encoding="utf-8") as f:
|
||||||
|
in_block = False
|
||||||
|
current_block_lines = []
|
||||||
|
|
||||||
|
for line in f:
|
||||||
|
if not in_block and ebnf_fence_start.match(line):
|
||||||
|
in_block = True
|
||||||
|
current_block_lines = []
|
||||||
|
continue
|
||||||
|
if in_block:
|
||||||
|
if ebnf_fence_end.match(line):
|
||||||
|
block_text = "\n".join(current_block_lines)
|
||||||
|
|
||||||
|
ebnf_blocks.append(block_text)
|
||||||
|
|
||||||
|
current_rule_name = None
|
||||||
|
current_rule_lines = []
|
||||||
|
for ln in current_block_lines:
|
||||||
|
m = rule_start_pattern.match(ln)
|
||||||
|
if m:
|
||||||
|
if current_rule_name:
|
||||||
|
full_def = "\n".join(current_rule_lines)
|
||||||
|
rule_defs[current_rule_name] = full_def
|
||||||
|
refs = set(rule_ref_pattern.findall(full_def))
|
||||||
|
refs.discard(current_rule_name)
|
||||||
|
rule_deps[current_rule_name].update(refs)
|
||||||
|
current_rule_name = m.group(1)
|
||||||
|
current_rule_lines = [ln]
|
||||||
|
else:
|
||||||
|
if current_rule_name:
|
||||||
|
current_rule_lines.append(ln)
|
||||||
|
|
||||||
|
if current_rule_name:
|
||||||
|
full_def = "\n".join(current_rule_lines)
|
||||||
|
|
||||||
|
rule_defs[current_rule_name] = full_def
|
||||||
|
refs = set(rule_ref_pattern.findall(full_def))
|
||||||
|
refs.discard(current_rule_name)
|
||||||
|
rule_deps[current_rule_name].update(refs)
|
||||||
|
|
||||||
|
in_block = False
|
||||||
|
continue
|
||||||
|
|
||||||
|
m = doc_comment_prefix.match(line)
|
||||||
|
if m:
|
||||||
|
current_block_lines.append(m.group(1))
|
||||||
|
|
||||||
|
if "Program" not in rule_defs:
|
||||||
|
raise SystemExit("Root rule 'Program' not found in EBNF definitions")
|
||||||
|
|
||||||
|
visited = set()
|
||||||
|
order = []
|
||||||
|
queue = deque(["Program"])
|
||||||
|
|
||||||
|
while queue:
|
||||||
|
rule = queue.popleft()
|
||||||
|
if rule not in visited and rule in rule_defs:
|
||||||
|
visited.add(rule)
|
||||||
|
order.append(rule)
|
||||||
|
for dep in sorted(rule_deps[rule]):
|
||||||
|
if dep not in visited:
|
||||||
|
queue.append(dep)
|
||||||
|
|
||||||
|
unused_rules = sorted(set(rule_defs.keys()) - visited)
|
||||||
|
|
||||||
|
if len(unused_rules) > 0:
|
||||||
|
print(
|
||||||
|
f"Appending {len(unused_rules)} unused rules to the end: {', '.join(unused_rules)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
order.extend(unused_rules)
|
||||||
|
|
||||||
|
with open("grammar.md", "w", encoding="utf-8") as out:
|
||||||
|
out.write("# Grammar of the Shulkerscript language\n\n")
|
||||||
|
|
||||||
|
for rule in order:
|
||||||
|
out.write(f"## {rule}\n\n```ebnf\n{rule_defs[rule]}\n```\n\n")
|
||||||
|
|
||||||
|
print(f"Wrote grammar.md with {len(order)} rules.")
|
||||||
|
added_rules = set(rule_defs.keys()) - previous_rules
|
||||||
|
if len(added_rules) > 0:
|
||||||
|
print(f"Added rules for: {', '.join(added_rules)}")
|
||||||
|
removed_rules = previous_rules - set(rule_defs.keys())
|
||||||
|
if len(removed_rules) > 0:
|
||||||
|
print(f"Removed rules for: {', '.join(removed_rules)}")
|
||||||
|
|
@ -1,19 +1,26 @@
|
||||||
/// An error that occurred during compilation.
|
/// An error that occurred during compilation.
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error, Clone, PartialEq)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("An error occurred while working with Input/Output.")]
|
#[error("FileProviderError: {0}")]
|
||||||
IoError(String),
|
FileProviderError(#[from] super::FileProviderError),
|
||||||
#[error("An error occurred while lexing the source code.")]
|
#[error(transparent)]
|
||||||
LexicalError(#[from] crate::lexical::Error),
|
LexicalError(#[from] crate::lexical::Error),
|
||||||
#[error("An error occured while tokenizing the source code.")]
|
|
||||||
TokenizeError(#[from] crate::lexical::token::TokenizeError),
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
ParseError(#[from] crate::syntax::error::Error),
|
ParseError(#[from] Box<crate::syntax::error::Error>),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
TranspileError(#[from] crate::transpile::TranspileError),
|
SemanticError(#[from] crate::semantic::error::Error),
|
||||||
#[error("An error occurred")]
|
#[error(transparent)]
|
||||||
Other(&'static str),
|
TranspileError(#[from] Box<crate::transpile::TranspileError>),
|
||||||
|
#[error("An error occurred: {0}")]
|
||||||
|
Other(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error {
|
||||||
|
/// Creates a new error from a string.
|
||||||
|
pub fn other<S: Into<String>>(error: S) -> Self {
|
||||||
|
Self::Other(error.into())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A specialized [`Result`] type for this crate.
|
/// A specialized [`Result`] type for this crate.
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,35 @@
|
||||||
use std::path::{Path, PathBuf};
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
use super::Error;
|
fmt::Display,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
/// A trait for providing file contents.
|
/// A trait for providing file contents.
|
||||||
pub trait FileProvider {
|
pub trait FileProvider {
|
||||||
|
/// Reads the contents of the file at the given path as bytes.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// - If an error occurs while reading the file.
|
||||||
|
/// - If the file does not exist.
|
||||||
|
fn read_bytes<P: AsRef<Path>>(&self, path: P) -> Result<Cow<'_, [u8]>, Error>;
|
||||||
|
|
||||||
/// Reads the contents of the file at the given path.
|
/// Reads the contents of the file at the given path.
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// - If an error occurs while reading the file.
|
/// - If an error occurs while reading the file.
|
||||||
/// - If the file does not exist.
|
/// - If the file does not exist.
|
||||||
fn read_to_string<P: AsRef<Path>>(&self, path: P) -> Result<String, Error>;
|
/// - If the file is not valid UTF-8.
|
||||||
|
fn read_str<P: AsRef<Path>>(&self, path: P) -> Result<Cow<'_, str>, Error> {
|
||||||
|
let bytes = self.read_bytes(path)?;
|
||||||
|
let string = std::str::from_utf8(&bytes)
|
||||||
|
.map_err(|err| {
|
||||||
|
let arc: Arc<dyn std::error::Error + Send + Sync> = Arc::new(err);
|
||||||
|
Error::other(arc)
|
||||||
|
})?
|
||||||
|
.to_string();
|
||||||
|
Ok(Cow::Owned(string))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Provides file contents from the file system.
|
/// Provides file contents from the file system.
|
||||||
|
|
@ -37,28 +57,155 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileProvider for FsProvider {
|
impl FileProvider for FsProvider {
|
||||||
fn read_to_string<P: AsRef<Path>>(&self, path: P) -> Result<String, Error> {
|
fn read_bytes<P: AsRef<Path>>(&self, path: P) -> Result<Cow<'_, [u8]>, Error> {
|
||||||
let full_path = self.root.join(path);
|
let full_path = self.root.join(path);
|
||||||
std::fs::read_to_string(full_path).map_err(|err| Error::IoError(err.to_string()))
|
std::fs::read(full_path)
|
||||||
|
.map(Cow::Owned)
|
||||||
|
.map_err(Error::from)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_str<P: AsRef<Path>>(&self, path: P) -> Result<Cow<'_, str>, Error> {
|
||||||
|
let full_path = self.root.join(path);
|
||||||
|
std::fs::read_to_string(full_path)
|
||||||
|
.map(Cow::Owned)
|
||||||
|
.map_err(Error::from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The error type for [`FileProvider`] operations.
|
||||||
|
#[allow(clippy::module_name_repetitions)]
|
||||||
|
#[derive(Debug, Clone, thiserror::Error)]
|
||||||
|
pub struct Error {
|
||||||
|
kind: std::io::ErrorKind,
|
||||||
|
#[source]
|
||||||
|
error: Option<Arc<dyn std::error::Error + Send + Sync>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error {
|
||||||
|
/// Creates a new [`Error`] from a known kind of error as well as an
|
||||||
|
/// arbitrary error payload.
|
||||||
|
///
|
||||||
|
/// The `error` argument is an arbitrary
|
||||||
|
/// payload which will be contained in this [`Error`].
|
||||||
|
///
|
||||||
|
/// Note that this function allocates memory on the heap.
|
||||||
|
/// If no extra payload is required, use the `From` conversion from
|
||||||
|
/// `ErrorKind`.
|
||||||
|
pub fn new<E>(kind: std::io::ErrorKind, error: E) -> Self
|
||||||
|
where
|
||||||
|
E: Into<Arc<dyn std::error::Error + Send + Sync>>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
kind,
|
||||||
|
error: Some(error.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new [`Error`] from an arbitrary error payload.
|
||||||
|
///
|
||||||
|
/// It is a shortcut for [`Error::new`]
|
||||||
|
/// with [`std::io::ErrorKind::Other`].
|
||||||
|
pub fn other<E>(error: E) -> Self
|
||||||
|
where
|
||||||
|
E: Into<Arc<dyn std::error::Error + Send + Sync>>,
|
||||||
|
{
|
||||||
|
Self::new(std::io::ErrorKind::Other, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a reference to the inner error wrapped by this error (if any).
|
||||||
|
///
|
||||||
|
/// If this [`Error`] was constructed via [`Self::new`] then this function will
|
||||||
|
/// return [`Some`], otherwise it will return [`None`].
|
||||||
|
#[must_use]
|
||||||
|
pub fn get_ref(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)> {
|
||||||
|
self.error.as_deref()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Consumes the [`Error`], returning its inner error (if any).
|
||||||
|
///
|
||||||
|
/// If this [`Error`] was constructed via [`Self::new`] then this function will
|
||||||
|
/// return [`Some`], otherwise it will return [`None`].
|
||||||
|
#[must_use]
|
||||||
|
pub fn into_inner(self) -> Option<Arc<dyn std::error::Error + Send + Sync>> {
|
||||||
|
self.error
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the corresponding [`std::io::ErrorKind`] for this error.
|
||||||
|
#[must_use]
|
||||||
|
pub fn kind(&self) -> std::io::ErrorKind {
|
||||||
|
self.kind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Error {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match &self.error {
|
||||||
|
Some(err) => write!(f, "{}: {}", self.kind, err),
|
||||||
|
None => write!(f, "{}", self.kind),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Error {
|
||||||
|
fn eq(&self, _: &Self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::io::ErrorKind> for Error {
|
||||||
|
fn from(value: std::io::ErrorKind) -> Self {
|
||||||
|
Self {
|
||||||
|
kind: value,
|
||||||
|
error: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::io::Error> for Error {
|
||||||
|
fn from(value: std::io::Error) -> Self {
|
||||||
|
let kind = value.kind();
|
||||||
|
let error = value.into_inner().map(Arc::from);
|
||||||
|
|
||||||
|
Self { kind, error }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "shulkerbox")]
|
#[cfg(feature = "shulkerbox")]
|
||||||
mod vfs {
|
mod vfs {
|
||||||
|
use std::{borrow::Cow, sync::Arc};
|
||||||
|
|
||||||
use super::{Error, FileProvider, Path};
|
use super::{Error, FileProvider, Path};
|
||||||
use shulkerbox::virtual_fs::{VFile, VFolder};
|
use shulkerbox::virtual_fs::{VFile, VFolder};
|
||||||
|
|
||||||
impl FileProvider for VFolder {
|
impl FileProvider for VFolder {
|
||||||
fn read_to_string<P: AsRef<Path>>(&self, path: P) -> Result<String, Error> {
|
fn read_bytes<P: AsRef<Path>>(&self, path: P) -> Result<Cow<'_, [u8]>, Error> {
|
||||||
normalize_path_str(path).map_or_else(
|
normalize_path_str(path).map_or_else(
|
||||||
|| Err(Error::IoError("Invalid path".to_string())),
|
|| Err(Error::from(std::io::ErrorKind::InvalidData)),
|
||||||
|path| {
|
|path| {
|
||||||
self.get_file(&path)
|
self.get_file(&path)
|
||||||
.ok_or_else(|| Error::IoError("File not found".to_string()))
|
.ok_or_else(|| Error::from(std::io::ErrorKind::NotFound))
|
||||||
|
.map(|file| Cow::Borrowed(file.as_bytes()))
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_str<P: AsRef<Path>>(&self, path: P) -> Result<Cow<'_, str>, Error> {
|
||||||
|
normalize_path_str(path).map_or_else(
|
||||||
|
|| Err(Error::from(std::io::ErrorKind::InvalidData)),
|
||||||
|
|path| {
|
||||||
|
self.get_file(&path)
|
||||||
|
.ok_or_else(|| Error::from(std::io::ErrorKind::NotFound))
|
||||||
.and_then(|file| match file {
|
.and_then(|file| match file {
|
||||||
VFile::Text(text) => Ok(text.to_owned()),
|
VFile::Text(text) => Ok(Cow::Borrowed(text.as_str())),
|
||||||
VFile::Binary(bin) => String::from_utf8(bin.clone())
|
VFile::Binary(bin) => {
|
||||||
.map_err(|err| Error::IoError(err.to_string())),
|
let string = std::str::from_utf8(bin).map_err(|err| {
|
||||||
|
let arc: Arc<dyn std::error::Error + Send + Sync> =
|
||||||
|
Arc::new(err);
|
||||||
|
Error::new(std::io::ErrorKind::InvalidData, arc)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(Cow::Borrowed(string))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
@ -112,15 +259,15 @@ mod vfs {
|
||||||
dir.add_file("foo.txt", VFile::Text("foo".to_string()));
|
dir.add_file("foo.txt", VFile::Text("foo".to_string()));
|
||||||
dir.add_file("bar/baz.txt", VFile::Text("bar, baz".to_string()));
|
dir.add_file("bar/baz.txt", VFile::Text("bar, baz".to_string()));
|
||||||
|
|
||||||
assert_eq!(dir.read_to_string("foo.txt").unwrap(), "foo".to_string());
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
dir.read_to_string("bar/baz.txt").unwrap(),
|
dir.read_str("foo.txt").unwrap().into_owned(),
|
||||||
|
"foo".to_string()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
dir.read_str("bar/baz.txt").unwrap().into_owned(),
|
||||||
"bar, baz".to_string()
|
"bar, baz".to_string()
|
||||||
);
|
);
|
||||||
assert!(matches!(
|
assert!(dir.read_str("nonexistent.txt").is_err());
|
||||||
dir.read_to_string("nonexistent.txt"),
|
|
||||||
Err(Error::IoError(_))
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ impl<'a, T> SourceCodeDisplay<'a, T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: Display> Display for SourceCodeDisplay<'a, T> {
|
impl<T: Display> Display for SourceCodeDisplay<'_, T> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let start_location = self.span.start_location();
|
let start_location = self.span.start_location();
|
||||||
let end_location = self.span.end_location();
|
let end_location = self.span.end_location();
|
||||||
|
|
@ -291,7 +291,7 @@ fn write_error_line(
|
||||||
(line_number == start_line && index >= start_location.column)
|
(line_number == start_line && index >= start_location.column)
|
||||||
|| (line_number == end_line
|
|| (line_number == end_line
|
||||||
&& (index + 1)
|
&& (index + 1)
|
||||||
< end_location
|
<= end_location
|
||||||
.map_or(usize::MAX, |end_location| end_location.column))
|
.map_or(usize::MAX, |end_location| end_location.column))
|
||||||
|| (line_number > start_line && line_number < end_line)
|
|| (line_number > start_line && line_number < end_line)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
//! The base module contains the core functionality of the `ShulkerScript` language.
|
//! The base module contains the core functionality of the `Shulkerscript` language.
|
||||||
|
|
||||||
pub mod source_file;
|
pub mod source_file;
|
||||||
|
|
||||||
|
|
@ -10,6 +10,6 @@ mod diagnostic;
|
||||||
pub use diagnostic::{Handler, PrintHandler, SilentHandler, VoidHandler};
|
pub use diagnostic::{Handler, PrintHandler, SilentHandler, VoidHandler};
|
||||||
|
|
||||||
mod file_provider;
|
mod file_provider;
|
||||||
pub use file_provider::{FileProvider, FsProvider};
|
pub use file_provider::{Error as FileProviderError, FileProvider, FsProvider};
|
||||||
|
|
||||||
pub mod log;
|
pub mod log;
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
cmp::Ordering,
|
cmp::Ordering,
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
iter::{Iterator, Peekable},
|
iter::Iterator,
|
||||||
ops::Range,
|
ops::Range,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
str::CharIndices,
|
str::CharIndices,
|
||||||
|
|
@ -11,6 +11,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use getset::{CopyGetters, Getters};
|
use getset::{CopyGetters, Getters};
|
||||||
|
use itertools::{structs::MultiPeek, Itertools as _};
|
||||||
|
|
||||||
use super::{file_provider::FileProvider, Error};
|
use super::{file_provider::FileProvider, Error};
|
||||||
|
|
||||||
|
|
@ -69,11 +70,12 @@ impl SourceFile {
|
||||||
|
|
||||||
/// Get the [`SourceIterator`] for the source file.
|
/// Get the [`SourceIterator`] for the source file.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn iter<'a>(self: &'a Arc<Self>) -> SourceIterator<'a> {
|
pub fn iter(self: &Arc<Self>) -> SourceIterator<'_> {
|
||||||
SourceIterator {
|
SourceIterator {
|
||||||
source_file: self,
|
source_file: self,
|
||||||
iterator: self.content().char_indices().peekable(),
|
iterator: self.content().char_indices().multipeek(),
|
||||||
prev: None,
|
prev: None,
|
||||||
|
in_template_string_expression_open_count: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,14 +88,18 @@ impl SourceFile {
|
||||||
/// Load the source file from the given file path.
|
/// Load the source file from the given file path.
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// - [`Error::IoError`]: Error occurred when reading the file contents.
|
/// - [`Error::FileProviderError`]: Error occurred when reading the file contents.
|
||||||
pub fn load(
|
pub fn load(
|
||||||
path: &Path,
|
path: &Path,
|
||||||
identifier: String,
|
identifier: String,
|
||||||
provider: &impl FileProvider,
|
provider: &impl FileProvider,
|
||||||
) -> Result<Arc<Self>, Error> {
|
) -> Result<Arc<Self>, Error> {
|
||||||
let source = provider.read_to_string(path)?;
|
let source = provider.read_str(path)?;
|
||||||
Ok(Self::new(path.to_path_buf(), identifier, source))
|
Ok(Self::new(
|
||||||
|
path.to_path_buf(),
|
||||||
|
identifier,
|
||||||
|
source.into_owned(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the [`Location`] of a given byte index
|
/// Get the [`Location`] of a given byte index
|
||||||
|
|
@ -132,11 +138,17 @@ impl SourceFile {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the relative path of the source file from the current working directory.
|
||||||
|
#[must_use]
|
||||||
|
pub fn path_relative(&self) -> Option<PathBuf> {
|
||||||
|
pathdiff::diff_paths(&self.path, std::env::current_dir().ok()?)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents a range of characters in a source file.
|
/// Represents a range of characters in a source file.
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
|
||||||
#[derive(Clone, Getters, CopyGetters)]
|
#[derive(Clone, Getters, CopyGetters)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct Span {
|
pub struct Span {
|
||||||
/// Get the start byte index of the span.
|
/// Get the start byte index of the span.
|
||||||
#[get_copy = "pub"]
|
#[get_copy = "pub"]
|
||||||
|
|
@ -148,6 +160,7 @@ pub struct Span {
|
||||||
|
|
||||||
/// Get the source file that the span is located in.
|
/// Get the source file that the span is located in.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
|
#[cfg_attr(feature = "serde", serde(with = "crate::serde::source_file"))]
|
||||||
source_file: Arc<SourceFile>,
|
source_file: Arc<SourceFile>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -206,6 +219,12 @@ impl std::hash::Hash for Span {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl SourceElement for Span {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Span {
|
impl Span {
|
||||||
/// Create a span from the given start and end byte indices in the source file.
|
/// Create a span from the given start and end byte indices in the source file.
|
||||||
///
|
///
|
||||||
|
|
@ -243,6 +262,26 @@ impl Span {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a span from the given start byte index to the end of the source file with an offset.
|
||||||
|
#[must_use]
|
||||||
|
pub fn to_end_with_offset(
|
||||||
|
source_file: Arc<SourceFile>,
|
||||||
|
start: usize,
|
||||||
|
end_offset: isize,
|
||||||
|
) -> Option<Self> {
|
||||||
|
if !source_file.content().is_char_boundary(start) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(Self {
|
||||||
|
start,
|
||||||
|
end: source_file
|
||||||
|
.content()
|
||||||
|
.len()
|
||||||
|
.saturating_add_signed(end_offset),
|
||||||
|
source_file,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the string slice of the source code that the span represents.
|
/// Get the string slice of the source code that the span represents.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn str(&self) -> &str {
|
pub fn str(&self) -> &str {
|
||||||
|
|
@ -306,18 +345,71 @@ pub struct SourceIterator<'a> {
|
||||||
/// Get the source file that the iterator is iterating over.
|
/// Get the source file that the iterator is iterating over.
|
||||||
#[get_copy = "pub"]
|
#[get_copy = "pub"]
|
||||||
source_file: &'a Arc<SourceFile>,
|
source_file: &'a Arc<SourceFile>,
|
||||||
iterator: Peekable<CharIndices<'a>>,
|
iterator: MultiPeek<CharIndices<'a>>,
|
||||||
/// Get the previous character that was iterated over.
|
/// Get the previous character that was iterated over.
|
||||||
#[get_copy = "pub"]
|
#[get_copy = "pub"]
|
||||||
prev: Option<(usize, char)>,
|
prev: Option<(usize, char)>,
|
||||||
|
/// Current state for parsing template strings.
|
||||||
|
in_template_string_expression_open_count: Vec<u32>,
|
||||||
}
|
}
|
||||||
impl<'a> SourceIterator<'a> {
|
impl SourceIterator<'_> {
|
||||||
/// Peek at the next character in the source file.
|
/// Peek at the next character in the source file.
|
||||||
pub fn peek(&mut self) -> Option<(usize, char)> {
|
pub fn peek(&mut self) -> Option<(usize, char)> {
|
||||||
|
self.iterator.reset_peek();
|
||||||
self.iterator.peek().copied()
|
self.iterator.peek().copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Peek at the next character in the source file.
|
||||||
|
pub fn multipeek(&mut self) -> Option<(usize, char)> {
|
||||||
|
self.iterator.peek().copied()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reset the multipeek state of the iterator.
|
||||||
|
pub fn reset_multipeek(&mut self) {
|
||||||
|
self.iterator.reset_peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Increase the count of open parentheses in the current template string expression.
|
||||||
|
pub fn increase_template_string_expression_open_paren_count(&mut self) {
|
||||||
|
if let Some(count) = self.in_template_string_expression_open_count.last_mut() {
|
||||||
|
*count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Decrease the count of open parentheses in the current template string expression.
|
||||||
|
pub fn decrease_template_string_expression_open_paren_count(&mut self) {
|
||||||
|
if let Some(count) = self.in_template_string_expression_open_count.last_mut() {
|
||||||
|
*count = count.saturating_sub(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Enter a template string expression.
|
||||||
|
pub fn enter_template_string(&mut self) {
|
||||||
|
self.in_template_string_expression_open_count.push(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Exit a template string expression.
|
||||||
|
pub fn exit_template_string(&mut self) {
|
||||||
|
self.in_template_string_expression_open_count.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if the iterator is currently in a template string expression.
|
||||||
|
#[must_use]
|
||||||
|
pub fn is_in_template_string_expression(&self) -> Option<bool> {
|
||||||
|
self.in_template_string_expression_open_count
|
||||||
|
.last()
|
||||||
|
.map(|&count| count > 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the number of open parentheses in the current template string expression.
|
||||||
|
#[must_use]
|
||||||
|
pub fn template_string_expression_open_paren_count(&self) -> Option<u32> {
|
||||||
|
self.in_template_string_expression_open_count
|
||||||
|
.last()
|
||||||
|
.copied()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl<'a> Iterator for SourceIterator<'a> {
|
impl Iterator for SourceIterator<'_> {
|
||||||
type Item = (usize, char);
|
type Item = (usize, char);
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
|
|
||||||
|
|
@ -7,16 +7,18 @@ use crate::base::{
|
||||||
source_file::Span,
|
source_file::Span,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::token_stream::Delimiter;
|
use super::{token, token_stream::Delimiter};
|
||||||
|
|
||||||
/// Represents an error that occurred during the lexical analysis of the source code.
|
/// Represents an error that occurred during the lexical analysis of the source code.
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, thiserror::Error)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, thiserror::Error)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("Comment is not terminated.")]
|
#[error(transparent)]
|
||||||
UnterminatedDelimitedComment(#[from] UnterminatedDelimitedComment),
|
UnterminatedDelimitedComment(#[from] UnterminatedDelimitedComment),
|
||||||
#[error("Delimiter is not terminated.")]
|
#[error(transparent)]
|
||||||
UndelimitedDelimiter(#[from] UndelimitedDelimiter),
|
UndelimitedDelimiter(#[from] UndelimitedDelimiter),
|
||||||
|
#[error("Tokenize error: {0}")]
|
||||||
|
TokenizeError(#[from] token::TokenizeError),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Source code contains an unclosed `/*` comment.
|
/// Source code contains an unclosed `/*` comment.
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,12 @@
|
||||||
//! Contains the [`Token`] struct and its related types.
|
//! Contains the [`Token`] struct and its related types.
|
||||||
|
|
||||||
use std::{borrow::Cow, collections::HashMap, fmt::Display, str::FromStr, sync::OnceLock};
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
|
collections::HashMap,
|
||||||
|
fmt::{Debug, Display},
|
||||||
|
str::FromStr,
|
||||||
|
sync::OnceLock,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::base::{
|
use crate::base::{
|
||||||
self,
|
self,
|
||||||
|
|
@ -9,8 +15,7 @@ use crate::base::{
|
||||||
};
|
};
|
||||||
use derive_more::From;
|
use derive_more::From;
|
||||||
use enum_as_inner::EnumAsInner;
|
use enum_as_inner::EnumAsInner;
|
||||||
use strum::IntoEnumIterator;
|
use strum::{EnumIter, IntoEnumIterator};
|
||||||
use strum_macros::EnumIter;
|
|
||||||
|
|
||||||
use super::error::{self, UnterminatedDelimitedComment};
|
use super::error::{self, UnterminatedDelimitedComment};
|
||||||
|
|
||||||
|
|
@ -41,6 +46,15 @@ pub enum KeywordKind {
|
||||||
Namespace,
|
Namespace,
|
||||||
From,
|
From,
|
||||||
Import,
|
Import,
|
||||||
|
Tag,
|
||||||
|
Replace,
|
||||||
|
Int,
|
||||||
|
Bool,
|
||||||
|
Macro,
|
||||||
|
Val,
|
||||||
|
Return,
|
||||||
|
While,
|
||||||
|
For,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for KeywordKind {
|
impl Display for KeywordKind {
|
||||||
|
|
@ -101,6 +115,15 @@ impl KeywordKind {
|
||||||
Self::Namespace => "namespace",
|
Self::Namespace => "namespace",
|
||||||
Self::From => "from",
|
Self::From => "from",
|
||||||
Self::Import => "import",
|
Self::Import => "import",
|
||||||
|
Self::Tag => "tag",
|
||||||
|
Self::Replace => "replace",
|
||||||
|
Self::Int => "int",
|
||||||
|
Self::Bool => "bool",
|
||||||
|
Self::Macro => "macro",
|
||||||
|
Self::Val => "val",
|
||||||
|
Self::Return => "return",
|
||||||
|
Self::While => "while",
|
||||||
|
Self::For => "for",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -135,29 +158,13 @@ pub enum Token {
|
||||||
Identifier(Identifier),
|
Identifier(Identifier),
|
||||||
Keyword(Keyword),
|
Keyword(Keyword),
|
||||||
Punctuation(Punctuation),
|
Punctuation(Punctuation),
|
||||||
Numeric(Numeric),
|
Integer(Integer),
|
||||||
|
Boolean(Boolean),
|
||||||
Comment(Comment),
|
Comment(Comment),
|
||||||
DocComment(DocComment),
|
DocComment(DocComment),
|
||||||
CommandLiteral(CommandLiteral),
|
CommandLiteral(CommandLiteral),
|
||||||
StringLiteral(StringLiteral),
|
StringLiteral(StringLiteral),
|
||||||
}
|
TemplateStringText(TemplateStringLiteralText),
|
||||||
|
|
||||||
impl Token {
|
|
||||||
/// Returns the span of the token.
|
|
||||||
#[must_use]
|
|
||||||
pub fn span(&self) -> &Span {
|
|
||||||
match self {
|
|
||||||
Self::WhiteSpaces(token) => &token.span,
|
|
||||||
Self::Identifier(token) => &token.span,
|
|
||||||
Self::Keyword(token) => &token.span,
|
|
||||||
Self::Punctuation(token) => &token.span,
|
|
||||||
Self::Numeric(token) => &token.span,
|
|
||||||
Self::Comment(token) => &token.span,
|
|
||||||
Self::DocComment(token) => &token.span,
|
|
||||||
Self::CommandLiteral(token) => &token.span,
|
|
||||||
Self::StringLiteral(token) => &token.span,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SourceElement for Token {
|
impl SourceElement for Token {
|
||||||
|
|
@ -167,11 +174,13 @@ impl SourceElement for Token {
|
||||||
Self::Identifier(token) => token.span(),
|
Self::Identifier(token) => token.span(),
|
||||||
Self::Keyword(token) => token.span(),
|
Self::Keyword(token) => token.span(),
|
||||||
Self::Punctuation(token) => token.span(),
|
Self::Punctuation(token) => token.span(),
|
||||||
Self::Numeric(token) => token.span(),
|
Self::Integer(token) => token.span(),
|
||||||
|
Self::Boolean(token) => token.span(),
|
||||||
Self::Comment(token) => token.span(),
|
Self::Comment(token) => token.span(),
|
||||||
Self::DocComment(token) => token.span(),
|
Self::DocComment(token) => token.span(),
|
||||||
Self::CommandLiteral(token) => token.span(),
|
Self::CommandLiteral(token) => token.span(),
|
||||||
Self::StringLiteral(token) => token.span(),
|
Self::StringLiteral(token) => token.span(),
|
||||||
|
Self::TemplateStringText(token) => token.span(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -237,21 +246,78 @@ impl SourceElement for Punctuation {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents a hardcoded numeric literal value in the source code.
|
/// Represents a hardcoded numeric integer literal value in the source code.
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct Numeric {
|
pub struct Integer {
|
||||||
/// Is the span that makes up the token.
|
/// Is the span that makes up the token.
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SourceElement for Numeric {
|
impl SourceElement for Integer {
|
||||||
fn span(&self) -> Span {
|
fn span(&self) -> Span {
|
||||||
self.span.clone()
|
self.span.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Integer {
|
||||||
|
/// Returns the integer value of the token.
|
||||||
|
#[must_use]
|
||||||
|
pub fn as_i64(&self) -> i64 {
|
||||||
|
self.span.str().parse().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for Integer {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let mut s = f.debug_struct("Integer");
|
||||||
|
s.field("value", &self.as_i64());
|
||||||
|
s.field("span", &self.span);
|
||||||
|
s.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents a hardcoded boolean literal value in the source code.
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
|
pub struct Boolean {
|
||||||
|
/// Is the span that makes up the token.
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceElement for Boolean {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.span.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Boolean {
|
||||||
|
/// Returns the boolean value of the token.
|
||||||
|
#[must_use]
|
||||||
|
pub fn value(&self) -> bool {
|
||||||
|
match self.span.str() {
|
||||||
|
"true" => true,
|
||||||
|
"false" => false,
|
||||||
|
_ => unreachable!("Invalid boolean literal"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for Boolean {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let mut s = f.debug_struct("Boolean");
|
||||||
|
s.field("value", &self.value());
|
||||||
|
s.field("span", &self.span);
|
||||||
|
s.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Represents a hardcoded string literal value in the source code.
|
/// Represents a hardcoded string literal value in the source code.
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
|
/// StringLiteral:
|
||||||
|
/// '"' TEXT '"';
|
||||||
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct StringLiteral {
|
pub struct StringLiteral {
|
||||||
|
|
@ -262,7 +328,7 @@ pub struct StringLiteral {
|
||||||
impl StringLiteral {
|
impl StringLiteral {
|
||||||
/// Returns the string content without escapement characters, leading and trailing double quotes.
|
/// Returns the string content without escapement characters, leading and trailing double quotes.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn str_content(&self) -> Cow<str> {
|
pub fn str_content(&self) -> Cow<'_, str> {
|
||||||
let string = self.span.str();
|
let string = self.span.str();
|
||||||
let string = &string[1..string.len() - 1];
|
let string = &string[1..string.len() - 1];
|
||||||
if string.contains('\\') {
|
if string.contains('\\') {
|
||||||
|
|
@ -285,6 +351,25 @@ impl SourceElement for StringLiteral {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents a hardcoded template string text value in the source code.
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
|
/// TemplateStringLiteralText:
|
||||||
|
/// TEXT ;
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
|
pub struct TemplateStringLiteralText {
|
||||||
|
/// Is the span that makes up the token.
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceElement for TemplateStringLiteralText {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.span.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Is an enumeration representing the two kinds of comments in the Shulkerscript programming language.
|
/// Is an enumeration representing the two kinds of comments in the Shulkerscript programming language.
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
|
|
@ -357,8 +442,9 @@ impl CommandLiteral {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Is an error that can occur when invoking the [`Token::tokenize`] method.
|
/// Is an error that can occur when invoking the [`Token::tokenize`] method.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, thiserror::Error, From)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, thiserror::Error)]
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
|
#[expect(missing_copy_implementations)]
|
||||||
pub enum TokenizeError {
|
pub enum TokenizeError {
|
||||||
#[error("encountered a fatal lexical error that causes the process to stop.")]
|
#[error("encountered a fatal lexical error that causes the process to stop.")]
|
||||||
FatalLexicalError,
|
FatalLexicalError,
|
||||||
|
|
@ -380,6 +466,7 @@ impl Token {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a span from the given start location to the current location of the iterator.
|
/// Creates a span from the given start location to the current location of the iterator.
|
||||||
|
#[must_use]
|
||||||
fn create_span(start: usize, iter: &mut SourceIterator) -> Span {
|
fn create_span(start: usize, iter: &mut SourceIterator) -> Span {
|
||||||
iter.peek().map_or_else(
|
iter.peek().map_or_else(
|
||||||
|| Span::to_end(iter.source_file().clone(), start).unwrap(),
|
|| Span::to_end(iter.source_file().clone(), start).unwrap(),
|
||||||
|
|
@ -422,16 +509,13 @@ impl Token {
|
||||||
let word = span.str();
|
let word = span.str();
|
||||||
|
|
||||||
// Checks if the word is a keyword
|
// Checks if the word is a keyword
|
||||||
KeywordKind::from_str(word).ok().map_or_else(
|
if let Ok(kw) = KeywordKind::from_str(word) {
|
||||||
|| Identifier { span: span.clone() }.into(),
|
Keyword { span, keyword: kw }.into()
|
||||||
|kw| {
|
} else if bool::from_str(word).is_ok() {
|
||||||
Keyword {
|
Boolean { span }.into()
|
||||||
span: span.clone(),
|
} else {
|
||||||
keyword: kw,
|
Identifier { span }.into()
|
||||||
}
|
}
|
||||||
.into()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handles a sequence starting with a slash
|
/// Handles a sequence starting with a slash
|
||||||
|
|
@ -455,17 +539,8 @@ impl Token {
|
||||||
|
|
||||||
Self::walk_iter(iter, |character| !(character == '\n' || character == '\r'));
|
Self::walk_iter(iter, |character| !(character == '\n' || character == '\r'));
|
||||||
|
|
||||||
let is_cr = iter
|
|
||||||
.peek()
|
|
||||||
.map_or(false, |(_, character)| character == '\r');
|
|
||||||
|
|
||||||
let span = Self::create_span(start, iter);
|
let span = Self::create_span(start, iter);
|
||||||
|
|
||||||
if let (true, Some((_, '\n'))) = (is_cr, iter.next()) {
|
|
||||||
// skips the crlf
|
|
||||||
iter.next();
|
|
||||||
}
|
|
||||||
|
|
||||||
let comment = if is_doccomment {
|
let comment = if is_doccomment {
|
||||||
DocComment { span }.into()
|
DocComment { span }.into()
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -507,11 +582,11 @@ impl Token {
|
||||||
handler.receive(error::Error::from(UnterminatedDelimitedComment {
|
handler.receive(error::Error::from(UnterminatedDelimitedComment {
|
||||||
span: Span::new(iter.source_file().clone(), start, start + 2).unwrap(),
|
span: Span::new(iter.source_file().clone(), start, start + 2).unwrap(),
|
||||||
}));
|
}));
|
||||||
return Err(TokenizeError::FatalLexicalError);
|
Err(TokenizeError::FatalLexicalError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// When there is no second slash and at the start of a line
|
// When there is no second slash and at the start of a line
|
||||||
else if prev_token.map_or(true, |token| token.span().str().contains('\n')) {
|
else if prev_token.is_none_or(|token| token.span().str().contains('\n')) {
|
||||||
Ok(Self::handle_command_literal(iter, start))
|
Ok(Self::handle_command_literal(iter, start))
|
||||||
}
|
}
|
||||||
// Just a single slash punctuation
|
// Just a single slash punctuation
|
||||||
|
|
@ -525,11 +600,11 @@ impl Token {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handles a sequence of digits
|
/// Handles a sequence of digits
|
||||||
fn handle_numeric_literal(iter: &mut SourceIterator, start: usize) -> Self {
|
fn handle_integer_literal(iter: &mut SourceIterator, start: usize) -> Self {
|
||||||
// Tokenizes the whole number part
|
// Tokenizes the whole number part
|
||||||
Self::walk_iter(iter, |character| character.is_ascii_digit());
|
Self::walk_iter(iter, |character| character.is_ascii_digit());
|
||||||
|
|
||||||
Numeric {
|
Integer {
|
||||||
span: Self::create_span(start, iter),
|
span: Self::create_span(start, iter),
|
||||||
}
|
}
|
||||||
.into()
|
.into()
|
||||||
|
|
@ -555,6 +630,87 @@ impl Token {
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handles a backticks for opening and closing template strings
|
||||||
|
fn handle_template_string_quotes(iter: &mut SourceIterator, start: usize) -> Self {
|
||||||
|
if iter
|
||||||
|
.is_in_template_string_expression()
|
||||||
|
.is_some_and(|last| !last)
|
||||||
|
{
|
||||||
|
// in template string text
|
||||||
|
iter.exit_template_string();
|
||||||
|
} else {
|
||||||
|
// outside template string or in expression
|
||||||
|
iter.enter_template_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
Punctuation {
|
||||||
|
span: Self::create_span(start, iter),
|
||||||
|
punctuation: '`',
|
||||||
|
}
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_template_string_inner(
|
||||||
|
iter: &mut SourceIterator,
|
||||||
|
start: usize,
|
||||||
|
character: char,
|
||||||
|
prev_token: Option<&Self>,
|
||||||
|
) -> Self {
|
||||||
|
let prev_was_backslash = iter.prev().is_some_and(|(_, c)| c == '\\');
|
||||||
|
|
||||||
|
if !prev_was_backslash && character == '$' && iter.peek().is_some_and(|(_, c)| c == '(') {
|
||||||
|
// starts immediately with expression, return punctuation
|
||||||
|
return Punctuation {
|
||||||
|
span: Self::create_span(start, iter),
|
||||||
|
punctuation: '$',
|
||||||
|
}
|
||||||
|
.into();
|
||||||
|
}
|
||||||
|
|
||||||
|
match (character, prev_token) {
|
||||||
|
('(', Some(Self::Punctuation(punc)))
|
||||||
|
if !prev_was_backslash && punc.punctuation == '$' =>
|
||||||
|
{
|
||||||
|
// Found expression opening parenthesis
|
||||||
|
iter.increase_template_string_expression_open_paren_count();
|
||||||
|
|
||||||
|
return Punctuation {
|
||||||
|
span: Self::create_span(start, iter),
|
||||||
|
punctuation: '(',
|
||||||
|
}
|
||||||
|
.into();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if character != '`' {
|
||||||
|
iter.reset_multipeek();
|
||||||
|
Self::walk_iter(iter, |c| !matches!(c, '$' | '`' | '\\'));
|
||||||
|
}
|
||||||
|
|
||||||
|
iter.reset_multipeek();
|
||||||
|
let first_peek = iter.multipeek().map(|(_, c)| c);
|
||||||
|
let second_peek_open_paren = iter.multipeek().is_some_and(|(_, c)| c == '(');
|
||||||
|
|
||||||
|
if first_peek.is_some_and(|c| c == '\\') {
|
||||||
|
iter.next();
|
||||||
|
iter.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
if character == '`' || first_peek.is_none_or(|c| c == '`') || second_peek_open_paren {
|
||||||
|
// Found expression start, end of text
|
||||||
|
|
||||||
|
break TemplateStringLiteralText {
|
||||||
|
span: Self::create_span(start, iter),
|
||||||
|
}
|
||||||
|
.into();
|
||||||
|
}
|
||||||
|
|
||||||
|
iter.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Handles a command that is preceeded by a slash
|
/// Handles a command that is preceeded by a slash
|
||||||
fn handle_command_literal(iter: &mut SourceIterator, start: usize) -> Self {
|
fn handle_command_literal(iter: &mut SourceIterator, start: usize) -> Self {
|
||||||
Self::walk_iter(iter, |c| !(c.is_whitespace() && c.is_ascii_control()));
|
Self::walk_iter(iter, |c| !(c.is_whitespace() && c.is_ascii_control()));
|
||||||
|
|
@ -585,8 +741,13 @@ impl Token {
|
||||||
.next()
|
.next()
|
||||||
.ok_or(TokenizeError::EndOfSourceCodeIteratorArgument)?;
|
.ok_or(TokenizeError::EndOfSourceCodeIteratorArgument)?;
|
||||||
|
|
||||||
|
if iter.is_in_template_string_expression().is_some_and(|b| !b) && character != '`' {
|
||||||
|
Ok(Self::handle_template_string_inner(
|
||||||
|
iter, start, character, prev_token,
|
||||||
|
))
|
||||||
|
}
|
||||||
// Found white spaces
|
// Found white spaces
|
||||||
if character.is_whitespace() {
|
else if character.is_whitespace() {
|
||||||
Ok(Self::handle_whitespace(iter, start))
|
Ok(Self::handle_whitespace(iter, start))
|
||||||
}
|
}
|
||||||
// Found identifier/keyword
|
// Found identifier/keyword
|
||||||
|
|
@ -596,15 +757,27 @@ impl Token {
|
||||||
// Found comment/single slash punctuation
|
// Found comment/single slash punctuation
|
||||||
else if character == '/' {
|
else if character == '/' {
|
||||||
Self::handle_comment(iter, start, character, prev_token, handler)
|
Self::handle_comment(iter, start, character, prev_token, handler)
|
||||||
} else if character == '"' {
|
}
|
||||||
|
// Found string literal
|
||||||
|
else if character == '"' {
|
||||||
Ok(Self::handle_string_literal(iter, start))
|
Ok(Self::handle_string_literal(iter, start))
|
||||||
}
|
}
|
||||||
// Found numeric literal
|
// Found macro string literal
|
||||||
|
else if character == '`' {
|
||||||
|
Ok(Self::handle_template_string_quotes(iter, start))
|
||||||
|
}
|
||||||
|
// Found integer literal
|
||||||
else if character.is_ascii_digit() {
|
else if character.is_ascii_digit() {
|
||||||
Ok(Self::handle_numeric_literal(iter, start))
|
Ok(Self::handle_integer_literal(iter, start))
|
||||||
}
|
}
|
||||||
// Found a punctuation
|
// Found a punctuation
|
||||||
else if character.is_ascii_punctuation() {
|
else if character.is_ascii_punctuation() {
|
||||||
|
if character == '(' {
|
||||||
|
iter.increase_template_string_expression_open_paren_count();
|
||||||
|
} else if character == ')' {
|
||||||
|
iter.decrease_template_string_expression_open_paren_count();
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Punctuation {
|
Ok(Punctuation {
|
||||||
span: Self::create_span(start, iter),
|
span: Self::create_span(start, iter),
|
||||||
punctuation: character,
|
punctuation: character,
|
||||||
|
|
@ -616,7 +789,7 @@ impl Token {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(all(test, feature = "shulkerbox"))]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::base::source_file::SourceFile;
|
use crate::base::source_file::SourceFile;
|
||||||
use shulkerbox::virtual_fs::{VFile, VFolder};
|
use shulkerbox::virtual_fs::{VFile, VFolder};
|
||||||
|
|
|
||||||
|
|
@ -184,7 +184,7 @@ pub enum TokenTree {
|
||||||
impl SourceElement for TokenTree {
|
impl SourceElement for TokenTree {
|
||||||
fn span(&self) -> Span {
|
fn span(&self) -> Span {
|
||||||
match self {
|
match self {
|
||||||
Self::Token(token) => token.span().to_owned(),
|
Self::Token(token) => token.span(),
|
||||||
Self::Delimited(delimited) => delimited
|
Self::Delimited(delimited) => delimited
|
||||||
.open
|
.open
|
||||||
.span()
|
.span()
|
||||||
|
|
|
||||||
61
src/lib.rs
61
src/lib.rs
|
|
@ -1,6 +1,6 @@
|
||||||
//! The `ShulkerScript` language.
|
//! The `Shulkerscript` language.
|
||||||
//!
|
//!
|
||||||
//! `ShulkerScript` is a simple, imperative scripting language for creating Minecraft data packs.
|
//! `Shulkerscript` is a simple, imperative scripting language for creating Minecraft data packs.
|
||||||
|
|
||||||
#![deny(
|
#![deny(
|
||||||
missing_debug_implementations,
|
missing_debug_implementations,
|
||||||
|
|
@ -12,12 +12,21 @@
|
||||||
#![warn(missing_docs, clippy::all, clippy::pedantic)]
|
#![warn(missing_docs, clippy::all, clippy::pedantic)]
|
||||||
#![allow(clippy::missing_panics_doc, clippy::missing_const_for_fn)]
|
#![allow(clippy::missing_panics_doc, clippy::missing_const_for_fn)]
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
pub use shulkerbox;
|
pub use shulkerbox;
|
||||||
|
|
||||||
pub mod base;
|
pub mod base;
|
||||||
pub mod lexical;
|
pub mod lexical;
|
||||||
|
pub mod semantic;
|
||||||
pub mod syntax;
|
pub mod syntax;
|
||||||
pub mod transpile;
|
pub mod transpile;
|
||||||
|
pub mod util;
|
||||||
|
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
pub(crate) mod serde;
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
#[cfg_attr(feature = "serde", doc(inline))]
|
||||||
|
pub use serde::SerdeWrapper;
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
|
@ -29,6 +38,11 @@ use shulkerbox::{datapack::Datapack, virtual_fs::VFolder};
|
||||||
|
|
||||||
use crate::lexical::token_stream::TokenStream;
|
use crate::lexical::token_stream::TokenStream;
|
||||||
|
|
||||||
|
/// The version of the `Shulkerscript` language.
|
||||||
|
///
|
||||||
|
/// Matches the version of this [`crate`].
|
||||||
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
/// Converts the given source code to tokens and returns a token stream.
|
/// Converts the given source code to tokens and returns a token stream.
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
|
|
@ -78,7 +92,7 @@ pub fn parse(
|
||||||
let tokens = tokenize(handler, file_provider, path, identifier)?;
|
let tokens = tokenize(handler, file_provider, path, identifier)?;
|
||||||
|
|
||||||
if handler.has_received() {
|
if handler.has_received() {
|
||||||
return Err(Error::Other(
|
return Err(Error::other(
|
||||||
"An error occurred while tokenizing the source code.",
|
"An error occurred while tokenizing the source code.",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
@ -86,24 +100,21 @@ pub fn parse(
|
||||||
tracing::info!("Parsing the source code at path: {}", path.display());
|
tracing::info!("Parsing the source code at path: {}", path.display());
|
||||||
|
|
||||||
let mut parser = Parser::new(&tokens);
|
let mut parser = Parser::new(&tokens);
|
||||||
let program = parser.parse_program(handler).ok_or(Error::Other(
|
let program = parser.parse_program(handler).map_err(Box::new)?;
|
||||||
"An error occurred while parsing the source code.",
|
|
||||||
))?;
|
|
||||||
|
|
||||||
if handler.has_received() {
|
if handler.has_received() {
|
||||||
return Err(Error::Other(
|
return Err(Error::other(
|
||||||
"An error occurred while parsing the source code.",
|
"An error occurred while parsing the source code.",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
program.analyze_semantics(handler)?;
|
||||||
|
|
||||||
Ok(program)
|
Ok(program)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transpiles the given source code into a shulkerbox [`Datapack`].
|
/// Transpiles the given source code into a shulkerbox [`Datapack`].
|
||||||
///
|
///
|
||||||
/// # Parameters:
|
|
||||||
/// - `script_paths`: A list of tuples containing the identifier and the path of each script file.
|
|
||||||
///
|
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// - If an error occurs during [`parse()`]
|
/// - If an error occurs during [`parse()`]
|
||||||
/// - If an error occurs while transpiling the source code.
|
/// - If an error occurs while transpiling the source code.
|
||||||
|
|
@ -116,6 +127,7 @@ pub fn parse(
|
||||||
/// let datapack = transpile(
|
/// let datapack = transpile(
|
||||||
/// &PrintHandler::new(),
|
/// &PrintHandler::new(),
|
||||||
/// &FsProvider::default(),
|
/// &FsProvider::default(),
|
||||||
|
/// "main",
|
||||||
/// 48,
|
/// 48,
|
||||||
/// &[
|
/// &[
|
||||||
/// (String::from("fileA"), Path::new("path/to/fileA.shu")),
|
/// (String::from("fileA"), Path::new("path/to/fileA.shu")),
|
||||||
|
|
@ -127,6 +139,7 @@ pub fn parse(
|
||||||
pub fn transpile<F, P>(
|
pub fn transpile<F, P>(
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
file_provider: &F,
|
file_provider: &F,
|
||||||
|
main_namespace_name: impl Into<String>,
|
||||||
pack_format: u8,
|
pack_format: u8,
|
||||||
script_paths: &[(String, P)],
|
script_paths: &[(String, P)],
|
||||||
) -> Result<Datapack>
|
) -> Result<Datapack>
|
||||||
|
|
@ -148,24 +161,16 @@ where
|
||||||
|
|
||||||
Ok(program)
|
Ok(program)
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Result<Vec<_>>>()?;
|
||||||
|
|
||||||
if programs.iter().any(Result::is_err) {
|
|
||||||
return Err(programs.into_iter().find_map(Result::err).unwrap());
|
|
||||||
}
|
|
||||||
let programs = programs
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(Result::ok)
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
tracing::info!("Transpiling the source code.");
|
tracing::info!("Transpiling the source code.");
|
||||||
|
|
||||||
let mut transpiler = Transpiler::new(pack_format);
|
let datapack = Transpiler::new(main_namespace_name, pack_format)
|
||||||
transpiler.transpile(&programs, handler)?;
|
.transpile(&programs, handler)
|
||||||
let datapack = transpiler.into_datapack();
|
.map_err(Box::new)?;
|
||||||
|
|
||||||
if handler.has_received() {
|
if handler.has_received() {
|
||||||
return Err(Error::Other(
|
return Err(Error::other(
|
||||||
"An error occurred while transpiling the source code.",
|
"An error occurred while transpiling the source code.",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
@ -189,6 +194,7 @@ where
|
||||||
/// let vfolder = compile(
|
/// let vfolder = compile(
|
||||||
/// &PrintHandler::new(),
|
/// &PrintHandler::new(),
|
||||||
/// &FsProvider::default(),
|
/// &FsProvider::default(),
|
||||||
|
/// "main",
|
||||||
/// 48,
|
/// 48,
|
||||||
/// &[
|
/// &[
|
||||||
/// (String::from("fileA"), Path::new("path/to/fileA.shu")),
|
/// (String::from("fileA"), Path::new("path/to/fileA.shu")),
|
||||||
|
|
@ -200,6 +206,7 @@ where
|
||||||
pub fn compile<F, P>(
|
pub fn compile<F, P>(
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
file_provider: &F,
|
file_provider: &F,
|
||||||
|
main_namespace_name: impl Into<String>,
|
||||||
pack_format: u8,
|
pack_format: u8,
|
||||||
script_paths: &[(String, P)],
|
script_paths: &[(String, P)],
|
||||||
) -> Result<VFolder>
|
) -> Result<VFolder>
|
||||||
|
|
@ -209,7 +216,13 @@ where
|
||||||
{
|
{
|
||||||
use shulkerbox::prelude::CompileOptions;
|
use shulkerbox::prelude::CompileOptions;
|
||||||
|
|
||||||
let datapack = transpile(handler, file_provider, pack_format, script_paths)?;
|
let datapack = transpile(
|
||||||
|
handler,
|
||||||
|
file_provider,
|
||||||
|
main_namespace_name,
|
||||||
|
pack_format,
|
||||||
|
script_paths,
|
||||||
|
)?;
|
||||||
|
|
||||||
tracing::info!("Compiling the source code.");
|
tracing::info!("Compiling the source code.");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,256 @@
|
||||||
|
//! Error types for the semantic analysis phase of the compiler.
|
||||||
|
|
||||||
|
#![allow(missing_docs)]
|
||||||
|
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
base::{
|
||||||
|
log::{Message, Severity, SourceCodeDisplay},
|
||||||
|
source_file::{SourceElement as _, Span},
|
||||||
|
},
|
||||||
|
lexical::token::StringLiteral,
|
||||||
|
syntax::syntax_tree::expression::Expression,
|
||||||
|
transpile::error::{AssignmentError, IllegalIndexing, MismatchedTypes, UnknownIdentifier},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, thiserror::Error)]
|
||||||
|
#[allow(missing_docs)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error(transparent)]
|
||||||
|
UnexpectedExpression(#[from] UnexpectedExpression),
|
||||||
|
#[error(transparent)]
|
||||||
|
ConflictingFunctionNames(#[from] ConflictingFunctionNames),
|
||||||
|
#[error(transparent)]
|
||||||
|
InvalidNamespaceName(#[from] InvalidNamespaceName),
|
||||||
|
#[error(transparent)]
|
||||||
|
UnresolvedMacroUsage(#[from] UnresolvedMacroUsage),
|
||||||
|
#[error(transparent)]
|
||||||
|
IncompatibleFunctionAnnotation(#[from] IncompatibleFunctionAnnotation),
|
||||||
|
#[error(transparent)]
|
||||||
|
IllegalIndexing(#[from] IllegalIndexing),
|
||||||
|
#[error(transparent)]
|
||||||
|
MismatchedTypes(#[from] MismatchedTypes),
|
||||||
|
#[error(transparent)]
|
||||||
|
UnknownIdentifier(#[from] UnknownIdentifier),
|
||||||
|
#[error(transparent)]
|
||||||
|
AssignmentError(#[from] AssignmentError),
|
||||||
|
#[error(transparent)]
|
||||||
|
NeverLoops(#[from] NeverLoops),
|
||||||
|
#[error("Lua is disabled, but a Lua function was used.")]
|
||||||
|
LuaDisabled,
|
||||||
|
#[error("Other: {0}")]
|
||||||
|
Other(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An error that occurs when a function declaration is missing.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct UnexpectedExpression(pub Box<Expression>);
|
||||||
|
|
||||||
|
impl Display for UnexpectedExpression {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
Message::new(Severity::Error, "encountered unexpected expression")
|
||||||
|
)?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.0.span(), Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for UnexpectedExpression {}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct ConflictingFunctionNames {
|
||||||
|
pub definition: Span,
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for ConflictingFunctionNames {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
Message::new(
|
||||||
|
Severity::Error,
|
||||||
|
format!(
|
||||||
|
"the following function declaration conflicts with an existing function with name `{}`",
|
||||||
|
self.name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.definition, Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for ConflictingFunctionNames {}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct InvalidNamespaceName {
|
||||||
|
pub name: StringLiteral,
|
||||||
|
pub invalid_chars: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for InvalidNamespaceName {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
Message::new(
|
||||||
|
Severity::Error,
|
||||||
|
format!(
|
||||||
|
"Invalid characters in namespace `{}`. The following characters are not allowed in namespace definitions: `{}`",
|
||||||
|
self.name.str_content(),
|
||||||
|
self.invalid_chars
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.name.span, Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for InvalidNamespaceName {}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct UnresolvedMacroUsage {
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for UnresolvedMacroUsage {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
Message::new(
|
||||||
|
Severity::Error,
|
||||||
|
format!(
|
||||||
|
"Macro `{}` was used, but could not be resolved.",
|
||||||
|
self.span.str(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(
|
||||||
|
&self.span,
|
||||||
|
Some(format!(
|
||||||
|
"You might want to add `{}` to the function parameters.",
|
||||||
|
self.span.str()
|
||||||
|
))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for UnresolvedMacroUsage {}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct IncompatibleFunctionAnnotation {
|
||||||
|
pub span: Span,
|
||||||
|
pub reason: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for IncompatibleFunctionAnnotation {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
Message::new(
|
||||||
|
Severity::Error,
|
||||||
|
format!(
|
||||||
|
"Annotation `{}` cannot be used here, because {}.",
|
||||||
|
self.span.str(),
|
||||||
|
self.reason
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)?;
|
||||||
|
|
||||||
|
write!(f, "\n{}", SourceCodeDisplay::new(&self.span, None::<u8>))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for IncompatibleFunctionAnnotation {}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct InvalidFunctionArguments {
|
||||||
|
pub span: Span,
|
||||||
|
pub expected: usize,
|
||||||
|
pub actual: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for InvalidFunctionArguments {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
Message::new(
|
||||||
|
Severity::Error,
|
||||||
|
format!(
|
||||||
|
"Expected {} arguments, but got {}.",
|
||||||
|
self.expected, self.actual
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let help_message = if self.expected > self.actual {
|
||||||
|
format!(
|
||||||
|
"You might want to add {} more arguments.",
|
||||||
|
self.expected - self.actual
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
format!(
|
||||||
|
"You might want to remove {} arguments.",
|
||||||
|
self.actual - self.expected
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.span, Some(help_message))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for InvalidFunctionArguments {}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, thiserror::Error)]
|
||||||
|
pub struct NeverLoops {
|
||||||
|
pub reason: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for NeverLoops {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
Message::new(Severity::Error, "Loop never actually loops.")
|
||||||
|
)?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(
|
||||||
|
&self.reason,
|
||||||
|
Some("This statement causes the loop to always terminate.")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,125 @@
|
||||||
|
use std::{collections::HashMap, sync::RwLock};
|
||||||
|
|
||||||
|
use crate::{base::source_file::Span, transpile::error::UnknownIdentifier};
|
||||||
|
|
||||||
|
/// Type of variable
|
||||||
|
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
|
||||||
|
pub enum VariableType {
|
||||||
|
/// A function.
|
||||||
|
Function,
|
||||||
|
/// A macro function parameter.
|
||||||
|
MacroParameter,
|
||||||
|
/// A scoreboard.
|
||||||
|
Scoreboard,
|
||||||
|
/// A scoreboard value.
|
||||||
|
ScoreboardValue,
|
||||||
|
/// Multiple values stored in scoreboard.
|
||||||
|
ScoreboardArray,
|
||||||
|
/// A tag applied to entities.
|
||||||
|
Tag,
|
||||||
|
/// A boolean stored in a data storage.
|
||||||
|
BooleanStorage,
|
||||||
|
/// Multiple booleans stored in a data storage array.
|
||||||
|
BooleanStorageArray,
|
||||||
|
/// Compiler internal function.
|
||||||
|
InternalFunction,
|
||||||
|
/// Compiler internal value.
|
||||||
|
ComptimeValue,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A scope that stores variables.
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct SemanticScope<'a> {
|
||||||
|
/// Parent scope where variables are inherited from.
|
||||||
|
parent: Option<&'a Self>,
|
||||||
|
/// Variables stored in the scope.
|
||||||
|
variables: RwLock<HashMap<String, VariableType>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> SemanticScope<'a> {
|
||||||
|
/// Creates a new scope.
|
||||||
|
#[must_use]
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let scope = Self::default();
|
||||||
|
|
||||||
|
scope.set_variable("print", VariableType::InternalFunction);
|
||||||
|
|
||||||
|
scope
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new scope with a parent.
|
||||||
|
#[must_use]
|
||||||
|
pub fn with_parent(parent: &'a Self) -> Self {
|
||||||
|
Self {
|
||||||
|
parent: Some(parent),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets a variable from the scope.
|
||||||
|
pub fn get_variable(&self, name: &str) -> Option<VariableType> {
|
||||||
|
let var = self.variables.read().unwrap().get(name).copied();
|
||||||
|
if var.is_some() {
|
||||||
|
var
|
||||||
|
} else {
|
||||||
|
self.parent
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|parent| parent.get_variable(name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets a variable in the scope.
|
||||||
|
pub fn set_variable(&self, name: &str, var: VariableType) {
|
||||||
|
self.variables
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.insert(name.to_string(), var);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the variables stored in the current scope.
|
||||||
|
pub fn get_local_variables(&self) -> &RwLock<HashMap<String, VariableType>> {
|
||||||
|
&self.variables
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets all variables stored in the scope.
|
||||||
|
///
|
||||||
|
/// This function does not return a reference to the variables, but clones them.
|
||||||
|
pub fn get_all_variables(&self) -> HashMap<String, VariableType> {
|
||||||
|
let mut variables = self.variables.read().unwrap().clone();
|
||||||
|
if let Some(parent) = self.parent.as_ref() {
|
||||||
|
variables.extend(parent.get_all_variables());
|
||||||
|
}
|
||||||
|
variables
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the parent scope.
|
||||||
|
pub fn get_parent(&self) -> Option<&Self> {
|
||||||
|
self.parent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UnknownIdentifier {
|
||||||
|
pub(super) fn from_semantic_scope(identifier: Span, scope: &SemanticScope<'_>) -> Self {
|
||||||
|
use itertools::Itertools as _;
|
||||||
|
|
||||||
|
let own_name = identifier.str();
|
||||||
|
let alternatives = scope
|
||||||
|
.get_all_variables()
|
||||||
|
.iter()
|
||||||
|
.filter_map(|(name, _)| {
|
||||||
|
let normalized_distance = strsim::normalized_damerau_levenshtein(own_name, name);
|
||||||
|
(normalized_distance > 0.8 || strsim::damerau_levenshtein(own_name, name) < 3)
|
||||||
|
.then_some((normalized_distance, name))
|
||||||
|
})
|
||||||
|
.sorted_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal))
|
||||||
|
.map(|(_, data)| data)
|
||||||
|
.take(8)
|
||||||
|
.cloned()
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
identifier,
|
||||||
|
alternatives,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,282 @@
|
||||||
|
//! Utilities for (de-)serializing
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
collections::BTreeMap,
|
||||||
|
marker::PhantomData,
|
||||||
|
sync::{Arc, LazyLock, Mutex, RwLock},
|
||||||
|
};
|
||||||
|
|
||||||
|
use serde::{
|
||||||
|
de::{self, Visitor},
|
||||||
|
ser::SerializeStruct,
|
||||||
|
Deserialize, Serialize,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::base::source_file::SourceFile;
|
||||||
|
|
||||||
|
thread_local! {
|
||||||
|
static DEDUPLICATE_SOURCE_FILES: LazyLock<RwLock<bool>> = LazyLock::new(|| RwLock::new(false));
|
||||||
|
|
||||||
|
static SERIALIZE_DATA: LazyLock<Mutex<SerializeData>> =
|
||||||
|
LazyLock::new(|| Mutex::new(SerializeData::default()));
|
||||||
|
|
||||||
|
static DESERIALIZE_DATA: LazyLock<RwLock<Option<DeserializeData>>> =
|
||||||
|
LazyLock::new(|| RwLock::new(None));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wrapper to remove duplicate source file data during (de-)serialization
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct SerdeWrapper<T>(pub T);
|
||||||
|
|
||||||
|
impl<T> Serialize for SerdeWrapper<T>
|
||||||
|
where
|
||||||
|
T: Serialize,
|
||||||
|
{
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
DEDUPLICATE_SOURCE_FILES.with(|d| *d.write().unwrap() = true);
|
||||||
|
SERIALIZE_DATA.with(|d| d.lock().unwrap().clear());
|
||||||
|
// hold guard so no other can serialize at the same time in same thread
|
||||||
|
let s = DEDUPLICATE_SOURCE_FILES.with(|d| {
|
||||||
|
let guard = d.read().unwrap();
|
||||||
|
let mut serialized_data = serde_json::Serializer::new(Vec::new());
|
||||||
|
self.0
|
||||||
|
.serialize(&mut serialized_data)
|
||||||
|
.map_err(|_| serde::ser::Error::custom("could not buffer serialization"))?;
|
||||||
|
drop(serialized_data);
|
||||||
|
let mut s = serializer.serialize_struct("SerdeWrapper", 3)?;
|
||||||
|
|
||||||
|
SERIALIZE_DATA.with(|d| {
|
||||||
|
s.serialize_field("source_files", &d.lock().unwrap().id_to_source_file)
|
||||||
|
})?;
|
||||||
|
s.serialize_field("data", &self.0)?;
|
||||||
|
drop(guard);
|
||||||
|
Ok(s)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
DEDUPLICATE_SOURCE_FILES.with(|d| *d.write().unwrap() = false);
|
||||||
|
s.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de, T> Deserialize<'de> for SerdeWrapper<T>
|
||||||
|
where
|
||||||
|
T: Deserialize<'de>,
|
||||||
|
{
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: de::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(field_identifier, rename_all = "snake_case")]
|
||||||
|
enum Field {
|
||||||
|
Data,
|
||||||
|
SourceFiles,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct WrapperVisitor<T>(PhantomData<T>);
|
||||||
|
|
||||||
|
impl<'de, T> Visitor<'de> for WrapperVisitor<T>
|
||||||
|
where
|
||||||
|
T: Deserialize<'de>,
|
||||||
|
{
|
||||||
|
type Value = SerdeWrapper<T>;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
formatter.write_str("struct SerdeWrapper")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
|
||||||
|
where
|
||||||
|
V: de::SeqAccess<'de>,
|
||||||
|
{
|
||||||
|
let source_files: BTreeMap<u64, SourceFile> = seq
|
||||||
|
.next_element()?
|
||||||
|
.ok_or_else(|| de::Error::invalid_length(0, &self))?;
|
||||||
|
DESERIALIZE_DATA.with(|d| {
|
||||||
|
*d.write().unwrap() = Some(DeserializeData {
|
||||||
|
id_to_source_file: source_files
|
||||||
|
.into_iter()
|
||||||
|
.map(|(k, v)| (k, Arc::new(v)))
|
||||||
|
.collect(),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
let data = seq
|
||||||
|
.next_element()?
|
||||||
|
.ok_or_else(|| de::Error::invalid_length(1, &self))?;
|
||||||
|
|
||||||
|
Ok(SerdeWrapper(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
|
||||||
|
where
|
||||||
|
V: de::MapAccess<'de>,
|
||||||
|
{
|
||||||
|
let mut source_files: Option<BTreeMap<u64, SourceFile>> = None;
|
||||||
|
let mut data = None;
|
||||||
|
|
||||||
|
while let Some(key) = map.next_key()? {
|
||||||
|
match key {
|
||||||
|
Field::Data => {
|
||||||
|
if data.is_some() {
|
||||||
|
return Err(de::Error::duplicate_field("data"));
|
||||||
|
}
|
||||||
|
DESERIALIZE_DATA.with(|d| {
|
||||||
|
*d.write().unwrap() =
|
||||||
|
source_files.as_ref().map(|source_files| DeserializeData {
|
||||||
|
id_to_source_file: source_files
|
||||||
|
.iter()
|
||||||
|
.map(|(&k, v)| (k, Arc::new(v.clone())))
|
||||||
|
.collect(),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
data = Some(map.next_value()?);
|
||||||
|
}
|
||||||
|
Field::SourceFiles => {
|
||||||
|
if source_files.is_some() {
|
||||||
|
return Err(de::Error::duplicate_field("source_files"));
|
||||||
|
}
|
||||||
|
source_files = Some(map.next_value()?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = data.ok_or_else(|| de::Error::missing_field("data"))?;
|
||||||
|
Ok(SerdeWrapper(data))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DEDUPLICATE_SOURCE_FILES.with(|d| *d.write().unwrap() = true);
|
||||||
|
DESERIALIZE_DATA.with(|d| *d.write().unwrap() = None);
|
||||||
|
let res = deserializer.deserialize_struct(
|
||||||
|
"SerdeWrapper",
|
||||||
|
&["source_files", "data"],
|
||||||
|
WrapperVisitor(PhantomData::<T>),
|
||||||
|
);
|
||||||
|
DEDUPLICATE_SOURCE_FILES.with(|d| *d.write().unwrap() = false);
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Internally used for Serialization
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
struct SerializeData {
|
||||||
|
id_counter: u64,
|
||||||
|
ptr_to_id: BTreeMap<usize, u64>,
|
||||||
|
id_to_source_file: BTreeMap<u64, SourceFile>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SerializeData {
|
||||||
|
fn clear(&mut self) {
|
||||||
|
self.id_counter = 0;
|
||||||
|
self.id_to_source_file.clear();
|
||||||
|
self.ptr_to_id.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get id of already stored [`Arc`] or store it and return new id
|
||||||
|
pub fn get_id_of(&mut self, source_file: &Arc<SourceFile>) -> u64 {
|
||||||
|
let ptr = Arc::as_ptr(source_file);
|
||||||
|
if let Some(&id) = self.ptr_to_id.get(&(ptr as usize)) {
|
||||||
|
id
|
||||||
|
} else {
|
||||||
|
let id = self.id_counter;
|
||||||
|
self.id_counter += 1;
|
||||||
|
|
||||||
|
self.ptr_to_id.insert(ptr as usize, id);
|
||||||
|
self.id_to_source_file
|
||||||
|
.insert(id, Arc::unwrap_or_clone(source_file.to_owned()));
|
||||||
|
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
struct DeserializeData {
|
||||||
|
id_to_source_file: BTreeMap<u64, Arc<SourceFile>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod source_file {
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use serde::{de, Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::{base::source_file::SourceFile, serde::DESERIALIZE_DATA};
|
||||||
|
|
||||||
|
use super::{DEDUPLICATE_SOURCE_FILES, SERIALIZE_DATA};
|
||||||
|
|
||||||
|
pub fn serialize<S>(this: &Arc<SourceFile>, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
if DEDUPLICATE_SOURCE_FILES.with(|d| *d.read().unwrap()) {
|
||||||
|
SERIALIZE_DATA.with(|d| {
|
||||||
|
let mut data = d.lock().unwrap();
|
||||||
|
serializer.serialize_u64(data.get_id_of(this))
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
this.as_ref().serialize(serializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deserialize<'de, D>(deserializer: D) -> Result<Arc<SourceFile>, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
if DEDUPLICATE_SOURCE_FILES.with(|d| *d.read().unwrap()) {
|
||||||
|
let id = u64::deserialize(deserializer)?;
|
||||||
|
Ok(DESERIALIZE_DATA.with(|d| {
|
||||||
|
d.read()
|
||||||
|
.unwrap()
|
||||||
|
.as_ref()
|
||||||
|
.ok_or_else(|| de::Error::custom("SourceFiles do not have been loaded yet"))?
|
||||||
|
.id_to_source_file
|
||||||
|
.get(&id)
|
||||||
|
.map(Arc::clone)
|
||||||
|
.ok_or_else(|| serde::de::Error::custom("invalid source_file id"))
|
||||||
|
}))?
|
||||||
|
} else {
|
||||||
|
Ok(Arc::new(SourceFile::deserialize(deserializer)?))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(all(test, feature = "shulkerbox"))]
|
||||||
|
mod tests {
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use shulkerbox::virtual_fs::{VFile, VFolder};
|
||||||
|
|
||||||
|
use crate::{base::SilentHandler, syntax::syntax_tree::program::ProgramFile};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_serde_wrapper() {
|
||||||
|
let mut vfolder = VFolder::new();
|
||||||
|
let vfile = VFile::Text(r#"namespace "test";"#.to_string());
|
||||||
|
vfolder.add_file("main.shu", vfile);
|
||||||
|
|
||||||
|
let parsed = crate::parse(
|
||||||
|
&SilentHandler::new(),
|
||||||
|
&vfolder,
|
||||||
|
Path::new("main.shu"),
|
||||||
|
"main".to_string(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let wrapper = SerdeWrapper(parsed);
|
||||||
|
|
||||||
|
let serialized = serde_json::to_string_pretty(&wrapper).unwrap();
|
||||||
|
let SerdeWrapper(deserialized) =
|
||||||
|
serde_json::from_str::<SerdeWrapper<ProgramFile>>(&serialized).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
Arc::as_ptr(deserialized.namespace().keyword().span.source_file()),
|
||||||
|
Arc::as_ptr(deserialized.namespace().name().span.source_file())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,27 +3,91 @@
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
base::log::{Message, Severity, SourceCodeDisplay},
|
base::{
|
||||||
|
log::{Message, Severity, SourceCodeDisplay},
|
||||||
|
source_file::{SourceElement as _, Span},
|
||||||
|
},
|
||||||
lexical::token::{KeywordKind, Token},
|
lexical::token::{KeywordKind, Token},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Result type for parsing operations.
|
||||||
|
pub type ParseResult<T> = Result<T, Error>;
|
||||||
|
|
||||||
|
/// An enumeration containing all kinds of syntactic errors that can occur while parsing the
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, thiserror::Error)]
|
||||||
|
#[allow(missing_docs)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error(transparent)]
|
||||||
|
UnexpectedSyntax(#[from] UnexpectedSyntax),
|
||||||
|
#[error(transparent)]
|
||||||
|
InvalidArgument(#[from] InvalidArgument),
|
||||||
|
#[error(transparent)]
|
||||||
|
InvalidAnnotation(#[from] InvalidAnnotation),
|
||||||
|
}
|
||||||
|
|
||||||
/// Enumeration containing all kinds of syntax that can be failed to parse.
|
/// Enumeration containing all kinds of syntax that can be failed to parse.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
pub enum SyntaxKind {
|
pub enum SyntaxKind {
|
||||||
|
Either(&'static [SyntaxKind]),
|
||||||
Punctuation(char),
|
Punctuation(char),
|
||||||
Keyword(KeywordKind),
|
Keyword(KeywordKind),
|
||||||
Identifier,
|
Identifier,
|
||||||
Declaration,
|
Declaration,
|
||||||
Numeric,
|
Integer,
|
||||||
|
Boolean,
|
||||||
StringLiteral,
|
StringLiteral,
|
||||||
|
TemplateStringLiteralPart,
|
||||||
|
AnyStringLiteral,
|
||||||
Statement,
|
Statement,
|
||||||
Expression,
|
Expression,
|
||||||
|
Operator,
|
||||||
Type,
|
Type,
|
||||||
ExecuteBlock,
|
ExecuteBlock,
|
||||||
ExecuteBlockTail,
|
ExecuteBlockTail,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl SyntaxKind {
|
||||||
|
fn expected_binding_str(&self) -> String {
|
||||||
|
match self {
|
||||||
|
Self::Either(variants) => {
|
||||||
|
if variants.is_empty() {
|
||||||
|
"end of file".to_string()
|
||||||
|
} else if variants.len() == 1 {
|
||||||
|
variants[0].expected_binding_str()
|
||||||
|
} else {
|
||||||
|
let comma_range = ..variants.len() - 1;
|
||||||
|
let comma_elements = variants[comma_range]
|
||||||
|
.iter()
|
||||||
|
.map(Self::expected_binding_str)
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
format!(
|
||||||
|
"{}, or {}",
|
||||||
|
comma_elements,
|
||||||
|
variants.last().unwrap().expected_binding_str()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Self::Identifier => "an identifier token".to_string(),
|
||||||
|
Self::Punctuation(char) => format!("a punctuation token `{char}`"),
|
||||||
|
Self::Keyword(keyword) => format!("a keyword token `{}`", keyword.as_str()),
|
||||||
|
Self::Declaration => "a declaration token".to_string(),
|
||||||
|
Self::Integer => "an integer token".to_string(),
|
||||||
|
Self::Boolean => "a boolean token".to_string(),
|
||||||
|
Self::StringLiteral => "a string literal".to_string(),
|
||||||
|
Self::TemplateStringLiteralPart => "part of a template string literal".to_string(),
|
||||||
|
Self::AnyStringLiteral => "a (template) string literal".to_string(),
|
||||||
|
Self::Statement => "a statement syntax".to_string(),
|
||||||
|
Self::Expression => "an expression syntax".to_string(),
|
||||||
|
Self::Operator => "an operator".to_string(),
|
||||||
|
Self::Type => "a type syntax".to_string(),
|
||||||
|
Self::ExecuteBlock => "an execute block syntax".to_string(),
|
||||||
|
Self::ExecuteBlockTail => "an execute block tail syntax".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A syntax/token is expected but found an other invalid token.
|
/// A syntax/token is expected but found an other invalid token.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct UnexpectedSyntax {
|
pub struct UnexpectedSyntax {
|
||||||
|
|
@ -36,19 +100,7 @@ pub struct UnexpectedSyntax {
|
||||||
|
|
||||||
impl Display for UnexpectedSyntax {
|
impl Display for UnexpectedSyntax {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let expected_binding = match self.expected {
|
let expected_binding = self.expected.expected_binding_str();
|
||||||
SyntaxKind::Identifier => "an identifier token".to_string(),
|
|
||||||
SyntaxKind::Punctuation(char) => format!("a punctuation token `{char}`"),
|
|
||||||
SyntaxKind::Keyword(keyword) => format!("a keyword token `{}`", keyword.as_str()),
|
|
||||||
SyntaxKind::Declaration => "a declaration token".to_string(),
|
|
||||||
SyntaxKind::Numeric => "a numeric token".to_string(),
|
|
||||||
SyntaxKind::StringLiteral => "a string literal".to_string(),
|
|
||||||
SyntaxKind::Statement => "a statement syntax".to_string(),
|
|
||||||
SyntaxKind::Expression => "an expression syntax".to_string(),
|
|
||||||
SyntaxKind::Type => "a type syntax".to_string(),
|
|
||||||
SyntaxKind::ExecuteBlock => "an execute block syntax".to_string(),
|
|
||||||
SyntaxKind::ExecuteBlockTail => "an execute block tail syntax".to_string(),
|
|
||||||
};
|
|
||||||
let found_binding = match self.found.clone() {
|
let found_binding = match self.found.clone() {
|
||||||
Some(Token::Comment(..)) => "a comment token".to_string(),
|
Some(Token::Comment(..)) => "a comment token".to_string(),
|
||||||
Some(Token::DocComment(..)) => "a doc comment token".to_string(),
|
Some(Token::DocComment(..)) => "a doc comment token".to_string(),
|
||||||
|
|
@ -60,9 +112,11 @@ impl Display for UnexpectedSyntax {
|
||||||
Some(Token::Punctuation(punctuation)) => {
|
Some(Token::Punctuation(punctuation)) => {
|
||||||
format!("a punctuation token `{}`", punctuation.punctuation)
|
format!("a punctuation token `{}`", punctuation.punctuation)
|
||||||
}
|
}
|
||||||
Some(Token::Numeric(..)) => "a numeric token".to_string(),
|
Some(Token::Integer(..)) => "an integer token".to_string(),
|
||||||
|
Some(Token::Boolean(..)) => "a boolean token".to_string(),
|
||||||
Some(Token::CommandLiteral(..)) => "a literal command token".to_string(),
|
Some(Token::CommandLiteral(..)) => "a literal command token".to_string(),
|
||||||
Some(Token::StringLiteral(..)) => "a string literal token".to_string(),
|
Some(Token::StringLiteral(..)) => "a string literal token".to_string(),
|
||||||
|
Some(Token::TemplateStringText(..)) => "a template string text token".to_string(),
|
||||||
|
|
||||||
None => "EOF".to_string(),
|
None => "EOF".to_string(),
|
||||||
};
|
};
|
||||||
|
|
@ -75,7 +129,7 @@ impl Display for UnexpectedSyntax {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"\n{}",
|
"\n{}",
|
||||||
SourceCodeDisplay::new(span.span(), Option::<u8>::None)
|
SourceCodeDisplay::new(&span.span(), Option::<u8>::None)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -83,10 +137,57 @@ impl Display for UnexpectedSyntax {
|
||||||
|
|
||||||
impl std::error::Error for UnexpectedSyntax {}
|
impl std::error::Error for UnexpectedSyntax {}
|
||||||
|
|
||||||
/// An enumeration containing all kinds of syntactic errors that can occur while parsing the
|
/// An error that occurred due to an invalid argument.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, thiserror::Error)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
#[allow(missing_docs)]
|
pub struct InvalidArgument {
|
||||||
pub enum Error {
|
/// The error message.
|
||||||
#[error(transparent)]
|
pub message: String,
|
||||||
UnexpectedSyntax(#[from] UnexpectedSyntax),
|
/// The span of the invalid argument.
|
||||||
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for InvalidArgument {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", Message::new(Severity::Error, &self.message))?;
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.span, Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for InvalidArgument {}
|
||||||
|
|
||||||
|
/// An error that occurred due to an invalid annotation.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
|
pub struct InvalidAnnotation {
|
||||||
|
/// The invalid annotation identifier.
|
||||||
|
pub annotation: Span,
|
||||||
|
/// The target of the annotation.
|
||||||
|
pub target: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for InvalidAnnotation {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
Message::new(
|
||||||
|
Severity::Error,
|
||||||
|
format!(
|
||||||
|
"Annotation '{}' cannot be applied to {}",
|
||||||
|
self.annotation.str(),
|
||||||
|
self.target
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)?;
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.annotation, Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for InvalidAnnotation {}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
//! This module contains the syntax tree and parser for the `ShulkerScript` language.
|
//! This module contains the syntax tree and parser for the `Shulkerscript` language.
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,12 @@ use enum_as_inner::EnumAsInner;
|
||||||
use crate::{
|
use crate::{
|
||||||
base::{self, Handler},
|
base::{self, Handler},
|
||||||
lexical::{
|
lexical::{
|
||||||
token::{Identifier, Keyword, KeywordKind, Numeric, Punctuation, StringLiteral, Token},
|
token::{Identifier, Integer, Keyword, KeywordKind, Punctuation, StringLiteral, Token},
|
||||||
token_stream::{Delimited, Delimiter, TokenStream, TokenTree},
|
token_stream::{Delimited, Delimiter, TokenStream, TokenTree},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::error::{Error, SyntaxKind, UnexpectedSyntax};
|
use super::error::{Error, ParseResult, SyntaxKind, UnexpectedSyntax};
|
||||||
|
|
||||||
/// Represents a parser that reads a token stream and constructs an abstract syntax tree.
|
/// Represents a parser that reads a token stream and constructs an abstract syntax tree.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deref, DerefMut)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deref, DerefMut)]
|
||||||
|
|
@ -38,12 +38,15 @@ impl<'a> Parser<'a> {
|
||||||
/// Steps into the [`Delimited`] token stream and parses the content within the delimiters.
|
/// Steps into the [`Delimited`] token stream and parses the content within the delimiters.
|
||||||
///
|
///
|
||||||
/// The parser's position must be at the delimited token stream.
|
/// The parser's position must be at the delimited token stream.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// - If the parser's position is not at the delimited token stream.
|
||||||
pub fn step_into<T>(
|
pub fn step_into<T>(
|
||||||
&mut self,
|
&mut self,
|
||||||
delimiter: Delimiter,
|
delimiter: Delimiter,
|
||||||
f: impl FnOnce(&mut Self) -> Option<T>,
|
f: impl FnOnce(&mut Self) -> ParseResult<T>,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> Option<DelimitedTree<T>> {
|
) -> ParseResult<DelimitedTree<T>> {
|
||||||
self.current_frame.stop_at_significant();
|
self.current_frame.stop_at_significant();
|
||||||
let raw_token_tree = self
|
let raw_token_tree = self
|
||||||
.current_frame
|
.current_frame
|
||||||
|
|
@ -62,7 +65,7 @@ impl<'a> Parser<'a> {
|
||||||
delimited_tree
|
delimited_tree
|
||||||
}
|
}
|
||||||
found => {
|
found => {
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::Punctuation(expected),
|
expected: SyntaxKind::Punctuation(expected),
|
||||||
found: Some(match found {
|
found: Some(match found {
|
||||||
TokenTree::Token(token) => token.clone(),
|
TokenTree::Token(token) => token.clone(),
|
||||||
|
|
@ -70,18 +73,20 @@ impl<'a> Parser<'a> {
|
||||||
Token::Punctuation(delimited_tree.open.clone())
|
Token::Punctuation(delimited_tree.open.clone())
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
}));
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
|
||||||
return None;
|
return Err(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::Punctuation(expected),
|
expected: SyntaxKind::Punctuation(expected),
|
||||||
found: self.get_reading(None).into_token(),
|
found: self.get_reading(None).into_token(),
|
||||||
}));
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
|
||||||
return None;
|
return Err(err);
|
||||||
};
|
};
|
||||||
|
|
||||||
// creates a new frame
|
// creates a new frame
|
||||||
|
|
@ -99,7 +104,10 @@ impl<'a> Parser<'a> {
|
||||||
let tree = f(self);
|
let tree = f(self);
|
||||||
|
|
||||||
// pops the current frame off the stack
|
// pops the current frame off the stack
|
||||||
let new_frame = self.stack.pop()?;
|
let new_frame = self
|
||||||
|
.stack
|
||||||
|
.pop()
|
||||||
|
.expect("frame has been pushed on the stack before");
|
||||||
|
|
||||||
// the current frame must be at the end
|
// the current frame must be at the end
|
||||||
if !self.current_frame.is_exhausted() {
|
if !self.current_frame.is_exhausted() {
|
||||||
|
|
@ -111,10 +119,12 @@ impl<'a> Parser<'a> {
|
||||||
.delimiter
|
.delimiter
|
||||||
.closing_char();
|
.closing_char();
|
||||||
|
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::Punctuation(expected),
|
expected: SyntaxKind::Punctuation(expected),
|
||||||
found: self.peek().into_token(),
|
found: self.peek().into_token(),
|
||||||
}));
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
return Err(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
let close_punctuation = self
|
let close_punctuation = self
|
||||||
|
|
@ -128,7 +138,7 @@ impl<'a> Parser<'a> {
|
||||||
// replaces the current frame with the popped one
|
// replaces the current frame with the popped one
|
||||||
self.current_frame = new_frame;
|
self.current_frame = new_frame;
|
||||||
|
|
||||||
Some(DelimitedTree {
|
Ok(DelimitedTree {
|
||||||
open,
|
open,
|
||||||
tree,
|
tree,
|
||||||
close: close_punctuation,
|
close: close_punctuation,
|
||||||
|
|
@ -137,12 +147,15 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
/// Tries to parse the given function, and if it fails, resets the current index to the
|
/// Tries to parse the given function, and if it fails, resets the current index to the
|
||||||
/// `current_index` before the function call.
|
/// `current_index` before the function call.
|
||||||
pub fn try_parse<T>(&mut self, f: impl FnOnce(&mut Self) -> Option<T>) -> Option<T> {
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// - If the given function returns an error.
|
||||||
|
pub fn try_parse<T>(&mut self, f: impl FnOnce(&mut Self) -> ParseResult<T>) -> ParseResult<T> {
|
||||||
let current_index = self.current_frame.current_index;
|
let current_index = self.current_frame.current_index;
|
||||||
|
|
||||||
let result = f(self);
|
let result = f(self);
|
||||||
|
|
||||||
if result.is_none() {
|
if result.is_err() {
|
||||||
self.current_frame.current_index = current_index;
|
self.current_frame.current_index = current_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -157,7 +170,7 @@ pub struct DelimitedTree<T> {
|
||||||
pub open: Punctuation,
|
pub open: Punctuation,
|
||||||
|
|
||||||
/// The tree inside the delimiter.
|
/// The tree inside the delimiter.
|
||||||
pub tree: Option<T>,
|
pub tree: ParseResult<T>,
|
||||||
|
|
||||||
/// The closing delimiter.
|
/// The closing delimiter.
|
||||||
pub close: Punctuation,
|
pub close: Punctuation,
|
||||||
|
|
@ -192,7 +205,7 @@ pub struct Frame<'a> {
|
||||||
current_index: usize,
|
current_index: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Frame<'a> {
|
impl Frame<'_> {
|
||||||
/// Checks if the current [`Frame`] doesn't have any more significant [`TokenTree`]s to
|
/// Checks if the current [`Frame`] doesn't have any more significant [`TokenTree`]s to
|
||||||
/// parse.
|
/// parse.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
|
@ -363,32 +376,37 @@ impl<'a> Frame<'a> {
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// If the next [`Token`] is not an [`Identifier`].
|
/// If the next [`Token`] is not an [`Identifier`].
|
||||||
pub fn parse_identifier(&mut self, handler: &impl Handler<base::Error>) -> Option<Identifier> {
|
pub fn parse_identifier(
|
||||||
|
&mut self,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> ParseResult<Identifier> {
|
||||||
match self.next_significant_token() {
|
match self.next_significant_token() {
|
||||||
Reading::Atomic(Token::Identifier(ident)) => Some(ident),
|
Reading::Atomic(Token::Identifier(ident)) => Ok(ident),
|
||||||
found => {
|
found => {
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::Identifier,
|
expected: SyntaxKind::Identifier,
|
||||||
found: found.into_token(),
|
found: found.into_token(),
|
||||||
}));
|
});
|
||||||
None
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expects the next [`Token`] to be an [`Numeric`], and returns it.
|
/// Expects the next [`Token`] to be an [`Integer`], and returns it.
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// If the next [`Token`] is not an [`Identifier`].
|
/// If the next [`Token`] is not an [`Identifier`].
|
||||||
pub fn parse_numeric(&mut self, handler: &impl Handler<Error>) -> Option<Numeric> {
|
pub fn parse_integer(&mut self, handler: &impl Handler<Error>) -> ParseResult<Integer> {
|
||||||
match self.next_significant_token() {
|
match self.next_significant_token() {
|
||||||
Reading::Atomic(Token::Numeric(ident)) => Some(ident),
|
Reading::Atomic(Token::Integer(ident)) => Ok(ident),
|
||||||
found => {
|
found => {
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::Numeric,
|
expected: SyntaxKind::Integer,
|
||||||
found: found.into_token(),
|
found: found.into_token(),
|
||||||
}));
|
});
|
||||||
None
|
handler.receive(err.clone());
|
||||||
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -400,15 +418,16 @@ impl<'a> Frame<'a> {
|
||||||
pub fn parse_string_literal(
|
pub fn parse_string_literal(
|
||||||
&mut self,
|
&mut self,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> Option<StringLiteral> {
|
) -> ParseResult<StringLiteral> {
|
||||||
match self.next_significant_token() {
|
match self.next_significant_token() {
|
||||||
Reading::Atomic(Token::StringLiteral(literal)) => Some(literal),
|
Reading::Atomic(Token::StringLiteral(literal)) => Ok(literal),
|
||||||
found => {
|
found => {
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::StringLiteral,
|
expected: SyntaxKind::StringLiteral,
|
||||||
found: found.into_token(),
|
found: found.into_token(),
|
||||||
}));
|
});
|
||||||
None
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -421,17 +440,18 @@ impl<'a> Frame<'a> {
|
||||||
&mut self,
|
&mut self,
|
||||||
expected: KeywordKind,
|
expected: KeywordKind,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> Option<Keyword> {
|
) -> ParseResult<Keyword> {
|
||||||
match self.next_significant_token() {
|
match self.next_significant_token() {
|
||||||
Reading::Atomic(Token::Keyword(keyword_token)) if keyword_token.keyword == expected => {
|
Reading::Atomic(Token::Keyword(keyword_token)) if keyword_token.keyword == expected => {
|
||||||
Some(keyword_token)
|
Ok(keyword_token)
|
||||||
}
|
}
|
||||||
found => {
|
found => {
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::Keyword(expected),
|
expected: SyntaxKind::Keyword(expected),
|
||||||
found: found.into_token(),
|
found: found.into_token(),
|
||||||
}));
|
});
|
||||||
None
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -445,7 +465,7 @@ impl<'a> Frame<'a> {
|
||||||
expected: char,
|
expected: char,
|
||||||
skip_insignificant: bool,
|
skip_insignificant: bool,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> Option<Punctuation> {
|
) -> ParseResult<Punctuation> {
|
||||||
match if skip_insignificant {
|
match if skip_insignificant {
|
||||||
self.next_significant_token()
|
self.next_significant_token()
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -454,14 +474,15 @@ impl<'a> Frame<'a> {
|
||||||
Reading::Atomic(Token::Punctuation(punctuation_token))
|
Reading::Atomic(Token::Punctuation(punctuation_token))
|
||||||
if punctuation_token.punctuation == expected =>
|
if punctuation_token.punctuation == expected =>
|
||||||
{
|
{
|
||||||
Some(punctuation_token)
|
Ok(punctuation_token)
|
||||||
}
|
}
|
||||||
found => {
|
found => {
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::Punctuation(expected),
|
expected: SyntaxKind::Punctuation(expected),
|
||||||
found: found.into_token(),
|
found: found.into_token(),
|
||||||
}));
|
});
|
||||||
None
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,393 +0,0 @@
|
||||||
//! Syntax tree nodes for conditions.
|
|
||||||
|
|
||||||
use std::{cmp::Ordering, collections::VecDeque};
|
|
||||||
|
|
||||||
use enum_as_inner::EnumAsInner;
|
|
||||||
use getset::Getters;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
base::{
|
|
||||||
self,
|
|
||||||
source_file::{SourceElement, Span},
|
|
||||||
VoidHandler, Handler,
|
|
||||||
},
|
|
||||||
lexical::{
|
|
||||||
token::{Punctuation, StringLiteral, Token},
|
|
||||||
token_stream::Delimiter,
|
|
||||||
},
|
|
||||||
syntax::{
|
|
||||||
error::{Error, SyntaxKind, UnexpectedSyntax},
|
|
||||||
parser::{Parser, Reading},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Syntax Synopsis:
|
|
||||||
///
|
|
||||||
/// ``` ebnf
|
|
||||||
/// PrimaryCondition:
|
|
||||||
/// ConditionalPrefix
|
|
||||||
/// | ParenthesizedCondition
|
|
||||||
/// | StringLiteral
|
|
||||||
/// ```
|
|
||||||
#[allow(missing_docs)]
|
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)]
|
|
||||||
pub enum PrimaryCondition {
|
|
||||||
Prefix(ConditionalPrefix),
|
|
||||||
Parenthesized(ParenthesizedCondition),
|
|
||||||
StringLiteral(StringLiteral),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SourceElement for PrimaryCondition {
|
|
||||||
fn span(&self) -> Span {
|
|
||||||
match self {
|
|
||||||
Self::Prefix(prefix) => prefix.span(),
|
|
||||||
Self::Parenthesized(parenthesized) => parenthesized.span(),
|
|
||||||
Self::StringLiteral(literal) => literal.span(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Syntax Synopsis:
|
|
||||||
///
|
|
||||||
/// ``` ebnf
|
|
||||||
/// BinaryCondition:
|
|
||||||
/// Condition ConditionalBinaryOperator Condition
|
|
||||||
/// ;
|
|
||||||
/// ```
|
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
|
||||||
pub struct BinaryCondition {
|
|
||||||
/// The left operand of the binary condition.
|
|
||||||
#[get = "pub"]
|
|
||||||
left_operand: Box<Condition>,
|
|
||||||
/// The operator of the binary condition.
|
|
||||||
#[get = "pub"]
|
|
||||||
operator: ConditionalBinaryOperator,
|
|
||||||
/// The right operand of the binary condition.
|
|
||||||
#[get = "pub"]
|
|
||||||
right_operand: Box<Condition>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SourceElement for BinaryCondition {
|
|
||||||
fn span(&self) -> Span {
|
|
||||||
self.left_operand
|
|
||||||
.span()
|
|
||||||
.join(&self.right_operand.span())
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BinaryCondition {
|
|
||||||
/// Dissolves the binary condition into its components
|
|
||||||
#[must_use]
|
|
||||||
pub fn dissolve(self) -> (Condition, ConditionalBinaryOperator, Condition) {
|
|
||||||
(*self.left_operand, self.operator, *self.right_operand)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Syntax Synopsis:
|
|
||||||
///
|
|
||||||
/// ``` ebnf
|
|
||||||
/// ConditionalBinaryOperator:
|
|
||||||
/// '&&'
|
|
||||||
/// | '||'
|
|
||||||
/// ;
|
|
||||||
/// ```
|
|
||||||
#[allow(missing_docs)]
|
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)]
|
|
||||||
pub enum ConditionalBinaryOperator {
|
|
||||||
LogicalAnd(Punctuation, Punctuation),
|
|
||||||
LogicalOr(Punctuation, Punctuation),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ConditionalBinaryOperator {
|
|
||||||
/// Gets the precedence of the operator (the higher the number, the first it will be evaluated)
|
|
||||||
///
|
|
||||||
/// The least operator has precedence 1.
|
|
||||||
#[must_use]
|
|
||||||
pub fn get_precedence(&self) -> u8 {
|
|
||||||
match self {
|
|
||||||
Self::LogicalOr(..) => 1,
|
|
||||||
Self::LogicalAnd(..) => 2,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SourceElement for ConditionalBinaryOperator {
|
|
||||||
fn span(&self) -> Span {
|
|
||||||
match self {
|
|
||||||
Self::LogicalAnd(a, b) | Self::LogicalOr(a, b) => a
|
|
||||||
.span
|
|
||||||
.join(&b.span)
|
|
||||||
.expect("Invalid tokens for ConditionalBinaryOperator"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Syntax Synopsis:
|
|
||||||
///
|
|
||||||
/// ``` ebnf
|
|
||||||
/// ParenthesizedCondition:
|
|
||||||
/// '(' Condition ')';
|
|
||||||
/// ```
|
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
|
||||||
pub struct ParenthesizedCondition {
|
|
||||||
/// The opening parenthesis.
|
|
||||||
#[get = "pub"]
|
|
||||||
pub open_paren: Punctuation,
|
|
||||||
/// The condition within the parenthesis.
|
|
||||||
#[get = "pub"]
|
|
||||||
pub condition: Box<Condition>,
|
|
||||||
/// The closing parenthesis.
|
|
||||||
#[get = "pub"]
|
|
||||||
pub close_paren: Punctuation,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ParenthesizedCondition {
|
|
||||||
/// Dissolves the parenthesized condition into its components
|
|
||||||
#[must_use]
|
|
||||||
pub fn dissolve(self) -> (Punctuation, Condition, Punctuation) {
|
|
||||||
(self.open_paren, *self.condition, self.close_paren)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SourceElement for ParenthesizedCondition {
|
|
||||||
fn span(&self) -> Span {
|
|
||||||
self.open_paren
|
|
||||||
.span()
|
|
||||||
.join(&self.close_paren.span())
|
|
||||||
.expect("The span of the parenthesis is invalid.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Syntax Synopsis:
|
|
||||||
///
|
|
||||||
/// ``` ebnf
|
|
||||||
/// ConditionalPrefixOperator: '!';
|
|
||||||
/// ```
|
|
||||||
#[allow(missing_docs)]
|
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)]
|
|
||||||
pub enum ConditionalPrefixOperator {
|
|
||||||
LogicalNot(Punctuation),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SourceElement for ConditionalPrefixOperator {
|
|
||||||
fn span(&self) -> Span {
|
|
||||||
match self {
|
|
||||||
Self::LogicalNot(token) => token.span.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Syntax Synopsis:
|
|
||||||
///
|
|
||||||
/// ```ebnf
|
|
||||||
/// ConditionalPrefix:
|
|
||||||
/// ConditionalPrefixOperator PrimaryCondition
|
|
||||||
/// ;
|
|
||||||
/// ```
|
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
|
||||||
pub struct ConditionalPrefix {
|
|
||||||
/// The operator of the prefix.
|
|
||||||
#[get = "pub"]
|
|
||||||
operator: ConditionalPrefixOperator,
|
|
||||||
/// The operand of the prefix.
|
|
||||||
#[get = "pub"]
|
|
||||||
operand: Box<PrimaryCondition>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SourceElement for ConditionalPrefix {
|
|
||||||
fn span(&self) -> Span {
|
|
||||||
self.operator.span().join(&self.operand.span()).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl ConditionalPrefix {
|
|
||||||
/// Dissolves the conditional prefix into its components
|
|
||||||
#[must_use]
|
|
||||||
pub fn dissolve(self) -> (ConditionalPrefixOperator, PrimaryCondition) {
|
|
||||||
(self.operator, *self.operand)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Syntax Synopsis:
|
|
||||||
///
|
|
||||||
/// ``` ebnf
|
|
||||||
/// Condition:
|
|
||||||
/// PrimaryCondition
|
|
||||||
/// | BinaryCondition
|
|
||||||
/// ;
|
|
||||||
/// ```
|
|
||||||
#[allow(missing_docs)]
|
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)]
|
|
||||||
pub enum Condition {
|
|
||||||
Primary(PrimaryCondition),
|
|
||||||
Binary(BinaryCondition),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SourceElement for Condition {
|
|
||||||
fn span(&self) -> Span {
|
|
||||||
match self {
|
|
||||||
Self::Primary(primary) => primary.span(),
|
|
||||||
Self::Binary(binary) => binary.span(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Parser<'a> {
|
|
||||||
/// Parses a [`Condition`].
|
|
||||||
pub fn parse_condition(&mut self, handler: &impl Handler<base::Error>) -> Option<Condition> {
|
|
||||||
let mut lhs = Condition::Primary(self.parse_primary_condition(handler)?);
|
|
||||||
let mut expressions = VecDeque::new();
|
|
||||||
|
|
||||||
// Parses a list of binary operators and expressions
|
|
||||||
while let Some(binary_operator) = self.try_parse_conditional_binary_operator() {
|
|
||||||
expressions.push_back((
|
|
||||||
binary_operator,
|
|
||||||
Some(Condition::Primary(self.parse_primary_condition(handler)?)),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut candidate_index = 0;
|
|
||||||
let mut current_precedence;
|
|
||||||
|
|
||||||
while !expressions.is_empty() {
|
|
||||||
// reset precedence
|
|
||||||
current_precedence = 0;
|
|
||||||
|
|
||||||
for (index, (binary_op, _)) in expressions.iter().enumerate() {
|
|
||||||
let new_precedence = binary_op.get_precedence();
|
|
||||||
match new_precedence.cmp(¤t_precedence) {
|
|
||||||
// Clear the candidate indices and set the current precedence to the
|
|
||||||
// precedence of the current binary operator.
|
|
||||||
Ordering::Greater => {
|
|
||||||
current_precedence = new_precedence;
|
|
||||||
candidate_index = index;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ordering::Less | Ordering::Equal => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ASSUMPTION: The assignments have 1 precedence and are right associative.
|
|
||||||
assert!(current_precedence > 0);
|
|
||||||
|
|
||||||
if candidate_index == 0 {
|
|
||||||
let (binary_op, rhs) = expressions.pop_front().expect("No binary operator found");
|
|
||||||
|
|
||||||
// fold the first expression
|
|
||||||
lhs = Condition::Binary(BinaryCondition {
|
|
||||||
left_operand: Box::new(lhs),
|
|
||||||
operator: binary_op,
|
|
||||||
right_operand: Box::new(rhs.unwrap()),
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
let (binary_op, rhs) = expressions
|
|
||||||
.remove(candidate_index)
|
|
||||||
.expect("No binary operator found");
|
|
||||||
|
|
||||||
// fold the expression at candidate_index
|
|
||||||
expressions[candidate_index - 1].1 = Some(Condition::Binary(BinaryCondition {
|
|
||||||
left_operand: Box::new(expressions[candidate_index - 1].1.take().unwrap()),
|
|
||||||
operator: binary_op,
|
|
||||||
right_operand: Box::new(rhs.unwrap()),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(lhs)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parses a [`PrimaryCondition`].
|
|
||||||
pub fn parse_primary_condition(
|
|
||||||
&mut self,
|
|
||||||
handler: &impl Handler<base::Error>,
|
|
||||||
) -> Option<PrimaryCondition> {
|
|
||||||
match self.stop_at_significant() {
|
|
||||||
// prefixed expression
|
|
||||||
Reading::Atomic(Token::Punctuation(punc)) if punc.punctuation == '!' => {
|
|
||||||
// eat prefix operator
|
|
||||||
self.forward();
|
|
||||||
|
|
||||||
let operator = match punc.punctuation {
|
|
||||||
'!' => ConditionalPrefixOperator::LogicalNot(punc),
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let operand = Box::new(self.parse_primary_condition(handler)?);
|
|
||||||
|
|
||||||
Some(PrimaryCondition::Prefix(ConditionalPrefix {
|
|
||||||
operator,
|
|
||||||
operand,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
// string literal
|
|
||||||
Reading::Atomic(Token::StringLiteral(literal)) => {
|
|
||||||
self.forward();
|
|
||||||
Some(PrimaryCondition::StringLiteral(literal))
|
|
||||||
}
|
|
||||||
|
|
||||||
// parenthesized condition
|
|
||||||
Reading::IntoDelimited(punc) if punc.punctuation == '(' => self
|
|
||||||
.parse_parenthesized_condition(handler)
|
|
||||||
.map(PrimaryCondition::Parenthesized),
|
|
||||||
|
|
||||||
unexpected => {
|
|
||||||
// make progress
|
|
||||||
self.forward();
|
|
||||||
|
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
|
||||||
expected: SyntaxKind::Expression,
|
|
||||||
found: unexpected.into_token(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parses a [`ParenthesizedCondition`].
|
|
||||||
pub fn parse_parenthesized_condition(
|
|
||||||
&mut self,
|
|
||||||
handler: &impl Handler<base::Error>,
|
|
||||||
) -> Option<ParenthesizedCondition> {
|
|
||||||
let token_tree = self.step_into(
|
|
||||||
Delimiter::Parenthesis,
|
|
||||||
|parser| {
|
|
||||||
let cond = parser.parse_condition(handler)?;
|
|
||||||
parser.stop_at_significant();
|
|
||||||
Some(cond)
|
|
||||||
},
|
|
||||||
handler,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Some(ParenthesizedCondition {
|
|
||||||
open_paren: token_tree.open,
|
|
||||||
condition: Box::new(token_tree.tree?),
|
|
||||||
close_paren: token_tree.close,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn try_parse_conditional_binary_operator(&mut self) -> Option<ConditionalBinaryOperator> {
|
|
||||||
self.try_parse(|parser| match parser.next_significant_token() {
|
|
||||||
Reading::Atomic(Token::Punctuation(punc)) => match punc.punctuation {
|
|
||||||
'&' => {
|
|
||||||
let b = parser.parse_punctuation('&', false, &VoidHandler)?;
|
|
||||||
Some(ConditionalBinaryOperator::LogicalAnd(punc, b))
|
|
||||||
}
|
|
||||||
'|' => {
|
|
||||||
let b = parser.parse_punctuation('|', false, &VoidHandler)?;
|
|
||||||
Some(ConditionalBinaryOperator::LogicalOr(punc, b))
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
},
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +1,11 @@
|
||||||
//! Syntax tree nodes for declarations.
|
//! Syntax tree nodes for declarations.
|
||||||
|
|
||||||
#![allow(missing_docs)]
|
#![expect(missing_docs)]
|
||||||
|
#![expect(clippy::struct_field_names)]
|
||||||
|
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
|
use enum_as_inner::EnumAsInner;
|
||||||
use getset::Getters;
|
use getset::Getters;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
|
@ -15,19 +19,26 @@ use crate::{
|
||||||
token_stream::Delimiter,
|
token_stream::Delimiter,
|
||||||
},
|
},
|
||||||
syntax::{
|
syntax::{
|
||||||
error::{Error, SyntaxKind, UnexpectedSyntax},
|
error::{Error, InvalidAnnotation, ParseResult, SyntaxKind, UnexpectedSyntax},
|
||||||
parser::{Parser, Reading},
|
parser::{Parser, Reading},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{statement::Block, ConnectedList};
|
use super::{
|
||||||
|
statement::{Block, VariableDeclaration},
|
||||||
|
Annotation, ConnectedList, DelimitedList,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Represents a declaration in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
///
|
///
|
||||||
/// ``` ebnf
|
/// ```ebnf
|
||||||
/// Declaration:
|
/// Declaration:
|
||||||
/// Function
|
/// Function
|
||||||
/// | Import
|
/// | Import
|
||||||
|
/// | TagDeclaration
|
||||||
|
/// | ('pub'? VariableDeclaration ';')
|
||||||
/// ;
|
/// ;
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
|
@ -35,6 +46,8 @@ use super::{statement::Block, ConnectedList};
|
||||||
pub enum Declaration {
|
pub enum Declaration {
|
||||||
Function(Function),
|
Function(Function),
|
||||||
Import(Import),
|
Import(Import),
|
||||||
|
Tag(Tag),
|
||||||
|
GlobalVariable((Option<Keyword>, VariableDeclaration, Punctuation)),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SourceElement for Declaration {
|
impl SourceElement for Declaration {
|
||||||
|
|
@ -42,70 +55,56 @@ impl SourceElement for Declaration {
|
||||||
match self {
|
match self {
|
||||||
Self::Function(function) => function.span(),
|
Self::Function(function) => function.span(),
|
||||||
Self::Import(import) => import.span(),
|
Self::Import(import) => import.span(),
|
||||||
|
Self::Tag(tag) => tag.span(),
|
||||||
|
Self::GlobalVariable((pub_kw, variable, semicolon)) => pub_kw
|
||||||
|
.as_ref()
|
||||||
|
.map_or_else(|| variable.span(), SourceElement::span)
|
||||||
|
.join(&semicolon.span)
|
||||||
|
.expect("invalid declaration span"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Syntax Synopsis:
|
|
||||||
///
|
|
||||||
/// ``` ebnf
|
|
||||||
/// Annotation:
|
|
||||||
/// '#[' Identifier ('=' StringLiteral)? ']'
|
|
||||||
/// ;
|
|
||||||
/// ```
|
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
|
||||||
pub struct Annotation {
|
|
||||||
#[get = "pub"]
|
|
||||||
pound_sign: Punctuation,
|
|
||||||
#[get = "pub"]
|
|
||||||
open_bracket: Punctuation,
|
|
||||||
#[get = "pub"]
|
|
||||||
identifier: Identifier,
|
|
||||||
#[get = "pub"]
|
|
||||||
value: Option<(Punctuation, StringLiteral)>,
|
|
||||||
#[get = "pub"]
|
|
||||||
close_bracket: Punctuation,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Annotation {
|
impl Declaration {
|
||||||
/// Dissolves the [`Annotation`] into its components.
|
/// Adds an annotation to the declaration.
|
||||||
#[must_use]
|
///
|
||||||
pub fn dissolve(
|
/// # Errors
|
||||||
self,
|
/// - if the annotation is invalid for the target declaration.
|
||||||
) -> (
|
pub fn with_annotation(self, annotation: Annotation) -> ParseResult<Self> {
|
||||||
Punctuation,
|
match self {
|
||||||
Punctuation,
|
Self::Function(mut function) => {
|
||||||
Identifier,
|
function.annotations.push_front(annotation);
|
||||||
Option<(Punctuation, StringLiteral)>,
|
|
||||||
Punctuation,
|
Ok(Self::Function(function))
|
||||||
) {
|
}
|
||||||
(
|
Self::GlobalVariable((pub_kw, var, semi)) => {
|
||||||
self.pound_sign,
|
let var_with_annotation = var.with_annotation(annotation)?;
|
||||||
self.open_bracket,
|
|
||||||
self.identifier,
|
Ok(Self::GlobalVariable((pub_kw, var_with_annotation, semi)))
|
||||||
self.value,
|
}
|
||||||
self.close_bracket,
|
_ => {
|
||||||
)
|
let err = Error::InvalidAnnotation(InvalidAnnotation {
|
||||||
}
|
annotation: annotation.assignment.identifier.span,
|
||||||
}
|
target: "declarations except functions".to_string(),
|
||||||
impl SourceElement for Annotation {
|
});
|
||||||
fn span(&self) -> Span {
|
|
||||||
self.pound_sign
|
Err(err)
|
||||||
.span
|
}
|
||||||
.join(&self.close_bracket.span())
|
}
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents a function declaration in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
///
|
///
|
||||||
/// ``` ebnf
|
/// ```ebnf
|
||||||
/// Function:
|
/// Function:
|
||||||
/// Annotation* 'pub'? 'fn' Identifier '(' ParameterList? ')' Block
|
/// Annotation* 'pub'? 'fn' Identifier '(' FunctionParameterList? ')' Block
|
||||||
/// ;
|
/// ;
|
||||||
///
|
///
|
||||||
/// ParameterList:
|
/// FunctionParameterList:
|
||||||
/// Identifier (',' Identifier)* ','?
|
/// FunctionArgument (',' FunctionArgument)* ','?
|
||||||
/// ;
|
/// ;
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
|
@ -114,7 +113,7 @@ pub struct Function {
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
public_keyword: Option<Keyword>,
|
public_keyword: Option<Keyword>,
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
annotations: Vec<Annotation>,
|
annotations: VecDeque<Annotation>,
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
function_keyword: Keyword,
|
function_keyword: Keyword,
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
|
|
@ -122,7 +121,7 @@ pub struct Function {
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
parameters: Option<ConnectedList<Identifier, Punctuation>>,
|
parameters: Option<ConnectedList<FunctionParameter, Punctuation>>,
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
|
|
@ -137,11 +136,11 @@ impl Function {
|
||||||
self,
|
self,
|
||||||
) -> (
|
) -> (
|
||||||
Option<Keyword>,
|
Option<Keyword>,
|
||||||
Vec<Annotation>,
|
VecDeque<Annotation>,
|
||||||
Keyword,
|
Keyword,
|
||||||
Identifier,
|
Identifier,
|
||||||
Punctuation,
|
Punctuation,
|
||||||
Option<ConnectedList<Identifier, Punctuation>>,
|
Option<ConnectedList<FunctionParameter, Punctuation>>,
|
||||||
Punctuation,
|
Punctuation,
|
||||||
Block,
|
Block,
|
||||||
) {
|
) {
|
||||||
|
|
@ -174,9 +173,47 @@ impl SourceElement for Function {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Represents a variable type keyword for function arguments.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
///
|
///
|
||||||
/// ``` ebnf
|
/// ```ebnf
|
||||||
|
/// FunctionVariableType:
|
||||||
|
/// 'macro' | 'int' | 'bool' | 'val'
|
||||||
|
/// ;
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)]
|
||||||
|
pub enum FunctionVariableType {
|
||||||
|
Macro(Keyword),
|
||||||
|
Integer(Keyword),
|
||||||
|
Boolean(Keyword),
|
||||||
|
Value(Keyword),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents a function argument in the syntax tree.
|
||||||
|
///
|
||||||
|
/// Syntax Synopsis:
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
|
/// FunctionArgument:
|
||||||
|
/// FunctionVariableType Identifier
|
||||||
|
/// ;
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
pub struct FunctionParameter {
|
||||||
|
#[get = "pub"]
|
||||||
|
variable_type: FunctionVariableType,
|
||||||
|
#[get = "pub"]
|
||||||
|
identifier: Identifier,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents an import declaration in the syntax tree.
|
||||||
|
///
|
||||||
|
/// Syntax Synopsis:
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
/// Import:
|
/// Import:
|
||||||
/// 'from' StringLiteral 'import' ('*' | Identifier (',' Identifier)*) ';'
|
/// 'from' StringLiteral 'import' ('*' | Identifier (',' Identifier)*) ';'
|
||||||
/// ;
|
/// ;
|
||||||
|
|
@ -196,6 +233,7 @@ pub struct Import {
|
||||||
semicolon: Punctuation,
|
semicolon: Punctuation,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Items to import.
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub enum ImportItems {
|
pub enum ImportItems {
|
||||||
|
|
@ -226,65 +264,92 @@ impl SourceElement for Import {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Parser<'a> {
|
/// Represents a tag declaration in the syntax tree.
|
||||||
pub fn parse_annotation(&mut self, handler: &impl Handler<base::Error>) -> Option<Annotation> {
|
///
|
||||||
match self.stop_at_significant() {
|
/// Syntax Synopsis:
|
||||||
Reading::Atomic(Token::Punctuation(punctuation)) if punctuation.punctuation == '#' => {
|
///
|
||||||
// eat the pound sign
|
/// ```ebnf
|
||||||
self.forward();
|
/// TagDeclaration:
|
||||||
|
/// 'tag' ('<' StringLiteral '>')? StringLiteral 'replace'? '[' (StringLiteral (',' StringLiteral)*)? ']'
|
||||||
|
/// ;
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
pub struct Tag {
|
||||||
|
#[get = "pub"]
|
||||||
|
tag_keyword: Keyword,
|
||||||
|
#[get = "pub"]
|
||||||
|
of_type: Option<(Punctuation, StringLiteral, Punctuation)>,
|
||||||
|
#[get = "pub"]
|
||||||
|
name: StringLiteral,
|
||||||
|
#[get = "pub"]
|
||||||
|
replace: Option<Keyword>,
|
||||||
|
#[get = "pub"]
|
||||||
|
entries: DelimitedList<StringLiteral>,
|
||||||
|
}
|
||||||
|
|
||||||
// step into the brackets
|
impl Tag {
|
||||||
let content = self.step_into(
|
#[must_use]
|
||||||
Delimiter::Bracket,
|
#[allow(clippy::type_complexity)]
|
||||||
|parser| {
|
pub fn dissolve(
|
||||||
let identifier = parser.parse_identifier(handler)?;
|
self,
|
||||||
|
) -> (
|
||||||
let value = if let Reading::Atomic(Token::Punctuation(punctuation)) =
|
Keyword,
|
||||||
parser.stop_at_significant()
|
Option<(Punctuation, StringLiteral, Punctuation)>,
|
||||||
{
|
StringLiteral,
|
||||||
if punctuation.punctuation == '=' {
|
Option<Keyword>,
|
||||||
// eat the equals sign
|
DelimitedList<StringLiteral>,
|
||||||
parser.forward();
|
) {
|
||||||
|
(
|
||||||
// parse the string literal
|
self.tag_keyword,
|
||||||
let string_literal = parser
|
self.of_type,
|
||||||
.next_significant_token()
|
self.name,
|
||||||
.into_token()?
|
self.replace,
|
||||||
.into_string_literal()
|
self.entries,
|
||||||
.ok()?;
|
)
|
||||||
|
|
||||||
Some((punctuation, string_literal))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
Some((identifier, value))
|
|
||||||
},
|
|
||||||
handler,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let (identifier, value) = content.tree?;
|
|
||||||
|
|
||||||
Some(Annotation {
|
|
||||||
pound_sign: punctuation,
|
|
||||||
open_bracket: content.open,
|
|
||||||
identifier,
|
|
||||||
value,
|
|
||||||
close_bracket: content.close,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
#[must_use]
|
||||||
|
pub fn tag_type(&self) -> shulkerbox::datapack::tag::TagType {
|
||||||
|
use shulkerbox::datapack::tag::TagType;
|
||||||
|
|
||||||
|
self.of_type
|
||||||
|
.as_ref()
|
||||||
|
.map_or(TagType::Function, |(_, tag_type, _)| {
|
||||||
|
match tag_type.str_content().as_ref() {
|
||||||
|
"function" => TagType::Function,
|
||||||
|
"block" => TagType::Block,
|
||||||
|
"entity_type" => TagType::Entity,
|
||||||
|
"fluid" => TagType::Fluid,
|
||||||
|
"game_event" => TagType::GameEvent,
|
||||||
|
"item" => TagType::Item,
|
||||||
|
other => TagType::Other(other.to_string()),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceElement for Tag {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.tag_keyword
|
||||||
|
.span()
|
||||||
|
.join(&self.entries.close.span)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parser<'_> {
|
||||||
|
/// Parses a declaration
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// - cannot parse declaration from current position
|
||||||
|
#[expect(clippy::too_many_lines)]
|
||||||
#[tracing::instrument(level = "trace", skip_all)]
|
#[tracing::instrument(level = "trace", skip_all)]
|
||||||
pub fn parse_declaration(
|
pub fn parse_declaration(
|
||||||
&mut self,
|
&mut self,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> Option<Declaration> {
|
) -> ParseResult<Declaration> {
|
||||||
match self.stop_at_significant() {
|
match self.stop_at_significant() {
|
||||||
Reading::Atomic(Token::Keyword(function_keyword))
|
Reading::Atomic(Token::Keyword(function_keyword))
|
||||||
if function_keyword.keyword == KeywordKind::Function =>
|
if function_keyword.keyword == KeywordKind::Function =>
|
||||||
|
|
@ -293,47 +358,42 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
tracing::trace!("Parsed function '{:?}'", function.identifier.span.str());
|
tracing::trace!("Parsed function '{:?}'", function.identifier.span.str());
|
||||||
|
|
||||||
Some(Declaration::Function(function))
|
Ok(Declaration::Function(function))
|
||||||
}
|
}
|
||||||
|
|
||||||
Reading::Atomic(Token::Keyword(pub_keyword))
|
Reading::Atomic(Token::Keyword(pub_keyword))
|
||||||
if pub_keyword.keyword == KeywordKind::Pub =>
|
if pub_keyword.keyword == KeywordKind::Pub =>
|
||||||
{
|
{
|
||||||
// eat the public keyword
|
match self.peek_offset(2) {
|
||||||
self.forward();
|
Some(Reading::Atomic(Token::Keyword(function_keyword)))
|
||||||
|
if function_keyword.keyword == KeywordKind::Function =>
|
||||||
|
{
|
||||||
|
let function = self.parse_function(handler)?;
|
||||||
|
tracing::trace!("Parsed function '{:?}'", function.identifier.span.str());
|
||||||
|
|
||||||
// parse the function keyword
|
Ok(Declaration::Function(function))
|
||||||
let function = self.parse_function(handler)?;
|
}
|
||||||
|
_ => {
|
||||||
|
// eat the pub keyword
|
||||||
|
self.forward();
|
||||||
|
|
||||||
Some(Declaration::Function(Function {
|
let var = self.parse_variable_declaration(handler)?;
|
||||||
public_keyword: Some(pub_keyword),
|
let semi = self.parse_punctuation(';', true, handler)?;
|
||||||
..function
|
|
||||||
}))
|
Ok(Declaration::GlobalVariable((Some(pub_keyword), var, semi)))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse annotations
|
// parse annotations
|
||||||
Reading::Atomic(Token::Punctuation(punctuation)) if punctuation.punctuation == '#' => {
|
Reading::Atomic(Token::Punctuation(punctuation)) if punctuation.punctuation == '#' => {
|
||||||
// parse the annotation
|
// parse the annotation
|
||||||
let mut annotations = Vec::new();
|
let annotation = self.parse_annotation(handler)?;
|
||||||
|
let declaration = self.parse_declaration(handler)?;
|
||||||
|
|
||||||
while let Some(annotation) =
|
declaration
|
||||||
self.try_parse(|parser| parser.parse_annotation(handler))
|
.with_annotation(annotation)
|
||||||
{
|
.inspect_err(|err| handler.receive(Box::new(err.clone())))
|
||||||
annotations.push(annotation);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.parse_declaration(handler).and_then(|declaration| {
|
|
||||||
if let Declaration::Function(mut function) = declaration {
|
|
||||||
function.annotations.extend(annotations);
|
|
||||||
Some(Declaration::Function(function))
|
|
||||||
} else {
|
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
|
||||||
expected: SyntaxKind::Keyword(KeywordKind::Function),
|
|
||||||
found: None,
|
|
||||||
}));
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Reading::Atomic(Token::Keyword(from_keyword))
|
Reading::Atomic(Token::Keyword(from_keyword))
|
||||||
|
|
@ -366,12 +426,12 @@ impl<'a> Parser<'a> {
|
||||||
// }
|
// }
|
||||||
;
|
;
|
||||||
|
|
||||||
if let Some(items) = items {
|
if let Ok(items) = items {
|
||||||
let semicolon = self.parse_punctuation(';', true, handler)?;
|
let semicolon = self.parse_punctuation(';', true, handler)?;
|
||||||
|
|
||||||
tracing::trace!("Parsed import from '{:?}'", module.str_content());
|
tracing::trace!("Parsed import from '{:?}'", module.str_content());
|
||||||
|
|
||||||
Some(Declaration::Import(Import {
|
Ok(Declaration::Import(Import {
|
||||||
from_keyword,
|
from_keyword,
|
||||||
module,
|
module,
|
||||||
import_keyword,
|
import_keyword,
|
||||||
|
|
@ -379,62 +439,199 @@ impl<'a> Parser<'a> {
|
||||||
semicolon,
|
semicolon,
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::Identifier,
|
expected: SyntaxKind::Punctuation('*'),
|
||||||
found: self.stop_at_significant().into_token(),
|
found: self.stop_at_significant().into_token(),
|
||||||
}));
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
|
||||||
None
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reading::Atomic(Token::Keyword(tag_keyword))
|
||||||
|
if tag_keyword.keyword == KeywordKind::Tag =>
|
||||||
|
{
|
||||||
|
// eat the tag keyword
|
||||||
|
self.forward();
|
||||||
|
|
||||||
|
let of_type = match self.stop_at_significant() {
|
||||||
|
Reading::Atomic(Token::Punctuation(punc)) if punc.punctuation == '<' => {
|
||||||
|
// eat the open bracket
|
||||||
|
self.forward();
|
||||||
|
|
||||||
|
let of_type = self.parse_string_literal(handler)?;
|
||||||
|
|
||||||
|
// eat the close bracket
|
||||||
|
let closing = self.parse_punctuation('>', true, handler)?;
|
||||||
|
|
||||||
|
Some((punc, of_type, closing))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
// parse the name
|
||||||
|
let name = self.parse_string_literal(handler)?;
|
||||||
|
|
||||||
|
let replace = self
|
||||||
|
.try_parse(|parser| parser.parse_keyword(KeywordKind::Replace, &VoidHandler))
|
||||||
|
.ok();
|
||||||
|
|
||||||
|
let entries = self.parse_enclosed_list(
|
||||||
|
Delimiter::Bracket,
|
||||||
|
',',
|
||||||
|
|parser| parser.parse_string_literal(handler),
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(Declaration::Tag(Tag {
|
||||||
|
tag_keyword,
|
||||||
|
of_type,
|
||||||
|
name,
|
||||||
|
replace,
|
||||||
|
entries,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
Reading::Atomic(Token::Keyword(keyword))
|
||||||
|
if matches!(
|
||||||
|
keyword.keyword,
|
||||||
|
KeywordKind::Int | KeywordKind::Bool | KeywordKind::Val
|
||||||
|
) =>
|
||||||
|
{
|
||||||
|
let var = self.parse_variable_declaration(handler)?;
|
||||||
|
let semi = self.parse_punctuation(';', true, handler)?;
|
||||||
|
|
||||||
|
Ok(Declaration::GlobalVariable((None, var, semi)))
|
||||||
|
}
|
||||||
|
|
||||||
unexpected => {
|
unexpected => {
|
||||||
// make progress
|
// make progress
|
||||||
self.forward();
|
self.forward();
|
||||||
|
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::Declaration,
|
expected: SyntaxKind::Declaration,
|
||||||
found: unexpected.into_token(),
|
found: unexpected.into_token(),
|
||||||
}));
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
|
||||||
None
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_function(&mut self, handler: &impl Handler<base::Error>) -> Option<Function> {
|
/// Parses a function.
|
||||||
if let Reading::Atomic(Token::Keyword(function_keyword)) = self.stop_at_significant() {
|
///
|
||||||
// eat the function keyword
|
/// # Errors
|
||||||
self.forward();
|
/// - if the parser is not at a function (not at annotation).
|
||||||
|
/// - if the parsing of the function fails.
|
||||||
|
pub fn parse_function(&mut self, handler: &impl Handler<base::Error>) -> ParseResult<Function> {
|
||||||
|
let pub_keyword =
|
||||||
|
self.try_parse(|parser| parser.parse_keyword(KeywordKind::Pub, &VoidHandler));
|
||||||
|
|
||||||
// parse the identifier
|
match self.stop_at_significant() {
|
||||||
let identifier = self.parse_identifier(handler)?;
|
Reading::Atomic(Token::Keyword(function_keyword))
|
||||||
let delimited_tree = self.parse_enclosed_list(
|
if function_keyword.keyword == KeywordKind::Function =>
|
||||||
Delimiter::Parenthesis,
|
{
|
||||||
',',
|
// eat the function keyword
|
||||||
|parser: &mut Parser<'_>| parser.parse_identifier(handler),
|
self.forward();
|
||||||
handler,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
// parse the block
|
// parse the identifier
|
||||||
let block = self.parse_block(handler)?;
|
let identifier = self.parse_identifier(handler)?;
|
||||||
|
let delimited_tree = self.parse_enclosed_list(
|
||||||
|
Delimiter::Parenthesis,
|
||||||
|
',',
|
||||||
|
|parser: &mut Parser<'_>| parser.parse_function_parameter(handler),
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
Some(Function {
|
// parse the block
|
||||||
public_keyword: None,
|
let block = self.parse_block(handler)?;
|
||||||
annotations: Vec::new(),
|
|
||||||
function_keyword,
|
Ok(Function {
|
||||||
identifier,
|
public_keyword: pub_keyword.ok(),
|
||||||
open_paren: delimited_tree.open,
|
annotations: VecDeque::new(),
|
||||||
parameters: delimited_tree.list,
|
function_keyword,
|
||||||
close_paren: delimited_tree.close,
|
identifier,
|
||||||
block,
|
open_paren: delimited_tree.open,
|
||||||
})
|
parameters: delimited_tree.list,
|
||||||
} else {
|
close_paren: delimited_tree.close,
|
||||||
handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax {
|
block,
|
||||||
expected: SyntaxKind::Keyword(KeywordKind::Function),
|
})
|
||||||
found: self.peek().into_token(),
|
}
|
||||||
}));
|
unexpected => {
|
||||||
None
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
|
expected: SyntaxKind::Keyword(KeywordKind::Function),
|
||||||
|
found: unexpected.into_token(),
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_function_parameter(
|
||||||
|
&mut self,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> ParseResult<FunctionParameter> {
|
||||||
|
match self.stop_at_significant() {
|
||||||
|
Reading::Atomic(Token::Keyword(keyword)) if keyword.keyword == KeywordKind::Int => {
|
||||||
|
let variable_type = FunctionVariableType::Integer(keyword);
|
||||||
|
self.forward();
|
||||||
|
|
||||||
|
let identifier = self.parse_identifier(handler)?;
|
||||||
|
|
||||||
|
Ok(FunctionParameter {
|
||||||
|
variable_type,
|
||||||
|
identifier,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Reading::Atomic(Token::Keyword(keyword)) if keyword.keyword == KeywordKind::Bool => {
|
||||||
|
let variable_type = FunctionVariableType::Boolean(keyword);
|
||||||
|
self.forward();
|
||||||
|
|
||||||
|
let identifier = self.parse_identifier(handler)?;
|
||||||
|
|
||||||
|
Ok(FunctionParameter {
|
||||||
|
variable_type,
|
||||||
|
identifier,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Reading::Atomic(Token::Keyword(keyword)) if keyword.keyword == KeywordKind::Macro => {
|
||||||
|
let variable_type = FunctionVariableType::Macro(keyword);
|
||||||
|
self.forward();
|
||||||
|
|
||||||
|
let identifier = self.parse_identifier(handler)?;
|
||||||
|
|
||||||
|
Ok(FunctionParameter {
|
||||||
|
variable_type,
|
||||||
|
identifier,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Reading::Atomic(Token::Keyword(keyword)) if keyword.keyword == KeywordKind::Val => {
|
||||||
|
let variable_type = FunctionVariableType::Value(keyword);
|
||||||
|
self.forward();
|
||||||
|
|
||||||
|
let identifier = self.parse_identifier(handler)?;
|
||||||
|
|
||||||
|
Ok(FunctionParameter {
|
||||||
|
variable_type,
|
||||||
|
identifier,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
unexpected => {
|
||||||
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
|
expected: SyntaxKind::Either(&[
|
||||||
|
SyntaxKind::Keyword(KeywordKind::Int),
|
||||||
|
SyntaxKind::Keyword(KeywordKind::Bool),
|
||||||
|
SyntaxKind::Keyword(KeywordKind::Macro),
|
||||||
|
SyntaxKind::Keyword(KeywordKind::Val),
|
||||||
|
]),
|
||||||
|
found: unexpected.into_token(),
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,6 +1,9 @@
|
||||||
//! Contains the syntax tree nodes that represent the structure of the source code.
|
//! Contains the syntax tree nodes that represent the structure of the source code.
|
||||||
|
|
||||||
|
use derive_more::derive::From;
|
||||||
|
use expression::Expression;
|
||||||
use getset::Getters;
|
use getset::Getters;
|
||||||
|
use strum::EnumIs;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
base::{
|
base::{
|
||||||
|
|
@ -9,15 +12,17 @@ use crate::{
|
||||||
Handler, VoidHandler,
|
Handler, VoidHandler,
|
||||||
},
|
},
|
||||||
lexical::{
|
lexical::{
|
||||||
token::{Punctuation, Token},
|
token::{Identifier, Punctuation, StringLiteral, Token},
|
||||||
token_stream::Delimiter,
|
token_stream::Delimiter,
|
||||||
},
|
},
|
||||||
syntax::parser::Reading,
|
syntax::{parser::Reading, syntax_tree::expression::TemplateStringLiteral},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::parser::Parser;
|
use super::{
|
||||||
|
error::{ParseResult, SyntaxKind, UnexpectedSyntax},
|
||||||
|
parser::Parser,
|
||||||
|
};
|
||||||
|
|
||||||
pub mod condition;
|
|
||||||
pub mod declaration;
|
pub mod declaration;
|
||||||
pub mod expression;
|
pub mod expression;
|
||||||
pub mod program;
|
pub mod program;
|
||||||
|
|
@ -49,6 +54,7 @@ pub struct ConnectedList<Element, Separator> {
|
||||||
|
|
||||||
/// Represents a syntax tree node with a pattern of having [`ConnectedList`] delimited by a pair of
|
/// Represents a syntax tree node with a pattern of having [`ConnectedList`] delimited by a pair of
|
||||||
/// punctuation like such `(a, b, c)`.
|
/// punctuation like such `(a, b, c)`.
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct DelimitedList<T> {
|
pub struct DelimitedList<T> {
|
||||||
/// The open punctuation of the list.
|
/// The open punctuation of the list.
|
||||||
|
|
@ -63,7 +69,166 @@ pub struct DelimitedList<T> {
|
||||||
pub close: Punctuation,
|
pub close: Punctuation,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Parser<'a> {
|
/// Represents a syntax tree node that can be either a string literal or a template string literal.
|
||||||
|
///
|
||||||
|
/// Syntax Synopsis:
|
||||||
|
/// ```ebnf
|
||||||
|
/// AnyStringLiteral: StringLiteral | TemplateStringLiteral ;
|
||||||
|
/// ```
|
||||||
|
#[allow(missing_docs)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, From)]
|
||||||
|
pub enum AnyStringLiteral {
|
||||||
|
StringLiteral(StringLiteral),
|
||||||
|
TemplateStringLiteral(TemplateStringLiteral),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceElement for AnyStringLiteral {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
match self {
|
||||||
|
Self::StringLiteral(string_literal) => string_literal.span(),
|
||||||
|
Self::TemplateStringLiteral(template_string_literal) => template_string_literal.span(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents an Annotation with optional value.
|
||||||
|
///
|
||||||
|
/// Syntax Synopsis:
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
|
/// Annotation:
|
||||||
|
/// '#[' AnnotationAssignment ']'
|
||||||
|
/// ;
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
pub struct Annotation {
|
||||||
|
/// The pound sign of the annotation.
|
||||||
|
#[get = "pub"]
|
||||||
|
pound_sign: Punctuation,
|
||||||
|
/// The open bracket of the annotation.
|
||||||
|
#[get = "pub"]
|
||||||
|
open_bracket: Punctuation,
|
||||||
|
/// The assignment inside the annotation.
|
||||||
|
#[get = "pub"]
|
||||||
|
assignment: AnnotationAssignment,
|
||||||
|
/// The close bracket of the annotation.
|
||||||
|
#[get = "pub"]
|
||||||
|
close_bracket: Punctuation,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Annotation {
|
||||||
|
/// Dissolves the [`Annotation`] into its components.
|
||||||
|
#[must_use]
|
||||||
|
pub fn dissolve(self) -> (Punctuation, Punctuation, AnnotationAssignment, Punctuation) {
|
||||||
|
(
|
||||||
|
self.pound_sign,
|
||||||
|
self.open_bracket,
|
||||||
|
self.assignment,
|
||||||
|
self.close_bracket,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if the annotation has the given identifier.
|
||||||
|
#[must_use]
|
||||||
|
pub fn has_identifier(&self, identifier: &str) -> bool {
|
||||||
|
self.assignment.identifier.span().str() == identifier
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl SourceElement for Annotation {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.pound_sign
|
||||||
|
.span
|
||||||
|
.join(&self.close_bracket.span())
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents a value of an annotation.
|
||||||
|
///
|
||||||
|
/// Syntax Synopsis:
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
|
/// AnnotationValue:
|
||||||
|
/// '=' Expression
|
||||||
|
/// | '(' AnnotationAssignment ( ',' AnnotationAssignment )* ')'
|
||||||
|
/// ;
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumIs)]
|
||||||
|
pub enum AnnotationValue {
|
||||||
|
/// A single value assignment.
|
||||||
|
///
|
||||||
|
/// '=' Expression
|
||||||
|
Single {
|
||||||
|
/// The equal sign of the assignment.
|
||||||
|
equal_sign: Punctuation,
|
||||||
|
/// The value of the assignment.
|
||||||
|
value: Expression,
|
||||||
|
},
|
||||||
|
/// A multiple value assignment.
|
||||||
|
///
|
||||||
|
/// '(' [`AnnotationAssignment`] ( ',' [`AnnotationAssignment`] )* ')'
|
||||||
|
Multiple {
|
||||||
|
/// The opening parenthesis of the assignment.
|
||||||
|
opening_parenthesis: Punctuation,
|
||||||
|
/// The list of assignments.
|
||||||
|
list: Box<ConnectedList<AnnotationAssignment, Punctuation>>,
|
||||||
|
/// The closing parenthesis of the assignment.
|
||||||
|
closing_parenthesis: Punctuation,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceElement for AnnotationValue {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
match self {
|
||||||
|
Self::Single { equal_sign, value } => equal_sign.span().join(&value.span()).unwrap(),
|
||||||
|
Self::Multiple {
|
||||||
|
opening_parenthesis,
|
||||||
|
closing_parenthesis,
|
||||||
|
..
|
||||||
|
} => opening_parenthesis
|
||||||
|
.span()
|
||||||
|
.join(&closing_parenthesis.span())
|
||||||
|
.unwrap(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents an assignment inside an annotation.
|
||||||
|
///
|
||||||
|
/// Syntax Synopsis:
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
|
/// AnnotationAssignment:
|
||||||
|
/// Identifier AnnotationValue
|
||||||
|
/// ;
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
pub struct AnnotationAssignment {
|
||||||
|
/// The identifier of the assignment.
|
||||||
|
pub identifier: Identifier,
|
||||||
|
/// The value of the assignment.
|
||||||
|
pub value: Option<AnnotationValue>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceElement for AnnotationAssignment {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.identifier
|
||||||
|
.span()
|
||||||
|
.join(
|
||||||
|
&self
|
||||||
|
.value
|
||||||
|
.as_ref()
|
||||||
|
.map_or_else(|| self.identifier.span(), AnnotationValue::span),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parser<'_> {
|
||||||
/// Parses a list of elements enclosed by a pair of delimiters, separated by a separator.
|
/// Parses a list of elements enclosed by a pair of delimiters, separated by a separator.
|
||||||
///
|
///
|
||||||
/// The parser position must be at the delimited list of the given delimiter. It will
|
/// The parser position must be at the delimited list of the given delimiter. It will
|
||||||
|
|
@ -76,9 +241,9 @@ impl<'a> Parser<'a> {
|
||||||
&mut self,
|
&mut self,
|
||||||
delimiter: Delimiter,
|
delimiter: Delimiter,
|
||||||
separator: char,
|
separator: char,
|
||||||
mut f: impl FnMut(&mut Self) -> Option<T>,
|
mut f: impl FnMut(&mut Self) -> ParseResult<T>,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> Option<DelimitedList<T>> {
|
) -> ParseResult<DelimitedList<T>> {
|
||||||
fn skip_to_next_separator(this: &mut Parser, separator: char) -> Option<Punctuation> {
|
fn skip_to_next_separator(this: &mut Parser, separator: char) -> Option<Punctuation> {
|
||||||
if let Reading::Atomic(Token::Punctuation(punc)) = this.stop_at(|token| {
|
if let Reading::Atomic(Token::Punctuation(punc)) = this.stop_at(|token| {
|
||||||
matches!(
|
matches!(
|
||||||
|
|
@ -101,7 +266,7 @@ impl<'a> Parser<'a> {
|
||||||
let mut trailing_separator: Option<Punctuation> = None;
|
let mut trailing_separator: Option<Punctuation> = None;
|
||||||
|
|
||||||
while !parser.is_exhausted() {
|
while !parser.is_exhausted() {
|
||||||
let Some(element) = f(parser) else {
|
let Ok(element) = f(parser) else {
|
||||||
skip_to_next_separator(parser, separator);
|
skip_to_next_separator(parser, separator);
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
@ -122,7 +287,7 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
// expect separator if not exhausted
|
// expect separator if not exhausted
|
||||||
if !parser.is_exhausted() {
|
if !parser.is_exhausted() {
|
||||||
let Some(separator) = parser.parse_punctuation(separator, true, handler)
|
let Ok(separator) = parser.parse_punctuation(separator, true, handler)
|
||||||
else {
|
else {
|
||||||
if let Some(punctuation) = skip_to_next_separator(parser, separator) {
|
if let Some(punctuation) = skip_to_next_separator(parser, separator) {
|
||||||
trailing_separator = Some(punctuation);
|
trailing_separator = Some(punctuation);
|
||||||
|
|
@ -135,7 +300,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(first.map(|first| ConnectedList {
|
Ok(first.map(|first| ConnectedList {
|
||||||
first,
|
first,
|
||||||
rest,
|
rest,
|
||||||
trailing_separator,
|
trailing_separator,
|
||||||
|
|
@ -144,7 +309,7 @@ impl<'a> Parser<'a> {
|
||||||
handler,
|
handler,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Some(DelimitedList {
|
Ok(DelimitedList {
|
||||||
open: delimited_tree.open,
|
open: delimited_tree.open,
|
||||||
list: delimited_tree.tree.unwrap(),
|
list: delimited_tree.tree.unwrap(),
|
||||||
close: delimited_tree.close,
|
close: delimited_tree.close,
|
||||||
|
|
@ -162,20 +327,20 @@ impl<'a> Parser<'a> {
|
||||||
pub fn parse_connected_list<T>(
|
pub fn parse_connected_list<T>(
|
||||||
&mut self,
|
&mut self,
|
||||||
seperator: char,
|
seperator: char,
|
||||||
mut f: impl FnMut(&mut Self) -> Option<T>,
|
mut f: impl FnMut(&mut Self) -> ParseResult<T>,
|
||||||
_handler: &impl Handler<base::Error>,
|
_handler: &impl Handler<base::Error>,
|
||||||
) -> Option<ConnectedList<T, Punctuation>> {
|
) -> ParseResult<ConnectedList<T, Punctuation>> {
|
||||||
let first = f(self)?;
|
let first = f(self)?;
|
||||||
|
|
||||||
let mut rest = Vec::new();
|
let mut rest = Vec::new();
|
||||||
|
|
||||||
while let Some(sep) =
|
while let Ok(sep) =
|
||||||
self.try_parse(|parser| parser.parse_punctuation(seperator, true, &VoidHandler))
|
self.try_parse(|parser| parser.parse_punctuation(seperator, true, &VoidHandler))
|
||||||
{
|
{
|
||||||
if let Some(element) = self.try_parse(&mut f) {
|
if let Ok(element) = self.try_parse(&mut f) {
|
||||||
rest.push((sep, element));
|
rest.push((sep, element));
|
||||||
} else {
|
} else {
|
||||||
return Some(ConnectedList {
|
return Ok(ConnectedList {
|
||||||
first,
|
first,
|
||||||
rest,
|
rest,
|
||||||
trailing_separator: Some(sep),
|
trailing_separator: Some(sep),
|
||||||
|
|
@ -183,12 +348,101 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(ConnectedList {
|
Ok(ConnectedList {
|
||||||
first,
|
first,
|
||||||
rest,
|
rest,
|
||||||
trailing_separator: None,
|
trailing_separator: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses an annotation.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// - if the parser position is not at an annotation.
|
||||||
|
/// - if the parsing of the annotation fails
|
||||||
|
pub fn parse_annotation(
|
||||||
|
&mut self,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> ParseResult<Annotation> {
|
||||||
|
match self.stop_at_significant() {
|
||||||
|
Reading::Atomic(Token::Punctuation(punctuation)) if punctuation.punctuation == '#' => {
|
||||||
|
// eat the pound sign
|
||||||
|
self.forward();
|
||||||
|
|
||||||
|
// step into the brackets
|
||||||
|
let content = self.step_into(
|
||||||
|
Delimiter::Bracket,
|
||||||
|
|parser| parser.parse_annotation_assignment(handler),
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(Annotation {
|
||||||
|
pound_sign: punctuation,
|
||||||
|
open_bracket: content.open,
|
||||||
|
assignment: content.tree?,
|
||||||
|
close_bracket: content.close,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
unexpected => {
|
||||||
|
let err = super::error::Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
|
expected: SyntaxKind::Punctuation('#'),
|
||||||
|
found: unexpected.into_token(),
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_annotation_assignment(
|
||||||
|
&mut self,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> ParseResult<AnnotationAssignment> {
|
||||||
|
let identifier = self.parse_identifier(handler)?;
|
||||||
|
|
||||||
|
match self.stop_at_significant() {
|
||||||
|
Reading::Atomic(Token::Punctuation(punc)) if punc.punctuation == '=' => {
|
||||||
|
// eat the equals sign
|
||||||
|
self.forward();
|
||||||
|
|
||||||
|
let value = self.parse_expression(handler)?;
|
||||||
|
|
||||||
|
Ok(AnnotationAssignment {
|
||||||
|
identifier,
|
||||||
|
value: Some(AnnotationValue::Single {
|
||||||
|
equal_sign: punc,
|
||||||
|
value,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Reading::IntoDelimited(delim) if delim.punctuation == '(' => {
|
||||||
|
let tree = self.step_into(
|
||||||
|
Delimiter::Parenthesis,
|
||||||
|
|p| {
|
||||||
|
p.parse_connected_list(
|
||||||
|
',',
|
||||||
|
|pp| pp.parse_annotation_assignment(handler),
|
||||||
|
handler,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(AnnotationAssignment {
|
||||||
|
identifier,
|
||||||
|
value: Some(AnnotationValue::Multiple {
|
||||||
|
opening_parenthesis: tree.open,
|
||||||
|
list: Box::new(tree.tree?),
|
||||||
|
closing_parenthesis: tree.close,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
_ => Ok(AnnotationAssignment {
|
||||||
|
identifier,
|
||||||
|
value: None,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Element: SourceElement, Separator: SourceElement> SourceElement
|
impl<Element: SourceElement, Separator: SourceElement> SourceElement
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
//! The program node of the syntax tree.
|
//! The program node of the syntax tree.
|
||||||
|
|
||||||
use getset::Getters;
|
use getset::Getters;
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
base::{
|
base::{
|
||||||
|
|
@ -11,7 +12,7 @@ use crate::{
|
||||||
lexical::token::{Keyword, KeywordKind, Punctuation, StringLiteral, Token},
|
lexical::token::{Keyword, KeywordKind, Punctuation, StringLiteral, Token},
|
||||||
syntax::{
|
syntax::{
|
||||||
self,
|
self,
|
||||||
error::{SyntaxKind, UnexpectedSyntax},
|
error::{ParseResult, SyntaxKind, UnexpectedSyntax},
|
||||||
parser::{Parser, Reading},
|
parser::{Parser, Reading},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
@ -19,6 +20,13 @@ use crate::{
|
||||||
use super::declaration::Declaration;
|
use super::declaration::Declaration;
|
||||||
|
|
||||||
/// Program is a collection of declarations preceeded by a namespace selector.
|
/// Program is a collection of declarations preceeded by a namespace selector.
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
|
/// Program:
|
||||||
|
/// Namespace
|
||||||
|
/// Declaration*
|
||||||
|
/// ;
|
||||||
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
pub struct ProgramFile {
|
pub struct ProgramFile {
|
||||||
|
|
@ -44,10 +52,10 @@ pub struct ProgramFile {
|
||||||
pub struct Namespace {
|
pub struct Namespace {
|
||||||
/// The `namespace` keyword.
|
/// The `namespace` keyword.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
namespace_keyword: Keyword,
|
keyword: Keyword,
|
||||||
/// The name of the namespace.
|
/// The name of the namespace.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
namespace_name: StringLiteral,
|
name: StringLiteral,
|
||||||
/// The semicolon.
|
/// The semicolon.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
semicolon: Punctuation,
|
semicolon: Punctuation,
|
||||||
|
|
@ -55,7 +63,7 @@ pub struct Namespace {
|
||||||
|
|
||||||
impl SourceElement for Namespace {
|
impl SourceElement for Namespace {
|
||||||
fn span(&self) -> Span {
|
fn span(&self) -> Span {
|
||||||
self.namespace_keyword
|
self.keyword
|
||||||
.span()
|
.span()
|
||||||
.join(&self.semicolon.span())
|
.join(&self.semicolon.span())
|
||||||
.expect("Invalid span")
|
.expect("Invalid span")
|
||||||
|
|
@ -66,55 +74,73 @@ impl Namespace {
|
||||||
/// Dissolves the namespace into its components.
|
/// Dissolves the namespace into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, StringLiteral, Punctuation) {
|
||||||
(self.namespace_keyword, self.namespace_name, self.semicolon)
|
(self.keyword, self.name, self.semicolon)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Validates a namespace string.
|
/// Validates a namespace string.
|
||||||
#[must_use]
|
///
|
||||||
pub fn validate_str(namespace: &str) -> bool {
|
/// # Errors
|
||||||
|
/// - If the namespace contains invalid characters.
|
||||||
|
pub fn validate_str(namespace: &str) -> Result<(), String> {
|
||||||
const VALID_CHARS: &str = "0123456789abcdefghijklmnopqrstuvwxyz_-.";
|
const VALID_CHARS: &str = "0123456789abcdefghijklmnopqrstuvwxyz_-.";
|
||||||
|
|
||||||
namespace.chars().all(|c| VALID_CHARS.contains(c))
|
let invalid_chars = namespace
|
||||||
|
.chars()
|
||||||
|
.filter(|c| !VALID_CHARS.contains(*c))
|
||||||
|
.sorted()
|
||||||
|
.unique()
|
||||||
|
.collect::<String>();
|
||||||
|
|
||||||
|
if invalid_chars.is_empty() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(invalid_chars)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Parser<'a> {
|
impl Parser<'_> {
|
||||||
/// Parses a [`ProgramFile`].
|
/// Parses a [`ProgramFile`].
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// - cannot parse a program file from current position
|
||||||
#[tracing::instrument(level = "debug", skip_all)]
|
#[tracing::instrument(level = "debug", skip_all)]
|
||||||
pub fn parse_program(&mut self, handler: &impl Handler<base::Error>) -> Option<ProgramFile> {
|
pub fn parse_program(
|
||||||
|
&mut self,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> ParseResult<ProgramFile> {
|
||||||
tracing::debug!("Parsing program");
|
tracing::debug!("Parsing program");
|
||||||
|
|
||||||
let namespace = match self.stop_at_significant() {
|
let namespace = match self.stop_at_significant() {
|
||||||
Reading::Atomic(Token::Keyword(namespace_keyword))
|
Reading::Atomic(Token::Keyword(keyword))
|
||||||
if namespace_keyword.keyword == KeywordKind::Namespace =>
|
if keyword.keyword == KeywordKind::Namespace =>
|
||||||
{
|
{
|
||||||
// eat the keyword
|
// eat the keyword
|
||||||
self.forward();
|
self.forward();
|
||||||
|
|
||||||
let namespace_name = self.parse_string_literal(handler).and_then(|name| {
|
let name = self.parse_string_literal(handler)?;
|
||||||
Namespace::validate_str(name.str_content().as_ref()).then_some(name)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let semicolon = self.parse_punctuation(';', true, handler)?;
|
let semicolon = self.parse_punctuation(';', true, handler)?;
|
||||||
|
|
||||||
Some(Namespace {
|
Ok(Namespace {
|
||||||
namespace_keyword,
|
keyword,
|
||||||
namespace_name,
|
name,
|
||||||
semicolon,
|
semicolon,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
unexpected => {
|
unexpected => {
|
||||||
handler.receive(syntax::error::Error::from(UnexpectedSyntax {
|
let err = syntax::error::Error::from(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::Keyword(KeywordKind::Namespace),
|
expected: SyntaxKind::Keyword(KeywordKind::Namespace),
|
||||||
found: unexpected.into_token(),
|
found: unexpected.into_token(),
|
||||||
}));
|
});
|
||||||
None
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
}
|
}
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
tracing::debug!(
|
tracing::debug!(
|
||||||
"Found namespace '{}', parsing declarations",
|
"Found namespace '{}', parsing declarations",
|
||||||
namespace.namespace_name.str_content()
|
namespace.name.str_content()
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut declarations = Vec::new();
|
let mut declarations = Vec::new();
|
||||||
|
|
@ -123,7 +149,7 @@ impl<'a> Parser<'a> {
|
||||||
let result = self.parse_declaration(handler);
|
let result = self.parse_declaration(handler);
|
||||||
|
|
||||||
#[allow(clippy::option_if_let_else)]
|
#[allow(clippy::option_if_let_else)]
|
||||||
if let Some(x) = result {
|
if let Ok(x) = result {
|
||||||
declarations.push(x);
|
declarations.push(x);
|
||||||
} else {
|
} else {
|
||||||
self.stop_at(|reading| {
|
self.stop_at(|reading| {
|
||||||
|
|
@ -137,7 +163,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(ProgramFile {
|
Ok(ProgramFile {
|
||||||
namespace,
|
namespace,
|
||||||
declarations,
|
declarations,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,5 +1,7 @@
|
||||||
//! Execute block statement syntax tree.
|
//! Execute block statement syntax tree.
|
||||||
|
|
||||||
|
#![expect(clippy::struct_field_names)]
|
||||||
|
|
||||||
use derive_more::From;
|
use derive_more::From;
|
||||||
use enum_as_inner::EnumAsInner;
|
use enum_as_inner::EnumAsInner;
|
||||||
use getset::Getters;
|
use getset::Getters;
|
||||||
|
|
@ -8,22 +10,23 @@ use crate::{
|
||||||
base::{
|
base::{
|
||||||
self,
|
self,
|
||||||
source_file::{SourceElement, Span},
|
source_file::{SourceElement, Span},
|
||||||
VoidHandler, Handler,
|
Handler, VoidHandler,
|
||||||
},
|
},
|
||||||
lexical::{
|
lexical::{
|
||||||
token::{Keyword, KeywordKind, Punctuation, StringLiteral, Token},
|
token::{Keyword, KeywordKind, Punctuation, Token},
|
||||||
token_stream::Delimiter,
|
token_stream::Delimiter,
|
||||||
},
|
},
|
||||||
syntax::{
|
syntax::{
|
||||||
self,
|
error::{Error, ParseResult, SyntaxKind, UnexpectedSyntax},
|
||||||
error::{SyntaxKind, UnexpectedSyntax},
|
|
||||||
parser::{DelimitedTree, Parser, Reading},
|
parser::{DelimitedTree, Parser, Reading},
|
||||||
syntax_tree::condition::ParenthesizedCondition,
|
syntax_tree::{expression::Parenthesized, AnyStringLiteral},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::Block;
|
use super::Block;
|
||||||
|
|
||||||
|
/// Represents an execute block statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// ExecuteBlock:
|
/// ExecuteBlock:
|
||||||
|
|
@ -52,12 +55,27 @@ impl SourceElement for ExecuteBlock {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents the head of an execute block statement.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
|
///
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// ExecuteBlockHead:
|
/// ExecuteBlockHead:
|
||||||
/// Conditional
|
/// Conditional
|
||||||
|
/// | Align
|
||||||
|
/// | Anchored
|
||||||
/// | As
|
/// | As
|
||||||
|
/// | AsAt
|
||||||
|
/// | At
|
||||||
|
/// | Facing
|
||||||
|
/// | In
|
||||||
|
/// | On
|
||||||
|
/// | Positioned
|
||||||
|
/// | Rotated
|
||||||
|
/// | Store
|
||||||
|
/// | Summon
|
||||||
/// ;
|
/// ;
|
||||||
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner, From)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner, From)]
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
|
|
@ -97,12 +115,15 @@ impl SourceElement for ExecuteBlockHead {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents the tail of an execute block statement.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// ExecuteBlockTail:
|
/// ExecuteBlockTail:
|
||||||
/// ExecuteBlock
|
/// ExecuteBlock
|
||||||
/// | Block
|
/// | Block
|
||||||
/// ;
|
/// ;
|
||||||
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner, From)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner, From)]
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
|
|
@ -123,11 +144,13 @@ impl SourceElement for ExecuteBlockTail {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents an conditional `if` statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
///
|
///
|
||||||
/// ``` ebnf
|
/// ```ebnf
|
||||||
/// Conditional:
|
/// Conditional:
|
||||||
/// 'if' ParenthizedCondition
|
/// 'if' Parenthized
|
||||||
/// ;
|
/// ;
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
|
@ -138,13 +161,13 @@ pub struct Conditional {
|
||||||
if_keyword: Keyword,
|
if_keyword: Keyword,
|
||||||
/// The condition of the conditional.
|
/// The condition of the conditional.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
condition: ParenthesizedCondition,
|
condition: Parenthesized,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Conditional {
|
impl Conditional {
|
||||||
/// Dissolves the [`Conditional`] into its components.
|
/// Dissolves the [`Conditional`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, ParenthesizedCondition) {
|
pub fn dissolve(self) -> (Keyword, Parenthesized) {
|
||||||
(self.if_keyword, self.condition)
|
(self.if_keyword, self.condition)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -158,9 +181,11 @@ impl SourceElement for Conditional {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents an `else` block in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
///
|
///
|
||||||
/// ``` ebnf
|
/// ```ebnf
|
||||||
/// Else:
|
/// Else:
|
||||||
/// 'else' Block
|
/// 'else' Block
|
||||||
/// ;
|
/// ;
|
||||||
|
|
@ -190,12 +215,13 @@ impl SourceElement for Else {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents an `as` execute statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
///
|
///
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// As:
|
/// As:
|
||||||
/// 'as' '(' StringLiteral ')'
|
/// 'as' '(' AnyStringLiteral ')' ;
|
||||||
/// ;
|
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
|
@ -208,7 +234,7 @@ pub struct As {
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
/// The selector of the as statement.
|
/// The selector of the as statement.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
as_selector: StringLiteral,
|
as_selector: AnyStringLiteral,
|
||||||
/// The close parenthesis.
|
/// The close parenthesis.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
|
|
@ -225,7 +251,7 @@ impl SourceElement for As {
|
||||||
impl As {
|
impl As {
|
||||||
/// Dissolves the [`As`] into its components.
|
/// Dissolves the [`As`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, Punctuation, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, Punctuation, AnyStringLiteral, Punctuation) {
|
||||||
(
|
(
|
||||||
self.as_keyword,
|
self.as_keyword,
|
||||||
self.open_paren,
|
self.open_paren,
|
||||||
|
|
@ -235,11 +261,13 @@ impl As {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents an `align` execute statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// Align:
|
/// Align:
|
||||||
/// 'align' '(' StringLiteral ')'
|
/// 'align' '(' AnyStringLiteral ')' ;
|
||||||
/// ;
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
pub struct Align {
|
pub struct Align {
|
||||||
|
|
@ -251,7 +279,7 @@ pub struct Align {
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
/// The selector of the align statement.
|
/// The selector of the align statement.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
align_selector: StringLiteral,
|
align_selector: AnyStringLiteral,
|
||||||
/// The close parenthesis.
|
/// The close parenthesis.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
|
|
@ -269,7 +297,7 @@ impl SourceElement for Align {
|
||||||
impl Align {
|
impl Align {
|
||||||
/// Dissolves the [`Align`] into its components.
|
/// Dissolves the [`Align`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, Punctuation, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, Punctuation, AnyStringLiteral, Punctuation) {
|
||||||
(
|
(
|
||||||
self.align_keyword,
|
self.align_keyword,
|
||||||
self.open_paren,
|
self.open_paren,
|
||||||
|
|
@ -279,11 +307,12 @@ impl Align {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents an `anchored` execute statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// Anchored:
|
/// Anchored:
|
||||||
/// 'anchored' '(' StringLiteral ')'
|
/// 'anchored' '(' AnyStringLiteral ')' ;
|
||||||
/// ;
|
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
|
@ -296,7 +325,7 @@ pub struct Anchored {
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
/// The selector of the anchored statement.
|
/// The selector of the anchored statement.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
anchored_selector: StringLiteral,
|
anchored_selector: AnyStringLiteral,
|
||||||
/// The close parenthesis.
|
/// The close parenthesis.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
|
|
@ -312,7 +341,7 @@ impl SourceElement for Anchored {
|
||||||
impl Anchored {
|
impl Anchored {
|
||||||
/// Dissolves the [`Anchored`] into its components.
|
/// Dissolves the [`Anchored`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, Punctuation, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, Punctuation, AnyStringLiteral, Punctuation) {
|
||||||
(
|
(
|
||||||
self.anchored_keyword,
|
self.anchored_keyword,
|
||||||
self.open_paren,
|
self.open_paren,
|
||||||
|
|
@ -322,11 +351,12 @@ impl Anchored {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents an `asat` execute statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// AsAt:
|
/// AsAt:
|
||||||
/// 'asat' '(' StringLiteral ')'
|
/// 'asat' '(' AnyStringLiteral ')' ;
|
||||||
/// ;
|
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
|
@ -339,7 +369,7 @@ pub struct AsAt {
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
/// The selector of the asat statement.
|
/// The selector of the asat statement.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
asat_selector: StringLiteral,
|
asat_selector: AnyStringLiteral,
|
||||||
/// The close parenthesis.
|
/// The close parenthesis.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
|
|
@ -355,7 +385,7 @@ impl SourceElement for AsAt {
|
||||||
impl AsAt {
|
impl AsAt {
|
||||||
/// Dissolves the [`AsAt`] into its components.
|
/// Dissolves the [`AsAt`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, Punctuation, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, Punctuation, AnyStringLiteral, Punctuation) {
|
||||||
(
|
(
|
||||||
self.asat_keyword,
|
self.asat_keyword,
|
||||||
self.open_paren,
|
self.open_paren,
|
||||||
|
|
@ -365,11 +395,12 @@ impl AsAt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents an `at` execute statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// At:
|
/// At:
|
||||||
/// 'at' '(' StringLiteral ')'
|
/// 'at' '(' AnyStringLiteral ')' ;
|
||||||
/// ;
|
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
|
@ -382,7 +413,7 @@ pub struct At {
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
/// The selector of the at statement.
|
/// The selector of the at statement.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
at_selector: StringLiteral,
|
at_selector: AnyStringLiteral,
|
||||||
/// The close parenthesis.
|
/// The close parenthesis.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
|
|
@ -398,7 +429,7 @@ impl SourceElement for At {
|
||||||
impl At {
|
impl At {
|
||||||
/// Dissolves the [`At`] into its components.
|
/// Dissolves the [`At`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, Punctuation, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, Punctuation, AnyStringLiteral, Punctuation) {
|
||||||
(
|
(
|
||||||
self.at_keyword,
|
self.at_keyword,
|
||||||
self.open_paren,
|
self.open_paren,
|
||||||
|
|
@ -408,11 +439,12 @@ impl At {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents a `facing` execute statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// Facing:
|
/// Facing:
|
||||||
/// 'facing' '(' StringLiteral ')'
|
/// 'facing' '(' AnyStringLiteral ')' ;
|
||||||
/// ;
|
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
|
@ -425,7 +457,7 @@ pub struct Facing {
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
/// The selector of the facing statement.
|
/// The selector of the facing statement.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
facing_selector: StringLiteral,
|
facing_selector: AnyStringLiteral,
|
||||||
/// The close parenthesis.
|
/// The close parenthesis.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
|
|
@ -441,7 +473,7 @@ impl SourceElement for Facing {
|
||||||
impl Facing {
|
impl Facing {
|
||||||
/// Dissolves the [`Facing`] into its components.
|
/// Dissolves the [`Facing`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, Punctuation, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, Punctuation, AnyStringLiteral, Punctuation) {
|
||||||
(
|
(
|
||||||
self.facing_keyword,
|
self.facing_keyword,
|
||||||
self.open_paren,
|
self.open_paren,
|
||||||
|
|
@ -451,11 +483,12 @@ impl Facing {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents an `in` execute statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// In:
|
/// In:
|
||||||
/// 'in' '(' StringLiteral ')'
|
/// 'in' '(' AnyStringLiteral ')' ;
|
||||||
/// ;
|
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
|
@ -468,7 +501,7 @@ pub struct In {
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
/// The selector of the in statement.
|
/// The selector of the in statement.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
in_selector: StringLiteral,
|
in_selector: AnyStringLiteral,
|
||||||
/// The close parenthesis.
|
/// The close parenthesis.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
|
|
@ -484,7 +517,7 @@ impl SourceElement for In {
|
||||||
impl In {
|
impl In {
|
||||||
/// Dissolves the [`In`] into its components.
|
/// Dissolves the [`In`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, Punctuation, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, Punctuation, AnyStringLiteral, Punctuation) {
|
||||||
(
|
(
|
||||||
self.in_keyword,
|
self.in_keyword,
|
||||||
self.open_paren,
|
self.open_paren,
|
||||||
|
|
@ -494,11 +527,12 @@ impl In {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents an `on` execute statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// On:
|
/// On:
|
||||||
/// 'on' '(' StringLiteral ')'
|
/// 'on' '(' AnyStringLiteral ')' ;
|
||||||
/// ;
|
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
|
@ -511,7 +545,7 @@ pub struct On {
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
/// The selector of the on statement.
|
/// The selector of the on statement.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
on_selector: StringLiteral,
|
on_selector: AnyStringLiteral,
|
||||||
/// The close parenthesis.
|
/// The close parenthesis.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
|
|
@ -527,7 +561,7 @@ impl SourceElement for On {
|
||||||
impl On {
|
impl On {
|
||||||
/// Dissolves the [`On`] into its components.
|
/// Dissolves the [`On`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, Punctuation, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, Punctuation, AnyStringLiteral, Punctuation) {
|
||||||
(
|
(
|
||||||
self.on_keyword,
|
self.on_keyword,
|
||||||
self.open_paren,
|
self.open_paren,
|
||||||
|
|
@ -537,11 +571,12 @@ impl On {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents a `positioned` execute statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// Positioned:
|
/// Positioned:
|
||||||
/// 'positioned' '(' StringLiteral ')'
|
/// 'positioned' '(' AnyStringLiteral ')' ;
|
||||||
/// ;
|
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
|
@ -554,7 +589,7 @@ pub struct Positioned {
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
/// The selector of the positioned statement.
|
/// The selector of the positioned statement.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
positioned_selector: StringLiteral,
|
positioned_selector: AnyStringLiteral,
|
||||||
/// The close parenthesis.
|
/// The close parenthesis.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
|
|
@ -570,7 +605,7 @@ impl SourceElement for Positioned {
|
||||||
impl Positioned {
|
impl Positioned {
|
||||||
/// Dissolves the [`Positioned`] into its components.
|
/// Dissolves the [`Positioned`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, Punctuation, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, Punctuation, AnyStringLiteral, Punctuation) {
|
||||||
(
|
(
|
||||||
self.positioned_keyword,
|
self.positioned_keyword,
|
||||||
self.open_paren,
|
self.open_paren,
|
||||||
|
|
@ -580,11 +615,12 @@ impl Positioned {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents a `rotated` execute statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// Rotated:
|
/// Rotated:
|
||||||
/// 'rotated' '(' StringLiteral ')'
|
/// 'rotated' '(' AnyStringLiteral ')' ;
|
||||||
/// ;
|
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
|
@ -597,7 +633,7 @@ pub struct Rotated {
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
/// The selector of the rotated statement.
|
/// The selector of the rotated statement.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
rotated_selector: StringLiteral,
|
rotated_selector: AnyStringLiteral,
|
||||||
/// The close parenthesis.
|
/// The close parenthesis.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
|
|
@ -613,7 +649,7 @@ impl SourceElement for Rotated {
|
||||||
impl Rotated {
|
impl Rotated {
|
||||||
/// Dissolves the [`Rotated`] into its components.
|
/// Dissolves the [`Rotated`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, Punctuation, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, Punctuation, AnyStringLiteral, Punctuation) {
|
||||||
(
|
(
|
||||||
self.rotated_keyword,
|
self.rotated_keyword,
|
||||||
self.open_paren,
|
self.open_paren,
|
||||||
|
|
@ -623,11 +659,12 @@ impl Rotated {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents a `store` execute statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// Store:
|
/// Store:
|
||||||
/// 'store' '(' StringLiteral ')'
|
/// 'store' '(' AnyStringLiteral ')' ;
|
||||||
/// ;
|
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
|
@ -640,7 +677,7 @@ pub struct Store {
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
/// The selector of the store statement.
|
/// The selector of the store statement.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
store_selector: StringLiteral,
|
store_selector: AnyStringLiteral,
|
||||||
/// The close parenthesis.
|
/// The close parenthesis.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
|
|
@ -656,7 +693,7 @@ impl SourceElement for Store {
|
||||||
impl Store {
|
impl Store {
|
||||||
/// Dissolves the [`Store`] into its components.
|
/// Dissolves the [`Store`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, Punctuation, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, Punctuation, AnyStringLiteral, Punctuation) {
|
||||||
(
|
(
|
||||||
self.store_keyword,
|
self.store_keyword,
|
||||||
self.open_paren,
|
self.open_paren,
|
||||||
|
|
@ -666,11 +703,12 @@ impl Store {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents a `summon` execute statement in the syntax tree.
|
||||||
|
///
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// Summon:
|
/// Summon:
|
||||||
/// 'summon' '(' StringLiteral ')'
|
/// 'summon' '(' AnyStringLiteral ')' ;
|
||||||
/// ;
|
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
|
@ -683,7 +721,7 @@ pub struct Summon {
|
||||||
open_paren: Punctuation,
|
open_paren: Punctuation,
|
||||||
/// The selector of the summon statement.
|
/// The selector of the summon statement.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
summon_selector: StringLiteral,
|
summon_selector: AnyStringLiteral,
|
||||||
/// The close parenthesis.
|
/// The close parenthesis.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
close_paren: Punctuation,
|
close_paren: Punctuation,
|
||||||
|
|
@ -699,7 +737,7 @@ impl SourceElement for Summon {
|
||||||
impl Summon {
|
impl Summon {
|
||||||
/// Dissolves the [`Summon`] into its components.
|
/// Dissolves the [`Summon`] into its components.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dissolve(self) -> (Keyword, Punctuation, StringLiteral, Punctuation) {
|
pub fn dissolve(self) -> (Keyword, Punctuation, AnyStringLiteral, Punctuation) {
|
||||||
(
|
(
|
||||||
self.summon_keyword,
|
self.summon_keyword,
|
||||||
self.open_paren,
|
self.open_paren,
|
||||||
|
|
@ -709,12 +747,16 @@ impl Summon {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Parser<'a> {
|
impl Parser<'_> {
|
||||||
/// Parses an [`ExecuteBlock`].
|
/// Parses an [`ExecuteBlock`].
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// - if not at the start of an execute block statement.
|
||||||
|
/// - if the parsing of the execute block statement fails.
|
||||||
pub fn parse_execute_block_statement(
|
pub fn parse_execute_block_statement(
|
||||||
&mut self,
|
&mut self,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> Option<ExecuteBlock> {
|
) -> ParseResult<ExecuteBlock> {
|
||||||
match self.stop_at_significant() {
|
match self.stop_at_significant() {
|
||||||
Reading::Atomic(Token::Keyword(if_keyword))
|
Reading::Atomic(Token::Keyword(if_keyword))
|
||||||
if if_keyword.keyword == KeywordKind::If =>
|
if if_keyword.keyword == KeywordKind::If =>
|
||||||
|
|
@ -722,7 +764,7 @@ impl<'a> Parser<'a> {
|
||||||
// eat the if keyword
|
// eat the if keyword
|
||||||
self.forward();
|
self.forward();
|
||||||
|
|
||||||
let condition = self.parse_parenthesized_condition(handler)?;
|
let condition = self.parse_parenthesized(handler)?;
|
||||||
|
|
||||||
let conditional = Conditional {
|
let conditional = Conditional {
|
||||||
if_keyword,
|
if_keyword,
|
||||||
|
|
@ -739,14 +781,17 @@ impl<'a> Parser<'a> {
|
||||||
// eat the else keyword
|
// eat the else keyword
|
||||||
parser.forward();
|
parser.forward();
|
||||||
|
|
||||||
let else_block = parser.parse_block(handler)?;
|
let else_block = parser.parse_block(&VoidHandler)?;
|
||||||
|
|
||||||
Some((else_keyword, else_block))
|
Ok((else_keyword, else_block))
|
||||||
}
|
}
|
||||||
_ => None,
|
unexpected => Err(UnexpectedSyntax {
|
||||||
|
expected: SyntaxKind::Keyword(KeywordKind::Else),
|
||||||
|
found: unexpected.into_token(),
|
||||||
|
}),
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
Some((
|
Ok((
|
||||||
block,
|
block,
|
||||||
Else {
|
Else {
|
||||||
else_keyword,
|
else_keyword,
|
||||||
|
|
@ -755,11 +800,11 @@ impl<'a> Parser<'a> {
|
||||||
))
|
))
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some((block, else_tail)) = else_tail {
|
if let Ok((block, else_tail)) = else_tail {
|
||||||
Some(ExecuteBlock::IfElse(conditional, block, else_tail))
|
Ok(ExecuteBlock::IfElse(conditional, block, else_tail))
|
||||||
} else {
|
} else {
|
||||||
let tail = self.parse_execute_block_tail(handler)?;
|
let tail = self.parse_execute_block_tail(handler)?;
|
||||||
Some(ExecuteBlock::HeadTail(
|
Ok(ExecuteBlock::HeadTail(
|
||||||
ExecuteBlockHead::Conditional(conditional),
|
ExecuteBlockHead::Conditional(conditional),
|
||||||
tail,
|
tail,
|
||||||
))
|
))
|
||||||
|
|
@ -767,21 +812,22 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Reading::Atomic(Token::Keyword(keyword)) if keyword.keyword.starts_execute_block() => {
|
Reading::Atomic(Token::Keyword(keyword)) if keyword.keyword.starts_execute_block() => {
|
||||||
// eat the as keyword
|
// eat the keyword
|
||||||
self.forward();
|
self.forward();
|
||||||
|
|
||||||
let argument = match self.stop_at_significant() {
|
let argument = match self.stop_at_significant() {
|
||||||
Reading::IntoDelimited(punc) if punc.punctuation == '(' => self.step_into(
|
Reading::IntoDelimited(punc) if punc.punctuation == '(' => self.step_into(
|
||||||
Delimiter::Parenthesis,
|
Delimiter::Parenthesis,
|
||||||
|parser| parser.parse_string_literal(handler),
|
|parser| parser.parse_any_string_literal(handler),
|
||||||
handler,
|
handler,
|
||||||
),
|
),
|
||||||
unexpected => {
|
unexpected => {
|
||||||
handler.receive(syntax::error::Error::from(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::Punctuation('('),
|
expected: SyntaxKind::Punctuation('('),
|
||||||
found: unexpected.into_token(),
|
found: unexpected.into_token(),
|
||||||
}));
|
});
|
||||||
None
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
}
|
}
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
|
|
@ -789,16 +835,17 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
let head = head_from_keyword(keyword, argument)?;
|
let head = head_from_keyword(keyword, argument)?;
|
||||||
|
|
||||||
Some(ExecuteBlock::HeadTail(head, tail))
|
Ok(ExecuteBlock::HeadTail(head, tail))
|
||||||
}
|
}
|
||||||
|
|
||||||
// unexpected
|
// unexpected
|
||||||
unexpected => {
|
unexpected => {
|
||||||
handler.receive(syntax::error::Error::from(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::ExecuteBlock,
|
expected: SyntaxKind::ExecuteBlock,
|
||||||
found: unexpected.into_token(),
|
found: unexpected.into_token(),
|
||||||
}));
|
});
|
||||||
None
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -806,7 +853,7 @@ impl<'a> Parser<'a> {
|
||||||
fn parse_execute_block_tail(
|
fn parse_execute_block_tail(
|
||||||
&mut self,
|
&mut self,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> Option<ExecuteBlockTail> {
|
) -> ParseResult<ExecuteBlockTail> {
|
||||||
match self.stop_at_significant() {
|
match self.stop_at_significant() {
|
||||||
// nested execute block
|
// nested execute block
|
||||||
Reading::Atomic(Token::Punctuation(punc)) if punc.punctuation == ',' => {
|
Reading::Atomic(Token::Punctuation(punc)) if punc.punctuation == ',' => {
|
||||||
|
|
@ -815,7 +862,7 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
let execute_block = self.parse_execute_block_statement(handler)?;
|
let execute_block = self.parse_execute_block_statement(handler)?;
|
||||||
|
|
||||||
Some(ExecuteBlockTail::ExecuteBlock(
|
Ok(ExecuteBlockTail::ExecuteBlock(
|
||||||
punc,
|
punc,
|
||||||
Box::new(execute_block),
|
Box::new(execute_block),
|
||||||
))
|
))
|
||||||
|
|
@ -825,15 +872,16 @@ impl<'a> Parser<'a> {
|
||||||
Reading::IntoDelimited(punc) if punc.punctuation == '{' => {
|
Reading::IntoDelimited(punc) if punc.punctuation == '{' => {
|
||||||
let block = self.parse_block(handler)?;
|
let block = self.parse_block(handler)?;
|
||||||
|
|
||||||
Some(ExecuteBlockTail::Block(block))
|
Ok(ExecuteBlockTail::Block(block))
|
||||||
}
|
}
|
||||||
|
|
||||||
unexpected => {
|
unexpected => {
|
||||||
handler.receive(syntax::error::Error::from(UnexpectedSyntax {
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
expected: SyntaxKind::ExecuteBlockTail,
|
expected: SyntaxKind::ExecuteBlockTail,
|
||||||
found: unexpected.into_token(),
|
found: unexpected.into_token(),
|
||||||
}));
|
});
|
||||||
None
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -841,9 +889,9 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
fn head_from_keyword(
|
fn head_from_keyword(
|
||||||
keyword: Keyword,
|
keyword: Keyword,
|
||||||
argument: DelimitedTree<StringLiteral>,
|
argument: DelimitedTree<AnyStringLiteral>,
|
||||||
) -> Option<ExecuteBlockHead> {
|
) -> ParseResult<ExecuteBlockHead> {
|
||||||
Some(match keyword.keyword {
|
Ok(match keyword.keyword {
|
||||||
KeywordKind::Align => Align {
|
KeywordKind::Align => Align {
|
||||||
align_keyword: keyword,
|
align_keyword: keyword,
|
||||||
open_paren: argument.open,
|
open_paren: argument.open,
|
||||||
|
|
@ -931,3 +979,91 @@ fn head_from_keyword(
|
||||||
_ => unreachable!("The keyword is not a valid execute block head."),
|
_ => unreachable!("The keyword is not a valid execute block head."),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Trait for the execute block head items with a [`AnyStringLiteral`] as their selector.
|
||||||
|
pub trait ExecuteBlockHeadItem {
|
||||||
|
/// Returns a reference to the selector of the execute block head item.
|
||||||
|
fn selector(&self) -> &AnyStringLiteral;
|
||||||
|
|
||||||
|
/// Analyzes the semantics of the execute block head item.
|
||||||
|
#[expect(clippy::missing_errors_doc)]
|
||||||
|
fn analyze_semantics(
|
||||||
|
&self,
|
||||||
|
scope: &crate::semantic::SemanticScope,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> Result<(), crate::semantic::error::Error> {
|
||||||
|
self.selector().analyze_semantics(scope, handler)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlockHeadItem for Align {
|
||||||
|
fn selector(&self) -> &AnyStringLiteral {
|
||||||
|
&self.align_selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlockHeadItem for Anchored {
|
||||||
|
fn selector(&self) -> &AnyStringLiteral {
|
||||||
|
&self.anchored_selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlockHeadItem for As {
|
||||||
|
fn selector(&self) -> &AnyStringLiteral {
|
||||||
|
&self.as_selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlockHeadItem for At {
|
||||||
|
fn selector(&self) -> &AnyStringLiteral {
|
||||||
|
&self.at_selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlockHeadItem for AsAt {
|
||||||
|
fn selector(&self) -> &AnyStringLiteral {
|
||||||
|
&self.asat_selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlockHeadItem for Facing {
|
||||||
|
fn selector(&self) -> &AnyStringLiteral {
|
||||||
|
&self.facing_selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlockHeadItem for In {
|
||||||
|
fn selector(&self) -> &AnyStringLiteral {
|
||||||
|
&self.in_selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlockHeadItem for On {
|
||||||
|
fn selector(&self) -> &AnyStringLiteral {
|
||||||
|
&self.on_selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlockHeadItem for Positioned {
|
||||||
|
fn selector(&self) -> &AnyStringLiteral {
|
||||||
|
&self.positioned_selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlockHeadItem for Rotated {
|
||||||
|
fn selector(&self) -> &AnyStringLiteral {
|
||||||
|
&self.rotated_selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlockHeadItem for Store {
|
||||||
|
fn selector(&self) -> &AnyStringLiteral {
|
||||||
|
&self.store_selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlockHeadItem for Summon {
|
||||||
|
fn selector(&self) -> &AnyStringLiteral {
|
||||||
|
&self.summon_selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,81 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
base::{self, Handler},
|
||||||
|
syntax::syntax_tree::statement::{
|
||||||
|
execute_block::{ExecuteBlock, ExecuteBlockTail},
|
||||||
|
AssignmentDestination, Block, SemicolonStatement, Statement,
|
||||||
|
},
|
||||||
|
transpile::{error::AssignmentError, Scope, TranspileError, TranspileResult, VariableData},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl Block {
|
||||||
|
pub(super) fn check_no_comptime_assignments(
|
||||||
|
&self,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<()> {
|
||||||
|
for statement in self.statements() {
|
||||||
|
match statement {
|
||||||
|
Statement::Semicolon(sem) => {
|
||||||
|
if let SemicolonStatement::Assignment(assignment) = sem.statement() {
|
||||||
|
if let AssignmentDestination::Identifier(ident) = assignment.destination() {
|
||||||
|
if let Some(VariableData::ComptimeValue { .. }) =
|
||||||
|
scope.get_variable(ident.span.str()).as_deref()
|
||||||
|
{
|
||||||
|
let err = TranspileError::AssignmentError(AssignmentError {
|
||||||
|
identifier: ident.span.clone(),
|
||||||
|
message: "cannot assign to a compile-time variable declared before a runtime conditional block"
|
||||||
|
.to_string(),
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Statement::Grouping(group) => {
|
||||||
|
group
|
||||||
|
.block()
|
||||||
|
.check_no_comptime_assignments(scope, handler)?;
|
||||||
|
}
|
||||||
|
Statement::ExecuteBlock(ex) => {
|
||||||
|
ex.check_no_comptime_assignments(scope, handler)?;
|
||||||
|
}
|
||||||
|
Statement::WhileLoop(while_loop) => {
|
||||||
|
while_loop
|
||||||
|
.block()
|
||||||
|
.check_no_comptime_assignments(scope, handler)?;
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecuteBlock {
|
||||||
|
pub(super) fn check_no_comptime_assignments(
|
||||||
|
&self,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<()> {
|
||||||
|
match self {
|
||||||
|
Self::HeadTail(_, tail) => match tail {
|
||||||
|
ExecuteBlockTail::Block(block) => {
|
||||||
|
block.check_no_comptime_assignments(scope, handler)?;
|
||||||
|
}
|
||||||
|
ExecuteBlockTail::ExecuteBlock(_, ex) => {
|
||||||
|
ex.check_no_comptime_assignments(scope, handler)?;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Self::IfElse(_, then, el) => {
|
||||||
|
then.check_no_comptime_assignments(scope, handler)?;
|
||||||
|
el.block().check_no_comptime_assignments(scope, handler)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,47 +1,82 @@
|
||||||
//! Conversion functions for converting between tokens/ast-nodes and [`shulkerbox`] types
|
//! Conversion functions for converting between tokens/ast-nodes and [`shulkerbox`] types
|
||||||
|
|
||||||
use shulkerbox::datapack::Condition as DpCondition;
|
use std::{borrow::Cow, collections::BTreeMap, sync::Arc};
|
||||||
|
|
||||||
use crate::syntax::syntax_tree::condition::{
|
use shulkerbox::{
|
||||||
BinaryCondition, Condition, ConditionalBinaryOperator, ConditionalPrefixOperator,
|
prelude::Command,
|
||||||
PrimaryCondition,
|
util::{MacroString as ExtMacroString, MacroStringPart as ExtMacroStringPart},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl From<Condition> for DpCondition {
|
use crate::{
|
||||||
fn from(value: Condition) -> Self {
|
base::{self, source_file::Span, Handler},
|
||||||
match value {
|
semantic::error::UnexpectedExpression,
|
||||||
Condition::Primary(primary) => primary.into(),
|
syntax::syntax_tree::expression::{TemplateStringLiteral, TemplateStringLiteralPart},
|
||||||
Condition::Binary(binary) => binary.into(),
|
transpile::{expression::DataLocation, Scope, TranspileError, TranspileResult},
|
||||||
|
util,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::util::{MacroString, MacroStringPart};
|
||||||
|
|
||||||
|
pub(crate) type ShulkerboxMacroStringMap = BTreeMap<String, (DataLocation, Vec<Command>, Span)>;
|
||||||
|
|
||||||
|
impl MacroString {
|
||||||
|
pub fn into_sb(self) -> (ExtMacroString, ShulkerboxMacroStringMap) {
|
||||||
|
match self {
|
||||||
|
Self::String(s) => (ExtMacroString::String(s), BTreeMap::new()),
|
||||||
|
Self::MacroString {
|
||||||
|
parts,
|
||||||
|
prepare_variables,
|
||||||
|
} => (
|
||||||
|
ExtMacroString::MacroString(
|
||||||
|
parts.into_iter().map(ExtMacroStringPart::from).collect(),
|
||||||
|
),
|
||||||
|
prepare_variables,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<PrimaryCondition> for DpCondition {
|
impl From<MacroStringPart> for ExtMacroStringPart {
|
||||||
fn from(value: PrimaryCondition) -> Self {
|
fn from(value: MacroStringPart) -> Self {
|
||||||
match value {
|
match value {
|
||||||
PrimaryCondition::StringLiteral(literal) => {
|
MacroStringPart::String(s) => Self::String(s),
|
||||||
Self::Atom(literal.str_content().to_string())
|
MacroStringPart::MacroUsage(m) => Self::MacroUsage(m),
|
||||||
}
|
}
|
||||||
PrimaryCondition::Parenthesized(cond) => cond.dissolve().1.into(),
|
}
|
||||||
PrimaryCondition::Prefix(prefix) => match prefix.operator() {
|
}
|
||||||
ConditionalPrefixOperator::LogicalNot(_) => {
|
|
||||||
Self::Not(Box::new(prefix.dissolve().1.into()))
|
impl TemplateStringLiteral {
|
||||||
|
pub fn as_str(
|
||||||
|
&self,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<Cow<'_, str>> {
|
||||||
|
let mut res = Cow::Borrowed("");
|
||||||
|
|
||||||
|
for part in &self.parts {
|
||||||
|
match part {
|
||||||
|
TemplateStringLiteralPart::Text(s) => {
|
||||||
|
let s = util::unescape_template_string(s.span.str());
|
||||||
|
if res.is_empty() {
|
||||||
|
res = s;
|
||||||
|
} else {
|
||||||
|
res.to_mut().push_str(&s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
TemplateStringLiteralPart::Expression { expression, .. } => {
|
||||||
|
let compiled = expression.comptime_eval(scope, handler)?;
|
||||||
|
let s = compiled.to_string_no_macro().ok_or_else(|| {
|
||||||
|
let err = TranspileError::UnexpectedExpression(UnexpectedExpression(
|
||||||
|
expression.clone(),
|
||||||
|
));
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
err
|
||||||
|
})?;
|
||||||
|
res.to_mut().push_str(&s);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<BinaryCondition> for DpCondition {
|
Ok(res)
|
||||||
fn from(value: BinaryCondition) -> Self {
|
|
||||||
let (lhs, op, rhs) = value.dissolve();
|
|
||||||
match op {
|
|
||||||
ConditionalBinaryOperator::LogicalAnd(_, _) => {
|
|
||||||
Self::And(Box::new(lhs.into()), Box::new(rhs.into()))
|
|
||||||
}
|
|
||||||
ConditionalBinaryOperator::LogicalOr(_, _) => {
|
|
||||||
Self::Or(Box::new(lhs.into()), Box::new(rhs.into()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,45 +2,408 @@
|
||||||
|
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
use getset::Getters;
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
use oxford_join::OxfordJoin as _;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
base::{
|
base::{
|
||||||
log::{Message, Severity, SourceCodeDisplay},
|
log::{Message, Severity, SourceCodeDisplay},
|
||||||
source_file::{SourceElement, Span},
|
source_file::Span,
|
||||||
},
|
},
|
||||||
syntax::syntax_tree::expression::Expression,
|
semantic::error::{ConflictingFunctionNames, InvalidFunctionArguments, UnexpectedExpression},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::expression::ExpectedType;
|
||||||
|
|
||||||
/// Errors that can occur during transpilation.
|
/// Errors that can occur during transpilation.
|
||||||
#[allow(clippy::module_name_repetitions, missing_docs)]
|
#[allow(clippy::module_name_repetitions, missing_docs)]
|
||||||
#[derive(Debug, thiserror::Error, Clone)]
|
#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
|
||||||
pub enum TranspileError {
|
pub enum TranspileError {
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
MissingFunctionDeclaration(#[from] MissingFunctionDeclaration),
|
UnexpectedExpression(#[from] UnexpectedExpression),
|
||||||
#[error("Unexpected expression: {}", .0.span().str())]
|
|
||||||
UnexpectedExpression(Expression),
|
|
||||||
#[error("Lua code evaluation is disabled.")]
|
#[error("Lua code evaluation is disabled.")]
|
||||||
LuaDisabled,
|
LuaDisabled,
|
||||||
#[error("Lua runtime error: {}", .0)]
|
#[error(transparent)]
|
||||||
LuaRuntimeError(String),
|
LuaRuntimeError(#[from] LuaRuntimeError),
|
||||||
|
#[error(transparent)]
|
||||||
|
ConflictingFunctionNames(#[from] ConflictingFunctionNames),
|
||||||
|
#[error(transparent)]
|
||||||
|
InvalidFunctionArguments(#[from] InvalidFunctionArguments),
|
||||||
|
#[error(transparent)]
|
||||||
|
IllegalAnnotationContent(#[from] IllegalAnnotationContent),
|
||||||
|
#[error(transparent)]
|
||||||
|
MismatchedTypes(#[from] MismatchedTypes),
|
||||||
|
#[error(transparent)]
|
||||||
|
FunctionArgumentsNotAllowed(#[from] FunctionArgumentsNotAllowed),
|
||||||
|
#[error(transparent)]
|
||||||
|
AssignmentError(#[from] AssignmentError),
|
||||||
|
#[error(transparent)]
|
||||||
|
UnknownIdentifier(#[from] UnknownIdentifier),
|
||||||
|
#[error(transparent)]
|
||||||
|
MissingValue(#[from] MissingValue),
|
||||||
|
#[error(transparent)]
|
||||||
|
IllegalIndexing(#[from] IllegalIndexing),
|
||||||
|
#[error(transparent)]
|
||||||
|
InvalidArgument(#[from] InvalidArgument),
|
||||||
|
#[error(transparent)]
|
||||||
|
NotComptime(#[from] NotComptime),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The result of a transpilation operation.
|
/// The result of a transpilation operation.
|
||||||
pub type TranspileResult<T> = Result<T, TranspileError>;
|
pub type TranspileResult<T> = Result<T, TranspileError>;
|
||||||
|
|
||||||
/// An error that occurs when a function declaration is missing.
|
/// An error that occurs when a function declaration is missing.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[allow(clippy::module_name_repetitions)]
|
||||||
pub struct MissingFunctionDeclaration {
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub span: Span,
|
pub struct LuaRuntimeError {
|
||||||
|
/// The span of the code block that caused the error.
|
||||||
|
pub code_block: Span,
|
||||||
|
/// The error message of the Lua runtime.
|
||||||
|
pub error_message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for MissingFunctionDeclaration {
|
impl Display for LuaRuntimeError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let message = format!(
|
let message = format!(
|
||||||
"no matching function declaration found for invocation of function `{}`",
|
r#"error during lua code execution: "{}""#,
|
||||||
self.span.str()
|
self.error_message
|
||||||
);
|
);
|
||||||
write!(f, "{}", Message::new(Severity::Error, message))?;
|
write!(f, "{}", Message::new(Severity::Error, message))?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.code_block, Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for LuaRuntimeError {}
|
||||||
|
|
||||||
|
#[cfg(feature = "lua")]
|
||||||
|
impl LuaRuntimeError {
|
||||||
|
/// Creates a new Lua runtime error from an mlua error.
|
||||||
|
#[must_use]
|
||||||
|
pub fn from_lua_err(err: &mlua::Error, span: Span) -> Self {
|
||||||
|
let err_string = err.to_string();
|
||||||
|
Self {
|
||||||
|
error_message: err_string
|
||||||
|
.strip_prefix("runtime error: ")
|
||||||
|
.unwrap_or(&err_string)
|
||||||
|
.to_string(),
|
||||||
|
code_block: span,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An error that occurs when an annotation has an illegal content.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Getters)]
|
||||||
|
pub struct IllegalAnnotationContent {
|
||||||
|
/// The span of the annotation.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub annotation: Span,
|
||||||
|
/// The error message.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for IllegalAnnotationContent {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let message = format!(
|
||||||
|
"illegal content in annotation `{}`: {}",
|
||||||
|
self.annotation.str(),
|
||||||
|
self.message
|
||||||
|
);
|
||||||
|
write!(f, "{}", Message::new(Severity::Error, message))?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.annotation, Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for IllegalAnnotationContent {}
|
||||||
|
|
||||||
|
/// An error that occurs when an expression can not evaluate to the wanted type.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Getters)]
|
||||||
|
pub struct MismatchedTypes {
|
||||||
|
/// The expression that can not evaluate to the wanted type.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub expression: Span,
|
||||||
|
/// The expected type.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub expected_type: ExpectedType,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for MismatchedTypes {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let message = format!("expression can not evaluate to type {}", self.expected_type);
|
||||||
|
write!(f, "{}", Message::new(Severity::Error, message))?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.expression, Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for MismatchedTypes {}
|
||||||
|
|
||||||
|
/// An error that occurs when an expression can not evaluate to the wanted type.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Getters)]
|
||||||
|
pub struct FunctionArgumentsNotAllowed {
|
||||||
|
/// The arguments that are not allowed.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub arguments: Span,
|
||||||
|
/// The error message.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for FunctionArgumentsNotAllowed {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", Message::new(Severity::Error, &self.message))?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.arguments, Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for FunctionArgumentsNotAllowed {}
|
||||||
|
|
||||||
|
/// An error that occurs when an expression can not evaluate to the wanted type.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Getters)]
|
||||||
|
pub struct AssignmentError {
|
||||||
|
/// The identifier that is assigned to.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub identifier: Span,
|
||||||
|
/// The error message.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for AssignmentError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", Message::new(Severity::Error, &self.message))?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.identifier, Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for AssignmentError {}
|
||||||
|
|
||||||
|
/// An error that occurs when an unknown identifier is used.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Getters)]
|
||||||
|
pub struct UnknownIdentifier {
|
||||||
|
/// The unknown identifier.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub(crate) identifier: Span,
|
||||||
|
/// Alternatives to the current identifier
|
||||||
|
#[get = "pub"]
|
||||||
|
pub(crate) alternatives: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UnknownIdentifier {
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
pub(crate) fn from_scope(
|
||||||
|
identifier: Span,
|
||||||
|
scope: &std::sync::Arc<super::variables::Scope>,
|
||||||
|
) -> Self {
|
||||||
|
use itertools::Itertools as _;
|
||||||
|
|
||||||
|
let own_name = identifier.str();
|
||||||
|
let alternatives = scope
|
||||||
|
.get_all_variables()
|
||||||
|
.iter()
|
||||||
|
.filter_map(|(name, _)| {
|
||||||
|
let normalized_distance = strsim::normalized_damerau_levenshtein(own_name, name);
|
||||||
|
(normalized_distance > 0.8 || strsim::damerau_levenshtein(own_name, name) < 3)
|
||||||
|
.then_some((normalized_distance, name))
|
||||||
|
})
|
||||||
|
.sorted_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal))
|
||||||
|
.map(|(_, data)| data)
|
||||||
|
.take(8)
|
||||||
|
.cloned()
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
identifier,
|
||||||
|
alternatives,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for UnknownIdentifier {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
Message::new(
|
||||||
|
Severity::Error,
|
||||||
|
format!("The identifier {} is not defined.", self.identifier.str())
|
||||||
|
)
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let help_message = if self.alternatives.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
let message = String::from("did you mean ");
|
||||||
|
let inner = self
|
||||||
|
.alternatives
|
||||||
|
.iter()
|
||||||
|
.map(|s| format!("`{s}`"))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
let inner = inner.oxford_or();
|
||||||
|
#[cfg(not(feature = "shulkerbox"))]
|
||||||
|
let inner = std::borrow::Cow::<str>::Owned(inner.join(", "));
|
||||||
|
|
||||||
|
Some(message + &inner + "?")
|
||||||
|
};
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.identifier, help_message.as_ref())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for UnknownIdentifier {}
|
||||||
|
|
||||||
|
/// An error that occurs when there is a value expected but none provided.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Getters)]
|
||||||
|
pub struct MissingValue {
|
||||||
|
/// The expression that is missing a value.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub expression: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for MissingValue {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
Message::new(
|
||||||
|
Severity::Error,
|
||||||
|
"The expression is expected to return a value, but no value is found."
|
||||||
|
)
|
||||||
|
)?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.expression, Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for MissingValue {}
|
||||||
|
|
||||||
|
/// An error that occurs when an indexing operation is not permitted.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Getters)]
|
||||||
|
pub struct IllegalIndexing {
|
||||||
|
/// The reason why the indexing operation is not permitted.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub reason: IllegalIndexingReason,
|
||||||
|
/// The expression that is the reason for the indexing being illegal.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub expression: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for IllegalIndexing {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", Message::new(Severity::Error, &self.reason))?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.expression, Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for IllegalIndexing {}
|
||||||
|
|
||||||
|
/// The reason why an indexing operation is not permitted.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub enum IllegalIndexingReason {
|
||||||
|
/// The expression is not an identifier.
|
||||||
|
NotIdentifier,
|
||||||
|
/// The expression cannot be indexed.
|
||||||
|
NotIndexable,
|
||||||
|
/// The expression can only be indexed with a specific type that can be evaluated at compile time.
|
||||||
|
InvalidComptimeType {
|
||||||
|
/// The expected type.
|
||||||
|
expected: ExpectedType,
|
||||||
|
},
|
||||||
|
/// The index is out of bounds.
|
||||||
|
IndexOutOfBounds {
|
||||||
|
/// The index that is out of bounds.
|
||||||
|
index: usize,
|
||||||
|
/// The length indexed object.
|
||||||
|
length: usize,
|
||||||
|
},
|
||||||
|
/// The index provided is invalid
|
||||||
|
InvalidIndex {
|
||||||
|
/// The invalid index
|
||||||
|
index: Span,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for IllegalIndexingReason {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::NotIdentifier => {
|
||||||
|
write!(f, "The expression is not an identifier.")
|
||||||
|
}
|
||||||
|
Self::NotIndexable => {
|
||||||
|
write!(f, "The expression cannot be indexed.")
|
||||||
|
}
|
||||||
|
Self::InvalidComptimeType { expected } => {
|
||||||
|
write!(f, "The expression can only be indexed with type {expected} that can be evaluated at compile time.")
|
||||||
|
}
|
||||||
|
Self::IndexOutOfBounds { index, length } => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"The index {index} is out of bounds for the expression with length {length}."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Self::InvalidIndex { index } => {
|
||||||
|
write!(f, "The index {index} is invalid.", index = index.str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An error that occurs when an indexing operation is not permitted.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Getters)]
|
||||||
|
pub struct InvalidArgument {
|
||||||
|
/// The span of the argument.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub span: Span,
|
||||||
|
/// The reason why the argument is invalid.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub reason: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for InvalidArgument {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", Message::new(Severity::Error, &self.reason))?;
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"\n{}",
|
"\n{}",
|
||||||
|
|
@ -49,4 +412,33 @@ impl Display for MissingFunctionDeclaration {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for MissingFunctionDeclaration {}
|
impl std::error::Error for InvalidArgument {}
|
||||||
|
|
||||||
|
/// An error that occurs when an indexing operation is not permitted.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Getters)]
|
||||||
|
pub struct NotComptime {
|
||||||
|
/// The expression that cannot be evaluated at compile time.
|
||||||
|
#[get = "pub"]
|
||||||
|
pub expression: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for NotComptime {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
Message::new(
|
||||||
|
Severity::Error,
|
||||||
|
"The expression cannot be evaluated at compile time but is required to."
|
||||||
|
)
|
||||||
|
)?;
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"\n{}",
|
||||||
|
SourceCodeDisplay::new(&self.expression, Option::<u8>::None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for NotComptime {}
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,899 @@
|
||||||
|
use chksum_md5 as md5;
|
||||||
|
use enum_as_inner::EnumAsInner;
|
||||||
|
use itertools::Itertools;
|
||||||
|
use std::{
|
||||||
|
borrow::{Cow, ToOwned},
|
||||||
|
collections::BTreeMap,
|
||||||
|
sync::{Arc, RwLock},
|
||||||
|
};
|
||||||
|
|
||||||
|
use shulkerbox::datapack::{Command, Execute};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
base::{
|
||||||
|
self,
|
||||||
|
source_file::{SourceElement, Span},
|
||||||
|
Handler,
|
||||||
|
},
|
||||||
|
semantic::error::{ConflictingFunctionNames, InvalidFunctionArguments},
|
||||||
|
syntax::syntax_tree::{
|
||||||
|
declaration::FunctionVariableType,
|
||||||
|
expression::{Expression, Primary},
|
||||||
|
statement::Statement,
|
||||||
|
},
|
||||||
|
transpile::{
|
||||||
|
error::IllegalAnnotationContent,
|
||||||
|
util::{MacroString, MacroStringPart},
|
||||||
|
variables::FunctionVariableDataType,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
error::{MismatchedTypes, TranspileError, TranspileResult, UnknownIdentifier},
|
||||||
|
expression::{ComptimeValue, ExpectedType, StorageType},
|
||||||
|
variables::{Scope, VariableData},
|
||||||
|
FunctionData, TranspileAnnotationValue, Transpiler,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum TranspiledFunctionArguments {
|
||||||
|
None,
|
||||||
|
Static(BTreeMap<String, MacroString>, Vec<Command>),
|
||||||
|
Dynamic(Vec<Command>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Transpiler {
|
||||||
|
/// Gets the function at the given path, or transpiles it if it hasn't been transpiled yet.
|
||||||
|
/// Returns the location of the function or None if the function does not exist.
|
||||||
|
#[tracing::instrument(level = "trace", skip(self, handler))]
|
||||||
|
pub(super) fn get_or_transpile_function(
|
||||||
|
&mut self,
|
||||||
|
identifier_span: &Span,
|
||||||
|
arguments: Option<&[&Expression]>,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<(String, TranspiledFunctionArguments)> {
|
||||||
|
let program_identifier = identifier_span.source_file().identifier();
|
||||||
|
let function = scope.get_variable(identifier_span.str());
|
||||||
|
|
||||||
|
let function_data = function.ok_or_else(|| {
|
||||||
|
let err = TranspileError::UnknownIdentifier(UnknownIdentifier::from_scope(
|
||||||
|
identifier_span.clone(),
|
||||||
|
scope,
|
||||||
|
));
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
err
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let (function_data, function_scope, function_type) = function_data
|
||||||
|
.as_ref()
|
||||||
|
.as_function()
|
||||||
|
.expect("called variable should be of type function");
|
||||||
|
|
||||||
|
match function_type {
|
||||||
|
FunctionVariableDataType::Simple {
|
||||||
|
path: function_path,
|
||||||
|
} => {
|
||||||
|
let already_transpiled = function_path.get().is_some();
|
||||||
|
|
||||||
|
if !already_transpiled {
|
||||||
|
self.prepare_transpile_function(
|
||||||
|
function_data,
|
||||||
|
program_identifier,
|
||||||
|
identifier_span,
|
||||||
|
|path| function_path.set(path).expect("not set before"),
|
||||||
|
std::iter::repeat(None),
|
||||||
|
None,
|
||||||
|
function_scope,
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let function_location = function_path
|
||||||
|
.get()
|
||||||
|
.expect("set in prepare_transpile_function if not previously set")
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let args = self.transpile_function_arguments(
|
||||||
|
function_data,
|
||||||
|
&function_location,
|
||||||
|
arguments,
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok((function_location, args))
|
||||||
|
}
|
||||||
|
FunctionVariableDataType::ComptimeArguments { function_paths } => {
|
||||||
|
let comptime_args = self.transpile_comptime_function_arguments(
|
||||||
|
function_data,
|
||||||
|
arguments,
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
let hash = comptime_args_hash(&comptime_args);
|
||||||
|
|
||||||
|
let read_guard = function_paths.read().unwrap();
|
||||||
|
let function_location = if let Some(data) = read_guard.get(&hash) {
|
||||||
|
data.to_owned()
|
||||||
|
} else {
|
||||||
|
drop(read_guard);
|
||||||
|
let function_scope = Scope::with_parent(function_scope.to_owned());
|
||||||
|
let mut path = String::new();
|
||||||
|
|
||||||
|
self.prepare_transpile_function(
|
||||||
|
function_data,
|
||||||
|
program_identifier,
|
||||||
|
identifier_span,
|
||||||
|
|p| path = p,
|
||||||
|
comptime_args.into_iter(),
|
||||||
|
Some(&hash),
|
||||||
|
&function_scope,
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
function_paths
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.insert(hash.clone(), path.clone());
|
||||||
|
|
||||||
|
path
|
||||||
|
};
|
||||||
|
|
||||||
|
let args = self.transpile_function_arguments(
|
||||||
|
function_data,
|
||||||
|
&function_location,
|
||||||
|
arguments,
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok((function_location, args))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[expect(clippy::too_many_arguments, clippy::too_many_lines)]
|
||||||
|
fn prepare_transpile_function(
|
||||||
|
&mut self,
|
||||||
|
function_data: &FunctionData,
|
||||||
|
program_identifier: &str,
|
||||||
|
identifier_span: &Span,
|
||||||
|
set_function_path: impl FnOnce(String),
|
||||||
|
comptime_args: impl Iterator<Item = Option<ComptimeValue>>,
|
||||||
|
function_suffix: Option<&str>,
|
||||||
|
function_scope: &Arc<Scope>,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<()> {
|
||||||
|
tracing::trace!("Function not transpiled yet, transpiling.");
|
||||||
|
|
||||||
|
let statements = function_data.statements.clone();
|
||||||
|
|
||||||
|
let mut modified_name = function_data.annotations.get("deobfuscate").map_or_else(
|
||||||
|
|| {
|
||||||
|
let hash_data = program_identifier.to_string() + "\0" + identifier_span.str();
|
||||||
|
Ok("shu/".to_string() + &md5::hash(hash_data).to_hex_lowercase())
|
||||||
|
},
|
||||||
|
|val| match val {
|
||||||
|
TranspileAnnotationValue::None(_) => Ok(identifier_span.str().to_string()),
|
||||||
|
TranspileAnnotationValue::Expression(expr, _) => expr
|
||||||
|
.comptime_eval(scope, handler)
|
||||||
|
.ok()
|
||||||
|
.and_then(|val| val.to_string_no_macro())
|
||||||
|
.ok_or_else(|| {
|
||||||
|
let err =
|
||||||
|
TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
|
||||||
|
annotation: identifier_span.clone(),
|
||||||
|
message: "Cannot evaluate annotation at compile time".to_string(),
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
err
|
||||||
|
}),
|
||||||
|
TranspileAnnotationValue::Map(_, span) => {
|
||||||
|
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
|
||||||
|
annotation: span.clone(),
|
||||||
|
message: "Deobfuscate annotation cannot be a map.".to_string(),
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
if let Some(suffix) = function_suffix {
|
||||||
|
use std::fmt::Write as _;
|
||||||
|
|
||||||
|
let _ = write!(modified_name, "/{suffix}");
|
||||||
|
}
|
||||||
|
|
||||||
|
let function_location = format!(
|
||||||
|
"{namespace}:{modified_name}",
|
||||||
|
namespace = function_data.namespace
|
||||||
|
);
|
||||||
|
|
||||||
|
set_function_path(function_location.clone());
|
||||||
|
|
||||||
|
for (i, (param, comptime_arg)) in function_data
|
||||||
|
.parameters
|
||||||
|
.iter()
|
||||||
|
.zip(comptime_args)
|
||||||
|
.enumerate()
|
||||||
|
{
|
||||||
|
let param_str = param.identifier().span.str();
|
||||||
|
match param.variable_type() {
|
||||||
|
FunctionVariableType::Macro(_) => {
|
||||||
|
function_scope.set_variable(
|
||||||
|
param_str,
|
||||||
|
VariableData::MacroParameter {
|
||||||
|
index: i,
|
||||||
|
macro_name: crate::util::identifier_to_macro(param_str).to_string(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
FunctionVariableType::Integer(_) => {
|
||||||
|
let objective = format!(
|
||||||
|
"shu_arguments_{}",
|
||||||
|
function_location.replace(['/', ':'], "_")
|
||||||
|
);
|
||||||
|
function_scope.set_variable(
|
||||||
|
param_str,
|
||||||
|
VariableData::ScoreboardValue {
|
||||||
|
objective: objective.clone(),
|
||||||
|
target: crate::util::identifier_to_scoreboard_target(param_str)
|
||||||
|
.into_owned(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
FunctionVariableType::Boolean(_) => {
|
||||||
|
let storage_name = format!(
|
||||||
|
"shulkerscript:arguments_{}",
|
||||||
|
function_location.replace(['/', ':'], "_")
|
||||||
|
);
|
||||||
|
// TODO: replace with proper path
|
||||||
|
function_scope.set_variable(
|
||||||
|
param_str,
|
||||||
|
VariableData::BooleanStorage {
|
||||||
|
storage_name,
|
||||||
|
path: crate::util::identifier_to_scoreboard_target(param_str)
|
||||||
|
.into_owned(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
FunctionVariableType::Value(_) => {
|
||||||
|
function_scope.set_variable(
|
||||||
|
param_str,
|
||||||
|
VariableData::ComptimeValue {
|
||||||
|
value: Arc::new(RwLock::new(comptime_arg)),
|
||||||
|
read_only: false,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let commands =
|
||||||
|
self.transpile_function(&statements, program_identifier, function_scope, handler)?;
|
||||||
|
|
||||||
|
let namespace = self.datapack.namespace_mut(&function_data.namespace);
|
||||||
|
|
||||||
|
if namespace.function(&modified_name).is_some() {
|
||||||
|
let err = TranspileError::ConflictingFunctionNames(ConflictingFunctionNames {
|
||||||
|
name: modified_name,
|
||||||
|
definition: identifier_span.clone(),
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
let function = namespace.function_mut(&modified_name);
|
||||||
|
function.get_commands_mut().extend(commands);
|
||||||
|
|
||||||
|
if function_data.annotations.contains_key("tick") {
|
||||||
|
self.datapack.add_tick(&function_location);
|
||||||
|
}
|
||||||
|
if function_data.annotations.contains_key("load") {
|
||||||
|
self.datapack.add_load(&function_location);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transpile_function(
|
||||||
|
&mut self,
|
||||||
|
statements: &[Statement],
|
||||||
|
program_identifier: &str,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<Vec<Command>> {
|
||||||
|
let mut errors = Vec::new();
|
||||||
|
let commands = statements
|
||||||
|
.iter()
|
||||||
|
.flat_map(|statement| {
|
||||||
|
self.transpile_statement(statement, program_identifier, scope, handler)
|
||||||
|
.unwrap_or_else(|err| {
|
||||||
|
errors.push(err);
|
||||||
|
Vec::new()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if !errors.is_empty() {
|
||||||
|
return Err(errors.remove(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(commands)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[expect(clippy::unused_self, clippy::needless_pass_by_ref_mut)]
|
||||||
|
fn transpile_comptime_function_arguments(
|
||||||
|
&mut self,
|
||||||
|
function_data: &FunctionData,
|
||||||
|
arguments: Option<&[&Expression]>,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<Vec<Option<ComptimeValue>>> {
|
||||||
|
let parameters = &function_data.parameters;
|
||||||
|
let arg_count = arguments.map(<[&Expression]>::len).unwrap_or_default();
|
||||||
|
|
||||||
|
let mut has_comptime = parameters
|
||||||
|
.iter()
|
||||||
|
.any(|param| matches!(param.variable_type(), FunctionVariableType::Value(_)));
|
||||||
|
|
||||||
|
if has_comptime {
|
||||||
|
if parameters.len() == arg_count {
|
||||||
|
let vals = parameters
|
||||||
|
.iter()
|
||||||
|
.zip(arguments.iter().flat_map(|args| args.iter()))
|
||||||
|
.map(|(param, arg)| match param.variable_type() {
|
||||||
|
FunctionVariableType::Value(_) => {
|
||||||
|
has_comptime = true;
|
||||||
|
let val = arg.comptime_eval(scope, handler)?;
|
||||||
|
Ok(Some(val))
|
||||||
|
}
|
||||||
|
_ => Ok(None),
|
||||||
|
})
|
||||||
|
.collect::<TranspileResult<Vec<Option<ComptimeValue>>>>()?;
|
||||||
|
|
||||||
|
Ok(vals)
|
||||||
|
} else {
|
||||||
|
let err = TranspileError::InvalidFunctionArguments(InvalidFunctionArguments {
|
||||||
|
expected: parameters.len(),
|
||||||
|
actual: arg_count,
|
||||||
|
span: function_data.identifier_span.clone(),
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(vec![None; parameters.len()])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[expect(clippy::too_many_lines)]
|
||||||
|
fn transpile_function_arguments(
|
||||||
|
&mut self,
|
||||||
|
function_data: &FunctionData,
|
||||||
|
function_location: &str,
|
||||||
|
arguments: Option<&[&Expression]>,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<TranspiledFunctionArguments> {
|
||||||
|
let parameters = &function_data.parameters;
|
||||||
|
let identifier_span = &function_data.identifier_span;
|
||||||
|
let arg_count = arguments.map(<[&Expression]>::len);
|
||||||
|
|
||||||
|
match arg_count {
|
||||||
|
Some(arg_count) if arg_count != parameters.len() => {
|
||||||
|
let err = TranspileError::InvalidFunctionArguments(InvalidFunctionArguments {
|
||||||
|
expected: parameters.len(),
|
||||||
|
actual: arg_count,
|
||||||
|
span: identifier_span.clone(),
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
Some(arg_count) if arg_count > 0 => {
|
||||||
|
#[derive(Debug, Clone, EnumAsInner)]
|
||||||
|
enum Parameter {
|
||||||
|
Comptime,
|
||||||
|
Static(MacroString),
|
||||||
|
Storage {
|
||||||
|
prepare_cmds: Vec<Command>,
|
||||||
|
storage_name: String,
|
||||||
|
path: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut compiled_args = Vec::<(Parameter, Span)>::new();
|
||||||
|
let mut errs = Vec::new();
|
||||||
|
|
||||||
|
for (expression, is_comptime) in arguments
|
||||||
|
.iter()
|
||||||
|
.flat_map(|expressions| expressions.iter())
|
||||||
|
.zip(
|
||||||
|
parameters
|
||||||
|
.iter()
|
||||||
|
.map(|p| matches!(p.variable_type(), FunctionVariableType::Value(_))),
|
||||||
|
)
|
||||||
|
{
|
||||||
|
let value = if is_comptime {
|
||||||
|
Ok(Parameter::Comptime)
|
||||||
|
} else {
|
||||||
|
match expression {
|
||||||
|
Expression::Primary(Primary::Lua(lua)) => {
|
||||||
|
lua.eval_comptime(scope, handler).and_then(|val| match val {
|
||||||
|
Ok(ComptimeValue::MacroString(s)) => Ok(Parameter::Static(s)),
|
||||||
|
Ok(val) => Ok(Parameter::Static(val.to_macro_string())),
|
||||||
|
Err(err) => {
|
||||||
|
let err = TranspileError::NotComptime(err);
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Expression::Primary(Primary::Integer(num)) => {
|
||||||
|
Ok(Parameter::Static(num.span.str().to_string().into()))
|
||||||
|
}
|
||||||
|
Expression::Primary(Primary::Boolean(bool)) => {
|
||||||
|
Ok(Parameter::Static(bool.span.str().to_string().into()))
|
||||||
|
}
|
||||||
|
Expression::Primary(Primary::StringLiteral(string)) => {
|
||||||
|
Ok(Parameter::Static(string.str_content().to_string().into()))
|
||||||
|
}
|
||||||
|
Expression::Primary(Primary::TemplateStringLiteral(literal)) => {
|
||||||
|
Ok(Parameter::Static(literal.to_macro_string(
|
||||||
|
Some(self),
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
)?))
|
||||||
|
}
|
||||||
|
Expression::Primary(primary @ Primary::Identifier(ident)) => {
|
||||||
|
let var =
|
||||||
|
scope.get_variable(ident.span.str()).ok_or_else(|| {
|
||||||
|
let err = TranspileError::UnknownIdentifier(
|
||||||
|
UnknownIdentifier::from_scope(ident.span(), scope),
|
||||||
|
);
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
err
|
||||||
|
})?;
|
||||||
|
match var.as_ref() {
|
||||||
|
VariableData::MacroParameter { macro_name, .. } => {
|
||||||
|
Ok(Parameter::Static(MacroString::MacroString {
|
||||||
|
parts: vec![MacroStringPart::MacroUsage(
|
||||||
|
macro_name.clone(),
|
||||||
|
)],
|
||||||
|
prepare_variables: BTreeMap::new(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
VariableData::BooleanStorage { .. }
|
||||||
|
| VariableData::ScoreboardValue { .. } => {
|
||||||
|
let (temp_storage, [temp_path]) =
|
||||||
|
self.get_temp_storage_locations_array();
|
||||||
|
let prepare_cmds = self.transpile_primary_expression(
|
||||||
|
primary,
|
||||||
|
&super::expression::DataLocation::Storage {
|
||||||
|
storage_name: temp_storage.clone(),
|
||||||
|
path: temp_path.clone(),
|
||||||
|
r#type: match var.as_ref() {
|
||||||
|
VariableData::BooleanStorage { .. } => {
|
||||||
|
StorageType::Boolean
|
||||||
|
}
|
||||||
|
VariableData::ScoreboardValue { .. } => {
|
||||||
|
StorageType::Int
|
||||||
|
}
|
||||||
|
_ => unreachable!("checked in parent match"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(Parameter::Storage {
|
||||||
|
prepare_cmds,
|
||||||
|
storage_name: temp_storage,
|
||||||
|
path: temp_path,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let err =
|
||||||
|
TranspileError::MismatchedTypes(MismatchedTypes {
|
||||||
|
expression: expression.span(),
|
||||||
|
expected_type: ExpectedType::AnyOf(vec![
|
||||||
|
ExpectedType::Integer,
|
||||||
|
ExpectedType::Boolean,
|
||||||
|
ExpectedType::String,
|
||||||
|
]),
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Expression::Primary(Primary::MemberAccess(member_access)) => {
|
||||||
|
if let Ok(value) = member_access.parent().comptime_member_access(
|
||||||
|
member_access,
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
) {
|
||||||
|
Ok(Parameter::Static(value.to_macro_string()))
|
||||||
|
} else {
|
||||||
|
let (storage_name, [path]) =
|
||||||
|
self.get_temp_storage_locations_array();
|
||||||
|
let prepare_cmds = self.transpile_expression(
|
||||||
|
expression,
|
||||||
|
&super::expression::DataLocation::Storage {
|
||||||
|
storage_name: storage_name.clone(),
|
||||||
|
path: path.clone(),
|
||||||
|
r#type: StorageType::Int,
|
||||||
|
},
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(Parameter::Storage {
|
||||||
|
prepare_cmds,
|
||||||
|
storage_name,
|
||||||
|
path,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Expression::Primary(
|
||||||
|
Primary::Parenthesized(_)
|
||||||
|
| Primary::Prefix(_)
|
||||||
|
| Primary::Indexed(_)
|
||||||
|
| Primary::FunctionCall(_),
|
||||||
|
)
|
||||||
|
| Expression::Binary(_) => {
|
||||||
|
let (temp_storage, [temp_path]) =
|
||||||
|
self.get_temp_storage_locations_array();
|
||||||
|
let prepare_cmds = self.transpile_expression(
|
||||||
|
expression,
|
||||||
|
&super::expression::DataLocation::Storage {
|
||||||
|
storage_name: temp_storage.clone(),
|
||||||
|
path: temp_path.clone(),
|
||||||
|
r#type: StorageType::Int,
|
||||||
|
},
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(Parameter::Storage {
|
||||||
|
prepare_cmds,
|
||||||
|
storage_name: temp_storage,
|
||||||
|
path: temp_path,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match value {
|
||||||
|
Ok(value) => {
|
||||||
|
compiled_args.push((value, expression.span()));
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
compiled_args.push((
|
||||||
|
Parameter::Static(MacroString::String(String::new())),
|
||||||
|
expression.span(),
|
||||||
|
));
|
||||||
|
errs.push(err.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(err) = errs.first() {
|
||||||
|
return Err(err.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
if compiled_args.iter().any(|(arg, _)| !arg.is_static()) {
|
||||||
|
let mut require_dyn_params = false;
|
||||||
|
let mut setup_cmds = Vec::new();
|
||||||
|
let mut move_cmds = Vec::new();
|
||||||
|
let mut statics = BTreeMap::new();
|
||||||
|
|
||||||
|
for (param, (data, span)) in parameters.clone().into_iter().zip(compiled_args) {
|
||||||
|
match param.variable_type() {
|
||||||
|
FunctionVariableType::Macro(_) => {
|
||||||
|
let arg_name =
|
||||||
|
crate::util::identifier_to_macro(param.identifier().span.str());
|
||||||
|
match data {
|
||||||
|
Parameter::Comptime => {}
|
||||||
|
Parameter::Static(s) => {
|
||||||
|
match s {
|
||||||
|
MacroString::String(value) => statics.insert(
|
||||||
|
arg_name.to_string(),
|
||||||
|
MacroString::String(
|
||||||
|
crate::util::escape_str(&value).to_string(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
MacroString::MacroString {
|
||||||
|
parts,
|
||||||
|
prepare_variables: preparation_cmds,
|
||||||
|
} => {
|
||||||
|
let parts = parts
|
||||||
|
.into_iter()
|
||||||
|
.map(|part| match part {
|
||||||
|
MacroStringPart::String(s) => {
|
||||||
|
MacroStringPart::String(
|
||||||
|
crate::util::escape_str(&s)
|
||||||
|
.to_string(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
MacroStringPart::MacroUsage(m) => {
|
||||||
|
MacroStringPart::MacroUsage(m)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
statics.insert(
|
||||||
|
arg_name.to_string(),
|
||||||
|
MacroString::MacroString {
|
||||||
|
parts,
|
||||||
|
prepare_variables: preparation_cmds,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Parameter::Storage {
|
||||||
|
prepare_cmds,
|
||||||
|
storage_name,
|
||||||
|
path,
|
||||||
|
} => {
|
||||||
|
require_dyn_params = true;
|
||||||
|
setup_cmds.extend(prepare_cmds);
|
||||||
|
move_cmds.push(Command::Raw(format!(
|
||||||
|
r"data modify storage shulkerscript:function_arguments {arg_name} set from storage {storage_name} {path}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FunctionVariableType::Integer(_) => {
|
||||||
|
let objective = format!(
|
||||||
|
"shu_arguments_{}",
|
||||||
|
function_location.replace(['/', ':'], "_")
|
||||||
|
);
|
||||||
|
let param_str = param.identifier().span.str();
|
||||||
|
let target =
|
||||||
|
crate::util::identifier_to_scoreboard_target(param_str);
|
||||||
|
|
||||||
|
match data {
|
||||||
|
Parameter::Comptime => {}
|
||||||
|
Parameter::Static(s) => match s.as_str() {
|
||||||
|
Ok(s) => {
|
||||||
|
if s.parse::<i32>().is_ok() {
|
||||||
|
move_cmds.push(Command::Raw(format!(
|
||||||
|
r"scoreboard players set {target} {objective} {s}"
|
||||||
|
)));
|
||||||
|
} else {
|
||||||
|
let err = TranspileError::MismatchedTypes(
|
||||||
|
MismatchedTypes {
|
||||||
|
expression: span,
|
||||||
|
expected_type: ExpectedType::Integer,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err((parts, prepare_variables)) => {
|
||||||
|
let (macro_string, prepare_variables) = MacroString::MacroString {
|
||||||
|
parts: std::iter::once(MacroStringPart::String(
|
||||||
|
format!(
|
||||||
|
"scoreboard players set {target} {objective} "
|
||||||
|
),
|
||||||
|
))
|
||||||
|
.chain(parts.iter().cloned())
|
||||||
|
.collect(),
|
||||||
|
prepare_variables: prepare_variables.to_owned(),
|
||||||
|
}
|
||||||
|
.into_sb();
|
||||||
|
let cmds = self
|
||||||
|
.transpile_commands_with_variable_macros(
|
||||||
|
vec![Command::UsesMacro(macro_string)],
|
||||||
|
prepare_variables,
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
move_cmds.extend(cmds);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Parameter::Storage {
|
||||||
|
prepare_cmds,
|
||||||
|
storage_name,
|
||||||
|
path,
|
||||||
|
} => {
|
||||||
|
setup_cmds.extend(prepare_cmds);
|
||||||
|
move_cmds.push(Command::Execute(Execute::Store(
|
||||||
|
format!("result score {target} {objective}").into(),
|
||||||
|
Box::new(Execute::Run(Box::new(Command::Raw(
|
||||||
|
format!("data get storage {storage_name} {path}"),
|
||||||
|
)))),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FunctionVariableType::Boolean(_) => {
|
||||||
|
require_dyn_params = true;
|
||||||
|
let target_storage_name = format!(
|
||||||
|
"shulkerscript:arguments_{}",
|
||||||
|
function_location.replace(['/', ':'], "_")
|
||||||
|
);
|
||||||
|
let param_str = param.identifier().span.str();
|
||||||
|
let target_path =
|
||||||
|
crate::util::identifier_to_scoreboard_target(param_str);
|
||||||
|
|
||||||
|
match data {
|
||||||
|
Parameter::Comptime => {}
|
||||||
|
Parameter::Static(s) => match s.as_str() {
|
||||||
|
Ok(s) => {
|
||||||
|
if let Ok(b) = s.parse::<bool>() {
|
||||||
|
move_cmds.push(Command::Raw(format!(
|
||||||
|
"data modify storage {target_storage_name} {target_path} set value {}",
|
||||||
|
if b { "1b" } else { "0b" }
|
||||||
|
)));
|
||||||
|
} else {
|
||||||
|
let err = TranspileError::MismatchedTypes(
|
||||||
|
MismatchedTypes {
|
||||||
|
expression: span,
|
||||||
|
expected_type: ExpectedType::Boolean,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err((parts, prepare_cmds)) => {
|
||||||
|
let (macro_string, prepare_variables) = MacroString::MacroString {
|
||||||
|
parts: std::iter::once(MacroStringPart::String(format!("data modify storage {target_storage_name} {target_path} set value ")))
|
||||||
|
.chain(parts.iter().cloned())
|
||||||
|
.collect(),
|
||||||
|
prepare_variables: prepare_cmds.to_owned(),
|
||||||
|
}
|
||||||
|
.into_sb();
|
||||||
|
let cmds = self
|
||||||
|
.transpile_commands_with_variable_macros(
|
||||||
|
vec![Command::UsesMacro(macro_string)],
|
||||||
|
prepare_variables,
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
move_cmds.extend(cmds);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Parameter::Storage {
|
||||||
|
prepare_cmds,
|
||||||
|
storage_name,
|
||||||
|
path,
|
||||||
|
} => {
|
||||||
|
setup_cmds.extend(prepare_cmds);
|
||||||
|
move_cmds.push(Command::Raw(format!("data modify storage {target_storage_name} {target_path} set from storage {storage_name} {path}")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FunctionVariableType::Value(_) => {
|
||||||
|
// handled before in `transpile_comptime_function_arguments`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let require_dyn_params = require_dyn_params;
|
||||||
|
let move_cmds = move_cmds;
|
||||||
|
let static_params = statics;
|
||||||
|
|
||||||
|
if require_dyn_params {
|
||||||
|
let statics_len = static_params.len();
|
||||||
|
let joined_statics = super::util::join_macro_strings(
|
||||||
|
static_params
|
||||||
|
.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, (k, v))| match v {
|
||||||
|
MacroString::String(s) => {
|
||||||
|
let mut s = format!(r#"{k}:"{s}""#);
|
||||||
|
if i < statics_len - 1 {
|
||||||
|
s.push(',');
|
||||||
|
}
|
||||||
|
MacroString::String(s)
|
||||||
|
}
|
||||||
|
MacroString::MacroString {
|
||||||
|
mut parts,
|
||||||
|
prepare_variables: preparation_cmds,
|
||||||
|
} => {
|
||||||
|
parts.insert(
|
||||||
|
0,
|
||||||
|
MacroStringPart::String(format!(r#"{k}:""#)),
|
||||||
|
);
|
||||||
|
let mut ending = '"'.to_string();
|
||||||
|
if i < statics_len - 1 {
|
||||||
|
ending.push(',');
|
||||||
|
}
|
||||||
|
parts.push(MacroStringPart::String(ending));
|
||||||
|
MacroString::MacroString {
|
||||||
|
parts,
|
||||||
|
prepare_variables: preparation_cmds,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
let storage_suffix = function_location.replace(['/', ':'], "_");
|
||||||
|
let statics_cmds = match joined_statics {
|
||||||
|
MacroString::String(s) => vec![Command::Raw(format!(
|
||||||
|
r"data merge storage shulkerscript:function_arguments_{storage_suffix} {{{s}}}"
|
||||||
|
))],
|
||||||
|
MacroString::MacroString { .. } => {
|
||||||
|
let prefix = MacroString::String(format!(
|
||||||
|
"data merge storage shulkerscript:function_arguments_{storage_suffix} {{"
|
||||||
|
));
|
||||||
|
let (macro_string, prepare_variables) =
|
||||||
|
super::util::join_macro_strings([
|
||||||
|
prefix,
|
||||||
|
joined_statics,
|
||||||
|
MacroString::String("}".to_string()),
|
||||||
|
])
|
||||||
|
.into_sb();
|
||||||
|
self.transpile_commands_with_variable_macros(
|
||||||
|
vec![Command::UsesMacro(macro_string)],
|
||||||
|
prepare_variables,
|
||||||
|
handler,
|
||||||
|
)?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
setup_cmds.extend(statics_cmds);
|
||||||
|
setup_cmds.extend(move_cmds);
|
||||||
|
|
||||||
|
Ok(TranspiledFunctionArguments::Dynamic(setup_cmds))
|
||||||
|
} else {
|
||||||
|
setup_cmds.extend(move_cmds);
|
||||||
|
|
||||||
|
Ok(TranspiledFunctionArguments::Static(
|
||||||
|
static_params,
|
||||||
|
setup_cmds,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let function_args =
|
||||||
|
parameters
|
||||||
|
.clone()
|
||||||
|
.into_iter()
|
||||||
|
.zip(compiled_args.into_iter().map(|(arg, _)| {
|
||||||
|
arg.into_static().expect("checked in if condition")
|
||||||
|
}))
|
||||||
|
.map(|(k, v)| (k.identifier().span.str().to_string(), v))
|
||||||
|
.collect();
|
||||||
|
Ok(TranspiledFunctionArguments::Static(
|
||||||
|
function_args,
|
||||||
|
Vec::new(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => Ok(TranspiledFunctionArguments::None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn comptime_args_hash(args: &[Option<ComptimeValue>]) -> String {
|
||||||
|
let combined = args
|
||||||
|
.iter()
|
||||||
|
.filter_map(Option::as_ref)
|
||||||
|
.map(|arg| match arg {
|
||||||
|
ComptimeValue::Boolean(b) => Cow::Owned(b.to_string()),
|
||||||
|
ComptimeValue::Integer(i) => Cow::Owned(i.to_string()),
|
||||||
|
ComptimeValue::String(s) => Cow::Borrowed(s.as_str()),
|
||||||
|
ComptimeValue::MacroString(s) => match s.as_str() {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err((parts, _)) => {
|
||||||
|
let s = parts
|
||||||
|
.iter()
|
||||||
|
.map(|p| match p {
|
||||||
|
MacroStringPart::String(s) => Cow::Borrowed(s.as_str()),
|
||||||
|
MacroStringPart::MacroUsage(u) => Cow::Owned(format!("`{u}`")),
|
||||||
|
})
|
||||||
|
.join("\0\0");
|
||||||
|
|
||||||
|
Cow::Owned(s)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.join("\0");
|
||||||
|
|
||||||
|
md5::hash(combined).to_hex_lowercase()
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,601 @@
|
||||||
|
//! Functions provided by the language itself.
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
ops::{Bound, Deref, RangeBounds},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
|
use cfg_if::cfg_if;
|
||||||
|
use shulkerbox::prelude::{Command, Execute};
|
||||||
|
|
||||||
|
use serde_json::{json, Value as JsonValue};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
base::{source_file::SourceElement as _, VoidHandler},
|
||||||
|
lexical::token::Identifier,
|
||||||
|
semantic::error::{InvalidFunctionArguments, UnexpectedExpression},
|
||||||
|
syntax::syntax_tree::expression::{
|
||||||
|
Expression, FunctionCall, Primary, TemplateStringLiteralPart,
|
||||||
|
},
|
||||||
|
transpile::{
|
||||||
|
error::{IllegalIndexing, IllegalIndexingReason, NotComptime, UnknownIdentifier},
|
||||||
|
expression::{ComptimeValue, DataLocation, ExpectedType, StorageType},
|
||||||
|
util::MacroString,
|
||||||
|
TranspileError,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{Scope, TranspileResult, Transpiler, VariableData};
|
||||||
|
|
||||||
|
/// A function that can be called from the language.
|
||||||
|
pub type InternalFunction =
|
||||||
|
fn(&mut Transpiler, &Arc<Scope>, &FunctionCall) -> TranspileResult<Vec<Command>>;
|
||||||
|
|
||||||
|
/// Adds all internal functions to the scope.
|
||||||
|
pub fn add_all_to_scope(scope: &Arc<Scope>) {
|
||||||
|
scope.set_variable(
|
||||||
|
"print",
|
||||||
|
VariableData::InternalFunction {
|
||||||
|
implementation: print_function,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_args_assert_in_range(
|
||||||
|
call: &FunctionCall,
|
||||||
|
range: impl RangeBounds<usize>,
|
||||||
|
) -> TranspileResult<Vec<&Expression>> {
|
||||||
|
let args = call
|
||||||
|
.arguments()
|
||||||
|
.as_ref()
|
||||||
|
.map(|args| args.elements().map(Deref::deref).collect::<Vec<_>>())
|
||||||
|
.unwrap_or_default();
|
||||||
|
if range.contains(&args.len()) {
|
||||||
|
Ok(args)
|
||||||
|
} else {
|
||||||
|
let span = args
|
||||||
|
.first()
|
||||||
|
.and_then(|first| {
|
||||||
|
args.last()
|
||||||
|
.map(|last| first.span().join(&last.span()).expect("invalid span"))
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
call.left_parenthesis()
|
||||||
|
.span()
|
||||||
|
.join(&call.right_parenthesis().span())
|
||||||
|
.expect("invalid span")
|
||||||
|
});
|
||||||
|
|
||||||
|
let actual = args.len();
|
||||||
|
let expected = match range.start_bound() {
|
||||||
|
Bound::Excluded(excluded) => (excluded + 1 > actual).then_some(excluded + 1),
|
||||||
|
Bound::Included(&included) => (included > actual).then_some(included),
|
||||||
|
Bound::Unbounded => None,
|
||||||
|
}
|
||||||
|
.or_else(|| match range.end_bound() {
|
||||||
|
Bound::Excluded(&excluded) => (excluded <= actual).then_some(excluded.wrapping_sub(1)),
|
||||||
|
Bound::Included(&included) => (included < actual).then_some(included),
|
||||||
|
Bound::Unbounded => None,
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
Err(TranspileError::InvalidFunctionArguments(
|
||||||
|
InvalidFunctionArguments {
|
||||||
|
expected,
|
||||||
|
actual: args.len(),
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[expect(clippy::too_many_lines)]
|
||||||
|
fn print_function(
|
||||||
|
transpiler: &mut Transpiler,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
call: &FunctionCall,
|
||||||
|
) -> TranspileResult<Vec<Command>> {
|
||||||
|
const PARAM_COLOR: &str = "gray";
|
||||||
|
|
||||||
|
fn get_identifier_part(
|
||||||
|
ident: &Identifier,
|
||||||
|
transpiler: &mut Transpiler,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
) -> TranspileResult<(bool, Option<Command>, JsonValue)> {
|
||||||
|
if let Some(var) = scope.get_variable(ident.span.str()).as_deref() {
|
||||||
|
match var {
|
||||||
|
VariableData::MacroParameter { macro_name, .. } => Ok((
|
||||||
|
true,
|
||||||
|
None,
|
||||||
|
json!({"text": format!("$({macro_name})"), "color": PARAM_COLOR}),
|
||||||
|
)),
|
||||||
|
VariableData::ScoreboardValue { objective, target } => {
|
||||||
|
let (cmd, value) = get_data_location(
|
||||||
|
&DataLocation::ScoreboardValue {
|
||||||
|
objective: objective.to_string(),
|
||||||
|
target: target.to_string(),
|
||||||
|
},
|
||||||
|
transpiler,
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok((false, cmd, value))
|
||||||
|
}
|
||||||
|
VariableData::BooleanStorage { storage_name, path } => {
|
||||||
|
let (cmd, value) = get_data_location(
|
||||||
|
&DataLocation::Storage {
|
||||||
|
storage_name: storage_name.to_string(),
|
||||||
|
path: path.to_string(),
|
||||||
|
r#type: StorageType::Boolean,
|
||||||
|
},
|
||||||
|
transpiler,
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok((false, cmd, value))
|
||||||
|
}
|
||||||
|
VariableData::ComptimeValue { value, .. } => {
|
||||||
|
let value = {
|
||||||
|
let guard = value.read().map_err(|_| {
|
||||||
|
TranspileError::NotComptime(NotComptime {
|
||||||
|
expression: ident.span(),
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
guard.as_ref().map_or_else(
|
||||||
|
|| "null".into(),
|
||||||
|
super::expression::ComptimeValue::to_macro_string,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
value.contains_macros(),
|
||||||
|
None,
|
||||||
|
json!({"text": value.to_string(), "color": PARAM_COLOR}),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
_ => Err(TranspileError::UnexpectedExpression(UnexpectedExpression(
|
||||||
|
Box::new(Expression::Primary(Primary::Identifier(ident.to_owned()))),
|
||||||
|
))),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(TranspileError::UnknownIdentifier(
|
||||||
|
UnknownIdentifier::from_scope(ident.span(), scope),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_indexed_part(
|
||||||
|
ident: &Identifier,
|
||||||
|
index: &Expression,
|
||||||
|
transpiler: &mut Transpiler,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
) -> TranspileResult<(bool, Option<Command>, JsonValue)> {
|
||||||
|
if let Some(var) = scope.get_variable(ident.span.str()).as_deref() {
|
||||||
|
match var {
|
||||||
|
VariableData::Scoreboard { objective } => {
|
||||||
|
let Ok(ComptimeValue::String(target)) =
|
||||||
|
index.comptime_eval(scope, &VoidHandler)
|
||||||
|
else {
|
||||||
|
return Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
reason: IllegalIndexingReason::InvalidComptimeType {
|
||||||
|
expected: ExpectedType::String,
|
||||||
|
},
|
||||||
|
expression: index.span(),
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
let (cmd, value) = get_data_location(
|
||||||
|
&DataLocation::ScoreboardValue {
|
||||||
|
objective: objective.to_string(),
|
||||||
|
target,
|
||||||
|
},
|
||||||
|
transpiler,
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok((false, cmd, value))
|
||||||
|
}
|
||||||
|
VariableData::ScoreboardArray { objective, targets } => {
|
||||||
|
let Ok(ComptimeValue::Integer(idx)) = index.comptime_eval(scope, &VoidHandler)
|
||||||
|
else {
|
||||||
|
return Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
reason: IllegalIndexingReason::InvalidComptimeType {
|
||||||
|
expected: ExpectedType::Integer,
|
||||||
|
},
|
||||||
|
expression: index.span(),
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
#[expect(clippy::option_if_let_else)]
|
||||||
|
if let Some(target) = usize::try_from(idx)
|
||||||
|
.ok()
|
||||||
|
.and_then(|index| targets.get(index))
|
||||||
|
{
|
||||||
|
let (cmd, value) = get_data_location(
|
||||||
|
&DataLocation::ScoreboardValue {
|
||||||
|
objective: objective.to_string(),
|
||||||
|
target: target.to_string(),
|
||||||
|
},
|
||||||
|
transpiler,
|
||||||
|
);
|
||||||
|
Ok((false, cmd, value))
|
||||||
|
} else {
|
||||||
|
Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
reason: IllegalIndexingReason::IndexOutOfBounds {
|
||||||
|
index: usize::try_from(idx).unwrap_or(usize::MAX),
|
||||||
|
length: targets.len(),
|
||||||
|
},
|
||||||
|
expression: index.span(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VariableData::BooleanStorageArray {
|
||||||
|
storage_name,
|
||||||
|
paths,
|
||||||
|
} => {
|
||||||
|
let Ok(ComptimeValue::Integer(idx)) = index.comptime_eval(scope, &VoidHandler)
|
||||||
|
else {
|
||||||
|
return Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
reason: IllegalIndexingReason::InvalidComptimeType {
|
||||||
|
expected: ExpectedType::Integer,
|
||||||
|
},
|
||||||
|
expression: index.span(),
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
#[expect(clippy::option_if_let_else)]
|
||||||
|
if let Some(path) = usize::try_from(idx).ok().and_then(|index| paths.get(index))
|
||||||
|
{
|
||||||
|
let (cmd, value) = get_data_location(
|
||||||
|
&DataLocation::Storage {
|
||||||
|
storage_name: storage_name.to_string(),
|
||||||
|
path: path.to_string(),
|
||||||
|
r#type: StorageType::Boolean,
|
||||||
|
},
|
||||||
|
transpiler,
|
||||||
|
);
|
||||||
|
Ok((false, cmd, value))
|
||||||
|
} else {
|
||||||
|
Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
reason: IllegalIndexingReason::IndexOutOfBounds {
|
||||||
|
index: usize::try_from(idx).unwrap_or(usize::MAX),
|
||||||
|
length: paths.len(),
|
||||||
|
},
|
||||||
|
expression: index.span(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VariableData::Tag { tag_name } => {
|
||||||
|
let Ok(ComptimeValue::String(entity)) =
|
||||||
|
index.comptime_eval(scope, &VoidHandler)
|
||||||
|
else {
|
||||||
|
return Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
reason: IllegalIndexingReason::InvalidComptimeType {
|
||||||
|
expected: ExpectedType::String,
|
||||||
|
},
|
||||||
|
expression: index.span(),
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
let (cmd, value) = get_data_location(
|
||||||
|
&DataLocation::Tag {
|
||||||
|
tag_name: tag_name.clone(),
|
||||||
|
entity,
|
||||||
|
},
|
||||||
|
transpiler,
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok((false, cmd, value))
|
||||||
|
}
|
||||||
|
_ => Err(TranspileError::UnexpectedExpression(UnexpectedExpression(
|
||||||
|
Box::new(Expression::Primary(Primary::Identifier(ident.to_owned()))),
|
||||||
|
))),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(TranspileError::UnknownIdentifier(
|
||||||
|
UnknownIdentifier::from_scope(ident.span(), scope),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_data_location(
|
||||||
|
location: &DataLocation,
|
||||||
|
transpiler: &mut Transpiler,
|
||||||
|
) -> (Option<Command>, JsonValue) {
|
||||||
|
match location {
|
||||||
|
DataLocation::ScoreboardValue { objective, target } => (
|
||||||
|
None,
|
||||||
|
json!({"score": {"name": target, "objective": objective}, "color": PARAM_COLOR}),
|
||||||
|
),
|
||||||
|
DataLocation::Storage {
|
||||||
|
storage_name, path, ..
|
||||||
|
} => (
|
||||||
|
None,
|
||||||
|
json!({"nbt": path, "storage": storage_name, "color": PARAM_COLOR}),
|
||||||
|
),
|
||||||
|
DataLocation::Tag { tag_name, entity } => {
|
||||||
|
let (temp_storage_name, [temp_storage_path]) =
|
||||||
|
transpiler.get_temp_storage_locations_array();
|
||||||
|
let selector =
|
||||||
|
super::util::add_to_entity_selector(entity, &format!("tag={tag_name}"));
|
||||||
|
let cmd = Command::Execute(Execute::Store(
|
||||||
|
format!("success storage {temp_storage_name} {temp_storage_path} byte 1.0")
|
||||||
|
.into(),
|
||||||
|
Box::new(Execute::Run(Box::new(Command::Raw(format!(
|
||||||
|
"execute if entity {selector}"
|
||||||
|
))))),
|
||||||
|
));
|
||||||
|
|
||||||
|
(
|
||||||
|
Some(cmd),
|
||||||
|
json!({"nbt": temp_storage_path, "storage": temp_storage_name, "color": PARAM_COLOR}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let args = get_args_assert_in_range(call, 1..=2)?;
|
||||||
|
let first = args.first().expect("checked range");
|
||||||
|
let (target, message_expression) = if let Some(second) = args.get(1) {
|
||||||
|
(
|
||||||
|
first
|
||||||
|
.comptime_eval(scope, &VoidHandler)
|
||||||
|
.map(|val| val.to_macro_string())
|
||||||
|
.map_err(TranspileError::NotComptime)?,
|
||||||
|
second,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
("@a".into(), first)
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut contains_macro = target.contains_macros();
|
||||||
|
|
||||||
|
let (mut cmds, parts) = match message_expression {
|
||||||
|
Expression::Primary(primary) => match primary {
|
||||||
|
Primary::Boolean(boolean) => Ok((
|
||||||
|
Vec::new(),
|
||||||
|
vec![JsonValue::String(boolean.value().to_string())],
|
||||||
|
)),
|
||||||
|
Primary::Integer(integer) => Ok((
|
||||||
|
Vec::new(),
|
||||||
|
vec![JsonValue::String(integer.as_i64().to_string())],
|
||||||
|
)),
|
||||||
|
Primary::StringLiteral(string) => Ok((
|
||||||
|
Vec::new(),
|
||||||
|
vec![JsonValue::String(string.str_content().to_string())],
|
||||||
|
)),
|
||||||
|
#[cfg_attr(not(feature = "lua"), expect(unused_variables))]
|
||||||
|
Primary::Lua(lua) => {
|
||||||
|
cfg_if! {
|
||||||
|
if #[cfg(feature = "lua")] {
|
||||||
|
let (ret, _lua) = lua.eval(scope, &VoidHandler)?;
|
||||||
|
Ok((
|
||||||
|
Vec::new(),
|
||||||
|
vec![JsonValue::String(ret.to_string().map_err(|err| {
|
||||||
|
TranspileError::LuaRuntimeError(super::error::LuaRuntimeError::from_lua_err(
|
||||||
|
&err,
|
||||||
|
lua.span(),
|
||||||
|
))
|
||||||
|
})?)],
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
Err(TranspileError::LuaDisabled)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Primary::Identifier(ident) => {
|
||||||
|
let (cur_contains_macro, cmd, part) =
|
||||||
|
get_identifier_part(ident, transpiler, scope)?;
|
||||||
|
contains_macro |= cur_contains_macro;
|
||||||
|
Ok((cmd.into_iter().collect(), vec![part]))
|
||||||
|
}
|
||||||
|
Primary::Indexed(indexed) => match indexed.object().as_ref() {
|
||||||
|
Primary::Identifier(ident) => {
|
||||||
|
match scope.get_variable(ident.span.str()).as_deref() {
|
||||||
|
Some(VariableData::Scoreboard { objective }) => {
|
||||||
|
if let Ok(ComptimeValue::String(index)) =
|
||||||
|
indexed.index().comptime_eval(scope, &VoidHandler)
|
||||||
|
{
|
||||||
|
let (cmd, value) = get_data_location(
|
||||||
|
&DataLocation::ScoreboardValue {
|
||||||
|
objective: objective.to_string(),
|
||||||
|
target: index,
|
||||||
|
},
|
||||||
|
transpiler,
|
||||||
|
);
|
||||||
|
Ok((cmd.into_iter().collect(), vec![value]))
|
||||||
|
} else {
|
||||||
|
// TODO: allow macro string, but throw error when index is not constant string
|
||||||
|
Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
reason: IllegalIndexingReason::InvalidComptimeType {
|
||||||
|
expected: ExpectedType::String,
|
||||||
|
},
|
||||||
|
expression: indexed.index().span(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(VariableData::ScoreboardArray { objective, targets }) => {
|
||||||
|
if let Ok(ComptimeValue::Integer(index)) =
|
||||||
|
indexed.index().comptime_eval(scope, &VoidHandler)
|
||||||
|
{
|
||||||
|
#[expect(clippy::option_if_let_else)]
|
||||||
|
if let Some(target) = usize::try_from(index)
|
||||||
|
.ok()
|
||||||
|
.and_then(|index| targets.get(index))
|
||||||
|
{
|
||||||
|
let (cmd, value) = get_data_location(
|
||||||
|
&DataLocation::ScoreboardValue {
|
||||||
|
objective: objective.to_string(),
|
||||||
|
target: target.to_string(),
|
||||||
|
},
|
||||||
|
transpiler,
|
||||||
|
);
|
||||||
|
Ok((cmd.into_iter().collect(), vec![value]))
|
||||||
|
} else {
|
||||||
|
Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
reason: IllegalIndexingReason::IndexOutOfBounds {
|
||||||
|
index: usize::try_from(index).unwrap_or(usize::MAX),
|
||||||
|
length: targets.len(),
|
||||||
|
},
|
||||||
|
expression: indexed.index().span(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
reason: IllegalIndexingReason::InvalidComptimeType {
|
||||||
|
expected: ExpectedType::Integer,
|
||||||
|
},
|
||||||
|
expression: indexed.index().span(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(VariableData::BooleanStorageArray {
|
||||||
|
storage_name,
|
||||||
|
paths,
|
||||||
|
}) => {
|
||||||
|
if let Ok(ComptimeValue::Integer(index)) =
|
||||||
|
indexed.index().comptime_eval(scope, &VoidHandler)
|
||||||
|
{
|
||||||
|
#[expect(clippy::option_if_let_else)]
|
||||||
|
if let Some(path) = usize::try_from(index)
|
||||||
|
.ok()
|
||||||
|
.and_then(|index| paths.get(index))
|
||||||
|
{
|
||||||
|
let (cmd, value) = get_data_location(
|
||||||
|
&DataLocation::Storage {
|
||||||
|
storage_name: storage_name.to_string(),
|
||||||
|
path: path.to_string(),
|
||||||
|
r#type: StorageType::Boolean,
|
||||||
|
},
|
||||||
|
transpiler,
|
||||||
|
);
|
||||||
|
Ok((cmd.into_iter().collect(), vec![value]))
|
||||||
|
} else {
|
||||||
|
Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
reason: IllegalIndexingReason::IndexOutOfBounds {
|
||||||
|
index: usize::try_from(index).unwrap_or(usize::MAX),
|
||||||
|
length: paths.len(),
|
||||||
|
},
|
||||||
|
expression: indexed.index().span(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
reason: IllegalIndexingReason::InvalidComptimeType {
|
||||||
|
expected: ExpectedType::Integer,
|
||||||
|
},
|
||||||
|
expression: indexed.index().span(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
reason: IllegalIndexingReason::NotIndexable,
|
||||||
|
expression: indexed.object().span(),
|
||||||
|
})),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => Err(TranspileError::IllegalIndexing(IllegalIndexing {
|
||||||
|
expression: indexed.object().span(),
|
||||||
|
reason: IllegalIndexingReason::NotIdentifier,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
Primary::TemplateStringLiteral(template_string) => {
|
||||||
|
let mut cmds = Vec::new();
|
||||||
|
let mut parts = Vec::new();
|
||||||
|
for part in template_string.parts() {
|
||||||
|
match part {
|
||||||
|
TemplateStringLiteralPart::Text(text) => {
|
||||||
|
parts.push(JsonValue::String(text.span.str().to_string()));
|
||||||
|
}
|
||||||
|
TemplateStringLiteralPart::Expression { expression, .. } => {
|
||||||
|
match expression.as_ref() {
|
||||||
|
Expression::Primary(Primary::Identifier(identifier)) => {
|
||||||
|
let (cur_contains_macro, cur_cmds, part) =
|
||||||
|
get_identifier_part(identifier, transpiler, scope)?;
|
||||||
|
contains_macro |= cur_contains_macro;
|
||||||
|
cmds.extend(cur_cmds);
|
||||||
|
parts.push(part);
|
||||||
|
}
|
||||||
|
Expression::Primary(Primary::Indexed(indexed)) => {
|
||||||
|
match indexed.object().as_ref() {
|
||||||
|
Primary::Identifier(ident) => {
|
||||||
|
let (cur_contains_macro, cur_cmds, part) =
|
||||||
|
get_indexed_part(
|
||||||
|
ident,
|
||||||
|
indexed.index(),
|
||||||
|
transpiler,
|
||||||
|
scope,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
contains_macro |= cur_contains_macro;
|
||||||
|
cmds.extend(cur_cmds);
|
||||||
|
parts.push(part);
|
||||||
|
}
|
||||||
|
_ => todo!("other expression in indexed"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => todo!("other expression in template string literal"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok((cmds, parts))
|
||||||
|
}
|
||||||
|
|
||||||
|
primary => {
|
||||||
|
let (storage_name, [storage_path]) = transpiler.get_temp_storage_locations_array();
|
||||||
|
let location = DataLocation::Storage {
|
||||||
|
storage_name,
|
||||||
|
path: storage_path,
|
||||||
|
r#type: StorageType::Int,
|
||||||
|
};
|
||||||
|
let cmds = transpiler.transpile_primary_expression(
|
||||||
|
primary,
|
||||||
|
&location,
|
||||||
|
scope,
|
||||||
|
&VoidHandler,
|
||||||
|
)?;
|
||||||
|
let (cmd, part) = get_data_location(&location, transpiler);
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
cmds.into_iter().chain(cmd.into_iter()).collect(),
|
||||||
|
vec![part],
|
||||||
|
))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Expression::Binary(binary) => {
|
||||||
|
let (storage_name, [storage_path]) = transpiler.get_temp_storage_locations_array();
|
||||||
|
let location = DataLocation::Storage {
|
||||||
|
storage_name,
|
||||||
|
path: storage_path,
|
||||||
|
r#type: StorageType::Int,
|
||||||
|
};
|
||||||
|
let cmds =
|
||||||
|
transpiler.transpile_binary_expression(binary, &location, scope, &VoidHandler)?;
|
||||||
|
let (cmd, part) = get_data_location(&location, transpiler);
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
cmds.into_iter().chain(cmd.into_iter()).collect(),
|
||||||
|
vec![part],
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}?;
|
||||||
|
|
||||||
|
// TODO: prepend prefix with datapack name to parts and remove following
|
||||||
|
let print_args = if parts.len() == 1 {
|
||||||
|
serde_json::to_string(&parts[0]).expect("json serialization failed")
|
||||||
|
} else {
|
||||||
|
serde_json::to_string(&parts).expect("json serialization failed")
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: throw correct error
|
||||||
|
let cmd = format!("tellraw {target} {print_args}");
|
||||||
|
|
||||||
|
let cmd = if contains_macro {
|
||||||
|
let (macro_string, _) = cmd.parse::<MacroString>().expect("cannot fail").into_sb();
|
||||||
|
Command::UsesMacro(macro_string)
|
||||||
|
} else {
|
||||||
|
Command::Raw(cmd)
|
||||||
|
};
|
||||||
|
|
||||||
|
cmds.push(cmd);
|
||||||
|
|
||||||
|
Ok(cmds)
|
||||||
|
}
|
||||||
|
|
@ -1,22 +1,36 @@
|
||||||
//! Executes the Lua code and returns the resulting command.
|
//! Executes the Lua code and returns the resulting command.
|
||||||
|
|
||||||
#[cfg(feature = "lua")]
|
#[cfg(all(feature = "lua", feature = "shulkerbox"))]
|
||||||
mod enabled {
|
mod enabled {
|
||||||
use mlua::Lua;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use mlua::{Lua, Table, Value};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
base::{self, source_file::SourceElement, Handler},
|
base::{self, source_file::SourceElement, Handler},
|
||||||
|
lexical::token::Identifier,
|
||||||
syntax::syntax_tree::expression::LuaCode,
|
syntax::syntax_tree::expression::LuaCode,
|
||||||
transpile::error::{TranspileError, TranspileResult},
|
transpile::{
|
||||||
|
error::{
|
||||||
|
InvalidArgument, LuaRuntimeError, MismatchedTypes, NotComptime, TranspileError,
|
||||||
|
TranspileResult, UnknownIdentifier,
|
||||||
|
},
|
||||||
|
expression::{ComptimeValue, ExpectedType},
|
||||||
|
Scope, VariableData,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl LuaCode {
|
impl LuaCode {
|
||||||
/// Evaluates the Lua code and returns the resulting command.
|
/// Evaluated the Lua code and returns the resulting value.
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// - If Lua code evaluation is disabled.
|
/// - If evaluation fails
|
||||||
#[tracing::instrument(level = "debug", name = "eval_lua", skip_all, ret)]
|
#[tracing::instrument(level = "debug", name = "eval_lua", skip_all, ret)]
|
||||||
pub fn eval_string(&self, handler: &impl Handler<base::Error>) -> TranspileResult<String> {
|
pub fn eval(
|
||||||
|
&self,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<(mlua::Value, mlua::Lua)> {
|
||||||
tracing::debug!("Evaluating Lua code");
|
tracing::debug!("Evaluating Lua code");
|
||||||
|
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
|
|
@ -24,7 +38,7 @@ mod enabled {
|
||||||
let name = {
|
let name = {
|
||||||
let span = self.span();
|
let span = self.span();
|
||||||
let file = span.source_file();
|
let file = span.source_file();
|
||||||
let path = file.path();
|
let path = file.path_relative().unwrap_or_else(|| file.path().clone());
|
||||||
|
|
||||||
let start = span.start_location();
|
let start = span.start_location();
|
||||||
let end = span.end_location().unwrap_or_else(|| {
|
let end = span.end_location().unwrap_or_else(|| {
|
||||||
|
|
@ -43,49 +57,407 @@ mod enabled {
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let lua_result = lua
|
self.add_globals(&lua, scope)
|
||||||
|
.inspect_err(|err| handler.receive(Box::new(err.clone())))?;
|
||||||
|
|
||||||
|
let res = lua
|
||||||
.load(self.code())
|
.load(self.code())
|
||||||
.set_name(name)
|
.set_name(name)
|
||||||
.eval::<String>()
|
.eval::<Value>()
|
||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
let err = TranspileError::from(err);
|
let err =
|
||||||
handler.receive(err.clone());
|
TranspileError::from(LuaRuntimeError::from_lua_err(&err, self.span()));
|
||||||
|
handler.receive(crate::Error::from(Box::new(err.clone())));
|
||||||
err
|
err
|
||||||
})?;
|
});
|
||||||
|
|
||||||
Ok(lua_result)
|
res.map(|v| {
|
||||||
|
tracing::debug!("Lua code evaluated successfully");
|
||||||
|
(v, lua)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl From<mlua::Error> for TranspileError {
|
/// Evaluates the Lua code and returns the resulting [`ComptimeValue`].
|
||||||
fn from(value: mlua::Error) -> Self {
|
///
|
||||||
let string = value.to_string();
|
/// # Errors
|
||||||
Self::LuaRuntimeError(
|
/// - If Lua code evaluation is disabled.
|
||||||
string
|
#[tracing::instrument(level = "debug", name = "eval_lua", skip_all, ret)]
|
||||||
.strip_prefix("runtime error: ")
|
pub fn eval_comptime(
|
||||||
.unwrap_or(&string)
|
&self,
|
||||||
.to_string(),
|
scope: &Arc<Scope>,
|
||||||
)
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<Result<ComptimeValue, NotComptime>> {
|
||||||
|
if let Some(res) = self.eval_result.get().cloned() {
|
||||||
|
return Ok(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
// required to keep the lua instance alive
|
||||||
|
let (lua_result, _lua) = self.eval(scope, handler)?;
|
||||||
|
|
||||||
|
let res = self.handle_lua_result(lua_result, handler).map(|res| {
|
||||||
|
res.ok_or_else(|| NotComptime {
|
||||||
|
expression: self.span(),
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
|
||||||
|
self.eval_result.set(res.clone()).ok();
|
||||||
|
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_globals(&self, lua: &Lua, scope: &Arc<Scope>) -> TranspileResult<()> {
|
||||||
|
let globals = lua.globals();
|
||||||
|
|
||||||
|
let shulkerscript_globals = self
|
||||||
|
.get_std_library(lua)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
|
||||||
|
if let Some(inputs) = self.inputs() {
|
||||||
|
for identifier in inputs.elements() {
|
||||||
|
let (name, value) = self.add_input_to_globals(identifier, lua, scope)?;
|
||||||
|
globals
|
||||||
|
.set(name, value)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
globals
|
||||||
|
.set("shulkerscript", shulkerscript_globals)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_std_library(&self, lua: &Lua) -> mlua::Result<Table> {
|
||||||
|
let table = lua.create_table()?;
|
||||||
|
|
||||||
|
let (location_path, location_start, location_end) = {
|
||||||
|
let span = self.span();
|
||||||
|
let file = span.source_file();
|
||||||
|
let path = file.path().to_owned();
|
||||||
|
let start_location = span.start_location();
|
||||||
|
let end_location = span.end_location().unwrap_or_else(|| {
|
||||||
|
let line_amount = file.line_amount();
|
||||||
|
let column = file.get_line(line_amount).expect("line amount used").len();
|
||||||
|
|
||||||
|
crate::base::source_file::Location {
|
||||||
|
line: line_amount,
|
||||||
|
column,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
(path, start_location, end_location)
|
||||||
|
};
|
||||||
|
|
||||||
|
table.set("file_path", location_path.to_string_lossy())?;
|
||||||
|
table.set("start_line", location_start.line)?;
|
||||||
|
table.set("start_column", location_start.column)?;
|
||||||
|
table.set("end_line", location_end.line)?;
|
||||||
|
table.set("end_column", location_end.column)?;
|
||||||
|
|
||||||
|
table.set("version", crate::VERSION)?;
|
||||||
|
|
||||||
|
// TODO: add functions for requesting data/scoreboard locations
|
||||||
|
|
||||||
|
Ok(table)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[expect(clippy::too_many_lines)]
|
||||||
|
fn add_input_to_globals<'a>(
|
||||||
|
&self,
|
||||||
|
identifier: &'a Identifier,
|
||||||
|
lua: &Lua,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
) -> TranspileResult<(&'a str, Value)> {
|
||||||
|
let name = identifier.span.str();
|
||||||
|
let value = match scope.get_variable(name).as_deref() {
|
||||||
|
Some(VariableData::MacroParameter { macro_name, .. }) => Value::String(
|
||||||
|
lua.create_string(format!("$({macro_name})"))
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?,
|
||||||
|
),
|
||||||
|
Some(VariableData::Scoreboard { objective }) => {
|
||||||
|
let table = lua
|
||||||
|
.create_table()
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
table
|
||||||
|
.set("objective", objective.as_str())
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
Value::Table(table)
|
||||||
|
}
|
||||||
|
Some(VariableData::ScoreboardValue { objective, target }) => {
|
||||||
|
let table = lua
|
||||||
|
.create_table()
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
table
|
||||||
|
.set(
|
||||||
|
"objective",
|
||||||
|
lua.create_string(objective)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?,
|
||||||
|
)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
table
|
||||||
|
.set(
|
||||||
|
"target",
|
||||||
|
lua.create_string(target)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?,
|
||||||
|
)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
Value::Table(table)
|
||||||
|
}
|
||||||
|
Some(VariableData::ScoreboardArray { objective, targets }) => {
|
||||||
|
let table = lua
|
||||||
|
.create_table()
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
table
|
||||||
|
.set("objective", objective.as_str())
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
let values = lua
|
||||||
|
.create_table_from(
|
||||||
|
targets
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, target)| (i + 1, target.as_str())),
|
||||||
|
)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
table
|
||||||
|
.set("targets", values)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
Value::Table(table)
|
||||||
|
}
|
||||||
|
Some(VariableData::BooleanStorage { storage_name, path }) => {
|
||||||
|
let table = lua
|
||||||
|
.create_table()
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
table
|
||||||
|
.set(
|
||||||
|
"storage",
|
||||||
|
lua.create_string(storage_name)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?,
|
||||||
|
)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
table
|
||||||
|
.set(
|
||||||
|
"path",
|
||||||
|
lua.create_string(path)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?,
|
||||||
|
)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
Value::Table(table)
|
||||||
|
}
|
||||||
|
Some(VariableData::BooleanStorageArray {
|
||||||
|
storage_name,
|
||||||
|
paths,
|
||||||
|
}) => {
|
||||||
|
let table = lua
|
||||||
|
.create_table()
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
table
|
||||||
|
.set("storage", storage_name.as_str())
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
let values = lua
|
||||||
|
.create_table_from(
|
||||||
|
paths
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, path)| (i + 1, path.as_str())),
|
||||||
|
)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
table
|
||||||
|
.set("paths", values)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
Value::Table(table)
|
||||||
|
}
|
||||||
|
Some(VariableData::Tag { tag_name }) => {
|
||||||
|
let table = lua
|
||||||
|
.create_table()
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
table
|
||||||
|
.set("name", tag_name.as_str())
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?;
|
||||||
|
Value::Table(table)
|
||||||
|
}
|
||||||
|
Some(VariableData::ComptimeValue {
|
||||||
|
value,
|
||||||
|
read_only: _,
|
||||||
|
}) => {
|
||||||
|
let value = value.read().unwrap();
|
||||||
|
match &*value {
|
||||||
|
Some(ComptimeValue::Boolean(b)) => Value::Boolean(*b),
|
||||||
|
Some(ComptimeValue::Integer(i)) => Value::Integer(*i),
|
||||||
|
Some(ComptimeValue::String(s)) => Value::String(
|
||||||
|
lua.create_string(s)
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?,
|
||||||
|
),
|
||||||
|
Some(ComptimeValue::MacroString(s)) => Value::String(
|
||||||
|
lua.create_string(s.to_string())
|
||||||
|
.map_err(|err| LuaRuntimeError::from_lua_err(&err, self.span()))?,
|
||||||
|
),
|
||||||
|
None => Value::Nil,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(VariableData::Function { .. } | VariableData::InternalFunction { .. }) => {
|
||||||
|
// TODO: add support for functions
|
||||||
|
return Err(TranspileError::InvalidArgument(InvalidArgument {
|
||||||
|
reason: "functions cannot be passed to Lua".to_string(),
|
||||||
|
span: identifier.span(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
return Err(TranspileError::UnknownIdentifier(
|
||||||
|
UnknownIdentifier::from_scope(identifier.span(), scope),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok((name, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_lua_result(
|
||||||
|
&self,
|
||||||
|
value: Value,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<Option<ComptimeValue>> {
|
||||||
|
match value {
|
||||||
|
Value::Nil => Ok(None),
|
||||||
|
Value::String(s) => Ok(Some(ComptimeValue::String(s.to_string_lossy()))),
|
||||||
|
Value::Integer(i) => Ok(Some(ComptimeValue::Integer(i))),
|
||||||
|
// TODO: change when floating point comptime numbers are supported
|
||||||
|
Value::Number(n) => Ok(Some(ComptimeValue::String(n.to_string()))),
|
||||||
|
Value::Function(f) => self.handle_lua_result(
|
||||||
|
f.call(()).map_err(|err| {
|
||||||
|
TranspileError::LuaRuntimeError(LuaRuntimeError::from_lua_err(
|
||||||
|
&err,
|
||||||
|
self.span(),
|
||||||
|
))
|
||||||
|
})?,
|
||||||
|
handler,
|
||||||
|
),
|
||||||
|
Value::Boolean(boolean) => Ok(Some(ComptimeValue::Boolean(boolean))),
|
||||||
|
Value::Table(table) => {
|
||||||
|
// TODO: allow to return arrays when comptime arrays are implemented
|
||||||
|
match table.get::<Value>("value") {
|
||||||
|
Ok(Value::Nil) => {
|
||||||
|
let err = TranspileError::LuaRuntimeError(LuaRuntimeError {
|
||||||
|
code_block: self.span(),
|
||||||
|
error_message: "return table must contain non-nil 'value'"
|
||||||
|
.to_string(),
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
Ok(value) => {
|
||||||
|
let value = match self.handle_lua_result(value, handler)? {
|
||||||
|
Some(ComptimeValue::String(s)) => {
|
||||||
|
let contains_macro = match table.get::<Value>("contains_macro")
|
||||||
|
{
|
||||||
|
Ok(Value::Boolean(boolean)) => Ok(boolean),
|
||||||
|
Ok(value) => {
|
||||||
|
if let Some(ComptimeValue::Boolean(boolean)) =
|
||||||
|
self.handle_lua_result(value, handler)?
|
||||||
|
{
|
||||||
|
Ok(boolean)
|
||||||
|
} else {
|
||||||
|
let err = TranspileError::MismatchedTypes(
|
||||||
|
MismatchedTypes {
|
||||||
|
expression: self.span(),
|
||||||
|
expected_type: ExpectedType::Boolean,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let err =
|
||||||
|
TranspileError::MismatchedTypes(MismatchedTypes {
|
||||||
|
expression: self.span(),
|
||||||
|
expected_type: ExpectedType::Boolean,
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}?;
|
||||||
|
|
||||||
|
if contains_macro {
|
||||||
|
Some(ComptimeValue::MacroString(
|
||||||
|
s.parse().expect("parsing cannot fail"),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
Some(ComptimeValue::String(s))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
value => value,
|
||||||
|
};
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
let err = TranspileError::LuaRuntimeError(
|
||||||
|
LuaRuntimeError::from_lua_err(&err, self.span()),
|
||||||
|
);
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::Error(_)
|
||||||
|
| Value::Thread(_)
|
||||||
|
| Value::UserData(_)
|
||||||
|
| Value::LightUserData(_)
|
||||||
|
| Value::Other(..) => {
|
||||||
|
let err = TranspileError::LuaRuntimeError(LuaRuntimeError {
|
||||||
|
code_block: self.span(),
|
||||||
|
error_message: format!("invalid return type {}", value.type_name()),
|
||||||
|
});
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "lua"))]
|
#[cfg(all(not(feature = "lua"), feature = "shulkerbox"))]
|
||||||
mod disabled {
|
mod disabled {
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
base::{self, Handler},
|
base::{self, Handler},
|
||||||
syntax::syntax_tree::expression::LuaCode,
|
syntax::syntax_tree::expression::LuaCode,
|
||||||
transpile::error::{TranspileError, TranspileResult},
|
transpile::error::{TranspileError, TranspileResult},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::transpile::{expression::ComptimeValue, Scope};
|
||||||
|
|
||||||
impl LuaCode {
|
impl LuaCode {
|
||||||
|
/// Will always return an error because Lua code evaluation is disabled.
|
||||||
|
/// Enable the feature `lua` to enable Lua code evaluation.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// - Always, as the lua feature is disabled
|
||||||
|
#[tracing::instrument(level = "debug", name = "eval_lua", skip_all, ret)]
|
||||||
|
pub fn eval(
|
||||||
|
&self,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<((), ())> {
|
||||||
|
let _ = scope;
|
||||||
|
handler.receive(Box::new(TranspileError::LuaDisabled));
|
||||||
|
tracing::error!("Lua code evaluation is disabled");
|
||||||
|
Err(TranspileError::LuaDisabled)
|
||||||
|
}
|
||||||
|
|
||||||
/// Will always return an error because Lua code evaluation is disabled.
|
/// Will always return an error because Lua code evaluation is disabled.
|
||||||
/// Enable the feature `lua` to enable Lua code evaluation.
|
/// Enable the feature `lua` to enable Lua code evaluation.
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// - If Lua code evaluation is disabled.
|
/// - If Lua code evaluation is disabled.
|
||||||
pub fn eval_string(&self, handler: &impl Handler<base::Error>) -> TranspileResult<String> {
|
pub fn eval_comptime(
|
||||||
handler.receive(TranspileError::LuaDisabled);
|
&self,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<Result<ComptimeValue, crate::transpile::error::NotComptime>> {
|
||||||
|
let _ = scope;
|
||||||
|
handler.receive(Box::new(TranspileError::LuaDisabled));
|
||||||
tracing::error!("Lua code evaluation is disabled");
|
tracing::error!("Lua code evaluation is disabled");
|
||||||
Err(TranspileError::LuaDisabled)
|
Err(TranspileError::LuaDisabled)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,26 @@
|
||||||
//! The transpile module is responsible for transpiling the abstract syntax tree into a data pack.
|
//! The transpile module is responsible for transpiling the abstract syntax tree into a data pack.
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
collections::{BTreeMap, HashMap},
|
||||||
|
fmt::Debug,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
base::source_file::{SourceElement, Span},
|
||||||
|
syntax::syntax_tree::{
|
||||||
|
declaration::FunctionParameter, expression::Expression, statement::Statement,
|
||||||
|
AnnotationValue,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[cfg(feature = "shulkerbox")]
|
#[cfg(feature = "shulkerbox")]
|
||||||
pub mod conversions;
|
pub mod conversions;
|
||||||
mod error;
|
|
||||||
|
pub mod error;
|
||||||
|
|
||||||
|
pub mod expression;
|
||||||
|
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
#[allow(clippy::module_name_repetitions)]
|
#[allow(clippy::module_name_repetitions)]
|
||||||
pub use error::{TranspileError, TranspileResult};
|
pub use error::{TranspileError, TranspileResult};
|
||||||
|
|
@ -11,7 +28,130 @@ pub use error::{TranspileError, TranspileResult};
|
||||||
pub mod lua;
|
pub mod lua;
|
||||||
#[cfg(feature = "shulkerbox")]
|
#[cfg(feature = "shulkerbox")]
|
||||||
mod transpiler;
|
mod transpiler;
|
||||||
#[doc(inline)]
|
use strum::EnumIs;
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
#[cfg_attr(feature = "shulkerbox", doc(inline))]
|
||||||
pub use transpiler::Transpiler;
|
pub use transpiler::Transpiler;
|
||||||
|
|
||||||
mod util;
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
pub mod internal_functions;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
pub mod function;
|
||||||
|
#[doc(inline)]
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
pub use function::TranspiledFunctionArguments;
|
||||||
|
|
||||||
|
mod variables;
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
pub use variables::{Scope, VariableData};
|
||||||
|
|
||||||
|
pub mod util;
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
mod checks;
|
||||||
|
|
||||||
|
/// Data of a function.
|
||||||
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
|
pub struct FunctionData {
|
||||||
|
pub(super) namespace: String,
|
||||||
|
pub(super) identifier_span: Span,
|
||||||
|
pub(super) parameters: Vec<FunctionParameter>,
|
||||||
|
pub(super) statements: Vec<Statement>,
|
||||||
|
pub(super) public: bool,
|
||||||
|
pub(super) annotations: HashMap<String, TranspileAnnotationValue>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Possible values for an annotation.
|
||||||
|
#[expect(clippy::module_name_repetitions)]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, EnumIs)]
|
||||||
|
pub enum TranspileAnnotationValue {
|
||||||
|
/// No value.
|
||||||
|
None(Span),
|
||||||
|
/// A single expression.
|
||||||
|
Expression(Expression, Span),
|
||||||
|
/// A map of key-value pairs.
|
||||||
|
Map(BTreeMap<String, TranspileAnnotationValue>, Span),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TranspileAnnotationValue {
|
||||||
|
/// Creates a new `TranspileAnnotationValue` from an [`AnnotationValue`] and [`Span`] of the key.
|
||||||
|
#[must_use]
|
||||||
|
pub fn from_annotation_value(value: Option<AnnotationValue>, key_span: &Span) -> Self {
|
||||||
|
match value {
|
||||||
|
None => Self::None(key_span.clone()),
|
||||||
|
Some(AnnotationValue::Single { value, .. }) => {
|
||||||
|
let span = value.span();
|
||||||
|
Self::Expression(value, span)
|
||||||
|
}
|
||||||
|
Some(AnnotationValue::Multiple { list, .. }) => {
|
||||||
|
let span = list.span();
|
||||||
|
let map = list
|
||||||
|
.into_elements()
|
||||||
|
.map(|elem| {
|
||||||
|
let key = elem.identifier.span.str().to_string();
|
||||||
|
let value = Self::from_annotation_value(elem.value, &elem.identifier.span);
|
||||||
|
(key, value)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
Self::Map(map, span)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for FunctionData {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
struct HiddenList;
|
||||||
|
impl Debug for HiddenList {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let mut list = f.debug_list();
|
||||||
|
list.entry(&..);
|
||||||
|
list.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AnnotationsWrapper<'a, I>(&'a I);
|
||||||
|
impl<'a, I> Debug for AnnotationsWrapper<'a, I>
|
||||||
|
where
|
||||||
|
&'a I: IntoIterator<Item = (&'a String, &'a TranspileAnnotationValue)>,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
struct AnnotationValueWrapper<'a>(&'a TranspileAnnotationValue);
|
||||||
|
impl Debug for AnnotationValueWrapper<'_> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self.0 {
|
||||||
|
TranspileAnnotationValue::None(_) => None::<u8>.fmt(f),
|
||||||
|
TranspileAnnotationValue::Expression(expr, _) => {
|
||||||
|
expr.span().str().fmt(f)
|
||||||
|
}
|
||||||
|
TranspileAnnotationValue::Map(map, _) => AnnotationsWrapper(map).fmt(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut m = f.debug_map();
|
||||||
|
|
||||||
|
m.entries(
|
||||||
|
self.0
|
||||||
|
.into_iter()
|
||||||
|
.map(|(k, v)| (k, AnnotationValueWrapper(v))),
|
||||||
|
);
|
||||||
|
|
||||||
|
m.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut s = f.debug_struct("FunctionData");
|
||||||
|
|
||||||
|
s.field("namespace", &self.namespace);
|
||||||
|
s.field("identifier", &self.identifier_span.str());
|
||||||
|
s.field("public", &self.public);
|
||||||
|
s.field("parameters", &self.parameters);
|
||||||
|
s.field("statements", &HiddenList);
|
||||||
|
s.field("annotations", &AnnotationsWrapper(&self.annotations));
|
||||||
|
|
||||||
|
s.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,3 +1,124 @@
|
||||||
|
//! Utility methods for transpiling
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
use std::{
|
||||||
|
collections::{BTreeMap, HashMap},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
use shulkerbox::prelude::Command;
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
use crate::{
|
||||||
|
base::{
|
||||||
|
self,
|
||||||
|
source_file::{SourceElement as _, Span},
|
||||||
|
Handler,
|
||||||
|
},
|
||||||
|
syntax::syntax_tree::{
|
||||||
|
expression::{Expression, Primary, TemplateStringLiteral, TemplateStringLiteralPart},
|
||||||
|
AnyStringLiteral,
|
||||||
|
},
|
||||||
|
transpile::{
|
||||||
|
error::{TranspileError, UnknownIdentifier},
|
||||||
|
expression::{ComptimeValue, DataLocation},
|
||||||
|
Scope, TranspileResult, Transpiler, VariableData,
|
||||||
|
},
|
||||||
|
util::identifier_to_macro,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// String that can contain macros
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub enum MacroString {
|
||||||
|
/// A normal string
|
||||||
|
String(String),
|
||||||
|
/// A string containing expressions
|
||||||
|
MacroString {
|
||||||
|
/// Parts that make up the macro string
|
||||||
|
parts: Vec<MacroStringPart>,
|
||||||
|
/// Variables that need special preparation before using the macro string
|
||||||
|
prepare_variables: BTreeMap<String, (DataLocation, Vec<Command>, Span)>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Part of a [`MacroString`]
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub enum MacroStringPart {
|
||||||
|
/// A normal string
|
||||||
|
String(String),
|
||||||
|
/// A macro usage
|
||||||
|
MacroUsage(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
impl std::fmt::Display for MacroString {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::String(s) => s.fmt(f),
|
||||||
|
Self::MacroString { parts, .. } => {
|
||||||
|
for part in parts {
|
||||||
|
match part {
|
||||||
|
MacroStringPart::String(s) => s.fmt(f)?,
|
||||||
|
MacroStringPart::MacroUsage(m) => write!(f, "$({m})")?,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
impl MacroString {
|
||||||
|
/// Check if the macro string contains any macros
|
||||||
|
#[must_use]
|
||||||
|
pub fn contains_macros(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::String(_) => false,
|
||||||
|
Self::MacroString { parts, .. } => parts
|
||||||
|
.iter()
|
||||||
|
.any(|p| matches!(p, MacroStringPart::MacroUsage(_))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the string representation of the macro string or the parts if it contains macros
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// - If the macro string contains macros
|
||||||
|
#[expect(clippy::type_complexity)]
|
||||||
|
pub fn as_str(
|
||||||
|
&self,
|
||||||
|
) -> Result<
|
||||||
|
std::borrow::Cow<'_, str>,
|
||||||
|
(
|
||||||
|
&[MacroStringPart],
|
||||||
|
&BTreeMap<String, (DataLocation, Vec<Command>, Span)>,
|
||||||
|
),
|
||||||
|
> {
|
||||||
|
match self {
|
||||||
|
Self::String(s) => Ok(std::borrow::Cow::Borrowed(s)),
|
||||||
|
Self::MacroString {
|
||||||
|
parts,
|
||||||
|
prepare_variables,
|
||||||
|
} if self.contains_macros() => Err((parts, prepare_variables)),
|
||||||
|
Self::MacroString { parts, .. } => Ok(std::borrow::Cow::Owned(
|
||||||
|
parts
|
||||||
|
.iter()
|
||||||
|
.map(|p| match p {
|
||||||
|
MacroStringPart::String(s) => s.clone(),
|
||||||
|
MacroStringPart::MacroUsage(m) => format!("$({m})"),
|
||||||
|
})
|
||||||
|
.collect::<String>(),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn normalize_program_identifier<S>(identifier: S) -> String
|
fn normalize_program_identifier<S>(identifier: S) -> String
|
||||||
where
|
where
|
||||||
S: AsRef<str>,
|
S: AsRef<str>,
|
||||||
|
|
@ -19,6 +140,8 @@ where
|
||||||
.join("/")
|
.join("/")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Calculate the identifier to import the function based on the current identifier and the import path
|
||||||
|
#[must_use]
|
||||||
pub fn calculate_import_identifier<S, T>(current_identifier: S, import_path: T) -> String
|
pub fn calculate_import_identifier<S, T>(current_identifier: S, import_path: T) -> String
|
||||||
where
|
where
|
||||||
S: AsRef<str>,
|
S: AsRef<str>,
|
||||||
|
|
@ -32,3 +155,495 @@ where
|
||||||
normalize_program_identifier(identifier_elements.join("/") + "/" + import_path.as_ref())
|
normalize_program_identifier(identifier_elements.join("/") + "/" + import_path.as_ref())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Join multiple macro strings into one
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
#[must_use]
|
||||||
|
pub fn join_macro_strings<I>(strings: I) -> MacroString
|
||||||
|
where
|
||||||
|
I: IntoIterator<Item = MacroString>,
|
||||||
|
{
|
||||||
|
strings
|
||||||
|
.into_iter()
|
||||||
|
.fold(MacroString::String(String::new()), |acc, cur| match acc {
|
||||||
|
MacroString::String(mut s) => match cur {
|
||||||
|
MacroString::String(cur) => {
|
||||||
|
s.push_str(&cur);
|
||||||
|
MacroString::String(s)
|
||||||
|
}
|
||||||
|
MacroString::MacroString {
|
||||||
|
parts: cur,
|
||||||
|
prepare_variables: preparation_cmds,
|
||||||
|
} => {
|
||||||
|
let mut parts = vec![MacroStringPart::String(s)];
|
||||||
|
parts.extend(cur);
|
||||||
|
MacroString::MacroString {
|
||||||
|
parts,
|
||||||
|
prepare_variables: preparation_cmds,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
MacroString::MacroString {
|
||||||
|
mut parts,
|
||||||
|
prepare_variables: mut preparation_cmds,
|
||||||
|
} => match cur {
|
||||||
|
MacroString::String(cur) => {
|
||||||
|
parts.push(MacroStringPart::String(cur));
|
||||||
|
MacroString::MacroString {
|
||||||
|
parts,
|
||||||
|
prepare_variables: preparation_cmds,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MacroString::MacroString {
|
||||||
|
parts: cur,
|
||||||
|
prepare_variables: cur_preparation_cmds,
|
||||||
|
} => {
|
||||||
|
parts.extend(cur);
|
||||||
|
preparation_cmds.extend(cur_preparation_cmds);
|
||||||
|
MacroString::MacroString {
|
||||||
|
parts,
|
||||||
|
prepare_variables: preparation_cmds,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add additional information to an entity selector
|
||||||
|
#[must_use]
|
||||||
|
pub fn add_to_entity_selector(selector: impl Into<String>, additional: &str) -> String {
|
||||||
|
let selector: String = selector.into();
|
||||||
|
if selector.starts_with('@') {
|
||||||
|
if selector.ends_with(']') {
|
||||||
|
selector[..selector.len() - 1].to_string() + "," + additional + "]"
|
||||||
|
} else {
|
||||||
|
selector + "[" + additional + "]"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
format!("@a[name={selector},{additional}]")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
impl std::str::FromStr for MacroString {
|
||||||
|
type Err = ();
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
let pos = s.find("$(");
|
||||||
|
if pos.is_some_and(|pos| s[pos..].contains(')')) {
|
||||||
|
let mut parts = Vec::new();
|
||||||
|
let mut rem = s;
|
||||||
|
while let Some(pos) = rem.find("$(") {
|
||||||
|
let (before, after) = rem.split_at(pos);
|
||||||
|
|
||||||
|
let last_macro_index = after
|
||||||
|
.char_indices()
|
||||||
|
.skip(2)
|
||||||
|
.take_while(|&(_, c)| c.is_ascii_alphanumeric() || c == '_')
|
||||||
|
.map(|(i, _)| i)
|
||||||
|
.last();
|
||||||
|
|
||||||
|
match last_macro_index {
|
||||||
|
Some(last_macro_index) if after[last_macro_index + 1..].starts_with(')') => {
|
||||||
|
if !before.is_empty() {
|
||||||
|
match parts.last_mut() {
|
||||||
|
Some(MacroStringPart::String(last)) => {
|
||||||
|
*last += before;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
parts.push(MacroStringPart::String(before.to_string()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parts.push(MacroStringPart::MacroUsage(
|
||||||
|
after[2..=last_macro_index].to_string(),
|
||||||
|
));
|
||||||
|
rem = &after[last_macro_index + 2..];
|
||||||
|
if rem.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let part = &rem[..=pos + 1];
|
||||||
|
match parts.last_mut() {
|
||||||
|
Some(MacroStringPart::String(last)) => {
|
||||||
|
*last += part;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
parts.push(MacroStringPart::String(part.to_string()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rem = &rem[pos + 2..];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !rem.is_empty() {
|
||||||
|
match parts.last_mut() {
|
||||||
|
Some(MacroStringPart::String(last)) => {
|
||||||
|
*last += rem;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
parts.push(MacroStringPart::String(rem.to_string()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if parts
|
||||||
|
.iter()
|
||||||
|
.any(|p| matches!(p, MacroStringPart::MacroUsage(_)))
|
||||||
|
{
|
||||||
|
Ok(Self::MacroString {
|
||||||
|
parts,
|
||||||
|
prepare_variables: BTreeMap::new(),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Ok(Self::String(s.to_string()))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(Self::String(s.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
impl<S> From<S> for MacroString
|
||||||
|
where
|
||||||
|
S: Into<String>,
|
||||||
|
{
|
||||||
|
fn from(value: S) -> Self {
|
||||||
|
Self::String(value.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
impl AnyStringLiteral {
|
||||||
|
/// Convert the any string literal to a macro string, using the provided scope to resolve variables
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// - If an identifier in a template string is not found in the scope
|
||||||
|
pub fn to_macro_string(
|
||||||
|
&self,
|
||||||
|
transpiler: Option<&mut Transpiler>,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<MacroString> {
|
||||||
|
match self {
|
||||||
|
Self::StringLiteral(literal) => Ok(MacroString::from(literal.str_content().as_ref())),
|
||||||
|
Self::TemplateStringLiteral(literal) => {
|
||||||
|
literal.to_macro_string(transpiler, scope, handler)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
impl TemplateStringLiteral {
|
||||||
|
/// Convert the template string literal to a macro string, using the provided scope to resolve variables
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// - If an identifier in a template string is not found in the scope
|
||||||
|
#[expect(clippy::too_many_lines)]
|
||||||
|
pub fn to_macro_string(
|
||||||
|
&self,
|
||||||
|
mut transpiler: Option<&mut Transpiler>,
|
||||||
|
scope: &Arc<Scope>,
|
||||||
|
handler: &impl Handler<base::Error>,
|
||||||
|
) -> TranspileResult<MacroString> {
|
||||||
|
if self.contains_expression() {
|
||||||
|
let mut prepare_variables = BTreeMap::new();
|
||||||
|
let mut prepare_variables_reverse = HashMap::<DataLocation, String>::new();
|
||||||
|
|
||||||
|
let parts = self
|
||||||
|
.parts()
|
||||||
|
.iter()
|
||||||
|
.map(|part| match part {
|
||||||
|
TemplateStringLiteralPart::Text(text) => Ok(vec![MacroStringPart::String(
|
||||||
|
crate::util::unescape_template_string(text.span.str()).into_owned(),
|
||||||
|
)]),
|
||||||
|
TemplateStringLiteralPart::Expression { expression, .. } => match expression
|
||||||
|
.as_ref()
|
||||||
|
{
|
||||||
|
Expression::Primary(Primary::Identifier(identifier)) => {
|
||||||
|
#[expect(clippy::option_if_let_else)]
|
||||||
|
if let Some(var_data) = scope.get_variable(identifier.span.str()) {
|
||||||
|
match var_data.as_ref() {
|
||||||
|
VariableData::MacroParameter { macro_name, .. } => {
|
||||||
|
Ok(vec![MacroStringPart::MacroUsage(macro_name.to_owned())])
|
||||||
|
}
|
||||||
|
VariableData::ComptimeValue { value, .. } => {
|
||||||
|
let value = value.read().unwrap().as_ref().map_or_else(
|
||||||
|
|| "null".into(),
|
||||||
|
ComptimeValue::to_macro_string,
|
||||||
|
);
|
||||||
|
|
||||||
|
match value.as_str() {
|
||||||
|
Ok(s) => {
|
||||||
|
Ok(vec![MacroStringPart::String(s.into_owned())])
|
||||||
|
}
|
||||||
|
Err((inner_parts, inner_prepare_variables)) => {
|
||||||
|
prepare_variables
|
||||||
|
.extend(inner_prepare_variables.to_owned());
|
||||||
|
Ok(inner_parts.to_vec())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VariableData::BooleanStorage { storage_name, path } => {
|
||||||
|
use crate::transpile::expression::StorageType;
|
||||||
|
|
||||||
|
let macro_name = if let Some(transpiler) = &mut transpiler {
|
||||||
|
let temp_count = transpiler.get_temp_count(1);
|
||||||
|
format!(
|
||||||
|
"shu_temp_{hash}",
|
||||||
|
hash = chksum_md5::hash(temp_count.to_le_bytes())
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
identifier_to_macro(identifier.span.str()).into_owned()
|
||||||
|
};
|
||||||
|
|
||||||
|
let data_location = DataLocation::Storage {
|
||||||
|
storage_name: storage_name.to_owned(),
|
||||||
|
path: path.to_owned(),
|
||||||
|
r#type: StorageType::Boolean,
|
||||||
|
};
|
||||||
|
|
||||||
|
let macro_name = if let Some(prev_macro_name) =
|
||||||
|
prepare_variables_reverse.get(&data_location)
|
||||||
|
{
|
||||||
|
prev_macro_name.to_string()
|
||||||
|
} else {
|
||||||
|
prepare_variables.insert(
|
||||||
|
macro_name.clone(),
|
||||||
|
(
|
||||||
|
data_location.clone(),
|
||||||
|
Vec::new(),
|
||||||
|
expression.span(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
prepare_variables_reverse
|
||||||
|
.insert(data_location, macro_name.clone());
|
||||||
|
|
||||||
|
macro_name
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(vec![MacroStringPart::MacroUsage(macro_name)])
|
||||||
|
}
|
||||||
|
VariableData::ScoreboardValue { objective, target } => {
|
||||||
|
let macro_name = if let Some(transpiler) = &mut transpiler {
|
||||||
|
let temp_count = transpiler.get_temp_count(1);
|
||||||
|
format!(
|
||||||
|
"shu_temp_{hash}",
|
||||||
|
hash = chksum_md5::hash(temp_count.to_le_bytes())
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
identifier_to_macro(identifier.span.str()).into_owned()
|
||||||
|
};
|
||||||
|
let data_location = DataLocation::ScoreboardValue {
|
||||||
|
objective: objective.to_owned(),
|
||||||
|
target: target.to_owned(),
|
||||||
|
};
|
||||||
|
let macro_name = if let Some(prev_macro_name) =
|
||||||
|
prepare_variables_reverse.get(&data_location)
|
||||||
|
{
|
||||||
|
prev_macro_name.to_string()
|
||||||
|
} else {
|
||||||
|
prepare_variables.insert(
|
||||||
|
macro_name.clone(),
|
||||||
|
(
|
||||||
|
data_location.clone(),
|
||||||
|
Vec::new(),
|
||||||
|
expression.span(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
prepare_variables_reverse
|
||||||
|
.insert(data_location, macro_name.clone());
|
||||||
|
|
||||||
|
macro_name
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(vec![MacroStringPart::MacroUsage(macro_name)])
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
use crate::semantic::error::UnexpectedExpression;
|
||||||
|
|
||||||
|
let err = TranspileError::UnexpectedExpression(
|
||||||
|
UnexpectedExpression(expression.to_owned()),
|
||||||
|
);
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let err = TranspileError::UnknownIdentifier(
|
||||||
|
UnknownIdentifier::from_scope(identifier.span(), scope),
|
||||||
|
);
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Expression::Primary(Primary::MemberAccess(member_access)) => {
|
||||||
|
let value = member_access
|
||||||
|
.parent()
|
||||||
|
.comptime_member_access(member_access, scope, handler)
|
||||||
|
.inspect_err(|err| {
|
||||||
|
handler.receive(Box::new(TranspileError::NotComptime(
|
||||||
|
err.clone(),
|
||||||
|
)));
|
||||||
|
})?
|
||||||
|
.to_macro_string();
|
||||||
|
|
||||||
|
match value.as_str() {
|
||||||
|
Ok(s) => Ok(vec![MacroStringPart::String(s.into_owned())]),
|
||||||
|
Err((inner_parts, inner_prepare_variables)) => {
|
||||||
|
prepare_variables.extend(inner_prepare_variables.to_owned());
|
||||||
|
Ok(inner_parts.to_vec())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
if let Some(transpiler) = &mut transpiler {
|
||||||
|
use crate::transpile::expression::{StorageType, ValueType};
|
||||||
|
|
||||||
|
let temp_count = transpiler.get_temp_count(1);
|
||||||
|
let macro_name = format!(
|
||||||
|
"shu_temp_{hash}",
|
||||||
|
hash = chksum_md5::hash(temp_count.to_le_bytes())
|
||||||
|
);
|
||||||
|
|
||||||
|
let data_location =
|
||||||
|
if expression.can_yield_type(ValueType::Integer, scope) {
|
||||||
|
let (scoreboard_name, [scoreboard_target]) =
|
||||||
|
transpiler.get_temp_scoreboard_locations_array();
|
||||||
|
|
||||||
|
DataLocation::ScoreboardValue {
|
||||||
|
objective: scoreboard_name,
|
||||||
|
target: scoreboard_target,
|
||||||
|
}
|
||||||
|
} else if expression.can_yield_type(ValueType::Boolean, scope) {
|
||||||
|
let (storage_name, [storage_path]) =
|
||||||
|
transpiler.get_temp_storage_locations_array();
|
||||||
|
DataLocation::Storage {
|
||||||
|
storage_name,
|
||||||
|
path: storage_path,
|
||||||
|
r#type: StorageType::Boolean,
|
||||||
|
}
|
||||||
|
} else if expression.can_yield_type(ValueType::String, scope) {
|
||||||
|
let (storage_name, [storage_path]) =
|
||||||
|
transpiler.get_temp_storage_locations_array();
|
||||||
|
DataLocation::Storage {
|
||||||
|
storage_name,
|
||||||
|
path: storage_path,
|
||||||
|
r#type: StorageType::String,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
use crate::semantic::error::UnexpectedExpression;
|
||||||
|
|
||||||
|
let err = TranspileError::UnexpectedExpression(
|
||||||
|
UnexpectedExpression(expression.to_owned()),
|
||||||
|
);
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
return Err(err);
|
||||||
|
};
|
||||||
|
|
||||||
|
let commands = transpiler.transpile_expression(
|
||||||
|
expression,
|
||||||
|
&data_location,
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
prepare_variables.insert(
|
||||||
|
macro_name.clone(),
|
||||||
|
(data_location, commands, expression.span()),
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(vec![MacroStringPart::MacroUsage(macro_name)])
|
||||||
|
} else {
|
||||||
|
use crate::semantic::error::UnexpectedExpression;
|
||||||
|
|
||||||
|
let err = TranspileError::UnexpectedExpression(
|
||||||
|
UnexpectedExpression(expression.to_owned()),
|
||||||
|
);
|
||||||
|
handler.receive(Box::new(err.clone()));
|
||||||
|
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.flat_map(|res| match res {
|
||||||
|
Ok(parts) => parts.into_iter().map(Ok).collect(),
|
||||||
|
Err(err) => vec![Err(err)],
|
||||||
|
})
|
||||||
|
.collect::<TranspileResult<Vec<MacroStringPart>>>()?;
|
||||||
|
|
||||||
|
Ok(MacroString::MacroString {
|
||||||
|
parts,
|
||||||
|
prepare_variables,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Ok(MacroString::String(
|
||||||
|
self.as_str(scope, handler)?.into_owned(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(all(test, feature = "shulkerbox"))]
|
||||||
|
mod tests {
|
||||||
|
use std::str::FromStr as _;
|
||||||
|
|
||||||
|
use assert_struct::assert_struct;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_macro_string() {
|
||||||
|
assert_struct!(
|
||||||
|
MacroString::from_str("Hello, $(world)!").unwrap(),
|
||||||
|
MacroString::MacroString {
|
||||||
|
parts: vec![
|
||||||
|
MacroStringPart::String("Hello, ".to_string()),
|
||||||
|
MacroStringPart::MacroUsage("world".to_string()),
|
||||||
|
MacroStringPart::String("!".to_string())
|
||||||
|
],
|
||||||
|
prepare_variables.is_empty(): true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_struct!(
|
||||||
|
MacroString::from_str("Hello, $(world)! $(world").unwrap(),
|
||||||
|
MacroString::MacroString {
|
||||||
|
parts: vec![
|
||||||
|
MacroStringPart::String("Hello, ".to_string()),
|
||||||
|
MacroStringPart::MacroUsage("world".to_string()),
|
||||||
|
MacroStringPart::String("! $(world".to_string()),
|
||||||
|
],
|
||||||
|
prepare_variables.is_empty(): true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_struct!(
|
||||||
|
MacroString::from_str("Hello $(a) from $(b) and $(c)").unwrap(),
|
||||||
|
MacroString::MacroString {
|
||||||
|
parts: vec![
|
||||||
|
MacroStringPart::String("Hello ".to_string()),
|
||||||
|
MacroStringPart::MacroUsage("a".to_string()),
|
||||||
|
MacroStringPart::String(" from ".to_string()),
|
||||||
|
MacroStringPart::MacroUsage("b".to_string()),
|
||||||
|
MacroStringPart::String(" and ".to_string()),
|
||||||
|
MacroStringPart::MacroUsage("c".to_string()),
|
||||||
|
],
|
||||||
|
prepare_variables.is_empty(): true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_struct!(
|
||||||
|
MacroString::from_str("Hello, $(world! $(world)!").unwrap(),
|
||||||
|
MacroString::MacroString {
|
||||||
|
parts: vec![
|
||||||
|
MacroStringPart::String("Hello, $(world! ".to_string()),
|
||||||
|
MacroStringPart::MacroUsage("world".to_string()),
|
||||||
|
MacroStringPart::String("!".to_string()),
|
||||||
|
],
|
||||||
|
prepare_variables.is_empty(): true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,181 @@
|
||||||
|
//! Utility functions for the `Shulkerscript` language.
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
/// Escapes `"` and `\` in a string.
|
||||||
|
#[must_use]
|
||||||
|
pub fn escape_str(s: &str) -> Cow<'_, str> {
|
||||||
|
if s.contains('"') || s.contains('\\') {
|
||||||
|
let mut escaped = String::with_capacity(s.len());
|
||||||
|
for c in s.chars() {
|
||||||
|
match c {
|
||||||
|
'"' => escaped.push_str("\\\""),
|
||||||
|
'\\' => escaped.push_str("\\\\"),
|
||||||
|
_ => escaped.push(c),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Cow::Owned(escaped)
|
||||||
|
} else {
|
||||||
|
Cow::Borrowed(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unescapes '\`', `\`, `\n`, `\r` and `\t`, `\$` in a string.
|
||||||
|
#[must_use]
|
||||||
|
pub fn unescape_template_string(s: &str) -> Cow<'_, str> {
|
||||||
|
if s.contains('\\') || s.contains('`') {
|
||||||
|
Cow::Owned(
|
||||||
|
s.replace("\\n", "\n")
|
||||||
|
.replace("\\r", "\r")
|
||||||
|
.replace("\\t", "\t")
|
||||||
|
.replace("\\`", "`")
|
||||||
|
.replace("\\$", "$")
|
||||||
|
.replace("\\\\", "\\"),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Cow::Borrowed(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transforms an identifier to a macro name that only contains `a-zA-Z0-9_`.
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
#[must_use]
|
||||||
|
pub fn identifier_to_macro(ident: &str) -> std::borrow::Cow<'_, str> {
|
||||||
|
if ident.contains("__")
|
||||||
|
|| ident
|
||||||
|
.chars()
|
||||||
|
.any(|c| c == '_' || !c.is_ascii_alphanumeric())
|
||||||
|
{
|
||||||
|
let new_ident = ident
|
||||||
|
.chars()
|
||||||
|
.filter(|c| *c != '_' && c.is_ascii_alphanumeric())
|
||||||
|
.collect::<String>();
|
||||||
|
|
||||||
|
let chksum = chksum_md5::hash(ident).to_hex_lowercase();
|
||||||
|
|
||||||
|
std::borrow::Cow::Owned(new_ident + "__" + &chksum[..8])
|
||||||
|
} else {
|
||||||
|
std::borrow::Cow::Borrowed(ident)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transforms an identifier to a macro name that only contains `a-zA-Z0-9_`.
|
||||||
|
/// Does only strip invalid characters if the `shulkerbox` feature is not enabled.
|
||||||
|
#[cfg(not(feature = "shulkerbox"))]
|
||||||
|
#[must_use]
|
||||||
|
pub fn identifier_to_macro(ident: &str) -> std::borrow::Cow<'_, str> {
|
||||||
|
if ident.contains("__")
|
||||||
|
|| ident
|
||||||
|
.chars()
|
||||||
|
.any(|c| c == '_' || !c.is_ascii_alphanumeric())
|
||||||
|
{
|
||||||
|
let new_ident = ident
|
||||||
|
.chars()
|
||||||
|
.filter(|c| *c != '_' && c.is_ascii_alphanumeric())
|
||||||
|
.collect::<String>();
|
||||||
|
|
||||||
|
std::borrow::Cow::Owned(new_ident)
|
||||||
|
} else {
|
||||||
|
std::borrow::Cow::Borrowed(ident)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transforms an identifier to a macro name that only contains `a-zA-Z0-9_`.
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
#[must_use]
|
||||||
|
pub fn identifier_to_scoreboard_target(ident: &str) -> std::borrow::Cow<'_, str> {
|
||||||
|
if !(..=16).contains(&ident.len())
|
||||||
|
|| ident
|
||||||
|
.chars()
|
||||||
|
.any(|c| c != '_' || !c.is_ascii_alphanumeric())
|
||||||
|
{
|
||||||
|
std::borrow::Cow::Owned(chksum_md5::hash(ident).to_hex_lowercase().split_off(16))
|
||||||
|
} else {
|
||||||
|
std::borrow::Cow::Borrowed(ident)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transforms an identifier to a name that only contains `a-zA-Z0-9_`.
|
||||||
|
/// Does only strip invalid characters if the `shulkerbox` feature is not enabled.
|
||||||
|
#[cfg(not(feature = "shulkerbox"))]
|
||||||
|
#[must_use]
|
||||||
|
pub fn identifier_to_scoreboard_target(ident: &str) -> std::borrow::Cow<'_, str> {
|
||||||
|
if !(..=16).contains(&ident.len())
|
||||||
|
|| ident
|
||||||
|
.chars()
|
||||||
|
.any(|c| c != '_' || !c.is_ascii_alphanumeric())
|
||||||
|
{
|
||||||
|
let new_ident = ident
|
||||||
|
.chars()
|
||||||
|
.map(|c| {
|
||||||
|
if c != '_' && !c.is_ascii_alphanumeric() {
|
||||||
|
'_'
|
||||||
|
} else {
|
||||||
|
c
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect::<String>();
|
||||||
|
|
||||||
|
std::borrow::Cow::Owned(new_ident)
|
||||||
|
} else {
|
||||||
|
std::borrow::Cow::Borrowed(ident)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns whether a string is a valid scoreboard name.
|
||||||
|
#[must_use]
|
||||||
|
pub fn is_valid_scoreboard_objective_name(name: &str) -> bool {
|
||||||
|
name.chars()
|
||||||
|
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '+' | '.'))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns whether a string is a valid scoreboard target.
|
||||||
|
#[must_use]
|
||||||
|
pub fn is_valid_scoreboard_target(name: &str) -> bool {
|
||||||
|
(..=16).contains(&name.len()) && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_escape_str() {
|
||||||
|
assert_eq!(escape_str("Hello, world!"), "Hello, world!");
|
||||||
|
assert_eq!(escape_str(r#"Hello, "world"!"#), r#"Hello, \"world\"!"#);
|
||||||
|
assert_eq!(escape_str(r"Hello, \world\!"), r"Hello, \\world\\!");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_unescape_macro_string() {
|
||||||
|
assert_eq!(unescape_template_string("Hello, world!"), "Hello, world!");
|
||||||
|
assert_eq!(
|
||||||
|
unescape_template_string(r#"Hello, "world"!"#),
|
||||||
|
r#"Hello, "world"!"#
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
unescape_template_string(r"Hello, \world\!"),
|
||||||
|
r"Hello, \world\!"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
unescape_template_string(r"Hello, \nworld\!"),
|
||||||
|
"Hello, \nworld\\!"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
unescape_template_string(r"Hello, \rworld\!"),
|
||||||
|
"Hello, \rworld\\!"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
unescape_template_string(r"Hello, \tworld\!"),
|
||||||
|
"Hello, \tworld\\!"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
unescape_template_string(r"Hello, \`world\!"),
|
||||||
|
r"Hello, `world\!"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
unescape_template_string(r"Hello, \\world\!"),
|
||||||
|
r"Hello, \world\!"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -20,10 +20,7 @@ fn parsing_test1() {
|
||||||
)
|
)
|
||||||
.expect("Failed to parse");
|
.expect("Failed to parse");
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(parsed.namespace().name().str_content(), "parsing-test");
|
||||||
parsed.namespace().namespace_name().str_content(),
|
|
||||||
"parsing-test"
|
|
||||||
);
|
|
||||||
|
|
||||||
let declarations = parsed.declarations();
|
let declarations = parsed.declarations();
|
||||||
assert_eq!(declarations.len(), 1);
|
assert_eq!(declarations.len(), 1);
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,14 @@ fn transpile_test1() {
|
||||||
let transpiled = shulkerscript::transpile(
|
let transpiled = shulkerscript::transpile(
|
||||||
&PrintHandler::default(),
|
&PrintHandler::default(),
|
||||||
&dir,
|
&dir,
|
||||||
|
"main",
|
||||||
48,
|
48,
|
||||||
&[("test1".to_string(), "./test1.shu")],
|
&[("test1".to_string(), "./test1.shu")],
|
||||||
)
|
)
|
||||||
.expect("Failed to transpile");
|
.expect("Failed to transpile");
|
||||||
|
|
||||||
let expected = {
|
let expected = {
|
||||||
let mut dp = Datapack::new(48);
|
let mut dp = Datapack::new("main", 48);
|
||||||
|
|
||||||
let namespace = dp.namespace_mut("transpiling-test");
|
let namespace = dp.namespace_mut("transpiling-test");
|
||||||
|
|
||||||
|
|
@ -28,9 +29,9 @@ fn transpile_test1() {
|
||||||
main_fn.add_command(Command::Raw("say Hello, World!".to_string()));
|
main_fn.add_command(Command::Raw("say Hello, World!".to_string()));
|
||||||
|
|
||||||
let exec_cmd = Command::Execute(Execute::As(
|
let exec_cmd = Command::Execute(Execute::As(
|
||||||
"@a".to_string(),
|
"@a".to_string().into(),
|
||||||
Box::new(Execute::If(
|
Box::new(Execute::If(
|
||||||
Condition::Atom("entity @p[distance=..5]".to_string()),
|
Condition::Atom("entity @p[distance=..5]".to_string().into()),
|
||||||
Box::new(Execute::Run(Box::new(Command::Raw(
|
Box::new(Execute::Run(Box::new(Command::Raw(
|
||||||
"say You are close to me!".to_string(),
|
"say You are close to me!".to_string(),
|
||||||
)))),
|
)))),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue