summaryrefslogtreecommitdiff
path: root/Homework/cs5300/project-two/scanner/TableReader.java
blob: a074547d64151addfc239c627dcacae7976c35bb (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Do not modify this file.
 */
package scanner;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * TableParser provides all functionality for parsing the table definition file.
 *
 * Do not modify this file.
 */
public final class TableReader {

  // CharCat class
  // Used in the classifier table
  public class CharCat {

    final private char c;
    final private String category;

    public CharCat(char c, String category) {
      this.c = c;
      this.category = category;
    }

    public char getC() {
      return c;
    }

    public String getCategory() {
      return category;
    }

  }

  // Transition class
  // Used in the transition table
  public class Transition {

    final private String fromState;
    final private String category;
    final private String toState;

    public Transition(String fromState, String category, String toState) {
      this.fromState = fromState;
      this.category = category;
      this.toState = toState;
    }

    public String getFromStateName() {
      return fromState;
    }

    public String getCategory() {
      return category;
    }

    public String getToStateName() {
      return toState;
    }
  }

  // TokenType class
  // Used in the token type table
  public class TokenType {

    final private String state;
    final private String type;

    public TokenType(String state, String type) {
      this.state = state;
      this.type = type;
    }

    public String getState() {
      return state;
    }

    public String getType() {
      return type;
    }
  }

  private enum WhichTable {

    CLASS, TRANS, TOKEN
  };

  private ArrayList<CharCat> classifier;
  private ArrayList<Transition> transitions;
  private ArrayList<TokenType> tokens;

  public TableReader(String filename) throws IOException {
    parse(filename);
  }

  public Iterable<CharCat> getClassifier() {
    return classifier;
  }

  public Iterable<Transition> getTransitions() {
    return transitions;
  }

  public Iterable<TokenType> getTokens() {
    return tokens;
  }

  public void parse(String fn) throws FileNotFoundException, IOException {
    this.classifier = new ArrayList<>();
    this.transitions = new ArrayList<>();
    this.tokens = new ArrayList<>();

    WhichTable table = WhichTable.CLASS;
    try (BufferedReader br = new BufferedReader(new FileReader(fn))) {
      String line;
      while ((line = br.readLine()) != null) {
        final int idx = line.indexOf("//");
        if (idx > -1) {
          line = line.substring(0, idx);
        }
        line = line.trim();
        if (!line.isEmpty()) {
          if (line.equals("ClassifierTable")) {
            table = WhichTable.CLASS;
          } else if (line.equals("TransitionTable")) {
            table = WhichTable.TRANS;
          } else if (line.equals("TokenTypeTable")) {
            table = WhichTable.TOKEN;
          } else {
            String[] lineTokens = line.split(" ");
            if (table == WhichTable.CLASS) {
              char c = lineTokens[0].charAt(0);
              if (lineTokens[0].equals("\\space")) {
                c = ' ';
              } else if (lineTokens[0].equals("\\t")) {
                c = '\t';
              } else if (lineTokens[0].equals("\\n")) {
                c = '\n';
              }
              CharCat cc = new CharCat(c, lineTokens[1]);
              classifier.add(cc);
            } else if (table == WhichTable.TRANS) {
              Transition t = new Transition(lineTokens[0], lineTokens[1], lineTokens[2]);
              transitions.add(t);
            } else if (table == WhichTable.TOKEN) {
              TokenType tt = new TokenType(lineTokens[0], lineTokens[1]);
              tokens.add(tt);
            }
          }
        }
      }
    }
  }
}