pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/jruby/jruby/pull/9357/files

in="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/code-2d31826944fd3be8.css" /> Backport implicitOrVariable code for prism parser by enebo · Pull Request #9357 · jruby/jruby · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/parser/RubyParserBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public Node declareIdentifier(ByteList byteName) {
node = new LocalVarNode(lexer.tokline, slot, name);
} else if (dyna_in_block() && id.equals("it")) {
if (!hasArguments()) {
int existing = currentScope.isDefinedNotImplicit(id);
int existing = currentScope.isDefinedOrImplicit(id);
boolean newIt = false;

if (existing == -1) {
Expand All @@ -416,7 +416,7 @@ public Node declareIdentifier(ByteList byteName) {
node = new DVarNode(lexer.tokline, slot, name);
if (newIt) set_it_id(node);
} else {
slot = currentScope.isDefinedNotImplicit(id);
slot = currentScope.isDefinedOrImplicit(id);
// A special it cannot exist without being marked as a special it.
if (it_id() != null) compile_error("`it` is not allowed when an ordinary parameter is defined");

Expand Down
80 changes: 59 additions & 21 deletions core/src/main/java/org/jruby/parser/StaticScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public int exists(String name) {
* @param name of the variable to find
* @return index of variable; -1 if it does not exist; -2 if is implicit.
*/
public int existsAndNotImplicit(String name) {
public int existsOrImplicit(String name) {
int slot = findVariableName(name);
if (slot >= 0 && isImplicitVariable(slot)) return IMPLICIT;
return slot;
Expand All @@ -455,14 +455,25 @@ public int isDefined(String name) {
}

/**
* Is this name visible to the current scope and not an implicit variable ("it", "_1", etc).
* Is this name visible to the current scope as either a variable or an implicit variable
*
* @param name to be looked for
* @return a location where the left-most 16 bits of number of scopes down it is and the
* right-most 16 bits represents its index in that scope
*/
public int isDefinedOrImplicit(String name) {
return isDefinedOrImplicit(name, 0);
}

/**
* Is this name visible to the current scope and not an implicit parameter ("it", "_1", etc).
*
* @param name to be looked for
* @return -1 if it is not defined; -2 if it is implicit; or a location where the left-most 16 bits of number of scopes down it is and the
* right-most 16 bits represents its index in that scope.
*/
public int isDefinedNotImplicit(String name) {
return isDefinedNotImplicit(name, 0);
public int isDefinedImplicitParameter(String name) {
return isDefinedImplicitParameter(name, 0);
}

/**
Expand Down Expand Up @@ -547,7 +558,8 @@ public <T> T collectVariables(IntFunction<T> collectionFactory, BiConsumer<T, St
* @return populated collection
*/
public <T> T collectAllVariables(ThreadContext context, BiFunction<ThreadContext, Integer, T> collectionFactory, BiConsumer<T, String> collectionPopulator) {
return collectVariables(context, collectionFactory, collectionPopulator, null);
T collection = collectVariables(context, collectionFactory, collectionPopulator, false, true);
return collectVariables(context, (t, i) -> collection, collectionPopulator, true, true);
}

/**
Expand All @@ -562,7 +574,7 @@ public <T> T collectAllVariables(ThreadContext context, BiFunction<ThreadContext
* @return populated collection
*/
public <T> T collectVariables(ThreadContext context, BiFunction<ThreadContext, Integer, T> collectionFactory, BiConsumer<T, String> collectionPopulator) {
return collectVariables(context, collectionFactory, collectionPopulator, false);
return collectVariables(context, collectionFactory, collectionPopulator, false, true);
}

/**
Expand All @@ -576,24 +588,30 @@ public <T> T collectVariables(ThreadContext context, BiFunction<ThreadContext, I
* @param <T> resulting collection type
* @return populated collection
*/
public <T> T collectImplicitVariables(ThreadContext context, BiFunction<ThreadContext, Integer, T> collectionFactory, BiConsumer<T, String> collectionPopulator) {
return collectVariables(context, collectionFactory, collectionPopulator, true);
public <T> T collectImplicitParameters(ThreadContext context, BiFunction<ThreadContext, Integer, T> collectionFactory, BiConsumer<T, String> collectionPopulator) {
return collectVariables(context, collectionFactory, collectionPopulator, true, false);
}

public <T> T collectVariables(ThreadContext context, BiFunction<ThreadContext, Integer, T> collectionFactory, BiConsumer<T, String> collectionPopulator, Boolean implicit) {
private <T> T collectVariables(ThreadContext context, BiFunction<ThreadContext, Integer, T> collectionFactory, BiConsumer<T, String> collectionPopulator, boolean implicit, boolean parents) {
StaticScope current = this;

T collection = collectionFactory.apply(context, current.variableNamesLength);

HashMap<String, Object> dedup = new HashMap<>();

while (current.isBlockOrEval) {
addVariableNamesToCollection(current, collectionPopulator, dedup, collection, implicit);
addVariableNamesToCollection(current, collectionPopulator, dedup, collection, implicit);

if (parents) {
current = current.enclosingScope;
}

// once more for method scope
addVariableNamesToCollection(current, collectionPopulator, dedup, collection, implicit);
while (current.isBlockOrEval) {
addVariableNamesToCollection(current, collectionPopulator, dedup, collection, implicit);
current = current.enclosingScope;
}

// once more for method scope
addVariableNamesToCollection(current, collectionPopulator, dedup, collection, implicit);
}

return collection;
}
Expand Down Expand Up @@ -632,7 +650,20 @@ public RubyArray getLocalVariables(Ruby runtime) {
* @return populated RubyArray
*/
public RubyArray getLocalVariables(ThreadContext context) {
return collectAllVariables(
return collectVariables(
context,
Create::allocArray,
(array, id) -> appendVariableIfValid(context, array, id));
}

/**
* Get a Ruby Array of all implicit local variables.
*
* @param context the current context
* @return populated RubyArray
*/
public RubyArray getImplicitParameters(ThreadContext context) {
return collectImplicitParameters(
context,
Create::allocArray,
(array, id) -> appendVariableIfValid(context, array, id));
Expand All @@ -654,19 +685,26 @@ public int isDefined(String name, int depth) {
}
}

public int isDefinedNotImplicit(String name, int depth) {
int slot = existsAndNotImplicit(name);
if (slot == IMPLICIT) return slot;

public int isDefinedOrImplicit(String name, int depth) {
if (isBlockOrEval) {
int slot = existsOrImplicit(name);
if (slot == IMPLICIT) return slot;

if (slot >= 0) return (depth << 16) | slot;

return enclosingScope.isDefinedNotImplicit(name, depth + 1);
return enclosingScope.isDefinedOrImplicit(name, depth + 1);
} else {
return (depth << 16) | slot;
return (depth << 16) | existsOrImplicit(name);
}
}

public int isDefinedImplicitParameter(String name, int depth) {
int slot = existsOrImplicit(name);
if (slot == IMPLICIT) return slot;

return -1;
}

public AssignableNode addAssign(int line, RubySymbol symbolID, Node value) {
int slot = addVariable(symbolID.idString());
// No bit math to store level since we know level is zero for this case
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy