Below is Lexer.java, Token.java, StringHandler.java, and Main.java. I need help in writing test cases. Please write unit tests for lexer.java and make sure to show the full code with the screenshot of the output. Make sure to include the test cases as well. Lexer.java import java.util.LinkedList; public class Lexer { private StringHandler stringHandler; private int lineNumber; private int charPosition; public Lexer(String input) { stringHandler = new StringHandler(input); lineNumber = 1; charPosition = 0; } public LinkedList lex() { LinkedList tokens = new LinkedList<>(); while (!stringHandler.isDone()) { char c = stringHandler.peek(0); if (c == ' ' || c == '\t') { stringHandler.swallow(1); charPosition++; } else if (c == '\n') { tokens.add(new Token(TokenType.SEPARATOR, lineNumber, charPosition)); stringHandler.swallow(1); lineNumber++; charPosition = 0; } else if (Character.isLetter(c)) { tokens.add(processWord()); } else if (Character.isDigit(c)) { tokens.add(processNumber()); } else { throw new RuntimeException("Unrecognized character: " + c); } } return tokens; } private Token processWord() { StringBuilder value = new StringBuilder(); while (!stringHandler.isDone() && (Character.isLetterOrDigit(stringHandler.peek(0)) || stringHandler.peek(0) == '_' || stringHandler.peek(0) == ',')) { value.append(stringHandler.getChar()); charPosition++; } return new Token(TokenType.WORD, value.toString(), lineNumber, charPosition - value.length()); } private Token processNumber() { StringBuilder value = new StringBuilder(); while (!stringHandler.isDone() && (Character.isDigit(stringHandler.peek(0)) || stringHandler.peek(0) == '.')) { value.append(stringHandler.getChar()); charPosition++; } return new Token(TokenType.NUMBER, value.toString(), lineNumber, charPosition - value.length()); } } Token.java enum TokenType { WORD, NUMBER, SEPARATOR } public class Token { private TokenType type; private String value; private int lineNumber; private int charPosition; public Token(TokenType type, int lineNumber, int charPosition) { this.type = type; this.lineNumber = lineNumber; this.charPosition = charPosition; } public Token(TokenType type, String value, int lineNumber, int charPosition) { this(type, lineNumber, charPosition); this.value = value; } public String toString() { if (value == null) { return type.toString(); } else { return type.toString() + "(" + value + ")"; } } } Main.java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.LinkedList; public class Main { public static void main(String[] args) { if (args.length != 1) { System.out.println("Usage: java Main "); return; } try { String content = new String(Files.readAllBytes(Paths.get(args[0]))); Lexer lexer = new Lexer(content); LinkedList tokens = lexer.lex(); for (Token token : tokens) { System.out.println(token); } } catch (IOException e) { e.printStackTrace(); } } } StringHandler.java public class StringHandler { private String document; private int index; public StringHandler(String document) { this.document = document; this.index = 0; } public char peek(int i) { return document.charAt(index + i); } public String peekString(int i) { return document.substring(index, index + i); } public char getChar() { char c = document.charAt(index); index++; return c; } public void swallow(int i) { index += i; } public boolean isDone() { return index >= document.length(); } public String remainder() { return document.substring(index); }
Below is Lexer.java, Token.java, StringHandler.java, and Main.java. I need help in writing test cases. Please write unit tests for lexer.java and make sure to show the full code with the screenshot of the output. Make sure to include the test cases as well. Lexer.java import java.util.LinkedList; public class Lexer { private StringHandler stringHandler; private int lineNumber; private int charPosition; public Lexer(String input) { stringHandler = new StringHandler(input); lineNumber = 1; charPosition = 0; } public LinkedList lex() { LinkedList tokens = new LinkedList<>(); while (!stringHandler.isDone()) { char c = stringHandler.peek(0); if (c == ' ' || c == '\t') { stringHandler.swallow(1); charPosition++; } else if (c == '\n') { tokens.add(new Token(TokenType.SEPARATOR, lineNumber, charPosition)); stringHandler.swallow(1); lineNumber++; charPosition = 0; } else if (Character.isLetter(c)) { tokens.add(processWord()); } else if (Character.isDigit(c)) { tokens.add(processNumber()); } else { throw new RuntimeException("Unrecognized character: " + c); } } return tokens; } private Token processWord() { StringBuilder value = new StringBuilder(); while (!stringHandler.isDone() && (Character.isLetterOrDigit(stringHandler.peek(0)) || stringHandler.peek(0) == '_' || stringHandler.peek(0) == ',')) { value.append(stringHandler.getChar()); charPosition++; } return new Token(TokenType.WORD, value.toString(), lineNumber, charPosition - value.length()); } private Token processNumber() { StringBuilder value = new StringBuilder(); while (!stringHandler.isDone() && (Character.isDigit(stringHandler.peek(0)) || stringHandler.peek(0) == '.')) { value.append(stringHandler.getChar()); charPosition++; } return new Token(TokenType.NUMBER, value.toString(), lineNumber, charPosition - value.length()); } } Token.java enum TokenType { WORD, NUMBER, SEPARATOR } public class Token { private TokenType type; private String value; private int lineNumber; private int charPosition; public Token(TokenType type, int lineNumber, int charPosition) { this.type = type; this.lineNumber = lineNumber; this.charPosition = charPosition; } public Token(TokenType type, String value, int lineNumber, int charPosition) { this(type, lineNumber, charPosition); this.value = value; } public String toString() { if (value == null) { return type.toString(); } else { return type.toString() + "(" + value + ")"; } } } Main.java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.LinkedList; public class Main { public static void main(String[] args) { if (args.length != 1) { System.out.println("Usage: java Main "); return; } try { String content = new String(Files.readAllBytes(Paths.get(args[0]))); Lexer lexer = new Lexer(content); LinkedList tokens = lexer.lex(); for (Token token : tokens) { System.out.println(token); } } catch (IOException e) { e.printStackTrace(); } } } StringHandler.java public class StringHandler { private String document; private int index; public StringHandler(String document) { this.document = document; this.index = 0; } public char peek(int i) { return document.charAt(index + i); } public String peekString(int i) { return document.substring(index, index + i); } public char getChar() { char c = document.charAt(index); index++; return c; } public void swallow(int i) { index += i; } public boolean isDone() { return index >= document.length(); } public String remainder() { return document.substring(index); }
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Below is Lexer.java, Token.java, StringHandler.java, and Main.java. I need help in writing test cases. Please write unit tests for lexer.java and make sure to show the full code with the screenshot of the output. Make sure to include the test cases as well.
Lexer.java
import java.util.LinkedList;
public class Lexer {
private StringHandler stringHandler;
private int lineNumber;
private int charPosition;
public Lexer(String input) {
stringHandler = new StringHandler(input);
lineNumber = 1;
charPosition = 0;
}
public LinkedList<Token> lex() {
LinkedList<Token> tokens = new LinkedList<>();
while (!stringHandler.isDone()) {
char c = stringHandler.peek(0);
if (c == ' ' || c == '\t') {
stringHandler.swallow(1);
charPosition++;
} else if (c == '\n') {
tokens.add(new Token(TokenType.SEPARATOR, lineNumber, charPosition));
stringHandler.swallow(1);
lineNumber++;
charPosition = 0;
} else if (Character.isLetter(c)) {
tokens.add(processWord());
} else if (Character.isDigit(c)) {
tokens.add(processNumber());
} else {
throw new RuntimeException("Unrecognized character: " + c);
}
}
return tokens;
}
private Token processWord() {
StringBuilder value = new StringBuilder();
while (!stringHandler.isDone() && (Character.isLetterOrDigit(stringHandler.peek(0)) || stringHandler.peek(0) == '_' || stringHandler.peek(0) == ',')) {
value.append(stringHandler.getChar());
charPosition++;
}
return new Token(TokenType.WORD, value.toString(), lineNumber, charPosition - value.length());
}
private Token processNumber() {
StringBuilder value = new StringBuilder();
while (!stringHandler.isDone() && (Character.isDigit(stringHandler.peek(0)) || stringHandler.peek(0) == '.')) {
value.append(stringHandler.getChar());
charPosition++;
}
return new Token(TokenType.NUMBER, value.toString(), lineNumber, charPosition - value.length());
}
}
Token.java
enum TokenType {
WORD, NUMBER, SEPARATOR
}
public class Token {
private TokenType type;
private String value;
private int lineNumber;
private int charPosition;
public Token(TokenType type, int lineNumber, int charPosition) {
this.type = type;
this.lineNumber = lineNumber;
this.charPosition = charPosition;
}
public Token(TokenType type, String value, int lineNumber, int charPosition) {
this(type, lineNumber, charPosition);
this.value = value;
}
public String toString() {
if (value == null) {
return type.toString();
} else {
return type.toString() + "(" + value + ")";
}
}
}
Main.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java Main <filename>");
return;
}
try {
String content = new String(Files.readAllBytes(Paths.get(args[0])));
Lexer lexer = new Lexer(content);
LinkedList<Token> tokens = lexer.lex();
for (Token token : tokens) {
System.out.println(token);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
StringHandler.java
public class StringHandler {
private String document;
private int index;
public StringHandler(String document) {
this.document = document;
this.index = 0;
}
public char peek(int i) {
return document.charAt(index + i);
}
public String peekString(int i) {
return document.substring(index, index + i);
}
public char getChar() {
char c = document.charAt(index);
index++;
return c;
}
public void swallow(int i) {
index += i;
}
public boolean isDone() {
return index >= document.length();
}
public String remainder() {
return document.substring(index);
}
Expert Solution
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education