Improve JAY: now we support EOF detection
[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 .  /** computes list of expected tokens on error by tracing the tables.
63 .      @param state for which to compute the list.
64 .      @return list of token names.
65 .    */
66 .  protected string[] yyExpecting (int state) {
67 .    int token, n, len = 0;
68 .    bool[] ok = new bool[yyNames.Length];
69 .
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 .
85 .    string [] result = new string[len];
86 .    for (n = token = 0; n < len;  ++ token)
87 .      if (ok[token]) result[n++] = yyNames[token];
88 .    return result;
89 .  }
90 .
91 .  /** the generated parser, with debugging messages.
92 .      Maintains a state and a value stack, currently with fixed maximum size.
93 .      @param yyLex scanner.
94 .      @param yydebug debug message writer implementing yyDebug, or null.
95 .      @return result of the last reduction, if any.
96 .      @throws yyException on irrecoverable parse error.
97 .    */
98 .  internal Object yyparse (yyParser.yyInput yyLex, Object yyd)
99 .                                {
100 t    this.debug = (yydebug.yyDebug)yyd;
101 .    return yyparse(yyLex);
102 .  }
103 .
104 .  /** initial size and increment of the state/value stack [default 256].
105 .      This is not final so that it can be overwritten outside of invocations
106 .      of yyparse().
107 .    */
108 .  protected int yyMax;
109 .
110 .  /** executed at the beginning of a reduce action.
111 .      Used as $$ = yyDefault($1), prior to the user-specified action, if any.
112 .      Can be overwritten to provide deep copy, etc.
113 .      @param first value for $1, or null.
114 .      @return first.
115 .    */
116 .  protected Object yyDefault (Object first) {
117 .    return first;
118 .  }
119 .
120 .  /** the generated parser.
121 .      Maintains a state and a value stack, currently with fixed maximum size.
122 .      @param yyLex scanner.
123 .      @return result of the last reduction, if any.
124 .      @throws yyException on irrecoverable parse error.
125 .    */
126 .  internal Object yyparse (yyParser.yyInput yyLex)
127 .  {
128 .    if (yyMax <= 0) yyMax = 256;                       // initial size
129 .    int yyState = 0;                                   // state stack ptr
130 .    int [] yyStates = new int[yyMax];                  // state stack 
131 .    Object yyVal = null;                               // value stack ptr
132 .    Object [] yyVals = new Object[yyMax];              // value stack
133 .    int yyToken = -1;                                  // current input
134 .    int yyErrorFlag = 0;                               // #tks to shift
135 .
136  local          ## %{ ... %} after the first %%
137
138 .    /*yyLoop:*/ for (int yyTop = 0;; ++ yyTop) {
139 .      if (yyTop >= yyStates.Length) {                  // dynamically increase
140 .        int[] i = new int[yyStates.Length+yyMax];
141 .        yyStates.CopyTo (i, 0);
142 .        yyStates = i;
143 .        Object[] o = new Object[yyVals.Length+yyMax];
144 .        yyVals.CopyTo (o, 0);
145 .        yyVals = o;
146 .      }
147 .      yyStates[yyTop] = yyState;
148 .      yyVals[yyTop] = yyVal;
149 t      if (debug != null) debug.push(yyState, yyVal);
150 .
151 .      /*yyDiscarded:*/ for (;;) {      // discarding a token does not change stack
152 .        int yyN;
153 .        if ((yyN = yyDefRed[yyState]) == 0) {  // else [default] reduce (yyN)
154 .          if (yyToken < 0) {
155 .            yyToken = yyLex.advance() ? yyLex.token() : 0;
156
157 t            if (debug != null)
158 t              debug.lex(yyState, yyToken, yyname(yyToken), yyLex.value());
159 .          }
160 .          if ((yyN = yySindex[yyState]) != 0 && ((yyN += yyToken) >= 0)
161 .              && (yyN < yyTable.Length) && (yyCheck[yyN] == yyToken)) {
162 t            if (debug != null)
163 t              debug.shift(yyState, yyTable[yyN], yyErrorFlag-1);
164 .            yyState = yyTable[yyN];            // shift to yyN
165 .            yyVal = yyLex.value();
166 .            yyToken = -1;
167 .            if (yyErrorFlag > 0) -- yyErrorFlag;
168 .            goto continue_yyLoop;
169 .          }
170 .          if ((yyN = yyRindex[yyState]) != 0 && (yyN += yyToken) >= 0
171 .              && yyN < yyTable.Length && yyCheck[yyN] == yyToken)
172 .            yyN = yyTable[yyN];                        // reduce (yyN)
173 .          else
174 .            switch (yyErrorFlag) {
175 .  
176 .            case 0:
177 .              // yyerror(String.Format ("syntax error, got token `{0}'", yyname (yyToken)), yyExpecting(yyState));
178 t              if (debug != null) debug.error("syntax error");
179 .              if (yyToken == 0 || yyToken == eof_token /* eof */) throw new yyParser.yyUnexpectedEof ();
180 .              goto case 1;
181 .            case 1: case 2:
182 .              yyErrorFlag = 3;
183 .              do {
184 .                if ((yyN = yySindex[yyStates[yyTop]]) != 0
185 .                    && (yyN += Token.yyErrorCode) >= 0 && yyN < yyTable.Length
186 .                    && yyCheck[yyN] == Token.yyErrorCode) {
187 t                  if (debug != null)
188 t                    debug.shift(yyStates[yyTop], yyTable[yyN], 3);
189 .                  yyState = yyTable[yyN];
190 .                  yyVal = yyLex.value();
191 .                  goto continue_yyLoop;
192 .                }
193 t                if (debug != null) debug.pop(yyStates[yyTop]);
194 .              } while (-- yyTop >= 0);
195 t              if (debug != null) debug.reject();
196 .              throw new yyParser.yyException("irrecoverable syntax error");
197 .  
198 .            case 3:
199 .              if (yyToken == 0) {
200 t                if (debug != null) debug.reject();
201 .                throw new yyParser.yyException("irrecoverable syntax error at end-of-file");
202 .              }
203 t              if (debug != null)
204 t                debug.discard(yyState, yyToken, yyname(yyToken),
205 t                                                       yyLex.value());
206 .              yyToken = -1;
207 .              goto continue_yyDiscarded;               // leave stack alone
208 .            }
209 .        }
210 .        int yyV = yyTop + 1-yyLen[yyN];
211 t        if (debug != null)
212 t          debug.reduce(yyState, yyStates[yyV-1], yyN, YYRules.getRule (yyN), yyLen[yyN]);
213 .        yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]);
214 .        switch (yyN) {
215
216  actions                ## code from the actions within the grammar
217
218 .        }
219 .        yyTop -= yyLen[yyN];
220 .        yyState = yyStates[yyTop];
221 .        int yyM = yyLhs[yyN];
222 .        if (yyState == 0 && yyM == 0) {
223 t          if (debug != null) debug.shift(0, yyFinal);
224 .          yyState = yyFinal;
225 .          if (yyToken < 0) {
226 .            yyToken = yyLex.advance() ? yyLex.token() : 0;
227                 
228 t            if (debug != null)
229 t               debug.lex(yyState, yyToken,yyname(yyToken), yyLex.value());
230 .          }
231 .          if (yyToken == 0) {
232 t            if (debug != null) debug.accept(yyVal);
233 .            return yyVal;
234 .          }
235 .          goto continue_yyLoop;
236 .        }
237 .        if (((yyN = yyGindex[yyM]) != 0) && ((yyN += yyState) >= 0)
238 .            && (yyN < yyTable.Length) && (yyCheck[yyN] == yyState))
239 .          yyState = yyTable[yyN];
240 .        else
241 .          yyState = yyDgoto[yyM];
242 t        if (debug != null) debug.shift(yyStates[yyTop], yyState);
243 .        goto continue_yyLoop;
244 .      continue_yyDiscarded: continue;  // implements the named-loop continue: 'continue yyDiscarded'
245 .      }
246 .    continue_yyLoop: continue;         // implements the named-loop continue: 'continue yyLoop'
247 .    }
248 .  }
249 .
250  tables                 ## tables for rules, default reduction, and action calls
251 .
252  epilog                 ## text following second %%
253 .namespace yydebug {
254 .        using System;
255 .        internal interface yyDebug {
256 .                void push (int state, Object value);
257 .                void lex (int state, int token, string name, Object value);
258 .                void shift (int from, int to, int errorFlag);
259 .                void pop (int state);
260 .                void discard (int state, int token, string name, Object value);
261 .                void reduce (int from, int to, int rule, string text, int len);
262 .                void shift (int from, int to);
263 .                void accept (Object value);
264 .                void error (string message);
265 .                void reject ();
266 .        }
267 .        
268 .        class yyDebugSimple : yyDebug {
269 .                void println (string s){
270 .                        Console.Error.WriteLine (s);
271 .                }
272 .                
273 .                public void push (int state, Object value) {
274 .                        println ("push\tstate "+state+"\tvalue "+value);
275 .                }
276 .                
277 .                public void lex (int state, int token, string name, Object value) {
278 .                        println("lex\tstate "+state+"\treading "+name+"\tvalue "+value);
279 .                }
280 .                
281 .                public void shift (int from, int to, int errorFlag) {
282 .                        switch (errorFlag) {
283 .                        default:                               // normally
284 .                                println("shift\tfrom state "+from+" to "+to);
285 .                                break;
286 .                        case 0: case 1: case 2:                // in error recovery
287 .                                println("shift\tfrom state "+from+" to "+to
288 .                                            +"\t"+errorFlag+" left to recover");
289 .                                break;
290 .                        case 3:                                // normally
291 .                                println("shift\tfrom state "+from+" to "+to+"\ton error");
292 .                                break;
293 .                        }
294 .                }
295 .                
296 .                public void pop (int state) {
297 .                        println("pop\tstate "+state+"\ton error");
298 .                }
299 .                
300 .                public void discard (int state, int token, string name, Object value) {
301 .                        println("discard\tstate "+state+"\ttoken "+name+"\tvalue "+value);
302 .                }
303 .                
304 .                public void reduce (int from, int to, int rule, string text, int len) {
305 .                        println("reduce\tstate "+from+"\tuncover "+to
306 .                                    +"\trule ("+rule+") "+text);
307 .                }
308 .                
309 .                public void shift (int from, int to) {
310 .                        println("goto\tfrom state "+from+" to "+to);
311 .                }
312 .                
313 .                public void accept (Object value) {
314 .                        println("accept\tvalue "+value);
315 .                }
316 .                
317 .                public void error (string message) {
318 .                        println("error\t"+message);
319 .                }
320 .                
321 .                public void reject () {
322 .                        println("reject");
323 .                }
324 .                
325 .        }
326 .}
327 .// %token constants
328 . class Token {
329  tokens public const int
330 . }
331 . namespace yyParser {
332 .  using System;
333 .  /** thrown for irrecoverable syntax errors and stack overflow.
334 .    */
335 .  internal class yyException : System.Exception {
336 .    public yyException (string message) : base (message) {
337 .    }
338 .  }
339 .  internal class yyUnexpectedEof : yyException {
340 .    public yyUnexpectedEof (string message) : base (message) {
341 .    }
342 .    public yyUnexpectedEof () : base ("") {
343 .    }
344 .  }
345 .
346 .  /** must be implemented by a scanner object to supply input to the parser.
347 .    */
348 .  internal interface yyInput {
349 .    /** move on to next token.
350 .        @return false if positioned beyond tokens.
351 .        @throws IOException on input error.
352 .      */
353 .    bool advance (); // throws java.io.IOException;
354 .    /** classifies current token.
355 .        Should not be called if advance() returned false.
356 .        @return current %token or single character.
357 .      */
358 .    int token ();
359 .    /** associated with current token.
360 .        Should not be called if advance() returned false.
361 .        @return value for token().
362 .      */
363 .    Object value ();
364 .  }
365 . }
366 .} // close outermost namespace, that MUST HAVE BEEN opened in the prolog