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();
484 for(int i=1;i<length();i+=2)
485 block.addStatement((Statement)get(i));
490 protected Object reduceConstructor() {
492 for(idPos=0;idPos<length();idPos+=2)
493 if(((Token)get(idPos)).id == SCLTerminals.ID)
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);
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));
514 protected Object reduceTypeVar() {
515 return new TVarAst(((Token)get(0)).text);
519 protected Object reduceTupleType() {
521 return new TTupleAst(TypeAst.EMPTY_ARRAY);
523 Symbol sym = (Symbol)get(1);
524 sym.location = Locations.NO_LOCATION;
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);
535 protected Object reduceListType() {
536 return new TListAst((TypeAst)get(1));
540 protected Object reduceListTypeConstructor() {
541 return new TVarAst("[]");
545 protected Object reduceTupleConstructor() {
546 return new EVar(Types.tupleConstructor(length()-1).name);
550 protected Object reduceVar() {
555 protected Object reduceBlank() {
556 return new EVar("_");
560 protected Object reduceInteger() {
561 return new EIntegerLiteral(((Token)get(0)).text);
565 protected Object reduceFloat() {
566 return new ERealLiteral(((Token)get(0)).text);
570 protected Object reduceString() {
575 protected Object reduceChar() {
576 String text = ((Token)get(0)).text;
577 char c = text.charAt(text.length()-2);
578 if(text.length() == 4) {
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;
587 return new ELiteral(new CharacterConstant(c));
591 protected Object reduceTuple() {
593 return new EConstant(Builtins.TUPLE_CONSTRUCTORS[0]);
595 Symbol sym = (Symbol)get(1);
596 sym.location = Locations.NO_LOCATION;
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);
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));
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)));
628 protected Object reduceLeftSection() {
629 return new EApply((EVar)get(2), (Expression)get(1));
633 protected Object reduceListLiteral() {
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);
644 protected Object reduceRange() {
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);
661 qualifier = new ListSeq(qualifier, right);
663 return new EListComprehension((Expression)get(1), qualifier);
667 protected Object reduceAs() {
668 Token id = (Token)get(0);
669 return new EAsPattern(new EVar(id.location, id.text), (Expression)get(2));
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));
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_);
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));
703 protected Object reduceIf() {
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);
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));
728 protected Object reduceSelect() {
729 return new ESelect(((Token)get(0)).id, (Expression)get(1), new QConjunction((Query[])get(3)));
733 protected Object reduceWhen() {
735 new QConjunction((Query[])get(1)),
740 protected Object reduceEnforce() {
741 return new EEnforce(new QConjunction((Query[])get(1)));
745 protected Object reduceApply() {
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);
755 protected Object reduceSymbol() {
756 return new EVar(((Token)get(0)).text);
760 protected Object reduceEscapedId() {
761 return new EVar(((Token)get(0)).text);
765 protected Object reduceMinus() {
766 return new EVar(((Token)get(0)).text);
770 protected Object reduceLess() {
771 return new EVar(((Token)get(0)).text);
775 protected Object reduceGreater() {
776 return new EVar(((Token)get(0)).text);
780 protected Object reduceDot() {
781 return new EVar(((Token)get(0)).text);
785 protected Object reduceCase() {
786 return new Case((Expression)get(0), (Expression)get(1));
790 protected Object reduceGuardQualifier() {
791 return new ListGuard((Expression)get(0));
795 protected Object reduceLetQualifier() {
796 return new ListAssignment((Expression)get(0), (Expression)get(2));
800 protected Object reduceBindQualifier() {
801 return new ListGenerator((Expression)get(0), (Expression)get(2));
805 protected Object reduceThenQualifier() {
806 return new ListThen((Expression)get(1), length()==4 ? (Expression)get(3) : null);
810 protected Object reduceGuardStatement() {
811 return new GuardStatement((Expression)get(0));
815 protected Object reduceLetStatement() {
816 return new LetStatement((Expression)get(0), (Expression)get(1));
820 protected Object reduceBindStatement() {
821 return new BindStatement((Expression)get(0), (Expression)get(2));
825 protected Object reduceSimpleCaseRhs() {
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);
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));
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;
854 return new TEffectAst(effects, (TypeAst)get(length()-1));
858 protected Object reduceJustEtype() {
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));
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);
879 protected Object reduceDummy1() {
880 throw new UnsupportedOperationException();
884 protected void postReduce(Object reduced) {
885 if(!(reduced instanceof Symbol))
887 Symbol sym = (Symbol)reduced;
888 if(sym.location != Locations.NO_LOCATION || length() == 0)
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);
897 Object[] ll = (Object[])last;
899 last = ll[ll.length-1];
901 last = get(length()-2);
904 sym.location = (((Symbol)get(0)).location & 0xffffffff00000000L)
905 | (((Symbol)last).location & 0xffffffffL);
906 /*for(int i=0;i<length();++i) {
908 System.out.print(obj.getClass().getSimpleName());
909 if(obj instanceof Token) {
910 Token t = (Token)obj;
911 System.out.print("(" + t.text + ")");
913 if(obj instanceof Symbol) {
914 Symbol s = (Symbol)obj;
915 System.out.print("["+ Locations.beginOf(s.location) + "-" + Locations.endOf(s.location) + "]");
917 System.out.print(" ");
919 System.out.println("-> " + reduced.getClass().getSimpleName() + " " +
920 Locations.beginOf(sym.location) + "-" + Locations.endOf(sym.location));*/
924 protected RuntimeException syntaxError(Token token, String description) {
925 throw new SCLSyntaxErrorException(token.location, description);
929 protected Object reduceIdAccessor() {
930 return new IdAccessor(((Token)get(0)).text);
934 protected Object reduceStringAccessor() {
935 return new StringAccessor(((Token)get(1)).text);
939 protected Object reduceExpAccessor() {
940 return new ExpressionAccessor((Expression)get(1));
944 protected Object reduceFieldAccess() {
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;
953 return new EFieldAccess((Expression)get(0), accessors);
957 protected Object reduceQueryBlock() {
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);
967 protected Object reduceGuardQuery() {
968 return new QPreGuard((Expression)get(0));
972 protected Object reduceEqualsQuery() {
973 return new QPreEquals((Expression)get(0), (Expression)get(2));
977 protected Object reduceBindQuery() {
978 return new QPreBinds((Expression)get(0), (Expression)get(2));
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();
994 protected Object reduceRuleStatement() {
995 return new RuleStatement((Expression)get(0), new QConjunction((Query[])get(2)));
999 protected Object reduceEntityTypeAnnotation() {
1000 return new EEntityTypeAnnotation((Expression)get(0), (Token)get(2),
1002 ? new QConjunction((Query[])get(length()-1))
1007 protected Object reduceHashedId() {
1008 return new EVar("#" + ((Token)get(1)).text);
1012 protected Object reduceStringLiteral() {
1013 int expCount = length()/3;
1015 return new ELiteral(new StringConstant(((Token)get(1)).text));
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);
1023 strings[expCount] = ((Token)get(expCount*3+1)).text;
1024 return new EStringLiteral(strings, expressions);
1029 protected Object reduceOneCommand() {
1034 protected Object reduceManyCommands() {
1039 protected Object reduceStatementCommand() {
1040 // to be extended in subclasses
1045 protected Object reduceImportCommand() {
1046 // to be extended in subclasses
1051 protected Object reduceImportValueItem() {
1052 return new EVar(((Token)get(0)).text);
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);
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);
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;
1079 private static final String[] EMPTY_STRING_ARRAY = new String[0];
1082 protected Object reduceRuleDefinition() {
1083 String[] extendsNames = EMPTY_STRING_ARRAY;
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;
1091 DRuleAst rule = new DRuleAst(
1092 ((Token)get(0)).id == SCLTerminals.ABSTRACT_RULE,
1093 ((Token)get(1)).text,
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));
1103 else if(decl instanceof Query) {
1105 section = rule.getSection("when");
1106 section.add((Query)decl);
1109 throw new InternalCompilerError();
1115 protected Object reduceQueryRuleDeclaration() {
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,
1130 protected Object reduceTransformation() {
1131 return new ETransformation(
1132 ((Token)get(1)).text,
1133 new QConjunction((Query[])get(3))
1138 protected Object reduceRelationDefinition() {
1139 return new DRelationAst((Expression)get(0),
1140 ((ArrayList<Object>)get(2)).toArray());
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);
1152 protected Object reduceField() {
1153 return new FieldAssignment(((Token)get(0)).text, (Expression)get(2));
1157 protected Object reduceFieldShorthand() {
1158 return new FieldAssignment(((Token)get(0)).text, null);
1162 protected Object reduceRecordConstructor() {
1164 for(idPos=0;idPos<length();idPos+=2)
1165 if(((Token)get(idPos)).id == SCLTerminals.ID)
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;
1178 return new ConstructorAst(annotations, ((Token)get(idPos)).text, parameters, fieldNames);
1182 protected Object reduceFieldDescription() {
1183 return new FieldDescription(((Token)get(0)).text, (TypeAst)get(2));
1187 protected Object reduceEq() {
1188 return (Expression)get(2);
1192 protected Object reduceEquationBlock() {
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);
1202 protected Object reduceGuardEquation() {
1203 return new EqGuard((Expression)get(0));
1207 protected Object reduceBasicEquation() {
1208 return new EqBasic((Expression)get(0), (Expression)get(2));