Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: text component SNBT changes #306

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mecha/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ class AstNbtByteArray(AstNbt):
elements: AstChildren[AstNbt] = required_field()

parser = None
array_prefix = "B"

def evaluate(self) -> Any:
return ByteArray([element.evaluate() for element in self.elements]) # type: ignore
Expand All @@ -844,6 +845,7 @@ class AstNbtIntArray(AstNbt):
elements: AstChildren[AstNbt] = required_field()

parser = None
array_prefix = "I"

def evaluate(self) -> Any:
return IntArray([element.evaluate() for element in self.elements]) # type: ignore
Expand All @@ -856,6 +858,7 @@ class AstNbtLongArray(AstNbt):
elements: AstChildren[AstNbt] = required_field()

parser = None
array_prefix = "L"

def evaluate(self) -> Any:
return LongArray([element.evaluate() for element in self.elements]) # type: ignore
Expand Down
44 changes: 18 additions & 26 deletions mecha/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
from .config import CommandTree
from .error import MechaError
from .spec import CommandSpec, Parser
from .utils import JsonQuoteHelper, QuoteHelper, string_to_number
from .utils import JsonQuoteHelper, NbtQuoteHelper, QuoteHelper, string_to_number

NUMBER_PATTERN: str = r"-?(?:\d+\.?\d*|\.\d+)"

Expand Down Expand Up @@ -493,8 +493,8 @@ def get_default_parsers() -> Dict[str, Parser]:
]
),
"command:argument:minecraft:column_pos": delegate("column_pos"),
"command:argument:minecraft:component": MultilineParser(delegate("json")),
"command:argument:minecraft:style": MultilineParser(delegate("json")),
"command:argument:minecraft:component": MultilineParser(delegate("nbt")),
"command:argument:minecraft:style": MultilineParser(delegate("nbt")),
"command:argument:minecraft:dimension": delegate("resource_location"),
"command:argument:minecraft:entity": delegate("entity"),
"command:argument:minecraft:entity_anchor": delegate("entity_anchor"),
Expand Down Expand Up @@ -583,6 +583,12 @@ def get_parsers(version: VersionNumber = LATEST_MINECRAFT_VERSION) -> Dict[str,
if version < (1, 20):
parsers["scoreboard_slot"] = BasicLiteralParser(AstLegacyScoreboardSlot)

if version < (1, 21):
parsers["command:argument:minecraft:component"] = MultilineParser(
delegate("json")
)
parsers["command:argument:minecraft:style"] = MultilineParser(delegate("json"))

return parsers


Expand Down Expand Up @@ -1165,13 +1171,7 @@ class NbtParser:
}
)

quote_helper: QuoteHelper = field(
default_factory=lambda: QuoteHelper(
escape_sequences={
r"\\": "\\",
}
)
)
quote_helper: QuoteHelper = field(default_factory=NbtQuoteHelper)

def __post_init__(self):
self.compound_entry_parser = self.parse_compound_entry
Expand Down Expand Up @@ -1234,24 +1234,16 @@ def __call__(self, stream: TokenStream) -> AstNbt:
node = AstNbtLongArray(elements=AstChildren(elements))
element_type = Long # type: ignore
msg = "Expected all elements to be long integers."

node = set_location(node, array, stream.current)

for element in node.elements:
if isinstance(element, AstNbtValue):
if type(element.value) is not element_type:
raise element.emit_error(InvalidSyntax(msg))
else:
node = AstNbtList(elements=AstChildren(elements))
element_type = None
msg = "Expected all elements to have the same type."

node = set_location(node, bracket or array, stream.current)

for element in node.elements:
if isinstance(element, AstNbtValue):
if not element_type:
element_type = type(element.value)
elif type(element.value) is not element_type:
raise element.emit_error(InvalidSyntax(msg))
elif isinstance(element, AstNbt): # type: ignore
if not element_type:
element_type = type(element)
elif type(element) is not element_type:
raise element.emit_error(InvalidSyntax(msg))
node = set_location(node, bracket, stream.current)

return node

Expand Down
71 changes: 66 additions & 5 deletions mecha/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@
AstMacroLineText,
AstMacroLineVariable,
AstMessage,
AstNbt,
AstNbtBool,
AstNbtByteArray,
AstNbtCompound,
AstNbtCompoundKey,
AstNbtIntArray,
AstNbtList,
AstNbtLongArray,
AstNbtPath,
AstNbtPathKey,
AstNbtPathSubscript,
AstNbtValue,
AstNode,
AstNumber,
AstParticle,
Expand All @@ -61,9 +66,10 @@
from .database import CompilationDatabase
from .dispatch import Visitor, rule
from .spec import CommandSpec
from .utils import QuoteHelper, number_to_string
from .utils import NbtQuoteHelper, QuoteHelper, number_to_string

