Errors when parsing escape characters #6

Closed
opened 2024-02-27 07:31:00 +01:00 by snnsnn · 2 comments
snnsnn commented 2024-02-27 07:31:00 +01:00 (Migrated from github.com)

Hi, I was trying to parse a string with escape characters, I got parsing errors for the value with escaped double quotes.

The text I was parsing:

{
  "description": "Some description",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "scripts": {
    "test": "echo \"no test specified\" && exit 1"
  },
  "dependencies": {
    "@lezer/common": "^1.2.1"
  }
}

The code I used to check for a parsing error:

if (node.type.isError) {
  console.log(code.slice(from, to));
}

I go three logs on the console output (I removed the line numbers for clarity):

no
test
specified" && exit 1"

I am not sure if I overlook something or I should escape the escape character itself first, like so:

"echo \\"no test specified\\" && exit 1"

Thank you for your time.

Hi, I was trying to parse a string with escape characters, I got parsing errors for the value with escaped double quotes. The text I was parsing: ```json { "description": "Some description", "keywords": [], "author": "", "license": "ISC", "scripts": { "test": "echo \"no test specified\" && exit 1" }, "dependencies": { "@lezer/common": "^1.2.1" } } ``` The code I used to check for a parsing error: ```ts if (node.type.isError) { console.log(code.slice(from, to)); } ``` I go three logs on the console output (I removed the line numbers for clarity): ``` no test specified" && exit 1" ``` I am not sure if I overlook something or I should escape the escape character itself first, like so: ```"echo \\"no test specified\\" && exit 1"``` Thank you for your time.
marijnh commented 2024-02-27 10:54:25 +01:00 (Migrated from github.com)

This code parses without any errors for me in @lezer/json 1.0.2:

JsonText(Object("{",
  Property(PropertyName,String),
  Property(PropertyName,Array("[","]")),
  Property(PropertyName,String),
  Property(PropertyName,String),
  Property(PropertyName,Object("{",Property(PropertyName,String),"}")),
  Property(PropertyName,Object("{",Property(PropertyName,String),"}")),
"}"))
This code parses without any errors for me in @lezer/json 1.0.2: ``` JsonText(Object("{", Property(PropertyName,String), Property(PropertyName,Array("[","]")), Property(PropertyName,String), Property(PropertyName,String), Property(PropertyName,Object("{",Property(PropertyName,String),"}")), Property(PropertyName,Object("{",Property(PropertyName,String),"}")), "}")) ```
marijnh commented 2024-02-27 12:36:04 +01:00 (Migrated from github.com)

You should double-escape those backslashes if you're using them in a JavaScript string literal. I.e. the problem is that your code string in that test code doesn't contain any actual backslashes.

You should double-escape those backslashes if you're using them in a JavaScript string literal. I.e. the problem is that your `code` string in that test code doesn't contain any actual backslashes.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
lezer/json#6
No description provided.