Merge pull request #722 from sharwell/fix-667

Fix multiple issues with getSourceName
This commit is contained in:
Terence Parr 2014-09-29 09:36:41 -07:00
commit 11aed36aa4
7 changed files with 24 additions and 13 deletions

View File

@ -29,6 +29,8 @@
*/
package org.antlr.v4.runtime;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import org.antlr.v4.runtime.misc.Utils;
import java.io.IOException;
@ -40,16 +42,16 @@ import java.io.IOException;
public class ANTLRFileStream extends ANTLRInputStream {
protected String fileName;
public ANTLRFileStream(String fileName) throws IOException {
public ANTLRFileStream(@NotNull String fileName) throws IOException {
this(fileName, null);
}
public ANTLRFileStream(String fileName, String encoding) throws IOException {
public ANTLRFileStream(@NotNull String fileName, String encoding) throws IOException {
this.fileName = fileName;
load(fileName, encoding);
}
public void load(String fileName, String encoding)
public void load(@NotNull String fileName, @Nullable String encoding)
throws IOException
{
data = Utils.readFile(fileName, encoding);

View File

@ -238,6 +238,10 @@ public class ANTLRInputStream implements CharStream {
@Override
public String getSourceName() {
if (name == null || name.isEmpty()) {
return UNKNOWN_SOURCE_NAME;
}
return name;
}

View File

@ -91,6 +91,7 @@ public interface TokenSource {
* non-null, non-empty string. If such a name is not known, this method
* returns {@link IntStream#UNKNOWN_SOURCE_NAME}.
*/
@NotNull
public String getSourceName();
/**

View File

@ -297,6 +297,10 @@ public class UnbufferedCharStream implements CharStream {
@Override
public String getSourceName() {
if (name == null || name.isEmpty()) {
return UNKNOWN_SOURCE_NAME;
}
return name;
}

View File

@ -96,11 +96,11 @@ public class Utils {
return buf.toString();
}
public static void writeFile(String fileName, String content) throws IOException {
public static void writeFile(@NotNull String fileName, @NotNull String content) throws IOException {
writeFile(fileName, content, null);
}
public static void writeFile(String fileName, String content, String encoding) throws IOException {
public static void writeFile(@NotNull String fileName, @NotNull String content, @Nullable String encoding) throws IOException {
File f = new File(fileName);
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter osw;
@ -119,14 +119,13 @@ public class Utils {
}
}
public static char[] readFile(String fileName) throws IOException {
@NotNull
public static char[] readFile(@NotNull String fileName) throws IOException {
return readFile(fileName, null);
}
public static char[] readFile(String fileName, String encoding) throws IOException {
if ( fileName==null ) {
return null;
}
@NotNull
public static char[] readFile(@NotNull String fileName, @Nullable String encoding) throws IOException {
File f = new File(fileName);
int size = (int)f.length();
InputStreamReader isr;

View File

@ -1269,7 +1269,7 @@ public abstract class BaseTest {
@Override
public String getSourceName() {
return null;
return UNKNOWN_SOURCE_NAME;
}
@Override

View File

@ -33,6 +33,7 @@ package org.antlr.v4.test;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.IntStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenFactory;
@ -235,7 +236,7 @@ public class TestCommonTokenStream extends TestBufferedTokenStream {
@Override
public String getSourceName() {
return null;
return IntStream.UNKNOWN_SOURCE_NAME;
}
@Override
@ -283,7 +284,7 @@ public class TestCommonTokenStream extends TestBufferedTokenStream {
@Override
public String getSourceName() {
return null;
return IntStream.UNKNOWN_SOURCE_NAME;
}
@Override