Skip to content

Commit

Permalink
smarter tag syntax error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 27, 2021
1 parent 9109834 commit b1116c1
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 34 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.denizenscript.denizencore.exceptions;

public class TagProcessingException extends Exception {

private static final long serialVersionUID = 3159108944857792068L;
public String message;

public TagProcessingException(String msg) {
message = msg;
}

@Override
public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.denizenscript.denizencore.objects.core;

import com.denizenscript.denizencore.exceptions.TagProcessingException;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.Fetchable;
import com.denizenscript.denizencore.objects.ObjectFetcher;
Expand Down Expand Up @@ -1632,6 +1633,14 @@ else if (input.size() > 1) {
if (attribute.hasContext(1)) {
tag = attribute.getRawContext(1);
}
Attribute subAttribute;
try {
subAttribute = tag == null ? null : new Attribute(tag, attribute.getScriptEntry(), attribute.context);
}
catch (TagProcessingException ex) {
attribute.echoError("Tag processing failed: " + ex.getMessage());
return null;
}

// <--[tag]
// @attribute <ListTag.lowest[(<tag>)].count[<#>]>
Expand All @@ -1651,7 +1660,7 @@ else if (input.size() > 1) {
for (int i = 0; i < object.size(); i++) {
ObjectTag obj = object.getObject(i);
if (tag != null) {
obj = CoreUtilities.autoAttribTyped(obj, new Attribute(tag, attribute.getScriptEntry(), attribute.context));
obj = CoreUtilities.autoAttribTyped(obj, new Attribute(subAttribute, attribute.getScriptEntry(), attribute.context));
}
String str = obj.toString();
if (ArgumentHelper.matchesDouble(str)) {
Expand Down Expand Up @@ -1682,7 +1691,7 @@ else if (input.size() > 1) {
for (ObjectTag obj : object.objectForms) {
ObjectTag actualObj = obj;
if (tag != null) {
obj = CoreUtilities.autoAttribTyped(obj, new Attribute(tag, attribute.getScriptEntry(), attribute.context));
obj = CoreUtilities.autoAttribTyped(obj, new Attribute(subAttribute, attribute.getScriptEntry(), attribute.context));
}
String str = obj.toString();
if (ArgumentHelper.matchesDouble(str)) {
Expand Down Expand Up @@ -1710,6 +1719,14 @@ else if (input.size() > 1) {
if (attribute.hasContext(1)) {
tag = attribute.getRawContext(1);
}
Attribute subAttribute;
try {
subAttribute = tag == null ? null : new Attribute(tag, attribute.getScriptEntry(), attribute.context);
}
catch (TagProcessingException ex) {
attribute.echoError("Tag processing failed: " + ex.getMessage());
return null;
}

// <--[tag]
// @attribute <ListTag.highest[(<tag>)].count[<#>]>
Expand All @@ -1729,7 +1746,7 @@ else if (input.size() > 1) {
for (int i = 0; i < object.size(); i++) {
ObjectTag obj = object.getObject(i);
if (tag != null) {
obj = CoreUtilities.autoAttribTyped(obj, new Attribute(tag, attribute.getScriptEntry(), attribute.context));
obj = CoreUtilities.autoAttribTyped(obj, new Attribute(subAttribute, attribute.getScriptEntry(), attribute.context));
}
String str = obj.toString();
if (ArgumentHelper.matchesDouble(str)) {
Expand Down Expand Up @@ -1760,7 +1777,7 @@ else if (input.size() > 1) {
for (ObjectTag obj : object.objectForms) {
ObjectTag actualObj = obj;
if (tag != null) {
obj = CoreUtilities.autoAttribTyped(obj, new Attribute(tag, attribute.getScriptEntry(), attribute.context));
obj = CoreUtilities.autoAttribTyped(obj, new Attribute(subAttribute, attribute.getScriptEntry(), attribute.context));
}
String str = obj.toString();
if (ArgumentHelper.matchesDouble(str)) {
Expand Down Expand Up @@ -1840,10 +1857,18 @@ else if (value > 0) {
ListTag newlist = new ListTag(object);
final NaturalOrderComparator comparator = new NaturalOrderComparator();
final String tag = attribute.getRawContext(1);
Attribute subAttribute;
try {
subAttribute = new Attribute(tag, attribute.getScriptEntry(), attribute.context);
}
catch (TagProcessingException ex) {
attribute.echoError("Tag processing failed: " + ex.getMessage());
return null;
}
try {
newlist.objectForms.sort((o1, o2) -> {
ObjectTag or1 = CoreUtilities.autoAttribTyped(o1, new Attribute(tag, attribute.getScriptEntry(), attribute.context));
ObjectTag or2 = CoreUtilities.autoAttribTyped(o2, new Attribute(tag, attribute.getScriptEntry(), attribute.context));
ObjectTag or1 = CoreUtilities.autoAttribTyped(o1, new Attribute(subAttribute, attribute.getScriptEntry(), attribute.context));
ObjectTag or2 = CoreUtilities.autoAttribTyped(o2, new Attribute(subAttribute, attribute.getScriptEntry(), attribute.context));
return comparator.compare(or1, or2);
});
return new ListTag(newlist.objectForms);
Expand All @@ -1870,10 +1895,18 @@ else if (value > 0) {
}
ListTag newlist = new ListTag(object);
final String tag = attribute.getRawContext(1);
Attribute subAttribute;
try {
subAttribute = new Attribute(tag, attribute.getScriptEntry(), attribute.context);
}
catch (TagProcessingException ex) {
attribute.echoError("Tag processing failed: " + ex.getMessage());
return null;
}
try {
newlist.objectForms.sort((o1, o2) -> {
ObjectTag or1 = CoreUtilities.autoAttribTyped(o1, new Attribute(tag, attribute.getScriptEntry(), attribute.context));
ObjectTag or2 = CoreUtilities.autoAttribTyped(o2, new Attribute(tag, attribute.getScriptEntry(), attribute.context));
ObjectTag or1 = CoreUtilities.autoAttribTyped(o1, new Attribute(subAttribute, attribute.getScriptEntry(), attribute.context));
ObjectTag or2 = CoreUtilities.autoAttribTyped(o2, new Attribute(subAttribute, attribute.getScriptEntry(), attribute.context));
try {
double r1 = Double.parseDouble(or1.toString());
double r2 = Double.parseDouble(or2.toString());
Expand Down Expand Up @@ -1990,10 +2023,18 @@ else if (value > 0) {
if (defaultValue) {
tag = tag.substring(0, tag.length() - "||true".length());
}
Attribute subAttribute;
try {
subAttribute = new Attribute(tag, attribute.getScriptEntry(), attribute.context);
}
catch (TagProcessingException ex) {
attribute.echoError("Tag processing failed: " + ex.getMessage());
return null;
}
ListTag newlist = new ListTag();
try {
for (ObjectTag obj : object.objectForms) {
Attribute tempAttrib = new Attribute(tag, attribute.getScriptEntry(), attribute.context);
Attribute tempAttrib = new Attribute(subAttribute, attribute.getScriptEntry(), attribute.context);
tempAttrib.setHadAlternative(true);
ObjectTag objs = CoreUtilities.autoAttribTyped(obj, tempAttrib);
if ((objs == null) ? defaultValue : CoreUtilities.equalsIgnoreCase(objs.toString(), "true")) {
Expand Down Expand Up @@ -2039,9 +2080,17 @@ else if (marks == 0 && c == '|' && tag.charAt(i + 1) == '|') {
}
}
}
Attribute subAttribute;
try {
subAttribute = new Attribute(tag, attribute.getScriptEntry(), attribute.context);
}
catch (TagProcessingException ex) {
attribute.echoError("Tag processing failed: " + ex.getMessage());
return null;
}
try {
for (ObjectTag obj : object.objectForms) {
Attribute tempAttrib = new Attribute(tag, attribute.getScriptEntry(), attribute.context);
Attribute tempAttrib = new Attribute(subAttribute, attribute.getScriptEntry(), attribute.context);
tempAttrib.setHadAlternative(attribute.hasAlternative() || fallback);
ObjectTag objs = CoreUtilities.autoAttribTyped(obj, tempAttrib);
if (objs == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.denizenscript.denizencore.objects.core;

import com.denizenscript.denizencore.exceptions.TagProcessingException;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
Expand Down Expand Up @@ -276,13 +277,21 @@ public static void registerTags() {
ArrayList<Map.Entry<StringHolder, ObjectTag>> entryList = new ArrayList<>(object.map.entrySet());
final NaturalOrderComparator comparator = new NaturalOrderComparator();
final String tag = attribute.hasContext(1) ? attribute.getRawContext(1) : null;
Attribute subAttribute;
try {
subAttribute = tag == null ? null : new Attribute(tag, attribute.getScriptEntry(), attribute.context);
}
catch (TagProcessingException ex) {
attribute.echoError("Tag processing failed: " + ex.getMessage());
return null;
}
try {
entryList.sort((e1, e2) -> {
ObjectTag o1 = e1.getValue();
ObjectTag o2 = e2.getValue();
if (tag != null) {
o1 = CoreUtilities.autoAttribTyped(o1, new Attribute(tag, attribute.getScriptEntry(), attribute.context));
o2 = CoreUtilities.autoAttribTyped(o2, new Attribute(tag, attribute.getScriptEntry(), attribute.context));
o1 = CoreUtilities.autoAttribTyped(o1, new Attribute(subAttribute, attribute.getScriptEntry(), attribute.context));
o2 = CoreUtilities.autoAttribTyped(o2, new Attribute(subAttribute, attribute.getScriptEntry(), attribute.context));
}
return comparator.compare(o1, o2);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static List<ScriptEntry> buildScriptEntries(List<Object> contents, Script
scriptCommands.add(newEntry);
}
catch (Exception e) {
Debug.echoError("Exception while building script '" + parent.getName() + "'...");
Debug.echoError("Exception while building script '" + (parent == null ? "(null)" : parent.getName()) + "'...");
Debug.echoError(e);
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/denizenscript/denizencore/tags/Attribute.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.denizenscript.denizencore.tags;

import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.exceptions.TagProcessingException;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
Expand Down Expand Up @@ -47,11 +49,14 @@ private static boolean isNumber(char c) {
return c >= '0' && c <= '9';
}

private static AttributeComponent[] separate_attributes(String attributes) {
private static AttributeComponent[] separate_attributes(String attributes) throws TagProcessingException {
AttributeComponent[] matchesRes = attribsLookup.get(attributes);
if (matchesRes != null) {
return matchesRes;
}
if (attributes.startsWith(".") || attributes.endsWith(".")) {
throw new TagProcessingException("The tag '" + attributes + "' is invalid due to a misplaced dot at the start or end of the tag.");
}
ArrayList<AttributeComponent> matches = new ArrayList<>(attributes.length() / 7);
int x1 = 0, x2 = -1;
int braced = 0;
Expand All @@ -73,11 +78,17 @@ else if (chr == '.' && !(x > 0 && isNumber(attrInp[x + 1]) && isNumber(attrInp[x
x2 = x;
}
if (x2 > -1) {
if (x2 <= x1) {
throw new TagProcessingException("The tag '" + attributes + "' is invalid, likely due to double dots '..' somewhere. Did you forget a sub-tag, or accidentally double-tap the dot key?");
}
matches.add(new AttributeComponent(attributes.substring(x1, x2)));
x2 = -1;
x1 = x + 1;
}
}
if (braced != 0) {
throw new TagProcessingException("The tag '" + attributes + "' is invalid due to misplaced [square brackets]. Did you forget to close some brackets?");
}
if (Debug.verbose) {
Debug.log("attribute splitter: '" + attributes + "' becomes: " + matches);
}
Expand Down Expand Up @@ -128,7 +139,7 @@ public Attribute(Attribute ref, ScriptEntry scriptEntry, TagContext context) {
}
}

public Attribute(String attributes, ScriptEntry scriptEntry, TagContext context) {
public Attribute(String attributes, ScriptEntry scriptEntry, TagContext context) throws TagProcessingException {
origin = attributes;
this.scriptEntry = scriptEntry;
this.context = context;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.denizenscript.denizencore.tags;

import com.denizenscript.denizencore.exceptions.TagProcessingException;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ScriptTag;
Expand Down Expand Up @@ -70,7 +71,7 @@ public ReplaceableTagEvent(ReferenceData ref, String tag, TagContext context) {
}
}

public ReplaceableTagEvent(String tag, TagContext context) {
public ReplaceableTagEvent(String tag, TagContext context) throws TagProcessingException {
this(refs.get(tag), tag, context);
if (mainRef != null) {
return;
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/com/denizenscript/denizencore/tags/TagManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.denizenscript.denizencore.tags;

import com.denizenscript.denizencore.exceptions.TagProcessingException;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
Expand Down Expand Up @@ -156,8 +157,14 @@ public static void executeWithTimeLimit(final ReplaceableTagEvent event, int sec
}

public static String readSingleTag(String str, TagContext context) {
ReplaceableTagEvent event = new ReplaceableTagEvent(str, context);
return readSingleTagObject(context, event).toString();
try {
ReplaceableTagEvent event = new ReplaceableTagEvent(str, context);
return readSingleTagObject(context, event).toString();
}
catch (TagProcessingException ex) {
Debug.echoError("Tag processing failed: " + ex.getMessage());
return null;
}
}

public static ObjectTag readSingleTagObject(ParseableTagPiece tag, TagContext context) {
Expand Down Expand Up @@ -342,10 +349,19 @@ public static List<ParseableTagPiece> genChain(String arg, TagContext context) {
ParseableTagPiece midTag = new ParseableTagPiece();
midTag.content = tagToProc;
midTag.isTag = true;
midTag.tagData = new ReplaceableTagEvent(tagToProc, context).mainRef;
pieces.add(midTag);
if (Debug.verbose) {
Debug.log("Tag: " + (preText == null ? "<null>" : preText.content) + " ||| " + midTag.content);
try {
midTag.tagData = new ReplaceableTagEvent(tagToProc, context).mainRef;
pieces.add(midTag);
if (Debug.verbose) {
Debug.log("Tag: " + (preText == null ? "<null>" : preText.content) + " ||| " + midTag.content);
}
}
catch (TagProcessingException ex) {
Debug.echoError("Tag processing failed: " + ex.getMessage());
ParseableTagPiece errorNote = new ParseableTagPiece();
errorNote.isError = true;
errorNote.content = "Tag processing failed: " + ex.getMessage();
pieces.add(errorNote);
}
arg = arg.substring(positions[1] + 1);
locateTag(arg, positions, 0);
Expand Down

0 comments on commit b1116c1

Please sign in to comment.