public Expression gen(Context ctx) throws SemanticException {
    Expression res = null;
    Expression srcExpr = null;
    String opname = fOp.getText();

    if (fSrcExpr != null) {
      srcExpr = fSrcExpr.gen(ctx);
      res = gen1(ctx, srcExpr);
    } else {
      // if no source expression is given, it is either a
      // variable or a reference to expression determined by
      // context. In the latter case we use the context as
      // source expression and proceed as if it has been given
      // explicitly.
      if (!fHasParentheses) {
        // variable?
        // (7) check for variable
        Type type = ctx.varTable().lookup(opname);
        if (type != null) res = new ExpVariable(opname, type);
      }

      // do we have a context expression that is implicitly
      // assumed to be the source expression?
      if (res == null) {
        ExprContext ec = ctx.exprContext();
        if (!ec.isEmpty()) {
          // construct source expression
          ExprContext.Entry e = ec.peek();
          srcExpr = new ExpVariable(e.fName, e.fType);
          if (e.fType.isCollection()) fFollowsArrow = true;
          res = gen1(ctx, srcExpr);
        } else
          throw new SemanticException(
              fOp,
              "Undefined " + (fHasParentheses ? "operation" : "variable") + " `" + opname + "'.");
      }
    }

    if (fIsPre) {
      if (!ctx.insidePostCondition())
        throw new SemanticException(fOp, "Modifier @pre is only allowed in postconditions.");
      res.setIsPre();
    }

    if (opname.equals("oclIsNew")) {
      if (!ctx.insidePostCondition())
        throw new SemanticException(fOp, "Operation oclIsNew is only allowed in postconditions.");
    }
    return res;
  }
