0

I am working on a code generator for my grammar that I have created:

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
    ;

I want my output of my code generator to look like this:

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

All I can get right is the static text, I can't seem to call: Category , title , tags , date I've been trying for so long now but I can't seem to get anywhere, I keep getting strange errors which i don't understand

One of my attempts for just seeing what I can generate is:

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)
    }
}

when I run the generator I don't get anything replaced by <>. I can't seem to figure it out.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Eyad
  • 1
  • 5
  • Welcome to Stack Overflow! I've submitted a suggested edit to apply the code formatting. Blocks of code are formatted by being indented by 4 spaces. There's a toolbar button which will do this for selected text when you're writing the question. – Jack Deeth Dec 02 '16 at 23:12

2 Answers2

1

is this a question on how to walk the AST. your grammar and thus the inferred metamodel is quite "bad" to walk so you may have to do something like

title: «(e.feature.head.component.head as Man).type»

so i recommend you to restructure your grammar/AST to fit the stuff you need.

you can set the encoding for the xtend/xtext plugin like this

tasks.withType(org.xtext.gradle.tasks.XtextGenerate) {
    options.encoding = 'ISO-8859-1'
}

does that help?

Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32
  • Thank you for your reply. Unfortunatily I'm new to all this and only one example was explained, so mainly I'm working on trial and error. If you can recommend recources that I can use that would be helpul. I've tried the xtext and xtend examples but I still don't understand how to solve it. – Eyad Dec 03 '16 at 13:26
  • This is my output when I run the code generator: --- layout: post title: �(e.feature.head.component.head as Man).type� categories:�e.name� excerpt: tags: [fun,hobbies] image: feature: date: 2016-06-01T14:19:19-04:00 modified: --- – Eyad Dec 03 '16 at 13:29
  • make sure you get the encoding right. what is the encoding of your xtend file / and or project (rightklick on project) – Christian Dietrich Dec 03 '16 at 13:33
  • this is the ecoding i found in the mwe2 file : windows-1252 – Eyad Dec 03 '16 at 13:47
  • i mean the one for the project: rightclick, properties, resource – Christian Dietrich Dec 03 '16 at 13:55
  • You should use utf 8 or ISO 8851 1 I think – Christian Dietrich Dec 03 '16 at 14:19
  • unfortunately I'm still having the same issue even after changing the encoding type – Eyad Dec 03 '16 at 14:41
  • anything else other than GitHub! Sorry I haven't used it – Eyad Dec 03 '16 at 17:32
  • just anything that lets me check your complete code – Christian Dietrich Dec 03 '16 at 17:41
  • my complete code is what ive posted, is there anything specifice? – Eyad Dec 03 '16 at 18:20
  • no i mean the genrator file, it it is encoding specific it is important i get the eclipse project with its prefs in the .settings folder and the complete generator file as is – Christian Dietrich Dec 03 '16 at 18:47
  • i have two 'prefs' files in my project setting folder, one is ' buildship ' and one is 'resourses' – Eyad Dec 03 '16 at 19:24
  • 'buildship' code : connection.arguments= connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) connection.java.home=null connection.jvm.arguments= connection.project.dir= derived.resources=.gradle,build eclipse.preferences.version=1 project.path=\: – Eyad Dec 03 '16 at 19:24
  • resource code: eclipse.preferences.version=1 encoding/=windows-1252 – Eyad Dec 03 '16 at 19:25
  • i thought you would have switched to utf-8? – Christian Dietrich Dec 03 '16 at 19:27
  • and how does the generated java file for the xtend file look like. where should be something like _builder.append("categories:"); Category _name = e.getName(); _builder.append(_name, ""); _builder.newLineIfNotEmpty(); – Christian Dietrich Dec 03 '16 at 19:33
  • i just booted windows and cp1252 is fine. thus please post the complete genenerated java file for the generator – Christian Dietrich Dec 03 '16 at 19:41
  • is there a way that i can share the whole project so u can c the code that you r talking about? also, i did shange the encoding in the project to ISO-8859-1 but i dont know why in the setting file it comes up as windows-1252!! – Eyad Dec 03 '16 at 19:52
  • this is what i meant before by uploading to https://github.com/ or https://gitlab.com or zipping it and share it via cloud storage etc - and it might be related to gradle that it is set to cp1252 – Christian Dietrich Dec 03 '16 at 19:54
-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);
    }
  }
}
Eyad
  • 1
  • 5