-
Notifications
You must be signed in to change notification settings - Fork 0
/
GollyRleFileLoader.java
206 lines (159 loc) · 4.68 KB
/
GollyRleFileLoader.java
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.util.ArrayList;
import java.util.regex.*;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.TerminalNode;
public class GollyRleFileLoader extends GollyRleBaseListener
{
/******* members *********/
private ArrayList<String> errors;
GollyRleConfiguration config;
private static final char startingPrefix = 'p';
private static final char startingActiveState = 'A';
private static final int states = 24;
/******* utilities ********/
public int translatePrefix(String prefix)
{
/* getting a char, btw the string is assumed to be of length 1 */
char p = prefix.charAt(0);
return p - startingPrefix + 1;
}
public int translateCellState(String state)
{
/* getting a char, btw the string is assumed to be of length 1 */
char s = state.charAt(0);
return s - startingActiveState + 1;
}
public int translateCellState(String prefix, String state)
{
int p = 0;
int c = translateCellState(state);
p = translatePrefix(prefix);
return p * states + c;
}
/******* constructors *******/
GollyRleFileLoader()
{
this.config = new GollyRleConfiguration();
}
/******* accessors *********/
public GollyRleConfiguration getConfiguration()
{
return config;
}
/******* listeners overridding ********/
public void enterRow(GollyRleParser.RowContext ctx)
{
/* Entering a row, adding a new one to the matrix */
config.addMatrixRow();
}
public void enterFinalRow(GollyRleParser.FinalRowContext ctx)
{
/* forget me not: parser rule 'finalRow' could contain another row...
or *just* cell patterns! In this case new row is needed before proceeding
*/
if (ctx.END_PATTERN() != null)
{
config.addMatrixRow();
}
}
public void exitHeight(GollyRleParser.HeightContext ctx)
{
String val = ctx.UINT().getText();
int height = Integer.parseInt(val);
config.setMatrixHeight(height);
//System.out.println("HEIGHT: " + height);
}
public void exitWidth(GollyRleParser.WidthContext ctx)
{
String val = ctx.UINT().getText();
int width = Integer.parseInt(val);
config.setMatrixWidth(width);
//System.out.println("WIDTH: " + width);
}
public void exitHeader(GollyRleParser.HeaderContext ctx)
{
config.initMatrix();
/*
this is cumbersome, need to get the text of the whole header
in order to get the rule name
*/
String ruleString = ctx.CA_RULE().getText();
String[] splits = ruleString.split("=");
String ruleName = splits[1].trim();
config.setRule(ruleName);
}
public void exitRle(GollyRleParser.RleContext ctx)
{
// config.checkMatrixIntegrity();
// config.drawMatrix();
}
public void exitCellPattern(GollyRleParser.CellPatternContext ctx)
{
/* Exiting cellPattern, cellMultiplier and cellState are known */
int cellMult;
if (ctx.UINT() != null)
{
String mult = ctx.UINT().getText();
cellMult = Integer.parseInt(mult);
}
else
{
cellMult = 1;
}
// int cellMult = Integer.parseInt(mult);
int cellState = config.getCellState();
//System.out.println("--> Adding " + cellMult + " cell" + (cellMult==1?"":"s") + " with value: " + cellState);
/* Adding cells */
for (int i = 0; i < cellMult; ++i)
{
config.addMatrixCell(cellState);
}
}
public void exitEndRow(GollyRleParser.EndRowContext ctx)
{
int emptyRowMultiplier;
if (ctx.UINT() != null)
{
String mult = ctx.UINT().getText();
emptyRowMultiplier = Integer.parseInt(mult) - 1;
}
else
{
emptyRowMultiplier = 0;
}
/* Adding (if present) empty rows */
config.addEmptyMatrixRow(emptyRowMultiplier);
}
public void exitSingleActive(GollyRleParser.SingleActiveContext ctx)
{
String state = ctx.SINGLE_ACTIVE_STATE().getText();
config.setCellState(1);
}
public void exitSingleInactive(GollyRleParser.SingleInactiveContext ctx)
{
String state = ctx.SINGLE_INACTIVE_STATE().getText();
config.setCellState(0);
}
public void exitMultiActive(GollyRleParser.MultiActiveContext ctx)
{
String state = ctx.MULTI_ACTIVE_STATE().getText();
int cellState;
if (ctx.PREFIX_STATE() != null)
{
String prefix = ctx.PREFIX_STATE().getText();
cellState = translateCellState(prefix, state);
}
else
{
cellState = translateCellState(state);
}
config.setCellState(cellState);
}
public void exitMultiInactive(GollyRleParser.MultiInactiveContext ctx)
{
String state = ctx.MULTI_INACTIVE_STATE().getText();
config.setCellState(0);
}
}