Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Whitespace in qualified names
Whitespace in qualified names [message #1819353] Mon, 13 January 2020 16:41 Go to next message
Aaron Greenhouse is currently offline Aaron GreenhouseFriend
Messages: 1
Registered: January 2020
Junior Member
I ran into an issue recently, and I was wondering a few things:
1. Did address the issue properly
2. Why are things like this?

The problem I had was that when I used white space or comments in the middle of a qualified name, then I ran into problems with name reference resolution. Qualified names that should be identical and cause cross references were instead yielding name lookup errors. That is
a:: b::c ::d
would not create a reference to a declaration of
a::b::c::d
.

The Xtext grammar rule in question is

QPREF:
	ID ('::' ID)?;


and it appears in a grammar marked with
hidden(WS, SL_COMMENT)


(where SL_COMMENT is a single line comment).

I found that the problem was the
org.eclipse.xtext.naming.QualifiedName
objects contained spaces in the name segments. I found that the source of this problem was that
NodeModelUtils.getTokenText()
returns strings that contain spaces, and that this in face quite deliberate:

    /**
     * This method converts a node to text.
     * 
     * Leading and trailing text from hidden tokens (whitespace/comments) is removed. Text from hidden tokens that is
     * surrounded by text from non-hidden tokens is summarized to a single whitespace.
     * 
     * The preferred use case of this method is to convert the {@link ICompositeNode} that has been created for a data
     * type rule to text.
     * 
     * This is also the recommended way to convert a node to text if you want to invoke
     * {@link org.eclipse.xtext.conversion.IValueConverterService#toValue(String, String, INode)}
     * 
     */


Ultimately, I solved this problem creating a
ValueConvertor
for the QPREF rule that strips out the spaces:

	@ValueConverter(rule = "QPREF")
	public IValueConverter<String> QPREF() {
		return new IValueConverter<String>() {
			@Override
			public String toValue(String string, INode node) {
				return removeSpacesFromQualifiedName(string);
			}

			@Override
			public String toString(String value) {
				return value;
			}
		};
	}



So again, my first question is: Did I address this issue correctly or is there something else I should be doing?

Also, more generally, I would like to know, why is this a problem to begin with? At first I thought that maybe my grammar and surrounding helper classes were doing something wrong somewhere and causing this problem. But then I built the SmallJava example from "Implementing Domain-Specific Languages with Xtext and Xtend", and it had the same exact problem.

On the one hand, I can understand that it might be useful to have
NodeModelUtils.getTokenText()
leave indicators behind of where there was whitespace. But on the other hand, I do not understand why qualified name lookup / resolution should be broken by default.



Re: Whitespace in qualified names [message #1819409 is a reply to message #1819353] Tue, 14 January 2020 17:35 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
yes. the value converter is a valid solution. you could customize org.eclipse.xtext.linking.impl.LinkingHelper.getCrossRefNodeAsString(INode, boolean) too

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Search for reference models in project's Maven classpath
Next Topic:Xtext editor backed by XMI resource
Goto Forum:
  


Current Time: Wed Sep 25 00:50:27 GMT 2024

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

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

Back to the top