Skip to content

Conversation

Copy link

Copilot AI commented Oct 21, 2025

Fixes parsing errors when NBT data contains strings surrounded by single quotes instead of double quotes.

Problem

The parser was failing to parse valid Minecraft NBT strings that use single quotes (') instead of double quotes ("). According to the Minecraft NBT format specification, single quotes are valid string delimiters.

This was causing errors like:

Error: Error parsing text '{"minecraft:custom_name":'":!!: book :!!:"'}'
    at Parser.feed

Examples that were failing:

  • {key:'value'}
  • {"minecraft:custom_name":'":!!: book :!!:"'}
  • Items with custom names that contain special characters

Solution

Extended the grammar to support single-quoted strings while maintaining backward compatibility:

  1. Added SINGLE_QUOTED_STRING grammar rule - Handles single-quoted strings with proper escape sequence support (including \', \", \n, \r, \t, \b, \f, \/, \\, and unicode escapes)

  2. Implemented disambiguation logic - When single-quoted content can be parsed as either a plain string or a compound object (e.g., '{"text":"..."}'), the parser now prefers the structured interpretation (compound), matching Minecraft's behavior

  3. Added comprehensive tests - 7 new test cases covering various single-quote scenarios

Examples

Now works correctly:

const mojangson = require('mojangson')

// Simple single-quoted string
mojangson.parse("{key:'value'}")
// Returns: { type: 'compound', value: { key: { value: 'value', type: 'string' } } }

// Single-quoted string with special characters  
mojangson.parse('{"minecraft:custom_name":\'":!!: book :!!:"\'}')
// Returns: { type: 'compound', value: { 'minecraft:custom_name': { value: '":!!: book :!!:"', type: 'string' } } }

// Single-quoted JSON (parsed as compound, not string)
mojangson.parse('\'{"text":"hello"}\'')
// Returns: { type: 'compound', value: { text: { value: 'hello', type: 'string' } } }

Testing

  • ✅ All 117 existing tests continue to pass
  • ✅ 7 new tests for single-quote support
  • ✅ Total: 124 tests passing
Original prompt

This section details on the original issue you should resolve

<issue_title>Single quote strings</issue_title>
<issue_description>Hello!

Thanks for this library! It has been very helpful.

I've ran into a problem involving strings that are surrounded by single quotes.
Examples
{key:'value'}
{"minecraft:custom_name":'":!!: book :!!:"'}

Error: Error parsing text '{"minecraft:custom_name":'":!!: book :!!:"'}'
    at Parser.feed (C:\Users\mike-\Documents\Projects\CMC_Stuff\AuctionMonitor\test_server\node_modules\nearley\lib\nearley.js:343:27)
    at Module.parse (C:\Users\mike-\Documents\Projects\CMC_Stuff\AuctionMonitor\test_server\node_modules\mojangson\index.js:94:14)
    at file:///C:/Users/mike-/Documents/Projects/CMC_Stuff/AuctionMonitor/test_server/snbt-parse-example.js:10:45
    at ModuleJob.run (node:internal/modules/esm/module_job:377:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:691:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5) {
  offset: 26,
  token: { value: '"' }

This has been happening for items with custom names.

Apparently it's valid for strings to sometimes be surrounded by single quotes instead of double quotes
https://minecraft.wiki/w/NBT_format

</issue_description>

<agent_instructions>update grammar and add test for single quote strings</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #52


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix parsing issue for single quote strings Add support for single-quoted strings in mojangson parser Oct 21, 2025
Copilot AI requested a review from extremeheat October 21, 2025 17:59
@BadgerCode
Copy link

Tested this myself and it does fix my original problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Single quote strings

3 participants