From 6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Thu, 2 Jul 2026 11:55:17 -0700 Subject: Init --- .../project-three/parser/ParserException.java | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Homework/cs5300/project-three/parser/ParserException.java (limited to 'Homework/cs5300/project-three/parser/ParserException.java') diff --git a/Homework/cs5300/project-three/parser/ParserException.java b/Homework/cs5300/project-three/parser/ParserException.java new file mode 100644 index 0000000..1c32080 --- /dev/null +++ b/Homework/cs5300/project-three/parser/ParserException.java @@ -0,0 +1,81 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package parser; + +import java.util.ArrayList; +import org.antlr.v4.runtime.Token; + +/** + * + * @author edwajohn + */ +public class ParserException extends Exception { + + /** + * Create a ParserException. This is a convenience factory method to easily + * format your output to meet project specification. + * + * @param words the words being parsed + * @param i the index to the current word being parsed + * @return + */ + public static ParserException create(final ArrayList words, final int i) { + String msg; + if (i < words.size()) { + msg = "Error parsing token \"" + words.get(i).getText() + "\" in \n "; + int preLen = 4; + for (int j = 0; j < i; ++j) { + Token token = words.get(j); + msg = msg + token.getText() + " "; + preLen += token.getText().length() + 1; + } + final int curLen = words.get(i).getText().length(); + msg = msg + words.get(i).getText(); + if (i < words.size() - 1) { + msg = msg + " "; + } + for (int j = i + 1; j < words.size(); ++j) { + Token token = words.get(j); + msg = msg + token.getText(); + if (j < words.size() - 1) { + msg = msg + " "; + } + } + + msg = msg + "\n"; + for (int j = 0; j < preLen; ++j) { + msg += " "; + } + for (int j = 0; j < curLen; ++j) { + msg += "^"; + } + } else { + msg = "Unexpectedly reached end of file in\n"; + for (int j = 0; j < words.size(); ++j) { + Token token = words.get(j); + msg = msg + token.getText(); + if (j < words.size() - 1) { + msg = msg + " "; + } + } + + } + return new ParserException(msg); + } + + /** + * Constructor. You will most likely NOT use this constructor directly, but + * rather use the convenience factory method create(). You may choose to use + * this constructor, however, if your data structures are set up differently + * than what the factory method expects. + * + * @param msg + */ + public ParserException(String msg) { + super(msg); + } + +} -- cgit v1.3