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

Fix: mixed line endings caused invalid source section indices.

parent 221a0e02
No related branches found
No related tags found
No related merge requests found
......@@ -91,6 +91,7 @@ public class ParserGeneration {
"different warning for hex and dec integer literals",
"raise ZERO_LENGTH_VARIABLE errors in parser",
"support for file delimiter",
"pass along TruffleRLanguage"
"pass along TruffleRLanguage",
"convert line endings"
};
}
......@@ -29,6 +29,10 @@ package com.oracle.truffle.r.parser;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.net.URISyntaxException;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.frame.MaterializedFrame;
......@@ -149,9 +153,15 @@ import com.oracle.truffle.r.runtime.RError;
if(q0 != -1 && q1 != -1) {
String path = commentLine.substring(q0+1, q1);
try {
source = RSource.fromFileName(path, false);
String content = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
String lineEnding = detectLineEnding(initialSource.getCode());
content = convertToLineEnding(content, lineEnding);
source = RSource.fromFileName(content, path, false);
fileStartOffset = commentToken.getStopIndex() + 1;
} catch (IOException e) {
resetSource();
} catch (URISyntaxException e) {
resetSource();
}
} else {
// fall back and use the initial source (the file being parsed)
......@@ -165,6 +175,25 @@ import com.oracle.truffle.r.runtime.RError;
fileStartOffset = 0;
}
private String detectLineEnding(String code) {
int lf = code.indexOf("\n");
int crlf = code.indexOf("\r\n");
if(crlf != -1 && crlf < lf) {
return "\r\n";
}
return "\n";
}
private String convertToLineEnding(String content, String lineEnding) {
if("\n".equals(lineEnding)) {
return content.replaceAll("\\r\\n", "\n");
} else if("\r\n".equals(lineEnding)) {
return content.replaceAll("\\n", "\r\n");
}
return content;
}
// without this override, the parser will not throw exceptions if it can recover
@Override
protected Object recoverFromMismatchedToken(IntStream input, int expected, BitSet follow) throws RecognitionException {
......
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