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

Multiline support for evcxr_repl #98

Merged
merged 1 commit into from Jan 20, 2020
Merged

Conversation

thomcc
Copy link
Contributor

@thomcc thomcc commented Jan 13, 2020

Fixes #4

This is something I've wanted for a while, and have been poking at off and on.

It ended up being a lot more involved than I had hoped. This is because essentially nothing that parses or scans rust seems to handle unmatched brackets at all, since they all operate on token trees, which must be well formed. I even asked wg-grammar channel on discord, and basically got told that I was going to have to go my own way, or use something like tree-sitter (which would only solve half the problem (lexing), requires taking a C dependency, and then either writing a tree-sitter grammar for rust or vendoring one from one of the text editors...

I considered the naive solution of just scanning for parens/brackets and ignoring anything else, but in practice it's pretty annoying: it's not really okay for the repl to see a "(" and then never send your input since it thinks it's invalid -- so you need to at least lex the input well enough to notice strings/chars/comments, so that's what I did.

Unfortunately, even just this much is pretty involved, as rust's syntax for these is surprisingly tricky: Block comments can nest, chars are hard to distinguish from lifetimes, and it has raw strings.

That said, it works well in practice, has lots of comments, and pretty good test coverage, so hopefully it's not too controversial.

I'd also like to improve the repl more in the near future -- rustyline seems to have support for a few more things that would be useful, and it's possible we can use syn to handle many of the cases this misses (in particular detecting missing semicolons on stuff like let x = 3 probably requires syn in order to be robust), but for now this patch was already pretty huge, so I left it as is.

Note: I've only tested this manually on macos at the moment, but have access to other platforms and will try to get around to it soon.

Copy link
Collaborator

@davidlattimore davidlattimore left a comment

Choose a reason for hiding this comment

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

Nice work! Definitely happy to merge this. There are a few very minor comments.

@@ -0,0 +1,86 @@
// Copyright 2018 Google Inc.
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could change this to "Copyright 2020 The Evcxr Authors".

Also welcome to leave it as-is and I'll do a bulk update at some point.

/// quote.
Normal,
/// Raw string. Closed after we see a " followed by the right num of
/// backslashes,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you mean "hashes" here?

/// after seeing a closing \", do we need to think we're done), or None if
/// something looks dodgy.
///
/// information that we should hopefully know whether or not the string closed,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this last paragraph complete? Looks like it might have been chopped off.

/// Expects to be called after `iter` has *fully consumed* the initial `//`.
///
/// Consumes the entire comment, including the `\n` and returns true, or returns
/// false if we exhausted `iter` before finding a `\n`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like it doesn't return anything. I guess that's OK, since presumably the input will always end in a '\n'?

}
}

/// This should be right called after `input` reads a `'`. See `do_eat_char` for
Copy link
Collaborator

Choose a reason for hiding this comment

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

Swap "right" and "called"?

@@ -13,9 +13,10 @@ edition = "2018"

[dependencies]
evcxr = { version = "=0.4.7", path = "../evcxr" }
rustyline = "5.0.2"
rustyline = "6.0.0"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like rustyline 6 uses the non_exhaustive feature, which was only stabilised in 1.40.0. I think I'm OK with dropping support for older versions of rust so long as it gives us something. Can you update .travis.yml to bump the minimum supported version to 1.40.0?

@davidlattimore
Copy link
Collaborator

I'd like to merge this. Let me know if you'd like more time to address the comments I made before, otherwise I'll just merge and attempt to address them myself. Thanks again for this awesome change and for such well commented code :)

@thomcc
Copy link
Contributor Author

thomcc commented Jan 20, 2020

Sorry, meant to get back to this, not at my computer this weekend, so feel free to land it yourself!

@davidlattimore davidlattimore merged commit 2b204d6 into evcxr:master Jan 20, 2020
davidlattimore added a commit that referenced this pull request Jan 21, 2020
@astrolemonade
Copy link

How to use this multiline feature? The \ is not working

@thomcc
Copy link
Contributor Author

thomcc commented Feb 22, 2020

It's automatically multiline if there are unclosed items, e.g. open parentheses, brackets, strings, etc.

@thomcc
Copy link
Contributor Author

thomcc commented Feb 22, 2020

Note that some issues with it exist, and are blocked on kkawakam/rustyline#334. (As well as other bugs in that repo)

@thomcc thomcc deleted the multiline branch February 22, 2020 22:58
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.

Add multiline support in the REPL
3 participants