Example #1
0
 public FlattenedGrammarAccess(final RuleNames names, final RuleFilter filter) {
   final Grammar grammar = names.getContextGrammar();
   Grammar flattenedGrammar = this.<Grammar>copy(grammar);
   String _name = grammar.getName();
   flattenedGrammar.setName(_name);
   LinkedHashMap<RuleWithParameterValues, AbstractRule> origToCopy =
       Maps.<RuleWithParameterValues, AbstractRule>newLinkedHashMap();
   List<AbstractRule> _rules = filter.getRules(grammar);
   boolean _isDiscardRuleTypeRef = filter.isDiscardRuleTypeRef();
   final ArrayList<AbstractRule> copies =
       this.copyRuleStubs(names, origToCopy, _rules, _isDiscardRuleTypeRef);
   EList<AbstractRule> _rules_1 = flattenedGrammar.getRules();
   Iterables.<AbstractRule>addAll(_rules_1, copies);
   Multimap<TerminalRule, AbstractRule> calledFrom = this.copyRuleBodies(copies, origToCopy);
   this.setHiddenTokens(flattenedGrammar, grammar, origToCopy);
   this.markAsFragment(calledFrom);
   boolean _isDiscardUnreachableRules = filter.isDiscardUnreachableRules();
   if (_isDiscardUnreachableRules) {
     Set<AbstractRule> usedRules = CollectionLiterals.<AbstractRule>newHashSet();
     boolean _isDiscardTerminalRules = filter.isDiscardTerminalRules();
     boolean _not = (!_isDiscardTerminalRules);
     if (_not) {
       List<TerminalRule> _allTerminalRules = GrammarUtil.allTerminalRules(flattenedGrammar);
       usedRules.addAll(_allTerminalRules);
     }
     UsedRulesFinder finder = new UsedRulesFinder(usedRules);
     finder.compute(flattenedGrammar);
     EList<AbstractRule> _rules_2 = flattenedGrammar.getRules();
     _rules_2.retainAll(usedRules);
   }
   this.flattenedGrammar = flattenedGrammar;
   OriginalGrammar _originalGrammar = new OriginalGrammar(grammar);
   _originalGrammar.attachToEmfObject(flattenedGrammar);
 }
Example #2
0
 private ArrayList<AbstractRule> copyRuleStubs(
     final RuleNames names,
     final Map<RuleWithParameterValues, AbstractRule> origToCopy,
     final List<AbstractRule> rulesToCopy,
     final boolean discardTypeRef) {
   final ArrayList<AbstractRule> result = CollectionLiterals.<AbstractRule>newArrayList();
   for (final AbstractRule rule : rulesToCopy) {
     {
       String ruleName = names.getAntlrRuleName(rule);
       boolean _matched = false;
       if (rule instanceof ParserRule) {
         _matched = true;
         List<Parameter> params = ((ParserRule) rule).getParameters();
         boolean _isEmpty = params.isEmpty();
         if (_isEmpty) {
           ParserRule copy = this.<ParserRule>copy(((ParserRule) rule));
           copy.setName(ruleName);
           boolean _isFragment = ((ParserRule) rule).isFragment();
           copy.setFragment(_isFragment);
           boolean _isWildcard = ((ParserRule) rule).isWildcard();
           copy.setWildcard(_isWildcard);
           if ((!discardTypeRef)) {
             TypeRef _type = ((ParserRule) rule).getType();
             TypeRef _copyTypeRef = this.copyTypeRef(_type);
             copy.setType(_copyTypeRef);
           }
           this.attachTo(copy, rule, origToCopy);
           result.add(copy);
         } else {
           ImmutableSet<Parameter> _copyOf = ImmutableSet.<Parameter>copyOf(params);
           Set<Set<Parameter>> _powerSet = Sets.<Parameter>powerSet(_copyOf);
           final Procedure2<Set<Parameter>, Integer> _function =
               (Set<Parameter> parameterConfig, Integer i) -> {
                 RuleWithParameterValues parameterValues =
                     new RuleWithParameterValues(rule, parameterConfig);
                 ParserRule copy_1 = this.<ParserRule>copy(((ParserRule) rule));
                 String _antlrRuleName = names.getAntlrRuleName(rule, (i).intValue());
                 copy_1.setName(_antlrRuleName);
                 boolean _isFragment_1 = ((ParserRule) rule).isFragment();
                 copy_1.setFragment(_isFragment_1);
                 boolean _isWildcard_1 = ((ParserRule) rule).isWildcard();
                 copy_1.setWildcard(_isWildcard_1);
                 if ((!discardTypeRef)) {
                   TypeRef _type_1 = ((ParserRule) rule).getType();
                   TypeRef _copyTypeRef_1 = this.copyTypeRef(_type_1);
                   copy_1.setType(_copyTypeRef_1);
                 }
                 origToCopy.put(parameterValues, copy_1);
                 parameterValues.attachToEmfObject(copy_1);
                 result.add(copy_1);
               };
           IterableExtensions.<Set<Parameter>>forEach(_powerSet, _function);
         }
       }
       if (!_matched) {
         if (rule instanceof TerminalRule) {
           _matched = true;
           TerminalRule orig = ((TerminalRule) rule);
           TerminalRule copy = this.<TerminalRule>copy(orig);
           copy.setName(ruleName);
           boolean _isFragment = orig.isFragment();
           copy.setFragment(_isFragment);
           this.attachTo(copy, orig, origToCopy);
           result.add(copy);
         }
       }
       if (!_matched) {
         if (rule instanceof EnumRule) {
           _matched = true;
           EnumRule copy = this.<EnumRule>copy(((EnumRule) rule));
           copy.setName(ruleName);
           this.attachTo(copy, rule, origToCopy);
           result.add(copy);
         }
       }
     }
   }
   return result;
 }