34 lines
1003 B
TypeScript
34 lines
1003 B
TypeScript
|
|
import {Command, ParseContext, unwrapString, wrapDestructured} from "./command.js";
|
||
|
|
import {LexInput} from "../lexer.js";
|
||
|
|
import {StrVM} from "../vm.js";
|
||
|
|
import {Awaitable} from "../../util/types.js";
|
||
|
|
|
||
|
|
export class Words extends Command<{}> {
|
||
|
|
attemptParse(context: ParseContext): {} {
|
||
|
|
return {}
|
||
|
|
}
|
||
|
|
|
||
|
|
getDisplayName(): string {
|
||
|
|
return 'words'
|
||
|
|
}
|
||
|
|
|
||
|
|
isParseCandidate(token: LexInput): boolean {
|
||
|
|
return this.isKeyword(token, 'words')
|
||
|
|
}
|
||
|
|
|
||
|
|
execute(vm: StrVM): Awaitable<StrVM> {
|
||
|
|
return vm.tapInPlace(ctx =>
|
||
|
|
ctx.replaceSubject(sub => {
|
||
|
|
const val = unwrapString(sub)
|
||
|
|
const separators = [...val.matchAll(/\s+/sg)]
|
||
|
|
const parts = val.split(/\s+/sg)
|
||
|
|
|
||
|
|
return wrapDestructured(
|
||
|
|
parts.map((part, idx) => ({
|
||
|
|
prefix: idx ? separators[idx - 1][0] : undefined,
|
||
|
|
value: part,
|
||
|
|
})))
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
}
|