2009-03-10 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / jay / skeleton.cs
1 #       jay skeleton
2
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
8
9 .// created by jay 0.7 (c) 1998 Axel.Schreiner@informatik.uni-osnabrueck.de
10 .
11  prolog         ## %{ ... %} prior to the first %%
12
13 .
14 .  /** error output stream.
15 .      It should be changeable.
16 .    */
17 .  public System.IO.TextWriter ErrorOutput = System.Console.Out;
18 .
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 .  }
25 .
26 .  /* An EOF token */
27 .  public int eof_token;
28 .
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 .  }
43 .
44 .  /** debugging support, requires the package jay.yydebug.
45 .      Set to null to suppress debugging messages.
46 .    */
47 t  internal yydebug.yyDebug debug;
48 .
49  debug                  ## tables for debugging support
50 .
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  }
61 .
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 .  }
96 .
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)
105 .                                {
106 t    this.debug = (yydebug.yyDebug)yyd;
107 .    return yyparse(yyLex);
108 .  }
109 .
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;
115 .
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;
124 .  }
125 .
126 .  /** the generated parser.
127 .      Maintains a state and a value stack, currently with fixed maximum size.
128 .      @param yyLex scanner.
129 .      @return result of the last reduction, if any.
130 .      @throws yyException on irrecoverable parse error.
131 .    */
132 .  internal Object yyparse (yyParser.yyInput yyLex)
133 .  {
134 .    if (yyMax <= 0) yyMax = 256;                       // initial size
135 .    int yyState = 0;                                   // state stack ptr
136 .    int [] yyStates = new int[yyMax];                  // state stack 
137 .    Object yyVal = null;                               // value stack ptr
138 .    Object [] yyVals = new Object[yyMax];              // value stack
139 .    int yyToken = -1;                                  // current input
140 .    int yyErrorFlag = 0;                               // #tks to shift
141 .
142  local          ## %{ ... %} after the first %%
143
144 .    /*yyLoop:*/ for (int yyTop = 0;; ++ yyTop) {
145 .      if (yyTop >= yyStates.Length) {                  // dynamically increase
146 .        int[] i = new int[yyStates.Length+yyMax];
147 .        yyStates.CopyTo (i, 0);
148 .        yyStates = i;
149 .        Object[] o = new Object[yyVals.Length+yyMax];
150 .        yyVals.CopyTo (o, 0);
151 .        yyVals = o;
152 .      }
153 .      yyStates[yyTop] = yyState;
154 .      yyVals[yyTop] = yyVal;
155 t      if (debug != null) debug.push(yyState, yyVal);
156 .
157 .      /*yyDiscarded:*/ for (;;) {      // discarding a token does not change stack
158 .        int yyN;
159 .        if ((yyN = yyDefRed[yyState]) == 0) {  // else [default] reduce (yyN)
160 .          if (yyToken < 0) {
161 .            yyToken = yyLex.advance() ? yyLex.token() : 0;
162
163 t            if (debug != null)
164 t              debug.lex(yyState, yyToken, yyname(yyToken), yyLex.value());
165 .          }
166 .          if ((yyN = yySindex[yyState]) != 0 && ((yyN += yyToken) >= 0)
167 .              && (yyN < yyTable.Length) && (yyCheck[yyN] == yyToken)) {
168 t            if (debug != null)
169 t              debug.shift(yyState, yyTable[yyN], yyErrorFlag-1);
170 .            yyState = yyTable[yyN];            // shift to yyN
171 .            yyVal = yyLex.value();
172 .            yyToken = -1;
173 .            if (yyErrorFlag > 0) -- yyErrorFlag;
174 .            goto continue_yyLoop;
175 .          }
176 .          if ((yyN = yyRindex[yyState]) != 0 && (yyN += yyToken) >= 0
177 .              && yyN < yyTable.Length && yyCheck[yyN] == yyToken)
178 .            yyN = yyTable[yyN];                        // reduce (yyN)
179 .          else
180 .            switch (yyErrorFlag) {
181 .  
182 .            case 0:
183 .              yyExpectingState = yyState;
184 .              // yyerror(String.Format ("syntax error, got token `{0}'", yyname (yyToken)), yyExpecting(yyState));
185 t              if (debug != null) debug.error("syntax error");
186 .              if (yyToken == 0 /*eof*/ || yyToken == eof_token) throw new yyParser.yyUnexpectedEof ();
187 .              goto case 1;
188 .            case 1: case 2:
189 .              yyErrorFlag = 3;
190 .              do {
191 .                if ((yyN = yySindex[yyStates[yyTop]]) != 0
192 .                    && (yyN += Token.yyErrorCode) >= 0 && yyN < yyTable.Length
193 .                    && yyCheck[yyN] == Token.yyErrorCode) {
194 t                  if (debug != null)
195 t                    debug.shift(yyStates[yyTop], yyTable[yyN], 3);
196 .                  yyState = yyTable[yyN];
197 .                  yyVal = yyLex.value();
198 .                  goto continue_yyLoop;
199 .                }
200 t                if (debug != null) debug.pop(yyStates[yyTop]);
201 .              } while (-- yyTop >= 0);
202 t              if (debug != null) debug.reject();
203 .              throw new yyParser.yyException("irrecoverable syntax error");
204 .  
205 .            case 3:
206 .              if (yyToken == 0) {
207 t                if (debug != null) debug.reject();
208 .                throw new yyParser.yyException("irrecoverable syntax error at end-of-file");
209 .              }
210 t              if (debug != null)
211 t                debug.discard(yyState, yyToken, yyname(yyToken),
212 t                                                       yyLex.value());
213 .              yyToken = -1;
214 .              goto continue_yyDiscarded;               // leave stack alone
215 .            }
216 .        }
217 .        int yyV = yyTop + 1-yyLen[yyN];
218 t        if (debug != null)
219 t          debug.reduce(yyState, yyStates[yyV-1], yyN, YYRules.getRule (yyN), yyLen[yyN]);
220 .        yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]);
221 .        switch (yyN) {
222
223  actions                ## code from the actions within the grammar
224
225 .        }
226 .        yyTop -= yyLen[yyN];
227 .        yyState = yyStates[yyTop];
228 .        int yyM = yyLhs[yyN];
229 .        if (yyState == 0 && yyM == 0) {
230 t          if (debug != null) debug.shift(0, yyFinal);
231 .          yyState = yyFinal;
232 .          if (yyToken < 0) {
233 .            yyToken = yyLex.advance() ? yyLex.token() : 0;
234                 
235 t            if (debug != null)
236 t               debug.lex(yyState, yyToken,yyname(yyToken), yyLex.value());
237 .          }
238 .          if (yyToken == 0) {
239 t            if (debug != null) debug.accept(yyVal);
240 .            return yyVal;
241 .          }
242 .          goto continue_yyLoop;
243 .        }
244 .        if (((yyN = yyGindex[yyM]) != 0) && ((yyN += yyState) >= 0)
245 .            && (yyN < yyTable.Length) && (yyCheck[yyN] == yyState))
246 .          yyState = yyTable[yyN];
247 .        else
248 .          yyState = yyDgoto[yyM];
249 t        if (debug != null) debug.shift(yyStates[yyTop], yyState);
250 .        goto continue_yyLoop;
251 .      continue_yyDiscarded: continue;  // implements the named-loop continue: 'continue yyDiscarded'
252 .      }
253 .    continue_yyLoop: continue;         // implements the named-loop continue: 'continue yyLoop'
254 .    }
255 .  }
256 .
257  tables                 ## tables for rules, default reduction, and action calls
258 .
259  epilog                 ## text following second %%
260 .namespace yydebug {
261 .        using System;
262 .        internal interface yyDebug {
263 .                void push (int state, Object value);
264 .                void lex (int state, int token, string name, Object value);
265 .                void shift (int from, int to, int errorFlag);
266 .                void pop (int state);
267 .                void discard (int state, int token, string name, Object value);
268 .                void reduce (int from, int to, int rule, string text, int len);
269 .                void shift (int from, int to);
270 .                void accept (Object value);
271 .                void error (string message);
272 .                void reject ();
273 .        }
274 .        
275 .        class yyDebugSimple : yyDebug {
276 .                void println (string s){
277 .                        Console.Error.WriteLine (s);
278 .                }
279 .                
280 .                public void push (int state, Object value) {
281 .                        println ("push\tstate "+state+"\tvalue "+value);
282 .                }
283 .                
284 .                public void lex (int state, int token, string name, Object value) {
285 .                        println("lex\tstate "+state+"\treading "+name+"\tvalue "+value);
286 .                }
287 .                
288 .                public void shift (int from, int to, int errorFlag) {
289 .                        switch (errorFlag) {
290 .                        default:                               // normally
291 .                                println("shift\tfrom state "+from+" to "+to);
292 .                                break;
293 .                        case 0: case 1: case 2:                // in error recovery
294 .                                println("shift\tfrom state "+from+" to "+to
295 .                                            +"\t"+errorFlag+" left to recover");
296 .                                break;
297 .                        case 3:                                // normally
298 .                                println("shift\tfrom state "+from+" to "+to+"\ton error");
299 .                                break;
300 .                        }
301 .                }
302 .                
303 .                public void pop (int state) {
304 .                        println("pop\tstate "+state+"\ton error");
305 .                }
306 .                
307 .                public void discard (int state, int token, string name, Object value) {
308 .                        println("discard\tstate "+state+"\ttoken "+name+"\tvalue "+value);
309 .                }
310 .                
311 .                public void reduce (int from, int to, int rule, string text, int len) {
312 .                        println("reduce\tstate "+from+"\tuncover "+to
313 .                                    +"\trule ("+rule+") "+text);
314 .                }
315 .                
316 .                public void shift (int from, int to) {
317 .                        println("goto\tfrom state "+from+" to "+to);
318 .                }
319 .                
320 .                public void accept (Object value) {
321 .                        println("accept\tvalue "+value);
322 .                }
323 .                
324 .                public void error (string message) {
325 .                        println("error\t"+message);
326 .                }
327 .                
328 .                public void reject () {
329 .                        println("reject");
330 .                }
331 .                
332 .        }
333 .}
334 .// %token constants
335 . class Token {
336  tokens public const int
337 . }
338 . namespace yyParser {
339 .  using System;
340 .  /** thrown for irrecoverable syntax errors and stack overflow.
341 .    */
342 .  internal class yyException : System.Exception {
343 .    public yyException (string message) : base (message) {
344 .    }
345 .  }
346 .  internal class yyUnexpectedEof : yyException {
347 .    public yyUnexpectedEof (string message) : base (message) {
348 .    }
349 .    public yyUnexpectedEof () : base ("") {
350 .    }
351 .  }
352 .
353 .  /** must be implemented by a scanner object to supply input to the parser.
354 .    */
355 .  internal interface yyInput {
356 .    /** move on to next token.
357 .        @return false if positioned beyond tokens.
358 .        @throws IOException on input error.
359 .      */
360 .    bool advance (); // throws java.io.IOException;
361 .    /** classifies current token.
362 .        Should not be called if advance() returned false.
363 .        @return current %token or single character.
364 .      */
365 .    int token ();
366 .    /** associated with current token.
367 .        Should not be called if advance() returned false.
368 .        @return value for token().
369 .      */
370 .    Object value ();
371 .  }
372 . }
373 .} // close outermost namespace, that MUST HAVE BEEN opened in the prolog