Skip to content

Commit

Permalink
✨ Add simple macro parser and colorizer (#1117)
Browse files Browse the repository at this point in the history
  • Loading branch information
misode authored May 6, 2024
1 parent a23e96f commit cd8d097
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
9 changes: 9 additions & 0 deletions packages/mcfunction/src/colorizer/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import * as core from '@spyglassmc/core'
import type {
CommandMacroNode,
LiteralCommandChildNode,
TrailingCommandChildNode,
} from '../node/index.js'

export function register(meta: core.MetaRegistry) {
meta.registerColorizer<CommandMacroNode>(
'mcfunction:command_macro',
macro,
)
meta.registerColorizer<LiteralCommandChildNode>(
'mcfunction:command_child/literal',
core.colorizer.literal,
Expand All @@ -14,3 +19,7 @@ export function register(meta: core.MetaRegistry) {
core.colorizer.error,
)
}

export const macro: core.Colorizer<CommandMacroNode> = (node, ctx) => {
return [core.ColorToken.create(node, 'string')]
}
3 changes: 2 additions & 1 deletion packages/mcfunction/src/completer/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DeepReadonly } from '@spyglassmc/core'
import * as core from '@spyglassmc/core'
import type { McfunctionNode } from '../node/index.js'
import { CommandMacroNode } from '../node/index.js'
import { CommandNode } from '../node/index.js'
import type { ArgumentTreeNode, RootTreeNode } from '../tree/index.js'
import {
Expand All @@ -26,7 +27,7 @@ export function entry(
return (node, ctx) => {
const tree = CommandTreeRegistry.instance.get(commandTreeName)
const childNode = core.AstNode.findChild(node, ctx.offset, true)
if (core.CommentNode.is(childNode)) {
if (core.CommentNode.is(childNode) || CommandMacroNode.is(childNode)) {
return []
} else {
return command(tree, getMockNodes)(
Expand Down
13 changes: 13 additions & 0 deletions packages/mcfunction/src/node/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ export namespace CommandNode {
}
}

export interface CommandMacroNode extends core.AstNode {
type: 'mcfunction:command_macro'
}

export const CommandMacroNode = Object.freeze({
is<T extends core.DeepReadonly<core.AstNode> | undefined>(
obj: T,
): obj is core.NodeIsHelper<CommandMacroNode, T> {
return (obj as CommandMacroNode | undefined)?.type ===
'mcfunction:command_macro'
},
})

export interface CommandChildNode extends core.AstNode {
type: 'mcfunction:command_child'
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/mcfunction/src/node/entry.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type * as core from '@spyglassmc/core'
import type { CommandNode } from './command.js'
import type { CommandMacroNode, CommandNode } from './command.js'

export interface McfunctionNode
extends core.SequenceNode<CommandNode | core.CommentNode>
extends core.SequenceNode<CommandNode | CommandMacroNode | core.CommentNode>
{
type: 'mcfunction:entry'
}
Expand Down
15 changes: 13 additions & 2 deletions packages/mcfunction/src/parser/entry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as core from '@spyglassmc/core'
import type { CommandNode, McfunctionNode } from '../node/index.js'
import type {
CommandMacroNode,
CommandNode,
McfunctionNode,
} from '../node/index.js'
import { CommandTreeRegistry } from '../tree/index.js'
import type { ArgumentParserGetter } from './argument.js'
import { command } from './command.js'
Expand All @@ -19,9 +23,16 @@ export function entry(
}

while (src.skipWhitespace().canReadInLine()) {
let result: core.CommentNode | CommandNode
let result: core.CommentNode | CommandNode | CommandMacroNode
if (src.peek() === '#') {
result = comment(src, ctx) as core.CommentNode
} else if (src.peek() === '$') {
const start = src.cursor
src.skipLine()
result = {
type: 'mcfunction:command_macro',
range: core.Range.create(start, src),
}
} else {
result = command(
CommandTreeRegistry.instance.get(commandTreeName),
Expand Down

0 comments on commit cd8d097

Please sign in to comment.