Skip to content
Open
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
19 changes: 19 additions & 0 deletions packages/core/src/node/StringNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ export const EscapeTable = new Map<EscapeChar, string>([
['t', '\t'],
])

export const UnicodeEscapeChars = ['x', 'u', 'U'] as const
export type UnicodeEscapeChar = (typeof UnicodeEscapeChars)[number]
export namespace UnicodeEscapeChar {
/* istanbul ignore next */
export function is(c: string): c is UnicodeEscapeChar {
return UnicodeEscapeChars.includes(c as any)
}
}

export const UnicodeEscapeLengths = new Map<UnicodeEscapeChar, number>([
['x', 2],
['u', 4],
['U', 8],
])

export type Quote = "'" | '"'

export interface StringOptions {
Expand All @@ -40,6 +55,10 @@ export interface StringOptions {
* Whether escapes like `\u####` where #### is a hexdecimal numeral are allowed.
*/
unicode?: boolean
/**
* Whether additional escapes like `\N{<name>}`, `\x##`, or `\U########` are allowed (currently only used in SNBT).
*/
extendedUnicode?: boolean
/**
* Whether unknown characters can be escaped, which would just result in the character itself.
*/
Expand Down
64 changes: 58 additions & 6 deletions packages/core/src/parser/string.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { localeQuote, localize } from '@spyglassmc/locales'
import { TextDocument } from 'vscode-languageserver-textdocument'
import type { Quote, StringNode, StringOptions } from '../node/index.js'
import { EscapeChar, EscapeTable } from '../node/index.js'
import { EscapeChar, EscapeTable, UnicodeEscapeChar, UnicodeEscapeLengths } from '../node/index.js'
import type { InfallibleParser } from '../parser/index.js'
import type { ParserContext } from '../service/index.js'
import type { IndexMap } from '../source/index.js'
import { Range, Source } from '../source/index.js'
import type { Parser, Result, Returnable } from './Parser.js'
import { Failure } from './Parser.js'
import unicodeLookupJson from './unicode-lookup-table.json' with { type: 'json' }

const UnicodeLookupTable = new Map<string, number>(Object.entries(unicodeLookupJson))

export function string(options: StringOptions): InfallibleParser<StringNode> {
return (src: Source, ctx: ParserContext): StringNode => {
Expand Down Expand Up @@ -40,10 +43,14 @@ export function string(options: StringOptions): InfallibleParser<StringNode> {
outer: Range.create(cStart, src),
})
ans.value += EscapeTable.get(c2)
} else if (options.escapable.unicode && c2 === 'u') {
const hex = src.peek(4)
if (/^[0-9a-f]{4}$/i.test(hex)) {
src.skip(4)
} else if (
options.escapable.unicode
&& (c2 === 'u' || (options.escapable.extendedUnicode && UnicodeEscapeChar.is(c2)))
) {
const sequenceLength = UnicodeEscapeLengths.get(c2) || 4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more common to use ?? 4 for default values.

const hex = src.peek(sequenceLength)
if (new RegExp(`^[0-9a-f]{${sequenceLength}}$`, 'i').test(hex)) {
src.skip(sequenceLength)
ans.valueMap.push({
inner: Range.create(ans.value.length, ans.value.length + 1),
outer: Range.create(cStart, src),
Expand All @@ -52,7 +59,52 @@ export function string(options: StringOptions): InfallibleParser<StringNode> {
} else {
ctx.err.report(
localize('parser.string.illegal-unicode-escape'),
Range.create(src, src.getCharRange(3).end),
Range.create(src, src.getCharRange(sequenceLength - 1).end),
)
ans.valueMap.push({
inner: Range.create(ans.value.length, ans.value.length + 1),
outer: Range.create(cStart, src),
})
ans.value += c2
}
} else if (options.escapable.extendedUnicode && c2 === 'N') {
if (!src.trySkip('{')) {
ctx.err.report(
localize('expected', localeQuote('{')),
src.getCharRange(-1),
)
ans.valueMap.push({
inner: Range.create(ans.value.length, ans.value.length + 1),
outer: Range.create(cStart, src),
})
ans.value += c2
cStart = src.cursor
continue
}
const name = src.peekUntil('}')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minecraft actually allows whitespace between the curly braces and the name. So you might need to trim the name or add some src.skipSpace calls.

if (src.peek(1, name.length) !== '}') {
ctx.err.report(
localize('expected', localeQuote('}')),
Range.create(src, src.getCharRange(name.length - 1).end),
)
ans.valueMap.push({
inner: Range.create(ans.value.length, ans.value.length + 1),
outer: Range.create(cStart, src),
})
ans.value += c2
} else if (
/^[-a-zA-Z0-9 ]+$/.test(name) && UnicodeLookupTable.has(name.toLowerCase())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing a has() and then get()! separately, I would use a single get() and check if the result is not undefined.

) {
src.skip(name.length + 1)
ans.valueMap.push({
inner: Range.create(ans.value.length, ans.value.length + 1),
outer: Range.create(cStart, src),
})
ans.value += String.fromCodePoint(UnicodeLookupTable.get(name.toLowerCase())!)
} else {
ctx.err.report(
localize('parser.string.illegal-unicode-escape-name'),
Range.create(src, src.getCharRange(name.length - 1).end),
)
ans.valueMap.push({
inner: Range.create(ans.value.length, ans.value.length + 1),
Expand Down
Loading