-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It's a bit shaky
- Loading branch information
Showing
8 changed files
with
203 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package ppke.itk.xplang.common; | ||
|
||
/** | ||
* A compiler error we would like to display to the programmer. | ||
*/ | ||
public final class CompilerMessage { | ||
public enum Severity { | ||
WARNING, | ||
ERROR; | ||
} | ||
|
||
public static CompilerMessage error(String message, Location location) { | ||
return new CompilerMessage(Severity.ERROR, message, location); | ||
} | ||
|
||
public static CompilerMessage warning(String message, Location location) { | ||
return new CompilerMessage(Severity.WARNING, message, location); | ||
} | ||
|
||
private final Severity severity; | ||
private final String message; | ||
private final Location location; | ||
|
||
private CompilerMessage(Severity severity, String message, Location location) { | ||
this.severity = severity; | ||
this.message = message; | ||
this.location = location; | ||
} | ||
|
||
public Severity getSeverity() { | ||
return severity; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public Location getLocation() { | ||
return location; | ||
} | ||
|
||
@Override public String toString() { | ||
return String.format("%s %s", location, message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ppke.itk.xplang.common; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ErrorLog { | ||
private final List<CompilerMessage> messages = new ArrayList<>(); | ||
|
||
public void add(CompilerMessage error) { | ||
messages.add(error); | ||
} | ||
|
||
public List<CompilerMessage> getErrorMessages() { | ||
return Collections.unmodifiableList(messages); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return messages.isEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/java/ppke/itk/xplang/parser/ParserErrorLoggingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package ppke.itk.xplang.parser; | ||
|
||
import org.junit.Test; | ||
import ppke.itk.xplang.ast.Root; | ||
import ppke.itk.xplang.common.CompilerMessage; | ||
import ppke.itk.xplang.common.Location; | ||
|
||
import java.io.Reader; | ||
import java.io.StringReader; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
public class ParserErrorLoggingTest { | ||
private final static Symbol SYMBOL_FSR = new Symbol("FUNNY_STARE_RIGHT", Pattern.compile(">_>")); | ||
private final static Symbol SYMBOL_FSL = new Symbol("FUNNY_STARE_LEFT", Pattern.compile("<_<")); | ||
private final static Symbol SYMBOL_WS = new Symbol("WS", Pattern.compile("\\s+"), Symbol.Precedence.DEFAULT, false); | ||
private final static Grammar grammar = new Grammar() { | ||
@Override protected void setup(Context ctx) { | ||
ctx.register(SYMBOL_FSR); | ||
ctx.register(SYMBOL_FSL); | ||
ctx.register(SYMBOL_WS); | ||
} | ||
@Override protected Root S(Parser parser) throws ParseError { | ||
int iteration = 0; | ||
while(!parser.actual().symbol().equals(Symbol.EOF)) { | ||
parser.accept(iteration %2 == 0? SYMBOL_FSL : SYMBOL_FSR, "Expected %s, got %s"); | ||
iteration++; | ||
} | ||
return null; | ||
} | ||
}; | ||
|
||
@Test | ||
public void parserShouldLogLexerError() { | ||
Reader source = new StringReader("<_< öö >_>"); | ||
Parser parser = new Parser(); | ||
parser.parse(source, grammar); | ||
|
||
List<CompilerMessage> messages = parser.getErrorLog().getErrorMessages(); | ||
assertFalse("Error log should not be empty after a lexing error.", messages.isEmpty()); | ||
assertEquals("Error log should give correct information about the error location.", | ||
new Location(1,4), | ||
messages.get(0).getLocation() | ||
); | ||
} | ||
|
||
@Test | ||
public void parserShouldLogSyntaxError() { | ||
Reader source = new StringReader("<_< >_> >_> >_>"); | ||
Parser parser = new Parser(); | ||
parser.parse(source, grammar); | ||
|
||
List<CompilerMessage> messages = parser.getErrorLog().getErrorMessages(); | ||
System.out.println(messages); | ||
assertFalse("Error log should not be empty after a syntax error.", parser.getErrorLog().isEmpty()); | ||
assertEquals("Error log should give correct information about the error location.", | ||
new Location(1,8), | ||
messages.get(0).getLocation() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters