@Override
 protected String _dataTypeEbnf2(final RuleCall it, final boolean supportActions) {
   String __dataTypeEbnf2 = super._dataTypeEbnf2(it, supportActions);
   boolean _or = false;
   boolean _isPassCurrentIntoFragment = this.isPassCurrentIntoFragment();
   boolean _not = (!_isPassCurrentIntoFragment);
   if (_not) {
     _or = true;
   } else {
     _or = (!supportActions);
   }
   String _argumentList = AntlrGrammarGenUtil.getArgumentList(it, Boolean.valueOf(_or));
   return (__dataTypeEbnf2 + _argumentList);
 }
 @Override
 protected String _dataTypeEbnf2(final Group it, final boolean supportActions) {
   String _xifexpression = null;
   Condition _guardCondition = it.getGuardCondition();
   boolean _tripleEquals = (_guardCondition == null);
   if (_tripleEquals) {
     _xifexpression = super._dataTypeEbnf2(it, supportActions);
   } else {
     String _guardConditionToAntlr = AntlrGrammarGenUtil.guardConditionToAntlr(it);
     String _plus = (_guardConditionToAntlr + "(");
     String __dataTypeEbnf2 = super._dataTypeEbnf2(it, supportActions);
     String _plus_1 = (_plus + __dataTypeEbnf2);
     _xifexpression = (_plus_1 + ")");
   }
   return _xifexpression;
 }
 @Override
 protected String _assignmentEbnf(
     final RuleCall it,
     final Assignment assignment,
     final AntlrOptions options,
     final boolean supportActions) {
   String __assignmentEbnf = super._assignmentEbnf(it, assignment, options, supportActions);
   boolean _or = false;
   boolean _isPassCurrentIntoFragment = this.isPassCurrentIntoFragment();
   boolean _not = (!_isPassCurrentIntoFragment);
   if (_not) {
     _or = true;
   } else {
     _or = (!supportActions);
   }
   String _argumentList = AntlrGrammarGenUtil.getArgumentList(it, Boolean.valueOf(_or));
   return (__assignmentEbnf + _argumentList);
 }
 @Override
 protected String crossrefEbnf(
     final AbstractRule it,
     final RuleCall call,
     final CrossReference ref,
     final boolean supportActions) {
   String _crossrefEbnf = super.crossrefEbnf(it, call, ref, supportActions);
   boolean _or = false;
   boolean _isPassCurrentIntoFragment = this.isPassCurrentIntoFragment();
   boolean _not = (!_isPassCurrentIntoFragment);
   if (_not) {
     _or = true;
   } else {
     _or = (!supportActions);
   }
   String _argumentList = AntlrGrammarGenUtil.getArgumentList(call, Boolean.valueOf(_or));
   return (_crossrefEbnf + _argumentList);
 }
 protected CharSequence _compileInitUnorderedGroups(
     final ParserRule it, final AntlrOptions options) {
   StringConcatenation _builder = new StringConcatenation();
   {
     boolean _definesUnorderedGroups =
         this._grammarAccessExtensions.definesUnorderedGroups(it, options);
     if (_definesUnorderedGroups) {
       _builder.newLineIfNotEmpty();
       _builder.append(
           "UnorderedGroupState myUnorderedGroupState = getUnorderedGroupHelper().snapShot(");
       _builder.newLine();
       {
         List<EObject> _eAllContentsAsList = EcoreUtil2.eAllContentsAsList(it);
         Iterable<UnorderedGroup> _filter =
             Iterables.<UnorderedGroup>filter(_eAllContentsAsList, UnorderedGroup.class);
         boolean _hasElements = false;
         for (final UnorderedGroup group : _filter) {
           if (!_hasElements) {
             _hasElements = true;
           } else {
             _builder.appendImmediate(", ", "");
           }
           _builder.append("grammarAccess.");
           UnorderedGroup _originalElement =
               AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(group);
           String _gaRuleElementAccessor =
               this._grammarAccess.gaRuleElementAccessor(_originalElement);
           _builder.append(_gaRuleElementAccessor, "");
           _builder.newLineIfNotEmpty();
         }
       }
       _builder.append(");");
     }
   }
   return _builder;
 }
