forked from jasder/antlr
Encapsulate several fields in ANTLRMessage
This commit is contained in:
parent
54c9382ce1
commit
2405219668
|
@ -30,11 +30,18 @@
|
|||
|
||||
package org.antlr.v4.tool;
|
||||
|
||||
import org.antlr.v4.runtime.misc.NotNull;
|
||||
import org.antlr.v4.runtime.misc.Nullable;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ANTLRMessage {
|
||||
public ErrorType errorType;
|
||||
public Object[] args;
|
||||
private static final Object[] EMPTY_ARGS = new Object[0];
|
||||
|
||||
@NotNull
|
||||
private final ErrorType errorType;
|
||||
@Nullable
|
||||
private final Object[] args;
|
||||
@Nullable
|
||||
private final Throwable e;
|
||||
|
||||
// used for location template
|
||||
|
@ -42,32 +49,35 @@ public class ANTLRMessage {
|
|||
public int line = -1;
|
||||
public int charPosition = -1;
|
||||
|
||||
public ANTLRMessage() {
|
||||
this(ErrorType.INVALID, (Throwable)null);
|
||||
}
|
||||
|
||||
public ANTLRMessage(ErrorType errorType) {
|
||||
public ANTLRMessage(@NotNull ErrorType errorType) {
|
||||
this(errorType, (Throwable)null);
|
||||
}
|
||||
|
||||
public ANTLRMessage(ErrorType errorType, Object... args) {
|
||||
public ANTLRMessage(@NotNull ErrorType errorType, Object... args) {
|
||||
this(errorType, null, args);
|
||||
}
|
||||
|
||||
public ANTLRMessage(ErrorType errorType, /*@Nullable*/ Throwable e, Object... args) {
|
||||
public ANTLRMessage(@NotNull ErrorType errorType, @Nullable Throwable e, Object... args) {
|
||||
this.errorType = errorType;
|
||||
this.e = e;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ErrorType getErrorType() {
|
||||
return errorType;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Object[] getArgs() {
|
||||
return args;
|
||||
if (args == null) {
|
||||
return EMPTY_ARGS;
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Throwable getCause() {
|
||||
return e;
|
||||
}
|
||||
|
@ -75,9 +85,9 @@ public class ANTLRMessage {
|
|||
@Override
|
||||
public String toString() {
|
||||
return "Message{" +
|
||||
"errorType=" + errorType +
|
||||
", args=" + (args == null ? null : Arrays.asList(args)) +
|
||||
", e=" + e +
|
||||
"errorType=" + getErrorType() +
|
||||
", args=" + Arrays.asList(getArgs()) +
|
||||
", e=" + getCause() +
|
||||
", fileName='" + fileName + '\'' +
|
||||
", line=" + line +
|
||||
", charPosition=" + charPosition +
|
||||
|
|
|
@ -96,24 +96,24 @@ public class ErrorManager {
|
|||
}
|
||||
|
||||
public ST getMessageTemplate(ANTLRMessage msg) {
|
||||
ST messageST = new ST(msg.errorType.msg);
|
||||
ST messageST = new ST(msg.getErrorType().msg);
|
||||
ST locationST = getLocationFormat();
|
||||
ST reportST = getReportFormat(msg.errorType.severity);
|
||||
ST reportST = getReportFormat(msg.getErrorType().severity);
|
||||
ST messageFormatST = getMessageFormat();
|
||||
|
||||
messageST.add("verbose", tool.longMessages);
|
||||
if ( msg.args!=null ) { // fill in arg1, arg2, ...
|
||||
for (int i=0; i<msg.args.length; i++) {
|
||||
String attr = "arg";
|
||||
if ( i>0 ) attr += i + 1;
|
||||
messageST.add(attr, msg.args[i]);
|
||||
}
|
||||
if ( msg.args.length<2 ) messageST.add("arg2", null); // some messages ref arg2
|
||||
Object[] args = msg.getArgs();
|
||||
for (int i=0; i<args.length; i++) {
|
||||
String attr = "arg";
|
||||
if ( i>0 ) attr += i + 1;
|
||||
messageST.add(attr, args[i]);
|
||||
}
|
||||
if ( args.length<2 ) messageST.add("arg2", null); // some messages ref arg2
|
||||
|
||||
if ( msg.getCause()!=null ) {
|
||||
messageST.add("exception", msg.getCause());
|
||||
messageST.add("stackTrace", msg.getCause().getStackTrace());
|
||||
Throwable cause = msg.getCause();
|
||||
if ( cause!=null ) {
|
||||
messageST.add("exception", cause);
|
||||
messageST.add("stackTrace", cause.getStackTrace());
|
||||
}
|
||||
else {
|
||||
messageST.add("exception", null); // avoid ST error msg
|
||||
|
@ -134,7 +134,7 @@ public class ErrorManager {
|
|||
locationValid = true;
|
||||
}
|
||||
|
||||
messageFormatST.add("id", msg.errorType.code);
|
||||
messageFormatST.add("id", msg.getErrorType().code);
|
||||
messageFormatST.add("text", messageST);
|
||||
|
||||
if (locationValid) reportST.add("location", locationST);
|
||||
|
|
|
@ -835,14 +835,14 @@ public abstract class BaseTest {
|
|||
ANTLRMessage foundMsg = null;
|
||||
for (int i = 0; i < equeue.errors.size(); i++) {
|
||||
ANTLRMessage m = equeue.errors.get(i);
|
||||
if (m.errorType==expectedMessage.errorType ) {
|
||||
if (m.getErrorType()==expectedMessage.getErrorType() ) {
|
||||
foundMsg = m;
|
||||
}
|
||||
}
|
||||
assertNotNull("no error; "+expectedMessage.errorType+" expected", foundMsg);
|
||||
assertNotNull("no error; "+expectedMessage.getErrorType()+" expected", foundMsg);
|
||||
assertTrue("error is not a GrammarSemanticsMessage",
|
||||
foundMsg instanceof GrammarSemanticsMessage);
|
||||
assertEquals(Arrays.toString(expectedMessage.args), Arrays.toString(foundMsg.args));
|
||||
assertEquals(Arrays.toString(expectedMessage.getArgs()), Arrays.toString(foundMsg.getArgs()));
|
||||
if ( equeue.size()!=1 ) {
|
||||
System.err.println(equeue);
|
||||
}
|
||||
|
@ -855,14 +855,14 @@ public abstract class BaseTest {
|
|||
ANTLRMessage foundMsg = null;
|
||||
for (int i = 0; i < equeue.warnings.size(); i++) {
|
||||
ANTLRMessage m = equeue.warnings.get(i);
|
||||
if (m.errorType==expectedMessage.errorType ) {
|
||||
if (m.getErrorType()==expectedMessage.getErrorType() ) {
|
||||
foundMsg = m;
|
||||
}
|
||||
}
|
||||
assertNotNull("no error; "+expectedMessage.errorType+" expected", foundMsg);
|
||||
assertNotNull("no error; "+expectedMessage.getErrorType()+" expected", foundMsg);
|
||||
assertTrue("error is not a GrammarSemanticsMessage",
|
||||
foundMsg instanceof GrammarSemanticsMessage);
|
||||
assertEquals(Arrays.toString(expectedMessage.args), Arrays.toString(foundMsg.args));
|
||||
assertEquals(Arrays.toString(expectedMessage.getArgs()), Arrays.toString(foundMsg.getArgs()));
|
||||
if ( equeue.size()!=1 ) {
|
||||
System.err.println(equeue);
|
||||
}
|
||||
|
@ -876,18 +876,18 @@ public abstract class BaseTest {
|
|||
ANTLRMessage foundMsg = null;
|
||||
for (int i = 0; i < equeue.errors.size(); i++) {
|
||||
ANTLRMessage m = equeue.errors.get(i);
|
||||
if (m.errorType==expectedMessage.errorType ) {
|
||||
if (m.getErrorType()==expectedMessage.getErrorType() ) {
|
||||
foundMsg = m;
|
||||
}
|
||||
}
|
||||
assertTrue("no error; "+expectedMessage.errorType+" expected", !equeue.errors.isEmpty());
|
||||
assertTrue("no error; "+expectedMessage.getErrorType()+" expected", !equeue.errors.isEmpty());
|
||||
assertTrue("too many errors; "+equeue.errors, equeue.errors.size()<=1);
|
||||
assertNotNull("couldn't find expected error: "+expectedMessage.errorType, foundMsg);
|
||||
assertNotNull("couldn't find expected error: "+expectedMessage.getErrorType(), foundMsg);
|
||||
/*
|
||||
assertTrue("error is not a GrammarSemanticsMessage",
|
||||
foundMsg instanceof GrammarSemanticsMessage);
|
||||
*/
|
||||
assertTrue(Arrays.equals(expectedMessage.args, foundMsg.args));
|
||||
assertTrue(Arrays.equals(expectedMessage.getArgs(), foundMsg.getArgs()));
|
||||
}
|
||||
|
||||
public static class FilteringTokenStream extends CommonTokenStream {
|
||||
|
|
|
@ -303,7 +303,7 @@ public class TestCompositeGrammars extends BaseTest {
|
|||
writeFile(tmpdir, "M.g4", master);
|
||||
Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue);
|
||||
|
||||
assertEquals(ErrorType.SYNTAX_ERROR, equeue.errors.get(0).errorType);
|
||||
assertEquals(ErrorType.SYNTAX_ERROR, equeue.errors.get(0).getErrorType());
|
||||
}
|
||||
|
||||
@Test public void testDelegatorRuleOverridesDelegate() throws Exception {
|
||||
|
|
Loading…
Reference in New Issue