Attach character + position to some errors

This commit is contained in:
Arne Keller 2021-03-06 20:39:10 +01:00
parent a176d299ce
commit 251d7a3fc2
6 changed files with 50 additions and 20 deletions

View File

@ -29,11 +29,6 @@
bottom: 2.1em;
}
.help-field {
justify-content: center;
align-items: center;
}
kbd {
background-color: #eee;
border-radius: 3px;
@ -46,4 +41,4 @@ kbd {
line-height: 1;
padding: 2px 4px;
white-space: nowrap;
}
}

View File

@ -82,15 +82,20 @@ public class LambdaLexer {
char c = term.charAt(pos);
switch (c) {
case '-':
if (pos + 1 < term.length() && term.charAt(pos + 1) == '>') {
t = new Token(TokenType.ARROW, "->", pos);
advance();
advance();
return new Result<>(t);
if (pos + 1 < term.length()) {
if (term.charAt(pos + 1) == '>') {
t = new Token(TokenType.ARROW, "->", pos);
advance();
advance();
return new Result<>(t);
} else {
return new Result<>(null, ParseError.UNEXPECTED_CHARACTER
.withCharacter(term.charAt(pos + 1), pos + 1));
}
} else {
return new Result<>(null, ParseError.UNEXPECTED_CHARACTER);
return new Result<>(null, ParseError.TOO_FEW_TOKENS); // actually too few *characters* ..
}
// bunch of single-character tokens
// bunch of single-character tokens
case '.':
t = new Token(TokenType.DOT, ".", pos);
advance();
@ -124,7 +129,8 @@ public class LambdaLexer {
} while (pos < term.length() && Character.isLetterOrDigit(term.charAt(pos))
&& (int) term.charAt(pos) < 128);
if (pos < term.length() && (int) term.charAt(pos) >= 128) {
return new Result<>(null, ParseError.UNEXPECTED_CHARACTER);
return new Result<>(null, ParseError.UNEXPECTED_CHARACTER
.withCharacter(term.charAt(pos), pos));
}
String s = sb.toString();
TokenType type;
@ -156,7 +162,7 @@ public class LambdaLexer {
} while (pos < term.length() && Character.isDigit(term.charAt(pos)));
return new Result<>(new Token(TokenType.NUMBER, sb.toString(), startPos));
} else {
return new Result<>(null, ParseError.UNEXPECTED_CHARACTER);
return new Result<>(null, ParseError.UNEXPECTED_CHARACTER.withCharacter(c, pos));
}
}
}

View File

@ -24,6 +24,8 @@ public enum ParseError {
UNEXPECTED_CHARACTER;
private Token cause = new Token(Token.TokenType.EOF, "", -1);
private char wrongChar = '\0';
private int position = -1;
/**
* Attach a token to this error.
@ -36,6 +38,19 @@ public enum ParseError {
return this;
}
/**
* Attach a character and position to this error.
*
* @param cause the character
* @param position it's position
* @return this object
*/
public ParseError withCharacter(char cause, int position) {
this.wrongChar = cause;
this.position = position;
return this;
}
/**
* @return the token associated with this error, or null if none
*/
@ -43,6 +58,14 @@ public enum ParseError {
return cause;
}
public char getWrongCharacter() {
return wrongChar;
}
public int getPosition() {
return position;
}
ParseError() {
}

View File

@ -46,6 +46,14 @@ public class ErrorView extends VerticalLayout implements LocaleChangeObserver {
if (error == ParseError.TOO_FEW_TOKENS) {
additionalInformation.add(new Span(getTranslation("root.tooFewTokensHelp")));
} else if (error == ParseError.UNEXPECTED_CHARACTER) {
char c = error.getWrongCharacter();
if (c != '\0') {
additionalInformation.add(new Span(getTranslation("root.wrongCharacter") + c));
additionalInformation.add(new Span(getTranslation("root.position") + error.getPosition()));
} else {
return summary;
}
} else {
if (error.getCause().getPos() == NO_ADDITIONAL_INFO) {
return summary;

View File

@ -15,8 +15,6 @@ import com.vaadin.flow.i18n.LocaleChangeObserver;
* These panels are used for the Accordion in the {@link edu.kit.typicalc.view.main.HelpDialog}.
*/
public class HelpContentField extends AccordionPanel implements LocaleChangeObserver {
private static final String CLASSNAME = "help-field";
private static final long serialVersionUID = -2793005857762897734L;
private final String summaryKey;

View File

@ -75,10 +75,10 @@ root.helpShareButton=Durch Benutzen des Teilen-Knopfs öffnet sich ein Dialog, i
Typherleitungsbaums des eingegebenen Terms und die benötigen Pakete zum Einbinden des LaTeX-Codes angezeigt werden. \
Zusätzlich dazu enthält der Dialog einen Permalink zur aktuellen Seite, der sowohl den Term als auch die Typannahmen \
kodiert.
root.TOO_FEW_TOKENS=Falsche Eingabe! Der Term endet abrupt.
root.TOO_FEW_TOKENS=Falsche Eingabe! Die Eingabe endet abrupt.
root.tooFewTokensHelp=Überprüfe, ob alle Let-, Abs- und App-Terme über die nötigen Argumente verfügen.
root.UNEXPECTED_TOKEN=Der Term entspricht nicht der im Info-Dialog spezifizierten Syntax!
root.UNEXPECTED_CHARACTER=Der Term enhält ein Zeichen, welches an dieser Stelle nicht erlaubt ist!
root.UNEXPECTED_TOKEN=Die Eingabe entspricht nicht der im Info-Dialog spezifizierten Syntax!
root.UNEXPECTED_CHARACTER=Die Eingabe enhält ein Zeichen, welches an dieser Stelle nicht erlaubt ist!
error.heading=Syntaktisch falsche Eingabe!
root.wrongCharacter=Falsches Zeichen: \u0020
root.position=An Position: \u0020