Skip to content

Commit

Permalink
🐛 Fix score holders starting with * (#1692)
Browse files Browse the repository at this point in the history
  • Loading branch information
misode authored Dec 27, 2024
1 parent 1b22dc1 commit da60293
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
26 changes: 14 additions & 12 deletions packages/core/src/source/Source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ export class ReadonlySource {
return this.string.slice(this.innerCursor + offset, this.innerCursor + offset + length)
}

canRead(length = 1) {
const needed = this.innerCursor + length
const available = this.string.length
return this.innerCursor + length <= this.string.length
}

canReadInLine() {
return this.canRead() && this.peek() !== CR && this.peek() !== LF
}

/**
* If the `expectedValue` is right after the cursor, returns `true`. Otherwise returns `false`.
*
Expand Down Expand Up @@ -109,12 +119,12 @@ export class ReadonlySource {
return this.peekUntil(CR, LF)
}

peekRemaining(): string {
return this.string.slice(this.innerCursor)
peekRemaining(offset = 0): string {
return this.string.slice(this.innerCursor + offset)
}

matchPattern(regex: RegExp): boolean {
return regex.test(this.peekRemaining())
matchPattern(regex: RegExp, offset = 0): boolean {
return regex.test(this.peekRemaining(offset))
}

/**
Expand Down Expand Up @@ -172,14 +182,6 @@ export class Source extends ReadonlySource {
return ans
}

canRead(length = 1) {
return this.innerCursor + length <= this.string.length
}

canReadInLine() {
return this.canRead() && this.peek() !== CR && this.peek() !== LF
}

read() {
return this.string.charAt(this.innerCursor++)
}
Expand Down
1 change: 1 addition & 0 deletions packages/java-edition/src/mcfunction/completer/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ const scoreHolder: Completer<ScoreHolderNode> = (node, ctx) => {
ctx,
)
ans.push(
...completer.literal(LiteralNode.mock(node, { pool: ['*'] }), ctx),
...selector(
EntitySelectorNode.mock(node, { pool: EntitySelectorAtVariable.filterAvailable(ctx) }),
ctx,
Expand Down
8 changes: 6 additions & 2 deletions packages/java-edition/src/mcfunction/parser/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1406,8 +1406,12 @@ export function scoreHolder(
): core.Parser<ScoreHolderNode> {
return core.map<core.LiteralNode | core.SymbolNode | EntitySelectorNode, ScoreHolderNode>(
core.select([
// Technically score holders can start with *, but this doesn't account for it
{ prefix: '*', parser: core.literal('*') },
{
predicate: (src) =>
src.peek() === '*'
&& (!src.canRead(2) || src.matchPattern(/^\s/, 1)),
parser: core.literal('*'),
},
{ prefix: '@', parser: selector() },
{ parser: scoreHolderFakeName(usageType) },
]),
Expand Down

0 comments on commit da60293

Please sign in to comment.