2025-11-11 21:37:32 -06:00
|
|
|
import {Command, ParseContext, StrTerm} from "./command.js";
|
|
|
|
|
import {LexInput} from "../lexer.js";
|
2026-02-09 18:09:47 -06:00
|
|
|
import {StrVM} from "../vm.js";
|
|
|
|
|
import {Awaitable} from "../../util/types.js";
|
|
|
|
|
|
|
|
|
|
export const QUOTEMARKS = ['"', '\'', '`']
|
|
|
|
|
|
|
|
|
|
export const stripQuotemarkLayer = (s: string, marks?: string[]): string => {
|
|
|
|
|
if ( !marks ) {
|
|
|
|
|
marks = QUOTEMARKS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ( const mark of marks ) {
|
|
|
|
|
if ( !s.startsWith(mark) || !s.endsWith(mark) ) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s = s.substring(mark.length, s.length - mark.length)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
}
|
2025-11-11 21:37:32 -06:00
|
|
|
|
|
|
|
|
export class Quote extends Command<{ with?: StrTerm }> {
|
|
|
|
|
attemptParse(context: ParseContext): { with?: StrTerm } {
|
|
|
|
|
return {
|
|
|
|
|
with: context.popOptionalTerm(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getDisplayName(): string {
|
|
|
|
|
return 'quote'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isParseCandidate(token: LexInput): boolean {
|
|
|
|
|
return this.isKeyword(token, 'quote')
|
|
|
|
|
}
|
2026-02-09 18:09:47 -06:00
|
|
|
|
|
|
|
|
execute(vm: StrVM, data: { with?: StrTerm }): Awaitable<StrVM> {
|
2026-02-09 23:49:30 -06:00
|
|
|
return vm.replaceContextMatchingTerm(ctx => ({
|
|
|
|
|
string: sub => {
|
2026-02-09 22:15:33 -06:00
|
|
|
let quote = '\''
|
|
|
|
|
if ( data.with ) {
|
|
|
|
|
quote = ctx.resolveString(data.with)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub = stripQuotemarkLayer(sub)
|
2026-03-02 23:31:38 -06:00
|
|
|
sub = sub.replaceAll(quote, `\\${quote}`)
|
2026-02-09 22:15:33 -06:00
|
|
|
return `${quote}${sub}${quote}`
|
2026-02-09 23:49:30 -06:00
|
|
|
}
|
|
|
|
|
}))
|
2026-02-09 18:09:47 -06:00
|
|
|
}
|
2025-11-11 21:37:32 -06:00
|
|
|
}
|