Whitespace in qualified names [message #1819353] |
Mon, 13 January 2020 16:41 |
Aaron Greenhouse 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 would not create a reference to a declaration of .
The Xtext grammar rule in question is
and it appears in a grammar marked with
(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 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.
|
|
|
|
Powered by
FUDForum. Page generated in 0.03880 seconds