Example #2
0
  private ExprContext expr(int _p) throws RecognitionException {
    ParserRuleContext _parentctx = _ctx;
    int _parentState = getState();
    ExprContext _localctx = new ExprContext(_ctx, _parentState);
    ExprContext _prevctx = _localctx;
    int _startState = 16;
    enterRecursionRule(_localctx, 16, RULE_expr, _p);
    int _la;
    try {
      int _alt;
      enterOuterAlt(_localctx, 1);
      {
        setState(107);
        switch (getInterpreter().adaptivePredict(_input, 10, _ctx)) {
          case 1:
            {
              _localctx = new NegateContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;

              setState(91);
              match(T__13);
              setState(92);
              expr(8);
            }
            break;
          case 2:
            {
              _localctx = new NotContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;
              setState(93);
              match(T__14);
              setState(94);
              expr(7);
            }
            break;
          case 3:
            {
              _localctx = new CallContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;
              setState(95);
              match(ID);
              setState(96);
              match(T__2);
              setState(98);
              _la = _input.LA(1);
              if ((((_la) & ~0x3f) == 0
                  && ((1L << _la)
                          & ((1L << T__2)
                              | (1L << T__13)
                              | (1L << T__14)
                              | (1L << ID)
                              | (1L << INT)))
                      != 0)) {
                {
                  setState(97);
                  exprList();
                }
              }

              setState(100);
              match(T__3);
            }
            break;
          case 4:
            {
              _localctx = new VarContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;
              setState(101);
              match(ID);
            }
            break;
          case 5:
            {
              _localctx = new IntContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;
              setState(102);
              match(INT);
            }
            break;
          case 6:
            {
              _localctx = new ParensContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;
              setState(103);
              match(T__2);
              setState(104);
              expr(0);
              setState(105);
              match(T__3);
            }
            break;
        }
        _ctx.stop = _input.LT(-1);
        setState(125);
        _errHandler.sync(this);
        _alt = getInterpreter().adaptivePredict(_input, 12, _ctx);
        while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
          if (_alt == 1) {
            if (_parseListeners != null) triggerExitRuleEvent();
            _prevctx = _localctx;
            {
              setState(123);
              switch (getInterpreter().adaptivePredict(_input, 11, _ctx)) {
                case 1:
                  {
                    _localctx = new MultContext(new ExprContext(_parentctx, _parentState));
                    pushNewRecursionContext(_localctx, _startState, RULE_expr);
                    setState(109);
                    if (!(precpred(_ctx, 6)))
                      throw new FailedPredicateException(this, "precpred(_ctx, 6)");
                    setState(110);
                    match(T__15);
                    setState(111);
                    expr(7);
                  }
                  break;
                case 2:
                  {
                    _localctx = new AddSubContext(new ExprContext(_parentctx, _parentState));
                    pushNewRecursionContext(_localctx, _startState, RULE_expr);
                    setState(112);
                    if (!(precpred(_ctx, 5)))
                      throw new FailedPredicateException(this, "precpred(_ctx, 5)");
                    setState(113);
                    _la = _input.LA(1);
                    if (!(_la == T__13 || _la == T__16)) {
                      _errHandler.recoverInline(this);
                    } else {
                      consume();
                    }
                    setState(114);
                    expr(6);
                  }
                  break;
                case 3:
                  {
                    _localctx = new EqualContext(new ExprContext(_parentctx, _parentState));
                    pushNewRecursionContext(_localctx, _startState, RULE_expr);
                    setState(115);
                    if (!(precpred(_ctx, 4)))
                      throw new FailedPredicateException(this, "precpred(_ctx, 4)");
                    setState(116);
                    match(T__17);
                    setState(117);
                    expr(5);
                  }
                  break;
                case 4:
                  {
                    _localctx = new IndexContext(new ExprContext(_parentctx, _parentState));
                    pushNewRecursionContext(_localctx, _startState, RULE_expr);
                    setState(118);
                    if (!(precpred(_ctx, 9)))
                      throw new FailedPredicateException(this, "precpred(_ctx, 9)");
                    setState(119);
                    match(T__11);
                    setState(120);
                    expr(0);
                    setState(121);
                    match(T__12);
                  }
                  break;
              }
            }
          }
          setState(127);
          _errHandler.sync(this);
          _alt = getInterpreter().adaptivePredict(_input, 12, _ctx);
        }
      }
    } catch (RecognitionException re) {
      _localctx.exception = re;
      _errHandler.reportError(this, re);
      _errHandler.recover(this, re);
    } finally {
      unrollRecursionContexts(_parentctx);
    }
    return _localctx;
  }
  public Expression gen(Context ctx) throws SemanticException {
    String opname = fOp.getText();
    Expression res = null;
    Expression range, expr;

    // check for empty range: do we have a context expression that
    // is implicitly assumed to be the source expression?
    if (fRange != null) range = fRange.gen(ctx);
    else {
      ExprContext ec = ctx.exprContext();
      if (!ec.isEmpty()) {
        // construct source expression
        ExprContext.Entry e = ec.peek();
        range = new ExpVariable(e.fName, e.fType);
      } else throw new SemanticException(fOp, "Need a collection to apply `" + opname + "'.");
    }

    if (!range.type().isCollection())
      throw new SemanticException(
          fOp,
          "Source of `"
              + opname
              + "' expression must be a collection, "
              + "found source expression of type `"
              + range.type()
              + "'.");

    VarDeclList declList = new VarDeclList(true);

    if (fDeclList.isEmpty()) {
      // when there are no explicit var decls, we declare an
      // internal variable with the element type
      ExprContext ec = ctx.exprContext();
      CollectionType ct = (CollectionType) range.type();
      String var = ec.push(ct.elemType());
      expr = fExpr.gen(ctx);
      ec.pop();

      // use the generated element variable
      VarDecl decl = new VarDecl(var, ct.elemType());
      declList.add(decl);
    } else {
      declList = fDeclList.gen(ctx, range);

      // enter declared variable into scope before evaluating
      // the argument expression
      Symtable vars = ctx.varTable();
      vars.enterScope();
      fDeclList.addVariablesToSymtable(vars, declList.varDecl(0).type());
      expr = fExpr.gen(ctx);
      vars.exitScope();
    }

    try {
      Integer id = (Integer) GParser.queryIdentMap.get(opname);
      if (id == null)
        throw new SemanticException(
            fOp, "Internal error: unknown query operation `" + opname + "'.");

      int idval = id.intValue();
      switch (idval) {
        case GParser.Q_SELECT_ID:
        case GParser.Q_COLLECT_ID:
        case GParser.Q_REJECT_ID:
        case GParser.Q_ISUNIQUE_ID:
        case GParser.Q_SORTEDBY_ID:
        case GParser.Q_ANY_ID:
        case GParser.Q_ONE_ID:
          VarDecl decl;
          if (declList.isEmpty()) decl = null;
          else if (declList.size() == 1) decl = declList.varDecl(0);
          else
            throw new SemanticException(
                fOp, "Only one element variable in " + opname + " expression allowed.");
          switch (idval) {
            case GParser.Q_SELECT_ID:
              res = new ExpSelect(decl, range, expr);
              break;
            case GParser.Q_COLLECT_ID:
              res = new ExpCollect(decl, range, expr);
              break;
            case GParser.Q_REJECT_ID:
              res = new ExpReject(decl, range, expr);
              break;
            case GParser.Q_ISUNIQUE_ID:
              res = new ExpIsUnique(decl, range, expr);
              break;
            case GParser.Q_SORTEDBY_ID:
              res = new ExpSortedBy(decl, range, expr);
              break;
            case GParser.Q_ANY_ID:
              res = new ExpAny(decl, range, expr);
              break;
            case GParser.Q_ONE_ID:
              res = new ExpOne(decl, range, expr);
              break;
          }
          break;
        case GParser.Q_EXISTS_ID:
          res = new ExpExists(declList, range, expr);
          break;
        case GParser.Q_FORALL_ID:
          res = new ExpForAll(declList, range, expr);
          break;
        default:
          // internal error
          throw new SemanticException(
              fOp, "Internal error: unknown query operation `" + opname + "'.");
      }
    } catch (ExpInvalidException ex) {
      throw new SemanticException(fOp, ex);
    }
    return res;
  }