예제 #6
0
 public CharSequence toStringLiteral(final AbstractElement it) {
   CharSequence _switchResult = null;
   boolean _matched = false;
   if (!_matched) {
     if (it instanceof RuleCall) {
       AbstractRule _rule = ((RuleCall) it).getRule();
       boolean _notEquals = (!Objects.equal(_rule, null));
       if (_notEquals) {
         _matched = true;
         StringConcatenation _builder = new StringConcatenation();
         _builder.append("\"");
         AbstractRule _rule_1 = ((RuleCall) it).getRule();
         String _name = _rule_1.getName();
         _builder.append(_name, "");
         _builder.append("\"");
         _switchResult = _builder;
       }
     }
   }
   if (!_matched) {
     if (it instanceof Keyword) {
       _matched = true;
       StringConcatenation _builder = new StringConcatenation();
       _builder.append("\"");
       String _value = ((Keyword) it).getValue();
       String _stringInAntlrAction = AntlrGrammarGenUtil.toStringInAntlrAction(_value);
       _builder.append(_stringInAntlrAction, "");
       _builder.append("\"");
       _switchResult = _builder;
     }
   }
   if (!_matched) {
     _switchResult = "null";
   }
   return _switchResult;
 }
 @Override
 protected String _ebnf2(
     final UnorderedGroup it, final AntlrOptions options, final boolean supportActions) {
   String _xifexpression = null;
   if (supportActions) {
     String _xblockexpression = null;
     {
       EList<AbstractElement> _elements = it.getElements();
       final Function1<AbstractElement, Boolean> _function =
           new Function1<AbstractElement, Boolean>() {
             @Override
             public Boolean apply(final AbstractElement it) {
               boolean _isOptionalCardinality = GrammarUtil.isOptionalCardinality(it);
               return Boolean.valueOf((!_isOptionalCardinality));
             }
           };
       Iterable<AbstractElement> _filter =
           IterableExtensions.<AbstractElement>filter(_elements, _function);
       final int mandatoryContent = IterableExtensions.size(_filter);
       StringConcatenation _builder = new StringConcatenation();
       _builder.append("(");
       _builder.newLine();
       _builder.append("\t");
       _builder.append("{ ");
       _builder.newLine();
       _builder.append("\t  ");
       _builder.append("getUnorderedGroupHelper().enter(grammarAccess.");
       UnorderedGroup _originalElement =
           AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it);
       String _gaRuleElementAccessor = this._grammarAccess.gaRuleElementAccessor(_originalElement);
       _builder.append(_gaRuleElementAccessor, "\t  ");
       _builder.append(");");
       _builder.newLineIfNotEmpty();
       _builder.append("\t");
       _builder.append("}");
       _builder.newLine();
       _builder.append("\t");
       _builder.append("(");
       _builder.newLine();
       _builder.append("\t\t");
       _builder.append("(");
       _builder.newLine();
       {
         EList<AbstractElement> _elements_1 = it.getElements();
         Iterable<Pair<Integer, AbstractElement>> _indexed =
             IterableExtensions.<AbstractElement>indexed(_elements_1);
         boolean _hasElements = false;
         for (final Pair<Integer, AbstractElement> element : _indexed) {
           if (!_hasElements) {
             _hasElements = true;
           } else {
             _builder.appendImmediate("|", "");
           }
           _builder.append("(");
           _builder.newLine();
           _builder.append("\t");
           _builder.append("{getUnorderedGroupHelper().canSelect(grammarAccess.");
           UnorderedGroup _originalElement_1 =
               AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it);
           String _gaRuleElementAccessor_1 =
               this._grammarAccess.gaRuleElementAccessor(_originalElement_1);
           _builder.append(_gaRuleElementAccessor_1, "\t");
           _builder.append(", ");
           Integer _key = element.getKey();
           _builder.append(_key, "\t");
           _builder.append(")}?=>(");
           _builder.newLineIfNotEmpty();
           _builder.append("\t\t");
           _builder.append("{");
           _builder.newLine();
           _builder.append("\t\t\t");
           _builder.append("getUnorderedGroupHelper().select(grammarAccess.");
           UnorderedGroup _originalElement_2 =
               AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it);
           String _gaRuleElementAccessor_2 =
               this._grammarAccess.gaRuleElementAccessor(_originalElement_2);
           _builder.append(_gaRuleElementAccessor_2, "\t\t\t");
           _builder.append(", ");
           Integer _key_1 = element.getKey();
           _builder.append(_key_1, "\t\t\t");
           _builder.append(");");
           _builder.newLineIfNotEmpty();
           _builder.append("\t\t");
           _builder.append("}");
           _builder.newLine();
           _builder.append("\t\t\t\t\t");
           _builder.append("({true}?=>(");
           AbstractElement _value = element.getValue();
           String _ebnf2 = this.ebnf2(_value, options, supportActions);
           _builder.append(_ebnf2, "\t\t\t\t\t");
           _builder.append("))");
           {
             AbstractElement _value_1 = element.getValue();
             boolean _isMultipleCardinality = GrammarUtil.isMultipleCardinality(_value_1);
             if (_isMultipleCardinality) {
               _builder.append("+");
             }
           }
           _builder.newLineIfNotEmpty();
           _builder.append("\t\t");
           _builder.append("{ ");
           _builder.newLine();
           _builder.append("\t\t\t");
           _builder.append("getUnorderedGroupHelper().returnFromSelection(grammarAccess.");
           UnorderedGroup _originalElement_3 =
               AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it);
           String _gaRuleElementAccessor_3 =
               this._grammarAccess.gaRuleElementAccessor(_originalElement_3);
           _builder.append(_gaRuleElementAccessor_3, "\t\t\t");
           _builder.append(");");
           _builder.newLineIfNotEmpty();
           _builder.append("\t\t");
           _builder.append("}");
           _builder.newLine();
           _builder.append("\t");
           _builder.append(")");
           _builder.newLine();
           _builder.append(")");
           _builder.newLine();
         }
       }
       _builder.append("\t\t");
       _builder.append(")");
       {
         if ((mandatoryContent != 0)) {
           _builder.append("+");
           _builder.newLineIfNotEmpty();
           _builder.append("\t\t");
           _builder.append("{getUnorderedGroupHelper().canLeave(grammarAccess.");
           UnorderedGroup _originalElement_4 =
               AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it);
           String _gaRuleElementAccessor_4 =
               this._grammarAccess.gaRuleElementAccessor(_originalElement_4);
           _builder.append(_gaRuleElementAccessor_4, "\t\t");
           _builder.append(")}?");
         } else {
           _builder.append("*");
         }
       }
       _builder.newLineIfNotEmpty();
       _builder.append("\t");
       _builder.append(")");
       _builder.newLine();
       _builder.append(")");
       _builder.newLine();
       _builder.append("\t");
       _builder.append("{ ");
       _builder.newLine();
       _builder.append("\t  ");
       _builder.append("getUnorderedGroupHelper().leave(grammarAccess.");
       UnorderedGroup _originalElement_5 =
           AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it);
       String _gaRuleElementAccessor_5 =
           this._grammarAccess.gaRuleElementAccessor(_originalElement_5);
       _builder.append(_gaRuleElementAccessor_5, "\t  ");
       _builder.append(");");
       _builder.newLineIfNotEmpty();
       _builder.append("\t");
       _builder.append("}");
       _builder.newLine();
       _xblockexpression = _builder.toString();
     }
     _xifexpression = _xblockexpression;
   } else {
     _xifexpression = super._ebnf2(it, options, supportActions);
   }
   return _xifexpression;
 }
  @Override
  public void generate(final Grammar grammar, final XpandExecutionContext ctx) {
    KeywordHelper helper = new KeywordHelper(grammar, getOptions().isIgnoreCase());
    super.generate(grammar, ctx);
    final String srcGenPath = ctx.getOutput().getOutlet(Generator.SRC_GEN).getPath();
    final String encoding = getEncoding(ctx, Generator.SRC_GEN);
    final String lexerBaseFileName =
        srcGenPath + "/" + getFragmentHelper().getLexerGrammarFileName(grammar).replace('.', '/');
    String libPath = lexerBaseFileName;
    libPath = libPath.substring(0, libPath.lastIndexOf('/'));
    String absoluteLexerFileName = lexerBaseFileName + ".g";
    String absoluteParserFileName =
        srcGenPath
            + "/"
            + getFragmentHelper().getParserGrammarFileName(grammar).replace('.', '/')
            + ".g";
    addAntlrParam("-fo");
    addAntlrParam(
        absoluteParserFileName
            .substring(0, absoluteParserFileName.lastIndexOf('/'))
            .replace("//", "/"));
    String[] lexerAntlrParams = getAntlrParams();
    lexerAntlrParams[lexerAntlrParams.length - 1] =
        absoluteLexerFileName
            .substring(0, absoluteLexerFileName.lastIndexOf('/'))
            .replace("//", "/");

    // copy
    copy(new File(absoluteLexerFileName), new File(lexerBaseFileName + ".gxtext"));
    writeFile(absoluteLexerFileName, readFile(absoluteLexerFileName.replace("src-gen", "java")));

    getAntlrTool()
        .runWithEncodingAndParams(
            absoluteLexerFileName.replace("//", "/"), encoding, lexerAntlrParams);
    cleanupLexerTokensFile(lexerBaseFileName);
    addAntlrParam("-lib");
    addAntlrParam(libPath.replace("//", "/"));

    getAntlrTool()
        .runWithEncodingAndParams(
            absoluteParserFileName.replace("//", "/"), encoding, getAntlrParams());
    simplifyUnorderedGroupPredicatesIfRequired(grammar, absoluteParserFileName);
    splitParserAndLexerIfEnabled(absoluteLexerFileName, absoluteParserFileName);
    suppressWarnings(absoluteLexerFileName, absoluteParserFileName);

    MutableTokenDefProvider provider = createLexerTokensProvider(lexerBaseFileName);
    for (Map.Entry<Integer, String> entry : provider.getTokenDefMap().entrySet()) {
      String value = entry.getValue();
      if (helper.isKeywordRule(value)) {
        String keywordAsAntlrString =
            AntlrGrammarGenUtil.toAntlrString(helper.getKeywordValue(value));
        entry.setValue("'" + keywordAsAntlrString + "'");
      } else if (value.startsWith("'")) {
        value = AntlrGrammarGenUtil.toAntlrString(value);
        entry.setValue("'" + value + "'");
      }
    }
    try {
      provider.writeTokenFile(
          new PrintWriter(
              new File(
                  srcGenPath
                      + "/"
                      + getFragmentHelper().getParserGrammarFileName(grammar).replace('.', '/')
                      + ".tokens")));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    helper.discardHelper(grammar);
  }