Displaying a more intelligent error message with invalid tokens in the

input stream. #20
This commit is contained in:
Rob Greene 2019-09-02 21:07:52 -05:00
parent cc12f7ccba
commit f729ef7c6d
1 changed files with 12 additions and 2 deletions

View File

@ -158,8 +158,18 @@ public class TokenReader {
break;
}
default:
throw new IOException(String.format(
"Unknown! ttype=%d, nval=%f, sval=%s\n", tokenizer.ttype, tokenizer.nval, tokenizer.sval));
String message = String.format("Unknown or unexpected character '%c'", tokenizer.ttype);
if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
message = String.format("Unknown or unexpected string '%s'", tokenizer.sval);
} else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
message = String.format("Unknown or unexpected number %f", tokenizer.nval);
} else if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
message = "Unexpected EOF";
} else if (tokenizer.ttype == StreamTokenizer.TT_EOL) {
message = "Unexpected EOL";
}
message += String.format(" found on line #%d", tokenizer.lineno());
throw new IOException(message);
}
}
}