diff --git a/SanityCheck.jar b/SanityCheck.jar index 7193ecd1a4089711f1fcfdc0f0d3a7ca8008a5b1..f19e52f690973f356affb974151449cdce45adcc 100644 Binary files a/SanityCheck.jar and b/SanityCheck.jar differ diff --git a/devTools/javaSanityCheck/info.txt b/devTools/javaSanityCheck/info.txt new file mode 100644 index 0000000000000000000000000000000000000000..e949bd8d03f785569f824dbae27c9c282bcb8014 --- /dev/null +++ b/devTools/javaSanityCheck/info.txt @@ -0,0 +1,2 @@ +Full sources for easy import into IDE can be found here: +https://gitgud.io/Arkerthan/twine-sanitycheck diff --git a/devTools/javaSanityCheck/src/DisallowedTagException.java b/devTools/javaSanityCheck/src/DisallowedTagException.java index cd479fbdf5f9c8b42a9271b8c0049cba10bcb72a..2dfc4a4f0cd3f43b805179a7ab62edcf17a234ff 100644 --- a/devTools/javaSanityCheck/src/DisallowedTagException.java +++ b/devTools/javaSanityCheck/src/DisallowedTagException.java @@ -1,8 +1,11 @@ package org.arkerthan.sanityCheck; +/** + * @author Arkerthan + */ public class DisallowedTagException extends RuntimeException { - public DisallowedTagException(String tag) { - super(tag); - } + public DisallowedTagException(String tag) { + super(tag); + } } diff --git a/devTools/javaSanityCheck/src/Main.java b/devTools/javaSanityCheck/src/Main.java index 84c471d2148d57206c18d5ac8a217f67bac07d21..15ed975f26878ccbf93d95ead95073f19077dfbd 100644 --- a/devTools/javaSanityCheck/src/Main.java +++ b/devTools/javaSanityCheck/src/Main.java @@ -14,315 +14,318 @@ import java.util.*; /** * @author Arkerthan + * @version 1.0 */ public class Main { - public static TagSearchTree<Tag> htmlTags, twineTags; - private static String currentFile; - private static int currentLine, currentPosition; - private static Stack<Element> stack; - private static List<SyntaxError> errors = new LinkedList<>(); - private static String[] excluded; - - public static void main(String[] args) { - - //setup - setupExclude(); - setupHtmlTags(); - setupTwineTags(); - Path workingDir = Paths.get("").toAbsolutePath(); - - //actual sanityCheck - runSanityCheckInDirectory(workingDir, new File("src/")); - - //output errors - for (SyntaxError e : - errors) { - System.out.println(e.getError()); - } - } - - - /** - * Goes through the whole directory including subdirectories and runs - * {@link Main#sanityCheck(Path)} on all .tw files - * - * @param dir to be checked - */ - private static void runSanityCheckInDirectory(Path workingDir, File dir) { - //subdirectories are checked recursively - - try { - for (File file : dir.listFiles()) { - if (file.isFile()) { //run sanityCheck if file is a .tw file - String path = file.getAbsolutePath(); - if (path.endsWith(".tw")) { - sanityCheck(workingDir.relativize(file.toPath())); - } - } else if (file.isDirectory()) { - runSanityCheckInDirectory(workingDir, file.getAbsoluteFile()); - } - } - } catch (NullPointerException e) { - e.printStackTrace(); - System.err.println("Couldn't read directory " + currentFile); - System.exit(-1); - } - } - - /** - * Runs the sanity check for one file. Does not run if file is excluded. - * - * @param path file to be checked - */ - private static void sanityCheck(Path path) { - File file = path.toFile(); - - // replace this with a known encoding if possible - Charset encoding = Charset.defaultCharset(); - - if (!excluded(file.getPath())) { - 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 a {@link TagSearchTree<Tag>} for fast access of HTML tags later - */ - private static void setupHtmlTags() { - //load HTML tags into a list - List<Tag> TagsList = loadTags("devTools/javaSanityCheck/htmlTags"); - - //turn List into alphabetical search tree - try { - htmlTags = new TagSearchTree<>(TagsList); - } catch (ArrayIndexOutOfBoundsException e) { - System.err.println("Illegal Character in devTools/javaSanityCheck/htmlTags"); - System.exit(-1); - } - } - - /** - * sets up a {@link TagSearchTree<Tag>} for fast access of twine tags later - */ - private static void setupTwineTags() { - //load twine tags into a list - List tagsList = loadTags("devTools/javaSanityCheck/twineTags"); - - //turn List into alphabetical search tree - try { - 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 - * - * @param s tag as String - * @return tag as Tag - */ - private static Tag parseTag(String s) { - String[] st = s.split(";"); - if (st.length > 1 && st[1].equals("1")) { - return new Tag(st[0], false); - } - return new Tag(st[0], true); - } - - /** - * sets up the excluded files array. - */ - private static void setupExclude() { - //load excluded files - List<String> excludedList = new ArrayList<>(); - try { - Files.lines(new File("devTools/javaSanityCheck/excluded").toPath()).map(String::trim) - .filter(s -> !s.startsWith("#")) - .forEach(excludedList::add); - } catch (IOException e) { - System.err.println("Couldn't read devTools/javaSanityCheck/excluded"); - } - - //turn excluded files into an array and change path to windows style if needed - if (isWindows()) { - excluded = new String[excludedList.size()]; - int i = 0; - for (String s : - excludedList) { - excluded[i++] = s.replaceAll("/", "\\\\"); - } - } else { - excluded = excludedList.toArray(new String[0]); - } - } - - /** - * @return whether OS is Windows or not - */ - private static boolean isWindows() { - return (System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows")); - } - - /** - * checks if a file or directory is excluded from the sanity check - * - * @param s file/directory to be checked - * @return whether it is excluded or not - */ - private static boolean excluded(String s) { - for (String ex : - excluded) { - if (s.startsWith(ex)) return true; - } - return false; - } - - /** - * Reads the file character by character. - * - * @param reader reader that is read - * @throws IOException thrown if the file can't be read - */ - private static void handleCharacters(Reader reader) throws IOException { - int r; - while ((r = reader.read()) != -1) { - char c = (char) r; - handleCharacter(c); - } - } - - /** - * Handles a single character - * - * @param c next character - */ - private static void handleCharacter(char c) { - //updating position - currentPosition++; - if (c == '\n') { - currentLine++; - currentPosition = 1; - } - - //try applying to the innermost element - if (!stack.empty()) { - int change; - try { - change = stack.peek().handleChar(c); - } catch (SyntaxError e) { - change = e.getChange(); - addError(e); - } - - //change greater 0 means the innermost element did some work - if (change > 0) { - //2 means the Element is complete - if (change == 2) { - //remove the topmost element from stack since it is complete - stack.pop(); - return; - } - //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 KnownElement k is closing another element, check if there is one and remove it - if (k.isClosing()) { - if (stack.empty()) { //there are no open elements at all - addError(new SyntaxError("Closed tag " + k.getShortDescription() + " without " + - "having any open tags.", -2)); - } else if (stack.peek() instanceof KnownElement) { - //get opening tag - KnownElement kFirst = (KnownElement) stack.pop(); - //check if closing element matches the opening element - if (!kFirst.isMatchingElement(k)) { - addError(new SyntaxError("Opening tag " + kFirst.getShortDescription() + - " does not match closing tag " + k.getShortDescription() + ".", -2)); - } - } else { - //There closing tag inside another not Known element: <div </html> - addError(new SyntaxError("Closing tag " + k.getShortDescription() + " inside " + - "another tag " + stack.peek().getShortDescription() + " without opening first.", - -2, true)); - } - } - //check if the element needs to be closed by another - if (k.isOpening()) { - stack.push(k); - } - return; - } - //means the element couldn't do anything with it and is finished - if (change == 4) { - stack.pop(); - } else { - return; - } - } - } - - - //innermost element was uninterested, trying to find matching element - switch (c) { - //case '@': - //stack.push(new AtElement(currentLine, currentPosition)); - //break; - case '<': - stack.push(new AngleBracketElement(currentLine, currentPosition)); - break; - //case '>': - //addError(new SyntaxError("Dangling \">\", current innermost: " + (stack.empty() ? "null" : stack.peek().getShortDescription()), -2)); - //break; - case '/': - stack.push(new CommentElement(currentLine, currentPosition)); - break; - } - } - - /** - * add an error to the error list - * - * @param e new error - */ - private static void addError(SyntaxError e) { - e.setFile(currentFile); - e.setLine(currentLine); - e.setPosition(currentPosition); - errors.add(e); - } + public static TagSearchTree<Tag> htmlTags, twineTags; + private static String currentFile; + private static int currentLine, currentPosition; + private static Stack<Element> stack; + private static List<SyntaxError> errors = new LinkedList<>(); + private static String[] excluded; + + public static void main(String[] args) { + + //setup + setupExclude(); + setupHtmlTags(); + setupTwineTags(); + Path workingDir = Paths.get("").toAbsolutePath(); + + //actual sanityCheck + runSanityCheckInDirectory(workingDir, new File("src/")); + + //output errors + for (SyntaxError e : + errors) { + System.out.println(e.getError()); + } + } + + + /** + * Goes through the whole directory including subdirectories and runs + * {@link Main#sanityCheck(Path)} on all .tw files + * + * @param dir to be checked + */ + private static void runSanityCheckInDirectory(Path workingDir, File dir) { + //subdirectories are checked recursively + + try { + for (File file : dir.listFiles()) { + if (file.isFile()) { //run sanityCheck if file is a .tw file + String path = file.getAbsolutePath(); + if (path.endsWith(".tw")) { + sanityCheck(workingDir.relativize(file.toPath())); + } + } else if (file.isDirectory()) { + runSanityCheckInDirectory(workingDir, file.getAbsoluteFile()); + } + } + } catch (NullPointerException e) { + e.printStackTrace(); + System.err.println("Couldn't read directory " + currentFile); + System.exit(-1); + } + } + + /** + * Runs the sanity check for one file. Does not run if file is excluded. + * + * @param path file to be checked + */ + private static void sanityCheck(Path path) { + File file = path.toFile(); + + // replace this with a known encoding if possible + Charset encoding = Charset.defaultCharset(); + + if (!excluded(file.getPath())) { + 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 a {@link TagSearchTree<Tag>} for fast access of HTML tags later + */ + private static void setupHtmlTags() { + //load HTML tags into a list + List<Tag> TagsList = loadTags("devTools/javaSanityCheck/htmlTags"); + + //turn List into alphabetical search tree + try { + htmlTags = new TagSearchTree<>(TagsList); + } catch (ArrayIndexOutOfBoundsException e) { + System.err.println("Illegal Character in devTools/javaSanityCheck/htmlTags"); + System.exit(-1); + } + } + + /** + * sets up a {@link TagSearchTree<Tag>} for fast access of twine tags later + */ + private static void setupTwineTags() { + //load twine tags into a list + List tagsList = loadTags("devTools/javaSanityCheck/twineTags"); + + //turn List into alphabetical search tree + try { + 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 + * + * @param s tag as String + * @return tag as Tag + */ + private static Tag parseTag(String s) { + String[] st = s.split(";"); + if (st.length > 1 && st[1].equals("1")) { + return new Tag(st[0], false); + } + return new Tag(st[0], true); + } + + /** + * sets up the excluded files array. + */ + private static void setupExclude() { + //load excluded files + List<String> excludedList = new ArrayList<>(); + try { + Files.lines(new File("devTools/javaSanityCheck/excluded").toPath()).map(String::trim) + .filter(s -> !s.startsWith("#")) + .forEach(excludedList::add); + } catch (IOException e) { + System.err.println("Couldn't read devTools/javaSanityCheck/excluded"); + } + + //turn excluded files into an array and change path to windows style if needed + if (isWindows()) { + excluded = new String[excludedList.size()]; + int i = 0; + for (String s : + excludedList) { + excluded[i++] = s.replaceAll("/", "\\\\"); + } + } else { + excluded = excludedList.toArray(new String[0]); + } + } + + /** + * @return whether OS is Windows or not + */ + private static boolean isWindows() { + return (System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows")); + } + + /** + * checks if a file or directory is excluded from the sanity check + * + * @param s file/directory to be checked + * @return whether it is excluded or not + */ + private static boolean excluded(String s) { + for (String ex : + excluded) { + if (s.startsWith(ex)) return true; + } + return false; + } + + /** + * Reads the file character by character. + * + * @param reader reader that is read + * @throws IOException thrown if the file can't be read + */ + private static void handleCharacters(Reader reader) throws IOException { + int r; + while ((r = reader.read()) != -1) { + char c = (char) r; + handleCharacter(c); + } + } + + /** + * Handles a single character + * + * @param c next character + */ + private static void handleCharacter(char c) { + //updating position + currentPosition++; + if (c == '\n') { + currentLine++; + currentPosition = 1; + } + + //try applying to the innermost element + if (!stack.empty()) { + int change; + try { + change = stack.peek().handleChar(c); + } catch (SyntaxError e) { + change = e.getChange(); + addError(e); + } + + //change greater 0 means the innermost element did some work + if (change > 0) { + //2 means the Element is complete + if (change == 2) { + //remove the topmost element from stack since it is complete + stack.pop(); + return; + } + //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 KnownElement k is closing another element, check if there is one and remove it + if (k.isClosing()) { + if (stack.empty()) { //there are no open elements at all + addError(new SyntaxError("Closed tag " + k.getShortDescription() + " without " + + "having any open tags.", -2)); + } else if (stack.peek() instanceof KnownElement) { + //get opening tag + KnownElement kFirst = (KnownElement) stack.pop(); + //check if closing element matches the opening element + if (!kFirst.isMatchingElement(k)) { + addError(new SyntaxError("Opening tag " + kFirst.getShortDescription() + + " does not match closing tag " + k.getShortDescription() + ".", -2)); + } + } else { + //There closing tag inside another not Known element: <div </html> + addError(new SyntaxError("Closing tag " + k.getShortDescription() + " inside " + + "another tag " + stack.peek().getShortDescription() + " without opening first.", + -2, true)); + } + } + //check if the element needs to be closed by another + if (k.isOpening()) { + stack.push(k); + } + return; + } + //means the element couldn't do anything with it and is finished + if (change == 4) { + stack.pop(); + } else { + return; + } + } + } + + + //innermost element was uninterested, trying to find matching element + switch (c) { + //case '@': + //stack.push(new AtElement(currentLine, currentPosition)); + //break; + case '<': + stack.push(new AngleBracketElement(currentLine, currentPosition)); + break; + //case '>': + //addError(new SyntaxError("Dangling \">\", current innermost: " + (stack.empty() ? "null" : stack.peek().getShortDescription()), -2)); + //break; + case '/': + stack.push(new CommentElement(currentLine, currentPosition)); + break; + //case '(': + // stack.push(new BracketElement(currentLine, currentPosition)); + } + } + + /** + * add an error to the error list + * + * @param e new error + */ + private static void addError(SyntaxError e) { + e.setFile(currentFile); + e.setLine(currentLine); + e.setPosition(currentPosition); + errors.add(e); + } } diff --git a/devTools/javaSanityCheck/src/SyntaxError.java b/devTools/javaSanityCheck/src/SyntaxError.java index c30e5fbc101c558ff3cabe669d38c0e89f50e3f7..1e1b8936f8120cce9dfc88ce42bf29cff0c4e869 100644 --- a/devTools/javaSanityCheck/src/SyntaxError.java +++ b/devTools/javaSanityCheck/src/SyntaxError.java @@ -1,64 +1,67 @@ package org.arkerthan.sanityCheck; +/** + * @author Arkerthan + */ public class SyntaxError extends Exception { - private String file; - private int line, position; - private String description; - private int change; //see Element for values; -2 means not thrown - private boolean warning = false; + private String file; + private int line, position; + private String description; + 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 + */ + 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 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 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 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; - } + /** + * @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 error message + */ + public String getError() { + String s = warning ? "Warning: " : "Error: "; + 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; - } + /** + * @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/Tag.java b/devTools/javaSanityCheck/src/Tag.java index 5c3c92651a71beb8a4e3cba73d1eb64238fc9908..10ca580417dee08dada0e598c05331e66175b901 100644 --- a/devTools/javaSanityCheck/src/Tag.java +++ b/devTools/javaSanityCheck/src/Tag.java @@ -1,11 +1,14 @@ package org.arkerthan.sanityCheck; +/** + * @author Arkerthan + */ public class Tag { - public final String tag; - public final boolean single; + public final String tag; + public final boolean single; - public Tag(String tag, boolean single) { - this.tag = tag; - this.single = single; - } + public Tag(String tag, boolean single) { + this.tag = tag; + this.single = single; + } } diff --git a/devTools/javaSanityCheck/src/TagSearchTree.java b/devTools/javaSanityCheck/src/TagSearchTree.java index 71e89ec769ff658b246886a179976055f2633709..54c45bbd07c34e905ed19f2a18a22add447397b0 100644 --- a/devTools/javaSanityCheck/src/TagSearchTree.java +++ b/devTools/javaSanityCheck/src/TagSearchTree.java @@ -3,78 +3,79 @@ package org.arkerthan.sanityCheck; import java.util.List; /** + * @param <E> Tag class to be stored + * @author Arkerthan + * <p> * Tag SearchTree stores Tags in an alphabetical search tree. * Once created the search tree can't be changed anymore. - * - * @param <E> Tag class to be stored */ public class TagSearchTree<E extends Tag> { - private static final int SIZE = 128; - private final TagSearchTree<E>[] branches; - private E element = null; - private String path; + private static final int SIZE = 128; + private final TagSearchTree<E>[] branches; + private E element = null; + private String path; - /** - * creates a new empty TagSearchTree - */ - private TagSearchTree() { - branches = new TagSearchTree[SIZE]; - } + /** + * creates a new empty TagSearchTree + */ + private TagSearchTree() { + branches = new TagSearchTree[SIZE]; + } - /** - * Creates a new filled TagSearchTree - * - * @param list Tags to be inserted - */ - public TagSearchTree(List<E> list) { - this(); - for (E e : list) { - this.add(e, 0); - } - } + /** + * Creates a new filled TagSearchTree + * + * @param list Tags to be inserted + */ + public TagSearchTree(List<E> list) { + this(); + for (E e : list) { + this.add(e, 0); + } + } - /** - * adds a new Tag to the TagSearchTree - * - * @param e Tag to be stored - * @param index index of relevant char for adding in tag - */ - private void add(E e, int index) { - //set the path to here - path = e.tag.substring(0, index); - //checks if tag has to be stored here or further down - if (e.tag.length() == index) { - element = e; - } else { - //store tag in correct branch - char c = e.tag.charAt(index); - if (branches[c] == null) { - branches[c] = new TagSearchTree<>(); - } - branches[c].add(e, index + 1); - } - } + /** + * adds a new Tag to the TagSearchTree + * + * @param e Tag to be stored + * @param index index of relevant char for adding in tag + */ + private void add(E e, int index) { + //set the path to here + path = e.tag.substring(0, index); + //checks if tag has to be stored here or further down + if (e.tag.length() == index) { + element = e; + } else { + //store tag in correct branch + char c = e.tag.charAt(index); + if (branches[c] == null) { + branches[c] = new TagSearchTree<>(); + } + branches[c].add(e, index + 1); + } + } - /** - * @param c character of branch needed - * @return branch or null if branch doesn't exist - */ - public TagSearchTree<E> getBranch(char c) { - if (c >= SIZE) return null; - return branches[c]; - } + /** + * @param c character of branch needed + * @return branch or null if branch doesn't exist + */ + public TagSearchTree<E> getBranch(char c) { + if (c >= SIZE) return null; + return branches[c]; + } - /** - * @return stored Tag, null if empty - */ - public E getElement() { - return element; - } + /** + * @return stored Tag, null if empty + */ + public E getElement() { + return element; + } - /** - * @return path inside full tree to get to this Branch - */ - public String getPath() { - return path; - } + /** + * @return path inside full tree to get to this Branch + */ + public String getPath() { + return path; + } } diff --git a/devTools/javaSanityCheck/src/UnknownStateException.java b/devTools/javaSanityCheck/src/UnknownStateException.java index 80dbe598682d23875efe5517170f4f1c91a96246..77c91e73e10567e49514d3dd75970c2c041f7d1a 100644 --- a/devTools/javaSanityCheck/src/UnknownStateException.java +++ b/devTools/javaSanityCheck/src/UnknownStateException.java @@ -1,8 +1,11 @@ package org.arkerthan.sanityCheck; +/** + * @author Arkerthan + */ public class UnknownStateException extends RuntimeException { - public UnknownStateException(int state) { - super(String.valueOf(state)); - } + public UnknownStateException(int state) { + super(String.valueOf(state)); + } } diff --git a/devTools/javaSanityCheck/src/element/AngleBracketElement.java b/devTools/javaSanityCheck/src/element/AngleBracketElement.java index 4b3f2526a3979154d571ddf3fdbdb131e949878f..8db115bf04cfa958d2a4dc88878249253de1f80f 100644 --- a/devTools/javaSanityCheck/src/element/AngleBracketElement.java +++ b/devTools/javaSanityCheck/src/element/AngleBracketElement.java @@ -5,6 +5,9 @@ import org.arkerthan.sanityCheck.*; import java.util.Arrays; import java.util.List; +/** + * @author Arkerthan + */ public class AngleBracketElement extends Element { private static final List<String> logicTags = Arrays.asList("if", "elseif", "else", "switch", "case", "default"); private int state = 0; @@ -309,7 +312,7 @@ public class AngleBracketElement extends Element { @Override public String getShortDescription() { StringBuilder builder = new StringBuilder(); - builder.append('[').append(line).append(":").append(pos).append("] "); + builder.append(getPositionAsString()).append(" "); switch (state) { case 0: builder.append("<"); diff --git a/devTools/javaSanityCheck/src/element/AtElement.java b/devTools/javaSanityCheck/src/element/AtElement.java index 168e340a4326b24c0dff912c64d25ec5042d6fe5..598dad63eea9d7aea1c8058c89a2584c752afee1 100644 --- a/devTools/javaSanityCheck/src/element/AtElement.java +++ b/devTools/javaSanityCheck/src/element/AtElement.java @@ -3,103 +3,106 @@ package org.arkerthan.sanityCheck.element; import org.arkerthan.sanityCheck.SyntaxError; import org.arkerthan.sanityCheck.UnknownStateException; +/** + * @author Arkerthan + */ public class AtElement extends Element { - private int state = 0; - // 0 = @ - // 1 = @@ - // 2 = @@. - // 3 = @@.a -- @@.ab -- @@.abc - // 4 = @@.abc;abc - // 5 = @@.abc;abc@ + private int state = 0; + // 0 = @ + // 1 = @@ + // 2 = @@. + // 3 = @@.a -- @@.ab -- @@.abc + // 4 = @@.abc;abc + // 5 = @@.abc;abc@ - // example: @@.red;some text@@ + // example: @@.red;some text@@ - public AtElement(int line, int pos) { - super(line, pos); - } + public AtElement(int line, int pos) { + super(line, pos); + } - @Override - public int handleChar(char c) throws SyntaxError { - switch (state) { - case 0: - state = 1; - if (c == '@') { - return 1; - } else { - if (c == '.') { - state = 2; - } - throw new SyntaxError("Opening \"@\" missing.", 1); - } - case 1: - if (c == '.') { - state = 2; - return 1; - } else { - state = 4; - throw new SyntaxError("\".\" missing, found \"" + c + "\". This might also indicate a " + - "missing closure in the previous color code.", 0, true); - } - case 2: - state = 3; - if (Character.isAlphabetic(c)) { - return 1; - } else { - throw new SyntaxError("Identifier might be wrong.", 1, true); - } - case 3: - if (c == ';') { - state = 4; - return 1; - } else if (c == ' ') { - state = 4; - throw new SyntaxError("\";\" missing or wrong space.", 1); - } - break; - case 4: - if (c == '@') { - state = 5; - return 1; - } - break; - case 5: - if (c == '@') { - return 2; - } else { - throw new SyntaxError("Closing \"@\" missing.", 2); - } - default: - throw new UnknownStateException(state); - } - return 0; - } + @Override + public int handleChar(char c) throws SyntaxError { + switch (state) { + case 0: + state = 1; + if (c == '@') { + return 1; + } else { + if (c == '.') { + state = 2; + } + throw new SyntaxError("Opening \"@\" missing.", 1); + } + case 1: + if (c == '.') { + state = 2; + return 1; + } else { + state = 4; + throw new SyntaxError("\".\" missing, found \"" + c + "\". This might also indicate a " + + "missing closure in the previous color code.", 0, true); + } + case 2: + state = 3; + if (Character.isAlphabetic(c)) { + return 1; + } else { + throw new SyntaxError("Identifier might be wrong.", 1, true); + } + case 3: + if (c == ';') { + state = 4; + return 1; + } else if (c == ' ') { + state = 4; + throw new SyntaxError("\";\" missing or wrong space.", 1); + } + break; + case 4: + if (c == '@') { + state = 5; + return 1; + } + break; + case 5: + if (c == '@') { + return 2; + } else { + throw new SyntaxError("Closing \"@\" missing.", 2); + } + default: + throw new UnknownStateException(state); + } + return 0; + } - @Override - public String getShortDescription() { - StringBuilder builder = new StringBuilder(); - builder.append(line).append(":").append(pos).append(" "); - switch (state) { - case 0: - builder.append("@"); - break; - case 1: - builder.append("@@"); - break; - case 2: - builder.append("@@."); - break; - case 3: - builder.append("@@.???"); - break; - case 4: - builder.append("@@???"); - break; - case 5: - builder.append("@@???@"); - break; - default: - throw new UnknownStateException(state); - } - return builder.toString(); - } + @Override + public String getShortDescription() { + StringBuilder builder = new StringBuilder(); + builder.append(getPositionAsString()).append(" "); + switch (state) { + case 0: + builder.append("@"); + break; + case 1: + builder.append("@@"); + break; + case 2: + builder.append("@@."); + break; + case 3: + builder.append("@@.???"); + break; + case 4: + builder.append("@@???"); + break; + case 5: + builder.append("@@???@"); + break; + default: + throw new UnknownStateException(state); + } + return builder.toString(); + } } diff --git a/devTools/javaSanityCheck/src/element/BracketElement.java b/devTools/javaSanityCheck/src/element/BracketElement.java new file mode 100644 index 0000000000000000000000000000000000000000..b9848941653e6cb3715e60ac7342a1d087e0ebba --- /dev/null +++ b/devTools/javaSanityCheck/src/element/BracketElement.java @@ -0,0 +1,28 @@ +package org.arkerthan.sanityCheck.element; + +import org.arkerthan.sanityCheck.SyntaxError; + +/** + * @author Arkerthan + */ +public class BracketElement extends Element { + //int state = 0; + + public BracketElement(int line, int pos) { + super(line, pos); + } + + @Override + public int handleChar(char c) throws SyntaxError { + if (c == ')') { + return 2; + } else { + return 0; + } + } + + @Override + public String getShortDescription() { + return getPositionAsString() + " (???"; + } +} diff --git a/devTools/javaSanityCheck/src/element/CommentElement.java b/devTools/javaSanityCheck/src/element/CommentElement.java index a1c80c7004233c1d620d389788213d33398a6c27..a03c929a2114615e8f243a7887b02ecb386b52ad 100644 --- a/devTools/javaSanityCheck/src/element/CommentElement.java +++ b/devTools/javaSanityCheck/src/element/CommentElement.java @@ -3,6 +3,9 @@ package org.arkerthan.sanityCheck.element; import org.arkerthan.sanityCheck.SyntaxError; import org.arkerthan.sanityCheck.UnknownStateException; +/** + * @author Arkerthan + */ public class CommentElement extends Element { private int state = 0; /* @@ -43,7 +46,7 @@ public class CommentElement extends Element { case 2: if (c == '/') { return 2; - } else if (c == '*'){ + } else if (c == '*') { return 1; } state = 1; @@ -67,6 +70,6 @@ public class CommentElement extends Element { @Override public String getShortDescription() { - return null; + return getPositionAsString() + "comment"; } } diff --git a/devTools/javaSanityCheck/src/element/Element.java b/devTools/javaSanityCheck/src/element/Element.java index d0e1bf7cd1210b08f1f888103278cd66ad1a218d..02400f2a5319b99aee1f19216ff401b3121e46aa 100644 --- a/devTools/javaSanityCheck/src/element/Element.java +++ b/devTools/javaSanityCheck/src/element/Element.java @@ -2,42 +2,54 @@ package org.arkerthan.sanityCheck.element; import org.arkerthan.sanityCheck.SyntaxError; +/** + * @author Arkerthan + */ public abstract class Element { - protected KnownElement k; - protected int line, pos; + protected KnownElement k; + protected int line, pos; - /** - * @param line Line the instance was created - * @param pos Position in line the instance was created - */ - protected Element(int line, int pos) { - this.line = line; - this.pos = pos; - } + /** + * @param line Line the instance was created + * @param pos Position in line the instance was created + */ + protected Element(int line, int pos) { + this.line = line; + this.pos = pos; + } - /** - * Parses a Char and returns an int depending on the state of the element - * 0 - the Element did nothing - * 1 - the Element changed state - * 2 - the Element is finished - * 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 char to be parsed - * @return state change - * @throws SyntaxError thrown when an syntax error is detected - */ - public abstract int handleChar(char c) throws SyntaxError; + /** + * Parses a Char and returns an int depending on the state of the element + * 0 - the Element did nothing + * 1 - the Element changed state + * 2 - the Element is finished + * 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 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; - } + /** + * @return the constructed KnownElement. null if none was constructed yet. + */ + public KnownElement getKnownElement() { + return k; + } - /** - * @return a short description usually based on state and position of the Element - */ - public abstract String getShortDescription(); + /** + * Returns the line and position of the Element in the file it was created in. + * + * @return position of Element in file as String + */ + public String getPositionAsString() { + return "[" + line + ":" + pos + "]"; + } + + /** + * @return a short description usually based on state and position of the Element + */ + public abstract String getShortDescription(); } diff --git a/devTools/javaSanityCheck/src/element/KnownElement.java b/devTools/javaSanityCheck/src/element/KnownElement.java index d09a5a7a5cc7279bda7cc03a0a674b1243217847..9b64eb18a877527e76b336fc602312df8d984e43 100644 --- a/devTools/javaSanityCheck/src/element/KnownElement.java +++ b/devTools/javaSanityCheck/src/element/KnownElement.java @@ -2,34 +2,37 @@ package org.arkerthan.sanityCheck.element; import org.arkerthan.sanityCheck.SyntaxError; +/** + * @author Arkerthan + */ 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); - } + /** + * @param line at which it begins + * @param pos at which it begins + */ + public KnownElement(int line, int pos) { + super(line, pos); + } - /** - * @return true, if it needs another Known Element to close it. - */ - public abstract boolean isOpening(); + /** + * @return true, if it needs another Known Element to close it. + */ + public abstract boolean isOpening(); - /** - * @return true if it closes another Element. - */ - public abstract boolean isClosing(); + /** + * @return true if it closes another Element. + */ + public abstract boolean isClosing(); - /** - * @param k Element to be checked - * @return true if given Element closes Element - */ - public abstract boolean isMatchingElement(KnownElement k); + /** + * @param k Element to be checked + * @return true if given Element closes Element + */ + public abstract boolean isMatchingElement(KnownElement k); - @Override - public int handleChar(char c) throws SyntaxError { - return 0; - } + @Override + public int handleChar(char c) throws SyntaxError { + return 0; + } } diff --git a/devTools/javaSanityCheck/src/element/KnownHtmlElement.java b/devTools/javaSanityCheck/src/element/KnownHtmlElement.java index 8ae21e1a402d3fd8c3a8a5010c03527aab2c9a3c..6edaa6723ba4d8ec68e0d549da56e7494dc20918 100644 --- a/devTools/javaSanityCheck/src/element/KnownHtmlElement.java +++ b/devTools/javaSanityCheck/src/element/KnownHtmlElement.java @@ -1,47 +1,50 @@ package org.arkerthan.sanityCheck.element; +/** + * @author Arkerthan + */ public class KnownHtmlElement extends KnownElement { - private boolean opening; - private String statement; + 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; - this.statement = 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; + this.statement = statement; + } - @Override - public String getShortDescription() { - StringBuilder builder = new StringBuilder(); - builder.append('[').append(line).append(":").append(pos).append("] <"); - if (!opening) { - builder.append("/"); - } - return builder.append(statement).append(">").toString(); - } + @Override + public String getShortDescription() { + StringBuilder builder = new StringBuilder(); + builder.append(getPositionAsString()).append(" <"); + if (!opening) { + builder.append("/"); + } + return builder.append(statement).append(">").toString(); + } - @Override - public boolean isOpening() { - return opening; - } + @Override + public boolean isOpening() { + return opening; + } - @Override - public boolean isClosing() { - return !opening; - } + @Override + public boolean isClosing() { + return !opening; + } - @Override - public boolean isMatchingElement(KnownElement k) { - if (k instanceof KnownHtmlElement) { - return ((KnownHtmlElement) k).statement.equals(this.statement); - } - return false; - } + @Override + public boolean isMatchingElement(KnownElement k) { + if (k instanceof KnownHtmlElement) { + return ((KnownHtmlElement) k).statement.equals(this.statement); + } + return false; + } } diff --git a/devTools/javaSanityCheck/src/element/KnownLogicElement.java b/devTools/javaSanityCheck/src/element/KnownLogicElement.java index 994201c5a9ba40a4aea01252bb0ba6765a5841b7..e887b2989362daf737204d8c378c8efe6c778ecf 100644 --- a/devTools/javaSanityCheck/src/element/KnownLogicElement.java +++ b/devTools/javaSanityCheck/src/element/KnownLogicElement.java @@ -6,112 +6,115 @@ import org.arkerthan.sanityCheck.UnknownStateException; import java.util.Arrays; import java.util.List; +/** + * @author Arkerthan + */ public class KnownLogicElement extends KnownElement { - private static final List<String> allowedTags = Arrays.asList("if", "elseif", "else"); - private final int state; - private boolean last; - /* - 0 - if - 1 - elseif - 2 - else - 3 - switch - 4 - case - 5 - default - */ + private static final List<String> allowedTags = Arrays.asList("if", "elseif", "else"); + private final int state; + private boolean last; + /* + 0 - if + 1 - elseif + 2 - else + 3 - switch + 4 - case + 5 - default + */ - public KnownLogicElement(int line, int pos, String tag, boolean last) { - this(line, pos, tag); - this.last = last; - } + public KnownLogicElement(int line, int pos, String tag, boolean last) { + this(line, pos, tag); + this.last = last; + } - public KnownLogicElement(int line, int pos, String tag) { - super(line, pos); - switch (tag) { - case "if": - state = 0; - break; - case "elseif": - state = 1; - break; - case "else": - state = 2; - break; - case "switch": - state = 3; - break; - case "case": - state = 4; - break; - case "default": - state = 5; - break; - default: - throw new DisallowedTagException(tag); - } - last = false; - } + public KnownLogicElement(int line, int pos, String tag) { + super(line, pos); + switch (tag) { + case "if": + state = 0; + break; + case "elseif": + state = 1; + break; + case "else": + state = 2; + break; + case "switch": + state = 3; + break; + case "case": + state = 4; + break; + case "default": + state = 5; + break; + default: + throw new DisallowedTagException(tag); + } + last = false; + } - @Override - public boolean isOpening() { - return !last; - } + @Override + public boolean isOpening() { + return !last; + } - @Override - public boolean isClosing() { - return (state != 0 && state != 3) || last; - } + @Override + public boolean isClosing() { + return (state != 0 && state != 3) || last; + } - @Override - public boolean isMatchingElement(KnownElement k) { - if (!(k instanceof KnownLogicElement)) { - return false; - } - KnownLogicElement l = (KnownLogicElement) k; - switch (state) { - case 0: - case 1: - return l.state == 1 || l.state == 2 || (l.state == 0 && l.last); - case 2: - return l.state == 0 && l.last; - case 3: - case 4: - return l.state == 3 || l.state == 4; - case 5: - return l.state == 3 && l.last; - default: - throw new UnknownStateException(state); - } - } + @Override + public boolean isMatchingElement(KnownElement k) { + if (!(k instanceof KnownLogicElement)) { + return false; + } + KnownLogicElement l = (KnownLogicElement) k; + switch (state) { + case 0: + case 1: + return l.state == 1 || l.state == 2 || (l.state == 0 && l.last); + case 2: + return l.state == 0 && l.last; + case 3: + case 4: + return l.state == 3 || l.state == 4; + case 5: + return l.state == 3 && l.last; + default: + throw new UnknownStateException(state); + } + } - @Override - public String getShortDescription() { - StringBuilder builder = new StringBuilder(); - builder.append("[").append(line).append(":").append(pos).append("] <<"); - if (last) { - builder.append('/'); - } - switch (state) { - case 0: - builder.append("if"); - break; - case 1: - builder.append("elseif"); - break; - case 2: - builder.append("else"); - break; - case 3: - builder.append("switch"); - break; - case 4: - builder.append("case"); - break; - case 5: - builder.append("default"); - break; - default: - throw new UnknownStateException(state); - } - return builder.append(">>").toString(); - } + @Override + public String getShortDescription() { + StringBuilder builder = new StringBuilder(); + builder.append(getPositionAsString()).append(" <<"); + if (last) { + builder.append('/'); + } + switch (state) { + case 0: + builder.append("if"); + break; + case 1: + builder.append("elseif"); + break; + case 2: + builder.append("else"); + break; + case 3: + builder.append("switch"); + break; + case 4: + builder.append("case"); + break; + case 5: + builder.append("default"); + break; + default: + throw new UnknownStateException(state); + } + return builder.append(">>").toString(); + } } diff --git a/devTools/javaSanityCheck/src/element/KnownTwineElement.java b/devTools/javaSanityCheck/src/element/KnownTwineElement.java index 4bd78b2d585d014186d7ded56d804352e8e5d8db..020aad7819fecac96bac7ac0f8da906d79d32321 100644 --- a/devTools/javaSanityCheck/src/element/KnownTwineElement.java +++ b/devTools/javaSanityCheck/src/element/KnownTwineElement.java @@ -1,48 +1,50 @@ package org.arkerthan.sanityCheck.element; +/** + * @author Arkerthan + */ public class KnownTwineElement extends KnownElement { - private boolean opening; - private String statement; + 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; - this.statement = 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; + this.statement = statement; + } - @Override - public String getShortDescription() { - StringBuilder builder = new StringBuilder(); - builder.append("[").append(line).append(":").append(pos).append("] <<"); - if (!opening) { - builder.append("/"); - } - return builder.append(statement).append(">>").toString(); - } + @Override + public String getShortDescription() { + StringBuilder builder = new StringBuilder(); + builder.append(getPositionAsString()).append(" <<"); + if (!opening) { + builder.append("/"); + } + return builder.append(statement).append(">>").toString(); + } - @Override - public boolean isOpening() { - return opening; - } + @Override + public boolean isOpening() { + return opening; + } - @Override - public boolean isClosing() { - return !opening; - } + @Override + public boolean isClosing() { + return !opening; + } - @Override - public boolean isMatchingElement(KnownElement k) { - if (k instanceof KnownTwineElement) { - return ((KnownTwineElement) k).statement.equals(this.statement); - } - return false; - } + @Override + public boolean isMatchingElement(KnownElement k) { + if (k instanceof KnownTwineElement) { + return ((KnownTwineElement) k).statement.equals(this.statement); + } + return false; + } }