2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / jay / skeleton.cs
blobdb18294b1b27a8dec4392c2e5f1b97c87240bef5
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 . /* An EOF token */
27 . public int eof_token;
29 . /** (syntax) error message.
30 . Can be overwritten to control message format.
31 . @param message text to be displayed.
32 . @param expected vector of acceptable tokens, if available.
33 . */
34 . public void yyerror (string message, string[] expected) {
35 . if ((yacc_verbose_flag > 0) && (expected != null) && (expected.Length > 0)) {
36 . ErrorOutput.Write (message+", expecting");
37 . for (int n = 0; n < expected.Length; ++ n)
38 . ErrorOutput.Write (" "+expected[n]);
39 . ErrorOutput.WriteLine ();
40 . } else
41 . ErrorOutput.WriteLine (message);
42 . }
44 . /** debugging support, requires the package jay.yydebug.
45 . Set to null to suppress debugging messages.
46 . */
47 t internal yydebug.yyDebug debug;
49 debug ## tables for debugging support
51 . /** index-checked interface to yyNames[].
52 . @param token single character or %token value.
53 . @return token name or [illegal] or [unknown].
54 . */
55 t public static string yyname (int token) {
56 t if ((token < 0) || (token > yyNames.Length)) return "[illegal]";
57 t string name;
58 t if ((name = yyNames[token]) != null) return name;
59 t return "[unknown]";
60 t }
62 . int yyExpectingState;
63 . /** computes list of expected tokens on error by tracing the tables.
64 . @param state for which to compute the list.
65 . @return list of token names.
66 . */
67 . protected int [] yyExpectingTokens (int state){
68 . int token, n, len = 0;
69 . bool[] ok = new bool[yyNames.Length];
70 . if ((n = yySindex[state]) != 0)
71 . for (token = n < 0 ? -n : 0;
72 . (token < yyNames.Length) && (n+token < yyTable.Length); ++ token)
73 . if (yyCheck[n+token] == token && !ok[token] && yyNames[token] != null) {
74 . ++ len;
75 . ok[token] = true;
76 . }
77 . if ((n = yyRindex[state]) != 0)
78 . for (token = n < 0 ? -n : 0;
79 . (token < yyNames.Length) && (n+token < yyTable.Length); ++ token)
80 . if (yyCheck[n+token] == token && !ok[token] && yyNames[token] != null) {
81 . ++ len;
82 . ok[token] = true;
83 . }
84 . int [] result = new int [len];
85 . for (n = token = 0; n < len; ++ token)
86 . if (ok[token]) result[n++] = token;
87 . return result;
88 . }
89 . protected string[] yyExpecting (int state) {
90 . int [] tokens = yyExpectingTokens (state);
91 . string [] result = new string[tokens.Length];
92 . for (int n = 0; n < tokens.Length; n++)
93 . result[n++] = yyNames[tokens [n]];
94 . return result;
95 . }
97 . /** the generated parser, with debugging messages.
98 . Maintains a state and a value stack, currently with fixed maximum size.
99 . @param yyLex scanner.
100 . @param yydebug debug message writer implementing yyDebug, or null.
101 . @return result of the last reduction, if any.
102 . @throws yyException on irrecoverable parse error.
103 . */
104 . internal Object yyparse (yyParser.yyInput yyLex, Object yyd)
106 t this.debug = (yydebug.yyDebug)yyd;
107 . return yyparse(yyLex);
110 . /** initial size and increment of the state/value stack [default 256].
111 . This is not final so that it can be overwritten outside of invocations
112 . of yyparse().
113 . */
114 . protected int yyMax;
116 . /** executed at the beginning of a reduce action.
117 . Used as $$ = yyDefault($1), prior to the user-specified action, if any.
118 . Can be overwritten to provide deep copy, etc.
119 . @param first value for $1, or null.
120 . @return first.
121 . */
122 . protected Object yyDefault (Object first) {
123 . return first;
126 . static int[] global_yyStates;
127 . static object[] global_yyVals;
128 . protected bool use_global_stacks;
130 . /** the generated parser.
131 . Maintains a state and a value stack, currently with fixed maximum size.
132 . @param yyLex scanner.
133 . @return result of the last reduction, if any.
134 . @throws yyException on irrecoverable parse error.
135 . */
136 . internal Object yyparse (yyParser.yyInput yyLex)
138 . if (yyMax <= 0) yyMax = 256; // initial size
139 . int yyState = 0; // state stack ptr
140 . int [] yyStates; // state stack
141 . Object yyVal = null; // value stack ptr
142 . Object [] yyVals; // value stack
143 . int yyToken = -1; // current input
144 . int yyErrorFlag = 0; // #tks to shift
145 . if (use_global_stacks && global_yyStates != null) {
146 . yyVals = global_yyVals;
147 . yyStates = global_yyStates;
148 . } else {
149 . yyVals = new object [yyMax];
150 . yyStates = new int [yyMax];
151 . if (use_global_stacks) {
152 . global_yyVals = yyVals;
153 . global_yyStates = yyStates;
157 local ## %{ ... %} after the first %%
159 . /*yyLoop:*/ for (int yyTop = 0;; ++ yyTop) {
160 . if (yyTop >= yyStates.Length) { // dynamically increase
161 . global::System.Array.Resize (ref yyStates, yyStates.Length+yyMax);
162 . global::System.Array.Resize (ref yyVals, yyVals.Length+yyMax);
164 . yyStates[yyTop] = yyState;
165 . yyVals[yyTop] = yyVal;
166 t if (debug != null) debug.push(yyState, yyVal);
168 . /*yyDiscarded:*/ for (;;) { // discarding a token does not change stack
169 . int yyN;
170 . if ((yyN = yyDefRed[yyState]) == 0) { // else [default] reduce (yyN)
171 . if (yyToken < 0) {
172 . yyToken = yyLex.advance() ? yyLex.token() : 0;
174 t if (debug != null)
175 t debug.lex(yyState, yyToken, yyname(yyToken), yyLex.value());
177 . if ((yyN = yySindex[yyState]) != 0 && ((yyN += yyToken) >= 0)
178 . && (yyN < yyTable.Length) && (yyCheck[yyN] == yyToken)) {
179 t if (debug != null)
180 t debug.shift(yyState, yyTable[yyN], yyErrorFlag-1);
181 . yyState = yyTable[yyN]; // shift to yyN
182 . yyVal = yyLex.value();
183 . yyToken = -1;
184 . if (yyErrorFlag > 0) -- yyErrorFlag;
185 . goto continue_yyLoop;
187 . if ((yyN = yyRindex[yyState]) != 0 && (yyN += yyToken) >= 0
188 . && yyN < yyTable.Length && yyCheck[yyN] == yyToken)
189 . yyN = yyTable[yyN]; // reduce (yyN)
190 . else
191 . switch (yyErrorFlag) {
193 . case 0:
194 . yyExpectingState = yyState;
195 . // yyerror(String.Format ("syntax error, got token `{0}'", yyname (yyToken)), yyExpecting(yyState));
196 t if (debug != null) debug.error("syntax error");
197 . if (yyToken == 0 /*eof*/ || yyToken == eof_token) throw new yyParser.yyUnexpectedEof ();
198 . goto case 1;
199 . case 1: case 2:
200 . yyErrorFlag = 3;
201 . do {
202 . if ((yyN = yySindex[yyStates[yyTop]]) != 0
203 . && (yyN += Token.yyErrorCode) >= 0 && yyN < yyTable.Length
204 . && yyCheck[yyN] == Token.yyErrorCode) {
205 t if (debug != null)
206 t debug.shift(yyStates[yyTop], yyTable[yyN], 3);
207 . yyState = yyTable[yyN];
208 . yyVal = yyLex.value();
209 . goto continue_yyLoop;
211 t if (debug != null) debug.pop(yyStates[yyTop]);
212 . } while (-- yyTop >= 0);
213 t if (debug != null) debug.reject();
214 . throw new yyParser.yyException("irrecoverable syntax error");
216 . case 3:
217 . if (yyToken == 0) {
218 t if (debug != null) debug.reject();
219 . throw new yyParser.yyException("irrecoverable syntax error at end-of-file");
221 t if (debug != null)
222 t debug.discard(yyState, yyToken, yyname(yyToken),
223 t yyLex.value());
224 . yyToken = -1;
225 . goto continue_yyDiscarded; // leave stack alone
228 . int yyV = yyTop + 1-yyLen[yyN];
229 t if (debug != null)
230 t debug.reduce(yyState, yyStates[yyV-1], yyN, YYRules.getRule (yyN), yyLen[yyN]);
231 . yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]);
232 . switch (yyN) {
234 actions ## code from the actions within the grammar
237 . yyTop -= yyLen[yyN];
238 . yyState = yyStates[yyTop];
239 . int yyM = yyLhs[yyN];
240 . if (yyState == 0 && yyM == 0) {
241 t if (debug != null) debug.shift(0, yyFinal);
242 . yyState = yyFinal;
243 . if (yyToken < 0) {
244 . yyToken = yyLex.advance() ? yyLex.token() : 0;
246 t if (debug != null)
247 t debug.lex(yyState, yyToken,yyname(yyToken), yyLex.value());
249 . if (yyToken == 0) {
250 t if (debug != null) debug.accept(yyVal);
251 . return yyVal;
253 . goto continue_yyLoop;
255 . if (((yyN = yyGindex[yyM]) != 0) && ((yyN += yyState) >= 0)
256 . && (yyN < yyTable.Length) && (yyCheck[yyN] == yyState))
257 . yyState = yyTable[yyN];
258 . else
259 . yyState = yyDgoto[yyM];
260 t if (debug != null) debug.shift(yyStates[yyTop], yyState);
261 . goto continue_yyLoop;
262 . continue_yyDiscarded: continue; // implements the named-loop continue: 'continue yyDiscarded'
264 . continue_yyLoop: continue; // implements the named-loop continue: 'continue yyLoop'
268 tables ## tables for rules, default reduction, and action calls
270 epilog ## text following second %%
271 .namespace yydebug {
272 . using System;
273 . internal interface yyDebug {
274 . void push (int state, Object value);
275 . void lex (int state, int token, string name, Object value);
276 . void shift (int from, int to, int errorFlag);
277 . void pop (int state);
278 . void discard (int state, int token, string name, Object value);
279 . void reduce (int from, int to, int rule, string text, int len);
280 . void shift (int from, int to);
281 . void accept (Object value);
282 . void error (string message);
283 . void reject ();
286 . class yyDebugSimple : yyDebug {
287 . void println (string s){
288 . Console.Error.WriteLine (s);
291 . public void push (int state, Object value) {
292 . println ("push\tstate "+state+"\tvalue "+value);
295 . public void lex (int state, int token, string name, Object value) {
296 . println("lex\tstate "+state+"\treading "+name+"\tvalue "+value);
299 . public void shift (int from, int to, int errorFlag) {
300 . switch (errorFlag) {
301 . default: // normally
302 . println("shift\tfrom state "+from+" to "+to);
303 . break;
304 . case 0: case 1: case 2: // in error recovery
305 . println("shift\tfrom state "+from+" to "+to
306 . +"\t"+errorFlag+" left to recover");
307 . break;
308 . case 3: // normally
309 . println("shift\tfrom state "+from+" to "+to+"\ton error");
310 . break;
314 . public void pop (int state) {
315 . println("pop\tstate "+state+"\ton error");
318 . public void discard (int state, int token, string name, Object value) {
319 . println("discard\tstate "+state+"\ttoken "+name+"\tvalue "+value);
322 . public void reduce (int from, int to, int rule, string text, int len) {
323 . println("reduce\tstate "+from+"\tuncover "+to
324 . +"\trule ("+rule+") "+text);
327 . public void shift (int from, int to) {
328 . println("goto\tfrom state "+from+" to "+to);
331 . public void accept (Object value) {
332 . println("accept\tvalue "+value);
335 . public void error (string message) {
336 . println("error\t"+message);
339 . public void reject () {
340 . println("reject");
345 .// %token constants
346 . class Token {
347 tokens public const int
349 . namespace yyParser {
350 . using System;
351 . /** thrown for irrecoverable syntax errors and stack overflow.
352 . */
353 . internal class yyException : System.Exception {
354 . public yyException (string message) : base (message) {
357 . internal class yyUnexpectedEof : yyException {
358 . public yyUnexpectedEof (string message) : base (message) {
360 . public yyUnexpectedEof () : base ("") {
364 . /** must be implemented by a scanner object to supply input to the parser.
365 . */
366 . internal interface yyInput {
367 . /** move on to next token.
368 . @return false if positioned beyond tokens.
369 . @throws IOException on input error.
370 . */
371 . bool advance (); // throws java.io.IOException;
372 . /** classifies current token.
373 . Should not be called if advance() returned false.
374 . @return current %token or single character.
375 . */
376 . int token ();
377 . /** associated with current token.
378 . Should not be called if advance() returned false.
379 . @return value for token().
380 . */
381 . Object value ();
384 .} // close outermost namespace, that MUST HAVE BEEN opened in the prolog