package org.jruby.parser;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import org.jruby.*;
import org.jruby.ast.Node;
import org.jruby.lexer.ByteListLexerSource;
import org.jruby.lexer.GetsLexerSource;
import org.jruby.lexer.LexerSource;
import org.jruby.lexer.yacc.*;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.LoadServiceResourceInputStream;
import org.jruby.util.ByteList;
public class Parser {
private final Ruby runtime;
private volatile long totalTime;
private volatile int totalBytes;
public Parser(Ruby runtime) {
this.runtime = runtime;
}
public long getTotalTime() {
return totalTime;
}
public int getTotalBytes() {
return totalBytes;
}
@SuppressWarnings("unchecked")
public Node parse(String file, ByteList content, DynamicScope blockScope,
ParserConfiguration configuration) {
configuration.setDefaultEncoding(content.getEncoding());
RubyArray list = getLines(configuration, runtime, file);
LexerSource lexerSource = new ByteListLexerSource(file, configuration.getLineNumber(), content, list);
return parse(file, lexerSource, blockScope, configuration);
}
@SuppressWarnings("unchecked")
public Node parse(String file, byte[] content, DynamicScope blockScope,
ParserConfiguration configuration) {
RubyArray list = getLines(configuration, runtime, file);
ByteList in = new ByteList(content, configuration.getDefaultEncoding());
LexerSource lexerSource = new ByteListLexerSource(file, configuration.getLineNumber(), in, list);
return parse(file, lexerSource, blockScope, configuration);
}
@SuppressWarnings("unchecked")
public Node parse(String file, InputStream content, DynamicScope blockScope,
ParserConfiguration configuration) {
if (content instanceof LoadServiceResourceInputStream) {
return parse(file, ((LoadServiceResourceInputStream) content).getBytes(), blockScope, configuration);
} else {
RubyArray list = getLines(configuration, runtime, file);
boolean requiresClosing = false;
RubyIO io;
if (content instanceof FileInputStream) {
io = new RubyFile(runtime, file, ((FileInputStream) content).getChannel());
} else {
requiresClosing = true;
io = RubyIO.newIO(runtime, Channels.newChannel(content));
}
LexerSource lexerSource = new GetsLexerSource(file, configuration.getLineNumber(), io, list, configuration.getDefaultEncoding());
try {
return parse(file, lexerSource, blockScope, configuration);
} finally {
if (requiresClosing && runtime.getObject().getConstantAt("DATA") != io) io.close();
runtime.setCurrentLine(0);
}
}
}
@SuppressWarnings("unchecked")
public Node parse(String file, LexerSource lexerSource, DynamicScope blockScope,
ParserConfiguration configuration) {
if (blockScope != null) {
configuration.parseAsBlock(blockScope);
}
long startTime = System.nanoTime();
RubyParser parser = new RubyParser(lexerSource, runtime.getWarnings());
RubyParserResult result;
try {
result = parser.parse(configuration);
if (parser.lexer.isEndSeen() && configuration.isSaveData()) {
IRubyObject verbose = runtime.getVerbose();
runtime.setVerbose(runtime.getNil());
runtime.defineGlobalConstant("DATA", lexerSource.getRemainingAsIO());
runtime.setVerbose(verbose);
}
} catch (IOException e) {
throw runtime.newSyntaxError("Problem reading source: " + e);
} catch (SyntaxException e) {
switch (e.getPid()) {
case UNKNOWN_ENCODING:
case NOT_ASCII_COMPATIBLE:
throw runtime.newArgumentError(e.getMessage());
default:
StringBuilder buffer = new StringBuilder(100);
buffer.append(e.getFile()).append(':');
buffer.append(e.getLine() + 1).append(": ");
buffer.append(e.getMessage());
throw runtime.newSyntaxError(buffer.toString());
}
}
if (result.getScope() != null) {
result.getScope().growIfNeeded();
}
Node ast = result.getAST();
totalTime += System.nanoTime() - startTime;
totalBytes += lexerSource.getOffset();
return ast;
}
private RubyArray getLines(ParserConfiguration configuration, Ruby runtime, String file) {
RubyArray list = null;
IRubyObject scriptLines = runtime.getObject().getConstantAt("SCRIPT_LINES__");
if (!configuration.isEvalParse() && scriptLines != null) {
if (scriptLines instanceof RubyHash) {
list = runtime.newArray();
((RubyHash) scriptLines).op_aset(runtime.getCurrentContext(), runtime.newString(file), list);
}
}
return list;
}
}