kach / nearley

📜🔜🌲 Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.57k stars 232 forks source link

newline characters are parsed the same as newlines #523

Closed andykais closed 4 years ago

andykais commented 4 years ago

If I create a parser like so:

InQuotes[X]               -> "'" $X "'"
StringTemplate            -> InQuotes[[^'\n\r]:*]

then both of these inputs are invalid with the error Unexpected "\n".:

'\n'
'
'

I would prefer that the latter is invalid but the former is valid (much how writing the former would be valid in say javascript, but the latter would not be. What I actually have to end up doing is writing

'\\n'

when I want to express a newline within quotes. Is there a way to make nearley parse strings closer to how most languages do?

kach commented 4 years ago

Hmm, it seems to work for me?

$ nearley-test <(pbpaste | nearleyc) -i "'\n'" -q
[
  [
    [ "'", [ [ '\\', 'n' ] ], "'" ]
  ]
]

If you're typing directly into nearley-test (as opposed to using -i) I wonder if hitting "enter" is creating a second newline at the end of your input, which is what is causing the problem.

andykais commented 4 years ago

hmm Ill see if I can create a repro tonight, thanks for responding!

andykais commented 4 years ago

ah. Looks like my problem is that javascript string literals (`) interpret newline characters as newlines directly. So the following strings are equivilant:

a = `\n`
b = `
`

a === b
// true