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

repl: support mult-line string-keyed objects #21775

Closed
wants to merge 0 commits into from

Conversation

rubys
Copy link
Member

@rubys rubys commented Jul 12, 2018

fixes #21657

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the repl Issues and PRs related to the REPL subsystem. label Jul 12, 2018
lib/repl.js Outdated
@@ -1521,12 +1521,161 @@ function isRecoverableError(e, code) {
return pos > 0 && frames[pos - 1].length === frames[pos].length;
}

if (message.startsWith('Unexpected token'))
Copy link
Contributor

@antsmartian antsmartian Jul 12, 2018

Choose a reason for hiding this comment

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

In version v8.11.3, without this particular check, things works. I'm wondering, now with this code in place, we are sort of re-doing the check (may be?). May be I'm wrong here.

Actually as discussed in the issue, this change broken the partial JSON stuff. The diff is over here:
ebfa8b1#diff-293fefe850253c8c28e2481c0b7f76e2L647

As seen in the comments of PR, looks like partial template literal also broken in latest version. For example:

`io.js ${a
... }`

works in v8/9. But broken in v10x.

@Trott
Copy link
Member

Trott commented Jul 12, 2018

@nodejs/repl

@BridgeAR
Copy link
Member

@rubys In general, the idea is great. Because this is going in a direction where the code is starting to be properly parsed. I am not sure though if this is the right approach in the current state of the repl. The reason is that the repl itself has so many hacks that it's a nightmare.

It would be really awesome if you would be able to use acorn instead. I am not sure if that would help in this specific moment but I'll have a look into that tomorrow and maybe you could also check that?

@rubys
Copy link
Member Author

rubys commented Jul 13, 2018

@BridegeAR: you are being too kind. It clearly is a hack. Ideally, V8 would be implemented as a streaming parser, but that isn't required for the browser. As Acorn isn't a streaming parser, it isn't likely to be able to help us here.

The root problem is that the JavaScript grammar is ambiguous (at least at the tokenization level), and backtracking is sometimes needed.

Examples of valid JavaScript:

a=1
{a*2}
{a, a}
{a, a*2}
label: {a, a}

Only one of the lines above is an object literal.

@devsnek
Copy link
Member

devsnek commented Jul 13, 2018

@rubys btw we're working on rewriting the REPL from scratch: https://github.com/nodejs/repl

it might be a good place to experiment with different ways of consuming input, as its still fairly small.

@rubys
Copy link
Member Author

rubys commented Jul 13, 2018

I may have been too quick to discount acorn; it will probably need a patch to be landed to support this use case, but it looks doable. I'm traveling for the next four days, so it may not be until next week that I can take a look at this.

lib/repl.js Outdated
// Check whether a code snippet is a partially complete JSON file.
// Consume the code provided using http://json.org/ until there is
// either an error or nothing left.
function isPartialJSON(code) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we split this out into a separate file in lib/internal/repl/?

@rubys
Copy link
Member Author

rubys commented Jul 13, 2018

Update: played with acorn on the first flight leg on this trip. Everything needed can be done with a plugin. Will create a separate pull request either later today or this weekend.

@rubys
Copy link
Member Author

rubys commented Jul 13, 2018

See #21805 for an acorn based implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Node REPL says "SyntaxError: unexpected symbol :" when parsing multi-line string-keyed objects (e.g. JSON)
7 participants