/
JavaCodeGenerator.template
217 lines (177 loc) · 6.02 KB
/
JavaCodeGenerator.template
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* This code was auto-generated by abnf-parser-generator.
*
* Changes to this file may cause incorrect behavior and will be lost if
* the code is regenerated.
*/
package generated;
import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;
public class Parser {
private static final int END_MARKER = 256;
private static final int MIN_RULE_ID = END_MARKER + 1;
private CompressedMatrix<Integer> gotoTable;
private CompressedMatrix<Action> actionTable;
private Dfa[] reverseDfas;
public Parser() {
@@InitGotoTable
@@InitActionTable
@@InitReverseDfas
}
public Object parse(InputStream stream) throws IOException, SyntaxError {
Stack<Integer> stateStack = new Stack<Integer>();
Stack<Integer> inputStack = new Stack<Integer>();
Stack<Object> objectStack = new Stack<Object>();
stateStack.push(0);
int c = stream.read();
if (c == -1) {
c = END_MARKER;
}
while (true) {
Action action = this.actionTable.get(stateStack.peek(), c);
if (action == null) {
throw new SyntaxError();
} else if (action.type() == ActionType.Shift) {
stateStack.push(action.state());
inputStack.push(c);
objectStack.push(c);
c = stream.read();
if (c == -1) {
c = END_MARKER;
}
} else if (action.type() == ActionType.Reduce) {
Dfa fa = this.reverseDfas[action.ruleId() - MIN_RULE_ID];
fa.setState(0);
Integer[] input = inputStack.toArray(new Integer[inputStack.size()]);
int lastMatchIndex = input.length;
for (int i = input.length - 1; i >= 0 && fa.step(input[i]); i--) {
if (fa.isMatch()) {
lastMatchIndex = i;
}
}
int bodyLength = input.length - lastMatchIndex;
Object[] body = new Object[bodyLength];
for (int i = bodyLength - 1; i >= 0; i--) {
inputStack.pop();
stateStack.pop();
body[i] = objectStack.pop();
}
stateStack.push(this.gotoTable.get(stateStack.peek(), action.ruleId()));
inputStack.push(action.ruleId());
objectStack.push(reduce(action.ruleId(), body));
} else if (action.type() == ActionType.Accept) {
break;
} else {
throw new SyntaxError();
}
}
return objectStack.pop();
}
protected Object reduce(int ruleId, Object[] body) {
return null;
}
public class SyntaxError extends Exception {
private static final long serialVersionUID = 1L;
public SyntaxError() {
super();
}
public SyntaxError(String message) {
super(message);
}
public SyntaxError(String message, Throwable cause) {
super(message, cause);
}
public SyntaxError(Throwable cause) {
super(cause);
}
}
private class Action {
private ActionType type;
private int data;
public Action(ActionType type, int data) {
this.type = type;
this.data = data;
}
public ActionType type() {
return this.type;
}
public int ruleId() {
return this.data;
}
public int state() {
return this.data;
}
}
private enum ActionType {
Shift, Reduce, Accept, Error
}
private class Dfa {
private CompressedMatrix<Integer> transitionTable;
private int[] acceptStates;
private int state;
public Dfa(CompressedMatrix<Integer> transitionTable, int[] acceptStates) {
this.transitionTable = transitionTable;
this.acceptStates = acceptStates;
this.state = 0;
}
public void setState(int state) {
this.state = state;
}
public boolean isMatch() {
int start = 0;
int end = this.acceptStates.length;
int mid = start + (end - start) / 2;
while (start < end) {
if (this.state < this.acceptStates[mid]) {
end = mid;
mid = start + (end - start) / 2;
} else if (this.state > this.acceptStates[mid]) {
start = mid + 1;
mid = start + (end - start) / 2;
} else {
break;
}
}
return this.state == this.acceptStates[mid];
}
public boolean step(int character) {
Integer newState = this.transitionTable.get(this.state, character);
if (newState == null) {
return false;
}
this.state = newState;
return true;
}
}
private class CompressedMatrix<T> {
private T[] a;
private int[] ia;
private int[] ja;
public CompressedMatrix(T[] a, int[] ia, int[] ja) {
this.a = a;
this.ia = ia;
this.ja = ja;
}
public T get(int x, int y) {
if (x >= this.ia.length - 1) {
return null;
}
int start = this.ia[x];
int end = this.ia[x + 1];
int mid = start + (end - start) / 2;
while (start < end) {
if (y < this.ja[mid]) {
end = mid;
mid = start + (end - start) / 2;
} else if (y > this.ja[mid]) {
start = mid + 1;
mid = start + (end - start) / 2;
} else {
break;
}
}
return (mid >= 0 && mid < this.ja.length && y == this.ja[mid]) ? this.a[mid] : null;
}
}
}