Xtext cross referencing and scoping

2019-05-27 17:35发布

I have some problems with xtext cross referencing Here is a very simple grammer:

grammar org.xtext.example.mydsl1.Test with org.eclipse.xtext.common.Terminals
generate test "http://www.xtext.org/example/mydsl1/Test"
Model: block=Block? cs+=Company* ; 
Block:  '{' g=[Employee] '}';
Company: 'Company' name=ID
    '{' es+= Employee* '}';
Employee: 'Employee' name=ID ';' ;

and it is my dsl :

{ Pooyan }
Company Sony{
    Employee Pooyan;
    Employee John;
}

It always shown that "Couldn't resolve reference to Employee 'Pooyan'." Could anyone please help me? I have no idea...

2条回答
2楼-- · 2019-05-27 18:29

Adding to Sebastians' answer to make it more precise: you need to change "fragment = exporting.QualifiedNamesFragment auto-inject {}" to "fragment = exporting.SimpleNamesFragment" in the corresponding .mwe2 file of your xtext project.

Hope this helps.

查看更多
ら.Afraid
3楼-- · 2019-05-27 18:33

The fully qualified name of Pooyan is Sony.Pooyan. Since the cross reference 'g' in your block is defined in another contain, you have to do a minor customizing to put it onto the scope.

  1. If your language always uses a flat namespace, you could enable the SimpleNamesFragment in the language generator and remove the QualifiedNamesFragment. This should do the trick.
  2. Alternatively, you could customize the scoping for the concrete reference 'g' in your scope provider.
查看更多
登录 后发表回答