Skip to content

Commit

Permalink
refactor: prepare preprocessor error improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudroques committed Feb 22, 2024
1 parent a48dd52 commit 3cc6823
Show file tree
Hide file tree
Showing 129 changed files with 412 additions and 432 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Warning, "version" should be the same in gradle.properties and Version.java
# Any idea anyone how to magically synchronize those :-) ?
version = 1.2024.4beta4
version = 1.2024.4beta5
org.gradle.workers.max = 3
4 changes: 2 additions & 2 deletions src/net/sourceforge/plantuml/preproc/Defines.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import net.sourceforge.plantuml.security.SecurityProfile;
import net.sourceforge.plantuml.security.SecurityUtils;
import net.sourceforge.plantuml.text.StringLocated;
import net.sourceforge.plantuml.tim.EaterExceptionLocated;
import net.sourceforge.plantuml.tim.EaterException;
import net.sourceforge.plantuml.tim.TMemory;
import net.sourceforge.plantuml.tim.TVariableScope;
import net.sourceforge.plantuml.utils.Log;
Expand All @@ -82,7 +82,7 @@ public static Defines createEmpty() {
return new Defines();
}

public void copyTo(TMemory memory, StringLocated location) throws EaterExceptionLocated {
public void copyTo(TMemory memory, StringLocated location) throws EaterException {
for (Entry<String, Define> ent : values.entrySet()) {
final String name = ent.getKey();
final Define def = ent.getValue();
Expand Down
4 changes: 2 additions & 2 deletions src/net/sourceforge/plantuml/preproc/Sub.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

import net.sourceforge.plantuml.text.StringLocated;
import net.sourceforge.plantuml.text.TLineType;
import net.sourceforge.plantuml.tim.EaterExceptionLocated;
import net.sourceforge.plantuml.tim.EaterException;
import net.sourceforge.plantuml.tim.EaterStartsub;
import net.sourceforge.plantuml.tim.TContext;
import net.sourceforge.plantuml.tim.TMemory;
Expand Down Expand Up @@ -79,7 +79,7 @@ public final List<StringLocated> lines() {
}

public static Sub fromFile(ReadLine reader, String blocname, TContext context, TMemory memory)
throws IOException, EaterExceptionLocated {
throws IOException, EaterException {
Sub result = null;
StringLocated s = null;
boolean skip = false;
Expand Down
10 changes: 5 additions & 5 deletions src/net/sourceforge/plantuml/preproc2/PreprocessorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import net.sourceforge.plantuml.preproc.Stdlib;
import net.sourceforge.plantuml.security.SURL;
import net.sourceforge.plantuml.text.StringLocated;
import net.sourceforge.plantuml.tim.EaterExceptionLocated;
import net.sourceforge.plantuml.tim.EaterException;
import net.sourceforge.plantuml.utils.Log;

public class PreprocessorUtils {
Expand Down Expand Up @@ -133,24 +133,24 @@ public static ReadLine getReaderStdlibInclude(StringLocated s, String filename)
}

public static ReadLine getReaderIncludeUrl(final SURL url, StringLocated s, String suf, Charset charset)
throws EaterExceptionLocated {
throws EaterException {
try {
if (StartDiagramExtractReader.containsStartDiagram(url, s, charset))
return StartDiagramExtractReader.build(url, s, suf, charset);

return getReaderInclude(url, s, charset);
} catch (IOException e) {
Logme.error(e);
throw EaterExceptionLocated.located("Cannot open URL " + e.getMessage(), s);
throw new EaterException("Cannot open URL " + e.getMessage(), s);
}

}

public static ReadLine getReaderInclude(SURL url, StringLocated s, Charset charset)
throws EaterExceptionLocated, UnsupportedEncodingException {
throws EaterException, UnsupportedEncodingException {
final InputStream is = url.openStream();
if (is == null)
throw EaterExceptionLocated.located("Cannot open URL", s);
throw new EaterException("Cannot open URL", s);

return ReadLineReader.create(new InputStreamReader(is, charset), url.toString(), s.getLocation());
}
Expand Down
54 changes: 27 additions & 27 deletions src/net/sourceforge/plantuml/tim/Eater.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ public final StringLocated getStringLocated() {
return stringLocated;
}

public abstract void analyze(TContext context, TMemory memory) throws EaterExceptionLocated;
public abstract void analyze(TContext context, TMemory memory) throws EaterException;

public int getCurrentPosition() {
return i;
}

final protected String eatAllToEnd() throws EaterExceptionLocated {
final protected String eatAllToEnd() throws EaterException {
final String result = stringLocated.getString().substring(i);
i = stringLocated.length();
return result;
}

final public TValue eatExpression(TContext context, TMemory memory) throws EaterExceptionLocated {
final public TValue eatExpression(TContext context, TMemory memory) throws EaterException {
final char ch = peekChar();
if (ch == '{' || ch == '[') {
final String data = eatAllToEnd();
Expand All @@ -89,22 +89,22 @@ final public TValue eatExpression(TContext context, TMemory memory) throws Eater
return tokenStack.getResult(getStringLocated(), context, memory);
}

final protected TokenStack eatTokenStack() throws EaterExceptionLocated {
final protected TokenStack eatTokenStack() throws EaterException {
final TokenStack tokenStack = new TokenStack();
addIntoTokenStack(tokenStack, false);
if (tokenStack.size() == 0)
throw EaterExceptionLocated.located("Missing expression", stringLocated);
throw new EaterException("Missing expression", stringLocated);

return tokenStack;
}

final protected TValue eatExpressionStopAtColon(TContext context, TMemory memory) throws EaterExceptionLocated {
final protected TValue eatExpressionStopAtColon(TContext context, TMemory memory) throws EaterException {
final TokenStack tokenStack = new TokenStack();
addIntoTokenStack(tokenStack, true);
return tokenStack.getResult(getStringLocated(), context, memory);
}

final protected void addIntoTokenStack(TokenStack tokenStack, boolean stopAtColon) throws EaterExceptionLocated {
final protected void addIntoTokenStack(TokenStack tokenStack, boolean stopAtColon) throws EaterException {
Token lastToken = null;
while (true) {
final Token token = TokenType.eatOneToken(lastToken, this, stopAtColon);
Expand All @@ -117,10 +117,10 @@ final protected void addIntoTokenStack(TokenStack tokenStack, boolean stopAtColo
}
}

final public String eatAndGetQuotedString() throws EaterExceptionLocated {
final public String eatAndGetQuotedString() throws EaterException {
final char separator = peekChar();
if (TLineType.isQuote(separator) == false)
throw EaterExceptionLocated.located("quote10", stringLocated);
throw new EaterException("quote10", stringLocated);

checkAndEatChar(separator);
final StringBuilder value = new StringBuilder();
Expand All @@ -129,7 +129,7 @@ final public String eatAndGetQuotedString() throws EaterExceptionLocated {
return value.toString();
}

final protected String eatAndGetOptionalQuotedString() throws EaterExceptionLocated {
final protected String eatAndGetOptionalQuotedString() throws EaterException {
final char quote = peekChar();
if (TLineType.isQuote(quote))
return eatAndGetQuotedString();
Expand All @@ -141,7 +141,7 @@ final protected String eatAndGetOptionalQuotedString() throws EaterExceptionLoca
while (true) {
char ch = peekChar();
if (ch == 0)
throw EaterExceptionLocated.located("until001", stringLocated);
throw new EaterException("until001", stringLocated);

if (level == 0 && (ch == ',' || ch == ')'))
return value.toString().trim();
Expand All @@ -159,7 +159,7 @@ else if (ch == ')')
// return value.toString();
}

final public String eatAndGetNumber() throws EaterExceptionLocated {
final public String eatAndGetNumber() throws EaterException {
final StringBuilder result = new StringBuilder();
while (true) {
final char ch = peekChar();
Expand All @@ -175,7 +175,7 @@ final public String eatAndGetNumber() throws EaterExceptionLocated {
}
}

final public String eatAndGetSpaces() throws EaterExceptionLocated {
final public String eatAndGetSpaces() throws EaterException {
final StringBuilder result = new StringBuilder();
while (true) {
final char ch = peekChar();
Expand All @@ -186,19 +186,19 @@ final public String eatAndGetSpaces() throws EaterExceptionLocated {
}
}

final protected String eatAndGetVarname() throws EaterExceptionLocated {
final protected String eatAndGetVarname() throws EaterException {
final StringBuilder varname = new StringBuilder("" + eatOneChar());
if (TLineType.isLetterOrUnderscoreOrDollar(varname.charAt(0)) == false)
throw EaterExceptionLocated.located("a002", stringLocated);
throw new EaterException("a002", stringLocated);

addUpToLastLetterOrUnderscoreOrDigit(varname);
return varname.toString();
}

final protected String eatAndGetFunctionName() throws EaterExceptionLocated {
final protected String eatAndGetFunctionName() throws EaterException {
final StringBuilder varname = new StringBuilder("" + eatOneChar());
if (TLineType.isLetterOrUnderscoreOrDollar(varname.charAt(0)) == false)
throw EaterExceptionLocated.located("a003", stringLocated);
throw new EaterException("a003", stringLocated);

addUpToLastLetterOrUnderscoreOrDigit(varname);
return varname.toString();
Expand Down Expand Up @@ -246,30 +246,30 @@ final public char eatOneChar() {
return ch;
}

final protected void checkAndEatChar(char ch) throws EaterExceptionLocated {
final protected void checkAndEatChar(char ch) throws EaterException {
if (i >= stringLocated.length() || stringLocated.charAt(i) != ch)
throw EaterExceptionLocated.located("a001", stringLocated);
throw new EaterException("a001", stringLocated);

i++;
}

final protected boolean safeCheckAndEatChar(char ch) throws EaterExceptionLocated {
final protected boolean safeCheckAndEatChar(char ch) throws EaterException {
if (i >= stringLocated.length() || stringLocated.charAt(i) != ch)
return false;

i++;
return true;
}

final protected void optionallyEatChar(char ch) throws EaterExceptionLocated {
final protected void optionallyEatChar(char ch) throws EaterException {
if (i >= stringLocated.length() || stringLocated.charAt(i) != ch)
return;

assert stringLocated.charAt(i) == ch;
i++;
}

final protected void checkAndEatChar(String s) throws EaterExceptionLocated {
final protected void checkAndEatChar(String s) throws EaterException {
for (int j = 0; j < s.length(); j++)
checkAndEatChar(s.charAt(j));

Expand Down Expand Up @@ -298,15 +298,15 @@ final protected void addUpTo(char separator, StringBuilder sb) {
}

final protected TFunctionImpl eatDeclareFunction(TContext context, TMemory memory, boolean unquoted,
StringLocated location, boolean allowNoParenthesis, TFunctionType type) throws EaterExceptionLocated {
StringLocated location, boolean allowNoParenthesis, TFunctionType type) throws EaterException {
final List<TFunctionArgument> args = new ArrayList<>();
final String functionName = eatAndGetFunctionName();
skipSpaces();
if (safeCheckAndEatChar('(') == false) {
if (allowNoParenthesis)
return new TFunctionImpl(functionName, args, unquoted, type);

throw EaterExceptionLocated.located("Missing opening parenthesis", stringLocated);
throw new EaterException("Missing opening parenthesis", stringLocated);
}
while (true) {
skipSpaces();
Expand All @@ -331,15 +331,15 @@ final protected TFunctionImpl eatDeclareFunction(TContext context, TMemory memor
checkAndEatChar(")");
break;
} else {
throw EaterExceptionLocated.located("Error in function definition", stringLocated);
throw new EaterException("Error in function definition", stringLocated);
}
}
skipSpaces();
return new TFunctionImpl(functionName, args, unquoted, type);
}

final protected TFunctionImpl eatDeclareReturnFunctionWithOptionalReturn(TContext context, TMemory memory,
boolean unquoted, StringLocated location) throws EaterExceptionLocated {
boolean unquoted, StringLocated location) throws EaterException {
final TFunctionImpl result = eatDeclareFunction(context, memory, unquoted, location, false,
TFunctionType.RETURN_FUNCTION);
if (peekChar() == 'r') {
Expand All @@ -357,7 +357,7 @@ final protected TFunctionImpl eatDeclareReturnFunctionWithOptionalReturn(TContex
}

final protected TFunctionImpl eatDeclareProcedure(TContext context, TMemory memory, boolean unquoted,
StringLocated location) throws EaterExceptionLocated {
StringLocated location) throws EaterException {
final TFunctionImpl result = eatDeclareFunction(context, memory, unquoted, location, false,
TFunctionType.PROCEDURE);
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/net/sourceforge/plantuml/tim/EaterAffectation.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public EaterAffectation(StringLocated sl) {
}

@Override
public void analyze(TContext context, TMemory memory) throws EaterExceptionLocated {
public void analyze(TContext context, TMemory memory) throws EaterException {
skipSpaces();
checkAndEatChar("!");
skipSpaces();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public EaterAffectationDefine(StringLocated s) {
}

@Override
public void analyze(TContext context, TMemory memory) throws EaterExceptionLocated {
public void analyze(TContext context, TMemory memory) throws EaterException {
skipSpaces();
checkAndEatChar("!define");
skipSpaces();
Expand Down
6 changes: 3 additions & 3 deletions src/net/sourceforge/plantuml/tim/EaterAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public EaterAssert(StringLocated s) {
}

@Override
public void analyze(TContext context, TMemory memory) throws EaterExceptionLocated {
public void analyze(TContext context, TMemory memory) throws EaterException {
skipSpaces();
checkAndEatChar("!assert");
skipSpaces();
Expand All @@ -55,9 +55,9 @@ public void analyze(TContext context, TMemory memory) throws EaterExceptionLocat
if (ch == ':') {
checkAndEatChar(':');
final TValue message = eatExpression(context, memory);
throw EaterExceptionLocated.located("Assertion error : " + message.toString(), getStringLocated());
throw new EaterException("Assertion error : " + message.toString(), getStringLocated());
}
throw EaterExceptionLocated.located("Assertion error", getStringLocated());
throw new EaterException("Assertion error", getStringLocated());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public EaterDeclareProcedure(StringLocated s) {
}

@Override
public void analyze(TContext context, TMemory memory) throws EaterExceptionLocated {
public void analyze(TContext context, TMemory memory) throws EaterException {
skipSpaces();
checkAndEatChar("!");
boolean unquoted = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public EaterDeclareReturnFunction(StringLocated s) {
}

@Override
public void analyze(TContext context, TMemory memory) throws EaterExceptionLocated {
public void analyze(TContext context, TMemory memory) throws EaterException {
skipSpaces();
checkAndEatChar("!");
boolean unquoted = false;
Expand Down
2 changes: 1 addition & 1 deletion src/net/sourceforge/plantuml/tim/EaterDumpMemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public EaterDumpMemory(StringLocated s) {
}

@Override
public void analyze(TContext context, TMemory memory) throws EaterExceptionLocated {
public void analyze(TContext context, TMemory memory) throws EaterException {
skipSpaces();
checkAndEatChar("!dump_memory");
skipSpaces();
Expand Down
2 changes: 1 addition & 1 deletion src/net/sourceforge/plantuml/tim/EaterElseIf.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public EaterElseIf(StringLocated s) {
}

@Override
public void analyze(TContext context, TMemory memory) throws EaterExceptionLocated {
public void analyze(TContext context, TMemory memory) throws EaterException {
skipSpaces();
checkAndEatChar("!elseif");
skipSpaces();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,14 @@

import net.sourceforge.plantuml.text.StringLocated;

public class EaterExceptionLocated extends Exception {
public class EaterException extends Exception {

private final String message;
private final StringLocated location;

private EaterExceptionLocated(String message, StringLocated location) {
this.message = message;
this.location = location;
}

public static EaterExceptionLocated located(String message, StringLocated location) {
return new EaterExceptionLocated(message, Objects.requireNonNull(location));
}

public static EaterExceptionLocated unlocated(String message, StringLocated location) {
return new EaterExceptionLocated(message, Objects.requireNonNull(location));
public EaterException(String message, StringLocated location) {
this.message = Objects.requireNonNull(message);
this.location = Objects.requireNonNull(location);
}

public final String getMessage() {
Expand Down
2 changes: 1 addition & 1 deletion src/net/sourceforge/plantuml/tim/EaterForeach.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public EaterForeach(StringLocated s) {
}

@Override
public void analyze(TContext context, TMemory memory) throws EaterExceptionLocated {
public void analyze(TContext context, TMemory memory) throws EaterException {
skipSpaces();
checkAndEatChar("!foreach");
skipSpaces();
Expand Down
Loading

0 comments on commit 3cc6823

Please sign in to comment.