Skip to content

Commit

Permalink
fix: reset generator to prevent dropping tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Oct 19, 2022
1 parent 1a00f20 commit 6551578
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 5 deletions.
4 changes: 4 additions & 0 deletions bolt/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,10 @@ class RootScopeHandler:

def __call__(self, stream: TokenStream) -> Any:
with stream.provide(root_scope="root_scope" not in stream.data):
# Resetting the underlying token generator shouldn't be necessary but
# currently overloading between nested_root/nested_yaml drops tokens
# for some reason (see summon macro example).
stream.generator = stream.generate_tokens()
return self.parser(stream)


Expand Down
6 changes: 6 additions & 0 deletions examples/bolt_summon/beet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require:
- bolt
data_pack:
load: "src"
pipeline:
- mecha
52 changes: 52 additions & 0 deletions examples/bolt_summon/src/data/demo/functions/foo.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from nbtlib import List, String

SUMMON_INIT_TAG = "custom_summon_init"

macro
summon
entity=minecraft:entity_summon
pos=minecraft:vec3
nbt=minecraft:nbt_compound_tag
body=mecha:nested_root:
nbt = nbt.evaluate()
nbt.setdefault("Tags", List[String]()).append(String(SUMMON_INIT_TAG))
summon entity pos nbt
as @e[type=entity.get_canonical_value(), tag=SUMMON_INIT_TAG] at @s:
yield body
tag @s remove SUMMON_INIT_TAG

# Overload to make nbt optional
macro
summon
entity=minecraft:entity_summon
pos=minecraft:vec3
body=mecha:nested_root:
summon entity pos {}:
yield body

# Overload to make pos optional
macro
summon
entity=minecraft:entity_summon
body=mecha:nested_root:
summon entity ~ ~ ~:
yield body

summon zombie ~ ~ ~ {Tags: ["my_custom_mob"]}:
say Hello I just spawned!
effect give @e[distance=..3] poison

summon zombie 1 2 3:
say no nbt

summon zombie 1 2 3:
Tags: ["using_nested_yaml"]

summon zombie:
say no position or nbt

def wtf():
say hello

summon zombie ~1 ~2 ~3:
wtf()
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_177__0.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#>ERROR Expected colon but reached end of file.
#>ERROR Expected colon but got newline '\n'.
# line 1, column 25
# 1 | append function demo:foo
append function demo:foo
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_184__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ positioned ~ ~a ~ function demo:foo
---
<class 'mecha.ast.AstRoot'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=41, lineno=2, colno=36)
end_location: SourceLocation(pos=42, lineno=3, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_184__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ _bolt_refs[4]
_bolt_refs[5]
<class 'mecha.ast.AstRoot'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=41, lineno=2, colno=36)
end_location: SourceLocation(pos=42, lineno=3, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'mecha.ast.AstCommand'>
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_198__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function #tag_name
---
<class 'mecha.ast.AstRoot'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=37, lineno=2, colno=19)
end_location: SourceLocation(pos=38, lineno=3, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_198__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ _bolt_refs[1]
_bolt_refs[2]
<class 'mecha.ast.AstRoot'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=37, lineno=2, colno=19)
end_location: SourceLocation(pos=38, lineno=3, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'mecha.ast.AstCommand'>
59 changes: 59 additions & 0 deletions tests/snapshots/examples__build_bolt_summon__0.pack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Lectern snapshot

## Data pack

`@data_pack pack.mcmeta`

```json
{
"pack": {
"pack_format": 10,
"description": ""
}
}
```

### demo

`@function demo:foo`

```mcfunction
summon zombie ~ ~ ~ {Tags: ["my_custom_mob", "custom_summon_init"]}
execute as @e[type=minecraft:zombie, tag=custom_summon_init] at @s run function demo:foo/nested_execute_0
summon zombie 1 2 3 {Tags: ["custom_summon_init"]}
execute as @e[type=minecraft:zombie, tag=custom_summon_init] at @s run function demo:foo/nested_execute_1
summon zombie 1 2 3 {Tags: ["using_nested_yaml"]}
summon zombie ~ ~ ~ {Tags: ["custom_summon_init"]}
execute as @e[type=minecraft:zombie, tag=custom_summon_init] at @s run function demo:foo/nested_execute_2
summon zombie ~1 ~2 ~3 {Tags: ["custom_summon_init"]}
execute as @e[type=minecraft:zombie, tag=custom_summon_init] at @s run function demo:foo/nested_execute_3
```

`@function demo:foo/nested_execute_0`

```mcfunction
say Hello I just spawned!
effect give @e[distance=..3] poison
tag @s remove custom_summon_init
```

`@function demo:foo/nested_execute_1`

```mcfunction
say no nbt
tag @s remove custom_summon_init
```

`@function demo:foo/nested_execute_2`

```mcfunction
say no position or nbt
tag @s remove custom_summon_init
```

`@function demo:foo/nested_execute_3`

```mcfunction
say hello
tag @s remove custom_summon_init
```

0 comments on commit 6551578

Please sign in to comment.