Skip to content

Commit

Permalink
fix: handle anonymous macro invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Oct 10, 2023
1 parent ab190ef commit 6d4d5c0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
18 changes: 18 additions & 0 deletions bolt/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ def create_bolt_command_parser(
parser = MemoHandler(parser)
parser = BindingStorageHandler(parser)
parser = ImportStatementHandler(parser, modules)
parser = ContextManagerHandler(parser)
parser = SubcommandConstraint(parser, command_identifiers=bolt_prototypes)
return parser

Expand Down Expand Up @@ -2044,6 +2045,23 @@ def __call__(self, stream: TokenStream) -> Any:
return node


@dataclass
class ContextManagerHandler:
"""Handle context managers."""

parser: Parser

def __call__(self, stream: TokenStream) -> Any:
node = self.parser(stream)

if isinstance(node, AstCommand) and node.identifier == "with:subcommand":
if node.arguments and isinstance(command := node.arguments[0], AstCommand):
if not command.identifier.startswith("with:expression"):
return command

return node


@dataclass
class FlushPendingBindingsParser:
"""Parser that flushes pending bindings."""
Expand Down
3 changes: 3 additions & 0 deletions tests/resources/bolt_examples.mcfunction
Original file line number Diff line number Diff line change
Expand Up @@ -1339,3 +1339,6 @@ predicate x[]
###
x = ./foo
predicate x []
###
with storage ./args:
$say $(message)
35 changes: 35 additions & 0 deletions tests/snapshots/bolt__parse_366__0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
with storage ./args:
$say $(message)
---
<class 'mecha.ast.AstRoot'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=41, lineno=3, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
location: SourceLocation(pos=5, lineno=1, colno=6)
end_location: SourceLocation(pos=40, lineno=2, colno=20)
identifier: 'with:storage:source:commands'
arguments:
<class 'mecha.ast.AstResourceLocation'>
location: SourceLocation(pos=13, lineno=1, colno=14)
end_location: SourceLocation(pos=19, lineno=1, colno=20)
is_tag: False
namespace: 'demo'
path: 'args'
<class 'mecha.ast.AstRoot'>
location: SourceLocation(pos=25, lineno=2, colno=5)
end_location: SourceLocation(pos=40, lineno=2, colno=20)
commands:
<class 'mecha.ast.AstMacroLine'>
location: SourceLocation(pos=25, lineno=2, colno=5)
end_location: SourceLocation(pos=40, lineno=2, colno=20)
identifier: 'mecha:sentinel'
arguments:
<class 'mecha.ast.AstMacroLineText'>
location: SourceLocation(pos=26, lineno=2, colno=6)
end_location: SourceLocation(pos=30, lineno=2, colno=10)
value: 'say '
<class 'mecha.ast.AstMacroLineVariable'>
location: SourceLocation(pos=30, lineno=2, colno=10)
end_location: SourceLocation(pos=40, lineno=2, colno=20)
value: 'message'
4 changes: 4 additions & 0 deletions tests/snapshots/bolt__parse_366__1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Nothing
---
output = None
---

0 comments on commit 6d4d5c0

Please sign in to comment.