xtend代码生成从xtext语法调用实体

时间:2016-12-02 21:45:41

标签: dsl xtext xtend

我正在为我创建的语法编写代码生成器:

Domainmodel:
    (elements+=AbstractElement)*;

PackageDeclaration:
    'package' name=QualifiedName '{'
        (elements+=AbstractElement)*
    '}';

AbstractElement:
    PackageDeclaration | Type | Import;

QualifiedName:
    ID ('-' ID)*;

QualifiedDate:
    INT('-' INT)*
;

Import:
    'import' importedNamespace=QualifiedNameWithWildcard;

QualifiedNameWithWildcard:
    QualifiedName '.*'?;

Type:
     (data+= DataType)* man+=Entity  ;


DataType:
    'tag' name=Tag;

Tag:
    Hobbies='hobbies' | Work= 'work' |Fun='fun'
;

Entity:
    name=Category '{'
       feature+=Feature*
    '}'
    ;
Feature:
    component+=Man(',' component+=Opt)*
;    
enum Category:
    Blog='blog' | Article='articles'
;

Man:
    name='title' '=' type=QualifiedName
 ;

 Opt:
    Tags|Date
 ;

 Tags:
    name='tags' '=' '['type= Tag(','tag+=Tag)*']' 
    |
    name='tags' '=' '[' ']' 
    ;

Date:
    name='date' '=' type=QualifiedDate
    ;

我希望我的代码生成器的输出看起来像这样:

---
layout: post
title: "My Trip"
categories: blog
excerpt:
tags: [fun,hobbies]
image:
feature:
date: 2016-06-01T14:19:19-04:00
modified:
---

所有我能说得对的是静态文本,我似乎无法调用:类别,标题,标签,日期 我已经尝试了这么久但我似乎无法到达任何地方,我一直得到奇怪的错误,我不明白

我试图看到我能够产生的东西之一是:

class MyDslGenerator implements IGenerator2 {

def compile(Entity e) 
{

    '''

    ---
    layout: post
    title: "My Trip"
    categories:«e.name»
    excerpt:
    tags: [fun,hobbies]
    image:
    feature:
    date: 2016-06-01T14:19:19-04:00
    modified:
    ---
    '''
}
override doGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
    for (e : input.allContents.toIterable.filter(Entity)) {
        fsa.generateFile(
            e.generateName,
            e.compile)
    }
}

当我运行生成器时,我没有得到任何被<>取代的内容。我似乎无法弄明白。

1 个答案:

答案 0 :(得分:-1)

/**
 * generated by Xtext 2.10.0
 */
package org.xtext.example.mydsl.tests;

import com.google.inject.Inject;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.junit4.InjectWith;
import org.eclipse.xtext.junit4.XtextRunner;
import org.eclipse.xtext.junit4.util.ParseHelper;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.xtext.example.mydsl.myDsl.Domainmodel;
import org.xtext.example.mydsl.tests.MyDslInjectorProvider;

@RunWith(XtextRunner.class)
@InjectWith(MyDslInjectorProvider.class)
@SuppressWarnings("all")
public class MyDslParsingTest {
  @Inject
  private ParseHelper<Domainmodel> parseHelper;

  @Test
  public void loadModel() {
    try {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("Hello Xtext!");
      _builder.newLine();
      final Domainmodel result = this.parseHelper.parse(_builder);
      Assert.assertNotNull(result);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
}