]> gerrit.simantics Code Review - simantics/platform.git/blob
bbf944f1e28b8c84cf78b00895fbca516158e8e4
[simantics/platform.git] /
1 package org.simantics.scl.compiler.internal.parsing.parser;
2
3 import java.io.Reader;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
7
8 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
9 import org.simantics.scl.compiler.common.precedence.Associativity;
10 import org.simantics.scl.compiler.common.precedence.Precedence;
11 import org.simantics.scl.compiler.constants.CharacterConstant;
12 import org.simantics.scl.compiler.constants.StringConstant;
13 import org.simantics.scl.compiler.elaboration.equation.EqBasic;
14 import org.simantics.scl.compiler.elaboration.equation.EqGuard;
15 import org.simantics.scl.compiler.elaboration.equation.Equation;
16 import org.simantics.scl.compiler.elaboration.expressions.Case;
17 import org.simantics.scl.compiler.elaboration.expressions.EApply;
18 import org.simantics.scl.compiler.elaboration.expressions.EAsPattern;
19 import org.simantics.scl.compiler.elaboration.expressions.EBinary;
20 import org.simantics.scl.compiler.elaboration.expressions.EBinaryRightSide;
21 import org.simantics.scl.compiler.elaboration.expressions.EBlock;
22 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
23 import org.simantics.scl.compiler.elaboration.expressions.EEnforce;
24 import org.simantics.scl.compiler.elaboration.expressions.EEntityTypeAnnotation;
25 import org.simantics.scl.compiler.elaboration.expressions.EEquations;
26 import org.simantics.scl.compiler.elaboration.expressions.EFieldAccess;
27 import org.simantics.scl.compiler.elaboration.expressions.EIf;
28 import org.simantics.scl.compiler.elaboration.expressions.EIntegerLiteral;
29 import org.simantics.scl.compiler.elaboration.expressions.ELambda;
30 import org.simantics.scl.compiler.elaboration.expressions.EListComprehension;
31 import org.simantics.scl.compiler.elaboration.expressions.EListLiteral;
32 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
33 import org.simantics.scl.compiler.elaboration.expressions.EMatch;
34 import org.simantics.scl.compiler.elaboration.expressions.ERange;
35 import org.simantics.scl.compiler.elaboration.expressions.ERealLiteral;
36 import org.simantics.scl.compiler.elaboration.expressions.ERecord;
37 import org.simantics.scl.compiler.elaboration.expressions.ESelect;
38 import org.simantics.scl.compiler.elaboration.expressions.ESimpleLambda;
39 import org.simantics.scl.compiler.elaboration.expressions.EStringLiteral;
40 import org.simantics.scl.compiler.elaboration.expressions.ETransformation;
41 import org.simantics.scl.compiler.elaboration.expressions.ETypeAnnotation;
42 import org.simantics.scl.compiler.elaboration.expressions.EVar;
43 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
44 import org.simantics.scl.compiler.elaboration.expressions.EWhen;
45 import org.simantics.scl.compiler.elaboration.expressions.Expression;
46 import org.simantics.scl.compiler.elaboration.expressions.GuardedExpression;
47 import org.simantics.scl.compiler.elaboration.expressions.GuardedExpressionGroup;
48 import org.simantics.scl.compiler.elaboration.expressions.Variable;
49 import org.simantics.scl.compiler.elaboration.expressions.accessor.ExpressionAccessor;
50 import org.simantics.scl.compiler.elaboration.expressions.accessor.FieldAccessor;
51 import org.simantics.scl.compiler.elaboration.expressions.accessor.IdAccessor;
52 import org.simantics.scl.compiler.elaboration.expressions.accessor.StringAccessor;
53 import org.simantics.scl.compiler.elaboration.expressions.block.BindStatement;
54 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
55 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
56 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
57 import org.simantics.scl.compiler.elaboration.expressions.block.Statement;
58 import org.simantics.scl.compiler.elaboration.expressions.list.ListAssignment;
59 import org.simantics.scl.compiler.elaboration.expressions.list.ListGenerator;
60 import org.simantics.scl.compiler.elaboration.expressions.list.ListGuard;
61 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifier;
62 import org.simantics.scl.compiler.elaboration.expressions.list.ListSeq;
63 import org.simantics.scl.compiler.elaboration.expressions.list.ListThen;
64 import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
65 import org.simantics.scl.compiler.elaboration.java.Builtins;
66 import org.simantics.scl.compiler.elaboration.query.QAlternative;
67 import org.simantics.scl.compiler.elaboration.query.QConjunction;
68 import org.simantics.scl.compiler.elaboration.query.QDisjunction;
69 import org.simantics.scl.compiler.elaboration.query.QNegation;
70 import org.simantics.scl.compiler.elaboration.query.Query;
71 import org.simantics.scl.compiler.elaboration.query.pre.QPreBinds;
72 import org.simantics.scl.compiler.elaboration.query.pre.QPreEquals;
73 import org.simantics.scl.compiler.elaboration.query.pre.QPreGuard;
74 import org.simantics.scl.compiler.errors.Locations;
75 import org.simantics.scl.compiler.internal.parsing.Symbol;
76 import org.simantics.scl.compiler.internal.parsing.Token;
77 import org.simantics.scl.compiler.internal.parsing.declarations.ConstructorAst;
78 import org.simantics.scl.compiler.internal.parsing.declarations.DAnnotationAst;
79 import org.simantics.scl.compiler.internal.parsing.declarations.DClassAst;
80 import org.simantics.scl.compiler.internal.parsing.declarations.DDataAst;
81 import org.simantics.scl.compiler.internal.parsing.declarations.DDerivingInstanceAst;
82 import org.simantics.scl.compiler.internal.parsing.declarations.DDocumentationAst;
83 import org.simantics.scl.compiler.internal.parsing.declarations.DEffectAst;
84 import org.simantics.scl.compiler.internal.parsing.declarations.DFixityAst;
85 import org.simantics.scl.compiler.internal.parsing.declarations.DImportJavaAst;
86 import org.simantics.scl.compiler.internal.parsing.declarations.DInstanceAst;
87 import org.simantics.scl.compiler.internal.parsing.declarations.DMappingRelationAst;
88 import org.simantics.scl.compiler.internal.parsing.declarations.DRelationAst;
89 import org.simantics.scl.compiler.internal.parsing.declarations.DRuleAst;
90 import org.simantics.scl.compiler.internal.parsing.declarations.DTypeAst;
91 import org.simantics.scl.compiler.internal.parsing.declarations.DValueAst;
92 import org.simantics.scl.compiler.internal.parsing.declarations.DValueTypeAst;
93 import org.simantics.scl.compiler.internal.parsing.declarations.DeclarationAst;
94 import org.simantics.scl.compiler.internal.parsing.declarations.FieldDescription;
95 import org.simantics.scl.compiler.internal.parsing.declarations.FundepAst;
96 import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException;
97 import org.simantics.scl.compiler.internal.parsing.types.TApplyAst;
98 import org.simantics.scl.compiler.internal.parsing.types.TEffectAst;
99 import org.simantics.scl.compiler.internal.parsing.types.TForAllAst;
100 import org.simantics.scl.compiler.internal.parsing.types.TFunctionAst;
101 import org.simantics.scl.compiler.internal.parsing.types.TListAst;
102 import org.simantics.scl.compiler.internal.parsing.types.TPredAst;
103 import org.simantics.scl.compiler.internal.parsing.types.TTupleAst;
104 import org.simantics.scl.compiler.internal.parsing.types.TVarAst;
105 import org.simantics.scl.compiler.internal.parsing.types.TypeAst;
106 import org.simantics.scl.compiler.module.ImportDeclaration;
107 import org.simantics.scl.compiler.types.Types;
108
109
110 public class SCLParserImpl extends SCLParser {
111
112     private final SCLPostLexer lexer;
113     private SCLParserOptions options;
114
115     public SCLParserImpl(Reader reader) {
116         lexer = new SCLPostLexer(reader);
117     }
118     
119     public void setParserOptions(SCLParserOptions options) {
120         this.options = options;
121         lexer.setParserOptions(options);
122     }
123     
124     public boolean isEmpty() throws Exception {
125         return lexer.peekToken().id == SCLTerminals.EOF;
126     }
127
128     @Override
129     protected Token nextToken() {
130         try {
131             Token token = lexer.nextToken();
132             /*System.out.println("TOKEN " + token.text + " (" + TERMINAL_NAMES[token.id] + ")" +
133                     " [" 
134                     + Locations.beginOf(token.location) + ".." 
135                     + Locations.endOf(token.location) + "]");*/
136             return token;
137         } catch(Exception e) {
138             if(e instanceof RuntimeException)
139                 throw (RuntimeException)e;
140             else
141                 throw new RuntimeException(e);
142         }
143     }
144
145     @Override
146     protected Object reduceDeclarations() {
147         ArrayList<DeclarationAst> declarations = new ArrayList<DeclarationAst>(length()/2);
148         for(int i=1;i<length();i+=2)
149             declarations.add((DeclarationAst)get(i));
150         return declarations;
151     }
152
153     @Override
154     protected Object reduceModule() {
155         ArrayList<DeclarationAst> declarations = new ArrayList<DeclarationAst>(length()/2+1);
156         for(int i=0;i<length();i+=2)
157             declarations.add((DeclarationAst)get(i));
158         return declarations;
159     }
160
161     @Override
162     protected Object reduceLocalTypeAnnotation() {
163         if(length() == 1)
164             return get(0);
165         return new ETypeAnnotation((Expression)get(0), (TypeAst)get(2));
166     }
167
168     @Override
169     protected Object reduceTypeAnnotation() {
170         EVar[] names = new EVar[length()/2];
171         for(int i=0;i<names.length;++i)
172             names[i] = (EVar)get(i*2);
173         return new DValueTypeAst(
174                 names,
175                 (TypeAst)get(length()-1)
176                 );
177     }
178
179     @Override
180     protected Object reduceValueDefinition() {
181         Expression rhs = (Expression)get(1);
182         return new DValueAst((Expression)get(0), rhs);
183     }
184
185     @Override
186     protected Object reduceDataDefinition() {
187         int i=2;
188         ArrayList<String> parameters = new ArrayList<String>();
189         while(i < length()) {
190             Token token = (Token)get(i++);
191             if(token.id != SCLTerminals.ID)
192                 break;
193             parameters.add(token.text);
194         }
195         ArrayList<ConstructorAst> constructors = new ArrayList<ConstructorAst>(); 
196         for(;i < length();i+=2)
197             constructors.add((ConstructorAst)get(i));
198         return new DDataAst(
199                 ((Token)get(1)).text,
200                 parameters.toArray(new String[parameters.size()]),
201                 constructors.toArray(new ConstructorAst[constructors.size()]),
202                 new String[0]
203                 );
204     }
205
206     @Override
207     protected Object reduceTypeDefinition() {
208         int i=2;
209         ArrayList<String> parameters = new ArrayList<String>();
210         while(true) {
211             Token token = (Token)get(i++);
212             if(token.id != SCLTerminals.ID)
213                 break;
214             parameters.add(token.text);
215         }
216         return new DTypeAst(
217                 ((Token)get(1)).text,
218                 parameters.toArray(new String[parameters.size()]),
219                 (TypeAst)get(i)
220                 );
221     }
222
223     @Override
224     protected Object reduceClassDefinition() {
225         int i=1;
226         ArrayList<TypeAst> context;
227         if(get(i) instanceof Token)
228             context = new ArrayList<TypeAst>(0);
229         else
230             context = (ArrayList<TypeAst>)get(i++);
231         String name = ((Token)get(i++)).text;
232         ArrayList<String> parameters = new ArrayList<String>();
233         while(i < length()) {
234             Token token = (Token)get(i);
235             if(token.id != SCLTerminals.ID)
236                 break;
237             parameters.add(token.text);
238             ++i;
239         }
240         ArrayList<DeclarationAst> declarations = null;
241         FundepAst[] fundeps = FundepAst.EMPTY_ARRAY;
242         while(i < length()) {
243             Token token = (Token)get(i++);
244             if(token.id == SCLTerminals.WHERE) {
245                 declarations = (ArrayList<DeclarationAst>)get(i++);
246             }
247             else if(token.id == SCLTerminals.BAR) {
248                 fundeps = (FundepAst[])get(i++);
249             }
250             else
251                 throw new InternalCompilerError();
252         }
253         return new DClassAst(context, name,
254                 parameters.toArray(new String[parameters.size()]),
255                 fundeps,
256                 declarations);
257     }
258
259     @Override
260     protected Object reduceFundep() {
261         String[] from = new String[length()-2];
262         for(int i=0;i<from.length;++i)
263             from[i] = ((Token)get(i)).text;
264         String to = ((Token)get(length()-1)).text;
265         return new FundepAst(from, to);
266     }
267
268     @Override
269     protected Object reduceFundeps() {
270         FundepAst[] fundeps = new FundepAst[(length()+1)/2];
271         for(int i=0;i<fundeps.length;++i)
272             fundeps[i] = (FundepAst)get(i*2);
273         return fundeps;
274     }
275
276     @Override
277     protected Object reduceInstanceDefinition() {
278         int i=1;
279         ArrayList<TypeAst> context;
280         if(get(i) instanceof Token)
281             context = new ArrayList<TypeAst>(0);
282         else
283             context = (ArrayList<TypeAst>)get(i++);
284         Token nameToken = (Token)get(i++);
285         EVar name = new EVar(nameToken.location, nameToken.text);
286         ArrayList<TypeAst> parameters = new ArrayList<TypeAst>();
287         while(i < length()) {
288             Object symbol = get(i++);
289             if(symbol instanceof Token)
290                 break;            
291             parameters.add((TypeAst)symbol);
292         }
293         ArrayList<DeclarationAst> declarations = null;
294         if(i < length())
295             declarations = (ArrayList<DeclarationAst>)get(i);
296         return new DInstanceAst(context, name,
297                 parameters.toArray(new TypeAst[parameters.size()]),
298                 declarations);
299     }
300
301     @Override
302     protected Object reduceDerivingInstanceDefinition() {
303         int i=2;
304         ArrayList<TypeAst> context;
305         if(get(i) instanceof Token)
306             context = new ArrayList<TypeAst>(0);
307         else
308             context = (ArrayList<TypeAst>)get(i++);
309         Token nameToken = (Token)get(i++);
310         EVar name = new EVar(nameToken.location, nameToken.text);
311         ArrayList<TypeAst> parameters = new ArrayList<TypeAst>();
312         while(i < length()) {
313             Object symbol = get(i++);
314             parameters.add((TypeAst)symbol);
315         }
316         return new DDerivingInstanceAst(context, name,
317                 parameters.toArray(new TypeAst[parameters.size()]));
318     }
319
320     @Override
321     protected Object reduceDocumentationString() {
322         return new DDocumentationAst(((Token)get(1)).text);
323     }
324
325     @Override
326     protected Object reduceAnnotation() {
327         ArrayList<Expression> parameters = new ArrayList<Expression>(length()-1);
328         for(int i=1;i<length();++i)
329             parameters.add((Expression)get(i));
330         return new DAnnotationAst((Token)get(0), parameters);
331     }
332
333     @Override
334     protected Object reducePrecedenceDefinition() {
335         EVar[] symbols = new EVar[length()/2];
336         for(int i=0;i<symbols.length;++i)
337             symbols[i] = (EVar)get(2*i + 2);
338         Associativity associativity;
339         Token token = (Token)get(0);
340         if(token.text.equals("infixl"))
341             associativity = Associativity.LEFT;
342         else if(token.text.equals("infixr"))
343             associativity = Associativity.RIGHT;
344         else
345             associativity = Associativity.NONASSOC;
346         return new DFixityAst(
347                 new Precedence(
348                         Integer.parseInt(((Token)get(1)).text), 
349                         associativity), 
350                         symbols);
351     }
352
353     @Override
354     protected Object reduceImport() {
355         // (AS ID)? importSpec?
356         int pos = 0;
357         String importKeyword = ((Token)get(pos++)).text; // (IMPORT | INCLUDE) 
358         ++pos; // BEGIN_STRING
359         String moduleName = ((Token)get(pos++)).text; // END_STRING
360         String localName = "";
361         if(pos < length()) {
362             Object temp = get(pos);
363             if(temp instanceof Token) {
364                 Token token = (Token)temp;
365                 if(token.id == SCLTerminals.AS) {
366                     ++pos; // AS
367                     localName = ((Token)get(pos++)).text; // ID
368                 }
369             }
370         }
371         ImportDeclaration.ImportSpec spec = ImportDeclaration.DEFAULT_SPEC;
372         if(pos < length())
373             spec = (ImportDeclaration.ImportSpec)get(pos++);
374         return new ImportDeclaration(moduleName, localName,
375                         importKeyword.equals("include"),
376                         spec);
377     }
378
379     @Override
380     protected Object reduceJustImport() {
381         return get(0);
382     }
383
384     @Override
385     protected Object reduceImportJava() {
386         return new DImportJavaAst(((Token)get(2)).text, (ArrayList<DeclarationAst>)get(4));
387     }
388
389     @Override
390     protected Object reduceEffectDefinition() {
391         return new DEffectAst(
392                 ((Token)get(1)).text,
393                 ((Token)get(3)).text,
394                 ((Token)get(5)).text);
395     }
396
397     @Override
398     protected Object reduceVarId() {
399         return new EVar(((Token)get(0)).text);
400     }
401
402     @Override
403     protected Object reduceEscapedSymbol() {
404         return new EVar(((Token)get(0)).text);
405     }
406
407     @Override
408     protected Object reduceTupleTypeConstructor() {
409         return new TVarAst(Types.tupleConstructor(length()-1).name);
410     }
411
412     @Override
413     protected Object reduceArrow() {
414         int i=length()-1;
415         TypeAst result = (TypeAst)get(i);
416         i-=2;
417         while(i >= 0) {
418             if( ((Token)get(i+1)).text.equals("=>") )
419                 result = new TPredAst((TypeAst)get(i), result);
420             else
421                 result = new TFunctionAst((TypeAst)get(i), result);
422             i-=2;
423         }
424         return result;
425     }
426
427     @Override
428     protected Object reduceBinary() {
429         if(length() == 1)
430             return get(0);
431         int i=0;
432         EVar negation = null;
433         if(get(i) instanceof Token) {
434             Token token = (Token)get(i++);
435             negation = new EVar(token.location, token.text);
436         }
437         EBinary binary = new EBinary((Expression)get(i++), negation);
438         while(i < length()) {
439             EVar operator = (EVar)get(i++);
440             Expression right = (Expression)get(i++);
441             binary.rights.add(new EBinaryRightSide(operator, right));
442         }
443         return binary;
444     }
445
446     @Override
447     protected Object reduceSimpleRhs() {
448         if(length() == 2)
449             return get(1);
450         else {
451             EBlock block = (EBlock)get(3);
452             Expression expression = (Expression)get(1);
453             block.addStatement(new GuardStatement(expression));
454             block.location = Locations.location(Locations.beginOf(expression.location), Locations.endOf(block.location));
455             return block;
456         }
457     }
458
459     private GuardedExpressionGroup reduceGuardedExpressionGroup(int length) {
460         GuardedExpression[] expressions = new GuardedExpression[length];
461         for(int i=0;i<expressions.length;++i) {
462             expressions[i] = (GuardedExpression)get(i);
463         }
464         return new GuardedExpressionGroup(expressions);
465     }
466
467     @Override
468     protected Object reduceGuardedRhs() {
469         int length = length();
470         if(length > 2 && get(length-2) instanceof Token) {
471             EBlock block = (EBlock)get(length-1);
472             block.addStatement(new GuardStatement(reduceGuardedExpressionGroup(length-2)));
473             block.location = Locations.NO_LOCATION;
474             return block;
475         }
476         else
477             return reduceGuardedExpressionGroup(length);
478     }
479
480     @Override
481     protected Object reduceStatements() {
482         EBlock block = new EBlock();
483         if(length() > 2)
484             for(int i=1;i<length();i+=2)
485                 block.addStatement((Statement)get(i));
486         return block;
487     }
488
489     @Override
490     protected Object reduceConstructor() {
491         int idPos;
492         for(idPos=0;idPos<length();idPos+=2)
493             if(((Token)get(idPos)).id == SCLTerminals.ID)
494                 break;
495         DAnnotationAst[] annotations = idPos == 0 ? DAnnotationAst.EMPTY_ARRAY : new DAnnotationAst[idPos/2];
496         for(int i=0;i<idPos/2;++i)
497             annotations[i] = new DAnnotationAst((Token)get(i*2), 
498                     Arrays.asList((Expression)get(i*2+1)));
499         TypeAst[] parameters = new TypeAst[length()-idPos-1];
500         for(int i=0;i<parameters.length;++i)
501             parameters[i] = (TypeAst)get(i+idPos+1);
502         return new ConstructorAst(annotations, ((Token)get(idPos)).text, parameters, null);
503     }
504
505     @Override
506     protected Object reduceContext() {
507         ArrayList<TypeAst> result = new ArrayList<TypeAst>(length()/2-1);
508         for(int i=1;i<length()-2;i+=2)
509             result.add((TypeAst)get(i));
510         return result;
511     }
512
513     @Override
514     protected Object reduceTypeVar() {
515         return new TVarAst(((Token)get(0)).text);
516     }
517
518     @Override
519     protected Object reduceTupleType() {
520         if(length() == 2)
521             return new TTupleAst(TypeAst.EMPTY_ARRAY);
522         if(length() == 3) {
523             Symbol sym = (Symbol)get(1);
524             sym.location = Locations.NO_LOCATION;
525             return sym;
526         }
527         int dim = length()/2;
528         TypeAst[] parameters = new TypeAst[dim];
529         for(int i=0;i<dim;++i)
530             parameters[i] = (TypeAst)get(i*2+1);
531         return new TTupleAst(parameters);
532     }
533
534     @Override
535     protected Object reduceListType() {
536         return new TListAst((TypeAst)get(1));
537     }
538
539     @Override
540     protected Object reduceListTypeConstructor() {
541         return new TVarAst("[]");
542     }
543
544     @Override
545     protected Object reduceTupleConstructor() {
546         return new EVar(Types.tupleConstructor(length()-1).name);
547     }
548
549     @Override
550     protected Object reduceVar() {
551         return get(0);
552     }
553
554     @Override
555     protected Object reduceBlank() {
556         return new EVar("_");
557     }
558
559     @Override
560     protected Object reduceInteger() {
561         return new EIntegerLiteral(((Token)get(0)).text);
562     }
563
564     @Override
565     protected Object reduceFloat() {
566         return new ERealLiteral(((Token)get(0)).text);
567     }
568
569     @Override
570     protected Object reduceString() {
571         return get(0);
572     }
573
574     @Override
575     protected Object reduceChar() {
576         String text = ((Token)get(0)).text;
577         char c = text.charAt(text.length()-2);
578         if(text.length() == 4) {
579             switch(c) {
580             case 'n': c = '\n'; break;
581             case 't': c = '\t'; break;
582             case 'b': c = '\b'; break;
583             case 'f': c = '\f'; break;
584             case 'r': c = '\r'; break;
585             }
586         }
587         return new ELiteral(new CharacterConstant(c));
588     }
589
590     @Override
591     protected Object reduceTuple() {
592         if(length() == 2)
593             return new EConstant(Builtins.TUPLE_CONSTRUCTORS[0]);
594         if(length() == 3) {
595             Symbol sym = (Symbol)get(1);
596             sym.location = Locations.NO_LOCATION;
597             return sym;
598         }
599         int dim = length()/2;
600         Expression[] parameters = new Expression[dim];
601         for(int i=0;i<dim;++i)
602             parameters[i] = (Expression)get(i*2+1);
603         EConstant tupleConstructor = new EConstant(Builtins.TUPLE_CONSTRUCTORS[dim]);
604         tupleConstructor.location = Locations.location(
605                 Locations.beginOf(((Token)get(0)).location),
606                 Locations.endOf(((Token)get(length()-1)).location));
607         return new EApply(tupleConstructor, parameters);
608     }
609
610     public static Expression rightSection(EVar op, Expression e) {
611         long loc = Locations.combine(op.location, e.location);
612         Variable var = new Variable("rightSectionTemp");
613         return new ESimpleLambda(loc, var, 
614                 new EApply(loc, op, new EVariable(loc, var), e));
615     }
616
617     @Override
618     protected Object reduceRightSection() {
619         Variable var = new Variable("rightSectionTemp");
620         long loc = Locations.combine(((Token)get(0)).location, ((Token)get(length()-1)).location);
621         EVar symbol = (EVar)get(1);
622         return new ESimpleLambda(var, 
623                 new EApply(loc, symbol,
624                         new EVariable(loc, var), (Expression)get(2)));
625     }
626
627     @Override
628     protected Object reduceLeftSection() {
629         return new EApply((EVar)get(2), (Expression)get(1));
630     }
631
632     @Override
633     protected Object reduceListLiteral() {
634         if(length() == 2)
635             return new EListLiteral(Expression.EMPTY_ARRAY);
636         int dim = length()/2;
637         Expression[] components = new Expression[dim];
638         for(int i=0;i<dim;++i)
639             components[i] = (Expression)get(i*2+1);
640         return new EListLiteral(components);
641     }
642
643     @Override
644     protected Object reduceRange() {
645         return new ERange(
646                 (Expression)get(1),
647                 (Expression)get(3)
648                 );
649     }
650
651     @Override
652     protected Object reduceListComprehension() {
653         ListQualifier qualifier = (ListQualifier)get(3);
654         for(int i=5;i<length();i+=2) {
655             ListQualifier right = (ListQualifier)get(i);
656             if(right instanceof ListThen) {
657                 ((ListThen) right).setLeft(qualifier);
658                 qualifier = right;
659             }
660             else
661                 qualifier = new ListSeq(qualifier, right);
662         }
663         return new EListComprehension((Expression)get(1), qualifier);
664     }
665
666     @Override
667     protected Object reduceAs() {
668         Token id = (Token)get(0);
669         return new EAsPattern(new EVar(id.location, id.text), (Expression)get(2));
670     }
671
672     @Override
673     protected Object reduceGuardedExpEq() {
674         Expression[] guards = new Expression[length()/2-1];
675         for(int i=0;i<guards.length;++i)
676             guards[i] = (Expression)get(i*2+1);
677         return new GuardedExpression(guards, (Expression)get(length()-1));
678     }
679
680     @Override
681     protected Object reduceLambda() {
682         Expression[] patterns = new Expression[length()-3];
683         for(int i=0;i<patterns.length;++i)
684             patterns[i] = (Expression)get(i+1);
685         Case case_ = new Case(patterns, (Expression)get(length()-1));
686         case_.setLhs(Locations.combine(patterns[0].location, patterns[patterns.length-1].location));
687         return new ELambda(case_);
688     }
689
690     @Override
691     protected Object reduceLet() {
692         EBlock block = (EBlock)get(1);
693         Expression expression = (Expression)get(3);
694         block.addStatement(new GuardStatement(expression));
695         Token letToken = (Token)get(0);
696         block.location = Locations.location(
697                 Locations.beginOf(letToken.location),
698                 Locations.endOf(expression.location));
699         return block;
700     }
701
702     @Override
703     protected Object reduceIf() {
704         return new EIf(
705                 (Expression)get(1),
706                 (Expression)get(3),
707                 (Expression)get(5));
708     }
709
710     @Override
711     protected Object reduceMatch() {
712         Case[] cases = new Case[length()/2-2];
713         for(int i=0;i<cases.length;++i)
714             cases[i] = (Case)get(i*2+4);
715         return new EMatch((Expression)get(1), cases);
716     }
717
718     @Override
719     protected Object reduceDo() {
720         EBlock block = (EBlock)get(1);
721         Token doToken = (Token)get(0);
722         block.setMonadic( doToken.text.equals("mdo") );
723         block.location = Locations.location(Locations.beginOf(doToken.location), Locations.endOf(block.location));
724         return block;
725     }
726
727     @Override
728     protected Object reduceSelect() {
729         return new ESelect(((Token)get(0)).id, (Expression)get(1), new QConjunction((Query[])get(3)));
730     }
731     
732     @Override
733     protected Object reduceWhen() {
734         return new EWhen(
735                 new QConjunction((Query[])get(1)),
736                 (Expression)get(3));
737     }
738     
739     @Override
740     protected Object reduceEnforce() {
741         return new EEnforce(new QConjunction((Query[])get(1)));
742     }
743
744     @Override
745     protected Object reduceApply() {
746         if(length() == 1)
747             return get(0);
748         Expression[] parameters = new Expression[length()-1];
749         for(int i=0;i<parameters.length;++i)
750             parameters[i] = (Expression)get(i+1);
751         return new EApply((Expression)get(0), parameters);
752     }
753
754     @Override
755     protected Object reduceSymbol() {
756         return new EVar(((Token)get(0)).text);
757     }
758
759     @Override
760     protected Object reduceEscapedId() {
761         return new EVar(((Token)get(0)).text);
762     }
763
764     @Override
765     protected Object reduceMinus() {
766         return new EVar(((Token)get(0)).text);
767     }
768
769     @Override
770     protected Object reduceLess() {
771         return new EVar(((Token)get(0)).text);
772     }
773
774     @Override
775     protected Object reduceGreater() {
776         return new EVar(((Token)get(0)).text);
777     }
778
779     @Override
780     protected Object reduceDot() {
781         return new EVar(((Token)get(0)).text);
782     }
783
784     @Override
785     protected Object reduceCase() {
786         return new Case((Expression)get(0), (Expression)get(1));
787     }
788
789     @Override
790     protected Object reduceGuardQualifier() {
791         return new ListGuard((Expression)get(0));
792     }
793
794     @Override
795     protected Object reduceLetQualifier() {
796         return new ListAssignment((Expression)get(0), (Expression)get(2));
797     }
798
799     @Override
800     protected Object reduceBindQualifier() {
801         return new ListGenerator((Expression)get(0), (Expression)get(2));
802     }
803
804     @Override
805     protected Object reduceThenQualifier() {
806         return new ListThen((Expression)get(1), length()==4 ? (Expression)get(3) : null);
807     }
808
809     @Override
810     protected Object reduceGuardStatement() {
811         return new GuardStatement((Expression)get(0));
812     }
813
814     @Override
815     protected Object reduceLetStatement() {
816         return new LetStatement((Expression)get(0), (Expression)get(1));
817     }
818
819     @Override
820     protected Object reduceBindStatement() {
821         return new BindStatement((Expression)get(0), (Expression)get(2));
822     }
823
824     @Override
825     protected Object reduceSimpleCaseRhs() {
826         return get(1);
827     }
828
829     @Override
830     protected Object reduceGuardedCaseRhs() {
831         GuardedExpression[] expressions = new GuardedExpression[length()];
832         for(int i=0;i<expressions.length;++i)
833             expressions[i] = (GuardedExpression)get(i);
834         return new GuardedExpressionGroup(expressions);
835     }
836
837     @Override
838     protected Object reduceGuardedExpArrow() {
839         Expression[] guards = new Expression[length()/2-1];
840         for(int i=0;i<guards.length;++i)
841             guards[i] = (Expression)get(i*2+1);
842         return new GuardedExpression(guards, (Expression)get(length()-1));
843     }
844
845     @Override
846     protected Object reduceEffect() {
847         ArrayList<TypeAst> effects = new ArrayList<TypeAst>(length()/2-1);
848         for(int i=1;i<length()-1;i+=2) {
849             Token token = (Token)get(i);
850             TVarAst ast = new TVarAst(token.text);
851             ast.location = token.location;
852             effects.add(ast);
853         }
854         return new TEffectAst(effects, (TypeAst)get(length()-1));
855     }
856
857     @Override
858     protected Object reduceJustEtype() {
859         return get(0);
860     }
861
862     @Override
863     protected Object reduceForAll() {
864         String[] vars = new String[length()-3];
865         for(int i=0;i<vars.length;++i)
866             vars[i] = ((Token)get(i+1)).text;
867         return new TForAllAst(vars, (TypeAst)get(length()-1));
868     }
869
870     @Override
871     protected Object reduceApplyType() {
872         TypeAst[] parameters = new TypeAst[length()-1];
873         for(int i=0;i<parameters.length;++i)
874             parameters[i] = (TypeAst)get(i+1);
875         return new TApplyAst((TypeAst)get(0), parameters);
876     }
877
878     @Override
879     protected Object reduceDummy1() {
880         throw new UnsupportedOperationException();
881     }
882
883     @Override
884     protected void postReduce(Object reduced) {
885         if(!(reduced instanceof Symbol))
886             return;
887         Symbol sym = (Symbol)reduced;
888         if(sym.location != Locations.NO_LOCATION || length() == 0)
889             return;
890         Object last = get(length()-1);
891         if(!(last instanceof Symbol)) {
892             if(last instanceof List) {
893                 List<Object> ll = (List<Object>)last;
894                 last = ll.get(ll.size()-1);
895             }
896             else {
897                 Object[] ll = (Object[])last;
898                 if(ll.length > 0)
899                     last = ll[ll.length-1];
900                 else
901                     last = get(length()-2);
902             }
903         }
904         sym.location = (((Symbol)get(0)).location & 0xffffffff00000000L) 
905                 | (((Symbol)last).location & 0xffffffffL);
906         /*for(int i=0;i<length();++i) {
907             Object obj = get(i);
908             System.out.print(obj.getClass().getSimpleName());
909             if(obj instanceof Token) {
910                 Token t = (Token)obj;
911                 System.out.print("(" + t.text + ")");
912             }
913             if(obj instanceof Symbol) {
914                 Symbol s = (Symbol)obj;
915                 System.out.print("["+ Locations.beginOf(s.location) + "-" + Locations.endOf(s.location) + "]");
916             }
917             System.out.print(" ");
918         }
919         System.out.println("-> " + reduced.getClass().getSimpleName() + " " +
920                 Locations.beginOf(sym.location) + "-" + Locations.endOf(sym.location));*/
921     }
922
923     @Override
924     protected RuntimeException syntaxError(Token token, String description) {
925         throw new SCLSyntaxErrorException(token.location, description);
926     }
927
928     @Override
929     protected Object reduceIdAccessor() {
930         return new IdAccessor(((Token)get(0)).text);
931     }
932
933     @Override
934     protected Object reduceStringAccessor() {
935         return new StringAccessor(((Token)get(1)).text);
936     }
937
938     @Override
939     protected Object reduceExpAccessor() {
940         return new ExpressionAccessor((Expression)get(1));
941     }
942
943     @Override
944     protected Object reduceFieldAccess() {
945         if(length() == 1)
946             return get(0);
947         FieldAccessor[] accessors = new FieldAccessor[length()/2];
948         for(int i=0;i<accessors.length;++i) {
949             FieldAccessor accessor = (FieldAccessor)get(i*2+2);
950             accessor.accessSeparator = ((Token)get(i*2+1)).text.charAt(0);
951             accessors[i] = accessor;
952         }
953         return new EFieldAccess((Expression)get(0), accessors);
954     }
955
956     @Override
957     protected Object reduceQueryBlock() {
958         if(length() == 2)
959             return Query.EMPTY_ARRAY;
960         Query[] queries = new Query[length()/2];
961         for(int i=0;i<queries.length;++i)
962             queries[i] = (Query)get(2*i+1);
963         return queries;
964     }
965
966     @Override
967     protected Object reduceGuardQuery() {
968         return new QPreGuard((Expression)get(0));
969     }
970
971     @Override
972     protected Object reduceEqualsQuery() {
973         return new QPreEquals((Expression)get(0), (Expression)get(2));
974     }
975
976     @Override
977     protected Object reduceBindQuery() {
978         return new QPreBinds((Expression)get(0), (Expression)get(2));
979     }
980
981     @Override
982     protected Object reduceCompositeQuery() {
983         Query[] queries = (Query[])get(1);
984         switch(((Token)get(0)).text.charAt(1)) {
985         case '&': return new QConjunction(queries);
986         case '|': return new QDisjunction(queries);
987         case '!': return new QNegation(new QConjunction(queries));
988         case '?': return new QAlternative(queries);
989         default: throw new InternalCompilerError();
990         }
991     }
992     
993     @Override
994     protected Object reduceRuleStatement() {
995         return new RuleStatement((Expression)get(0), new QConjunction((Query[])get(2)));
996     }
997     
998     @Override
999     protected Object reduceEntityTypeAnnotation() {
1000         return new EEntityTypeAnnotation((Expression)get(0), (Token)get(2), 
1001                 length() > 3 
1002                 ? new QConjunction((Query[])get(length()-1))
1003                 : null);
1004     }
1005
1006     @Override
1007     protected Object reduceHashedId() {
1008         return new EVar("#" + ((Token)get(1)).text);
1009     }
1010
1011     @Override
1012     protected Object reduceStringLiteral() {
1013         int expCount = length()/3;
1014         if(expCount == 0)
1015             return new ELiteral(new StringConstant(((Token)get(1)).text));
1016         else {
1017             String[] strings = new String[expCount+1];
1018             Expression[] expressions = new Expression[expCount];
1019             for(int i=0;i<expCount;++i) {
1020                 strings[i] = ((Token)get(i*3+1)).text;
1021                 expressions[i] = (Expression)get(i*3+2);
1022             }
1023             strings[expCount] = ((Token)get(expCount*3+1)).text;
1024             return new EStringLiteral(strings, expressions);
1025         }
1026     }
1027
1028     @Override
1029     protected Object reduceOneCommand() {
1030         return null;
1031     }
1032     
1033     @Override
1034     protected Object reduceManyCommands() {
1035         return null;
1036     }
1037
1038     @Override
1039     protected Object reduceStatementCommand() {
1040         // to be extended in subclasses
1041         return null;
1042     }
1043
1044     @Override
1045     protected Object reduceImportCommand() {
1046         // to be extended in subclasses
1047         return null;
1048     }
1049     
1050     @Override
1051     protected Object reduceImportValueItem() {
1052         return new EVar(((Token)get(0)).text);
1053     }
1054     
1055     @Override
1056     protected Object reduceImportHiding() {
1057         EVar[] values = new EVar[(length()-2)/2];
1058         for(int i=0;i<values.length;++i)
1059             values[i] = (EVar)get(i*2+2);
1060         return new ImportDeclaration.ImportSpec(true, values);
1061     }
1062     
1063     @Override
1064     protected Object reduceImportShowing() {
1065         EVar[] values = new EVar[(length()-1)/2];
1066         for(int i=0;i<values.length;++i)
1067             values[i] = (EVar)get(i*2+1);
1068         return new ImportDeclaration.ImportSpec(false, values);
1069     }
1070
1071     @Override
1072     protected Object reduceRuleDeclarations() {
1073         ArrayList<Object> declarations = new ArrayList<Object>(length()/2);
1074         for(int i=1;i<length();i+=2)
1075             declarations.add((Object)get(i));
1076         return declarations;
1077     }
1078
1079     private static final String[] EMPTY_STRING_ARRAY = new String[0];
1080     
1081     @Override
1082     protected Object reduceRuleDefinition() {
1083         String[] extendsNames = EMPTY_STRING_ARRAY;
1084         if(length() >= 6) {
1085             int extendsCount = (length() - 4) / 2;
1086             extendsNames = new String[extendsCount]; 
1087             for(int i=0;i<extendsCount;++i)
1088                 extendsNames[i] = ((Token)get(3+i*2)).text;
1089         }
1090         
1091         DRuleAst rule = new DRuleAst(
1092                 ((Token)get(0)).id == SCLTerminals.ABSTRACT_RULE,
1093                 ((Token)get(1)).text,
1094                 extendsNames);
1095         
1096         ArrayList<Object> ruleDeclarations = (ArrayList<Object>)get(length()-1);
1097         ArrayList<Query> section = null;
1098         for(Object decl : ruleDeclarations) {
1099             if(decl instanceof DAnnotationAst) {
1100                 DAnnotationAst annotation = (DAnnotationAst)decl;
1101                 section = rule.getSection(annotation.id.text.substring(1));
1102             }
1103             else if(decl instanceof Query) {
1104                 if(section == null)
1105                     section = rule.getSection("when");
1106                 section.add((Query)decl);
1107             }
1108             else
1109                 throw new InternalCompilerError();
1110         }
1111         return rule;
1112     }
1113
1114     @Override
1115     protected Object reduceQueryRuleDeclaration() {
1116         return get(0);
1117     }
1118
1119     @Override
1120     protected Object reduceMappingRelationDefinition() {
1121         TypeAst[] types = new TypeAst[length()-2];
1122         for(int i=0;i<types.length;++i)
1123             types[i] = (TypeAst)get(i+2);
1124         return new DMappingRelationAst(
1125                 ((Token)get(1)).text,
1126                 types);
1127     }
1128
1129     @Override
1130     protected Object reduceTransformation() {
1131         return new ETransformation(
1132                 ((Token)get(1)).text,
1133                 new QConjunction((Query[])get(3))
1134                 );
1135     }
1136
1137     @Override
1138     protected Object reduceRelationDefinition() {
1139         return new DRelationAst((Expression)get(0), 
1140                 ((ArrayList<Object>)get(2)).toArray());
1141     }
1142
1143     @Override
1144     protected Object reduceRecord() {
1145         FieldAssignment[] fields = new FieldAssignment[length()/2-1];
1146         for(int i=0;i<fields.length;++i)
1147             fields[i] = (FieldAssignment)get(2+i*2);
1148         return new ERecord((Token)get(0), fields);
1149     }
1150
1151     @Override
1152     protected Object reduceField() {
1153         return new FieldAssignment(((Token)get(0)).text, (Expression)get(2));
1154     }
1155     
1156     @Override
1157     protected Object reduceFieldShorthand() {
1158         return new FieldAssignment(((Token)get(0)).text, null);
1159     }
1160
1161     @Override
1162     protected Object reduceRecordConstructor() {
1163         int idPos;
1164         for(idPos=0;idPos<length();idPos+=2)
1165             if(((Token)get(idPos)).id == SCLTerminals.ID)
1166                 break;
1167         DAnnotationAst[] annotations = idPos == 0 ? DAnnotationAst.EMPTY_ARRAY : new DAnnotationAst[idPos/2];
1168         for(int i=0;i<idPos/2;++i)
1169             annotations[i] = new DAnnotationAst((Token)get(i*2), 
1170                     Arrays.asList((Expression)get(i*2+1)));
1171         TypeAst[] parameters = new TypeAst[(length()-idPos-1)/2];
1172         String[] fieldNames = new String[parameters.length];
1173         for(int i=0;i<parameters.length;++i) {
1174             FieldDescription fieldDesc = (FieldDescription)get(idPos+(i+1)*2);
1175             parameters[i] = fieldDesc.type;
1176             fieldNames[i] = fieldDesc.name;
1177         }
1178         return new ConstructorAst(annotations, ((Token)get(idPos)).text, parameters, fieldNames);
1179     }
1180
1181     @Override
1182     protected Object reduceFieldDescription() {
1183         return new FieldDescription(((Token)get(0)).text, (TypeAst)get(2));
1184     }
1185
1186     @Override
1187     protected Object reduceEq() {
1188         return (Expression)get(2);
1189     }
1190
1191     @Override
1192     protected Object reduceEquationBlock() {
1193         if(length() == 0)
1194             return new EEquations(Equation.EMPTY_ARRAY);
1195         Equation[] equations = new Equation[length()/2+1];
1196         for(int i=0;i<equations.length;++i)
1197             equations[i] = (Equation)get(2*i);
1198         return new EEquations(equations);
1199     }
1200
1201     @Override
1202     protected Object reduceGuardEquation() {
1203         return new EqGuard((Expression)get(0));
1204     }
1205
1206     @Override
1207     protected Object reduceBasicEquation() {
1208         return new EqBasic((Expression)get(0), (Expression)get(2));
1209     }
1210 }