diff --git a/SanityCheck.jar b/SanityCheck.jar index bebdf1da3e6277f966e8fff78ea96d884fe27b0b..36c886155cf61858bb6e54466cb88efc05f615cd 100644 Binary files a/SanityCheck.jar and b/SanityCheck.jar differ diff --git a/devTools/javaSanityCheck/src/Main.java b/devTools/javaSanityCheck/src/Main.java index eea3a4ff611203b0025cd29ce1fe431447f80e1c..87b18682d2391fc678a383ccc4b47480278c0473 100644 --- a/devTools/javaSanityCheck/src/Main.java +++ b/devTools/javaSanityCheck/src/Main.java @@ -12,6 +12,9 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; +/** + * @author Arkerthan + */ public class Main { public static TagSearchTree<Tag> htmlTags, twineTags; @@ -22,6 +25,7 @@ public class Main { private static String[] excluded; public static void main(String[] args) { + //setup setupExclude(); setupHtmlTags(); @@ -31,7 +35,7 @@ public class Main { //actual sanityCheck runSanityCheckInDirectory(workingDir, new File("src/")); - //handle errors + //output errors for (SyntaxError e : errors) { System.out.println(e.getError()); @@ -41,7 +45,7 @@ public class Main { /** * Goes through the whole directory including subdirectories and runs - * sanityCheck() on all .tw files + * {@link Main#sanityCheck(Path)} on all .tw files * * @param dir to be checked */ @@ -61,7 +65,7 @@ public class Main { } } catch (NullPointerException e) { e.printStackTrace(); - System.err.println("Couldn't find directory " + currentFile); + System.err.println("Couldn't read directory " + currentFile); System.exit(-1); } } @@ -78,42 +82,33 @@ public class Main { Charset encoding = Charset.defaultCharset(); if (!excluded(file.getPath())) { - try { - currentFile = file.getPath(); - currentLine = 1; - stack = new Stack<>(); - - //actually opening and reading the file - try (InputStream in = new FileInputStream(file); - Reader reader = new InputStreamReader(in, encoding); - // buffer for efficiency - Reader buffer = new BufferedReader(reader)) { - handleCharacters(buffer); - } + currentFile = file.getPath(); + currentLine = 1; + stack = new Stack<>(); + + //actually opening and reading the file + try (InputStream in = new FileInputStream(file); + Reader reader = new InputStreamReader(in, encoding); + // buffer for efficiency + Reader buffer = new BufferedReader(reader)) { + handleCharacters(buffer); } catch (IOException e) { + e.printStackTrace(); System.err.println("Couldn't read " + file); } } } /** - * sets up the alphabetical search tree for fast access of HTML tags later + * sets up a {@link TagSearchTree<Tag>} for fast access of HTML tags later */ private static void setupHtmlTags() { //load HTML tags into a list - List<Tag> TagsList = new LinkedList<>(); - try { - - Files.lines(new File("devTools/javaSanityCheck/htmlTags").toPath()).map(String::trim) - .filter(s -> !s.startsWith("#")) - .forEach(s -> TagsList.add(parseTag(s))); - } catch (IOException e) { - System.err.println("Couldn't read devTools/javaSanityCheck/htmlTags"); - } + List<Tag> TagsList = loadTags("devTools/javaSanityCheck/htmlTags"); //turn List into alphabetical search tree try { - htmlTags = new TagSearchTree(TagsList); + htmlTags = new TagSearchTree<>(TagsList); } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Illegal Character in devTools/javaSanityCheck/htmlTags"); System.exit(-1); @@ -121,29 +116,39 @@ public class Main { } /** - * sets up the alphabetical search tree for fast access of twine tags later + * sets up a {@link TagSearchTree<Tag>} for fast access of twine tags later */ private static void setupTwineTags() { //load twine tags into a list - List<Tag> TagsList = new LinkedList<>(); - try { - - Files.lines(new File("devTools/javaSanityCheck/twineTags").toPath()).map(String::trim) - .filter(s -> !s.startsWith("#")) - .forEach(s -> TagsList.add(parseTag(s))); - } catch (IOException e) { - System.err.println("Couldn't read devTools/javaSanityCheck/twineTags"); - } + List tagsList = loadTags("devTools/javaSanityCheck/twineTags"); //turn List into alphabetical search tree try { - twineTags = new TagSearchTree(TagsList); + twineTags = new TagSearchTree<>(tagsList); } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Illegal Character in devTools/javaSanityCheck/twineTags"); System.exit(-1); } } + /** + * Loads a list of tags from a file + * + * @param filePath file to load tags from + * @return loaded tags + */ + private static List<Tag> loadTags(String filePath) { + List<Tag> tagsList = new LinkedList<>(); + try { + Files.lines(new File(filePath).toPath()).map(String::trim) + .filter(s -> !s.startsWith("#")) + .forEach(s -> tagsList.add(parseTag(s))); + } catch (IOException e) { + System.err.println("Couldn't read " + filePath); + } + return tagsList; + } + /** * Turns a string into a Tag * ";1" at the end of the String indicates that the tag needs to be closed later @@ -160,7 +165,7 @@ public class Main { } /** - * sets up the excluded array. + * sets up the excluded files array. */ private static void setupExclude() { //load excluded files @@ -173,7 +178,7 @@ public class Main { System.err.println("Couldn't read devTools/javaSanityCheck/excluded"); } - //turn excluded files into an array and change them to windows style if needed + //turn excluded files into an array and change path to windows style if needed if (isWindows()) { excluded = new String[excludedList.size()]; int i = 0; @@ -252,15 +257,13 @@ public class Main { stack.pop(); return; } - //3 means the Element is complete and part of a two tag system + //3 means the Element is complete and part of a two or more tag system if (change == 3) { //remove the topmost element from stack since it is complete KnownElement k = stack.pop().getKnownElement(); - /*if (k.isOpening()) { - stack.push(k); - } else */ + //if KnownElement k is closing another tag check if there is one and remove it if (k.isClosing()) { - if (stack.empty()) { + if (stack.empty()) { //there are no open tags at all addError(new SyntaxError("Closed tag " + k.getShortDescription() + " without having any open tags.", -2)); } else if (stack.peek() instanceof KnownElement) { KnownElement kFirst = (KnownElement) stack.pop(); @@ -268,7 +271,6 @@ public class Main { addError(new SyntaxError("Opening tag " + kFirst.getShortDescription() + " does not match closing tag " + k.getShortDescription() + ".", -2)); } - //stack.pop(); } else { addError(new SyntaxError("Closing tag " + k.getShortDescription() + " inside " + "another tag: " + stack.peek().getShortDescription(), -2, true)); diff --git a/devTools/javaSanityCheck/src/SyntaxError.java b/devTools/javaSanityCheck/src/SyntaxError.java index 150f2bd449b8c318b5a05884868a71538554ec67..ad7aa7cdf3c2c2f01caa4f552f01180861d3569a 100644 --- a/devTools/javaSanityCheck/src/SyntaxError.java +++ b/devTools/javaSanityCheck/src/SyntaxError.java @@ -7,33 +7,57 @@ public class SyntaxError extends Exception { private int change; //see Element for values; -2 means not thrown private boolean warning = false; + /** + * @param description description of error + * @param change state change as specified in Element + */ public SyntaxError(String description, int change) { this.description = description; this.change = change; } + /** + * @param description description of error + * @param change state change as specified in Element + * @param warning whether it is a warning or an error + */ public SyntaxError(String description, int change, boolean warning) { this(description, change); this.warning = warning; } + /** + * @param file at which the error occurred + */ public void setFile(String file) { this.file = file; } + /** + * @param line in which the error occurred + */ public void setLine(int line) { this.line = line; } + /** + * @param position at which the error occurred + */ public void setPosition(int position) { this.position = position; } + /** + * @return error message + */ public String getError() { String s = warning ? "Warning: " : "Error: "; - return s + file + ": " + line + ":" + position + " : "+ description; + return s + file + ": " + line + ":" + position + " : " + description; } + /** + * @return change that happened in Element before it was thrown. -1 if not thrown. + */ public int getChange() { return change; } diff --git a/devTools/javaSanityCheck/src/element/AngleBracketElement.java b/devTools/javaSanityCheck/src/element/AngleBracketElement.java index e4819ba37ea48e4924dc944827d651fa14e02ad1..233c4f303205ab5e24b7c7ffd8c45088002ed471 100644 --- a/devTools/javaSanityCheck/src/element/AngleBracketElement.java +++ b/devTools/javaSanityCheck/src/element/AngleBracketElement.java @@ -64,7 +64,7 @@ public class AngleBracketElement extends Element { return handleOpeningHTML(c); } catch (SyntaxError e) { state = 1; - throw new SyntaxError("Opening \"<\" missing, found " + c + " [debug:initialCase]", 1); + throw new SyntaxError("Opening \"<\" missing, found " + c, 1); } } case 1: @@ -105,7 +105,7 @@ public class AngleBracketElement extends Element { state = 3; return 1; } else { - throw new SyntaxError("Closing \">\" missing, opened [" + line + ":" + pos + "]", 2); + throw new SyntaxError("Closing \">\" missing, opened tag at [" + line + ":" + pos + "]", 2); } case 4: if (c == '>') { @@ -120,20 +120,20 @@ public class AngleBracketElement extends Element { state = 4; return 1; } else { - throw new SyntaxError("Closing \">\" missing, opened [" + line + ":" + pos + "]", 2); + throw new SyntaxError("Closing \">\" missing, opened tag at[" + line + ":" + pos + "]", 2); } case 5: if (c == '>') { state = -5; return 1; } else { - throw new SyntaxError("Closing \">\" missing, opened [" + line + ":" + pos + "]", 2); + throw new SyntaxError("Closing \">\" missing, opened tag at [" + line + ":" + pos + "]", 2); } case -5: if (c == '>') { return 2; } - throw new SyntaxError("Closing \">\" missing, opened [" + line + ":" + pos + "]", 2); + throw new SyntaxError("Closing \">\" missing, opened tag at [" + line + ":" + pos + "]", 2); case 6: if (c == '>') { return 3; @@ -141,13 +141,13 @@ public class AngleBracketElement extends Element { state = 3; return 1; } else { - throw new SyntaxError("Closing \">\" missing, opened [" + line + ":" + pos + "]", 3); + throw new SyntaxError("Closing \">\" missing, opened tag at [" + line + ":" + pos + "]", 3); } case -6: if (c == '>') { return 3; } - throw new SyntaxError("Closing \">\" missing, opened [" + line + ":" + pos + "]", 3); + throw new SyntaxError("Closing \">\" missing, opened tag at [" + line + ":" + pos + "]", 3); case -9: if (c == '>') { diff --git a/devTools/javaSanityCheck/src/element/CommentElement.java b/devTools/javaSanityCheck/src/element/CommentElement.java index 3a3cc12aff9f3c2e4b06cc7240d14824575cc96e..159b92afcd56f36b21952f173fa417276880b4c4 100644 --- a/devTools/javaSanityCheck/src/element/CommentElement.java +++ b/devTools/javaSanityCheck/src/element/CommentElement.java @@ -4,7 +4,7 @@ import org.arkerthan.sanityCheck.SyntaxError; import org.arkerthan.sanityCheck.UnknownStateException; public class CommentElement extends Element { - int state = 0; + private int state = 0; /* 0 - / 1 - /*??? @@ -13,6 +13,10 @@ public class CommentElement extends Element { 4 - /%???% */ + /** + * @param line line in which comment starts + * @param pos position in line where comment starts + */ public CommentElement(int line, int pos) { super(line, pos); } diff --git a/devTools/javaSanityCheck/src/element/Element.java b/devTools/javaSanityCheck/src/element/Element.java index b0f99fa904f66a6a9ebcecb5ffdfb7040493b08b..96f71976f3dd99e0ff49d69de72b18db7153aa9d 100644 --- a/devTools/javaSanityCheck/src/element/Element.java +++ b/devTools/javaSanityCheck/src/element/Element.java @@ -23,12 +23,15 @@ public abstract class Element { * 3 - the Element is finished and a KnownHtmlElement was generated * 4 - the Element is finished and the char is still open for use * - * @param c - * @return - * @throws Error + * @param c char to be parsed + * @return state change + * @throws SyntaxError thrown when an syntax error is detected */ public abstract int handleChar(char c) throws SyntaxError; + /** + * @return the constructed KnownElement. null if none was constructed yet. + */ public KnownElement getKnownElement() { return k; } diff --git a/devTools/javaSanityCheck/src/element/KnownElement.java b/devTools/javaSanityCheck/src/element/KnownElement.java index 305be2fe65210b191d96ead352487b35e448ce39..7bc0b7a1ee7d746855cc8fe34354c099f66e5b30 100644 --- a/devTools/javaSanityCheck/src/element/KnownElement.java +++ b/devTools/javaSanityCheck/src/element/KnownElement.java @@ -4,6 +4,10 @@ import org.arkerthan.sanityCheck.SyntaxError; public abstract class KnownElement extends Element { + /** + * @param line at which it begins + * @param pos at which it begins + */ public KnownElement(int line, int pos) { super(line, pos); } diff --git a/devTools/javaSanityCheck/src/element/KnownHtmlElement.java b/devTools/javaSanityCheck/src/element/KnownHtmlElement.java index d086a74bc547a0aa90520e50c4398f5cf6dc365e..c262263b9b79886dfa229b6b29b79c7a6e2f8b2d 100644 --- a/devTools/javaSanityCheck/src/element/KnownHtmlElement.java +++ b/devTools/javaSanityCheck/src/element/KnownHtmlElement.java @@ -5,6 +5,12 @@ public class KnownHtmlElement extends KnownElement { private boolean opening; private String statement; + /** + * @param line at which it begins + * @param pos at which it begins + * @param opening if it opens a tag: <tag> or closes it: </tag> + * @param statement statement inside the tag + */ public KnownHtmlElement(int line, int pos, boolean opening, String statement) { super(line, pos); this.opening = opening; diff --git a/devTools/javaSanityCheck/src/element/KnownTwineElement.java b/devTools/javaSanityCheck/src/element/KnownTwineElement.java index 24003fd00be0bb79aa6a01b2fbd2d9a91439ac6a..765d9b682ee32cc9ac54cf095d318e241af2af5d 100644 --- a/devTools/javaSanityCheck/src/element/KnownTwineElement.java +++ b/devTools/javaSanityCheck/src/element/KnownTwineElement.java @@ -5,6 +5,13 @@ public class KnownTwineElement extends KnownElement { private boolean opening; private String statement; + /** + * + * @param line at which it begins + * @param pos at which it begins + * @param opening if it opens a tag: <<tag>> or closes it: <</tag>> + * @param statement statement inside the tag + */ public KnownTwineElement(int line, int pos, boolean opening, String statement) { super(line, pos); this.opening = opening;