diff options
author | j-james | 2022-10-17 15:29:53 +0000 |
---|---|---|
committer | j-james | 2022-10-17 15:29:53 +0000 |
commit | 21b8e5f6cdcd9fab275efce5e88f02addfd19e7e (patch) | |
tree | 8a629525a63f3ff8a0123d4eefc30bc0b578af16 /src/main/model | |
parent | 9dad27fef462d20adec671efe6d1e795966f5300 (diff) |
Implement fairly comprehensive ParserTests
100% class and method coverage, 95% line coverage
Diffstat (limited to 'src/main/model')
-rw-r--r-- | src/main/model/css/CssParser.java | 64 | ||||
-rw-r--r-- | src/main/model/html/ElementNode.java | 17 | ||||
-rw-r--r-- | src/main/model/html/HtmlParser.java | 25 |
3 files changed, 63 insertions, 43 deletions
diff --git a/src/main/model/css/CssParser.java b/src/main/model/css/CssParser.java index 25b6752..a382b14 100644 --- a/src/main/model/css/CssParser.java +++ b/src/main/model/css/CssParser.java @@ -4,7 +4,10 @@ import org.javatuples.*; import java.util.*; -/* +/** + * This class represents the state of and implements an LL(1) CSS parser. + * For convenience, the following (slightly wrong) context-free grammar for CSS is below. + * <br> * RULES ::= (RULE)+ * RULE ::= SELECTORS '{' (PROPERTY | (PROPERTY ';')*) '}' * SELECTORS ::= SELECTOR (COMBINATOR SELECTOR)* @@ -216,13 +219,14 @@ public class CssParser { previousChar = '\0'; } else { // possibly not the best way to handle this, may be better to keep the backslash - currentValue = currentValue.substring(0, currentValue.length() - 2); + currentValue = currentValue.substring(0, currentValue.length() - 1); currentValue += c; previousChar = c; } break; default: currentValue += c; + previousChar = c; break; } } @@ -239,13 +243,14 @@ public class CssParser { currentValue += c; previousChar = '\0'; } else { - currentValue = currentValue.substring(0, currentValue.length() - 2); + currentValue = currentValue.substring(0, currentValue.length() - 1); currentValue += c; previousChar = c; } break; default: currentValue += c; + previousChar = c; break; } } @@ -258,7 +263,7 @@ public class CssParser { * REQUIRES: A string of the form [NUMBER][VALIDUNIT] * EFFECTS: Returns a number, in pixels, that has been converted appropriately */ - private static double parseUnits(String input) { + public static double parseUnits(String input) { String numbers = ""; String units = ""; // imagine making a language without iterable strings, fml @@ -305,30 +310,29 @@ public class CssParser { } /* - * body { - * background-color: #f0f0f2; - * margin: 0; - * padding: 0; - * font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", - * "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; - * - * } - * div { - * width: 600px; - * margin: 5em auto; - * padding: 2em; - * background-color: #fdfdff; - * border-radius: 0.5em; - * box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); - * } - * a:link, a:visited { - * color: #38488f; - * text-decoration: none; - * } - * @media (max - width : 700px) { - * div { - * margin: 0 auto; - * width: auto; - * } - * } +body { + background-color: #f0f0f2; + margin: 0; + padding: 0; + font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", + "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; +} +div { + width: 600px; + margin: 5em auto; + padding: 2em; + background-color: #fdfdff; + border-radius: 0.5em; + box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); +} +a:link, a:visited { + color: #38488f; + text-decoration: none; +} +@media (max - width : 700px) { + div { + margin: 0 auto; + width: auto; + } +} */ diff --git a/src/main/model/html/ElementNode.java b/src/main/model/html/ElementNode.java index a1ad90c..98995d0 100644 --- a/src/main/model/html/ElementNode.java +++ b/src/main/model/html/ElementNode.java @@ -28,7 +28,16 @@ public class ElementNode implements Node { * MODIFIES: this */ public ElementNode(String tag, ArrayList<Pair<String, String>> attributes) { - this(tag, attributes, new ArrayList<Node>()); + this(tag, attributes, new ArrayList<>()); + } + + /** + * Overloads the constructor for ease of use. Should probably only be used for tests. + * EFFECTS: Constructs a new ElementNode from the arguments provided. + * MODIFIES: this + */ + public ElementNode(String tag) { + this(tag, new ArrayList<>(), new ArrayList<>()); } /** @@ -43,12 +52,16 @@ public class ElementNode implements Node { return this.tag; } + public ArrayList<Pair<String, String>> getAttributes() { + return this.attributes; + } + public ArrayList<Node> getChildren() { return this.children; } // We implement this method for easy debugging. public String getData() { - return getTag(); + return getTag() + " " + getAttributes().toString(); } } diff --git a/src/main/model/html/HtmlParser.java b/src/main/model/html/HtmlParser.java index e9dc0c4..bfdd57c 100644 --- a/src/main/model/html/HtmlParser.java +++ b/src/main/model/html/HtmlParser.java @@ -5,15 +5,18 @@ import java.util.*; import model.util.Node; import org.javatuples.*; -/* +/** + * This class represents the state of and implements an LL(1) HTML parser. + * For convenience, the following (defo wrong) context-free grammar for HTML is below. + * <br> * HTML ::= '<!DOCTYPE html>' (NODE)* - * NODE ::= '<'TAG (' ' WORD '=' ('"'TEXT'"' | TEXT))* '>' (NODE)* '</' TAG '>' - * | '<'SINGLE_TAG (' ' WORD '=' ('"'TEXT'"' | TEXT))* ('>'|'/>') + * NODE ::= '<'TAG (' ' WORD '=' ('"'TEXT'"' | TEXT))* '>' (NODE)* '<\/' TAG '>' + * | '<'SELF_CLOSING_TAG (' ' WORD '=' ('"'TEXT'"' | TEXT))* ('>'|'/>') * | (TEXT | NODE)* * TEXT ::= UNICODE - {'"'} + {'\"'} * TAG ::= 'body' | 'div' | ... - * SINGLE_TAG ::= 'img' | ... - * (note that \forall T \in SINGLE_TAG, T \notin TAG) + * SELF_CLOSING_TAG ::= 'img' | ... + * (note that \forall T \in SELF_CLOSING_TAG, T \notin TAG) */ public class HtmlParser { @@ -216,16 +219,16 @@ public class HtmlParser { */ private void caseValue(char c) { switch (c) { - case '\'': - state = ParserState.SINGLE_QUOTES; + case '\'': state = ParserState.SINGLE_QUOTES; break; - case '\"': - state = ParserState.DOUBLE_QUOTES; + case '\"': state = ParserState.DOUBLE_QUOTES; break; case ' ': case '\n': + state = ParserState.KEY; currentAttributes.add(new Pair<>(currentKey, currentValue)); currentKey = ""; currentValue = ""; + break; // THE FOOTGUN DESIGN STRIKES AGAIN case '>': if (!currentKey.equals("") || !currentValue.equals("")) { currentAttributes.add(new Pair<>(currentKey, currentValue)); @@ -251,7 +254,7 @@ public class HtmlParser { state = ParserState.VALUE; previousChar = '\0'; } else { - currentValue = currentValue.substring(0, currentValue.length() - 2); + currentValue = currentValue.substring(0, currentValue.length() - 1); currentValue += c; previousChar = c; } @@ -274,7 +277,7 @@ public class HtmlParser { state = ParserState.VALUE; previousChar = '\0'; } else { - currentValue = currentValue.substring(0, currentValue.length() - 2); + currentValue = currentValue.substring(0, currentValue.length() - 1); currentValue += c; previousChar = c; } |