summaryrefslogtreecommitdiff
path: root/Homework/cs5300/project-three/parser/ParserException.java
blob: 1c320808158113b131ffcb44834e8c36c6e8c1d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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<? extends Token> 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);
  }

}