Skip to content

Commit

Permalink
return component test nodes to being infallible
Browse files Browse the repository at this point in the history
  • Loading branch information
Trivaxy committed Jun 15, 2024
1 parent f0077d7 commit 5cfdfa1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
8 changes: 4 additions & 4 deletions packages/java-edition/src/mcfunction/node/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,9 @@ export interface ComponentTestBaseNode extends core.AstNode {

export interface ComponentTestExactNode extends ComponentTestBaseNode {
type: 'mcfunction:component_test_exact'
children: [core.ResourceLocationNode, nbt.NbtNode]
children: (core.ResourceLocationNode | nbt.NbtNode)[]
component: core.ResourceLocationNode
value: nbt.NbtNode
value?: nbt.NbtNode
}

export interface ComponentTestExistsNode extends ComponentTestBaseNode {
Expand All @@ -421,9 +421,9 @@ export interface ComponentTestExistsNode extends ComponentTestBaseNode {

export interface ComponentTestSubpredicateNode extends ComponentTestBaseNode {
type: 'mcfunction:component_test_sub_predicate'
children: [core.ResourceLocationNode, nbt.NbtNode]
children: (core.ResourceLocationNode | nbt.NbtNode)[]
subPredicateType: core.ResourceLocationNode
subPredicate: nbt.NbtNode
subPredicate?: nbt.NbtNode
}

export type ComponentTestNode =
Expand Down
18 changes: 7 additions & 11 deletions packages/java-edition/src/mcfunction/parser/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,7 @@ const components: core.InfallibleParser<ComponentListNode> = core.map(
},
)

const componentTest: core.Parser<ComponentTestNode> = (src, ctx) => {
const componentTest: core.InfallibleParser<ComponentTestNode> = (src, ctx) => {
const start = src.cursor
src.skipWhitespace()
const negated = src.trySkip('!')
Expand All @@ -1858,19 +1858,19 @@ const componentTest: core.Parser<ComponentTestNode> = (src, ctx) => {
src.skipWhitespace()

if (src.trySkip('=')) {
const value = nbt.parser.entry(src, ctx)
let value: core.Result<nbt.NbtNode> | undefined = nbt.parser.entry(src, ctx);

if (value == core.Failure) {
ctx.err.report(localize('expected', localize('nbt.node')), src)
src.skipUntilOrEnd(',', '|', ']')
return core.Failure
value = undefined
}

src.skipWhitespace()
const ans: ComponentTestExactNode = {
type: 'mcfunction:component_test_exact',
range: core.Range.create(start, src),
children: [resLoc, value],
children: [resLoc, ...(value ? [value] : [])],
component: resLoc,
value: value,
negated,
Expand All @@ -1879,19 +1879,19 @@ const componentTest: core.Parser<ComponentTestNode> = (src, ctx) => {
}
if (src.trySkip('~')) {
resLoc.options.category = 'item_sub_predicate_type'
const predicate = nbt.parser.entry(src, ctx)
let predicate: core.Result<nbt.NbtNode> | undefined = nbt.parser.entry(src, ctx)

if (predicate == core.Failure) {
ctx.err.report(localize('expected', localize('nbt.node')), src)
src.skipUntilOrEnd(',', '|', ']')
return core.Failure
predicate = undefined
}

src.skipWhitespace()
const ans: ComponentTestSubpredicateNode = {
type: 'mcfunction:component_test_sub_predicate',
range: core.Range.create(start, src),
children: [resLoc, predicate],
children: [resLoc, ...(predicate ? [predicate] : [])],
subPredicateType: resLoc,
subPredicate: predicate,
negated,
Expand Down Expand Up @@ -1923,10 +1923,6 @@ const componentTestsAllOf: core.InfallibleParser<ComponentTestsAllOfNode> = (
src.skipWhitespace()
const testNode = componentTest(src, ctx)

if (testNode == core.Failure) {
continue
}

children.push(testNode)
src.skipWhitespace()

Expand Down

0 comments on commit 5cfdfa1

Please sign in to comment.