public String defaultValue(final AbstractElement ele, final Set<AbstractElement> visited) {
   String _switchResult = null;
   boolean _matched = false;
   if (!_matched) {
     boolean _add = visited.add(ele);
     boolean _not = (!_add);
     if (_not) {
       _matched = true;
       _switchResult = "";
     }
   }
   if (!_matched) {
     boolean _isOptionalCardinality = GrammarUtil.isOptionalCardinality(ele);
     if (_isOptionalCardinality) {
       _matched = true;
       _switchResult = "";
     }
   }
   if (!_matched) {
     if (ele instanceof Alternatives) {
       _matched = true;
       EList<AbstractElement> _elements = ((Alternatives) ele).getElements();
       AbstractElement _head = IterableExtensions.<AbstractElement>head(_elements);
       _switchResult = this.defaultValue(_head, visited);
     }
   }
   if (!_matched) {
     if (ele instanceof Group) {
       _matched = true;
       EList<AbstractElement> _elements = ((Group) ele).getElements();
       final Function1<AbstractElement, String> _function =
           new Function1<AbstractElement, String>() {
             @Override
             public String apply(final AbstractElement e) {
               return AbstractSyntacticSequencer.this.defaultValue(e, visited);
             }
           };
       List<String> _map = ListExtensions.<AbstractElement, String>map(_elements, _function);
       _switchResult = IterableExtensions.join(_map);
     }
   }
   if (!_matched) {
     if (ele instanceof Keyword) {
       _matched = true;
       _switchResult = ((Keyword) ele).getValue();
     }
   }
   if (!_matched) {
     if (ele instanceof RuleCall) {
       _matched = true;
       AbstractRule _rule = ((RuleCall) ele).getRule();
       AbstractElement _alternatives = _rule.getAlternatives();
       _switchResult = this.defaultValue(_alternatives, visited);
     }
   }
   if (!_matched) {
     _switchResult = "";
   }
   return _switchResult;
 }
Exemplo n.º 2
0
 protected AbstractElement _predicatedElement(final RuleCall it) {
   AbstractElement _xifexpression = null;
   boolean _isPredicated = it.isPredicated();
   if (_isPredicated) {
     _xifexpression = it;
   } else {
     AbstractRule _rule = it.getRule();
     AbstractElement _alternatives = _rule.getAlternatives();
     EList<AbstractElement> _elements = ((Group) _alternatives).getElements();
     AbstractElement _head = IterableExtensions.<AbstractElement>head(_elements);
     _xifexpression = this.predicatedElement(_head);
   }
   return _xifexpression;
 }
Exemplo n.º 3
0
 public void assertPredicate(final String expectation, final String grammar) {
   try {
     EObject _model = this.getModel(grammar);
     final Grammar parsed = ((Grammar) _model);
     EList<AbstractRule> _rules = parsed.getRules();
     AbstractRule _head = IterableExtensions.<AbstractRule>head(_rules);
     final AbstractElement body = _head.getAlternatives();
     final AbstractElement predicate = AntlrGrammarGenUtil.getPredicatedElement(body);
     String _xtext = this.toXtext(predicate);
     Assert.assertEquals(expectation, _xtext);
   } catch (Throwable _e) {
     throw Exceptions.sneakyThrow(_e);
   }
 }
Exemplo n.º 4
0
  public static AbstractElement getPredicatedElement(AbstractElement element) {
    if (element.isPredicated()) {
      return element;
    }
    if (element instanceof Assignment) {
      return getPredicatedElement(((Assignment) element).getTerminal());
    }
    if (element instanceof RuleCall) {
      final RuleCall ruleCall = (RuleCall) element;
      final AbstractRule calledRule = ruleCall.getRule();
      Group group = (Group) calledRule.getAlternatives();
      AbstractElement first = group.getElements().get(0);
      AbstractElement result = getPredicatedElement(first);
      if (ruleCall.getArguments().isEmpty()) {
        return result;
      }
      EcoreUtil.Copier copier =
          new EcoreUtil.Copier() {
            private static final long serialVersionUID = 1L;

            @Override
            public EObject copy(EObject eObject) {
              if (eObject instanceof ParameterReference) {
                ParameterReference paramRef = (ParameterReference) eObject;
                Parameter parameter = paramRef.getParameter();
                if (calledRule == GrammarUtil.containingRule(parameter)) {
                  for (NamedArgument givenArgument : ruleCall.getArguments()) {
                    if (givenArgument.getParameter() == parameter) {
                      EObject result = copy(givenArgument.getValue());
                      return result;
                    }
                  }
                  throw new IllegalStateException();
                }
              }
              return super.copy(eObject);
            }
          };
      AbstractElement clone = (AbstractElement) copier.copy(result);
      copier.copyReferences();
      return clone;
    }
    return element;
  }
