Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » XTEXT Scoping Issue
XTEXT Scoping Issue [message #1853496] Tue, 05 July 2022 13:49 Go to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
Example of Grammar that doesnt Work:

grammar org.xtext.EcuCValues_Extend with org.eclipse.xtext.common.Terminals

generate ecuCValues_Extend "http://www.xtext.org/EcuCValues_Extend"

Model:
(lines+=Line)*
;

Line:
(rule = Rule)
;

Rule:
("ForEach" anchor=AnchorValue);

AnchorValue:
(ARClass = TAG_NO_DOT ".class") | (arpath=AbsoluteARPathContainer);

AbsoluteARPathContainer:
"/" path = ARPathContainer_With_Relative
;
ARPathContainer_With_Relative:
path+=(ARPathElement)+;

ARPathElement:
//(move=TAG_NO_DOT_RULE | move_rel=RELATIVE_MOVEMENT) "/"
(move=[TAG_NO_DOT_FUNC] | move_rel=RELATIVE_MOVEMENT) "/"
;
TAG_NO_DOT_FUNC:
name = TAG_NO_DOT
;
QualifiedName:
TAG_NO_DOT
;
enum RELATIVE_MOVEMENT:
ONEBACK=".." | HERE=".";

terminal TAG_NO_DOT:
('a'..'z' | 'A'..'Z' | '_' | '0'..'9')+
;


Example of Grammar that does work:

grammar org.xtext.EcuCValues_Extend with org.eclipse.xtext.common.Terminals

generate ecuCValues_Extend "http://www.xtext.org/EcuCValues_Extend"

Model:
(lines+=Line)*
;

Line:
(rule = Rule)
;

Rule:
("ForEach" anchor=AnchorValue);

AnchorValue:
(ARClass = TAG_NO_DOT ".class") | (arpath=AbsoluteARPathContainer);

AbsoluteARPathContainer:
"/" path = ARPathContainer_With_Relative
;
ARPathContainer_With_Relative:
path+=(ARPathElement)+;

ARPathElement:
//(move=TAG_NO_DOT_RULE | move_rel=RELATIVE_MOVEMENT) "/"
(move=TAG_NO_DOT_FUNC | move_rel=RELATIVE_MOVEMENT) "/"
;
TAG_NO_DOT_FUNC:
name = TAG_NO_DOT
;
QualifiedName:
TAG_NO_DOT
;
enum RELATIVE_MOVEMENT:
ONEBACK=".." | HERE=".";

terminal TAG_NO_DOT:
('a'..'z' | 'A'..'Z' | '_' | '0'..'9')+
;

Line of Syntax I want it to parse:

ForEach /Com/ComConfig/ComSignalGroup/

for the XTEXT that doesnt work I get:

required (...)+ loop did not match anything at input

The only difference between the two XTEXT files is the introduction of Scoping. Why does the Scoping break my Grammar? I dont understand and its hard to google for the solution.

[Updated on: Wed, 06 July 2022 09:00]

Report message to a moderator

Re: XTEXT Scoping Issue [message #1853502 is a reply to message #1853496] Tue, 05 July 2022 19:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,
can you please provide a complete testcase


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XTEXT Scoping Issue [message #1853515 is a reply to message #1853502] Wed, 06 July 2022 08:57 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
Example of Grammar that doesnt Work:

grammar org.xtext.EcuCValues_Extend with org.eclipse.xtext.common.Terminals

generate ecuCValues_Extend "http://www.xtext.org/EcuCValues_Extend"

Model:
(lines+=Line)*
;

Line:
(rule = Rule)
;

Rule:
("ForEach" anchor=AnchorValue);

AnchorValue:
(ARClass = TAG_NO_DOT ".class") | (arpath=AbsoluteARPathContainer);

AbsoluteARPathContainer:
"/" path = ARPathContainer_With_Relative
;
ARPathContainer_With_Relative:
path+=(ARPathElement)+;

ARPathElement:
//(move=TAG_NO_DOT_RULE | move_rel=RELATIVE_MOVEMENT) "/"
(move=[TAG_NO_DOT_FUNC] | move_rel=RELATIVE_MOVEMENT) "/"
;
TAG_NO_DOT_FUNC:
name = TAG_NO_DOT
;
QualifiedName:
TAG_NO_DOT
;
enum RELATIVE_MOVEMENT:
ONEBACK=".." | HERE=".";

terminal TAG_NO_DOT:
('a'..'z' | 'A'..'Z' | '_' | '0'..'9')+
;


Example of Grammar that does work:

grammar org.xtext.EcuCValues_Extend with org.eclipse.xtext.common.Terminals

generate ecuCValues_Extend "http://www.xtext.org/EcuCValues_Extend"

Model:
(lines+=Line)*
;

Line:
(rule = Rule)
;

Rule:
("ForEach" anchor=AnchorValue);

AnchorValue:
(ARClass = TAG_NO_DOT ".class") | (arpath=AbsoluteARPathContainer);

AbsoluteARPathContainer:
"/" path = ARPathContainer_With_Relative
;
ARPathContainer_With_Relative:
path+=(ARPathElement)+;

ARPathElement:
//(move=TAG_NO_DOT_RULE | move_rel=RELATIVE_MOVEMENT) "/"
(move=TAG_NO_DOT_FUNC | move_rel=RELATIVE_MOVEMENT) "/"
;
TAG_NO_DOT_FUNC:
name = TAG_NO_DOT
;
QualifiedName:
TAG_NO_DOT
;
enum RELATIVE_MOVEMENT:
ONEBACK=".." | HERE=".";

terminal TAG_NO_DOT:
('a'..'z' | 'A'..'Z' | '_' | '0'..'9')+
;

Line of Syntax I want it to parse:

ForEach /Com/ComConfig/ComSignalGroup/

The only difference between the two XTEXT files is the introduction of Scoping. Why does the Scoping break my Grammar? I dont understand and its hard to google for the solution.
Re: XTEXT Scoping Issue [message #1853516 is a reply to message #1853515] Wed, 06 July 2022 09:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
move=[TAG_NO_DOT_FUNC]

is short for

move=[TAG_NO_DOT_FUNC|ID]

which means: parse an ID to reference a TAG_NO_DOT_FUNC

so you may actually want move=[TAG_NO_DOT_FUNC|TAG_NO_DOT]


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XTEXT Scoping Issue [message #1853517 is a reply to message #1853516] Wed, 06 July 2022 10:50 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
Hello Christian,

first I want to thank you for your quick answer.

I have changed my grammar to:

grammar org.xtext.EcuCValues_Extend with org.eclipse.xtext.common.Terminals

generate ecuCValues_Extend "http://www.xtext.org/EcuCValues_Extend"

Model:
(lines+=Line)*
;

Line:
(rule = Rule)
;

Rule:
("ForEach" anchor=AnchorValue);

AnchorValue:
(ARClass = TAG_NO_DOT ".class") | (arpath=AbsoluteARPathContainer);

AbsoluteARPathContainer:
"/" path = ARPathContainer_With_Relative
;
ARPathContainer_With_Relative:
path+=(ARPathElement)+;

ARPathElement:
//(move=TAG_NO_DOT_RULE | move_rel=RELATIVE_MOVEMENT) "/"
(move=[TAG_NO_DOT_FUNC|QualifiedName] | move_rel=RELATIVE_MOVEMENT) "/"
;
TAG_NO_DOT_FUNC:
name = TAG_NO_DOT
;
QualifiedName:
TAG_NO_DOT
;
enum RELATIVE_MOVEMENT:
ONEBACK=".." | HERE=".";

terminal TAG_NO_DOT:
('a'..'z' | 'A'..'Z' | '_' | '0'..'9')+
;

Now I get the error in my editor:

Couldn't resolve reference to TAG_NO_DOT_FUNC 'Com'.

Why can he not resolve the reference to TAG_NO_DOT_FUNC?
Re: XTEXT Scoping Issue [message #1853518 is a reply to message #1853517] Wed, 06 July 2022 12:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
depends on how you implemented scoping. by default xtext can only resolved things that have a name. and that are declared somehwere else.
i propose to debug DefaultLinkingService class

i your grammar i dont see where you declare the TAG_NO_DOT_FUNC
i just s the reference but no declaration

TAG_NO_DOT_FUNC is uncalled


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 06 July 2022 12:10]

Report message to a moderator

Re: XTEXT Scoping Issue [message #1853520 is a reply to message #1853518] Wed, 06 July 2022 12:50 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
Thank you for your useful input Christian. You have found the spot where my Understanding of XTEXT is lacking.

I thought the declaration of TAG_NO_DOT_FUNC is:

TAG_NO_DOT_FUNC:
name = TAG_NO_DOT
;


apparently for you this is just a reference.

> xtext can only resolved things that have a name.

As you can see TAG_NO_DOT_FUNC does have an element called name. So why can it not be resolved by default?

> TAG_NO_DOT_FUNC is uncalled

What does uncalled mean? How can I call it?

[Updated on: Wed, 06 July 2022 13:20]

Report message to a moderator

Re: XTEXT Scoping Issue [message #1853524 is a reply to message #1853520] Wed, 06 July 2022 13:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
image this grammar:

Model:
definitions+=Definition* usages+=Usage*;
Definition: "define" name=ID;
Usage: "use" def=[Usage];

and example model:

define A
define B
use A
use B

i see just the use in your grammar, not the define


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XTEXT Scoping Issue [message #1853525 is a reply to message #1853524] Wed, 06 July 2022 13:58 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
You define Usage as "use" and then an Element of Type Usage gets stored into an element called "def". How is this not a circular definition? What is Usage? To which Terminals can it be resolved too?

I must have some fundamental misunderstanding because this is not making sense to me.

You told me before that def=[Usage]; is short for def=[Usage|ID]; Is ID the terminal here? Then what is Usage? Why do we even need it if we already have the Terminal? I seem to not understand the Scoping Semantics.

Can you tell me where in your Grammar is the definition for Usage?
Re: XTEXT Scoping Issue [message #1853527 is a reply to message #1853525] Wed, 06 July 2022 14:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
sorry. typo
should have been

Usage: "use" def=[Definition];


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XTEXT Scoping Issue [message #1853538 is a reply to message #1853527] Thu, 07 July 2022 09:40 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
Ok, but I still do not really understand what my issue is....

My current grammar is:

grammar org.xtext.EcuCValues_Extend with org.eclipse.xtext.common.Terminals

generate ecuCValues_Extend "http://www.xtext.org/EcuCValues_Extend"

Model:
(lines+=Line)*
;

Line:
(rule = Rule)
;

Rule:
("ForEach" anchor=AnchorValue);

AnchorValue:
(ARClass = TAG_NO_DOT ".class") | (arpath=AbsoluteARPathContainer);

AbsoluteARPathContainer:
"/" path = ARPathContainer_With_Relative
;
ARPathContainer_With_Relative:
path+=(ARPathElement)+;

ARPathElement:
//(move=TAG_NO_DOT_RULE | move_rel=RELATIVE_MOVEMENT) "/"
(name=[TAG_NO_DOT_FUNC|QualifiedName] | move_rel=RELATIVE_MOVEMENT) "/"
;
TAG_NO_DOT_FUNC:
name = TAG_NO_DOT
;
QualifiedName:
ID ('.' ID)*
;
enum RELATIVE_MOVEMENT:
ONEBACK=".." | HERE=".";

terminal TAG_NO_DOT:
('a'..'z' | 'A'..'Z' | '_' | '0'..'9')+
;

For the Model:
ForEach /Comd/CanTpConfig/CanTpChannel/CanTpTxNSdu

it complains "required (...)+ loop did not match anything at input 'Comd'". Why does it say this? I have my own AbstractEcuCValues_ExtendScopeProvider. Everything else I currently use the default.

The Objects that I feed the ScopeProvider look like this:
public class Scope_Object implements EObject, IEObjectDescription {

public QualifiedName QualifiedName;
public Object obj;

public Scope_Object(final String name, final EObject eo) {
setName(name);
setObject(eo);
}
...}
Re: XTEXT Scoping Issue [message #1853542 is a reply to message #1853538] Thu, 07 July 2022 12:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
in this grammar TAG_NO_DOT_FUNC is still uncalled
=> you miss the definitions+=Definition* from my grammar.

also the scope_ methods only work if your scope provider inherits from AbstractDeclarativeScopeProvider
by default you need to override getScope(EObject,EReference)

the Comd does not match ID (from QualifiedName), it matches TAG_NO_DOT


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Thu, 07 July 2022 12:03]

Report message to a moderator

Re: XTEXT Scoping Issue [message #1853547 is a reply to message #1853542] Thu, 07 July 2022 13:17 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
>also the scope_ methods only work if your scope provider inherits from AbstractDeclarativeScopeProvider
by default you need to override getScope(EObject,EReference)

This part works. I do get the context menu with ctrl space. And it shows me the scopes that I filled my IScope with.

>in this grammar TAG_NO_DOT_FUNC is still uncalled
=> you miss the definitions+=Definition* from my grammar.

I am unable to understand this. Could you add this definitions into my example grammar so that I can see what you mean? I think that would be the most helpful.
Re: XTEXT Scoping Issue [message #1853549 is a reply to message #1853547] Thu, 07 July 2022 13:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Model:
(stuff+=TAG_NO_DOT_FUNC)*
(lines+=Line)*
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XTEXT Scoping Issue [message #1853552 is a reply to message #1853549] Thu, 07 July 2022 14:53 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
I did add this entry to my Model:
grammar org.xtext.EcuCValues_Extend with org.eclipse.xtext.common.Terminals

generate ecuCValues_Extend "http://www.xtext.org/EcuCValues_Extend"

Model:
(stuff+=TAG_NO_DOT_FUNC)*
(lines+=Line)*
;

Line:
(rule = Rule)
;

Rule:
("ForEach" anchor=AnchorValue);

AnchorValue:
(ARClass = TAG_NO_DOT ".class") | (arpath=AbsoluteARPathContainer);

AbsoluteARPathContainer:
"/" path = ARPathContainer_With_Relative
;
ARPathContainer_With_Relative:
path+=(ARPathElement)+;

ARPathElement:
(name=[TAG_NO_DOT_FUNC|QualifiedName] | move_rel=RELATIVE_MOVEMENT) "/"
;
TAG_NO_DOT_FUNC:
name = ID
;
QualifiedName:
ID('.' ID)*
;

enum RELATIVE_MOVEMENT:
ONEBACK=".." | HERE=".";

terminal TAG_NO_DOT:
('a'..'z' | 'A'..'Z' | '_' | '0'..'9')+
;

I still get "required (...)+ loop did not match anything at input 'Com'".

for

ForEach /Com/CanTpConfig/CanTpChannel/CanTpTxNSdu/

[Updated on: Thu, 07 July 2022 15:04]

Report message to a moderator

Re: XTEXT Scoping Issue [message #1853557 is a reply to message #1853552] Thu, 07 July 2022 15:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
as said:

QualifiedName:
ID('.' ID)*
;

ID conflicts with TAG_NO_DOT

so Com will be lexed as TAG_NO_DOT and not as ID so no QualifiedName will be found


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XTEXT Scoping Issue [message #1853567 is a reply to message #1853557] Fri, 08 July 2022 04:34 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
So how do I tell to use the String for both TAG_NO_DOT_FUNC and QualifiedName? Do I have to solve this in the code, if yes in which part of the code?
Re: XTEXT Scoping Issue [message #1853568 is a reply to message #1853567] Fri, 08 July 2022 05:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
i dont know the semantics of you language

so it could be


QualifiedName:
TAG_NO_DOT ('.' TAG_NO_DOT )*
;



or

ARPathElement:
(name=[TAG_NO_DOT |QualifiedName] | move_rel=RELATIVE_MOVEMENT) "/"
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XTEXT Scoping Issue [message #1853609 is a reply to message #1853568] Mon, 11 July 2022 07:41 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
Hello Christian,

I am not progressing much with my issue. I am reading a lot about Scoping in XTEXT in parallel to better understand. It seems to be the most difficult aspect of XTEXT and my knowledge is still too fuzzy.

Could you please help me once more?

Here is my Grammar:

grammar org.xtext.EcuCValues_Extend with org.eclipse.xtext.common.Terminals

generate ecuCValues_Extend "http://www.xtext.org/EcuCValues_Extend"

Model:
(tagnodots+=TAG_NO_DOT_FUNC)*
(lines+=Line)*
;

Line:
(rule = Rule)
;

Rule:
("ForEach" anchor=AnchorValue);

AnchorValue:
(ARClass = TAG_NO_DOT ".class") | (arpath=AbsoluteARPathContainer);

AbsoluteARPathContainer:
"/" path = ARPathContainer_With_Relative
;
ARPathContainer_With_Relative:
name=(ARPathElement);

ARPathElement:
name=[TAG_NO_DOT_FUNC|QualifiedName] "/"
;
TAG_NO_DOT_FUNC:
name = ID
;
QualifiedName:
ID('.'ID)*
;

enum RELATIVE_MOVEMENT:
ONEBACK=".." | HERE=".";

terminal TAG_NO_DOT:
('a'..'z' | 'A'..'Z' | '_' | '0'..'9')+
;

I tried to follow your advice so far but its not helping. For Model:

ForEach /CanTpdsConfig/CanTpChannel/CanTpTxNSdu/

I get the Error:

Couldn't resolve reference to TAG_NO_DOT_FUNC 'canTpdsConfig'.

I am trying to Debug the Parser Code. I can see where it goes wrong but I can not understand what goes wrong because the Parser code is not that "human readable".

I can see that he gets a eRecognitionException in function:

// InternalEcuCValues_Extend.g:422:1: ruleQualifiedName returns [AntlrDatatypeRuleToken current=new AntlrDatatypeRuleToken()] : (this_ID_0= RULE_ID (kw= '.' this_ID_2= RULE_ID )* ) ;
public final AntlrDatatypeRuleToken ruleQualifiedName() throws RecognitionException {
AntlrDatatypeRuleToken current = new AntlrDatatypeRuleToken();

Token this_ID_0=null;
Token kw=null;
Token this_ID_2=null;


enterRule();

try {
// InternalEcuCValues_Extend.g:428:2: ( (this_ID_0= RULE_ID (kw= '.' this_ID_2= RULE_ID )* ) )
// InternalEcuCValues_Extend.g:429:2: (this_ID_0= RULE_ID (kw= '.' this_ID_2= RULE_ID )* )
{
// InternalEcuCValues_Extend.g:429:2: (this_ID_0= RULE_ID (kw= '.' this_ID_2= RULE_ID )* )
// InternalEcuCValues_Extend.g:430:3: this_ID_0= RULE_ID (kw= '.' this_ID_2= RULE_ID )*
{
this_ID_0=(Token)match(input,RULE_ID,FOLLOW_9);

current.merge(this_ID_0);


newLeafNode(this_ID_0, grammarAccess.getQualifiedNameAccess().getIDTerminalRuleCall_0());

// InternalEcuCValues_Extend.g:437:3: (kw= '.' this_ID_2= RULE_ID )*
loop4:
do {
int alt4=2;
int LA4_0 = input.LA(1);

if ( (LA4_0==15) ) {
alt4=1;
}


switch (alt4) {
case 1 :
// InternalEcuCValues_Extend.g:438:4: kw= '.' this_ID_2= RULE_ID
{
kw=(Token)match(input,15,FOLLOW_7);

current.merge(kw);
newLeafNode(kw, grammarAccess.getQualifiedNameAccess().getFullStopKeyword_1_0());

this_ID_2=(Token)match(input,RULE_ID,FOLLOW_9);

current.merge(this_ID_2);


newLeafNode(this_ID_2, grammarAccess.getQualifiedNameAccess().getIDTerminalRuleCall_1_1());


}
break;

default :
break loop4;
}
} while (true);


}


}


leaveRule();

}

catch (RecognitionException re) {
recover(input,re);
appendSkippedTokens();
}
finally {
}
return current;
}
// $ANTLR end "ruleQualifiedName"

in line: this_ID_0=(Token)match(input,RULE_ID,FOLLOW_9);

Is my problem in my Code or is it in my grammar? I would prefer to fix it in the grammar as there is less room for error here..

Re: XTEXT Scoping Issue [message #1853610 is a reply to message #1853609] Mon, 11 July 2022 07:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
where do you have canTpdsConfig defined
your snippet shows only the use, not the define

// define it
CanTpdsConfig
// use it
ForEach /CanTpdsConfig/CanTpChannel/CanTpTxNSdu/

i also dont understand why you always switch between parsing problems and scoping problems.
which one are we looking at now


also again

terminal TAG_NO_DOT:
('a'..'z' | 'A'..'Z' | '_' | '0'..'9')+
;

and the ID conflict
and the lexer is context free

so you have to either drop TAG_NO_DOT and use ID everywhere
or dont use ID at all


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XTEXT Scoping Issue [message #1853616 is a reply to message #1853610] Mon, 11 July 2022 13:45 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
Hi Christian, thank you for your patience with me.

>where do you have canTpdsConfig defined
>your snippet shows only the use, not the define

I do not want it to be defined. My goal is for my my getscope implementation to fetch me a list of entries for my context menu for ctrl+space. This I have already successfully implemented. My Problem is that the result that is fetched is not recognized as valid by the Xtext parser. The Canditates that I provide in my IScope shall not be defined previously!

I have replaced ID with TAG_NO_DOT everywhere.

grammar org.xtext.EcuCValues_Extend with org.eclipse.xtext.common.Terminals

generate ecuCValues_Extend "http://www.xtext.org/EcuCValues_Extend"

Model:
(tagnodots+=TAG_NO_DOT_FUNC)*
(lines+=Line)*
;

Line:
(rule = Rule)
;

Rule:
("ForEach" anchor=AnchorValue);

AnchorValue:
(ARClass = TAG_NO_DOT ".class") | (arpath=AbsoluteARPathContainer);

AbsoluteARPathContainer:
"/" path = ARPathContainer_With_Relative
;
ARPathContainer_With_Relative:
name=(ARPathElement);

ARPathElement:
name=[TAG_NO_DOT_FUNC|QualifiedName] "/"
;
TAG_NO_DOT_FUNC:
name = TAG_NO_DOT
;
QualifiedName:
TAG_NO_DOT('.'TAG_NO_DOT)*
;

enum RELATIVE_MOVEMENT:
ONEBACK=".." | HERE=".";

terminal TAG_NO_DOT:
('a'..'z' | 'A'..'Z' | '_' | '0'..'9')+
;

This doesn't change the Error I am getting from the Editor:

Couldn't resolve reference to TAG_NO_DOT_FUNC 'cdsanTpdsConfig'.

How can I tell my grammar to accept the Strings that my IScope fetches?

[Updated on: Mon, 11 July 2022 13:49]

Report message to a moderator

Re: XTEXT Scoping Issue [message #1853622 is a reply to message #1853616] Mon, 11 July 2022 17:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
As I Said. Cannot resolve reference is a scoping problem not a parse problem If you have a reference
Xxxx=[Yyyyyy] then the Yyyyyy needs to be declared somewhere else

If you just want to do content assist and not declare really reference something

Can you give some details what you do in scoping


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XTEXT Scoping Issue [message #1853623 is a reply to message #1853622] Mon, 11 July 2022 17:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
If you don't want to have references but just strings you might do just create a child object

ARPathElement:
name=QualifiedName "/"
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XTEXT Scoping Issue [message #1853636 is a reply to message #1853623] Tue, 12 July 2022 05:14 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
>Can you give some details what you do in scoping

yes, of course. I am writing a DSL for navigating AUTOSAR models. The content assist I want to enable is for helping the user to reference objects in the AUTOSAR model and not in the XTEXT model. Therefore I do not want to previously declare the objects in the XTEXT model.

>If you don't want to have references but just strings you might do just create a child >object

>ARPathElement:
>name=QualifiedName "/"
>;

I have started like this and it works but I want to offer my user content assist and autocomplete. From my reading this is only possible with scoping.

How can I over valid Qualified names upon ctrl + space without scoping?
Re: XTEXT Scoping Issue [message #1853640 is a reply to message #1853636] Tue, 12 July 2022 07:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
you cannot. but if you want to have references into autosar. then you need to load a reference into autosar resources into scoping.
e.g. the ecucvalues dsl part of artop does that.
so the question again would be: what did you do in scoping?
did you debug DefaultLinkingServices why scoping fails?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XTEXT Scoping Issue [message #1853645 is a reply to message #1853640] Tue, 12 July 2022 09:47 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
>then you need to load a reference into autosar resources into scoping

did you mean that I need to load the AUTOSAR resources into my scoping? I did load the AUTOSAR objects into my IScope. I created a wrapper class Scope_Object for loading the AR Objects into my IScope. I looks like this:

/*
* Copyright (c) Robert Bosch GmbH. All rights reserved.
*/
package org.xtext.scoping;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;


/**
* @author AMK2ABT
*/
public class Scope_Object implements EObject, IEObjectDescription {

public QualifiedName QualifiedName;
public Object obj;

public Scope_Object(final String name, final EObject eo) {
setName(name);
setObject(eo);
}

/**
* {@inheritDoc}
*/
@Override
public EList<Adapter> eAdapters() {
EObject eo = (EObject) this.obj;
return eo.eAdapters();
}

/**
* {@inheritDoc}
*/
@Override
public boolean eDeliver() {
EObject eo = (EObject) this.obj;
return eo.eDeliver();
}

/**
* {@inheritDoc}
*/
@Override
public void eSetDeliver(final boolean deliver) {
EObject eo = (EObject) this.obj;
eo.eSetDeliver(deliver);
}

/**
* {@inheritDoc}
*/
@Override
public void eNotify(final Notification notification) {
EObject eo = (EObject) this.obj;
eo.eNotify(notification);
}

public void setName(final String name) {
this.QualifiedName = org.eclipse.xtext.naming.QualifiedName.create(name);
return;
}

/**
* {@inheritDoc}
*/
public void setQualifiedName(final String name) {
this.QualifiedName = org.eclipse.xtext.naming.QualifiedName.create(name);
return;
}

public void setObject(final EObject eo) {
this.obj = eo;
}

public Object getObject() {
return this.obj;
}

/**
* {@inheritDoc}
*/
@Override
public QualifiedName getName() {

return this.QualifiedName;
}

/**
* {@inheritDoc}
*/
@Override
public QualifiedName getQualifiedName() {

return this.QualifiedName;
}

/**
* {@inheritDoc}
*/
@Override
public EObject getEObjectOrProxy() {

return (EObject) this.obj;
}

/**
* {@inheritDoc}
*/
@Override
public URI getEObjectURI() {
EObject eo = (EObject) this.obj;
return EcoreUtil.getURI(eo);
}

/**
* {@inheritDoc}
*/
@Override
public EClass getEClass() {
EObject eo = (EObject) this.obj;
EClass ec = eo.eClass();
return ec;
}

/**
* {@inheritDoc}
*/
@Override
public String getUserData(final String key) {
String str = "";
for (String string : this.QualifiedName.getSegments()) {
str += string;
}
return str;
}

/**
* {@inheritDoc}
*/
@Override
public String[] getUserDataKeys() {
String[] str_arr = (String[]) this.QualifiedName.getSegments().toArray();

return str_arr;
}

/**
* {@inheritDoc}
*/
@Override
public EClass eClass() {
EObject eo = (EObject) this.obj;
EClass ec = eo.eClass();
return ec;
}

/**
* {@inheritDoc}
*/
@Override
public Resource eResource() {
EObject eo = (EObject) this.obj;
Resource er = eo.eResource();
return er;
}

/**
* {@inheritDoc}
*/
@Override
public EObject eContainer() {
EObject eo = (EObject) this.obj;
EObject ec = eo.eContainer();
return ec;
}

/**
* {@inheritDoc}
*/
@Override
public EStructuralFeature eContainingFeature() {
EObject eo = (EObject) this.obj;
EStructuralFeature esf = eo.eContainingFeature();
return esf;
}

/**
* {@inheritDoc}
*/
@Override
public EReference eContainmentFeature() {
EObject eo = (EObject) this.obj;
EReference er = eo.eContainmentFeature();
return er;
}

/**
* {@inheritDoc}
*/
@Override
public EList<EObject> eContents() {
EObject eo = (EObject) this.obj;
EList<EObject> eo_lst = eo.eContents();
return eo_lst;
}

/**
* {@inheritDoc}
*/
@Override
public TreeIterator<EObject> eAllContents() {
EObject eo = (EObject) this.obj;

return null;
}

/**
* {@inheritDoc}
*/
@Override
public boolean eIsProxy() {
EObject eo = (EObject) this.obj;

return eo.eIsProxy();
}

/**
* {@inheritDoc}
*/
@Override
public EList<EObject> eCrossReferences() {
EObject eo = (EObject) this.obj;
return eo.eCrossReferences();
}

/**
* {@inheritDoc}
*/
@Override
public Object eGet(final EStructuralFeature feature) {
EObject eo = (EObject) this.obj;
return eo.eGet(feature);
}

/**
* {@inheritDoc}
*/
@Override
public Object eGet(final EStructuralFeature feature, final boolean resolve) {
EObject eo = (EObject) this.obj;
return eo.eGet(feature, resolve);
}

/**
* {@inheritDoc}
*/
@Override
public void eSet(final EStructuralFeature feature, final Object newValue) {
EObject eo = (EObject) this.obj;
eo.eSet(feature, newValue);
return;
}

/**
* {@inheritDoc}
*/
@Override
public boolean eIsSet(final EStructuralFeature feature) {
EObject eo = (EObject) this.obj;
boolean eIsSet = eo.eIsSet(feature);
return eIsSet;
}

/**
* {@inheritDoc}
*/
@Override
public void eUnset(final EStructuralFeature feature) {
EObject eo = (EObject) this.obj;
eo.eUnset(feature);
}

/**
* {@inheritDoc}
*/
@Override
public Object eInvoke(final EOperation operation, final EList<?> arguments) throws InvocationTargetException {
EObject eo = (EObject) this.obj;
return eo.eInvoke(operation, arguments);
}

}

Then I have written a custom Scope Provider that extends the AbstractDeclarativeScopeProvider:

/*
* generated by Xtext 2.20.0
*/
package org.xtext.scoping;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.Scopes;
import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider;
import org.xtext.ecuCValues_Extend.ARPathContainer_With_Relative;
import org.xtext.ecuCValues_Extend.ARPathElement;
import org.xtext.ecuCValues_Extend.AbsoluteARPathContainer;

import com.bosch.bct.javaaction.modelaccess.artop.IASWModel;

import autosar40.ecucdescription.EcucModuleConfigurationValues;

/**
* This class contains custom scoping description. See
* https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#scoping on how and when to use it.
*/
public class EcuCValues_ExtendScopeProvider extends AbstractDeclarativeScopeProvider {

@Override
public IScope getScope(final EObject context, final EReference reference) {
EcuCValues_ExtendIndex index = new EcuCValues_ExtendIndex();
List<ARPathElement> candidates = new ArrayList<>();
String projectName = null;
// Get the project name from the EObject
projectName = context.eContainer().eResource().getURI().segment(1);
List<Scope_Object> scope_obj_lst = new BasicEList<>();
IScope scopeFor = null;
// Load the Artop project context and if needed the central elements from CEL
if (projectName != null) {
IASWModel artopModelSingleton = ArtopModelSingleton.getArtopModelSingleton(projectName);
if (context instanceof AbsoluteARPathContainer) {
AbsoluteARPathContainer arpc = (AbsoluteARPathContainer) context;
EObject eContainer = context.eContainer();
ARPathContainer_With_Relative path = arpc.getPath();
// IResourceDescription resourceDescription_1 = index.getResourceDescription(arpc);
// Iterable<IEObjectDescription> exportedEObjectDescriptions_1 = index.getExportedEObjectDescriptions(arpc);
if (path != null) {
ARPathElement full_path = path.getName();
}


List<EcucModuleConfigurationValues> ecuCValueModules =
artopModelSingleton.getElements(EcucModuleConfigurationValues.class);

for (EcucModuleConfigurationValues values : ecuCValueModules) {
Scope_Object scope_obj = new Scope_Object(values.getDefinition().getShortName(), values);
scope_obj_lst.add(scope_obj);
}


EObject rootElement = EcoreUtil.getRootContainer(context);
DefaultDeclarativeQualifiedNameProvider qnp = new Scoping_Object_QualifiedName_Provider();
scopeFor = Scopes.scopeFor(scope_obj_lst, qnp, IScope.NULLSCOPE);
}
if (context instanceof ARPathElement) {
ARPathElement arpe = (ARPathElement) context;
List<EcucModuleConfigurationValues> ecuCValueModules =
artopModelSingleton.getElements(EcucModuleConfigurationValues.class);

for (EcucModuleConfigurationValues values : ecuCValueModules) {
Scope_Object scope_obj = new Scope_Object(values.getDefinition().getShortName(), values);
scope_obj_lst.add(scope_obj);
}


EObject rootElement = EcoreUtil.getRootContainer(context);
DefaultDeclarativeQualifiedNameProvider qnp = new Scoping_Object_QualifiedName_Provider();
scopeFor = Scopes.scopeFor(scope_obj_lst, qnp, IScope.NULLSCOPE);
}
else {
List<EcucModuleConfigurationValues> ecuCValueModules =
artopModelSingleton.getElements(EcucModuleConfigurationValues.class);
List<EcucModuleConfigurationValues> ecuCValueModules2 =
artopModelSingleton.getElements(EcucModuleConfigurationValues.class);
for (EcucModuleConfigurationValues values : ecuCValueModules) {
Scope_Object scope_obj = new Scope_Object(values.getDefinition().getShortName(), values);
scope_obj_lst.add(scope_obj);
}


DefaultDeclarativeQualifiedNameProvider qnp = new Scoping_Object_QualifiedName_Provider();
scopeFor = Scopes.scopeFor(scope_obj_lst, qnp, IScope.NULLSCOPE);
}
}
return scopeFor;
}
}

This getScope works in the sense that it is executed and that it is providing me with my context menu entries.


> did you debug DefaultLinkingServices why scoping fails?
I did not but will seek to do so now.

Re: XTEXT Scoping Issue [message #1853646 is a reply to message #1853645] Tue, 12 July 2022 09:54 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
I debugged the class you suggested now I and I can see where the problem is:

public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node) throws IllegalNodeException {
final EClass requiredType = ref.getEReferenceType();
if (requiredType == null) {
return Collections.<EObject>emptyList();
}
final String crossRefString = getCrossRefNodeAsString(node);
if (crossRefString == null || crossRefString.equals("")) {
return Collections.<EObject>emptyList();
}
if (logger.isDebugEnabled()) {
logger.debug("before getLinkedObjects: node: '" + crossRefString + "'");
}
final IScope scope = getScope(context, ref);
if (scope == null) {
throw new AssertionError(
"Scope provider " + scopeProvider.getClass().getName() + " must not return null for context "
+ context + ", reference " + ref + "! Consider to return IScope.NULLSCOPE instead.");
}
final QualifiedName qualifiedLinkName = qualifiedNameConverter.toQualifiedName(crossRefString);
final IEObjectDescription eObjectDescription = scope.getSingleElement(qualifiedLinkName);
if (logger.isDebugEnabled()) {
logger.debug("after getLinkedObjects: node: '" + crossRefString + "' result: " + eObjectDescription);
}
if (eObjectDescription == null) {
return Collections.emptyList();
}
final EObject result = eObjectDescription.getEObjectOrProxy();
return Collections.singletonList(result);
}

In this method the Line:

final IScope scope = getScope(context, ref);

is not calling my getscope as I thought it is supposed to. And then in line

final IEObjectDescription eObjectDescription = scope.getSingleElement(qualifiedLinkName);

I get an exception because the IScope is not proper..
Re: XTEXT Scoping Issue [message #1853649 is a reply to message #1853646] Tue, 12 July 2022 10:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
did you create a new scope provider class or edit the pregenerated one
if you want to use reflective scoping make sure
method name and parameters are correct


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Tue, 12 July 2022 10:59]

Report message to a moderator

Re: XTEXT Scoping Issue [message #1853914 is a reply to message #1853649] Fri, 22 July 2022 11:22 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
Hello Christian, I am a bit further now. What solved the issues for me is to implement some interfaces with my Scope_Object Wrapper. I implemented: TAG_NO_DOT_FUNC, EObject, IEObjectDescription, InternalEObject. Dont know for sure if this is sensible but it seemed to solve the issue.

The Scope_Object class is what I use to fill my IScope in my ScopeProvider.

Now I still have 1 issue. In my custom public IScope getScope(final EObject context, final EReference reference)

function I need to get the context for which I try to get the IScope for. Unfortunatly my context seems to not be fully parsed. If I browse through my context and search for my current TAG_NO_DOT_FUNC, I have the issue that the tag = null in my org.xtext.ecuCValues_Extend.impl.TAG_NO_DOT_FUNCImpl.

TAG_NO_DOT_FUNC:
tag = TAG_NO_DOT
;
terminal TAG_NO_DOT:
('a'..'z' | 'A'..'Z' | '_' | '0'..'9')+
;

I do absolutely not understand why the tag field has not been parsed properly. If I debug the Parser in org.xtext.parser.antlr.internal then he is able to resolve the tags for my model. Can you please help me to find out why in the context of my public IScope getScope(final EObject context, final EReference reference) function I get called with a broken context?



Re: XTEXT Scoping Issue [message #1853915 is a reply to message #1853914] Fri, 22 July 2022 13:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
can you please provide a complete but minimal grammar + a unit test showing your parse problem

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XTEXT Scoping Issue [message #1854558 is a reply to message #1853915] Thu, 25 August 2022 12:38 Go to previous messageGo to next message
Marc Kaiser is currently offline Marc KaiserFriend
Messages: 16
Registered: July 2022
Junior Member
Hello Christian, you are from ITEMIS, right? Could you tell me a contact for requesting official support? Could you provide me an estimate how much a 1 day support for an XTEXT Expert would cost? Thank you.
Re: XTEXT Scoping Issue [message #1854561 is a reply to message #1854558] Thu, 25 August 2022 14:12 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
yes can you write me a mail, i can then forward it to the right person

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Xtext 2.28.0.M3 is out
Next Topic:Xtext 2.28.0 is out
Goto Forum:
  


Current Time: Thu Mar 28 14:06:31 GMT 2024

Powered by FUDForum. Page generated in 0.07014 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top