Example #4
0
  public final ExprContext expr(int _p) throws RecognitionException {
    ParserRuleContext _parentctx = _ctx;
    int _parentState = getState();
    ExprContext _localctx = new ExprContext(_ctx, _parentState, _p);
    ExprContext _prevctx = _localctx;
    int _startState = 4;
    enterRecursionRule(_localctx, RULE_expr);
    int _la;
    try {
      int _alt;
      enterOuterAlt(_localctx, 1);
      {
        setState(29);
        switch (_input.LA(1)) {
          case INT:
            {
              setState(23);
              match(INT);
            }
            break;
          case ID:
            {
              setState(24);
              match(ID);
            }
            break;
          case 5:
            {
              setState(25);
              match(5);
              setState(26);
              expr(0);
              setState(27);
              match(1);
            }
            break;
          default:
            throw new NoViableAltException(this);
        }
        _ctx.stop = _input.LT(-1);
        setState(39);
        _errHandler.sync(this);
        _alt = getInterpreter().adaptivePredict(_input, 4, _ctx);
        while (_alt != 2 && _alt != -1) {
          if (_alt == 1) {
            if (_parseListeners != null) triggerExitRuleEvent();
            _prevctx = _localctx;
            {
              setState(37);
              switch (getInterpreter().adaptivePredict(_input, 3, _ctx)) {
                case 1:
                  {
                    _localctx = new ExprContext(_parentctx, _parentState, _p);
                    pushNewRecursionContext(_localctx, _startState, RULE_expr);
                    setState(31);
                    if (!(5 >= _localctx._p)) throw new FailedPredicateException(this, "5 >= $_p");
                    setState(32);
                    _la = _input.LA(1);
                    if (!(_la == 3 || _la == 6)) {
                      _errHandler.recoverInline(this);
                    }
                    consume();
                    setState(33);
                    expr(6);
                  }
                  break;

                case 2:
                  {
                    _localctx = new ExprContext(_parentctx, _parentState, _p);
                    pushNewRecursionContext(_localctx, _startState, RULE_expr);
                    setState(34);
                    if (!(4 >= _localctx._p)) throw new FailedPredicateException(this, "4 >= $_p");
                    setState(35);
                    _la = _input.LA(1);
                    if (!(_la == 2 || _la == 4)) {
                      _errHandler.recoverInline(this);
                    }
                    consume();
                    setState(36);
                    expr(5);
                  }
                  break;
              }
            }
          }
          setState(41);
          _errHandler.sync(this);
          _alt = getInterpreter().adaptivePredict(_input, 4, _ctx);
        }
      }
    } catch (RecognitionException re) {
      _localctx.exception = re;
      _errHandler.reportError(this, re);
      _errHandler.recover(this, re);
    } finally {
      unrollRecursionContexts(_parentctx);
    }
    return _localctx;
  }
