Skip to content

Commit

Permalink
Revert "Attempt to fix error system & various quirks"
Browse files Browse the repository at this point in the history
I accidentally pushed this to master
This reverts commit 6c8cb4d.
  • Loading branch information
Syst3ms committed Mar 27, 2019
1 parent 6c8cb4d commit 98ad5a7
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 128 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/github/syst3ms/skriptparser/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.List;

public class Main {
public static final String CONSOLE_FORMAT = "[%tT] %s: %s%n";
public static final String CONSOLE_FORMAT = "[%tT] %s: %s";
private static SkriptRegistration registration;

public static void main(String[] args) {
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public List<FileElement> parseFileLines(String fileName, List<String> lines, int
} else {
content = line.replace("##", "#").trim();
}
if (content.matches("[\\s#]*")) {
if (content.matches("\\s*")) {
elements.add(new VoidElement(fileName, lastLine + i, expectedIndentation));
continue;
}
Expand All @@ -60,7 +60,7 @@ public List<FileElement> parseFileLines(String fileName, List<String> lines, int
elements.add(new FileSection(fileName, lastLine + i, content.substring(0, content.length() - 1),
sectionElements, expectedIndentation
));
i += count(sectionElements);
i += sectionElements.size();
}
} else {
elements.add(new FileElement(fileName, lastLine + i, content, expectedIndentation));
Expand All @@ -69,15 +69,4 @@ public List<FileElement> parseFileLines(String fileName, List<String> lines, int
return elements;
}

private int count(List<FileElement> elements) {
int count = 0;
for (FileElement element : elements) {
if (element instanceof FileSection) {
count += count(((FileSection) element).getElements()) + 1;
} else {
count++;
}
}
return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public abstract class CodeSection extends Statement {
* @param logger
*/
public void loadSection(FileSection section, SkriptLogger logger) {
logger.startLogHandle();
setItems(ScriptLoader.loadItems(section, logger));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContex

@Override
public void loadSection(FileSection section, SkriptLogger logger) {
logger.startLogHandle();
setItems(event.loadSection(section, logger));
}

Expand Down
93 changes: 26 additions & 67 deletions src/main/java/io/github/syst3ms/skriptparser/log/SkriptLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,27 @@

import io.github.syst3ms.skriptparser.file.FileElement;
import io.github.syst3ms.skriptparser.file.FileSection;
import io.github.syst3ms.skriptparser.util.MultiMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* A class managing Skript's I/O messages.
*/
public class SkriptLogger {
public static final String LOG_FORMAT = "%s (line %d: \"%s\", %s)";
// State

private final boolean debug;
private boolean open = true;
private boolean hasError = false;
private int recursion = 1;
// File

private String fileName;
private List<FileElement> fileElements;
private int line = -1;
// Logs
private MultiMap<Integer, LogEntry> logEntries = new MultiMap<>();
private List<LogEntry> logEntries = new ArrayList<>();
private List<LogEntry> logged = new ArrayList<>();

private boolean open = true;
private boolean hasError = false;
public SkriptLogger(boolean debug) {
this.debug = debug;
}
Expand All @@ -41,34 +37,31 @@ public void setFileInfo(String fileName, List<FileElement> fileElements) {
}

private List<FileElement> flatten(List<FileElement> fileElements) {
List<FileElement> list = new ArrayList<>();
for (FileElement element : fileElements) {
list.add(element);
if (element instanceof FileSection) {
list.addAll(flatten(((FileSection) element).getElements()));
}
}
return list;
return fileElements.stream()
.flatMap(e -> {
if (e instanceof FileSection) {
FileSection sec = (FileSection) e;
return Stream.concat(
Stream.of(e),
flatten(sec.getElements()).stream()
);
} else {
return Stream.of(e);
}
})
.collect(Collectors.toList());
}

public void nextLine() {
line++;
}

public void startLogHandle() {
recursion++;
}

public void closeLogHandle() {
recursion--;
}

private void log(String message, LogType type) {
if (open) {
if (line == -1) {
logEntries.putOne(recursion, new LogEntry(message, type));
logEntries.add(new LogEntry(message, type));
} else {
logEntries.putOne(recursion, new LogEntry(String.format(LOG_FORMAT, message, line + 1, fileElements.get(line).getLineContent(), fileName), type));
logEntries.add(new LogEntry(String.format(LOG_FORMAT, message, line + 1, fileElements.get(line).getLineContent(), fileName), type));
}
}
}
Expand All @@ -95,58 +88,24 @@ public void debug(String message) {
}

public void clearNotError() {
int i = recursion;
while (logEntries.containsKey(i)) {
List<LogEntry> previous = logEntries.remove(i);
logEntries.put(
i,
previous.stream()
.filter(e -> e.getType() == LogType.ERROR || e.getType() == LogType.DEBUG)
.collect(Collectors.toList())
);
i++;
}
this.logEntries = logEntries.stream().filter(e -> e.getType() == LogType.ERROR || e.getType() == LogType.DEBUG).collect(Collectors.toList());
}

public void forgetError() {
public void clearError() {
this.logEntries = logEntries.stream().filter(e -> e.getType() != LogType.ERROR).collect(Collectors.toList());
hasError = false;
}

public void clearLogs() {
int i = recursion;
while (logEntries.containsKey(i)) {
List<LogEntry> previous = logEntries.remove(i);
logEntries.put(
i,
previous.stream()
.filter(e -> e.getType() == LogType.DEBUG)
.collect(Collectors.toList())
);
i++;
}
logEntries.clear();
hasError = false;
}

public void logOutput() {
List<LogEntry> flatView = logEntries.entrySet()
.stream()
.flatMap(e -> e.getValue().stream().sorted((e1, e2) -> e2.getType().ordinal() - e1.getType().ordinal()))
.collect(Collectors.toList());
boolean hasError = false;
for (LogEntry entry : flatView) {
if (entry.getType() != LogType.ERROR || !hasError) {
logged.add(entry);
hasError = entry.getType() == LogType.ERROR;
}
}
logged.addAll(logEntries);
clearLogs();
}

public void logAndClose() {
logOutput();
closeLogHandle();
}

public List<LogEntry> close() {
open = false;
return logged;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public static List<LogEntry> loadScript(File script, boolean debug) {
}
logger.setFileInfo(script.getName(), elements);
for (FileElement element : elements) {
logger.logOutput();
logger.nextLine();
if (element instanceof VoidElement)
continue;
Expand All @@ -69,6 +68,7 @@ public static List<LogEntry> loadScript(File script, boolean debug) {
} else {
logger.error("Can't have code outside of a trigger");
}
logger.logOutput();
}
SkriptAddon.getAddons().forEach(SkriptAddon::finishedLoading);
return logger.close();
Expand All @@ -85,7 +85,6 @@ public static List<Statement> loadItems(FileSection section, SkriptLogger logger
List<FileElement> elements = section.getElements();
for (FileElement element : elements) {
logger.logOutput();
logger.nextLine();
if (element instanceof FileSection) {
FileSection sec = (FileSection) element;
String content = sec.getLineContent();
Expand Down Expand Up @@ -139,7 +138,6 @@ public static List<Statement> loadItems(FileSection section, SkriptLogger logger
for (int i = 0; i + 1 < items.size(); i++) {
items.get(i).setNext(items.get(i + 1));
}
logger.closeLogHandle();
return items;
}

Expand Down
Loading

0 comments on commit 98ad5a7

Please sign in to comment.