REGEX_COMMENTS = re.compile(r"^(?:(\s*#.*)|.+)", re.MULTILINE)
UNQUOTED_COMPOUND_KEY = re.compile(r"^[a-zA-Z0-9._+-]+$")


class FormattingOptions(BaseModel):
Expand Down Expand Up @@ -97,6 +103,7 @@ class Serializer(Visitor):
}
)
)
nbt_quote_helper: QuoteHelper = field(default_factory=NbtQuoteHelper)

def __call__(self, node: AstNode, **kwargs: Any) -> str: # type: ignore
result: List[str] = []
Expand Down Expand Up @@ -237,14 +244,68 @@ def json(self, node: AstJson, result: List[str]):
)
)

@rule(AstNbt)
def nbt(self, node: AstNbt, result: List[str]):
result.append(node.evaluate().snbt(compact=self.formatting.nbt_compact))
@rule(AstNbtValue)
def nbt_value(self, node: AstNbtValue, result: List[str]):
if isinstance(node.value, str):
result.append(self.nbt_quote_helper.quote_string(node.value))
else:
result.append(node.evaluate().snbt(compact=self.formatting.nbt_compact))

@rule(AstNbtBool)
def nbt_bool(self, node: AstNbtBool, result: List[str]):
result.append("true" if node.value else "false")

@rule(AstNbtList)
def nbt_list(self, node: AstNbtList, result: List[str]):
result.append("[")
comma = "," if self.formatting.nbt_compact else ", "
sep = ""
for element in node.elements:
result.append(sep)
sep = comma
yield element
result.append("]")

@rule(AstNbtCompoundKey)
def nbt_compound_key(self, node: AstNbtCompoundKey, result: List[str]):
if UNQUOTED_COMPOUND_KEY.match(node.value):
result.append(node.value)
else:
result.append(self.quote_helper.quote_string(node.value))

@rule(AstNbtCompound)
def nbt_compound(self, node: AstNbtCompound, result: List[str]):
result.append("{")
comma, colon = (",", ":") if self.formatting.nbt_compact else (", ", ": ")
sep = ""
for entry in node.entries:
result.append(sep)
sep = comma
yield entry.key
result.append(colon)
yield entry.value
result.append("}")

@rule(AstNbtByteArray)
@rule(AstNbtIntArray)
@rule(AstNbtLongArray)
def nbt_array(
self,
node: Union[AstNbtByteArray, AstNbtIntArray, AstNbtLongArray],
result: List[str],
):
result.append("[")
result.append(node.array_prefix)
semicolon = ";" if self.formatting.nbt_compact else "; "
result.append(semicolon)
comma = "," if self.formatting.nbt_compact else ", "
sep = ""
for element in node.elements:
result.append(sep)
sep = comma
yield element
result.append("]")

@rule(AstResourceLocation)
def resource_location(self, node: AstResourceLocation, result: List[str]):
result.append(node.get_value())
Expand Down
54 changes: 51 additions & 3 deletions mecha/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"QuoteHelper",
"QuoteHelperWithUnicode",
"JsonQuoteHelper",
"NbtQuoteHelper",
"InvalidEscapeSequence",
"normalize_whitespace",
"string_to_number",
Expand All @@ -14,7 +15,7 @@
import re
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, Optional, Union
from typing import Any, Dict, List, Optional, Union

from beet import File
from beet.core.utils import FileSystemPath
Expand All @@ -25,6 +26,7 @@
ESCAPE_REGEX = re.compile(r"\\.")
UNICODE_ESCAPE_REGEX = re.compile(r"\\(?:u([0-9a-fA-F]{4})|.)")
AVOID_QUOTES_REGEX = re.compile(r"^[0-9A-Za-z_\.\+\-]+$")
QUOTE_REGEX = re.compile(r"\"|'")

WHITESPACE_REGEX = re.compile(r"\s+")

Expand Down Expand Up @@ -70,10 +72,15 @@ class QuoteHelper:
avoid_quotes_regex: "re.Pattern[str]" = AVOID_QUOTES_REGEX

escape_sequences: Dict[str, str] = field(default_factory=dict)
unquote_only_escape_characters: List[str] = field(default_factory=list)
escape_characters: Dict[str, str] = field(init=False)

def __post_init__(self):
self.escape_characters = {v: k for k, v in self.escape_sequences.items()}
self.escape_characters = {
v: k
for k, v in self.escape_sequences.items()
if not k in self.unquote_only_escape_characters
}

def unquote_string(self, token: Token) -> str:
"""Remove quotes and substitute escaped characters."""
Expand Down Expand Up @@ -106,9 +113,14 @@ def quote_string(self, value: str, quote: str = '"') -> str:
"""Wrap the string in quotes if it can't be represented unquoted."""
if self.avoid_quotes_regex.match(value):
return value
value = self.handle_quoting(value)
return quote + value.replace(quote, "\\" + quote) + quote

def handle_quoting(self, value: str) -> str:
"""Handle escape characters during quoting."""
for match, seq in self.escape_characters.items():
value = value.replace(match, seq)
return quote + value.replace(quote, "\\" + quote) + quote
return value


@dataclass
Expand All @@ -122,6 +134,17 @@ def handle_substitution(self, token: Token, match: "re.Match[str]") -> str:
return chr(int(unicode_hex, 16))
return super().handle_substitution(token, match)

def handle_quoting(self, value: str) -> str:
value = super().handle_quoting(value)

def escape_char(char: str) -> str:
codepoint = ord(char)
if codepoint < 128:
return char
return f"\\u{codepoint:04x}"

return "".join(escape_char(c) for c in value)


@dataclass
class JsonQuoteHelper(QuoteHelperWithUnicode):
Expand All @@ -138,6 +161,31 @@ class JsonQuoteHelper(QuoteHelperWithUnicode):
)


@dataclass
class NbtQuoteHelper(QuoteHelperWithUnicode):
"""Quote helper used for snbt."""

escape_sequences: Dict[str, str] = field(
default_factory=lambda: {
r"\\": "\\",
r"\b": "\b",
r"\f": "\f",
r"\n": "\n",
r"\r": "\r",
r"\s": " ",
r"\t": "\t",
}
)
unquote_only_escape_characters: List[str] = field(default_factory=lambda: [r"\s"])

def quote_string(self, value: str, quote: Optional[str] = None) -> str:
if not quote:
found = QUOTE_REGEX.search(value)
quote = ("'" if found.group() == '"' else '"') if found else "'"
value = super().handle_quoting(value)
return quote + value.replace(quote, "\\" + quote) + quote


def underline_code(
source: str,
location: SourceLocation,
Expand Down
13 changes: 7 additions & 6 deletions tests/resources/argument_examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,13 @@
"{\"text\":\"hello world\"}",
"[\"\"]",
"\"\\u2588\"",
"{\"\\u2588\\\"\\n\":[123]}"
"{\"\\u2588\\\"\\n\":[123]}",
"wat"
]
},
{
"invalid": true,
"examples": ["wat", "{\"hey\": ]}"]
"examples": ["{\"hey\": ]}"]
}
],
"minecraft:dimension": [
Expand Down Expand Up @@ -415,18 +416,18 @@
"[L;1l,2l]",
"[a, b, c]",
"[[], [1], [foo]]",
"[[],[[[[[[[[[[],[[[[5,1]],[]]]]]]]]]]],[[[[]]]]]]"
"[[],[[[[[[[[[[],[[[[5,1]],[]]]]]]]]]]],[[[[]]]]]]",
"\"\\n\"",
"[a,1]",
"[[],[],1b]"
]
},
{
"invalid": true,
"examples": [
"\"\\\"",
"\"\\n\"",
"\"\\\\\\\"",
"{\"\\\":1}",
"[a,1]",
"[[],[],1b]",
"[B;5l,4l,3]",
"[I;5l,4l,3]",
"[L;5l,4l,3]",
Expand Down
4 changes: 2 additions & 2 deletions tests/snapshots/examples__build_basic_embed__0.pack.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"functions": [
{
"function": "minecraft:set_nbt",
"tag": "{custom: {value: \"owo\"}}"
"tag": "{custom: {value: 'owo'}}"
}
]
}
Expand All @@ -59,7 +59,7 @@
"functions": [
{
"function": "minecraft:set_nbt",
"tag": "{custom: {item: \"owo\", json_text_component: '{\"function\": \"minecraft:set_nbt\", \"tag\": \"{who_cares: \\\\\"owo\\\\\"}\", \"text\": \"nonsense\", \"color\": \"red\"}'}}"
"tag": "{custom: {item: 'owo', json_text_component: '{\"function\": \"minecraft:set_nbt\", \"tag\": \"{who_cares: \\'owo\\'}\", \"text\": \"nonsense\", \"color\": \"red\"}'}}"
}
]
}
Expand Down
Loading