From 3e9bb5fae16c35938bc1f7f7669c12cc355c9331 Mon Sep 17 00:00:00 2001 From: j-james Date: Sun, 16 Oct 2022 23:25:45 -0700 Subject: Basic prototypes of HTML/CSS lexers --- src/main/model/util/AbstractTree.java | 35 +++++++++++++++++++++ src/main/model/util/Lexer.java | 58 +++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/main/model/util/AbstractTree.java create mode 100644 src/main/model/util/Lexer.java (limited to 'src/main/model/util') diff --git a/src/main/model/util/AbstractTree.java b/src/main/model/util/AbstractTree.java new file mode 100644 index 0000000..4c74732 --- /dev/null +++ b/src/main/model/util/AbstractTree.java @@ -0,0 +1,35 @@ +package model.util; + +import org.javatuples.*; + +import java.util.*; + +// Utility class for a general tree: we'll be using these a lot +public abstract class AbstractTree { + + // An AbstractTree holds some kind of data; we'll want this to be generic + // e.g. a tag, attributes, a tag and attributes, etc + private T data; + // Since it's a tree every node also has children. + private ArrayList> children; + + // future implementations may want to consider adding an Optional<> parent; or an Optional<> prevSibling + + public T getData() { + return data; + } + + public ArrayList> getChildren() { + return children; + } + + // god so much boilerplate + public AbstractTree(T data, ArrayList> children) { + this.data = data; + this.children = children; + } + + public void addChild(AbstractTree child) { + this.children.add(child); + } +} diff --git a/src/main/model/util/Lexer.java b/src/main/model/util/Lexer.java new file mode 100644 index 0000000..b35caa6 --- /dev/null +++ b/src/main/model/util/Lexer.java @@ -0,0 +1,58 @@ +package model.util; + +import java.util.*; + +// General-purpose Lexer +public class Lexer { + + // private static final Set whitespace = new HashSet(" ", "\n"); + + // unused, helper function for if we implement finding identifers longer than a character + private static int longestDelimiter(Set delimiters) { + int longestDelimiter = 0; + for (String delimiter : delimiters) { + if (delimiter.length() > longestDelimiter) { + longestDelimiter = delimiter.length(); + } + } + return longestDelimiter; + } + + /** + * Lexes a "free-form" language. "free-form" has a specific meaning here that's important to preserve: + * "free-form" means that _additional_ whitespace characters do not affect the language: e.g. two newlines + * instead of one, four spaces instead of two, etc. They are _not_ "whitespace-insensitive", which is usually + * a misnomer. + * The name's a bit of a joke: free-form languages are generally referred to as whitespace-insensitive --> + * insensitive == rude. Jokes are funnier when you have to explain them. + * Also, insensitiveLex() and freeformLex() aren't really that good of names. + * + * NOTE: This lexer only works with single-character deliminators. + * TODO: deduplicate whitespace + */ + // public static ArrayList rudeLex(String input, Set delimiters) {} + + /** + * We might as well implement a lexer for non-free-form languages, but whatever. We won't use it. + */ + public static ArrayList sensitiveLex(String input, Set delimiters) { + // int longestDelimiter = longestDelimiter(delimiters); + + ArrayList tokens = new ArrayList(); + String currentToken = ""; + // terrible c-style for loop because we may need to manipulate the index in the future + for (int i = 0; i < input.length(); i++) { + char nextToken = input.charAt(i); + if (delimiters.contains(nextToken)) { + if (!currentToken.equals("")) { + tokens.add(currentToken); + } + tokens.add(Character.toString(nextToken)); + currentToken = ""; + } else { + currentToken += input.charAt(i); + } + } + return tokens; + } +} -- cgit v1.2.3-70-g09d2