Skip to content

Commit

Permalink
Fix some broken tests for ripper. This adds:
Browse files Browse the repository at this point in the history
 - Ripper::lex_state_name(n) = concat'd string of all lex_states atm
 - Ripper#state = Fixnum of current lex_states

tbh I tried figuring out where these methods were defined in MRI and I gave up.
The specs made it fairly clear what they were for, but where are these?

[coming up: need to update grammar in ripper for 2.5 productions]
  • Loading branch information
enebo committed Mar 20, 2018
1 parent afa651d commit c9a3691
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Expand Up @@ -32,7 +32,6 @@
import org.jcodings.Encoding;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyString;
import org.jruby.lexer.LexerSource;
import org.jruby.runtime.Helpers;
import org.jruby.lexer.yacc.StackState;
Expand Down
39 changes: 37 additions & 2 deletions core/src/main/java/org/jruby/ext/ripper/RubyRipper.java
Expand Up @@ -57,7 +57,7 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {

ripper.defineConstant("SCANNER_EVENT_TABLE", createScannerEventTable(runtime, ripper));
ripper.defineConstant("PARSER_EVENT_TABLE", createParserEventTable(runtime, ripper));

ripper.defineAnnotatedMethods(RubyRipper.class);
}

Expand Down Expand Up @@ -318,6 +318,11 @@ public IRubyObject lineno(ThreadContext context) {

return context.runtime.newFixnum(parser.getLineno());
}

@JRubyMethod
public IRubyObject state(ThreadContext context) {
return context.runtime.newFixnum(parser.getState());
}

@JRubyMethod
public IRubyObject parse(ThreadContext context) {
Expand Down Expand Up @@ -357,6 +362,29 @@ public static IRubyObject dedent_string(ThreadContext context, IRubyObject self,
public IRubyObject dedent_string(ThreadContext context, IRubyObject _input, IRubyObject _width) {
return dedent_string(context, this, _input, _width);
}

@JRubyMethod(meta = true)
public static IRubyObject lex_state_name(ThreadContext context, IRubyObject self, IRubyObject lexStateParam) {
int lexState = lexStateParam.convertToInteger().getIntValue();

boolean needsSeparator = false;
RubyString name = null;
for (int i = 0; i < lexStateNames.length; i++) {
if ((lexState & (1<<i)) != 0) {
if (!needsSeparator) {
name = context.runtime.newString(lexStateNames[i]);
needsSeparator = true;
} else {
name.cat('|');
name.catString(lexStateNames[i]);
}
}
}

if (name == null) name = context.runtime.newString("EXPR_NONE");

return name;
}

private LexerSource source(ThreadContext context, IRubyObject src, String filename, int lineno) {
// FIXME: respond_to? returns private methods
Expand All @@ -380,8 +408,15 @@ private int lineAsInt(ThreadContext context, IRubyObject line) {

return RubyNumeric.fix2int(line.convertToInteger()) - 1;
}

private RipperParserBase parser = null;
private IRubyObject filename = null;
private boolean parseStarted = false;

// FIXME: Consider moving this to LexingCommon but it is very specific to ripper (perhaps I can make it more useful wit debuggin?).
// These are ordered in same order as LexerCommon. Any changes to lex_state should update this.
private static String[] lexStateNames = new String[] {
"EXPR_BEG", "EXPR_END", "EXPR_ENDARG", "EXPR_ENDFN", "EXPR_ARG", "EXPR_CMDARG",
"EXPR_MID", "EXPR_FNAME", "EXPR_DOT", "EXPR_CLASS", "EXPR_LABEL", "EXPR_LABELED", "EXPR_FITEM"
};
}

0 comments on commit c9a3691

Please sign in to comment.