Call lexer.Cleanup when the lexer reaches the EOF.
[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 .  /** simplified error message.
15 .      @see <a href="#yyerror(java.lang.String, java.lang.String[])">yyerror</a>
16 .    */
17 .  public void yyerror (string message) {
18 .    yyerror(message, null);
19 .  }
20 .
21 .  /** (syntax) error message.
22 .      Can be overwritten to control message format.
23 .      @param message text to be displayed.
24 .      @param expected vector of acceptable tokens, if available.
25 .    */
26 .  public void yyerror (string message, string[] expected) {
27 .    if ((expected != null) && (expected.Length  > 0)) {
28 .      System.Console.Write (message+", expecting");
29 .      for (int n = 0; n < expected.Length; ++ n)
30 .        System.Console.Write (" "+expected[n]);
31 .        System.Console.WriteLine ();
32 .    } else
33 .      System.Console.WriteLine (message);
34 .  }
35 .
36 .  /** debugging support, requires the package jay.yydebug.
37 .      Set to null to suppress debugging messages.
38 .    */
39 t  protected yydebug.yyDebug debug;
40 .
41  debug                  ## tables for debugging support
42 .
43 .  /** index-checked interface to yyName[].
44 .      @param token single character or %token value.
45 .      @return token name or [illegal] or [unknown].
46 .    */
47 t  public static string yyname (int token) {
48 t    if ((token < 0) || (token > yyName.Length)) return "[illegal]";
49 t    string name;
50 t    if ((name = yyName[token]) != null) return name;
51 t    return "[unknown]";
52 t  }
53 .
54 .  /** computes list of expected tokens on error by tracing the tables.
55 .      @param state for which to compute the list.
56 .      @return list of token names.
57 .    */
58 .  protected string[] yyExpecting (int state) {
59 .    int token, n, len = 0;
60 .    bool[] ok = new bool[yyName.Length];
61 .
62 .    if ((n = yySindex[state]) != 0)
63 .      for (token = n < 0 ? -n : 0;
64 .           (token < yyName.Length) && (n+token < yyTable.Length); ++ token)
65 .        if (yyCheck[n+token] == token && !ok[token] && yyName[token] != null) {
66 .          ++ len;
67 .          ok[token] = true;
68 .        }
69 .    if ((n = yyRindex[state]) != 0)
70 .      for (token = n < 0 ? -n : 0;
71 .           (token < yyName.Length) && (n+token < yyTable.Length); ++ token)
72 .        if (yyCheck[n+token] == token && !ok[token] && yyName[token] != null) {
73 .          ++ len;
74 .          ok[token] = true;
75 .        }
76 .
77 .    string [] result = new string[len];
78 .    for (n = token = 0; n < len;  ++ token)
79 .      if (ok[token]) result[n++] = yyName[token];
80 .    return result;
81 .  }
82 .
83 .  /** the generated parser, with debugging messages.
84 .      Maintains a state and a value stack, currently with fixed maximum size.
85 .      @param yyLex scanner.
86 .      @param yydebug debug message writer implementing yyDebug, or null.
87 .      @return result of the last reduction, if any.
88 .      @throws yyException on irrecoverable parse error.
89 .    */
90 .  public Object yyparse (yyParser.yyInput yyLex, Object yyd)
91 .                                {
92 t    this.debug = (yydebug.yyDebug)yyd;
93 .    return yyparse(yyLex);
94 .  }
95 .
96 .  /** initial size and increment of the state/value stack [default 256].
97 .      This is not final so that it can be overwritten outside of invocations
98 .      of yyparse().
99 .    */
100 .  protected int yyMax;
101 .
102 .  /** executed at the beginning of a reduce action.
103 .      Used as $$ = yyDefault($1), prior to the user-specified action, if any.
104 .      Can be overwritten to provide deep copy, etc.
105 .      @param first value for $1, or null.
106 .      @return first.
107 .    */
108 .  protected Object yyDefault (Object first) {
109 .    return first;
110 .  }
111 .
112 .  /** the generated parser.
113 .      Maintains a state and a value stack, currently with fixed maximum size.
114 .      @param yyLex scanner.
115 .      @return result of the last reduction, if any.
116 .      @throws yyException on irrecoverable parse error.
117 .    */
118 .  public Object yyparse (yyParser.yyInput yyLex)
119 .                               {
120 .    if (yyMax <= 0) yyMax = 256;                       // initial size
121 .    int yyState = 0;                                   // state stack ptr
122 .    int [] yyStates = new int[yyMax];                  // state stack 
123 .    Object yyVal = null;                               // value stack ptr
124 .    Object [] yyVals = new Object[yyMax];              // value stack
125 .    int yyToken = -1;                                  // current input
126 .    int yyErrorFlag = 0;                               // #tks to shift
127 .
128  local          ## %{ ... %} after the first %%
129
130 .    int yyTop = 0;
131 .    goto skip;
132 .    yyLoop:
133 .    yyTop++;
134 .    skip:
135 .    for (;; ++ 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;
143 .      }
144 .      yyStates[yyTop] = yyState;
145 .      yyVals[yyTop] = yyVal;
146 t      if (debug != null) debug.push(yyState, yyVal);
147 .
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;
153 .            if (yyToken == 0)
154 .               yyLex.Cleanup ();
155 t            if (debug != null)
156 t              debug.lex(yyState, yyToken, yyname(yyToken), yyLex.value());
157 .          }
158 .          if ((yyN = yySindex[yyState]) != 0 && ((yyN += yyToken) >= 0)
159 .              && (yyN < yyTable.Length) && (yyCheck[yyN] == yyToken)) {
160 t            if (debug != null)
161 t              debug.shift(yyState, yyTable[yyN], yyErrorFlag-1);
162 .            yyState = yyTable[yyN];            // shift to yyN
163 .            yyVal = yyLex.value();
164 .            yyToken = -1;
165 .            if (yyErrorFlag > 0) -- yyErrorFlag;
166 .            goto yyLoop;
167 .          }
168 .          if ((yyN = yyRindex[yyState]) != 0 && (yyN += yyToken) >= 0
169 .              && yyN < yyTable.Length && yyCheck[yyN] == yyToken)
170 .            yyN = yyTable[yyN];                        // reduce (yyN)
171 .          else
172 .            switch (yyErrorFlag) {
173 .  
174 .            case 0:
175 .              yyerror("syntax error", yyExpecting(yyState));
176 t              if (debug != null) debug.error("syntax error");
177 .              goto case 1;
178 .            case 1: case 2:
179 .              yyErrorFlag = 3;
180 .              do {
181 .                if ((yyN = yySindex[yyStates[yyTop]]) != 0
182 .                    && (yyN += Token.yyErrorCode) >= 0 && yyN < yyTable.Length
183 .                    && yyCheck[yyN] == Token.yyErrorCode) {
184 t                  if (debug != null)
185 t                    debug.shift(yyStates[yyTop], yyTable[yyN], 3);
186 .                  yyState = yyTable[yyN];
187 .                  yyVal = yyLex.value();
188 .                  goto yyLoop;
189 .                }
190 t                if (debug != null) debug.pop(yyStates[yyTop]);
191 .              } while (-- yyTop >= 0);
192 t              if (debug != null) debug.reject();
193 .              throw new yyParser.yyException("irrecoverable syntax error");
194 .  
195 .            case 3:
196 .              if (yyToken == 0) {
197 t                if (debug != null) debug.reject();
198 .                throw new yyParser.yyException("irrecoverable syntax error at end-of-file");
199 .              }
200 t              if (debug != null)
201 t                debug.discard(yyState, yyToken, yyname(yyToken),
202 t                                                       yyLex.value());
203 .              yyToken = -1;
204 .              goto yyDiscarded;                // leave stack alone
205 .            }
206 .        }
207 .        int yyV = yyTop + 1-yyLen[yyN];
208 t        if (debug != null)
209 t          debug.reduce(yyState, yyStates[yyV-1], yyN, yyRule[yyN], yyLen[yyN]);
210 .        yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]);
211 .        switch (yyN) {
212
213  actions                ## code from the actions within the grammar
214
215 .        }
216 .        yyTop -= yyLen[yyN];
217 .        yyState = yyStates[yyTop];
218 .        int yyM = yyLhs[yyN];
219 .        if (yyState == 0 && yyM == 0) {
220 t          if (debug != null) debug.shift(0, yyFinal);
221 .          yyState = yyFinal;
222 .          if (yyToken < 0) {
223 .            yyToken = yyLex.advance() ? yyLex.token() : 0;
224 .            if (yyToken == 0)
225 .                 yyLex.Cleanup (); 
226                 
227 t            if (debug != null)
228 t               debug.lex(yyState, yyToken,yyname(yyToken), yyLex.value());
229 .          }
230 .          if (yyToken == 0) {
231 t            if (debug != null) debug.accept(yyVal);
232 .            return yyVal;
233 .          }
234 .          goto yyLoop;
235 .        }
236 .        if (((yyN = yyGindex[yyM]) != 0) && ((yyN += yyState) >= 0)
237 .            && (yyN < yyTable.Length) && (yyCheck[yyN] == yyState))
238 .          yyState = yyTable[yyN];
239 .        else
240 .          yyState = yyDgoto[yyM];
241 t        if (debug != null) debug.shift(yyStates[yyTop], yyState);
242 .        goto yyLoop;
243 .      }
244 .    }
245 .  }
246 .
247  tables                 ## tables for rules, default reduction, and action calls
248 .
249  epilog                 ## text following second %%
250 .namespace yydebug {
251 .        using System;
252 .        public interface yyDebug {
253 .                void push (int state, Object value);
254 .                void lex (int state, int token, string name, Object value);
255 .                void shift (int from, int to, int errorFlag);
256 .                void pop (int state);
257 .                void discard (int state, int token, string name, Object value);
258 .                void reduce (int from, int to, int rule, string text, int len);
259 .                void shift (int from, int to);
260 .                void accept (Object value);
261 .                void error (string message);
262 .                void reject ();
263 .        }
264 .        
265 .        class yyDebugSimple : yyDebug {
266 .                void println (string s){
267 .                        Console.WriteLine (s);
268 .                }
269 .                
270 .                public void push (int state, Object value) {
271 .                        println ("push\tstate "+state+"\tvalue "+value);
272 .                }
273 .                
274 .                public void lex (int state, int token, string name, Object value) {
275 .                        println("lex\tstate "+state+"\treading "+name+"\tvalue "+value);
276 .                }
277 .                
278 .                public void shift (int from, int to, int errorFlag) {
279 .                        switch (errorFlag) {
280 .                        default:                               // normally
281 .                                println("shift\tfrom state "+from+" to "+to);
282 .                                break;
283 .                        case 0: case 1: case 2:                // in error recovery
284 .                                println("shift\tfrom state "+from+" to "+to
285 .                                            +"\t"+errorFlag+" left to recover");
286 .                                break;
287 .                        case 3:                                // normally
288 .                                println("shift\tfrom state "+from+" to "+to+"\ton error");
289 .                                break;
290 .                        }
291 .                }
292 .                
293 .                public void pop (int state) {
294 .                        println("pop\tstate "+state+"\ton error");
295 .                }
296 .                
297 .                public void discard (int state, int token, string name, Object value) {
298 .                        println("discard\tstate "+state+"\ttoken "+name+"\tvalue "+value);
299 .                }
300 .                
301 .                public void reduce (int from, int to, int rule, string text, int len) {
302 .                        println("reduce\tstate "+from+"\tuncover "+to
303 .                                    +"\trule ("+rule+") "+text);
304 .                }
305 .                
306 .                public void shift (int from, int to) {
307 .                        println("goto\tfrom state "+from+" to "+to);
308 .                }
309 .                
310 .                public void accept (Object value) {
311 .                        println("accept\tvalue "+value);
312 .                }
313 .                
314 .                public void error (string message) {
315 .                        println("error\t"+message);
316 .                }
317 .                
318 .                public void reject () {
319 .                        println("reject");
320 .                }
321 .                
322 .        }
323 .}
324 .// %token constants
325 . class Token {
326  tokens public const int
327 . }
328 . namespace yyParser {
329 .  using System;
330 .  /** thrown for irrecoverable syntax errors and stack overflow.
331 .    */
332 .  public class yyException : System.Exception {
333 .    public yyException (string message) : base (message) {
334 .    }
335 .  }
336 .
337 .  /** must be implemented by a scanner object to supply input to the parser.
338 .    */
339 .  public interface yyInput {
340 .    /** move on to next token.
341 .        @return false if positioned beyond tokens.
342 .        @throws IOException on input error.
343 .      */
344 .    bool advance (); // throws java.io.IOException;
345 .    /** classifies current token.
346 .        Should not be called if advance() returned false.
347 .        @return current %token or single character.
348 .      */
349 .    int token ();
350 .    /** associated with current token.
351 .        Should not be called if advance() returned false.
352 .        @return value for token().
353 .      */
354 .    Object value ();
355 .    /** Cleanup, this is called when advance returns false.
356 .     */
357 .    void Cleanup ();
358 .  }
359 . }
360 .} // close outermost namespace, that MUST HAVE BEEN opened in the prolog