Search code examples
xtext

Writing Xtext Grammar for method calls like java


Hi I have already written grammar like below for one of my requirement which uses xtext editor.

Feature:
    dclass=[ecore::EClass] (".")? (feature=[ecore::EStructuralFeature])?;

Basically i'm crossreferencing my emf model, so that i can call methods on my Objects, Which supports functionality like below- If I have Employee class with methods getSalary() And Salary as class with method getBasic() Presently below call is supported

Employee.salary

But what i want is since the above code returns Salary i want to call further methods on that

Employee.salary.amount

or Employee.getSalary().getAmount()

How can i achieve that? Any help?


Solution

  • I just changed the grammar as below and then through scope provider i achieved the content assist.

    Feature:
        {Feature} dclass=[ecore::EClass];
    
    DotExpression:
        (Feature) ({DotExpression.ref=current} "."tail=[ecore::EStructuralFeature])*;
    

    Use DotExpression to get the java like behaviour