79553fc39ead4c2c9c7a1e5e8741eb7926fd33d9
[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 yyNames[].
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 > yyNames.Length)) return "[illegal]";
49 t    string name;
50 t    if ((name = yyNames[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[yyNames.Length];
61 .
62 .    if ((n = yySindex[state]) != 0)
63 .      for (token = n < 0 ? -n : 0;
64 .           (token < yyNames.Length) && (n+token < yyTable.Length); ++ token)
65 .        if (yyCheck[n+token] == token && !ok[token] && yyNames[token] != null) {
66 .          ++ len;
67 .          ok[token] = true;
68 .        }
69 .    if ((n = yyRindex[state]) != 0)
70 .      for (token = n < 0 ? -n : 0;
71 .           (token < yyNames.Length) && (n+token < yyTable.Length); ++ token)
72 .        if (yyCheck[n+token] == token && !ok[token] && yyNames[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++] = yyNames[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
154 t            if (debug != null)
155 t              debug.lex(yyState, yyToken, yyname(yyToken), yyLex.value());
156 .          }
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 yyLoop;
166 .          }
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) {
172 .  
173 .            case 0:
174 .              yyerror("syntax error", 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 yyLoop;
188 .                }
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");
193 .  
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");
198 .              }
199 t              if (debug != null)
200 t                debug.discard(yyState, yyToken, yyname(yyToken),
201 t                                                       yyLex.value());
202 .              yyToken = -1;
203 .              goto yyDiscarded;                // leave stack alone
204 .            }
205 .        }
206 .        int yyV = yyTop + 1-yyLen[yyN];
207 t        if (debug != null)
208 t          debug.reduce(yyState, yyStates[yyV-1], yyN, yyRule[yyN], yyLen[yyN]);
209 .        yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]);
210 .        switch (yyN) {
211
212  actions                ## code from the actions within the grammar
213
214 .        }
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;
223                 
224 t            if (debug != null)
225 t               debug.lex(yyState, yyToken,yyname(yyToken), yyLex.value());
226 .          }
227 .          if (yyToken == 0) {
228 t            if (debug != null) debug.accept(yyVal);
229 .            return yyVal;
230 .          }
231 .          goto yyLoop;
232 .        }
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 yyLoop;
240 .      }
241 .    }
242 .  }
243 .
244  tables                 ## tables for rules, default reduction, and action calls
245 .
246  epilog                 ## text following second %%
247 .namespace yydebug {
248 .        using System;
249 .        public interface yyDebug {
250 .                void push (int state, Object value);
251 .                void lex (int state, int token, string name, Object value);
252 .                void shift (int from, int to, int errorFlag);
253 .                void pop (int state);
254 .                void discard (int state, int token, string name, Object value);
255 .                void reduce (int from, int to, int rule, string text, int len);
256 .                void shift (int from, int to);
257 .                void accept (Object value);
258 .                void error (string message);
259 .                void reject ();
260 .        }
261 .        
262 .        class yyDebugSimple : yyDebug {
263 .                void println (string s){
264 .                        Console.WriteLine (s);
265 .                }
266 .                
267 .                public void push (int state, Object value) {
268 .                        println ("push\tstate "+state+"\tvalue "+value);
269 .                }
270 .                
271 .                public void lex (int state, int token, string name, Object value) {
272 .                        println("lex\tstate "+state+"\treading "+name+"\tvalue "+value);
273 .                }
274 .                
275 .                public void shift (int from, int to, int errorFlag) {
276 .                        switch (errorFlag) {
277 .                        default:                               // normally
278 .                                println("shift\tfrom state "+from+" to "+to);
279 .                                break;
280 .                        case 0: case 1: case 2:                // in error recovery
281 .                                println("shift\tfrom state "+from+" to "+to
282 .                                            +"\t"+errorFlag+" left to recover");
283 .                                break;
284 .                        case 3:                                // normally
285 .                                println("shift\tfrom state "+from+" to "+to+"\ton error");
286 .                                break;
287 .                        }
288 .                }
289 .                
290 .                public void pop (int state) {
291 .                        println("pop\tstate "+state+"\ton error");
292 .                }
293 .                
294 .                public void discard (int state, int token, string name, Object value) {
295 .                        println("discard\tstate "+state+"\ttoken "+name+"\tvalue "+value);
296 .                }
297 .                
298 .                public void reduce (int from, int to, int rule, string text, int len) {
299 .                        println("reduce\tstate "+from+"\tuncover "+to
300 .                                    +"\trule ("+rule+") "+text);
301 .                }
302 .                
303 .                public void shift (int from, int to) {
304 .                        println("goto\tfrom state "+from+" to "+to);
305 .                }
306 .                
307 .                public void accept (Object value) {
308 .                        println("accept\tvalue "+value);
309 .                }
310 .                
311 .                public void error (string message) {
312 .                        println("error\t"+message);
313 .                }
314 .                
315 .                public void reject () {
316 .                        println("reject");
317 .                }
318 .                
319 .        }
320 .}
321 .// %token constants
322 . class Token {
323  tokens public const int
324 . }
325 . namespace yyParser {
326 .  using System;
327 .  /** thrown for irrecoverable syntax errors and stack overflow.
328 .    */
329 .  public class yyException : System.Exception {
330 .    public yyException (string message) : base (message) {
331 .    }
332 .  }
333 .
334 .  /** must be implemented by a scanner object to supply input to the parser.
335 .    */
336 .  public interface yyInput {
337 .    /** move on to next token.
338 .        @return false if positioned beyond tokens.
339 .        @throws IOException on input error.
340 .      */
341 .    bool advance (); // throws java.io.IOException;
342 .    /** classifies current token.
343 .        Should not be called if advance() returned false.
344 .        @return current %token or single character.
345 .      */
346 .    int token ();
347 .    /** associated with current token.
348 .        Should not be called if advance() returned false.
349 .        @return value for token().
350 .      */
351 .    Object value ();
352 .  }
353 . }
354 .} // close outermost namespace, that MUST HAVE BEEN opened in the prolog