Exemplo n.º 5
0
 protected boolean _predicated(final RuleCall it) {
   boolean _or = false;
   boolean _isPredicated = it.isPredicated();
   if (_isPredicated) {
     _or = true;
   } else {
     boolean _xblockexpression = false;
     {
       AbstractRule _rule = it.getRule();
       final AbstractElement group = _rule.getAlternatives();
       boolean _xifexpression = false;
       if ((group instanceof Group)) {
         EList<AbstractElement> _elements = ((Group) group).getElements();
         AbstractElement _head = IterableExtensions.<AbstractElement>head(_elements);
         _xifexpression = this.predicated(_head);
       } else {
         _xifexpression = false;
       }
       _xblockexpression = _xifexpression;
     }
     _or = _xblockexpression;
   }
   return _or;
 }
Exemplo n.º 6
0
  private Multimap<TerminalRule, AbstractRule> copyRuleBodies(
      final List<AbstractRule> copies,
      final Map<RuleWithParameterValues, AbstractRule> origToCopy) {
    abstract class __FlattenedGrammarAccess_1 extends EcoreUtil.Copier {
      final __FlattenedGrammarAccess_1 _this__FlattenedGrammarAccess_1 = this;

      abstract Set<Parameter> getParameterConfig(
          final RuleCall origRuleCall, final RuleCall copyRuleCall);

      abstract void mergePredicates(final AbstractElement into, final AbstractElement from);

      abstract void mergeCardinalities(final AbstractElement into, final AbstractElement from);

      abstract boolean evaluate(final Condition condition);
    }

    final HashMultimap<TerminalRule, AbstractRule> calledFrom =
        HashMultimap.<TerminalRule, AbstractRule>create();
    for (final AbstractRule copy : copies) {
      {
        AbstractRule orig = RuleWithParameterValues.getOriginalRule(copy);
        final Set<Parameter> paramValues = RuleWithParameterValues.getParamValues(copy);
        EcoreUtil.Copier copier =
            new __FlattenedGrammarAccess_1() {
              @Override
              protected void copyReference(
                  final EReference eReference, final EObject eObject, final EObject copyEObject) {
                if ((eReference == XtextPackage.Literals.RULE_CALL__RULE)) {
                  RuleCall origRuleCall = ((RuleCall) eObject);
                  RuleCall copyRuleCall = ((RuleCall) copyEObject);
                  AbstractRule _rule = origRuleCall.getRule();
                  Set<Parameter> _parameterConfig =
                      this.getParameterConfig(origRuleCall, copyRuleCall);
                  RuleWithParameterValues _ruleWithParameterValues =
                      new RuleWithParameterValues(_rule, _parameterConfig);
                  AbstractRule calledCopy = origToCopy.get(_ruleWithParameterValues);
                  copyRuleCall.setRule(calledCopy);
                  if ((calledCopy instanceof TerminalRule)) {
                    calledFrom.put(((TerminalRule) calledCopy), copy);
                  }
                } else {
                  super.copyReference(eReference, eObject, copyEObject);
                }
              }

              Set<Parameter> getParameterConfig(
                  final RuleCall origRuleCall, final RuleCall copyRuleCall) {
                EList<NamedArgument> _arguments = origRuleCall.getArguments();
                boolean _isEmpty = _arguments.isEmpty();
                if (_isEmpty) {
                  return Collections.<Parameter>emptySet();
                }
                EList<NamedArgument> _arguments_1 = origRuleCall.getArguments();
                final Function1<NamedArgument, Boolean> _function =
                    (NamedArgument it) -> {
                      Condition _value = it.getValue();
                      return Boolean.valueOf(this.evaluate(_value));
                    };
                Iterable<NamedArgument> _filter =
                    IterableExtensions.<NamedArgument>filter(_arguments_1, _function);
                final Function1<NamedArgument, Parameter> _function_1 =
                    (NamedArgument it) -> {
                      return it.getParameter();
                    };
                Iterable<Parameter> _map =
                    IterableExtensions.<NamedArgument, Parameter>map(_filter, _function_1);
                Set<Parameter> result = IterableExtensions.<Parameter>toSet(_map);
                return result;
              }

              @Override
              protected void copyContainment(
                  final EReference eReference, final EObject eObject, final EObject copyEObject) {
                boolean _matched = false;
                if (Objects.equal(eReference, XtextPackage.Literals.RULE_CALL__ARGUMENTS)) {
                  _matched = true;
                }
                if (!_matched) {
                  if (Objects.equal(eReference, XtextPackage.Literals.GROUP__GUARD_CONDITION)) {
                    _matched = true;
                  }
                }
                if (_matched) {
                  return;
                }
                super.copyContainment(eReference, eObject, copyEObject);
              }

              @Override
              public EObject copy(final EObject eObject) {
                if ((eObject instanceof Group)) {
                  Group group = ((Group) eObject);
                  Condition _guardCondition = group.getGuardCondition();
                  boolean _tripleNotEquals = (_guardCondition != null);
                  if (_tripleNotEquals) {
                    Condition _guardCondition_1 = group.getGuardCondition();
                    boolean _evaluate = this.evaluate(_guardCondition_1);
                    boolean _not = (!_evaluate);
                    if (_not) {
                      return null;
                    }
                  }
                }
                EObject result = super.copy(eObject);
                if ((result instanceof CompoundElement)) {
                  List<AbstractElement> elements = ((CompoundElement) result).getElements();
                  int _size = elements.size();
                  boolean _tripleEquals = (_size == 1);
                  if (_tripleEquals) {
                    if (((!((CompoundElement) result).isFirstSetPredicated())
                        && (!((CompoundElement) result).isPredicated()))) {
                      AbstractElement element = elements.get(0);
                      this.mergeCardinalities(element, ((AbstractElement) result));
                      this.mergePredicates(element, ((AbstractElement) result));
                      return element;
                    } else {
                      AbstractElement element_1 = elements.get(0);
                      this.mergePredicates(((AbstractElement) result), element_1);
                      element_1.setFirstSetPredicated(false);
                      element_1.setPredicated(false);
                    }
                  }
                }
                if ((eObject instanceof AbstractElement)) {
                  OriginalElement original = new OriginalElement(((AbstractElement) eObject));
                  EClass _eClass = ((AbstractElement) eObject).eClass();
                  EClass _eClass_1 = result.eClass();
                  boolean _notEquals = (!Objects.equal(_eClass, _eClass_1));
                  if (_notEquals) {
                    EClass _eClass_2 = result.eClass();
                    String _name = _eClass_2.getName();
                    String _plus = ("copy is: \'" + _name);
                    String _plus_1 = (_plus + "\' but original was: \'");
                    EClass _eClass_3 = ((AbstractElement) eObject).eClass();
                    String _name_1 = _eClass_3.getName();
                    String _plus_2 = (_plus_1 + _name_1);
                    String _plus_3 = (_plus_2 + "\'");
                    throw new IllegalStateException(_plus_3);
                  }
                  original.attachToEmfObject(result);
                }
                return result;
              }

              void mergePredicates(final AbstractElement into, final AbstractElement from) {
                boolean _isPredicated = from.isPredicated();
                if (_isPredicated) {
                  into.setPredicated(true);
                  into.setFirstSetPredicated(false);
                } else {
                  if (((!into.isPredicated()) && from.isFirstSetPredicated())) {
                    into.setFirstSetPredicated(true);
                  }
                }
              }

              void mergeCardinalities(final AbstractElement into, final AbstractElement from) {
                String c1 = into.getCardinality();
                String c2 = from.getCardinality();
                String _switchResult = null;
                boolean _matched = false;
                if ((Objects.equal(c1, "*") || Objects.equal(c2, "*"))) {
                  _matched = true;
                }
                if (!_matched) {
                  if ((Objects.equal(c1, "+") && Objects.equal(c2, "?"))) {
                    _matched = true;
                  }
                }
                if (!_matched) {
                  if ((Objects.equal(c1, "?") && Objects.equal(c2, "+"))) {
                    _matched = true;
                  }
                }
                if (_matched) {
                  _switchResult = "*";
                }
                if (!_matched) {
                  if (Objects.equal(c1, null)) {
                    _matched = true;
                    _switchResult = c2;
                  }
                }
                if (!_matched) {
                  _switchResult = c1;
                }
                into.setCardinality(_switchResult);
              }

              boolean evaluate(final Condition condition) {
                ConditionEvaluator _conditionEvaluator = new ConditionEvaluator(paramValues);
                boolean result = _conditionEvaluator.evaluate(condition);
                return result;
              }
            };
        AbstractElement _alternatives = orig.getAlternatives();
        EObject _copy = copier.copy(_alternatives);
        AbstractElement copiedBody = ((AbstractElement) _copy);
        copier.copyReferences();
        copy.setAlternatives(copiedBody);
        if ((orig instanceof ParserRule)) {
          ParserRule castedCopy = ((ParserRule) copy);
          boolean _isDefinesHiddenTokens = ((ParserRule) orig).isDefinesHiddenTokens();
          if (_isDefinesHiddenTokens) {
            castedCopy.setDefinesHiddenTokens(true);
            EList<AbstractRule> _hiddenTokens = ((ParserRule) orig).getHiddenTokens();
            for (final AbstractRule rule : _hiddenTokens) {
              {
                RuleWithParameterValues _ruleWithParameterValues =
                    new RuleWithParameterValues(rule);
                final AbstractRule copiedTerminalRule = origToCopy.get(_ruleWithParameterValues);
                EList<AbstractRule> _hiddenTokens_1 = castedCopy.getHiddenTokens();
                _hiddenTokens_1.add(copiedTerminalRule);
                calledFrom.put(((TerminalRule) copiedTerminalRule), castedCopy);
              }
            }
          }
        }
      }
    }
    return calledFrom;
  }
 public String genGetUnassignedRuleCallToken(
     final JavaFile file, final AbstractRule rule, final boolean isAbstract) {
   if ((rule instanceof TerminalRule)) {
     boolean _and = false;
     if (!this.detectSyntheticTerminals) {
       _and = false;
     } else {
       boolean _isSyntheticTerminalRule =
           this.syntheticTerminalDetector.isSyntheticTerminalRule(((TerminalRule) rule));
       _and = _isSyntheticTerminalRule;
     }
     if (_and) {
       StringConcatenation _builder = new StringConcatenation();
       _builder.append("/**");
       _builder.newLine();
       _builder.append(" ");
       _builder.append(
           "* Synthetic terminal rule. The concrete syntax is to be specified by clients.");
       _builder.newLine();
       {
         if ((!isAbstract)) {
           _builder.append(" * Defaults to the empty string.");
         }
       }
       _builder.newLineIfNotEmpty();
       _builder.append(" ");
       _builder.append("*/");
       _builder.newLine();
       _builder.append("protected ");
       {
         if (isAbstract) {
           _builder.append("abstract ");
         }
       }
       _builder.append("String ");
       CharSequence _unassignedCalledTokenRuleName = this.unassignedCalledTokenRuleName(rule);
       _builder.append(_unassignedCalledTokenRuleName, "");
       _builder.append("(EObject semanticObject, RuleCall ruleCall, INode node)");
       {
         if (isAbstract) {
           _builder.append(";");
         } else {
           _builder.append(" { return \"\"; }");
         }
       }
       _builder.newLineIfNotEmpty();
       return _builder.toString();
     }
   }
   StringConcatenation _builder_1 = new StringConcatenation();
   _builder_1.append("/**");
   _builder_1.newLine();
   _builder_1.append(" ");
   _builder_1.append("* ");
   ICompositeNode _node = NodeModelUtils.getNode(rule);
   String _textWithoutComments = this.textWithoutComments(_node);
   String _trim = _textWithoutComments.trim();
   String _replace = _trim.replace("\n", "\n* ");
   _builder_1.append(_replace, " ");
   _builder_1.newLineIfNotEmpty();
   _builder_1.append(" ");
   _builder_1.append("*/");
   _builder_1.newLine();
   _builder_1.append("protected String ");
   CharSequence _unassignedCalledTokenRuleName_1 = this.unassignedCalledTokenRuleName(rule);
   _builder_1.append(_unassignedCalledTokenRuleName_1, "");
   _builder_1.append("(EObject semanticObject, RuleCall ruleCall, INode node) {");
   _builder_1.newLineIfNotEmpty();
   _builder_1.append("\t");
   _builder_1.append("if (node != null)");
   _builder_1.newLine();
   _builder_1.append("\t\t");
   _builder_1.append("return getTokenText(node);");
   _builder_1.newLine();
   _builder_1.append("\t");
   _builder_1.append("return \"");
   AbstractElement _alternatives = rule.getAlternatives();
   HashSet<AbstractElement> _newHashSet = CollectionLiterals.<AbstractElement>newHashSet();
   String _defaultValue = this.defaultValue(_alternatives, _newHashSet);
   String _convertToJavaString = Strings.convertToJavaString(_defaultValue);
   _builder_1.append(_convertToJavaString, "\t");
   _builder_1.append("\";");
   _builder_1.newLineIfNotEmpty();
   _builder_1.append("}");
   _builder_1.newLine();
   return _builder_1.toString();
 }