Skip to content

Commit

Permalink
track line numbers
Browse files Browse the repository at this point in the history
hacky as hell
  • Loading branch information
mcmonkey4eva committed Jul 23, 2019
1 parent bdc004e commit bcbff9b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.denizenscript.denizencore.objects.core;

import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.scripts.ScriptBuilder;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
Expand Down Expand Up @@ -299,19 +300,8 @@ public String run(Attribute attribute, ObjectTag object) {
registerTag("relative_filename", new TagRunnable() {
@Override
public String run(Attribute attribute, ObjectTag object) {
try {
String fn = ((ScriptTag) object).container.getFileName().replace(DenizenCore.getImplementation()
.getScriptFolder().getParentFile().getCanonicalPath(), "").replace("\\", "/");
while (fn.startsWith("/")) {
fn = fn.substring(1);
}
return new ElementTag(fn)
.getAttribute(attribute.fulfill(1));
}
catch (Exception e) {
Debug.echoError(e);
}
return null;
return new ElementTag(((ScriptTag) object).container.getRelativeFileName())
.getAttribute(attribute.fulfill(1));
}
});

Expand Down Expand Up @@ -428,7 +418,7 @@ public String run(Attribute attribute, ObjectTag object) {
if (each == null) {
each = "null";
}
list.add(each.toString());
list.add(ScriptBuilder.stripLinePrefix(each.toString()));
}
return list.getAttribute(attribute.fulfill(1));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public static List<ScriptEntry> addObjectToEntries(List<ScriptEntry> scriptEntry
return scriptEntryList;
}

public static char LINE_PREFIX_CHAR = '^'; // This would be an invisible special character... if SnakeYAML allowed them!

public static String stripLinePrefix(String rawLine) {
if (!rawLine.startsWith(String.valueOf(LINE_PREFIX_CHAR))) {
return rawLine;
}
int infoEnd = rawLine.indexOf(LINE_PREFIX_CHAR, 1);
return rawLine.substring(infoEnd + 2); // Skip the symbol and the space after.
}

/*
* Builds ScriptEntry(ies) of items read from a script
*/
Expand Down Expand Up @@ -64,26 +74,24 @@ public static List<ScriptEntry> buildScriptEntries(List<Object> contents, Script
inside = null;
}


String[] scriptEntry;
String[] splitEntry = entry.split(" ", 2);

if (splitEntry.length == 1) {
scriptEntry = new String[2];
scriptEntry[0] = entry;
scriptEntry[1] = null;
}
else {
scriptEntry = splitEntry;
int lineNum = 1;
if (entry.startsWith(String.valueOf(LINE_PREFIX_CHAR))) {
int infoEnd = entry.indexOf(LINE_PREFIX_CHAR, 1);
String lineNumStr = entry.substring(1, infoEnd);
entry = entry.substring(infoEnd + 2); // Skip the symbol and the space after.
lineNum = Integer.valueOf(lineNumStr);
}

String[] scriptEntry = entry.split(" ", 2);

try {
/* Build new script commands */
String[] args = ArgumentHelper.buildArgs(scriptEntry[1]);
String[] args = scriptEntry.length > 1 ? ArgumentHelper.buildArgs(scriptEntry[1]) : null;
if (Debug.showScriptBuilder) {
Debug.echoDebug(parent, "Adding '" + scriptEntry[0] + "' Args: " + Arrays.toString(args));
}
ScriptEntry newEntry = new ScriptEntry(scriptEntry[0], args, parent, inside);
newEntry.internal.lineNumber = lineNum;
newEntry.internal.originalLine = entry;
newEntry.entryData.transferDataFrom(data);
scriptCommands.add(newEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public static class ScriptEntryInternal {
public Object specialProcessedData = null;

public String originalLine = null;

public int lineNumber;
}

public static class InternalArgument {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,31 @@ public static String getOriginalName(String script) {
public static String ClearComments(String filename, String input, boolean trackSources) {
StringBuilder result = new StringBuilder(input.length());
String[] lines = input.replace("\t", " ").replace("\r", "").split("\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i].trim();
String trimStart = lines[i].replaceAll("^[\\s\\t]+", "");
if (trackSources && !line.startsWith("#") && trimStart.length() == lines[i].length() && line.endsWith(":") && line.length() > 1) {
String name = line.substring(0, line.length() - 1).replace('\"', '\'').replace("'", "");
for (int lineNum = 0; lineNum < lines.length; lineNum++) {
String trimmedLine = lines[lineNum].trim();
String trimStart = lines[lineNum].replaceAll("^[\\s]+", "");
if (trackSources && !trimmedLine.startsWith("#") && trimStart.length() == lines[lineNum].length() && trimmedLine.endsWith(":") && trimmedLine.length() > 1) {
String name = trimmedLine.substring(0, trimmedLine.length() - 1).replace('\"', '\'').replace("'", "");
scriptSources.put(name.toUpperCase(), filename);
scriptOriginalNames.put(name.toUpperCase(), name);
result.append(name.toUpperCase() + ":\n");
}
else if (!line.startsWith("#")) {
if ((line.startsWith("}") || line.startsWith("{") || line.startsWith("else")) && !line.endsWith(":")) {
result.append(' ').append(lines[i].replace('\0', ' ')
else if (!trimmedLine.startsWith("#")) {
if ((trimmedLine.startsWith("}") || trimmedLine.startsWith("{") || trimmedLine.startsWith("else")) && !trimmedLine.endsWith(":")) {
result.append(' ').append(lines[lineNum].replace('\0', ' ')
.replace(": ", "<&co>").replace("#", "<&ns>")).append("\n");
}
else {
String liner = lines[i].replace('\0', ' ');
if (!line.endsWith(":") && line.startsWith("-")) {
liner = liner.replace(": ", "<&co> ");
liner = liner.replace("#", "<&ns>");
String curLine = lines[lineNum].replace('\0', ' ');
if (!trimmedLine.endsWith(":") && trimmedLine.startsWith("-")) {
curLine = curLine.replace(": ", "<&co> ");
curLine = curLine.replace("#", "<&ns>");
}
result.append(liner.replace('\0', ' ')).append("\n");
if (trimmedLine.startsWith("- ")) {
int dashIndex = curLine.indexOf('-');
curLine = curLine.substring(0, dashIndex + 1) + " " + ScriptBuilder.LINE_PREFIX_CHAR + (lineNum + 1) + ScriptBuilder.LINE_PREFIX_CHAR + curLine.substring(dashIndex + 1);
}
result.append(curLine).append("\n");
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public static boolean handleDefs(ScriptEntry scriptEntry, boolean genned) {
public boolean execute(ScriptEntry scriptEntry) {
if (scriptEntry.dbCallShouldDebug()) {
StringBuilder output = new StringBuilder();
output.append("<G>(line ").append(scriptEntry.internal.lineNumber).append(")<W> ");
output.append(scriptEntry.getCommandName());
if (scriptEntry.getOriginalArguments() == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Original Arguments null for " + scriptEntry.getCommandName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.objects.core.ScriptTag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -124,6 +125,20 @@ public String getFileName() {
return ScriptHelper.getSource(getName());
}

public String getRelativeFileName() {
try {
String fn = getFileName().replace(DenizenCore.getImplementation().getScriptFolder().getParentFile().getCanonicalPath(), "");
while (fn.startsWith("/")) {
fn = fn.substring(1);
}
return fn;
}
catch (Exception e) {
Debug.echoError(e);
return getFileName();
}
}

public String getOriginalName() {
return ScriptHelper.getOriginalName(getName());
}
Expand Down Expand Up @@ -190,7 +205,15 @@ public String getString(String path, String def) {


public List<String> getStringList(String path) {
return contents.getStringList(path.toUpperCase());
List<String> strs = contents.getStringList(path.toUpperCase());
if (strs == null) {
return null;
}
ArrayList<String> output = new ArrayList<>(strs.size());
for (String str : strs) {
output.add(ScriptBuilder.stripLinePrefix(str));
}
return output;
}


Expand Down

0 comments on commit bcbff9b

Please sign in to comment.