Some modifications to allow to print the name of the file when a syntax error is...
[repair.git] / Repair / RepairCompiler / MCC / SDL.cup
1 package MCC;
2 import MCC.IR.ParseNode;
3 import MCC.IR.ParseNodeVector;
4 import java.util.*;
5
6 action code {:
7
8         public static boolean errors;
9         public static boolean debug;
10
11         // debugMessage: writes debug production message only if debug = true
12
13         void debugMessage (String production) {
14                 if (debug) {
15                         System.out.println("Applying production: " + production);
16                 }
17         }
18
19         String unescape (String str) {
20             StringBuffer sb = new StringBuffer();
21             int i;
22             // Note that we skip the first and last characters (they're "'s)
23             for (i = 1; i < str.length() - 1; i++) {
24                 if (str.charAt(i) == '\\') {
25                     i++;
26                     switch (str.charAt(i)) {
27                     case '\"':
28                         sb.append('\"');
29                         break;
30                     case '\'':
31                         sb.append('\'');
32                         break;
33                     case '\\':
34                         sb.append('\\');
35                         break;
36                     case 't':
37                         sb.append('\t');
38                         break;
39                     case 'n':
40                         sb.append('\n');
41                         break;
42                     default:
43                         System.err.print("Error in string literal: ");
44                         System.err.println(str.charAt(i));
45                         System.err.println("Aborting...");
46                         break;
47                     }
48                 } else {
49                     sb.append(str.charAt(i));
50                 }
51             }
52             return sb.toString();
53         }
54 :}
55
56 init with {: :}
57
58 parser code {:
59        
60         public void syntax_error (java_cup.runtime.Symbol current) {
61
62                 CUP$SDLParser$actions.errors = true;
63                 Symbol symbol = (Symbol) current;
64                 report_error("SDL: Syntax error at line " + (symbol.line + 1)
65                 + ", column " + LineCount.getColumn(symbol.left) + ": " + current.value, current);
66         }
67
68         public void report_fatal_error (String message, Object info) {
69                 
70                  done_parsing();
71                  report_error(message, info);
72                  CUP$SDLParser$actions.errors = true;
73         }
74
75         public int curPos () {
76                 return cur_token.left;
77         }
78
79         public int curLine (int back) {
80                 Stack st = new Stack();
81                 int i;
82
83                 for (i = 0; i < back; i++) {
84                         st.push(stack.pop());
85                 }
86
87                 java_cup.runtime.Symbol s;
88                 s = (java_cup.runtime.Symbol) st.peek();
89
90                 for (i = 0; i < back; i++) {
91                         stack.push(st.pop());
92                 }
93
94                 return LineCount.getLine(s.left);
95         }
96         
97 :}
98
99 // TERMINALS /////////////////////////////////////////////////////////////
100
101     terminal BAD;
102
103     terminal String ID;
104     terminal String DECIMAL;
105     terminal String CHAR;
106     terminal String STRING;
107
108     terminal OPENBRACE;
109     terminal CLOSEBRACE;
110     terminal OPENPAREN;
111     terminal CLOSEPAREN; 
112     terminal OPENBRACKET;
113     terminal CLOSEBRACKET;
114
115     terminal ADD; 
116     terminal SUB; 
117     terminal MULT; 
118     terminal DIV;
119
120     terminal NOT;
121     terminal LT;
122     terminal GT;
123     terminal LE;
124     terminal GE;
125     terminal EQ;
126     terminal NE;
127
128     terminal FORALL;
129     terminal IN;
130     terminal INTEST;
131
132     terminal COMMA;
133     terminal SIZEOF;
134
135     terminal DOT;
136     terminal DOTINV;
137
138     terminal AND;
139     terminal OR;
140
141     terminal LITERAL;
142
143     terminal IMPLIES;
144     terminal TRUE;
145     terminal FALSE;
146     terminal ISVALID;
147     terminal FOR;
148     terminal TO;
149     terminal CAST;
150
151     terminal PARAM;
152     terminal STRUCTURE;
153     terminal RESERVED;
154     terminal BIT;
155     terminal BYTE;
156     terminal SHORT;
157       
158     terminal LABEL;
159     terminal INT;
160     terminal SUBTYPE;
161     terminal OF;
162
163     terminal SEMICOLON;
164     terminal COLON;
165
166     terminal SET;
167     terminal ARROW;
168     terminal MANY;
169     terminal BAR;
170
171     terminal PARTITION;
172     terminal ELEMENT;
173     terminal DELAY;
174     terminal STATIC;
175
176     terminal NULL;
177     terminal CRASH;
178
179 // NON-TERMINALS /////////////////////////////////////////////////////////
180
181 /*
182                 TYPE                    NAME
183 ------------------------------------------------------------------------*/
184 nonterminal     ParseNode               spacedefs;
185 nonterminal     ParseNode               space;
186 nonterminal     ParseNode               mult;
187 nonterminal     ParseNode               optstatic;
188 nonterminal     ParseNode               optpartition;
189 nonterminal     ParseNode               setlist;
190 nonterminal     ParseNode               type;
191 nonterminal     ParseNode               optrange;
192
193 precedence left OR;
194 precedence left AND;
195 precedence right EQ, NE; 
196 precedence right LT, LE, GE, GT;
197 precedence left ADD, SUB;
198 precedence left MULT, DIV;
199 precedence left NOT;
200 precedence left DOT;
201
202 // PRODUCTION RULES  /////////////////////////////////////////////////////
203
204 start with spacedefs;
205
206 spacedefs ::= 
207           spacedefs:spacedefs space:space
208         {:
209         debugMessage(PRODSTRING);
210         spacedefs.addChild(space);
211         RESULT = spacedefs;
212         :}
213           | space:space
214         {:
215         debugMessage(PRODSTRING);
216         ParseNode spacedefs = new ParseNode("space", parser.curLine(1));
217         spacedefs.addChild(space);
218         RESULT = spacedefs;
219         :}
220           ;
221
222 space ::= 
223       SET ID:setname OPENPAREN type:settype CLOSEPAREN SEMICOLON
224         {:
225         debugMessage(PRODSTRING);
226         ParseNode set = new ParseNode("setdefinition", parser.curLine(6));
227         set.addChild("name", parser.curLine(5)).addChild(setname);
228         set.addChild(settype);
229         RESULT = set;
230         :}
231       | SET ID:setname OPENPAREN type:settype CLOSEPAREN COLON optpartition:partition setlist:setlist SEMICOLON
232         {:
233         debugMessage(PRODSTRING);
234         ParseNode set = new ParseNode("setdefinition", parser.curLine(8));
235         set.addChild("name", parser.curLine(7)).addChild(setname);
236         set.addChild(settype);
237         if (partition != null) set.addChild(partition);
238         if (setlist != null) set.addChild(setlist);
239         RESULT = set;
240         :}
241       | ID:name optstatic:optstatic COLON type:domain ARROW type:range optrange:optrange SEMICOLON 
242         {:
243         debugMessage(PRODSTRING);
244         ParseNode relation = new ParseNode("relationdefinition", parser.curLine(8));
245         if (optstatic != null) relation.addChild(optstatic);
246         relation.addChild("name", parser.curLine(7)).addChild(name);
247         relation.addChild("domain").addChild(domain);
248         relation.addChild("range").addChild(range);
249         if (optrange != null) { 
250             relation.getChild("domain").addChild(optrange.getChild("domainmult"));
251             relation.getChild("range").addChild(optrange.getChild("rangemult"));
252         }
253         RESULT = relation;
254         :}
255       ;
256
257 optrange ::= 
258         OPENPAREN mult:domainmult ARROW mult:rangemult CLOSEPAREN 
259            {:
260              debugMessage(PRODSTRING);
261              ParseNode optrange = new ParseNode("optrange", parser.curLine(5));
262              optrange.addChild("domainmult").addChild(domainmult);
263              optrange.addChild("rangemult").addChild(rangemult);
264              RESULT = optrange;
265            :}
266         | /* nothing */
267            {:
268              RESULT = null;
269            :}
270         ;
271
272 mult ::=
273      DECIMAL:one
274         {:
275         debugMessage(PRODSTRING);
276         ParseNode mult = new ParseNode("mult", parser.curLine(1));
277         mult.addChild(one);
278         RESULT = mult;
279         :}
280      | MANY
281         {:
282         debugMessage(PRODSTRING);
283         ParseNode mult = new ParseNode("mult", parser.curLine(1));
284         mult.addChild("many");
285         RESULT = mult;
286         :}
287      ;
288
289 optstatic ::= 
290           STATIC
291         {:
292         debugMessage(PRODSTRING);
293         RESULT = new ParseNode("static", parser.curLine(1));
294         :}
295           | /* nothing */
296         {:
297         debugMessage(PRODSTRING);
298         RESULT = null;
299         :}
300           ;
301
302 optpartition ::= 
303              PARTITION
304         {:
305         debugMessage(PRODSTRING);
306         RESULT = new ParseNode("partition", parser.curLine(1));
307         :}
308              | /* nothing */
309         {:
310         debugMessage(PRODSTRING);
311         RESULT = null;
312         :}
313              ;
314
315 setlist ::=
316         setlist:setlist BAR ID:set
317         {:
318         debugMessage(PRODSTRING);
319         setlist.addChild(set, parser.curLine(1));
320         RESULT = setlist;
321         :}
322         | ID:set
323         {:
324         debugMessage(PRODSTRING);
325         ParseNode setlist = new ParseNode("setlist");
326         setlist.addChild(set, parser.curLine(1));
327         RESULT = setlist;
328         :}
329         |
330         {:
331         debugMessage(PRODSTRING);
332         RESULT = null;
333         :}
334         ;
335
336 type ::= 
337      BIT
338         {:
339         debugMessage(PRODSTRING);
340         ParseNode type = new ParseNode("type", parser.curLine(1));
341         type.addChild("bit");
342         RESULT = type;
343         :}
344      | BYTE
345         {:
346         debugMessage(PRODSTRING);
347         ParseNode type = new ParseNode("type", parser.curLine(1));
348         type.addChild("byte");
349         RESULT = type;
350         :}
351      | SHORT
352         {:
353         debugMessage(PRODSTRING);
354         ParseNode type = new ParseNode("type", parser.curLine(1));
355         type.addChild("short");
356         RESULT = type;
357         :}
358      | INT 
359         {:
360         debugMessage(PRODSTRING);
361         ParseNode type = new ParseNode("type", parser.curLine(1));
362         type.addChild("int");
363         RESULT = type;
364         :}
365      | ID:typename
366         {:
367         debugMessage(PRODSTRING);
368         ParseNode type = new ParseNode("type", parser.curLine(1));
369         type.addChild(typename);
370         RESULT = type;
371         :}
372      ;
373
374
375
376
377
378