Compare commits

...

5 Commits

Author SHA1 Message Date
Moritz Hölting bd8270bd5d fix semantic analysis to allow assignment to indexed identifier 2025-08-13 18:55:47 +02:00
Moritz Hölting e0c3253ab6 fix comptime variable errors 2025-08-13 18:24:48 +02:00
Moritz Hölting 15dde037b7 only use function arg storage if required 2025-08-13 18:05:15 +02:00
Moritz Hölting 0ee456756a apply clippy lints from version 0.1.89 2025-08-13 15:26:36 +02:00
Moritz Hölting d9ae985282 change syntax of array declaration: move brackets from after name to after type 2025-08-13 12:58:35 +02:00
26 changed files with 495 additions and 424 deletions

View File

@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Boolean map (tag)
- Member access (e.g. `.objective` to get objective name where int is stored)
- Return statement
- internal `print` function
- Example: barebones compiler
### Changed

View File

@ -99,7 +99,7 @@ FunctionParameterList:
```ebnf
ArrayVariableDeclaration:
('int' | 'bool') identifier '[' integer ']' VariableDeclarationAssignment?
('int' | 'bool') '[' integer ']' identifier VariableDeclarationAssignment?
```
## ComptimeValueDeclaration

View File

@ -7,11 +7,11 @@ pub enum Error {
#[error(transparent)]
LexicalError(#[from] crate::lexical::Error),
#[error(transparent)]
ParseError(#[from] crate::syntax::error::Error),
ParseError(#[from] Box<crate::syntax::error::Error>),
#[error(transparent)]
SemanticError(#[from] crate::semantic::error::Error),
#[error(transparent)]
TranspileError(#[from] crate::transpile::TranspileError),
TranspileError(#[from] Box<crate::transpile::TranspileError>),
#[error("An error occurred: {0}")]
Other(String),
}

View File

@ -12,7 +12,7 @@ pub trait FileProvider {
/// # 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>;
fn read_bytes<P: AsRef<Path>>(&self, path: P) -> Result<Cow<'_, [u8]>, Error>;
/// Reads the contents of the file at the given path.
///
@ -20,7 +20,7 @@ pub trait FileProvider {
/// - If an error occurs while reading the file.
/// - If the file does not exist.
/// - If the file is not valid UTF-8.
fn read_str<P: AsRef<Path>>(&self, path: P) -> Result<Cow<str>, Error> {
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| {
@ -57,14 +57,14 @@ where
}
impl FileProvider for FsProvider {
fn read_bytes<P: AsRef<Path>>(&self, path: P) -> Result<Cow<[u8]>, Error> {
fn read_bytes<P: AsRef<Path>>(&self, path: P) -> Result<Cow<'_, [u8]>, Error> {
let full_path = self.root.join(path);
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> {
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)
@ -178,7 +178,7 @@ mod vfs {
use shulkerbox::virtual_fs::{VFile, VFolder};
impl FileProvider for VFolder {
fn read_bytes<P: AsRef<Path>>(&self, path: P) -> Result<Cow<[u8]>, Error> {
fn read_bytes<P: AsRef<Path>>(&self, path: P) -> Result<Cow<'_, [u8]>, Error> {
normalize_path_str(path).map_or_else(
|| Err(Error::from(std::io::ErrorKind::InvalidData)),
|path| {
@ -189,7 +189,7 @@ mod vfs {
)
}
fn read_str<P: AsRef<Path>>(&self, path: P) -> Result<Cow<str>, Error> {
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| {

View File

@ -69,7 +69,7 @@ impl SourceFile {
/// Get the [`SourceIterator`] for the source file.
#[must_use]
pub fn iter<'a>(self: &'a Arc<Self>) -> SourceIterator<'a> {
pub fn iter(self: &Arc<Self>) -> SourceIterator<'_> {
SourceIterator {
source_file: self,
iterator: self.content().char_indices().peekable(),

View File

@ -161,7 +161,7 @@ pub enum Token {
DocComment(DocComment),
CommandLiteral(CommandLiteral),
StringLiteral(StringLiteral),
MacroStringLiteral(MacroStringLiteral),
MacroStringLiteral(Box<MacroStringLiteral>),
}
impl SourceElement for Token {
@ -325,7 +325,7 @@ pub struct StringLiteral {
impl StringLiteral {
/// Returns the string content without escapement characters, leading and trailing double quotes.
#[must_use]
pub fn str_content(&self) -> Cow<str> {
pub fn str_content(&self) -> Cow<'_, str> {
let string = self.span.str();
let string = &string[1..string.len() - 1];
if string.contains('\\') {
@ -740,7 +740,7 @@ impl Token {
handler.receive(error::Error::from(UnterminatedDelimitedComment {
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
@ -883,14 +883,14 @@ impl Token {
}
.into())
} else {
Ok(MacroStringLiteral {
Ok(Box::new(MacroStringLiteral {
starting_backtick,
parts,
ending_backtick: Punctuation {
span: Self::create_span(start, iter),
punctuation: '`',
},
}
})
.into())
}
}

View File

@ -100,7 +100,7 @@ pub fn parse(
tracing::info!("Parsing the source code at path: {}", path.display());
let mut parser = Parser::new(&tokens);
let program = parser.parse_program(handler)?;
let program = parser.parse_program(handler).map_err(Box::new)?;
if handler.has_received() {
return Err(Error::other(
@ -165,8 +165,9 @@ where
tracing::info!("Transpiling the source code.");
let datapack =
Transpiler::new(main_namespace_name, pack_format).transpile(&programs, handler)?;
let datapack = Transpiler::new(main_namespace_name, pack_format)
.transpile(&programs, handler)
.map_err(Box::new)?;
if handler.has_received() {
return Err(Error::other(

View File

@ -48,7 +48,7 @@ pub enum Error {
/// An error that occurs when a function declaration is missing.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UnexpectedExpression(pub Expression);
pub struct UnexpectedExpression(pub Box<Expression>);
impl Display for UnexpectedExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

View File

@ -70,7 +70,7 @@ fn get_global_scope(declarations: &[Declaration]) -> SemanticScope<'static> {
impl Namespace {
/// Analyzes the semantics of the namespace.
fn analyze_semantics(&self, handler: &impl Handler<base::Error>) -> Result<(), error::Error> {
let name = self.namespace_name();
let name = self.name();
Self::validate_str(name.str_content().as_ref()).map_err(|invalid_chars| {
let err = error::Error::from(InvalidNamespaceName {
name: name.clone(),
@ -393,12 +393,22 @@ impl Assignment {
if let Some(variable_type) = variable_type {
let expected = match variable_type {
VariableType::BooleanStorage | VariableType::Tag => ValueType::Boolean,
VariableType::BooleanStorage => ValueType::Boolean,
VariableType::ScoreboardValue => ValueType::Integer,
VariableType::ComptimeValue => {
// TODO: check if the expression is a constant expression
return Ok(());
}
VariableType::BooleanStorageArray | VariableType::Tag
if matches!(self.destination(), AssignmentDestination::Indexed(..)) =>
{
ValueType::Boolean
}
VariableType::Scoreboard | VariableType::ScoreboardArray
if matches!(self.destination(), AssignmentDestination::Indexed(..)) =>
{
ValueType::Integer
}
_ => {
let err = error::Error::AssignmentError(AssignmentError {
identifier: self.destination().span(),
@ -561,7 +571,7 @@ impl Primary {
VariableType::Function | VariableType::InternalFunction => Ok(()),
_ => {
let err = error::Error::UnexpectedExpression(UnexpectedExpression(
Expression::Primary(self.clone()),
Box::new(Expression::Primary(self.clone())),
));
handler.receive(err.clone());
Err(err)
@ -706,13 +716,14 @@ impl Primary {
_ => false,
},
Self::MemberAccess(_) => {
// TODO:
// TODO: correct return value after check
true
}
Self::Identifier(ident) => match scope.get_variable(ident.span.str()) {
Some(VariableType::BooleanStorage) => expected == ValueType::Boolean,
Some(VariableType::ScoreboardValue) => expected == ValueType::Integer,
Some(VariableType::Tag) => expected == ValueType::String,
Some(VariableType::ComptimeValue) => true,
_ => false,
},
Self::Prefix(prefixed) => match prefixed.operator() {
@ -930,9 +941,10 @@ impl MacroStringLiteral {
MacroStringLiteralPart::MacroUsage { identifier, .. } => {
if let Some(variable_type) = scope.get_variable(identifier.span.str()) {
if variable_type != VariableType::MacroParameter {
let err = error::Error::UnexpectedExpression(UnexpectedExpression(
Expression::Primary(Primary::Identifier(identifier.clone())),
));
let err =
error::Error::UnexpectedExpression(UnexpectedExpression(Box::new(
Expression::Primary(Primary::Identifier(identifier.clone())),
)));
handler.receive(err.clone());
errs.push(err);
}

View File

@ -80,7 +80,7 @@ impl<'a> Parser<'a> {
}
}),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
@ -90,7 +90,7 @@ impl<'a> Parser<'a> {
expected: SyntaxKind::Punctuation(expected),
found: self.get_reading(None).into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
};
@ -129,7 +129,7 @@ impl<'a> Parser<'a> {
expected: SyntaxKind::Punctuation(expected),
found: self.peek().into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
@ -393,7 +393,7 @@ impl Frame<'_> {
expected: SyntaxKind::Identifier,
found: found.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -432,7 +432,7 @@ impl Frame<'_> {
expected: SyntaxKind::StringLiteral,
found: found.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -447,13 +447,13 @@ impl Frame<'_> {
handler: &impl Handler<base::Error>,
) -> ParseResult<MacroStringLiteral> {
match self.next_significant_token() {
Reading::Atomic(Token::MacroStringLiteral(literal)) => Ok(literal),
Reading::Atomic(Token::MacroStringLiteral(literal)) => Ok(*literal),
found => {
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
expected: SyntaxKind::MacroStringLiteral,
found: found.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -469,13 +469,13 @@ impl Frame<'_> {
) -> ParseResult<AnyStringLiteral> {
match self.next_significant_token() {
Reading::Atomic(Token::StringLiteral(literal)) => Ok(literal.into()),
Reading::Atomic(Token::MacroStringLiteral(literal)) => Ok(literal.into()),
Reading::Atomic(Token::MacroStringLiteral(literal)) => Ok((*literal).into()),
found => {
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
expected: SyntaxKind::AnyStringLiteral,
found: found.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -499,7 +499,7 @@ impl Frame<'_> {
expected: SyntaxKind::Keyword(expected),
found: found.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -530,7 +530,7 @@ impl Frame<'_> {
expected: SyntaxKind::Punctuation(expected),
found: found.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}

View File

@ -1,6 +1,7 @@
//! Syntax tree nodes for declarations.
#![allow(missing_docs)]
#![expect(missing_docs)]
#![expect(clippy::struct_field_names)]
use std::collections::VecDeque;
@ -391,7 +392,7 @@ impl Parser<'_> {
declaration
.with_annotation(annotation)
.inspect_err(|err| handler.receive(err.clone()))
.inspect_err(|err| handler.receive(Box::new(err.clone())))
}
Reading::Atomic(Token::Keyword(from_keyword))
@ -441,7 +442,7 @@ impl Parser<'_> {
expected: SyntaxKind::Punctuation('*'),
found: self.stop_at_significant().into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
@ -511,7 +512,7 @@ impl Parser<'_> {
expected: SyntaxKind::Declaration,
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
@ -562,7 +563,7 @@ impl Parser<'_> {
expected: SyntaxKind::Keyword(KeywordKind::Function),
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -615,7 +616,7 @@ impl Parser<'_> {
]),
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}

View File

@ -666,7 +666,7 @@ impl Parser<'_> {
// eat the macro string literal
self.forward();
Ok(Primary::MacroStringLiteral(macro_string_literal))
Ok(Primary::MacroStringLiteral(*macro_string_literal))
}
// lua code expression
@ -687,7 +687,7 @@ impl Parser<'_> {
expected: syntax::error::SyntaxKind::Identifier,
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
},
@ -741,7 +741,7 @@ impl Parser<'_> {
expected: syntax::error::SyntaxKind::Expression,
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
@ -829,7 +829,7 @@ impl Parser<'_> {
expected: syntax::error::SyntaxKind::Operator,
found: Some(Token::Punctuation(punc)),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
},
@ -838,7 +838,7 @@ impl Parser<'_> {
expected: syntax::error::SyntaxKind::Operator,
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}

View File

@ -388,7 +388,7 @@ impl Parser<'_> {
expected: SyntaxKind::Punctuation('#'),
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}

View File

@ -52,10 +52,10 @@ pub struct ProgramFile {
pub struct Namespace {
/// The `namespace` keyword.
#[get = "pub"]
namespace_keyword: Keyword,
keyword: Keyword,
/// The name of the namespace.
#[get = "pub"]
namespace_name: StringLiteral,
name: StringLiteral,
/// The semicolon.
#[get = "pub"]
semicolon: Punctuation,
@ -63,7 +63,7 @@ pub struct Namespace {
impl SourceElement for Namespace {
fn span(&self) -> Span {
self.namespace_keyword
self.keyword
.span()
.join(&self.semicolon.span())
.expect("Invalid span")
@ -74,7 +74,7 @@ impl Namespace {
/// Dissolves the namespace into its components.
#[must_use]
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.
@ -112,19 +112,19 @@ impl Parser<'_> {
tracing::debug!("Parsing program");
let namespace = match self.stop_at_significant() {
Reading::Atomic(Token::Keyword(namespace_keyword))
if namespace_keyword.keyword == KeywordKind::Namespace =>
Reading::Atomic(Token::Keyword(keyword))
if keyword.keyword == KeywordKind::Namespace =>
{
// eat the keyword
self.forward();
let namespace_name = self.parse_string_literal(handler)?;
let name = self.parse_string_literal(handler)?;
let semicolon = self.parse_punctuation(';', true, handler)?;
Ok(Namespace {
namespace_keyword,
namespace_name,
keyword,
name,
semicolon,
})
}
@ -133,14 +133,14 @@ impl Parser<'_> {
expected: SyntaxKind::Keyword(KeywordKind::Namespace),
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}?;
tracing::debug!(
"Found namespace '{}', parsing declarations",
namespace.namespace_name.str_content()
namespace.name.str_content()
);
let mut declarations = Vec::new();

View File

@ -493,7 +493,7 @@ impl SingleVariableDeclaration {
///
/// ```ebnf
/// ArrayVariableDeclaration:
/// ('int' | 'bool') identifier '[' integer ']' VariableDeclarationAssignment?
/// ('int' | 'bool') '[' integer ']' identifier VariableDeclarationAssignment?
/// ```
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
@ -501,9 +501,6 @@ pub struct ArrayVariableDeclaration {
/// The type of the variable.
#[get = "pub"]
variable_type: Keyword,
/// The identifier of the variable.
#[get = "pub"]
identifier: Identifier,
/// The opening bracket of the array.
#[get = "pub"]
open_bracket: Punctuation,
@ -513,6 +510,9 @@ pub struct ArrayVariableDeclaration {
/// The closing bracket of the array.
#[get = "pub"]
close_bracket: Punctuation,
/// The identifier of the variable.
#[get = "pub"]
identifier: Identifier,
/// The optional assignment of the variable.
#[get = "pub"]
assignment: Option<VariableDeclarationAssignment>,
@ -798,7 +798,7 @@ pub enum AssignmentDestination {
/// Assignment to an identifier.
Identifier(Identifier),
/// Assignment to an indexed identifier.
Indexed(Identifier, Punctuation, Expression, Punctuation),
Indexed(Identifier, Punctuation, Box<Expression>, Punctuation),
}
impl SourceElement for AssignmentDestination {
@ -872,7 +872,7 @@ impl Parser<'_> {
statement
.with_annotation(annotation)
.inspect_err(|err| handler.receive(err.clone()))
.inspect_err(|err| handler.receive(Box::new(err.clone())))
}
// variable declaration
Reading::Atomic(Token::CommandLiteral(command)) => {
@ -971,7 +971,12 @@ impl Parser<'_> {
let close = tree.close;
let expression = tree.tree?;
AssignmentDestination::Indexed(identifier, open, expression, close)
AssignmentDestination::Indexed(
identifier,
open,
Box::new(expression),
close,
)
}
_ => AssignmentDestination::Identifier(identifier),
}
@ -1036,7 +1041,7 @@ impl Parser<'_> {
]),
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
};
@ -1053,11 +1058,7 @@ impl Parser<'_> {
None
};
// read identifier
self.stop_at_significant();
let identifier = self.parse_identifier(handler)?;
match self.stop_at_significant() {
let index = match self.stop_at_significant() {
Reading::IntoDelimited(punc)
if punc.punctuation == '['
&& matches!(variable_type.keyword, KeywordKind::Int | KeywordKind::Bool) =>
@ -1080,7 +1081,7 @@ impl Parser<'_> {
expected: SyntaxKind::Integer,
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
};
@ -1094,67 +1095,72 @@ impl Parser<'_> {
let close_bracket = tree.close;
let inner = tree.tree?;
match inner {
IndexingType::IntegerSize(size) => {
let assignment = self
.try_parse(|p| {
// read equals sign
let equals = p.parse_punctuation('=', true, &VoidHandler)?;
Some((open_bracket, inner, close_bracket))
}
_ => None,
};
// read expression
let expression = p.parse_expression(&VoidHandler)?;
// read identifier
self.stop_at_significant();
let identifier = self.parse_identifier(handler)?;
Ok(VariableDeclarationAssignment { equals, expression })
})
.ok();
// TODO: allow assignment when map and array literals have been implemented
let assignment = if index.is_none() {
self.try_parse(|p| {
// read equals sign
let equals = p.parse_punctuation('=', true, &VoidHandler)?;
Ok(VariableDeclaration::Array(ArrayVariableDeclaration {
variable_type,
identifier,
open_bracket,
size,
close_bracket,
assignment,
annotations: VecDeque::new(),
}))
}
IndexingType::None => match variable_type.keyword {
KeywordKind::Int => {
Ok(VariableDeclaration::Score(ScoreVariableDeclaration {
int_keyword: variable_type,
criteria: criteria_selection,
identifier,
open_bracket,
close_bracket,
assignment: None,
annotations: VecDeque::new(),
}))
}
KeywordKind::Bool => Ok(VariableDeclaration::Tag(TagVariableDeclaration {
bool_keyword: variable_type,
identifier,
open_bracket,
close_bracket,
target_assignment: None,
annotations: VecDeque::new(),
})),
_ => unreachable!(),
},
// read expression
let expression = p.parse_expression(&VoidHandler)?;
Ok(VariableDeclarationAssignment { equals, expression })
})
.ok()
} else {
None
};
match index {
Some((open_bracket, IndexingType::IntegerSize(size), close_bracket)) => {
Ok(VariableDeclaration::Array(ArrayVariableDeclaration {
variable_type,
identifier,
open_bracket,
size,
close_bracket,
assignment,
annotations: VecDeque::new(),
}))
}
Some((open_bracket, IndexingType::None, close_bracket)) => {
match variable_type.keyword {
KeywordKind::Int => Ok(VariableDeclaration::Score(ScoreVariableDeclaration {
int_keyword: variable_type,
criteria: criteria_selection,
identifier,
open_bracket,
close_bracket,
assignment: None,
annotations: VecDeque::new(),
})),
KeywordKind::Bool => Ok(VariableDeclaration::Tag(TagVariableDeclaration {
bool_keyword: variable_type,
identifier,
open_bracket,
close_bracket,
target_assignment: None,
annotations: VecDeque::new(),
})),
_ => unreachable!(),
}
}
// SingleVariableDeclaration with Assignment
Reading::Atomic(Token::Punctuation(punc)) if punc.punctuation == '=' => {
self.forward();
let equals = punc;
let expression = self.parse_expression(handler)?;
let assignment = VariableDeclarationAssignment { equals, expression };
if variable_type.keyword == KeywordKind::Val {
None => {
if matches!(variable_type.keyword, KeywordKind::Val) {
Ok(VariableDeclaration::ComptimeValue(
ComptimeValueDeclaration {
val_keyword: variable_type,
identifier,
assignment: Some(assignment),
assignment,
annotations: VecDeque::new(),
},
))
@ -1162,18 +1168,11 @@ impl Parser<'_> {
Ok(VariableDeclaration::Single(SingleVariableDeclaration {
variable_type,
identifier,
assignment: Some(assignment),
assignment,
annotations: VecDeque::new(),
}))
}
}
// SingleVariableDeclaration without Assignment
_ => Ok(VariableDeclaration::Single(SingleVariableDeclaration {
variable_type,
identifier,
assignment: None,
annotations: VecDeque::new(),
})),
}
}
}

View File

@ -1,5 +1,7 @@
//! Execute block statement syntax tree.
#![expect(clippy::struct_field_names)]
use derive_more::From;
use enum_as_inner::EnumAsInner;
use getset::Getters;
@ -824,7 +826,7 @@ impl Parser<'_> {
expected: SyntaxKind::Punctuation('('),
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}?;
@ -842,7 +844,7 @@ impl Parser<'_> {
expected: SyntaxKind::ExecuteBlock,
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -878,7 +880,7 @@ impl Parser<'_> {
expected: SyntaxKind::ExecuteBlockTail,
found: unexpected.into_token(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}

View File

@ -104,6 +104,8 @@ impl MissingFunctionDeclaration {
impl Display for MissingFunctionDeclaration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use std::fmt::Write as _;
let message = format!(
"no matching function declaration found for invocation of function `{}`",
self.span.str()
@ -118,7 +120,7 @@ impl Display for MissingFunctionDeclaration {
if i > 0 {
message.push_str(", ");
}
message.push_str(&format!("`{}`", alternative.identifier_span.str()));
let _ = write!(message, "`{}`", alternative.identifier_span.str());
}
Some(message + "?")
};
@ -398,6 +400,11 @@ pub enum IllegalIndexingReason {
/// The length indexed object.
length: usize,
},
/// The index provided is invalid
InvalidIndex {
/// The invalid index
index: Span,
},
}
impl Display for IllegalIndexingReason {
@ -421,6 +428,9 @@ impl Display for IllegalIndexingReason {
"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())
}
}
}
}

View File

@ -405,7 +405,7 @@ impl Primary {
Self::Lua(lua) => lua
.eval_comptime(scope, &VoidHandler)
.inspect_err(|err| {
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
})
.map_err(|_| NotComptime {
expression: lua.span(),
@ -1191,7 +1191,7 @@ impl Transpiler {
.into(),
},
);
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
} else {
let prepare_cmd = Command::Raw(format!("tag {entity} remove {tag_name}"));
@ -1225,7 +1225,18 @@ impl Transpiler {
.parent()
.comptime_member_access(member_access, scope, handler)
.map_or_else(
|_| todo!("implement non-comptime member access"),
|_| {
// TODO: implement non-comptime access
let err = TranspileError::IllegalIndexing(IllegalIndexing {
expression: member_access.member().span(),
reason: IllegalIndexingReason::InvalidIndex {
index: member_access.member().span(),
},
});
handler.receive(Box::new(err.clone()));
Err(err)
},
|value| self.store_comptime_value(&value, target, member_access, handler),
),
Primary::Lua(lua) =>
@ -1237,7 +1248,7 @@ impl Transpiler {
let err = TranspileError::MissingValue(MissingValue {
expression: lua.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1262,7 +1273,7 @@ impl Transpiler {
expected_type: target.value_type().into(),
expression: primary.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1326,7 +1337,7 @@ impl Transpiler {
expression: prefix.span(),
expected_type: target.value_type().into(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
},
@ -1422,7 +1433,7 @@ impl Transpiler {
expected_type: target.value_type().into(),
expression: primary.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}?;
@ -1432,7 +1443,7 @@ impl Transpiler {
let err = TranspileError::UnknownIdentifier(UnknownIdentifier {
identifier: ident.span.clone(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1444,7 +1455,7 @@ impl Transpiler {
reason: IllegalIndexingReason::NotIdentifier,
expression: indexed.object().span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}?;
let variable = scope.get_variable(ident.span.str());
@ -1466,7 +1477,7 @@ impl Transpiler {
},
expression: indexed.index().span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1491,7 +1502,7 @@ impl Transpiler {
},
expression: indexed.index().span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
} else {
@ -1501,7 +1512,7 @@ impl Transpiler {
},
expression: indexed.index().span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1530,7 +1541,7 @@ impl Transpiler {
},
expression: indexed.index().span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
} else {
@ -1540,7 +1551,7 @@ impl Transpiler {
},
expression: indexed.index().span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1549,7 +1560,7 @@ impl Transpiler {
expected_type: target.value_type().into(),
expression: primary.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}?;
@ -1559,7 +1570,7 @@ impl Transpiler {
let err = TranspileError::UnknownIdentifier(UnknownIdentifier {
identifier: ident.span.clone(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1636,7 +1647,7 @@ impl Transpiler {
expected_type: ExpectedType::Boolean,
expression: primary.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
Primary::StringLiteral(s) => Ok((
@ -1659,7 +1670,7 @@ impl Transpiler {
message: "Function calls as conditions do not support arguments."
.into(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
} else {
let (func_location, _) = self.get_or_transpile_function(
@ -1702,7 +1713,7 @@ impl Transpiler {
expected_type: ExpectedType::Boolean,
expression: primary.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1710,7 +1721,7 @@ impl Transpiler {
let err = TranspileError::UnknownIdentifier(UnknownIdentifier {
identifier: ident.span.clone(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1725,7 +1736,7 @@ impl Transpiler {
reason: IllegalIndexingReason::NotIdentifier,
expression: indexed.object().span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}?;
#[expect(clippy::option_if_let_else)]
@ -1759,7 +1770,7 @@ impl Transpiler {
},
expression: indexed.index().span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
} else {
@ -1769,7 +1780,7 @@ impl Transpiler {
},
expression: indexed.index().span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1778,7 +1789,7 @@ impl Transpiler {
expected_type: ExpectedType::Boolean,
expression: primary.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1786,7 +1797,7 @@ impl Transpiler {
let err = TranspileError::UnknownIdentifier(UnknownIdentifier {
identifier: ident.span.clone(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1794,7 +1805,18 @@ impl Transpiler {
.parent()
.comptime_member_access(member_access, scope, handler)
.map_or_else(
|_| todo!("implement non-comptime member access"),
|_| {
// TODO: implement non-comptime access
let err = TranspileError::IllegalIndexing(IllegalIndexing {
expression: member_access.member().span(),
reason: IllegalIndexingReason::InvalidIndex {
index: member_access.member().span(),
},
});
handler.receive(Box::new(err.clone()));
Err(err)
},
|value| match value {
ComptimeValue::Boolean(b) => {
Ok((Vec::new(), ExtendedCondition::Comptime(b)))
@ -1804,7 +1826,7 @@ impl Transpiler {
expected_type: ExpectedType::Boolean,
expression: primary.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
ComptimeValue::String(s) => Ok((
@ -1860,7 +1882,7 @@ impl Transpiler {
expected_type: ExpectedType::Boolean,
expression: primary.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
},
@ -1881,7 +1903,7 @@ impl Transpiler {
expected_type: ExpectedType::Boolean,
expression: primary.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
},
@ -1911,7 +1933,7 @@ impl Transpiler {
expected_type: ExpectedType::Boolean,
expression: binary.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -2000,7 +2022,7 @@ impl Transpiler {
expected_type: ExpectedType::Boolean,
expression: binary.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
DataLocation::Storage {
@ -2027,7 +2049,7 @@ impl Transpiler {
expected_type: ExpectedType::Boolean,
expression: binary.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
},
@ -2173,7 +2195,7 @@ impl Transpiler {
expected_type: ExpectedType::Boolean,
expression: source.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
DataLocation::Storage {
@ -2197,7 +2219,7 @@ impl Transpiler {
expression: source.span(),
expected_type: target.value_type().into(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -2223,7 +2245,7 @@ impl Transpiler {
expression: source.span(),
expected_type: target.value_type().into(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -2258,7 +2280,7 @@ impl Transpiler {
expected_type: target.value_type().into(),
expression: source.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -2294,7 +2316,7 @@ impl Transpiler {
expected_type: ExpectedType::Boolean,
expression: source.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
}

View File

@ -32,7 +32,7 @@ use super::{
#[derive(Debug, Clone)]
pub enum TranspiledFunctionArguments {
None,
Static(BTreeMap<String, MacroString>),
Static(BTreeMap<String, MacroString>, Vec<Command>),
Dynamic(Vec<Command>),
}
@ -59,11 +59,11 @@ impl Transpiler {
.expect("called variable should be of type function");
let function_data = function.ok_or_else(|| {
let error = TranspileError::MissingFunctionDeclaration(
let err = TranspileError::MissingFunctionDeclaration(
MissingFunctionDeclaration::from_scope(identifier_span.clone(), scope),
);
handler.receive(error.clone());
error
handler.receive(Box::new(err.clone()));
err
})?;
let VariableData::Function {
@ -99,7 +99,7 @@ impl Transpiler {
.to_string(),
},
);
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
err
}),
TranspileAnnotationValue::Map(_, span) => {
@ -108,7 +108,7 @@ impl Transpiler {
annotation: span.clone(),
message: "Deobfuscate annotation cannot be a map.".to_string(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
},
@ -175,7 +175,7 @@ impl Transpiler {
name: modified_name,
definition: identifier_span.clone(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
@ -193,11 +193,11 @@ impl Transpiler {
let function_location = function_path
.get()
.ok_or_else(|| {
let error = TranspileError::MissingFunctionDeclaration(
let err = TranspileError::MissingFunctionDeclaration(
MissingFunctionDeclaration::from_scope(identifier_span.clone(), scope),
);
handler.receive(error.clone());
error
handler.receive(Box::new(err.clone()));
err
})
.map(String::to_owned)?;
@ -258,7 +258,7 @@ impl Transpiler {
actual: arg_count,
span: identifier_span.clone(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
Some(arg_count) if arg_count > 0 => {
@ -283,7 +283,7 @@ impl Transpiler {
Ok(val) => Ok(Parameter::Static(val.to_macro_string())),
Err(err) => {
let err = TranspileError::NotComptime(err);
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
})
@ -305,7 +305,7 @@ impl Transpiler {
let err = TranspileError::UnknownIdentifier(UnknownIdentifier {
identifier: ident.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
err
})?;
match var.as_ref() {
@ -353,7 +353,7 @@ impl Transpiler {
ExpectedType::String,
]),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -430,9 +430,9 @@ impl Transpiler {
}
if compiled_args.iter().any(|arg| !arg.is_static()) {
let (mut setup_cmds, move_cmds, static_params) = parameters.clone().into_iter().zip(compiled_args).fold(
(Vec::new(), Vec::new(), BTreeMap::new()),
|(mut acc_setup, mut acc_move, mut statics), (param, data)| {
let (require_dyn_params, mut setup_cmds, move_cmds, static_params) = parameters.clone().into_iter().zip(compiled_args).fold(
(false, Vec::new(), Vec::new(), BTreeMap::new()),
|(mut require_dyn_params, mut acc_setup, mut acc_move, mut statics), (param, data)| {
match param.variable_type() {
FunctionVariableType::Macro(_) => {
let arg_name = crate::util::identifier_to_macro(param.identifier().span.str());
@ -455,6 +455,7 @@ impl Transpiler {
};
}
Parameter::Storage { prepare_cmds, storage_name, path } => {
require_dyn_params = true;
acc_setup.extend(prepare_cmds);
acc_move.push(Command::Raw(
format!(r"data modify storage shulkerscript:function_arguments {arg_name} set from storage {storage_name} {path}")
@ -495,6 +496,7 @@ impl Transpiler {
}
},
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);
@ -524,50 +526,67 @@ impl Transpiler {
}
},
}
(acc_setup, acc_move, statics)},
(require_dyn_params, acc_setup, acc_move, statics)},
);
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(',');
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::String(s)
}
MacroString::MacroString(mut parts) => {
parts.insert(0, MacroStringPart::String(format!(r#"{k}:""#)));
let mut ending = '"'.to_string();
if i < statics_len - 1 {
ending.push(',');
MacroString::MacroString(mut parts) => {
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)
}
parts.push(MacroStringPart::String(ending));
MacroString::MacroString(parts)
}
},
));
let statics_cmd = match joined_statics {
MacroString::String(s) => Command::Raw(format!(
r"data merge storage shulkerscript:function_arguments {{{s}}}"
)),
MacroString::MacroString(_) => Command::UsesMacro(
super::util::join_macro_strings([
MacroString::String(
"data merge storage shulkerscript:function_arguments {"
.to_string(),
),
joined_statics,
MacroString::String("}".to_string()),
])
.into(),
),
};
setup_cmds.push(statics_cmd);
setup_cmds.extend(move_cmds);
}),
);
let storage_suffix = function_location.replace(['/', ':'], "_");
let statics_cmd = match joined_statics {
MacroString::String(s) => 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} {{"),
);
Command::UsesMacro(
super::util::join_macro_strings([
prefix,
joined_statics,
MacroString::String("}".to_string()),
])
.into(),
)
}
};
setup_cmds.push(statics_cmd);
setup_cmds.extend(move_cmds);
Ok(TranspiledFunctionArguments::Dynamic(setup_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()
@ -579,7 +598,10 @@ impl Transpiler {
)
.map(|(k, v)| (k.identifier().span.str().to_string(), v))
.collect();
Ok(TranspiledFunctionArguments::Static(function_args))
Ok(TranspiledFunctionArguments::Static(
function_args,
Vec::new(),
))
}
}
_ => Ok(TranspiledFunctionArguments::None),

View File

@ -132,7 +132,7 @@ fn print_function(
Ok((false, cmd, value))
}
_ => Err(TranspileError::UnexpectedExpression(UnexpectedExpression(
Expression::Primary(Primary::Identifier(ident.to_owned())),
Box::new(Expression::Primary(Primary::Identifier(ident.to_owned()))),
))),
}
} else {

View File

@ -58,7 +58,7 @@ mod enabled {
};
self.add_globals(&lua, scope)
.inspect_err(|err| handler.receive(err.clone()))?;
.inspect_err(|err| handler.receive(Box::new(err.clone())))?;
let res = lua
.load(self.code())
@ -67,7 +67,7 @@ mod enabled {
.map_err(|err| {
let err =
TranspileError::from(LuaRuntimeError::from_lua_err(&err, self.span()));
handler.receive(crate::Error::from(err.clone()));
handler.receive(crate::Error::from(Box::new(err.clone())));
err
});
@ -329,7 +329,7 @@ mod enabled {
code_block: self.span(),
error_message: "return table must contain non-nil 'value'".to_string(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
Ok(value) => {
@ -348,7 +348,7 @@ mod enabled {
expression: self.span(),
expected_type: ExpectedType::Boolean,
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -358,7 +358,7 @@ mod enabled {
expression: self.span(),
expected_type: ExpectedType::Boolean,
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}?;
@ -380,7 +380,7 @@ mod enabled {
&err,
self.span(),
));
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
},
@ -393,7 +393,7 @@ mod enabled {
code_block: self.span(),
error_message: format!("invalid return type {}", value.type_name()),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}

View File

@ -196,13 +196,12 @@ impl Transpiler {
}
TranspileAnnotationValue::Expression(_, span)
| TranspileAnnotationValue::Map(_, span) => {
let error =
TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: span.clone(),
message: "uninstall annotation must not have a value".to_string(),
});
handler.receive(error.clone());
Err(error)
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: span.clone(),
message: "uninstall annotation must not have a value".to_string(),
});
handler.receive(Box::new(err.clone()));
Err(err)
}
})
.collect::<TranspileResult<Vec<_>>>()?;
@ -273,7 +272,7 @@ impl Transpiler {
})
.collect();
let function_data = FunctionData {
namespace: namespace.namespace_name().str_content().to_string(),
namespace: namespace.name().str_content().to_string(),
identifier_span: identifier_span.clone(),
parameters: function
.parameters()
@ -318,9 +317,7 @@ impl Transpiler {
}
}
Declaration::Tag(tag) => {
let namespace = self
.datapack
.namespace_mut(namespace.namespace_name().str_content());
let namespace = self.datapack.namespace_mut(namespace.name().str_content());
let sb_tag = namespace.tag_mut(tag.name().str_content(), tag.tag_type());
if let Some(list) = &tag.entries().list {
@ -344,7 +341,7 @@ impl Transpiler {
self.setup_cmds.extend(setup_variable_cmds);
}
};
}
Ok(())
}
@ -415,11 +412,11 @@ impl Transpiler {
self.transpile_run_expression(prefix.operand(), scope, handler)
}
unexpected => {
let error = TranspileError::UnexpectedExpression(UnexpectedExpression(
unexpected.clone(),
let err = TranspileError::UnexpectedExpression(UnexpectedExpression(
Box::new(unexpected.clone()),
));
handler.receive(error.clone());
Err(error)
handler.receive(Box::new(err.clone()));
Err(err)
}
},
SemicolonStatement::VariableDeclaration(decl) => self
@ -495,11 +492,11 @@ impl Transpiler {
read_only: _,
} => value.read().unwrap().as_ref().map_or_else(
|| {
let error = TranspileError::MissingValue(MissingValue {
let err = TranspileError::MissingValue(MissingValue {
expression: ident.span.clone(),
});
handler.receive(error.clone());
Err(error)
handler.receive(Box::new(err.clone()));
Err(err)
},
|val| {
let cmd = val.to_string_no_macro().map_or_else(
@ -532,20 +529,21 @@ impl Transpiler {
)))),
),
_ => {
let error =
TranspileError::UnexpectedExpression(UnexpectedExpression(
Expression::Primary(Primary::Identifier(ident.clone())),
));
handler.receive(error.clone());
return Err(error);
let err = TranspileError::UnexpectedExpression(
UnexpectedExpression(Box::new(Expression::Primary(
Primary::Identifier(ident.clone()),
))),
);
handler.receive(Box::new(err.clone()));
return Err(err);
}
}
} else {
let error = TranspileError::UnknownIdentifier(UnknownIdentifier {
let err = TranspileError::UnknownIdentifier(UnknownIdentifier {
identifier: ident.span.clone(),
});
handler.receive(error.clone());
return Err(error);
handler.receive(Box::new(err.clone()));
return Err(err);
}
}
_ => {
@ -590,11 +588,11 @@ impl Transpiler {
read_only: _,
}) => value.read().unwrap().as_ref().map_or_else(
|| {
let error = TranspileError::MissingValue(MissingValue {
let err = TranspileError::MissingValue(MissingValue {
expression: ident.span.clone(),
});
handler.receive(error.clone());
Err(error)
handler.receive(Box::new(err.clone()));
Err(err)
},
|val| {
let cmd = val.to_string_no_macro().map_or_else(
@ -605,18 +603,18 @@ impl Transpiler {
},
),
Some(_) => {
let error = TranspileError::UnexpectedExpression(UnexpectedExpression(
let err = TranspileError::UnexpectedExpression(UnexpectedExpression(Box::new(
Expression::Primary(expression.clone()),
));
handler.receive(error.clone());
Err(error)
)));
handler.receive(Box::new(err.clone()));
Err(err)
}
None => {
let error = TranspileError::UnknownIdentifier(UnknownIdentifier {
let err = TranspileError::UnknownIdentifier(UnknownIdentifier {
identifier: ident.span.clone(),
});
handler.receive(error.clone());
Err(error)
handler.receive(Box::new(err.clone()));
Err(err)
}
},
@ -625,11 +623,11 @@ impl Transpiler {
| Primary::Prefix(_)
| Primary::Indexed(_)
| Primary::MemberAccess(_) => {
let error = TranspileError::UnexpectedExpression(UnexpectedExpression(
let err = TranspileError::UnexpectedExpression(UnexpectedExpression(Box::new(
Expression::Primary(expression.clone()),
));
handler.receive(error.clone());
Err(error)
)));
handler.receive(Box::new(err.clone()));
Err(err)
}
Primary::StringLiteral(string) => {
Ok(vec![Command::Raw(string.str_content().to_string())])
@ -643,14 +641,14 @@ impl Transpiler {
expected_type: ExpectedType::String,
expression: code.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
Err(_) => {
let err = TranspileError::MissingValue(MissingValue {
expression: code.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
},
@ -667,12 +665,12 @@ impl Transpiler {
expression: bin.span(),
expected_type: ExpectedType::String,
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
Err(not_comptime) => {
let err = TranspileError::NotComptime(not_comptime);
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
},
@ -694,7 +692,7 @@ impl Transpiler {
scope.get_variable(func.identifier().span.str()).as_deref()
{
implementation(self, scope, func).inspect_err(|err| {
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
})
} else {
let (location, arguments) = self.get_or_transpile_function(
@ -705,48 +703,61 @@ impl Transpiler {
)?;
let mut function_call = format!("function {location}");
match arguments {
TranspiledFunctionArguments::Static(arguments) => {
TranspiledFunctionArguments::Static(arguments, mut setup_cmds) => {
use std::fmt::Write;
let arguments_iter = arguments.iter().map(|(ident, v)| match v {
MacroString::String(s) => MacroString::String(format!(
r#"{macro_name}:"{escaped}""#,
macro_name = crate::util::identifier_to_macro(ident),
escaped = crate::util::escape_str(s)
)),
MacroString::MacroString(parts) => MacroString::MacroString(
std::iter::once(MacroStringPart::String(format!(
r#"{macro_name}:""#,
macro_name = crate::util::identifier_to_macro(ident)
)))
.chain(parts.clone().into_iter().map(|part| match part {
MacroStringPart::String(s) => {
MacroStringPart::String(crate::util::escape_str(&s).to_string())
}
macro_usage @ MacroStringPart::MacroUsage(_) => macro_usage,
}))
.chain(std::iter::once(MacroStringPart::String('"'.to_string())))
.collect(),
),
});
let arguments = super::util::join_macro_strings(arguments_iter);
let cmd = match arguments {
MacroString::String(arguments) => {
write!(function_call, " {{{arguments}}}").unwrap();
Command::Raw(function_call)
}
MacroString::MacroString(mut parts) => {
function_call.push_str(" {");
parts.insert(0, MacroStringPart::String(function_call));
parts.push(MacroStringPart::String('}'.to_string()));
Command::UsesMacro(MacroString::MacroString(parts).into())
let cmd = if arguments.is_empty() {
Command::Raw(function_call)
} else {
let arguments_iter = arguments.iter().map(|(ident, v)| match v {
MacroString::String(s) => MacroString::String(format!(
r#"{macro_name}:"{escaped}""#,
macro_name = crate::util::identifier_to_macro(ident),
escaped = crate::util::escape_str(s)
)),
MacroString::MacroString(parts) => MacroString::MacroString(
std::iter::once(MacroStringPart::String(format!(
r#"{macro_name}:""#,
macro_name = crate::util::identifier_to_macro(ident)
)))
.chain(parts.clone().into_iter().map(|part| match part {
MacroStringPart::String(s) => MacroStringPart::String(
crate::util::escape_str(&s).to_string(),
),
macro_usage @ MacroStringPart::MacroUsage(_) => macro_usage,
}))
.chain(std::iter::once(MacroStringPart::String('"'.to_string())))
.collect(),
),
});
let arguments = super::util::join_macro_strings(arguments_iter);
match arguments {
MacroString::String(arguments) => {
write!(function_call, " {{{arguments}}}").unwrap();
Command::Raw(function_call)
}
MacroString::MacroString(mut parts) => {
function_call.push_str(" {");
parts.insert(0, MacroStringPart::String(function_call));
parts.push(MacroStringPart::String('}'.to_string()));
Command::UsesMacro(MacroString::MacroString(parts).into())
}
}
};
Ok(vec![cmd])
setup_cmds.push(cmd);
Ok(setup_cmds)
}
TranspiledFunctionArguments::Dynamic(mut cmds) => {
function_call.push_str(" with storage shulkerscript:function_arguments");
use std::fmt::Write as _;
let _ = write!(
function_call,
" with storage shulkerscript:function_arguments_{suffix}",
suffix = location.replace(['/', ':'], "_")
);
cmds.push(Command::Raw(function_call));
Ok(cmds)
}

View File

@ -60,7 +60,7 @@ impl MacroString {
///
/// # Errors
/// - If the macro string contains macros
pub fn as_str(&self) -> Result<std::borrow::Cow<str>, &[MacroStringPart]> {
pub fn as_str(&self) -> Result<std::borrow::Cow<'_, str>, &[MacroStringPart]> {
match self {
Self::String(s) => Ok(std::borrow::Cow::Borrowed(s)),
Self::MacroString(parts) if self.contains_macros() => Err(parts),

View File

@ -308,7 +308,7 @@ impl Transpiler {
.comptime_eval(scope, handler)
.map_err(|err| {
let err = TranspileError::NotComptime(err);
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
err
})?,
)
@ -569,7 +569,7 @@ impl Transpiler {
},
expression: expression.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
Some(_) => {
@ -579,7 +579,7 @@ impl Transpiler {
expected: ExpectedType::String,
},
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
None => {
@ -588,7 +588,7 @@ impl Transpiler {
identifier: identifier.span(),
message: "Cannot assign to a scoreboard without indexing".to_string(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
},
@ -617,7 +617,7 @@ impl Transpiler {
length: targets.len(),
},
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
}
@ -628,7 +628,7 @@ impl Transpiler {
expected: ExpectedType::Integer,
},
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
None => {
@ -637,7 +637,7 @@ impl Transpiler {
identifier: identifier.span(),
message: "Cannot assign to an array without indexing".to_string(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
}
@ -671,7 +671,7 @@ impl Transpiler {
length: paths.len(),
},
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
}
@ -682,7 +682,7 @@ impl Transpiler {
expected: ExpectedType::Integer,
},
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
None => {
@ -691,7 +691,7 @@ impl Transpiler {
identifier: identifier.span(),
message: "Cannot assign to an array without indexing".to_string(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
}
@ -709,7 +709,7 @@ impl Transpiler {
expected: ExpectedType::String,
},
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
Some(_) => {
@ -719,7 +719,7 @@ impl Transpiler {
expected: ExpectedType::String,
},
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
None => {
@ -728,7 +728,7 @@ impl Transpiler {
identifier: identifier.span(),
message: "Cannot assign to a tag without indexing".to_string(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
},
@ -738,13 +738,13 @@ impl Transpiler {
identifier: identifier.span(),
message: "Cannot assign to a read-only value.".to_string(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
return Err(err);
}
let comptime_value =
expression.comptime_eval(scope, handler).map_err(|err| {
let err = TranspileError::NotComptime(err);
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
err
})?;
*value.write().unwrap() = Some(comptime_value);
@ -765,7 +765,7 @@ impl Transpiler {
}
),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}?;
@ -775,7 +775,7 @@ impl Transpiler {
identifier: identifier.span(),
message: "Variable does not exist.".to_string(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -796,12 +796,12 @@ impl Transpiler {
let deobfuscate_annotation = deobfuscate_annotations.next();
if let Some(duplicate) = deobfuscate_annotations.next() {
let error = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: duplicate.span(),
message: "Multiple deobfuscate annotations are not allowed.".to_string(),
});
handler.receive(error.clone());
return Err(error);
handler.receive(Box::new(err.clone()));
return Err(err);
}
if let Some(deobfuscate_annotation) = deobfuscate_annotation {
let deobfuscate_annotation_value = TranspileAnnotationValue::from_annotation_value(
@ -818,33 +818,32 @@ impl Transpiler {
{
// TODO: change invalid criteria if boolean
if !crate::util::is_valid_scoreboard_objective_name(&name_eval) {
let error = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation must be a valid scoreboard objective name.".to_string()
});
handler.receive(error.clone());
return Err(error);
handler.receive(Box::new(err.clone()));
return Err(err);
}
Ok(name_eval)
} else {
let error = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation could not have been evaluated at compile time.".to_string()
});
handler.receive(error.clone());
Err(error)
handler.receive(Box::new(err.clone()));
Err(err)
}
}
TranspileAnnotationValue::None(_) => Ok(identifier.span.str().to_string()),
TranspileAnnotationValue::Map(_, _) => {
let error =
TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation must have no value or must be string."
.to_string(),
});
handler.receive(error.clone());
Err(error)
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation must have no value or must be string."
.to_string(),
});
handler.receive(Box::new(err.clone()));
Err(err)
}
}
} else {
@ -881,12 +880,12 @@ impl Transpiler {
let deobfuscate_annotation = deobfuscate_annotations.next();
if let Some(duplicate) = deobfuscate_annotations.next() {
let error = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: duplicate.span(),
message: "Multiple deobfuscate annotations are not allowed.".to_string(),
});
handler.receive(error.clone());
return Err(error);
handler.receive(Box::new(err.clone()));
return Err(err);
}
if let Some(deobfuscate_annotation) = deobfuscate_annotation {
let deobfuscate_annotation_value = TranspileAnnotationValue::from_annotation_value(
@ -896,14 +895,13 @@ impl Transpiler {
if let TranspileAnnotationValue::Map(map, _) = deobfuscate_annotation_value {
if map.len() > 2 {
let error =
TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation must have at most 2 key-value pairs."
.to_string(),
});
handler.receive(error.clone());
return Err(error);
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation must have at most 2 key-value pairs."
.to_string(),
});
handler.receive(Box::new(err.clone()));
return Err(err);
}
if let (Some(name), Some(target)) = (map.get("name"), map.get("target")) {
if let (
@ -923,56 +921,54 @@ impl Transpiler {
) {
// TODO: change invalid criteria if boolean
if !crate::util::is_valid_scoreboard_objective_name(&name_eval) {
let error = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation 'name' must be a valid scoreboard objective name.".to_string()
});
handler.receive(error.clone());
return Err(error);
handler.receive(Box::new(err.clone()));
return Err(err);
}
if !crate::util::is_valid_scoreboard_target(&target_eval) {
let error = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation 'target' must be a valid scoreboard player name.".to_string()
});
handler.receive(error.clone());
return Err(error);
handler.receive(Box::new(err.clone()));
return Err(err);
}
Ok((name_eval, target_eval))
} else {
let error = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation 'name' or 'target' could not have been evaluated at compile time.".to_string()
});
handler.receive(error.clone());
Err(error)
handler.receive(Box::new(err.clone()));
Err(err)
}
} else {
let error = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation 'name' and 'target' must be compile time expressions.".to_string()
});
handler.receive(error.clone());
Err(error)
handler.receive(Box::new(err.clone()));
Err(err)
}
} else {
let error =
TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message:
"Deobfuscate annotation must have both 'name' and 'target' keys."
.to_string(),
});
handler.receive(error.clone());
Err(error)
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation must have both 'name' and 'target' keys."
.to_string(),
});
handler.receive(Box::new(err.clone()));
Err(err)
}
} else {
let error = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation must be a map.".to_string(),
});
handler.receive(error.clone());
Err(error)
handler.receive(Box::new(err.clone()));
Err(err)
}
} else {
let hashed = md5::hash(program_identifier).to_hex_lowercase();
@ -1017,12 +1013,12 @@ impl Transpiler {
let deobfuscate_annotation = deobfuscate_annotations.next();
if let Some(duplicate) = deobfuscate_annotations.next() {
let error = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: duplicate.span(),
message: "Multiple deobfuscate annotations are not allowed.".to_string(),
});
handler.receive(error.clone());
return Err(error);
handler.receive(Box::new(err.clone()));
return Err(err);
}
if let Some(deobfuscate_annotation) = deobfuscate_annotation {
let deobfuscate_annotation_value = TranspileAnnotationValue::from_annotation_value(
@ -1048,24 +1044,21 @@ impl Transpiler {
}
TranspileAnnotationValue::Map(map, _) => {
// TODO: implement when map deobfuscate annotation is implemented
let error =
TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation value must be a string or none."
.to_string(),
});
handler.receive(error.clone());
Err(error)
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation value must be a string or none."
.to_string(),
});
handler.receive(Box::new(err.clone()));
Err(err)
}
TranspileAnnotationValue::Expression(_, _) => {
let error =
TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation value must be a map or none."
.to_string(),
});
handler.receive(error.clone());
Err(error)
let err = TranspileError::IllegalAnnotationContent(IllegalAnnotationContent {
annotation: deobfuscate_annotation.span(),
message: "Deobfuscate annotation value must be a map or none.".to_string(),
});
handler.receive(Box::new(err.clone()));
Err(err)
}
}
} else {
@ -1168,7 +1161,7 @@ impl Transpiler {
expression: expression.span(),
expected_type: to.value_type().into(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1219,7 +1212,7 @@ impl Transpiler {
expression: expression.span(),
expected_type: to.value_type().into(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}
@ -1228,7 +1221,7 @@ impl Transpiler {
expected_type: ExpectedType::Boolean,
expression: expression.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
},
@ -1237,7 +1230,7 @@ impl Transpiler {
expected_type: to.value_type().into(),
expression: expression.span(),
});
handler.receive(err.clone());
handler.receive(Box::new(err.clone()));
Err(err)
}
}

View File

@ -4,7 +4,7 @@ use std::borrow::Cow;
/// Escapes `"` and `\` in a string.
#[must_use]
pub fn escape_str(s: &str) -> Cow<str> {
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() {
@ -22,7 +22,7 @@ pub fn escape_str(s: &str) -> Cow<str> {
/// Unescapes '\`', `\`, `\n`, `\r` and `\t` in a string.
#[must_use]
pub fn unescape_macro_string(s: &str) -> Cow<str> {
pub fn unescape_macro_string(s: &str) -> Cow<'_, str> {
if s.contains('\\') || s.contains('`') {
Cow::Owned(
s.replace("\\n", "\n")
@ -39,7 +39,7 @@ pub fn unescape_macro_string(s: &str) -> Cow<str> {
/// 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> {
pub fn identifier_to_macro(ident: &str) -> std::borrow::Cow<'_, str> {
if ident.contains("__")
|| ident
.chars()
@ -82,7 +82,7 @@ pub fn identifier_to_macro(ident: &str) -> std::borrow::Cow<str> {
/// 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> {
pub fn identifier_to_scoreboard_target(ident: &str) -> std::borrow::Cow<'_, str> {
if !(..=16).contains(&ident.len())
|| ident
.chars()

View File

@ -20,10 +20,7 @@ fn parsing_test1() {
)
.expect("Failed to parse");
assert_eq!(
parsed.namespace().namespace_name().str_content(),
"parsing-test"
);
assert_eq!(parsed.namespace().name().str_content(), "parsing-test");
let declarations = parsed.declarations();
assert_eq!(declarations.len(), 1);