Skip to content
Snippets Groups Projects
Commit 86ab4655 authored by Florian Angerer's avatar Florian Angerer
Browse files

Create proper file source if file option provided.

parent b857754e
No related branches found
No related tags found
No related merge requests found
Showing with 81 additions and 6 deletions
......@@ -59,6 +59,7 @@ public final class EmbeddedConsoleHandler extends DelegatingConsoleHandler {
private Context context;
private Supplier<ConsoleHandler> delegateFactory;
private ConsoleHandler delegate;
private int currentLine;
private CallTarget readLineCallTarget;
private CallTarget writeCallTarget;
......@@ -78,7 +79,9 @@ public final class EmbeddedConsoleHandler extends DelegatingConsoleHandler {
@Override
public String readLine() {
try (ContextClose ignored = inContext()) {
return isOverridden("R_ReadConsole") ? (String) getReadLineCallTarget().call("TODO prompt>") : getDelegate().readLine();
String l = isOverridden("R_ReadConsole") ? (String) getReadLineCallTarget().call("TODO prompt>") : getDelegate().readLine();
currentLine++;
return l;
}
}
......@@ -239,4 +242,9 @@ public final class EmbeddedConsoleHandler extends DelegatingConsoleHandler {
return null;
}
}
@Override
public int getCurrentLineIndex() {
return currentLine;
}
}
......@@ -39,6 +39,11 @@ public abstract class ConsoleHandler {
*/
public abstract String readLine();
/**
* Return the current 1-based line number.
*/
public abstract int getCurrentLineIndex();
/**
* Set the R prompt.
*/
......
......@@ -35,6 +35,7 @@ public class DefaultConsoleHandler extends ConsoleHandler {
private final BufferedReader in;
private final PrintStream out;
private String prompt;
private int currentLine;
public DefaultConsoleHandler(InputStream in, OutputStream out, boolean interactive) {
this.in = new BufferedReader(new InputStreamReader(in));
......@@ -49,6 +50,7 @@ public class DefaultConsoleHandler extends ConsoleHandler {
out.print(prompt);
}
String line = in.readLine();
currentLine++;
if ((line == null || "".equals(line.trim())) && prompt != null && !interactive) {
out.println();
}
......@@ -62,4 +64,9 @@ public class DefaultConsoleHandler extends ConsoleHandler {
public void setPrompt(String prompt) {
this.prompt = prompt;
}
@Override
public int getCurrentLineIndex() {
return currentLine;
}
}
......@@ -38,6 +38,7 @@ public class JLineConsoleHandler extends ConsoleHandler {
private final ConsoleReader console;
private final MemoryHistory history;
private final boolean noPrompt;
private int currentLine;
public JLineConsoleHandler(InputStream inStream, OutputStream outStream, boolean noPrompt) {
this.noPrompt = noPrompt;
......@@ -65,6 +66,7 @@ public class JLineConsoleHandler extends ConsoleHandler {
public String readLine() {
try {
console.getTerminal().init();
currentLine++;
return console.readLine();
} catch (UserInterruptException e) {
// interrupted by ctrl-c
......@@ -94,4 +96,9 @@ public class JLineConsoleHandler extends ConsoleHandler {
}
return result;
}
@Override
public int getCurrentLineIndex() {
return currentLine;
}
}
......@@ -262,7 +262,6 @@ public class RCommand {
*/
public static int readEvalPrint(Context context, ConsoleHandler consoleHandler, File srcFile) {
int lastStatus = 0;
int line = 0;
try {
while (true) { // processing inputs
boolean doEcho = doEcho(context);
......@@ -272,7 +271,6 @@ public class RCommand {
if (input == null) {
throw new EOFException();
}
line++;
String trInput = input.trim();
if (trInput.equals("") || trInput.charAt(0) == '#') {
// nothing to parse
......@@ -281,12 +279,13 @@ public class RCommand {
String continuePrompt = null;
StringBuilder sb = new StringBuilder(input);
int startLine = consoleHandler.getCurrentLineIndex();
while (true) { // processing subsequent lines while input is incomplete
lastStatus = 0;
try {
Source src;
if (srcFile != null) {
src = Source.newBuilder("R", sb.toString(), srcFile.getName() + "#" + line).interactive(true).uri(srcFile.toURI()).buildLiteral();
src = Source.newBuilder("R", sb.toString(), srcFile.getName() + "#" + startLine).interactive(true).uri(srcFile.toURI()).buildLiteral();
} else {
src = Source.newBuilder("R", sb.toString(), "<REPL>").interactive(true).buildLiteral();
}
......
......@@ -54,4 +54,9 @@ class StringConsoleHandler extends ConsoleHandler {
public void setPrompt(String prompt) {
this.prompt = prompt;
}
@Override
public int getCurrentLineIndex() {
return currentLine;
}
}
......@@ -96,6 +96,7 @@ public class ParserGeneration {
"handle four and more dots as identifier",
"allow greek characters in identifiers",
"allow everything but newlines in %<ident>% operators",
"allow strings in :: and :::"
"allow strings in :: and :::",
"use file for interactive single-line source"
};
}
......@@ -28,6 +28,7 @@ package com.oracle.truffle.r.parser;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
......@@ -37,6 +38,7 @@ import java.net.URISyntaxException;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.source.Source.Builder;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.r.runtime.RError;
......@@ -78,10 +80,51 @@ import com.oracle.truffle.r.runtime.RError;
public RParser(Source source, RCodeBuilder<T> builder, TruffleRLanguage language) {
super(new CommonTokenStream(new RLexer(new ANTLRStringStream(source.getCharacters().toString()))));
assert source != null && builder != null;
this.source = source;
this.initialSource = source;
this.builder = builder;
this.language = language;
if (source.getURI() != null && source.getName().contains("#")) {
this.source = createFullSource(source);
} else {
this.source = source;
}
}
private Source createFullSource(Source original) {
String originalName = original.getName();
// check if source name is like 'path/to/source.R#45-54'
int hash_idx = originalName.lastIndexOf("#");
if (hash_idx == -1) {
return original;
}
String fileName = originalName.substring(0, hash_idx);
String lineRange = originalName.substring(hash_idx + 1);
try {
// check for line range, e.g. '45-54'
int startLine = -1;
int endLine = -1;
int dashIdx = lineRange.indexOf('-');
if (dashIdx != -1) {
startLine = Integer.parseInt(lineRange.substring(0, dashIdx));
endLine = Integer.parseInt(lineRange.substring(dashIdx + 1));
} else {
startLine = Integer.parseInt(lineRange);
}
Builder<IOException, RuntimeException, RuntimeException> newBuilder = Source.newBuilder(new File(fileName));
if (original.isInteractive()) {
newBuilder.interactive();
}
Source fullSource = newBuilder.build();
fileStartOffset = -fullSource.getLineStartOffset(startLine);
return fullSource;
} catch (NumberFormatException e) {
} catch (IOException e) {
} catch (RuntimeException e) {
}
return original;
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment