Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser re-escapes \ in text for raw strings. #41

Open
dmfxyz opened this issue May 22, 2022 · 0 comments
Open

Parser re-escapes \ in text for raw strings. #41

dmfxyz opened this issue May 22, 2022 · 0 comments

Comments

@dmfxyz
Copy link

dmfxyz commented May 22, 2022

Take this simple grammer:

grammar = `
str          ::= '"' (unsafe | SAFE)* '"'
SAFE         ::= #x21 | [#x24-#x5A] | [#x5E-#x7A] | #x7C | #x7E
unsafe       ::= ESCAPE #x22
ESCAPE       ::= #x5C
`

If we define a raw string as follows:

str = String.raw`"stringwith\"escapes"`
console.log(str)

We get the representation:

"stringwith\"escapes"

Now if we define rules and a parser for this grammar and run it on that raw string:

rules = ebnf.Grammars.W3C.getRules(grammar)
parser = new ebnf.Parser(rules)
ast = parser.getAST(str)
console.log(ast)

We see the ast:

<ref *1> {
  type: 'str',
  text: '"stringwith\\"escapes"',
  children: [
    {
      type: 'unsafe',
      text: '\\"',
      children: [],
      end: 13,
      errors: [],
      fullText: '',
      parent: [Circular *1],
      start: 11,
      rest: ''
    }
  ],
  end: 21,
  errors: [],
  fullText: '',
  parent: null,
  start: 0,
  rest: ''
}

The text property of both the parent str and child unsafe have had the \ re-escaped. I don't think this re-escapement should happen for raw strings.

You can see a basic example here: https://github.com/dmfxyz/node-ebnf-issue-example
And the repo in which we originally discovered this behavior: nmushegian/jams#23

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

No branches or pull requests

1 participant