jruby / jruby-parser

JRuby's parser customized for IDE usage

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Need a way to pass in an existing local scope

chrisseaton opened this issue · comments

We need a way to parse Ruby code in an existing local scope, so with existing local variables.

For example, parsing the expression a will generate a method call by default. I'd like to be able to tell jruby-parser that a is an existing local variable, and get it to generate a local variable read.

This has at least two applications - parsing code to be eval'd in a Binding, and parsing lines of a REPL.

A workaround is to add dummy assignments to the local variables in your scope, and then remove them later.

This might be a place where pulling in functionality from rsense will help. In order to do its type inference work, rsense tracks scope, context and , of course, type, in a sort of shadow-grid alongside the AST it gets from jruby-parser. If we bring this into jruby-parser, and make the AST aware of the additional information, it seems like it ought to be easy enough to create an API for passing in a scope... I have only thought about doing this in a pretty abstract way so far, but I was reading this paper , which was an inspiration for rsense, which seems to suggest some possibilities.

jruby-parser already has everything it needs to track the local variables, as it does it fine for variables it can see being assigned, so we shouldn't need any new functionality or data structures. All we need is an API to pass in an initial set of local variables.

Everything we need is here: https://github.com/jruby/jruby-parser/blob/master/src/org/jrubyparser/StaticScope.java. we just need to be able to pass one of these in at the start of parsing.

For satisying this bug it is true we only need to add staticscope, but Eric is also looking at adding similar functionality to what rsense has to jruby-parser, so he has an eye towards a bigger fish. I suspect adding staticscope will be compatible with the work he is doing so we can add all this stuff piecemeal.

yeah sorry, that probably seemed really random...

Fixed in commit 60730a2. @chrisseaton you can look at spec/helpers/parser_helpers.rb to see how I set up local scope. You will need to work back to Java syntax but I don't think that is so difficult. Secondly, I test this from spec/jruby-parser/parse_spec.rb.

@edubkendo I believe rsense must use dynamicscope and not staticscope since that is what mainline jruby uses. This may complicate things a little for rsense if they are using dynamicscope to hold live values (dynamic scope is the value and static scope is the name). If so then we will need to talk about how to address that. I personally think static scope fits better since this is not a live runtime.

@enebo well, it actually tracks both, in a hashmap: https://github.com/edubkendo/rsense/blob/master/src/org/cx4a/rsense/ruby/LocalScope.java and then there is a class extending LocalScope, called DynamicScope which is focused on testing equality. Most usages within rsense are of LocalScope, with the DynamicScope class being used in one method. I would have to look more closely to see how its using the values it stores, but I'm sure we can find a way around it.