Merge pull request #722 from sharwell/fix-667
Fix multiple issues with getSourceName
This commit is contained in:
commit
11aed36aa4
|
@ -29,6 +29,8 @@
|
||||||
*/
|
*/
|
||||||
package org.antlr.v4.runtime;
|
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 org.antlr.v4.runtime.misc.Utils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -40,16 +42,16 @@ import java.io.IOException;
|
||||||
public class ANTLRFileStream extends ANTLRInputStream {
|
public class ANTLRFileStream extends ANTLRInputStream {
|
||||||
protected String fileName;
|
protected String fileName;
|
||||||
|
|
||||||
public ANTLRFileStream(String fileName) throws IOException {
|
public ANTLRFileStream(@NotNull String fileName) throws IOException {
|
||||||
this(fileName, null);
|
this(fileName, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ANTLRFileStream(String fileName, String encoding) throws IOException {
|
public ANTLRFileStream(@NotNull String fileName, String encoding) throws IOException {
|
||||||
this.fileName = fileName;
|
this.fileName = fileName;
|
||||||
load(fileName, encoding);
|
load(fileName, encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load(String fileName, String encoding)
|
public void load(@NotNull String fileName, @Nullable String encoding)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
data = Utils.readFile(fileName, encoding);
|
data = Utils.readFile(fileName, encoding);
|
||||||
|
|
|
@ -238,6 +238,10 @@ public class ANTLRInputStream implements CharStream {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getSourceName() {
|
public String getSourceName() {
|
||||||
|
if (name == null || name.isEmpty()) {
|
||||||
|
return UNKNOWN_SOURCE_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -91,6 +91,7 @@ public interface TokenSource {
|
||||||
* non-null, non-empty string. If such a name is not known, this method
|
* non-null, non-empty string. If such a name is not known, this method
|
||||||
* returns {@link IntStream#UNKNOWN_SOURCE_NAME}.
|
* returns {@link IntStream#UNKNOWN_SOURCE_NAME}.
|
||||||
*/
|
*/
|
||||||
|
@NotNull
|
||||||
public String getSourceName();
|
public String getSourceName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -297,6 +297,10 @@ public class UnbufferedCharStream implements CharStream {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getSourceName() {
|
public String getSourceName() {
|
||||||
|
if (name == null || name.isEmpty()) {
|
||||||
|
return UNKNOWN_SOURCE_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -96,11 +96,11 @@ public class Utils {
|
||||||
return buf.toString();
|
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);
|
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);
|
File f = new File(fileName);
|
||||||
FileOutputStream fos = new FileOutputStream(f);
|
FileOutputStream fos = new FileOutputStream(f);
|
||||||
OutputStreamWriter osw;
|
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);
|
return readFile(fileName, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static char[] readFile(String fileName, String encoding) throws IOException {
|
@NotNull
|
||||||
if ( fileName==null ) {
|
public static char[] readFile(@NotNull String fileName, @Nullable String encoding) throws IOException {
|
||||||
return null;
|
|
||||||
}
|
|
||||||
File f = new File(fileName);
|
File f = new File(fileName);
|
||||||
int size = (int)f.length();
|
int size = (int)f.length();
|
||||||
InputStreamReader isr;
|
InputStreamReader isr;
|
||||||
|
|
|
@ -1269,7 +1269,7 @@ public abstract class BaseTest {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getSourceName() {
|
public String getSourceName() {
|
||||||
return null;
|
return UNKNOWN_SOURCE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -33,6 +33,7 @@ package org.antlr.v4.test;
|
||||||
import org.antlr.v4.runtime.CharStream;
|
import org.antlr.v4.runtime.CharStream;
|
||||||
import org.antlr.v4.runtime.CommonToken;
|
import org.antlr.v4.runtime.CommonToken;
|
||||||
import org.antlr.v4.runtime.CommonTokenStream;
|
import org.antlr.v4.runtime.CommonTokenStream;
|
||||||
|
import org.antlr.v4.runtime.IntStream;
|
||||||
import org.antlr.v4.runtime.Lexer;
|
import org.antlr.v4.runtime.Lexer;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
import org.antlr.v4.runtime.TokenFactory;
|
import org.antlr.v4.runtime.TokenFactory;
|
||||||
|
@ -235,7 +236,7 @@ public class TestCommonTokenStream extends TestBufferedTokenStream {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getSourceName() {
|
public String getSourceName() {
|
||||||
return null;
|
return IntStream.UNKNOWN_SOURCE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -283,7 +284,7 @@ public class TestCommonTokenStream extends TestBufferedTokenStream {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getSourceName() {
|
public String getSourceName() {
|
||||||
return null;
|
return IntStream.UNKNOWN_SOURCE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue