diverse-project / dev-dashboard

The tracker of this repository is used to track development requests so we can improve internal collaboration by organizing code sprints in the team
0 stars 0 forks source link

Xtext: a case for custom metamodel inference #19

Open FAMILIAR-project opened 5 years ago

FAMILIAR-project commented 5 years ago

Hi,

tldr; some troubles to customize the metamodel inferred by Xtext

As part of a project in M2 SIF, we would like to have a language for SAT formula: https://github.com/FAMILIAR-project/HackOurLanguages-SIF#evaluation https://docs.google.com/document/d/1v1YUDhcCjx42-amBqXqNzrI5LF9HfjXxku7kvG9zMoE/edit?usp=sharing

It's quite closed to an Expression language and there is this nice tutorial: https://typefox.io/parsing-expressions-with-xtext

A possible solution is something like:

Biimplies returns Proposition:
    Implies ({Biimplies.left=current} ('<->' | '↔' /*| '\u2194'*/) right=Implies)*;

Implies returns Proposition:
    ExclusiveDisjunction ({Implies.left=current} ('-->' | '⟶'  /*|'\u27F6'*/) right=ExclusiveDisjunction)*;

ExclusiveDisjunction returns Proposition:
    NegativeDisjunction ({ExclusiveDisjunction.left=current} ('xor' | '⨂' /*| '\u2A02'*/) right=NegativeDisjunction)*;

NegativeDisjunction returns Proposition:
    Disjunction ({NegativeDisjunction.left=current} ('nor' | '↓' /*| '\u2193'*/) right=Disjunction)*;

Disjunction returns Proposition:
    NegativeConjunction ({Disjunction.left=current} ('or' | '∨' /*| '\u2228' */) right=NegativeConjunction)*;

NegativeConjunction returns Proposition:
    Conjunction ({NegativeConjunction.left=current} ('nand' | '↑' /*| '\u2191'*/) right=Conjunction)*;

Conjunction returns Proposition:
    Proposition ({Conjunction.left=current} ('and' | '∧' /*| '\u2227' */) right=Proposition)*;

Proposition returns Proposition:
    '(' Biimplies ')' |
    Negation |
    Primitive;
    // |
//;
Negation returns Negation:
    ('not' | '¬' /*| '\u00AC'*/) content=Proposition;

Primitive returns Primitive:
    name=('true' | '⟙' /*| '\u27D9'*/) |
    name=('false' | '⟘' /*| '\u27D8'*/) |
    name=ID;

What we (students and I) would like to do is to reinforce a bit the structure of the underlying metamodel and have an intermediate concept called Binary that has a left and right-hand side.

something like

Binary: 
    ( NegativeConjunction | NegativeDisjunction | ExclusiveDisjunction | Implies | Disjunction | Biimplies | Conjunction );

does not help since the generated code and underlying metamodel looks like:

public interface Binary extends EObject
{
} // Binary

ie Xtext does not infer that Binary has a left and right-hand side.

A student found a workaround by mapping concept of an existing metamodel (this metamodel contains Binary class): https://gitlab.com/quentinLeDilavrec/sat-solver/blob/tryInXtext/dslWorkspace/sat/src/sat/PropositionalLogicLanguage.xtext

but it's not ideal and a bit ad-hoc. He was brave to follow this nice tutorial: http://www.lorenzobettini.it/2014/02/switching-from-an-inferred-ecore-model-to-an-imported-one-in-your-xtext-grammar/#comment-10819

I am seeking a more elegant and simple solution ;)