Example #5
0
  public final ExprContext expr(int _p) throws RecognitionException {
    ParserRuleContext _parentctx = _ctx;
    int _parentState = getState();
    ExprContext _localctx = new ExprContext(_ctx, _parentState, _p);
    ExprContext _prevctx = _localctx;
    int _startState = 16;
    enterRecursionRule(_localctx, RULE_expr);
    int _la;
    try {
      int _alt;
      enterOuterAlt(_localctx, 1);
      {
        setState(105);
        switch (getInterpreter().adaptivePredict(_input, 9, _ctx)) {
          case 1:
            {
              _localctx = new NegateContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;

              setState(89);
              match(6);
              setState(90);
              expr(8);
            }
            break;

          case 2:
            {
              _localctx = new NotContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;
              setState(91);
              match(21);
              setState(92);
              expr(7);
            }
            break;

          case 3:
            {
              _localctx = new CallContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;
              setState(93);
              match(ID);
              setState(94);
              match(8);
              setState(96);
              _la = _input.LA(1);
              if ((((_la) & ~0x3f) == 0
                  && ((1L << _la) & ((1L << 6) | (1L << 8) | (1L << 21) | (1L << ID) | (1L << INT)))
                      != 0)) {
                {
                  setState(95);
                  exprList();
                }
              }

              setState(98);
              match(2);
            }
            break;

          case 4:
            {
              _localctx = new VarContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;
              setState(99);
              match(ID);
            }
            break;

          case 5:
            {
              _localctx = new IntContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;
              setState(100);
              match(INT);
            }
            break;

          case 6:
            {
              _localctx = new ParensContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;
              setState(101);
              match(8);
              setState(102);
              expr(0);
              setState(103);
              match(2);
            }
            break;
        }
        _ctx.stop = _input.LT(-1);
        setState(123);
        _errHandler.sync(this);
        _alt = getInterpreter().adaptivePredict(_input, 11, _ctx);
        while (_alt != 2 && _alt != -1) {
          if (_alt == 1) {
            if (_parseListeners != null) triggerExitRuleEvent();
            _prevctx = _localctx;
            {
              setState(121);
              switch (getInterpreter().adaptivePredict(_input, 10, _ctx)) {
                case 1:
                  {
                    _localctx = new MultContext(new ExprContext(_parentctx, _parentState, _p));
                    pushNewRecursionContext(_localctx, _startState, RULE_expr);
                    setState(107);
                    if (!(6 >= _localctx._p)) throw new FailedPredicateException(this, "6 >= $_p");
                    setState(108);
                    match(7);
                    setState(109);
                    expr(7);
                  }
                  break;

                case 2:
                  {
                    _localctx = new AddSubContext(new ExprContext(_parentctx, _parentState, _p));
                    pushNewRecursionContext(_localctx, _startState, RULE_expr);
                    setState(110);
                    if (!(5 >= _localctx._p)) throw new FailedPredicateException(this, "5 >= $_p");
                    setState(111);
                    _la = _input.LA(1);
                    if (!(_la == 4 || _la == 6)) {
                      _errHandler.recoverInline(this);
                    }
                    consume();
                    setState(112);
                    expr(6);
                  }
                  break;

                case 3:
                  {
                    _localctx = new EqualContext(new ExprContext(_parentctx, _parentState, _p));
                    pushNewRecursionContext(_localctx, _startState, RULE_expr);
                    setState(113);
                    if (!(4 >= _localctx._p)) throw new FailedPredicateException(this, "4 >= $_p");
                    setState(114);
                    match(18);
                    setState(115);
                    expr(5);
                  }
                  break;

                case 4:
                  {
                    _localctx = new IndexContext(new ExprContext(_parentctx, _parentState, _p));
                    pushNewRecursionContext(_localctx, _startState, RULE_expr);
                    setState(116);
                    if (!(9 >= _localctx._p)) throw new FailedPredicateException(this, "9 >= $_p");
                    setState(117);
                    match(5);
                    setState(118);
                    expr(0);
                    setState(119);
                    match(1);
                  }
                  break;
              }
            }
          }
          setState(125);
          _errHandler.sync(this);
          _alt = getInterpreter().adaptivePredict(_input, 11, _ctx);
        }
      }
    } catch (RecognitionException re) {
      _localctx.exception = re;
      _errHandler.reportError(this, re);
      _errHandler.recover(this, re);
    } finally {
      unrollRecursionContexts(_parentctx);
    }
    return _localctx;
  }
