Ejemplo n.º 1
0
    @Override
    public SemanticContext evalPrecedence(Recognizer<?, ?> parser, RuleContext parserCallStack) {
      boolean differs = false;
      List<SemanticContext> operands = new ArrayList<SemanticContext>();
      for (SemanticContext context : opnds) {
        SemanticContext evaluated = context.evalPrecedence(parser, parserCallStack);
        differs |= (evaluated != context);
        if (evaluated == NONE) {
          // The OR context is true if any element is true
          return NONE;
        } else if (evaluated != null) {
          // Reduce the result by skipping false elements
          operands.add(evaluated);
        }
      }

      if (!differs) {
        return this;
      }

      if (operands.isEmpty()) {
        // all elements were false, so the OR context is false
        return null;
      }

      SemanticContext result = operands.get(0);
      for (int i = 1; i < operands.size(); i++) {
        result = SemanticContext.or(result, operands.get(i));
      }

      return result;
    }
Ejemplo n.º 2
0
 /**
  * {@inheritDoc}
  *
  * <p>The evaluation of predicates by this context is short-circuiting, but unordered.
  */
 @Override
 public boolean eval(Recognizer<?, ?> parser, RuleContext parserCallStack) {
   for (SemanticContext opnd : opnds) {
     if (opnd.eval(parser, parserCallStack)) return true;
   }
   return false;
 }
Ejemplo n.º 3
0
 /**
  * Given an arbitrarily complex SemanticContext, walk the "tree" and get display string. Pull
  * predicates from grammar text.
  */
 public String getSemanticContextDisplayString(SemanticContext semctx) {
   if (semctx instanceof SemanticContext.Predicate) {
     return getPredicateDisplayString((SemanticContext.Predicate) semctx);
   }
   if (semctx instanceof SemanticContext.AND) {
     SemanticContext.AND and = (SemanticContext.AND) semctx;
     return joinPredicateOperands(and, " and ");
   }
   if (semctx instanceof SemanticContext.OR) {
     SemanticContext.OR or = (SemanticContext.OR) semctx;
     return joinPredicateOperands(or, " or ");
   }
   return semctx.toString();
 }