2007-01-23 Chris Toshok <toshok@ximian.com>
[mcs.git] / jay / skeleton.cs
blob8584cb24a18f1056d02da6038e9aed19aec2ff99
1 # jay skeleton
3 # character in column 1 determines outcome...
4 # # is a comment
5 # . is copied
6 # t is copied as //t if -t is set
7 # other lines are interpreted to call jay procedures
9 .// created by jay 0.7 (c) 1998 Axel.Schreiner@informatik.uni-osnabrueck.de
11 prolog ## %{ ... %} prior to the first %%
14 . /** error output stream.
15 . It should be changeable.
16 . */
17 . public System.IO.TextWriter ErrorOutput = System.Console.Out;
19 . /** simplified error message.
20 . @see <a href="#yyerror(java.lang.String, java.lang.String[])">yyerror</a>
21 . */
22 . public void yyerror (string message) {
23 . yyerror(message, null);
24 . }
26 . /** (syntax) error message.
27 . Can be overwritten to control message format.
28 . @param message text to be displayed.
29 . @param expected vector of acceptable tokens, if available.
30 . */
31 . public void yyerror (string message, string[] expected) {
32 . if ((yacc_verbose_flag > 0) && (expected != null) && (expected.Length > 0)) {
33 . ErrorOutput.Write (message+", expecting");
34 . for (int n = 0; n < expected.Length; ++ n)
35 . ErrorOutput.Write (" "+expected[n]);
36 . ErrorOutput.WriteLine ();
37 . } else
38 . ErrorOutput.WriteLine (message);
39 . }
41 . /** debugging support, requires the package jay.yydebug.
42 . Set to null to suppress debugging messages.
43 . */
44 t internal yydebug.yyDebug debug;
46 debug ## tables for debugging support
48 . /** index-checked interface to yyNames[].
49 . @param token single character or %token value.
50 . @return token name or [illegal] or [unknown].
51 . */
52 t public static string yyname (int token) {
53 t if ((token < 0) || (token > yyNames.Length)) return "[illegal]";
54 t string name;
55 t if ((name = yyNames[token]) != null) return name;
56 t return "[unknown]";
57 t }
59 . /** computes list of expected tokens on error by tracing the tables.
60 . @param state for which to compute the list.
61 . @return list of token names.
62 . */
63 . protected string[] yyExpecting (int state) {
64 . int token, n, len = 0;
65 . bool[] ok = new bool[yyNames.Length];
67 . if ((n = yySindex[state]) != 0)
68 . for (token = n < 0 ? -n : 0;
69 . (token < yyNames.Length) && (n+token < yyTable.Length); ++ token)
70 . if (yyCheck[n+token] == token && !ok[token] && yyNames[token] != null) {
71 . ++ len;
72 . ok[token] = true;
73 . }
74 . if ((n = yyRindex[state]) != 0)
75 . for (token = n < 0 ? -n : 0;
76 . (token < yyNames.Length) && (n+token < yyTable.Length); ++ token)
77 . if (yyCheck[n+token] == token && !ok[token] && yyNames[token] != null) {
78 . ++ len;
79 . ok[token] = true;
80 . }
82 . string [] result = new string[len];
83 . for (n = token = 0; n < len; ++ token)
84 . if (ok[token]) result[n++] = yyNames[token];
85 . return result;
86 . }
88 . /** the generated parser, with debugging messages.
89 . Maintains a state and a value stack, currently with fixed maximum size.
90 . @param yyLex scanner.
91 . @param yydebug debug message writer implementing yyDebug, or null.
92 . @return result of the last reduction, if any.
93 . @throws yyException on irrecoverable parse error.
94 . */
95 . internal Object yyparse (yyParser.yyInput yyLex, Object yyd)
96 . {
97 t this.debug = (yydebug.yyDebug)yyd;
98 . return yyparse(yyLex);
99 . }
101 . /** initial size and increment of the state/value stack [default 256].
102 . This is not final so that it can be overwritten outside of invocations
103 . of yyparse().
104 . */
105 . protected int yyMax;
107 . /** executed at the beginning of a reduce action.
108 . Used as $$ = yyDefault($1), prior to the user-specified action, if any.
109 . Can be overwritten to provide deep copy, etc.
110 . @param first value for $1, or null.
111 . @return first.
112 . */
113 . protected Object yyDefault (Object first) {
114 . return first;
117 . /** the generated parser.
118 . Maintains a state and a value stack, currently with fixed maximum size.
119 . @param yyLex scanner.
120 . @return result of the last reduction, if any.
121 . @throws yyException on irrecoverable parse error.
122 . */
123 . internal Object yyparse (yyParser.yyInput yyLex)
125 . if (yyMax <= 0) yyMax = 256; // initial size
126 . int yyState = 0; // state stack ptr
127 . int [] yyStates = new int[yyMax]; // state stack
128 . Object yyVal = null; // value stack ptr
129 . Object [] yyVals = new Object[yyMax]; // value stack
130 . int yyToken = -1; // current input
131 . int yyErrorFlag = 0; // #tks to shift
133 local ## %{ ... %} after the first %%
135 . /*yyLoop:*/ for (int yyTop = 0;; ++ yyTop) {
136 . if (yyTop >= yyStates.Length) { // dynamically increase
137 . int[] i = new int[yyStates.Length+yyMax];
138 . yyStates.CopyTo (i, 0);
139 . yyStates = i;
140 . Object[] o = new Object[yyVals.Length+yyMax];
141 . yyVals.CopyTo (o, 0);
142 . yyVals = o;
144 . yyStates[yyTop] = yyState;
145 . yyVals[yyTop] = yyVal;
146 t if (debug != null) debug.push(yyState, yyVal);
148 . /*yyDiscarded:*/ for (;;) { // discarding a token does not change stack
149 . int yyN;
150 . if ((yyN = yyDefRed[yyState]) == 0) { // else [default] reduce (yyN)
151 . if (yyToken < 0) {
152 . yyToken = yyLex.advance() ? yyLex.token() : 0;
154 t if (debug != null)
155 t debug.lex(yyState, yyToken, yyname(yyToken), yyLex.value());
157 . if ((yyN = yySindex[yyState]) != 0 && ((yyN += yyToken) >= 0)
158 . && (yyN < yyTable.Length) && (yyCheck[yyN] == yyToken)) {
159 t if (debug != null)
160 t debug.shift(yyState, yyTable[yyN], yyErrorFlag-1);
161 . yyState = yyTable[yyN]; // shift to yyN
162 . yyVal = yyLex.value();
163 . yyToken = -1;
164 . if (yyErrorFlag > 0) -- yyErrorFlag;
165 . goto continue_yyLoop;
167 . if ((yyN = yyRindex[yyState]) != 0 && (yyN += yyToken) >= 0
168 . && yyN < yyTable.Length && yyCheck[yyN] == yyToken)
169 . yyN = yyTable[yyN]; // reduce (yyN)
170 . else
171 . switch (yyErrorFlag) {
173 . case 0:
174 . // yyerror(String.Format ("syntax error, got token `{0}'", yyname (yyToken)), yyExpecting(yyState));
175 t if (debug != null) debug.error("syntax error");
176 . goto case 1;
177 . case 1: case 2:
178 . yyErrorFlag = 3;
179 . do {
180 . if ((yyN = yySindex[yyStates[yyTop]]) != 0
181 . && (yyN += Token.yyErrorCode) >= 0 && yyN < yyTable.Length
182 . && yyCheck[yyN] == Token.yyErrorCode) {
183 t if (debug != null)
184 t debug.shift(yyStates[yyTop], yyTable[yyN], 3);
185 . yyState = yyTable[yyN];
186 . yyVal = yyLex.value();
187 . goto continue_yyLoop;
189 t if (debug != null) debug.pop(yyStates[yyTop]);
190 . } while (-- yyTop >= 0);
191 t if (debug != null) debug.reject();
192 . throw new yyParser.yyException("irrecoverable syntax error");
194 . case 3:
195 . if (yyToken == 0) {
196 t if (debug != null) debug.reject();
197 . throw new yyParser.yyException("irrecoverable syntax error at end-of-file");
199 t if (debug != null)
200 t debug.discard(yyState, yyToken, yyname(yyToken),
201 t yyLex.value());
202 . yyToken = -1;
203 . goto continue_yyDiscarded; // leave stack alone
206 . int yyV = yyTop + 1-yyLen[yyN];
207 t if (debug != null)
208 t debug.reduce(yyState, yyStates[yyV-1], yyN, YYRules.getRule (yyN), yyLen[yyN]);
209 . yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]);
210 . switch (yyN) {
212 actions ## code from the actions within the grammar
215 . yyTop -= yyLen[yyN];
216 . yyState = yyStates[yyTop];
217 . int yyM = yyLhs[yyN];
218 . if (yyState == 0 && yyM == 0) {
219 t if (debug != null) debug.shift(0, yyFinal);
220 . yyState = yyFinal;
221 . if (yyToken < 0) {
222 . yyToken = yyLex.advance() ? yyLex.token() : 0;
224 t if (debug != null)
225 t debug.lex(yyState, yyToken,yyname(yyToken), yyLex.value());
227 . if (yyToken == 0) {
228 t if (debug != null) debug.accept(yyVal);
229 . return yyVal;
231 . goto continue_yyLoop;
233 . if (((yyN = yyGindex[yyM]) != 0) && ((yyN += yyState) >= 0)
234 . && (yyN < yyTable.Length) && (yyCheck[yyN] == yyState))
235 . yyState = yyTable[yyN];
236 . else
237 . yyState = yyDgoto[yyM];
238 t if (debug != null) debug.shift(yyStates[yyTop], yyState);
239 . goto continue_yyLoop;
240 . continue_yyDiscarded: continue; // implements the named-loop continue: 'continue yyDiscarded'
242 . continue_yyLoop: continue; // implements the named-loop continue: 'continue yyLoop'
246 tables ## tables for rules, default reduction, and action calls
248 epilog ## text following second %%
249 .namespace yydebug {
250 . using System;
251 . internal interface yyDebug {
252 . void push (int state, Object value);
253 . void lex (int state, int token, string name, Object value);
254 . void shift (int from, int to, int errorFlag);
255 . void pop (int state);
256 . void discard (int state, int token, string name, Object value);
257 . void reduce (int from, int to, int rule, string text, int len);
258 . void shift (int from, int to);
259 . void accept (Object value);
260 . void error (string message);
261 . void reject ();
264 . class yyDebugSimple : yyDebug {
265 . void println (string s){
266 . Console.Error.WriteLine (s);
269 . public void push (int state, Object value) {
270 . println ("push\tstate "+state+"\tvalue "+value);
273 . public void lex (int state, int token, string name, Object value) {
274 . println("lex\tstate "+state+"\treading "+name+"\tvalue "+value);
277 . public void shift (int from, int to, int errorFlag) {
278 . switch (errorFlag) {
279 . default: // normally
280 . println("shift\tfrom state "+from+" to "+to);
281 . break;
282 . case 0: case 1: case 2: // in error recovery
283 . println("shift\tfrom state "+from+" to "+to
284 . +"\t"+errorFlag+" left to recover");
285 . break;
286 . case 3: // normally
287 . println("shift\tfrom state "+from+" to "+to+"\ton error");
288 . break;
292 . public void pop (int state) {
293 . println("pop\tstate "+state+"\ton error");
296 . public void discard (int state, int token, string name, Object value) {
297 . println("discard\tstate "+state+"\ttoken "+name+"\tvalue "+value);
300 . public void reduce (int from, int to, int rule, string text, int len) {
301 . println("reduce\tstate "+from+"\tuncover "+to
302 . +"\trule ("+rule+") "+text);
305 . public void shift (int from, int to) {
306 . println("goto\tfrom state "+from+" to "+to);
309 . public void accept (Object value) {
310 . println("accept\tvalue "+value);
313 . public void error (string message) {
314 . println("error\t"+message);
317 . public void reject () {
318 . println("reject");
323 .// %token constants
324 . class Token {
325 tokens public const int
327 . namespace yyParser {
328 . using System;
329 . /** thrown for irrecoverable syntax errors and stack overflow.
330 . */
331 . internal class yyException : System.Exception {
332 . public yyException (string message) : base (message) {
336 . /** must be implemented by a scanner object to supply input to the parser.
337 . */
338 . internal interface yyInput {
339 . /** move on to next token.
340 . @return false if positioned beyond tokens.
341 . @throws IOException on input error.
342 . */
343 . bool advance (); // throws java.io.IOException;
344 . /** classifies current token.
345 . Should not be called if advance() returned false.
346 . @return current %token or single character.
347 . */
348 . int token ();
349 . /** associated with current token.
350 . Should not be called if advance() returned false.
351 . @return value for token().
352 . */
353 . Object value ();
356 .} // close outermost namespace, that MUST HAVE BEEN opened in the prolog