Example #6
0
 protected void setValueBody(ExprContext context, Object target, Object value)
     throws ExprException {
   context.put(name, value);
 }
Example #7
0
 protected Object getValueBody(ExprContext context, Object source) throws ExprException {
   return context.get(name);
 }
  private ExprContext expr(int _p) throws RecognitionException {
    ParserRuleContext _parentctx = _ctx;
    int _parentState = getState();
    ExprContext _localctx = new ExprContext(_ctx, _parentState);
    ExprContext _prevctx = _localctx;
    int _startState = 6;
    enterRecursionRule(_localctx, 6, RULE_expr, _p);
    int _la;
    try {
      int _alt;
      enterOuterAlt(_localctx, 1);
      {
        setState(58);
        switch (_input.LA(1)) {
          case NUMBER:
            {
              _localctx = new NumContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;

              setState(53);
              match(NUMBER);
            }
            break;
          case ID:
            {
              _localctx = new IdContext(_localctx);
              _ctx = _localctx;
              _prevctx = _localctx;
              setState(54);
              match(ID);
              setState(56);
              switch (getInterpreter().adaptivePredict(_input, 4, _ctx)) {
                case 1:
                  {
                    setState(55);
                    formal();
                  }
                  break;
              }
            }
            break;
          default:
            throw new NoViableAltException(this);
        }
        _ctx.stop = _input.LT(-1);
        setState(68);
        _errHandler.sync(this);
        _alt = getInterpreter().adaptivePredict(_input, 7, _ctx);
        while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
          if (_alt == 1) {
            if (_parseListeners != null) triggerExitRuleEvent();
            _prevctx = _localctx;
            {
              setState(66);
              switch (getInterpreter().adaptivePredict(_input, 6, _ctx)) {
                case 1:
                  {
                    _localctx = new MulDivContext(new ExprContext(_parentctx, _parentState));
                    pushNewRecursionContext(_localctx, _startState, RULE_expr);
                    setState(60);
                    if (!(precpred(_ctx, 4)))
                      throw new FailedPredicateException(this, "precpred(_ctx, 4)");
                    setState(61);
                    ((MulDivContext) _localctx).op = _input.LT(1);
                    _la = _input.LA(1);
                    if (!(_la == MUL || _la == DIV)) {
                      ((MulDivContext) _localctx).op = (Token) _errHandler.recoverInline(this);
                    } else {
                      consume();
                    }
                    setState(62);
                    expr(5);
                  }
                  break;
                case 2:
                  {
                    _localctx = new AddSubContext(new ExprContext(_parentctx, _parentState));
                    pushNewRecursionContext(_localctx, _startState, RULE_expr);
                    setState(63);
                    if (!(precpred(_ctx, 3)))
                      throw new FailedPredicateException(this, "precpred(_ctx, 3)");
                    setState(64);
                    ((AddSubContext) _localctx).op = _input.LT(1);
                    _la = _input.LA(1);
                    if (!(_la == ADD || _la == SUB)) {
                      ((AddSubContext) _localctx).op = (Token) _errHandler.recoverInline(this);
                    } else {
                      consume();
                    }
                    setState(65);
                    expr(4);
                  }
                  break;
              }
            }
          }
          setState(70);
          _errHandler.sync(this);
          _alt = getInterpreter().adaptivePredict(_input, 7, _ctx);
        }
      }
    } catch (RecognitionException re) {
      _localctx.exception = re;
      _errHandler.reportError(this, re);
      _errHandler.recover(this, re);
    } finally {
      unrollRecursionContexts(_parentctx);
    }
    return _localctx;
  }