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

🐛 Improve command literal parsing #1687

Merged
merged 2 commits into from
Dec 27, 2024
Merged
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/java-edition/src/mcfunction/mcdocAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function registerMcdocAttributes(meta: core.MetaRegistry, rootTreeNode: m
}),
})
mcdoc.runtime.registerAttribute(meta, 'item_slots', () => undefined, {
stringParser: () => parser.itemSlots,
stringParser: (_, __, ctx) => core.literal({ pool: getItemSlotsArgumentValues(ctx) }),
stringMocker: (_, __, ctx) =>
core.LiteralNode.mock(ctx.offset, { pool: getItemSlotsArgumentValues(ctx) }),
})
Expand Down
45 changes: 26 additions & 19 deletions packages/java-edition/src/mcfunction/parser/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const argument: mcf.ArgumentParserGetter = (
case 'minecraft:color':
return wrap(
core.map(
core.literal(...ColorArgumentValues),
commandLiteral({ pool: ColorArgumentValues }),
(res) => ({
...res,
color: core.Color.NamedColors.has(res.value)
Expand All @@ -167,7 +167,7 @@ export const argument: mcf.ArgumentParserGetter = (
case 'minecraft:entity':
return wrap(entity(treeNode.properties.amount, treeNode.properties.type))
case 'minecraft:entity_anchor':
return wrap(core.literal(...EntityAnchorArgumentValues))
return wrap(commandLiteral({ pool: EntityAnchorArgumentValues }))
case 'minecraft:entity_summon':
return wrap(core.resourceLocation({ category: 'entity_type' }))
case 'minecraft:float_range':
Expand All @@ -183,11 +183,11 @@ export const argument: mcf.ArgumentParserGetter = (
case 'minecraft:function':
return wrap(core.resourceLocation({ category: 'function', allowTag: true }))
case 'minecraft:gamemode':
return wrap(core.literal(...GamemodeArgumentValues))
return wrap(commandLiteral({ pool: GamemodeArgumentValues }))
case 'minecraft:game_profile':
return wrap(entity('multiple', 'players'))
case 'minecraft:heightmap':
return wrap(core.literal(...HeightmapValues))
return wrap(commandLiteral({ pool: HeightmapValues }))
case 'minecraft:int_range':
return wrap(
range(
Expand All @@ -203,9 +203,13 @@ export const argument: mcf.ArgumentParserGetter = (
case 'minecraft:item_predicate':
return wrap(itemPredicate)
case 'minecraft:item_slot':
return wrap(itemSlot)
return wrap((src, ctx) => {
return commandLiteral({ pool: getItemSlotArgumentValues(ctx) })(src, ctx)
})
case 'minecraft:item_slots':
return wrap(itemSlots)
return wrap((src, ctx) => {
return commandLiteral({ pool: getItemSlotsArgumentValues(ctx) })(src, ctx)
})
case 'minecraft:item_stack':
return wrap(itemStack)
case 'minecraft:loot_modifier':
Expand Down Expand Up @@ -235,7 +239,7 @@ export const argument: mcf.ArgumentParserGetter = (
case 'minecraft:objective_criteria':
return wrap(objectiveCriteria)
case 'minecraft:operation':
return wrap(core.literal({ pool: OperationArgumentValues, colorTokenType: 'operator' }))
return wrap(commandLiteral({ pool: OperationArgumentValues, colorTokenType: 'operator' }))
case 'minecraft:particle':
return wrap(particle)
case 'minecraft:resource':
Expand All @@ -262,12 +266,12 @@ export const argument: mcf.ArgumentParserGetter = (
// `BELOWNAME` and `sidebar.team.r--.+++e----__d` are also legal slots.
// But I do not want to spend time supporting them.
return wrap((src, ctx) => {
return core.literal(...getScoreboardSlotArgumentValues(ctx))(src, ctx)
return commandLiteral({ pool: getScoreboardSlotArgumentValues(ctx) })(src, ctx)
})
case 'minecraft:style':
return wrap(jsonParser('::java::server::util::text::TextStyle'))
case 'minecraft:swizzle':
return wrap(core.literal(...SwizzleArgumentValues))
return wrap(commandLiteral({ pool: SwizzleArgumentValues }))
case 'minecraft:team':
return wrap(
team(
Expand All @@ -277,9 +281,9 @@ export const argument: mcf.ArgumentParserGetter = (
),
)
case 'minecraft:template_mirror':
return wrap(core.literal(...MirrorValues))
return wrap(commandLiteral({ pool: MirrorValues }))
case 'minecraft:template_rotation':
return wrap(core.literal(...RotationValues))
return wrap(commandLiteral({ pool: RotationValues }))
case 'minecraft:time':
return wrap(time)
case 'minecraft:uuid':
Expand Down Expand Up @@ -463,14 +467,6 @@ const greedyString: core.InfallibleParser<core.StringNode> = core.string({
unquotable: { blockList: new Set(['\n', '\r']) },
})

const itemSlot: core.InfallibleParser<core.LiteralNode> = (src, ctx) => {
return core.literal(...getItemSlotArgumentValues(ctx))(src, ctx)
}

export const itemSlots: core.InfallibleParser<core.LiteralNode> = (src, ctx) => {
return core.literal(...getItemSlotsArgumentValues(ctx))(src, ctx)
}

const itemStack: core.InfallibleParser<ItemStackNode> = (src, ctx) => {
const oldFormat = shouldUseOldItemStackFormat(ctx)
return core.map<
Expand Down Expand Up @@ -540,6 +536,17 @@ export function jsonParser(typeRef: `::${string}::${string}`): core.Parser<json.
} satisfies json.TypedJsonNode))
}

function commandLiteral(options: core.LiteralOptions): core.Parser<core.LiteralNode> {
return (src, ctx) => {
const ans = core.literal(options)(src, ctx)
if (ans.value.length === 0) {
ans.value = src.readUntil(...core.Whitespaces)
ans.range = core.Range.create(ans.range.start, src)
}
return ans
}
}

const message: core.InfallibleParser<MessageNode> = (src, ctx) => {
const ans: MessageNode = {
type: 'mcfunction:message',
Expand Down
Loading