1 package org.simantics.scl.compiler.internal.parsing.parser;
4 import java.util.ArrayList;
5 import java.util.Arrays;
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;
110 public class SCLParserImpl extends SCLParser {
112 private final SCLPostLexer lexer;
113 private SCLParserOptions options;
115 public SCLParserImpl(Reader reader) {
116 lexer = new SCLPostLexer(reader);
119 public void setParserOptions(SCLParserOptions options) {
120 this.options = options;
121 lexer.setParserOptions(options);
124 public boolean isEmpty() throws Exception {
125 return lexer.peekToken().id == SCLTerminals.EOF;
129 protected Token nextToken() {
131 Token token = lexer.nextToken();
132 /*System.out.println("TOKEN " + token.text + " (" + TERMINAL_NAMES[token.id] + ")" +
134 + Locations.beginOf(token.location) + ".."
135 + Locations.endOf(token.location) + "]");*/
137 } catch(Exception e) {
138 if(e instanceof RuntimeException)
139 throw (RuntimeException)e;
141 throw new RuntimeException(e);
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));
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));
162 protected Object reduceLocalTypeAnnotation() {
165 return new ETypeAnnotation((Expression)get(0), (TypeAst)get(2));
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(
175 (TypeAst)get(length()-1)
180 protected Object reduceValueDefinition() {
181 Expression rhs = (Expression)get(1);
182 return new DValueAst((Expression)get(0), rhs);
186 protected Object reduceDataDefinition() {
188 ArrayList<String> parameters = new ArrayList<String>();
189 while(i < length()) {
190 Token token = (Token)get(i++);
191 if(token.id != SCLTerminals.ID)
193 parameters.add(token.text);
195 ArrayList<ConstructorAst> constructors = new ArrayList<ConstructorAst>();
196 for(;i < length();i+=2)
197 constructors.add((ConstructorAst)get(i));
199 ((Token)get(1)).text,
200 parameters.toArray(new String[parameters.size()]),
201 constructors.toArray(new ConstructorAst[constructors.size()]),
207 protected Object reduceTypeDefinition() {
209 ArrayList<String> parameters = new ArrayList<String>();
211 Token token = (Token)get(i++);
212 if(token.id != SCLTerminals.ID)
214 parameters.add(token.text);
217 ((Token)get(1)).text,
218 parameters.toArray(new String[parameters.size()]),
224 protected Object reduceClassDefinition() {
226 ArrayList<TypeAst> context;
227 if(get(i) instanceof Token)
228 context = new ArrayList<TypeAst>(0);
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)
237 parameters.add(token.text);
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++);
247 else if(token.id == SCLTerminals.BAR) {
248 fundeps = (FundepAst[])get(i++);
251 throw new InternalCompilerError();
253 return new DClassAst(context, name,
254 parameters.toArray(new String[parameters.size()]),
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);
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);
277 protected Object reduceInstanceDefinition() {
279 ArrayList<TypeAst> context;
280 if(get(i) instanceof Token)
281 context = new ArrayList<TypeAst>(0);
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)
291 parameters.add((TypeAst)symbol);
293 ArrayList<DeclarationAst> declarations = null;
295 declarations = (ArrayList<DeclarationAst>)get(i);
296 return new DInstanceAst(context, name,
297 parameters.toArray(new TypeAst[parameters.size()]),
302 protected Object reduceDerivingInstanceDefinition() {
304 ArrayList<TypeAst> context;
305 if(get(i) instanceof Token)
306 context = new ArrayList<TypeAst>(0);
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);
316 return new DDerivingInstanceAst(context, name,
317 parameters.toArray(new TypeAst[parameters.size()]));
321 protected Object reduceDocumentationString() {
322 return new DDocumentationAst(((Token)get(1)).text);
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);
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;
345 associativity = Associativity.NONASSOC;
346 return new DFixityAst(
348 Integer.parseInt(((Token)get(1)).text),
354 protected Object reduceImport() {
355 // (AS ID)? importSpec?
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 = "";
362 Object temp = get(pos);
363 if(temp instanceof Token) {
364 Token token = (Token)temp;
365 if(token.id == SCLTerminals.AS) {
367 localName = ((Token)get(pos++)).text; // ID
371 ImportDeclaration.ImportSpec spec = ImportDeclaration.DEFAULT_SPEC;
373 spec = (ImportDeclaration.ImportSpec)get(pos++);
374 return new ImportDeclaration(moduleName, localName,
375 importKeyword.equals("include"),
380 protected Object reduceJustImport() {
385 protected Object reduceImportJava() {
386 return new DImportJavaAst(((Token)get(2)).text, (ArrayList<DeclarationAst>)get(4));
390 protected Object reduceEffectDefinition() {
391 return new DEffectAst(
392 ((Token)get(1)).text,
393 ((Token)get(3)).text,
394 ((Token)get(5)).text);
398 protected Object reduceVarId() {
399 return new EVar(((Token)get(0)).text);
403 protected Object reduceEscapedSymbol() {
404 return new EVar(((Token)get(0)).text);
408 protected Object reduceTupleTypeConstructor() {
409 return new TVarAst(Types.tupleConstructor(length()-1).name);
413 protected Object reduceArrow() {
415 TypeAst result = (TypeAst)get(i);
418 if( ((Token)get(i+1)).text.equals("=>") )
419 result = new TPredAst((TypeAst)get(i), result);
421 result = new TFunctionAst((TypeAst)get(i), result);
428 protected Object reduceBinary() {
432 EVar negation = null;
433 if(get(i) instanceof Token) {
434 Token token = (Token)get(i++);
435 negation = new EVar(token.location, token.text);
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));
447 protected Object reduceSimpleRhs() {
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));
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);
464 return new GuardedExpressionGroup(expressions);
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;
477 return reduceGuardedExpressionGroup(length);
481 protected Object reduceStatements() {
482 EBlock block = new EBlock();
483 for(int i=1;i<length();i+=2)
484 block.addStatement((Statement)get(i));
489 protected Object reduceConstructor() {
491 for(idPos=0;idPos<length();idPos+=2)
492 if(((Token)get(idPos)).id == SCLTerminals.ID)
494 DAnnotationAst[] annotations = idPos == 0 ? DAnnotationAst.EMPTY_ARRAY : new DAnnotationAst[idPos/2];
495 for(int i=0;i<idPos/2;++i)
496 annotations[i] = new DAnnotationAst((Token)get(i*2),
497 Arrays.asList((Expression)get(i*2+1)));
498 TypeAst[] parameters = new TypeAst[length()-idPos-1];
499 for(int i=0;i<parameters.length;++i)
500 parameters[i] = (TypeAst)get(i+idPos+1);
501 return new ConstructorAst(annotations, ((Token)get(idPos)).text, parameters, null);
505 protected Object reduceContext() {
506 ArrayList<TypeAst> result = new ArrayList<TypeAst>(length()/2-1);
507 for(int i=1;i<length()-2;i+=2)
508 result.add((TypeAst)get(i));
513 protected Object reduceTypeVar() {
514 return new TVarAst(((Token)get(0)).text);
518 protected Object reduceTupleType() {
520 return new TTupleAst(TypeAst.EMPTY_ARRAY);
522 Symbol sym = (Symbol)get(1);
523 sym.location = Locations.NO_LOCATION;
526 int dim = length()/2;
527 TypeAst[] parameters = new TypeAst[dim];
528 for(int i=0;i<dim;++i)
529 parameters[i] = (TypeAst)get(i*2+1);
530 return new TTupleAst(parameters);
534 protected Object reduceListType() {
535 return new TListAst((TypeAst)get(1));
539 protected Object reduceListTypeConstructor() {
540 return new TVarAst("[]");
544 protected Object reduceTupleConstructor() {
545 return new EVar(Types.tupleConstructor(length()-1).name);
549 protected Object reduceVar() {
554 protected Object reduceBlank() {
555 return new EVar("_");
559 protected Object reduceInteger() {
560 return new EIntegerLiteral(((Token)get(0)).text);
564 protected Object reduceFloat() {
565 return new ERealLiteral(((Token)get(0)).text);
569 protected Object reduceString() {
574 protected Object reduceChar() {
575 String text = ((Token)get(0)).text;
576 char c = text.charAt(text.length()-2);
577 if(text.length() == 4) {
579 case 'n': c = '\n'; break;
580 case 't': c = '\t'; break;
581 case 'b': c = '\b'; break;
582 case 'f': c = '\f'; break;
583 case 'r': c = '\r'; break;
586 return new ELiteral(new CharacterConstant(c));
590 protected Object reduceTuple() {
592 return new EConstant(Builtins.TUPLE_CONSTRUCTORS[0]);
594 Symbol sym = (Symbol)get(1);
595 sym.location = Locations.NO_LOCATION;
598 int dim = length()/2;
599 Expression[] parameters = new Expression[dim];
600 for(int i=0;i<dim;++i)
601 parameters[i] = (Expression)get(i*2+1);
602 EConstant tupleConstructor = new EConstant(Builtins.TUPLE_CONSTRUCTORS[dim]);
603 tupleConstructor.location = Locations.location(
604 Locations.beginOf(((Token)get(0)).location),
605 Locations.endOf(((Token)get(length()-1)).location));
606 return new EApply(tupleConstructor, parameters);
609 public static Expression rightSection(EVar op, Expression e) {
610 long loc = Locations.combine(op.location, e.location);
611 Variable var = new Variable("rightSectionTemp");
612 return new ESimpleLambda(loc, var,
613 new EApply(loc, op, new EVariable(loc, var), e));
617 protected Object reduceRightSection() {
618 Variable var = new Variable("rightSectionTemp");
619 long loc = Locations.combine(((Token)get(0)).location, ((Token)get(length()-1)).location);
620 EVar symbol = (EVar)get(1);
621 return new ESimpleLambda(var,
622 new EApply(loc, symbol,
623 new EVariable(loc, var), (Expression)get(2)));
627 protected Object reduceLeftSection() {
628 return new EApply((EVar)get(2), (Expression)get(1));
632 protected Object reduceListLiteral() {
634 return new EListLiteral(Expression.EMPTY_ARRAY);
635 int dim = length()/2;
636 Expression[] components = new Expression[dim];
637 for(int i=0;i<dim;++i)
638 components[i] = (Expression)get(i*2+1);
639 return new EListLiteral(components);
643 protected Object reduceRange() {
651 protected Object reduceListComprehension() {
652 ListQualifier qualifier = (ListQualifier)get(3);
653 for(int i=5;i<length();i+=2) {
654 ListQualifier right = (ListQualifier)get(i);
655 if(right instanceof ListThen) {
656 ((ListThen) right).setLeft(qualifier);
660 qualifier = new ListSeq(qualifier, right);
662 return new EListComprehension((Expression)get(1), qualifier);
666 protected Object reduceAs() {
667 Token id = (Token)get(0);
668 return new EAsPattern(new EVar(id.location, id.text), (Expression)get(2));
672 protected Object reduceGuardedExpEq() {
673 Expression[] guards = new Expression[length()/2-1];
674 for(int i=0;i<guards.length;++i)
675 guards[i] = (Expression)get(i*2+1);
676 return new GuardedExpression(guards, (Expression)get(length()-1));
680 protected Object reduceLambda() {
681 Expression[] patterns = new Expression[length()-3];
682 for(int i=0;i<patterns.length;++i)
683 patterns[i] = (Expression)get(i+1);
684 Case case_ = new Case(patterns, (Expression)get(length()-1));
685 case_.setLhs(Locations.combine(patterns[0].location, patterns[patterns.length-1].location));
686 return new ELambda(case_);
690 protected Object reduceLet() {
691 EBlock block = (EBlock)get(1);
692 Expression expression = (Expression)get(3);
693 block.addStatement(new GuardStatement(expression));
694 Token letToken = (Token)get(0);
695 block.location = Locations.location(
696 Locations.beginOf(letToken.location),
697 Locations.endOf(expression.location));
702 protected Object reduceIf() {
710 protected Object reduceMatch() {
711 Case[] cases = new Case[length()/2-2];
712 for(int i=0;i<cases.length;++i)
713 cases[i] = (Case)get(i*2+4);
714 return new EMatch((Expression)get(1), cases);
718 protected Object reduceDo() {
719 EBlock block = (EBlock)get(1);
720 Token doToken = (Token)get(0);
721 block.setMonadic( doToken.text.equals("mdo") );
722 block.location = Locations.location(Locations.beginOf(doToken.location), Locations.endOf(block.location));
727 protected Object reduceSelect() {
728 return new ESelect(((Token)get(0)).id, (Expression)get(1), new QConjunction((Query[])get(3)));
732 protected Object reduceWhen() {
734 new QConjunction((Query[])get(1)),
739 protected Object reduceEnforce() {
740 return new EEnforce(new QConjunction((Query[])get(1)));
744 protected Object reduceApply() {
747 Expression[] parameters = new Expression[length()-1];
748 for(int i=0;i<parameters.length;++i)
749 parameters[i] = (Expression)get(i+1);
750 return new EApply((Expression)get(0), parameters);
754 protected Object reduceSymbol() {
755 return new EVar(((Token)get(0)).text);
759 protected Object reduceEscapedId() {
760 return new EVar(((Token)get(0)).text);
764 protected Object reduceMinus() {
765 return new EVar(((Token)get(0)).text);
769 protected Object reduceLess() {
770 return new EVar(((Token)get(0)).text);
774 protected Object reduceGreater() {
775 return new EVar(((Token)get(0)).text);
779 protected Object reduceDot() {
780 return new EVar(((Token)get(0)).text);
784 protected Object reduceCase() {
785 return new Case((Expression)get(0), (Expression)get(1));
789 protected Object reduceGuardQualifier() {
790 return new ListGuard((Expression)get(0));
794 protected Object reduceLetQualifier() {
795 return new ListAssignment((Expression)get(0), (Expression)get(2));
799 protected Object reduceBindQualifier() {
800 return new ListGenerator((Expression)get(0), (Expression)get(2));
804 protected Object reduceThenQualifier() {
805 return new ListThen((Expression)get(1), length()==4 ? (Expression)get(3) : null);
809 protected Object reduceGuardStatement() {
810 return new GuardStatement((Expression)get(0));
814 protected Object reduceLetStatement() {
815 return new LetStatement((Expression)get(0), (Expression)get(1));
819 protected Object reduceBindStatement() {
820 return new BindStatement((Expression)get(0), (Expression)get(2));
824 protected Object reduceSimpleCaseRhs() {
829 protected Object reduceGuardedCaseRhs() {
830 GuardedExpression[] expressions = new GuardedExpression[length()];
831 for(int i=0;i<expressions.length;++i)
832 expressions[i] = (GuardedExpression)get(i);
833 return new GuardedExpressionGroup(expressions);
837 protected Object reduceGuardedExpArrow() {
838 Expression[] guards = new Expression[length()/2-1];
839 for(int i=0;i<guards.length;++i)
840 guards[i] = (Expression)get(i*2+1);
841 return new GuardedExpression(guards, (Expression)get(length()-1));
845 protected Object reduceEffect() {
846 ArrayList<TypeAst> effects = new ArrayList<TypeAst>(length()/2-1);
847 for(int i=1;i<length()-1;i+=2) {
848 Token token = (Token)get(i);
849 TVarAst ast = new TVarAst(token.text);
850 ast.location = token.location;
853 return new TEffectAst(effects, (TypeAst)get(length()-1));
857 protected Object reduceJustEtype() {
862 protected Object reduceForAll() {
863 String[] vars = new String[length()-3];
864 for(int i=0;i<vars.length;++i)
865 vars[i] = ((Token)get(i+1)).text;
866 return new TForAllAst(vars, (TypeAst)get(length()-1));
870 protected Object reduceApplyType() {
871 TypeAst[] parameters = new TypeAst[length()-1];
872 for(int i=0;i<parameters.length;++i)
873 parameters[i] = (TypeAst)get(i+1);
874 return new TApplyAst((TypeAst)get(0), parameters);
878 protected Object reduceDummy1() {
879 throw new UnsupportedOperationException();
883 protected void postReduce(Object reduced) {
884 if(!(reduced instanceof Symbol))
886 Symbol sym = (Symbol)reduced;
887 if(sym.location != Locations.NO_LOCATION || length() == 0)
889 Object last = get(length()-1);
890 if(!(last instanceof Symbol)) {
891 if(last instanceof List) {
892 List<Object> ll = (List<Object>)last;
893 last = ll.get(ll.size()-1);
896 Object[] ll = (Object[])last;
898 last = ll[ll.length-1];
900 last = get(length()-2);
903 sym.location = (((Symbol)get(0)).location & 0xffffffff00000000L)
904 | (((Symbol)last).location & 0xffffffffL);
905 /*for(int i=0;i<length();++i) {
907 System.out.print(obj.getClass().getSimpleName());
908 if(obj instanceof Token) {
909 Token t = (Token)obj;
910 System.out.print("(" + t.text + ")");
912 if(obj instanceof Symbol) {
913 Symbol s = (Symbol)obj;
914 System.out.print("["+ Locations.beginOf(s.location) + "-" + Locations.endOf(s.location) + "]");
916 System.out.print(" ");
918 System.out.println("-> " + reduced.getClass().getSimpleName() + " " +
919 Locations.beginOf(sym.location) + "-" + Locations.endOf(sym.location));*/
923 protected RuntimeException syntaxError(Token token, String description) {
924 throw new SCLSyntaxErrorException(token.location, description);
928 protected Object reduceIdAccessor() {
929 return new IdAccessor(((Token)get(0)).text);
933 protected Object reduceStringAccessor() {
934 return new StringAccessor(((Token)get(1)).text);
938 protected Object reduceExpAccessor() {
939 return new ExpressionAccessor((Expression)get(1));
943 protected Object reduceFieldAccess() {
946 FieldAccessor[] accessors = new FieldAccessor[length()/2];
947 for(int i=0;i<accessors.length;++i) {
948 FieldAccessor accessor = (FieldAccessor)get(i*2+2);
949 accessor.accessSeparator = ((Token)get(i*2+1)).text.charAt(0);
950 accessors[i] = accessor;
952 return new EFieldAccess((Expression)get(0), accessors);
956 protected Object reduceQueryBlock() {
958 return Query.EMPTY_ARRAY;
959 Query[] queries = new Query[length()/2];
960 for(int i=0;i<queries.length;++i)
961 queries[i] = (Query)get(2*i+1);
966 protected Object reduceGuardQuery() {
967 return new QPreGuard((Expression)get(0));
971 protected Object reduceEqualsQuery() {
972 return new QPreEquals((Expression)get(0), (Expression)get(2));
976 protected Object reduceBindQuery() {
977 return new QPreBinds((Expression)get(0), (Expression)get(2));
981 protected Object reduceCompositeQuery() {
982 Query[] queries = (Query[])get(1);
983 switch(((Token)get(0)).text.charAt(1)) {
984 case '&': return new QConjunction(queries);
985 case '|': return new QDisjunction(queries);
986 case '!': return new QNegation(new QConjunction(queries));
987 case '?': return new QAlternative(queries);
988 default: throw new InternalCompilerError();
993 protected Object reduceRuleStatement() {
994 return new RuleStatement((Expression)get(0), new QConjunction((Query[])get(2)));
998 protected Object reduceEntityTypeAnnotation() {
999 return new EEntityTypeAnnotation((Expression)get(0), (Token)get(2),
1001 ? new QConjunction((Query[])get(length()-1))
1006 protected Object reduceHashedId() {
1007 return new EVar("#" + ((Token)get(1)).text);
1011 protected Object reduceStringLiteral() {
1012 int expCount = length()/3;
1014 return new ELiteral(new StringConstant(((Token)get(1)).text));
1016 String[] strings = new String[expCount+1];
1017 Expression[] expressions = new Expression[expCount];
1018 for(int i=0;i<expCount;++i) {
1019 strings[i] = ((Token)get(i*3+1)).text;
1020 expressions[i] = (Expression)get(i*3+2);
1022 strings[expCount] = ((Token)get(expCount*3+1)).text;
1023 return new EStringLiteral(strings, expressions);
1028 protected Object reduceOneCommand() {
1033 protected Object reduceManyCommands() {
1038 protected Object reduceStatementCommand() {
1039 // to be extended in subclasses
1044 protected Object reduceImportCommand() {
1045 // to be extended in subclasses
1050 protected Object reduceImportValueItem() {
1051 return new EVar(((Token)get(0)).text);
1055 protected Object reduceImportHiding() {
1056 EVar[] values = new EVar[(length()-2)/2];
1057 for(int i=0;i<values.length;++i)
1058 values[i] = (EVar)get(i*2+2);
1059 return new ImportDeclaration.ImportSpec(true, values);
1063 protected Object reduceImportShowing() {
1064 EVar[] values = new EVar[(length()-1)/2];
1065 for(int i=0;i<values.length;++i)
1066 values[i] = (EVar)get(i*2+1);
1067 return new ImportDeclaration.ImportSpec(false, values);
1071 protected Object reduceRuleDeclarations() {
1072 ArrayList<Object> declarations = new ArrayList<Object>(length()/2);
1073 for(int i=1;i<length();i+=2)
1074 declarations.add((Object)get(i));
1075 return declarations;
1078 private static final String[] EMPTY_STRING_ARRAY = new String[0];
1081 protected Object reduceRuleDefinition() {
1082 String[] extendsNames = EMPTY_STRING_ARRAY;
1084 int extendsCount = (length() - 4) / 2;
1085 extendsNames = new String[extendsCount];
1086 for(int i=0;i<extendsCount;++i)
1087 extendsNames[i] = ((Token)get(3+i*2)).text;
1090 DRuleAst rule = new DRuleAst(
1091 ((Token)get(0)).id == SCLTerminals.ABSTRACT_RULE,
1092 ((Token)get(1)).text,
1095 ArrayList<Object> ruleDeclarations = (ArrayList<Object>)get(length()-1);
1096 ArrayList<Query> section = null;
1097 for(Object decl : ruleDeclarations) {
1098 if(decl instanceof DAnnotationAst) {
1099 DAnnotationAst annotation = (DAnnotationAst)decl;
1100 section = rule.getSection(annotation.id.text.substring(1));
1102 else if(decl instanceof Query) {
1104 section = rule.getSection("when");
1105 section.add((Query)decl);
1108 throw new InternalCompilerError();
1114 protected Object reduceQueryRuleDeclaration() {
1119 protected Object reduceMappingRelationDefinition() {
1120 TypeAst[] types = new TypeAst[length()-2];
1121 for(int i=0;i<types.length;++i)
1122 types[i] = (TypeAst)get(i+2);
1123 return new DMappingRelationAst(
1124 ((Token)get(1)).text,
1129 protected Object reduceTransformation() {
1130 return new ETransformation(
1131 ((Token)get(1)).text,
1132 new QConjunction((Query[])get(3))
1137 protected Object reduceRelationDefinition() {
1138 return new DRelationAst((Expression)get(0),
1139 ((ArrayList<Object>)get(2)).toArray());
1143 protected Object reduceRecord() {
1144 FieldAssignment[] fields = new FieldAssignment[length()/2-1];
1145 for(int i=0;i<fields.length;++i)
1146 fields[i] = (FieldAssignment)get(2+i*2);
1147 return new ERecord((Token)get(0), fields);
1151 protected Object reduceField() {
1152 return new FieldAssignment(((Token)get(0)).text, (Expression)get(2));
1156 protected Object reduceFieldShorthand() {
1157 return new FieldAssignment(((Token)get(0)).text, null);
1161 protected Object reduceRecordConstructor() {
1163 for(idPos=0;idPos<length();idPos+=2)
1164 if(((Token)get(idPos)).id == SCLTerminals.ID)
1166 DAnnotationAst[] annotations = idPos == 0 ? DAnnotationAst.EMPTY_ARRAY : new DAnnotationAst[idPos/2];
1167 for(int i=0;i<idPos/2;++i)
1168 annotations[i] = new DAnnotationAst((Token)get(i*2),
1169 Arrays.asList((Expression)get(i*2+1)));
1170 TypeAst[] parameters = new TypeAst[(length()-idPos-1)/2];
1171 String[] fieldNames = new String[parameters.length];
1172 for(int i=0;i<parameters.length;++i) {
1173 FieldDescription fieldDesc = (FieldDescription)get(idPos+(i+1)*2);
1174 parameters[i] = fieldDesc.type;
1175 fieldNames[i] = fieldDesc.name;
1177 return new ConstructorAst(annotations, ((Token)get(idPos)).text, parameters, fieldNames);
1181 protected Object reduceFieldDescription() {
1182 return new FieldDescription(((Token)get(0)).text, (TypeAst)get(2));
1186 protected Object reduceEq() {
1187 return (Expression)get(2);
1191 protected Object reduceEquationBlock() {
1193 return new EEquations(Equation.EMPTY_ARRAY);
1194 Equation[] equations = new Equation[length()/2+1];
1195 for(int i=0;i<equations.length;++i)
1196 equations[i] = (Equation)get(2*i);
1197 return new EEquations(equations);
1201 protected Object reduceGuardEquation() {
1202 return new EqGuard((Expression)get(0));
1206 protected Object reduceBasicEquation() {
1207 return new EqBasic((Expression)get(0), (Expression)get(2));